opencode-self-improvement 0.15.28 → 0.15.29

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.15.28",
3
+ "version": "0.15.29",
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/core/hooks.ts CHANGED
@@ -1,4 +1,24 @@
1
1
  // Fail-soft wrapper for plugin bookkeeping hooks (#168).
2
+ //
3
+ // Fail-soft must never mean invisible (#138): a plugin whose hook failed on
4
+ // EVERY turn looked identical to a healthy one in every artifact, because the
5
+ // only trace was a console.warn into the host log. setup() installs a failure
6
+ // reporter that mirrors each swallowed error into the evidence file; the
7
+ // reporter is optional so this module stays free of the evidence dependency
8
+ // and a broken reporter can never change the swallow contract.
9
+ export type GuardedFailure = (
10
+ name: string,
11
+ err: unknown,
12
+ ) => void | Promise<void>;
13
+
14
+ let failureReporter: GuardedFailure | undefined;
15
+
16
+ export function setGuardedFailureReporter(
17
+ reporter: GuardedFailure | undefined,
18
+ ): void {
19
+ failureReporter = reporter;
20
+ }
21
+
2
22
  export async function guarded(
3
23
  name: string,
4
24
  fn: () => Promise<void>,
@@ -10,5 +30,13 @@ export async function guarded(
10
30
  `[opencode-self-improvement] ${name} hook failed (ignored):`,
11
31
  err,
12
32
  );
33
+ // Report AFTER the warn, and await the reporter so a caller that awaits
34
+ // guarded() can observe the evidence line; any reporter failure is itself
35
+ // swallowed — the console line is the primary trace, evidence the mirror.
36
+ try {
37
+ await failureReporter?.(name, err);
38
+ } catch {
39
+ // a failing reporter is itself a swallowed failure — never a throw
40
+ }
13
41
  }
14
42
  }
package/src/index.ts CHANGED
@@ -8,7 +8,7 @@
8
8
  // src/command.ts. The default export `{ id, setup(ctx) }` is checked against
9
9
  // the SDK's Plugin type using a type-only import (see the note on `plugin`).
10
10
  import { registerHooks } from "./events.ts";
11
- import { guarded } from "./core/hooks.ts";
11
+ import { guarded, setGuardedFailureReporter } from "./core/hooks.ts";
12
12
  import type { PluginCtx } from "./core/host.ts";
13
13
  import type { Cleanup, Plugin } from "@opencode/plugin/promise/plugin";
14
14
  import { basename, join } from "node:path";
@@ -122,6 +122,23 @@ const impl = {
122
122
  // only as the gate's verification tree when no session location is known.
123
123
  const setupRoot = ctx?.location?.directory;
124
124
  const liveRoot = setupRoot ?? process.cwd();
125
+ // #138: mirror every swallowed guarded() failure into the evidence file.
126
+ // Hook name + error class ONLY — never the message, which can carry
127
+ // conversation content. Fire-and-forget: evidence must not add a failure
128
+ // to a path that already failed softly.
129
+ setGuardedFailureReporter((name, err) =>
130
+ record(
131
+ "hook_error",
132
+ {
133
+ hook: name,
134
+ error:
135
+ err instanceof Error
136
+ ? err.constructor?.name || "Error"
137
+ : typeof err,
138
+ },
139
+ false,
140
+ ).catch(() => {}),
141
+ );
125
142
  setRepoScope(setupRoot ? resolveRepo(setupRoot) : null);
126
143
  // THIS instance's own workspace. The module scope says who loaded last;
127
144
  // this says who I am. An unbound session that calls into THIS instance
@@ -285,7 +302,12 @@ const impl = {
285
302
  );
286
303
  });
287
304
  const captureState = !wantCapture
288
- ? "off"
305
+ ? // captureOnCompact alone is a distinct misconfiguration (the
306
+ // runtime warns about it and stays disabled) — name it, or "off
307
+ // (default)" and "wrong switch set" share one line (#138).
308
+ ctx.options?.captureOnCompact === true
309
+ ? "switch_mismatch"
310
+ : "off"
289
311
  : !modelOk
290
312
  ? "invalid_model"
291
313
  : capabilities.length < 3
@@ -79,3 +79,92 @@ export async function captureLagLines(days = 30): Promise<string[]> {
79
79
  }
80
80
  return out;
81
81
  }
