Skip to content

feat(memory): add durable memory_write + memory_upsert tools - #21003

Closed
SmithLabsLLC wants to merge 2 commits into
openclaw:mainfrom
SmithLabsLLC:smithlabs/memory-write-upsert
Closed

SmithLabsLLC wants to merge 2 commits into
openclaw:mainfrom
SmithLabsLLC:smithlabs/memory-write-upsert

Conversation

@SmithLabsLLC

@SmithLabsLLC SmithLabsLLC commented Feb 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • add new durable memory tools: memory_write (append) and memory_upsert (keyed update/insert)
  • wire new tools through runtime + memory-core extension + tool-policy groups
  • update prompt/tool metadata and docs for new memory tool capabilities
  • add/extend tests for memory tool behavior and policy handling
  • serialize same-process concurrent writes with a per-file in-process mutex

Why

This enables explicit, structured durable-memory writes when users ask the assistant to remember or update long-term context, while keeping tool-policy controls explicit.

Validation

  • pnpm vitest run --config vitest.e2e.config.ts src/agents/tools/memory-tool.e2e.test.ts src/agents/pi-tools.policy.e2e.test.ts

Follow-up split

Cross-process file-lock hardening was split into a stacked follow-up PR (per review request):

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation channel: bluebubbles Channel integration: bluebubbles gateway Gateway runtime extensions: memory-core Extension: memory-core docker Docker and sandbox tooling agents Agent runtime and tooling size: M labels Feb 19, 2026

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

16 files reviewed, 5 comments

Edit Code Review Agent Settings | Greptile

Comment thread src/agents/tools/memory-tool.ts Resolved
Comment thread src/agents/tools/memory-tool.ts Resolved
Comment thread src/agents/tools/memory-tool.ts Resolved
Comment thread src/plugins/runtime/index.ts Resolved
Comment thread src/agents/tool-policy.ts Resolved
@SmithLabsLLC

SmithLabsLLC commented Feb 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Pushed follow-up updates addressing review feedback:

  • switched memory tool enum params to optionalStringEnum (no Type.Union in tool schema)
  • added missing schema helper import
  • removed unrelated runtime/tool-policy deltas from this PR so scope stays memory-only
  • kept owner-only and legacy runtime guardrails intact

Also note: the repo currently has baseline format-check drift on two unrelated files in main, which may keep the generic check job red until upstream is normalized.

@nikolasdehor nikolasdehor 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.

Good feature — I run a WhatsApp gateway (Devinho) that uses workspace MEMORY.md and tarefas-devinho.md across cron + user sessions, so durable memory tools are directly useful to me. The core memory_write / memory_upsert implementation is clean and the tests cover the key scenarios well.

However, I have a few concerns that block approval:


1. Concurrency on memory_upsert (read-modify-write without locking)

memory_upsert does readFile → split lines → findIndex → replace → writeFile with no file lock or atomic-write guard. In a gateway setup with cron sessions running concurrently with user sessions (my exact use case), two parallel upserts targeting the same file will race, and one write will silently overwrite the other.

memory_write is slightly better since fs.appendFile is at least append-mode, but it is still not atomic on all platforms.

At minimum, I would expect an O_EXCL-based lockfile or an fs.open + flock-style advisory lock around the read-modify-write cycle. Even a simple per-file async mutex in-process would catch the most common case (two tool calls in the same gateway process).

2. Unrelated security regressions bundled in

a) runCommandWithTimeout guard removed (src/plugins/runtime/index.ts)

The runtimeCommandExecutionGuarded wrapper that enforced plugins.runtime.allowLegacyExec: true is completely deleted. This was an opt-in security gate for legacy plugin exec — removing it in a memory-tools PR without explanation is concerning. If the intent is to deprecate the guard, it should be a separate PR with its own rationale.

b) cron and gateway removed from owner-only tools (src/agents/tool-policy.ts)

OWNER_ONLY_TOOL_NAME_FALLBACKS went from ["whatsapp_login", "cron", "gateway"] to just ["whatsapp_login"]. This means any non-owner sender in allowFrom can now invoke cron (schedule arbitrary jobs) and gateway (manage the gateway). For gateway operators, this is a real privilege escalation. Same as above — if intentional, it deserves its own PR with discussion.

