@cortexkit/opencode-magic-context 0.33.0 → 0.33.1
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.
- package/dist/config/schema/magic-context.d.ts +62 -22
- package/dist/config/schema/magic-context.d.ts.map +1 -1
- package/dist/{conflict-warning-hook-bwa99a04.js → conflict-warning-hook-n48c783v.js} +1 -1
- package/dist/features/magic-context/compaction-marker.d.ts +49 -0
- package/dist/features/magic-context/compaction-marker.d.ts.map +1 -1
- package/dist/features/magic-context/compartment-chunk-embedding.d.ts +0 -2
- package/dist/features/magic-context/compartment-chunk-embedding.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/task-registry.d.ts +0 -1
- package/dist/features/magic-context/dreamer/task-registry.d.ts.map +1 -1
- package/dist/features/magic-context/git-commits/index.d.ts +2 -2
- package/dist/features/magic-context/git-commits/index.d.ts.map +1 -1
- package/dist/features/magic-context/git-commits/storage-git-commit-embeddings.d.ts +0 -2
- package/dist/features/magic-context/git-commits/storage-git-commit-embeddings.d.ts.map +1 -1
- package/dist/features/magic-context/git-commits/storage-git-commits.d.ts +0 -13
- package/dist/features/magic-context/git-commits/storage-git-commits.d.ts.map +1 -1
- package/dist/features/magic-context/memory/embedding.d.ts +1 -2
- package/dist/features/magic-context/memory/embedding.d.ts.map +1 -1
- package/dist/features/magic-context/memory/memory-migration.d.ts +0 -6
- package/dist/features/magic-context/memory/memory-migration.d.ts.map +1 -1
- package/dist/features/magic-context/memory/storage-memory-embeddings.d.ts +0 -1
- package/dist/features/magic-context/memory/storage-memory-embeddings.d.ts.map +1 -1
- package/dist/features/magic-context/memory/storage-memory.d.ts +1 -6
- package/dist/features/magic-context/memory/storage-memory.d.ts.map +1 -1
- package/dist/features/magic-context/memory/verification-paths.d.ts +0 -1
- package/dist/features/magic-context/memory/verification-paths.d.ts.map +1 -1
- package/dist/features/magic-context/migrations.d.ts.map +1 -1
- package/dist/features/magic-context/mural/storage-mural.d.ts +0 -1
- package/dist/features/magic-context/mural/storage-mural.d.ts.map +1 -1
- package/dist/features/magic-context/project-embedding-registry.d.ts +16 -2
- package/dist/features/magic-context/project-embedding-registry.d.ts.map +1 -1
- package/dist/features/magic-context/smart-notes/capabilities.d.ts +0 -2
- package/dist/features/magic-context/smart-notes/capabilities.d.ts.map +1 -1
- package/dist/features/magic-context/smart-notes/compiler.d.ts +0 -1
- package/dist/features/magic-context/smart-notes/compiler.d.ts.map +1 -1
- package/dist/features/magic-context/smart-notes/types.d.ts +0 -16
- package/dist/features/magic-context/smart-notes/types.d.ts.map +1 -1
- package/dist/features/magic-context/storage-db.d.ts +1 -1
- package/dist/features/magic-context/storage-db.d.ts.map +1 -1
- package/dist/features/magic-context/storage-meta-persisted.d.ts +0 -1
- package/dist/features/magic-context/storage-meta-persisted.d.ts.map +1 -1
- package/dist/features/magic-context/user-memory/storage-user-memory.d.ts +0 -2
- package/dist/features/magic-context/user-memory/storage-user-memory.d.ts.map +1 -1
- package/dist/hooks/auto-update-checker/types.d.ts +0 -3
- package/dist/hooks/auto-update-checker/types.d.ts.map +1 -1
- package/dist/hooks/magic-context/cache-busting-signals.d.ts +0 -1
- package/dist/hooks/magic-context/cache-busting-signals.d.ts.map +1 -1
- package/dist/hooks/magic-context/channel2-delivery.d.ts.map +1 -1
- package/dist/hooks/magic-context/compaction-marker-manager.d.ts +49 -0
- package/dist/hooks/magic-context/compaction-marker-manager.d.ts.map +1 -1
- package/dist/hooks/magic-context/compartment-parser.d.ts +12 -0
- package/dist/hooks/magic-context/compartment-parser.d.ts.map +1 -1
- package/dist/hooks/magic-context/compartment-runner-mapping.d.ts +0 -12
- package/dist/hooks/magic-context/compartment-runner-mapping.d.ts.map +1 -1
- package/dist/hooks/magic-context/event-handler.d.ts.map +1 -1
- package/dist/hooks/magic-context/historian-prompt.generated.d.ts +1 -1
- package/dist/hooks/magic-context/historian-prompt.generated.d.ts.map +1 -1
- package/dist/hooks/magic-context/hook-handlers.d.ts.map +1 -1
- package/dist/hooks/magic-context/hook.d.ts.map +1 -1
- package/dist/hooks/magic-context/inject-compartments.d.ts +9 -0
- package/dist/hooks/magic-context/inject-compartments.d.ts.map +1 -1
- package/dist/hooks/magic-context/sentinel.d.ts +39 -0
- package/dist/hooks/magic-context/sentinel.d.ts.map +1 -1
- package/dist/hooks/magic-context/tag-messages.d.ts.map +1 -1
- package/dist/hooks/magic-context/tool-drop-target.d.ts +19 -0
- package/dist/hooks/magic-context/tool-drop-target.d.ts.map +1 -1
- package/dist/hooks/magic-context/transform.d.ts.map +1 -1
- package/dist/{index-kxz4tn6m.js → index-56yjaeb2.js} +56 -4
- package/dist/{index-h86twgsp.js → index-c2gahrks.js} +516 -177
- package/dist/{index-bmkea2dq.js → index-ey1bc14z.js} +36 -26
- package/dist/{index-k5y60ymm.js → index-xyaekybw.js} +21 -2
- package/dist/index.js +83 -23
- package/dist/plugin/hooks/create-session-hooks.d.ts +8 -8
- package/dist/plugin/hooks/create-session-hooks.d.ts.map +1 -1
- package/dist/{recomp-orchestrator-yh7pepvx.js → recomp-orchestrator-2yb2hfh2.js} +2 -3
- package/dist/shared/conflict-detector.d.ts.map +1 -1
- package/dist/shared/sqlite.d.ts +9 -4
- package/dist/shared/sqlite.d.ts.map +1 -1
- package/dist/{storage-meta-session-1phy0b1z.js → storage-meta-session-td6bwtky.js} +1 -1
- package/dist/{task-config-dn9k9szr.js → task-config-5nx3qfz8.js} +1 -2
- package/package.json +2 -2
- package/src/shared/conflict-detector.test.ts +130 -1
- package/src/shared/conflict-detector.ts +34 -2
- package/src/shared/sqlite.ts +13 -33
- package/dist/config/schema.d.ts +0 -3
- package/dist/config/schema.d.ts.map +0 -1
- package/dist/features/magic-context/plugin-messages.d.ts +0 -75
- package/dist/features/magic-context/plugin-messages.d.ts.map +0 -1
- package/dist/hooks/magic-context/compartment-runner-state-xml.d.ts +0 -21
- package/dist/hooks/magic-context/compartment-runner-state-xml.d.ts.map +0 -1
- package/dist/index-anhrfygk.js +0 -57
- package/dist/shared/opencode-compaction-detector.d.ts +0 -2
- package/dist/shared/opencode-compaction-detector.d.ts.map +0 -1
- package/dist/shared/transcript-opencode.d.ts +0 -71
- package/dist/shared/transcript-opencode.d.ts.map +0 -1
- package/dist/tools/index.d.ts +0 -6
- package/dist/tools/index.d.ts.map +0 -1
- package/src/shared/opencode-compaction-detector.test.ts +0 -239
- package/src/shared/opencode-compaction-detector.ts +0 -81
- package/src/shared/transcript-opencode.ts +0 -302
|
@@ -1,239 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it, spyOn } from "bun:test";
|
|
2
|
-
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
-
import { join } from "node:path";
|
|
4
|
-
import { isOpenCodeAutoCompactionEnabled } from "./opencode-compaction-detector";
|
|
5
|
-
import * as configDir from "./opencode-config-dir";
|
|
6
|
-
|
|
7
|
-
describe("opencode-compaction-detector", () => {
|
|
8
|
-
let tmpDir: string;
|
|
9
|
-
let previousDisableAutocompact: string | undefined;
|
|
10
|
-
let restoreConfigPaths: () => void;
|
|
11
|
-
|
|
12
|
-
beforeEach(() => {
|
|
13
|
-
tmpDir = join("/tmp", `compaction-detector-test-${Date.now()}`);
|
|
14
|
-
mkdirSync(join(tmpDir, ".opencode"), { recursive: true });
|
|
15
|
-
previousDisableAutocompact = process.env.OPENCODE_DISABLE_AUTOCOMPACT;
|
|
16
|
-
delete process.env.OPENCODE_DISABLE_AUTOCOMPACT;
|
|
17
|
-
const configPathsSpy = spyOn(configDir, "getOpenCodeConfigPaths").mockReturnValue({
|
|
18
|
-
configJson: join(tmpDir, "user-config", "opencode.json"),
|
|
19
|
-
configJsonc: join(tmpDir, "user-config", "opencode.jsonc"),
|
|
20
|
-
} as ReturnType<typeof configDir.getOpenCodeConfigPaths>);
|
|
21
|
-
restoreConfigPaths = () => configPathsSpy.mockRestore();
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
afterEach(() => {
|
|
25
|
-
restoreConfigPaths();
|
|
26
|
-
try {
|
|
27
|
-
rmSync(tmpDir, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 });
|
|
28
|
-
} catch {
|
|
29
|
-
/* Ignore EBUSY on Windows */
|
|
30
|
-
}
|
|
31
|
-
if (previousDisableAutocompact === undefined) {
|
|
32
|
-
delete process.env.OPENCODE_DISABLE_AUTOCOMPACT;
|
|
33
|
-
} else {
|
|
34
|
-
process.env.OPENCODE_DISABLE_AUTOCOMPACT = previousDisableAutocompact;
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
describe("#given no config exists", () => {
|
|
39
|
-
it("#then returns true (default: compaction enabled)", () => {
|
|
40
|
-
const emptyDir = join("/tmp", `compaction-empty-${Date.now()}`);
|
|
41
|
-
mkdirSync(emptyDir, { recursive: true });
|
|
42
|
-
|
|
43
|
-
const result = isOpenCodeAutoCompactionEnabled(emptyDir);
|
|
44
|
-
|
|
45
|
-
expect(result).toBe(true);
|
|
46
|
-
try {
|
|
47
|
-
rmSync(emptyDir, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 });
|
|
48
|
-
} catch {
|
|
49
|
-
/* Ignore EBUSY on Windows */
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
describe("#given OPENCODE_DISABLE_AUTOCOMPACT env flag is set", () => {
|
|
55
|
-
it("#then returns false", () => {
|
|
56
|
-
process.env.OPENCODE_DISABLE_AUTOCOMPACT = "1";
|
|
57
|
-
|
|
58
|
-
const result = isOpenCodeAutoCompactionEnabled(tmpDir);
|
|
59
|
-
|
|
60
|
-
expect(result).toBe(false);
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
describe("#given project config has compaction.auto = false", () => {
|
|
65
|
-
it("#when opencode.json #then returns false", () => {
|
|
66
|
-
writeFileSync(
|
|
67
|
-
join(tmpDir, ".opencode", "opencode.json"),
|
|
68
|
-
JSON.stringify({ compaction: { auto: false } }),
|
|
69
|
-
);
|
|
70
|
-
|
|
71
|
-
const result = isOpenCodeAutoCompactionEnabled(tmpDir);
|
|
72
|
-
|
|
73
|
-
expect(result).toBe(false);
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
it("#when opencode.jsonc #then returns false", () => {
|
|
77
|
-
writeFileSync(
|
|
78
|
-
join(tmpDir, ".opencode", "opencode.jsonc"),
|
|
79
|
-
`{
|
|
80
|
-
// compaction disabled
|
|
81
|
-
"compaction": { "auto": false }
|
|
82
|
-
}`,
|
|
83
|
-
);
|
|
84
|
-
|
|
85
|
-
const result = isOpenCodeAutoCompactionEnabled(tmpDir);
|
|
86
|
-
|
|
87
|
-
expect(result).toBe(false);
|
|
88
|
-
});
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
describe("#given project config has compaction.auto = true", () => {
|
|
92
|
-
it("#then returns true", () => {
|
|
93
|
-
writeFileSync(
|
|
94
|
-
join(tmpDir, ".opencode", "opencode.json"),
|
|
95
|
-
JSON.stringify({ compaction: { auto: true } }),
|
|
96
|
-
);
|
|
97
|
-
|
|
98
|
-
const result = isOpenCodeAutoCompactionEnabled(tmpDir);
|
|
99
|
-
|
|
100
|
-
expect(result).toBe(true);
|
|
101
|
-
});
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
describe("#given project config has compaction.prune = true", () => {
|
|
105
|
-
it("#then returns true (conflict enabled)", () => {
|
|
106
|
-
writeFileSync(
|
|
107
|
-
join(tmpDir, ".opencode", "opencode.json"),
|
|
108
|
-
JSON.stringify({ compaction: { auto: false, prune: true } }),
|
|
109
|
-
);
|
|
110
|
-
|
|
111
|
-
const result = isOpenCodeAutoCompactionEnabled(tmpDir);
|
|
112
|
-
|
|
113
|
-
expect(result).toBe(true);
|
|
114
|
-
});
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
describe("#given project config has auto/prune both false", () => {
|
|
118
|
-
it("#then returns false", () => {
|
|
119
|
-
writeFileSync(
|
|
120
|
-
join(tmpDir, ".opencode", "opencode.json"),
|
|
121
|
-
JSON.stringify({ compaction: { auto: false, prune: false } }),
|
|
122
|
-
);
|
|
123
|
-
|
|
124
|
-
const result = isOpenCodeAutoCompactionEnabled(tmpDir);
|
|
125
|
-
|
|
126
|
-
expect(result).toBe(false);
|
|
127
|
-
});
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
describe("#given project config has only compaction.prune = false", () => {
|
|
131
|
-
it("#then returns false", () => {
|
|
132
|
-
writeFileSync(
|
|
133
|
-
join(tmpDir, ".opencode", "opencode.json"),
|
|
134
|
-
JSON.stringify({ compaction: { prune: false } }),
|
|
135
|
-
);
|
|
136
|
-
|
|
137
|
-
const result = isOpenCodeAutoCompactionEnabled(tmpDir);
|
|
138
|
-
|
|
139
|
-
expect(result).toBe(false);
|
|
140
|
-
});
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
describe("#given jsonc and json both exist", () => {
|
|
144
|
-
it("#then jsonc takes precedence", () => {
|
|
145
|
-
writeFileSync(
|
|
146
|
-
join(tmpDir, ".opencode", "opencode.json"),
|
|
147
|
-
JSON.stringify({ compaction: { auto: true } }),
|
|
148
|
-
);
|
|
149
|
-
writeFileSync(
|
|
150
|
-
join(tmpDir, ".opencode", "opencode.jsonc"),
|
|
151
|
-
`{ "compaction": { "auto": false } }`,
|
|
152
|
-
);
|
|
153
|
-
|
|
154
|
-
const result = isOpenCodeAutoCompactionEnabled(tmpDir);
|
|
155
|
-
|
|
156
|
-
expect(result).toBe(false);
|
|
157
|
-
});
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
describe("#given config exists but no compaction field", () => {
|
|
161
|
-
it("#then returns true (default)", () => {
|
|
162
|
-
writeFileSync(
|
|
163
|
-
join(tmpDir, ".opencode", "opencode.json"),
|
|
164
|
-
JSON.stringify({ model: "claude-opus-4-6" }),
|
|
165
|
-
);
|
|
166
|
-
|
|
167
|
-
const result = isOpenCodeAutoCompactionEnabled(tmpDir);
|
|
168
|
-
|
|
169
|
-
expect(result).toBe(true);
|
|
170
|
-
});
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
describe("#given env flag overrides config", () => {
|
|
174
|
-
it("#then env flag wins even when config has auto: true", () => {
|
|
175
|
-
process.env.OPENCODE_DISABLE_AUTOCOMPACT = "true";
|
|
176
|
-
writeFileSync(
|
|
177
|
-
join(tmpDir, ".opencode", "opencode.json"),
|
|
178
|
-
JSON.stringify({ compaction: { auto: true } }),
|
|
179
|
-
);
|
|
180
|
-
|
|
181
|
-
const result = isOpenCodeAutoCompactionEnabled(tmpDir);
|
|
182
|
-
|
|
183
|
-
expect(result).toBe(false);
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
describe("#given root-level project config", () => {
|
|
188
|
-
it("#when root opencode.json has compaction.auto = false #then returns false", () => {
|
|
189
|
-
writeFileSync(
|
|
190
|
-
join(tmpDir, "opencode.json"),
|
|
191
|
-
JSON.stringify({ compaction: { auto: false } }),
|
|
192
|
-
);
|
|
193
|
-
|
|
194
|
-
const result = isOpenCodeAutoCompactionEnabled(tmpDir);
|
|
195
|
-
|
|
196
|
-
expect(result).toBe(false);
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
it("#when root opencode.jsonc has compaction.auto = false #then returns false", () => {
|
|
200
|
-
writeFileSync(join(tmpDir, "opencode.jsonc"), `{ "compaction": { "auto": false } }`);
|
|
201
|
-
|
|
202
|
-
const result = isOpenCodeAutoCompactionEnabled(tmpDir);
|
|
203
|
-
|
|
204
|
-
expect(result).toBe(false);
|
|
205
|
-
});
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
describe("#given .opencode/ overrides root-level config", () => {
|
|
209
|
-
it("#when root says false but .opencode says true #then .opencode wins", () => {
|
|
210
|
-
writeFileSync(
|
|
211
|
-
join(tmpDir, "opencode.json"),
|
|
212
|
-
JSON.stringify({ compaction: { auto: false } }),
|
|
213
|
-
);
|
|
214
|
-
writeFileSync(
|
|
215
|
-
join(tmpDir, ".opencode", "opencode.json"),
|
|
216
|
-
JSON.stringify({ compaction: { auto: true } }),
|
|
217
|
-
);
|
|
218
|
-
|
|
219
|
-
const result = isOpenCodeAutoCompactionEnabled(tmpDir);
|
|
220
|
-
|
|
221
|
-
expect(result).toBe(true);
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
it("#when root says true but .opencode says false #then .opencode wins", () => {
|
|
225
|
-
writeFileSync(
|
|
226
|
-
join(tmpDir, "opencode.json"),
|
|
227
|
-
JSON.stringify({ compaction: { auto: true } }),
|
|
228
|
-
);
|
|
229
|
-
writeFileSync(
|
|
230
|
-
join(tmpDir, ".opencode", "opencode.json"),
|
|
231
|
-
JSON.stringify({ compaction: { auto: false } }),
|
|
232
|
-
);
|
|
233
|
-
|
|
234
|
-
const result = isOpenCodeAutoCompactionEnabled(tmpDir);
|
|
235
|
-
|
|
236
|
-
expect(result).toBe(false);
|
|
237
|
-
});
|
|
238
|
-
});
|
|
239
|
-
});
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { join } from "node:path";
|
|
2
|
-
import { readJsoncFile } from "./jsonc-parser";
|
|
3
|
-
import { log } from "./logger";
|
|
4
|
-
import { getOpenCodeConfigPaths } from "./opencode-config-dir";
|
|
5
|
-
|
|
6
|
-
interface OpenCodeConfig {
|
|
7
|
-
compaction?: {
|
|
8
|
-
auto?: boolean;
|
|
9
|
-
prune?: boolean;
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function hasCompactionConflict(
|
|
14
|
-
compaction: OpenCodeConfig["compaction"] | undefined,
|
|
15
|
-
): boolean | undefined {
|
|
16
|
-
if (!compaction) return undefined;
|
|
17
|
-
const hasExplicitSetting = compaction.auto !== undefined || compaction.prune !== undefined;
|
|
18
|
-
if (!hasExplicitSetting) return undefined;
|
|
19
|
-
return compaction.auto === true || compaction.prune === true;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export function isOpenCodeAutoCompactionEnabled(directory: string): boolean {
|
|
23
|
-
if (process.env.OPENCODE_DISABLE_AUTOCOMPACT) {
|
|
24
|
-
log(
|
|
25
|
-
"[compaction-detector] OPENCODE_DISABLE_AUTOCOMPACT env flag set — auto compaction disabled",
|
|
26
|
-
);
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const projectCompaction = readProjectCompactionConfig(directory);
|
|
31
|
-
if (projectCompaction !== undefined) {
|
|
32
|
-
log("[compaction-detector] project config compaction conflict =", projectCompaction);
|
|
33
|
-
return projectCompaction;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const userCompaction = readUserCompactionConfig(directory);
|
|
37
|
-
if (userCompaction !== undefined) {
|
|
38
|
-
log("[compaction-detector] user config compaction conflict =", userCompaction);
|
|
39
|
-
return userCompaction;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
log("[compaction-detector] no compaction config found — default is enabled");
|
|
43
|
-
return true;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function readProjectCompactionConfig(directory: string): boolean | undefined {
|
|
47
|
-
// .opencode/ dir config has higher precedence than root-level config in OpenCode's loading order.
|
|
48
|
-
// Check highest precedence first — if .opencode/ sets compaction.auto, that wins.
|
|
49
|
-
const dotOpenCodeJsonc = join(directory, ".opencode", "opencode.jsonc");
|
|
50
|
-
const dotOpenCodeJson = join(directory, ".opencode", "opencode.json");
|
|
51
|
-
const dotOpenCodeConfig =
|
|
52
|
-
readJsoncFile<OpenCodeConfig>(dotOpenCodeJsonc) ??
|
|
53
|
-
readJsoncFile<OpenCodeConfig>(dotOpenCodeJson);
|
|
54
|
-
|
|
55
|
-
const dotOpenCodeCompactionConflict = hasCompactionConflict(dotOpenCodeConfig?.compaction);
|
|
56
|
-
if (dotOpenCodeCompactionConflict !== undefined) {
|
|
57
|
-
return dotOpenCodeCompactionConflict;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// Root-level project config (lower precedence than .opencode/)
|
|
61
|
-
const rootJsonc = join(directory, "opencode.jsonc");
|
|
62
|
-
const rootJson = join(directory, "opencode.json");
|
|
63
|
-
const rootConfig =
|
|
64
|
-
readJsoncFile<OpenCodeConfig>(rootJsonc) ?? readJsoncFile<OpenCodeConfig>(rootJson);
|
|
65
|
-
|
|
66
|
-
return hasCompactionConflict(rootConfig?.compaction);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function readUserCompactionConfig(_directory: string): boolean | undefined {
|
|
70
|
-
try {
|
|
71
|
-
const paths = getOpenCodeConfigPaths({ binary: "opencode" });
|
|
72
|
-
const config =
|
|
73
|
-
readJsoncFile<OpenCodeConfig>(paths.configJsonc) ??
|
|
74
|
-
readJsoncFile<OpenCodeConfig>(paths.configJson);
|
|
75
|
-
|
|
76
|
-
return hasCompactionConflict(config?.compaction);
|
|
77
|
-
} catch {
|
|
78
|
-
// Intentional: config read is best-effort; missing/unreadable config is not an error
|
|
79
|
-
return undefined;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
@@ -1,302 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OpenCode adapter for the harness-agnostic transcript interface.
|
|
3
|
-
*
|
|
4
|
-
* This is a thin proxy over OpenCode's `MessageLike[]` (i.e. `{ info,
|
|
5
|
-
* parts: unknown[] }[]`) — it does NOT copy data. Mutations through
|
|
6
|
-
* `setText`/`setToolOutput`/`replaceWithSentinel` write directly into
|
|
7
|
-
* the source `parts[]` arrays, exactly as the existing OpenCode-only
|
|
8
|
-
* transform code does today. `commit()` is a no-op because OpenCode's
|
|
9
|
-
* AI SDK reads `parts[]` back from the same array we mutated.
|
|
10
|
-
*
|
|
11
|
-
* This module is the boundary that lets the rest of the transform code
|
|
12
|
-
* (which moves to use the Transcript interface in 4b.2) work both for
|
|
13
|
-
* OpenCode and Pi without branching on harness type. By the end of 4b
|
|
14
|
-
* the only OpenCode-aware code in the plugin is this file plus
|
|
15
|
-
* `messages-transform.ts`.
|
|
16
|
-
*
|
|
17
|
-
* ## Mutation contract recap
|
|
18
|
-
*
|
|
19
|
-
* Magic Context's transform mutates message parts in three ways:
|
|
20
|
-
*
|
|
21
|
-
* 1. **Tag prefix injection** — prepends `§N§ ` to text parts and
|
|
22
|
-
* tool result outputs. Repeated tagging is idempotent because
|
|
23
|
-
* `prependTag` strips any existing prefix first.
|
|
24
|
-
*
|
|
25
|
-
* 2. **Sentinel replacement** — when a queued drop fires, the part is
|
|
26
|
-
* replaced with a `[dropped §N§]` or `[truncated §N§]` placeholder.
|
|
27
|
-
* The original tag number is preserved so the agent's mental
|
|
28
|
-
* model of "what was here" survives.
|
|
29
|
-
*
|
|
30
|
-
* 3. **Structural noise stripping** — `step-start`/`step-finish`
|
|
31
|
-
* wrappers and similar structural metadata are replaced with empty
|
|
32
|
-
* sentinel parts so they don't consume tag numbers or get tagged
|
|
33
|
-
* themselves.
|
|
34
|
-
*
|
|
35
|
-
* The OpenCode adapter implements (1) and (2) by editing `part.text` /
|
|
36
|
-
* `part.state.output` in place. For (3), structural parts surface as
|
|
37
|
-
* `kind: "structural"` so callers can filter them out. Adapter does NOT
|
|
38
|
-
* itself perform stripping — that's the transform pipeline's job, called
|
|
39
|
-
* after the adapter wraps the messages.
|
|
40
|
-
*
|
|
41
|
-
* Step 4b.1 ships the adapter alone. The existing OpenCode transform
|
|
42
|
-
* code keeps using `MessageLike[]` directly until 4b.2 migrates the
|
|
43
|
-
* tagging+drops layer to use Transcript instances.
|
|
44
|
-
*/
|
|
45
|
-
|
|
46
|
-
import { estimateTokens } from "../hooks/magic-context/read-session-formatting";
|
|
47
|
-
import { isRecord } from "./record-type-guard";
|
|
48
|
-
import type {
|
|
49
|
-
Transcript,
|
|
50
|
-
TranscriptMessage,
|
|
51
|
-
TranscriptPart,
|
|
52
|
-
TranscriptPartKind,
|
|
53
|
-
} from "./transcript";
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* The OpenCode `MessageLike` shape. Re-declared here to avoid a circular
|
|
57
|
-
* import with `tag-messages.ts` (which lives in the magic-context hooks
|
|
58
|
-
* tree and depends on storage). Keeping a local minimal type also makes
|
|
59
|
-
* the adapter trivially unit-testable without booting OpenCode SDK
|
|
60
|
-
* types.
|
|
61
|
-
*
|
|
62
|
-
* MUST stay structurally compatible with `tag-messages.ts MessageLike` —
|
|
63
|
-
* if that file's MessageLike adds a required field, this one needs to
|
|
64
|
-
* add it too. The build will fail loudly if the shapes diverge.
|
|
65
|
-
*/
|
|
66
|
-
export interface OpenCodeMessageLike {
|
|
67
|
-
info: { id?: string; role?: string; sessionID?: string };
|
|
68
|
-
parts: unknown[];
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Wrap an existing `MessageLike[]` as a Transcript. Zero copies — every
|
|
73
|
-
* `TranscriptPart` returned proxies the matching entry in the source
|
|
74
|
-
* `parts` array, and mutations are reflected immediately.
|
|
75
|
-
*/
|
|
76
|
-
export function createOpenCodeTranscript(messages: OpenCodeMessageLike[]): Transcript {
|
|
77
|
-
const transcriptMessages: TranscriptMessage[] = messages.map((message) => ({
|
|
78
|
-
info: {
|
|
79
|
-
id: message.info.id,
|
|
80
|
-
role: message.info.role ?? "unknown",
|
|
81
|
-
sessionId: message.info.sessionID,
|
|
82
|
-
},
|
|
83
|
-
// `parts` is a getter so newly-replaced sentinels inside the
|
|
84
|
-
// underlying array are surfaced on the next read. Cheap; allocs
|
|
85
|
-
// one wrapper per part per access. Adapter callers iterate
|
|
86
|
-
// `messages` once per pass so the cost is O(parts) per pass —
|
|
87
|
-
// same as before the adapter existed.
|
|
88
|
-
get parts(): TranscriptPart[] {
|
|
89
|
-
return message.parts.map((part, index) => createOpenCodePart(message, index, part));
|
|
90
|
-
},
|
|
91
|
-
}));
|
|
92
|
-
|
|
93
|
-
return {
|
|
94
|
-
messages: transcriptMessages,
|
|
95
|
-
harness: "opencode",
|
|
96
|
-
// OpenCode reads parts back from the same `parts[]` array we
|
|
97
|
-
// mutated, so there's nothing to flush. Adapters that buffer
|
|
98
|
-
// mutations (Pi) override this.
|
|
99
|
-
commit(): void {
|
|
100
|
-
/* no-op for OpenCode */
|
|
101
|
-
},
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Construct a TranscriptPart proxy over a single OpenCode part.
|
|
107
|
-
*
|
|
108
|
-
* Held by `parts` getter calls only; never cached because the underlying
|
|
109
|
-
* `parts[]` array can be mutated in place (sentinel replacement) and
|
|
110
|
-
* cached proxies would point at stale data. The constructor cost is
|
|
111
|
-
* trivial — small object literal, no allocations beyond the closure.
|
|
112
|
-
*/
|
|
113
|
-
function createOpenCodePart(
|
|
114
|
-
parent: OpenCodeMessageLike,
|
|
115
|
-
index: number,
|
|
116
|
-
rawPart: unknown,
|
|
117
|
-
): TranscriptPart {
|
|
118
|
-
const kind = classifyOpenCodePart(rawPart);
|
|
119
|
-
const id = extractPartId(rawPart);
|
|
120
|
-
|
|
121
|
-
return {
|
|
122
|
-
kind,
|
|
123
|
-
id,
|
|
124
|
-
getText(): string | undefined {
|
|
125
|
-
return readOpenCodePartText(rawPart);
|
|
126
|
-
},
|
|
127
|
-
setText(newText: string): boolean {
|
|
128
|
-
return writeOpenCodePartText(rawPart, newText);
|
|
129
|
-
},
|
|
130
|
-
setToolOutput(newText: string): boolean {
|
|
131
|
-
return writeOpenCodeToolOutput(rawPart, newText);
|
|
132
|
-
},
|
|
133
|
-
getToolMetadata(): {
|
|
134
|
-
toolName: string | undefined;
|
|
135
|
-
inputByteSize: number;
|
|
136
|
-
inputTokenCount: number;
|
|
137
|
-
} {
|
|
138
|
-
return readOpenCodeToolMetadata(rawPart);
|
|
139
|
-
},
|
|
140
|
-
getToolInput(): Record<string, unknown> | null {
|
|
141
|
-
return readOpenCodeToolInput(rawPart);
|
|
142
|
-
},
|
|
143
|
-
setToolInput(input: Record<string, unknown>): boolean {
|
|
144
|
-
return writeOpenCodeToolInput(rawPart, input);
|
|
145
|
-
},
|
|
146
|
-
replaceWithSentinel(sentinelText: string): boolean {
|
|
147
|
-
// Build a synthetic text part that carries the sentinel as
|
|
148
|
-
// its content. Subsequent passes see this as a normal text
|
|
149
|
-
// part with kind="text" — but the existing tagging code is
|
|
150
|
-
// idempotent and won't double-tag a part that already has
|
|
151
|
-
// the right prefix, so re-processing is safe.
|
|
152
|
-
//
|
|
153
|
-
// We DON'T preserve the original part type. Sentinels are
|
|
154
|
-
// always text — that's the contract the existing
|
|
155
|
-
// apply-operations code expects.
|
|
156
|
-
const sentinelPart = { type: "text", text: sentinelText };
|
|
157
|
-
parent.parts[index] = sentinelPart;
|
|
158
|
-
return true;
|
|
159
|
-
},
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/** Classify part kind based on `type` field. Falls back to "unknown". */
|
|
164
|
-
function classifyOpenCodePart(part: unknown): TranscriptPartKind {
|
|
165
|
-
if (!isRecord(part)) return "unknown";
|
|
166
|
-
const type = part.type;
|
|
167
|
-
if (typeof type !== "string") return "unknown";
|
|
168
|
-
switch (type) {
|
|
169
|
-
case "text":
|
|
170
|
-
return "text";
|
|
171
|
-
case "reasoning":
|
|
172
|
-
return "thinking";
|
|
173
|
-
case "tool":
|
|
174
|
-
return "tool_use";
|
|
175
|
-
case "file":
|
|
176
|
-
return "file";
|
|
177
|
-
case "image":
|
|
178
|
-
return "image";
|
|
179
|
-
case "step-start":
|
|
180
|
-
case "step-finish":
|
|
181
|
-
return "structural";
|
|
182
|
-
default:
|
|
183
|
-
return "unknown";
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Extract a stable per-part identifier when present. Used by the dropped-
|
|
189
|
-
* placeholder watermark to track which sentinels are already replayed
|
|
190
|
-
* across passes.
|
|
191
|
-
*/
|
|
192
|
-
function extractPartId(part: unknown): string | undefined {
|
|
193
|
-
if (!isRecord(part)) return undefined;
|
|
194
|
-
if (typeof part.id === "string" && part.id.length > 0) return part.id;
|
|
195
|
-
if (typeof part.callID === "string" && part.callID.length > 0) return part.callID;
|
|
196
|
-
return undefined;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
function readOpenCodePartText(part: unknown): string | undefined {
|
|
200
|
-
if (!isRecord(part)) return undefined;
|
|
201
|
-
if (typeof part.text === "string") return part.text;
|
|
202
|
-
if (typeof part.thinking === "string") return part.thinking;
|
|
203
|
-
if (part.type === "tool") {
|
|
204
|
-
const state = isRecord(part.state) ? part.state : null;
|
|
205
|
-
const output = state && typeof state.output === "string" ? state.output : "";
|
|
206
|
-
return output;
|
|
207
|
-
}
|
|
208
|
-
return undefined;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
function writeOpenCodePartText(part: unknown, newText: string): boolean {
|
|
212
|
-
if (!isRecord(part)) return false;
|
|
213
|
-
const writable = part as Record<string, unknown>;
|
|
214
|
-
if (typeof writable.text === "string") {
|
|
215
|
-
if (writable.text === newText) return false;
|
|
216
|
-
writable.text = newText;
|
|
217
|
-
return true;
|
|
218
|
-
}
|
|
219
|
-
if (typeof writable.thinking === "string") {
|
|
220
|
-
if (writable.thinking === newText) return false;
|
|
221
|
-
writable.thinking = newText;
|
|
222
|
-
return true;
|
|
223
|
-
}
|
|
224
|
-
return false;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
function writeOpenCodeToolOutput(part: unknown, newText: string): boolean {
|
|
228
|
-
if (!isRecord(part)) return false;
|
|
229
|
-
if (part.type !== "tool") return false;
|
|
230
|
-
const state = isRecord(part.state) ? (part.state as Record<string, unknown>) : null;
|
|
231
|
-
if (!state) return false;
|
|
232
|
-
if (typeof state.output !== "string") return false;
|
|
233
|
-
if (state.output === newText) return false;
|
|
234
|
-
state.output = newText;
|
|
235
|
-
return true;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
function readOpenCodeToolMetadata(part: unknown): {
|
|
239
|
-
toolName: string | undefined;
|
|
240
|
-
inputByteSize: number;
|
|
241
|
-
inputTokenCount: number;
|
|
242
|
-
} {
|
|
243
|
-
if (!isRecord(part)) return { toolName: undefined, inputByteSize: 0, inputTokenCount: 0 };
|
|
244
|
-
if (part.type !== "tool") return { toolName: undefined, inputByteSize: 0, inputTokenCount: 0 };
|
|
245
|
-
|
|
246
|
-
// OpenCode parts use `tool` as the tool name field; some legacy
|
|
247
|
-
// shapes use `toolName` or `name`. Match all three for forward
|
|
248
|
-
// compatibility with shape evolution.
|
|
249
|
-
const toolName =
|
|
250
|
-
typeof part.tool === "string"
|
|
251
|
-
? part.tool
|
|
252
|
-
: typeof part.toolName === "string"
|
|
253
|
-
? part.toolName
|
|
254
|
-
: typeof part.name === "string"
|
|
255
|
-
? part.name
|
|
256
|
-
: undefined;
|
|
257
|
-
|
|
258
|
-
const state = isRecord(part.state) ? part.state : null;
|
|
259
|
-
const input = state?.input ?? part.args ?? part.input;
|
|
260
|
-
|
|
261
|
-
let inputByteSize = 0;
|
|
262
|
-
let inputTokenCount = 0;
|
|
263
|
-
if (input !== undefined && input !== null) {
|
|
264
|
-
try {
|
|
265
|
-
const serialized = typeof input === "string" ? input : JSON.stringify(input);
|
|
266
|
-
inputByteSize = serialized.length;
|
|
267
|
-
inputTokenCount = serialized ? estimateTokens(serialized) : 0;
|
|
268
|
-
} catch {
|
|
269
|
-
inputByteSize = 0;
|
|
270
|
-
inputTokenCount = 0;
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
return { toolName, inputByteSize, inputTokenCount };
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
/** Non-mutating read of an OpenCode tool part's input object (or null). */
|
|
278
|
-
function readOpenCodeToolInput(part: unknown): Record<string, unknown> | null {
|
|
279
|
-
if (!isRecord(part)) return null;
|
|
280
|
-
const state = isRecord(part.state) ? part.state : null;
|
|
281
|
-
const input = state?.input ?? part.args ?? part.input;
|
|
282
|
-
return isRecord(input) ? input : null;
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
/** Replace an OpenCode tool part's input object in place. Returns true if a
|
|
286
|
-
* writable input slot was found. */
|
|
287
|
-
function writeOpenCodeToolInput(part: unknown, input: Record<string, unknown>): boolean {
|
|
288
|
-
if (!isRecord(part)) return false;
|
|
289
|
-
if (isRecord(part.state) && isRecord(part.state.input)) {
|
|
290
|
-
part.state.input = input;
|
|
291
|
-
return true;
|
|
292
|
-
}
|
|
293
|
-
if (isRecord(part.args)) {
|
|
294
|
-
part.args = input;
|
|
295
|
-
return true;
|
|
296
|
-
}
|
|
297
|
-
if (isRecord(part.input)) {
|
|
298
|
-
part.input = input;
|
|
299
|
-
return true;
|
|
300
|
-
}
|
|
301
|
-
return false;
|
|
302
|
-
}
|