Skip to content

memory.apply refuses unknown keys in nested target and action objects - #2171

Merged
zlareb1 merged 3 commits into
ClickHouse:mainfrom
Suneeth12:fix/2155
Dec 5, 2026
Merged

zlareb1 merged 3 commits into
ClickHouse:mainfrom
Suneeth12:fix/2155

Conversation

@Suneeth12

@Suneeth12 Suneeth12 commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #2155

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in a release)

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

memory.apply refuses unknown keys inside target (filter, search, keys) and its action blocks (expire, lock, label, demote) with a 422 VALIDATION_FAILED naming the dotted path, on both dry runs and enforcing calls, before reading or writing any rows.

Why

The seven nested input models in server/memory/api/models_apply.py (ApplyFilter, ApplySearch, ApplyTarget, ApplyExpire, ApplyLock, ApplyLabel, ApplyDemote) kept Pydantic's default extra="ignore". Because server/transport/arg_check.py only inspects top-level keys, an unknown predicate such as target.filter.pinned: false, a misspelled filter key (target.filter.tag_any), a misspelled search floor (target.search.min_similarity), or a wrong action key (label.op: "remove") was dropped before resolve ran. The call then read and wrote a wider set of rows (or applied the default label mode) without naming the dropped key.

Adding model_config = ConfigDict(extra="forbid") to all seven nested input models mirrors KnowledgeSourceCrawlOptions and NamespaceRetrievalDefaults: both HTTP (422 VALIDATION_FAILED) and MCP (isError: true) now refuse the call before authorization or handler execution and report the dotted path (target.filter.pinned, label.op, etc.) on both dry_run=true and dry_run=false. A filter carrying only unknown keys fails with extra_forbidden at that key's path instead of "filter needs at least one predicate".

Because arg_check.known_fields returns None for extra="forbid" models and arg_feedback._nested_model previously checked only direct BaseModel annotations one level deep, setting extra="forbid" on ApplyTarget broke arg_feedback.guesses for nested keys. known_fields now accepts include_forbidden: bool = False (leaving default unknown_args and top-level ignored_args behavior unchanged), and arg_feedback.guesses / example walk optional (T | None) and Annotated nested models recursively with a seen cycle guard while leaving shaped() (loom_op catalog) unchanged.

Scope and risk

  • Touches server/memory/api/models_apply.py (the 7 nested input models only), server/transport/arg_check.py (known_fields(..., include_forbidden=False)), server/transport/arg_feedback.py (recursive nested guesses / example), server/transport/mcp_descriptions.py (trim repeated nested field enumerations from memory.apply), and focused unit tests (tests/unit/test_memory_apply.py, tests/unit/test_arg_feedback.py).
  • MemoryApplyInput stays on default extra="ignore", so top-level transport/identity keys, ignored_args / X-Loom-Ignored-Args for unmapped top-level keys, and 422 UNKNOWN_ARGUMENT for top-level write misspellings are unchanged.
  • tests/unit/test_arg_feedback.py::test_memory_apply_names_the_nested_key_and_shows_the_corrected_call now asserts "target.memory_keys: Extra inputs are not permitted" instead of "target must name exactly one of keys | filter | search", because Pydantic rejects the extra key directly before _exactly_one runs.
  • tests/unit/test_mcp_listing_budget.py stays untouched at its 6_000 ceiling: trimming the repeated filter and action field lists from server/transport/mcp_descriptions.py pays for the seven "additionalProperties": false entries, bringing ("core", False) to 5,974 <= 6,000.
  • Deliberately leaves other operations' nested models (knowledge.query, memory.record.query, memory.bulk_set) untouched.

Compatibility, rollout and rollback

  • Wire: memory.apply now returns 422 VALIDATION_FAILED (extra_forbidden) instead of silently dropping unknown keys inside target, target.filter, target.search, expire, lock, label, or demote. During a rolling upgrade where a newer release adds a predicate, an older replica running this change refuses the unknown predicate rather than sweeping without it.
  • Operator steps: none.
  • Rollback: reverting restores the prior extra="ignore" behavior; no stored data, migrations, or generated files are affected.

