opencode-self-improvement 0.15.11 → 0.15.13

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.
@@ -2,7 +2,7 @@
2
2
  // index.ts; this module owns frozen bytes, refresh decisions, and their status.
3
3
  import { loadAll, malformedCount } from "../store/files/load.ts"
4
4
  import { type Memory } from "../store/model/memory.ts"
5
- import { isActive, isInjectable } from "../store/model/predicates.ts"
5
+ import { isInjectable } from "../store/model/predicates.ts"
6
6
  import { isVisibleTo, readerFromSid } from "../store/model/visibility.ts";
7
7
  import { SESSION_CAP, SessionLru } from "../core/lru.ts";
8
8
  import { record } from "../evidence/record.ts";
@@ -11,7 +11,8 @@ import { systemDigest } from "./digest.ts"
11
11
  import { storeVersion } from "./version.ts"
12
12
  import { selectRelevant } from "../recall/relevant.ts";
13
13
  import { attributeSession, deriveMemoryRefs, type MemoryRefs } from "./attribution.ts";
14
- import { neverRetrieved, readLedger, useSignals } from "../hits/ledger.ts";
14
+ import { readLedger, useSignals } from "../hits/ledger.ts";
15
+ import { summarizeStore } from "../status/store-summary.ts";
15
16
  import { emptyStats, type Measured, type SessionStats, type StatusPayload } from "../status/payload.ts"
16
17
  import { writeStatus } from "../status/write.ts";
17
18
  import { callsFor } from "../capture/triggers.ts";
