Skip to content

Commit fc96631

Browse files
committed
fix: run work-metrics update on every transform pass
The initial work-metrics integration (commit fbfbc5a) gated the computeOpenCodeWorkMetrics + setSessionWorkMetrics call on workExecutedSuccessfully (OpenCode) and result.executedWorkThisPass (Pi). That gate is overly conservative: the work-metrics update is a pure DB read on the OpenCode message table plus a pure DB write to session_meta. It does not mutate any tag state, does not rewrite message[0], and is cache-stable. Net effect of the bug: sessions sitting below the execute threshold (default 65%) never see populated new_work_tokens / total_input_tokens columns, so the TUI sidebar Stats section shows 0/0/0 for any session that hasn't crossed threshold yet. For long-running but low-pressure work this is essentially permanent. Fix moves the work-metrics update out of the executedWorkSuccessfully branch and runs it on every transform pass. Both OpenCode and Pi paths updated symmetrically. Verified: - packages/plugin: typecheck clean, work-metrics.test.ts 6/6 pass (including live AFT smoke target) - packages/pi-plugin: typecheck clean, work-metrics-pi.test.ts 1/1 pass - Both dist bundles rebuilt
1 parent 3e4a147 commit fc96631

2 files changed

Lines changed: 40 additions & 29 deletions

File tree

packages/pi-plugin/src/context-handler.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1827,21 +1827,26 @@ export function registerPiContextHandler(
18271827
);
18281828
}
18291829

1830-
if (result.executedWorkThisPass) {
1831-
try {
1832-
const metrics = computePiWorkMetrics(outputMessages as unknown[]);
1833-
setSessionWorkMetrics(
1834-
options.db,
1835-
sessionId,
1836-
metrics.newWorkTokens,
1837-
metrics.totalInputTokens,
1838-
);
1839-
} catch (err) {
1840-
sessionLog(
1841-
sessionId,
1842-
`work-metrics update failed: ${err instanceof Error ? err.message : String(err)}`,
1843-
);
1844-
}
1830+
// Work-metrics update runs on EVERY transform pass (not just
1831+
// execute passes). The Pi compute helper is pure-read on
1832+
// outputMessages; setSessionWorkMetrics is a pure write to
1833+
// session_meta (no tag state, no message[0] mutation, no
1834+
// cache-busting). Gating on executedWorkThisPass would mean
1835+
// sessions sitting below execute threshold never see populated
1836+
// values, making Pi's status surface permanently zero.
1837+
try {
1838+
const metrics = computePiWorkMetrics(outputMessages as unknown[]);
1839+
setSessionWorkMetrics(
1840+
options.db,
1841+
sessionId,
1842+
metrics.newWorkTokens,
1843+
metrics.totalInputTokens,
1844+
);
1845+
} catch (err) {
1846+
sessionLog(
1847+
sessionId,
1848+
`work-metrics update failed: ${err instanceof Error ? err.message : String(err)}`,
1849+
);
18451850
}
18461851

18471852
logTransformTiming(sessionId, "postTransformPhase", tPostTransform);

packages/plugin/src/hooks/magic-context/transform-postprocess-phase.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -960,21 +960,27 @@ export async function runPostTransformPhase(
960960
heuristicsRanSuccessfully ||
961961
pendingOpsRanSuccessfully;
962962

963-
if (workExecutedSuccessfully) {
964-
try {
965-
const metrics = withReadOnlySessionDb((openCodeDb) =>
966-
computeOpenCodeWorkMetrics(openCodeDb, args.sessionId),
967-
);
968-
setSessionWorkMetrics(
969-
args.db,
970-
args.sessionId,
971-
metrics.newWorkTokens,
972-
metrics.totalInputTokens,
973-
);
974-
} catch (err) {
975-
sessionLog(args.sessionId, "work-metrics update failed:", getErrorMessage(err));
976-
}
963+
// Work-metrics update runs on EVERY transform pass (not just execute passes).
964+
// The SQL helper is pure-read on OpenCode's message table; setSessionWorkMetrics
965+
// is a pure write to session_meta (no tag state, no message[0] mutation, no
966+
// cache-busting). Gating on workExecutedSuccessfully would mean sessions
967+
// sitting below execute threshold never see populated values, making the
968+
// TUI Stats section permanently zero for low-pressure work.
969+
try {
970+
const metrics = withReadOnlySessionDb((openCodeDb) =>
971+
computeOpenCodeWorkMetrics(openCodeDb, args.sessionId),
972+
);
973+
setSessionWorkMetrics(
974+
args.db,
975+
args.sessionId,
976+
metrics.newWorkTokens,
977+
metrics.totalInputTokens,
978+
);
979+
} catch (err) {
980+
sessionLog(args.sessionId, "work-metrics update failed:", getErrorMessage(err));
981+
}
977982

983+
if (workExecutedSuccessfully) {
978984
try {
979985
const currentFlag = peekDeferredExecutePending(args.db, args.sessionId);
980986
if (currentFlag !== null) {

0 commit comments

Comments
 (0)