xcodebug 1.2.0 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,321 @@
1
+ import { expect, mock, test } from 'claude-code/testing'
2
+
3
+ const BAND = {
4
+ plugin: 'xcodebug-live', component: 'AbovePrompt', surface: 'terminal',
5
+ viewport: { columns: 120, rows: 40 }, props: {},
6
+ } as const
7
+
8
+ const TOOL = 'mcp__xcodebug__xcodebug'
9
+ const LAUNCH = { tool: TOOL, action: 'launch' } as const
10
+ const QUIET_ACTIONS = ['doctor', 'devices', 'changes', 'status', 'logs', 'wait', 'stop', 'terminate']
11
+ const START = { surface: 'terminal', isInteractive: true, cwd: '/work' } as const
12
+
13
+ const reply = (snapshot: unknown) => ({ value: { content: [{ type: 'text', text: JSON.stringify(snapshot) }] } })
14
+ const failure = { value: { isError: true, content: [{ type: 'text', text: 'server unavailable' }] } }
15
+
16
+ test('an idle session reads the display once, then never again', async ($, on) => {
17
+ const clock = mock.clock(on)
18
+ let reads = 0
19
+ on('session.start', () => ({ cwd: '/work' }))
20
+ on('mcp.call', () => {
21
+ reads += 1
22
+ return reply({ rows: [] })
23
+ })
24
+ await $.session.start(START)
25
+ await clock.advance(5000)
26
+ expect(reads).toBe(1)
27
+ })
28
+
29
+ test('a capture already running when the session starts is shown', async ($, on) => {
30
+ const clock = mock.clock(on)
31
+ on('session.start', () => ({ cwd: '/work' }))
32
+ on('mcp.call', () => reply({ rows: ['capturing · 9 lines', 'earlier log'] }))
33
+ await $.session.start(START)
34
+ const ui = await $.ui.mount(BAND)
35
+ await clock.advance(1000)
36
+ expect(await ui.find({ type: 'Text', text: 'earlier log' })).toBeDefined()
37
+ })
38
+
39
+ test('an xcodebug call starts the display, and an empty snapshot ends it', async ($, on) => {
40
+ const clock = mock.clock(on)
41
+ let snapshot: { rows: string[] } = { rows: [] }
42
+ let reads = 0
43
+ on('session.start', () => ({ cwd: '/work' }))
44
+ on('tool.call', () => ({ result: {} }))
45
+ on('mcp.call', ($, e) => {
46
+ expect(e.server).toBe('xcodebug')
47
+ expect(e.tool).toBe('xcodebug_ui')
48
+ reads += 1
49
+ return reply(snapshot)
50
+ })
51
+ await $.session.start(START)
52
+ await clock.advance(1000)
53
+ reads = 0
54
+
55
+ snapshot = { rows: ['capturing · 1 line', 'first log'] }
56
+ await $.tool.call(LAUNCH)
57
+ const ui = await $.ui.mount(BAND)
58
+ await clock.advance(1000)
59
+ expect(await ui.find({ type: 'Text', text: 'first log' })).toBeDefined()
60
+ snapshot = { rows: ['capturing · 2 lines', 'second log'] }
61
+ await clock.advance(1000)
62
+ expect(await ui.find({ type: 'Text', text: 'second log' })).toBeDefined()
63
+ expect(await ui.find({ type: 'Text', text: 'first log' })).toBeUndefined()
64
+ snapshot = { rows: [] }
65
+ await clock.advance(1000)
66
+ expect(await ui.find({ type: 'Text', text: /capturing/ })).toBeUndefined()
67
+ expect(reads).toBe(3)
68
+ await clock.advance(5000)
69
+ expect(reads).toBe(3)
70
+
71
+ snapshot = { rows: ['capturing · 1 line', 'relaunched'] }
72
+ await $.tool.call(LAUNCH)
73
+ await clock.advance(1000)
74
+ expect(await ui.find({ type: 'Text', text: 'relaunched' })).toBeDefined()
75
+ })
76
+
77
+ test('a call still running keeps the display reading an empty snapshot', async ($, on) => {
78
+ const clock = mock.clock(on)
79
+ let reads = 0
80
+ let finish: (result: unknown) => void = () => {}
81
+ on('session.start', () => ({ cwd: '/work' }))
82
+ on('tool.call', () => new Promise(resolve => { finish = resolve }))
83
+ on('mcp.call', () => {
84
+ reads += 1
85
+ return reply({ rows: [] })
86
+ })
87
+ await $.session.start(START)
88
+ await clock.advance(1000)
89
+ reads = 0
90
+
91
+ const call = $.tool.call(LAUNCH)
92
+ await clock.advance(4000)
93
+ expect(reads).toBe(4)
94
+ finish({ result: {} })
95
+ await call
96
+ await clock.advance(5000)
97
+ expect(reads).toBe(5)
98
+ })
99
+
100
+ test('actions that paint nothing do not start the display', async ($, on) => {
101
+ const clock = mock.clock(on)
102
+ let reads = 0
103
+ on('session.start', () => ({ cwd: '/work' }))
104
+ on('tool.call', () => ({ result: {} }))
105
+ on('mcp.call', () => {
106
+ reads += 1
107
+ return reply({ rows: [] })
108
+ })
109
+ await $.session.start(START)
110
+ await clock.advance(1000)
111
+ reads = 0
112
+
113
+ for (const action of QUIET_ACTIONS) {
114
+ await $.tool.call({ tool: TOOL, action })
115
+ await clock.advance(2000)
116
+ }
117
+ expect(reads).toBe(0)
118
+ })
119
+
120
+ test('a quiet action that carries a command shows its phase, then stops', async ($, on) => {
121
+ const clock = mock.clock(on)
122
+ let snapshot: { rows: string[] } = { rows: [] }
123
+ let reads = 0
124
+ let finish: (result: unknown) => void = () => {}
125
+ on('session.start', () => ({ cwd: '/work' }))
126
+ on('tool.call', () => new Promise(resolve => { finish = resolve }))
127
+ on('mcp.call', () => {
128
+ reads += 1
129
+ return reply(snapshot)
130
+ })
131
+ await $.session.start(START)
132
+ await clock.advance(1000)
133
+ reads = 0
134
+
135
+ snapshot = { rows: ['Xcodebug · running', ' command: sleep 3'] }
136
+ const call = $.tool.call({ tool: TOOL, action: 'changes', command: 'sleep 3' })
137
+ const ui = await $.ui.mount(BAND)
138
+ await clock.advance(1000)
139
+ expect(await ui.find({ type: 'Text', text: /command: sleep 3/ })).toBeDefined()
140
+ snapshot = { rows: [] }
141
+ finish({ result: {} })
142
+ await call
143
+ await clock.advance(1000)
144
+ expect(await ui.find({ type: 'Text', text: /command: sleep 3/ })).toBeUndefined()
145
+ expect(reads).toBe(2)
146
+ await clock.advance(5000)
147
+ expect(reads).toBe(2)
148
+ })
149
+
150
+ test('a failed UI read removes stale logs and recovers on the next timer', async ($, on) => {
151
+ const clock = mock.clock(on)
152
+ let failed = false
153
+ on('session.start', () => ({ cwd: '/work' }))
154
+ on('tool.call', () => ({ result: {} }))
155
+ on('mcp.call', () => failed ? failure : reply({ rows: ['current log'] }))
156
+ await $.session.start(START)
157
+ await $.tool.call(LAUNCH)
158
+ const ui = await $.ui.mount(BAND)
159
+ await clock.advance(1000)
160
+ expect(await ui.find({ type: 'Text', text: 'current log' })).toBeDefined()
161
+ failed = true
162
+ await clock.advance(1000)
163
+ expect(await ui.find({ type: 'Text', text: /live display unavailable/ })).toBeDefined()
164
+ expect(await ui.find({ type: 'Text', text: 'current log' })).toBeUndefined()
165
+ failed = false
166
+ await clock.advance(1000)
167
+ expect(await ui.find({ type: 'Text', text: 'current log' })).toBeDefined()
168
+ expect(await ui.find({ type: 'Text', text: /unavailable/ })).toBeUndefined()
169
+ // Failures apart from each other never add up to giving up.
170
+ failed = true
171
+ await clock.advance(2000)
172
+ failed = false
173
+ await clock.advance(1000)
174
+ expect(await ui.find({ type: 'Text', text: 'current log' })).toBeDefined()
175
+ })
176
+
177
+ test('a server that stays down is given up on, and the band clears', async ($, on) => {
178
+ const clock = mock.clock(on)
179
+ let reads = 0
180
+ on('session.start', () => ({ cwd: '/work' }))
181
+ on('tool.call', () => ({ result: {} }))
182
+ on('mcp.call', () => {
183
+ reads += 1
184
+ return failure
185
+ })
186
+ await $.session.start(START)
187
+ await $.tool.call(LAUNCH)
188
+ const ui = await $.ui.mount(BAND)
189
+ await clock.advance(1000)
190
+ expect(await ui.find({ type: 'Text', text: /live display unavailable/ })).toBeDefined()
191
+ await clock.advance(30000)
192
+ expect(reads).toBe(3)
193
+ expect(await ui.find({ type: 'Text', text: /unavailable/ })).toBeUndefined()
194
+ })
195
+
196
+ const FILE = '/agent/ui/42.json'
197
+
198
+ test('one tool call per activity: later reads come from the snapshot file', async ($, on) => {
199
+ const clock = mock.clock(on)
200
+ let snapshot = { rows: [] as string[], seq: 0 }
201
+ let toolReads = 0
202
+ let fileReads = 0
203
+ on('session.start', () => ({ cwd: '/work' }))
204
+ on('tool.call', () => ({ result: {} }))
205
+ on('mcp.call', () => {
206
+ toolReads += 1
207
+ return reply({ ...snapshot, file: FILE })
208
+ })
209
+ on('fs.read', ($, e) => {
210
+ expect(e.path).toBe(FILE)
211
+ fileReads += 1
212
+ return { value: JSON.stringify(snapshot) }
213
+ })
214
+ await $.session.start(START)
215
+ await clock.advance(1000)
216
+ toolReads = 0
217
+ fileReads = 0
218
+
219
+ const ui = await $.ui.mount(BAND)
220
+ for (const round of ['first', 'second']) {
221
+ snapshot = { rows: [`capturing · ${round}`], seq: snapshot.seq + 1 }
222
+ await $.tool.call(LAUNCH)
223
+ await clock.advance(1000)
224
+ expect(await ui.find({ type: 'Text', text: `capturing · ${round}` })).toBeDefined()
225
+ for (let tick = 0; tick < 10; tick++) {
226
+ snapshot = { rows: [`capturing · ${round} · ${tick}`], seq: snapshot.seq + 1 }
227
+ await clock.advance(1000)
228
+ expect(await ui.find({ type: 'Text', text: `capturing · ${round} · ${tick}` })).toBeDefined()
229
+ }
230
+ snapshot = { rows: [], seq: snapshot.seq + 1 }
231
+ await clock.advance(1000)
232
+ expect(await ui.find({ type: 'Text', text: /capturing/ })).toBeUndefined()
233
+ await clock.advance(5000)
234
+ }
235
+ expect(toolReads).toBe(2)
236
+ expect(fileReads).toBe(22)
237
+ })
238
+
239
+ test('a snapshot file that stops changing sends the next read to the server', async ($, on) => {
240
+ const clock = mock.clock(on)
241
+ let toolReads = 0
242
+ let down = false
243
+ on('session.start', () => ({ cwd: '/work' }))
244
+ on('tool.call', () => ({ result: {} }))
245
+ on('mcp.call', () => {
246
+ toolReads += 1
247
+ return down ? failure : reply({ rows: ['capturing · stuck'], seq: 7, file: FILE })
248
+ })
249
+ // The server died without clearing its file: same seq on every read.
250
+ on('fs.read', () => ({ value: JSON.stringify({ rows: ['capturing · stuck'], seq: 7 }) }))
251
+ await $.session.start(START)
252
+ await $.tool.call(LAUNCH)
253
+ const ui = await $.ui.mount(BAND)
254
+ await clock.advance(1000)
255
+ toolReads = 0
256
+ down = true
257
+ await clock.advance(4000)
258
+ expect(toolReads).toBe(0)
259
+ expect(await ui.find({ type: 'Text', text: 'capturing · stuck' })).toBeDefined()
260
+ await clock.advance(30000)
261
+ expect(toolReads).toBe(3)
262
+ expect(await ui.find({ type: 'Text', text: /stuck|unavailable/ })).toBeUndefined()
263
+ })
264
+
265
+ test('a snapshot file that cannot be read sends that read to the server', async ($, on) => {
266
+ const clock = mock.clock(on)
267
+ let toolReads = 0
268
+ let gone = false
269
+ on('session.start', () => ({ cwd: '/work' }))
270
+ on('tool.call', () => ({ result: {} }))
271
+ on('mcp.call', () => {
272
+ toolReads += 1
273
+ return reply({ rows: [`from the server · ${toolReads}`], seq: toolReads, file: FILE })
274
+ })
275
+ on('fs.read', () => {
276
+ if (gone) throw new Error('ENOENT')
277
+ return { value: JSON.stringify({ rows: ['from the file'], seq: 100 }) }
278
+ })
279
+ await $.session.start(START)
280
+ await $.tool.call(LAUNCH)
281
+ const ui = await $.ui.mount(BAND)
282
+ await clock.advance(2000)
283
+ expect(await ui.find({ type: 'Text', text: 'from the file' })).toBeDefined()
284
+ gone = true
285
+ toolReads = 0
286
+ await clock.advance(1000)
287
+ expect(toolReads).toBe(1)
288
+ expect(await ui.find({ type: 'Text', text: /from the server/ })).toBeDefined()
289
+ expect(await ui.find({ type: 'Text', text: /unavailable/ })).toBeUndefined()
290
+ })
291
+
292
+ test('a call while the display runs asks the server again, in case it was replaced', async ($, on) => {
293
+ const clock = mock.clock(on)
294
+ const OLD = '/agent/ui/1.json'
295
+ const NEW = '/agent/ui/2.json'
296
+ let server = { rows: ['old capture'], seq: 1, file: OLD }
297
+ let oldSeq = 1
298
+ let oldServerRuns = true
299
+ let newSeq = 1
300
+ on('session.start', () => ({ cwd: '/work' }))
301
+ on('tool.call', () => ({ result: {} }))
302
+ on('mcp.call', () => reply(server))
303
+ on('fs.read', ($, e) => ({
304
+ value: JSON.stringify(e.path === OLD
305
+ ? { rows: ['old capture'], seq: oldServerRuns ? ++oldSeq : oldSeq }
306
+ : { rows: ['new build'], seq: ++newSeq }),
307
+ }))
308
+ await $.session.start(START)
309
+ await $.tool.call(LAUNCH)
310
+ const ui = await $.ui.mount(BAND)
311
+ await clock.advance(3000)
312
+ expect(await ui.find({ type: 'Text', text: 'old capture' })).toBeDefined()
313
+
314
+ // The old server was killed and left its file; the connection now reaches a new one.
315
+ oldServerRuns = false
316
+ server = { rows: ['new build'], seq: 1, file: NEW }
317
+ await $.tool.call(LAUNCH)
318
+ await clock.advance(1000)
319
+ expect(await ui.find({ type: 'Text', text: 'new build' })).toBeDefined()
320
+ expect(await ui.find({ type: 'Text', text: 'old capture' })).toBeUndefined()
321
+ })
package/devices.ts CHANGED
@@ -4,6 +4,8 @@
4
4
  * devicectl accepts a uuid, ecid, serial number, udid, name or DNS name, so
