@cortexkit/opencode-magic-context 0.32.0 → 0.32.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 (191) hide show
  1. package/README.md +3 -3
  2. package/dist/config/index.d.ts.map +1 -1
  3. package/dist/config/project-security.d.ts +3 -0
  4. package/dist/config/project-security.d.ts.map +1 -1
  5. package/dist/config/schema/magic-context.d.ts +82 -2
  6. package/dist/config/schema/magic-context.d.ts.map +1 -1
  7. package/dist/config/transform-mode.d.ts +15 -0
  8. package/dist/config/transform-mode.d.ts.map +1 -0
  9. package/dist/features/magic-context/compartment-embedding.d.ts.map +1 -1
  10. package/dist/features/magic-context/dreamer/cron.d.ts +2 -2
  11. package/dist/features/magic-context/dreamer/cron.d.ts.map +1 -1
  12. package/dist/features/magic-context/dreamer/evaluate-smart-notes.d.ts.map +1 -1
  13. package/dist/features/magic-context/git-commits/indexer.d.ts.map +1 -1
  14. package/dist/features/magic-context/memory/embedding-identity.d.ts.map +1 -1
  15. package/dist/features/magic-context/memory/embedding-provider.d.ts +6 -0
  16. package/dist/features/magic-context/memory/embedding-provider.d.ts.map +1 -1
  17. package/dist/features/magic-context/memory/embedding-synapse.d.ts +98 -0
  18. package/dist/features/magic-context/memory/embedding-synapse.d.ts.map +1 -0
  19. package/dist/features/magic-context/memory/embedding.d.ts +1 -1
  20. package/dist/features/magic-context/memory/embedding.d.ts.map +1 -1
  21. package/dist/features/magic-context/message-index-async.d.ts +4 -1
  22. package/dist/features/magic-context/message-index-async.d.ts.map +1 -1
  23. package/dist/features/magic-context/message-index.d.ts +7 -4
  24. package/dist/features/magic-context/message-index.d.ts.map +1 -1
  25. package/dist/features/magic-context/migrations.d.ts +21 -16
  26. package/dist/features/magic-context/migrations.d.ts.map +1 -1
  27. package/dist/features/magic-context/plugin-messages.d.ts.map +1 -1
  28. package/dist/features/magic-context/project-embedding-registry.d.ts +27 -0
  29. package/dist/features/magic-context/project-embedding-registry.d.ts.map +1 -1
  30. package/dist/features/magic-context/search-measurement.d.ts +14 -0
  31. package/dist/features/magic-context/search-measurement.d.ts.map +1 -0
  32. package/dist/features/magic-context/search.d.ts +11 -1
  33. package/dist/features/magic-context/search.d.ts.map +1 -1
  34. package/dist/features/magic-context/session-project-backfill.d.ts +6 -3
  35. package/dist/features/magic-context/session-project-backfill.d.ts.map +1 -1
  36. package/dist/features/magic-context/session-project-storage.d.ts.map +1 -1
  37. package/dist/features/magic-context/smart-notes/capabilities.d.ts.map +1 -1
  38. package/dist/features/magic-context/smart-notes/compiler.d.ts +3 -1
  39. package/dist/features/magic-context/smart-notes/compiler.d.ts.map +1 -1
  40. package/dist/features/magic-context/smart-notes/runner.d.ts +1 -0
  41. package/dist/features/magic-context/smart-notes/runner.d.ts.map +1 -1
  42. package/dist/features/magic-context/smart-notes/sandbox-runner.d.ts +8 -1
  43. package/dist/features/magic-context/smart-notes/sandbox-runner.d.ts.map +1 -1
  44. package/dist/features/magic-context/smart-notes/ssrf-guard.d.ts +10 -0
  45. package/dist/features/magic-context/smart-notes/ssrf-guard.d.ts.map +1 -1
  46. package/dist/features/magic-context/smart-notes/storage.d.ts +16 -1
  47. package/dist/features/magic-context/smart-notes/storage.d.ts.map +1 -1
  48. package/dist/features/magic-context/storage-clone.d.ts +4 -0
  49. package/dist/features/magic-context/storage-clone.d.ts.map +1 -1
  50. package/dist/features/magic-context/storage-db.d.ts +7 -1
  51. package/dist/features/magic-context/storage-db.d.ts.map +1 -1
  52. package/dist/features/magic-context/storage-embedding-measurements.d.ts +60 -0
  53. package/dist/features/magic-context/storage-embedding-measurements.d.ts.map +1 -0
  54. package/dist/features/magic-context/storage-meta-persisted.d.ts +10 -5
  55. package/dist/features/magic-context/storage-meta-persisted.d.ts.map +1 -1
  56. package/dist/features/magic-context/storage-meta-session.d.ts.map +1 -1
  57. package/dist/features/magic-context/storage-schema-helpers.d.ts +3 -4
  58. package/dist/features/magic-context/storage-schema-helpers.d.ts.map +1 -1
  59. package/dist/features/magic-context/storage-tags.d.ts.map +1 -1
  60. package/dist/features/magic-context/storage.d.ts +1 -0
  61. package/dist/features/magic-context/storage.d.ts.map +1 -1
  62. package/dist/features/magic-context/tool-owner-backfill.d.ts +2 -0
  63. package/dist/features/magic-context/tool-owner-backfill.d.ts.map +1 -1
  64. package/dist/features/magic-context/transform-decision-log.d.ts +1 -1
  65. package/dist/features/magic-context/transform-decision-log.d.ts.map +1 -1
  66. package/dist/features/magic-context/user-memory/review-user-memories.d.ts.map +1 -1
  67. package/dist/hooks/magic-context/auto-search-runner.d.ts +7 -1
  68. package/dist/hooks/magic-context/auto-search-runner.d.ts.map +1 -1
  69. package/dist/hooks/magic-context/channel2-delivery.d.ts +2 -0
  70. package/dist/hooks/magic-context/channel2-delivery.d.ts.map +1 -1
  71. package/dist/hooks/magic-context/command-handler.d.ts +5 -0
  72. package/dist/hooks/magic-context/command-handler.d.ts.map +1 -1
  73. package/dist/hooks/magic-context/compaction-marker-manager.d.ts +4 -0
  74. package/dist/hooks/magic-context/compaction-marker-manager.d.ts.map +1 -1
  75. package/dist/hooks/magic-context/compartment-render-epoch.d.ts +12 -0
  76. package/dist/hooks/magic-context/compartment-render-epoch.d.ts.map +1 -0
  77. package/dist/hooks/magic-context/compartment-runner-validation.d.ts.map +1 -1
  78. package/dist/hooks/magic-context/decay-render.d.ts.map +1 -1
  79. package/dist/hooks/magic-context/emergency-drop.d.ts +1 -0
  80. package/dist/hooks/magic-context/emergency-drop.d.ts.map +1 -1
  81. package/dist/hooks/magic-context/emergency-fail-closed.d.ts +7 -0
  82. package/dist/hooks/magic-context/emergency-fail-closed.d.ts.map +1 -0
  83. package/dist/hooks/magic-context/event-handler.d.ts.map +1 -1
  84. package/dist/hooks/magic-context/final-wire-token-estimate.d.ts +29 -0
  85. package/dist/hooks/magic-context/final-wire-token-estimate.d.ts.map +1 -0
  86. package/dist/hooks/magic-context/heuristic-cleanup.d.ts +1 -0
  87. package/dist/hooks/magic-context/heuristic-cleanup.d.ts.map +1 -1
  88. package/dist/hooks/magic-context/historian-prompt.generated.d.ts +1 -1
  89. package/dist/hooks/magic-context/historian-prompt.generated.d.ts.map +1 -1
  90. package/dist/hooks/magic-context/hook-handlers.d.ts +6 -0
  91. package/dist/hooks/magic-context/hook-handlers.d.ts.map +1 -1
  92. package/dist/hooks/magic-context/hook.d.ts +19 -3
  93. package/dist/hooks/magic-context/hook.d.ts.map +1 -1
  94. package/dist/hooks/magic-context/index.d.ts +1 -1
  95. package/dist/hooks/magic-context/index.d.ts.map +1 -1
  96. package/dist/hooks/magic-context/inject-compartments.d.ts +1 -0
  97. package/dist/hooks/magic-context/inject-compartments.d.ts.map +1 -1
  98. package/dist/hooks/magic-context/lkg-replay.d.ts +51 -0
  99. package/dist/hooks/magic-context/lkg-replay.d.ts.map +1 -0
  100. package/dist/hooks/magic-context/lkg-slot.d.ts +25 -0
  101. package/dist/hooks/magic-context/lkg-slot.d.ts.map +1 -0
  102. package/dist/hooks/magic-context/module-state-sync.d.ts +169 -0
  103. package/dist/hooks/magic-context/module-state-sync.d.ts.map +1 -0
  104. package/dist/hooks/magic-context/module-wire.d.ts +73 -0
  105. package/dist/hooks/magic-context/module-wire.d.ts.map +1 -0
  106. package/dist/hooks/magic-context/pass-outcome.d.ts +15 -0
  107. package/dist/hooks/magic-context/pass-outcome.d.ts.map +1 -0
  108. package/dist/hooks/magic-context/read-session-chunk.d.ts +17 -1
  109. package/dist/hooks/magic-context/read-session-chunk.d.ts.map +1 -1
  110. package/dist/hooks/magic-context/read-session-raw.d.ts +30 -2
  111. package/dist/hooks/magic-context/read-session-raw.d.ts.map +1 -1
  112. package/dist/hooks/magic-context/recomp-orchestrator.d.ts.map +1 -1
  113. package/dist/hooks/magic-context/rust-mode-transform.d.ts +64 -0
  114. package/dist/hooks/magic-context/rust-mode-transform.d.ts.map +1 -0
  115. package/dist/hooks/magic-context/shadow-sender.d.ts +84 -49
  116. package/dist/hooks/magic-context/shadow-sender.d.ts.map +1 -1
  117. package/dist/hooks/magic-context/tag-messages.d.ts +2 -0
  118. package/dist/hooks/magic-context/tag-messages.d.ts.map +1 -1
  119. package/dist/hooks/magic-context/transform-postprocess-phase.d.ts +56 -0
  120. package/dist/hooks/magic-context/transform-postprocess-phase.d.ts.map +1 -1
  121. package/dist/hooks/magic-context/transform.d.ts +9 -0
  122. package/dist/hooks/magic-context/transform.d.ts.map +1 -1
  123. package/dist/index.d.ts.map +1 -1
  124. package/dist/index.js +17355 -10510
  125. package/dist/plugin/boot-quiet.d.ts +12 -0
  126. package/dist/plugin/boot-quiet.d.ts.map +1 -0
  127. package/dist/plugin/dream-timer.d.ts +6 -2
  128. package/dist/plugin/dream-timer.d.ts.map +1 -1
  129. package/dist/plugin/embedding-bootstrap-helpers.d.ts.map +1 -1
  130. package/dist/plugin/embedding-bootstrap.d.ts.map +1 -1
  131. package/dist/plugin/embedding-routing.d.ts +32 -0
  132. package/dist/plugin/embedding-routing.d.ts.map +1 -0
  133. package/dist/plugin/hooks/create-session-hooks.d.ts +54 -2
  134. package/dist/plugin/hooks/create-session-hooks.d.ts.map +1 -1
  135. package/dist/plugin/messages-transform.d.ts +4 -3
  136. package/dist/plugin/messages-transform.d.ts.map +1 -1
  137. package/dist/plugin/rpc-handlers.d.ts +19 -2
  138. package/dist/plugin/rpc-handlers.d.ts.map +1 -1
  139. package/dist/plugin/rust-tool-backends.d.ts +10 -0
  140. package/dist/plugin/rust-tool-backends.d.ts.map +1 -0
  141. package/dist/plugin/tool-registry.d.ts +2 -0
  142. package/dist/plugin/tool-registry.d.ts.map +1 -1
  143. package/dist/shared/models-dev-cache.d.ts +5 -0
  144. package/dist/shared/models-dev-cache.d.ts.map +1 -1
  145. package/dist/shared/rpc-client.d.ts +3 -1
  146. package/dist/shared/rpc-client.d.ts.map +1 -1
  147. package/dist/shared/rpc-notifications.d.ts +11 -6
  148. package/dist/shared/rpc-notifications.d.ts.map +1 -1
  149. package/dist/shared/rpc-server.d.ts +2 -2
  150. package/dist/shared/rpc-server.d.ts.map +1 -1
  151. package/dist/shared/rpc-types.d.ts +3 -0
  152. package/dist/shared/rpc-types.d.ts.map +1 -1
  153. package/dist/shared/sqlite.d.ts.map +1 -1
  154. package/dist/shared/tui-config.d.ts +5 -3
  155. package/dist/shared/tui-config.d.ts.map +1 -1
  156. package/dist/tools/ctx-memory/tools.d.ts.map +1 -1
  157. package/dist/tools/ctx-memory/types.d.ts +2 -0
  158. package/dist/tools/ctx-memory/types.d.ts.map +1 -1
  159. package/dist/tools/ctx-reduce/tools.d.ts +2 -0
  160. package/dist/tools/ctx-reduce/tools.d.ts.map +1 -1
  161. package/dist/tui/data/context-db.d.ts +9 -2
  162. package/dist/tui/data/context-db.d.ts.map +1 -1
  163. package/dist/tui/data/notification-socket.d.ts +4 -5
  164. package/dist/tui/data/notification-socket.d.ts.map +1 -1
  165. package/dist/tui-compiled/data/context-db.d.ts +9 -2
  166. package/dist/tui-compiled/data/context-db.d.ts.map +1 -1
  167. package/dist/tui-compiled/data/notification-socket.d.ts +4 -5
  168. package/dist/tui-compiled/data/notification-socket.d.ts.map +1 -1
  169. package/package.json +2 -1
  170. package/src/shared/models-dev-cache.test.ts +5 -0
  171. package/src/shared/models-dev-cache.ts +44 -6
  172. package/src/shared/opencode-compaction-detector.test.ts +11 -2
  173. package/src/shared/rpc-client.test.ts +213 -9
  174. package/src/shared/rpc-client.ts +48 -39
  175. package/src/shared/rpc-notifications.test.ts +26 -5
  176. package/src/shared/rpc-notifications.ts +44 -35
  177. package/src/shared/rpc-server.ts +69 -33
  178. package/src/shared/rpc-types.ts +3 -0
  179. package/src/shared/sqlite.ts +22 -3
  180. package/src/shared/tui-config.test.ts +4 -12
  181. package/src/shared/tui-config.ts +6 -6
  182. package/src/tui/data/context-db.test.ts +113 -0
  183. package/src/tui/data/context-db.ts +26 -19
  184. package/src/tui/data/notification-socket.test.ts +277 -11
  185. package/src/tui/data/notification-socket.ts +257 -86
  186. package/src/tui/index.tsx +42 -25
  187. package/src/tui/slots/sidebar-content.tsx +62 -24
  188. package/src/tui-compiled/data/context-db.ts +26 -19
  189. package/src/tui-compiled/data/notification-socket.ts +257 -86
  190. package/src/tui-compiled/index.tsx +83 -30
  191. package/src/tui-compiled/slots/sidebar-content.tsx +45 -23
