@circuitwall/jarela 2.5.0 → 2.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/.next/standalone/.next/BUILD_ID +1 -1
  2. package/.next/standalone/.next/app-path-routes-manifest.json +5 -5
  3. package/.next/standalone/.next/build-manifest.json +2 -2
  4. package/.next/standalone/.next/prerender-manifest.json +3 -3
  5. package/.next/standalone/.next/react-loadable-manifest.json +2 -2
  6. package/.next/standalone/.next/server/app/_global-error/page_client-reference-manifest.js +1 -1
  7. package/.next/standalone/.next/server/app/_global-error.html +1 -1
  8. package/.next/standalone/.next/server/app/_global-error.rsc +1 -1
  9. package/.next/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
  10. package/.next/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
  11. package/.next/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
  12. package/.next/standalone/.next/server/app/_not-found/page_client-reference-manifest.js +1 -1
  13. package/.next/standalone/.next/server/app/_not-found.html +2 -2
  14. package/.next/standalone/.next/server/app/_not-found.rsc +2 -2
  15. package/.next/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +2 -2
  16. package/.next/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +2 -2
  17. package/.next/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
  18. package/.next/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +2 -2
  19. package/.next/standalone/.next/server/app/api/v1/bridges/[id]/chats/route.js +8 -1
  20. package/.next/standalone/.next/server/app/api/v1/bridges/[id]/chats/route.js.nft.json +1 -1
  21. package/.next/standalone/.next/server/app/api/v1/bridges/[id]/lookup/route.js +8 -1
  22. package/.next/standalone/.next/server/app/api/v1/bridges/[id]/lookup/route.js.nft.json +1 -1
  23. package/.next/standalone/.next/server/app/api/v1/bridges/[id]/pair/route.js +8 -1
  24. package/.next/standalone/.next/server/app/api/v1/bridges/[id]/pair/route.js.nft.json +1 -1
  25. package/.next/standalone/.next/server/app/api/v1/bridges/[id]/route.js +8 -1
  26. package/.next/standalone/.next/server/app/api/v1/bridges/[id]/route.js.nft.json +1 -1
  27. package/.next/standalone/.next/server/app/api/v1/bridges/[id]/status/route.js +8 -1
  28. package/.next/standalone/.next/server/app/api/v1/bridges/[id]/status/route.js.nft.json +1 -1
  29. package/.next/standalone/.next/server/app/api/v1/builtin-tools/route.js +7 -0
  30. package/.next/standalone/.next/server/app/api/v1/builtin-tools/route.js.nft.json +1 -1
  31. package/.next/standalone/.next/server/app/api/v1/credentials/[id]/route.js +7 -0
  32. package/.next/standalone/.next/server/app/api/v1/credentials/[id]/route.js.nft.json +1 -1
  33. package/.next/standalone/.next/server/app/api/v1/credentials/route.js +7 -0
  34. package/.next/standalone/.next/server/app/api/v1/credentials/route.js.nft.json +1 -1
  35. package/.next/standalone/.next/server/app/api/v1/dashboard/pricing-task/route.js +8 -1
  36. package/.next/standalone/.next/server/app/api/v1/dashboard/pricing-task/route.js.nft.json +1 -1
  37. package/.next/standalone/.next/server/app/api/v1/delegations/codex/[job_id]/route.js +7 -0
  38. package/.next/standalone/.next/server/app/api/v1/delegations/codex/[job_id]/route.js.nft.json +1 -1
  39. package/.next/standalone/.next/server/app/api/v1/documents/sources/[id]/reindex/route.js +7 -0
  40. package/.next/standalone/.next/server/app/api/v1/documents/sources/[id]/reindex/route.js.nft.json +1 -1
  41. package/.next/standalone/.next/server/app/api/v1/documents/sources/[id]/route.js +8 -1
  42. package/.next/standalone/.next/server/app/api/v1/documents/sources/[id]/route.js.nft.json +1 -1
  43. package/.next/standalone/.next/server/app/api/v1/documents/sources/route.js +8 -1
  44. package/.next/standalone/.next/server/app/api/v1/documents/sources/route.js.nft.json +1 -1
  45. package/.next/standalone/.next/server/app/api/v1/events/route.js +8 -1
  46. package/.next/standalone/.next/server/app/api/v1/events/route.js.nft.json +1 -1
  47. package/.next/standalone/.next/server/app/api/v1/extension/agents/route.js +8 -1
  48. package/.next/standalone/.next/server/app/api/v1/extension/agents/route.js.nft.json +1 -1
  49. package/.next/standalone/.next/server/app/api/v1/extension/fill/route.js +8 -1
  50. package/.next/standalone/.next/server/app/api/v1/extension/fill/route.js.nft.json +1 -1
  51. package/.next/standalone/.next/server/app/api/v1/extension/refine/route.js +8 -1
  52. package/.next/standalone/.next/server/app/api/v1/extension/refine/route.js.nft.json +1 -1
  53. package/.next/standalone/.next/server/app/api/v1/extension/turn/route.js +8 -1
  54. package/.next/standalone/.next/server/app/api/v1/extension/turn/route.js.nft.json +1 -1
  55. package/.next/standalone/.next/server/app/api/v1/extensions/route.js +8 -1
  56. package/.next/standalone/.next/server/app/api/v1/extensions/route.js.nft.json +1 -1
  57. package/.next/standalone/.next/server/app/api/v1/extensions/tools/[name]/config/route.js +8 -1
  58. package/.next/standalone/.next/server/app/api/v1/extensions/tools/[name]/config/route.js.nft.json +1 -1
  59. package/.next/standalone/.next/server/app/api/v1/extensions/tools/[name]/route.js +8 -1
  60. package/.next/standalone/.next/server/app/api/v1/extensions/tools/[name]/route.js.nft.json +1 -1
  61. package/.next/standalone/.next/server/app/api/v1/extensions/tools/[name]/secrets/route.js +8 -1
  62. package/.next/standalone/.next/server/app/api/v1/extensions/tools/[name]/secrets/route.js.nft.json +1 -1
  63. package/.next/standalone/.next/server/app/api/v1/health/probes/route.js +7 -0
  64. package/.next/standalone/.next/server/app/api/v1/health/probes/route.js.nft.json +1 -1
  65. package/.next/standalone/.next/server/app/api/v1/integrations/[name]/test/route.js +7 -0
  66. package/.next/standalone/.next/server/app/api/v1/integrations/[name]/test/route.js.nft.json +1 -1
  67. package/.next/standalone/.next/server/app/api/v1/integrations/gmail/oauth/callback/route.js +22 -1
  68. package/.next/standalone/.next/server/app/api/v1/integrations/gmail/oauth/start/route.js +22 -1
  69. package/.next/standalone/.next/server/app/api/v1/integrations/gmail/oauth/status/route.js +22 -1
  70. package/.next/standalone/.next/server/app/api/v1/integrations/outlook/oauth/callback/route.js +19 -2
  71. package/.next/standalone/.next/server/app/api/v1/integrations/outlook/oauth/start/route.js +19 -2
  72. package/.next/standalone/.next/server/app/api/v1/integrations/outlook/oauth/status/route.js +19 -2
  73. package/.next/standalone/.next/server/app/api/v1/packages/defaults/[id]/route.js +12 -2
  74. package/.next/standalone/.next/server/app/api/v1/packages/defaults/[id]/route.js.nft.json +1 -1
  75. package/.next/standalone/.next/server/app/api/v1/packages/install/[id]/route.js +7 -0
  76. package/.next/standalone/.next/server/app/api/v1/packages/install/[id]/route.js.nft.json +1 -1
  77. package/.next/standalone/.next/server/app/api/v1/packages/install/route.js +7 -0
  78. package/.next/standalone/.next/server/app/api/v1/packages/install/route.js.nft.json +1 -1
  79. package/.next/standalone/.next/server/app/api/v1/packages/manifests/[name]/enabled/route.js +7 -0
  80. package/.next/standalone/.next/server/app/api/v1/packages/manifests/[name]/enabled/route.js.nft.json +1 -1
  81. package/.next/standalone/.next/server/app/api/v1/packages/manifests/[name]/route.js +7 -0
  82. package/.next/standalone/.next/server/app/api/v1/packages/manifests/[name]/route.js.nft.json +1 -1
  83. package/.next/standalone/.next/server/app/api/v1/packages/manifests/route.js +7 -0
  84. package/.next/standalone/.next/server/app/api/v1/packages/manifests/route.js.nft.json +1 -1
  85. package/.next/standalone/.next/server/app/api/v1/packages/reload/route.js +12 -2
  86. package/.next/standalone/.next/server/app/api/v1/packages/reload/route.js.nft.json +1 -1
  87. package/.next/standalone/.next/server/app/api/v1/packages/route.js +12 -2
  88. package/.next/standalone/.next/server/app/api/v1/packages/route.js.nft.json +1 -1
  89. package/.next/standalone/.next/server/app/api/v1/page-capture/route.js +8 -1
  90. package/.next/standalone/.next/server/app/api/v1/page-capture/route.js.nft.json +1 -1
  91. package/.next/standalone/.next/server/app/api/v1/pending-actions/[id]/approve/route.js +7 -0
  92. package/.next/standalone/.next/server/app/api/v1/pending-actions/[id]/approve/route.js.nft.json +1 -1
  93. package/.next/standalone/.next/server/app/api/v1/pending-actions/[id]/deny/route.js +7 -0
  94. package/.next/standalone/.next/server/app/api/v1/pending-actions/[id]/deny/route.js.nft.json +1 -1
  95. package/.next/standalone/.next/server/app/api/v1/pending-actions/route.js +7 -0
  96. package/.next/standalone/.next/server/app/api/v1/pending-actions/route.js.nft.json +1 -1
  97. package/.next/standalone/.next/server/app/api/v1/scheduled-tasks/[id]/run/route.js +8 -1
  98. package/.next/standalone/.next/server/app/api/v1/scheduled-tasks/[id]/run/route.js.nft.json +1 -1
  99. package/.next/standalone/.next/server/app/api/v1/system/restart/route.js +7 -0
  100. package/.next/standalone/.next/server/app/api/v1/system/restart/route.js.nft.json +1 -1
  101. package/.next/standalone/.next/server/app/api/v1/threads/[thread_id]/run/route.js +52 -45
  102. package/.next/standalone/.next/server/app/api/v1/threads/[thread_id]/run/route.js.nft.json +1 -1
  103. package/.next/standalone/.next/server/app/api/v1/tools/route.js +8 -1
  104. package/.next/standalone/.next/server/app/api/v1/tools/route.js.nft.json +1 -1
  105. package/.next/standalone/.next/server/app/api/v1/update/apply/route.js.nft.json +1 -1
  106. package/.next/standalone/.next/server/app/api/v1/watchers/[id]/run/route.js +8 -1
  107. package/.next/standalone/.next/server/app/api/v1/watchers/[id]/run/route.js.nft.json +1 -1
  108. package/.next/standalone/.next/server/app/api/v1/watchers/route.js +8 -1
  109. package/.next/standalone/.next/server/app/api/v1/watchers/route.js.nft.json +1 -1
  110. package/.next/standalone/.next/server/app/page.js +73385 -73375
  111. package/.next/standalone/.next/server/app/page_client-reference-manifest.js +1 -1
  112. package/.next/standalone/.next/server/app/setup/page_client-reference-manifest.js +1 -1
  113. package/.next/standalone/.next/server/app-paths-manifest.json +5 -5
  114. package/.next/standalone/.next/server/chunks/1433.js +1 -1
  115. package/.next/standalone/.next/server/chunks/210.js +6 -6
  116. package/.next/standalone/.next/server/chunks/3524.js +31 -15
  117. package/.next/standalone/.next/server/chunks/3566.js +1 -1
  118. package/.next/standalone/.next/server/chunks/4189.js +1 -1
  119. package/.next/standalone/.next/server/chunks/{9590.js → 423.js} +10 -3
  120. package/.next/standalone/.next/server/chunks/4596.js +5 -2
  121. package/.next/standalone/.next/server/chunks/46.js +7 -0
  122. package/.next/standalone/.next/server/chunks/540.js +29 -5
  123. package/.next/standalone/.next/server/chunks/5585.js +41 -3
  124. package/.next/standalone/.next/server/chunks/5908.js +10 -10
  125. package/.next/standalone/.next/server/chunks/{3399.js → 6058.js} +485 -3
  126. package/.next/standalone/.next/server/chunks/{9164.js → 6190.js} +13828 -13581
  127. package/.next/standalone/.next/server/chunks/{1388.js → 8842.js} +5961 -5793
  128. package/.next/standalone/.next/server/chunks/9032.js +5 -5
  129. package/.next/standalone/.next/server/chunks/{7547.js → 9217.js} +485 -3
  130. package/.next/standalone/.next/server/instrumentation.js +7 -0
  131. package/.next/standalone/.next/server/middleware-build-manifest.js +2 -2
  132. package/.next/standalone/.next/server/middleware-react-loadable-manifest.js +1 -1
  133. package/.next/standalone/.next/server/middleware.js +7 -0
  134. package/.next/standalone/.next/server/pages/404.html +2 -2
  135. package/.next/standalone/.next/server/pages/500.html +1 -1
  136. package/.next/standalone/.next/server/preview-props.json +3 -3
  137. package/.next/standalone/.next/server/server-reference-manifest.json +1 -1
  138. package/.next/standalone/.next/static/chunks/app/{page-2ef8d75d40eb4311.js → page-4112e14360a9909e.js} +1291 -1281
  139. package/.next/standalone/.next/static/css/{c160ea862133acd2.css → 5b30046ba49144c5.css} +2 -2
  140. package/.next/standalone/docs/adoption/changesets.json +151 -0
  141. package/.next/standalone/package.json +2 -1
  142. package/CHANGELOG.md +46 -0
  143. package/browser-extension/background.js +5 -539
  144. package/browser-extension/lib/form-editing.mjs +541 -0
  145. package/components/chat/MessageBubble.tsx +4 -736
  146. package/components/chat/MessageContent.tsx +749 -0
  147. package/components/dashboard/DashboardPanel.test.tsx +34 -0
  148. package/components/dashboard/DashboardPanel.tsx +1 -1
  149. package/components/layout/MenuPanel.tsx +1 -1
  150. package/components/ui/BootScreen.test.tsx +1 -0
  151. package/components/ui/BootScreen.tsx +1 -0
  152. package/components/ui/ServerStatus.test.tsx +50 -0
  153. package/components/ui/ServerStatus.tsx +10 -8
  154. package/hooks/useSSE.test.ts +46 -0
  155. package/lib/agents/llm.ts +1 -1
  156. package/lib/agents/output-validator/validator.test.ts +4 -0
  157. package/lib/agents/output-validator/validator.ts +1 -1
  158. package/lib/agents/prompt-registry.test.ts +11 -0
  159. package/lib/agents/prompt-registry.ts +13 -0
  160. package/lib/agents/retry-prompts.ts +17 -0
  161. package/lib/agents/run-retry.ts +671 -0
  162. package/lib/agents/run-thread.retry.test.ts +176 -2
  163. package/lib/agents/run-thread.test.ts +6 -6
  164. package/lib/agents/run-thread.ts +9 -717
  165. package/lib/agents/stream-collector.test.ts +28 -4
  166. package/lib/agents/stream-collector.ts +5 -28
  167. package/lib/agents/tool-outcomes.ts +61 -0
  168. package/lib/documents/remote/confluence.test.ts +65 -0
  169. package/lib/documents/remote/confluence.ts +35 -9
  170. package/lib/documents/remote/jira.ts +1 -1
  171. package/lib/health/probes.test.ts +32 -0
  172. package/lib/health/probes.ts +29 -14
  173. package/lib/integrations/atlassian/manifest.ts +5 -3
  174. package/lib/integrations/github/manifest.ts +7 -4
  175. package/lib/integrations/gmail-oauth.ts +16 -1
  176. package/lib/integrations/microsoft-oauth.ts +17 -2
  177. package/lib/integrations/outlook/manifest.ts +10 -8
  178. package/lib/stores/integrations.ts +1 -0
  179. package/lib/tools/communications/calendar-aliases.test.ts +77 -1
  180. package/lib/tools/communications/gmail.test.ts +13 -0
  181. package/lib/tools/communications/ms-graph.test.ts +26 -3
  182. package/lib/tools/communications/ms-graph.ts +9 -11
  183. package/lib/tools/communications/outlook-calendar.ts +94 -17
  184. package/lib/tools/packages/default-packages/atlassian.ts +8 -2
  185. package/package.json +2 -1
  186. package/scripts/standalone-footprint.mjs +1 -1
  187. /package/.next/standalone/.next/static/{544OFJV-q0RC6Gjgje4LT → VZXMorlOmSmi3dUr7K5UX}/_buildManifest.js +0 -0
  188. /package/.next/standalone/.next/static/{544OFJV-q0RC6Gjgje4LT → VZXMorlOmSmi3dUr7K5UX}/_ssgManifest.js +0 -0