5
5
  * every identifier we keep is accepted verbatim by later commands.
6
6
  */
7
+ import { mkdtemp, readFile, rename, rm } from "node:fs/promises";
8
+ import { basename, join } from "node:path";
7
9
  import { asRecord, asString, runCommand } from "./exec.ts";
8
10
 
9
11
  export interface DeviceInfo {
@@ -265,14 +267,83 @@ async function findProcessPidOnce(
265
267
  }
266
268
  const processes = asRecord(asRecord(parsed).result).runningProcesses;
267
269
  if (!Array.isArray(processes)) return { queryFailed: true };
270
+ // The app's own executable sits directly in its bundle. An extension runs from
271
+ // `<App>.app/PlugIns/…` as a process of its own, and taking it for the app
272
+ // would report a pid that is not the app's and an app that is not there.
268
273
  const needle = `/${executableName}.app/`;
269
274
  for (const raw of processes) {
270
275
  const entry = asRecord(raw);
271
276
  const executable = asString(entry.executable) ?? "";
272
- if (executable.includes(needle) && typeof entry.processIdentifier === "number") {
273
- return { pid: entry.processIdentifier, queryFailed: false };
274
- }
277
+ const at = executable.lastIndexOf(needle);
278
+ if (at < 0 || executable.indexOf("/", at + needle.length) >= 0) continue;
279
+ if (typeof entry.processIdentifier === "number") return { pid: entry.processIdentifier, queryFailed: false };
275
280
  }
276
281
  // The table was read successfully and the app is not in it: a real exit.
277
282
  return { queryFailed: false };
278
283
  }
