Skip to content

Commit 872985b

Browse files
thymikeeclaude
andcommitted
test(logs): settle a real escaped descendant through the managed app-log host
Drive the escaped-descendant termination test through the real runCmdBackground with a shell whose background loop keeps the inherited pipes open, so it exercises Node's close on stream release. Name the simulator gate after every Apple simulator in the docs and error text. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent edb3480 commit 872985b

3 files changed

Lines changed: 25 additions & 37 deletions

File tree

‎packages/platform-apple/src/logs/start.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ async function waitForSimulatorStreamLaunch(
245245
if ((await Promise.race([launched, timedOut, aborted])) === 'timed-out') {
246246
throw new AppError(
247247
'COMMAND_FAILED',
248-
`iOS simulator log stream did not start within ${SIMULATOR_STREAM_LAUNCH_TIMEOUT_MS}ms`,
248+
`Simulator log stream did not start within ${SIMULATOR_STREAM_LAUNCH_TIMEOUT_MS}ms`,
249249
{
250250
reason: 'simulator-log-stream-launch-timeout',
251251
timeoutMs: SIMULATOR_STREAM_LAUNCH_TIMEOUT_MS,

‎src/platform-runtime-app-log-process.test.ts‎

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -182,52 +182,40 @@ describe('managed app-log process host', () => {
182182
});
183183

184184
test('terminate settles when an escaped descendant keeps the output pipes open', async () => {
185+
const { runCmdBackground } = await vi.importActual<
186+
typeof import('@agent-device/host-kit/command')
187+
>('@agent-device/host-kit/command');
185188
const fixture = processFixture();
186-
const events: string[] = [];
187-
const child = Object.assign(new EventEmitter(), {
188-
pid: 99,
189-
exitCode: null,
190-
stdout: new PassThrough(),
191-
stderr: new PassThrough(),
192-
kill: vi.fn((signal?: NodeJS.Signals | number) => {
193-
events.push(String(signal));
194-
child.emit('exit', null, signal);
195-
return true;
196-
}),
189+
let launched: ReturnType<typeof runCmdBackground> | undefined;
190+
exec.run.mockImplementation((...args: Parameters<typeof runCmdBackground>) => {
191+
launched = runCmdBackground(...args);
192+
return launched;
197193
});
198-
// Like a Node child process, close needs the exit and both output pipes closed.
199-
const wait = new Promise<{ stdout: string; stderr: string; exitCode: number }>((resolve) => {
200-
let exited = false;
201-
let open = 2;
202-
const close = () => {
203-
if (exited && open === 0) resolve({ stdout: '', stderr: '', exitCode: 1 });
204-
};
205-
child.once('exit', () => {
206-
exited = true;
207-
close();
208-
});
209-
for (const stream of [child.stdout, child.stderr]) {
210-
stream.on('close', () => {
211-
events.push('pipe-closed');
212-
open -= 1;
213-
close();
214-
});
215-
}
194+
// SIGINT reaches only the direct child; the background loop keeps the inherited pipes open,
195+
// like a log process that simctl launched inside the simulator.
196+
let ticks = 0;
197+
const running = await createManagedAppLogProcesses(fixture.sessionsDir, process.env).start({
198+
...fixture.request,
199+
output: { ...fixture.request.output, write: async () => void (ticks += 1) },
200+
command: {
201+
kind: 'host',
202+
request: {
203+
executable: 'sh',
204+
args: ['-c', '(while echo tick; do sleep 0.1; done) & exec sleep 30'],
205+
allowFailure: true,
206+
},
207+
},
216208
});
217-
exec.run.mockReturnValue({ child, wait });
218-
const running = await createManagedAppLogProcesses(fixture.sessionsDir, process.env).start(
219-
fixture.request,
220-
);
209+
await vi.waitFor(() => expect(ticks).toBeGreaterThan(0));
221210
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
222211
try {
223212
let terminated = false;
224213
void running.terminate().then(() => (terminated = true));
214+
await vi.waitFor(() => expect(launched?.child.signalCode).toBe('SIGINT'));
225215

226216
await vi.advanceTimersByTimeAsync(4_000);
227217

228218
await vi.waitFor(() => expect(terminated).toBe(true));
229-
expect(events[0]).toBe('SIGINT');
230-
expect(events.slice(-2)).toEqual(['pipe-closed', 'pipe-closed']);
231219
await expect(running.wait).resolves.toMatchObject({ exitCode: 1 });
232220
expect(fs.existsSync(fixture.markerPath)).toBe(false);
233221
} finally {

‎website/docs/docs/commands.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,7 @@ agent-device network dump 25 --include headers --platform web # Browser requests
10621062
```
10631063
10641064
- `logs` works on iOS simulators, iOS physical devices, Android, and macOS (app-scoped Unified Logging). On iOS, capture relies on Unified Logging (`os_log`); plain stdout/stderr may be limited.
1065-
- `logs start` appends to `app.log` and rotates at 5 MB. Override with `AGENT_DEVICE_APP_LOG_MAX_BYTES` and `AGENT_DEVICE_APP_LOG_MAX_FILES`; set `AGENT_DEVICE_APP_LOG_REDACT_PATTERNS` to a comma-separated regex list for write-time redaction. On iOS simulators, `logs start` returns once the simulator log stream is running, so lines that the app logs after that are captured. If the stream does not start within 10 s, `logs start` fails with `details.reason: "simulator-log-stream-launch-timeout"`.
1065+
- `logs start` appends to `app.log` and rotates at 5 MB. Override with `AGENT_DEVICE_APP_LOG_MAX_BYTES` and `AGENT_DEVICE_APP_LOG_MAX_FILES`; set `AGENT_DEVICE_APP_LOG_REDACT_PATTERNS` to a comma-separated regex list for write-time redaction. On Apple simulators, `logs start` returns once the simulator log stream is running, so lines that the app logs after that are captured. If the stream does not start within 10 s, `logs start` fails with `details.reason: "simulator-log-stream-launch-timeout"`.
10661066
- `open` prints `Session state: <path>`; see [Sessions](/docs/sessions#find-a-sessions-logs-and-artifacts) for the files it contains.
10671067
- `events` reads the session timeline, which leaves out user-entered content; `--no-record` suppresses `action.recorded` entries. `events.ndjson` rotates at 5 MB (`AGENT_DEVICE_EVENT_LOG_MAX_BYTES`) and keeps one generation. Cursors stay valid across rotation; a cursor older than the retained window fails with `details.reason: "EVENT_LOG_CURSOR_EXPIRED"` and `details.earliestCursor`. If the retained files were edited or deleted, `events` fails with `details.reason: "EVENT_LOG_WINDOW_UNVERIFIED"`.
10681068
- `network dump [limit] [summary|headers|body|all]` parses recent HTTP(s) entries from `app.log`, or from managed `agent-browser` request history on web; `network log` is an alias. Prefer `--include` over the positional detail level. It scans up to 4000 log lines, returns up to 200 entries, and truncates fields at 2048 characters. Web does not expose bodies.

0 commit comments

Comments
 (0)