@cortexkit/opencode-magic-context 0.15.3 → 0.15.4

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 (37) hide show
  1. package/README.md +2 -2
  2. package/dist/features/magic-context/memory/embedding-openai.d.ts.map +1 -1
  3. package/dist/hooks/magic-context/compartment-runner-incremental.d.ts.map +1 -1
  4. package/dist/hooks/magic-context/compartment-runner-recomp.d.ts.map +1 -1
  5. package/dist/hooks/magic-context/compartment-runner-types.d.ts +13 -0
  6. package/dist/hooks/magic-context/compartment-runner-types.d.ts.map +1 -1
  7. package/dist/hooks/magic-context/hook-handlers.d.ts +71 -2
  8. package/dist/hooks/magic-context/hook-handlers.d.ts.map +1 -1
  9. package/dist/hooks/magic-context/hook.d.ts +3 -0
  10. package/dist/hooks/magic-context/hook.d.ts.map +1 -1
  11. package/dist/hooks/magic-context/live-session-state.d.ts +19 -0
  12. package/dist/hooks/magic-context/live-session-state.d.ts.map +1 -1
  13. package/dist/hooks/magic-context/system-prompt-hash.d.ts +17 -1
  14. package/dist/hooks/magic-context/system-prompt-hash.d.ts.map +1 -1
  15. package/dist/hooks/magic-context/tokenizer-calibration.d.ts +85 -0
  16. package/dist/hooks/magic-context/tokenizer-calibration.d.ts.map +1 -0
  17. package/dist/hooks/magic-context/transform-compartment-phase.d.ts +4 -0
  18. package/dist/hooks/magic-context/transform-compartment-phase.d.ts.map +1 -1
  19. package/dist/hooks/magic-context/transform-postprocess-phase.d.ts +8 -1
  20. package/dist/hooks/magic-context/transform-postprocess-phase.d.ts.map +1 -1
  21. package/dist/hooks/magic-context/transform.d.ts +17 -1
  22. package/dist/hooks/magic-context/transform.d.ts.map +1 -1
  23. package/dist/index.js +313 -40
  24. package/dist/plugin/hooks/create-session-hooks.d.ts.map +1 -1
  25. package/dist/plugin/rpc-handlers.d.ts.map +1 -1
  26. package/dist/plugin/sidebar-snapshot-cache.d.ts +19 -0
  27. package/dist/plugin/sidebar-snapshot-cache.d.ts.map +1 -0
  28. package/dist/shared/rpc-types.d.ts +0 -9
  29. package/dist/shared/rpc-types.d.ts.map +1 -1
  30. package/dist/tui/data/context-db.d.ts.map +1 -1
  31. package/package.json +1 -1
  32. package/src/shared/rpc-types.ts +0 -9
  33. package/src/tui/data/context-db.ts +66 -4
  34. package/src/tui/index.tsx +0 -3
  35. package/src/tui/slots/sidebar-content.tsx +0 -15
  36. package/dist/plugin/tui-action-consumer.d.ts +0 -15
  37. package/dist/plugin/tui-action-consumer.d.ts.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"context-db.d.ts","sourceRoot":"","sources":["../../../src/tui/data/context-db.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAA0B,eAAe,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAEpG,YAAY,EAAE,eAAe,EAAE,YAAY,EAAE,CAAC;AAS9C,2DAA2D;AAC3D,wBAAgB,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAGrD;AAED,+BAA+B;AAC/B,wBAAgB,QAAQ,IAAI,IAAI,CAG/B;AA4BD,sDAAsD;AACtD,wBAAsB,mBAAmB,CACrC,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,GAClB,OAAO,CAAC,eAAe,CAAC,CAc1B;AAED,wDAAwD;AACxD,wBAAsB,gBAAgB,CAClC,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,EACjB,QAAQ,CAAC,EAAE,MAAM,GAClB,OAAO,CAAC,YAAY,CAAC,CA4CvB;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,MAAM,WAAW,UAAU;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACjC,SAAS,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,yDAAyD;AACzD,wBAAsB,kBAAkB,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC,CAchE"}
