@cortexkit/opencode-magic-context 0.30.0 → 0.30.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.
@@ -1,8 +1,12 @@
1
+ import { MagicContextRpcClient } from "../../shared/rpc-client";
1
2
  import type { EmbedDetail, SidebarSnapshot, StatusDetail } from "../../shared/rpc-types";
2
3
  export type { EmbedDetail, SidebarSnapshot, StatusDetail };
3
4
  /** Initialize the RPC client. Call once on TUI startup. */
4
5
  export declare function initRpcClient(directory: string): void;
5
6
  export declare function getRpcGeneration(): number;
7
+ /** The live RPC client (for the WS notification socket's endpoint discovery).
8
+ * Null before init / after close. */
9
+ export declare function getRpcClient(): MagicContextRpcClient | null;
6
10
  /** Clean up the RPC client. */
7
11
  export declare function closeRpc(): void;
8
12
  /** Fetch sidebar snapshot from the server via RPC. */
@@ -24,12 +28,6 @@ export declare function requestUpgrade(sessionId: string): Promise<boolean>;
24
28
  export declare function dismissUpgradeReminder(sessionId: string): Promise<boolean>;
25
29
  /** Resolve global toast duration from server config via RPC. */
26
30
  export declare function loadToastDurationMs(): Promise<number>;
27
- export interface TuiMessage {
28
- id: number;
29
- type: string;
30
- payload: Record<string, unknown>;
31
- sessionId?: string;
32
- }
33
31
  /**
34
32
  * Fetch the current startup announcement from the server, if any.
35
33
  * Returns `{show: false}` when there's nothing to announce or when the
@@ -44,12 +42,4 @@ export interface AnnouncementResponse {
44
42
  export declare function getAnnouncement(): Promise<AnnouncementResponse>;
45
43
  /** Mark the current ANNOUNCEMENT_VERSION as dismissed on the server. */
46
44
  export declare function markAnnounced(): Promise<boolean>;
