@cortexkit/opencode-magic-context 0.49.8 → 0.49.10

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 (105) hide show
  1. package/dist/agents/hidden-agent-registrations.d.ts +5 -3
  2. package/dist/agents/hidden-agent-registrations.d.ts.map +1 -1
  3. package/dist/auto-search-worker.js +1 -1
  4. package/dist/config/schema/agent-overrides.d.ts +13 -13
  5. package/dist/config/schema/magic-context.d.ts +500 -500
  6. package/dist/{context-x0ccfv0f.js → context-ey3ap3cc.js} +29 -24
  7. package/dist/features/magic-context/primer-clustering.d.ts.map +1 -1
  8. package/dist/features/magic-context/smart-notes/compiler-prompt.d.ts +1 -1
  9. package/dist/features/magic-context/smart-notes/compiler-prompt.d.ts.map +1 -1
  10. package/dist/features/magic-context/smart-notes/compiler.d.ts.map +1 -1
  11. package/dist/hooks/magic-context/__fixtures__/tail-hygiene-refresh.d.ts +34 -0
  12. package/dist/hooks/magic-context/__fixtures__/tail-hygiene-refresh.d.ts.map +1 -0
  13. package/dist/hooks/magic-context/compartment-runner-historian.d.ts.map +1 -1
  14. package/dist/hooks/magic-context/ctx-reduce-nudge.d.ts +1 -0
  15. package/dist/hooks/magic-context/ctx-reduce-nudge.d.ts.map +1 -1
  16. package/dist/hooks/magic-context/dropped-input-guard.d.ts.map +1 -1
  17. package/dist/hooks/magic-context/final-wire-token-estimate.d.ts +2 -0
  18. package/dist/hooks/magic-context/final-wire-token-estimate.d.ts.map +1 -1
  19. package/dist/hooks/magic-context/hook.d.ts.map +1 -1
  20. package/dist/hooks/magic-context/provider-visible-parts.d.ts +10 -4
  21. package/dist/hooks/magic-context/provider-visible-parts.d.ts.map +1 -1
  22. package/dist/hooks/magic-context/supersession-reclaim.d.ts +5 -3
  23. package/dist/hooks/magic-context/supersession-reclaim.d.ts.map +1 -1
  24. package/dist/hooks/magic-context/tail-hygiene-output-count.d.ts +18 -0
  25. package/dist/hooks/magic-context/tail-hygiene-output-count.d.ts.map +1 -0
  26. package/dist/hooks/magic-context/tail-hygiene-walk.d.ts +5 -1
  27. package/dist/hooks/magic-context/tail-hygiene-walk.d.ts.map +1 -1
  28. package/dist/hooks/magic-context/tool-reclaim.d.ts +5 -2
  29. package/dist/hooks/magic-context/tool-reclaim.d.ts.map +1 -1
  30. package/dist/hooks/magic-context/transform-postprocess-phase.d.ts +1 -0
  31. package/dist/hooks/magic-context/transform-postprocess-phase.d.ts.map +1 -1
  32. package/dist/hooks/magic-context/transform.d.ts.map +1 -1
  33. package/dist/{index-b4gcb3rk.js → index-0x9cn9w1.js} +2 -2
  34. package/dist/{index-ydjgz36f.js → index-16vwdyy5.js} +1 -1
  35. package/dist/{index-xf6k8szp.js → index-21919gcq.js} +3 -3
  36. package/dist/{index-hxrvx6cy.js → index-3403gbzj.js} +129 -103
  37. package/dist/{index-gzg9ekc1.js → index-6k86es2p.js} +129 -15
  38. package/dist/{index-8kk7yh6m.js → index-9jvab89d.js} +1 -1
  39. package/dist/{index-qgp5vnt4.js → index-a865mj02.js} +1 -1
  40. package/dist/{index-xpn05jxq.js → index-f8cn4xqa.js} +287 -58
  41. package/dist/{index-2rw244rh.js → index-ffa8x2as.js} +4 -4
  42. package/dist/{index-bd9cjab3.js → index-g7jkrz0x.js} +35 -15
  43. package/dist/{index-pjgzdezw.js → index-nenz4gpb.js} +3 -3
  44. package/dist/{index-zf9nydbd.js → index-swgpe402.js} +4 -4
  45. package/dist/{index-9g1c4bt9.js → index-wpwpn7nj.js} +88 -8
  46. package/dist/{index-w1ejb9qr.js → index-x57ep4fp.js} +33 -19
  47. package/dist/index.d.ts.map +1 -1
  48. package/dist/index.js +50 -46
  49. package/dist/{message-index-zs882h8e.js → message-index-6r1x9cqh.js} +5 -5
  50. package/dist/{message-index-bounded-hxjp56qk.js → message-index-bounded-fskdyc81.js} +5 -5
  51. package/dist/migration-worker.js +4 -4
  52. package/dist/plugin/hooks/create-session-hooks.d.ts.map +1 -1
  53. package/dist/plugin/rpc-handlers.d.ts.map +1 -1
  54. package/dist/raw-ordinal-warmup-worker.js +3 -3
  55. package/dist/{read-session-chunk-t9r7r0sg.js → read-session-chunk-zgpvs423.js} +4 -4
  56. package/dist/{recomp-orchestrator-gr36zvd2.js → recomp-orchestrator-77xc8wpe.js} +10 -10
  57. package/dist/{search-qjen1pmz.js → search-72fkjwk2.js} +8 -8
  58. package/dist/shared/hidden-zen-read.d.ts +24 -0
  59. package/dist/shared/hidden-zen-read.d.ts.map +1 -0
  60. package/dist/shared/reclaim-distance.d.ts +33 -0
  61. package/dist/shared/reclaim-distance.d.ts.map +1 -0
  62. package/dist/shared/stable-json.d.ts +7 -0
  63. package/dist/shared/stable-json.d.ts.map +1 -1
  64. package/dist/shared/tail-hygiene-json.d.ts +22 -0
  65. package/dist/shared/tail-hygiene-json.d.ts.map +1 -0
  66. package/dist/shared/transcript.d.ts +5 -0
  67. package/dist/shared/transcript.d.ts.map +1 -1
  68. package/dist/{storage-tags-a513rkj3.js → storage-tags-35m4gms2.js} +2 -2
  69. package/dist/{transform-postprocess-phase-yv463b7y.js → transform-postprocess-phase-r8d406j0.js} +10 -10
  70. package/dist/v2/auto-search-worker.js +1 -1
  71. package/dist/v2/{context-wd82v9z6.js → context-z2yagwy4.js} +168 -35
  72. package/dist/v2/hidden-completion.d.ts.map +1 -1
  73. package/dist/v2/hooks/hidden-child.d.ts +2 -2
  74. package/dist/v2/hooks/hidden-child.d.ts.map +1 -1
  75. package/dist/v2/{message-index-yd94vt9n.js → message-index-9ezq6wze.js} +5 -5
  76. package/dist/v2/{message-index-bounded-madg2kfr.js → message-index-bounded-rqc603f2.js} +5 -5
  77. package/dist/v2/migration-worker.js +4 -4
  78. package/dist/v2/raw-ordinal-warmup-worker.js +3 -3
  79. package/dist/v2/{read-session-chunk-7qs791f3.js → read-session-chunk-7f87tg2q.js} +4 -4
  80. package/dist/v2/{recomp-orchestrator-ee91r9s6.js → recomp-orchestrator-s5tdpsbs.js} +9 -9
  81. package/dist/v2/{search-pn15rr52.js → search-2td1b1wy.js} +8 -8
  82. package/dist/v2/{server-08k4zkfk.js → server-23znwjqk.js} +4 -4
  83. package/dist/v2/{server-y5g32dhq.js → server-2hxypdx5.js} +4 -4
  84. package/dist/v2/{server-yak3sxya.js → server-4zwx20yg.js} +1 -1
  85. package/dist/v2/{server-shme41gj.js → server-89fcbe37.js} +35 -15
  86. package/dist/v2/{server-q360kdb8.js → server-c9w52qss.js} +30 -13
  87. package/dist/v2/{server-4gz0xkrc.js → server-f27he3j1.js} +1 -1
  88. package/dist/v2/{server-xj4mewze.js → server-gc8x2xdb.js} +66 -8
  89. package/dist/v2/{server-jjsf29aq.js → server-kmapq3v0.js} +286 -57
  90. package/dist/v2/{server-adfte03x.js → server-xqf2d2mg.js} +1 -1
  91. package/dist/v2/{server-j3dm74ey.js → server-z0rjemg1.js} +2 -2
  92. package/dist/v2/server.js +2 -2
  93. package/dist/v2/{storage-tags-spa92sgj.js → storage-tags-c4zfcxv4.js} +2 -2
  94. package/dist/v2/{wrapup-orchestrator-5wxy1ggv.js → wrapup-orchestrator-wg8dmmm9.js} +9 -9
  95. package/dist/{wrapup-orchestrator-gv08c6f9.js → wrapup-orchestrator-b1z99yg5.js} +11 -11
  96. package/package.json +1 -1
  97. package/src/agents/hidden-agent-registrations.ts +22 -4
  98. package/src/shared/hidden-zen-read.test.ts +99 -0
  99. package/src/shared/hidden-zen-read.ts +88 -0
  100. package/src/shared/reclaim-distance.test.ts +111 -0
  101. package/src/shared/reclaim-distance.ts +91 -0
  102. package/src/shared/stable-json.ts +56 -14
  103. package/src/shared/tail-hygiene-json.test.ts +117 -0
  104. package/src/shared/tail-hygiene-json.ts +217 -0
  105. package/src/shared/transcript.ts +7 -0
