Skip to content

Flag a turn that creates two unlinked schemas - #2915

Merged
mstomar125 merged 3 commits into
mainfrom
issue-2839-linked-pair-guard
Sep 23, 2026
Merged

mstomar125 merged 3 commits into
mainfrom
issue-2839-linked-pair-guard

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

Closes #2839.

Problem

Relaxing the second-create_schema guard (PR #2818) made a new end state reachable: the user asks for a linked pair, both types get created, and the relationship is never declared — two types, no edge, no error. The old guard refused any second create_schema in a turn outright, so this couldn't happen; narrowing it to refuse only types the user never named is what lets a legitimately-requested pair land as two sequential calls, but nothing requires either call to actually declare the relationship back to the other.

targetType: None stays deliberately unvalidated (validate_relationship_targets_exist, packages/core/src/schema/mod.rs) — a relationship-free schema is legitimate on its own — so nothing previously noticed when both halves of a requested pair land without one.

Fix

At turn end (LocalAgentServiceImpl::run_ai_chat_turn, packages/daemon/src/services/local_agent_service.rs), a new unlinked_pair_note helper:

  • Collects every schema created this turn from completed_writes_from(&result.tool_calls_made).
  • If fewer than two, does nothing.
  • Otherwise re-fetches each schema from storage and checks whether any relationship connects two of them (checked both directions, and against relationships added via a same-turn update_schema, not just ones declared at create_schema time).
  • If none do, appends a short note to the turn's response text: "Note: this turn created N new types (...) and none of them declares a relationship to any of the others. If they were meant to be linked, ask me to add the relationship."

This deliberately does not try to infer from the user's phrasing whether a linked pair was actually requested — the issue itself calls that signal the hard part, and guessing wrong risks nagging over two independently-requested types. Instead it surfaces the weaker, unambiguous fact (multiple new types, zero edges between them) as an informational note, leaving the decision to the user or a follow-up turn.

Scope note: the second-create_schema guard and the agent's turn-loop live in packages/agent/src/local_agent/agent_loop.rs, which is out of scope for this change. This fix lives entirely in the daemon's turn-completion path instead, re-fetching persisted schema state rather than needing any change to the guard or the ReAct loop.

Tests

Three new tests in packages/daemon/src/services/local_agent_service.rs, using the real handle_create_schema pipeline against an isolated NodeService://

  • unlinked_pair_note_flags_two_schemas_created_without_a_relationship — two schemas, no relationship → note naming both.
  • unlinked_pair_note_is_silent_when_the_pair_is_linked — second schema declares the relationship back to the first → no note.
  • unlinked_pair_note_is_silent_for_a_single_schema — one schema created → no note.

Verification

  • cargo test -p nodespace-daemon --lib — 245 passed, 0 failed (242 pre-existing + 3 new)
  • bun run quality:fix — clean (clippy -D warnings, fmt, tsc, design-tokens, skill)
  • Pre-push gate (bun run test:all, cargo build --bin nodespaced, bun run test:e2e) — passed in full

🤖 Generated with Claude Code

mstomar125 and others added 3 commits September 23, 2026 14:38
Relaxing the second-create_schema guard (PR #2818) made it possible for
a legitimately-requested linked pair to land as two sequential calls
that never declare a relationship to each other -- targetType: None is
intentionally unvalidated (a relationship-free schema is legitimate on
its own), so two unlinked types was a silent, valid outcome.

At turn end, re-fetch every schema created this turn and check whether
any relationship connects two of them. When two or more were created
and none do, append a short note to the assistant's response rather
than guess at user intent from phrasing.

Closes #2839.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Per repo convention, code comments describe behavior/constraints
directly rather than embedding GitHub issue or PR numbers.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Address review: per-schema orphan detection instead of a single
existential check over the whole batch. With three or more schemas
created in a turn, two of them being linked to each other used to
suppress the note even when a third was a genuine orphan. Now every
created schema is checked individually against the set, and the note
names only the orphan(s) when the batch is partially linked.

Also log a genuine re-fetch failure (a real NodeService error, not
"schema not found") via tracing::warn!, matching this file's other
error arms, instead of collapsing it into the same silent return as
the benign case.

Addresses reviewer recommendations from PR review.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@mstomar125

Copy link
Copy Markdown
Contributor Author

Addressed `/pragmatic-code-review` findings:

  • Per-schema orphan detection. The original `any_linked` check was a single existential test over the whole batch — with 3+ schemas created in one turn, any one linked pair suppressed the note even if another schema in the same turn was a genuine orphan. Replaced with a per-schema check: each created schema is marked linked the moment it's connected (either direction) to another schema in the set, and the note now names only the orphan(s) when the batch is partially linked. New regression test: `unlinked_pair_note_flags_the_orphan_when_only_some_of_three_are_linked`.
  • Logged re-fetch errors. A genuine `NodeService` error while re-fetching a just-created schema was silently collapsed into the same "stay quiet" path as the benign "schema not found" case, with no log line — unlike this file's other error arms. Now logs via `tracing::warn!(schema_id, error, ...)` and excludes just that schema from the check rather than risk a false claim about a state no longer confirmable.

Full pyramid re-verified: `cargo test -p nodespace-daemon --lib` (243 passed), `bun run quality:fix` clean, pre-push gate passed in full.

Re-Review Decision

Decision: NO RE-REVIEW NEEDED

Rationale: Both changes are localized to `unlinked_pair_note` itself (no change to its call site, signature, or the surrounding turn-completion flow). The fixes are straightforward — a batch-wide `any()` replaced with a per-schema linked-set computation, and a bare `Err(_)` arm split into a logged case — verified by the existing tests plus one new regression test targeting the exact scenario the review found. No new architectural surface, no new external interaction.

@mstomar125
mstomar125 merged commit 26a870f into main Sep 23, 2026
@mstomar125
mstomar125 deleted the issue-2839-linked-pair-guard branch September 23, 2026 13:19
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.

A linked-pair request can now end with both types created but unlinked

1 participant