@@ -102,7 +102,13 @@ const STICKY_MAX_ENTRIES = 100;
102
102
  const stickySidebarCache = new Map<string, CachedSnapshot>();
103
103
 
104
104
  function rememberSidebarSnapshot(snapshot: SidebarSnapshot): void {
105
- if (!snapshot.sessionId || snapshot.inputTokens <= 0) return;
105
+ if (!snapshot.sessionId) return;
106
+ if (snapshot.inputTokens <= 0) {
107
+ // A successful zero is authoritative (new/deleted/reverted session) and
108
+ // must prevent a later transport failure from resurrecting old values.
109
+ stickySidebarCache.delete(snapshot.sessionId);
110
+ return;
111
+ }
106
112
  // LRU-style bound: drop the oldest entry once we hit the cap. With a
107
113
  // 5-min TTL most stale entries time out naturally; this just prevents
108
114
  // unbounded growth across many session switches in a long TUI session.
@@ -126,19 +132,9 @@ function recallSidebarSnapshot(sessionId: string, fallback: SidebarSnapshot): Si
126
132
  stickySidebarCache.delete(sessionId);
127
133
  return fallback;
128
134
  }
129
- if (!hasInFlightEvidence(fallback)) {
130
- stickySidebarCache.delete(sessionId);
131
- return fallback;
132
- }
133
135
  return cached.snapshot;
