@wolfx/opencode-magic-context 0.28.0 → 0.30.3

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 (108) hide show
  1. package/dist/agents/magic-context-prompt.d.ts +1 -1
  2. package/dist/agents/magic-context-prompt.d.ts.map +1 -1
  3. package/dist/config/schema/magic-context.d.ts +11 -0
  4. package/dist/config/schema/magic-context.d.ts.map +1 -1
  5. package/dist/features/magic-context/compartment-chunk-embedding.d.ts.map +1 -1
  6. package/dist/features/magic-context/dreamer/retrospective-raw-provider.d.ts +0 -1
  7. package/dist/features/magic-context/dreamer/retrospective-raw-provider.d.ts.map +1 -1
  8. package/dist/features/magic-context/dreamer/storage-task-schedule.d.ts +10 -0
  9. package/dist/features/magic-context/dreamer/storage-task-schedule.d.ts.map +1 -1
  10. package/dist/features/magic-context/dreamer/task-executor.d.ts +0 -3
  11. package/dist/features/magic-context/dreamer/task-executor.d.ts.map +1 -1
  12. package/dist/features/magic-context/dreamer/task-prompts.d.ts +1 -1
  13. package/dist/features/magic-context/dreamer/task-prompts.d.ts.map +1 -1
  14. package/dist/features/magic-context/dreamer/task-registry.d.ts +0 -1
  15. package/dist/features/magic-context/dreamer/task-registry.d.ts.map +1 -1
  16. package/dist/features/magic-context/memory/embedding-openai.d.ts.map +1 -1
  17. package/dist/features/magic-context/project-embedding-registry.d.ts.map +1 -1
  18. package/dist/features/magic-context/recursive-text-splitter.d.ts +36 -0
  19. package/dist/features/magic-context/recursive-text-splitter.d.ts.map +1 -0
  20. package/dist/features/magic-context/smart-notes/sandbox-runner.d.ts.map +1 -1
  21. package/dist/features/magic-context/storage-db.d.ts +2 -21
  22. package/dist/features/magic-context/storage-db.d.ts.map +1 -1
  23. package/dist/features/magic-context/storage-schema-helpers.d.ts +30 -0
  24. package/dist/features/magic-context/storage-schema-helpers.d.ts.map +1 -0
  25. package/dist/features/magic-context/storage-tags.d.ts.map +1 -1
  26. package/dist/features/magic-context/types.d.ts +12 -1
  27. package/dist/features/magic-context/types.d.ts.map +1 -1
  28. package/dist/hooks/magic-context/apply-operations.d.ts +8 -1
  29. package/dist/hooks/magic-context/apply-operations.d.ts.map +1 -1
  30. package/dist/hooks/magic-context/channel2-delivery.d.ts +9 -5
  31. package/dist/hooks/magic-context/channel2-delivery.d.ts.map +1 -1
  32. package/dist/hooks/magic-context/edit-marker.d.ts +11 -0
  33. package/dist/hooks/magic-context/edit-marker.d.ts.map +1 -0
  34. package/dist/hooks/magic-context/event-handler.d.ts +1 -4
  35. package/dist/hooks/magic-context/event-handler.d.ts.map +1 -1
  36. package/dist/hooks/magic-context/hook.d.ts +1 -2
  37. package/dist/hooks/magic-context/hook.d.ts.map +1 -1
  38. package/dist/hooks/magic-context/read-session-formatting.d.ts.map +1 -1
  39. package/dist/hooks/magic-context/supersession-reclaim.d.ts +34 -0
  40. package/dist/hooks/magic-context/supersession-reclaim.d.ts.map +1 -0
  41. package/dist/hooks/magic-context/system-prompt-hash.d.ts +5 -0
  42. package/dist/hooks/magic-context/system-prompt-hash.d.ts.map +1 -1
  43. package/dist/hooks/magic-context/tag-messages.d.ts +8 -0
  44. package/dist/hooks/magic-context/tag-messages.d.ts.map +1 -1
  45. package/dist/hooks/magic-context/tool-drop-target.d.ts +2 -0
  46. package/dist/hooks/magic-context/tool-drop-target.d.ts.map +1 -1
  47. package/dist/hooks/magic-context/transform-postprocess-phase.d.ts +8 -0
  48. package/dist/hooks/magic-context/transform-postprocess-phase.d.ts.map +1 -1
  49. package/dist/hooks/magic-context/transform.d.ts +4 -0
  50. package/dist/hooks/magic-context/transform.d.ts.map +1 -1
  51. package/dist/index.d.ts.map +1 -1
  52. package/dist/index.js +3587 -5086
  53. package/dist/plugin/hooks/create-session-hooks.d.ts.map +1 -1
  54. package/dist/plugin/rpc-handlers.d.ts.map +1 -1
  55. package/dist/plugin/tool-registry.d.ts.map +1 -1
  56. package/dist/shared/announcement.d.ts +1 -1
  57. package/dist/shared/announcement.d.ts.map +1 -1
  58. package/dist/shared/commit-detection.d.ts +29 -0
  59. package/dist/shared/commit-detection.d.ts.map +1 -0
  60. package/dist/shared/data-path.d.ts.map +1 -1
  61. package/dist/shared/exit-abort-registry.d.ts +25 -0
  62. package/dist/shared/exit-abort-registry.d.ts.map +1 -0
  63. package/dist/shared/harness-provider-map.d.ts +30 -0
  64. package/dist/shared/harness-provider-map.d.ts.map +1 -0
  65. package/dist/shared/rpc-client.d.ts +8 -0
  66. package/dist/shared/rpc-client.d.ts.map +1 -1
  67. package/dist/shared/rpc-notifications.d.ts +28 -10
  68. package/dist/shared/rpc-notifications.d.ts.map +1 -1
  69. package/dist/shared/rpc-server.d.ts +22 -3
  70. package/dist/shared/rpc-server.d.ts.map +1 -1
  71. package/dist/shared/tag-transcript.d.ts.map +1 -1
  72. package/dist/shared/transcript.d.ts +15 -0
  73. package/dist/shared/transcript.d.ts.map +1 -1
  74. package/dist/tools/ctx-note/tools.d.ts.map +1 -1
  75. package/dist/tui/badge-contrast.d.ts +37 -22
  76. package/dist/tui/badge-contrast.d.ts.map +1 -1
  77. package/dist/tui/data/context-db.d.ts +4 -14
  78. package/dist/tui/data/context-db.d.ts.map +1 -1
  79. package/dist/tui/data/notification-socket.d.ts +39 -0
  80. package/dist/tui/data/notification-socket.d.ts.map +1 -0
  81. package/package.json +78 -77
  82. package/src/shared/announcement.ts +2 -3
  83. package/src/shared/commit-detection.test.ts +63 -0
  84. package/src/shared/commit-detection.ts +53 -0
  85. package/src/shared/data-path.test.ts +28 -0
  86. package/src/shared/data-path.ts +5 -0
  87. package/src/shared/exit-abort-registry.test.ts +50 -0
  88. package/src/shared/exit-abort-registry.ts +46 -0
  89. package/src/shared/harness-provider-map.test.ts +63 -0
  90. package/src/shared/harness-provider-map.ts +56 -0
  91. package/src/shared/rpc-client.ts +14 -0
  92. package/src/shared/rpc-notifications.test.ts +68 -11
  93. package/src/shared/rpc-notifications.ts +75 -36
  94. package/src/shared/rpc-server.ts +249 -150
  95. package/src/shared/tag-transcript.ts +32 -0
  96. package/src/shared/transcript-opencode.ts +33 -0
  97. package/src/shared/transcript.ts +17 -0
  98. package/src/tui/badge-contrast.test.ts +39 -1
  99. package/src/tui/badge-contrast.ts +63 -25
  100. package/src/tui/data/context-db.ts +10 -64
  101. package/src/tui/data/notification-socket.ts +229 -0
  102. package/src/tui/index.tsx +68 -118
  103. package/src/tui/slots/sidebar-content.tsx +2 -2
  104. package/dist/hooks/is-anthropic-provider.d.ts +0 -2
  105. package/dist/hooks/is-anthropic-provider.d.ts.map +0 -1
  106. package/dist/shared/live-server-client.d.ts +0 -50
  107. package/dist/shared/live-server-client.d.ts.map +0 -1
  108. package/src/shared/live-server-client.ts +0 -152
