Skip to content

Commit f90a483

Browse files
fix(recording): preserve explicit macOS session surfaces
1 parent 2212eac commit f90a483

5 files changed

Lines changed: 62 additions & 3 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import assert from 'node:assert/strict';
2+
import { test } from 'vitest';
3+
import { AppError } from '@agent-device/kernel/errors';
4+
import { parseReplayOpenFlags } from '../open-script.ts';
5+
6+
test.each([
7+
{ args: ['--surface'] },
8+
{ args: ['--surface', 'unknown'] },
9+
{ args: ['--surface', '--relaunch'] },
10+
])('open replay refuses malformed surface arguments $args', ({ args }) => {
11+
assert.throws(
12+
() => parseReplayOpenFlags(args),
13+
(error: unknown) => error instanceof AppError && error.code === 'INVALID_ARGS',
14+
);
15+
});

‎packages/ad-script/src/internal/open-script.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { SessionAction } from '@agent-device/contracts/session';
2+
import { parseSessionSurface } from '@agent-device/contracts/session';
23
import {
34
appendRuntimeHintFlags,
45
formatScriptArg,
@@ -46,6 +47,9 @@ export function appendOpenActionScriptArgs(
4647
if (action.flags?.relaunch) {
4748
parts.push('--relaunch');
4849
}
50+
if (action.flags?.surface !== undefined) {
51+
parts.push('--surface', action.flags.surface);
52+
}
4953
if (action.flags?.testIme === true) {
5054
parts.push('--test-ime');
5155
} else if (action.flags?.testIme === false) {
@@ -61,7 +65,13 @@ export function parseReplayOpenFlags(args: string[]): {
6165
} {
6266
const argsWithoutRelaunch: string[] = [];
6367
const flags: SessionAction['flags'] = {};
64-
for (const token of args) {
68+
for (let index = 0; index < args.length; index += 1) {
69+
const token = args[index]!;
70+
if (token === '--surface') {
71+
flags.surface = parseSessionSurface(args[index + 1]);
72+
index += 1;
73+
continue;
74+
}
6575
if (token === '--relaunch') {
6676
flags.relaunch = true;
6777
continue;

‎packages/command-registry/src/flag-definitions-target.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export const TARGET_FLAG_DEFINITIONS: readonly FlagDefinition[] = [
6969
usageLabel: '--surface app|frontmost-app|desktop|menubar',
7070
usageDescription: 'macOS session surface for open (defaults to app)',
7171
projectConfig: true,
72-
recorded: false,
72+
recorded: true,
7373
},
7474
{
7575
key: 'headless',

‎src/daemon/__tests__/session-store.test.ts‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import { buildRequestFinishedEvent } from '@agent-device/session-journal/session
99
import { HEAL_COMPLETE_SENTINEL } from '../session-script-writer.ts';
1010
import { parseReplayScriptDetailed } from '@agent-device/ad-script';
1111
import type { TargetAnnotationV1 } from '@agent-device/contracts/replay';
12-
import { repairPublication } from '../../__tests__/test-utils/session-factories.ts';
12+
import {
13+
makeMacOsSession,
14+
repairPublication,
15+
} from '../../__tests__/test-utils/session-factories.ts';
1316

1417
import { mkdtempForTestSync } from '../../__tests__/test-utils/tmp-dir.ts';
1518

@@ -187,6 +190,36 @@ test('recordAction writes a paged session event log', async () => {
187190
assert.equal(secondPage.nextCursor, undefined);
188191
});
189192

193+
test.each(['app', 'frontmost-app', 'desktop', 'menubar', undefined] as const)(
194+
'recordAction preserves an explicit macOS surface %s in events and saved open scripts',
195+
async (surface) => {
196+
const fixture = makeFixture('agent-device-session-macos-surface-');
197+
fixture.session = makeMacOsSession('default');
198+
recordOpen(fixture.store, fixture.session, {
199+
platform: 'macos',
200+
saveScript: true,
201+
...(surface === undefined ? {} : { surface }),
202+
});
203+
await fixture.store.flushEvents(fixture.session.name);
204+
205+
const event = fixture.store.readEvents(fixture.session.name).events[0];
206+
assert.equal(event?.kind, 'action.recorded');
207+
assert.deepEqual(event?.details?.flags, {
208+
platform: 'macos',
209+
...(surface === undefined ? {} : { surface }),
210+
});
211+
212+
recordClose(fixture.store, fixture.session);
213+
const script = writeScript(fixture);
214+
const open = parseReplayScriptDetailed(script).actions.find(
215+
(action) => action.command === 'open',
216+
);
217+
assert.equal(open?.flags.surface, surface);
218+
assert.deepEqual(open?.positionals, ['Settings']);
219+
assert.equal(script.includes('--surface'), surface !== undefined);
220+
},
221+
);
222+
190223
test('recordAction event log redacts typed text and its length from display positionals', async () => {
191224
const { store, session } = makeFixture('agent-device-session-events-redaction-');
192225
store.recordAction(storeSessionForTest(store, session), {

‎website/docs/docs/sessions.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Session artifact directories contain per-run evidence for concurrent agents:
3030
`events.ndjson` is privacy-shaped for automation timelines. It preserves command names, status,
3131
durations, bounded device/app inventory previews, lifecycle outcomes, artifact basenames, and
3232
structural action details such as scroll distance/direction, safe refs, and coordinates.
33+
An explicitly supplied macOS `open --surface` is preserved in recorded action flags and saved `.ad` scripts; an omitted surface stays omitted.
3334
User-entered text, clipboard contents, push/event payloads, selector values, free-form
3435
flags/messages/paths, and raw unknown command arguments are omitted or replaced with content-free
3536
placeholders. `--no-record` suppresses recorded action entries; request start/finish entries still

0 commit comments

Comments
 (0)