Repository navigation
feat(memory): add durable memory_write + memory_upsert tools - #21003
SmithLabsLLC wants to merge 2 commits into
Conversation
|
Pushed follow-up updates addressing review feedback:
Also note: the repo currently has baseline format-check drift on two unrelated files in main, which may keep the generic |
nikolasdehor
left a comment
There was a problem hiding this comment.
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
- Split out the two security changes (runtime exec guard removal + owner-only policy change) into their own PR(s).
- Add a file-level lock (or at least an in-process async mutex) around the upsert read-modify-write cycle.
- Fix the
Type.Union→optionalStringEnumschema 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.
|
@nikolasdehor thanks for the detailed review — this was helpful. I pushed follow-up changes to address the blocking points:
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. |
|
@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. |
|
@nikolasdehor I split this into two PRs as requested:
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. |
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. |
|
Quick re-review ping 🙏
If you'd like, I can split/reshape any section further to make review easier. |
|
Follow-up on prior bot review threads:
|
|
This pull request has been automatically marked as stale due to inactivity. |
|
Closing due to inactivity. |
Summary
memory_write(append) andmemory_upsert(keyed update/insert)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.tsFollow-up split
Cross-process file-lock hardening was split into a stacked follow-up PR (per review request):