@ai-devkit/agent-manager 0.39.0 → 0.41.0

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.
Files changed (61) hide show
  1. package/dist/AgentManager.d.ts +12 -0
  2. package/dist/AgentManager.d.ts.map +1 -1
  3. package/dist/AgentManager.js +20 -1
  4. package/dist/AgentManager.js.map +1 -1
  5. package/dist/__tests__/AgentManager.test.js +111 -0
  6. package/dist/__tests__/AgentManager.test.js.map +1 -1
  7. package/dist/__tests__/capacity/devin.test.js +239 -0
  8. package/dist/__tests__/capacity/devin.test.js.map +1 -0
  9. package/dist/__tests__/fixtures.test.js +218 -0
  10. package/dist/__tests__/fixtures.test.js.map +1 -0
  11. package/dist/__tests__/readiness-fixtures.test.js +54 -0
  12. package/dist/__tests__/readiness-fixtures.test.js.map +1 -0
  13. package/dist/capacity/index.d.ts +3 -0
  14. package/dist/capacity/index.d.ts.map +1 -1
  15. package/dist/capacity/index.js +8 -0
  16. package/dist/capacity/index.js.map +1 -1
  17. package/dist/capacity/providers/devin.d.ts +13 -0
  18. package/dist/capacity/providers/devin.d.ts.map +1 -0
  19. package/dist/capacity/providers/devin.js +92 -0
  20. package/dist/capacity/providers/devin.js.map +1 -0
  21. package/dist/capacity/sources/devin.d.ts +12 -0
  22. package/dist/capacity/sources/devin.d.ts.map +1 -0
  23. package/dist/capacity/sources/devin.js +29 -0
  24. package/dist/capacity/sources/devin.js.map +1 -0
  25. package/dist/fixtures/bundle.d.ts +67 -0
  26. package/dist/fixtures/bundle.d.ts.map +1 -0
  27. package/dist/fixtures/bundle.js +86 -0
  28. package/dist/fixtures/bundle.js.map +1 -0
  29. package/dist/fixtures/capture.d.ts +11 -0
  30. package/dist/fixtures/capture.d.ts.map +1 -0
  31. package/dist/fixtures/capture.js +317 -0
  32. package/dist/fixtures/capture.js.map +1 -0
  33. package/dist/harnesses/devin/credentials.d.ts +10 -0
  34. package/dist/harnesses/devin/credentials.d.ts.map +1 -0
  35. package/dist/harnesses/devin/credentials.js +66 -0
  36. package/dist/harnesses/devin/credentials.js.map +1 -0
  37. package/dist/harnesses/shared.d.ts.map +1 -1
  38. package/dist/harnesses/shared.js +1 -1
  39. package/dist/harnesses/shared.js.map +1 -1
  40. package/dist/index.d.ts +1 -1
  41. package/dist/index.d.ts.map +1 -1
  42. package/dist/index.js +1 -1
  43. package/dist/index.js.map +1 -1
  44. package/package.json +1 -1
  45. package/src/AgentManager.ts +37 -1
  46. package/src/__tests__/AgentManager.test.ts +84 -0
  47. package/src/__tests__/capacity/devin.test.ts +210 -0
  48. package/src/__tests__/capacity/fixtures/devin-config.json +6 -0
  49. package/src/__tests__/capacity/fixtures/devin-credentials.toml +4 -0
  50. package/src/__tests__/capacity/fixtures/devin-quota-nonplan.json +5 -0
  51. package/src/__tests__/capacity/fixtures/devin-quota.json +10 -0
  52. package/src/__tests__/fixtures.test.ts +247 -0
  53. package/src/__tests__/readiness-fixtures.test.ts +79 -0
  54. package/src/capacity/index.ts +12 -0
  55. package/src/capacity/providers/devin.ts +121 -0
  56. package/src/capacity/sources/devin.ts +49 -0
  57. package/src/fixtures/bundle.ts +130 -0
  58. package/src/fixtures/capture.ts +349 -0
  59. package/src/harnesses/devin/credentials.ts +88 -0
  60. package/src/harnesses/shared.ts +1 -1
  61. package/src/index.ts +1 -0
