-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Expand file tree
/
Copy pathfake-agents.ts
More file actions
788 lines (755 loc) · 29.1 KB
/
Copy pathfake-agents.ts
File metadata and controls
788 lines (755 loc) · 29.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
import { chmod, mkdir, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path from 'node:path';
export type FakeAgentId =
| 'claude'
| 'codex'
| 'copilot'
| 'cursor-agent'
| 'deepseek'
| 'gemini'
| 'opencode'
| 'qoder'
| 'qwen';
export type FakeAgentRuntime = {
agentId: FakeAgentId;
bin: string;
envKey: string;
env: Record<string, string>;
};
export type FakeAgentRuntimeOptions = {
root?: string;
runtimeIds?: FakeAgentId[];
};
const AGENT_BIN_NAMES: Record<FakeAgentId, string> = {
claude: 'claude-e2e.cjs',
codex: 'codex-e2e.cjs',
copilot: 'copilot-e2e.cjs',
'cursor-agent': 'cursor-agent-e2e.cjs',
deepseek: 'deepseek-e2e.cjs',
gemini: 'gemini-e2e.cjs',
opencode: 'opencode-e2e.cjs',
qoder: 'qodercli-e2e.cjs',
qwen: 'qwen-e2e.cjs',
};
const AGENT_BIN_ENV_KEYS: Record<FakeAgentId, string> = {
claude: 'CLAUDE_BIN',
codex: 'CODEX_BIN',
copilot: 'COPILOT_BIN',
'cursor-agent': 'CURSOR_AGENT_BIN',
deepseek: 'DEEPSEEK_BIN',
gemini: 'GEMINI_BIN',
opencode: 'OPENCODE_BIN',
qoder: 'QODER_BIN',
qwen: 'QWEN_BIN',
};
export const FAKE_AGENT_RUNTIME_IDS: FakeAgentId[] = [
'claude',
'opencode',
'cursor-agent',
'qwen',
'qoder',
'copilot',
];
export async function createFakeAgentRuntimes(
runtimeIds?: FakeAgentId[],
): Promise<Record<FakeAgentId, FakeAgentRuntime>>;
export async function createFakeAgentRuntimes(
options?: FakeAgentRuntimeOptions,
): Promise<Record<FakeAgentId, FakeAgentRuntime>>;
export async function createFakeAgentRuntimes(
input: FakeAgentId[] | FakeAgentRuntimeOptions = {},
): Promise<Record<FakeAgentId, FakeAgentRuntime>> {
const runtimeIds = Array.isArray(input)
? input
: (input.runtimeIds ?? ['codex', ...FAKE_AGENT_RUNTIME_IDS]);
const root = Array.isArray(input)
? path.join(tmpdir(), `open-design-fake-agents-${process.pid}`)
: (input.root ?? path.join(tmpdir(), `open-design-fake-agents-${process.pid}`));
await mkdir(root, { recursive: true });
const runtimes = {} as Record<FakeAgentId, FakeAgentRuntime>;
for (const agentId of runtimeIds) {
const script = path.join(root, AGENT_BIN_NAMES[agentId]);
const parsedScript = path.parse(script);
const bin = process.platform === 'win32'
? path.join(parsedScript.dir, `${parsedScript.name}.cmd`)
: script;
await writeFile(script, renderFakeAgentScript(agentId), 'utf8');
if (process.platform === 'win32') {
await writeFile(bin, '@echo off\r\nnode "%~dp0%~n0.cjs" %*\r\n', 'utf8');
} else {
await chmod(bin, 0o755);
}
const envKey = AGENT_BIN_ENV_KEYS[agentId];
runtimes[agentId] = { agentId, bin, envKey, env: { [envKey]: bin } };
}
return runtimes;
}
function renderFakeAgentScript(agentId: FakeAgentId): string {
return `#!/usr/bin/env node
const agentId = ${JSON.stringify(agentId)};
const args = process.argv.slice(2);
const { mkdir, writeFile: writeFileFs } = require('node:fs/promises');
const { join } = require('node:path');
if (args.includes('--version')) {
process.stdout.write(agentId + '-e2e 0.0.0\\n');
process.exitCode = 0;
} else if (agentId === 'claude' && args[0] === '-p' && args.includes('--help')) {
process.stdout.write('--add-dir --include-partial-messages\\n');
process.exitCode = 0;
} else if ((agentId === 'opencode' || agentId === 'cursor-agent') && args[0] === 'models') {
process.stdout.write('fake/default\\n');
process.exitCode = 0;
} else {
let prompt = '';
let emitted = false;
let emitTimer = null;
process.stdin.setEncoding('utf8');
process.stdin.resume();
process.stdin.on('data', (chunk) => {
prompt += chunk;
if (emitted) return;
if (emitTimer) clearTimeout(emitTimer);
emitTimer = setTimeout(() => {
void emitRun(prompt).catch(failUnhandled);
}, 25);
});
process.stdin.on('end', () => {
void emitRun(prompt).catch(failUnhandled);
});
if (process.stdin.isTTY || agentId === 'deepseek') {
prompt = args.join(' ');
void emitRun(prompt).catch(failUnhandled);
}
async function emitRun(promptText) {
if (emitted) return;
emitted = true;
if (promptText.includes('Hold the daemon run open until canceled')) {
// Stay running (busy) without ever emitting a terminal result, so a test
// can queue a follow-up turn and interrupt it via send-now. Keep the event
// loop alive indefinitely; the daemon kills this child (SIGTERM) when the
// run is canceled.
setInterval(() => {}, 1 << 30);
return;
}
if (promptText.includes('Return an intentional daemon smoke failure')) {
emitFailure();
return;
}
if (promptText.includes('Return a daemon 429 service failure')) {
emitServiceFailure(429);
return;
}
if (promptText.includes('Return a daemon 503 service failure')) {
emitServiceFailure(503);
return;
}
if (promptText.includes('Return a daemon model-not-found failure')) {
emitModelUnavailableFailure();
return;
}
if (promptText.includes('Return a daemon timeout failure')) {
emitTimeoutFailure();
return;
}
if (promptText.includes('Return a daemon socket-drop failure')) {
emitSocketDropFailure();
return;
}
if (promptText.includes('Return an empty daemon smoke response')) {
emitEmptySuccess();
return;
}
if (promptText.includes('Return a stderr-only daemon smoke failure')) {
process.stderr.write('stderr-only daemon smoke failure from fake ' + agentId + '\\n');
process.exitCode = 1;
exitSoon(1);
return;
}
if (
promptText.includes('Create an Open Design plugin for:') &&
promptText.includes('produce a folder named generated-plugin')
) {
await emitPluginAuthoringRun();
return;
}
// Checked before the plan-document fixture: a follow-up generation turn's
// stdin can carry the first turn's "Create a deterministic plan document"
// text as conversation history.
if (promptText.includes('Generate the deterministic artifact from the plan document')) {
await emitPlanArtifactGenerateRun();
return;
}
if (promptText.includes('Create a deterministic plan document')) {
await emitPlanDocumentRun();
return;
}
// Work-completeness fixtures (#1247 / #1060): drive a Claude run that ends its
// turn while its TodoWrite plan still has unfinished tasks (or is truncated by
// max_tokens), so tests can assert the run reports endedWithUnfinishedWork.
if (promptText.includes('Emit an unfinished-todo run')) {
emitClaudeTodoRun([
{ content: 'Draft layout', status: 'completed' },
{ content: 'Build components', status: 'in_progress' },
{ content: 'Run QA', status: 'pending' },
], 'end_turn');
return;
}
if (promptText.includes('Emit a stopped-todo run')) {
emitClaudeTodoRun([{ content: 'Build components', status: 'stopped' }], 'end_turn');
return;
}
if (promptText.includes('Emit a max-tokens truncated run')) {
// All todos look done, but the turn was cut off — truncation alone must flag
// the run incomplete.
emitClaudeTodoRun([{ content: 'Draft layout', status: 'completed' }], 'max_tokens');
return;
}
if (promptText.includes('Emit an all-completed-todo run')) {
emitClaudeTodoRun([
{ content: 'Draft layout', status: 'completed' },
{ content: 'Build components', status: 'completed' },
], 'end_turn');
return;
}
if (promptText.includes('Edit the existing deterministic smoke artifact through the managed project alias')) {
await emitManagedAliasArtifactEditRun(promptText);
return;
}
if (promptText.includes('Edit the existing deterministic smoke artifact')) {
await emitExistingArtifactEditRun(promptText);
return;
}
const isSlowReload = promptText.includes('Create a slow reload deterministic smoke artifact');
const isDelayed = promptText.includes('Create a delayed deterministic smoke artifact');
const isChunked = promptText.includes('Create a chunked deterministic smoke artifact');
const isFollowUp = promptText.includes('Create a follow-up deterministic smoke artifact');
const isDefaultSmoke = promptText.includes('Create a deterministic smoke artifact');
const isOrbit = promptText.includes("Create today's Orbit daily digest as a Live Artifact.");
if (isOrbit) {
await emitOrbitRun();
return;
}
const isRuntime = promptText.match(/Fake runtime smoke for ([a-z0-9-]+)/i);
const runtimeId = isRuntime ? isRuntime[1] : agentId;
const heading = isSlowReload ? 'Slow Reload Daemon Smoke' : isDelayed ? 'Delayed Daemon Smoke' : isChunked ? 'Chunked Daemon Smoke' : isFollowUp ? 'Follow-up Daemon Smoke' : isDefaultSmoke ? 'Real Daemon Smoke' : 'Fake Agent Runtime ' + runtimeId;
const identifier = isSlowReload ? 'slow-reload-daemon-smoke' : isDelayed ? 'delayed-daemon-smoke' : isChunked ? 'chunked-daemon-smoke' : isFollowUp ? 'follow-up-daemon-smoke' : isDefaultSmoke ? 'real-daemon-smoke' : 'fake-agent-runtime-' + runtimeId;
const text = isSlowReload ? 'Generated after a reload while the daemon run was active.' : isDelayed ? 'Generated after a delayed daemon turn.' : isChunked ? 'Chunked through the daemon run path.' : isFollowUp ? 'Generated after an earlier daemon turn.' : isDefaultSmoke ? 'Generated through the daemon run path.' : 'Generated through fake ' + runtimeId + ' runtime.';
const html = '<!doctype html><html><body><main><h1>' + heading + '</h1><p>' + text + '</p></main></body></html>';
const artifact = '<artifact identifier="' + identifier + '" type="text/html" title="' + heading + '">' + html + '</artifact>';
const assistantText = isSlowReload
? 'I stayed attached after the reload and will persist the artifact now.\\n\\n' + artifact
: isDelayed
? 'I recovered the delayed reasoning path and will persist the artifact now.\\n\\n' + artifact
: artifact;
if (isSlowReload) {
await new Promise((resolve) => setTimeout(resolve, 15_000));
}
if (isDelayed) {
await new Promise((resolve) => setTimeout(resolve, 1200));
}
emitSuccess(assistantText, isChunked, isDelayed || isSlowReload);
process.exitCode = 0;
exitSoon(0);
}
async function emitPluginAuthoringRun() {
const folder = join(projectDir(), 'generated-plugin');
await mkdir(join(folder, 'examples'), { recursive: true });
await writeFileFs(
join(folder, 'open-design.json'),
JSON.stringify({
specVersion: 1,
name: 'generated-plugin',
version: '0.1.0',
description: 'Fake plugin authoring smoke scaffold.',
mode: 'agent',
taskKind: 'new-generation',
inputs: [{ id: 'prompt', type: 'string', label: 'Prompt' }],
}, null, 2) + '\\n',
'utf8',
);
await writeFileFs(
join(folder, 'SKILL.md'),
'# Generated Plugin\\n\\nThis fake plugin exists for plugin authoring smoke coverage.\\n',
'utf8',
);
await writeFileFs(
join(folder, 'examples', 'demo.md'),
'# Demo\\n\\nGenerated by the fake plugin authoring runtime.\\n',
'utf8',
);
const summary = [
'Created generated-plugin with open-design.json, SKILL.md, and examples/demo.md.',
'od plugin validate: passed',
'od plugin pack: generated-plugin-0.1.0.tgz',
'od plugin install --source: passed',
].join('\\n');
emitSuccess(summary, false, false);
process.exitCode = 0;
exitSoon(0);
}
async function emitPlanDocumentRun() {
await writeFileFs(
join(projectDir(), 'plan.md'),
[
'# Deterministic Plan',
'',
'## Scope',
'- Confirm the target workflow.',
'- Draft the project milestones.',
'',
'## Risks',
'- Keep the plan editable before design handoff.',
'',
].join('\\n'),
'utf8',
);
emitSuccess('Created plan.md with a deterministic planning outline.', false, false);
process.exitCode = 0;
exitSoon(0);
}
// Plan-mode generation turn (issue: Plan 模式生成 HTML 后没有自动打开): the
// agent reads the reviewed plan document, writes the final HTML deliverable
// as a project FILE (not an inline <artifact> echo), then touches the plan
// document again (e.g. updating its "Next step" section). Mirrors the real
// event order captured in the bug report: index.html first, plan.md second.
async function emitPlanArtifactGenerateRun() {
const dir = projectDir();
const html = '<!doctype html><html><body><main><h1>Plan Generated Deck</h1><p>Generated from the reviewed plan document.</p></main></body></html>';
const planUpdate = [
'# Deterministic Plan',
'',
'## Scope',
'- Confirm the target workflow.',
'- Draft the project milestones.',
'',
'## Next step',
'- Review the generated index.html.',
'',
].join('\\n');
await writeFileFs(join(dir, 'index.html'), html, 'utf8');
await writeFileFs(join(dir, 'plan.md'), planUpdate, 'utf8');
if (agentId === 'claude') {
// Emit the real Claude stream-json shape (tool_use + tool_result pairs)
// so the web per-write auto-open path sees the same events a live
// filesystem run produces.
writeJson({ type: 'system', subtype: 'init', model: 'fake-claude', session_id: 'fake-session' });
writeJson({
type: 'assistant',
message: {
id: 'msg-1',
content: [{ type: 'tool_use', id: 'toolu-plan-html', name: 'Write', input: { file_path: join(dir, 'index.html'), content: html } }],
},
});
writeJson({ type: 'user', message: { content: [{ type: 'tool_result', tool_use_id: 'toolu-plan-html', content: 'ok' }] } });
writeJson({
type: 'assistant',
message: {
id: 'msg-2',
content: [{ type: 'tool_use', id: 'toolu-plan-md', name: 'Write', input: { file_path: join(dir, 'plan.md'), content: planUpdate } }],
},
});
writeJson({ type: 'user', message: { content: [{ type: 'tool_result', tool_use_id: 'toolu-plan-md', content: 'ok' }] } });
writeJson({
type: 'assistant',
message: { id: 'msg-3', content: [{ type: 'text', text: 'Generated index.html from plan.md and refreshed the plan next steps.' }] },
});
writeJson({ type: 'result', usage: { input_tokens: 1, output_tokens: 1 }, total_cost_usd: 0, duration_ms: 1, stop_reason: 'end_turn' });
process.exitCode = 0;
exitSoon(0);
return;
}
emitSuccess('Generated index.html from plan.md and refreshed the plan next steps.', false, false);
process.exitCode = 0;
exitSoon(0);
}
async function emitExistingArtifactEditRun(promptText) {
const projectId = process.env.OD_PROJECT_ID || projectIdFromPrompt(promptText);
const daemonUrl = process.env.OD_DAEMON_URL;
if (!projectId || !daemonUrl) {
throw new Error('fake artifact edit requires OD_PROJECT_ID and OD_DAEMON_URL');
}
const response = await fetch(new URL('/api/projects/' + encodeURIComponent(projectId) + '/files', daemonUrl), {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
name: 'real-daemon-smoke.html',
content: '<!doctype html><html><body><main><h1>Real Daemon Smoke Edited</h1><p>Edited in place by a follow-up daemon run.</p></main></body></html>',
}),
});
if (!response.ok) {
throw new Error('fake artifact edit write failed: HTTP ' + response.status + ' ' + (await response.text()).slice(0, 500));
}
emitSuccess('Updated real-daemon-smoke.html in place with a deterministic follow-up edit.', false, false);
process.exitCode = 0;
exitSoon(0);
}
async function emitManagedAliasArtifactEditRun(promptText) {
if (agentId !== 'claude') {
throw new Error('managed-project alias edit fixture requires the Claude fake runtime');
}
const projectId = process.env.OD_PROJECT_ID || projectIdFromPrompt(promptText);
if (!projectId) {
throw new Error('managed-project alias edit fixture requires OD_PROJECT_ID');
}
const fileName = 'real-daemon-smoke.html';
const content = '<!doctype html><html><body><main><h1 data-od-id="smoke-title">Real Daemon Smoke Edited</h1><p>Edited in place through a managed-project alias.</p></main></body></html>';
await writeFileFs(join(projectDir(promptText), fileName), content, 'utf8');
writeJson({ type: 'system', subtype: 'init', model: 'fake-claude', session_id: 'fake-session' });
writeJson({
type: 'assistant',
message: {
id: 'msg-managed-alias-write',
content: [{
type: 'tool_use',
id: 'toolu-managed-alias-write',
name: 'Write',
input: {
file_path: '.od/projects/' + projectId + '/' + fileName,
content,
},
}],
},
});
writeJson({
type: 'user',
message: {
content: [{
type: 'tool_result',
tool_use_id: 'toolu-managed-alias-write',
content: 'Updated ' + fileName,
}],
},
});
writeJson({
type: 'assistant',
message: {
id: 'msg-managed-alias-summary',
content: [{ type: 'text', text: 'Updated ' + fileName + ' in place.' }],
},
});
writeJson({
type: 'result',
usage: { input_tokens: 1, output_tokens: 1 },
total_cost_usd: 0,
duration_ms: 1,
stop_reason: 'end_turn',
});
process.exitCode = 0;
exitSoon(0);
}
function projectIdFromPrompt(promptText = '') {
const marker = '.od/projects/';
const idx = promptText.indexOf(marker);
if (idx === -1) return '';
return promptText
.slice(idx + marker.length)
.split(/[\\s/]/)[0]
.replace(/[^a-zA-Z0-9_-].*$/, '');
}
function projectDir(promptText = '') {
const marker = 'current working directory: \`';
const idx = promptText.toLowerCase().indexOf(marker);
const fromPrompt = idx === -1
? ''
: promptText.slice(idx + marker.length).split('\`')[0] || '';
const fromEnv = process.env.OD_DATA_DIR && process.env.OD_PROJECT_ID
? join(process.env.OD_DATA_DIR, 'projects', process.env.OD_PROJECT_ID)
: '';
const cwdFlagIndex = args.indexOf('-C');
const fromArgs = cwdFlagIndex >= 0 && typeof args[cwdFlagIndex + 1] === 'string'
? args[cwdFlagIndex + 1]
: '';
return process.env.OD_PROJECT_DIR || fromEnv || fromArgs || fromPrompt || process.cwd();
}
function writeJson(value) {
process.stdout.write(JSON.stringify(value) + '\\n');
}
function exitSoon(code) {
setTimeout(() => process.exit(code), 10);
}
// Emit a Claude stream-json turn that carries a TodoWrite tool_use snapshot and
// a chosen terminal stop_reason. The turn ends cleanly (exit 0 -> succeeded), but
// its declared work is left in whatever state the todos describe — the fixture the
// #1247 / #1060 completeness tests drive. Only the claude runtime models a
// content-level tool_use + per-turn stop_reason, so these fixtures use it.
function emitClaudeTodoRun(todos, stopReason) {
if (agentId !== 'claude') {
throw new Error('emitClaudeTodoRun fixtures require the claude fake runtime, got ' + agentId);
}
writeJson({ type: 'system', subtype: 'init', model: 'fake-claude', session_id: 'fake-session' });
writeJson({
type: 'assistant',
message: {
id: 'msg-1',
stop_reason: stopReason,
content: [
{ type: 'tool_use', id: 'tw-1', name: 'TodoWrite', input: { todos } },
{ type: 'text', text: 'Here is the plan.' },
],
},
});
writeJson({ type: 'result', usage: { input_tokens: 1, output_tokens: 1 }, total_cost_usd: 0, duration_ms: 1, stop_reason: stopReason });
process.exitCode = 0;
exitSoon(0);
}
function emitSuccess(artifact, isChunked, includeThinking) {
const first = artifact.slice(0, Math.ceil(artifact.length / 2));
const second = artifact.slice(Math.ceil(artifact.length / 2));
switch (agentId) {
case 'codex':
writeJson({ type: 'thread.started' });
writeJson({ type: 'turn.started' });
if (isChunked) {
writeJson({ type: 'item.completed', item: { type: 'agent_message', text: first } });
writeJson({ type: 'item.completed', item: { type: 'agent_message', text: second } });
} else {
writeJson({ type: 'item.completed', item: { type: 'agent_message', text: artifact } });
}
writeJson({ type: 'turn.completed', usage: { input_tokens: 1, output_tokens: 1 } });
return;
case 'claude':
writeJson({ type: 'system', subtype: 'init', model: 'fake-claude', session_id: 'fake-session' });
writeJson({
type: 'assistant',
message: {
id: 'msg-1',
content: [
...(includeThinking ? [{ type: 'thinking', thinking: 'Recovered delayed reasoning trace.' }] : []),
{ type: 'text', text: artifact },
],
},
});
writeJson({ type: 'result', usage: { input_tokens: 1, output_tokens: 1 }, total_cost_usd: 0, duration_ms: 1, stop_reason: 'end_turn' });
return;
case 'gemini':
writeJson({ type: 'init', session_id: 'fake-gemini', model: 'fake-gemini' });
writeJson({ type: 'message', role: 'assistant', content: artifact, delta: true });
writeJson({ type: 'result', status: 'success', stats: { input_tokens: 1, output_tokens: 1, cached: 0, duration_ms: 1 } });
return;
case 'opencode':
writeJson({ type: 'step_start', sessionID: 'fake-opencode', part: { type: 'step-start' } });
writeJson({ type: 'text', sessionID: 'fake-opencode', part: { type: 'text', text: artifact } });
writeJson({ type: 'step_finish', sessionID: 'fake-opencode', part: { type: 'step-finish', tokens: { input: 1, output: 1 }, cost: 0 } });
return;
case 'cursor-agent':
writeJson({ type: 'system', subtype: 'init', model: 'fake-cursor' });
writeJson({ type: 'assistant', timestamp_ms: 1, message: { role: 'assistant', content: [{ type: 'text', text: artifact }] } });
writeJson({ type: 'result', duration_ms: 1, usage: { inputTokens: 1, outputTokens: 1, cacheReadTokens: 0, cacheWriteTokens: 0 } });
return;
case 'qoder':
writeJson({ type: 'system', subtype: 'init', qodercli_version: '0.0.0', model: 'fake-qoder', session_id: 'fake-qoder' });
writeJson({ type: 'assistant', message: { content: [{ type: 'text', text: artifact }] }, session_id: 'fake-qoder' });
writeJson({ type: 'result', subtype: 'success', duration_ms: 1, is_error: false, stop_reason: 'end_turn', total_cost_usd: 0, usage: { input_tokens: 1, output_tokens: 1 } });
return;
case 'copilot':
writeJson({ type: 'session.tools_updated', data: { model: 'fake-copilot' } });
writeJson({ type: 'assistant.turn_start', data: {} });
writeJson({ type: 'assistant.message_delta', data: { deltaContent: artifact } });
writeJson({ type: 'result', success: true, exitCode: 0, usage: { input_tokens: 1, output_tokens: 1, sessionDurationMs: 1 } });
return;
case 'qwen':
case 'deepseek':
process.stdout.write(artifact + '\\n');
return;
default:
process.stdout.write(artifact + '\\n');
}
}
async function emitOrbitRun() {
const artifact = await createOrbitLiveArtifact();
const text = 'Orbit fake digest registered live artifact ' + artifact.id + ' for project ' + artifact.projectId + '.';
emitSuccess(text, false);
process.exitCode = 0;
exitSoon(0);
}
async function createOrbitLiveArtifact() {
const baseUrl = process.env.OD_DAEMON_URL;
const token = process.env.OD_TOOL_TOKEN;
if (!baseUrl || !token) {
throw new Error('Orbit fake run requires OD_DAEMON_URL and OD_TOOL_TOKEN');
}
const url = new URL('/api/tools/live-artifacts/create', baseUrl);
const payload = {
input: {
title: 'Orbit Daily Digest',
slug: 'orbit-daily-digest',
preview: { type: 'html', entry: 'index.html' },
document: {
format: 'html_template_v1',
templatePath: 'template.html',
generatedPreviewPath: 'index.html',
dataPath: 'data.json',
dataJson: {
headline: 'Orbit daily digest',
takeaway1: 'Fake connector activity was summarized through the daemon Orbit path.',
takeaway2: 'The live artifact tool token was accepted.',
takeaway3: 'The digest can be opened and previewed from the Orbit project.',
checked: 'fake activity feed and fake task updates',
},
},
},
templateHtml: '<!doctype html><html><body><main><h1>{{data.headline}}</h1><ul><li>{{data.takeaway1}}</li><li>{{data.takeaway2}}</li><li>{{data.takeaway3}}</li></ul><p>{{data.checked}}</p></main></body></html>',
provenanceJson: {
generatedAt: new Date().toISOString(),
generatedBy: 'agent',
sources: [{ label: 'Fake Orbit e2e data', type: 'derived' }],
},
};
const response = await fetch(url, {
method: 'POST',
headers: {
authorization: 'Bearer ' + token,
'content-type': 'application/json',
},
body: JSON.stringify(payload),
});
const text = await response.text();
let body = {};
try {
body = text ? JSON.parse(text) : {};
} catch {
body = { raw: text };
}
if (!response.ok || !body.artifact) {
throw new Error('Orbit live artifact create failed: HTTP ' + response.status + ' ' + text.slice(0, 500));
}
return body.artifact;
}
function failUnhandled(error) {
process.stderr.write((error && error.stack ? error.stack : String(error)) + '\\n');
process.exitCode = 1;
exitSoon(1);
}
function emitFailure() {
switch (agentId) {
case 'codex':
writeJson({ type: 'thread.started' });
writeJson({ type: 'turn.started' });
writeJson({ type: 'turn.failed', error: { message: 'intentional fake codex failure' } });
process.exitCode = 0;
exitSoon(0);
return;
case 'opencode':
writeJson({ type: 'error', error: { data: { message: 'intentional fake opencode failure' } } });
process.exitCode = 0;
exitSoon(0);
return;
case 'qoder':
writeJson({ type: 'assistant', message: { content: [] }, error: { message: 'intentional fake qoder failure' } });
process.exitCode = 0;
exitSoon(0);
return;
default:
process.stderr.write('intentional fake ' + agentId + ' failure\\n');
process.exitCode = 1;
exitSoon(1);
}
}
function emitServiceFailure(statusCode) {
const message =
statusCode === 429
? 'HTTP 429 Too Many Requests: rate limit exceeded for the current provider.'
: 'HTTP 503 Service Unavailable: upstream model provider is temporarily unavailable.';
switch (agentId) {
case 'codex':
writeJson({ type: 'thread.started' });
writeJson({ type: 'turn.started' });
writeJson({ type: 'turn.failed', error: { message } });
process.exitCode = 0;
exitSoon(0);
return;
case 'opencode':
writeJson({ type: 'error', error: { data: { message } } });
process.exitCode = 0;
exitSoon(0);
return;
default:
process.stderr.write(message + '\\n');
process.exitCode = 1;
exitSoon(1);
}
}
function emitModelUnavailableFailure() {
const message = 'The selected model is not available for this account: model not found.';
writeJson({ type: 'thread.started' });
writeJson({ type: 'turn.started' });
writeJson({ type: 'turn.failed', error: { message } });
process.exitCode = 0;
exitSoon(0);
}
function emitTimeoutFailure() {
const message = 'The upstream model request timed out while waiting for a response.';
writeJson({ type: 'thread.started' });
writeJson({ type: 'turn.started' });
writeJson({ type: 'turn.failed', error: { message } });
process.exitCode = 0;
exitSoon(0);
}
// Reproduces a connection that dropped mid-response. This shape is NOT guessed:
// it was captured by pointing the real Claude Code CLI (2.1.168) at a fake
// Anthropic endpoint that accepts the request, starts streaming, then destroys
// the TCP socket. The real CLI exhausts its internal retries and then, on
// STDOUT (not stderr), emits the SDK error as a synthetic assistant text block
// plus a result frame carrying is_error:true with subtype "success", and exits
// 1. The daemon's claude diagnostic reads the stdout tail and classifies it as
// a retryable connection drop.
//
// Only claude is modeled here: the daemon connection-drop diagnostic is
// claude-specific, and the real Codex CLI fails this class differently (it
// streams "Reconnecting... N/5" and a turn.failed event over its own
// transport), so a faithful Codex case belongs with Codex-specific handling,
// not this claude reproduction.
function emitSocketDropFailure() {
const sdkError =
'API Error: The socket connection was closed unexpectedly. For more information, pass \`verbose: true\` in the second argument to fetch()';
if (agentId === 'claude') {
writeJson({ type: 'system', subtype: 'init', model: 'fake-claude', session_id: 'fake-session' });
writeJson({
type: 'assistant',
message: {
id: 'msg-1',
model: '<synthetic>',
role: 'assistant',
stop_reason: 'stop_sequence',
content: [{ type: 'text', text: sdkError }],
},
error: 'unknown',
});
writeJson({
type: 'result',
subtype: 'success',
is_error: true,
result: sdkError,
stop_reason: 'stop_sequence',
duration_ms: 1,
total_cost_usd: 0,
usage: { input_tokens: 0, output_tokens: 0 },
});
process.exitCode = 1;
exitSoon(1);
return;
}
// Other runtimes are not the subject of the claude connection diagnostic;
// surface a generic non-zero exit carrying the same SDK error text.
process.stderr.write(sdkError + '\\n');
process.exitCode = 1;
exitSoon(1);
}
function emitEmptySuccess() {
switch (agentId) {
case 'codex':
writeJson({ type: 'thread.started' });
writeJson({ type: 'turn.started' });
writeJson({ type: 'turn.completed', usage: { input_tokens: 1, output_tokens: 0 } });
process.exitCode = 0;
exitSoon(0);
return;
default:
process.exitCode = 0;
exitSoon(0);
}
}
}
`;
}