82
+
83
+ /**
84
+ * Capture health (#138): the answer to "is auto-capture off, misconfigured,
85
+ * armed-but-inert, or alive?" read from the evidence file ALONE — the
86
+ * `plugin_load` heartbeat names the configured state, `auto_capture_*` lines
87
+ * name the activity. The newest heartbeat wins (a reload with new options
88
+ * replaces the old state); activity is counted inside the trailing window.
89
+ */
90
+ export type CaptureHealthState =
91
+ | "unknown"
92
+ | "off"
93
+ | "switch_mismatch"
94
+ | "invalid_model"
95
+ | "no_capabilities"
96
+ | "on";
97
+
98
+ export interface CaptureHealth {
99
+ state: CaptureHealthState;
100
+ /** Promoted capture rows in the window. */
101
+ rows: number;
102
+ /** Blocked / refused / partial capture outcomes in the window. */
103
+ blocked: number;
104
+ /** Newest `auto_capture_*` line, or null when nothing fired. */
105
+ lastFired: string | null;
106
+ /**
107
+ * Newest `capture_lag` completion receipt in the window: proof the capture
108
+ * ran, independent of candidate outcome. A model response of `[]` writes
109
+ * only this line, so the inert verdict must consult it (#288 F1).
110
+ */
111
+ lastExecuted: string | null;
112
+ }
113
+
114
+ const CAPTURE_STATES: readonly CaptureHealthState[] = [
115
+ "off",
116
+ "switch_mismatch",
117
+ "invalid_model",
118
+ "no_capabilities",
119
+ "on",
120
+ ];
121
+
122
+ export function captureHealth(
123
+ lines: string[],
124
+ days = 30,
125
+ now = Date.now(),
126
+ ): CaptureHealth {
127
+ const cutoff = new Date(now - days * 86_400_000).toISOString();
128
+ let state: CaptureHealthState = "unknown";
129
+ let newestHeartbeat = "";
130
+ let rows = 0;
131
+ let blocked = 0;
132
+ let lastFired: string | null = null;
133
+ let lastExecuted: string | null = null;
134
+ for (const line of lines) {
135
+ const f = parseLine(line);
136
+ if (!f) continue;
137
+ const ev = String(f.ev ?? "");
138
+ const ts = typeof f.ts === "string" ? f.ts : "";
139
+ if (ev === "plugin_load") {
140
+ // Newest wins regardless of the order files were read in. A heartbeat
141
+ // whose capture field is missing or unrecognized names no state.
142
+ if (ts >= newestHeartbeat) {
143
+ newestHeartbeat = ts;
144
+ const c = String(f.capture ?? "");
145
+ state = (CAPTURE_STATES as readonly string[]).includes(c)
146
+ ? (c as CaptureHealthState)
147
+ : "unknown";
148
+ }
149
+ continue;
150
+ }
151
+ if (ev === "capture_lag") {
152
+ // Completion receipt: the model call finished whether or not it found
153
+ // candidates. Execution evidence on its own (#288 F1).
154
+ if (ts && ts >= cutoff && (!lastExecuted || ts > lastExecuted))
155
+ lastExecuted = ts;
156
+ continue;
157
+ }
158
+ if (!ev.startsWith("auto_capture_")) continue;
159
+ if (ts && ts < cutoff) continue;
160
+ if (ev.endsWith("promoted")) rows++;
161
+ else if (
162
+ ev.endsWith("blocked") ||
163
+ ev.endsWith("refused") ||
164
+ ev.endsWith("partial")
165
+ )
166
+ blocked++;
167
+ if (ts && (!lastFired || ts > lastFired)) lastFired = ts;
168
+ }
169
+ return { state, rows, blocked, lastFired, lastExecuted };
170
+ }
@@ -1,7 +1,12 @@
1
1
  // report — split from src/status.ts (#168 domain restructure). Declaration bodies are unchanged.
2
2
  import { join } from "node:path";
3
3
  import { cacheRate, estTok, fmtBytes, fmtCount, fmtMs } from "./format.ts";
4
- import { captureLag, ledgerIsYoung, rowsPerDay } from "./metrics.ts";
4
+ import {
5
+ type CaptureHealth,
6
+ captureLag,
7
+ ledgerIsYoung,
8
+ rowsPerDay,
9
+ } from "./metrics.ts";
5
10
  import {
6
11
  type BlockedRow,
7
12
  type StatusPayload,
@@ -136,6 +141,50 @@ export function attributionLines(p: StatusPayload): string[] {
136
141
  ];
137
142
  }
138
143
 
