Skip to content

Commit 2dd629d

Browse files
thymikeeclaude
andcommitted
refactor(daemon): migrate script publication onto the tagged aggregate (#1478 P4a)
The eight co-resident SessionState fields (scriptRecordingState, saveScriptPath, saveScriptForce, saveScriptBoundary, saveScriptComplete, saveScriptCommitted, repairPlatformCloseReceipt, repairSourcePath) are gone; SessionState.scriptPublication holds the aggregate, and every writer migrated in this commit — no shadow state. Two daemon-private projections own the writes, enforced by the R7 ownership gate: - session-replay-transaction.ts (ReplaySessionTransaction): repair arm/demote/ complete/abort, close receipts, and the uncommitted/boundary/sourcePath reads that idle-reap, tombstones, divergence-hold, and the recorder's exclusion key off. - session-script-publication-capability.ts (SessionScriptPublication): authoring arm on open, the recorded --save-script flag ingress, active publication, the published transitions, and the effective per-target force decision (#1258). The writer keeps the commit transition so idempotence stays colocated with the atomic publish. Failure/retry transitions pinned as the brief requires: platform-close failure leaves state unchanged (no receipt, retry re-dispatches); publication failure retains target+force+receipt (same-identity retry skips close dispatch); committed and aborted are explicit terminal states that drop the receipt. Design decisions resolved: - Force retention across a default->explicit retarget is preserved as-is and still flagged in resolveScriptTarget's docblock as a probable #1258 gap; tightening it stays a separate product change. - The never-armed 'close --save-script' whole-log publication folds into the authoring lifecycle (armed at the recorded close, published in the same request) instead of a fourth variant: every close path that reaches the write deletes the session, so the transient armed state cannot leak into 'session save-script' eligibility, whose not-armed-before-this-journey rejection is untouched. One real bug caught by the migrated tests and fixed in resolveScriptTarget: a bare (pathless) re-arm collapsed an already-materialized explicit target back to the daemon default, wiping the healed-sibling path on every per-step repair re-arm and defeating the persisted-force preflight bypass. A bare re-arm now keeps the previous target and only adds a live force grant. R7 rows consolidated to one scriptPublication entry (three owners) and the recordSession row narrowed; the R10 baseline drops to 22 writer-owned fields / 28 owner claims so the consolidation cannot regrow. Gates: typecheck, lint, format, layering clean; 624 files / 5220 tests pass (two known contention-flake timeouts reproduce only under full-suite load and pass in isolation). Refs #1478 Co-Authored-By: Claude <[email protected]>
1 parent ec3ba0c commit 2dd629d

34 files changed

Lines changed: 978 additions & 412 deletions

‎scripts/layering/daemon-modularity.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ const LARGEST_TYPE_CYCLE_ZONE_CEILINGS: Readonly<Record<string, number>> = {
1313

1414
export const DAEMON_MODULARITY_BASELINE = {
1515
sessionState: {
16-
writerOwnedFields: 29,
17-
ownerFileClaims: 40,
16+
writerOwnedFields: 22,
17+
ownerFileClaims: 28,
1818
},
1919
largestTypeCycle: {
2020
zoneMembers: LARGEST_TYPE_CYCLE_ZONE_CEILINGS,

‎scripts/layering/session-state.ts‎

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,35 +49,23 @@ export const SESSION_STATE_FIELD_OWNERS: Readonly<Record<string, readonly string
4949
lastComparisonSafeSnapshot: ['src/daemon/session-snapshot.ts'],
5050
androidSnapshotFreshness: ['src/daemon/android-snapshot-freshness.ts'],
5151

52-
// ADR 0016 active-session publication. `scriptRecordingState` tracks the armed -> published
53-
// lifecycle; `recordSession` is the broader "record actions" flag and is deliberately set
54-
// on its own by paths that record without arming a publication.
55-
scriptRecordingState: [
56-
'src/daemon/handlers/session-open.ts',
57-
'src/daemon/handlers/session-script-publication.ts',
52+
// #1478 P4a script publication. The tagged aggregate replaced the eight co-resident
53+
// `saveScript*`/`scriptRecordingState`/`repair*` fields; its ONLY writers are the two
54+
// daemon-private projections (`session-replay-transaction.ts`,
55+
// `session-script-publication-capability.ts`) and the writer's commit transition.
56+
// `recordSession` is the broader "record actions" flag and is deliberately set on its own by
57+
// paths that record without arming a publication.
58+
scriptPublication: [
59+
'src/daemon/session-replay-transaction.ts',
60+
'src/daemon/session-script-publication-capability.ts',
61+
'src/daemon/session-script-writer.ts',
5862
],
5963
recordSession: [
6064
'src/daemon/handlers/session-close-script.ts',
61-
'src/daemon/handlers/session-open.ts',
62-
'src/daemon/handlers/session-replay-runtime.ts',
63-
'src/daemon/handlers/session-script-publication.ts',
64-
'src/daemon/session-action-recorder.ts',
65+
'src/daemon/session-replay-transaction.ts',
66+
'src/daemon/session-script-publication-capability.ts',
6567
],
66-
saveScriptPath: [
67-
'src/daemon/handlers/session-replay-runtime.ts',
68-
'src/daemon/handlers/session-script-publication.ts',
69-
'src/daemon/session-action-recorder.ts',
70-
],
71-
saveScriptForce: [
72-
'src/daemon/handlers/session-replay-runtime.ts',
73-
'src/daemon/handlers/session-script-publication.ts',
74-
'src/daemon/session-action-recorder.ts',
75-
],
76-
saveScriptBoundary: ['src/daemon/handlers/session-replay-runtime.ts'],
77-
saveScriptCommitted: ['src/daemon/session-script-writer.ts'],
78-
repairSourcePath: ['src/daemon/handlers/session-replay-runtime.ts'],
7968
pendingRecordAndHeal: ['src/daemon/handlers/session-replay-resume.ts'],
80-
repairPlatformCloseReceipt: ['src/daemon/handlers/session-close.ts'],
8169

8270
trace: ['src/daemon/handlers/record-trace.ts'],
8371
recording: ['src/daemon/handlers/record-trace-recording.ts'],
@@ -93,7 +81,6 @@ export const SESSION_STATE_FIELD_OWNERS: Readonly<Record<string, readonly string
9381
appName: ['src/daemon/snapshot-runtime.ts'],
9482
lease: ['src/daemon/handlers/session-open.ts'],
9583
deviceClaim: ['src/daemon/handlers/session-open.ts'],
96-
saveScriptComplete: ['src/daemon/handlers/session-replay-runtime.ts'],
9784
};
9885

9986
/**

‎src/__tests__/test-utils/session-factories.ts‎

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,42 @@
11
import type { SessionState } from '../../daemon/types.ts';
2+
import type {
3+
SessionScriptPublicationState,
4+
SessionScriptRepairStatus,
5+
} from '../../daemon/session-script-publication-state.ts';
26
import { IOS_SIMULATOR, ANDROID_EMULATOR, MACOS_DEVICE } from './device-fixtures.ts';
37

8+
/** A repair-variant `scriptPublication` literal for test setups. */
9+
export function repairPublication(
10+
status: SessionScriptRepairStatus,
11+
opts?: { boundary?: number; path?: string; force?: boolean; sourcePath?: string },
12+
): SessionScriptPublicationState {
13+
return {
14+
kind: 'repair',
15+
status,
16+
target:
17+
opts?.path !== undefined
18+
? { kind: 'explicit', path: opts.path, force: opts?.force === true }
19+
: { kind: 'default', force: opts?.force === true },
20+
boundary: opts?.boundary ?? 0,
21+
...(opts?.sourcePath !== undefined ? { sourcePath: opts.sourcePath } : {}),
22+
};
23+
}
24+
25+
/** An authoring-variant `scriptPublication` literal for test setups. */
26+
export function authoringPublication(
27+
status: 'armed' | 'aborted' | 'published',
28+
opts?: { path?: string; force?: boolean },
29+
): SessionScriptPublicationState {
30+
return {
31+
kind: 'authoring',
32+
status,
33+
target:
34+
opts?.path !== undefined
35+
? { kind: 'explicit', path: opts.path, force: opts?.force === true }
36+
: { kind: 'default', force: opts?.force === true },
37+
};
38+
}
39+
440
export function makeSession(name: string, overrides?: Partial<SessionState>): SessionState {
541
return {
642
name,
@@ -25,22 +61,18 @@ export function makeMacOsSession(name: string, overrides?: Partial<SessionState>
2561

2662
// --- Script-authoring session states ---
2763
//
28-
// The three factories below name the states a session-script session can be in,
29-
// instead of leaving each test to re-derive them from a pile of `saveScript*`
30-
// booleans. They exist because the fields are NOT independent: `recordSession`
31-
// without a boundary is an ordinary recording, a boundary without
32-
// `saveScriptComplete` is an ARMED-but-uncommittable repair, and only the
33-
// COMPLETE combination publishes. Spelling that out per test made the
34-
// distinction the tests are actually about (ordinary vs repair, armed vs
35-
// complete) the hardest thing to see in them.
64+
// The three factories below name the states a session-script session can be in.
65+
// Since #1478 P4a those states are structural (`scriptPublication` is a tagged
66+
// aggregate), but the factories keep naming the states production actually
67+
// produces: ordinary recording, an ARMED-but-uncommittable repair, and the
68+
// COMPLETE combination that publishes.
3669
//
37-
// A test that deliberately exercises an odd combination (a boundary with no
38-
// recording, say) should still build it inline — these are for the states
39-
// production actually produces.
70+
// A test that deliberately exercises an odd combination (a repair variant with
71+
// no recording, say) should still build it inline.
4072

4173
/**
4274
* ADR 0016 ordinary authoring recording: `recordSession` armed, NO repair
43-
* boundary. This is a plain `open --save-script` / `close --save-script`
75+
* variant. This is a plain `open --save-script` / `close --save-script`
4476
* session, and the baseline for every authoring-side handler test (target-v1
4577
* evidence, parameterized fills, landmark waits) that only needs the session to
4678
* be recording its actions.
@@ -57,8 +89,7 @@ export function makeAuthoringSession(
5789

5890
/**
5991
* ADR 0012 decision 6: a session ARMED for repair by `replay --save-script` —
60-
* recording plus the repair-run boundary watermark, which is what the writer's
61-
* `repairArmed` actually keys off (`saveScriptBoundary !== undefined`).
92+
* recording plus the repair variant with its boundary watermark at 0.
6293
*
6394
* ARMED, not COMPLETE: a writer handed this session ABORTS (publishes no
6495
* prefix) rather than committing. Use `makeRepairCompleteSession` for a
@@ -68,7 +99,15 @@ export function makeRepairArmedSession(
6899
name: string,
69100
overrides?: Partial<SessionState>,
70101
): SessionState {
71-
return makeAuthoringSession(name, { saveScriptBoundary: 0, ...overrides });
102+
return makeAuthoringSession(name, {
103+
scriptPublication: {
104+
kind: 'repair',
105+
status: 'armed',
106+
target: { kind: 'default', force: false },
107+
boundary: 0,
108+
},
109+
...overrides,
110+
});
72111
}
73112

74113
/**
@@ -80,5 +119,13 @@ export function makeRepairCompleteSession(
80119
name: string,
81120
overrides?: Partial<SessionState>,
82121
): SessionState {
83-
return makeRepairArmedSession(name, { saveScriptComplete: true, ...overrides });
122+
return makeAuthoringSession(name, {
123+
scriptPublication: {
124+
kind: 'repair',
125+
status: 'complete',
126+
target: { kind: 'default', force: false },
127+
boundary: 0,
128+
},
129+
...overrides,
130+
});
84131
}

‎src/daemon/__tests__/request-router-repair-expired.test.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,13 @@ function tombstonedSession(name: string): SessionState {
4242
device: { platform: 'apple', id: 'sim-1', name: 'iPhone', kind: 'simulator', booted: true },
4343
createdAt: Date.now(),
4444
actions: [],
45-
saveScriptBoundary: 0,
46-
repairSourcePath: '/flows/login.ad',
45+
scriptPublication: {
46+
kind: 'repair',
47+
status: 'armed',
48+
target: { kind: 'default', force: false },
49+
boundary: 0,
50+
sourcePath: '/flows/login.ad',
51+
},
4752
};
4853
}
4954

‎src/daemon/__tests__/request-router-typed-error.test.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,11 @@ test('#1391: an ordinary close-time script-save failure surfaces details.reason/
189189
`agent-device-router-typed-error-${Date.now()}-${Math.random().toString(36).slice(2)}.ad`,
190190
);
191191
fs.writeFileSync(targetPath, 'pre-existing\n');
192-
session.saveScriptPath = targetPath;
192+
session.scriptPublication = {
193+
kind: 'authoring',
194+
status: 'armed',
195+
target: { kind: 'explicit', path: targetPath, force: false },
196+
};
193197
sessionStore.set('typed-error', session);
194198

195199
try {

‎src/daemon/__tests__/request-save-script-transports.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* `.ad` artifact behind.
1212
*/
1313
import fs from 'node:fs';
14+
import { NO_SCRIPT_PUBLICATION, scriptTargetPath } from '../session-script-publication-state.ts';
1415
import net from 'node:net';
1516
import os from 'node:os';
1617
import path from 'node:path';
@@ -192,7 +193,7 @@ for (const [transport, send] of TRANSPORTS) {
192193
expect(session.actions).toEqual([]);
193194
// No arming: neither the recording marker nor the publication target moved.
194195
expect(session.recordSession).toBe(undefined);
195-
expect(session.saveScriptPath).toBe(undefined);
196+
expect(session.scriptPublication).toBe(undefined);
196197
// No artifact: the write a later close/teardown would attempt publishes nothing.
197198
expect(sessionStore.writeSessionLog(session)).toEqual({ written: false });
198199
expect(listAdArtifacts(root)).toEqual([]);
@@ -283,7 +284,7 @@ test('an owner-armed session still records its target and publishes its script',
283284
result: { session: SESSION },
284285
});
285286
expect(session.recordSession).toBe(true);
286-
expect(session.saveScriptPath).toBe(target);
287+
expect(scriptTargetPath(session.scriptPublication ?? NO_SCRIPT_PUBLICATION)).toBe(target);
287288

288289
const result = sessionStore.writeSessionLog(session);
289290
expect(result).toEqual({ written: true, path: target, actionCount: 1 });

‎src/daemon/__tests__/selector-recording.test.ts‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ function planStepReq(command: string, flags: DaemonRequest['flags'] = {}): Daemo
3232

3333
test('a repair-armed session excludes get/is/find by default but keeps recording wait', () => {
3434
const store = makeStore();
35-
const session = makeIosSession('default', { saveScriptBoundary: 0 });
35+
const session = makeIosSession('default', {
36+
scriptPublication: {
37+
kind: 'repair',
38+
status: 'armed',
39+
target: { kind: 'default', force: false },
40+
boundary: 0,
41+
},
42+
});
3643
store.set('default', session);
3744

3845
recordIfSession(store, 'default', req('get'), {});
@@ -50,7 +57,14 @@ test('a repair-armed session excludes get/is/find by default but keeps recording
5057
// with --record to keep them.
5158
test('a repair-armed session still records get/is/find dispatched as replay plan steps (authored provenance)', () => {
5259
const store = makeStore();
53-
const session = makeIosSession('default', { saveScriptBoundary: 0 });
60+
const session = makeIosSession('default', {
61+
scriptPublication: {
62+
kind: 'repair',
63+
status: 'armed',
64+
target: { kind: 'default', force: false },
65+
boundary: 0,
66+
},
67+
});
5468
store.set('default', session);
5569

5670
recordIfSession(store, 'default', planStepReq('get'), {});
@@ -62,7 +76,14 @@ test('a repair-armed session still records get/is/find dispatched as replay plan
6276

6377
test('--record forces get/is/find through even while repair-armed', () => {
6478
const store = makeStore();
65-
const session = makeIosSession('default', { saveScriptBoundary: 0 });
79+
const session = makeIosSession('default', {
80+
scriptPublication: {
81+
kind: 'repair',
82+
status: 'armed',
83+
target: { kind: 'default', force: false },
84+
boundary: 0,
85+
},
86+
});
6687
store.set('default', session);
6788

6889
recordIfSession(store, 'default', req('get', { record: true }), {});
@@ -75,7 +96,7 @@ test('--record forces get/is/find through even while repair-armed', () => {
7596
test('outside a repair-armed session, get/is/find/wait all record normally', () => {
7697
const store = makeStore();
7798
const session = makeIosSession('default');
78-
expect(session.saveScriptBoundary).toBeUndefined();
99+
expect(session.scriptPublication).toBeUndefined();
79100
store.set('default', session);
80101

81102
recordIfSession(store, 'default', req('get'), {});

‎src/daemon/__tests__/session-action-recorder.test.ts‎

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ import { recordActionEntry } from '../session-action-recorder.ts';
1010
import { makeIosSession } from '../../__tests__/test-utils/session-factories.ts';
1111

1212
test('an observation-only action is excluded while repair-armed and no --record is given', () => {
13-
const session = makeIosSession('default', { saveScriptBoundary: 0 });
13+
const session = makeIosSession('default', {
14+
scriptPublication: {
15+
kind: 'repair',
16+
status: 'armed',
17+
target: { kind: 'default', force: false },
18+
boundary: 0,
19+
},
20+
});
1421
const action = recordActionEntry(session, {
1522
command: 'get',
1623
positionals: ['attrs', 'id="save"'],
@@ -23,7 +30,14 @@ test('an observation-only action is excluded while repair-armed and no --record
2330
});
2431

2532
test('--record forces an observation-only action through while repair-armed', () => {
26-
const session = makeIosSession('default', { saveScriptBoundary: 0 });
33+
const session = makeIosSession('default', {
34+
scriptPublication: {
35+
kind: 'repair',
36+
status: 'armed',
37+
target: { kind: 'default', force: false },
38+
boundary: 0,
39+
},
40+
});
2741
const action = recordActionEntry(session, {
2842
command: 'get',
2943
positionals: ['attrs', 'id="save"'],
@@ -37,7 +51,7 @@ test('--record forces an observation-only action through while repair-armed', ()
3751

3852
test('an observation-only action records normally outside a repair-armed session (ordinary authoring recording is unchanged)', () => {
3953
const session = makeIosSession('default');
40-
expect(session.saveScriptBoundary).toBeUndefined();
54+
expect(session.scriptPublication).toBeUndefined();
4155
const action = recordActionEntry(session, {
4256
command: 'is',
4357
positionals: ['visible', 'id="save"'],
@@ -50,7 +64,14 @@ test('an observation-only action records normally outside a repair-armed session
5064
});
5165

5266
test('a mutating action is never excluded, repair-armed or not', () => {
53-
const session = makeIosSession('default', { saveScriptBoundary: 0 });
67+
const session = makeIosSession('default', {
68+
scriptPublication: {
69+
kind: 'repair',
70+
status: 'armed',
71+
target: { kind: 'default', force: false },
72+
boundary: 0,
73+
},
74+
});
5475
const action = recordActionEntry(session, {
5576
command: 'press',
5677
positionals: ['@e5'],
@@ -62,7 +83,14 @@ test('a mutating action is never excluded, repair-armed or not', () => {
6283
});
6384

6485
test('a command explicitly marked NOT observation-only (e.g. the top-level `wait`) always records, even while repair-armed', () => {
65-
const session = makeIosSession('default', { saveScriptBoundary: 0 });
86+
const session = makeIosSession('default', {
87+
scriptPublication: {
88+
kind: 'repair',
89+
status: 'armed',
90+
target: { kind: 'default', force: false },
91+
boundary: 0,
92+
},
93+
});
6694
const action = recordActionEntry(session, {
6795
command: 'wait',
6896
positionals: ['500'],
@@ -75,7 +103,14 @@ test('a command explicitly marked NOT observation-only (e.g. the top-level `wait
75103
});
76104

77105
test('--no-record still takes precedence over an observation-only action, repair-armed or not', () => {
78-
const session = makeIosSession('default', { saveScriptBoundary: 0 });
106+
const session = makeIosSession('default', {
107+
scriptPublication: {
108+
kind: 'repair',
109+
status: 'armed',
110+
target: { kind: 'default', force: false },
111+
boundary: 0,
112+
},
113+
});
79114
const action = recordActionEntry(session, {
80115
command: 'get',
81116
positionals: ['attrs', 'id="save"'],

0 commit comments

Comments
 (0)