opencode-self-improvement 0.14.23 → 0.14.24

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-self-improvement",
3
- "version": "0.14.23",
3
+ "version": "0.14.24",
4
4
  "description": "Durable memory for opencode: remember facts, decisions, and commands across sessions, plus a one-command per-repo setup (/repo-init).",
5
5
  "main": "index.ts",
6
6
  "exports": {
package/src/gates.ts CHANGED
@@ -14,11 +14,12 @@ import {
14
14
  type SupersedeFields,
15
15
  } from "./memory.ts";
16
16
  import { chainId, contentHash, record, recordWriteBlocked } from "./evidence.ts";
17
- import { noteAuthored } from "./state.ts";
17
+ import { getCurrent, noteAuthored } from "./state.ts";
18
18
  import { MAX_WRITE_EVIDENCE_REFS } from "./triggers.ts";
19
19
  import { flattenMemoryText } from "./sanitize.ts";
20
20
  import { containsSecret, redactSecrets } from "./secrets.ts";
21
- import { readerForSession } from "./memory.ts";
21
+ import { readerFromSid, UnresolvedWriterError, type Reader } from "./memory.ts";
22
+ import { writeTarget, type WriteTarget } from "./repokey.ts";
22
23
  import {
23
24
  displayCall,
24
25
  extractCallForms,
@@ -69,6 +70,11 @@ export const auditCaveat = (outcome: GateOutcome): string =>
69
70
 
70
71
  export interface GateContext {
71
72
  liveRoot: string;
73
+ // Capture authority before awaiting locks, model calls, or gate evidence.
74
+ // Named callers read their repo; named-unbound callers read global-only;
75
+ // absent/"?" callers capture ambient authority at entry (not after awaits).
76
+ reader?: Reader;
77
+ writeAt?: WriteTarget;
72
78
  evidenceCalls: string[];
73
79
  // Total before the telemetry window was selected. Legacy/direct callers
74
80
  // may still supply their complete list; the writer bounds that too.
@@ -274,6 +280,8 @@ export async function runWriteGate(
274
280
  candidate: WriteCandidate,
275
281
  ctx: GateContext,
276
282
  ): Promise<GateOutcome> {
283
+ const reader = ctx.reader ?? readerFromSid(ctx.sessionID);
284
+ const writeAt = ctx.writeAt ?? writeTarget(ctx.sessionID ?? getCurrent().sid);
277
285
  const chain = chainId();
278
286
  // Gate audit lines thread the session: runWriteGate awaits (analysis, the
279
287
  // locked append), and another session's dispatch can move the ambient
@@ -379,13 +387,7 @@ export async function runWriteGate(
379
387
  const scopeMems = candidate.scope
380
388
  ? await loadScope(
381
389
  candidate.scope,
382
- // Dedup/stale reads partition on the WRITE's session, captured at
383
- // flow entry — not the ambient current after this await (#127). An
384
- // unknown "?" (or absent) caller is the readerFromSid AMBIENT case,
385
- // exactly as before: readerForSession is for named sessions only.
386
- ctx.sessionID !== undefined && ctx.sessionID !== "?"
387
- ? readerForSession(ctx.sessionID)
388
- : undefined,
390
+ reader,
389
391
  )
390
392
  : [];
391
393
  // One tree index per write, shared by gate1 and every gate2 stale scan.
@@ -740,7 +742,15 @@ export async function runWriteGate(
740
742
  const snapshot = await snapshotFiles([join(memoryRoot(), `${new Date().toISOString().split("T")[0]}.logfmt`)]);
741
743
  // The write carries the CALLER's session, not whatever current says after
742
744
  // the gate's own awaits - capture's stamp must follow the captured session.
743
- const ts = await appendMemory({ ...candidate, sessionID: ctx.sessionID });
745
+ if (candidate.repo === undefined && !writeAt.ok) throw new UnresolvedWriterError(writeAt.sid);
746
+ // A captured unscoped ambient writer deliberately publishes repo=global.
747
+ // Leaving repo undefined would make appendMemory resolve ambient ownership
748
+ // again after awaits. This changes the old unstamped shape: exports now keep
749
+ // this row global on import instead of adopting the importing repository.
750
+ const owner = candidate.repo === undefined && writeAt.ok
751
+ ? { repo: writeAt.scope?.key ?? "global", repoPath: writeAt.scope?.path }
752
+ : {};
753
+ const ts = await appendMemory({ ...candidate, ...owner, sessionID: ctx.sessionID });
744
754
  // #165: remember who made this row, so the writing session is not billed a
745
755
  // prompt-cache bust to be shown its own thought. Only promoted writes are
746
756
  // attributed — a blocked candidate created nothing.
package/src/index.ts CHANGED
@@ -296,6 +296,8 @@ const impl = {
296
296
  const bound = sid ?? getCurrent().sid;
297
297
  return {
298
298
  sessionID: sid,
299
+ reader: readerFromSid(sid),
300
+ writeAt: writeTarget(bound),
299
301
  // NOTE (re-review): this one lookup is intentionally NON-firm — a
300
302
  // provisional binding may supply the gate's verification TREE. The
301
303
  // tree is where a referenced file is checked to exist, not who the
@@ -687,7 +689,7 @@ const impl = {
687
689
  repo: requested.repo,
688
690
  repoPath: target.repo ? target.repoPath : undefined,
689
691
  },
690
- { ...gateContext(sid), supersedesTs: target.ts, durable: gateDurable },
692
+ { ...gateContext(sid), reader, supersedesTs: target.ts, durable: gateDurable },
691
693
  );
692
694
  if (outcome.action === "promoted" && outcome.ts && target) {
693
695
  mutated = true;
@@ -1137,13 +1139,14 @@ const impl = {
1137
1139
  await bindSessionIdentity(sid);
1138
1140
  const ownerless = ownerlessWriter(sid);
1139
1141
  if (ownerless) return await refuseOwnerlessWrite(ownerless, "import");
1142
+ const importGate = gateContext(sid);
1140
1143
  try {
1141
1144
  const t = await importMemories(
1142
1145
  args.file,
1143
1146
  (c) =>
1144
1147
  runWriteGate(
1145
1148
  { ...c, tags: [...new Set([...(c.tags ?? []), "import"])] },
1146
- gateContext(sid),
1149
+ importGate,
1147
1150
  ),
1148
1151
  sid,
1149
1152
  );
@@ -1529,6 +1532,7 @@ const impl = {
1529
1532
  await record("auto_capture_refused", { session: sid, trigger }, true, sid);
1530
1533
  return;
1531
1534
  }
1535
+ const captureGate = gateContext(sid);
1532
1536
  let transcript = seeded;
1533
1537
  let msgs = seededMsgs;
1534
1538
  if (transcript.length < 200) {
@@ -1561,7 +1565,7 @@ const impl = {
1561
1565
  ? ["auto-capture", CAPTURE_COMPACT_TAG]
1562
1566
  : ["auto-capture"],
1563
1567
  },
1564
- gateContext(sid),
1568
+ captureGate,
1565
1569
  );
1566
1570
  await record(
1567
1571
  trigger === "compaction"
package/src/init.ts CHANGED
@@ -8,10 +8,11 @@
8
8
  import { basename, join } from "node:path";
9
9
  import { existsSync } from "node:fs";
10
10
  import { cbmBin } from "./config.ts";
11
- import { isVisibleTo, loadAll, markSuperseded, readerFromSid } from "./memory.ts";
11
+ import { isVisibleTo, loadAll, markSuperseded, readerFromSid, type Reader } from "./memory.ts";
12
12
  import { runWriteGate, type GateContext } from "./gates.ts";
13
13
  import { record } from "./evidence.ts";
14
14
  import { writeEvidence, hasGateCheck } from "./triggers.ts";
15
+ import { writeTarget } from "./repokey.ts";
15
16
  import { getCurrent } from "./state.ts";
16
17
 
17
18
  export interface ScriptRecord {
@@ -190,13 +191,12 @@ export interface InitStamp {
190
191
  head: string;
191
192
  }
192
193
 
193
- export async function initStamp(scope: string, sid?: string): Promise<InitStamp | null> {
194
+ export async function initStamp(scope: string, sid?: string, reader: Reader = readerFromSid(sid)): Promise<InitStamp | null> {
194
195
  // Scope is a basename, so two different repos can share one
195
196
  // ("api" next to "api", a fork beside its upstream). Visibility filtering
196
197
  // makes each repo see only its own stamp — basename collisions no longer
197
198
  // alias init state between repos. The reader comes from the CALLER's
198
199
  // session, not from the global current after this await (#127).
199
- const reader = readerFromSid(sid);
200
200
  const mems = (await loadAll()).filter((m) => !m.superseded_by && isVisibleTo(m, reader));
201
201
  for (const m of mems) {
202
202
  if (m.type !== "context" || m.scope !== scope) continue;
@@ -208,6 +208,8 @@ export async function initStamp(scope: string, sid?: string): Promise<InitStamp
208
208
  }
209
209
 
210
210
  export async function runInit(args: InitArgs): Promise<InitSummary> {
211
+ const reader = readerFromSid(args.sessionID);
212
+ const writeAt = writeTarget(args.sessionID ?? getCurrent().sid);
211
213
  const root = await gitRootOf(args.liveRoot);
212
214
  const scope = basename(root) || basename(args.liveRoot);
213
215
  const head = await gitHead(root);
@@ -216,6 +218,8 @@ export async function runInit(args: InitArgs): Promise<InitSummary> {
216
218
 
217
219
  const gateCtx: GateContext = {
218
220
  liveRoot: root,
221
+ reader,
222
+ writeAt,
219
223
  ...writeEvidence(args.sessionID ?? getCurrent().sid),
220
224
  hasGateCheck: hasGateCheck(args.sessionID ?? getCurrent().sid),
221
225
  // Stamps land on the caller's session even though runInit awaits git
@@ -224,7 +228,7 @@ export async function runInit(args: InitArgs): Promise<InitSummary> {
224
228
  trusted: true,
225
229
  };
226
230
 
227
- const prior = await initStamp(scope, args.sessionID);
231
+ const prior = await initStamp(scope, args.sessionID, reader);
228
232
  if (prior && !force && prior.head === head) {
229
233
  await record("init_skip", { scope, head, since: prior.ts }, false);
230
234
  return {
package/src/portable.ts CHANGED
@@ -288,6 +288,10 @@ export async function importMemories(
288
288
  // the ambient current.
289
289
  sessionID?: string,
290
290
  ): Promise<ImportTally> {
291
+ // Capture the importing owner before file I/O: later session moves must not
292
+ // re-home rows partway through the import (#183). Resolve with the writer
293
+ // rule so an unresolved named session never borrows the ambient repo (#152).
294
+ const writeAt = writeTarget(sessionID ?? getCurrent().sid);
291
295
  const target = await resolveContained(path, memoryRoot(), {
292
296
  label: "memory_import file",
293
297
  // Deliberately no mention of the operator escape hatch: this string goes
@@ -307,12 +311,8 @@ export async function importMemories(
307
311
  throw err;
308
312
  }
309
313
  const tally: ImportTally = { promoted: 0, collapsed: 0, blocked: 0, skipped: 0, downgraded: 0, unaudited: 0 };
310
- // #152: the importing session's repository is resolved BEFORE the first row,
311
- // through the writer rule rather than the ambient one. An unresolved named
312
- // session has no write target, and half a file re-stamped into whatever
313
- // workspace happened to set up last is worse than none — so this throws
314
- // before a single row is gated, not partway through the tally.
315
- const writeAt = writeTarget(sessionID ?? getCurrent().sid);
314
+ // Preserve file-validation error precedence, but reject an unresolved owner
315
+ // before gating any row. Resolution happened at entry, not at this await.
316
316
  if (!writeAt.ok) throw new UnresolvedWriterError(writeAt.sid);
317
317
  const currentScope = writeAt.scope;
318
318
  let allowFrontier = false;