Repository navigation
fix(memory-lancedb): auto-capture stores OpenClaw's own [System] retry turns as user memories - #189956
fix(memory-lancedb): auto-capture stores OpenClaw's own [System] retry turns as user memories#189956amittell wants to merge 7 commits into
Conversation
…apture
Core writes stranded-reply retries, restart-recovery notes and transcript
follow-ups as user-role turns prefixed with [System] (formatSystemTurnPrompt).
memory-lancedb's agent_end capture treats them like any user text, so a retry
that quotes a reply containing a trigger word ("remember", "always", or a
custom trigger) was stored as a user memory and could be recalled later.
shouldCapture now rejects text that starts with the marker. isRecallableMemoryText,
which gates both recall and duplicate matching, skips rows already stored. The
marker is matched in the plugin because plugins cannot import the core helper.
Stored rows are not deleted.
Adds the qa scenario memory-lancedb-system-turn-capture: a real gateway with
message_tool replies and the mock provider. It fails on main with the [System]
row stored and passes with this change.
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
🟡 Changes recommended
Sanitization can strip the system-turn marker and allow unintended capture, while the retry test can pass before background capture completes.
2 open issues
What changed in this PR
Prevents memory-lancedb from treating OpenClaw-generated [System] turns as user memories, addressing #189955 without deleting existing stored rows.
Changes:
- Rejects system-prefixed text during capture, recall, and duplicate matching.
- Adds unit coverage for retry turns, restart notes, and previously stored system rows.
- Adds a QA scenario exercising stranded-reply recovery with auto-capture enabled.
| File | Description |
|---|---|
qa/scenarios/memory/memory-lancedb-system-turn-capture.yaml |
Exercises recovery and checks that user text—but not system turns—is stored. |
extensions/memory-lancedb/memory-policy.ts |
Filters system-prefixed text from capture, recall, and duplicate matching. |
extensions/memory-lancedb/memory-policy.test.ts |
Tests system-turn rejection and preservation of ordinary user memories. |
Review effort: Balanced
|
Codex review: blocked before merge. What this changesExclude OpenClaw-generated [System] turns from automatic memory capture, recall, and duplicate matching. Example: A stranded-reply retry quotes “I'll remember that the deploy window is Tuesdays.”
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Merge readiness⛔ Blocked before merge - 1 item remains Keep open: this PR fixes a current LanceDB memory-contamination bug, preserves existing storage, and addresses both earlier findings; no new correctness defects were found. Reviewed head: Before merge
FindingsNone. Agent review detailsHow this fits togetherThe LanceDB plugin receives user-role transcript messages after agent runs, filters and embeds capturable text, and filters stored search results before recall or duplicate matching. flowchart TD
A[User and internal turns] --> B[Agent end hook]
B --> C[Raw system marker check]
C --> D[Sanitization and capture policy]
D --> E[LanceDB store]
E --> F[Shared recall filter]
F --> G[Recall and duplicate matching]
Technical reviewBest possible solution: Land the focused plugin policy repair once the required exact-head workflows complete successfully. Do we have a high-confidence way to reproduce the issue? The core retry formatter and current-main capture loop establish the failure path, corroborated by the author's recorded failing Gateway run; this read-only review executed no target code. Is this the best way to solve the issue? The change uses the existing capture and recall owners, rejects the marker before sanitization can erase it, and preserves stored rows; the documented limitation is that user-authored text beginning with [System] is also excluded. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 18e53d806dbb. Provenance checked
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +29, Tests +60, Other +136. Total +225 across 6 files. Added test files: 0. View PR surface stats
Preflight sourceSource unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified. Root-cause clusterRelationship: Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. LabelsLabel changes:
Label justifications:
Rating scale6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior. WorkflowClawSweeper edits this one comment on every review. Comment HistoryReview history (3 earlier review cycles)
Reviewed November 27, 2026, 1:32 PM ET / 18:32 UTC (Revision 4). |
… the scenario on a barrier Sanitizing can drop the [System] line itself: when a stranded-reply retry quotes a reply that opens with a context block, MARKER_JSON_BLOCK_RE removes the whole first line, and the "Remember ..." sentence that follows was captured. Capture now goes through capturableUserText, which checks the raw turn for the marker (after any leading timestamp envelope) before sanitizing, then applies shouldCapture as before. The qa scenario no longer sleeps a fixed 8 s. It sends a later capturable turn in the same conversation and waits for that row. Captures for one session run in order, so the retry's capture has finished when the row appears. The first prompt is not capturable, so no earlier row can absorb the retry turn through duplicate matching. The barrier text sits far below the 0.95 duplicate threshold under the mock's 16-dimension embeddings.
… qa scenario The scenario now looks for any stored row that contains the retry turn's "send it now with message(action=send)" instruction, so it also catches a retry whose [System] first line was removed before storage.
|
@clawsweeper re-review Both P2s are addressed at |
|
🦞👀 Re-review progress:
|
Upstream head dcf76a1. It replaces the capture and recall half of the fork's f2c2804 ("keep OpenClaw system-turn prompts out of memory"), which covered the same behaviour: shouldCapture rejects text starting with [System], and recall and duplicate matching skip it. Upstream is reviewing the PR's version, so it becomes the one owner. The PR adds two things the fork lacked: - capturableUserText checks the raw turn for the marker before sanitizing. The fork sanitized first, so a stranded-reply retry that quotes a context block lost its [System] line with the block and stored "Remember ..." as a user memory. - isSystemTurnPromptText allows a leading timestamp envelope before the marker. Changes against the branch: - memory-policy.ts: the PR's isSystemTurnPromptText, the isRecallableMemoryText comment and capturableUserText. - memory-capture-sanitization.ts: the fork's isSystemTurnPromptText is removed; the PR's stripLeadingTimestampPrefix is added. - index.ts: the capture loop goes through capturableUserText. - memory-policy.test.ts: the PR's, unchanged. - extensions/memory-lancedb/system-turn-capture.test.ts (fork) is removed: the PR's memory-policy tests make the same assertions (capture rejects the restart and retry turns, the same words from the user are captured, a timestamped turn is rejected, stored rows leave recall) and add the raw-before-sanitize and duplicate cases. Kept from the fork, because the PR does not do them: - memory-lancedb-system-turn-rows, the doctor-only migration that deletes stored [System] rows (the PR leaves deletion out as a retention change). doctor-contract-api.ts now imports the PR's predicate from memory-policy.ts, so capture, recall and deletion match one predicate; deletion now also matches a row whose [System] marker follows a timestamp envelope, as recall already skips it. - test/memory-lancedb-system-turn-capture.test.ts and the index.ts re-export it uses: it runs core's real formatSystemTurnPrompt through the plugin, so CI catches marker drift. The PR's unit tests copy the marker, and its qa scenario is not in any CI profile. - docs/plugins/memory-lancedb.md already describes this behaviour. Dropped: the PR's qa scenario memory-lancedb-system-turn-capture. It fails on this branch before reaching its assertions. Run on the M4 after pnpm build (mock-openai, --concurrency 1 --fast): "timed out after 180000ms" twice, at the wait for the delivered reply. Here the embedded runner's silent-stop nudge (incomplete-turn-recovery.ts, SILENT_STOP_DELIVERY_RETRY_INSTRUCTION, fork-only) answers the stranded final first. The mock only calls message(action=send) for core's stranded retry text ("you did not call message(action=send)"), so the reply is never delivered, core's [System] retry never reaches the model (2 provider requests per attempt), and the barrier turn is never sent. test-lines allocation: carried, with the count set to this tree's net test-line change (11: +62 in memory-policy.test.ts, -51 for the removed fork test). Proof (M4, Node 26.12.1, --maxWorkers=1): - All 23 memory-lancedb test files except the live one and the root test/memory-lancedb-system-turn-capture.test.ts: passed, in a run that also had qa-lab's scenario-catalog.test.ts and coverage-report.test.ts with the scenario present (26 files, 239 tests, shards 1 + 62 + 130 + 46). Dropping the scenario changes no other file. - Red-before, the four source files restored to the fork variant: memory-policy.test.ts 2 failed, 2 passed of 4. The two failures are isSystemTurnPromptText and capturableUserText missing from memory-policy.ts; the recall and duplicate case passes, because the fork already skipped those rows. - Red-before, capturableUserText with the fork's sanitize-first order (the raw-turn check removed): the context-block case fails with "expected 'Remember the deployment window is Tue...' to be undefined" (1 failed, 3 passed of 4). That is the retry the fork stored.
…ear its stored memories The embedded runner's silent-stop delivery nudge (fork-only, incomplete-turn-recovery.ts) is a user-role turn OpenClaw writes itself, but unlike the fork's stalled-turn, stranded-reply and goal-continuation prompts it was not built with formatSystemTurnPrompt. Its "never" matches memory-lancedb's capture triggers, so with autoCapture on it was stored as a user memory: the openclaw#189956 capture check on this branch returns the nudge text unchanged, and the qa run of that PR's scenario logged "auto-captured 1 memories" right after the nudge fired. Each bot's store holds one such row (mac-mini written 2026-10-27, rh-bot 2026-08-20), as measured by the carry lead. - SILENT_STOP_DELIVERY_RETRY_INSTRUCTION is now formatSystemTurnPrompt(<same text>), so it starts with [System] and the carried capture check and recall skip it. Its users (terminal-resolution.ts and two tests) read the constant; no test or snapshot pins the text. - memory-lancedb-system-turn-rows, the fork's doctor-only cleanup, also deletes rows whose text is exactly the legacy unmarked nudge. The text has not changed since upgrade-v2026.7.2. Exact match only: a row that starts the same way and then differs is kept. The rows already stored go when the deploy runs Doctor (doctor --fix), after the database backup; startup never runs this cleanup. - The plugin entry's fork re-export for the root test adds capturableUserText, the capture check itself. Proof (M4, Node 26.12.1, --maxWorkers=1): - The root test, doctor-contract-api.test.ts, memory-policy.test.ts and the 15 embedded-runner test files that import incomplete-turn-recovery or terminal-resolution (including run.plugin-runtime-refresh.integration.test.ts): 18 files, 231 tests passed. - Red-before, the wrap and the cleanup reverted: the root test's new case fails (capturableUserText returns the nudge text), 1 failed of 2. With only the cleanup reverted, doctor-contract-api.test.ts fails on "delete 2" for "delete 3" (1 failed, 6 passed of 7). - Mutation: matching the nudge by prefix instead of exactly deletes the near-miss row too, and the same test fails on "delete 4". - tsgo:core, tsgo:extensions, tsgo:core:test, tsgo:extensions:test and tsgo:test:root rc=0; oxfmt --check and oxlint on the five files rc=0; check-line-cap-ratchet and check-test-mock-exports with --base the v2026.11.2 tag rc=0; prompt:snapshots:check rc=0 (7 files current).
…e rules pnpm deadcode:report flagged isSystemTurnPromptText and shouldCapture: once index.ts called capturableUserText, both exports had only test importers. userTextForCapture now only drops OpenClaw system turns, checked on the raw text before sanitizing, and returns the sanitized user text. index.ts runs shouldCapture on that text as main does, so shouldCapture keeps its production consumer and isSystemTurnPromptText becomes module-private.
|
@clawsweeper re-review Merged main
Not re-run on this head: the QA scenario |
|
🦞👀 Re-review progress:
|
# Conflicts: # extensions/memory-lancedb/memory-policy.ts
|
@clawsweeper re-review Merged main |
|
🦞👀 Re-review progress:
|

