opencode-codex-memory 0.11.0 → 0.12.1
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 -2
- package/dist/gui-experimental.cjs +1 -1
- package/dist/src/capture.d.ts +2 -2
- package/dist/src/capture.js +4 -4
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.js +7 -2
- package/dist/src/phase1.d.ts +11 -1
- package/dist/src/phase1.js +37 -21
- package/dist/src/phase2.d.ts +2 -0
- package/dist/src/phase2.js +1 -1
- package/dist/src/pipeline.d.ts +4 -1
- package/dist/src/pipeline.js +42 -25
- package/dist/src/recovery-info.d.ts +30 -0
- package/dist/src/recovery-info.js +33 -0
- package/dist/src/recovery-status.d.ts +10 -0
- package/dist/src/recovery-status.js +54 -0
- package/dist/src/retry.d.ts +14 -0
- package/dist/src/retry.js +147 -0
- package/dist/src/store.d.ts +11 -3
- package/dist/src/store.js +72 -17
- package/dist/src/v2/panel-messages.d.ts +53 -0
- package/dist/src/v2/panel-messages.js +223 -0
- package/dist/src/v2/panel-status.d.ts +7 -4
- package/dist/src/v2/panel-status.js +31 -13
- package/dist/src/v2/plugin.js +21 -2
- package/dist/src/v2/status-rpc.d.ts +94 -0
- package/dist/src/v2/status-rpc.js +70 -2
- package/dist/src/v2/status.js +50 -28
- package/dist/src/v2/tools.js +3 -2
- package/dist/src/v2/tui.js +611 -108
- package/dist/tools/control.d.ts +1 -0
- package/dist/tools/control.js +81 -4
- package/package.json +10 -9
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { pluginOptions } from "./options.js";
|
|
2
|
+
import { isProviderCapacityBlocked, isProviderCapacityError } from "./ratelimit.js";
|
|
3
|
+
import { redact } from "./redact.js";
|
|
4
|
+
import { SHUTDOWN_JOB_ERROR } from "./store.js";
|
|
5
|
+
/** Recovery advice is based on active writers, regardless of the current session's read version. */
|
|
6
|
+
export function recoveryNotices(writers, now) {
|
|
7
|
+
if (!pluginOptions.generate_memories)
|
|
8
|
+
return [];
|
|
9
|
+
const extractionBlocked = isProviderCapacityBlocked("phase1", pluginOptions.extract_model, now);
|
|
10
|
+
const consolidationBlocked = isProviderCapacityBlocked("phase2", pluginOptions.consolidation_model, now);
|
|
11
|
+
const notices = [];
|
|
12
|
+
const add = (entry) => notices.push(entry.detail ? { ...entry, detail: redact(entry.detail).slice(0, 400) } : entry);
|
|
13
|
+
const retryAt = (unixSeconds) => unixSeconds != null && unixSeconds * 1000 > now ? unixSeconds * 1000 : undefined;
|
|
14
|
+
const failure = (phase, version, error, time) => {
|
|
15
|
+
const capacity = isProviderCapacityError(error);
|
|
16
|
+
const blocked = phase === "extraction" ? extractionBlocked : consolidationBlocked;
|
|
17
|
+
add({
|
|
18
|
+
kind: capacity ? "provider_capacity" : phase === "extraction" ? "extraction_retry" : "consolidation_failed",
|
|
19
|
+
phase,
|
|
20
|
+
version,
|
|
21
|
+
...(error ? { detail: error } : {}),
|
|
22
|
+
...(time !== undefined ? { retryAt: time } : {}),
|
|
23
|
+
...(!blocked && (!capacity || time === undefined) ? { retryPhase: phase } : {}),
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
for (const { version, phase1, phase2 } of writers) {
|
|
27
|
+
if (phase2?.last_error && phase2.last_error !== SHUTDOWN_JOB_ERROR)
|
|
28
|
+
failure("consolidation", version, phase2.last_error, retryAt(phase2.retry_at));
|
|
29
|
+
const exhausted = phase1.latest_exhausted;
|
|
30
|
+
if (exhausted)
|
|
31
|
+
add({
|
|
32
|
+
kind: "extraction_exhausted",
|
|
33
|
+
phase: "extraction",
|
|
34
|
+
version,
|
|
35
|
+
detail: exhausted.last_error,
|
|
36
|
+
sessionID: exhausted.session_id,
|
|
37
|
+
...(!extractionBlocked ? { retryPhase: "extraction" } : {}),
|
|
38
|
+
});
|
|
39
|
+
const { backoff, due } = phase1.by_failure_class;
|
|
40
|
+
if (backoff || due) {
|
|
41
|
+
const error = phase1.recent_errors.find((entry) => entry.failure_class === (due ? "due" : "backoff"));
|
|
42
|
+
failure("extraction", version, error?.last_error, due ? undefined : retryAt(phase1.next_retry_at));
|
|
43
|
+
}
|
|
44
|
+
const legacyCapacity = phase1.recent_errors.find((error) => error.failure_class === "provider_capacity");
|
|
45
|
+
if (legacyCapacity)
|
|
46
|
+
failure("extraction", version, legacyCapacity.last_error, retryAt(legacyCapacity.retry_at));
|
|
47
|
+
}
|
|
48
|
+
if (extractionBlocked && !notices.some((entry) => entry.kind === "provider_capacity" && entry.phase === "extraction"))
|
|
49
|
+
add({ kind: "provider_capacity", phase: "extraction" });
|
|
50
|
+
if (consolidationBlocked &&
|
|
51
|
+
!notices.some((entry) => entry.kind === "provider_capacity" && entry.phase === "consolidation"))
|
|
52
|
+
add({ kind: "provider_capacity", phase: "consolidation" });
|
|
53
|
+
return notices;
|
|
54
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { MemoryRetry, MemoryRetryPhase } from "./recovery-info.js";
|
|
2
|
+
export declare function memoryRetryStatus(maxAgeMs?: number): MemoryRetry | null;
|
|
3
|
+
export declare function memoryRetryBlockedReason(): string | undefined;
|
|
4
|
+
export declare function onMemoryRetryChanged(listener: () => void): () => void;
|
|
5
|
+
export declare function clearMemoryRetryResult(): void;
|
|
6
|
+
export declare function waitForMemoryRetry(): Promise<void>;
|
|
7
|
+
/** Same process-wide retry for panel actions and tools. Callers may await the outcome or read status. */
|
|
8
|
+
export declare function requestMemoryRetry(phase: MemoryRetryPhase, excludeSession?: string): {
|
|
9
|
+
ok: false;
|
|
10
|
+
message: string;
|
|
11
|
+
} | {
|
|
12
|
+
ok: true;
|
|
13
|
+
completion: Promise<MemoryRetry>;
|
|
14
|
+
};
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { isMemoryWorkInFlight, retryFailedExtractions, runMemoryConsolidation } from "./pipeline.js";
|
|
2
|
+
import { isPluginShuttingDown } from "./lifecycle.js";
|
|
3
|
+
import { pluginOptions } from "./options.js";
|
|
4
|
+
import { recordDiagnostic } from "./diagnostics.js";
|
|
5
|
+
import { existingMemoryStores } from "./store.js";
|
|
6
|
+
const capacity = {
|
|
7
|
+
state: "blocked",
|
|
8
|
+
message: "A background model is still rate-limited. Wait for the limit to clear. Backup models can help on future runs.",
|
|
9
|
+
};
|
|
10
|
+
const running = {
|
|
11
|
+
state: "blocked",
|
|
12
|
+
message: "Memory work is already running. Try again when it finishes.",
|
|
13
|
+
};
|
|
14
|
+
async function retryExtraction(excludeSession) {
|
|
15
|
+
const passes = await retryFailedExtractions(excludeSession);
|
|
16
|
+
const completed = passes.filter((pass) => pass.status === "completed");
|
|
17
|
+
const saved = completed.reduce((total, pass) => total + pass.saved, 0);
|
|
18
|
+
const noOutput = completed.reduce((total, pass) => total + pass.noOutput, 0);
|
|
19
|
+
const failed = completed.reduce((total, pass) => total + pass.failed, 0);
|
|
20
|
+
const interrupted = completed.some((pass) => pass.skipped > 0) || passes.some((pass) => pass.status === "shutting_down");
|
|
21
|
+
const passFailed = passes.some((pass) => pass.status === "failed");
|
|
22
|
+
const savedMessage = saved ? `Saved ${saved} ${saved === 1 ? "recap" : "recaps"} for consolidation. ` : "";
|
|
23
|
+
if (failed || passFailed) {
|
|
24
|
+
return {
|
|
25
|
+
state: "failed",
|
|
26
|
+
message: `${savedMessage}Some attempts still failed. Check the latest diagnostics in the panel or memory_inspect.`,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
if (passes.some((pass) => pass.status === "discovery_failed"))
|
|
30
|
+
return {
|
|
31
|
+
state: "blocked",
|
|
32
|
+
message: savedMessage + "Couldn’t list conversations. Check the OpenCode service, then retry.",
|
|
33
|
+
};
|
|
34
|
+
if (passes.some((pass) => pass.status === "provider_capacity"))
|
|
35
|
+
return { state: "blocked", message: savedMessage + capacity.message };
|
|
36
|
+
if (passes.some((pass) => pass.status === "already_running"))
|
|
37
|
+
return { state: "blocked", message: savedMessage + running.message };
|
|
38
|
+
if (interrupted)
|
|
39
|
+
return { state: "blocked", message: savedMessage + "The retry was interrupted. Try again when OpenCode is ready." };
|
|
40
|
+
if (saved || noOutput) {
|
|
41
|
+
return {
|
|
42
|
+
state: "completed",
|
|
43
|
+
message: saved
|
|
44
|
+
? `${savedMessage}Memory will update on the next eligible consolidation.`
|
|
45
|
+
: "Extraction finished. There was no new memory to save.",
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
state: "blocked",
|
|
50
|
+
message: "No failed sessions were eligible. Check learning preferences, idle and age limits, and provider backoff.",
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
async function retryConsolidation() {
|
|
54
|
+
const results = await runMemoryConsolidation(true);
|
|
55
|
+
const succeeded = results.some((status) => status === "succeeded" || status === "no_workspace_changes");
|
|
56
|
+
const remains = results.filter((status) => !["succeeded", "no_workspace_changes", "skipped_no_failure"].includes(status));
|
|
57
|
+
if (!remains.length)
|
|
58
|
+
return {
|
|
59
|
+
state: succeeded ? "completed" : "blocked",
|
|
60
|
+
message: succeeded
|
|
61
|
+
? "Consolidation finished. Memory is ready to use."
|
|
62
|
+
: "No failed consolidation is left to retry.",
|
|
63
|
+
};
|
|
64
|
+
const prefix = succeeded ? "Consolidation finished for one memory version. " : "";
|
|
65
|
+
if (remains.every((status) => status === "skipped_rate_limit" || status === "skipped_retry_unavailable"))
|
|
66
|
+
return { state: "blocked", message: prefix + capacity.message };
|
|
67
|
+
if (remains.every((status) => status === "already_running" || status === "skipped_running"))
|
|
68
|
+
return { state: "blocked", message: prefix + running.message };
|
|
69
|
+
if (remains.every((status) => status === "shutting_down"))
|
|
70
|
+
return { state: "blocked", message: prefix + "The retry was interrupted. Try again when OpenCode is ready." };
|
|
71
|
+
if (remains.every((status) => status === "generation_disabled"))
|
|
72
|
+
return { state: "blocked", message: prefix + "Turn on Learn from sessions first." };
|
|
73
|
+
return {
|
|
74
|
+
state: "failed",
|
|
75
|
+
message: prefix + "Consolidation still failed. Check the latest error before retrying again.",
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
const listeners = new Set();
|
|
79
|
+
let activeRetry;
|
|
80
|
+
let latestRetry = null;
|
|
81
|
+
let finishedAt;
|
|
82
|
+
export function memoryRetryStatus(maxAgeMs = Infinity) {
|
|
83
|
+
if (!latestRetry || (finishedAt !== undefined && Date.now() - finishedAt >= maxAgeMs))
|
|
84
|
+
return null;
|
|
85
|
+
return { ...latestRetry };
|
|
86
|
+
}
|
|
87
|
+
export function memoryRetryBlockedReason() {
|
|
88
|
+
if (isPluginShuttingDown())
|
|
89
|
+
return "Memory is stopping. Try again when OpenCode is ready.";
|
|
90
|
+
if (!pluginOptions.generate_memories)
|
|
91
|
+
return "Turn on Learn from sessions first.";
|
|
92
|
+
if (activeRetry || isMemoryWorkInFlight() || existingMemoryStores().some((store) => store.hasActiveJobLease()))
|
|
93
|
+
return running.message;
|
|
94
|
+
}
|
|
95
|
+
export function onMemoryRetryChanged(listener) {
|
|
96
|
+
listeners.add(listener);
|
|
97
|
+
return () => listeners.delete(listener);
|
|
98
|
+
}
|
|
99
|
+
function notify() {
|
|
100
|
+
for (const listener of listeners) {
|
|
101
|
+
try {
|
|
102
|
+
listener();
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
recordDiagnostic("warn", "manual-retry", `Status notification failed: ${String(error)}`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
export function clearMemoryRetryResult() {
|
|
110
|
+
if (activeRetry)
|
|
111
|
+
return;
|
|
112
|
+
latestRetry = null;
|
|
113
|
+
finishedAt = undefined;
|
|
114
|
+
}
|
|
115
|
+
export async function waitForMemoryRetry() {
|
|
116
|
+
await activeRetry;
|
|
117
|
+
}
|
|
118
|
+
/** Same process-wide retry for panel actions and tools. Callers may await the outcome or read status. */
|
|
119
|
+
export function requestMemoryRetry(phase, excludeSession) {
|
|
120
|
+
const message = memoryRetryBlockedReason();
|
|
121
|
+
if (message)
|
|
122
|
+
return { ok: false, message };
|
|
123
|
+
latestRetry = {
|
|
124
|
+
phase,
|
|
125
|
+
state: "running",
|
|
126
|
+
message: phase === "extraction"
|
|
127
|
+
? "Retrying a batch of failed sessions with the current background models."
|
|
128
|
+
: "Retrying the failed memory update with the current background models.",
|
|
129
|
+
};
|
|
130
|
+
finishedAt = undefined;
|
|
131
|
+
const completion = (phase === "extraction" ? retryExtraction(excludeSession) : retryConsolidation())
|
|
132
|
+
.catch((error) => {
|
|
133
|
+
recordDiagnostic("error", "manual-retry", String(error));
|
|
134
|
+
return { state: "failed", message: "The retry could not finish. Check memory_inspect for details." };
|
|
135
|
+
})
|
|
136
|
+
.then((outcome) => {
|
|
137
|
+
const result = { phase, ...outcome };
|
|
138
|
+
latestRetry = result;
|
|
139
|
+
finishedAt = Date.now();
|
|
140
|
+
activeRetry = undefined;
|
|
141
|
+
notify();
|
|
142
|
+
return result;
|
|
143
|
+
});
|
|
144
|
+
activeRetry = completion;
|
|
145
|
+
notify();
|
|
146
|
+
return { ok: true, completion };
|
|
147
|
+
}
|
package/dist/src/store.d.ts
CHANGED
|
@@ -39,7 +39,10 @@ export type Phase2ClaimResult = {
|
|
|
39
39
|
type: "skipped_running";
|
|
40
40
|
} | {
|
|
41
41
|
type: "skipped_retry_unavailable";
|
|
42
|
+
} | {
|
|
43
|
+
type: "skipped_no_failure";
|
|
42
44
|
};
|
|
45
|
+
export declare const SHUTDOWN_JOB_ERROR = "plugin shutting down";
|
|
43
46
|
/** Wipe jobs/outputs in every versioned DB. Leaves memory_session_meta on memory.db. */
|
|
44
47
|
export declare function clearAllVersionMemoryData(wipeWorkspace: () => void): void;
|
|
45
48
|
/** Include inactive existing stores for deletion, reset guards and diagnostics. */
|
|
@@ -52,6 +55,7 @@ export declare class MemoryStore {
|
|
|
52
55
|
constructor(db?: Database, meta?: Database, version?: import("./options.js").MemoryVersion);
|
|
53
56
|
stage1Outputs(): Stage1Output[];
|
|
54
57
|
stage1OutputCount(): number;
|
|
58
|
+
hasActiveJobLease(): boolean;
|
|
55
59
|
hasStage1Output(sessionId: string): boolean;
|
|
56
60
|
/**
|
|
57
61
|
* Deletes stale rows; snapshots consumed by the last successful Phase 2 are
|
|
@@ -66,10 +70,10 @@ export declare class MemoryStore {
|
|
|
66
70
|
* context calls, and either observation may happen after a process restart.
|
|
67
71
|
*/
|
|
68
72
|
recordUsageOnce(sessionId: string, assistantMessageId: string, citedSessionIds: string[]): string[];
|
|
69
|
-
claimStage1Jobs(sessions: ClaimableSession[], excludeSession?: string, maxClaimed?: number): Stage1Claim[];
|
|
70
|
-
markStage1Succeeded(sessionId: string, ownershipToken: string, out: Omit<Stage1Output, "usage_count" | "last_usage">):
|
|
73
|
+
claimStage1Jobs(sessions: ClaimableSession[], excludeSession?: string, maxClaimed?: number, retryFailed?: boolean): Stage1Claim[];
|
|
74
|
+
markStage1Succeeded(sessionId: string, ownershipToken: string, out: Omit<Stage1Output, "usage_count" | "last_usage">): boolean;
|
|
71
75
|
/** Extraction succeeded but produced nothing worth keeping: finish the job and drop any stale output. */
|
|
72
|
-
markStage1SucceededNoOutput(sessionId: string, ownershipToken: string, sourceUpdatedAt: number):
|
|
76
|
+
markStage1SucceededNoOutput(sessionId: string, ownershipToken: string, sourceUpdatedAt: number): boolean;
|
|
73
77
|
markStage1Failed(sessionId: string, ownershipToken: string, error: unknown): void;
|
|
74
78
|
/**
|
|
75
79
|
* Re-open stage-1 jobs that exhausted their retry budget solely because of
|
|
@@ -91,6 +95,7 @@ export declare class MemoryStore {
|
|
|
91
95
|
private enqueueGlobalConsolidation;
|
|
92
96
|
claimGlobalPhase2Job(opts?: {
|
|
93
97
|
bypassCooldown?: boolean;
|
|
98
|
+
retryFailed?: boolean;
|
|
94
99
|
}): Phase2ClaimResult;
|
|
95
100
|
private claimPhase2;
|
|
96
101
|
heartbeatPhase2Job(ownershipToken: string): boolean;
|
|
@@ -197,6 +202,9 @@ export declare class MemoryStore {
|
|
|
197
202
|
};
|
|
198
203
|
stale_exhausted: number;
|
|
199
204
|
recent_errors: Stage1RecentError[];
|
|
205
|
+
latest_exhausted: Stage1RecentError | null;
|
|
206
|
+
next_retry_at: number | null;
|
|
207
|
+
active_claims: number;
|
|
200
208
|
};
|
|
201
209
|
}
|
|
202
210
|
export type Stage1FailureClass = "backoff" | "provider_capacity" | "other_exhausted" | "due";
|
package/dist/src/store.js
CHANGED
|
@@ -13,6 +13,7 @@ export const PHASE2_COOLDOWN_MS = 6 * 60 * 60 * 1000;
|
|
|
13
13
|
export const STAGE1_CONCURRENCY = 8;
|
|
14
14
|
export const SCAN_LIMIT = 5000;
|
|
15
15
|
export const PRUNE_BATCH_SIZE = 200;
|
|
16
|
+
export const SHUTDOWN_JOB_ERROR = "plugin shutting down";
|
|
16
17
|
function clearJobTables(db) {
|
|
17
18
|
db.transaction(() => {
|
|
18
19
|
db.run("DELETE FROM memory_stage1_outputs");
|
|
@@ -104,6 +105,11 @@ export class MemoryStore {
|
|
|
104
105
|
stage1OutputCount() {
|
|
105
106
|
return this.db.prepare("SELECT COUNT(*) AS count FROM memory_stage1_outputs").get().count;
|
|
106
107
|
}
|
|
108
|
+
hasActiveJobLease() {
|
|
109
|
+
return (this.db
|
|
110
|
+
.query("SELECT 1 FROM memory_jobs WHERE status='running' AND (lease_until IS NULL OR lease_until > ?) LIMIT 1")
|
|
111
|
+
.get(nowSec()) !== null);
|
|
112
|
+
}
|
|
107
113
|
hasStage1Output(sessionId) {
|
|
108
114
|
return this.db.prepare("SELECT 1 FROM memory_stage1_outputs WHERE session_id = ?").get(sessionId) !== null;
|
|
109
115
|
}
|
|
@@ -204,18 +210,38 @@ export class MemoryStore {
|
|
|
204
210
|
.immediate();
|
|
205
211
|
return fresh;
|
|
206
212
|
}
|
|
207
|
-
claimStage1Jobs(sessions, excludeSession, maxClaimed) {
|
|
213
|
+
claimStage1Jobs(sessions, excludeSession, maxClaimed, retryFailed = false) {
|
|
208
214
|
const workerId = newId();
|
|
209
215
|
// Cap per-pass claims at codex's max_rollouts_per_startup (max_claimed,
|
|
210
216
|
// default 2, clamp 1-128). codex also uses max_claimed as the
|
|
211
217
|
// cross-process running-jobs cap; execution concurrency is limited
|
|
212
218
|
// separately (STAGE1_CONCURRENCY, codex buffer_unordered(8)).
|
|
213
219
|
const claimCap = Math.max(1, maxClaimed ?? 2);
|
|
220
|
+
const manual = retryFailed ? 1 : 0;
|
|
214
221
|
const claimed = [];
|
|
222
|
+
const failedJobs = `SELECT job_key, last_error, retry_at FROM memory_jobs
|
|
223
|
+
WHERE kind='memory_stage1' AND status IN ('failed', 'pending') AND last_error IS NOT NULL AND last_error != ?`;
|
|
224
|
+
const failure = retryFailed
|
|
225
|
+
? this.db.query(`${failedJobs} AND job_key = ?`)
|
|
226
|
+
: null;
|
|
227
|
+
const failedIDs = retryFailed
|
|
228
|
+
? new Set(this.db
|
|
229
|
+
.query(failedJobs)
|
|
230
|
+
.all(SHUTDOWN_JOB_ERROR)
|
|
231
|
+
.map((row) => row.job_key))
|
|
232
|
+
: null;
|
|
215
233
|
const claimOne = this.db.transaction((s, ownershipToken, lease) => {
|
|
234
|
+
const startedAt = nowSec();
|
|
235
|
+
if (failure) {
|
|
236
|
+
const row = failure.get(SHUTDOWN_JOB_ERROR, s.id);
|
|
237
|
+
if (!row || (row.retry_at != null && row.retry_at > startedAt && isProviderCapacityError(row.last_error)))
|
|
238
|
+
return false;
|
|
239
|
+
if (this.getMemoryMode(s.id) === "disabled" || this.isPolluted(s.id))
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
216
242
|
const activeRow = this.db
|
|
217
243
|
.prepare("SELECT COUNT(*) AS c FROM memory_jobs WHERE kind='memory_stage1' AND status='running' AND (lease_until IS NULL OR lease_until > ?)")
|
|
218
|
-
.get(
|
|
244
|
+
.get(startedAt);
|
|
219
245
|
if (activeRow.c >= claimCap)
|
|
220
246
|
return false;
|
|
221
247
|
// Mirrors codex try_claim_stage1_job: a newer input watermark (session
|
|
@@ -237,23 +263,24 @@ export class MemoryStore {
|
|
|
237
263
|
last_error = NULL,
|
|
238
264
|
retry_remaining = CASE
|
|
239
265
|
WHEN excluded.input_watermark > COALESCE(memory_jobs.input_watermark, -1) THEN excluded.retry_remaining
|
|
266
|
+
WHEN ? THEN MAX(memory_jobs.retry_remaining, 1)
|
|
240
267
|
ELSE memory_jobs.retry_remaining
|
|
241
268
|
END,
|
|
242
269
|
input_watermark = excluded.input_watermark
|
|
243
270
|
WHERE (memory_jobs.status != 'running' OR memory_jobs.lease_until IS NULL OR memory_jobs.lease_until <= excluded.started_at)
|
|
244
|
-
|
|
271
|
+
AND (? OR memory_jobs.retry_at IS NULL
|
|
245
272
|
OR memory_jobs.retry_at <= excluded.started_at
|
|
246
273
|
OR excluded.input_watermark > COALESCE(memory_jobs.input_watermark, -1))
|
|
247
|
-
|
|
274
|
+
AND (? OR memory_jobs.retry_remaining > 0
|
|
248
275
|
OR excluded.input_watermark > COALESCE(memory_jobs.input_watermark, -1))
|
|
249
276
|
AND (memory_jobs.status != 'done'
|
|
250
277
|
OR memory_jobs.last_success_watermark IS NULL
|
|
251
278
|
OR memory_jobs.last_success_watermark < excluded.input_watermark)`)
|
|
252
|
-
.run(s.id, workerId, ownershipToken,
|
|
279
|
+
.run(s.id, workerId, ownershipToken, startedAt, lease, DEFAULT_RETRY_REMAINING, s.updated_at, manual, manual, manual);
|
|
253
280
|
return result.changes > 0;
|
|
254
281
|
});
|
|
255
282
|
for (const s of sessions) {
|
|
256
|
-
if (s.id === excludeSession)
|
|
283
|
+
if (s.id === excludeSession || (failedIDs && !failedIDs.has(s.id)))
|
|
257
284
|
continue;
|
|
258
285
|
if (claimed.length >= claimCap)
|
|
259
286
|
break;
|
|
@@ -267,7 +294,7 @@ export class MemoryStore {
|
|
|
267
294
|
return claimed;
|
|
268
295
|
}
|
|
269
296
|
markStage1Succeeded(sessionId, ownershipToken, out) {
|
|
270
|
-
this.db
|
|
297
|
+
return this.db
|
|
271
298
|
.transaction(() => {
|
|
272
299
|
const res = this.db
|
|
273
300
|
.prepare(`UPDATE memory_jobs SET status='done', finished_at=?, lease_until=NULL, last_error=NULL,
|
|
@@ -280,12 +307,13 @@ export class MemoryStore {
|
|
|
280
307
|
this.upsertStage1Output(out);
|
|
281
308
|
this.enqueueGlobalConsolidation(out.source_updated_at);
|
|
282
309
|
}
|
|
310
|
+
return res.changes > 0;
|
|
283
311
|
})
|
|
284
312
|
.immediate();
|
|
285
313
|
}
|
|
286
314
|
/** Extraction succeeded but produced nothing worth keeping: finish the job and drop any stale output. */
|
|
287
315
|
markStage1SucceededNoOutput(sessionId, ownershipToken, sourceUpdatedAt) {
|
|
288
|
-
this.db
|
|
316
|
+
return this.db
|
|
289
317
|
.transaction(() => {
|
|
290
318
|
const res = this.db
|
|
291
319
|
.prepare(`UPDATE memory_jobs SET status='done', finished_at=?, lease_until=NULL, last_error=NULL,
|
|
@@ -293,10 +321,11 @@ export class MemoryStore {
|
|
|
293
321
|
WHERE kind='memory_stage1' AND job_key=? AND status='running' AND ownership_token=?`)
|
|
294
322
|
.run(nowSec(), sourceUpdatedAt, sessionId, ownershipToken);
|
|
295
323
|
if (res.changes === 0)
|
|
296
|
-
return;
|
|
324
|
+
return false;
|
|
297
325
|
const deleted = this.db.prepare("DELETE FROM memory_stage1_outputs WHERE session_id = ?").run(sessionId);
|
|
298
326
|
if (deleted.changes > 0)
|
|
299
327
|
this.enqueueGlobalConsolidation(sourceUpdatedAt);
|
|
328
|
+
return true;
|
|
300
329
|
})
|
|
301
330
|
.immediate();
|
|
302
331
|
}
|
|
@@ -370,7 +399,7 @@ export class MemoryStore {
|
|
|
370
399
|
finished_at = ?,
|
|
371
400
|
lease_until = NULL
|
|
372
401
|
WHERE kind='memory_stage1' AND job_key=? AND status='running' AND ownership_token=?`)
|
|
373
|
-
.run(
|
|
402
|
+
.run(SHUTDOWN_JOB_ERROR, nowSec(), sessionId, ownershipToken);
|
|
374
403
|
}
|
|
375
404
|
/**
|
|
376
405
|
* Enqueues global consolidation after stage-1 state changes. If phase 2 is
|
|
@@ -416,6 +445,8 @@ export class MemoryStore {
|
|
|
416
445
|
.prepare("SELECT * FROM memory_jobs WHERE kind='memory_consolidate_global' AND job_key='global'")
|
|
417
446
|
.get();
|
|
418
447
|
if (!row) {
|
|
448
|
+
if (opts.retryFailed)
|
|
449
|
+
return { type: "skipped_no_failure" };
|
|
419
450
|
this.db
|
|
420
451
|
.prepare(`INSERT INTO memory_jobs
|
|
421
452
|
(kind, job_key, status, worker_id, ownership_token, started_at, lease_until, retry_remaining)
|
|
@@ -423,9 +454,14 @@ export class MemoryStore {
|
|
|
423
454
|
.run(workerId, ownershipToken, tNow, lease, DEFAULT_RETRY_REMAINING);
|
|
424
455
|
return { type: "claimed", workerId, ownershipToken };
|
|
425
456
|
}
|
|
426
|
-
if (row.status === "running" && row.lease_until
|
|
457
|
+
if (row.status === "running" && (row.lease_until == null ? opts.retryFailed : row.lease_until > tNow)) {
|
|
427
458
|
return { type: "skipped_running" };
|
|
428
459
|
}
|
|
460
|
+
if (opts.retryFailed &&
|
|
461
|
+
((row.status !== "failed" && row.status !== "pending") ||
|
|
462
|
+
!row.last_error ||
|
|
463
|
+
row.last_error === SHUTDOWN_JOB_ERROR))
|
|
464
|
+
return { type: "skipped_no_failure" };
|
|
429
465
|
// codex: cooldown after a clean success (last_error IS NULL AND
|
|
430
466
|
// finished_at within the window); failures fall through to retry_at.
|
|
431
467
|
if (!opts.bypassCooldown &&
|
|
@@ -436,7 +472,9 @@ export class MemoryStore {
|
|
|
436
472
|
}
|
|
437
473
|
// codex gates on retry_at regardless of status and never exhausts
|
|
438
474
|
// phase-2 retries; retry_remaining is informational only.
|
|
439
|
-
if (row.retry_at != null &&
|
|
475
|
+
if (row.retry_at != null &&
|
|
476
|
+
row.retry_at > tNow &&
|
|
477
|
+
(!opts.retryFailed || isProviderCapacityError(row.last_error))) {
|
|
440
478
|
return { type: "skipped_retry_unavailable" };
|
|
441
479
|
}
|
|
442
480
|
this.db
|
|
@@ -622,7 +660,7 @@ export class MemoryStore {
|
|
|
622
660
|
finished_at = ?,
|
|
623
661
|
lease_until = NULL
|
|
624
662
|
WHERE kind='memory_consolidate_global' AND job_key='global' AND ownership_token=? AND status='running'`)
|
|
625
|
-
.run(
|
|
663
|
+
.run(SHUTDOWN_JOB_ERROR, nowSec(), ownershipToken);
|
|
626
664
|
}
|
|
627
665
|
/**
|
|
628
666
|
* Phase 2 input set, mirroring codex get_phase2_input_selection:
|
|
@@ -760,13 +798,14 @@ export class MemoryStore {
|
|
|
760
798
|
* versions do not bury live due/backoff failures.
|
|
761
799
|
*/
|
|
762
800
|
stage1JobSnapshot(staleBeforeSec) {
|
|
801
|
+
const tNow = nowSec();
|
|
763
802
|
const rows = this.db
|
|
764
|
-
.prepare(
|
|
765
|
-
|
|
803
|
+
.prepare(`SELECT status, COUNT(*) AS c, COUNT(CASE WHEN lease_until IS NULL OR lease_until > ? THEN 1 END) AS active
|
|
804
|
+
FROM memory_jobs WHERE kind='memory_stage1' GROUP BY status`)
|
|
805
|
+
.all(tNow);
|
|
766
806
|
const by_status = {};
|
|
767
807
|
for (const r of rows)
|
|
768
808
|
by_status[r.status] = r.c;
|
|
769
|
-
const tNow = nowSec();
|
|
770
809
|
const errorRows = this.db
|
|
771
810
|
.prepare(`SELECT job_key AS session_id, last_error, retry_at, status, retry_remaining, finished_at FROM memory_jobs
|
|
772
811
|
WHERE kind='memory_stage1' AND last_error IS NOT NULL
|
|
@@ -775,6 +814,7 @@ export class MemoryStore {
|
|
|
775
814
|
const by_failure_class = { backoff: 0, provider_capacity: 0, other_exhausted: 0, due: 0 };
|
|
776
815
|
const live = [];
|
|
777
816
|
let stale_exhausted = 0;
|
|
817
|
+
let nextRetryAt = null;
|
|
778
818
|
for (const row of errorRows) {
|
|
779
819
|
const failure_class = classifyStage1Failure(row, tNow);
|
|
780
820
|
if (failure_class)
|
|
@@ -791,10 +831,25 @@ export class MemoryStore {
|
|
|
791
831
|
stale_exhausted++;
|
|
792
832
|
continue;
|
|
793
833
|
}
|
|
834
|
+
if ((failure_class === "backoff" || failure_class === "provider_capacity") &&
|
|
835
|
+
row.retry_at != null &&
|
|
836
|
+
row.retry_at > tNow &&
|
|
837
|
+
(nextRetryAt === null || row.retry_at < nextRetryAt)) {
|
|
838
|
+
nextRetryAt = row.retry_at;
|
|
839
|
+
}
|
|
794
840
|
live.push(entry);
|
|
795
841
|
}
|
|
796
842
|
live.sort((a, b) => liveErrorRank(a.failure_class) - liveErrorRank(b.failure_class));
|
|
797
|
-
return {
|
|
843
|
+
return {
|
|
844
|
+
by_status,
|
|
845
|
+
active_claims: rows.find((row) => row.status === "running")?.active ?? 0,
|
|
846
|
+
by_failure_class,
|
|
847
|
+
stale_exhausted,
|
|
848
|
+
recent_errors: live.slice(0, 5),
|
|
849
|
+
// Due jobs can fill recent_errors; the panel still needs an affected session.
|
|
850
|
+
latest_exhausted: live.find((error) => error.failure_class === "other_exhausted") ?? null,
|
|
851
|
+
next_retry_at: nextRetryAt,
|
|
852
|
+
};
|
|
798
853
|
}
|
|
799
854
|
}
|
|
800
855
|
function liveErrorRank(klass) {
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { type MemoryNotice, type MemoryRetryPhase, type MemoryStatus } from "./status-rpc.js";
|
|
2
|
+
export declare const memoryHelp: {
|
|
3
|
+
readonly models: {
|
|
4
|
+
readonly label: "Background model setup";
|
|
5
|
+
readonly path: "configuration.md#choose-background-models";
|
|
6
|
+
};
|
|
7
|
+
readonly providers: {
|
|
8
|
+
readonly label: "Open provider settings";
|
|
9
|
+
readonly path: null;
|
|
10
|
+
};
|
|
11
|
+
readonly backup: {
|
|
12
|
+
readonly label: "Set up backup models";
|
|
13
|
+
readonly path: "configuration.md#backup-models";
|
|
14
|
+
};
|
|
15
|
+
readonly configuration: {
|
|
16
|
+
readonly label: "Memory configuration";
|
|
17
|
+
readonly path: "configuration.md#where-to-put-options";
|
|
18
|
+
};
|
|
19
|
+
readonly service: {
|
|
20
|
+
readonly label: "Check the OpenCode service";
|
|
21
|
+
readonly path: "troubleshooting.md#check-opencodes-server";
|
|
22
|
+
};
|
|
23
|
+
readonly troubleshooting: {
|
|
24
|
+
readonly label: "Troubleshooting";
|
|
25
|
+
readonly path: "troubleshooting.md#common-symptoms";
|
|
26
|
+
};
|
|
27
|
+
readonly codex: {
|
|
28
|
+
readonly label: "Codex import setup";
|
|
29
|
+
readonly path: "integrations.md#sharing-memory-with-the-codex-cli";
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
export declare function memoryHelpURL(help: keyof typeof memoryHelp): string | null;
|
|
33
|
+
export declare const retryLabels: {
|
|
34
|
+
extraction: {
|
|
35
|
+
action: string;
|
|
36
|
+
result: string;
|
|
37
|
+
};
|
|
38
|
+
consolidation: {
|
|
39
|
+
action: string;
|
|
40
|
+
result: string;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
export interface MemoryMessage {
|
|
44
|
+
title: string;
|
|
45
|
+
description: string;
|
|
46
|
+
warning?: boolean;
|
|
47
|
+
help?: keyof typeof memoryHelp;
|
|
48
|
+
affectedSessionID?: string;
|
|
49
|
+
details?: MemoryNotice[];
|
|
50
|
+
retryPhases?: MemoryRetryPhase[];
|
|
51
|
+
retryBlockedReason?: string;
|
|
52
|
+
}
|
|
53
|
+
export declare function memoryMessages(status: MemoryStatus, now: number, sessionID?: string): MemoryMessage[];
|