1
+ {"version":3,"file":"context-db.d.ts","sourceRoot":"","sources":["../../../src/tui/data/context-db.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAA0B,eAAe,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAEpG,YAAY,EAAE,eAAe,EAAE,YAAY,EAAE,CAAC;AAS9C,2DAA2D;AAC3D,wBAAgB,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAGrD;AAED,+BAA+B;AAC/B,wBAAgB,QAAQ,IAAI,IAAI,CAG/B;AA4ED,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,CA4CvB;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,MAAM,WAAW,UAAU;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACjC,SAAS,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,yDAAyD;AACzD,wBAAsB,kBAAkB,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC,CAchE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/opencode-magic-context",
3
- "version": "0.15.3",
3
+ "version": "0.15.4",
4
4
  "type": "module",
5
5
  "description": "OpenCode plugin for Magic Context — cross-session memory and context management",
6
6
  "main": "dist/index.js",
@@ -44,15 +44,6 @@ export interface SidebarSnapshot {
44
44
  * shows this as "Tool Definitions".
45
45
  */
46
46
  toolDefinitionTokens: number;
47
- /**
48
- * Residual catch-all: provider-side wrapping not captured elsewhere —
49
- * the JSON envelope around the `tools` array, tool-choice fields,
50
- * provider-specific cache-control markers, tokenizer imprecision, etc.
51
- * Computed as `inputTokens − systemPromptTokens − messagesBlock −
52
- * toolCallTokens − toolDefinitionTokens` and clamped to ≥ 0. Display
53
- * layer shows this as "Overhead".
54
- */
55
- overheadTokens: number;
56
47
  }
57
48
 
58
49
  export interface StatusDetail extends SidebarSnapshot {
@@ -51,26 +51,88 @@ const EMPTY_SNAPSHOT: SidebarSnapshot = {
51
51
  conversationTokens: 0,
52
52
  toolCallTokens: 0,
53
53
  toolDefinitionTokens: 0,
54
- overheadTokens: 0,
55
54
  };
56
55
 
56
+ /**
57
+ * Per-session client-side sticky cache. Mirrors the server-side cache in
58
+ * `sidebar-snapshot-cache.ts` but covers the cases the server can't:
59
+ * - RPC call fails entirely (timeout, abort, parse error) → server is never reached
60
+ * - RPC server is not yet up (port file missing, retries exhausted)
61
+ * - Server returns an error envelope
62
+ *
63
+ * In all three cases the breakdown bar would otherwise disappear until the
64
+ * next successful refresh. With this cache, the client returns the most
65
+ * recent good snapshot for the same session so the UI stays stable through
66
+ * transient RPC blips. 5-minute staleness ceiling keeps it from showing
67
+ * obviously old data after long disconnects.
68
+ */
69
+ interface CachedSnapshot {
70
+ snapshot: SidebarSnapshot;
71
+ cachedAt: number;
72
+ }
73
+ const STICKY_TTL_MS = 5 * 60 * 1000;
74
+ const STICKY_MAX_ENTRIES = 100;
75
+ const stickySidebarCache = new Map<string, CachedSnapshot>();
76
+
77
+ function rememberSidebarSnapshot(snapshot: SidebarSnapshot): void {
78
+ if (!snapshot.sessionId || snapshot.inputTokens <= 0) return;
79
+ // LRU-style bound: drop the oldest entry once we hit the cap. With a
80
+ // 5-min TTL most stale entries time out naturally; this just prevents
81
+ // unbounded growth across many session switches in a long TUI session.
82
+ if (
83
+ stickySidebarCache.size >= STICKY_MAX_ENTRIES &&
84
+ !stickySidebarCache.has(snapshot.sessionId)
85
+ ) {
86
+ const firstKey = stickySidebarCache.keys().next().value;
87
+ if (firstKey) stickySidebarCache.delete(firstKey);
88
+ }
89
+ stickySidebarCache.set(snapshot.sessionId, {
90
+ snapshot,
91
+ cachedAt: Date.now(),
92
+ });
93
+ }
94
+
95
+ function recallSidebarSnapshot(sessionId: string, fallback: SidebarSnapshot): SidebarSnapshot {
96
+ const cached = stickySidebarCache.get(sessionId);
97
+ if (!cached) return fallback;
98
+ if (Date.now() - cached.cachedAt > STICKY_TTL_MS) {
99
+ stickySidebarCache.delete(sessionId);
100
+ return fallback;
101
+ }
102
+ return cached.snapshot;
103
+ }
104
+
57
105
  /** Fetch sidebar snapshot from the server via RPC. */
58
106
  export async function loadSidebarSnapshot(
59
107
  sessionId: string,
60
108
  directory: string,
61
109
  ): Promise<SidebarSnapshot> {
62
- if (!rpcClient) return { ...EMPTY_SNAPSHOT, sessionId };
110
+ const empty: SidebarSnapshot = { ...EMPTY_SNAPSHOT, sessionId };
111
+ if (!rpcClient) return recallSidebarSnapshot(sessionId, empty);
63
112
  try {
64
113
  const result = await rpcClient.call<SidebarSnapshot>("sidebar-snapshot", {
65
114
  sessionId,
66
115
  directory,
67
116
  });
68
117
  if ((result as unknown as Record<string, unknown>).error) {
69
- return { ...EMPTY_SNAPSHOT, sessionId };
118
+ return recallSidebarSnapshot(sessionId, empty);
70
119
  }
120
+ // Trust successful server responses. The server has its own sticky
121
+ // sidebar cache (`sidebar-snapshot-cache.ts`) that handles transient
122
+ // zero-token windows by hybriding cached breakdown values into a
123
+ // fresh snapshot, AND clears that cache on `session.deleted`. If the
124
+ // server reaches us with `inputTokens === 0`, that's its considered
125
+ // answer — typically because the session was deleted, reverted, or
126
+ // is brand-new with no responses yet.
127
+ //
128
+ // Falling back to the client cache here would resurrect old token
129
+ // data for a deleted session (the client never sees `session.deleted`
130
+ // events, so its cache TTL is the only expiry). Sticky behavior is
131
+ // owned exclusively by the server side.
132
+ rememberSidebarSnapshot(result);
71
133
  return result;
72
134
  } catch {
73
- return { ...EMPTY_SNAPSHOT, sessionId };
135
+ return recallSidebarSnapshot(sessionId, empty);
74
136
  }
75
137
  }
76
138
 
package/src/tui/index.tsx CHANGED
@@ -219,7 +219,6 @@ const StatusDialog = (props: { api: TuiPluginApi; s: StatusDetail }) => {
219
219
  conversation: "#f87171",
220
220
  toolCalls: "#fb923c",
221
221
  toolDefs: "#f472b6",
222
- overhead: "#9ca3af",
223
222
  }
224
223
 
225
224
  const breakdownSegments = () => {
@@ -257,8 +256,6 @@ const StatusDialog = (props: { api: TuiPluginApi; s: StatusDetail }) => {
257
256
  segs.push({ label: "Tool Calls", tokens: d.toolCallTokens, color: COLORS.toolCalls })
258
257
  if (d.toolDefinitionTokens > 0)
259
258
  segs.push({ label: "Tool Defs", tokens: d.toolDefinitionTokens, color: COLORS.toolDefs })
260
- if (d.overheadTokens > 0)
261
- segs.push({ label: "Overhead", tokens: d.overheadTokens, color: COLORS.overhead })
262
259
 
263
260
  return { segs, total }
264
261
  }
@@ -33,7 +33,6 @@ const COLORS = {
33
33
  conversation: "#f87171", // Red
34
34
  toolCalls: "#fb923c", // Orange
35
35
  toolDefs: "#f472b6", // Pink
36
- overhead: "#9ca3af", // Gray — catch-all residual
37
36
  }
38
37
 
39
38
  interface TokenSegment {
@@ -130,20 +129,6 @@ const TokenBreakdown = (props: {
130
129
  })
131
130
  }
132
131
 
133
- // Overhead = residual between input tokens and everything measured above.
134
- // Captures provider-side JSON wrapping around the tools array,
135
- // tool_choice/cache-control markers, and tokenizer imprecision. Before
136
- // the first turn's tool.definition measurement lands, the real tool
137
- // schema cost also shows up here.
138
- if (s.overheadTokens > 0) {
139
- result.push({
140
- key: "overhead",
141
- tokens: s.overheadTokens,
142
- color: COLORS.overhead,
143
- label: "Overhead",
144
- })
145
- }
146
-
147
132
  return result
148
133
  })
149
134
 
@@ -1,15 +0,0 @@
1
- import type { MagicContextConfig } from "../config/schema/magic-context";
2
- import type { LiveSessionState } from "../hooks/magic-context/live-session-state";
3
- import type { PluginContext } from "./types";
4
- /**
5
- * Start a server-side consumer that polls plugin_messages for TUI→server
6
- * action messages and dispatches them. Currently handles:
7
- * - { command: "recomp" } — executes /ctx-recomp for the given session
8
- */
9
- export declare function startTuiActionConsumer(args: {
10
- client: PluginContext["client"];
11
- directory: string;
12
- config: MagicContextConfig;
13
- liveSessionState: LiveSessionState;
14
- }): (() => void) | undefined;
15
- //# sourceMappingURL=tui-action-consumer.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"tui-action-consumer.d.ts","sourceRoot":"","sources":["../../src/plugin/tui-action-consumer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AASzE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAGlF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAO7C;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE;IACzC,MAAM,EAAE,aAAa,CAAC,QAAQ,CAAC,CAAC;IAChC,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,kBAAkB,CAAC;IAC3B,gBAAgB,EAAE,gBAAgB,CAAC;CACtC,GAAG,CAAC,MAAM,IAAI,CAAC,GAAG,SAAS,CAiF3B"}