From 06568d1d4612abd7e619d6e2f65a1776905db90a Mon Sep 17 00:00:00 2001 From: xiatiandeairen Date: Wed, 18 Nov 2026 16:03:14 +0400 Subject: [PATCH] fix(logs): disclose the physical iOS device app relaunch on logs start --- packages/capture-kit/src/app-log-runtime.ts | 7 +++- .../src/observability/output.ts | 2 + .../src/observability/index.ts | 2 +- packages/contracts/src/app-log-runtime.ts | 2 + .../internal/__tests__/session-logs.test.ts | 19 ++++++++- .../internal/session-observability.ts | 5 ++- .../platform-apple/src/logs/start.test.ts | 41 +++++++++++++++++++ packages/platform-apple/src/logs/start.ts | 3 ++ src/__tests__/cli-help.test.ts | 10 +++++ src/__tests__/cli-logs.test.ts | 11 +++++ .../schema/cli-help-workflows.test.ts | 6 ++- src/commands/schema/cli-help-workflows.ts | 2 +- .../live-observability-scenario.ts | 1 + .../ios-simulator-e2e/live-full-scenarios.ts | 1 + website/docs/docs/commands.md | 1 + 15 files changed, 107 insertions(+), 6 deletions(-) diff --git a/packages/capture-kit/src/app-log-runtime.ts b/packages/capture-kit/src/app-log-runtime.ts index 0ef38dc441..27a95e7687 100644 --- a/packages/capture-kit/src/app-log-runtime.ts +++ b/packages/capture-kit/src/app-log-runtime.ts @@ -19,8 +19,13 @@ const APP_LOG_RESOURCE_KIND = 'app-log' as const; export function createAppLogStartResult( handle: AppLogLiveHandle, envelope: DurableResourceEnvelope, + options: Readonly<{ relaunched?: boolean }> = {}, ): AppLogStartResult { - return Object.freeze({ pendingHandle: new PendingTransferGuard(handle), envelope }); + return Object.freeze({ + pendingHandle: new PendingTransferGuard(handle), + envelope, + ...(options.relaunched ? { relaunched: true as const } : {}), + }); } type AppLogRecoveryContext = Readonly<{ diff --git a/packages/command-output/src/observability/output.ts b/packages/command-output/src/observability/output.ts index af95fef28b..e56267a674 100644 --- a/packages/command-output/src/observability/output.ts +++ b/packages/command-output/src/observability/output.ts @@ -10,6 +10,7 @@ type LogsActionFields = { marked?: true; cleared?: true; restarted?: true; + relaunched?: true; removedRotatedFiles?: number; }; @@ -48,6 +49,7 @@ const LOG_ACTION_FIELD_KEYS = [ 'marked', 'cleared', 'restarted', + 'relaunched', 'removedRotatedFiles', ] as const satisfies readonly (keyof LogsActionFields)[]; diff --git a/packages/command-surface/src/observability/index.ts b/packages/command-surface/src/observability/index.ts index fa6f6e6742..afb9ba999e 100644 --- a/packages/command-surface/src/observability/index.ts +++ b/packages/command-surface/src/observability/index.ts @@ -31,7 +31,7 @@ const AUDIO_ACTION_VALUES = ['probe'] as const; const AUDIO_PROBE_ACTION_VALUES = ['start', 'status', 'stop'] as const; const logsCommandDescription = - 'Session app log info, start/stop streaming, diagnostics, and markers'; + 'Session app log info, start/stop streaming, diagnostics, and markers. On iOS physical devices, starting the stream relaunches the session app and reports relaunched: true, and stopping the stream ends the app.'; const eventsCommandDescription = 'Read the daemon-owned session event timeline as paged JSON-friendly entries'; const networkCommandDescription = 'Dump recent HTTP(s) traffic parsed from the session app log'; diff --git a/packages/contracts/src/app-log-runtime.ts b/packages/contracts/src/app-log-runtime.ts index 4e0a2e56a0..d904514d0a 100644 --- a/packages/contracts/src/app-log-runtime.ts +++ b/packages/contracts/src/app-log-runtime.ts @@ -68,6 +68,8 @@ export type AppLogStartInput = Readonly<{ export type AppLogStartResult = Readonly<{ pendingHandle: PendingTransferGuard; envelope: DurableResourceEnvelope; + /** Set when the capture relaunched the session app to attach to it; stopping it ends the app. */ + relaunched?: true; }>; type AppLogReattachInput = Readonly<{ diff --git a/packages/daemon/src/sessions/session-observability/internal/__tests__/session-logs.test.ts b/packages/daemon/src/sessions/session-observability/internal/__tests__/session-logs.test.ts index 0c7acec797..6bf7bbc0df 100644 --- a/packages/daemon/src/sessions/session-observability/internal/__tests__/session-logs.test.ts +++ b/packages/daemon/src/sessions/session-observability/internal/__tests__/session-logs.test.ts @@ -245,6 +245,22 @@ test('logs clear --restart finishes generation one before adopting generation tw }); }); +test.each([ + { relaunched: true, expected: { relaunched: true } }, + { relaunched: false, expected: {} }, +])( + 'logs start reports relaunched only when the start relaunched the app ($relaunched)', + async ({ relaunched, expected }) => { + runtime = createRuntimeHarness({ relaunched }); + const { sessionStore, sessionName } = openSession(); + const started = await runLogs(sessionStore, sessionName, ['start'], {}, runtime.bindDevice); + expect(started).toEqual({ + ok: true, + data: { path: expect.any(String), started: true, ...expected }, + }); + }, +); + test('cancellation after native start persists recovery truth and cleans the pending handle', async () => { const { sessionStore, sessionName } = openSession(); const response = await runLogs( @@ -355,6 +371,7 @@ function createRuntimeHarness( options: { inspectAvailable?: boolean; inspectFact?: RuntimeOperationFact; + relaunched?: boolean; } = {}, ) { const owner = localRuntimeOwner('apple'); @@ -394,7 +411,7 @@ function createRuntimeHarness( lifecycle: 'open', descriptor: { version: 1, body: { outputPath: input.outputPath } }, }); - return createAppLogStartResult(handle, envelope); + return createAppLogStartResult(handle, envelope, { relaunched: options.relaunched }); }); const operations: DeviceBinding['operations'] = { appLogInspect: inspect, diff --git a/packages/daemon/src/sessions/session-observability/internal/session-observability.ts b/packages/daemon/src/sessions/session-observability/internal/session-observability.ts index d1f85c6e73..d1ec5e460e 100644 --- a/packages/daemon/src/sessions/session-observability/internal/session-observability.ts +++ b/packages/daemon/src/sessions/session-observability/internal/session-observability.ts @@ -388,7 +388,10 @@ async function startSessionAppLog( envelope: result.envelope, throwIfCanceled: params.throwIfCanceled ?? (() => {}), }); - return { ok: true, data: { path: outputPath, started: true } }; + return { + ok: true, + data: { path: outputPath, started: true, ...(result.relaunched ? { relaunched: true } : {}) }, + }; } catch (error) { const normalized = recordSessionAppLogFailure({ ref: params.ref, diff --git a/packages/platform-apple/src/logs/start.test.ts b/packages/platform-apple/src/logs/start.test.ts index 35f2074a78..b137ffc091 100644 --- a/packages/platform-apple/src/logs/start.test.ts +++ b/packages/platform-apple/src/logs/start.test.ts @@ -202,6 +202,47 @@ test('simulator start cancelled before the stream launches terminates the proces expect(fixture.outputDisposed()).toBe(true); }); +test.each([ + { + name: 'a CoreDevice physical iOS device', + device: appleDevice({ kind: 'device', iosPhysicalDeviceBackend: 'coredevice' }), + relaunched: true, + }, + { name: 'a host macOS target', device: appleDevice({ appleOs: 'macos', kind: 'device' }) }, +])('start on $name reports relaunched only for devicectl console capture', async (scenario) => { + const fixture = hostFixture({ + appleToolRun: async () => ({ + stdout: 'USAGE: devicectl device process launch --console --terminate-existing', + stderr: '', + exitCode: 0, + }), + }); + + const started = await startAppleAppLogs( + fixture.host, + scenario.device, + startInput(), + localRuntimeOwner('apple'), + ); + + expect(started.relaunched).toBe(scenario.relaunched); + if (scenario.relaunched) expect(fixture.backgroundCommands[0]).toContain('--terminate-existing'); +}); + +test('simulator start does not report relaunched', async () => { + const stream = launchingStream({ exited: true }); + const fixture = hostFixture({ startProcess: stream.start, sleep: sleepUntilAborted }); + + const started = await startAppleAppLogs( + fixture.host, + appleDevice(), + startInput(), + localRuntimeOwner('apple'), + ); + + expect(started.relaunched).toBeUndefined(); +}); + function startInput() { return { sessionId: 'session', diff --git a/packages/platform-apple/src/logs/start.ts b/packages/platform-apple/src/logs/start.ts index 212cff6682..7fc1fda44c 100644 --- a/packages/platform-apple/src/logs/start.ts +++ b/packages/platform-apple/src/logs/start.ts @@ -91,6 +91,9 @@ export async function startAppleAppLogs( fence: input.fence, descriptor, }), + // devicectl --console --terminate-existing relaunches the app, and devicectl forwards the + // stop signal to the app, which ends it. + { relaunched: backend === 'ios-device' }, ); adopted = true; return result; diff --git a/src/__tests__/cli-help.test.ts b/src/__tests__/cli-help.test.ts index fcd08b0faf..f27ff5a6e4 100644 --- a/src/__tests__/cli-help.test.ts +++ b/src/__tests__/cli-help.test.ts @@ -42,6 +42,16 @@ test('appstate --help prints command help and skips daemon dispatch', async () = assert.doesNotMatch(result.stdout, /Global Flags:/); }); +test('help logs discloses the physical iOS device relaunch on start and app end on stop', async () => { + const result = await runCliCapture(['help', 'logs']); + assert.equal(result.code, 0); + assert.equal(result.calls.length, 0); + assert.match( + result.stdout, + /On iOS physical devices, starting the stream relaunches the session app and reports relaunched: true, and stopping the stream ends the app\./, + ); +}); + test('prepare help documents iOS runner CI setup', async () => { const result = await runCliCapture(['help', 'prepare']); assert.equal(result.code, 0); diff --git a/src/__tests__/cli-logs.test.ts b/src/__tests__/cli-logs.test.ts index 3ec13062ce..1b05316640 100644 --- a/src/__tests__/cli-logs.test.ts +++ b/src/__tests__/cli-logs.test.ts @@ -21,3 +21,14 @@ test('logs clear prints action metadata and forwards --restart flag', async () = assert.match(result.stderr, /restarted=true/); assert.match(result.stderr, /removedRotatedFiles=2/); }); + +test('logs start prints the physical-device relaunch disclosure', async () => { + const result = await runCliCapture(['logs', 'start'], async () => ({ + ok: true, + data: { path: '/tmp/app.log', started: true, relaunched: true }, + })); + + assert.equal(result.code, null); + assert.match(result.stderr, /started=true/); + assert.match(result.stderr, /relaunched=true/); +}); diff --git a/src/commands/schema/cli-help-workflows.test.ts b/src/commands/schema/cli-help-workflows.test.ts index 926a25e8cf..9d26e81b8a 100644 --- a/src/commands/schema/cli-help-workflows.test.ts +++ b/src/commands/schema/cli-help-workflows.test.ts @@ -7,7 +7,11 @@ test('usageForCommand resolves debugging help topic', async () => { if (help === null) throw new Error('Expected debugging help text'); assert.match(help, /^agent-device \S+ — debugging/); assert.match(help, /Use logs when you need the lead-up timeline/); - assert.match(help, /relaunches the session app through devicectl process launch --console/); + assert.match( + help, + /logs start and logs clear --restart relaunch the session app through devicectl process launch --console/, + ); + assert.match(help, /logs stop ends the app/); assert.match(help, /Use debug symbols when you have crash\.ips\/crash\.log/); assert.match(help, /Use Xcode\/LLDB when you need live state/); assert.match(help, /debug symbols --artifact crash\.ips --search-path \.\/build/); diff --git a/src/commands/schema/cli-help-workflows.ts b/src/commands/schema/cli-help-workflows.ts index 0c36d11a98..38e59be6c1 100644 --- a/src/commands/schema/cli-help-workflows.ts +++ b/src/commands/schema/cli-help-workflows.ts @@ -77,7 +77,7 @@ Logs: Do not cat a full stale log into agent context. Open or grep only the relevant window when needed. logs clear --restart is the compact command to clear old logs and start a fresh capture; do not split it into logs stop, logs clear, logs start. On iOS simulators, logs scope by bundle id and resolved app executable, so use this instead of raw simctl log stream predicates. - On iOS physical devices, logs clear --restart relaunches the session app through devicectl process launch --console so stdout/stderr can be captured. + On iOS physical devices, logs start and logs clear --restart relaunch the session app through devicectl process launch --console so stdout/stderr can be captured; logs start reports relaunched: true. logs stop ends the app. For iOS simulator launch-time stdout/stderr, use --launch-console on the direct app launch: agent-device open MyApp --platform ios --relaunch --launch-console ./artifacts/app.console.log --launch-console is only for local or Limrun iOS simulator app launches, not URL opens. diff --git a/test/integration/android-emulator-e2e/live-observability-scenario.ts b/test/integration/android-emulator-e2e/live-observability-scenario.ts index 0e15f24dc6..56fd492afc 100644 --- a/test/integration/android-emulator-e2e/live-observability-scenario.ts +++ b/test/integration/android-emulator-e2e/live-observability-scenario.ts @@ -44,6 +44,7 @@ async function assertMetrics(context: LiveContext): Promise { async function assertLogs(context: LiveContext): Promise { const started = await runStep(context, 'start Android fixture logcat stream', ['logs', 'start']); assert.equal(started.json?.data?.started, true, JSON.stringify(started.json)); + assert.equal(started.json?.data?.relaunched, undefined, JSON.stringify(started.json)); assert.ok(fs.existsSync(String(started.json?.data?.path)), JSON.stringify(started.json)); const inspected = await runStep(context, 'inspect Android fixture logcat stream', [ 'logs', diff --git a/test/integration/ios-simulator-e2e/live-full-scenarios.ts b/test/integration/ios-simulator-e2e/live-full-scenarios.ts index e75dffd63e..86ace55e16 100644 --- a/test/integration/ios-simulator-e2e/live-full-scenarios.ts +++ b/test/integration/ios-simulator-e2e/live-full-scenarios.ts @@ -254,6 +254,7 @@ export async function assertObservabilityAndArtifacts(context: LiveContext): Pro const logsStart = await runStep(context, 'start fixture log stream', ['logs', 'start']); assert.equal(logsStart.json?.data?.started, true, JSON.stringify(logsStart.json)); + assert.equal(logsStart.json?.data?.relaunched, undefined, JSON.stringify(logsStart.json)); assert.ok(fs.existsSync(String(logsStart.json?.data?.path)), JSON.stringify(logsStart.json)); const logsPath = await runStep(context, 'inspect fixture log stream', ['logs', 'path']); assert.equal(logsPath.json?.data?.active, true, JSON.stringify(logsPath.json)); diff --git a/website/docs/docs/commands.md b/website/docs/docs/commands.md index 076d8bc389..02d06de736 100644 --- a/website/docs/docs/commands.md +++ b/website/docs/docs/commands.md @@ -1153,6 +1153,7 @@ agent-device network dump 25 --include headers --platform web # Browser requests ``` - `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. +- On iOS physical devices, `logs start` relaunches the session app through `devicectl device process launch --console --terminate-existing` and reports `relaunched: true`; `logs stop` ends the app. Run `open ` again after `logs stop` to keep using it. Other targets do not report `relaunched`. - `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. Commas inside a `{m,n}` quantifier, `[...]`, or `(...)` stay in their pattern; write `\,` for any other literal comma. If a pattern does not compile, `logs start` fails with `INVALID_ARGS`, `details.reason: "app_log_redact_pattern_invalid"`, and the zero-based `details.patternIndex` of that comma-separated field (empty fields count); the pattern text is not echoed. 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"`. - `open` prints `Session state: `; see [Sessions](/docs/sessions#find-a-sessions-logs-and-artifacts) for the files it contains. - `events` reads the session timeline, which leaves out user-entered content; `--no-record` suppresses `action.recorded` entries. `events.ndjson` rotates before an entry would exceed 5 MiB (`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"`.