134
136
  }
135
137
 
136
- function hasInFlightEvidence(snapshot: SidebarSnapshot): boolean {
137
- return (
138
- snapshot.compartmentInProgress || snapshot.historianRunning || snapshot.pendingOpsCount > 0
139
- );
140
- }
141
-
142
138
  /** Fetch sidebar snapshot from the server via RPC. */
143
139
  export async function loadSidebarSnapshot(
144
140
  sessionId: string,
@@ -152,9 +148,11 @@ export async function loadSidebarSnapshot(
152
148
  directory,
153
149
  });
154
150
  if ((result as unknown as Record<string, unknown>).error) {
151
+ // Snapshot-build errors are explicit failure envelopes, equivalent to
152
+ // a transport failure: retain the last known-good client snapshot.
155
153
  return recallSidebarSnapshot(sessionId, empty);
156
154
  }
157
- // Trust successful server responses. The server has its own sticky
155
+ // Trust successful server responses, including authoritative zeroes. The server has its own sticky
158
156
  // sidebar cache (`sidebar-snapshot-cache.ts`) that handles transient
159
157
  // zero-token windows by hybriding cached breakdown values into a
160
158
  // fresh snapshot, AND clears that cache on `session.deleted`. If the
@@ -249,14 +247,23 @@ export async function loadEmbedDetail(sessionId: string, directory: string): Pro
249
247
  }
