Skip to content

Commit cee1fec

Browse files
committed
fix(record): arm recording through the publication lifecycle, not around it
`recordSession` is an evidence-capture flag, but three surfaces set it directly without consulting the publication aggregate, so it could contradict a terminal ABORTED authoring status. The #1533 fix closed the recorded-action ingress and made the writer refuse an ABORTED lifecycle, then documented the remaining contradiction as acceptable — the writer's own comment noted that "something can re-arm that boolean behind the terminal status". That something was live: `buildNextOpenSession` re-armed recording for any `open --save-script`, and `applyOrdinaryScriptRecordingOpenOutcome` only aborts a lifecycle that is still ARMED. A third `open --save-script` on an already-ABORTED session therefore left `recordSession` true behind the terminal status. The writer gate hid the publication symptom, but the session kept paying recording-time costs for a recording that can never publish: `recordSession` disables the direct iOS selector fast paths for click and get, forcing every interaction onto the snapshot route. Route the flag through one rule owned by the publication projection (`recordSessionAfterSaveScriptFlag`), which answers "not recording" for an ABORTED lifecycle on every surface that handles it — the re-open builder, the close finalizer, and the recorded-action ingress. The writer's gate is unchanged and still correct; it now stands on the aggregate alone rather than as a net under a known drift, so the comments defending the drift are replaced by statements of the rule. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01PFW9gJqz1wEHoowkdd1nFW
1 parent f24f472 commit cee1fec

9 files changed

Lines changed: 114 additions & 39 deletions

‎docs/adr/0016-active-session-script-publication.md‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,23 @@ fresh session is the only re-arming boundary.
113113
114114
> **Amendment (#1533, shipped).** ABORTED terminality above was enforced only by
115115
> `abortAuthoringOnSecondOpen` clearing `session.recordSession` — inert by ordering, not by
116-
> construction. When the second `open` carried `--save-script` itself, the recorded action's shared
117-
> flag ingress re-armed that boolean while the status stayed ABORTED, and a *bare* `close` then
116+
> construction. `recordSession` is an evidence-capture flag that several surfaces set directly, so
117+
> a later `--save-script` re-armed it while the status stayed ABORTED, and a *bare* `close` then
118118
> published the full session log through a writer that only knew how to refuse repair
119119
> transactions. The refusal this ADR specifies for `close --save-script` in ABORTED promises the
120120
> caller that plain `close` "tears down without writing", so the gap also made an existing error
121121
> message untrue.
122122
>
123-
> ABORTED is now terminal by construction on both boundaries: the `--save-script` ingress takes no
124-
> branch for an ABORTED lifecycle — it neither re-arms recording nor retargets the output path —
125-
> and the script writer asks one publication-blocked question covering not-recording, an
126-
> uncommittable or committed repair, and an ABORTED authoring lifecycle alike, so every path that
127-
> reaches it (bare `close`, teardown, idle-reap, active publication) refuses. ARMED and PUBLISHED
128-
> lifecycles and every repair transaction are unchanged; no state name, transition, or entry point
129-
> in the lifecycle above is added or altered.
123+
> ABORTED is now terminal by construction. `--save-script` arms recording through one rule owned by
124+
> the publication projection (`recordSessionAfterSaveScriptFlag`), which answers "not recording"
125+
> for an ABORTED lifecycle on every surface that handles the flag — the re-open builder, the close
126+
> finalizer, and the recorded-action ingress — so the flag can no longer contradict the status.
127+
> Publication authorization is likewise the aggregate's to answer: the writer asks one
128+
> publication-blocked question covering not-recording, an uncommittable or committed repair, and an
129+
> ABORTED authoring lifecycle alike, so every path that reaches it (bare `close`, teardown,
130+
> idle-reap, active publication) refuses. ARMED and PUBLISHED lifecycles and every repair
131+
> transaction are unchanged; no state name, transition, or entry point in the lifecycle above is
132+
> added or altered.
130133
131134
This lifecycle is distinct from ADR 0012's repair transaction. `session save-script` rejects a session
132135
with `saveScriptBoundary` set and directs the caller to finish or abort the repair through its existing

‎src/daemon/__tests__/session-script-writer.test.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -681,10 +681,11 @@ test('write() publishes atomically: no stray temp file survives a successful rep
681681
expect(fs.readFileSync(outPath, 'utf8')).toContain(HEAL_COMPLETE_SENTINEL);
682682
});
683683

