Skip to content

fix(agent-memory): server-stamp byteSize + updatedAt on PUT /memory - #189

Merged
lilyshen0722 merged 1 commit into
mainfrom
fix/adr-003-phase1-server-stamp
Apr 14, 2026
Merged

lilyshen0722 merged 1 commit into
mainfrom
fix/adr-003-phase1-server-stamp

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

Summary

Follow-up to ADR-003 Phase 1 (#188). Production smoke test surfaced `byteSize: 0` in GET /memory responses because the route was trusting client-supplied metadata. This patch stamps `byteSize` (UTF-8 byte length of `content`) and `updatedAt` (now) server-side on every section write; client-supplied values for these fields are discarded. Also defaults missing `visibility` to `'private'` at the write layer so ADR-003 invariant #2 ("Private by default") is enforced both schema-side and route-side.

Scope (strict)

  • Server-stamp `byteSize` + `updatedAt` on single-object sections (`soul | long_term | dedup_state | shared | runtime_meta`)
  • Server-stamp `updatedAt` on `relationships[]` entries
  • Don't stamp `daily[]` entries (no byteSize/updatedAt per ADR shape)
  • Document the whole-array-replace semantics for `daily` / `relationships` — Phase 2's `/memory/sync` with `mode: 'patch'` will add element-level merge keyed by `date` / `otherInstanceId`
  • Document that `sections: { long_term: { content: '' } }` is an intentional clear that blanks the v1 mirror

Changes

  • `backend/services/agentMemoryService.ts` — `stampSectionsForWrite(sections, now?)`. Object sections routed through `makeSection`; array sections whole-replaced with per-entry stamp on relationships
  • `backend/routes/agentsRuntime.ts` PUT `/memory` — call the stamper before per-key `$set` merge; expanded docstring codifies merge semantics
  • Tests (13 new; 63/63 agent-memory tests pass)

Reviewer findings addressed

Reviewed by the `code-reviewer` subagent pre-commit. Verdict: Approve with suggestions. Applied fixes:

  • Locked whole-array replace semantics for `relationships`/`daily` with a test (Important fixing github workflow for unit testing #2) — intentional Phase 1 scope; `/memory/sync` adds merge in Phase 2
  • Refactored helper to use `makeSection` instead of inlining (Nit)
  • Added comment explaining the TS cast on `Object.keys(sections)` relies on upstream validation (Nit)
  • Extended idempotence test to cover `daily` + `relationships` (Nit)
  • Added integration test locking the "empty `long_term.content` clears v1 mirror" semantics (Q5)

Follow-ups deliberately deferred

  • Per-entry freshness for `relationships` (stamp only when content changes) — needs Phase 2 `/memory/sync` patch mode to be meaningful
  • Date shape validation (`'YYYY-MM-DD'`) on `daily[]` entries — will land with `/memory/sync` validator

Test plan

  • 63/63 agent-memory tests pass locally
  • Reviewer agent pass with all suggestions addressed
  • CI green
  • Post-merge: rebuild backend, helm upgrade, verify `byteSize` non-zero in live GET

🤖 Generated with Claude Code

Follow-up to ADR-003 Phase 1. Production smoke test showed `byteSize: 0`
in responses because the route was trusting client-supplied metadata.
This patch stamps `byteSize` (from utf-8 content length) and `updatedAt`
(now) server-side on every section write. Client-supplied values for
these fields are discarded. Also enforces ADR-003 invariant #2
("Private by default") at write time by defaulting missing `visibility`
to `'private'`.

Changes:
- backend/services/agentMemoryService.ts — adds `stampSectionsForWrite`;
  single-object sections go through `makeSection` for consistency.
  Whole-array replace semantics for `daily` and `relationships` are
  documented inline (Phase 2's /memory/sync `mode: 'patch'` will add
  element-level merge keyed by `date` / `otherInstanceId`).
- backend/routes/agentsRuntime.ts PUT /memory — calls the stamper
  before the per-key dotted-$set merge; expanded docstring spells out
  merge semantics for each section category, the v1 content mirror
  behavior (incl. that `long_term: { content: '' }` is an intentional
  clear), and that `byteSize` + `updatedAt` are always server-stamped.

Tests (13 new; 63/63 agent-memory pass):
- Unit: stampSectionsForWrite covers single-object stamping, multi-byte
  utf-8 byteSize, client-value override, visibility default + preserve,
  no-sibling-creation, daily shape (no byteSize/updatedAt per ADR),
  relationships shape (updatedAt stamped, no byteSize), idempotence
  under same `now` across object + array sections.
- Integration: byteSize returned end-to-end; multi-section writes each
  get correct per-section byteSize; array sections are whole-array
  replace (lock for Phase 1 intentional semantics); `long_term` clear
  via empty content blanks the v1 mirror.

Reviewer finding (code-reviewer subagent) that `updatedAt` on every
relationship entry bumps to `now` on array replace is intentional and
consistent with whole-array semantics; proper per-entry freshness lands
with Phase 2's /memory/sync.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@lilyshen0722
lilyshen0722 merged commit 55e2511 into main Apr 14, 2026
9 checks passed
@lilyshen0722
lilyshen0722 deleted the fix/adr-003-phase1-server-stamp branch April 14, 2026 18:45
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.

1 participant