Skip to content

fix(memory): require facts in replacement imports - #8131

Merged
ShenAC-SAC merged 4 commits into
bytedance:mainfrom
Wang-Zephyr:fix/memory-import-require-facts
Nov 11, 2026
Merged

ShenAC-SAC merged 4 commits into
bytedance:mainfrom
Wang-Zephyr:fix/memory-import-require-facts

Conversation

@Wang-Zephyr

@Wang-Zephyr Wang-Zephyr commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #8130

Why

The memory import endpoint uses the response schema, which defaults a missing facts field to an empty array. A backup that omits the field therefore returns 200 and clears the selected bucket.

What changed

Require an explicit facts array in the import request. Omitting it now returns 422 before any write. An explicit empty array still clears the selected bucket. Export and read responses retain their existing schema.

Surface area

  • Backend API — request validation for POST /api/memory/import
  • Default behavior change — backups missing facts are rejected

Bug fix verification

backend/tests/test_memory_router.py::test_import_requires_explicit_facts_to_replace_saved_memory failed on bf4aa82f for both the default and named-agent buckets (200 with empty facts), then passed with the fix. The test checks that a rejected import preserves the exported snapshot and that a subsequent explicit empty import clears the selected bucket.

Validation

  • Memory router and backup import suites: 132 passed.
  • Ruff lint, format, and whitespace checks passed.
  • Agent guidance: zero errors and one size warning below the hard limit.
  • Local validation covered the affected suites. Full backend testing is left to CI.

AI assistance

Tool(s) used: OpenAI Codex.

How you used it: Investigated the import boundary, reproduced the behavior, implemented the request schema and tests, and drafted the issue and PR.

  • I've read and understand every line of this change and take responsibility for it — it's not unreviewed AI output.

@Wang-Zephyr
Wang-Zephyr marked this pull request as ready for review November 11, 2026 02:55
@github-actions github-actions Bot added area:backend Gateway / runtime / core backend under backend/ area:docs Documentation and Markdown only needs-validation Touches front/back contract surface; needs real-path validation risk:high High risk: backend API, agents, sandbox, auth, deps, CI size/S PR changes 20-100 lines labels Nov 11, 2026

@willem-bd willem-bd 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.

Reviewed against head f580731. Verified the gate and its preservation property at the head tree (PYTHONPATH-pinned imports, not the local checkout):

  • Gate placement is pre-mutation, confirmed. MemoryImportRequest redeclares facts as required, and FastAPI body validation runs before the handler — a rejected import never constructs the manager or touches the store. Model-level probes at head: missing facts -> 422 (missing), facts: null -> 422 (list_type), facts: [] -> accepted (the documented explicit clear).
  • No bypass found. Whitespace-only content passes the schema but is rejected pre-storage by the updater's existing gate (ValueError -> 400, raised before normalize_memory_data or any storage read; confirmed present at base bf4aa82), so the router schema and updater validation form working defense in depth.
  • Replacement semantics: agent-scoped import is a full replace via the apply_changes change set (upserts + deletes of ids absent from the backup + summary replacement); the default bucket forces facts to []. Consistent with the 409-on-concurrency path.
  • Callers verified compatible: the frontend normalizeMemoryPayload already rejects facts-less backups client-side (default "reject" strategy) and always sends an explicit facts array on import; the benchmark runner seeds facts explicitly; the harness client.import_memory bypasses the router but still hits the updater gate.
  • Tests: backend/tests/test_memory_router.py — 82 passed at the head tree, including the new parametrized test covering rejection (422 + loc pinned), snapshot preservation, and the subsequent explicit clear. ruff format --check and ruff check clean.

Two non-blocking notes below/in the summary: the PR is missing a CHANGELOG.md Unreleased entry (the Fixed section at head carries entries for prior memory fixes in this family, and this is a user-facing API behavior change — previously accepted backups now return 422), and a one-line OpenAPI description tweak is suggested inline.

)
@require_permission("memory", "write")
async def import_memory(body: MemoryResponse, request: Request, agent_name: str | None = None) -> MemoryResponse:
async def import_memory(body: MemoryImportRequest, request: Request, agent_name: str | None = None) -> MemoryResponse:

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.

Nit: the route's description (line 575) predates the new contract. Consider mentioning it so OpenAPI/docs consumers see the rule at the endpoint itself, e.g. "Requires an explicit facts array; omitting it returns 422, while an empty array clears the selected bucket." — the README and routers/AGENTS.md now document it, but this string is what OpenAPI codegen surfaces. Optional since the request schema already marks facts required.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review. In 36ccd82, I updated the endpoint’s OpenAPI description to say that omitting facts returns HTTP 422 and that an explicit empty array clears the selected fact bucket. I also added an Unreleased changelog entry for the behavior change. All 82 memory-router tests passed after the update, and Ruff lint and format checks passed.

@willem-bd willem-bd 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.

Re-reviewed at 36ccd82. Both prior notes are addressed: the OpenAPI description now states the explicit-facts requirement with the 422-vs-empty-clear semantics, and a CHANGELOG Unreleased/Fixed entry documents the behavior change. Gate re-verified intact at the new head — MemoryImportRequest.facts remains schema-required, the delta touches only the decorator string, and the rejection/preservation tests are unchanged. One nit remains on the new changelog lines.

Comment thread CHANGELOG.md

- **memory:** Importing a backup without a `facts` array now returns HTTP 422
and preserves saved memory. An explicit empty array still clears the selected
fact bucket. ([#8131])

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.

Nit: ([#8131]) has no matching link reference — the definitions at the bottom of the file end at [#8072], so this renders as literal text instead of a link. Please add [#8131]: https://github.com/bytedance/deer-flow/pull/8131 to the reference block, following the sibling entries (e.g. [#7794], [#8072]).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added the [#8131] link definition to the changelog’s reference block in 4aa18bc. The Unreleased entry now links to this PR. git diff --check passed.

@ShenAC-SAC
ShenAC-SAC merged commit d9b2cb4 into bytedance:main Nov 11, 2026
17 checks passed
zuizhuai added a commit to zuizhuai/deer-flow that referenced this pull request Nov 12, 2026
Resolve the CHANGELOG.md conflict by keeping upstream's bytedance#8131 memory entry and appending the bytedance#7939 agents entry after it. The nine code and test changes are unchanged from the previous head.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:backend Gateway / runtime / core backend under backend/ area:docs Documentation and Markdown only needs-validation Touches front/back contract surface; needs real-path validation risk:high High risk: backend API, agents, sandbox, auth, deps, CI size/S PR changes 20-100 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] Memory import clears saved facts when the request omits facts

4 participants