Tests

  • Focused unit, MCP surface, and contract tests on 9d360926a: tests/unit/test_mcp_listing_budget.py, tests/unit/test_mcp_initialize_surface.py, tests/unit/test_hidden_op_says_why.py, tests/unit/test_memory_apply.py, tests/unit/test_arg_feedback.py, tests/unit/test_sdk_go_codegen.py, tests/unit/test_op_transport_contract.py, tests/unit/test_tunable_constants.py, tests/unit/test_transport_layering.py (365 passed), plus tests/unit/test_public_copy.py (6 passed).
  • Reproduction script from the issue confirms calls 1–4 (target.filter.pinned, target.filter.tag_any, target.search.min_similarity, label.op) refuse with VALIDATION_FAILED (extra_forbidden), call 5 (top-level pinned) serves with ignored_args: ['pinned'], and call 6 (top-level dryrun) refuses with UNKNOWN_ARGUMENT.
  • Local gates passing: ruff check and ruff format --check on all 6 modified Python files, mypy on all 4 modified server files, scripts/gates/function_length_gate.py, scripts/gates/check_no_secrets.py, and --check on all 14 codegen gates (sdk_go_gen, sdk_rust_gen, sdk_rust_gen --fixtures, sdk_py_gen, sdk_py_client_gen, sdk_py_admin_gen, sdk_ts_gen, agent_plugin_sync, standalone_skills, served_skills, settings_reference, settings_schema, docs_reference, and op_reach_census).
  • Left for CI: full make test-unit and integration tests (no local ClickHouse).

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Antigravity. Verified all 7 nested models, arg_check / arg_feedback recursion and retry-example safety, HTTP and MCP dry_run=true/false refusals, MCP listing budget delta, and all local tests and lint/type/codegen gates.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • Local format, lint, type checks and focused tests pass. The full make test-unit and applicable integration results are reported by CI; a full local run is not required before pushing.
  • Generated files need no change (sdk_go_gen, sdk_rust_gen, sdk_py_gen, sdk_py_client_gen, sdk_py_admin_gen, sdk_ts_gen, docs_reference, and op_reach_census --check all pass).
  • No migration.
  • No plugin-shipped file changed.
  • No frontend change.
  • Docs need no change.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers (tests/unit/test_public_copy.py passes).
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.
  • I signed the Contributor License Agreement (CLA) if CLA assistant requested a signature.

Note

Medium Risk
Changes validation for memory.apply nested inputs—clients that relied on silently ignored typos (e.g. tag_any, label.op) now get 422s instead of broader or default behavior—but scope is limited to that op's nested models plus arg feedback helpers.

Overview
memory.apply now rejects unknown keys inside nested target and action payloads instead of silently dropping them, which could widen filters or apply wrong defaults. Seven nested Pydantic models (ApplyFilter, ApplyTarget, expire/lock/label/demote blocks, etc.) get extra="forbid", so HTTP and MCP return 422 VALIDATION_FAILED with dotted paths (e.g. target.filter.pinned, label.op) before any handler runs.

Argument feedback is updated to match: known_fields(..., include_forbidden=True) can list legal keys on forbid-models for nested typo detection; guesses and example recurse through optional/Annotated nested objects so deep misspellings get “did you mean” hints and corrected examples when safe. The memory.apply MCP blurb is shortened to offset schema size. Tests cover nested extra_forbidden, transport refusal before writes, and deep nested feedback behavior.

Top-level memory.apply still ignores unknown keys (ignored_args / UNKNOWN_ARGUMENT for close top-level typos) unchanged.

Reviewed by Cursor Bugbot for commit 248aa41. Bugbot is set up for automated code reviews on this repo. Configure here.

@vercel

vercel Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
loom-docs Ready Ready Preview Dec 5, 2026 12:32pm UTC
loom-telemetry Ready Ready Preview Dec 5, 2026 12:32pm UTC

Request Review

@cla-assistant

cla-assistant Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@zlareb1 zlareb1 self-assigned this Dec 4, 2026

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nested target and action blocks reject unknown keys before the handler can read or change rows. HTTP and MCP both exercise dry runs and enforcing calls. Top-level argument handling stays intact, and MCP retry examples are validated after the proposed corrections.

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The Go SDK generation gate fails on this head and passes on its exact base.

Comment thread server/memory/api/models_apply.py Resolved
@Suneeth12