@@ -0,0 +1,117 @@
1
+ import { expect, test } from "bun:test";
2
+ import { jsonHashSink, stableStringify, stableStringifyHash } from "./stable-json";
3
+ import { piJsonHash, TailHygieneJsonMemo } from "./tail-hygiene-json";
4
+
5
+ function hash(text: string): string {
6
+ const sink = jsonHashSink("excluded\0");
7
+ sink.write(text);
8
+ return sink.digest();
9
+ }
10
+
11
+ function piStringify(value: unknown): string {
12
+ const seen = new Set<object>();
13
+ try {
14
+ return (
15
+ JSON.stringify(value, (_key, current) => {
16
+ if (!current || typeof current !== "object" || Array.isArray(current))
17
+ return current;
18
+ if (seen.has(current)) return "[Circular]";
19
+ seen.add(current);
20
+ return Object.fromEntries(
21
+ Object.entries(current).sort(([a], [b]) => a.localeCompare(b)),
22
+ );
23
+ }) ?? ""
24
+ );
25
+ } catch {
26
+ return "";
27
+ }
28
+ }
29
+
30
+ test("streamed excluded fingerprints equal both original serializers on randomized values", () => {
31
+ let state = 704;
32
+ const random = (n: number): number => {
33
+ state = (Math.imul(state, 1664525) + 1013904223) | 0;
34
+ return (state >>> 0) % n;
35
+ };
36
+ const leaves = [
37
+ undefined,
38
+ null,
39
+ -0,
40
+ NaN,
41
+ Infinity,
42
+ 1e-7,
43
+ 1e23,
44
+ false,
45
+ "\ud800",
46
+ "é😀\n\u2028",
47
+ '"\\',
48
+ ];
49
+ const generate = (depth: number): unknown => {
50
+ if (!depth || random(3) === 0) return leaves[random(leaves.length)];
51
+ if (random(2)) {
52
+ const array = new Array(4);
53
+ array[0] = generate(depth - 1);
54
+ array[2] = undefined;
55
+ array[3] = generate(depth - 1);
56
+ return array;
57
+ }
58
+ const value: Record<string, unknown> = {};
59
+ const keys = ["10", "2", "Z", "ä", "a", "e\u0301", "é"];
60
+ for (let i = 0; i < 4; i++) value[keys[random(keys.length)]] = generate(depth - 1);
61
+ if (random(5) === 0) value.self = value;
62
+ return value;
63
+ };
64
+ const shared = { a: undefined, z: "shared" };
65
+ const cycle: unknown[] = [];
66
+ cycle.push(cycle);
67
+ const samples: unknown[] = [
68
+ cycle,
69
+ { a: shared, b: shared },
70
+ 1n,
71
+ new Date(0),
72
+ { toJSON: () => "date" },
73
+ ];
74
+ for (let i = 0; i < 2_000; i++) samples.push(generate(4));
75
+ for (const value of samples) {
76
+ try {
77
+ const expected = stableStringify(value);
78
+ expect(stableStringifyHash(value, "excluded\0")).toBe(hash(expected));
79
+ } catch {
80
+ expect(() => stableStringifyHash(value, "excluded\0")).toThrow();
81
+ }
82
+ expect(piJsonHash(value, () => piStringify(value))).toBe(hash(piStringify(value)));
83
+ }
84
+ console.log(`excluded fingerprint differential: ${samples.length} values per serializer`);
85
+ });
86
+
87
+ test("part memo catches in-place edits, alias changes, key order and accessor values", () => {
88
+ const memo = new TailHygieneJsonMemo();
89
+ let calls = 0;
90
+ const get = (value: unknown): string => {
91
+ memo.beginPass();
92
+ return memo.get("part", value, () => {
93
+ calls++;
94
+ return stableStringify(value);
95
+ });
96
+ };
97
+ const shared = { value: "first" };
98
+ const value = { a: shared, b: shared };
99
+ expect(get(value)).toBe(get(structuredClone(value)));
100
+ expect(calls).toBe(1);
101
+ shared.value = "other";
102
+ expect(get(value)).toContain("other");
103
+ expect(calls).toBe(2);
104
+ value.b = { value: "other" };
105
+ expect(get(value)).not.toContain("Circular");
106
+ expect(calls).toBe(3);
107
+ expect(get({ b: value.b, a: value.a })).toBe(get(value));
108
+ expect(calls).toBe(5);
109
+ let changing = 0;
110
+ const accessor = {
111
+ get value() {
112
+ return ++changing;
113
+ },
114
+ };
115
+ expect(get(accessor)).not.toBe(get(accessor));
116
+ expect(calls).toBe(7);
117
+ });
@@ -0,0 +1,217 @@
1
+ import { jsonHashSink } from "./stable-json";
2
+
3
+ const MAX_MEMO_BYTES = 64 * 1024 * 1024;
4
+ const MAX_MEMO_ENTRIES = 10_000;
5
+
6
+ /**
7
+ * A bounded, process-local memo for individual parts and selected tool fields.
8
+ * Detached snapshots catch in-place host edits; reference equality alone cannot.
9
+ * Key collisions between sessions only evict entries, never imply equality.
10
+ */
11
+ export class TailHygieneJsonMemo {
12
+ private entries = new Map<string, { snapshot: unknown; result: string; bytes: number }>();
13
+ private bytes = 0;
14
+ private passText = new WeakMap<object, string>();
15
+ private passHash = new WeakMap<object, string>();
16
+
17
+ beginPass(): void {
18
+ this.passText = new WeakMap();
19
+ this.passHash = new WeakMap();
20
+ }
21
+
22
+ get(key: string, value: unknown, compute: () => string, fingerprint = false): string {
23
+ const pass = fingerprint ? this.passHash : this.passText;
24
+ const object = value !== null && typeof value === "object" ? value : undefined;
25
+ const local = object ? pass.get(object) : undefined;
26
+ if (local !== undefined) return local;
27
+ const previous = this.entries.get(key);
28
+ if (previous && sameJson(previous.snapshot, value)) {
29
+ if (object) pass.set(object, previous.result);
30
+ return previous.result;
31
+ }
32
+ const result = compute();
33
+ if (previous) {
34
+ this.bytes -= previous.bytes;
35
+ this.entries.delete(key);
36
+ }
37
+ try {
38
+ const copy = copyJson(value);
39
+ const bytes = copy.bytes + (key.length + result.length) * 2;
40
+ if (bytes <= MAX_MEMO_BYTES) {
41
+ this.entries.set(key, { snapshot: copy.value, result, bytes });
42
+ this.bytes += bytes;
43
+ if (object) pass.set(object, result);
44
+ }
45
+ } catch {
46
+ // Accessors and exotic objects retain their original serialization behavior.
47
+ }
48
+ while (this.bytes > MAX_MEMO_BYTES || this.entries.size > MAX_MEMO_ENTRIES) {
49
+ const oldest = this.entries.entries().next().value;
50
+ if (!oldest) break;
51
+ this.bytes -= oldest[1].bytes;
52
+ this.entries.delete(oldest[0]);
53
+ }
54
+ return result;
55
+ }
56
+ }
57
+
58
+ function copyJson(value: unknown): { value: unknown; bytes: number } {
59
+ const seen = new Map<object, unknown>();
60
+ let bytes = 0;
61
+ const copy = (current: unknown): unknown => {
62
+ bytes += 16;
63
+ if (typeof current === "string") bytes += current.length * 2;
64
+ if (bytes > MAX_MEMO_BYTES) throw new Error("memo capacity");
65
+ if (current === null || typeof current !== "object") {
66
+ if (["function", "symbol", "bigint"].includes(typeof current))
67
+ throw new Error("exotic");
68
+ return current;
69
+ }
70
+ if (seen.has(current)) return seen.get(current);
71
+ const prototype = Object.getPrototypeOf(current);
72
+ if (!Array.isArray(current) && prototype !== Object.prototype && prototype !== null)
73
+ throw new Error("exotic");
74
+ const result = Array.isArray(current)
75
+ ? new Array(current.length)
76
+ : Object.create(prototype);
77
+ seen.set(current, result);
78
+ for (const key of Object.keys(current)) {
79
+ const descriptor = Object.getOwnPropertyDescriptor(current, key);
80
+ if (!descriptor || !("value" in descriptor)) throw new Error("accessor");
81
+ bytes += key.length * 2 + 16;
82
+ if (key === "__proto__") {
83
+ Object.defineProperty(result, key, {
84
+ value: copy(descriptor.value),
85
+ enumerable: true,
86
+ writable: true,
87
+ configurable: true,
88
+ });
89
+ } else result[key] = copy(descriptor.value);
90
+ }
91
+ return result;
92
+ };
93
+ return { value: copy(value), bytes };
94
+ }
95
+
96
+ function sameJson(before: unknown, after: unknown): boolean {
97
+ const forward = new Map<object, object>();
98
+ const reverse = new Set<object>();
99
+ const same = (left: unknown, right: unknown): boolean => {
100
+ if (
101
+ left === null ||
102
+ right === null ||
103
+ typeof left !== "object" ||
104
+ typeof right !== "object"
105
+ )
106
+ return Object.is(left, right);
107
+ if (forward.has(left)) return forward.get(left) === right;
108
+ if (reverse.has(right) || Object.getPrototypeOf(left) !== Object.getPrototypeOf(right))
109
+ return false;
110
+ forward.set(left, right);
111
+ reverse.add(right);
112
+ if (Array.isArray(left) && (!Array.isArray(right) || left.length !== right.length))
113
+ return false;
114
+ const keys = Object.keys(left);
115
+ const otherKeys = Object.keys(right);
116
+ if (keys.length !== otherKeys.length) return false;
117
+ for (let index = 0; index < keys.length; index++) {
118
+ const key = keys[index];
119
+ // Pi sorts with localeCompare, which can tie distinct Unicode spellings.
120
+ // Preserve insertion order too, because a tied sort keeps that order.
121
+ if (key !== otherKeys[index]) return false;
122
+ const descriptor = Object.getOwnPropertyDescriptor(right, key);
123
+ if (
124
+ !descriptor ||
125
+ !("value" in descriptor) ||
126
+ !same((left as Record<string, unknown>)[key], descriptor.value)
127
+ )
128
+ return false;
129
+ }
130
+ return true;
131
+ };
132
+ return same(before, after);
133
+ }
134
+
135
+ /**
136
+ * Fingerprint excluded Pi content without retaining its whole JSON string.
137
+ * Match Pi's native JSON replacer: localeCompare key order, omitted undefined
138
+ * object fields, null array holes, and a marker for repeated non-array objects.
139
+ * Unsupported objects and array cycles use the original serializer; serialization
140
+ * errors produce the fingerprint of an empty string, as they did before.
141
+ */
142
+ export function piJsonHash(value: unknown, fallback: () => string): string {
143
+ const sink = jsonHashSink("excluded\0");
144
+ try {
145
+ const seen = new Set<object>();
146
+ const arrays = new Set<object>();
147
+ const write = (current: unknown): void => {
148
+ if (current === null || typeof current !== "object") {
149
+ if (["function", "symbol", "bigint"].includes(typeof current))
150
+ throw new Error("exotic");
151
+ sink.write(JSON.stringify(current) ?? "");
152
+ } else if (Array.isArray(current)) {
153
+ if (
154
+ Object.getPrototypeOf(current) !== Array.prototype ||
155
+ Object.hasOwn(current, "toJSON")
156
+ )
157
+ throw new Error("exotic array");
158
+ if (arrays.has(current)) throw new Error("array cycle");
159
+ arrays.add(current);
160
+ sink.write("[");
161
+ for (let index = 0; index < current.length; index++) {
162
+ if (index) sink.write(",");
163
+ const descriptor = Object.getOwnPropertyDescriptor(current, index);
164
+ if (descriptor && !("value" in descriptor)) throw new Error("accessor");
165
+ if (!descriptor && index in current) throw new Error("inherited array element");
166
+ write(descriptor?.value === undefined ? null : descriptor.value);
167
+ }
168
+ sink.write("]");
169
+ arrays.delete(current);
170
+ } else if (seen.has(current)) {
171
+ sink.write('"[Circular]"');
172
+ } else {
173
+ const prototype = Object.getPrototypeOf(current);
174
+ if (prototype !== Object.prototype && prototype !== null) throw new Error("exotic");
175
+ seen.add(current);
176
+ const entries = Object.keys(current).map((key): [string, unknown] => {
177
+ const descriptor = Object.getOwnPropertyDescriptor(current, key);
178
+ if (!descriptor || !("value" in descriptor)) throw new Error("accessor");
179
+ return [key, descriptor.value];
180
+ });
181
+ const sorted = Object.fromEntries(entries.sort(([a], [b]) => a.localeCompare(b)));
182
+ if (
183
+ entries.length <= 16 &&
184
+ entries.every(
185
+ ([key, child]) =>
186
+ key.length <= 4096 &&
187
+ (child === null ||
188
+ child === undefined ||
189
+ typeof child === "number" ||
190
+ typeof child === "boolean" ||
191
+ (typeof child === "string" && child.length <= 4096)),
192
+ )
193
+ ) {
194
+ sink.write(JSON.stringify(sorted));
195
+ return;
196
+ }
197
+ sink.write("{");
198
+ let first = true;
199
+ for (const [key, child] of Object.entries(sorted)) {
200
+ if (child === undefined) continue;
201
+ if (!first) sink.write(",");
202
+ first = false;
203
+ sink.write(JSON.stringify(key));
204
+ sink.write(":");
205
+ write(child);
206
+ }
207
+ sink.write("}");
208
+ }
209
+ };
210
+ write(value);
211
+ return sink.digest();
212
+ } catch {
213
+ const failed = jsonHashSink("excluded\0");
214
+ failed.write(fallback());
215
+ return failed.digest();
216
+ }
217
+ }
@@ -78,6 +78,13 @@ export interface TranscriptPart {
78
78
  /** Discriminator for filter logic. Stable across mutations. */
79
79
  readonly kind: TranscriptPartKind;
80
80
 
81
+ /** Host provenance, retained so control notices cannot masquerade as user turns. */
82
+ readonly synthetic?: boolean;
83
+ readonly ignored?: boolean;
84
+
85
+ /** Visual token mass using the host's normal request estimator. */
86
+ imageTokens?(): number;
87
+
81
88
  /** True when the host marks this tool result as carrying a user's answer. */
82
89
  hasUserAnswer?(): boolean;
83
90