284
+
285
+ /**
286
+ * Copy the crash report the device wrote for one instance of an app into
287
+ * `directory`, and return where it landed.
288
+ *
289
+ * Crash reports are named `<executable>-<date>.ips`. The resource diagnostics
290
+ * beside them (`<executable>.cpu_resource-…`) share the prefix up to the dot and
291
+ * are not crashes. A report names the process it is about, so with `pid` only
292
+ * that instance's report is returned: the next launch of the same app, crashing
293
+ * in turn, writes one that is not this instance's. Without `pid` the newest
294
+ * report since `since` is taken.
295
+ */
296
+ export async function pullCrashReport(
297
+ device: DeviceInfo,
298
+ executableName: string,
299
+ options: { since: number; directory: string; pid?: number },
300
+ ): Promise<string | undefined> {
301
+ const listed = await runCommand(
302
+ "xcrun",
303
+ [
304
+ "devicectl", "device", "info", "files", "--device", device.id,
305
+ "--domain-type", "systemCrashLogs", "--search", executableName, "--json-output", "-",
306
+ ],
307
+ { timeoutMs: 45_000 },
308
+ );
309
+ if (listed.code !== 0) return undefined;
310
+ let files: unknown;
311
+ try {
312
+ files = asRecord(asRecord(JSON.parse(listed.stdout)).result).files;
313
+ } catch {
314
+ return undefined;
315
+ }
316
+ if (!Array.isArray(files)) return undefined;
317
+ const candidates: Array<{ name: string; at: number }> = [];
318
+ for (const raw of files) {
319
+ const entry = asRecord(raw);
320
+ const name = asString(entry.name);
321
+ const at = Date.parse(asString(asRecord(entry.metadata).lastModDate) ?? "");
322
+ if (name?.startsWith(`${executableName}-`) && name.endsWith(".ips") && basename(name) === name && at >= options.since) candidates.push({ name, at });
323
+ }
324
+ for (const { name } of candidates.sort((a, b) => b.at - a.at)) {
325
+ const directory = await mkdtemp(join(options.directory, "crash-")).catch(() => undefined);
326
+ if (!directory) return undefined;
327
+ const destination = join(directory, `crash-${name}`);
328
+ try {
329
+ const copied = await runCommand(
330
+ "xcrun",
331
+ [
332
+ "devicectl", "device", "copy", "from", "--device", device.id,
333
+ "--domain-type", "systemCrashLogs", "--source", name, "--destination", destination,
334
+ ],
335
+ { timeoutMs: 60_000 },
336
+ );
337
+ if (copied.code !== 0) continue;
338
+ const report = await readFile(destination, "utf8").catch(() => undefined);
339
+ if (report === undefined) continue;
340
+ const reported = /"pid"\s*:\s*(\d+)/.exec(report)?.[1];
341
+ if (options.pid !== undefined && Number(reported) !== options.pid) continue;
342
+ const retained = join(options.directory, `${basename(directory)}-${name}`);
343
+ if (await rename(destination, retained).then(() => true, () => false)) return retained;
344
+ } finally {
345
+ await rm(directory, { recursive: true, force: true }).catch(() => {});
346
+ }
347
+ }
348
+ return undefined;
349
+ }