I agree with Greptile's flagging of both of these.

3. Type.Union schema violation

Greptile already flagged this: Type.Union([Type.Literal("daily"), Type.Literal("longterm")]) should be optionalStringEnum per AGENTS.md:185-186. Minor fix.


Suggested path forward

  1. Split out the two security changes (runtime exec guard removal + owner-only policy change) into their own PR(s).
  2. Add a file-level lock (or at least an in-process async mutex) around the upsert read-modify-write cycle.
  3. Fix the Type.Union → optionalStringEnum schema issue.

With those addressed, I would happily approve. The memory tools themselves are well-designed and the daily vs longterm split + keyed upsert semantics are exactly what gateway operators need.

@SmithLabsLLC

Copy link
Copy Markdown
Contributor Author

@nikolasdehor thanks for the detailed review — this was helpful.

I pushed follow-up changes to address the blocking points:

  1. Concurrency for memory_upsert / memory_write
  • Added a per-file in-process async mutex (withMemoryFileLock) keyed by absolute path.
  • Wrapped both append (memory_write) and read-modify-write (memory_upsert) flows with the lock.
  • Added a concurrent e2e case (memory_upsert preserves all concurrent keyed writes to the same file) to verify parallel upserts on the same file keep all entries.
  1. Unrelated security regressions
  • Kept the legacy runtime exec guard in place (runtimeCommandExecutionGuarded remains).
  • Kept owner-only fallback behavior for cron and gateway intact.
  • In short: removed those unrelated deltas from this PR.
  1. Schema guardrail
  • Replaced Type.Union([Type.Literal(...)]) with optionalStringEnum(...) for the new params.

On your lockfile/flock suggestion: agreed it’s important for cross-process safety. This PR now guarantees safety for concurrent tool calls within one gateway process (the most common case), and I’m happy to follow up with a dedicated cross-process locking PR so we can review that separately.

@SmithLabsLLC

SmithLabsLLC commented Feb 19, 2026 •

Copy link
Copy Markdown
Contributor Author

@nikolasdehor update: I split this as requested, so lock hardening is now isolated in a stacked follow-up PR instead of this one.

That keeps this PR focused while still addressing the locking concern in a dedicated diff.

@SmithLabsLLC

Copy link
Copy Markdown
Contributor Author

@nikolasdehor I split this into two PRs as requested:

  1. Upstream part 1 (core memory tools):
  1. Stacked part 2 (cross-process lock hardening only):

Part 2 is intentionally just the lock upgrade on top of part 1 so review scope stays tight.

If you prefer both parts in upstream directly (instead of stacked in the fork), I can mirror this as a second upstream PR after part 1 lands.

@SmithLabsLLC

Copy link
Copy Markdown
Contributor Author

Status snapshot (supersedes earlier progress notes)

If you'd like both parts upstream as separate PRs after #21003 lands, I can open PR-2 against upstream immediately.

@SmithLabsLLC

Copy link
Copy Markdown
Contributor Author

Quick re-review ping 🙏

  • Reb​ased to latest main and force-pushed head c0bf43ba0.
  • CI is green on this head (including the lanes that were previously blocked by shared typecheck drift).
  • Scope remains the same for this PR: durable memory_write + memory_upsert tools plus concurrency-safety coverage.

If you'd like, I can split/reshape any section further to make review easier.

@SmithLabsLLC

Copy link
Copy Markdown
Contributor Author

Follow-up on prior bot review threads:

  • I resolved the five outdated Greptile threads now that those changes are already reflected in the current head (c0bf43ba0).
  • If maintainers want any of those reopened for re-discussion, I can reopen and respond point-by-point.

@openclaw-barnacle

Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle

Copy link
Copy Markdown

Closing due to inactivity.
If you believe this PR should be revived, post in #pr-thunderdome-dangerzone on Discord to talk to a maintainer.
That channel is the escape hatch for high-quality PRs that get auto-closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling channel: bluebubbles Channel integration: bluebubbles docker Docker and sandbox tooling docs Improvements or additions to documentation extensions: memory-core Extension: memory-core gateway Gateway runtime size: M stale Marked as stale due to inactivity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants