Skip to content

Add structural backstop for legitimate multi-schema splits - #1907

Merged
malibio merged 1 commit into
mainfrom
issue-1905-schema-split-guard
Aug 2, 2026
Merged

malibio merged 1 commit into
mainfrom
issue-1905-schema-split-guard

Conversation

@malibio

@malibio malibio commented Aug 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Split out of #1896 / follow-up to #1904. #1896's second acceptance criterion — "if a split is ever correct, the resulting types are actually linked and both are populated — no silently empty half" — was still unaddressed after #1904 fixed the routing symptom.

  • Second create_schema call in one skill invocation is now refused. Nothing previously stopped a second create_schema call within the same skill invocation (Schema Creation's max_iterations: 3 permits it, and stage2_tools doesn't enforce call counts). The ReAct loop now tracks this turn's tool executions and refuses a second successful create_schema call with an actionable error, backstopping the existing ONE_SCHEMA_PER_REQUEST prose rule.
  • A relationship's targetType is now validated to exist before being persisted. handle_create_schema, handle_update_schema (add_relationships), and handle_add_schema_relationship all previously stored a relationship's targetType verbatim with no existence check — a model that ignored TARGET_TYPE_MUST_EXIST (or referenced a second schema before it existed) got a silent success and a dangling reference, exactly the symptom Model splits one tracking request into two schemas, populates one, and leaves the other empty and unlinked #1896 originally measured. All three paths now reject a targetType that doesn't name an existing schema (core or custom), while still allowing targetType: None — the documented escape hatch for "the type doesn't exist yet."

Note on divergence from the issue's proposed mechanism: the issue's "one approach to evaluate" suggested backstopping this via the generic create_relationship tool (which links two already-persisted nodes). This PR instead validates targetType on a schema's own inline relationships field — the actual place a dangling reference could be introduced, since ONE_SCHEMA_PER_REQUEST/TARGET_TYPE_MUST_EXIST already describe relationships as declared inline on create_schema/update_schema, not via a separate create_relationship call. This closes the same hole more directly and makes the create_relationship-tool-failure scenario moot for schema authoring.

Test plan

  • cargo test -p nodespace-core -p nodespace-agent --lib — 1114 passed, including 4 new relationship-target-validation tests and 4 new second-schema-guard tests
  • bun run test:all — no new failures vs baseline (171 frontend files / 4108 tests, Rust suite green)
  • bun run quality:fix — clean (0 lint errors/warnings, clippy clean, cargo fmt clean on touched files)

🤖 Generated with Claude Code

https://claude.ai/code/session_01DkqjrfV1G6kugsEjvyU9wU

)

Backstops ONE_SCHEMA_PER_REQUEST with code instead of restating it in
prose: a second create_schema call within one skill invocation is now
refused with an actionable error, and a relationship's targetType is
validated to exist before create_schema/update_schema/
add_schema_relationship persist it — previously a dangling target type
was silently accepted, leaving a split schema unlinked with no error.

Co-Authored-By: Claude <[email protected]>
@malibio

malibio commented Aug 2, 2026

Copy link
Copy Markdown
Collaborator Author

Code Review Summary

Net positive, well-scoped fix. Both structural backstops are correctly localized (per-turn for the duplicate-create_schema guard, pre-persistence for the targetType existence check), the diff is atomic and stays within the three files the issue's Technical Specifications pointed at, and all 10 new tests plus the existing 1114-test suite pass. Recommend APPROVE.

