Skip to content

Commit 15ca66f

Browse files
fix(copilot): seq migration (#4804)
1 parent c51c41f commit 15ca66f

3 files changed

Lines changed: 56 additions & 2 deletions

File tree

apps/sim/lib/logs/execution/logging-factory.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,4 +658,58 @@ describe('calculateCostSummary', () => {
658658
expect(result.models['gpt-4o'].total).toBe(0.03)
659659
expect(result.totalCost).toBeCloseTo(0.03 + BASE_EXECUTION_CHARGE, 10)
660660
})
661+
662+
test('does not double-count deeply nested (3-level) sub-workflow roots', () => {
663+
// A → B → C: each level is its own synthetic { type: 'workflow' } root with
664+
// an aggregate cost. Only the leaf agents at the bottom must be billed, once.
665+
const leafAgent = (id: string, model: string, total: number) => ({
666+
id,
667+
name: 'Agent',
668+
type: 'agent',
669+
model,
670+
cost: { input: total / 3, output: (total * 2) / 3, total },
671+
tokens: { input: 100, output: 200, total: 300 },
672+
})
673+
674+
const traceSpans = [
675+
{
676+
id: 'root',
677+
name: 'Workflow Execution',
678+
type: 'workflow',
679+
cost: { total: 0.06 }, // aggregate of everything below
680+
children: [
681+
leafAgent('parent-agent', 'gpt-4o', 0.02),
682+
{
683+
id: 'sub-a-root',
684+
name: 'Workflow Execution',
685+
type: 'workflow',
686+
cost: { total: 0.04 },
687+
children: [
688+
leafAgent('a-agent', 'gpt-4o', 0.01),
689+
{
690+
id: 'sub-b-root',
691+
name: 'Workflow Execution',
692+
type: 'workflow',
693+
cost: { total: 0.03 },
694+
children: [leafAgent('b-agent', 'claude-sonnet-4-6', 0.03)],
695+
},
696+
],
697+
},
698+
],
699+
},
700+
]
701+
702+
const result = calculateCostSummary(traceSpans)
703+
704+
expect(result.charges['Workflow Execution']).toBeUndefined()
705+
// gpt-4o appears at two levels (0.02 + 0.01) and merges by model name.
706+
expect(result.models['gpt-4o'].total).toBeCloseTo(0.03, 10)
707+
expect(result.models['claude-sonnet-4-6'].total).toBeCloseTo(0.03, 10)
708+
expect(result.totalCost).toBeCloseTo(0.06 + BASE_EXECUTION_CHARGE, 10)
709+
const ledgerSum =
710+
result.baseExecutionCharge +
711+
Object.values(result.models).reduce((s, m) => s + m.total, 0) +
712+
Object.values(result.charges).reduce((s, c) => s + c.total, 0)
713+
expect(ledgerSum).toBeCloseTo(result.totalCost, 10)
714+
})
661715
})

packages/db/.env.swp

-12 KB
Binary file not shown.

packages/db/migrations/0219_amused_leo.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ALTER TABLE "copilot_messages" ADD COLUMN "seq" integer;--> statement-breakpoint
1+
ALTER TABLE "copilot_messages" ADD COLUMN IF NOT EXISTS "seq" integer;--> statement-breakpoint
22
WITH ordered AS (
33
SELECT c."id" AS chat_id, elem.value->>'id' AS message_id, elem.ord AS ord
44
FROM "copilot_chats" c
@@ -16,4 +16,4 @@ ranked AS (
1616
UPDATE "copilot_messages" m SET "seq" = r.seq
1717
FROM ranked r
1818
WHERE m."chat_id" = r.chat_id AND m."message_id" = r.message_id;--> statement-breakpoint
19-
CREATE INDEX "copilot_messages_chat_seq_idx" ON "copilot_messages" USING btree ("chat_id","seq") WHERE "copilot_messages"."deleted_at" IS NULL;
19+
CREATE INDEX IF NOT EXISTS "copilot_messages_chat_seq_idx" ON "copilot_messages" USING btree ("chat_id","seq") WHERE "copilot_messages"."deleted_at" IS NULL;

0 commit comments

Comments
 (0)