@@ -105,7 +106,6 @@ export function createInjectionEpochs({
105
106
  const ep = epoch.get(sid);
106
107
  if (!ep) return null;
107
108
  const all = await loadAll();
108
- const act = all.filter(isActive);
109
109
  const led = await readLedger();
110
110
  return {
111
111
  at: ep.at,
@@ -116,15 +116,7 @@ export function createInjectionEpochs({
116
116
  blockChars: ep.block.length,
117
117
  sectionChars: ep.prefix.length,
118
118
  budget,
119
- store: {
120
- active: act.length,
121
- superseded: all.length - act.length,
122
- open: act.filter((m) => m.status === "open").length,
123
- scopes: new Set(act.map((m) => m.scope)).size,
124
- malformed: malformedCount(),
125
- neverRetrieved: neverRetrieved(act, led).length,
126
- ledgerSince: led.since,
127
- },
119
+ store: summarizeStore(all, led, malformedCount()).counts,
128
120
  stats: statsFor(sid),
129
121
  measured: measuredTurns.get(sid) ?? null,
130
122
  attribution: attributeSession(callsFor(sid), ep.refs),
@@ -0,0 +1,26 @@
1
+ // Shared store counts for epoch payloads and the chat report (#168).
2
+ // Callers own I/O and visibility policy; this summarizes exactly their rows.
3
+ import { neverRetrieved, type UseLedger } from "../hits/ledger.ts";
4
+ import type { Memory } from "../store/model/memory.ts";
5
+ import { isActive } from "../store/model/predicates.ts";
6
+ import type { StoreCounts } from "./payload.ts";
7
+
8
+ export function summarizeStore(
9
+ rows: Memory[],
10
+ ledger: UseLedger,
11
+ malformed: number,
12
+ ): { active: Memory[]; counts: StoreCounts } {
13
+ const active = rows.filter(isActive);
14
+ return {
15
+ active,
16
+ counts: {
17
+ active: active.length,
18
+ superseded: rows.length - active.length,
19
+ open: active.filter((m) => m.status === "open").length,
20
+ scopes: new Set(active.map((m) => m.scope)).size,
21
+ malformed,
22
+ neverRetrieved: neverRetrieved(active, ledger).length,
23
+ ledgerSince: ledger.since,
24
+ },
25
+ };
26
+ }
@@ -1,5 +1,6 @@
1
- // visibility — split from src/memory.ts (#168 domain restructure). Declaration bodies are unchanged.
2
- import { repoScopeFor, sessionScope, type RepoScope } from "../../identity/repokey.ts";
1
+ // Repository visibility, shared by memory rows, restore lines, and evidence.
2
+ import { createHash } from "node:crypto";
3
+ import { normalizeDirPath, repoScopeFor, sessionScope, type RepoScope } from "../../identity/repokey.ts";
3
4
  import { getCurrent } from "../../core/state.ts";
4
5
  import { parseLine } from "../../core/logfmt/format.ts";
5
6
  import { prune } from "../files/prune.ts";
@@ -60,6 +61,25 @@ export function readerFromSid(sid?: unknown): Reader {
60
61
  : readerForAmbient();
61
62
  }
62
63
 
64
+ // Before #109, path-derived keys on win32/darwin hashed the whole canonical
65
+ // path in lowercase, even on case-sensitive volumes. Such a key can equal a
66
+ // distinct lowercase sibling's current key (#254). The stored path identifies
67
+ // these legacy stamps without a migration: require the same directory, allowing
68
+ // real symlink/case-insensitive aliases, instead of trusting that key alone.
69
+ // Remote-derived keys still share across clones with different paths. A stamp
70
+ // without repoPath cannot be classified this way and keeps key-only matching.
71
+ function matchesRepository(repo: string, repoPath: string | undefined, scope: RepoScope): boolean {
72
+ if (repoPath === scope.path) return true; // pre-hash and legacy owner access
73
+ if (repoPath) {
74
+ // The historical hash used realpath too: repoPath itself may name a
75
+ // symlink/junction, a short Windows spelling, or contain dot segments.
76
+ const canonical = normalizeDirPath(repoPath);
77
+ const legacyKey = createHash("sha1").update(canonical.toLowerCase()).digest("hex").slice(0, 12);
78
+ if (repo === legacyKey) return canonical === normalizeDirPath(scope.path);
79
+ }
80
+ return repo === scope.key;
81
+ }
82
+
63
83
  // The ONLY repo filter every read path uses (injection sinks, recall, list,
64
84
  // update targeting, prune, decay, gate scope reads). A row is visible when it
65
85
  // is global (explicit marker OR no stamp — legacy rows and writes by
@@ -70,7 +90,7 @@ export function isVisibleTo(m: Pick<Memory, "repo" | "repoPath">, reader: Reader
70
90
  if (reader.kind === "admin") return true;
71
91
  if (!m.repo || m.repo === "global") return true;
72
92
  if (reader.kind === "global-only") return false;
73
- return m.repo === reader.scope.key || (!!m.repoPath && m.repoPath === reader.scope.path);
93
+ return matchesRepository(m.repo, m.repoPath, reader.scope);
74
94
  }
75
95
 
76
96
  export function visibleTo(mems: Memory[], reader: Reader): Memory[] {
@@ -116,10 +136,7 @@ export function evidenceVisibleTo(
116
136
  if (repo === "global") return true;
117
137
  if (reader.kind === "global-only") return false;
118
138
  const repoPath = fields.repoPath;
119
- return (
120
- repo === reader.scope.key ||
121
- (typeof repoPath === "string" && !!repoPath && repoPath === reader.scope.path)
122
- );
139
+ return matchesRepository(repo, typeof repoPath === "string" ? repoPath : undefined, reader.scope);
123
140
  }
124
141
  const sid = fields.sid;
125
142
  if (typeof sid === "string" && sid !== "" && sid !== "?") {
@@ -1,18 +1,16 @@
1
- // The memory_status tool, extracted from index.ts (#168). Schema, description
2
- // and handler are byte-identical to the pre-split source; closure access arrives
3
- // through ToolDeps.
1
+ // The memory_status tool, extracted from index.ts (#168).
2
+ // Closure access arrives through ToolDeps.
4
3
  import type { ToolRegister } from "./deps.ts";
5
- import { neverRetrieved, readLedger } from "../hits/ledger.ts";
4
+ import { readLedger } from "../hits/ledger.ts";
6
5
  import { formatMemory } from "../inject/format.ts";
7
6
  import { loadAll, malformedCount } from "../store/files/load.ts"
8
- import { isActive } from "../store/model/predicates.ts"
9
7
  import { isVisibleTo, readerFromSid } from "../store/model/visibility.ts";
10
8
  import { isQuarantined, quarantineReasons } from "../store/quarantine/quarantine.ts";
11
9
  import { EmptyArgs, validated } from "./schema.ts";
12
10
  import { captureLag, captureLagLines, rowsPerDay } from "../status/metrics.ts"
13
11
  import { renderMemoryReport } from "../status/report.ts"
14
12
  import { blockedRecent } from "../status/write.ts";
15
- import type { StoreCounts } from "../status/payload.ts";
13
+ import { summarizeStore } from "../status/store-summary.ts";
16
14
 
17
15
  export const register: ToolRegister = (editor, deps) => {
18
16
  const { toolIds, sessions, direct } = deps;
@@ -28,19 +26,11 @@ export const register: ToolRegister = (editor, deps) => {
28
26
  if (sid !== "?") await sessions.refresh(sid);
29
27
  const status = sid === "?" ? null : await sessions.buildPayload(sid);
30
28
  const all = await loadAll();
31
- const act = all.filter(isActive);
32
- const store: StoreCounts = {
33
- active: act.length,
34
- superseded: all.length - act.length,
35
- open: act.filter((m) => m.status === "open").length,
36
- scopes: new Set(act.map((m) => m.scope)).size,
37
- malformed: malformedCount(),
38
- };
29
+ const malformed = malformedCount();
39
30
  // Use ledger (#42): how much of the store has never been surfaced.
40
31
  // Reported, never acted on — retirement is a decision, not a metric.
41
32
  const ledger = await readLedger();
42
- store.neverRetrieved = neverRetrieved(act, ledger).length;
43
- store.ledgerSince = ledger.since;
33
+ const { active: act, counts: store } = summarizeStore(all, ledger, malformed);
44
34
  // Self-metrics (#44): what the store costs, how fast it grows, and how
45
35
  // stale a capture is when it fires. Same rule as above — the numbers
46
36
  // are the point, not a verdict on them.
@@ -21,6 +21,7 @@ import { snapshotFiles } from "../store/files/snapshots.ts"
21
21
  import { markSuperseded } from "../store/files/supersede.ts"
22
22
  import { type Memory } from "../store/model/memory.ts"
23
23
  import { releaseQuarantine } from "../store/quarantine/release.ts";
24
+ import { assertViewerWriteAllowed } from "./rejections.ts";
24
25
 
25
26
  export interface EditInput {
26
27
  ts: string;
@@ -75,6 +76,9 @@ export async function editMemory(input: EditInput): Promise<MutationResult> {
75
76
  const chain = chainId();
76
77
  const target = await findMemory(input.ts);
77
78
 
79
+ // Editing is not an implicit un-reject. Refuse before snapshots, appends,
80
+ // retirement or audit so a rejected edit leaves its original row intact.
81
+ await assertViewerWriteAllowed(input.content);
78
82
  const snapshot = await snapshotFiles([target.file]);
79
83
  const newTs = await appendMemory({
80
84
  type: input.type || target.type,
@@ -0,0 +1,15 @@
1
+ // Viewer writes are deliberate admin edits, independent of model grounding
2
+ // gates. They still honor value rejections until an explicit release (#222).
3
+ import { redactSecrets } from "../store/secrets.ts";
4
+ import { probeRejection } from "../store/tombstones/rejection.ts";
5
+
6
+ export async function assertViewerWriteAllowed(content: string): Promise<void> {
7
+ // Match the bytes appendMemory persists, as the write gate already does.
8
+ // Read/probe errors propagate: an unavailable rejection set cannot grant a write.
9
+ const { rejection } = await probeRejection(redactSecrets(content));
10
+ if (!rejection) return;
11
+ throw new Error(
12
+ `viewer write blocked: previously_rejected (${rejection.kind}; hash ${rejection.tombstone.ch}). ` +
13
+ "Nothing was stored. Use Rejections → un-reject in the viewer to explicitly lift that rejection before retrying.",
14
+ );
15
+ }
@@ -20,6 +20,7 @@ import { releaseQuarantinedMemory } from "./mutate.ts";
20
20
  import { isQuarantined, quarantineReasons } from "../store/quarantine/quarantine.ts";
21
21
  import { deleteMemory, editMemory, blockMemory, unblockMemory, unrejectMemory, type MutationResult } from "./mutate.ts";
22
22
  import { tombstones } from "../store/tombstones/tombstone.ts";
23
+ import { assertViewerWriteAllowed } from "./rejections.ts";
23
24
 
24
25
  const PAGE = join(import.meta.dir, "page.html");
25
26
  const HOST = process.env.OPENCODE_VIEWER_HOST || "127.0.0.1";
@@ -315,6 +316,7 @@ async function unrejectRoute(body: z.infer<typeof UnrejectBody>): Promise<Respon
315
316
  }
316
317
 
317
318
  async function restoreEntryRoute(body: z.infer<typeof RestoreEntryBody>): Promise<Response> {
319
+ await assertViewerWriteAllowed(body.content);
318
320
  // Origin survives the restore round trip (#129): when the audit line carried
319
321
  // a repo stamp, the restored row carries it again — the viewer's own missing
320
322
  // repository scope must never be what decides a row's visibility. No repo