Skip to content

Commit bc4b9ce

Browse files
committed
fix(daemon): stop cwd-scoped session resources by store address on close
stopBestEffortSessionResources addressed the app-log, audio-probe, perf-capture and recording records by session.name. For an implicit session that is "default" while the record lives under the store address "cwd:<hash>:default" (SessionRef), so close reported "App-log resource record is missing", left the record open/active and leaked the simctl log stream child; the next logs start on the device then failed with "has not reached a confirmed terminal state". Thread the address the caller already holds into the helper. Adds a regression test that reproduces the missing-record error without the fix. Closes #2647
1 parent fffa01e commit bc4b9ce

3 files changed

Lines changed: 123 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Unreleased
44

5+
- Fixed (daemon): `close` now stops an active app-log stream (and audio probe / perf capture /
6+
recording) on an implicitly cwd-scoped session. Teardown addressed those resources by
7+
`session.name` (`default`) instead of the store address (`cwd:<hash>:default`), so the record
8+
read as missing, the `log stream` child leaked, and the next `logs start` on that device failed
9+
with "has not reached a confirmed terminal state" (#2647).
510
- Added (limrun): `longpress` on Limrun iOS direct sessions. The interactor refused it as
611
unsupported although the SDK exposes the HID primitives; it now holds one touch as a
712
`performActions` batch of `touchDown`, `wait`, `touchUp`, defaulting to the 800 ms the Android
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { beforeEach, expect, test, vi } from 'vitest';
2+
import type { AppLogLiveHandle } from '@agent-device/contracts/app-log-runtime';
3+
import { createDurableResourceEnvelope } from '@agent-device/capture-kit';
4+
import { appLogResourceStore } from '../../../app-log-resource-store.ts';
5+
import {
6+
sessionCloseShutdownFixture,
7+
type SessionState,
8+
} from './session-close-shutdown.fixtures.ts';
9+
import { mkdtempForTestSync } from '../../../../__tests__/test-utils/tmp-dir.ts';
10+
11+
const {
12+
handleSessionCommands,
13+
localRuntimeOwner,
14+
makeSession,
15+
makeSessionStore,
16+
noopInvoke,
17+
path,
18+
resetSessionCloseShutdownMocks,
19+
} = sessionCloseShutdownFixture;
20+
21+
beforeEach(resetSessionCloseShutdownMocks);
22+
23+
// An implicitly cwd-scoped session is *named* `default` but *stored* under
24+
// `cwd:<hash>:default` (see `SessionRef`). Its durable app-log record lives in
25+
// the address's directory, so teardown must address it by `sessionName`, not
26+
// `session.name`. When it used `session.name`, `close` reported
27+
// "App-log resource record is missing", left the record `open`/`active`, and
28+
// leaked the `log stream` child; the next `logs start` on that device then
29+
// failed with "has not reached a confirmed terminal state".
30+
test('close stops the app log of a cwd-scoped session whose name differs from its store address', async () => {
31+
const sessionStore = makeSessionStore();
32+
const sessionAddress = 'cwd:0f803c4542a46e92:default';
33+
const sessionName = 'default';
34+
const device: SessionState['device'] = {
35+
platform: 'apple',
36+
appleOs: 'ios',
37+
id: '9105AAA0-3184-40BC-A9FC-46634C90DFFB',
38+
name: 'iPhone 16',
39+
kind: 'simulator',
40+
booted: true,
41+
};
42+
const sessionDir = sessionStore.resolveSessionDir(sessionAddress);
43+
const outputPath = path.join(sessionDir, 'app.log');
44+
const forceCleanup = vi.fn(async () => ({ status: 'cleaned' }) as const);
45+
const handle: AppLogLiveHandle = {
46+
inspect: () => ({ backend: 'ios-simulator', state: 'active', startedAt: Date.now() - 1000 }),
47+
finish: async () => ({
48+
status: 'completed' as const,
49+
result: { backend: 'ios-simulator', outputPath, completedAt: Date.now() },
50+
}),
51+
forceCleanup,
52+
[Symbol.asyncDispose]: async () => {},
53+
};
54+
const envelope = createDurableResourceEnvelope({
55+
resourceKind: 'app-log',
56+
sessionId: sessionAddress,
57+
device: { id: device.id, family: 'apple', appleOs: 'ios', kind: 'simulator', target: 'mobile' },
58+
owner: localRuntimeOwner('apple'),
59+
fence: { token: 'app-log-fence', generation: 1 },
60+
lifecycle: 'open',
61+
descriptor: {
62+
version: 1,
63+
body: {
64+
transport: 'apple-log-stream',
65+
backend: 'ios-simulator',
66+
outputPath,
67+
pidPath: path.join(sessionDir, 'app-log.pid'),
68+
},
69+
},
70+
metadata: { phase: 'active' },
71+
});
72+
const session: SessionState = {
73+
...makeSession(sessionName, device),
74+
appBundleId: 'com.apple.Preferences',
75+
appLog: { handle, envelope },
76+
};
77+
sessionStore.set(sessionAddress, session);
78+
const resourcePath = appLogResourceStore.resolvePath(sessionDir);
79+
appLogResourceStore.write(resourcePath, envelope);
80+
// The record must NOT exist under the bare name: that directory is a different session.
81+
expect(
82+
appLogResourceStore.read(
83+
appLogResourceStore.resolvePath(sessionStore.resolveSessionDir(sessionName)),
84+
).status,
85+
).toBe('missing');
86+
87+
const response = await handleSessionCommands({
88+
req: {
89+
token: 't',
90+
session: sessionAddress,
91+
command: 'close',
92+
positionals: [],
93+
flags: {},
94+
},
95+
sessionName: sessionAddress,
96+
logPath: path.join(mkdtempForTestSync('daemon'), 'daemon.log'),
97+
sessionStore,
98+
invoke: noopInvoke,
99+
});
100+
101+
expect(response?.ok).toBe(true);
102+
expect(forceCleanup).toHaveBeenCalledOnce();
103+
const record = appLogResourceStore.read(resourcePath);
104+
expect(record.status).toBe('decoded');
105+
if (record.status === 'decoded') expect(record.envelope.lifecycle).toBe('completed');
106+
expect(sessionStore.get(sessionAddress)).toBeUndefined();
107+
});

‎src/daemon/session-lifecycle/internal/session-close-lifecycle-teardown.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export async function runSessionCloseTeardown(params: {
8787
const configuredRuntimeHints = sessionStore.getRuntimeHints(sessionName);
8888
await stopBestEffortSessionResources(
8989
session,
90+
sessionName,
9091
sessionStore,
9192
attemptCleanup,
9293
params.platformResourceCleanup,
@@ -128,31 +129,36 @@ export async function runSessionCloseTeardown(params: {
128129

129130
type CleanupRunner = (step: string, run: () => Promise<void>) => Promise<void>;
130131

132+
// `sessionName` is the store address (`cwd:<hash>:default` for an implicit
133+
// session), not `session.name` (`default`). Every durable-capture record lives
134+
// under the address's directory, so addressing it by `session.name` reports the
135+
// app-log record as missing and leaks the `log stream` child (see `SessionRef`).
131136
async function stopBestEffortSessionResources(
132137
session: SessionState,
138+
sessionName: string,
133139
sessionStore: SessionStore,
134140
attemptCleanup: CleanupRunner,
135141
platformCleanup: PlatformResourceCleanup,
136142
): Promise<void> {
137143
// Recording overlay finalization needs the Apple runner.
138-
const currentSession = sessionStore.get(session.name) ?? session;
144+
const currentSession = sessionStore.get(sessionName) ?? session;
139145
if (currentSession.screenRecording) {
140146
await attemptCleanup('recording', () =>
141147
finishSessionScreenRecording({
142148
session: currentSession,
143-
sessionName: session.name,
149+
sessionName,
144150
sessionStore,
145151
}),
146152
);
147153
}
148154
await attemptCleanup('app_log', () =>
149-
stopSessionAppLog({ session, sessionName: session.name, sessionStore }),
155+
stopSessionAppLog({ session, sessionName, sessionStore }),
150156
);
151157
await attemptCleanup('audio_probe', () =>
152-
finishSessionAudioProbe({ session, sessionName: session.name, sessionStore }),
158+
finishSessionAudioProbe({ session, sessionName, sessionStore }),
153159
);
154160
await attemptCleanup('perf_capture', () =>
155-
stopSessionPerfCapture({ session, sessionName: session.name, sessionStore }),
161+
stopSessionPerfCapture({ session, sessionName, sessionStore }),
156162
);
157163
await attemptCleanup('platform_snapshot_helper', () =>
158164
stopSessionSnapshotHelper(session, platformCleanup),

0 commit comments

Comments
 (0)