@blackbelt-technology/pi-dashboard-extension 0.5.4 → 0.6.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 (254) hide show
  1. package/.pi/skills/browser/LICENSE +201 -0
  2. package/.pi/skills/browser/SKILL.md +105 -0
  3. package/.pi/skills/browser/UPSTREAM.md +46 -0
  4. package/.pi/skills/browser/references/electron.md +351 -0
  5. package/.pi/skills/browser/references/web.md +2789 -0
  6. package/.pi/skills/browser/scripts/detect-dashboard.sh +55 -0
  7. package/.pi/skills/doctor/AGENTS.md +27 -0
  8. package/.pi/skills/doctor/SKILL.md +89 -0
  9. package/.pi/skills/doctor/_lib/checks.ts +177 -0
  10. package/.pi/skills/doctor/_lib/derive-tokens.ts +43 -0
  11. package/.pi/skills/doctor/_lib/front-matter.ts +122 -0
  12. package/.pi/skills/doctor/_lib/index.ts +14 -0
  13. package/.pi/skills/doctor/_lib/knowledge-hash.ts +74 -0
  14. package/.pi/skills/doctor/_lib/provenance.ts +50 -0
  15. package/.pi/skills/doctor/_lib/regenerate.ts +100 -0
  16. package/.pi/skills/doctor/_lib/router.ts +155 -0
  17. package/.pi/skills/doctor/_lib/server-tier.ts +62 -0
  18. package/.pi/skills/doctor/modules/build-reload.knowledge.hash +1 -0
  19. package/.pi/skills/doctor/modules/build-reload.md +51 -0
  20. package/.pi/skills/doctor/modules/env-node.knowledge.hash +1 -0
  21. package/.pi/skills/doctor/modules/env-node.md +49 -0
  22. package/.pi/skills/doctor/modules/install-topology.knowledge.hash +1 -0
  23. package/.pi/skills/doctor/modules/install-topology.md +53 -0
  24. package/.pi/skills/doctor/modules/model-resolution.knowledge.hash +1 -0
  25. package/.pi/skills/doctor/modules/model-resolution.md +53 -0
  26. package/.pi/skills/doctor/modules/peers.knowledge.hash +1 -0
  27. package/.pi/skills/doctor/modules/peers.md +57 -0
  28. package/.pi/skills/doctor/modules/pi-resolution.knowledge.hash +1 -0
  29. package/.pi/skills/doctor/modules/pi-resolution.md +60 -0
  30. package/.pi/skills/doctor/modules/plugins-bridges.knowledge.hash +1 -0
  31. package/.pi/skills/doctor/modules/plugins-bridges.md +56 -0
  32. package/.pi/skills/pi-dashboard/SKILL.md +68 -0
  33. package/.pi/skills/pi-dashboard/commands/README.md +45 -0
  34. package/.pi/skills/pi-dashboard/commands/dashboard-flow-abort.md +10 -0
  35. package/.pi/skills/pi-dashboard/commands/dashboard-flow-auto.md +10 -0
  36. package/.pi/skills/pi-dashboard/commands/dashboard-git-branches.md +6 -0
  37. package/.pi/skills/pi-dashboard/commands/dashboard-git-init.md +8 -0
  38. package/.pi/skills/pi-dashboard/commands/dashboard-git-stash-pop.md +8 -0
  39. package/.pi/skills/pi-dashboard/commands/dashboard-list-models.md +10 -0
  40. package/.pi/skills/pi-dashboard/commands/dashboard-peer-list.md +6 -0
  41. package/.pi/skills/pi-dashboard/commands/dashboard-peer-scan.md +6 -0
  42. package/.pi/skills/pi-dashboard/commands/dashboard-pin-list.md +5 -0
  43. package/.pi/skills/pi-dashboard/commands/dashboard-proposal-archive.md +6 -0
  44. package/.pi/skills/pi-dashboard/commands/dashboard-proposal-attach.md +10 -0
  45. package/.pi/skills/pi-dashboard/commands/dashboard-proposal-detach.md +10 -0
  46. package/.pi/skills/pi-dashboard/commands/dashboard-server-config.md +5 -0
  47. package/.pi/skills/pi-dashboard/commands/dashboard-server-health.md +6 -0
  48. package/.pi/skills/pi-dashboard/commands/dashboard-server-tunnel-off.md +6 -0
  49. package/.pi/skills/pi-dashboard/commands/dashboard-server-tunnel-on.md +6 -0
  50. package/.pi/skills/pi-dashboard/commands/dashboard-server-tunnel-status.md +6 -0
  51. package/.pi/skills/pi-dashboard/commands/dashboard-session-abort-all.md +14 -0
  52. package/.pi/skills/pi-dashboard/commands/dashboard-session-abort.md +10 -0
  53. package/.pi/skills/pi-dashboard/commands/dashboard-session-diff.md +11 -0
  54. package/.pi/skills/pi-dashboard/commands/dashboard-session-fork.md +10 -0
  55. package/.pi/skills/pi-dashboard/commands/dashboard-session-hide.md +10 -0
  56. package/.pi/skills/pi-dashboard/commands/dashboard-session-info.md +11 -0
  57. package/.pi/skills/pi-dashboard/commands/dashboard-session-kill.md +10 -0
  58. package/.pi/skills/pi-dashboard/commands/dashboard-session-list-active.md +8 -0
  59. package/.pi/skills/pi-dashboard/commands/dashboard-session-list-here.md +8 -0
  60. package/.pi/skills/pi-dashboard/commands/dashboard-session-list.md +6 -0
  61. package/.pi/skills/pi-dashboard/commands/dashboard-session-model.md +10 -0
  62. package/.pi/skills/pi-dashboard/commands/dashboard-session-rename.md +10 -0
  63. package/.pi/skills/pi-dashboard/commands/dashboard-session-resume.md +10 -0
  64. package/.pi/skills/pi-dashboard/commands/dashboard-session-spawn.md +10 -0
  65. package/.pi/skills/pi-dashboard/commands/dashboard-session-tell.md +10 -0
  66. package/.pi/skills/pi-dashboard/commands/dashboard-session-thinking.md +10 -0
  67. package/.pi/skills/pi-dashboard/commands/dashboard-session-unhide.md +10 -0
  68. package/.pi/skills/pi-dashboard/references/api-reference.md +30 -0
  69. package/.pi/skills/pi-dashboard/references/recipes.md +38 -0
  70. package/.pi/skills/pi-dashboard/references/slash-commands.md +71 -0
  71. package/.pi/skills/pi-dashboard/scripts/dashboard-api.sh +6 -0
  72. package/.pi/skills/pi-dashboard/scripts/dashboard-bus.ts +217 -0
  73. package/.pi/skills/project-init/SKILL.md +279 -0
  74. package/.pi/skills/project-init/dox-doctrine.md +104 -0
  75. package/.pi/skills/project-init/profiles/coding/AGENTS.md.tmpl +111 -0
  76. package/.pi/skills/project-init/profiles/coding/profile.json +5 -0
  77. package/.pi/skills/project-init/profiles/coding/prompts/plan-change.md +5 -0
  78. package/.pi/skills/project-init/profiles/coding/prompts/tdd-loop.md +6 -0
  79. package/.pi/skills/project-init/profiles/coding/settings.json.tmpl +9 -0
  80. package/.pi/skills/project-init/profiles/docs/AGENTS.md.tmpl +37 -0
  81. package/.pi/skills/project-init/profiles/docs/profile.json +4 -0
  82. package/.pi/skills/project-init/profiles/docs/prompts/draft-page.md +5 -0
  83. package/.pi/skills/project-init/profiles/docs/settings.json.tmpl +9 -0
  84. package/package.json +12 -4
  85. package/src/AGENTS.md +56 -0
  86. package/src/__tests__/abort-latch.test.ts +97 -0
  87. package/src/__tests__/agent-settled.test.ts +82 -0
  88. package/src/__tests__/artifact-roots.test.ts +83 -0
  89. package/src/__tests__/ask-user-attachments.test.ts +106 -0
  90. package/src/__tests__/ask-user-tool.test.ts +90 -27
  91. package/src/__tests__/auto-session-namer.test.ts +339 -0
  92. package/src/__tests__/bridge-default-model-apply.test.ts +242 -0
  93. package/src/__tests__/bridge-default-model-gate.test.ts +6 -3
  94. package/src/__tests__/bridge-followup-mutation.test.ts +217 -0
  95. package/src/__tests__/bridge-followup-queue-drain.test.ts +266 -0
  96. package/src/__tests__/bridge-hasui-flip.test.ts +109 -0
  97. package/src/__tests__/bridge-no-queue-mutation.test.ts +112 -0
  98. package/src/__tests__/bridge-plugin-emit-event.test.ts +44 -0
  99. package/src/__tests__/bridge-shutdown-reset.test.ts +57 -136
  100. package/src/__tests__/bridge-slash-command-routing.test.ts +7 -7
  101. package/src/__tests__/bridge-stop-after-turn.test.ts +72 -0
  102. package/src/__tests__/bridge-system-followup.test.ts +163 -0
  103. package/src/__tests__/bridge-thinking-level-select.test.ts +68 -0
  104. package/src/__tests__/browser-skill-registered.test.ts +84 -0
  105. package/src/__tests__/build-provider-catalogue.test.ts +35 -1
  106. package/src/__tests__/command-handler.test.ts +331 -35
  107. package/src/__tests__/commit-draft.test.ts +100 -0
  108. package/src/__tests__/connection-dropped-frames.test.ts +78 -0
  109. package/src/__tests__/custom-provider-apikey-roundtrip.test.ts +128 -0
  110. package/src/__tests__/cwd-missing-probe.test.ts +82 -0
  111. package/src/__tests__/dashboard-commands-shipped.test.ts +53 -0
  112. package/src/__tests__/dashboard-context-injector.test.ts +190 -0
  113. package/src/__tests__/doctor/checks.test.ts +121 -0
  114. package/src/__tests__/doctor/modules.test.ts +64 -0
  115. package/src/__tests__/doctor/provenance-hash.test.ts +94 -0
  116. package/src/__tests__/doctor/router.test.ts +143 -0
  117. package/src/__tests__/empty-actionable-guard-config.test.ts +32 -0
  118. package/src/__tests__/empty-actionable-guard.test.ts +73 -0
  119. package/src/__tests__/enrich-model-metadata.test.ts +21 -0
  120. package/src/__tests__/extension-slash-command-detection.test.ts +37 -1
  121. package/src/__tests__/filter-enabled-models.test.ts +151 -0
  122. package/src/__tests__/fixtures/turn-actionability-fixtures.ts +69 -0
  123. package/src/__tests__/git-info-status.test.ts +73 -0
  124. package/src/__tests__/git-poll.test.ts +69 -0
  125. package/src/__tests__/markdown-image-inliner.test.ts +37 -0
  126. package/src/__tests__/model-resolve.test.ts +379 -0
  127. package/src/__tests__/pi-version-tracker.test.ts +109 -0
  128. package/src/__tests__/process-scanner.test.ts +112 -22
  129. package/src/__tests__/project-init-detect-stack.test.ts +61 -0
  130. package/src/__tests__/project-init-dox-kb-config.test.ts +53 -0
  131. package/src/__tests__/project-init-profiles.test.ts +84 -0
  132. package/src/__tests__/project-init-scaffold.test.ts +139 -0
  133. package/src/__tests__/project-init-seed-doctrine.test.ts +100 -0
  134. package/src/__tests__/project-trust.test.ts +92 -0
  135. package/src/__tests__/prompt-expander.test.ts +191 -1
  136. package/src/__tests__/provider-register-reload.test.ts +36 -6
  137. package/src/__tests__/provider-register-thinking-levels.test.ts +51 -0
  138. package/src/__tests__/retry-tracker.test.ts +75 -70
  139. package/src/__tests__/role-manager.test.ts +585 -0
  140. package/src/__tests__/role-model-tools-registry-readiness.test.ts +114 -0
  141. package/src/__tests__/role-model-tools.test.ts +229 -0
  142. package/src/__tests__/search-files-completeness.test.ts +111 -0
  143. package/src/__tests__/search-files-ranking.test.ts +168 -0
  144. package/src/__tests__/server-auto-start.test.ts +121 -4
  145. package/src/__tests__/server-launcher-launch.test.ts +24 -6
  146. package/src/__tests__/server-launcher.test.ts +19 -2
  147. package/src/__tests__/session-sync.test.ts +36 -0
  148. package/src/__tests__/subagent-frame-buffer.test.ts +239 -0
  149. package/src/__tests__/tool-result-image-inliner.test.ts +206 -0
  150. package/src/__tests__/tui-prompt-adapter.test.ts +70 -64
  151. package/src/__tests__/turn-actionability.test.ts +86 -0
  152. package/src/__tests__/vcs-info.test.ts +145 -4
  153. package/src/__tests__/visibility-intent.test.ts +43 -0
  154. package/src/abort-latch.ts +55 -0
  155. package/src/abort-latch.ts.AGENTS.md +3 -0
  156. package/src/agent-settled.ts +72 -0
  157. package/src/agent-settled.ts.AGENTS.md +3 -0
  158. package/src/artifact-roots.ts +73 -0
  159. package/src/artifact-roots.ts.AGENTS.md +3 -0
  160. package/src/ask-user-attachments.ts +121 -0
  161. package/src/ask-user-attachments.ts.AGENTS.md +3 -0
  162. package/src/ask-user-tool.ts +56 -58
  163. package/src/ask-user-tool.ts.AGENTS.md +3 -0
  164. package/src/auto-session-namer.ts +334 -0
  165. package/src/auto-session-namer.ts.AGENTS.md +3 -0
  166. package/src/bridge-context.ts +86 -8
  167. package/src/bridge-context.ts.AGENTS.md +3 -0
  168. package/src/bridge-default-model-gate.ts +22 -6
  169. package/src/bridge-default-model-gate.ts.AGENTS.md +3 -0
  170. package/src/bridge.ts +1241 -360
  171. package/src/bridge.ts.AGENTS.md +3 -0
  172. package/src/canvas-tool.ts +107 -0
  173. package/src/command-handler.ts +490 -51
  174. package/src/command-handler.ts.AGENTS.md +3 -0
  175. package/src/commit-draft-agent.ts +114 -0
  176. package/src/commit-draft-agent.ts.AGENTS.md +3 -0
  177. package/src/commit-draft.ts +136 -0
  178. package/src/commit-draft.ts.AGENTS.md +3 -0
  179. package/src/connection.ts +32 -1
  180. package/src/connection.ts.AGENTS.md +3 -0
  181. package/src/dashboard-context-injector.ts +124 -0
  182. package/src/dashboard-context-injector.ts.AGENTS.md +3 -0
  183. package/src/dashboard-default-adapter.ts.AGENTS.md +3 -0
  184. package/src/dev-build.ts.AGENTS.md +3 -0
  185. package/src/empty-actionable-guard-config.ts +32 -0
  186. package/src/empty-actionable-guard-config.ts.AGENTS.md +3 -0
  187. package/src/empty-actionable-guard.ts +90 -0
  188. package/src/empty-actionable-guard.ts.AGENTS.md +3 -0
  189. package/src/event-forwarder.ts.AGENTS.md +3 -0
  190. package/src/flow-event-wiring.ts +4 -14
  191. package/src/flow-event-wiring.ts.AGENTS.md +3 -0
  192. package/src/git-link-builder.ts.AGENTS.md +3 -0
  193. package/src/git-poll.ts +38 -0
  194. package/src/git-poll.ts.AGENTS.md +3 -0
  195. package/src/hasui-flip.ts +56 -0
  196. package/src/hasui-flip.ts.AGENTS.md +3 -0
  197. package/src/markdown-image-inliner.ts +45 -2
  198. package/src/markdown-image-inliner.ts.AGENTS.md +3 -0
  199. package/src/model-tracker.ts +145 -19
  200. package/src/model-tracker.ts.AGENTS.md +3 -0
  201. package/src/multiselect-decode.ts.AGENTS.md +3 -0
  202. package/src/multiselect-list.ts.AGENTS.md +3 -0
  203. package/src/multiselect-polyfill.ts.AGENTS.md +3 -0
  204. package/src/process-metrics.ts.AGENTS.md +3 -0
  205. package/src/process-scanner.ts +114 -103
  206. package/src/process-scanner.ts.AGENTS.md +3 -0
  207. package/src/project-init/AGENTS.md +12 -0
  208. package/src/project-init/detect-stack.ts +146 -0
  209. package/src/project-init/dox-kb-config.ts +47 -0
  210. package/src/project-init/profiles.ts +135 -0
  211. package/src/project-init/scaffold.ts +192 -0
  212. package/src/project-init/seed-doctrine.ts +95 -0
  213. package/src/project-trust.ts +63 -0
  214. package/src/project-trust.ts.AGENTS.md +3 -0
  215. package/src/prompt-bus.ts +10 -1
  216. package/src/prompt-bus.ts.AGENTS.md +3 -0
  217. package/src/prompt-expander.ts +238 -37
  218. package/src/prompt-expander.ts.AGENTS.md +3 -0
  219. package/src/provider-register.ts +323 -54
  220. package/src/provider-register.ts.AGENTS.md +3 -0
  221. package/src/retry-tracker.ts +64 -39
  222. package/src/retry-tracker.ts.AGENTS.md +3 -0
  223. package/src/role-manager.ts +436 -0
  224. package/src/role-manager.ts.AGENTS.md +3 -0
  225. package/src/role-model-tools.ts +301 -0
  226. package/src/role-model-tools.ts.AGENTS.md +3 -0
  227. package/src/server-auto-start.ts +59 -23
  228. package/src/server-auto-start.ts.AGENTS.md +3 -0
  229. package/src/server-launcher.ts +56 -16
  230. package/src/server-launcher.ts.AGENTS.md +3 -0
  231. package/src/session-sync.ts +111 -15
  232. package/src/session-sync.ts.AGENTS.md +3 -0
  233. package/src/slash-dispatch.ts +13 -11
  234. package/src/slash-dispatch.ts.AGENTS.md +3 -0
  235. package/src/source-detector.ts.AGENTS.md +3 -0
  236. package/src/subagent-frame-buffer.ts +220 -0
  237. package/src/subagent-frame-buffer.ts.AGENTS.md +3 -0
  238. package/src/tool-result-image-inliner.ts +204 -0
  239. package/src/tool-result-image-inliner.ts.AGENTS.md +3 -0
  240. package/src/tui-prompt-adapter.ts +123 -0
  241. package/src/tui-prompt-adapter.ts.AGENTS.md +3 -0
  242. package/src/turn-actionability.ts +117 -0
  243. package/src/turn-actionability.ts.AGENTS.md +3 -0
  244. package/src/ui-modules.ts.AGENTS.md +3 -0
  245. package/src/vcs-info.ts +91 -129
  246. package/src/vcs-info.ts.AGENTS.md +3 -0
  247. package/src/visibility-intent.ts +34 -0
  248. package/src/visibility-intent.ts.AGENTS.md +3 -0
  249. package/src/__tests__/bridge-retry-ordering.test.ts +0 -148
  250. package/src/__tests__/bridge-shadow-queue-drain.test.ts +0 -221
  251. package/src/__tests__/fixtures/usage-limit-error-strings.ts +0 -127
  252. package/src/__tests__/usage-limit-orderer.test.ts +0 -117
  253. package/src/__tests__/vcs-info-jj.test.ts +0 -145
  254. package/src/usage-limit-orderer.ts +0 -93
