opencode-codex-memory 0.10.3 → 0.12.0

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.
@@ -1,11 +1,13 @@
1
1
  /**
2
- * Shared server↔TUI wire contract for the memory sidebar status.
2
+ * Shared server↔UI wire contract for the memory panels.
3
3
  *
4
4
  * Dependency-free on purpose: the TUI bundle runs in the CLI sandbox, where
5
5
  * only `@opencode/plugin/tui` (+ rendering peers) is guaranteed to resolve.
6
6
  * `Rpc.define` is a runtime passthrough (validation only), so a plain object
7
7
  * with JSON Schema nodes is an equivalent portable definition.
8
8
  */
9
+ import { MEMORY_NOTICE_KINDS, MEMORY_RETRY_PHASES, MEMORY_RETRY_STATES, } from "../recovery-info.js";
10
+ export { MEMORY_NOTICE_KINDS } from "../recovery-info.js";
9
11
  export const MEMORY_STATUS_ACTIVITIES = [
10
12
  "idle",
11
13
  "extracting",
@@ -27,6 +29,7 @@ const INJECTED_SCHEMA = {
27
29
  required: ["sessionTokens", "sessionRequests", "totalTokens", "totalRequests"],
28
30
  additionalProperties: false,
29
31
  };
32
+ const RETRY_PHASE_SCHEMA = { type: "string", enum: [...MEMORY_RETRY_PHASES] };
30
33
  export const MemoryStatusRpc = {
31
34
  id: "opencode-codex-memory",
32
35
  methods: {
@@ -66,6 +69,7 @@ export const MemoryStatusRpc = {
66
69
  phase2CooldownUntil: { type: ["number", "null"] },
67
70
  phase1RetryAt: { type: ["number", "null"] },
68
71
  phase2RetryAt: { type: ["number", "null"] },
72
+ phase2LeaseUntil: { type: ["number", "null"] },
69
73
  },
70
74
  required: ["version", "stage1Count", "extracting", "phase2Status", "lastError"],
71
75
  additionalProperties: false,
@@ -78,6 +82,33 @@ export const MemoryStatusRpc = {
78
82
  lastSuccessAt: { type: ["number", "null"] },
79
83
  retryAt: { type: ["number", "null"] },
80
84
  warnings: { type: "array", items: { type: "string" } },
85
+ notices: {
86
+ type: "array",
87
+ items: {
88
+ type: "object",
89
+ properties: {
90
+ kind: { type: "string", enum: [...MEMORY_NOTICE_KINDS] },
91
+ detail: { type: "string" },
92
+ version: { type: "string", enum: ["v1", "v2"] },
93
+ phase: RETRY_PHASE_SCHEMA,
94
+ sessionID: { type: "string" },
95
+ retryAt: { type: "number" },
96
+ retryPhase: RETRY_PHASE_SCHEMA,
97
+ },
98
+ required: ["kind"],
99
+ additionalProperties: false,
100
+ },
101
+ },
102
+ manualRetry: {
103
+ type: ["object", "null"],
104
+ properties: {
105
+ phase: RETRY_PHASE_SCHEMA,
106
+ state: { type: "string", enum: [...MEMORY_RETRY_STATES] },
107
+ message: { type: "string" },
108
+ },
109
+ required: ["phase", "state", "message"],
110
+ additionalProperties: false,
111
+ },
81
112
  sessionMode: { type: ["string", "null"], enum: ["enabled", "disabled", "polluted", null] },
82
113
  memoryRoot: { type: "string" },
83
114
  injected: INJECTED_SCHEMA,
@@ -147,11 +178,26 @@ export const MemoryStatusRpc = {
147
178
  input: { type: "object", properties: {}, additionalProperties: false },
148
179
  output: {
149
180
  type: "object",
150
- properties: { status: { type: "string" } },
181
+ properties: { status: { type: "string" }, message: { type: "string" } },
151
182
  required: ["status"],
152
183
  additionalProperties: false,
153
184
  },
154
185
  },
186
+ /** Try failed work with current options. Progress and outcome appear in status. */
187
+ retryFailed: {
188
+ input: {
189
+ type: "object",
190
+ properties: { phase: RETRY_PHASE_SCHEMA },
191
+ required: ["phase"],
192
+ additionalProperties: false,
193
+ },
194
+ output: {
195
+ type: "object",
196
+ properties: { ok: { type: "boolean" }, message: { type: "string" } },
197
+ required: ["ok"],
198
+ additionalProperties: false,
199
+ },
200
+ },
155
201
  /** Same wipe as the V1 memory_reset tool; the panel's confirm step is the approval. */
156
202
  resetMemory: {
157
203
  input: {
@@ -175,6 +221,9 @@ export const MemoryStatusRpc = {
175
221
  function isRecord(value) {
176
222
  return typeof value === "object" && value !== null && !Array.isArray(value);
177
223
  }
224
+ export function isMemoryRetryPhase(value) {
225
+ return value === "extraction" || value === "consolidation";
226
+ }
178
227
  /** Runtime guard for the untyped RPC client result (no zod in the TUI bundle). */
179
228
  export function isMemoryStatus(value) {
180
229
  if (!isRecord(value))
@@ -217,7 +266,7 @@ export function isMemoryStatus(value) {
217
266
  pipeline.extracting >= 0 &&
218
267
  (pipeline.phase2Status === null || typeof pipeline.phase2Status === "string") &&
219
268
  (pipeline.lastError === null || typeof pipeline.lastError === "string") &&
220
- ["phase2CooldownUntil", "phase1RetryAt", "phase2RetryAt"].every((key) => pipeline[key] === undefined ||
269
+ ["phase2CooldownUntil", "phase1RetryAt", "phase2RetryAt", "phase2LeaseUntil"].every((key) => pipeline[key] === undefined ||
221
270
  pipeline[key] === null ||
222
271
  (typeof pipeline[key] === "number" && Number.isFinite(pipeline[key])))))
223
272
  return false;
@@ -230,6 +279,25 @@ export function isMemoryStatus(value) {
230
279
  if (!Array.isArray(value.warnings) || !value.warnings.every((w) => typeof w === "string")) {
231
280
  return false;
232
281
  }
282
+ if (value.notices !== undefined &&
283
+ (!Array.isArray(value.notices) ||
284
+ !value.notices.every((notice) => isRecord(notice) &&
285
+ typeof notice.kind === "string" &&
286
+ MEMORY_NOTICE_KINDS.includes(notice.kind) &&
287
+ (notice.version === undefined || notice.version === "v1" || notice.version === "v2") &&
288
+ (notice.phase === undefined || isMemoryRetryPhase(notice.phase)) &&
289
+ (notice.detail === undefined || typeof notice.detail === "string") &&
290
+ (notice.sessionID === undefined || typeof notice.sessionID === "string") &&
291
+ (notice.retryPhase === undefined || isMemoryRetryPhase(notice.retryPhase)) &&
292
+ (notice.retryAt === undefined || (typeof notice.retryAt === "number" && Number.isFinite(notice.retryAt))))))
293
+ return false;
294
+ const retry = value.manualRetry;
295
+ if (retry != null &&
296
+ (!isRecord(retry) ||
297
+ !isMemoryRetryPhase(retry.phase) ||
298
+ typeof retry.message !== "string" ||
299
+ !MEMORY_RETRY_STATES.includes(retry.state)))
300
+ return false;
233
301
  if (value.sessionMode !== null &&
234
302
  value.sessionMode !== "enabled" &&
235
303
  value.sessionMode !== "disabled" &&
@@ -5,6 +5,8 @@ import { isPhase2InFlight } from "../phase2.js";
5
5
  import { isPluginShuttingDown } from "../lifecycle.js";
6
6
  import { activeProviderCapacityBackoffs } from "../ratelimit.js";
7
7
  import { codexInteropBlockReason } from "../codex-interop.js";
8
+ import { redact } from "../redact.js";
9
+ import { recoveryNotices } from "../recovery-status.js";
8
10
  import { injectionTotals, sessionInjection } from "./injection.js";
9
11
  import { memoryRoot } from "../paths.js";
10
12
  import { readMigrationStatus } from "../migration.js";
@@ -17,6 +19,8 @@ export function readMemoryStatus(sessionID, minConsolidatedThreads) {
17
19
  const store = withMemoryVersion(sessionVersion, () => new MemoryStore());
18
20
  const options = pluginOptions;
19
21
  const now = Date.now();
22
+ const writeVersions = writeMemoryVersions();
23
+ const consolidating = isPhase2InFlight();
20
24
  const staleBeforeSec = Math.floor(now / 1000) - options.max_rollout_age_days * 86_400;
21
25
  const snapshots = existingMemoryStores().map((pipelineStore) => ({
22
26
  version: pipelineStore.version,
@@ -24,6 +28,7 @@ export function readMemoryStatus(sessionID, minConsolidatedThreads) {
24
28
  phase1: pipelineStore.stage1JobSnapshot(staleBeforeSec),
25
29
  phase2: pipelineStore.phase2JobSnapshot(),
26
30
  }));
31
+ const activeWriters = snapshots.filter((pipeline) => writeVersions.includes(pipeline.version));
27
32
  const nextRetry = (times) => {
28
33
  const future = times.filter((time) => time != null && time * 1000 > now);
29
34
  return future.length ? Math.min(...future) * 1000 : null;
@@ -35,61 +40,77 @@ export function readMemoryStatus(sessionID, minConsolidatedThreads) {
35
40
  return {
36
41
  version: pipeline.version,
37
42
  stage1Count: pipeline.stage1Count,
38
- extracting: pipeline.phase1.by_status.running ?? 0,
43
+ extracting: pipeline.phase1.active_claims,
39
44
  phase2Status: pipeline.phase2?.status ?? null,
40
- lastError: pipeline.phase2?.last_error ?? null,
45
+ lastError: pipeline.phase2?.last_error ? redact(pipeline.phase2.last_error) : null,
41
46
  phase2CooldownUntil: cooldown !== null && cooldown > now ? cooldown : null,
42
- phase1RetryAt: nextRetry(pipeline.phase1.recent_errors
43
- .filter((error) => error.failure_class === "backoff" || error.failure_class === "provider_capacity")
44
- .map((error) => error.retry_at)),
47
+ ...(pipeline.phase2?.status === "running" && pipeline.phase2.lease_until != null
48
+ ? { phase2LeaseUntil: pipeline.phase2.lease_until * 1000 }
49
+ : {}),
50
+ phase1RetryAt: nextRetry([pipeline.phase1.next_retry_at]),
45
51
  phase2RetryAt: nextRetry([pipeline.phase2?.retry_at]),
46
52
  };
47
53
  });
48
- const active = pipelines.filter((pipeline) => writeMemoryVersions().includes(pipeline.version));
54
+ const active = pipelines.filter((pipeline) => writeVersions.includes(pipeline.version));
49
55
  const phase2 = store.phase2JobSnapshot();
50
56
  const session = sessionInjection(sessionID);
51
57
  const total = injectionTotals();
52
58
  const retryAt = nextRetry([
53
- ...activeProviderCapacityBackoffs().map((backoff) => backoff.retry_at),
59
+ ...activeProviderCapacityBackoffs(now).map((backoff) => backoff.retry_at),
54
60
  ...active.flatMap((pipeline) => [pipeline.phase1RetryAt, pipeline.phase2RetryAt].map((time) => (time === null ? null : time / 1000))),
55
61
  ]);
56
62
  const warnings = [...getConfigWarnings()];
63
+ const notices = [];
64
+ const notice = (entry) => {
65
+ notices.push(entry.detail ? { ...entry, detail: redact(entry.detail).slice(0, 400) } : entry);
66
+ };
67
+ for (const detail of warnings)
68
+ notice({ kind: "configuration", detail });
57
69
  const discoveryWarning = getV2DiscoveryStatus()?.warning;
58
- if (options.generate_memories && discoveryWarning)
70
+ if (options.generate_memories && discoveryWarning) {
59
71
  warnings.push(discoveryWarning);
72
+ notice({ kind: "discovery", detail: discoveryWarning });
73
+ }
60
74
  const health = getAgentHealth();
61
75
  if (options.generate_memories && health.observed) {
62
76
  // V2 extraction is sessionless; only the consolidator agent is used.
63
- warnings.push(...health.agents.memorize.issues.map((issue) => `memorize: ${issue}`));
77
+ for (const detail of health.agents.memorize.issues) {
78
+ warnings.push(`memorize: ${detail}`);
79
+ notice({ kind: "consolidator_setup", detail });
80
+ }
64
81
  }
65
82
  if (phase2?.last_error)
66
83
  warnings.push("Consolidation failed; see memory_inspect for details.");
67
84
  for (const pipeline of active) {
68
- if (pipeline.version !== sessionVersion && pipeline.lastError)
69
- warnings.push(`${pipeline.version}: consolidation failed; see memory_inspect.`);
85
+ const { version, lastError } = pipeline;
86
+ if (!lastError)
87
+ continue;
88
+ if (version !== sessionVersion)
89
+ warnings.push(`${version}: consolidation failed; see memory_inspect.`);
70
90
  }
71
- for (const pipeline of snapshots.filter((pipeline) => writeMemoryVersions().includes(pipeline.version))) {
72
- const phase1 = pipeline.phase1;
73
- const prefix = `${pipeline.version}: `;
74
- if (phase1.by_failure_class.backoff > 0) {
75
- const error = phase1.recent_errors.find((error) => error.failure_class === "backoff");
76
- const count = phase1.by_failure_class.backoff;
77
- warnings.push(`${prefix}Extraction retry (${count} ${count === 1 ? "job" : "jobs"})${error ? `: ${error.last_error.slice(0, 200)}` : "."}`);
91
+ for (const { version, phase1 } of activeWriters) {
92
+ const prefix = `${version}: `;
93
+ const { backoff, due, provider_capacity: capacity } = phase1.by_failure_class;
94
+ const waitingError = phase1.recent_errors.find((error) => error.failure_class === "backoff");
95
+ if (backoff > 0) {
96
+ warnings.push(`${prefix}Extraction retry (${backoff} ${backoff === 1 ? "job" : "jobs"})${waitingError ? `: ${redact(waitingError.last_error).slice(0, 200)}` : "."}`);
78
97
  }
79
- if (phase1.by_failure_class.due > 0)
98
+ if (due > 0)
80
99
  warnings.push(`${prefix}Some extraction jobs are due to retry.`);
81
- if (phase1.by_failure_class.other_exhausted > phase1.stale_exhausted) {
100
+ if (phase1.latest_exhausted)
82
101
  warnings.push(`${prefix}Some extraction jobs exhausted their retries.`);
83
- }
84
- if (phase1.by_failure_class.provider_capacity > 0)
102
+ if (capacity > 0)
85
103
  warnings.push(`${prefix}Some extraction jobs hit provider capacity limits.`);
86
104
  }
105
+ notices.push(...recoveryNotices(activeWriters, now));
87
106
  let codexImport = false;
88
107
  if (options.codex_interop.import) {
89
- for (const version of writeMemoryVersions()) {
108
+ for (const version of writeVersions) {
90
109
  const reason = withMemoryVersion(version, () => codexInteropBlockReason(options.codex_interop));
91
- if (reason)
110
+ if (reason) {
92
111
  warnings.push(`Codex import disabled (${version}): ${reason}`);
112
+ notice({ kind: "codex_import", version, detail: reason });
113
+ }
93
114
  else
94
115
  codexImport = true;
95
116
  }
@@ -97,14 +118,14 @@ export function readMemoryStatus(sessionID, minConsolidatedThreads) {
97
118
  // A `running` row this process does not own is either another opencode
98
119
  // instance's live job or an orphaned lease (e.g. a server restart killed the
99
120
  // owner mid-run). Neither is "consolidating" here; surface it instead.
100
- const foreignLease = !isPhase2InFlight() && phase2?.status === "running" && phase2.lease_until != null && phase2.lease_until * 1000 > now;
101
- if (foreignLease) {
102
- warnings.push(`Consolidation lease held by another process until ${new Date(phase2.lease_until * 1000).toLocaleTimeString()}.`);
121
+ const foreignLease = !consolidating && phase2?.status === "running" ? nextRetry([phase2.lease_until]) : null;
122
+ if (foreignLease !== null) {
123
+ warnings.push(`Consolidation lease held by another process until ${new Date(foreignLease).toLocaleTimeString()}.`);
103
124
  }
104
125
  let activity = "idle";
105
126
  if (isPluginShuttingDown())
106
127
  activity = "stopping";
107
- else if (isPhase2InFlight())
128
+ else if (consolidating)
108
129
  activity = "consolidating";
109
130
  else if (active.some((pipeline) => pipeline.extracting > 0))
110
131
  activity = "extracting";
@@ -130,6 +151,7 @@ export function readMemoryStatus(sessionID, minConsolidatedThreads) {
130
151
  lastSuccessAt: phase2?.success_finished_at != null ? phase2.success_finished_at * 1000 : null,
131
152
  retryAt,
132
153
  warnings,
154
+ notices,
133
155
  sessionMode: sessionID ? store.getMemoryMode(sessionID) : null,
134
156
  memoryRoot: memoryRoot(sessionVersion),
135
157
  injected: {
@@ -11,7 +11,7 @@
11
11
  */
12
12
  import { tool } from "@opencode-ai/plugin";
13
13
  import { memory_read, memory_search, memory_list, memory_add_note } from "../../tools/memory.js";
14
- import { memory_inspect, memory_mode } from "../../tools/control.js";
14
+ import { memory_inspect, memory_mode, memory_retry } from "../../tools/control.js";
15
15
  import { pluginOptions } from "../options.js";
16
16
  import { getV2DiscoveryStatus } from "./shim.js";
17
17
  function adaptTool(name, v1, ask) {
@@ -67,8 +67,9 @@ const ALL_MEMORY_TOOLS = [
67
67
  ["memory_add_note", memory_add_note],
68
68
  ["memory_inspect", memory_inspect],
69
69
  ["memory_mode", memory_mode],
70
+ ["memory_retry", memory_retry],
70
71
  ];
71
- const CONTROL_ONLY = new Set(["memory_inspect", "memory_mode"]);
72
+ const CONTROL_ONLY = new Set(["memory_inspect", "memory_mode", "memory_retry"]);
72
73
  /**
73
74
  * Same gating as the V1 entry: the read/search/list/add-note tools require
74
75
  * BOTH use_memories and dedicated_tools (codex MemoriesExtension); the