Closes #189955
What Problem This Solves
Fixes: memory-lancedb auto-capture stores OpenClaw's own
[System]turns as user memories when a stranded-reply retry, or asessions_sendrestart note, quotes text that contains a capture trigger.User Impact
User impact: with
autoCaptureon, only what the user said is stored.[System]rows already in a store stop coming back from recall and are no longer used for duplicate matching. They stay on disk and still show inopenclaw ltm list.Why This Change Was Made
Core marks the turns it writes itself with
[System]throughformatSystemTurnPrompt(src/sessions/system-turn-prompt.ts). On main19aff5bfd42its producers are the stranded-reply retry (src/auto-reply/reply/stranded-reply-recovery.ts), the transcript follow-up run (src/auto-reply/reply/transcript-followup-run.ts) andsessions_send(src/agents/tools/sessions-send-executor.ts). I have not shown that every internal turn goes through it. These turns are sent as user-role messages, soextractUserTextContenthands it to capture like any user text. Capture now goes throughcapturableUserText, which checks the raw turn for that marker before sanitizing, because sanitizing can remove the marker line along with a quoted context block. It then appliesshouldCapture, which also rejects the marker.isRecallableMemoryText, which gates both recall and duplicate matching, skips such rows too. The marker is matched in the plugin because plugins cannot import the core helper, and a leading timestamp envelope is allowed before it.Deleting rows already stored is left out. That is a retention change, and recall no longer surfaces them. One tradeoff: a user message that itself starts with
[System]is no longer captured.Evidence
Update 2026-11-25 (head
ae07b96240c)Merged main
9fb2059f340asbfaa318c55d(clean; the branch was 8,755 commits behind) and pushedae07b96240con top. That commit fixes a dead-export failure this PR caused:check-deadcode-exports.mts --allflaggedisSystemTurnPromptText(new) andshouldCapture(no longer imported byindex.ts) inmemory-policy.ts.userTextForCapture(text)now returnsundefinedfor a system turn, checked on the raw text before sanitizing, and otherwise the sanitized text.index.tsis back to main's loop:userTextForCapture, thenshouldCapture.isSystemTurnPromptTextis no longer exported.scripts/e2e/parallels/host-server.ts: detectParallelsHostIpandscripts/e2e/parallels/parallels-vm.ts: listVmNames; main's own scheduled CI fails the same way (run 45654340109 on0b99555d5f7), and this PR touches nothing underscripts/.bfaa318c55d, with main'smemory-policy.ts,memory-capture-sanitization.tsandindex.ts, the PR's test fails 3 of 4, andshouldCapture(strandedRetry)istrue. No main commit has touched the PR's files since9fb2059f340(checked through0f73e04b3f9).expected 'Remember the deployment window is Tue...' to be undefined).memory-policy4/4,index44/44,memory-capture-sanitization28/28,auto-recall2/2,auto-recall.deadline3/3,recall-service2/2.tsgo:extensionsandtsgo:extensions:testpass, with a TS2322 control on each.memory-policy.test.ts --maxWorkers=1atae07b96240c, three runs: 8, 5 and 6 s wall (Vitest duration 6.04, 3.59 and 3.79 s). +60 net test lines.Not re-run on this head: the QA scenario
memory-lancedb-system-turn-capture. Main's catalog still loads it.memory-lancedb-system-turn-capture(in this PR): real gateway, qa-channel, mock-openai,messages.visibleReplies: message_tool, memory-lancedbautoCapture: true.[System]retry is the first capturable text. A later turn in the same conversation,QA-MEMORY-BARRIER 記住 ..., must be stored before the store is read; captures for one session run in order, so the retry's capture has settled by then.send it now with message(action=send)instruction, so it catches the retry whether or not its[System]line survived.eef407423d4: fails after the barrier settles, with[System] Your previous reply was not delivered because you did not call message(action=send). Your reply text was: "QA-STRANDED-85714:...". If the user should see a reply, send it now with message(action=send). If no reply is needed, answer exactly NO_REPLY.stored. The gateway logs twoauto-captured 1 memories: the retry turn and the barrier.f517e265d7f: passes withbarrier rows=1; retry rows=0.node scripts/run-node.mjs qa suite --provider-mode mock-openai --scenario memory-lancedb-system-turn-capture --concurrency 1 --fast, afterpnpm build.pnpm test extensions/memory-lancedb/memory-policy.test.ts --maxWorkers=1: 4 passed.memory-policy.tsfrom main:expected true to be falseonshouldCapture(strandedRetry), and recall returned['stored', 'user'].Remember the deployment window is Tuesdays.". If the user should see a reply, send it now with message(action=send). ...andshouldCapturereturns true.capturableUserTextreturns undefined for it.Sender: ⟦openclaw:ctx⟧and a fenced JSON block, main stored[System] ... Your reply text was: "Remember the deployment window is Tuesdays. QA-STRANDED-85714...with the block already removed and the[System]line intact. The first commit on this branch (sanitize, then check) passed that run. The raw-turn check is defense for other system-turn producers, so the fixture is not in this PR.pnpm tsgo:allexits 0. oxlint and oxfmt are clean on the changed files.3855c896edd: slack 0), sodcf76a14600addsconfig/test-lines-allocations/189956-memory-capture-skip-system-turns.txtwith this PR's exact net test-line growth, 62 lines. They are the tests that pin this change: Memory auto-capture skips OpenClaw's own [System] retry turns. Merged onto3855c896edd,check-test-lines-ratchet.mts --base 3855c896edd --no-merge-basereadsTest lines OK: 6592430; ceiling: 6592430; slack: 0., and an allocation of 63 is refused.pnpm test <file> --maxWorkers=1, Bun, the local default, macOS arm64, two runs each):memory-policy.test.ts2.7 s and 1.5 s wall, 39 ms and 18 ms of tests. CI run 41271447272, Bun:memory-policy.test.ts30 ms of tests in a shared run (checks-node-changed-extensions-bundle-1).