47
- /** Poll for pending server→TUI notifications via RPC. */
48
- export declare function consumeTuiMessages(sessionId: string): Promise<TuiMessage[]>;
49
- /**
50
- * Advance the delivered-message cursor for one active TUI session.
51
- * Callers must pass only the contiguous handled prefix of the drained batch;
52
- * this helper remains empty-safe and monotonic for that prefix.
53
- */
54
- export declare function markTuiMessagesHandled(sessionId: string, messages: TuiMessage[]): void;
55
45
  //# sourceMappingURL=context-db.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"context-db.d.ts","sourceRoot":"","sources":["../../../src/tui/data/context-db.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EACR,WAAW,EAEX,eAAe,EACf,YAAY,EACf,MAAM,wBAAwB,CAAC;AAEhC,YAAY,EAAE,WAAW,EAAE,eAAe,EAAE,YAAY,EAAE,CAAC;AAe3D,2DAA2D;AAC3D,wBAAgB,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAOrD;AAED,wBAAgB,gBAAgB,IAAI,MAAM,CAEzC;AAED,+BAA+B;AAC/B,wBAAgB,QAAQ,IAAI,IAAI,CAO/B;AA4FD,sDAAsD;AACtD,wBAAsB,mBAAmB,CACrC,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,GAClB,OAAO,CAAC,eAAe,CAAC,CA4B1B;AAED,wDAAwD;AACxD,wBAAsB,gBAAgB,CAClC,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,EACjB,QAAQ,CAAC,EAAE,MAAM,GAClB,OAAO,CAAC,YAAY,CAAC,CA0CvB;AAYD,gEAAgE;AAChE,wBAAsB,eAAe,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,CAchG;AAED,qCAAqC;AACrC,wBAAsB,mBAAmB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAQ5E;AAED,6CAA6C;AAC7C,wBAAsB,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAQvE;AAED;mFACmF;AACnF,wBAAsB,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAQxE;AAED;;iDAEiD;AACjD,wBAAsB,sBAAsB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,C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AAI,OAAO,CAAC,OAAO,CAAC,CAQtD"}
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Persistent WebSocket to the server plugin's RPC server, replacing the old
3
+ * 500ms HTTP notification poll.
4
+ *
5
+ * Why this exists: the TUI plugin and the server plugin run in separate Bun
6
+ * runners in the same process, so they bridge over a localhost socket. The old
7
+ * bridge polled `pending-notifications` over HTTP every 500ms — and each poll
8
+ * opened a NEW loopback TCP connection (Bun's fetch isn't pooled to our server),
9
+ * which was the entire source of idle TUI CPU (#200). A single long-lived WS
10
+ * carries server→TUI pushes with zero per-event connection cost, and the server
11
+ * pushes notifications the instant they're queued (no polling latency).
12
+ *
13
+ * Session scope: the socket carries the TUI's active session in its `hello` so
14
+ * the server delivers only that session's (plus global) notifications and its
15
+ * `isTuiConnected(session)` routing stays correct. The active session is tracked
16
+ * with a cheap watcher that only reads `api.route.current` (a property access,
17
+ * no IPC) and re-scopes the socket ONLY when the session actually changes — so
18
+ * unlike the old poll it does no network work at idle.
19
+ */
20
+ export interface SocketNotification {
21
+ id: number;
22
+ type: string;
23
+ payload: Record<string, unknown>;
24
+ sessionId?: string;
25
+ }
26
+ interface NotificationSocketOptions {
27
+ /** Current active session id (re-read cheaply to follow session switches). */
28
+ getSessionId: () => string | null;
29
+ /** Handle one delivered notification. Returns true if it was consumed (so its
30
+ * id can advance the ack cursor). Async because dialog handlers await. */
31
+ onNotification: (notification: SocketNotification) => boolean | Promise<boolean>;
32
+ }
33
+ /** Open the persistent notification socket. Idempotent: a second call while open
34
+ * is a no-op. Reconnects on its own after any drop. */
35
+ export declare function startNotificationSocket(options: NotificationSocketOptions): void;
36
+ /** Close the socket and stop reconnecting. Call on TUI dispose. */
37
+ export declare function stopNotificationSocket(): void;
38
+ export {};
39
+ //# sourceMappingURL=notification-socket.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"notification-socket.d.ts","sourceRoot":"","sources":["../../../src/tui/data/notification-socket.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAIH,MAAM,WAAW,kBAAkB;IAC/B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACjC,SAAS,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,UAAU,yBAAyB;IAC/B,8EAA8E;IAC9E,YAAY,EAAE,MAAM,MAAM,GAAG,IAAI,CAAC;IAClC;+EAC2E;IAC3E,cAAc,EAAE,CAAC,YAAY,EAAE,kBAAkB,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CACpF;AAsBD;wDACwD;AACxD,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,yBAAyB,GAAG,IAAI,CAQhF;AAED,mEAAmE;AACnE,wBAAgB,sBAAsB,IAAI,IAAI,CAkB7C"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/opencode-magic-context",
3
- "version": "0.30.0",
3
+ "version": "0.30.2",
4
4
  "type": "module",
5
5
  "description": "OpenCode plugin for Magic Context — cross-session memory and context management",
6
6
  "main": "dist/index.js",
@@ -24,16 +24,14 @@ import { getMagicContextStorageDir } from "./data-path";
24
24
  * Bump only when there are user-visible changes worth a startup dialog.
25
25
  * Does NOT need to match the published package version.
26
26
  */
27
- export const ANNOUNCEMENT_VERSION = "0.30.0";
27
+ export const ANNOUNCEMENT_VERSION = "0.30.2";
28
28
 
29
29
  /**
30
30
  * Short, user-facing bullet strings. Keep each line ~80 chars or shorter so the
31
31
  * TUI dialog renders cleanly without horizontal scroll on a typical terminal.
32
32
  */
33
33
  export const ANNOUNCEMENT_FEATURES: ReadonlyArray<string> = [
34
- "Setup and doctor now recognize an OpenCode Desktop install that has no CLI on PATH, instead of reporting OpenCode as not installed (#196). Desktop-only users continue setup with manual model entry.",
35
- "TUI sidebar badge color follows your theme again (#198): it uses the theme background like sibling badges, with a fallback so the label can't disappear on transparent themes.",
36
- "doctor clears a stale plugin cache more reliably (#199): it compares against the plugin's latest published version and preserves the cache when offline.",
34
+ "Fixed high idle CPU from the TUI sidebar (#200): it now uses a single persistent connection to the plugin instead of polling, so an idle session no longer burns CPU.",
37
35
  ];
38
36
 
39
37
  /**
@@ -97,6 +97,20 @@ export class MagicContextRpcClient {
97
97
  }
98
98
  }
99
99
 
100
+ /** Resolve the live server's port + bearer token (for opening the WS push
101
+ * channel). Reuses the same health-checked port-file discovery as `call`,
102
+ * so the WS client and the HTTP client always agree on which server instance
103
+ * (and token) to use. Returns null when no live server is found. */
104
+ async resolveEndpoint(): Promise<{ port: number; token: string | null } | null> {
105
+ try {
106
+ const port = await this.resolvePort();
107
+ if (port === null) return null;
108
+ return { port, token: this.token };
109
+ } catch {
110
+ return null;
111
+ }
112
+ }
113
+
100
114
  private async resolvePort(): Promise<number | null> {
101
115
  if (this.port && this.healthChecked) {
102
116
  return this.port;
@@ -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
  }