Skip to content

Commit 2516812

Browse files
committed
fix(agent): key the rtk savings counter to the task and drop the pct field
counter_id was the run_id, but a warm snapshot-resume restarts the sandbox with its filesystem — and rtk tally — intact under a new run_id, so a resumed run's reading re-counted the prior run's savings under a fresh counter. The task tracks the database's lifetime; a cold resume reads a dropped value, which the reset rule already handles. Also make the zero-baseline rule explicit in the consumption contract (a single reading counts in full) and stop emitting avg_savings_pct — an average cannot be differenced; consumers derive the ratio from the two cumulative token columns. Generated-By: PostHog Code Task-Id: b86391cc-4634-4a2b-ae34-fe1f9c0626a0
1 parent 5fe748e commit 2516812

4 files changed

Lines changed: 21 additions & 18 deletions

File tree

packages/agent/src/server/agent-server.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,6 @@ describe("AgentServer HTTP Mode", () => {
509509
inputTokens: 1000,
510510
outputTokens: 350,
511511
tokensSaved: 650,
512-
avgSavingsPct: 65,
513512
}),
514513
}),
515514
);
@@ -534,8 +533,12 @@ describe("AgentServer HTTP Mode", () => {
534533
task_id: "test-task-id",
535534
run_id: "test-run-id",
536535
team_id: 1,
537-
counter_id: "test-run-id",
536+
// The counter follows the task: a warm snapshot-resume keeps
537+
// the sandbox's rtk tally across run_ids.
538+
counter_id: "test-task-id",
538539
cumulative_commands: 4,
540+
cumulative_input_tokens: 1000,
541+
cumulative_output_tokens: 350,
539542
cumulative_tokens_saved: 650,
540543
}),
541544
}),
@@ -559,7 +562,6 @@ describe("AgentServer HTTP Mode", () => {
559562
inputTokens: 100,
560563
outputTokens: 50,
561564
tokensSaved: 30,
562-
avgSavingsPct: 30,
563565
}),
564566
}) as unknown as {
565567
eventStreamSender: {

packages/agent/src/server/agent-server.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3387,13 +3387,16 @@ ${signedCommitInstructions}
33873387
*
33883388
* Gauge semantics: the `cumulative_*` properties are counter READS of the
33893389
* host's machine-global rtk tally, not per-run deltas. Consumers must group
3390-
* by `counter_id` and difference readings (argMax - argMin per window,
3391-
* treating a drop as a counter reset à la `rate()` — `rtk gain --reset`
3392-
* exists), never sum event values. This makes the numbers correct even when
3393-
* several sessions share one rtk database: concurrent reads of the shared
3394-
* counter dedupe under max() instead of double-counting. In the ephemeral
3395-
* cloud sandbox the counter starts at zero, so a single reading is also this
3396-
* run's savings.
3390+
* by `counter_id` and difference consecutive readings per counter, with an
3391+
* implicit 0 before a counter's first reading (a single reading therefore
3392+
* counts in full) and a value drop treated as a reset starting a new
3393+
* segment à la `rate()` — `rtk gain --reset` exists. Never sum event
3394+
* values. This makes the numbers correct even when several sessions share
3395+
* one rtk database: concurrent reads of the shared counter dedupe under
3396+
* max() instead of double-counting. A savings ratio is derivable as
3397+
* cumulative_tokens_saved / cumulative_input_tokens; rtk's per-command
3398+
* average is deliberately not emitted since an average cannot be
3399+
* differenced under this contract.
33973400
*
33983401
* Best-effort and cloud-only for now (no-op when the event stream isn't
33993402
* configured; a desktop emit through the analytics pipeline can reuse the
@@ -3426,14 +3429,16 @@ ${signedCommitInstructions}
34263429
run_id: this.config.runId,
34273430
team_id: this.config.projectId,
34283431
// The identity of the rtk database this reading came from — the
3429-
// group-by key for differencing. The cloud sandbox is fresh per
3430-
// run, so the run owns its counter.
3431-
counter_id: this.config.runId,
3432+
// group-by key for differencing. Keyed to the task, not the run:
3433+
// a warm snapshot-resume restarts the sandbox with its filesystem
3434+
// (and rtk tally) intact under a new run_id, so the task tracks
3435+
// the database's lifetime. A cold resume gets a fresh sandbox and
3436+
// reads a dropped value, which the reset rule already handles.
3437+
counter_id: this.config.taskId,
34323438
cumulative_commands: savings.totalCommands,
34333439
cumulative_input_tokens: savings.inputTokens,
34343440
cumulative_output_tokens: savings.outputTokens,
34353441
cumulative_tokens_saved: savings.tokensSaved,
3436-
avg_savings_pct: savings.avgSavingsPct,
34373442
},
34383443
},
34393444
});

packages/agent/src/server/rtk-savings.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ describe("resolveRtkSavings", () => {
3232
inputTokens: 502691,
3333
outputTokens: 5835,
3434
tokensSaved: 496856,
35-
avgSavingsPct: 98.8392471717218,
3635
});
3736
});
3837

@@ -93,7 +92,6 @@ describe("resolveRtkSavings", () => {
9392
inputTokens: 0,
9493
outputTokens: 0,
9594
tokensSaved: 0,
96-
avgSavingsPct: 0,
9795
});
9896
});
9997
});

packages/agent/src/server/rtk-savings.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export interface RtkSavingsSummary {
1515
inputTokens: number;
1616
outputTokens: number;
1717
tokensSaved: number;
18-
avgSavingsPct: number;
1918
}
2019

2120
interface ResolveRtkSavingsOptions {
@@ -41,7 +40,6 @@ function parseGainSummary(stdout: string): RtkSavingsSummary | null {
4140
inputTokens: toFiniteNumber(summary.total_input),
4241
outputTokens: toFiniteNumber(summary.total_output),
4342
tokensSaved: toFiniteNumber(summary.total_saved),
44-
avgSavingsPct: toFiniteNumber(summary.avg_savings_pct),
4543
};
4644
}
4745

0 commit comments

Comments
 (0)