Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(session): make failed archives skippable and tune OpenClaw auto-commit (#2775)

LinQiang3917fa34c952063
diff --git a/examples/openclaw-plugin/README.md b/examples/openclaw-plugin/README.md
index d2ad2c584..634005472 100644
--- a/examples/openclaw-plugin/README.md
+++ b/examples/openclaw-plugin/README.md
@@ -204,7 +204,7 @@ That means OpenClaw sees "compressed history summary + archive index + active me
- it strips injected `<openviking-context>` blocks, historical `<relevant-memories>` blocks, and metadata noise before capture
- it appends the sanitized turn text into the OpenViking session
-After that, the plugin checks `pending_tokens`. Once the session crosses `commitTokenThreshold`, it triggers `commit(wait=false)`:
+After that, the plugin checks `pending_tokens`. Once it reaches `commitTokenThresholdRatio` of the model context window (`tokenBudget`), it triggers `commit(wait=false)`:
- archive generation and Phase 2 memory extraction continue asynchronously on the server
- the current turn is not blocked waiting for extraction
diff --git a/examples/openclaw-plugin/README_CN.md b/examples/openclaw-plugin/README_CN.md
index 02afa60f2..e954f4d5b 100644
--- a/examples/openclaw-plugin/README_CN.md
+++ b/examples/openclaw-plugin/README_CN.md
@@ -232,7 +232,7 @@ preflight 阶段的 `assemble()` 并不是简单地把旧聊天记录塞回来
- 会先剥掉注入过的 `<relevant-memories>` 和元数据噪音
- 最终把清洗后的增量内容追加到 OpenViking session
-之后插件会读取 session 的 `pending_tokens`。当它超过 `commitTokenThreshold` 时,会触发一次 `commit(wait=false)`:
+之后插件会读取 session 的 `pending_tokens`。当它达到「模型上下文窗口(`tokenBudget`)× `commitTokenThresholdRatio`」时,会触发一次 `commit(wait=false)`:
- archive 和 Phase 2 记忆抽取在服务端异步继续跑
- 当前 turn 不会因为等待抽取而阻塞
diff --git a/examples/openclaw-plugin/commands/setup.ts b/examples/openclaw-plugin/commands/setup.ts
index 498c1b082..732da3721 100644
--- a/examples/openclaw-plugin/commands/setup.ts
+++ b/examples/openclaw-plugin/commands/setup.ts
@@ -71,7 +71,7 @@ const CONFIG_KEYS_TO_PRESERVE = [
"recallMaxContentChars",
"recallPreferAbstract",
"recallTokenBudget",
- "commitTokenThreshold",
+ "commitTokenThresholdRatio",
"commitKeepRecentCount",
"bypassSessionPatterns",
"emitStandardDiagnostics",
diff --git a/examples/openclaw-plugin/config.ts b/examples/openclaw-plugin/config.ts
index 59d63b995..dbbc03182 100644
--- a/examples/openclaw-plugin/config.ts
+++ b/examples/openclaw-plugin/config.ts
@@ -31,7 +31,15 @@ export type MemoryOpenVikingConfig = {
recallPreferAbstract?: boolean;
/** @deprecated Use recallMaxInjectedChars. */
recallTokenBudget?: number;
- commitTokenThreshold?: number;
+ /**
+ * Auto-commit threshold expressed as a fraction (0-1) of the model context
+ * window (tokenBudget). afterTurn triggers an async commit once estimated
+ * pending tokens reach `commitTokenThresholdRatio * tokenBudget`
+ * (e.g. 0.5 = 50% of the window). Replaces the former absolute
+ * `commitTokenThreshold` (still accepted but ignored for backward
+ * compatibility). Set to 0 to commit every turn.
+ */
+ commitTokenThresholdRatio?: number;
/**
* WM v2: number of most-recent messages to keep live after an afterTurn
* commit so the next turn still has immediate context. Forwarded to the
@@ -109,7 +117,7 @@ const DEFAULT_RECALL_SCORE_THRESHOLD = 0.15;
const DEFAULT_RECALL_MAX_CONTENT_CHARS = 5000;
const DEFAULT_RECALL_PREFER_ABSTRACT = false;
const DEFAULT_RECALL_MAX_INJECTED_CHARS = 4000;
-const DEFAULT_COMMIT_TOKEN_THRESHOLD = 20000;
+const DEFAULT_COMMIT_TOKEN_THRESHOLD_RATIO = 0.5;
const DEFAULT_COMMIT_KEEP_RECENT_COUNT = 10;
const DEFAULT_BYPASS_SESSION_PATTERNS: string[] = [];
const DEFAULT_EMIT_STANDARD_DIAGNOSTICS = false;
@@ -394,6 +402,7 @@ export const memoryOpenVikingConfigSchema = {
"recallPreferAbstract",
"recallTokenBudget",
"commitTokenThreshold",
+ "commitTokenThresholdRatio",
"commitKeepRecentCount",
"bypassSessionPatterns",
"ingestReplyAssist",
@@ -510,9 +519,9 @@ export const memoryOpenVikingConfigSchema = {
: DEFAULT_RECALL_PREFER_ABSTRACT,
recallMaxInjectedChars,
recallTokenBudget: recallMaxInjectedChars,
- commitTokenThreshold: Math.max(
+ commitTokenThresholdRatio: Math.max(
0,
- Math.min(100_000, Math.floor(toNumber(cfg.commitTokenThreshold, DEFAULT_COMMIT_TOKEN_THRESHOLD))),
+ Math.min(1, toNumber(cfg.commitTokenThresholdRatio, DEFAULT_COMMIT_TOKEN_THRESHOLD_RATIO)),
),
commitKeepRecentCount: Math.max(
0,
@@ -747,11 +756,11 @@ export const memoryOpenVikingConfigSchema = {
help: "Completely bypass OpenViking for matching session keys. Use * within one segment and ** across segments.",
advanced: true,
},
- commitTokenThreshold: {
- label: "Commit Token Threshold",
- placeholder: String(DEFAULT_COMMIT_TOKEN_THRESHOLD),
+ commitTokenThresholdRatio: {
+ label: "Commit Token Threshold Ratio",
+ placeholder: String(DEFAULT_COMMIT_TOKEN_THRESHOLD_RATIO),
advanced: true,
- help: "Minimum estimated pending tokens before auto-commit triggers. Set to 0 to commit every turn.",
+ help: "Auto-commit triggers once estimated pending tokens reach this fraction (0-1) of the model context window (e.g. 0.5 = 50%). Set to 0 to commit every turn.",
},
commitKeepRecentCount: {
label: "Commit Keep Recent Count",
diff --git a/examples/openclaw-plugin/context-engine.ts b/examples/openclaw-plugin/context-engine.ts
index 9e1899bb5..9eb4cdfc0 100644
--- a/examples/openclaw-plugin/context-engine.ts
+++ b/examples/openclaw-plugin/context-engine.ts
@@ -370,12 +370,14 @@ export function createMemoryOpenVikingContextEngine(params: {
},
async afterTurn(afterTurnParams): Promise<void> {
+ const tokenBudget = validTokenBudget(afterTurnParams.tokenBudget) ?? 128_000;
await afterTurnOpenVikingSession({
sessionId: afterTurnParams.sessionId,
sessionKey: resolveSessionKey(afterTurnParams),
messages: afterTurnParams.messages,
prePromptMessageCount: afterTurnParams.prePromptMessageCount,
isHeartbeat: afterTurnParams.isHeartbeat,
+ tokenBudget,
runtimeContext: afterTurnParams.runtimeContext,
cfg,
getClient,
diff --git a/examples/openclaw-plugin/docs/openviking-install-real-scenario-verification-report.md b/examples/openclaw-plugin/docs/openviking-install-real-scenario-verification-report.md
index 7a95b6662..d419c8705 100644
--- a/examples/openclaw-plugin/docs/openviking-install-real-scenario-verification-report.md
+++ b/examples/openclaw-plugin/docs/openviking-install-real-scenario-verification-report.md
@@ -98,7 +98,7 @@ openclaw config get plugins.entries.openviking.config --json
"autoCapture": true,
"logFindRequests": true,
"emitStandardDiagnostics": true,
- "commitTokenThreshold": 1
+ "commitTokenThresholdRatio": 0
}
```
diff --git a/examples/openclaw-plugin/docs/openviking-openclaw-plugin-guide.md b/examples/openclaw-plugin/docs/openviking-openclaw-plugin-guide.md
index 7d4ef9219..3b883cc22 100644
--- a/examples/openclaw-plugin/docs/openviking-openclaw-plugin-guide.md
+++ b/examples/openclaw-plugin/docs/openviking-openclaw-plugin-guide.md
@@ -123,7 +123,7 @@ transformContext auto recall 流程:
4. 清理 `<relevant-memories>`、metadata、时间戳、心跳等噪音。
5. 逐条调用 `POST /api/v1/sessions/{sessionId}/messages`:`context-engine.ts:1378`、`client.ts:703`。
6. 调 `GET /api/v1/sessions/{sessionId}` 读取 `pending_tokens`:`context-engine.ts:1389`、`client.ts:770`。
-7. 若 `pending_tokens < commitTokenThreshold`,本轮结束。
+7. 若 `pending_tokens < tokenBudget × commitTokenThresholdRatio`,本轮结束。
8. 否则调用 `commitSession(wait=false, keepRecentCount=cfg.commitKeepRecentCount)`;服务端 Phase 2 记忆抽取异步继续执行:`context-engine.ts:1403`。
9. 开启 `logFindRequests` 时,插件轮询 task 结果并打印 Phase 2 抽取状态:`context-engine.ts:1424`。
@@ -173,7 +173,7 @@ transformContext auto recall 流程:
- `afterTurn` 路径:`wait=false`,异步 Phase 2,默认保留最近 10 条消息。
- `compact` 路径:`wait=true`,同步等待 Phase 2,`keepRecentCount=0`,形成明确压缩边界。
-`commitKeepRecentCount` 默认 10,`commitTokenThreshold` 默认 20000:`config.ts:68`。
+`commitKeepRecentCount` 默认 10,`commitTokenThresholdRatio` 默认 0.5(模型上下文窗口的 50%):`config.ts`。
### 5.3 显式记忆工具
@@ -730,7 +730,7 @@ openclaw config get plugins.slots.contextEngine
| `recallLimit` | `6` | 召回条数 |
| `recallScoreThreshold` | `0.15` | 召回阈值 |
| `recallMaxInjectedChars` | `4000` | 注入字符预算 |
-| `commitTokenThreshold` | `20000` | `pending_tokens` 超过该值触发 afterTurn commit;设 0 可每轮 commit |
+| `commitTokenThresholdRatio` | `0.5` | `pending_tokens` 达到「模型上下文窗口 × 该比例」触发 afterTurn commit(0-1,例 0.5=50%);设 0 可每轮 commit |
| `commitKeepRecentCount` | `10` | afterTurn commit 后保留最近消息数;compact 固定 0 |
| `bypassSessionPatterns` | `[]` | 匹配 sessionKey/sessionId 时完全绕过 OpenViking |
| `emitStandardDiagnostics` | `false` | 输出 `openviking: diag {...}` 结构化诊断日志 |
@@ -1358,7 +1358,7 @@ python health_check_tools/ov-healthcheck.py
- [ ] `X-OpenViking-Actor-Peer` 与预期 agent/session 一致。
- [ ] `autoCapture/autoRecall` 未被关闭。
- [ ] 当前 session 没有命中 `bypassSessionPatterns`。
-- [ ] `pending_tokens` 是否达到 `commitTokenThreshold`。
+- [ ] `pending_tokens` 是否达到 `tokenBudget × commitTokenThresholdRatio`。
- [ ] Phase 2 task 是否 completed。
- [ ] 需要细节时是否使用 archive 工具回查。
diff --git a/examples/openclaw-plugin/docs/openviking-plugin-reference.md b/examples/openclaw-plugin/docs/openviking-plugin-reference.md
index e50e24034..d16d20473 100644
--- a/examples/openclaw-plugin/docs/openviking-plugin-reference.md
+++ b/examples/openclaw-plugin/docs/openviking-plugin-reference.md
@@ -85,7 +85,8 @@ $OPENCLAW_STATE_DIR/openclaw.json
| `autoCapture` | boolean | `true` | — | 是否在会话过程中自动将消息写入 OpenViking session 并触发记忆抽取。 |
| `captureMode` | `"semantic"` \| `"keyword"` | `"semantic"` | — | 捕获模式。非法值会导致配置解析失败。 |
| `captureMaxLength` | number | `24000` | — | 自动捕获文本最大长度,范围 `200` 到 `200000`。 |
-| `commitTokenThreshold` | number | `20000` | — | afterTurn 中 pending tokens 达到阈值后触发 commit;`0` 表示每轮都提交。 |
+| `commitTokenThreshold` | number | 已废弃 | — | 旧的绝对 token 阈值,已被 `commitTokenThresholdRatio` 取代;为兼容老配置保留(可解析但被忽略,不再生效)。 |
+| `commitTokenThresholdRatio` | number | `0.5` | — | afterTurn 中 pending tokens 达到「模型上下文窗口 × 该比例」时触发 commit(0-1,例 `0.5`=50%);`0` 表示每轮都提交。 |
| `commitKeepRecentCount` | number | `10` | — | afterTurn commit 后保留最近消息数,范围 `0` 到 `1000`。compact 路径始终使用 `0`。 |
### 3.4 自动召回与显式召回
diff --git a/examples/openclaw-plugin/docs/workmemory-v2-design.md b/examples/openclaw-plugin/docs/workmemory-v2-design.md
index bb3d80a71..b1c143c3f 100644
--- a/examples/openclaw-plugin/docs/workmemory-v2-design.md
+++ b/examples/openclaw-plugin/docs/workmemory-v2-design.md
@@ -204,7 +204,7 @@ commit 归档时不全量清空消息,保留最近 N 条维持上下文连贯
├── addSessionMessage → 逐条 POST /sessions/{id}/messages
│ 服务端: append msg + 滑动窗口更新 pending_tokens + save meta
├── GET /sessions/{id} → 返回 pending_tokens(O(1))
- └── pending_tokens >= commitTokenThreshold?
+ └── pending_tokens >= tokenBudget * commitTokenThresholdRatio?
│
YES → commitSession(wait=false, keepRecentCount=cfg.commitKeepRecentCount)
│
diff --git a/examples/openclaw-plugin/openclaw.plugin.json b/examples/openclaw-plugin/openclaw.plugin.json
index f7f58c103..e91181bd4 100644
--- a/examples/openclaw-plugin/openclaw.plugin.json
+++ b/examples/openclaw-plugin/openclaw.plugin.json
@@ -196,11 +196,11 @@
"help": "Completely bypass OpenViking for matching session keys. Use * within one segment and ** across segments.",
"advanced": true
},
- "commitTokenThreshold": {
- "label": "Commit Token Threshold",
- "placeholder": "20000",
+ "commitTokenThresholdRatio": {
+ "label": "Commit Token Threshold Ratio",
+ "placeholder": "0.5",
"advanced": true,
- "help": "Minimum estimated pending tokens before auto-commit triggers. Set to 0 to commit every turn."
+ "help": "Auto-commit triggers once estimated pending tokens reach this fraction (0-1) of the model context window (e.g. 0.5 = 50%). Set to 0 to commit every turn."
},
"commitKeepRecentCount": {
"label": "Commit Keep Recent Count",
@@ -358,6 +358,11 @@
"commitTokenThreshold": {
"type": "number"
},
+ "commitTokenThresholdRatio": {
+ "type": "number",
+ "minimum": 0,
+ "maximum": 1
+ },
"commitKeepRecentCount": {
"type": "number",
"minimum": 0,
diff --git a/examples/openclaw-plugin/services/context-lifecycle-service.ts b/examples/openclaw-plugin/services/context-lifecycle-service.ts
index 7a3956d5c..1bb3a0c40 100644
--- a/examples/openclaw-plugin/services/context-lifecycle-service.ts
+++ b/examples/openclaw-plugin/services/context-lifecycle-service.ts
@@ -123,10 +123,12 @@ export type AfterTurnOpenVikingSessionParams = {
messages?: AgentMessage[];
prePromptMessageCount?: number;
isHeartbeat?: boolean;
+ /** Model context window in tokens; the auto-commit threshold is a fraction of this. */
+ tokenBudget: number;
runtimeContext?: Record<string, unknown>;
cfg: {
autoCapture: boolean;
- commitTokenThreshold: number;
+ commitTokenThresholdRatio: number;
commitKeepRecentCount: number;
logFindRequests: boolean;
};
@@ -753,6 +755,7 @@ export async function afterTurnOpenVikingSession({
messages: rawMessages,
prePromptMessageCount,
isHeartbeat,
+ tokenBudget,
runtimeContext,
cfg,
getClient,
@@ -880,11 +883,15 @@ export async function afterTurnOpenVikingSession({
const session = await client.getSession(ovSessionId, agentId);
const pendingTokens = session.pending_tokens ?? 0;
- if (pendingTokens < cfg.commitTokenThreshold) {
+ const commitTokenThreshold = Math.floor(tokenBudget * cfg.commitTokenThresholdRatio);
+
+ if (pendingTokens < commitTokenThreshold) {
diag("afterTurn_skip", ovSessionId, {
reason: "below_threshold",
pendingTokens,
- commitTokenThreshold: cfg.commitTokenThreshold,
+ commitTokenThreshold,
+ commitTokenThresholdRatio: cfg.commitTokenThresholdRatio,
+ tokenBudget,
senderIdFound: sender.found,
senderId: sender.senderId ?? null,
});
@@ -904,7 +911,9 @@ export async function afterTurnOpenVikingSession({
diag("afterTurn_commit", ovSessionId, {
pendingTokens,
- commitTokenThreshold: cfg.commitTokenThreshold,
+ commitTokenThreshold,
+ commitTokenThresholdRatio: cfg.commitTokenThresholdRatio,
+ tokenBudget,
status: commitResult.status,
archived: commitResult.archived ?? false,
taskId: commitResult.task_id ?? null,
diff --git a/examples/openclaw-plugin/skills/install-openviking-memory/SKILL.md b/examples/openclaw-plugin/skills/install-openviking-memory/SKILL.md
index 06a8e0959..76f59ef6e 100644
--- a/examples/openclaw-plugin/skills/install-openviking-memory/SKILL.md
+++ b/examples/openclaw-plugin/skills/install-openviking-memory/SKILL.md
@@ -603,7 +603,7 @@ These are the keys under `plugins.entries.openviking.config` in `openclaw.json`.
| `recallMaxInjectedChars` | `4000` | Hard cap on injected character count. Complete memories that do not fit are skipped, not truncated. |
| `recallPreferAbstract` | (plugin default) | Prefer abstract memories over raw. |
| `recallTokenBudget` | deprecated | Compatibility alias for `recallMaxInjectedChars`. |
-| `commitTokenThreshold` | `20000` | Pending-token threshold for async commit after turns; `0` commits every turn. |
+| `commitTokenThresholdRatio` | `0.5` | Async-commit threshold as a fraction (0-1) of the model context window (e.g. 0.5 = 50%); `0` commits every turn. |
| `commitKeepRecentCount` | `10` | Recent messages kept live after afterTurn commit. Compact always uses `0`. |
| `bypassSessionPatterns` | — | Glob patterns for sessions skipped by capture. |
| `ingestReplyAssist` | (plugin default) | Reply-assist ingestion toggle. |
diff --git a/examples/openclaw-plugin/skills/openviking-context-database/SKILL.md b/examples/openclaw-plugin/skills/openviking-context-database/SKILL.md
index 7e4c289f1..c1ac41de9 100644
--- a/examples/openclaw-plugin/skills/openviking-context-database/SKILL.md
+++ b/examples/openclaw-plugin/skills/openviking-context-database/SKILL.md
@@ -42,7 +42,7 @@ OpenClaw owns agent execution, prompts, and tool invocation. OpenViking owns lon
| Layer | Current behavior |
|---|---|
| `assemble` | Rebuilds compressed session history from OpenViking and injects relevant recall into the latest user message. |
-| `afterTurn` | Appends only the new turn to the OpenViking session; may trigger async commit when `pending_tokens >= commitTokenThreshold`. |
+| `afterTurn` | Appends only the new turn to the OpenViking session; may trigger async commit when `pending_tokens >= tokenBudget * commitTokenThresholdRatio`. |
| `compact` | Runs `commit(wait=true)`, waits for archive/extraction completion, and reads back latest archive overview. |
| Tools | Memory recall/store/forget, archive search/expand, resource/skill import/search, recall trace query, tool-result list/search/read. |
@@ -77,7 +77,7 @@ Core config lives under `plugins.entries.openviking.config`:
| `recallLimit` | `6` | Max selected recall items. |
| `recallScoreThreshold` | `0.15` | Min score after post-processing. |
| `recallMaxInjectedChars` | `4000` | Total injected character cap; complete memories that do not fit are skipped. |
-| `commitTokenThreshold` | `20000` | Pending-token threshold for async commit after turns; `0` commits every turn. |
+| `commitTokenThresholdRatio` | `0.5` | Async-commit threshold as a fraction (0-1) of the model context window (e.g. 0.5 = 50%); `0` commits every turn. |
| `commitKeepRecentCount` | `10` | Recent messages kept live after afterTurn commit. Compact always uses `0`. |
| `bypassSessionPatterns` | empty | Glob-like session keys that completely bypass OpenViking (`*` segment, `**` multi-segment). |
| `emitStandardDiagnostics` | `false` | Emit structured `openviking: diag {...}` lines. |
diff --git a/examples/openclaw-plugin/tests/e2e/test-archive-expand.py b/examples/openclaw-plugin/tests/e2e/test-archive-expand.py
index 441011bc3..8ffc96905 100644
--- a/examples/openclaw-plugin/tests/e2e/test-archive-expand.py
+++ b/examples/openclaw-plugin/tests/e2e/test-archive-expand.py
@@ -70,9 +70,10 @@ ov_archive_expand 归档展开端到端测试 — 用户: 小杰(后端开发
- plugins.slots.contextEngine = "openviking"
- plugins.entries.openviking.enabled = true
- plugins.entries.openviking.config.autoCapture = true
- - plugins.entries.openviking.config.commitTokenThreshold = 50
- ↑ 此值控制 auto-commit 时机。32 轮对话需要多次 auto-commit 产生归档,
- 若值过大可能导致归档数不足。建议 <= 5000。
+ - plugins.entries.openviking.config.commitTokenThresholdRatio = 0.02
+ ↑ 此值控制 auto-commit 时机,按模型上下文窗口的比例计算(0.02 = 2%)。
+ 32 轮对话需要多次 auto-commit 产生归档,比例越小归档越多;
+ 设为 0 表示每轮都 commit。
- agents.defaults.alsoAllow = ["ov_archive_expand"]
↑ 必须显式允许 ov_archive_expand 工具,否则 LLM 无法调用
diff --git a/examples/openclaw-plugin/tests/e2e/test-memory-chain.py b/examples/openclaw-plugin/tests/e2e/test-memory-chain.py
index d43d15372..a0036ada8 100644
--- a/examples/openclaw-plugin/tests/e2e/test-memory-chain.py
+++ b/examples/openclaw-plugin/tests/e2e/test-memory-chain.py
@@ -53,10 +53,10 @@ OpenClaw 记忆链路完整端到端测试
- plugins.entries.openviking.enabled = true # 启用 OV 插件
- plugins.entries.openviking.config.autoCapture = true # afterTurn 自动捕获
- plugins.entries.openviking.config.autoRecall = true # 新用户自动召回记忆
- - plugins.entries.openviking.config.commitTokenThreshold = 50
- ↑ 此值控制 auto-commit 触发时机。设为 50(token)时几乎每轮触发;
- 若值较大(如 10000),测试中 auto-commit 不会提前发生,
- Phase 3 的 commit 验证行为会不同。脚本已兼容两种场景。
+ - plugins.entries.openviking.config.commitTokenThresholdRatio = 0
+ ↑ 此值控制 auto-commit 触发时机,按模型上下文窗口的比例计算。
+ 设为 0 时每轮都 commit;若比例较大(如 0.8),测试中 auto-commit
+ 不会提前发生,Phase 3 的 commit 验证行为会不同。脚本已兼容两种场景。
================================================================================
四、使用方法
diff --git a/examples/openclaw-plugin/tests/ut/config.test.ts b/examples/openclaw-plugin/tests/ut/config.test.ts
index 385b2a470..b67613351 100644
--- a/examples/openclaw-plugin/tests/ut/config.test.ts
+++ b/examples/openclaw-plugin/tests/ut/config.test.ts
@@ -19,7 +19,7 @@ describe("memoryOpenVikingConfigSchema.parse()", () => {
expect(cfg.recallPreferAbstract).toBe(false);
expect(cfg.recallMaxInjectedChars).toBe(4000);
expect(cfg.recallTokenBudget).toBe(4000);
- expect(cfg.commitTokenThreshold).toBe(20000);
+ expect(cfg.commitTokenThresholdRatio).toBe(0.5);
expect(cfg.captureMode).toBe("semantic");
expect(cfg.captureMaxLength).toBe(24000);
expect(cfg.autoRecallTimeoutMs).toBe(5000);
@@ -48,6 +48,11 @@ describe("memoryOpenVikingConfigSchema.parse()", () => {
expect(cfg.agentExperience.enabled).toBe(false);
});
+ it("tolerates the deprecated commitTokenThreshold key and ignores it", () => {
+ const cfg = memoryOpenVikingConfigSchema.parse({ commitTokenThreshold: 20000 });
+ expect(cfg.commitTokenThresholdRatio).toBe(0.5);
+ });
+
it("enables add_resource only when explicitly allowed", () => {
const disabled = memoryOpenVikingConfigSchema.parse({});
expect(disabled.enableAddResourceTool).toBe(false);
diff --git a/examples/openclaw-plugin/tests/ut/context-engine-afterTurn.test.ts b/examples/openclaw-plugin/tests/ut/context-engine-afterTurn.test.ts
index 25b96fc75..0c592a9e2 100644
--- a/examples/openclaw-plugin/tests/ut/context-engine-afterTurn.test.ts
+++ b/examples/openclaw-plugin/tests/ut/context-engine-afterTurn.test.ts
@@ -14,7 +14,7 @@ function makeLogger() {
function makeEngine(opts?: {
autoCapture?: boolean;
- commitTokenThreshold?: number;
+ commitTokenThresholdRatio?: number;
getSession?: Record<string, unknown>;
addSessionMessageError?: Error;
cfgOverrides?: Record<string, unknown>;
@@ -24,7 +24,7 @@ function makeEngine(opts?: {
baseUrl: "http://127.0.0.1:1933",
autoCapture: opts?.autoCapture ?? true,
autoRecall: false,
- commitTokenThreshold: opts?.commitTokenThreshold ?? 20000,
+ commitTokenThresholdRatio: opts?.commitTokenThresholdRatio ?? 0.5,
emitStandardDiagnostics: true,
...(opts?.cfgOverrides ?? {}),
});
@@ -201,7 +201,7 @@ describe("context-engine afterTurn()", () => {
it("records senderId from runtimeContext in afterTurn diagnostics", async () => {
const { engine, logger } = makeEngine({
- commitTokenThreshold: 50,
+ commitTokenThresholdRatio: 0.01,
getSession: { pending_tokens: 5000 },
});
@@ -263,7 +263,7 @@ describe("context-engine afterTurn()", () => {
it("does not commit when pendingTokens < threshold", async () => {
const { engine, client } = makeEngine({
- commitTokenThreshold: 20000,
+ commitTokenThresholdRatio: 0.8,
getSession: { pending_tokens: 100 },
});
@@ -284,7 +284,7 @@ describe("context-engine afterTurn()", () => {
it("commits when pendingTokens >= threshold", async () => {
const { engine, client } = makeEngine({
- commitTokenThreshold: 20000,
+ commitTokenThresholdRatio: 0.1,
getSession: { pending_tokens: 25000 },
});
@@ -307,7 +307,7 @@ describe("context-engine afterTurn()", () => {
it("keeps afterTurn write and commit enabled when recall target types default to resources only", async () => {
const { engine, client } = makeEngine({
- commitTokenThreshold: 20000,
+ commitTokenThresholdRatio: 0.1,
getSession: { pending_tokens: 25000 },
cfgOverrides: {
recallTargetTypes: [],
@@ -355,7 +355,7 @@ describe("context-engine afterTurn()", () => {
it("commit uses OV session ID derived from sessionId", async () => {
const { engine, client } = makeEngine({
- commitTokenThreshold: 100,
+ commitTokenThresholdRatio: 0.01,
getSession: { pending_tokens: 5000 },
});
@@ -377,7 +377,7 @@ describe("context-engine afterTurn()", () => {
it("commit passes wait=false for afterTurn (async Phase 2)", async () => {
const { engine, client } = makeEngine({
- commitTokenThreshold: 100,
+ commitTokenThresholdRatio: 0.01,
getSession: { pending_tokens: 5000 },
});
diff --git a/examples/openclaw-plugin/tests/ut/context-engine-modules.test.ts b/examples/openclaw-plugin/tests/ut/context-engine-modules.test.ts
index 1b557c173..f0c6f1c0b 100644
--- a/examples/openclaw-plugin/tests/ut/context-engine-modules.test.ts
+++ b/examples/openclaw-plugin/tests/ut/context-engine-modules.test.ts
@@ -287,10 +287,11 @@ describe("context-engine lifecycle service seam", () => {
timestamp: 1775037660000,
}],
prePromptMessageCount: 0,
+ tokenBudget: 100000,
runtimeContext: { senderId: "telegram:123", agentId: "runtime-agent" },
cfg: {
autoCapture: true,
- commitTokenThreshold: 20000,
+ commitTokenThresholdRatio: 0.2,
commitKeepRecentCount: 7,
logFindRequests: false,
},
diff --git a/examples/openclaw-plugin/tests/ut/plugin-normal-flow-real-server.test.ts b/examples/openclaw-plugin/tests/ut/plugin-normal-flow-real-server.test.ts
index dba0d3347..a01fb728c 100644
--- a/examples/openclaw-plugin/tests/ut/plugin-normal-flow-real-server.test.ts
+++ b/examples/openclaw-plugin/tests/ut/plugin-normal-flow-real-server.test.ts
@@ -195,7 +195,7 @@ describe("plugin normal flow with healthy backend", () => {
autoCapture: true,
autoRecall: true,
baseUrl,
- commitTokenThreshold: 20000,
+ commitTokenThresholdRatio: 0.1,
mode: "remote",
},
registerContextEngine: (_id, factory) => {
diff --git a/openviking/session/session.py b/openviking/session/session.py
index eb36ebb23..9585e7393 100644
--- a/openviking/session/session.py
+++ b/openviking/session/session.py
@@ -10,7 +10,7 @@ import json
import re
from dataclasses import dataclass, field
from datetime import datetime, timezone
-from typing import TYPE_CHECKING, Any, Dict, List, Optional
+from typing import TYPE_CHECKING, Any, Awaitable, Callable, Dict, List, Optional
from uuid import uuid4
from openviking.core.namespace import canonical_session_uri
@@ -34,6 +34,7 @@ from openviking.session.tool_result_synopsis import (
)
from openviking.telemetry import get_current_telemetry, tracer
from openviking.telemetry.request_wait_tracker import get_request_wait_tracker
+from openviking.utils.model_retry import is_retryable_api_error, retry_async
from openviking.utils.time_utils import get_current_timestamp
from openviking.utils.token_estimation import estimate_text_tokens
from openviking_cli.exceptions import FailedPreconditionError
@@ -50,6 +51,9 @@ logger = get_logger(__name__)
_ARCHIVE_WAIT_POLL_SECONDS = 0.1
_PHASE2_QUEUE_WAIT_TIMEOUT_SECONDS = 1800.0
+_MEMORY_EXTRACTION_MAX_RETRIES = 3
+_MEMORY_EXTRACTION_RETRY_BASE_DELAY_SECONDS = 1.0
+_MEMORY_EXTRACTION_RETRY_MAX_DELAY_SECONDS = 8.0
def _wm_debug(msg: str) -> None:
@@ -1093,7 +1097,6 @@ class Session:
"""
from openviking.service.task_tracker import get_task_tracker
from openviking.storage.transaction import LockContext, get_lock_manager
- from openviking_cli.exceptions import FailedPreconditionError
trace_id = tracer.get_trace_id()
keep_recent_count = max(0, int(keep_recent_count or 0))
@@ -1122,14 +1125,6 @@ class Session:
"trace_id": trace_id,
}
- blocking_archive = await self._get_blocking_failed_archive_ref()
- if blocking_archive:
- raise FailedPreconditionError(
- f"Session {self.session_id} has unresolved failed archive "
- f"{blocking_archive['archive_id']}; fix it before committing again.",
- details={"archive_id": blocking_archive["archive_id"]},
- )
-
# Use filesystem-based distributed lock so this works across workers/processes.
session_path = self._viking_fs._uri_to_path(self._session_uri, ctx=self.ctx)
async with LockContext(get_lock_manager(), [session_path], lock_mode="exact"):
@@ -1171,38 +1166,35 @@ class Session:
}
self._compression.compression_index += 1
+ archive_uri = (
+ f"{self._session_uri}/history/archive_{self._compression.compression_index:03d}"
+ )
if keep_recent_count > 0:
split_idx = total - keep_recent_count
messages_to_archive = self._messages[:split_idx]
- self._messages = self._messages[split_idx:]
+ retained_messages = self._messages[split_idx:]
else:
messages_to_archive = self._messages.copy()
- self._messages = []
+ retained_messages = []
try:
- # Persist the retained tail (may be empty when keep=0). This
- # replaces messages.jsonl under the lock, consistent with the
- # previous behavior of writing empty messages on full clear.
+ # Persist archive raw messages before trimming live messages so
+ # an archive write failure cannot drop live conversation history.
+ if self._viking_fs:
+ lines = [m.to_jsonl() for m in messages_to_archive]
+ await self._viking_fs.write_file(
+ uri=f"{archive_uri}/messages.jsonl",
+ content="\n".join(lines) + "\n",
+ ctx=self.ctx,
+ )
+ self._messages = retained_messages
await self._write_to_agfs_async(messages=self._messages)
except Exception as e:
- # Rollback: restore messages so they aren't lost
- logger.error(f"[commit] Failed to write messages.jsonl: {e}")
- self._messages = list(messages_to_archive) + list(self._messages)
+ logger.error(f"[commit] Failed to persist archive/live messages: {e}")
+ self._messages = list(messages_to_archive) + list(retained_messages)
self._compression.compression_index -= 1
raise
- # Lock released — live session now contains only the retained tail.
-
- # ===== Phase 1 continued: Write raw archive (no LLM calls, no lock needed) =====
- archive_uri = (
- f"{self._session_uri}/history/archive_{self._compression.compression_index:03d}"
- )
- if self._viking_fs:
- lines = [m.to_jsonl() for m in messages_to_archive]
- await self._viking_fs.write_file(
- uri=f"{archive_uri}/messages.jsonl",
- content="\n".join(lines) + "\n",
- ctx=self.ctx,
- )
+ # Lock released; live session now contains only the retained tail.
# WM v2: live session is now the retained tail; pending_tokens resets
# because anything that was pending has been archived.
@@ -1288,24 +1280,7 @@ class Session:
redo_log = lock_manager.redo_log
try:
- if not await self._wait_for_previous_archive_done(archive_index):
- await self._write_failed_marker(
- archive_uri,
- stage="waiting_previous_done",
- error=(
- f"Previous archive archive_{archive_index - 1:03d} failed; "
- "this archive cannot proceed"
- ),
- blocked_by=f"archive_{archive_index - 1:03d}",
- )
- await tracker.fail(
- task_id,
- f"Previous archive archive_{archive_index - 1:03d} failed; "
- "cannot continue session commit",
- account_id=self.ctx.account_id,
- user_id=self.ctx.user.user_id,
- )
- return
+ await self._wait_for_previous_archive_done(archive_index)
await tracker.start(
task_id,
@@ -1363,6 +1338,25 @@ class Session:
ctx=self.ctx,
)
+ async def _run_retryable_phase2_step(
+ operation_name: str,
+ fn: Callable[[], Awaitable[Any]],
+ ) -> Any:
+ # Secondary safety net on top of the per-call retry that the
+ # VLM/embedding layer already performs. Reuses the shared
+ # transient-error classifier so permanent failures (auth,
+ # quota, content-safety, 400, oversized input) fail fast
+ # instead of being retried pointlessly.
+ return await retry_async(
+ fn,
+ max_retries=_MEMORY_EXTRACTION_MAX_RETRIES,
+ base_delay=_MEMORY_EXTRACTION_RETRY_BASE_DELAY_SECONDS,
+ max_delay=_MEMORY_EXTRACTION_RETRY_MAX_DELAY_SECONDS,
+ is_retryable=is_retryable_api_error,
+ logger=logger,
+ operation_name=operation_name,
+ )
+
# Summary, long-term memory, and execution-derived memory run concurrently.
ov_config = get_openviking_config()
memory_extraction_enabled = ov_config.memory.extraction_enabled
@@ -1410,70 +1404,87 @@ class Session:
self._session_compressor, "extract_execution_memories"
)
- extraction_tasks: List[Any] = [_run_archive_summary()]
+ extraction_tasks: List[Any] = [
+ _run_retryable_phase2_step("archive_summary", _run_archive_summary)
+ ]
extraction_labels = ["archive_summary"]
if long_term_has_work:
- extraction_tasks.append(
- self._session_compressor.extract_long_term_memories(
+
+ async def _run_long_term_memory_extraction() -> Any:
+ # strict_extract_errors=True lets transient failures
+ # surface so _run_retryable_phase2_step can retry them
+ # (and so a final failure is recorded as a skipped
+ # archive instead of silently dropping the memory).
+ return await self._session_compressor.extract_long_term_memories(
messages=extraction_messages,
user=self.user,
session_id=self.session_id,
ctx=self.ctx,
+ strict_extract_errors=True,
latest_archive_overview=latest_archive_overview,
archive_uri=archive_uri,
allowed_memory_types=long_term_memory_types,
allow_self_memory=self_memory_enabled,
allowed_peer_ids=allowed_peer_ids,
)
+
+ extraction_tasks.append(
+ _run_retryable_phase2_step(
+ "long_term_memory_extraction",
+ _run_long_term_memory_extraction,
+ )
)
extraction_labels.append("long_term")
if has_execution_memory and execution_memory_has_work:
- try:
- extraction_tasks.append(
- self._session_compressor.extract_execution_memories(
- messages=extraction_messages,
- ctx=self.ctx,
- latest_archive_overview=latest_archive_overview,
- archive_uri=archive_uri,
- allowed_memory_types=execution_memory_types,
- include_session_skills=session_skill_extraction_enabled,
- )
+
+ async def _run_execution_memory_extraction() -> Any:
+ # See _run_long_term_memory_extraction: surface errors
+ # so retries can engage and final failures are visible.
+ return await self._session_compressor.extract_execution_memories(
+ messages=extraction_messages,
+ ctx=self.ctx,
+ strict_extract_errors=True,
+ latest_archive_overview=latest_archive_overview,
+ archive_uri=archive_uri,
+ allowed_memory_types=execution_memory_types,
+ include_session_skills=session_skill_extraction_enabled,
)
- extraction_labels.append("execution")
- except Exception as exc:
- logger.error(
- "Execution memory extraction failed: %s",
- exc,
- exc_info=exc,
+
+ extraction_tasks.append(
+ _run_retryable_phase2_step(
+ "execution_memory_extraction",
+ _run_execution_memory_extraction,
)
+ )
+ extraction_labels.append("execution")
_results = await asyncio.gather(
*extraction_tasks,
return_exceptions=True,
)
- summary_result = _results[0]
-
- if isinstance(summary_result, Exception):
- logger.error(
- f"Archive summary generation failed: {summary_result}",
- exc_info=summary_result,
- )
-
- total_extracted = 0
- extraction_errors: list[BaseException] = []
- for label, result in zip(extraction_labels[1:], _results[1:], strict=True):
+ # Binary archive outcome: if ANY Phase 2 step (Working
+ # Memory summary, long-term, or execution memory) still
+ # fails after retries, the whole archive is marked
+ # .failed.json and skipped. There is no partial state.
+ extraction_error: Optional[BaseException] = None
+ for label, result in zip(extraction_labels, _results, strict=True):
if isinstance(result, Exception):
logger.error(
- "Memory extraction failed for %s: %s",
+ "Phase 2 step %s failed: %s",
label,
result,
exc_info=result,
)
- extraction_errors.append(result)
- continue
+ if extraction_error is None:
+ extraction_error = result
+
+ if extraction_error is not None:
+ raise extraction_error
+ total_extracted = 0
+ for label, result in zip(extraction_labels[1:], _results[1:], strict=True):
if isinstance(result, dict):
target_contexts = list(result.get("contexts", []))
target_skills = list(result.get("session_skills", []))
@@ -1492,9 +1503,6 @@ class Session:
if target_skills:
extracted_skill_results.extend(target_skills)
- if extraction_errors:
- raise extraction_errors[0]
-
if total_extracted:
self._stats.memories_extracted += total_extracted
if extracted_skill_results:
@@ -1509,7 +1517,7 @@ class Session:
"Memory and session skill extraction skipped "
"(disabled by config or memory_policy)"
)
- await _run_archive_summary()
+ await _run_retryable_phase2_step("archive_summary", _run_archive_summary)
# Write relations (using snapshot, not self._usage_records)
if self._viking_fs:
@@ -1567,7 +1575,9 @@ class Session:
telemetry_snapshot=snapshot,
)
- # Write .done file last — signals that all state is finalized
+ # Write .done file last — signals that all state is finalized. We
+ # only reach here when every Phase 2 step succeeded; any failure
+ # would have raised above and marked the archive .failed.json.
await self._write_done_file(archive_uri, first_message_id, last_message_id)
await tracker.complete(
@@ -1658,6 +1668,7 @@ class Session:
stage: str,
error: str,
blocked_by: str = "",
+ skipped: bool = True,
) -> None:
"""Persist a terminal failure marker for the archive."""
if not self._viking_fs:
@@ -1666,6 +1677,7 @@ class Session:
"stage": stage,
"error": error,
"failed_at": get_current_timestamp(),
+ "skipped": skipped,
}
if blocked_by:
payload["blocked_by"] = blocked_by
@@ -1899,24 +1911,6 @@ class Session:
return completed
- async def _get_blocking_failed_archive_ref(self) -> Optional[Dict[str, Any]]:
- """Return the earliest unresolved failed archive, if any."""
- for archive in sorted(await self._list_archive_refs(), key=lambda item: item["index"]):
- try:
- await self._viking_fs.read_file(f"{archive['archive_uri']}/.done", ctx=self.ctx)
- continue
- except Exception:
- pass
- try:
- await self._viking_fs.read_file(
- f"{archive['archive_uri']}/.failed.json",
- ctx=self.ctx,
- )
- except Exception:
- continue
- return archive
- return None
-
async def _read_archive_overview(self, archive_uri: str) -> str:
"""Read archive overview text."""
try:
@@ -2001,18 +1995,30 @@ class Session:
return summary["overview"] if summary else ""
async def _get_pending_archive_messages(self) -> List[Message]:
- """Return messages from incomplete archives newer than the latest completed archive."""
+ """Return messages from in-progress archives newer than the latest completed archive."""
latest_completed_index = max(0, self._meta.commit_count)
- incomplete_archives: List[Dict[str, Any]] = []
+ pending_archives: List[Dict[str, Any]] = []
for archive in sorted(await self._list_archive_refs(), key=lambda item: item["index"]):
try:
await self._viking_fs.read_file(f"{archive['archive_uri']}/.done", ctx=self.ctx)
latest_completed_index = max(latest_completed_index, archive["index"])
+ continue
except Exception:
- incomplete_archives.append(archive)
+ pass
+
+ try:
+ await self._viking_fs.read_file(
+ f"{archive['archive_uri']}/.failed.json",
+ ctx=self.ctx,
+ )
+ continue
+ except Exception:
+ pass
+
+ pending_archives.append(archive)
pending_messages: List[Message] = []
- for archive in incomplete_archives:
+ for archive in pending_archives:
if archive["index"] <= latest_completed_index:
continue
pending_messages.extend(await self._read_archive_messages(archive["archive_uri"]))
@@ -2028,7 +2034,7 @@ class Session:
return int(match.group(1))
async def _wait_for_previous_archive_done(self, archive_index: int) -> bool:
- """Wait until the previous archive is done, or report dependency failure."""
+ """Wait until the previous archive reaches a terminal state."""
if archive_index <= 1 or not self._viking_fs:
return True
@@ -2045,7 +2051,11 @@ class Session:
f"{previous_archive_uri}/.failed.json",
ctx=self.ctx,
)
- return False
+ logger.info(
+ "Previous archive %s failed and is skipped; continuing",
+ previous_archive_uri,
+ )
+ return True
except Exception:
pass
diff --git a/tests/server/test_api_sessions.py b/tests/server/test_api_sessions.py
index 628ad74cd..5aaed5880 100644
--- a/tests/server/test_api_sessions.py
+++ b/tests/server/test_api_sessions.py
@@ -92,6 +92,14 @@ async def _wait_for_task(client: httpx.AsyncClient, task_id: str, timeout: float
raise TimeoutError(f"Task {task_id} did not complete within {timeout}s")
+async def _archive_marker_exists(session, archive_uri: str, name: str) -> bool:
+ try:
+ await session._viking_fs.read_file(f"{archive_uri}/{name}", ctx=session.ctx)
+ return True
+ except Exception:
+ return False
+
+
def _session_route_request(
*,
auth_mode: str = "api_key",
@@ -353,6 +361,55 @@ async def test_get_session_context_includes_incomplete_archive_messages(
]
+async def test_get_session_context_skips_failed_archive_messages(
+ client: httpx.AsyncClient, service
+):
+ create_resp = await client.post("/api/v1/sessions", json={})
+ session_id = create_resp.json()["result"]["session_id"]
+
+ await client.post(
+ f"/api/v1/sessions/{session_id}/messages",
+ json=_message_request("user", content="Archived seed"),
+ )
+ commit_resp = await client.post(f"/api/v1/sessions/{session_id}/commit")
+ assert commit_resp.status_code == 200
+
+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.ROOT)
+ session = service.sessions.session(ctx, session_id)
+ await session.load()
+ failed_messages = [
+ Message(
+ id="failed-user",
+ role="user",
+ parts=[TextPart("Failed archive message")],
+ peer_id=DEFAULT_USER.user_id,
+ )
+ ]
+ failed_archive_uri = f"{session.uri}/history/archive_002"
+ await session._viking_fs.write_file(
+ uri=f"{failed_archive_uri}/messages.jsonl",
+ content="\n".join(msg.to_jsonl() for msg in failed_messages) + "\n",
+ ctx=session.ctx,
+ )
+ await session._viking_fs.write_file(
+ uri=f"{failed_archive_uri}/.failed.json",
+ content=json.dumps({"stage": "memory_extraction", "error": "synthetic"}),
+ ctx=session.ctx,
+ )
+
+ await client.post(
+ f"/api/v1/sessions/{session_id}/messages",
+ json=_message_request("user", content="Current live message"),
+ )
+
+ resp = await client.get(f"/api/v1/sessions/{session_id}/context")
+ assert resp.status_code == 200
+ body = resp.json()
+ assert [m["parts"][0]["text"] for m in body["result"]["messages"]] == [
+ "Current live message",
+ ]
+
+
async def test_add_message(client: httpx.AsyncClient):
create_resp = await client.post("/api/v1/sessions", json={})
session_id = create_resp.json()["result"]["session_id"]
@@ -795,7 +852,18 @@ async def test_extract_session_jsonable_regression(client: httpx.AsyncClient, se
async def test_get_session_context_endpoint_returns_trimmed_latest_archive_and_messages(
client: httpx.AsyncClient,
+ service,
):
+ # Memory extraction (long-term/execution) uses a VLM backend the server fake
+ # does not cover; stub it so the archive completes deterministically. This
+ # test exercises the context endpoint, not memory extraction.
+ async def _no_memories(*args, **kwargs):
+ del args, kwargs
+ return []
+
+ service.sessions._session_compressor.extract_long_term_memories = _no_memories
+ service.sessions._session_compressor.extract_execution_memories = _no_memories
+
create_resp = await client.post("/api/v1/sessions", json={})
session_id = create_resp.json()["result"]["session_id"]
session_uri = create_resp.json()["result"]["uri"]
@@ -846,7 +914,20 @@ async def test_get_session_context_endpoint_returns_trimmed_latest_archive_and_m
assert result["stats"]["failedArchives"] == 0
-async def test_get_session_archive_endpoint_returns_archive_details(client: httpx.AsyncClient):
+async def test_get_session_archive_endpoint_returns_archive_details(
+ client: httpx.AsyncClient,
+ service,
+):
+ # See test_get_session_context_*: stub memory extraction (not covered by the
+ # server fake VLM) so the archive completes; this test checks the archive
+ # endpoint, not memory extraction.
+ async def _no_memories(*args, **kwargs):
+ del args, kwargs
+ return []
+
+ service.sessions._session_compressor.extract_long_term_memories = _no_memories
+ service.sessions._session_compressor.extract_execution_memories = _no_memories
+
create_resp = await client.post("/api/v1/sessions", json={})
session_id = create_resp.json()["result"]["session_id"]
@@ -875,10 +956,14 @@ async def test_get_session_archive_endpoint_returns_archive_details(client: http
]
-async def test_commit_endpoint_rejects_after_failed_archive(
+async def test_commit_failed_when_long_term_extraction_fails_does_not_block_next_commit(
client: httpx.AsyncClient,
service,
):
+ """Binary archive outcome: if long-term memory extraction fails (after
+ retries), the whole archive is marked .failed.json and skipped — there is
+ no partial state — but a failed archive must not block the next commit.
+ """
create_resp = await client.post("/api/v1/sessions", json={})
session_id = create_resp.json()["result"]["session_id"]
@@ -895,16 +980,147 @@ async def test_commit_endpoint_rejects_after_failed_archive(
commit_resp = await client.post(f"/api/v1/sessions/{session_id}/commit")
task_id = commit_resp.json()["result"]["task_id"]
task = await _wait_for_task(client, task_id)
+ # Any Phase 2 step failing fails the whole archive (no partial state).
assert task["status"] == "failed"
+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.ROOT)
+ session = service.sessions.session(ctx, session_id)
+ await session.load()
+ archive_uri = f"{session.uri}/history/archive_001"
+ assert await _archive_marker_exists(session, archive_uri, ".failed.json")
+ assert not await _archive_marker_exists(session, archive_uri, ".done")
+ assert not await _archive_marker_exists(session, archive_uri, ".partial.json")
+
+ # The failed archive is skipped, not retrievable as a completed archive.
+ archive_resp = await client.get(f"/api/v1/sessions/{session_id}/archives/archive_001")
+ archive_body = archive_resp.json()
+ assert archive_body["status"] == "error"
+ assert archive_body["error"]["code"] == "NOT_FOUND"
+
+ # A failed archive is a skippable terminal state; the next commit proceeds.
await client.post(
f"/api/v1/sessions/{session_id}/messages",
json=_message_request("user", content="second round"),
)
resp = await client.post(f"/api/v1/sessions/{session_id}/commit")
+ assert resp.status_code == 200
+ body = resp.json()
+ assert body["status"] == "ok"
+ assert body["result"]["archived"] is True
- assert resp.status_code == 412
+
+async def test_commit_failed_when_execution_extraction_fails_does_not_block_next_commit(
+ client: httpx.AsyncClient,
+ service,
+):
+ """Binary archive outcome: if execution memory extraction fails (after
+ retries), the whole archive is marked .failed.json and skipped — there is
+ no partial state — but a failed archive must not block the next commit.
+ """
+ create_resp = await client.post("/api/v1/sessions", json={})
+ session_id = create_resp.json()["result"]["session_id"]
+
+ # Stub long-term extraction to succeed so the only failing Phase 2 step is
+ # execution memory extraction, isolating the execution failure path.
+ async def _no_memories(*args, **kwargs):
+ del args, kwargs
+ return []
+
+ async def failing_extract(*args, **kwargs):
+ del args, kwargs
+ raise RuntimeError("synthetic execution failure")
+
+ service.sessions._session_compressor.extract_long_term_memories = _no_memories
+ service.sessions._session_compressor.extract_execution_memories = failing_extract
+
+ await client.post(
+ f"/api/v1/sessions/{session_id}/messages",
+ json=_message_request("user", content="first round"),
+ )
+ commit_resp = await client.post(f"/api/v1/sessions/{session_id}/commit")
+ task_id = commit_resp.json()["result"]["task_id"]
+ task = await _wait_for_task(client, task_id)
+ # Any Phase 2 step failing fails the whole archive (no partial state).
+ assert task["status"] == "failed"
+
+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.ROOT)
+ session = service.sessions.session(ctx, session_id)
+ await session.load()
+ archive_uri = f"{session.uri}/history/archive_001"
+ assert await _archive_marker_exists(session, archive_uri, ".failed.json")
+ assert not await _archive_marker_exists(session, archive_uri, ".done")
+ assert not await _archive_marker_exists(session, archive_uri, ".partial.json")
+
+ # The marker carries the execution-step error, proving execution extraction
+ # (not some other step) is what failed the archive.
+ failed_payload = json.loads(
+ await session._viking_fs.read_file(
+ f"{archive_uri}/.failed.json",
+ ctx=session.ctx,
+ )
+ )
+ assert failed_payload.get("skipped") is True
+ assert "synthetic execution failure" in failed_payload["error"]
+
+ # The failed archive is skipped, not retrievable as a completed archive.
+ archive_resp = await client.get(f"/api/v1/sessions/{session_id}/archives/archive_001")
+ archive_body = archive_resp.json()
+ assert archive_body["status"] == "error"
+ assert archive_body["error"]["code"] == "NOT_FOUND"
+
+ # A failed archive is a skippable terminal state; the next commit proceeds.
+ await client.post(
+ f"/api/v1/sessions/{session_id}/messages",
+ json=_message_request("user", content="second round"),
+ )
+ resp = await client.post(f"/api/v1/sessions/{session_id}/commit")
+ assert resp.status_code == 200
body = resp.json()
- assert body["status"] == "error"
- assert body["error"]["code"] == "FAILED_PRECONDITION"
- assert "unresolved failed archive" in body["error"]["message"]
+ assert body["status"] == "ok"
+ assert body["result"]["archived"] is True
+
+
+async def test_commit_failed_when_summary_fails_does_not_block_next_commit(
+ client: httpx.AsyncClient,
+ service,
+):
+ """If the core Working Memory summary fails, the archive is .failed.json (no
+ .done) and the task fails — but a failed archive must not block later commits.
+ """
+ create_resp = await client.post("/api/v1/sessions", json={})
+ session_id = create_resp.json()["result"]["session_id"]
+
+ async def failing_summary(*args, **kwargs):
+ del args, kwargs
+ raise RuntimeError("synthetic summary failure")
+
+ with patch(
+ "openviking.session.session.Session._generate_archive_summary_async",
+ new=failing_summary,
+ ):
+ await client.post(
+ f"/api/v1/sessions/{session_id}/messages",
+ json=_message_request("user", content="first round"),
+ )
+ commit_resp = await client.post(f"/api/v1/sessions/{session_id}/commit")
+ task_id = commit_resp.json()["result"]["task_id"]
+ task = await _wait_for_task(client, task_id)
+ assert task["status"] == "failed"
+
+ ctx = RequestContext(user=UserIdentifier.the_default_user(), role=Role.ROOT)
+ session = service.sessions.session(ctx, session_id)
+ await session.load()
+ archive_uri = f"{session.uri}/history/archive_001"
+ assert await _archive_marker_exists(session, archive_uri, ".failed.json")
+ assert not await _archive_marker_exists(session, archive_uri, ".done")
+
+ # A failed archive is a skippable terminal state; the next commit proceeds.
+ await client.post(
+ f"/api/v1/sessions/{session_id}/messages",
+ json=_message_request("user", content="second round"),
+ )
+ resp = await client.post(f"/api/v1/sessions/{session_id}/commit")
+ assert resp.status_code == 200
+ body = resp.json()
+ assert body["status"] == "ok"
+ assert body["result"]["archived"] is True
diff --git a/tests/server/test_http_client_sdk.py b/tests/server/test_http_client_sdk.py
index 93a14e7e7..facb0e799 100644
--- a/tests/server/test_http_client_sdk.py
+++ b/tests/server/test_http_client_sdk.py
@@ -10,7 +10,7 @@ import pytest
import pytest_asyncio
from openviking_cli.client.http import AsyncHTTPClient
-from openviking_cli.exceptions import ConflictError, FailedPreconditionError, ProcessingError
+from openviking_cli.exceptions import ConflictError, ProcessingError
from tests.server.conftest import SAMPLE_MD_CONTENT, TEST_TMP_DIR
from tests.server.ovpack_test_helpers import build_ovpack_bytes
@@ -290,7 +290,16 @@ async def test_sdk_commit_session_keeps_telemetry_as_second_positional_argument(
async def test_sdk_get_session_archive(http_client):
- client, _ = http_client
+ client, svc = http_client
+
+ # Memory extraction uses a VLM backend the fake does not cover; stub it so
+ # the archive completes (this test checks archive retrieval, not extraction).
+ async def _no_memories(*args, **kwargs):
+ del args, kwargs
+ return []
+
+ svc.session_compressor.extract_long_term_memories = _no_memories
+ svc.session_compressor.extract_execution_memories = _no_memories
session_info = await client.create_session()
session_id = session_info["session_id"]
@@ -312,7 +321,7 @@ async def test_sdk_get_session_archive(http_client):
assert [m["parts"][0]["text"] for m in archive["messages"]] == ["Archive me"]
-async def test_sdk_commit_raises_failed_precondition_after_failed_archive(http_client):
+async def test_sdk_commit_not_blocked_after_failed_archive(http_client):
client, svc = http_client
session_info = await client.create_session()
@@ -328,15 +337,21 @@ async def test_sdk_commit_raises_failed_precondition_after_failed_archive(http_c
commit_result = await client.commit_session(session_id)
task_id = commit_result["task_id"]
+ task = None
for _ in range(100):
task = await client.get_task(task_id)
if task and task["status"] in ("completed", "failed"):
break
await asyncio.sleep(0.1)
+ # Any Phase 2 step failing marks the whole archive .failed.json (skipped).
+ assert task is not None and task["status"] == "failed"
+
+ # A failed archive is a skippable terminal state and must not block the
+ # next commit (this previously raised FailedPreconditionError).
await client.add_message(session_id, "user", "Second round")
- with pytest.raises(FailedPreconditionError, match="unresolved failed archive"):
- await client.commit_session(session_id)
+ second = await client.commit_session(session_id)
+ assert second["task_id"]
# ===================================================================
diff --git a/tests/session/test_session_commit.py b/tests/session/test_session_commit.py
index bad2258ae..5bc75be1f 100644
--- a/tests/session/test_session_commit.py
+++ b/tests/session/test_session_commit.py
@@ -15,7 +15,6 @@ from openviking.message import TextPart
from openviking.service.task_tracker import get_task_tracker
from openviking.session import Session
from openviking.storage.transaction import get_lock_manager
-from openviking_cli.exceptions import FailedPreconditionError
async def _wait_for_task(task_id: str, timeout: float = 30.0) -> dict:
@@ -29,6 +28,14 @@ async def _wait_for_task(task_id: str, timeout: float = 30.0) -> dict:
raise TimeoutError(f"Task {task_id} did not complete within {timeout}s")
+async def _marker_exists(session, archive_uri: str, name: str) -> bool:
+ try:
+ await session._viking_fs.read_file(f"{archive_uri}/{name}", ctx=session.ctx)
+ return True
+ except Exception:
+ return False
+
+
class TestCommit:
"""Test commit"""
@@ -474,9 +481,14 @@ class TestCommit:
f"active_count not incremented: before={count_before}, after={count_after}"
)
- async def test_commit_blocks_after_failed_archive(self, client: AsyncOpenViking):
- """A failed archive should block the next commit until it is resolved."""
- session = client.session(session_id="failed_archive_blocks_new_commit")
+ async def test_commit_failed_after_long_term_extraction_failure_does_not_block(
+ self, client: AsyncOpenViking
+ ):
+ """Binary archive outcome: if long-term extraction fails (after retries),
+ the whole archive is marked .failed.json and skipped — there is no
+ partial state — but a failed archive must not block the next commit.
+ """
+ session = client.session(session_id="failed_archive_does_not_block_commit")
async def failing_extract(*args, **kwargs):
del args, kwargs
@@ -490,17 +502,25 @@ class TestCommit:
assert task_result["status"] == "failed"
- failed_marker = await session._viking_fs.read_file(
- f"{result['archive_uri']}/.failed.json",
- ctx=session.ctx,
+ archive_uri = result["archive_uri"]
+ assert await _marker_exists(session, archive_uri, ".failed.json")
+ assert not await _marker_exists(session, archive_uri, ".done")
+ assert not await _marker_exists(session, archive_uri, ".partial.json")
+
+ failed_payload = json.loads(
+ await session._viking_fs.read_file(
+ f"{archive_uri}/.failed.json",
+ ctx=session.ctx,
+ )
)
- failed_payload = json.loads(failed_marker)
- assert failed_payload["stage"] == "memory_extraction"
+ assert failed_payload.get("skipped") is True
assert "synthetic extraction failure" in failed_payload["error"]
+ # A failed archive is a skippable terminal state and must not block the
+ # next commit (this previously raised FailedPreconditionError).
session.add_message("user", [TextPart("Second round message")])
- with pytest.raises(FailedPreconditionError, match="unresolved failed archive"):
- await session.commit_async()
+ second = await session.commit_async()
+ assert second["status"] == "accepted"
async def test_commit_skips_redo_when_recovery_disabled(
self, session_with_messages: Session, monkeypatch: pytest.MonkeyPatch