Copy link
Copy Markdown
Contributor Author

Updated in ec3af00: sdk/go/wire/models.gen.go now carries the updated registry schema SHA-256 (no Go struct or client code changed).

zlareb1
zlareb1 previously approved these changes Dec 4, 2026

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The Go SDK generator matches the committed wire models. The focused suite passed 267 tests, including HTTP and MCP rejection of unknown nested keys and validation of the corrected retry examples.

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

GitHub reports a merge conflict in sdk/go/wire/models.gen.go. Please reconcile with main and regenerate the Go SDK from the combined wire models.

@Suneeth12

Copy link
Copy Markdown
Contributor Author

@zlareb1 Rebased onto main (db86afa) and regenerated sdk/go/wire/models.gen.go in c308ceb; sdk_go_gen --check and the focused unit tests pass locally.

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Both transports refuse the nested unknown keys before entering the handler. The Go generator gate passed, and 234 focused tests passed. The default MCP listing exceeds its existing budget.

Comment thread tests/unit/test_mcp_listing_budget.py Resolved
The nested input models of memory.apply kept Pydantic's default
extra="ignore", so an unknown predicate such as target.filter.pinned or
a misspelled key such as target.filter.tag_any or label.op was dropped
silently and the call acted on a wider set of rows or a default mode.

All seven nested input models (ApplyFilter, ApplySearch, ApplyTarget,
ApplyExpire, ApplyLock, ApplyLabel, ApplyDemote) now set
ConfigDict(extra="forbid"), matching KnowledgeSourceCrawlOptions and
NamespaceRetrievalDefaults. MemoryApplyInput stays on default
extra="ignore" so top-level transport keys, ignored_args and
UNKNOWN_ARGUMENT refusals are unchanged.

arg_check.known_fields accepts include_forbidden=True and
arg_feedback.guesses/example walk optional and annotated nested models
recursively so MCP validation errors still name unknown nested paths
and suggest close field names without emitting retry examples that drop
unmapped keys. Trimming repeated nested field lists from memory.apply's
TOOL_DESCRIPTIONS entry keeps the default MCP listing under the 6,000
ceiling, and sdk/go/wire/models.gen.go carries the updated registry
schema SHA-256.

Signed-off-by: Suneeth12 <suneethreddy2005@gmail.com>
zlareb1
zlareb1 previously approved these changes Dec 5, 2026

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The nested target and action models reject unknown keys before either transport enters the handler. The original MCP listing ceiling passed, and the Go generator reports both files current. All 234 focused tests passed.

Signed-off-by: Suneeth12 <suneethreddy2005@gmail.com>
@Suneeth12

Copy link
Copy Markdown
Contributor Author

@zlareb1 Merged main (3990e8f) into fix/2155 and regenerated sdk/go/wire/models.gen.go in 5f5862d (sdk_go_gen --check and 234 focused tests pass). Maintainer edits are enabled on this branch if main moves again before merge.

zlareb1
zlareb1 previously approved these changes Dec 5, 2026

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

HTTP and MCP reject unknown keys in nested targets and actions before the handler runs. All 234 focused tests, including the MCP listing budget, passed. The Go generator reports both committed files current. GitHub reports a merge conflict in sdk/go/wire/models.gen.go.

… digest

Signed-off-by: Suneeth12 <suneethreddy2005@gmail.com>
@Suneeth12

Copy link
Copy Markdown
Contributor Author

Thanks for #2250 @zlareb1 — merged main (87a15f9) in 248aa41 so sdk/go/wire/models.gen.go is no longer touched by this PR. sdk_go_gen --check and all 234 focused tests pass.

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Both transports reject unknown nested fields before the handler runs. The corrected retry examples, the existing MCP listing budget, and the Go generator checks passed in the 279 focused tests.

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The request SLI test reports the same missing _role entry on this run's clean base. The focused test passes on current main after the fixture repair in #2258. Failed job · Repair

@zlareb1
zlareb1 merged commit c35d187 into ClickHouse:main Dec 5, 2026
77 of 80 checks passed
@Suneeth12
Suneeth12 deleted the fix/2155 branch December 5, 2026 17:23
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.apply drops an unknown target.filter key and acts on more rows than asked

2 participants