@@ -1,5 +1,11 @@
1
1
  import { describe, expect, test } from "bun:test";
2
- import { drainNotifications, isTuiConnected, pushNotification } from "./rpc-notifications";
2
+ import {
3
+ drainNotifications,
4
+ isTuiConnected,
5
+ type NotificationSink,
6
+ pushNotification,
7
+ registerNotificationSink,
8
+ } from "./rpc-notifications";
3
9
 
4
10
  describe("rpc notifications", () => {
5
11
  test("keeps messages queued until the client acks their id", () => {
@@ -45,17 +51,68 @@ describe("rpc notifications", () => {
45
51
  expect(poll.map((m) => m.type).sort()).toEqual(["x", "y"]);
46
52
  });
47
53
 
48
- test("isTuiConnected is per-session: a TUI on session A does not mark session B connected", () => {
49
- // A TUI draining for tuiA must not make tuiB's producers think a TUI is
50
- // polling for tuiB (which would route tuiB's /ctx-status, upgrade
51
- // reminder, etc. to the dialog path and lose them in the unrelated TUI).
52
- // Use ids no other test drains so the per-session window is unambiguous.
53
- drainNotifications(0, "ses_tuiA_only");
54
- expect(isTuiConnected("ses_tuiA_only")).toBe(true);
55
- expect(isTuiConnected("ses_tuiB_never_drained")).toBe(false);
56
- // The session-less (global) query still reports recent activity for the
57
- // legacy callers that have no session context.
54
+ test("isTuiConnected reflects live WS sinks per-session", () => {
55
+ // No sinks nothing connected.
56
+ expect(isTuiConnected("ses_anything")).toBe(false);
57
+ expect(isTuiConnected()).toBe(false);
58
+
59
+ // A live sink scoped to session A marks ONLY A connected (so B's producers
60
+ // don't misroute B's /ctx-status / upgrade reminder to the dialog path and
61
+ // lose it in an unrelated TUI), and the global query is also "connected".
62
+ const unregister = registerNotificationSink({ sessionId: "ses_A", send: () => {} });
63
+ expect(isTuiConnected("ses_A")).toBe(true);
64
+ expect(isTuiConnected("ses_B")).toBe(false);
58
65
  expect(isTuiConnected()).toBe(true);
66
+
67
+ // Closing the socket removes the sink → disconnected again.
68
+ unregister();
69
+ expect(isTuiConnected("ses_A")).toBe(false);
70
+ expect(isTuiConnected()).toBe(false);
71
+ });
72
+
73
+ test("a session-less sink counts as connected for any session query", () => {
74
+ const unregister = registerNotificationSink({ sessionId: undefined, send: () => {} });
75
+ expect(isTuiConnected("ses_whatever")).toBe(true);
76
+ expect(isTuiConnected()).toBe(true);
77
+ unregister();
78
+ });
79
+
80
+ test("pushNotification fans out live to a matching sink and skips a foreign session", () => {
81
+ drainNotifications(Number.MAX_SAFE_INTEGER);
82
+ const received: string[] = [];
83
+ const sink: NotificationSink = {
84
+ sessionId: "ses_live",
85
+ send: (n) => received.push(n.type),
86
+ };
87
+ const unregister = registerNotificationSink(sink);
88
+
89
+ pushNotification("for-live", { action: "show-status-dialog" }, "ses_live");
90
+ pushNotification("for-other", { action: "show-status-dialog" }, "ses_other");
91
+ pushNotification("global", { action: "show-status-dialog" });
92
+
93
+ // The sink sees its own session + global, never the foreign session.
94
+ expect(received.sort()).toEqual(["for-live", "global"]);
95
+ unregister();
96
+ });
97
+
98
+ test("a dead sink (throwing send) does not block delivery to other sinks", () => {
99
+ drainNotifications(Number.MAX_SAFE_INTEGER);
100
+ const live: string[] = [];
101
+ const unregDead = registerNotificationSink({
102
+ sessionId: undefined,
103
+ send: () => {
104
+ throw new Error("socket dead");
105
+ },
106
+ });
107
+ const unregLive = registerNotificationSink({
108
+ sessionId: undefined,
109
+ send: (n) => live.push(n.type),
110
+ });
111
+ // Must not throw, and the live sink still receives it.
112
+ expect(() => pushNotification("resilient", { ok: true })).not.toThrow();
113
+ expect(live).toEqual(["resilient"]);
114
+ unregDead();
115
+ unregLive();
59
116
  });
60
117
 
61
118
  test("queue-cap eviction is session-fair: a noisy session cannot evict another session's newest unseen item", () => {
@@ -16,31 +16,73 @@ export interface RpcNotification {
16
16
 
17
17
  let queue: RpcNotification[] = [];
18
18
  let nextNotificationId = 1;
19
- // Timestamp of last drain — used to detect if a TUI is actively polling.
20
- // The TUI polls every 500ms; we consider it connected if it polled within
21
- // the last 3 seconds (6× the poll interval, tolerates transient delays).
22
- //
23
- // PER-SESSION: a single server process can serve MANY sessions (e.g. a TUI on
24
- // session A plus an OpenCode Desktop opened on session B for the same project,
25
- // whose newer RPC server this TUI's port discovery then selects). The TUI
26
- // poller drains with ITS active session id, so a session is "TUI-connected"
27
- // only if a TUI recently drained FOR THAT session. A process-global timestamp
28
- // would make session B's producers (`/ctx-status`, upgrade reminder) take the
29
- // TUI-dialog path because session A's TUI is polling — queuing a B-scoped
30
- // dialog action that A's poller correctly refuses to show, so B's notice is
31
- // lost (it also suppressed B's non-TUI fallback). Tracking drains per session
32
- // routes each producer to the right delivery path.
33
- const lastDrainAtBySession = new Map<string, number>();
34
- let lastDrainAtAny = 0;
35
- const TUI_CONNECTED_WINDOW_MS = 3_000;
36
19
 
37
- /** Push a notification for TUI to pick up via polling. */
20
+ /**
21
+ * A connected TUI notification sink — one per authenticated WebSocket. The RPC
22
+ * server registers a sink when a TUI socket authenticates (hello) and removes
23
+ * it on close. `send` is sink-agnostic (the server owns the actual WS socket)
24
+ * so this module stays free of Bun/WS types.
25
+ */
26
+ export interface NotificationSink {
27
+ /** The TUI's active session at connect time (its hello scope). */
28
+ sessionId?: string;
29
+ /** Deliver one notification over this sink's live socket. */
30
+ send: (notification: RpcNotification) => void;
31
+ }
32
+
33
+ // Live sinks replace the old poll-drain-timestamp inference. "TUI connected for
34
+ // a session" is now exact socket liveness — accurate and immediate — instead of
35
+ // "did a 500ms poll drain within the last 3s". Per-session scoping still matters:
36
+ // one process can serve MANY sessions (a TUI on session A plus an OpenCode
37
+ // Desktop opened on session B for the same project, whose newer RPC server this
38
+ // TUI's port discovery then selects). Each sink carries ITS session, so a
39
+ // B-scoped producer (`/ctx-status`, upgrade reminder) only sees B's TUI as
40
+ // connected and routes its dialog there, never to A.
41
+ const sinks = new Set<NotificationSink>();
42
+
43
+ /** Register a live TUI sink. Returns an unregister fn (call on socket close). */
44
+ export function registerNotificationSink(sink: NotificationSink): () => void {
45
+ sinks.add(sink);
46
+ return () => {
47
+ sinks.delete(sink);
48
+ };
49
+ }
50
+
51
+ /** Whether a given notification may be delivered to a given sink. A global
52
+ * notification (no sessionId) reaches every sink; a session-scoped one reaches
53
+ * only sinks for that session (or session-less sinks). Mirrors the drain filter
54
+ * from the sink's perspective. */
55
+ function notificationMatchesSink(notification: RpcNotification, sink: NotificationSink): boolean {
56
+ return (
57
+ notification.sessionId === undefined ||
58
+ sink.sessionId === undefined ||
59
+ notification.sessionId === sink.sessionId
60
+ );
61
+ }
62
+
63
+ /** Push a notification to the TUI. Fans out to any live WS sink immediately and
64
+ * also enqueues it so a TUI that is momentarily disconnected (reconnecting, or
65
+ * not yet connected) still receives it on its next hello via the backlog drain.
66
+ * At-least-once: a live push that the socket drops is re-delivered from the
67
+ * queue on reconnect (pruned only when the client acks via `lastReceivedId`). */
38
68
  export function pushNotification(
39
69
  type: string,
40
70
  payload: Record<string, unknown>,
41
71
  sessionId?: string,
42
72
  ): void {
43
- queue.push({ id: nextNotificationId++, type, payload, sessionId });
73
+ const notification: RpcNotification = { id: nextNotificationId++, type, payload, sessionId };
74
+ queue.push(notification);
75
+ // Fan out to every live sink this notification is scoped to. A delivery throw
76
+ // (dead socket mid-send) must not block other sinks or the caller.
77
+ for (const sink of sinks) {
78
+ if (!notificationMatchesSink(notification, sink)) continue;
79
+ try {
80
+ sink.send(notification);
81
+ } catch {
82
+ // Socket died between liveness check and send; the close handler will
83
+ // unregister it, and the queue backlog re-delivers on reconnect.
84
+ }
85
+ }
44
86
  // Cap queue size to prevent unbounded growth if a TUI is not draining.
45
87
  // Session-FAIR eviction: a naive `slice(-50)` drops the globally-oldest
46
88
  // items, so a noisy session could evict ANOTHER session's single unseen
@@ -66,13 +108,12 @@ export function pushNotification(
66
108
  }
67
109
 
68
110
  /** Return pending notifications after acking the client's last received id.
69
- * Updates lastDrainAt so isTuiConnected() reflects recent activity.
70
111
  *
71
112
  * Session scoping: when `sessionId` is provided, only notifications tagged for
72
113
  * that session (or session-less/global ones) are returned and pruned — a
73
114
  * notification tagged for a DIFFERENT session is never handed to this client
74
115
  * and is never pruned by this client's ack. This matters because the in-memory
75
- * queue is per-process but a TUI can end up draining a process that also serves
116
+ * queue is per-process but a TUI can end up bound to a process that also serves
76
117
  * OTHER sessions: e.g. opening OpenCode Desktop on the same project starts a
77
118
  * newer RPC server that the TUI's port discovery (newest-pid-wins) then selects,
78
119
  * so a Desktop-session upgrade-dialog action would otherwise surface in an
@@ -82,11 +123,9 @@ export function pushNotification(
82
123
  *
83
124
  * Delivery is at-least-once (non-destructive return + prune-on-ack): a returned
84
125
  * notification stays queued until a later call acks it via a higher
85
- * `lastReceivedId`, so a lost poll response re-delivers on the next poll. */
126
+ * `lastReceivedId`, so a dropped WS socket re-delivers the backlog on reconnect
127
+ * (the client sends its `lastReceivedId` in the hello). */
86
128
  export function drainNotifications(lastReceivedId = 0, sessionId?: string): RpcNotification[] {
87
- const now = Date.now();
88
- lastDrainAtAny = now;
89
- if (sessionId !== undefined) lastDrainAtBySession.set(sessionId, now);
90
129
  const matchesClient = (notification: RpcNotification): boolean =>
91
130
  sessionId === undefined ||
92
131
  notification.sessionId === undefined ||
@@ -103,20 +142,20 @@ export function drainNotifications(lastReceivedId = 0, sessionId?: string): RpcN
103
142
  );
104
143
  }
105
144
 
106
- /** Whether a TUI client is actively polling for notifications.
107
- * Returns true only if a TUI has drained within the last 3 seconds.
145
+ /** Whether a TUI client is connected via a live notification socket.
146
+ * Now exact socket liveness (a registered WS sink), not a poll-drain timestamp.
108
147
  *
109
- * Pass `sessionId` (preferred) to ask whether a TUI is polling FOR THAT
148
+ * Pass `sessionId` (preferred) to ask whether a TUI is connected FOR THAT
110
149
  * SESSION — this is what producers (`/ctx-status`, `/ctx-recomp`, the upgrade
111
150
  * reminder) must use to decide dialog-vs-message, so a TUI on a different
112
- * session in the same process does not misroute their delivery. Omit it only
113
- * for legacy/global callers that genuinely have no session context; they fall
114
- * back to "any session recently drained" (the pre-per-session behavior). */
151
+ * session in the same process does not misroute their delivery. A session-less
152
+ * sink (legacy/global) counts for any session query. Omit `sessionId` only for
153
+ * callers with no session context; they get "any sink connected". */
115
154
  export function isTuiConnected(sessionId?: string): boolean {
116
- const now = Date.now();
117
- if (sessionId !== undefined) {
118
- const at = lastDrainAtBySession.get(sessionId) ?? 0;
119
- return at > 0 && now - at < TUI_CONNECTED_WINDOW_MS;
155
+ if (sinks.size === 0) return false;
156
+ if (sessionId === undefined) return true;
157
+ for (const sink of sinks) {
158
+ if (sink.sessionId === undefined || sink.sessionId === sessionId) return true;
120
159
  }
121
- return lastDrainAtAny > 0 && now - lastDrainAtAny < TUI_CONNECTED_WINDOW_MS;
160
+ return false;
122
161
  }