@@ -1,13 +1,17 @@
1
1
  /**
2
2
  * Pins the Bridge → `launchDashboardServer` forwarding contract: the
3
3
  * extension's `launchServer` must always pass `starter: "Bridge"`,
4
- * `stdio: "ignore"`, and `healthTimeoutMs: 2_000`. The shared
5
- * launcher is mocked so this test never spawns a real server.
4
+ * `stdio: { logFile: getDashboardServerLogPath() }`, and
5
+ * `healthTimeoutMs: 10_000`. The shared launcher is mocked so this test
6
+ * never spawns a real server.
6
7
  *
7
- * See change: unify-server-launch-ts-loader (§3.1.2).
8
+ * See change: unify-server-launch-ts-loader (§3.1.2),
9
+ * fix-bridge-server-start-diagnostics (bridge now owns the server log
10
+ * and uses a 10 s cold-start window).
8
11
  */
9
12
  import { describe, it, expect, vi, beforeEach } from "vitest";
10
13
  import type { launchDashboardServer } from "@blackbelt-technology/pi-dashboard-shared/server-launcher.js";
14
+ import { getDashboardServerLogPath } from "@blackbelt-technology/pi-dashboard-shared/dashboard-paths.js";
11
15
 
12
16
  const { launchSpy } = vi.hoisted(() => ({
13
17
  launchSpy: vi.fn<typeof launchDashboardServer>(async () => ({ childPid: 1, reportedPid: 1, healthOk: true as const })),
@@ -46,14 +50,14 @@ beforeEach(() => {
46
50
  });
47
51
 
48
52
  describe("Bridge launchServer → launchDashboardServer forwarding", () => {
49
- it("passes starter:Bridge + stdio:ignore + 2s health timeout + port", async () => {
53
+ it("passes starter:Bridge + stdio:{logFile} + 10s health timeout + port", async () => {
50
54
  const r = await launchServer(cfg);
51
55
  expect(r.success).toBe(true);
52
56
  expect(launchSpy).toHaveBeenCalledOnce();
53
57
  const opts = launchSpy.mock.calls[0]![0]!;
54
58
  expect(opts.starter).toBe("Bridge");
55
- expect(opts.stdio).toBe("ignore");
56
- expect(opts.healthTimeoutMs).toBe(2000);
59
+ expect(opts.stdio).toEqual({ logFile: getDashboardServerLogPath() });
60
+ expect(opts.healthTimeoutMs).toBe(10000);
57
61
  expect(opts.port).toBe(3000);
58
62
  expect(opts.extraArgs).toEqual(["--port", "3000", "--pi-port", "4000"]);
59
63
  });
@@ -75,4 +79,18 @@ describe("Bridge launchServer → launchDashboardServer forwarding", () => {
75
79
  expect(r.success).toBe(false);
76
80
  expect(r.message).toMatch(/code=17/);
77
81
  });
82
+
83
+ // fix-bridge-server-start-diagnostics: the EarlyExitError copy must point
84
+ // at the path the bridge spawn actually writes (getDashboardServerLogPath),
85
+ // not a hardcoded "~/.pi/dashboard/server.log" string.
86
+ it("EarlyExitError message references getDashboardServerLogPath(), not a hardcoded path", async () => {
87
+ const { EarlyExitError } = await import("@blackbelt-technology/pi-dashboard-shared/server-launcher.js");
88
+ const err = new (EarlyExitError as unknown as new (...args: unknown[]) => Error & { code: number })();
89
+ err.code = 1;
90
+ launchSpy.mockRejectedValueOnce(err);
91
+ const r = await launchServer(cfg);
92
+ expect(r.success).toBe(false);
93
+ expect(r.message).toContain(getDashboardServerLogPath());
94
+ expect(r.message).not.toContain("~/.pi/dashboard/server.log");
95
+ });
78
96
  });
@@ -1,7 +1,7 @@
1
- import { describe, it, expect } from "vitest";
2
- import { resolveServerCliPath, buildSpawnArgs, buildSpawnEnv } from "../server-launcher.js";
3
1
  import { existsSync } from "node:fs";
4
2
  import path from "node:path";
3
+ import { describe, expect, it } from "vitest";
4
+ import { buildSpawnArgs, buildSpawnEnv, resolveServerCliPath } from "../server-launcher.js";
5
5
 
6
6
  describe("server-launcher", () => {
7
7
  describe("resolveServerCliPath", () => {
@@ -80,5 +80,22 @@ describe("server-launcher", () => {
80
80
  const env = buildSpawnEnv({ DEFINED: "yes", UNDEF: undefined });
81
81
  expect(Object.keys(env)).not.toContain("UNDEF");
82
82
  });
83
+
84
+ it("adds --max-old-space-size to NODE_OPTIONS by default", () => {
85
+ const env = buildSpawnEnv({});
86
+ expect(env["NODE_OPTIONS"]).toContain("--max-old-space-size=8192");
87
+ });
88
+
89
+ it("appends the flag to an existing NODE_OPTIONS without a heap limit", () => {
90
+ const env = buildSpawnEnv({ NODE_OPTIONS: "--enable-source-maps" });
91
+ expect(env["NODE_OPTIONS"]).toBe(
92
+ "--enable-source-maps --max-old-space-size=8192",
93
+ );
94
+ });
95
+
96
+ it("never overrides a user-supplied --max-old-space-size", () => {
97
+ const env = buildSpawnEnv({ NODE_OPTIONS: "--max-old-space-size=2048" });
98
+ expect(env["NODE_OPTIONS"]).toBe("--max-old-space-size=2048");
99
+ });
83
100
  });
84
101
  });
@@ -35,6 +35,7 @@ function createMockBridgeContext(overrides?: Partial<BridgeContext>): BridgeCont
35
35
  lastGitPrNumber: undefined,
36
36
  lastSessionName: undefined,
37
37
  hasRegisteredOnce: false,
38
+ dashboardSpawned: false,
38
39
  ...overrides,
39
40
  // Expose sent messages for assertions
40
41
  _sent: sent,
@@ -183,6 +184,41 @@ describe("sendStateSync: spawnToken from env", () => {
183
184
  });
184
185
  });
185
186
 
187
+ // See change: fix-spawn-token-env-leak.
188
+ it("first register emits token + dashboardSpawned:true and scrubs the env var", () => {
189
+ withEnvVar("tok_first", () => {
190
+ const bc = createMockBridgeContext({ hasRegisteredOnce: false, dashboardSpawned: true } as any);
191
+ sendStateSync(bc, () => []);
192
+ const registerMsg = (bc as any)._sent.find((m: any) => m.type === "session_register");
193
+ expect(registerMsg.spawnToken).toBe("tok_first");
194
+ expect(registerMsg.dashboardSpawned).toBe(true);
195
+ // Single-use token scrubbed so descendants can't inherit it.
196
+ expect(process.env.PI_DASHBOARD_SPAWN_TOKEN).toBeUndefined();
197
+ });
198
+ });
199
+
200
+ it("second register (reattach) omits token but keeps dashboardSpawned:true via capture-once boolean", () => {
201
+ // Env already scrubbed (token gone) but the captured boolean persists.
202
+ withEnvVar(undefined, () => {
203
+ const bc = createMockBridgeContext({ hasRegisteredOnce: true, dashboardSpawned: true } as any);
204
+ sendStateSync(bc, () => []);
205
+ const registerMsg = (bc as any)._sent.find((m: any) => m.type === "session_register");
206
+ expect(registerMsg.spawnToken).toBeUndefined();
207
+ expect(registerMsg.dashboardSpawned).toBe(true);
208
+ expect(registerMsg.registerReason).toBe("reattach");
209
+ });
210
+ });
211
+
212
+ it("descendant child (no token, dashboardSpawned:false) emits neither field", () => {
213
+ withEnvVar(undefined, () => {
214
+ const bc = createMockBridgeContext({ hasRegisteredOnce: false, dashboardSpawned: false } as any);
215
+ sendStateSync(bc, () => []);
216
+ const registerMsg = (bc as any)._sent.find((m: any) => m.type === "session_register");
217
+ expect(registerMsg.spawnToken).toBeUndefined();
218
+ expect(registerMsg.dashboardSpawned).toBeUndefined();
219
+ });
220
+ });
221
+
186
222
  it("handleSessionChange register omits spawnToken (in-process new/fork/resume)", () => {
187
223
  withEnvVar("tok_first", () => {
188
224
  const bc = createMockBridgeContext({ hasRegisteredOnce: true } as any);
@@ -0,0 +1,239 @@
1
+ /**
2
+ * SubagentFrameBuffer — D1 buffer-and-flush + D2 resync unit coverage.
3
+ * See change: fix-subagent-live-detail-reliability (tasks 4.3, 5.4).
4
+ */
5
+ import { describe, expect, it } from "vitest";
6
+ import { SubagentFrameBuffer } from "../subagent-frame-buffer.js";
7
+
8
+ function frame(id: string, entries: unknown[] = []) {
9
+ return { id, type: "Explore", description: "", details: { entries } };
10
+ }
11
+
12
+ /** A frame whose details carry the producer's v7 runner `agentSessionId`. */
13
+ function frameWithSession(id: string, agentSessionId: string, entries: unknown[] = []) {
14
+ return { id, type: "Explore", description: "", details: { entries, agentSessionId } };
15
+ }
16
+
17
+ describe("SubagentFrameBuffer — channel classification", () => {
18
+ it("recognizes subagent channels only", () => {
19
+ expect(SubagentFrameBuffer.isSubagentChannel("subagents:started")).toBe(true);
20
+ expect(SubagentFrameBuffer.isSubagentChannel("subagents:completed")).toBe(true);
21
+ expect(SubagentFrameBuffer.isSubagentChannel("flow:agent-started")).toBe(false);
22
+ expect(SubagentFrameBuffer.isSubagentChannel("some:custom")).toBe(false);
23
+ });
24
+
25
+ it("extracts agentId from data.id", () => {
26
+ expect(SubagentFrameBuffer.agentIdOf({ id: "abc" })).toBe("abc");
27
+ expect(SubagentFrameBuffer.agentIdOf({})).toBeUndefined();
28
+ expect(SubagentFrameBuffer.agentIdOf(undefined)).toBeUndefined();
29
+ });
30
+ });
31
+
32
+ describe("D1 — buffer while not ready, flush on re-register", () => {
33
+ it("retains a not-ready frame and returns it on drain (emission order)", () => {
34
+ const buf = new SubagentFrameBuffer();
35
+ buf.buffer("subagents:started", frame("a"));
36
+ buf.buffer("subagents:started", frame("b"));
37
+ const drained = buf.drain();
38
+ expect(drained.map((f) => f.data.id)).toEqual(["a", "b"]);
39
+ expect(buf.stats.buffered).toBe(2);
40
+ expect(buf.stats.flushed).toBe(2);
41
+ // Buffer is empty after drain.
42
+ expect(buf.pendingSize).toBe(0);
43
+ expect(buf.drain()).toEqual([]);
44
+ });
45
+
46
+ it("keeps the latest snapshot per agentId (same agent, buffer pressure)", () => {
47
+ const buf = new SubagentFrameBuffer();
48
+ buf.buffer("subagents:started", frame("a", [1]));
49
+ buf.buffer("subagents:started", frame("a", [1, 2]));
50
+ buf.buffer("subagents:started", frame("a", [1, 2, 3]));
51
+ const drained = buf.drain();
52
+ expect(drained).toHaveLength(1);
53
+ expect((drained[0]!.data.details as { entries: unknown[] }).entries).toEqual([1, 2, 3]);
54
+ });
55
+
56
+ it("bounds the buffer to maxAgents, dropping the oldest agent", () => {
57
+ const buf = new SubagentFrameBuffer(2);
58
+ buf.buffer("subagents:started", frame("a"));
59
+ buf.buffer("subagents:started", frame("b"));
60
+ buf.buffer("subagents:started", frame("c")); // evicts "a"
61
+ const ids = buf.drain().map((f) => f.data.id);
62
+ expect(ids).toEqual(["b", "c"]);
63
+ });
64
+
65
+ it("re-inserting an agent moves it to the most-recent position", () => {
66
+ const buf = new SubagentFrameBuffer(2);
67
+ buf.buffer("subagents:started", frame("a"));
68
+ buf.buffer("subagents:started", frame("b"));
69
+ buf.buffer("subagents:started", frame("a", [1])); // a → most recent
70
+ buf.buffer("subagents:started", frame("c")); // evicts "b" (now oldest)
71
+ const ids = buf.drain().map((f) => f.data.id);
72
+ expect(ids).toEqual(["a", "c"]);
73
+ });
74
+
75
+ it("counts capacity evictions in stats.overflowEvicted", () => {
76
+ const buf = new SubagentFrameBuffer(2);
77
+ buf.buffer("subagents:started", frame("a"));
78
+ buf.buffer("subagents:started", frame("b"));
79
+ buf.buffer("subagents:started", frame("c")); // evicts "a" from pending + snapshots
80
+ // One eviction from pending + one from snapshots for the same overflow.
81
+ expect(buf.stats.overflowEvicted).toBe(2);
82
+ });
83
+
84
+ it("cannot buffer a frame without an agentId (counts as dropped)", () => {
85
+ const buf = new SubagentFrameBuffer();
86
+ expect(buf.buffer("subagents:started", { details: {} })).toBe(false);
87
+ expect(buf.stats.droppedNoAgentId).toBe(1);
88
+ expect(buf.pendingSize).toBe(0);
89
+ });
90
+
91
+ it("markForwarded counts the ready path without buffering", () => {
92
+ const buf = new SubagentFrameBuffer();
93
+ buf.markForwarded("subagents:started", frame("a", [1]));
94
+ expect(buf.stats.forwarded).toBe(1);
95
+ expect(buf.pendingSize).toBe(0);
96
+ });
97
+ });
98
+
99
+ describe("D2 — resync responder", () => {
100
+ it("returns the latest snapshot for a running subagent", () => {
101
+ const buf = new SubagentFrameBuffer();
102
+ buf.markForwarded("subagents:started", frame("a", [1]));
103
+ buf.markForwarded("subagents:started", frame("a", [1, 2]));
104
+ const snap = buf.resync("a");
105
+ expect(snap).toBeDefined();
106
+ expect((snap!.data.details as { entries: unknown[] }).entries).toEqual([1, 2]);
107
+ expect(buf.stats.resyncServed).toBe(1);
108
+ });
109
+
110
+ it("resync tracks buffered (not-ready) frames too", () => {
111
+ const buf = new SubagentFrameBuffer();
112
+ buf.buffer("subagents:started", frame("a", [1, 2, 3]));
113
+ const snap = buf.resync("a");
114
+ expect((snap!.data.details as { entries: unknown[] }).entries).toEqual([1, 2, 3]);
115
+ });
116
+
117
+ it("no-op for an unknown agent", () => {
118
+ const buf = new SubagentFrameBuffer();
119
+ expect(buf.resync("nope")).toBeUndefined();
120
+ expect(buf.stats.resyncNoop).toBe(1);
121
+ expect(buf.stats.resyncServed).toBe(0);
122
+ });
123
+
124
+ it("no-op for a finished (completed/failed) agent", () => {
125
+ const buf = new SubagentFrameBuffer();
126
+ buf.markForwarded("subagents:started", frame("a", [1]));
127
+ buf.markForwarded("subagents:completed", frame("a", [1, 2]));
128
+ expect(buf.resync("a")).toBeUndefined();
129
+ expect(buf.stats.resyncNoop).toBe(1);
130
+ });
131
+
132
+ it("bounds retained snapshots to maxAgents (drop-oldest), keeping newest", () => {
133
+ const buf = new SubagentFrameBuffer(2);
134
+ // Three running agents forwarded live → snapshots must not exceed 2.
135
+ buf.markForwarded("subagents:started", frame("a", [1]));
136
+ buf.markForwarded("subagents:started", frame("b", [1]));
137
+ buf.markForwarded("subagents:started", frame("c", [1])); // evicts oldest "a"
138
+ expect(buf.resync("a")).toBeUndefined(); // oldest evicted
139
+ expect(buf.resync("b")).toBeDefined();
140
+ expect(buf.resync("c")).toBeDefined();
141
+ });
142
+ });
143
+
144
+ describe("D3 — resync resolves by either id (agentId or derived agentSessionId)", () => {
145
+ it("agentSessionIdOf reads details.agentSessionId (undefined when absent)", () => {
146
+ expect(SubagentFrameBuffer.agentSessionIdOf({ details: { agentSessionId: "S" } })).toBe("S");
147
+ expect(SubagentFrameBuffer.agentSessionIdOf({ details: {} })).toBeUndefined();
148
+ expect(SubagentFrameBuffer.agentSessionIdOf({})).toBeUndefined();
149
+ expect(SubagentFrameBuffer.agentSessionIdOf(undefined)).toBeUndefined();
150
+ });
151
+
152
+ // X2: retained running snapshot for A with details.agentSessionId=S →
153
+ // resync(S) returns it via the values-scan; resync(A) via the fast path.
154
+ it("resolves a retained running snapshot by its derived agentSessionId (values-scan)", () => {
155
+ const buf = new SubagentFrameBuffer();
156
+ buf.markForwarded("subagents:started", frameWithSession("A", "S", [1, 2]));
157
+ const byId = buf.resync("A");
158
+ expect(byId).toBeDefined();
159
+ expect(buf.stats.resyncByAgentId).toBe(1);
160
+ const bySession = buf.resync("S");
161
+ expect(bySession).toBeDefined();
162
+ // state.id stays canonical (the agentId), even when resolved via the v7 id.
163
+ expect(bySession!.data.id).toBe("A");
164
+ expect(buf.stats.resyncByAgentSessionId).toBe(1);
165
+ expect(buf.stats.resyncServed).toBe(2);
166
+ });
167
+
168
+ // X3: terminal frame removes A's snapshot → resync(A) and resync(S) both no-op.
169
+ it("terminated run resolves to nothing by either id", () => {
170
+ const buf = new SubagentFrameBuffer();
171
+ buf.markForwarded("subagents:started", frameWithSession("A", "S", [1]));
172
+ buf.markForwarded("subagents:completed", frameWithSession("A", "S", [1, 2]));
173
+ expect(buf.resync("A")).toBeUndefined();
174
+ expect(buf.resync("S")).toBeUndefined();
175
+ expect(buf.stats.resyncNoop).toBe(2);
176
+ });
177
+
178
+ // X1 (bridge/frame-buffer half of graceful degrade): frames never carry
179
+ // agentSessionId → no session-id resolution, no throw, agentId path intact.
180
+ it("graceful degrade: frames without agentSessionId → resync(sessionId) no-ops, no throw", () => {
181
+ const buf = new SubagentFrameBuffer();
182
+ buf.markForwarded("subagents:started", frame("A", [1]));
183
+ expect(buf.resync("A")).toBeDefined();
184
+ expect(buf.resync("S")).toBeUndefined();
185
+ expect(buf.stats.resyncByAgentSessionId).toBe(0);
186
+ });
187
+
188
+ // E5: 65 distinct running subagents (each with its own S), then complete →
189
+ // the evicted first agentId and its S both no-op; retained ≤ 64; no separate
190
+ // index retains completed runs by session id.
191
+ it("adds no independent bound (BVA on 64) — session-id resolution inherits the snapshot bound", () => {
192
+ const buf = new SubagentFrameBuffer(64);
193
+ for (let i = 0; i < 65; i++) {
194
+ buf.markForwarded("subagents:started", frameWithSession(`a${i}`, `s${i}`, [1]));
195
+ }
196
+ // The 65th insert evicted the oldest (a0/s0): retained running snapshots ≤ 64.
197
+ expect(buf.resync("a0")).toBeUndefined();
198
+ expect(buf.resync("s0")).toBeUndefined();
199
+ expect(buf.resync("a64")).toBeDefined();
200
+ expect(buf.resync("s64")).toBeDefined();
201
+ expect(buf.stats.overflowEvicted).toBeGreaterThanOrEqual(1);
202
+ // Complete every still-tracked run → no snapshot retained by either id.
203
+ for (let i = 1; i < 65; i++) {
204
+ buf.markForwarded("subagents:completed", frameWithSession(`a${i}`, `s${i}`, [1]));
205
+ }
206
+ expect(buf.resync("a64")).toBeUndefined();
207
+ expect(buf.resync("s64")).toBeUndefined();
208
+ });
209
+
210
+ // P1: snapshots at the 64 cap, resync by a non-matching agentSessionId
211
+ // (worst case: full scan, miss) → single call is cheap. The design intent is
212
+ // sub-millisecond; the assertion uses a generous upper bound (25 ms) so a
213
+ // 64-element scan (microseconds in practice) never false-fails under CI CPU
214
+ // jitter / tick resolution, while still catching a pathological regression
215
+ // (e.g. an accidental O(n²) or per-call allocation blow-up). CodeRabbit nit.
216
+ it("derived scan is cheap on a full buffer (worst-case full-scan miss)", () => {
217
+ const buf = new SubagentFrameBuffer(64);
218
+ for (let i = 0; i < 64; i++) {
219
+ buf.markForwarded("subagents:started", frameWithSession(`a${i}`, `s${i}`, [1]));
220
+ }
221
+ const t0 = performance.now();
222
+ const snap = buf.resync("non-matching-session-id");
223
+ const dt = performance.now() - t0;
224
+ expect(snap).toBeUndefined();
225
+ expect(dt).toBeLessThan(25);
226
+ });
227
+ });
228
+
229
+ describe("reset drops all retained state", () => {
230
+ it("clears pending and snapshots", () => {
231
+ const buf = new SubagentFrameBuffer();
232
+ buf.buffer("subagents:started", frame("a", [1]));
233
+ buf.markForwarded("subagents:started", frame("b", [1]));
234
+ buf.reset();
235
+ expect(buf.pendingSize).toBe(0);
236
+ expect(buf.resync("a")).toBeUndefined();
237
+ expect(buf.resync("b")).toBeUndefined();
238
+ });
239
+ });
@@ -0,0 +1,206 @@
1
+ /**
2
+ * Tests for the bridge's tool-result image inliner (Fix B).
3
+ * See change: inline-agent-screenshot-artifacts.
4
+ */
5
+ import { describe, it, expect } from "vitest";
6
+ import { inlineToolResultImages, type ImageContentBlock } from "../tool-result-image-inliner.js";
7
+ import { hashBytes, type ReadFileOutcome } from "../markdown-image-inliner.js";
8
+
9
+ const PNG = Buffer.from("png-bytes");
10
+ const PNG_2 = Buffer.from("other-png-bytes");
11
+
12
+ /** In-memory readFile: missing keys → ENOENT. */
13
+ function fakeReader(files: Record<string, Buffer>) {
14
+ return (absolutePath: string): ReadFileOutcome => {
15
+ const v = files[absolutePath];
16
+ if (v === undefined) return { ok: false, kind: "ENOENT" };
17
+ return { ok: true, bytes: v };
18
+ };
19
+ }
20
+
21
+ function imageBlocks(result: unknown): ImageContentBlock[] {
22
+ const content = (result as any)?.content;
23
+ if (!Array.isArray(content)) return [];
24
+ return content.filter((c: any) => c?.type === "image");
25
+ }
26
+
27
+ function textOf(result: unknown): string {
28
+ const content = (result as any)?.content;
29
+ if (!Array.isArray(content)) return "";
30
+ return content
31
+ .filter((c: any) => c?.type === "text")
32
+ .map((c: any) => c.text)
33
+ .join("\n");
34
+ }
35
+
36
+ describe("inlineToolResultImages", () => {
37
+ it("single screenshot path → one image block, path stripped (no link)", () => {
38
+ const out = inlineToolResultImages("Screenshot saved: /tmp/shots/a.png", {
39
+ readFile: fakeReader({ "/tmp/shots/a.png": PNG }),
40
+ });
41
+ expect(out.inlinedCount).toBe(1);
42
+ const blocks = imageBlocks(out.result);
43
+ expect(blocks).toHaveLength(1);
44
+ expect(blocks[0]).toEqual({
45
+ type: "image",
46
+ data: PNG.toString("base64"),
47
+ mimeType: "image/png",
48
+ });
49
+ // The consumed path no longer appears in the text → not linkified.
50
+ expect(textOf(out.result)).not.toContain("/tmp/shots/a.png");
51
+ expect(textOf(out.result)).toContain("Screenshot saved:");
52
+ });
53
+
54
+ it("two paths, one over per-image cap → one inlined, one left as link", () => {
55
+ const big = Buffer.alloc(100);
56
+ const out = inlineToolResultImages(
57
+ "small /tmp/small.png and big /tmp/big.png",
58
+ {
59
+ readFile: fakeReader({ "/tmp/small.png": PNG, "/tmp/big.png": big }),
60
+ maxPerImageBytes: 50,
61
+ },
62
+ );
63
+ expect(out.inlinedCount).toBe(1);
64
+ expect(imageBlocks(out.result)).toHaveLength(1);
65
+ // small path stripped, big path remains as text → falls back to Fix A.
66
+ const text = textOf(out.result);
67
+ expect(text).not.toContain("/tmp/small.png");
68
+ expect(text).toContain("/tmp/big.png");
69
+ });
70
+
71
+ it("isAllowedPath gate: out-of-root path left as text, in-root path inlined", () => {
72
+ const reader = fakeReader({ "/allowed/shot.png": PNG, "/etc/secret.png": PNG_2 });
73
+ // Out-of-root → not inlined, result unchanged.
74
+ const blocked = inlineToolResultImages("leaked /etc/secret.png", {
75
+ readFile: reader,
76
+ isAllowedPath: (p) => p.startsWith("/allowed/"),
77
+ });
78
+ expect(blocked.inlinedCount).toBe(0);
79
+ expect(blocked.result).toBe("leaked /etc/secret.png");
80
+
81
+ // In-root → inlined.
82
+ const allowed = inlineToolResultImages("saved /allowed/shot.png", {
83
+ readFile: reader,
84
+ isAllowedPath: (p) => p.startsWith("/allowed/"),
85
+ });
86
+ expect(allowed.inlinedCount).toBe(1);
87
+ expect(imageBlocks(allowed.result)).toHaveLength(1);
88
+ });
89
+
90
+ it("non-image path → untouched (result unchanged, no image block)", () => {
91
+ const original = "Wrote /tmp/notes.txt";
92
+ const out = inlineToolResultImages(original, {
93
+ readFile: fakeReader({ "/tmp/notes.txt": Buffer.from("hi") }),
94
+ });
95
+ expect(out.inlinedCount).toBe(0);
96
+ expect(out.result).toBe(original);
97
+ });
98
+
99
+ it("non-existent image path → untouched", () => {
100
+ const original = "Screenshot saved: /tmp/missing.png";
101
+ const out = inlineToolResultImages(original, { readFile: fakeReader({}) });
102
+ expect(out.inlinedCount).toBe(0);
103
+ expect(out.result).toBe(original);
104
+ });
105
+
106
+ it("caps the number of images inlined per result", () => {
107
+ const files: Record<string, Buffer> = {};
108
+ let text = "";
109
+ for (let i = 0; i < 6; i++) {
110
+ const p = `/tmp/s${i}.png`;
111
+ files[p] = Buffer.from(`png-${i}`);
112
+ text += ` ${p}`;
113
+ }
114
+ const out = inlineToolResultImages(text, {
115
+ readFile: fakeReader(files),
116
+ maxImagesPerResult: 4,
117
+ });
118
+ expect(out.inlinedCount).toBe(4);
119
+ expect(imageBlocks(out.result)).toHaveLength(4);
120
+ });
121
+
122
+ it("respects the per-result cumulative byte budget", () => {
123
+ const out = inlineToolResultImages("/tmp/a.png /tmp/b.png", {
124
+ readFile: fakeReader({ "/tmp/a.png": PNG, "/tmp/b.png": PNG_2 }),
125
+ // Budget room for one image only.
126
+ maxPerMessageBytes: PNG.length + 1,
127
+ });
128
+ expect(out.inlinedCount).toBe(1);
129
+ });
130
+
131
+ it("extracts image paths from a content-array result", () => {
132
+ const out = inlineToolResultImages(
133
+ { content: [{ type: "text", text: "saved /tmp/c.png ok" }] },
134
+ { readFile: fakeReader({ "/tmp/c.png": PNG }) },
135
+ );
136
+ expect(out.inlinedCount).toBe(1);
137
+ expect(imageBlocks(out.result)[0].mimeType).toBe("image/png");
138
+ });
139
+
140
+ it("preserves pre-existing non-text content blocks when inlining", () => {
141
+ const existingImg = { type: "image", data: "cHJlZXhpc3Rpbmc=", mimeType: "image/jpeg" };
142
+ const out = inlineToolResultImages(
143
+ {
144
+ content: [
145
+ { type: "text", text: "saw /tmp/e.png here" },
146
+ existingImg,
147
+ { type: "resource", uri: "file:///x" },
148
+ ],
149
+ },
150
+ { readFile: fakeReader({ "/tmp/e.png": PNG }) },
151
+ );
152
+ expect(out.inlinedCount).toBe(1);
153
+ const content = (out.result as any).content;
154
+ // Pre-existing image + resource blocks survive; new image appended.
155
+ expect(content).toContainEqual(existingImg);
156
+ expect(content).toContainEqual({ type: "resource", uri: "file:///x" });
157
+ expect(content.filter((c: any) => c.type === "image")).toHaveLength(2);
158
+ // Consumed path stripped from the original text block.
159
+ const textBlock = content.find((c: any) => c.type === "text");
160
+ expect(textBlock.text).not.toContain("/tmp/e.png");
161
+ });
162
+
163
+ it("MCP `browser` shape: native image + path to that SAME file → no duplicate", () => {
164
+ // The MCP `browser` screenshot tool returns a text block referencing the
165
+ // saved path AND a native image block for that same screenshot. The inliner
166
+ // must not append a byte-identical second copy (rendered side-by-side).
167
+ const nativeImg = { type: "image", data: PNG.toString("base64"), mimeType: "image/png" };
168
+ const out = inlineToolResultImages(
169
+ {
170
+ content: [
171
+ { type: "text", text: "Screenshot saved: /tmp/shots/a.png" },
172
+ nativeImg,
173
+ ],
174
+ },
175
+ { readFile: fakeReader({ "/tmp/shots/a.png": PNG }) },
176
+ );
177
+ // No NEW image inlined …
178
+ expect(out.inlinedCount).toBe(0);
179
+ // … exactly one image block remains (the native one) — no side-by-side dup.
180
+ expect(imageBlocks(out.result)).toHaveLength(1);
181
+ expect(imageBlocks(out.result)[0]).toEqual(nativeImg);
182
+ // … and the redundant path is stripped so it isn't also linkified.
183
+ expect(textOf(out.result)).not.toContain("/tmp/shots/a.png");
184
+ expect(textOf(out.result)).toContain("Screenshot saved:");
185
+ });
186
+
187
+ it("native image + path to a DIFFERENT file → both images kept", () => {
188
+ // Distinct bytes must NOT be de-duped: the mixed case still yields 2 images.
189
+ const nativeImg = { type: "image", data: PNG_2.toString("base64"), mimeType: "image/jpeg" };
190
+ const out = inlineToolResultImages(
191
+ { content: [{ type: "text", text: "also /tmp/other.png" }, nativeImg] },
192
+ { readFile: fakeReader({ "/tmp/other.png": PNG }) },
193
+ );
194
+ expect(out.inlinedCount).toBe(1);
195
+ expect(imageBlocks(out.result)).toHaveLength(2);
196
+ });
197
+
198
+ it("dedups a repeated path into a single image block", () => {
199
+ const out = inlineToolResultImages("/tmp/d.png twice /tmp/d.png", {
200
+ readFile: fakeReader({ "/tmp/d.png": PNG }),
201
+ });
202
+ expect(out.inlinedCount).toBe(1);
203
+ expect(imageBlocks(out.result)).toHaveLength(1);
204
+ expect(hashBytes(PNG)).toBeTruthy(); // sanity: helper imported
205
+ });
206
+ });