opencode-self-improvement 0.15.17 → 0.15.19
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/README.md +4 -0
- package/package.json +4 -2
- package/src/capture/candidates.ts +17 -6
- package/src/capture/lag.ts +6 -2
- package/src/capture/prompt.ts +1 -1
- package/src/capture/runtime.ts +85 -21
- package/src/capture/session-model.ts +2 -1
- package/src/capture/transcript.ts +10 -4
- package/src/capture/triggers.ts +48 -14
- package/src/command/ensure.ts +6 -1
- package/src/consolidate/propose.ts +47 -17
- package/src/consolidate/render.ts +3 -1
- package/src/core/cmp-ts.ts +2 -2
- package/src/core/config/budgets.ts +0 -1
- package/src/core/config/paths.ts +2 -3
- package/src/core/hooks.ts +8 -3
- package/src/core/host.ts +8 -3
- package/src/core/logfmt/format.ts +20 -5
- package/src/core/logfmt/publish.ts +21 -4
- package/src/core/lru.ts +20 -20
- package/src/core/permissions.ts +21 -5
- package/src/core/runtime.ts +7 -3
- package/src/core/state.ts +12 -2
- package/src/core/time.ts +0 -1
- package/src/core/tokenize.ts +16 -5
- package/src/core/types.ts +8 -1
- package/src/events.ts +74 -18
- package/src/evidence/record.ts +29 -9
- package/src/gate/gate.ts +208 -64
- package/src/gate/resolver.ts +105 -20
- package/src/hits/ledger.ts +35 -10
- package/src/identity/host-events.ts +35 -11
- package/src/identity/repokey.ts +27 -9
- package/src/identity/session-context.ts +39 -8
- package/src/index.ts +108 -42
- package/src/init/detect.ts +62 -12
- package/src/init/run.ts +50 -11
- package/src/inject/anchor.ts +11 -5
- package/src/inject/attribution.ts +29 -6
- package/src/inject/budget.ts +9 -2
- package/src/inject/digest.ts +16 -8
- package/src/inject/epochs.ts +43 -15
- package/src/inject/pull.ts +11 -5
- package/src/inject/version.ts +10 -5
- package/src/portable/decay.ts +20 -9
- package/src/portable/export.ts +30 -10
- package/src/portable/import.ts +43 -12
- package/src/recall/relevant.ts +11 -5
- package/src/recall/score.ts +5 -3
- package/src/status/format.ts +8 -2
- package/src/status/metrics.ts +22 -6
- package/src/status/payload.ts +7 -2
- package/src/status/report.ts +63 -18
- package/src/status/write.ts +21 -7
- package/src/store/blocklist.ts +16 -4
- package/src/store/containment.ts +27 -8
- package/src/store/files/append.ts +57 -37
- package/src/store/files/deletions.ts +5 -2
- package/src/store/files/load.ts +37 -11
- package/src/store/files/prune.ts +18 -5
- package/src/store/files/snapshots.ts +6 -2
- package/src/store/files/supersede.ts +32 -12
- package/src/store/files/target.ts +4 -1
- package/src/store/lock.ts +10 -2
- package/src/store/model/visibility.ts +26 -6
- package/src/store/prune.ts +1 -1
- package/src/store/quarantine/quarantine.ts +36 -9
- package/src/store/quarantine/release.ts +9 -3
- package/src/store/secrets.ts +4 -2
- package/src/store/tombstones/rejection.ts +21 -5
- package/src/store/tombstones/tombstone.ts +48 -11
- package/src/store/undo.ts +43 -18
- package/src/tools/consolidate.ts +18 -8
- package/src/tools/decay.ts +37 -9
- package/src/tools/deps.ts +10 -2
- package/src/tools/evidence.ts +7 -4
- package/src/tools/export.ts +10 -4
- package/src/tools/forget.ts +15 -5
- package/src/tools/import.ts +16 -5
- package/src/tools/init.ts +13 -4
- package/src/tools/list.ts +12 -6
- package/src/tools/merge-review.ts +52 -20
- package/src/tools/recall.ts +17 -9
- package/src/tools/registry.ts +4 -1
- package/src/tools/remember.ts +21 -7
- package/src/tools/schema.ts +55 -18
- package/src/tools/status-tool.ts +29 -11
- package/src/tools/undo.ts +23 -8
- package/src/tools/update.ts +116 -40
- package/src/tui/panel.tsx +119 -30
- package/src/viewer/mutate.ts +57 -20
- package/src/viewer/rejections.ts +1 -1
- package/src/viewer/server.ts +88 -28
|
@@ -28,8 +28,8 @@
|
|
|
28
28
|
// 4. The most recently used member is never a loser (#42's rule, inherited).
|
|
29
29
|
import type { Memory } from "../store/model/memory.ts";
|
|
30
30
|
import type { UseSignal } from "../hits/ledger.ts";
|
|
31
|
-
import { cmpTs } from "../core/cmp-ts.ts"
|
|
32
|
-
import { formatMemory } from "../inject/format.ts"
|
|
31
|
+
import { cmpTs } from "../core/cmp-ts.ts";
|
|
32
|
+
import { formatMemory } from "../inject/format.ts";
|
|
33
33
|
import { scoreMemories, words } from "../recall/score.ts";
|
|
34
34
|
|
|
35
35
|
/** Default jaccard floor for "these two rows are the same statement". */
|
|
@@ -54,7 +54,10 @@ export interface ClusterOptions {
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
function tokens(text: string): string[] {
|
|
57
|
-
return text
|
|
57
|
+
return text
|
|
58
|
+
.toLowerCase()
|
|
59
|
+
.split(/[^a-z0-9_.]+/)
|
|
60
|
+
.filter(Boolean);
|
|
58
61
|
}
|
|
59
62
|
|
|
60
63
|
function jaccard(a: Set<string>, b: Set<string>): number {
|
|
@@ -73,7 +76,10 @@ function jaccard(a: Set<string>, b: Set<string>): number {
|
|
|
73
76
|
* material to compress, and collapsing it would destroy the frontier the panel
|
|
74
77
|
* is showing the user.
|
|
75
78
|
*/
|
|
76
|
-
export function clustersFor(
|
|
79
|
+
export function clustersFor(
|
|
80
|
+
rows: Memory[],
|
|
81
|
+
opts: ClusterOptions = {},
|
|
82
|
+
): Memory[][] {
|
|
77
83
|
const min = opts.jaccard ?? DEFAULT_JACCARD;
|
|
78
84
|
const minMembers = opts.minMembers ?? 2;
|
|
79
85
|
const maxMembers = opts.maxMembers ?? 6;
|
|
@@ -123,7 +129,10 @@ export function clustersFor(rows: Memory[], opts: ClusterOptions = {}): Memory[]
|
|
|
123
129
|
* recency in the ledger — a row that retrieval has actually been landing on is
|
|
124
130
|
* the one worth keeping, and this is #42's rule applied to merges.
|
|
125
131
|
*/
|
|
126
|
-
export function survivorFor(
|
|
132
|
+
export function survivorFor(
|
|
133
|
+
cluster: Memory[],
|
|
134
|
+
use?: Map<string, UseSignal>,
|
|
135
|
+
): Memory {
|
|
127
136
|
return [...cluster].sort((a, b) => {
|
|
128
137
|
const la = use?.get(a.ts)?.last ?? "";
|
|
129
138
|
const lb = use?.get(b.ts)?.last ?? "";
|
|
@@ -153,7 +162,9 @@ export interface MergePlan {
|
|
|
153
162
|
|
|
154
163
|
/** Distinctive vocabulary of the absorbed rows: what the survivor does not say. */
|
|
155
164
|
function inheritedKw(cluster: Memory[], survivor: Memory): string[] {
|
|
156
|
-
const have = new Set(
|
|
165
|
+
const have = new Set(
|
|
166
|
+
words(`${survivor.content} ${(survivor.kw ?? []).join(" ")}`),
|
|
167
|
+
);
|
|
157
168
|
const out = [...(survivor.kw ?? [])];
|
|
158
169
|
for (const m of cluster) {
|
|
159
170
|
if (m.ts === survivor.ts) continue;
|
|
@@ -172,10 +183,13 @@ function buildMerged(cluster: Memory[], survivor: Memory): Memory {
|
|
|
172
183
|
return {
|
|
173
184
|
...survivor,
|
|
174
185
|
kw: kw.length ? kw : undefined,
|
|
175
|
-
tags: [
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
186
|
+
tags: [
|
|
187
|
+
...new Set([
|
|
188
|
+
"consolidated",
|
|
189
|
+
...(survivor.tags ?? []),
|
|
190
|
+
...cluster.flatMap((m) => m.tags ?? []),
|
|
191
|
+
]),
|
|
192
|
+
].slice(0, 12),
|
|
179
193
|
};
|
|
180
194
|
}
|
|
181
195
|
|
|
@@ -188,15 +202,24 @@ function buildMerged(cluster: Memory[], survivor: Memory): Memory {
|
|
|
188
202
|
// (correctly — it is the row id in the store), but nothing here may assume two
|
|
189
203
|
// rows cannot share one timestamp: the eval store does exactly that, and a
|
|
190
204
|
// ts-keyed filter would silently absorb an entire cluster into its survivor.
|
|
191
|
-
function replayReads(
|
|
205
|
+
function replayReads(
|
|
206
|
+
rows: Memory[],
|
|
207
|
+
cluster: Memory[],
|
|
208
|
+
replacement: Memory | null,
|
|
209
|
+
reads: ReadSet[],
|
|
210
|
+
): number {
|
|
192
211
|
const inCluster = new Set(cluster);
|
|
193
|
-
const store = replacement
|
|
212
|
+
const store = replacement
|
|
213
|
+
? [...rows.filter((m) => !inCluster.has(m)), replacement]
|
|
214
|
+
: rows;
|
|
194
215
|
let hits = 0;
|
|
195
216
|
for (const r of reads) {
|
|
196
217
|
const query = r.terms.join(" ");
|
|
197
218
|
if (!query.trim()) continue;
|
|
198
219
|
const top = scoreMemories(query, store, 3);
|
|
199
|
-
const landed = top.some((m) =>
|
|
220
|
+
const landed = top.some((m) =>
|
|
221
|
+
replacement ? m === replacement : inCluster.has(m),
|
|
222
|
+
);
|
|
200
223
|
if (landed) hits++;
|
|
201
224
|
}
|
|
202
225
|
return hits;
|
|
@@ -205,7 +228,9 @@ function replayReads(rows: Memory[], cluster: Memory[], replacement: Memory | nu
|
|
|
205
228
|
/** Reads the ledger says this cluster served (any member credited). */
|
|
206
229
|
export function readsFor(cluster: Memory[], allReads: ReadSet[]): ReadSet[] {
|
|
207
230
|
const ids = new Set(cluster.map((m) => m.ts));
|
|
208
|
-
return allReads.filter(
|
|
231
|
+
return allReads.filter(
|
|
232
|
+
(r) => r.rows.some((id) => ids.has(id)) && r.terms.length > 0,
|
|
233
|
+
);
|
|
209
234
|
}
|
|
210
235
|
|
|
211
236
|
/**
|
|
@@ -235,12 +260,16 @@ export function proposeCluster(
|
|
|
235
260
|
const bytesAfter = formatMemory(merged).length;
|
|
236
261
|
|
|
237
262
|
const reasons: string[] = [];
|
|
238
|
-
if (!reads.length)
|
|
263
|
+
if (!reads.length)
|
|
264
|
+
reasons.push(
|
|
265
|
+
"unmeasured: the ledger has no read for any member of this cluster",
|
|
266
|
+
);
|
|
239
267
|
else if (readsAfter < bestMemberBefore)
|
|
240
268
|
reasons.push(
|
|
241
269
|
`reads would drop: merged row catches ${readsAfter} of ${reads.length}, the best member alone caught ${bestMemberBefore}`,
|
|
242
270
|
);
|
|
243
|
-
if (bytesAfter > bytesBefore)
|
|
271
|
+
if (bytesAfter > bytesBefore)
|
|
272
|
+
reasons.push("merged row is larger than the rows it absorbs");
|
|
244
273
|
|
|
245
274
|
return {
|
|
246
275
|
id: `cl-${cluster
|
|
@@ -286,7 +315,8 @@ export function proposeConsolidations(
|
|
|
286
315
|
.map((c) => proposeCluster(rows, c, use, reads))
|
|
287
316
|
.filter((p) => p.accepted);
|
|
288
317
|
const absorbed = new Set<Memory>(plans.flatMap((p) => p.losers));
|
|
289
|
-
const injected = (list: Memory[]) =>
|
|
318
|
+
const injected = (list: Memory[]) =>
|
|
319
|
+
list.reduce((n, m) => n + formatMemory(m).length, 0);
|
|
290
320
|
return {
|
|
291
321
|
plans,
|
|
292
322
|
rowsBefore: rows.length,
|
|
@@ -25,6 +25,8 @@ export function renderProposals(report: ProposalReport): string {
|
|
|
25
25
|
`STORE ${report.rowsBefore} rows → ${report.rowsAfter} if every proposal is resolved · ` +
|
|
26
26
|
`injected ${report.injectedBefore}c → ${report.injectedAfter}c`,
|
|
27
27
|
);
|
|
28
|
-
out.push(
|
|
28
|
+
out.push(
|
|
29
|
+
"Resolving is supersede-not-delete: absorbed rows stay recoverable via memory_undo.",
|
|
30
|
+
);
|
|
29
31
|
return out.join("\n");
|
|
30
32
|
}
|
package/src/core/cmp-ts.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
// cmp-ts — split from src/inject.ts (#168 domain restructure). Declaration bodies are unchanged.
|
|
2
2
|
|
|
3
|
-
|
|
4
3
|
// Numeric compare, with the string compare kept as fallback for non-dates
|
|
5
4
|
// (the ledger hands "" when a row was never used). ISO strings agree with
|
|
6
5
|
// Date.parse only while the FORMAT agrees: a legacy second-precision stamp
|
|
@@ -11,6 +10,7 @@
|
|
|
11
10
|
export function cmpTs(a: string, b: string): number {
|
|
12
11
|
const na = Date.parse(a);
|
|
13
12
|
const nb = Date.parse(b);
|
|
14
|
-
if (!Number.isNaN(na) && !Number.isNaN(nb))
|
|
13
|
+
if (!Number.isNaN(na) && !Number.isNaN(nb))
|
|
14
|
+
return na < nb ? -1 : na > nb ? 1 : 0;
|
|
15
15
|
return a < b ? -1 : a > b ? 1 : 0;
|
|
16
16
|
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
// budgets — split from src/config.ts (#168 domain restructure). Declaration bodies are unchanged.
|
|
2
2
|
|
|
3
|
-
|
|
4
3
|
// Character budget for the injected "Relevant durable memory" block (the
|
|
5
4
|
// cache-safe epoch injection). Overridable via OPENCODE_MEMORY_INJECT_BUDGET
|
|
6
5
|
// (plugin options take precedence — see src/index.ts); falls back to 1200 on
|
package/src/core/config/paths.ts
CHANGED
|
@@ -8,10 +8,9 @@ import { fileURLToPath } from "node:url";
|
|
|
8
8
|
import { tightenDirectory } from "../permissions.ts";
|
|
9
9
|
|
|
10
10
|
export function memoryRoot(): string {
|
|
11
|
-
const root =
|
|
11
|
+
const root =
|
|
12
12
|
process.env.OPENCODE_MEMORY_DIR ||
|
|
13
|
-
join(homedir(), ".config", "opencode", "memory")
|
|
14
|
-
);
|
|
13
|
+
join(homedir(), ".config", "opencode", "memory");
|
|
15
14
|
// Protect old stores even on read-only use; do not create an absent store.
|
|
16
15
|
tightenDirectory(root);
|
|
17
16
|
return root;
|
package/src/core/hooks.ts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
// Fail-soft wrapper for plugin bookkeeping hooks (#168).
|
|
2
|
-
export async function guarded(
|
|
2
|
+
export async function guarded(
|
|
3
|
+
name: string,
|
|
4
|
+
fn: () => Promise<void>,
|
|
5
|
+
): Promise<void> {
|
|
3
6
|
try {
|
|
4
7
|
await fn();
|
|
5
8
|
} catch (err) {
|
|
6
|
-
console.warn(
|
|
9
|
+
console.warn(
|
|
10
|
+
`[opencode-self-improvement] ${name} hook failed (ignored):`,
|
|
11
|
+
err,
|
|
12
|
+
);
|
|
7
13
|
}
|
|
8
14
|
}
|
|
9
|
-
|
package/src/core/host.ts
CHANGED
|
@@ -17,10 +17,16 @@ export interface PluginCtx {
|
|
|
17
17
|
options?: Record<string, unknown>;
|
|
18
18
|
tool?: {
|
|
19
19
|
transform: (cb: (editor: ToolEditorLike) => void) => Promise<unknown>;
|
|
20
|
-
hook: (
|
|
20
|
+
hook: (
|
|
21
|
+
name: string,
|
|
22
|
+
cb: (event: any) => Promise<void> | void,
|
|
23
|
+
) => Promise<HookReg | void>;
|
|
21
24
|
};
|
|
22
25
|
session?: {
|
|
23
|
-
hook: (
|
|
26
|
+
hook: (
|
|
27
|
+
name: string,
|
|
28
|
+
cb: (event: any) => Promise<void> | void,
|
|
29
|
+
) => Promise<HookReg | void>;
|
|
24
30
|
context?: (input: { sessionID: string }) => Promise<unknown>;
|
|
25
31
|
get?: (input: { sessionID: string }) => Promise<unknown>;
|
|
26
32
|
};
|
|
@@ -31,4 +37,3 @@ export interface PluginCtx {
|
|
|
31
37
|
text: (input: unknown) => Promise<{ text?: string } | string>;
|
|
32
38
|
};
|
|
33
39
|
}
|
|
34
|
-
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
// format — split from src/logfmt.ts (#168 domain restructure). Declaration bodies are unchanged.
|
|
2
2
|
|
|
3
|
-
|
|
4
3
|
export const IS_WINDOWS = process.platform === "win32";
|
|
5
4
|
|
|
6
5
|
export type Fields = Record<string, unknown>;
|
|
@@ -9,9 +8,21 @@ export type Fields = Record<string, unknown>;
|
|
|
9
8
|
// value would end the record early and the rest of the value would be read
|
|
10
9
|
// back as a separate line of fields. Backslash is escaped too, so a value
|
|
11
10
|
// ending in `\` cannot swallow its closing quote.
|
|
12
|
-
const ESCAPE: Record<string, string> = {
|
|
11
|
+
const ESCAPE: Record<string, string> = {
|
|
12
|
+
"\\": "\\\\",
|
|
13
|
+
'"': '\\"',
|
|
14
|
+
"\n": "\\n",
|
|
15
|
+
"\r": "\\r",
|
|
16
|
+
"\t": "\\t",
|
|
17
|
+
};
|
|
13
18
|
|
|
14
|
-
const UNESCAPE: Record<string, string> = {
|
|
19
|
+
const UNESCAPE: Record<string, string> = {
|
|
20
|
+
"\\": "\\",
|
|
21
|
+
'"': '"',
|
|
22
|
+
n: "\n",
|
|
23
|
+
r: "\r",
|
|
24
|
+
t: "\t",
|
|
25
|
+
};
|
|
15
26
|
|
|
16
27
|
// Lines written before these escapes existed stored backslashes raw, so in an
|
|
17
28
|
// old line `C:\new` is a backslash and an `n`, not a newline. A line whose
|
|
@@ -27,7 +38,9 @@ const MARKER_VALUE = "1";
|
|
|
27
38
|
export function quote(value: unknown): string {
|
|
28
39
|
const s = String(value);
|
|
29
40
|
if (s === "") return '""';
|
|
30
|
-
return /[\s"=]/.test(s)
|
|
41
|
+
return /[\s"=]/.test(s)
|
|
42
|
+
? `"${s.replace(/[\\"\n\r\t]/g, (c) => ESCAPE[c])}"`
|
|
43
|
+
: s;
|
|
31
44
|
}
|
|
32
45
|
|
|
33
46
|
export function formatLine(fields: Fields): string {
|
|
@@ -63,7 +76,9 @@ export function parseLine(line: string): Fields | null {
|
|
|
63
76
|
if (!m) break;
|
|
64
77
|
tokens.push([m[1], m[2]]);
|
|
65
78
|
}
|
|
66
|
-
const marker = tokens.findIndex(
|
|
79
|
+
const marker = tokens.findIndex(
|
|
80
|
+
([k, v]) => k === MARKER && v === MARKER_VALUE,
|
|
81
|
+
);
|
|
67
82
|
const fields: Fields = {};
|
|
68
83
|
tokens.forEach(([key, value], i) => {
|
|
69
84
|
if (key === MARKER) return;
|
|
@@ -24,7 +24,14 @@ export async function appendLog(file: string, line: string): Promise<void> {
|
|
|
24
24
|
// Appending is safe against other appenders only. Callers writing a file that
|
|
25
25
|
// is ALSO rewritten whole (a daily memory file, the blocklist) hold the store
|
|
26
26
|
// lock around this — see lock.ts.
|
|
27
|
-
const handle = await open(
|
|
27
|
+
const handle = await open(
|
|
28
|
+
file,
|
|
29
|
+
constants.O_WRONLY |
|
|
30
|
+
constants.O_APPEND |
|
|
31
|
+
constants.O_CREAT |
|
|
32
|
+
(constants.O_NOFOLLOW ?? 0),
|
|
33
|
+
0o600,
|
|
34
|
+
);
|
|
28
35
|
try {
|
|
29
36
|
if (!IS_WINDOWS) await handle.chmod((await handle.stat()).mode & 0o600);
|
|
30
37
|
await handle.appendFile(next);
|
|
@@ -59,7 +66,11 @@ export async function fileIdentity(file: string): Promise<string> {
|
|
|
59
66
|
// Without it, publishing behaves exactly as it always has: one rename.
|
|
60
67
|
let tmpSeq = 0;
|
|
61
68
|
|
|
62
|
-
export async function writeAtomic(
|
|
69
|
+
export async function writeAtomic(
|
|
70
|
+
file: string,
|
|
71
|
+
content: string,
|
|
72
|
+
since?: string,
|
|
73
|
+
): Promise<void> {
|
|
63
74
|
await ensureDir(dirname(file));
|
|
64
75
|
// Unique PER CALL, not per process: shared store writers now fail closed on
|
|
65
76
|
// lock timeout, but direct/legacy callers can still overlap. A pid-stable
|
|
@@ -94,7 +105,11 @@ export async function writeAtomic(file: string, content: string, since?: string)
|
|
|
94
105
|
// reader handle, a mismatch means the write's premise is gone and it fails
|
|
95
106
|
// with the original error, as the no-retry path always did. Hence also: no
|
|
96
107
|
// `since`, no retry.
|
|
97
|
-
async function handoverRename(
|
|
108
|
+
async function handoverRename(
|
|
109
|
+
tmp: string,
|
|
110
|
+
file: string,
|
|
111
|
+
since?: string,
|
|
112
|
+
): Promise<void> {
|
|
98
113
|
try {
|
|
99
114
|
await rename(tmp, file);
|
|
100
115
|
return;
|
|
@@ -105,7 +120,9 @@ async function handoverRename(tmp: string, file: string, since?: string): Promis
|
|
|
105
120
|
for (;;) {
|
|
106
121
|
const now = Date.now();
|
|
107
122
|
if (now >= deadline) throw err;
|
|
108
|
-
await new Promise((r) =>
|
|
123
|
+
await new Promise((r) =>
|
|
124
|
+
setTimeout(r, Math.min(backoff, deadline - now)),
|
|
125
|
+
);
|
|
109
126
|
backoff = Math.min(backoff * 2, 50);
|
|
110
127
|
if ((await fileIdentity(file)) !== since) throw err; // destination moved; our copy is stale
|
|
111
128
|
try {
|
package/src/core/lru.ts
CHANGED
|
@@ -8,27 +8,27 @@
|
|
|
8
8
|
export const SESSION_CAP = 64;
|
|
9
9
|
|
|
10
10
|
export class SessionLru {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
private readonly order = new Set<string>();
|
|
12
|
+
private readonly cap: number;
|
|
13
|
+
private readonly onEvict: (sid: string) => void;
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
constructor(cap: number, onEvict: (sid: string) => void) {
|
|
16
|
+
this.cap = cap;
|
|
17
|
+
this.onEvict = onEvict;
|
|
18
|
+
}
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
20
|
+
// Mark a session as just used; evicts the least recently used beyond the cap.
|
|
21
|
+
touch(sid: string): void {
|
|
22
|
+
this.order.delete(sid);
|
|
23
|
+
this.order.add(sid);
|
|
24
|
+
while (this.order.size > this.cap) {
|
|
25
|
+
const oldest = this.order.values().next().value as string;
|
|
26
|
+
this.order.delete(oldest);
|
|
27
|
+
this.onEvict(oldest);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
get size(): number {
|
|
32
|
+
return this.order.size;
|
|
33
|
+
}
|
|
34
34
|
}
|
package/src/core/permissions.ts
CHANGED
|
@@ -1,14 +1,24 @@
|
|
|
1
1
|
// POSIX modes protect local store data, not Windows ACLs. Never follow a
|
|
2
2
|
// final-component link while tightening permissions, or add owner access that
|
|
3
3
|
// an operator deliberately removed (e.g. a read-only store).
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
closeSync,
|
|
6
|
+
constants,
|
|
7
|
+
fchmodSync,
|
|
8
|
+
fstatSync,
|
|
9
|
+
mkdirSync,
|
|
10
|
+
openSync,
|
|
11
|
+
} from "node:fs";
|
|
5
12
|
import { dirname } from "node:path";
|
|
6
13
|
|
|
7
14
|
export function tightenDirectory(path: string): void {
|
|
8
15
|
if (process.platform === "win32") return;
|
|
9
16
|
let fd: number;
|
|
10
17
|
try {
|
|
11
|
-
fd = openSync(
|
|
18
|
+
fd = openSync(
|
|
19
|
+
path,
|
|
20
|
+
constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW,
|
|
21
|
+
);
|
|
12
22
|
} catch (err) {
|
|
13
23
|
if ((err as NodeJS.ErrnoException).code === "ENOENT") return;
|
|
14
24
|
throw err;
|
|
@@ -25,7 +35,8 @@ export function tightenDirectory(path: string): void {
|
|
|
25
35
|
// its existing permissions were tightened. Open-time refusals above
|
|
26
36
|
// remain policy errors; unexpected I/O failures still propagate.
|
|
27
37
|
const code = (err as NodeJS.ErrnoException).code;
|
|
28
|
-
if (!code || !["EPERM", "EROFS", "EOPNOTSUPP", "ENOSYS"].includes(code))
|
|
38
|
+
if (!code || !["EPERM", "EROFS", "EOPNOTSUPP", "ENOSYS"].includes(code))
|
|
39
|
+
throw err;
|
|
29
40
|
}
|
|
30
41
|
}
|
|
31
42
|
} finally {
|
|
@@ -38,9 +49,14 @@ export function tightenDirectory(path: string): void {
|
|
|
38
49
|
export function preparePrivateFile(path: string): void {
|
|
39
50
|
mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
|
|
40
51
|
tightenDirectory(dirname(path));
|
|
41
|
-
const fd = openSync(
|
|
52
|
+
const fd = openSync(
|
|
53
|
+
path,
|
|
54
|
+
constants.O_WRONLY | constants.O_CREAT | (constants.O_NOFOLLOW ?? 0),
|
|
55
|
+
0o600,
|
|
56
|
+
);
|
|
42
57
|
try {
|
|
43
|
-
if (process.platform !== "win32")
|
|
58
|
+
if (process.platform !== "win32")
|
|
59
|
+
fchmodSync(fd, fstatSync(fd).mode & 0o600);
|
|
44
60
|
} finally {
|
|
45
61
|
closeSync(fd);
|
|
46
62
|
}
|
package/src/core/runtime.ts
CHANGED
|
@@ -28,13 +28,17 @@ function cmpTriples(a: readonly number[], b: readonly number[]): number {
|
|
|
28
28
|
// could not be parsed; null when the version looks fine. Warn-only by design:
|
|
29
29
|
// an old embedded bun should be visible, but must not stop memory from
|
|
30
30
|
// loading. The caller decides how often to emit it (src/index.ts: once).
|
|
31
|
-
export function bunFloorWarning(
|
|
31
|
+
export function bunFloorWarning(
|
|
32
|
+
versionString: string | undefined,
|
|
33
|
+
): string | null {
|
|
32
34
|
const floor = MIN_BUN.join(".");
|
|
33
|
-
const seen =
|
|
35
|
+
const seen =
|
|
36
|
+
typeof versionString === "string" ? parseBunVersion(versionString) : null;
|
|
34
37
|
if (seen) {
|
|
35
38
|
if (cmpTriples(seen, MIN_BUN) >= 0) return null;
|
|
36
39
|
return `[opencode-self-improvement] Bun ${seen.join(".")} is below the supported minimum ${floor}; memory features may misbehave.`;
|
|
37
40
|
}
|
|
38
|
-
const shown =
|
|
41
|
+
const shown =
|
|
42
|
+
versionString === undefined ? "unknown" : JSON.stringify(versionString);
|
|
39
43
|
return `[opencode-self-improvement] Bun version ${shown} is unrecognized (supported minimum ${floor}); memory features may misbehave.`;
|
|
40
44
|
}
|
package/src/core/state.ts
CHANGED
|
@@ -28,7 +28,10 @@ export interface SessionState {
|
|
|
28
28
|
sid: string;
|
|
29
29
|
calls: ToolResult[];
|
|
30
30
|
// Last observed status per tool, so a FAIL->PASS transition is visible.
|
|
31
|
-
lastByTool: Map<
|
|
31
|
+
lastByTool: Map<
|
|
32
|
+
string,
|
|
33
|
+
{ status: "FAIL" | "PASS"; call: string; emitted: boolean }
|
|
34
|
+
>;
|
|
32
35
|
// A pending completion-gate failure awaiting a later PASS.
|
|
33
36
|
pendingGate: {
|
|
34
37
|
call: string;
|
|
@@ -104,7 +107,14 @@ const sessions = new Map<string, SessionState>();
|
|
|
104
107
|
export function session(sid: string): SessionState {
|
|
105
108
|
let s = sessions.get(sid);
|
|
106
109
|
if (!s) {
|
|
107
|
-
s = {
|
|
110
|
+
s = {
|
|
111
|
+
sid,
|
|
112
|
+
calls: [],
|
|
113
|
+
lastByTool: new Map(),
|
|
114
|
+
pendingGate: null,
|
|
115
|
+
turns: 0,
|
|
116
|
+
finalized: false,
|
|
117
|
+
};
|
|
108
118
|
sessions.set(sid, s);
|
|
109
119
|
}
|
|
110
120
|
return s;
|
package/src/core/time.ts
CHANGED
package/src/core/tokenize.ts
CHANGED
|
@@ -89,16 +89,28 @@ export const POL_NEG = "neg";
|
|
|
89
89
|
const POLARITY: ReadonlyMap<string, string> = new Map<string, string>(
|
|
90
90
|
[
|
|
91
91
|
["enable enables enabled enabling", "disable disables disabled disabling"],
|
|
92
|
-
[
|
|
92
|
+
[
|
|
93
|
+
"allow allows allowed allowing",
|
|
94
|
+
"deny denies denied denying disallow disallows disallowed",
|
|
95
|
+
],
|
|
93
96
|
[
|
|
94
97
|
"permit permits permitted permitting",
|
|
95
98
|
"refuse refuses refused refusing forbid forbids forbidden prohibit prohibits prohibited prohibiting",
|
|
96
99
|
],
|
|
97
100
|
["accept accepts accepted accepting", "reject rejects rejected rejecting"],
|
|
98
|
-
[
|
|
101
|
+
[
|
|
102
|
+
"include includes included including",
|
|
103
|
+
"exclude excludes excluded excluding",
|
|
104
|
+
],
|
|
99
105
|
["add adds added adding", "remove removes removed removing"],
|
|
100
|
-
[
|
|
101
|
-
|
|
106
|
+
[
|
|
107
|
+
"start starts started starting begin begins began beginning",
|
|
108
|
+
"stop stops stopped stopping halt halts halted halting cease ceases ceased ceasing",
|
|
109
|
+
],
|
|
110
|
+
[
|
|
111
|
+
"require requires required requiring",
|
|
112
|
+
"waive waives waived waiving exempt exempts exempted exempting",
|
|
113
|
+
],
|
|
102
114
|
["keep keeps kept keeping", "drop drops dropped dropping"],
|
|
103
115
|
["true", "false"],
|
|
104
116
|
["always", "never"],
|
|
@@ -205,4 +217,3 @@ export function polaritySigns(text: string): string[] {
|
|
|
205
217
|
if (held) signs.add(held);
|
|
206
218
|
return [...signs].sort();
|
|
207
219
|
}
|
|
208
|
-
|
package/src/core/types.ts
CHANGED
|
@@ -2,5 +2,12 @@
|
|
|
2
2
|
// (src/capture.ts) can reference the admissible values without importing the
|
|
3
3
|
// plugin entry. schema.ts uses these in tool schemas; capture.ts uses TYPES
|
|
4
4
|
// to prompt and validate.
|
|
5
|
-
export const TYPES = [
|
|
5
|
+
export const TYPES = [
|
|
6
|
+
"decision",
|
|
7
|
+
"learning",
|
|
8
|
+
"preference",
|
|
9
|
+
"blocker",
|
|
10
|
+
"context",
|
|
11
|
+
"pattern",
|
|
12
|
+
] as const;
|
|
6
13
|
export const STATUSES = ["open", "settled"] as const;
|