684-
// --- #1533: an ABORTED ordinary authoring recording publishes nothing, even
685-
// once something re-arms `recordSession` behind the terminal status ---
684+
// --- #1533: an ABORTED ordinary authoring recording publishes nothing, and the
685+
// lifecycle alone decides that — a session handed to the writer still recording
686+
// is refused on the strength of its terminal status ---
686687

687-
test('#1533: write() publishes nothing for an aborted authoring lifecycle whose recordSession was re-armed', () => {
688+
test('#1533: write() publishes nothing for an aborted authoring lifecycle that is still recording', () => {
688689
const root = mkdtempForTestSync('agent-device-script-writer-authoring-aborted-');
689690
const sessionsDir = path.join(root, 'sessions');
690691
const writer = new SessionScriptWriter(sessionsDir);

‎src/daemon/handlers/__tests__/session-open-surface.test.ts‎

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import assert from 'node:assert/strict';
22
import { test } from 'vitest';
33
import { AppError } from '@agent-device/kernel/errors';
4-
import { buildOpenResult, resolveRequestedOpenSurface } from '../session-open-surface.ts';
4+
import {
5+
buildNextOpenSession,
6+
buildOpenResult,
7+
resolveRequestedOpenSurface,
8+
} from '../session-open-surface.ts';
9+
import {
10+
authoringPublication,
11+
makeIosSession,
12+
} from '../../../__tests__/test-utils/session-factories.ts';
13+
import { IOS_SIMULATOR } from '../../../__tests__/test-utils/device-fixtures.ts';
514

615
test('resolveRequestedOpenSurface rejects surface flag on iOS', () => {
716
assert.throws(
@@ -48,3 +57,54 @@ test('buildOpenResult exposes the Vega serial without leaking an internal platfo
4857
assert.equal(result.serial, 'amazon-vvd');
4958
assert.equal(result.target, 'tv');
5059
});
60+
61+
// --- #1533: `--save-script` arms recording through the publication lifecycle, not around it ---
62+
//
63+
// A re-open is the surface that used to set `recordSession` on its own. An ABORTED authoring
64+
// lifecycle is terminal, so the flag must arm nothing here either — otherwise the session keeps
65+
// paying recording-time costs (the direct-selector fast paths stay disabled) for a recording that
66+
// can never publish, and the writer is the only thing standing between it and a stray script.
67+
68+
function reopen(existingSession: ReturnType<typeof makeIosSession>, saveScript: boolean) {
69+
return buildNextOpenSession({
70+
existingSession,
71+
sessionName: existingSession.name,
72+
device: IOS_SIMULATOR,
73+
surface: 'app',
74+
appBundleId: 'com.example.other',
75+
saveScript,
76+
});
77+
}
78+
79+
test('#1533: re-opening an aborted authoring session with --save-script does not re-arm recording', () => {
80+
const aborted = makeIosSession('s', {
81+
recordSession: false,
82+
scriptPublication: authoringPublication('aborted'),
83+
});
84+
85+
assert.equal(reopen(aborted, true).recordSession, false);
86+
});
87+
88+
test('#1533: an aborted lifecycle that is already recording is corrected, not carried forward', () => {
89+
const drifted = makeIosSession('s', {
90+
recordSession: true,
91+
scriptPublication: authoringPublication('aborted'),
92+
});
93+
94+
assert.equal(reopen(drifted, true).recordSession, false);
95+
});
96+
97+
test('a re-open with --save-script still arms recording for a session with no publication yet', () => {
98+
const plain = makeIosSession('s', { recordSession: false });
99+
100+
assert.equal(reopen(plain, true).recordSession, true);
101+
});
102+
103+
test('a re-open without --save-script leaves an armed authoring session recording', () => {
104+
const armed = makeIosSession('s', {
105+
recordSession: true,
106+
scriptPublication: authoringPublication('armed'),
107+
});
108+
109+
assert.equal(reopen(armed, false).recordSession, true);
110+
});

‎src/daemon/handlers/session-close-script.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
effectiveWriteForce,
99
isSessionScriptPublished,
1010
markCloseGeneratedPublicationDone,
11+
recordSessionAfterSaveScriptFlag,
1112
} from '../session-script-publication-capability.ts';
1213
import { abortRepairTransaction, isRepairArmedSession } from '../session-replay-transaction.ts';
1314

@@ -80,7 +81,9 @@ export function finalizeOrdinaryCloseScript(params: {
8081
// The recorded close action already armed target/force through the recorder's flag ingress.
8182
// On a platform-close failure that action was never recorded; the log still publishes, but —
8283
// as before this migration — to the session default, not the request's explicit path.
83-
if (req.flags?.saveScript) session.recordSession = true;
84+
if (req.flags?.saveScript) {
85+
session.recordSession = recordSessionAfterSaveScriptFlag(session, true);
86+
}
8487

8588
try {
8689
const result = sessionStore.writeSessionLog(session, {

‎src/daemon/handlers/session-close.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,10 @@ async function stopOrRetainAppleRunnerAfterClose(
293293
// (`armAuthoringOnOpen`), so an unarmed session has nothing to retroactively arm; the only
294294
// correct response is refusal, before any teardown or publication work runs.
295295
//
296-
// #1533 (aborted-mid-recording) is the adjacent already-armed case, resolved separately: this
297-
// refusal promises "plain close tears down without writing", and an aborted authoring lifecycle
298-
// now makes that true by construction — the flag ingress no longer re-arms it
299-
// (`applyRecordedSaveScriptFlags`) and the script writer refuses to publish it from every path
300-
// that reaches it.
296+
// #1533 (aborted-mid-recording) is the adjacent already-armed case: this refusal promises "plain
297+
// close tears down without writing", and an ABORTED authoring lifecycle keeps that promise —
298+
// `--save-script` re-arms nothing on any surface (`recordSessionAfterSaveScriptFlag`) and the
299+
// writer refuses to publish the lifecycle from every path that reaches it.
301300
function assertTerminalRecordingCloseAllowed(req: DaemonRequest, session: SessionState): void {
302301
if (!req.flags?.saveScript) return;
303302
if (isAuthoringArmedSession(session)) return;

‎src/daemon/handlers/session-open-surface.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
type DeviceInfo,
99
} from '@agent-device/kernel/device';
1010
import type { SessionRuntimeHints, SessionState } from '../types.ts';
11+
import { recordSessionAfterSaveScriptFlag } from '../session-script-publication-capability.ts';
1112
import { AppError } from '@agent-device/kernel/errors';
1213
import { successText } from '../../utils/success-text.ts';
1314
import type { StartupPerfSample } from './session-startup-metrics.ts';
@@ -107,7 +108,7 @@ export function buildNextOpenSession(params: {
107108
surface,
108109
appBundleId,
109110
appName,
110-
recordSession: existingSession.recordSession || saveScript,
111+
recordSession: recordSessionAfterSaveScriptFlag(existingSession, saveScript),
111112
snapshot: undefined,
112113
};
113114
}

‎src/daemon/session-script-publication-capability.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,23 @@ export function abortAuthoringOnSecondOpen(session: SessionState): void {
5757
session.scriptPublication = abortAuthoring(publicationState(session));
5858
}
5959

60+
/**
61+
* Whether a session carrying `--save-script` on THIS request is left recording.
62+
*
63+
* Recording is a property of the publication lifecycle, not a flag each surface may set on its
64+
* own: an ABORTED authoring lifecycle is terminal (#1533), so the flag arms nothing on any
65+
* surface that handles it — the re-open builder, the close finalizer, and the recorded-action
66+
* ingress all get the same answer. Every other lifecycle keeps the old meaning: the flag arms
67+
* recording, and an already-recording session stays recording without it.
68+
*/
69+
export function recordSessionAfterSaveScriptFlag(
70+
session: SessionState,
71+
saveScript: boolean,
72+
): boolean {
73+
if (isAuthoringAborted(publicationState(session))) return false;
74+
return session.recordSession === true || saveScript;
75+
}
76+
6077
/**
6178
* The shared `--save-script` ingress on a RECORDED action (`open`/`close`; #1501 pins every
6279
* other command's raw flag closed at the router). Arms recording and applies target/force to
@@ -73,11 +90,8 @@ export function abortAuthoringOnSecondOpen(session: SessionState): void {
7390
* - `authoring` -> retarget under the #1258 per-target force rule (`resolveScriptTarget`).
7491
* - `repair` -> retarget the repair target the same way (a replayed step may carry the flag).
7592
*
76-
* #1533: an ABORTED authoring lifecycle is terminal and takes none of those branches. The flag
77-
* ingress used to re-arm `session.recordSession` for it — the second `open --save-script` that
78-
* caused the abort carries the flag into its own recorded action — leaving a session the caller
79-
* was told was aborted but that a bare `close` still published. Recording-time evidence stops at
80-
* the abort; there is nothing left to retarget or re-arm.
93+
* - `authoring{aborted}` -> nothing. The lifecycle is terminal (#1533), so the flag neither
94+
* re-arms recording nor retargets: recording-time evidence stopped at the abort.
8195
*/
8296
export function applyRecordedSaveScriptFlags(session: SessionState, flags: CommandFlags): void {
8397
if (!flags.saveScript) return;

‎src/daemon/session-script-publication-state.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,10 @@ export function isRepairCommittable(state: SessionScriptPublicationState): boole
148148

149149
/**
150150
* #1533: terminal authoring failure — a second successful `open` ended the journey, so the
151-
* recording publishes NOTHING, forever. The authoring counterpart of an aborted repair.
152-
*
153-
* `aborted` is terminal in the same sense `repair{aborted}` is, but it used to be enforced only
154-
* by `abortAuthoringOnSecondOpen` clearing `session.recordSession` — inert by ordering, not by
155-
* construction. A later `--save-script` flag ingress re-armed that boolean while leaving the
156-
* status terminal, and a bare `close` then published the full session log through a writer that
157-
* only knew how to block repairs. Making the state itself answer the question is what closes it.
151+
* recording publishes NOTHING, forever. The authoring counterpart of an aborted repair, and
152+
* terminal in the same sense: the state itself answers the question, so no caller re-derives
153+
* terminality from `session.recordSession`, which is an evidence-capture flag rather than a
154+
* publication authorization.
158155
*/
159156
export function isAuthoringAborted(state: SessionScriptPublicationState): boolean {
160157
return state.kind === 'authoring' && state.status === 'aborted';

‎src/daemon/session-script-writer.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,11 @@ function isRepairArmedWriteBlocked(session: SessionState): boolean {
9595
*
9696
* - not recording — the ordinary resting state of a session that never armed;
9797
* - a repair transaction that is committed or not yet committable (above);
98-
* - #1533: an ABORTED ordinary authoring lifecycle.
98+
* - an ABORTED ordinary authoring lifecycle (#1533) — the authoring counterpart of that gate.
9999
*
100-
* The last one is the authoring counterpart of the repair gate, and it is not redundant with the
101-
* recording check even though `abortAuthoringOnSecondOpen` clears `recordSession`: something can
102-
* re-arm that boolean behind the terminal status, which is exactly what the `--save-script` flag
103-
* ingress used to do. `close`'s own refusal already promises the caller "Retry with plain close;
104-
* it will tear down the session without writing" — this is what makes that true from every path
105-
* reaching the writer (bare `close`, teardown, idle-reap, active publication).
100+
* The last one is what makes `close`'s refusal ("Retry with plain close; it will tear down the
101+
* session without writing") true from every path reaching the writer: bare `close`, teardown,
102+
* idle-reap, active publication.
106103
*/
107104
function isPublicationWriteBlocked(session: SessionState): boolean {
108105
if (!session.recordSession) return true;

0 commit comments

Comments
 (0)