Skip to content

Commit 5ef1e70

Browse files
committed
fix: restore runnerRoundTrips agent-cost field dropped in #968
PR #968 (apple-platform-consolidation) accidentally dropped the runnerRoundTrips field from the agent-cost block during an over-broad conflict resolution. This restores the shipped Phase-4 feature: - src/kernel/contracts.ts: re-add ResponseCost.runnerRoundTrips: number - src/utils/diagnostics.ts: restore the countDiagnosticEventsByPhase() accessor over the flush-surviving phaseCounts tally (the tally itself survived #968; only the accessor was removed) - src/daemon/request-router.ts: repopulate runnerRoundTrips in the cost graft by counting the two real round-trip diagnostic phases (ios_runner_command_send + ios_runner_readiness_preflight). The RUNNER_ROUND_TRIP_PHASES constant is now defined locally (its former home, the dev-only runner-request-count.ts, was removed in #968 and is out of scope to restore) - request-router-cost.test.ts: restore the round-trip counting test and the runnerRoundTrips:0 assertion Byte-identical-default invariant preserved: with --cost OFF or on an error response the serialized payload is unchanged.
1 parent 26ac865 commit 5ef1e70

4 files changed

Lines changed: 63 additions & 1 deletion

File tree

‎src/daemon/__tests__/request-router-cost.test.ts‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ vi.mock('../device-ready.ts', () => ({ ensureDeviceReady: vi.fn(async () => {})
1717

1818
import { dispatchCommand } from '../../core/dispatch.ts';
1919
import { createRequestHandler } from '../request-router.ts';
20+
import { emitDiagnostic } from '../../utils/diagnostics.ts';
2021
import type { DaemonRequest, SessionState } from '../types.ts';
2122
import { LeaseRegistry } from '../lease-registry.ts';
2223
import { makeSessionStore } from '../../__tests__/test-utils/store-factory.ts';
@@ -110,9 +111,12 @@ test('(b) flag-on additive-only: cost block is the ONLY delta vs flag-off', asyn
110111
expect(respFlagOn.ok).toBe(true);
111112
if (!respFlagOff.ok || !respFlagOn.ok) return;
112113

114+
// The cost block carries BOTH wallClockMs and runnerRoundTrips, both numbers
115+
// ≥ 0. A request that never touches the iOS runner reports 0 — honest.
113116
const cost = respFlagOn.data?.cost;
114117
expect(cost).toMatchObject({
115118
wallClockMs: expect.any(Number),
119+
runnerRoundTrips: 0,
116120
});
117121
expect(cost?.wallClockMs).toBeGreaterThanOrEqual(0);
118122
// This payload has no node tree, so nodeCount is omitted entirely.
@@ -123,7 +127,30 @@ test('(b) flag-on additive-only: cost block is the ONLY delta vs flag-off', asyn
123127
expect(respFlagOn.data).toEqual(respFlagOff.data);
124128
});
125129

126-
test('(c) nodeCount reports the node-tree size whenever data carries a nodes array, additive-only', async () => {
130+
test('(c) runnerRoundTrips counts real iOS-runner round-trip diagnostics in scope', async () => {
131+
const { sessionStore, handler } = makeHandler();
132+
sessionStore.set('cost-session', makeIosSession('cost-session'));
133+
134+
// The mocked dispatch runs inside the request's diagnostics scope, so emitting
135+
// here is equivalent to the runner-session emitting these phases per round-trip.
136+
mockDispatch.mockImplementation(async () => {
137+
emitDiagnostic({ phase: 'ios_runner_readiness_preflight' }); // real round-trip
138+
emitDiagnostic({ phase: 'ios_runner_command_send' }); // real round-trip
139+
emitDiagnostic({ phase: 'ios_runner_command_send' }); // real round-trip
140+
emitDiagnostic({ level: 'debug', phase: 'ios_runner_readiness_preflight_skipped' }); // NOT
141+
emitDiagnostic({ phase: 'some_other_phase' }); // NOT
142+
return { ...REPRESENTATIVE_PAYLOAD };
143+
});
144+
145+
const resp = await handler(baseRequest({ meta: { includeCost: true } }));
146+
expect(resp.ok).toBe(true);
147+
if (!resp.ok) return;
148+
// 1 preflight + 2 command_send = 3; the _skipped marker and unrelated phases
149+
// are excluded.
150+
expect(resp.data?.cost?.runnerRoundTrips).toBe(3);
151+
});
152+
153+
test('(c2) nodeCount reports the node-tree size whenever data carries a nodes array, additive-only', async () => {
127154
const { sessionStore, handler } = makeHandler();
128155
sessionStore.set('cost-session', makeIosSession('cost-session'));
129156

‎src/daemon/request-router.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
withRequestPlatformProviderScope,
2323
} from './request-platform-providers.ts';
2424
import {
25+
countDiagnosticEventsByPhase,
2526
emitDiagnostic,
2627
flushDiagnosticsToSessionFile,
2728
getDiagnosticsMeta,
@@ -344,12 +345,26 @@ function applyResponseLevelView(
344345
return view ? { ok: true, data: view(response.data ?? {}, level) } : response;
345346
}
346347

348+
// Diagnostic phases emitted once per real iOS-runner round-trip. `..._command_send`
349+
// is the command itself; `..._readiness_preflight` is the pre-command uptime probe
350+
// (a real network round-trip). The `..._skipped` / `..._recovered` markers do NOT
351+
// hit the runner and are intentionally excluded.
352+
const RUNNER_ROUND_TRIP_PHASES = [
353+
'ios_runner_command_send',
354+
'ios_runner_readiness_preflight',
355+
] as const;
356+
347357
function buildResponseCost(
348358
originalData: DaemonResponseData | undefined,
349359
startedAt: number,
350360
): ResponseCost {
351361
const cost: ResponseCost = {
352362
wallClockMs: Date.now() - startedAt,
363+
// Counts this request's real runner round-trips from the flush-surviving
364+
// diagnostics phase tally. Reads 0 when no runner was hit (e.g. a no-op or a
365+
// command served entirely from the daemon). Must run inside the request's
366+
// diagnostics scope (see `applyAgentCostGrafts` call site).
367+
runnerRoundTrips: countDiagnosticEventsByPhase(RUNNER_ROUND_TRIP_PHASES),
353368
};
354369
// nodeCount reads the ORIGINAL node tree (the digest view may have already
355370
// collapsed `data.nodes`), so the count stays accurate.

‎src/kernel/contracts.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ export type DaemonArtifact = {
120120

121121
export type ResponseCost = {
122122
wallClockMs: number;
123+
// Number of real iOS-runner round-trips made while serving the request (the
124+
// `ios_runner_command_send` + `ios_runner_readiness_preflight` diagnostic
125+
// phases). Always present when cost is included; 0 when no runner was hit.
126+
runnerRoundTrips: number;
123127
// Number of UI/accessibility nodes in the response, when the command returns a
124128
// node tree (e.g. snapshot). Absent for commands that produce no nodes, so an
125129
// agent can size a snapshot before re-fetching at a different depth/scope.

‎src/utils/diagnostics.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,22 @@ export function getDiagnosticsMeta(): {
8686
};
8787
}
8888

89+
/**
90+
* Sum the number of diagnostic events emitted in the current scope whose phase
91+
* is one of `phases`. Backed by the flush-surviving `phaseCounts` tally, so it
92+
* stays accurate for the whole request even under `--debug` (where `events` is
93+
* streamed out and reset). Returns 0 when called outside a diagnostics scope.
94+
*/
95+
export function countDiagnosticEventsByPhase(phases: readonly string[]): number {
96+
const scope = diagnosticsStorage.getStore();
97+
if (!scope) return 0;
98+
let total = 0;
99+
for (const phase of phases) {
100+
total += scope.phaseCounts.get(phase) ?? 0;
101+
}
102+
return total;
103+
}
104+
89105
export function emitDiagnostic(event: {
90106
level?: DiagnosticLevel;
91107
phase: string;

0 commit comments

Comments
 (0)