Skip to content

Memory: a refused cleanup takes back only its own changes - #509

Merged
lydakis merged 8 commits into
mainfrom
claude/project-thread-2jed7q
Oct 31, 2026
Merged

lydakis merged 8 commits into
mainfrom
claude/project-thread-2jed7q

Conversation

@lydakis

@lydakis lydakis commented Oct 31, 2026 •

Copy link
Copy Markdown
Owner

Fixes #471, flagged as a priority in the day-six external review.

The bug

When the memory loss guard refuses a cleanup, it reverts the cleanup's whole commit. Anything another agent saved or removed while the cleanup was open went into that commit too, so the revert silently took it back. Git still had it in the refused commit, but memory had lost it and no agent was told.

The fix

The guard now takes back only the cleanup's own writes and never another agent's. In effect this is snapshot isolation.

  • cleanup start records who runs the cleanup in .cleanup as by, taken from AGENT_BOT_ID, or none for the person.
  • While a cleanup is open, a save or rm by anyone else appends the fact as it now stands (its text, or null for a removal) to .cleanup-others. The file is append-only JSON lines with mode 0600, and it is git-ignored.
  • On refusal, git revert runs as before. Then each noted fact is put back as its agent last left it, the affected folders are reindexed under the lock, and the result is committed as "Memory saved while a refused cleanup ran". The refusal message now says how many facts were kept.
  • A cleanup that finishes, or a new cleanup start, removes the stale notes file.

The memory-cleanup skill's step 6 says this in one line ("Only your changes go back: what other agents saved meanwhile stays"), and the memory row in sonny/FEATURES.md is updated.

Checks

  • New test a_refused_cleanup_keeps_what_other_agents_saved_meanwhile. Another agent saves one fact, rewrites a second and removes a third during a cleanup the guard refuses. All three of its changes survive, and the cleanup's own changes are reverted.
  • cargo test -p agent-client (80 passed), cargo clippy -p agent-client --all-targets -D warnings and cargo fmt --check all pass.

Perf: this changes only the memory script (client crate) and adds no daemon path. A save outside a cleanup costs one extra stat of .cleanup.

A parallel batch records results in call order, so a later call's outcome
can wait in the batch's buffer, its output file already written, behind an
earlier call still running. Once written, the file names left the cleanup
guard for a plain vector in the outcome: stopping the turn then dropped the
outcome and left the file with no artifact row to find it by, so neither
prune nor delete removed it.

`Outcome.files` is now `Files`, which removes its files when dropped
unless the store recorded them, as a temporary file goes unless kept.
`tool_finish` and the background `process_finish` mark them recorded once
their savepoint commits. Copies share the files through an Arc, so the
held answer a group's rollback snapshot clones doesn't remove them twice;
no files allocates nothing. The keeper's own guard folds into it.

New runtime test: a batch with a slow call beside a large-output one,
stopped after the second wrote its file, leaves no file; it fails on main.

Flaky tests sync on events instead of tight wall-clock bounds:
- #502: the two sandboxed commands each wait for the other's mark, so
  they pass only by running at once, with no 1.1 s bound.
- #484: the lapse test waits for the answered call to start and gives the
  gate two seconds instead of 100 ms.

Closes #484. Closes #502.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013jkdGFBzSEQmnVuCCNV83g
…-2jed7q

# Conflicts:
#	docs/DAEMON_MEASUREMENTS.md
…-2jed7q

# Conflicts:
#	docs/DAEMON_MEASUREMENTS.md
The loss guard reverted the cleanup's whole commit, and with it any fact
another agent saved or removed while the cleanup was open: git kept it in
the refused commit, but memory lost it and nothing told the agent.

`cleanup start` now records which agent runs the cleanup (AGENT_BOT_ID, or
none for the person). A save or removal by anyone else while it is open is
noted in `.cleanup-others` as the fact now stands. When the guard refuses
the cleanup, those facts are put back over the revert, each as its agent
last left it, their folders reindexed and committed, and the refusal says
how many were kept. Snapshot isolation in effect: the guard takes back the
cleanup's writes, never another writer's.

Closes #471.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013jkdGFBzSEQmnVuCCNV83g
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-31T08:08:50.154126Z f311858 New commits
🔒 Security Review ✅ Completed 2026-10-31T08:11:10.432413Z f311858 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 676cc50e17

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread client/src/memory.rs Resolved
Comment thread client/src/memory.rs Resolved
Comment thread client/src/memory.rs Resolved
…wner, say when kept facts overfill the index

Review fixes on the refused-cleanup put-back:
- A save or rm hands the cleanup journal the fact as it wrote it while
  the folder is still locked, instead of rereading the file after the
  lock is gone, where the cleanup could have changed it in between.
- The cleanup's owner is AGENT_STORE#AGENT_BOT_ID: bot ids are each
  store's own, and stores under one HOME share its memory.
- When the put-back leaves a folder's index past its limit (the
  cleanup's removals made room for saves the revert brings back
  beside), the refusal says so; nothing saved is dropped to fit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013jkdGFBzSEQmnVuCCNV83g

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ae9b6cfb6d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread client/src/memory.rs Resolved
…rt and put-back

A save between put_back reading the journal and replaying it was
overwritten with the older noted text, and a save during the revert
itself could be clobbered. The finish now takes every folder's lock,
in sorted order, from its commit to its end; saves and removals wait
and land after.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013jkdGFBzSEQmnVuCCNV83g
@lydakis
lydakis merged commit 76c4b2a into main Oct 31, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory cleanup: a save made during the cleanup is reverted with it when the loss guard trips

2 participants