144
+ /**
145
+ * AUTO-CAPTURE line (#138): the chat-facing answer to "is capture off,
146
+ * misconfigured, armed-but-inert, or alive?" — the states the evidence file
147
+ * distinguishes, surfaced so a user never has to read a logfmt file to learn
148
+ * the pipeline never ran. `unknown` means no heartbeat was found at all, which
149
+ * is itself the answer the dead-pipeline bug needed.
150
+ */
151
+ export function captureLines(c: CaptureHealth): string[] {
152
+ switch (c.state) {
153
+ case "off":
154
+ return [" auto-capture: off (default)"];
155
+ case "invalid_model":
156
+ return [
157
+ " auto-capture: invalid_model — autoCapture is on but captureModel is malformed; nothing fires",
158
+ ];
159
+ case "no_capabilities":
160
+ return [
161
+ " auto-capture: no_capabilities — autoCapture is on but the host lacks event.subscribe/generate.text/session.context",
162
+ ];
163
+ case "switch_mismatch":
164
+ return [
165
+ " auto-capture: switch_mismatch — captureOnCompact is on but autoCapture is off; nothing fires",
166
+ ];
167
+ case "on": {
168
+ // Inert means NO execution evidence at all: no candidate line and no
169
+ // `capture_lag` completion receipt. A successful capture whose model
170
+ // returned `[]` writes only the receipt, and is alive, not inert
171
+ // (#288 F1).
172
+ if (!c.lastExecuted && c.rows === 0 && c.blocked === 0)
173
+ return [
174
+ " auto-capture: ARMED BUT NEVER FIRED — capture is on, no auto_capture_* or capture_lag lines in the last 30d",
175
+ ];
176
+ const fired = c.lastFired ?? c.lastExecuted;
177
+ return [
178
+ ` auto-capture: on, last fired ${fired ? ago(fired) : "now"}, ${c.rows} rows, ${c.blocked} refused`,
179
+ ];
180
+ }
181
+ default:
182
+ return [
183
+ " auto-capture: unknown — no plugin_load heartbeat in the evidence files (plugin never loaded here?)",
184
+ ];
185
+ }
186
+ }
187
+
139
188
  /**
140
189
  * Plain-text rendering shared by the memory_status tool (and any other chat
141
190
  * client). Sections match the terminal /memory panel one-for-one.
@@ -145,6 +194,7 @@ export function renderMemoryReport(input: {
145
194
  store: StoreCounts;
146
195
  blocked: BlockedRow[];
147
196
  quarantined?: { ts: string; patterns: string[] }[];
197
+ capture?: CaptureHealth;
148
198
  }): string {
149
199
  const { status, store, blocked } = input;
150
200
  const out: string[] = ["MEMORY STATUS", ""];
@@ -239,6 +289,13 @@ export function renderMemoryReport(input: {
239
289
  );
240
290
  }
241
291
  if (sizing.length) out.push(` ${sizing.join(" · ")}`);
292
+ if (input.capture) {
293
+ out.push(
294
+ "",
295
+ "AUTO-CAPTURE — is it off, misconfigured, armed, or alive?",
296
+ ...captureLines(input.capture),
297
+ );
298
+ }
242
299
  out.push(
243
300
  "",
244
301
  "Terminal (CLI/TUI) users: /memory opens this as a live panel; the footer shows a one-line badge.",
@@ -10,7 +10,12 @@ import {
10
10
  quarantineReasons,
11
11
  } from "../store/quarantine/quarantine.ts";
12
12
  import { EmptyArgs, validated } from "./schema.ts";
13
- import { captureLag, captureLagLines, rowsPerDay } from "../status/metrics.ts";
13
+ import {
14
+ captureHealth,
15
+ captureLag,
16
+ captureLagLines,
17
+ rowsPerDay,
18
+ } from "../status/metrics.ts";
14
19
  import { renderMemoryReport } from "../status/report.ts";
15
20
  import { blockedRecent } from "../status/write.ts";
16
21
  import { summarizeStore } from "../status/store-summary.ts";
@@ -50,7 +55,8 @@ export const register: ToolRegister = (editor, deps) => {
50
55
  ? Math.round(store.bytes / act.length)
51
56
  : 0;
52
57
  store.rowsPerDay = rowsPerDay(act) ?? undefined;
53
- store.captureLag = captureLag(await captureLagLines());
58
+ const evidenceLines = await captureLagLines();
59
+ store.captureLag = captureLag(evidenceLines);
54
60
  return {
55
61
  content: renderMemoryReport({
56
62
  status,
@@ -59,6 +65,9 @@ export const register: ToolRegister = (editor, deps) => {
59
65
  quarantined: act
60
66
  .filter((m) => isVisibleTo(m, reportReader) && isQuarantined(m))
61
67
  .map((m) => ({ ts: m.ts, patterns: quarantineReasons(m) })),
68
+ // #138: capture state read from the same evidence window, so the
69
+ // chat surface distinguishes off / misconfigured / inert / alive.
70
+ capture: captureHealth(evidenceLines),
62
71
  }),
63
72
  };
64
73
  },