250
248
  }
251
249
 
252
- /** Get compartment count via RPC. */
253
- export async function getCompartmentCount(sessionId: string): Promise<number> {
254
- if (!rpcClient) return 0;
250
+ export type CompartmentCountResult = { ok: true; count: number } | { ok: false; error: string };
251
+
252
+ /** Get compartment count without making transport failure look like a real zero. */
253
+ export async function getCompartmentCount(sessionId: string): Promise<CompartmentCountResult> {
254
+ if (!rpcClient) return { ok: false, error: "RPC client is not initialized" };
255
255
  try {
256
- const result = await rpcClient.call<{ count: number }>("compartment-count", { sessionId });
257
- return result.count ?? 0;
258
- } catch {
259
- return 0;
256
+ const result = await rpcClient.call<{ count?: number; error?: string }>(
257
+ "compartment-count",
258
+ { sessionId },
259
+ );
260
+ if (typeof result.error === "string") return { ok: false, error: result.error };
261
+ if (typeof result.count !== "number" || !Number.isFinite(result.count)) {
262
+ return { ok: false, error: "Invalid compartment count response" };
263
+ }
264
+ return { ok: true, count: result.count };
265
+ } catch (error) {
266
+ return { ok: false, error: error instanceof Error ? error.message : String(error) };
260
267
  }
261
268
  }
262
269
 
@@ -1,13 +1,15 @@
1
1
  import { afterEach, describe, expect, test } from "bun:test";
2
- import { mkdtempSync, rmSync } from "node:fs";
2
+ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
3
3
  import { tmpdir } from "node:os";
4
- import { join } from "node:path";
4
+ import { dirname, join } from "node:path";
5
5
  import {
6
+ __resetNotificationStateForTests,
6
7
  drainNotifications,
7
8
  isTuiConnected,
8
9
  pushNotification,
9
10
  } from "../../shared/rpc-notifications";
10
11
  import { MagicContextRpcServer } from "../../shared/rpc-server";
12
+ import { rpcPortFilePath } from "../../shared/rpc-utils";
11
13
  import { closeRpc, initRpcClient } from "./context-db";
12
14
  import {
13
15
  _resetNotificationSocketStateForTesting,
@@ -18,6 +20,7 @@ import {
18
20
  const originalXdgDataHome = process.env.XDG_DATA_HOME;
19
21
  const tempDirs: string[] = [];
20
22
  const servers: MagicContextRpcServer[] = [];
23
+ const legacyServers: Array<ReturnType<typeof Bun.serve>> = [];
21
24
 
22
25
  afterEach(() => {
23
26
  _resetNotificationSocketStateForTesting();
@@ -25,7 +28,8 @@ afterEach(() => {
25
28
  for (const server of servers.splice(0)) {
26
29
  server.stop();
27
30
  }
28
- drainNotifications(Number.MAX_SAFE_INTEGER);
31
+ for (const server of legacyServers.splice(0)) server.stop(true);
32
+ __resetNotificationStateForTests();
29
33
  for (const dir of tempDirs.splice(0)) {
30
34
  rmSync(dir, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 });
31
35
  }
@@ -54,6 +58,77 @@ async function startServer(dataHome: string, directory: string): Promise<MagicCo
54
58
  return server;
55
59
  }
56
60
 
61
+ function startLegacyServer(
62
+ dataHome: string,
63
+ directory: string,
64
+ notifications: SocketNotification[],
65
+ ): {
66
+ sockets: Set<{ close(): void }>;
67
+ helloCursors: number[];
68
+ ackCursors: number[];
69
+ } {
70
+ const token = "v032-query-token";
71
+ const sockets = new Set<{ close(): void }>();
72
+ const helloCursors: number[] = [];
73
+ const ackCursors: number[] = [];
74
+ const server = Bun.serve({
75
+ hostname: "127.0.0.1",
76
+ port: 0,
77
+ fetch(req, bunServer) {
78
+ const url = new URL(req.url);
79
+ if (url.pathname === "/health") {
80
+ // Frozen v0.32 response: pid only, with no instance_id.
81
+ return Response.json({ ok: true, pid: process.pid });
82
+ }
83
+ if (
84
+ url.pathname === "/ws" &&
85
+ req.headers.get("authorization") === `Bearer ${token}` &&
86
+ bunServer.upgrade(req, { data: {} })
87
+ ) {
88
+ return undefined;
89
+ }
90
+ return new Response("Not Found", { status: 404 });
91
+ },
92
+ websocket: {
93
+ open(ws) {
94
+ sockets.add(ws);
95
+ },
96
+ message(ws, raw) {
97
+ const msg = JSON.parse(String(raw)) as {
98
+ type?: string;
99
+ lastReceivedId?: number;
100
+ cursor?: number;
101
+ };
102
+ if (msg.type === "hello") {
103
+ const cursor = Number(msg.lastReceivedId ?? 0);
104
+ helloCursors.push(cursor);
105
+ for (const notification of notifications) {
106
+ if (notification.id > cursor) {
107
+ ws.send(JSON.stringify({ type: "notification", notification }));
108
+ }
109
+ }
110
+ // Frozen v0.32 acknowledgement: protocol epoch fields did not exist.
111
+ ws.send(JSON.stringify({ type: "hello-ack" }));
112
+ } else if (msg.type === "ack" && typeof msg.cursor === "number") {
113
+ ackCursors.push(msg.cursor);
114
+ }
115
+ },
116
+ close(ws) {
117
+ sockets.delete(ws);
118
+ },
119
+ },
120
+ });
121
+ legacyServers.push(server);
122
+
123
+ const portFile = rpcPortFilePath(storageDir(dataHome), directory, process.pid);
124
+ mkdirSync(dirname(portFile), { recursive: true });
125
+ writeFileSync(
126
+ portFile,
127
+ JSON.stringify({ port: server.port, pid: process.pid, started_at: Date.now(), token }),
128
+ );
129
+ return { sockets, helloCursors, ackCursors };
130
+ }
131
+
57
132
  async function waitFor(condition: () => boolean, label: string, timeoutMs = 4_000): Promise<void> {
58
133
  const start = Date.now();
59
134
  while (Date.now() - start < timeoutMs) {
@@ -64,6 +139,58 @@ async function waitFor(condition: () => boolean, label: string, timeoutMs = 4_00
64
139
  }
65
140
 
66
141
  describe("notification socket", () => {
142
+ test("a home-screen socket receives globals but leaves scoped notifications pending", async () => {
143
+ drainNotifications(Number.MAX_SAFE_INTEGER);
144
+ const dataHome = makeDataHome();
145
+ const directory = "/repo-home-screen";
146
+ await startServer(dataHome, directory);
147
+ initRpcClient(directory);
148
+
149
+ const received: SocketNotification[] = [];
150
+ startNotificationSocket({
151
+ getSessionId: () => null,
152
+ onNotification: (notification) => {
153
+ received.push(notification);
154
+ return true;
155
+ },
156
+ });
157
+ await waitFor(() => isTuiConnected(), "home-screen socket connection");
158
+ expect(isTuiConnected("ses_hidden")).toBe(false);
159
+
160
+ pushNotification("scoped", { action: "show-status-dialog" }, "ses_hidden");
161
+ pushNotification("global", { action: "show-status-dialog" });
162
+ await waitFor(
163
+ () => received.some((notification) => notification.type === "global"),
164
+ "global home-screen notification",
165
+ );
166
+ expect(received.some((notification) => notification.type === "scoped")).toBe(false);
167
+ expect(drainNotifications(0, "ses_hidden", { sessionOnly: true })).toHaveLength(1);
168
+ });
169
+
170
+ test("overlapping starts create one socket and one delivery", async () => {
171
+ drainNotifications(Number.MAX_SAFE_INTEGER);
172
+ const dataHome = makeDataHome();
173
+ const directory = "/repo-overlapping-start";
174
+ await startServer(dataHome, directory);
175
+ initRpcClient(directory);
176
+
177
+ let deliveries = 0;
178
+ const options = {
179
+ getSessionId: () => "ses_once",
180
+ onNotification: () => {
181
+ deliveries += 1;
182
+ return true;
183
+ },
184
+ };
185
+ startNotificationSocket(options);
186
+ startNotificationSocket(options);
187
+ await waitFor(() => isTuiConnected("ses_once"), "single overlapping connection");
188
+ pushNotification("once", { ok: true }, "ses_once");
189
+ await waitFor(() => deliveries === 1, "single notification delivery");
190
+ await new Promise((resolve) => setTimeout(resolve, 75));
191
+ expect(deliveries).toBe(1);
192
+ });
193
+
67
194
  test("uses the active session cursor when switching sessions", async () => {
68
195
  drainNotifications(Number.MAX_SAFE_INTEGER);
69
196
  const dataHome = makeDataHome();
@@ -99,8 +226,8 @@ describe("notification socket", () => {
99
226
  expect(received.map((notification) => notification.type)).toContain("for-b");
100
227
  });
101
228
 
102
- test("resets the cached endpoint after websocket close so reconnect discovers a new server", async () => {
103
- drainNotifications(Number.MAX_SAFE_INTEGER);
229
+ test("clears cursors and deduplication when a fresh server reuses notification ids", async () => {
230
+ __resetNotificationStateForTests();
104
231
  const dataHome = makeDataHome();
105
232
  const directory = "/repo-reconnect";
106
233
  const first = await startServer(dataHome, directory);
@@ -116,16 +243,155 @@ describe("notification socket", () => {
116
243
  });
117
244
  await waitFor(() => isTuiConnected("ses_R"), "initial websocket connection");
118
245
 
119
- const second = await startServer(dataHome, directory);
120
- expect(second).toBeDefined();
246
+ pushNotification("before-restart", { action: "show-status-dialog" }, "ses_R");
247
+ await waitFor(() => received.length === 1, "first notification acknowledgement");
248
+ await waitFor(
249
+ () => drainNotifications(0, "ses_R").length === 0,
250
+ "first server queue to empty",
251
+ );
252
+ expect(received[0].id).toBe(1);
253
+
121
254
  first.stop();
122
255
  await waitFor(() => !isTuiConnected("ses_R"), "old websocket sink removed");
123
- await waitFor(() => isTuiConnected("ses_R"), "websocket reconnected to replacement server");
256
+ // A process restart recreates module state as well as the RPC server. The
257
+ // client state intentionally survives so reused ids exercise epoch reset.
258
+ __resetNotificationStateForTests();
259
+ pushNotification("queued-after-restart", { action: "show-status-dialog" }, "ses_R");
260
+ const second = await startServer(dataHome, directory);
261
+ expect(second).toBeDefined();
124
262
 
125
- pushNotification("after-restart", { action: "show-status-dialog" }, "ses_R");
126
263
  await waitFor(
127
- () => received.some((notification) => notification.type === "after-restart"),
128
- "notification from replacement server",
264
+ () => received.some((notification) => notification.type === "queued-after-restart"),
265
+ "reused id from replacement server backlog",
129
266
  );
267
+ const restarted = received.find(
268
+ (notification) => notification.type === "queued-after-restart",
269
+ );
270
+ expect(restarted?.id).toBe(received[0].id);
271
+ });
272
+
273
+ test("acknowledges only consumed ids and redelivers a failed earlier notification", async () => {
274
+ __resetNotificationStateForTests();
275
+ const dataHome = makeDataHome();
276
+ const directory = "/repo-exact-ack";
277
+ await startServer(dataHome, directory);
278
+ initRpcClient(directory);
279
+
280
+ let releaseFirst: ((consumed: boolean) => void) | undefined;
281
+ const firstResult = new Promise<boolean>((resolve) => {
282
+ releaseFirst = resolve;
283
+ });
284
+ let slowCalls = 0;
285
+ let fastCalls = 0;
286
+ startNotificationSocket({
287
+ getSessionId: () => "ses_exact",
288
+ onNotification: (notification) => {
289
+ if (notification.type === "slow") {
290
+ slowCalls += 1;
291
+ return slowCalls === 1 ? firstResult : true;
292
+ }
293
+ fastCalls += 1;
294
+ return true;
295
+ },
296
+ });
297
+ await waitFor(() => isTuiConnected("ses_exact"), "exact ack socket connection");
298
+
299
+ pushNotification("slow", { action: "show-status-dialog" }, "ses_exact");
300
+ pushNotification("fast", { action: "show-toast" }, "ses_exact");
301
+ await waitFor(() => slowCalls === 1, "blocked first handler");
302
+ expect(fastCalls).toBe(0);
303
+ releaseFirst?.(false);
304
+ await waitFor(() => fastCalls === 1, "later handler completion");
305
+ await waitFor(() => {
306
+ const pending = drainNotifications(0, "ses_exact");
307
+ return pending.length === 1 && pending[0].type === "slow";
308
+ }, "only failed notification to remain queued");
309
+
310
+ _resetNotificationSocketStateForTesting();
311
+ await waitFor(() => !isTuiConnected("ses_exact"), "socket disconnect before redelivery");
312
+ startNotificationSocket({
313
+ getSessionId: () => "ses_exact",
314
+ onNotification: (notification) => {
315
+ if (notification.type === "slow") slowCalls += 1;
316
+ if (notification.type === "fast") fastCalls += 1;
317
+ return true;
318
+ },
319
+ });
320
+ await waitFor(() => slowCalls === 2, "failed notification redelivery");
321
+ expect(fastCalls).toBe(1);
322
+ });
323
+
324
+ test("legacy mode keeps its watermark behind a declined notification across reconnect", async () => {
325
+ const dataHome = makeDataHome();
326
+ const directory = "/repo-v032-notifications";
327
+ const sessionId = "ses_v032";
328
+ const legacy = startLegacyServer(dataHome, directory, [
329
+ { id: 1, type: "declined", payload: {}, sessionId },
330
+ { id: 2, type: "consumed", payload: {}, sessionId },
331
+ ]);
332
+ initRpcClient(directory);
333
+
334
+ let declinedCalls = 0;
335
+ let consumedCalls = 0;
336
+ startNotificationSocket({
337
+ getSessionId: () => sessionId,
338
+ onNotification: (notification) => {
339
+ if (notification.type === "declined") {
340
+ declinedCalls += 1;
341
+ return declinedCalls > 1;
342
+ }
343
+ consumedCalls += 1;
344
+ return true;
345
+ },
346
+ });
347
+
348
+ await waitFor(() => declinedCalls === 1 && consumedCalls === 1, "legacy backlog handling");
349
+ expect(Math.max(0, ...legacy.ackCursors)).toBe(0);
350
+ for (const ws of legacy.sockets) ws.close();
351
+
352
+ await waitFor(() => legacy.helloCursors.length >= 2, "legacy reconnect hello");
353
+ await waitFor(() => declinedCalls === 2, "declined notification redelivery");
354
+ expect(legacy.helloCursors[1]).toBe(0);
355
+ expect(consumedCalls).toBe(1);
356
+ await waitFor(() => legacy.ackCursors.includes(2), "gap-safe legacy watermark advance");
357
+ });
358
+
359
+ test("serializes back-to-back dialog notification handlers", async () => {
360
+ __resetNotificationStateForTests();
361
+ const dataHome = makeDataHome();
362
+ const directory = "/repo-serialized-dialogs";
363
+ await startServer(dataHome, directory);
364
+ initRpcClient(directory);
365
+
366
+ let releaseFirst: (() => void) | undefined;
367
+ const firstSettled = new Promise<void>((resolve) => {
368
+ releaseFirst = resolve;
369
+ });
370
+ const events: string[] = [];
371
+ startNotificationSocket({
372
+ getSessionId: () => "ses_dialogs",
373
+ onNotification: async (notification) => {
374
+ events.push(`start:${notification.type}`);
375
+ if (notification.type === "dialog-one") await firstSettled;
376
+ events.push(`finish:${notification.type}`);
377
+ return true;
378
+ },
379
+ });
380
+ await waitFor(() => isTuiConnected("ses_dialogs"), "dialog socket connection");
381
+
382
+ pushNotification("dialog-one", { action: "show-status-dialog" }, "ses_dialogs");
383
+ pushNotification("dialog-two", { action: "show-upgrade-dialog" }, "ses_dialogs");
384
+ await waitFor(() => events.includes("start:dialog-one"), "first dialog handler start");
385
+ await new Promise((resolve) => setTimeout(resolve, 75));
386
+ expect(events).toEqual(["start:dialog-one"]);
387
+
388
+ releaseFirst?.();
389
+ await waitFor(() => events.includes("finish:dialog-two"), "second dialog handler finish");
390
+ expect(events).toEqual([
391
+ "start:dialog-one",
392
+ "finish:dialog-one",
393
+ "start:dialog-two",
394
+ "finish:dialog-two",
395
+ ]);
130
396
  });
131
397
  });