@@ -22,8 +22,11 @@ const { collectStream } = await import("./stream-collector");
22
22
  const { deleteModelConfig, upsertModelConfig } = await import("@/lib/stores/model-config");
23
23
  const { upsertAgentConfig } = await import("@/lib/stores/agent-configs");
24
24
  const { resetConfigCache } = await import("@/lib/env/config");
25
- const { createThread, getMessagesPage } = await import("@/lib/stores/threads");
25
+ const { createThread, getMessagesPage, getMessages } = await import("@/lib/stores/threads");
26
+ const { createAssistantTranscriptDraft, persistAssistantDraftChunk } = await import("./transcript-draft");
26
27
  const { spillFileBuffer } = await import("@/lib/attachments/spill");
28
+ const { transientRetryStream } = await import("./run-retry");
29
+ const stallClassifier = await import("./hallucination-classifier");
27
30
 
28
31
  afterAll(() => {
29
32
  try { rmSync(tmpRoot, { recursive: true, force: true }); } catch {}
@@ -38,6 +41,48 @@ function chunks(...items: StreamChunk[]): AsyncIterable<StreamChunk> {
38
41
  }
39
42
 
40
43
  describe("prepareThreadRun transient retry", () => {
44
+ it("cancels backoff without preparing another attempt", async () => {
45
+ vi.useFakeTimers();
46
+ const controller = new AbortController();
47
+ const prepareRetry = vi.fn(async () => ({ stream: chunks({ type: "done", data: {} }) }));
48
+ try {
49
+ const collected = collectStream(transientRetryStream(
50
+ chunks({ type: "error", data: { code: "rate_limited", message: "429", retry_after_ms: 60_000 } }),
51
+ { thread_id: "backoff-test", message: "hello", signal: controller.signal },
52
+ 1,
53
+ prepareRetry,
54
+ ));
55
+ await vi.advanceTimersByTimeAsync(0);
56
+ controller.abort();
57
+ expect(await collected).toMatchObject({ terminal: "error", aborted: true, errorCode: "aborted" });
58
+ expect(prepareRetry).not.toHaveBeenCalled();
59
+ } finally {
60
+ vi.useRealTimers();
61
+ }
62
+ });
63
+
64
+ it("honors a negative model verdict with no explanation instead of falling back to regex", async () => {
65
+ upsertModelConfig("default", "openai", "gpt-4o-mini", { api_key: "sk-test" }, true);
66
+ upsertAgentConfig({
67
+ id: "negative-stall-verdict", name: "Classifier", identity: "helper", instructions: "Be helpful.", tools: [], model_config_name: null,
68
+ anti_hallucination_mode: "model", anti_hallucination_model_config: "default",
69
+ });
70
+ const thread = createThread("negative-stall-verdict");
71
+ const classifier = vi.spyOn(stallClassifier, "classifyStall").mockResolvedValue({ stalled: false, reason: "" });
72
+ try {
73
+ streamWithConfigMock.mockReturnValue(chunks({ type: "text_delta", data: { delta: "Let me check." } }, { type: "done", data: {} }));
74
+ const prepared = await prepareThreadRun({
75
+ thread_id: thread.thread_id, message: "Explain this phrase.",
76
+ context_profile: { include_hot: true, include_warm: false, include_facts: false, include_recall: false },
77
+ });
78
+ const collected = await collectStream(prepared.stream);
79
+ expect(collected.assistantContent).toBe("Let me check.");
80
+ expect(streamWithConfigMock).toHaveBeenCalledTimes(1);
81
+ } finally {
82
+ classifier.mockRestore();
83
+ }
84
+ });
85
+
41
86
  it("blocks state-changing tools on a completion turn even when the old user request asked for a restart", async () => {
42
87
  upsertModelConfig("default", "openai", "gpt-4o-mini", { api_key: "sk-test" }, true);
43
88
  upsertAgentConfig({ id: "completion-read-only", name: "Completion", identity: "helper", instructions: "Be helpful.", tools: ["restart_server", "local_exec"], model_config_name: null });
@@ -650,7 +695,8 @@ describe("prepareThreadRun transient retry", () => {
650
695
  const collected = await collectStream(prepared.stream);
651
696
  expect(collected.terminal).toBe("done");
652
697
  expect(streamWithConfigMock).toHaveBeenCalledTimes(1);
653
- expect(collected.usedTools).toEqual([toolName, toolName, toolName]);
698
+ expect(collected.usedTools).toEqual([toolName, toolName]);
699
+ expect(collected.toolEvents.filter((event) => event.phase === "call")).toHaveLength(3);
654
700
  expect(collected.assistantContent).toContain("Retry guard skipped automatic retry");
655
701
  expect(collected.assistantContent).toContain(toolName);
656
702
  expect(collected.assistantContent).not.toContain("↻ Auto-retry");
@@ -868,6 +914,134 @@ describe("prepareThreadRun transient retry", () => {
868
914
  );
869
915
  });
870
916
 
917
+ it.each([
918
+ ["failed", [
919
+ { type: "text_delta", data: { delta: "partial correction" } },
920
+ { type: "error", data: { code: "stream_error", message: "synthetic failure" } },
921
+ ]],
922
+ ["empty", [
923
+ { type: "text_delta", data: { delta: "" } },
924
+ { type: "done", data: {} },
925
+ ]],
926
+ ["interrupted", [
927
+ { type: "text_delta", data: { delta: "partial correction" } },
928
+ { type: "done", data: { aborted: true } },
929
+ ]],
930
+ ] as const)("keeps the original draft when replacement is %s", async (outcome, replacement) => {
931
+ upsertModelConfig("default", "openai", "gpt-4o-mini", { api_key: "sk-test" }, true);
932
+ const agentId = `replacement-${outcome}`;
933
+ upsertAgentConfig({ id: agentId, name: "Replacement", identity: "helper", instructions: "Be helpful.", tools: [], model_config_name: null });
934
+ const thread = createThread(agentId);
935
+ const draft = "Option A costs 10 units. Option B costs 20 units. I saved the comparison.";
936
+ streamWithConfigMock
937
+ .mockReturnValueOnce(chunks({ type: "text_delta", data: { delta: draft } }, { type: "done", data: {} }))
938
+ .mockReturnValueOnce(chunks(...replacement));
939
+ const prepared = await prepareThreadRun({
940
+ thread_id: thread.thread_id, message: "Compare the options.",
941
+ context_profile: { include_hot: true, include_warm: false, include_facts: false, include_recall: false },
942
+ });
943
+ const events: StreamChunk[] = [];
944
+ const collected = await collectStream(prepared.stream, { onChunk: (chunk) => events.push(chunk) });
945
+ expect(collected.assistantContent).toBe(draft);
946
+ if (outcome === "empty") expect(collected).toMatchObject({ terminal: "error", errorCode: "correction_failed" });
947
+ expect(events.some((event) => event.type === "reset_text")).toBe(false);
948
+ expect(events.filter((event) => event.type === "text_delta")).toHaveLength(1);
949
+ });
950
+
951
+ it("provides the whole draft and commits replacement text only after completion", async () => {
952
+ upsertModelConfig("default", "openai", "gpt-4o-mini", { api_key: "sk-test" }, true);
953
+ upsertAgentConfig({ id: "replacement-complete", name: "Replacement", identity: "helper", instructions: "Be helpful.", tools: [], model_config_name: null });
954
+ const thread = createThread("replacement-complete");
955
+ const draft = `Comparison details: ${"x".repeat(2_100)}\nFinal table row: option B costs 20 units. I saved the comparison.`;
956
+ let completed = false;
957
+ streamWithConfigMock
958
+ .mockReturnValueOnce(chunks({ type: "text_delta", data: { delta: draft } }, { type: "done", data: {} }))
959
+ .mockImplementationOnce(() => ({
960
+ async *[Symbol.asyncIterator]() {
961
+ yield { type: "text_delta", data: { delta: "The complete comparison. " } } as StreamChunk;
962
+ completed = true;
963
+ yield { type: "text_delta", data: { delta: "It was not saved." } } as StreamChunk;
964
+ yield { type: "done", data: {} } as StreamChunk;
965
+ },
966
+ }));
967
+ const prepared = await prepareThreadRun({
968
+ thread_id: thread.thread_id, message: "Compare the options.",
969
+ context_profile: { include_hot: true, include_warm: false, include_facts: false, include_recall: false },
970
+ });
971
+ const events: StreamChunk[] = [];
972
+ const collected = await collectStream(prepared.stream, {
973
+ onChunk: (chunk) => {
974
+ if (chunk.type === "reset_text") expect(completed).toBe(true);
975
+ events.push(chunk);
976
+ },
977
+ });
978
+ expect(JSON.stringify(streamWithConfigMock.mock.calls[1][1])).toContain("Final table row: option B costs 20 units");
979
+ expect(events.slice(-3).map((chunk) => chunk.type)).toEqual(["reset_text", "text_delta", "done"]);
980
+ expect(events.some((chunk) => chunk.type === "status" && chunk.data.label === "Checking answer...")).toBe(true);
981
+ expect(collected.assistantContent).toBe("The complete comparison. It was not saved.");
982
+ });
983
+
984
+ it.each(["success", "failure"])("persists the complete comparison across correction and finalization (%s)", async (outcome) => {
985
+ upsertModelConfig("default", "openai", "gpt-4o-mini", { api_key: "sk-test" }, true);
986
+ const agentId = `persisted-comparison-${outcome}`;
987
+ upsertAgentConfig({ id: agentId, name: "Persistence", identity: "helper", instructions: "Be helpful.", tools: [], model_config_name: null });
988
+ const thread = createThread(agentId);
989
+ const table = "| Option | Cost |\n| --- | --- |\n| A | 10 |\n| B | 20 |\n| C | 30 |\n| D | 40 |\n| E | 50 |";
990
+ const draftText = `${table}\nI saved the comparison.`;
991
+ const repairedText = `${table}\nThe comparison has not been saved.`;
992
+ let release!: () => void;
993
+ let markPending!: () => void;
994
+ const gate = new Promise<void>((resolve) => { release = resolve; });
995
+ const pending = new Promise<void>((resolve) => { markPending = resolve; });
996
+ streamWithConfigMock
997
+ .mockReturnValueOnce(chunks({ type: "text_delta", data: { delta: draftText } }, { type: "done", data: {} }))
998
+ .mockImplementationOnce(() => ({
999
+ async *[Symbol.asyncIterator]() {
1000
+ yield { type: "text_delta", data: { delta: table } } as StreamChunk;
1001
+ markPending();
1002
+ await gate;
1003
+ if (outcome === "failure") {
1004
+ yield { type: "error", data: { code: "stream_error", message: "synthetic correction failure" } } as StreamChunk;
1005
+ return;
1006
+ }
1007
+ yield { type: "text_delta", data: { delta: "\nThe comparison has not been saved." } } as StreamChunk;
1008
+ yield { type: "done", data: {} } as StreamChunk;
1009
+ },
1010
+ }));
1011
+ const prepared = await prepareThreadRun({
1012
+ thread_id: thread.thread_id, message: "Compare five options.",
1013
+ context_profile: { include_hot: true, include_warm: false, include_facts: false, include_recall: false },
1014
+ });
1015
+ const draft = createAssistantTranscriptDraft(thread.thread_id);
1016
+ const collecting = collectStream(prepared.stream, { onChunk: (chunk) => persistAssistantDraftChunk(draft, chunk) });
1017
+ try {
1018
+ await pending;
1019
+ expect(getMessages(thread.thread_id).find((message) => message.msg_id === draft.msg_id))
1020
+ .toMatchObject({ content: draftText, transcript_status: "in_progress" });
1021
+ release();
1022
+ const collected = await collecting;
1023
+ expect(collected.terminal).toBe(outcome === "success" ? "done" : "error");
1024
+ persistAssistantMessage(
1025
+ thread.thread_id, collected.assistantContent, collected.usedTools, collected.toolEvents,
1026
+ null, collected.usage, prepared.context_snapshot, prepared.source_manifest, null, null, undefined,
1027
+ { draftMessageId: draft.msg_id, transcriptStatus: outcome === "success" ? "completed" : "failed" },
1028
+ );
1029
+ const assistants = getMessages(thread.thread_id).filter((message) => message.role === "assistant");
1030
+ expect(assistants).toHaveLength(1);
1031
+ expect(assistants[0].msg_id).toBe(draft.msg_id);
1032
+ expect(assistants[0].content).toContain(table);
1033
+ expect(assistants[0].transcript_status).toBe(outcome === "success" ? "completed" : "failed");
1034
+ if (outcome === "success") expect(assistants[0].content).toBe(repairedText);
1035
+ else {
1036
+ expect(assistants[0].content.startsWith(draftText)).toBe(true);
1037
+ expect(assistants[0].content).toContain("Output validator flagged");
1038
+ }
1039
+ } finally {
1040
+ release();
1041
+ await collecting;
1042
+ }
1043
+ });
1044
+
871
1045
  it("keeps the flagged reply when preparing its replacement fails", async () => {
872
1046
  upsertModelConfig("default", "openai", "gpt-4o-mini", { api_key: "sk-test" }, true);
873
1047
  upsertAgentConfig({
@@ -87,19 +87,19 @@ describe("buildRetryContextSummary", () => {
87
87
  });
88
88
 
89
89
  describe("buildFabricationNudge", () => {
90
- it("tells the model the flagged reply was discarded and not to restate it", () => {
90
+ it("requires a complete corrected answer without losing valid content", () => {
91
91
  const nudge = buildFabricationNudge("You wrote \"(via web_search)\" but did not call \"web_search\" this turn.");
92
- expect(nudge).toContain("discarded");
93
- expect(nudge).toContain("Do NOT restate");
92
+ expect(nudge).toContain("complete corrected answer");
93
+ expect(nudge).toContain("Preserve all valid explanations, calculations, tables, and conclusions");
94
+ expect(nudge).toContain("Do not return only a correction or a tool confirmation");
94
95
  expect(nudge).toContain("(via web_search)");
95
96
  expect(nudge).not.toMatch(/redo this turn/i);
96
97
  });
97
98
 
98
99
  it("omits the trailing reason clause when no reason is given", () => {
99
100
  const nudge = buildFabricationNudge("");
100
- expect(nudge).toBe(
101
- "↻ Auto-retry: output validator flagged your reply. The flagged reply has been discarded — the user will see ONLY what you write next, not the original. Do NOT restate, summarize, or repeat any part of the previous answer. Emit ONLY the fix: call the tool that's actually missing, or state the corrected claim in one or two sentences.",
102
- );
101
+ expect(nudge).toContain("flagged your reply. Return a complete corrected answer");
102
+ expect(nudge).not.toContain("reply —");
103
103
  });
104
104
  });
105
105