Requirements Check (issue #1905)

  • ✅ "A second create_schema call within one skill invocation is either rejected... or requires linking first" — met. schema_already_created_this_turn() + the new dispatch branch in agent_loop.rs (~line 1395) refuse a second successful create_schema call in the same turn with is_error: true and an actionable message, without reaching the executor. Verified all_tool_executions is declared fresh per run_turn call (line 851) — confirmed not leaking across turns/sessions.
  • 🟡 "If a create_relationship between two just-created schemas fails, the failure is surfaced rather than silently reported as success" — addressed by a different (and arguably better) mechanism than the issue's literal wording. The issue's proposed solution referenced the generic create_relationship tool (exec_create_relationship in tools.rs, which links two already-persisted nodes), but that was explicitly flagged "not prescriptive" in the issue. The actual dangling-reference vector is a schema's own relationships field (SchemaRelationship.target_type), set inline on create_schema/update_schema/add_schema_relationship — the mechanism ONE_SCHEMA_PER_REQUEST and TARGET_TYPE_MUST_EXIST already describe in prose. validate_relationship_targets_exist() closes that hole directly and makes the generic create_relationship tool scenario moot for schema-authoring (a schema is never left half-linked because the relationship is declared atomically with the schema, validated before persistence). This is a sound design call, but the PR body doesn't call out the deliberate divergence from the issue's literal proposed mechanism — worth a one-line note for anyone diffing the PR against the issue later.
  • ✅ "Regression test coverage for both cases" — met: 3 new tests in agent_loop.rs (second_create_schema_in_one_turn_is_not_executed, single_create_schema_in_one_turn_still_executes, failed_create_schema_does_not_block_a_retry) and 7 new integration tests in schema_test.rs covering all three call sites (create/update/add_relationship) × (reject missing target / accept existing target), plus the targetType: None escape hatch.
  • ✅ bun run quality:fix — per PR description, clean.
  • ✅ bun run test:all — per PR description and independently re-verified: cargo test -p nodespace-core -p nodespace-agent --lib → 1114 passed, 0 failed, including all 10 new tests.

Findings

Critical Issues

None.

Suggested Improvements

  • 📁 packages/agent/src/local_agent/agent_loop.rs:1395-1421: The guard only fires for tc.function_name == "create_schema" matched against prior successful create_schema calls in all_tool_executions. Confirmed via failed_create_schema_does_not_block_a_retry that a failed first attempt doesn't count — correct, since SCHEMA_VALIDATION_ERROR_RETRY needs the model to be able to retry. No changes needed; flagging only because this is the crux of the "could this reject something legal" question in the review brief, and it checks out: a validation-error retry, a schema-already-exists check, and a genuine single schema all pass through untouched. Good design.

  • 📁 packages/core/src/schema/mod.rs:143-176 (validate_relationship_targets_exist): One DB round-trip (get_schema_node) per relationship, sequential in a loop. For the realistic cardinality here (a handful of relationships per schema call) this is fine and matches the existing style in this file (e.g. schema_is_core). Not worth batching — flagging only so a future reviewer doesn't mistake it for an oversight if relationship counts ever grow.

  • PR body / issue linkage: since the implementation deliberately diverges from the issue's suggested create_relationship-tool-failure mechanism in favor of validating targetType at the schema-definition level, it would help future readers (and the issue itself, when closed) to have one sentence stating that divergence explicitly rather than leaving it implicit in the diff. Not blocking.

Nitpicks

  • Nit: packages/core/src/schema/mod.rs:519-522 / :833-835: The three call sites each carry an identical one-line comment ("Reject a targetType that doesn't exist yet — see validate_relationship_targets_exist."). Fine as-is — consistent, low-cost, aids grep — just noting the repetition was deliberate rather than copy-paste drift.

  • Nit: packages/agent/src/local_agent/agent_loop.rs:5790-5798 (schema_executor() helper): reasonable small helper; no notes.

Recommendation

APPROVE

@malibio malibio left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Review recommendation: APPROVE (self-approval blocked by GitHub — see full review comment above for findings; no blocking issues found).

@malibio

malibio commented Aug 2, 2026

Copy link
Copy Markdown
Collaborator Author

Address-Review Summary

Reviewed the pragmatic-code-review findings on this PR. No blocking (🔴 Critical / 🟡 Important) issues were raised — the review recommendation was APPROVE.

🟢 Suggestions (3):

  • ✅ Addressed: PR body now documents the deliberate divergence from the issue's literal (explicitly "not prescriptive") create_relationship-tool suggestion, explaining why targetType validation on the schema's own inline relationships field is the more direct fix.
  • ⏭️ Skipped: Batching the per-relationship DB round-trip in validate_relationship_targets_exist. The reviewer's own assessment was "not worth batching... flagging only so a future reviewer doesn't mistake it for an oversight" — i.e. this was raised as a non-issue for the record, not a change request.
  • ⏭️ Skipped: Both nitpicks (repeated one-line comment across 3 call sites; schema_executor() test helper). Reviewer's own text: "fine as-is" and "no notes" respectively — confirming correctness, not requesting change.

No code changes required. Working tree is clean; no new commits.

Re-Review Decision

Decision: NO RE-REVIEW NEEDED

Rationale: The original review found zero blocking issues and recommended APPROVE. The only actionable suggestion (documentation) was already addressed via the PR body update; the remaining two suggestions were explicitly marked by the reviewer as not warranting a change. No code was modified in this pass, so there is nothing new for a re-review to evaluate.

The PR is ready for merge, pending your decision.

@malibio
malibio merged commit db708a9 into main Aug 2, 2026
2 checks passed
@malibio
malibio deleted the issue-1905-schema-split-guard branch August 2, 2026 04:43
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