Skip to content

Commit 9902936

Browse files
committed
refactor: bind action recording and metadata patches to session lifetimes
1 parent 0b76607 commit 9902936

46 files changed

Lines changed: 675 additions & 284 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/daemon/__tests__/generic-settle.test.ts‎

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ async function dispatchGeneric(params: {
187187
};
188188
return await dispatchGenericCommand({
189189
req,
190-
session: params.session,
190+
ref: params.sessionStore.lookup(params.sessionName)!,
191191
sessionName: params.sessionName,
192192
logPath: '',
193193
sessionStore: params.sessionStore,
@@ -549,32 +549,31 @@ test('scroll without --settle takes no observation captures and issues no refs',
549549
expect(refFrameState(sessionStore.get(sessionName) as SessionState)).toBe('expired');
550550
});
551551

552-
test('a settle observation that cannot build a runtime degrades instead of failing the action', async () => {
552+
test('an ended generic lifetime is refused before dispatch or settle construction', async () => {
553553
const sessionStore = makeSessionStore();
554554
const sessionName = 'generic-settle-evicted';
555-
// The session the router handed us is no longer in the store — evicted
556-
// between dispatch and observation. Building the settle runtime throws
557-
// SESSION_NOT_FOUND, and the observation is best-effort: the scroll already
558-
// happened, so the response keeps its result and simply carries no settle.
559-
const session = makeIosSession(sessionName);
560-
setSessionSnapshot(
561-
session,
562-
buildSnapshotState({ nodes: BEFORE_NODES, backend: 'xctest', producer: 'apple-runner' }, {}),
563-
);
564-
activateCompleteRefFrame(session);
555+
seedSession(sessionName, sessionStore);
556+
const ref = sessionStore.lookup(sessionName)!;
557+
sessionStore.retire(ref);
565558
mockCommandDispatch([AFTER_NODES]);
566-
567-
const response = await dispatchGeneric({
568-
sessionName,
569-
sessionStore,
570-
session,
571-
command: 'scroll',
572-
positionals: ['down'],
573-
flags: { ...SETTLE_FLAGS },
574-
});
575-
576-
const data = expectOkData(response);
577-
expect(data.settle).toBeUndefined();
559+
await expect(
560+
dispatchGenericCommand({
561+
req: {
562+
token: 't',
563+
session: sessionName,
564+
command: 'scroll',
565+
positionals: ['down'],
566+
flags: SETTLE_FLAGS,
567+
},
568+
ref,
569+
sessionName,
570+
sessionStore,
571+
logPath: '',
572+
contextFromFlags,
573+
executePlatformCommand: platformExecution,
574+
}),
575+
).rejects.toMatchObject({ details: { reason: 'session_lifetime_ended' } });
576+
expect(mockDispatch).not.toHaveBeenCalled();
578577
expect(captureObservations).toEqual([]);
579578
});
580579

‎src/daemon/__tests__/replay-repair/session-replay-repair-acceptance.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ test('a healed script survives repair + fresh-session replay: self-contained ope
114114
expect(session.actions.map((a) => a.command)).toEqual(['open']);
115115

116116
// --- Agent presses the blessed @ref (record-and-heal): recorded live. ---
117-
sessionStore.recordAction(session, {
117+
sessionStore.recordAction(storeSessionForTest(sessionStore, session), {
118118
command: 'press',
119119
positionals: ['@e7'],
120120
flags: {},

‎src/daemon/__tests__/replay-repair/session-replay-repair-empty-tail.test.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ test('a record-and-heal divergence on the LAST step resumes with an empty tail a
153153
expect(session.actions.map((a) => a.command)).toEqual(['open']);
154154

155155
// --- Agent performs the corrective press (blessed @ref), recorded live. ---
156-
sessionStore.recordAction(session, {
156+
sessionStore.recordAction(storeSessionForTest(sessionStore, session), {
157157
command: 'press',
158158
positionals: ['@e6'],
159159
flags: {},
@@ -280,7 +280,7 @@ test('a manual divergence (unannotated action-failure) on the LAST step resumes
280280

281281
// --- Agent performs the step's intent as a recorded action (blessed
282282
// @ref), recorded live. ---
283-
sessionStore.recordAction(session, {
283+
sessionStore.recordAction(storeSessionForTest(sessionStore, session), {
284284
command: 'press',
285285
positionals: ['@e6'],
286286
flags: {},
@@ -408,7 +408,7 @@ test('a caution (identity-mismatch) divergence on the LAST step resumes with an
408408
// --- Agent presses the actual (renamed) control via a blessed @ref,
409409
// recorded live — the record-and-heal-shaped repair for path (a) from
410410
// #1262 ("selector binds the wrong node on the right screen"). ---
411-
sessionStore.recordAction(session, {
411+
sessionStore.recordAction(storeSessionForTest(sessionStore, session), {
412412
command: 'press',
413413
positionals: ['@e6'],
414414
flags: {},
@@ -659,7 +659,7 @@ test('a stale --plan-digest on an empty-tail resume is rejected WITHOUT consumin
659659
expect(divergence.resume.from).toBe(3);
660660

661661
const session = sessionStore.get(sessionName)!;
662-
sessionStore.recordAction(session, {
662+
sessionStore.recordAction(storeSessionForTest(sessionStore, session), {
663663
command: 'press',
664664
positionals: ['@e6'],
665665
flags: {},

‎src/daemon/__tests__/replay-repair/session-replay-repair-loop.test.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { storeSessionForTest } from '../../../__tests__/test-utils/store-factory.ts';
12
import { isSessionRecording } from '../../session-script-publication-capability.ts';
23
/**
34
* ADR 0012 decision 6: `replay --save-script` arming (R1), the repair-run
@@ -134,7 +135,7 @@ test('R1/R2/R6: prefix steps get fresh evidence, corrective + resumed steps land
134135
expect(divergence.resume.from).toBe(3);
135136

136137
// --- Agent performs the corrective action live (recorded). ---
137-
sessionStore.recordAction(session, {
138+
sessionStore.recordAction(storeSessionForTest(sessionStore, session), {
138139
command: 'press',
139140
positionals: ['@e9'],
140141
flags: {},
@@ -381,13 +382,13 @@ test('a --no-record state-fix action never enters session.actions', () => {
381382

382383
// Agent fixes app state with --no-record, then performs the real corrective
383384
// action (recorded).
384-
sessionStore.recordAction(session, {
385+
sessionStore.recordAction(storeSessionForTest(sessionStore, session), {
385386
command: 'press',
386387
positionals: ['100', '200'],
387388
flags: { noRecord: true },
388389
result: {},
389390
});
390-
sessionStore.recordAction(session, {
391+
sessionStore.recordAction(storeSessionForTest(sessionStore, session), {
391392
command: 'press',
392393
positionals: ['@e9'],
393394
flags: {},
@@ -578,7 +579,7 @@ test('Fix 3: a --from resume that lands on the terminal close skips it too, lett
578579
expect(divergence.resume.from).toBe(2);
579580

580581
const session = sessionStore.get(sessionName)!;
581-
sessionStore.recordAction(session, {
582+
sessionStore.recordAction(storeSessionForTest(sessionStore, session), {
582583
command: 'press',
583584
positionals: ['@e9'],
584585
flags: {},

‎src/daemon/__tests__/replay-repair/session-replay-repair-record-exclusion.test.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { storeSessionForTest } from '../../../__tests__/test-utils/store-factory.ts';
12
/**
23
* #1271 stage 2 (ADR 0012 amendment): repair-segment default exclusion of
34
* observation-only commands (`snapshot`/`get`/`is`/a read-only `find`), the
@@ -193,14 +194,14 @@ test('diagnostic get/is reads mid-repair are excluded from the healed script by
193194
// --- The agent explores mid-repair: a couple of diagnostic reads to locate
194195
// the renamed control. Both are observation-only and the session is
195196
// repair-armed, so per the default exclusion neither is appended. ---
196-
ctx.sessionStore.recordAction(session, {
197+
ctx.sessionStore.recordAction(storeSessionForTest(ctx.sessionStore, session), {
197198
command: 'get',
198199
positionals: ['attrs', '@e5'],
199200
flags: {},
200201
result: { selectorChain: ['id="save-v2"'] },
201202
interactiveObservation: true,
202203
});
203-
ctx.sessionStore.recordAction(session, {
204+
ctx.sessionStore.recordAction(storeSessionForTest(ctx.sessionStore, session), {
204205
command: 'is',
205206
positionals: ['visible', 'id="save-v2"'],
206207
flags: {},
@@ -212,7 +213,7 @@ test('diagnostic get/is reads mid-repair are excluded from the healed script by
212213

213214
// --- The agent performs the corrective press (blessed @ref), recorded live —
214215
// a mutating action is never observation-only, so it is unaffected. ---
215-
ctx.sessionStore.recordAction(session, {
216+
ctx.sessionStore.recordAction(storeSessionForTest(ctx.sessionStore, session), {
216217
command: 'press',
217218
positionals: ['@e7'],
218219
flags: {},
@@ -295,7 +296,7 @@ test("a --record'ed diagnostic read lands in the healed script (the diverged-ste
295296
// --- The agent's correction for the diverged step IS itself a read (the
296297
// wave-3 E3 shape): `get attrs` on the renamed control, explicitly forced
297298
// into the heal with `--record` since it would otherwise be excluded. ---
298-
ctx.sessionStore.recordAction(session, {
299+
ctx.sessionStore.recordAction(storeSessionForTest(ctx.sessionStore, session), {
299300
command: 'get',
300301
positionals: ['attrs', '@e7'],
301302
flags: { record: true },
@@ -374,14 +375,14 @@ test('empty-segment guard: a --from resume refuses with an actionable --record h
374375
// --- The agent ONLY inspects — never performs a corrective action. Both
375376
// reads are excluded from `session.actions`, so nothing was recorded in
376377
// this repair segment. ---
377-
ctx.sessionStore.recordAction(session, {
378+
ctx.sessionStore.recordAction(storeSessionForTest(ctx.sessionStore, session), {
378379
command: 'get',
379380
positionals: ['attrs', '@e5'],
380381
flags: {},
381382
result: { selectorChain: ['id="save-v2"'] },
382383
interactiveObservation: true,
383384
});
384-
ctx.sessionStore.recordAction(session, {
385+
ctx.sessionStore.recordAction(storeSessionForTest(ctx.sessionStore, session), {
385386
command: 'find',
386387
positionals: ['id', 'save-v2', 'exists'],
387388
flags: {},
@@ -418,7 +419,7 @@ test('non-repair authoring recording is unchanged: a read in a fresh `open --sav
418419
session.scriptPublication = authoringPublication('armed');
419420
expect(session.scriptPublication.kind).not.toBe('repair');
420421

421-
ctx.sessionStore.recordAction(session, {
422+
ctx.sessionStore.recordAction(storeSessionForTest(ctx.sessionStore, session), {
422423
command: 'get',
423424
positionals: ['attrs', '@e5'],
424425
flags: {},

‎src/daemon/__tests__/replay-repair/session-replay-repair-transaction-force.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { storeSessionForTest } from '../../../__tests__/test-utils/store-factory.ts';
12
/**
23
* ADR 0012 decision 6 repair-transaction `--force`/`--overwrite` semantics (#1258): a
34
* `--save-script` target that already exists is refused at arm time, before any step runs, unless
@@ -244,7 +245,7 @@ test('#1258 preflight honors PERSISTED force: a --from continuation without --fo
244245

245246
// The agent's corrective press (blessed @ref), recorded live.
246247
const session = sessionStore.get(sessionName)!;
247-
sessionStore.recordAction(session, {
248+
sessionStore.recordAction(storeSessionForTest(sessionStore, session), {
248249
command: 'press',
249250
positionals: ['@e7'],
250251
flags: {},
@@ -317,7 +318,7 @@ test('#1258 preflight is per-target: a --from continuation RETARGETING to an exi
317318
expect(sessionTargetPath(session)).toBe(targetA);
318319
expect(sessionTargetForce(session)).toBe(true);
319320
// The agent's corrective press.
320-
sessionStore.recordAction(session, {
321+
sessionStore.recordAction(storeSessionForTest(sessionStore, session), {
321322
command: 'press',
322323
positionals: ['@e7'],
323324
flags: {},

‎src/daemon/__tests__/replay-repair/session-replay-repair-transaction.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ test('end-to-end repair transaction: cold divergence stays alive, corrective res
158158

159159
// --- Agent performs the corrective press (blessed @ref), recorded live. ---
160160
const session = sessionStore.get(sessionName)!;
161-
sessionStore.recordAction(session, {
161+
sessionStore.recordAction(storeSessionForTest(sessionStore, session), {
162162
command: 'press',
163163
positionals: ['@e7'],
164164
flags: {},

‎src/daemon/__tests__/replay-repair/session-replay-repair.fixtures.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { storeSessionForTest } from '../../../__tests__/test-utils/store-factory.ts';
12
import { isSessionRecording } from '../../session-script-publication-capability.ts';
23
/**
34
* Shared fixtures for the ADR 0012 decision 6 repair-loop tests. The mock
@@ -59,7 +60,7 @@ export function makeRecordingReplayInvoke(config: RecordingReplayInvokeConfig):
5960
}
6061
const session = resolveInvokeSession(config, req);
6162
const evidence = isSessionRecording(session) ? config.evidence?.(req) : undefined;
62-
sessionStore.recordAction(session, {
63+
sessionStore.recordAction(storeSessionForTest(sessionStore, session), {
6364
command: req.command,
6465
positionals: req.positionals ?? [],
6566
flags: req.flags ?? {},

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ test('an owner-armed session still records its target and publishes its script',
289289

290290
// What `open`/`close --save-script` do once past the seam: arm the session,
291291
// then publish at teardown. Unchanged by the ingress rejection.
292-
sessionStore.recordAction(session, {
292+
sessionStore.recordAction(storeSessionForTest(sessionStore, session), {
293293
command: 'open',
294294
positionals: ['Example'],
295295
flags: { saveScript: target },

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ test('a repair-armed session excludes get/is/find by default but keeps recording
4848
});
4949
store.set('default', session);
5050

51-
recordIfSession(store, 'default', req('get'), {});
52-
recordIfSession(store, 'default', req('is'), {});
53-
recordIfSession(store, 'default', req('find'), {});
54-
recordIfSession(store, 'default', req('wait'), {});
51+
recordIfSession(store, store.lookup('default'), req('get'), {});
52+
recordIfSession(store, store.lookup('default'), req('is'), {});
53+
recordIfSession(store, store.lookup('default'), req('find'), {});
54+
recordIfSession(store, store.lookup('default'), req('wait'), {});
5555

5656
expect(store.get('default')!.actions.map((a) => a.command)).toEqual(['wait']);
5757
});
@@ -73,9 +73,9 @@ test('a repair-armed session still records get/is/find dispatched as replay plan
7373
});
7474
store.set('default', session);
7575

76-
recordIfSession(store, 'default', planStepReq('get'), {});
77-
recordIfSession(store, 'default', planStepReq('is'), {});
78-
recordIfSession(store, 'default', planStepReq('find'), {});
76+
recordIfSession(store, store.lookup('default'), planStepReq('get'), {});
77+
recordIfSession(store, store.lookup('default'), planStepReq('is'), {});
78+
recordIfSession(store, store.lookup('default'), planStepReq('find'), {});
7979

8080
expect(store.get('default')!.actions.map((a) => a.command)).toEqual(['get', 'is', 'find']);
8181
});
@@ -92,9 +92,9 @@ test('--record forces get/is/find through even while repair-armed', () => {
9292
});
9393
store.set('default', session);
9494

95-
recordIfSession(store, 'default', req('get', { record: true }), {});
96-
recordIfSession(store, 'default', req('is', { record: true }), {});
97-
recordIfSession(store, 'default', req('find', { record: true }), {});
95+
recordIfSession(store, store.lookup('default'), req('get', { record: true }), {});
96+
recordIfSession(store, store.lookup('default'), req('is', { record: true }), {});
97+
recordIfSession(store, store.lookup('default'), req('find', { record: true }), {});
9898

9999
expect(store.get('default')!.actions.map((a) => a.command)).toEqual(['get', 'is', 'find']);
100100
});
@@ -105,10 +105,10 @@ test('outside a repair-armed session, get/is/find/wait all record normally', ()
105105
expect(session.scriptPublication).toBeUndefined();
106106
store.set('default', session);
107107

108-
recordIfSession(store, 'default', req('get'), {});
109-
recordIfSession(store, 'default', req('is'), {});
110-
recordIfSession(store, 'default', req('find'), {});
111-
recordIfSession(store, 'default', req('wait'), {});
108+
recordIfSession(store, store.lookup('default'), req('get'), {});
109+
recordIfSession(store, store.lookup('default'), req('is'), {});
110+
recordIfSession(store, store.lookup('default'), req('find'), {});
111+
recordIfSession(store, store.lookup('default'), req('wait'), {});
112112

113113
expect(store.get('default')!.actions.map((a) => a.command)).toEqual([
114114
'get',
@@ -122,7 +122,7 @@ test('wait absent records positionals without target-v1 annotation', () => {
122122
const store = makeStore();
123123
store.set('default', makeIosSession('default'));
124124

125-
recordIfSession(store, 'default', waitAbsentReq(), { waitedMs: 0 });
125+
recordIfSession(store, store.lookup('default'), waitAbsentReq(), { waitedMs: 0 });
126126

127127
expect(store.get('default')!.actions[0]).toMatchObject({
128128
command: 'wait',
@@ -131,3 +131,12 @@ test('wait absent records positionals without target-v1 annotation', () => {
131131
});
132132
expect(store.get('default')!.actions[0]?.targetEvidence).toBeUndefined();
133133
});
134+
135+
test('sessionless recording cannot acquire a newly published address', () => {
136+
const store = makeStore();
137+
const admitted = store.lookup('default');
138+
const session = makeIosSession('default');
139+
store.publish('default', session);
140+
recordIfSession(store, admitted, req('wait'), { waitedMs: 100 });
141+
expect(session.actions).toEqual([]);
142+
});

0 commit comments

Comments
 (0)