@@ -8,6 +8,8 @@
8
8
  import type {
9
9
  AgentAdapter,
10
10
  AgentInfo,
11
+ AgentStatus,
12
+ AgentType,
11
13
  SessionSummary,
12
14
  ListSessionsOptions,
13
15
  ProcessInfo,
@@ -45,6 +47,20 @@ export interface AgentManagerOptions {
45
47
  | (() => AgentRuntimeProvider | Promise<AgentRuntimeProvider>);
46
48
  fetchHerdrAgentPanes?: () => Promise<readonly HerdrAgentPane[]>;
47
49
  onRuntimeDiscoveryError?: (error: unknown) => void;
50
+ /**
51
+ * Daemon-side enriched agents ({@link DaemonClient.listAgents} wire
52
+ * shape: `AgentInfo` with `lastActive` as an ISO string). A non-null
53
+ * result is authoritative for every harness type — all local adapters
54
+ * are skipped. Returning null or throwing forces the local path;
55
+ * omitting it disables daemon consumption.
56
+ */
57
+ fetchEnrichedAgents?: () => Promise<Array<
58
+ Omit<AgentInfo, "lastActive" | "type" | "status"> & {
59
+ type: string;
60
+ status: string;
61
+ lastActive: string;
62
+ }
63
+ > | null>;
48
64
  }
49
65
 
50
66
  class AgentNotRunningError extends Error {
@@ -162,7 +178,27 @@ export class AgentManager {
162
178
  const allAgents: AgentInfo[] = [];
163
179
  const errors: Array<{ type: string; error: Error }> = [];
164
180
 
165
- const adapters = Array.from(this.adapters.values());
181
+ // A non-null daemon answer is authoritative for every harness type, so
182
+ // local adapters only run when the daemon is absent.
183
+ let daemonAgents: AgentInfo[] | null = null;
184
+ if (this.options.fetchEnrichedAgents) {
185
+ try {
186
+ const enriched = await this.options.fetchEnrichedAgents();
187
+ if (enriched) {
188
+ daemonAgents = enriched.map((a) => ({
189
+ ...a,
190
+ type: a.type as AgentType,
191
+ status: a.status as AgentStatus,
192
+ lastActive: new Date(a.lastActive),
193
+ }));
194
+ }
195
+ } catch {
196
+ daemonAgents = null;
197
+ }
198
+ }
199
+ if (daemonAgents) allAgents.push(...daemonAgents);
200
+
201
+ const adapters = daemonAgents ? [] : Array.from(this.adapters.values());
166
202
  const processNames = Array.from(
167
203
  new Set(
168
204
  adapters.flatMap((adapter) => (adapter.processNames ? [...adapter.processNames] : [])),
@@ -478,6 +478,90 @@ describe("AgentManager", () => {
478
478
  });
479
479
  });
480
480
 
481
+ describe("listAgents — daemon enrichment", () => {
482
+ const wireAgent = (overrides: Record<string, unknown> = {}) => ({
483
+ name: "daemon-claude",
484
+ type: "claude",
485
+ status: "waiting",
486
+ summary: "daemon summary",
487
+ pid: 777,
488
+ projectPath: "/daemon/proj",
489
+ sessionId: "daemon-sid",
490
+ lastActive: "2026-10-08T12:00:00.000Z",
491
+ ...overrides,
492
+ });
493
+
494
+ it("serves agents from the daemon and skips all local adapters", async () => {
495
+ const local = new MockAdapter("claude", [createMockAgent({ name: "local-claude" })]);
496
+ const detectSpy = vi.spyOn(local, "detectAgents");
497
+ const m = new AgentManager(new AgentRegistry(path.join(tmpDir, "agents.json")), undefined, {
498
+ fetchEnrichedAgents: async () => [wireAgent()],
499
+ });
500
+ m.registerAdapter(local);
501
+
502
+ const agents = await m.listAgents();
503
+ expect(detectSpy).not.toHaveBeenCalled();
504
+ expect(agents).toHaveLength(1);
505
+ expect(agents[0].name).toBe("daemon-claude");
506
+ expect(agents[0].lastActive).toEqual(new Date("2026-10-08T12:00:00.000Z"));
507
+ });
508
+
509
+ it("falls back to all-local when the daemon returns null", async () => {
510
+ const m = new AgentManager(new AgentRegistry(path.join(tmpDir, "agents.json")), undefined, {
511
+ fetchEnrichedAgents: async () => null,
512
+ });
513
+ m.registerAdapter(new MockAdapter("claude", [createMockAgent({ name: "local-claude" })]));
514
+ const agents = await m.listAgents();
515
+ expect(agents.map((a) => a.name)).toEqual(["local-claude"]);
516
+ });
517
+
518
+ it("falls back to all-local when the daemon call throws", async () => {
519
+ const m = new AgentManager(new AgentRegistry(path.join(tmpDir, "agents.json")), undefined, {
520
+ fetchEnrichedAgents: async () => {
521
+ throw new Error("socket gone");
522
+ },
523
+ });
524
+ m.registerAdapter(new MockAdapter("claude", [createMockAgent({ name: "local-claude" })]));
525
+ const agents = await m.listAgents();
526
+ expect(agents.map((a) => a.name)).toEqual(["local-claude"]);
527
+ });
528
+
529
+ it("serves every type from the daemon in one sweep", async () => {
530
+ const claudeLocal = new MockAdapter("claude", [createMockAgent({ name: "local-claude" })]);
531
+ const codexLocal = new MockAdapter("codex", [
532
+ createMockAgent({ name: "local-codex", type: "codex" }),
533
+ ]);
534
+ const claudeSpy = vi.spyOn(claudeLocal, "detectAgents");
535
+ const codexSpy = vi.spyOn(codexLocal, "detectAgents");
536
+ const m = new AgentManager(new AgentRegistry(path.join(tmpDir, "agents.json")), undefined, {
537
+ fetchEnrichedAgents: async () => [
538
+ wireAgent(),
539
+ wireAgent({ type: "codex", name: "daemon-codex", pid: 888 }),
540
+ ],
541
+ });
542
+ m.registerAdapter(claudeLocal);
543
+ m.registerAdapter(codexLocal);
544
+
545
+ const agents = await m.listAgents();
546
+ expect(claudeSpy).not.toHaveBeenCalled();
547
+ expect(codexSpy).not.toHaveBeenCalled();
548
+ expect(agents.map((a) => a.name).sort()).toEqual(["daemon-claude", "daemon-codex"]);
549
+ });
550
+
551
+ it("treats an empty daemon answer as authoritative", async () => {
552
+ const local = new MockAdapter("claude", [createMockAgent({ name: "local-claude" })]);
553
+ const detectSpy = vi.spyOn(local, "detectAgents");
554
+ const m = new AgentManager(new AgentRegistry(path.join(tmpDir, "agents.json")), undefined, {
555
+ fetchEnrichedAgents: async () => [],
556
+ });
557
+ m.registerAdapter(local);
558
+
559
+ const agents = await m.listAgents();
560
+ expect(detectSpy).not.toHaveBeenCalled();
561
+ expect(agents).toEqual([]);
562
+ });
563
+ });
564
+
481
565
  describe("listAgents — registry persistence", () => {
482
566
  let tmpDir: string;
483
567
  let regPath: string;
@@ -0,0 +1,210 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import { fileURLToPath } from "node:url";
3
+ import { describe, expect, it, vi } from "vitest";
4
+ import { parseDevinCapacity } from "../../capacity/providers/devin.js";
5
+ import { probeDevinCapacity } from "../../capacity/sources/devin.js";
6
+ import { resolveDevinCredential } from "../../harnesses/devin/credentials.js";
7
+
8
+ const checkedAt = "2026-10-05T10:00:00.000Z";
9
+ const fixture = (name: string) =>
10
+ readFile(fileURLToPath(new URL(`./fixtures/${name}`, import.meta.url)), "utf8");
11
+
12
+ const credentialFiles = async (path: string): Promise<string> => {
13
+ if (path === "/users/test/.local/share/devin/credentials.toml") {
14
+ return fixture("devin-credentials.toml");
15
+ }
16
+ if (path === "/users/test/.config/devin/config.json") {
17
+ return fixture("devin-config.json");
18
+ }
19
+ throw Object.assign(new Error("missing"), { code: "ENOENT" });
20
+ };
21
+
22
+ describe("devin credential resolution", () => {
23
+ it("prefers DEVIN_API_KEY and DEVIN_ORG_ID without reading files", async () => {
24
+ const reader = vi.fn(credentialFiles);
25
+ await expect(
26
+ resolveDevinCredential({
27
+ env: { DEVIN_API_KEY: "env-key", DEVIN_ORG_ID: "org-env" },
28
+ readFile: reader,
29
+ }),
30
+ ).resolves.toEqual({ key: "env-key", orgId: "org-env" });
31
+ expect(reader).not.toHaveBeenCalled();
32
+ });
33
+
34
+ it("reads the key from credentials.toml and org from config.json", async () => {
35
+ const reader = vi.fn(credentialFiles);
36
+ await expect(
37
+ resolveDevinCredential({ env: { HOME: "/users/test" }, readFile: reader }),
38
+ ).resolves.toEqual({ key: "devin-test-key", orgId: "org-test1234" });
39
+ expect(reader).toHaveBeenCalledWith("/users/test/.local/share/devin/credentials.toml", "utf8");
40
+ expect(reader).toHaveBeenCalledWith("/users/test/.config/devin/config.json", "utf8");
41
+ });
42
+
43
+ it.each([
44
+ [
45
+ "missing credentials file",
46
+ async (path: string) => {
47
+ if (path.endsWith("config.json")) return fixture("devin-config.json");
48
+ throw Object.assign(new Error("missing"), { code: "ENOENT" });
49
+ },
50
+ /Devin API key not found/,
51
+ ],
52
+ [
53
+ "malformed credentials file",
54
+ async (path: string) => {
55
+ if (path.endsWith("credentials.toml")) return "not = [valid";
56
+ return fixture("devin-config.json");
57
+ },
58
+ /credentials file is malformed/,
59
+ ],
60
+ [
61
+ "credentials without key",
62
+ async (path: string) => {
63
+ if (path.endsWith("credentials.toml")) return 'api_server_url = "https://x"';
64
+ return fixture("devin-config.json");
65
+ },
66
+ /Devin API key not found/,
67
+ ],
68
+ [
69
+ "missing config file",
70
+ async (path: string) => {
71
+ if (path.endsWith("credentials.toml")) return fixture("devin-credentials.toml");
72
+ throw Object.assign(new Error("missing"), { code: "ENOENT" });
73
+ },
74
+ /Devin organization id not found/,
75
+ ],
76
+ [
77
+ "config without org_id",
78
+ async (path: string) => {
79
+ if (path.endsWith("credentials.toml")) return fixture("devin-credentials.toml");
80
+ return JSON.stringify({ devin: {} });
81
+ },
82
+ /Devin organization id not found/,
83
+ ],
84
+ ])("fails clearly for %s without exposing file content", async (_name, readFileImpl, pattern) => {
85
+ await expect(
86
+ resolveDevinCredential({ env: { HOME: "/users/test" }, readFile: readFileImpl }),
87
+ ).rejects.toThrow(pattern);
88
+ });
89
+ });
90
+
91
+ describe("devin quota mapping", () => {
92
+ it("maps daily and weekly windows with credits", async () => {
93
+ const report = parseDevinCapacity(JSON.parse(await fixture("devin-quota.json")));
94
+ expect(report).toMatchObject({
95
+ authenticated: true,
96
+ available: "yes",
97
+ creditsRemaining: 12.5,
98
+ });
99
+ expect(report.windows).toEqual([
100
+ {
101
+ id: "devin:daily",
102
+ label: "Daily",
103
+ durationMinutes: 1440,
104
+ usedPercent: 25,
105
+ resetsAt: "2026-10-06T08:00:00.000Z",
106
+ },
107
+ {
108
+ id: "devin:weekly",
109
+ label: "Weekly",
110
+ durationMinutes: 10080,
111
+ usedPercent: 60,
112
+ resetsAt: "2026-10-11T08:00:00.000Z",
113
+ },
114
+ ]);
115
+ });
116
+
117
+ it("omits the daily window when hide_daily_quota is true", () => {
118
+ const report = parseDevinCapacity({
119
+ is_quota_plan: true,
120
+ has_quota_allocation: true,
121
+ daily_percentage: 90,
122
+ weekly_percentage: 10,
123
+ hide_daily_quota: true,
124
+ });
125
+ expect(report.windows.map((window) => window.id)).toEqual(["devin:weekly"]);
126
+ });
127
+
128
+ it("reports authenticated unknown availability for non-quota plans", async () => {
129
+ const report = parseDevinCapacity(JSON.parse(await fixture("devin-quota-nonplan.json")));
130
+ expect(report).toEqual({
131
+ authenticated: true,
132
+ available: "unknown",
133
+ windows: [],
134
+ creditsRemaining: null,
135
+ });
136
+ });
137
+
138
+ it("clamps percentages and degrades missing fields honestly", () => {
139
+ const report = parseDevinCapacity({
140
+ is_quota_plan: true,
141
+ has_quota_allocation: true,
142
+ daily_percentage: 140,
143
+ weekly_reset_at: "not-a-date",
144
+ overage_balance: "unknown",
145
+ });
146
+ const [daily, weekly] = report.windows;
147
+ expect(daily.usedPercent).toBe(100);
148
+ expect(weekly.usedPercent).toBeNull();
149
+ expect(weekly.resetsAt).toBeNull();
150
+ expect(report.creditsRemaining).toBeNull();
151
+ expect(report.available).toBe("no");
152
+ });
153
+
154
+ it("rejects non-object responses", () => {
155
+ expect(() => parseDevinCapacity("nope")).toThrow(/Devin quota/);
156
+ expect(() => parseDevinCapacity(null)).toThrow(/Devin quota/);
157
+ });
158
+ });
159
+
160
+ describe("devin request", () => {
161
+ it("GETs the org quota endpoint with only Bearer authentication", async () => {
162
+ const body = await fixture("devin-quota.json");
163
+ const fetch = vi.fn(async () => new Response(body, { status: 200 }));
164
+ const report = await probeDevinCapacity({
165
+ checkedAt,
166
+ env: { DEVIN_API_KEY: "request-test-key", DEVIN_ORG_ID: "org-abc" },
167
+ fetch,
168
+ });
169
+ expect(fetch).toHaveBeenCalledWith(
170
+ "https://app.devin.ai/api/org-abc/billing/quota/usage",
171
+ expect.objectContaining({
172
+ method: "GET",
173
+ headers: { Authorization: "Bearer request-test-key" },
174
+ }),
175
+ );
176
+ expect(report.harness).toBe("devin");
177
+ expect(report.provider).toBe("devin");
178
+ expect(JSON.stringify(report)).not.toContain("request-test-key");
179
+ });
180
+
181
+ it.each([401, 403])("reports HTTP %i as unauthenticated", async (status) => {
182
+ const report = await probeDevinCapacity({
183
+ checkedAt,
184
+ env: { DEVIN_API_KEY: "request-test-key", DEVIN_ORG_ID: "org-abc" },
185
+ fetch: vi.fn(async () => new Response("denied", { status })),
186
+ });
187
+ expect(report).toMatchObject({
188
+ authenticated: false,
189
+ available: "unknown",
190
+ windows: [],
191
+ });
192
+ });
193
+
194
+ it("rejects other non-200 and malformed JSON responses with sanitized errors", async () => {
195
+ await expect(
196
+ probeDevinCapacity({
197
+ checkedAt,
198
+ env: { DEVIN_API_KEY: "never-expose-this-key", DEVIN_ORG_ID: "org-abc" },
199
+ fetch: vi.fn(async () => new Response("private body", { status: 503 })),
200
+ }),
201
+ ).rejects.toThrow("Devin quota request failed: HTTP 503");
202
+ await expect(
203
+ probeDevinCapacity({
204
+ checkedAt,
205
+ env: { DEVIN_API_KEY: "never-expose-this-key", DEVIN_ORG_ID: "org-abc" },
206
+ fetch: vi.fn(async () => new Response("not-json", { status: 200 })),
207
+ }),
208
+ ).rejects.toThrow("Devin quota response is not valid JSON");
209
+ });
210
+ });
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 1,
3
+ "devin": {
4
+ "org_id": "org-test1234"
5
+ }
6
+ }
@@ -0,0 +1,4 @@
1
+ windsurf_api_key = "devin-test-key"
2
+ api_server_url = "https://server.codeium.com"
3
+ devin_webapp_host = "app.devin.ai"
4
+ devin_api_url = "https://api.devin.ai"
@@ -0,0 +1,5 @@
1
+ {
2
+ "is_quota_plan": false,
3
+ "has_quota_allocation": false,
4
+ "overage_balance": 0.0
5
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "is_quota_plan": true,
3
+ "has_quota_allocation": true,
4
+ "daily_percentage": 25,
5
+ "weekly_percentage": 60,
6
+ "daily_reset_at": "2026-10-06T00:00:00-08:00",
7
+ "weekly_reset_at": "2026-10-11T00:00:00-08:00",
8
+ "overage_balance": 12.5,
9
+ "hide_daily_quota": false
10
+ }
@@ -0,0 +1,247 @@
1
+ /**
2
+ * Fixture replay: materialize each committed bundle into a fake HOME,
3
+ * run the adapter's detectAgents with the recorded processes + frozen
4
+ * clock, and require byte-identical output to `expected`.
5
+ *
6
+ * The same bundles are consumed by devkit-harness Rust tests — this file is
7
+ * the TS↔TS side of the parity oracle.
8
+ */
9
+
10
+ import { describe, it, expect, afterEach } from "vitest";
11
+ import * as fs from "node:fs";
12
+ import * as os from "node:os";
13
+ import * as path from "node:path";
14
+ import Database from "better-sqlite3";
15
+ import { createBuiltinAdapters } from "../harnesses/index.js";
16
+ import { CodexAdapter } from "../harnesses/codex/CodexAdapter.js";
17
+ import { PiAdapter } from "../harnesses/pi/PiAdapter.js";
18
+ import { GeminiCliAdapter } from "../harnesses/gemini/GeminiCliAdapter.js";
19
+ import { CopilotAdapter } from "../harnesses/copilot/CopilotAdapter.js";
20
+ import { AgentRegistry, type RegistryEntry } from "../utils/AgentRegistry.js";
21
+ import { expand, expandNow, normalizeAgents, withFrozenClock } from "../fixtures/bundle.js";
22
+ import type { FixtureBundle } from "../fixtures/bundle.js";
23
+ import { captureLive, FIXTURES_ROOT } from "../fixtures/capture.js";
24
+
25
+ const bundlePaths = fs.existsSync(FIXTURES_ROOT)
26
+ ? fs
27
+ .readdirSync(FIXTURES_ROOT, { withFileTypes: true })
28
+ .flatMap((d) =>
29
+ d.isDirectory()
30
+ ? fs
31
+ .readdirSync(path.join(FIXTURES_ROOT, d.name))
32
+ .filter((f) => f.endsWith(".json"))
33
+ .map((f) => path.join(FIXTURES_ROOT, d.name, f))
34
+ : [],
35
+ )
36
+ : [];
37
+
38
+ /** Adapters whose detect path consults AgentRegistry. */
39
+ const REGISTRY_AWARE = new Set(["codex", "pi", "gemini_cli", "copilot"]);
40
+
41
+ function adapterFor(type: string, registry?: AgentRegistry) {
42
+ // Adapters that consult the registry during detection get the seeded
43
+ // isolated instance so replay never reads real ~/.ai-devkit state.
44
+ if (type === "codex" && registry) return new CodexAdapter(registry);
45
+ if (type === "pi" && registry) return new PiAdapter(registry);
46
+ if (type === "gemini_cli" && registry) return new GeminiCliAdapter(registry);
47
+ if (type === "copilot" && registry) return new CopilotAdapter(registry);
48
+ const adapter = createBuiltinAdapters().find((a) => a.type === type);
49
+ if (!adapter) throw new Error(`no builtin adapter for "${type}"`);
50
+ return adapter;
51
+ }
52
+
53
+ /** Seed an isolated registry at `<home>/.ai-devkit/agents.json`. */
54
+ function seedRegistry(bundle: FixtureBundle, home: string, nowIso: string): AgentRegistry | undefined {
55
+ if (!REGISTRY_AWARE.has(bundle.adapter)) return undefined;
56
+ const registry = new AgentRegistry(path.join(home, ".ai-devkit", "agents.json"));
57
+ const entries = (bundle.registry ?? []).map(
58
+ (e) =>
59
+ ({
60
+ name: "",
61
+ type: bundle.adapter,
62
+ runtime: "tmux",
63
+ runtimeRef: null,
64
+ cwd: "",
65
+ startedAt: nowIso,
66
+ sessionId: "",
67
+ sessionFilePath: "",
68
+ pinned: false,
69
+ ...expand(expandNow(e as Record<string, unknown>, nowIso), home),
70
+ }) as RegistryEntry,
71
+ );
72
+ registry.registerBatch(entries);
73
+ return registry;
74
+ }
75
+
76
+ /** Materialize a bundle into a temp HOME; returns cleanup + replay inputs. */
77
+ function materialize(bundle: FixtureBundle) {
78
+ const home = fs.mkdtempSync(path.join(os.tmpdir(), "fixture-home-"));
79
+ // "$TODAY" resolves to the local `YYYY/MM/DD` day key at write time — Codex
80
+ // date-dir discovery derives day keys from process start times in local
81
+ // time, so static dirs would drift across replay days/timezones.
82
+ const now = new Date();
83
+ const todayKey = [
84
+ String(now.getFullYear()).padStart(4, "0"),
85
+ String(now.getMonth() + 1).padStart(2, "0"),
86
+ String(now.getDate()).padStart(2, "0"),
87
+ ].join("/");
88
+ for (const [rel, content] of Object.entries(bundle.home)) {
89
+ const p = path.join(home, expand(rel.split("$TODAY").join(todayKey), home));
90
+ fs.mkdirSync(path.dirname(p), { recursive: true });
91
+ fs.writeFileSync(p, expand(content.split("$TODAY").join(todayKey), home));
92
+ }
93
+ // SQLite stores (OpenCode's opencode.db) materialize from ordered SQL
94
+ // statements — binary dbs can't travel in the JSON `home` map.
95
+ for (const [rel, stmts] of Object.entries(bundle.sqlite ?? {})) {
96
+ const p = path.join(home, expand(rel.split("$TODAY").join(todayKey), home));
97
+ fs.mkdirSync(path.dirname(p), { recursive: true });
98
+ const db = new Database(p);
99
+ try {
100
+ for (const stmt of stmts) {
101
+ db.exec(expand(stmt.split("$TODAY").join(todayKey), home));
102
+ }
103
+ } finally {
104
+ db.close();
105
+ }
106
+ }
107
+ // "$NOW" resolves to the instant home files were written — legacy
108
+ // cwd+birthtime matching compares process start times against real file
109
+ // creation times, so a static timestamp would drift out of tolerance.
110
+ const nowIso = new Date().toISOString();
111
+ // Deterministic mtimes for adapters that read them (Grok lastActive /
112
+ // latest-session pick) — "$NOW" resolves like the process sentinel.
113
+ for (const [rel, mt] of Object.entries(bundle.mtimes ?? {})) {
114
+ const p = path.join(home, expand(rel.split("$TODAY").join(todayKey), home));
115
+ const ms = mt === "$NOW" ? Date.parse(nowIso) : mt;
116
+ fs.utimesSync(p, ms / 1000, ms / 1000);
117
+ }
118
+ // JSON round-tripped Date fields (e.g. ProcessInfo.startTime) come back as
119
+ // ISO strings — adapters expect real Date instances.
120
+ const processes = expandNow(expand(bundle.processes, home), nowIso).map((p) => ({
121
+ ...p,
122
+ startTime: p.startTime ? new Date(p.startTime) : undefined,
123
+ }));
124
+ const frozenNow = bundle.frozenNow === "$NOW" ? Date.parse(nowIso) : bundle.frozenNow;
125
+ // Pids whose startTime resolves to the materialization instant — Codex
126
+ // derives session day-dirs from start times, so only these paths contain
127
+ // the replay-day key and normalize to $TODAY.
128
+ const nowPids = new Set(
129
+ bundle.processes.filter((p) => p.startTime === "$NOW").map((p) => p.pid),
130
+ );
131
+ return {
132
+ home,
133
+ processes,
134
+ frozenNow,
135
+ todayKey,
136
+ // Only $NOW resolves — $FIXTURE_HOME and $TODAY stay literal to match
137
+ // the normalized actual form.
138
+ expected: expandNow(bundle.expected, nowIso),
139
+ cleanup: () => fs.rmSync(home, { recursive: true, force: true }),
140
+ nowIso,
141
+ nowPids,
142
+ };
143
+ }
144
+
145
+ describe("fixture replay (TS parity oracle)", () => {
146
+ const saved = process.env.HOME;
147
+ afterEach(() => {
148
+ if (saved === undefined) delete process.env.HOME;
149
+ else process.env.HOME = saved;
150
+ });
151
+
152
+ if (bundlePaths.length === 0) {
153
+ it.todo("no fixture bundles committed yet — run AI_DEVKIT_FIXTURE_CAPTURE=1");
154
+ return;
155
+ }
156
+
157
+ for (const bundlePath of bundlePaths) {
158
+ it(`replays ${path.relative(FIXTURES_ROOT, bundlePath)}`, async () => {
159
+ const bundle: FixtureBundle = JSON.parse(fs.readFileSync(bundlePath, "utf8"));
160
+ const { home, processes, frozenNow, expected, cleanup, todayKey, nowIso, nowPids } =
161
+ materialize(bundle);
162
+ // Pin XDG to the fixture's canonical share dir — an ambient
163
+ // XDG_DATA_HOME would resolve the real opencode.db into replay.
164
+ const savedXdg = process.env.XDG_DATA_HOME;
165
+ // Same for ANTIGRAVITY_CLI_HOME — clearing it falls the adapter
166
+ // back to the pinned HOME's canonical base dir.
167
+ const savedAgy = process.env.ANTIGRAVITY_CLI_HOME;
168
+ try {
169
+ process.env.HOME = home;
170
+ process.env.XDG_DATA_HOME = path.join(home, ".local", "share");
171
+ delete process.env.ANTIGRAVITY_CLI_HOME;
172
+ const registry = seedRegistry(bundle, home, nowIso);
173
+ const agents = await withFrozenClock(frozenNow, () =>
174
+ adapterFor(bundle.adapter, registry).detectAgents({ processes }),
175
+ );
176
+ // Day-key dirs normalize back to $TODAY only for agents whose
177
+ // process startTime was $NOW — static-start agents keep concrete
178
+ // dates so live bundles stay replayable on any day.
179
+ const actual = JSON.parse(JSON.stringify(normalizeAgents(agents, home))).map((a) =>
180
+ nowPids.has(a.pid)
181
+ ? JSON.parse(JSON.stringify(a).split(todayKey).join("$TODAY"))
182
+ : a,
183
+ );
184
+ expect(actual).toEqual(expected);
185
+ } finally {
186
+ if (savedXdg === undefined) delete process.env.XDG_DATA_HOME;
187
+ else process.env.XDG_DATA_HOME = savedXdg;
188
+ if (savedAgy === undefined) delete process.env.ANTIGRAVITY_CLI_HOME;
189
+ else process.env.ANTIGRAVITY_CLI_HOME = savedAgy;
190
+ cleanup();
191
+ }
192
+ });
193
+ }
194
+ });
195
+
196
+ // Opt-in live capture: AI_DEVKIT_FIXTURE_CAPTURE=1 npx vitest run fixtures
197
+ describe.runIf(process.env.AI_DEVKIT_FIXTURE_CAPTURE === "1")("fixture capture (live)", () => {
198
+ it("captures claude bundle from the live machine", async () => {
199
+ const out = await captureLive(adapterFor("claude"), "live");
200
+ expect(fs.existsSync(out)).toBe(true);
201
+ }, 30000);
202
+
203
+ it("captures codex bundle from the live machine", async () => {
204
+ const out = await captureLive(adapterFor("codex"), "live");
205
+ expect(fs.existsSync(out)).toBe(true);
206
+ }, 30000);
207
+
208
+ it("captures pi bundle from the live machine", async () => {
209
+ const out = await captureLive(adapterFor("pi"), "live");
210
+ expect(fs.existsSync(out)).toBe(true);
211
+ }, 30000);
212
+
213
+ it("captures gemini_cli bundle from the live machine", async () => {
214
+ const out = await captureLive(adapterFor("gemini_cli"), "live");
215
+ expect(fs.existsSync(out)).toBe(true);
216
+ }, 30000);
217
+
218
+ it("captures copilot bundle from the live machine", async () => {
219
+ const out = await captureLive(adapterFor("copilot"), "live");
220
+ expect(fs.existsSync(out)).toBe(true);
221
+ }, 30000);
222
+
223
+ it("captures grok_cli bundle from the live machine", async () => {
224
+ const out = await captureLive(adapterFor("grok_cli"), "live");
225
+ expect(fs.existsSync(out)).toBe(true);
226
+ }, 30000);
227
+
228
+ it("captures opencode bundle from the live machine", async () => {
229
+ const out = await captureLive(adapterFor("opencode"), "live");
230
+ expect(fs.existsSync(out)).toBe(true);
231
+ }, 30000);
232
+
233
+ it("captures devin bundle from the live machine", async () => {
234
+ const out = await captureLive(adapterFor("devin"), "live");
235
+ expect(fs.existsSync(out)).toBe(true);
236
+ }, 30000);
237
+
238
+ it("captures kiro bundle from the live machine", async () => {
239
+ const out = await captureLive(adapterFor("kiro"), "live");
240
+ expect(fs.existsSync(out)).toBe(true);
241
+ }, 30000);
242
+
243
+ it("captures antigravity_cli bundle from the live machine", async () => {
244
+ const out = await captureLive(adapterFor("antigravity_cli"), "live");
245
+ expect(fs.existsSync(out)).toBe(true);
246
+ }, 30000);
247
+ });