Skip to content

Declare context paths on a schema, derive a node's skills from the queries it matches, and return a versioned context read - #3614

Merged
mstomar125 merged 5 commits into
mainfrom
issue-3557-context-paths
Oct 5, 2026
Merged

mstomar125 merged 5 commits into
mainfrom
issue-3557-context-paths

Conversation

@malibio

@malibio malibio commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

Closes #3557. Implements ADR-094 §2 (context paths), §4 (a node's skills are derived from the node) and §7 (a read has a version).

What changes

Context paths on a schema

  • SchemaNode carries contextPaths (RelationshipPath[]), stored in the schema node's properties.
  • update_schema takes add_context_paths / remove_context_paths, as a list of hops or in the dotted form node context --path takes. Each path is resolved from the type through the schemas before anything is written; a refusal names the path.
  • A type's context paths are its ancestors' and then its own (NodeService::resolve_context_paths); schema get reports that set. A path is declared once in a chain and removed where it is declared.
  • Works on core schemas. Core schemas are created once and not rewritten on open, so a user's paths survive a restart and a re-seed; a test reopens the database to show it.
  • A generic node update that changes a schema's context paths is refused.

Applicable skills

  • A context read returns the skills attached to the node, to each saved query the node currently matches, and to each node a path reached. Each skill once, with attached_to and matched_queries.
  • Membership is asked per read, only of saved queries that have a skill attached (SkillQueries), each as stored: relative dates read on the day, no run-time narrowing, sorting and limit ignored.

Version

  • A context read returns a version: a digest of the id and version of every node returned, of what each path reached, and of each skill's content and what it was reached through. --version-only / version_only returns it alone.

Run with context

  • query run --with-context and run_query with with_context return each item as a context read returns it, with each skill once. The limit applies to items; at most 50.

Node type change (node type sequence)

  • Type: schema (the meta-type; flat properties, no schema of its own).
  • Added: one declaration, contextPaths. No field, relationship, structural rule or subtype of any other core type changes. No core schema is seeded with a path.
  • TypeScript regenerated (schema-node.ts, update-schema-params.ts, schema-update-output.ts).
  • Docs: components/node-types.md (the schema sheet), components/schema-management.md (Context paths), components/seeded-nodes.md, architecture/prompt-skill-system.md, architecture/relationship-paths.md in nodespace-docs, pushed with this merge.

Decisions worth a look

  • create_schema takes no context paths. A path is checked against stored relationships, so it is added by update_schema once they exist. For the same reason a path over a relationship the same call adds is refused.
  • A context path whose relationship is later removed fails the read, naming the schema and the path, instead of silently returning less context.
  • A stored query that cannot be run is skipped for membership and logged, so one broken queue does not fail every context read.
  • Putting a shipped change to a core schema's context paths to the user (ADR-094 §8) is not built here: nothing ships any, which is this issue's non-goal.

Tests

  • packages/core/tests/it/context_paths_test.rs: saving and refusing paths, subtype inheritance, a core schema across a restart, default and explicit paths, a stale path, moving a node between two queues by changing a field, membership asked only of queries with skills (relative date, stored limit), the version (changes and non-changes), run with context.
  • skill_attached_to_test.rs: the task flow and the user-defined workflow fixture now include query membership.
  • Agent tools (node_context_tools.rs), CLI and daemon (cli_integration.rs, get_schema_definition), wire types.

🤖 Generated with Claude Code

@malibio

malibio commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator Author

Pragmatic Code Review: PR #3614 (issue #3557)

Review Type: Initial Review

Recommendation: APPROVE — no blockers. Three 🟡 items are worth folding in before merge; the rest is optional.

Summary

The change does what ADR-094 §2, §4 and §7 decide, and the design is clean: one write path for context paths (update_schema, with a guard on generic updates), membership asked per read and never stored, a version derived from stored state with length-prefixed parts, and one shared context_of for the single read and the run with context, so the two cannot drift (the test asserts an item's version equals a lone read's). No type is named anywhere in the rule. check-node-type-literals, check-lifecycle-reads and skill:check pass; I ran all three. I did not re-run the test suites (test:changed had already passed).

Requirements Check

Context paths

  • ✅ Schema declares RelationshipPath[], validated on save, refusal names the path — packages/core/src/schema/mod.rs:2452-2550; test a_schema_declares_context_paths_checked_when_saved. One hole in the validation order, see finding 1.
  • ✅ Subtype's paths are its ancestors' plus its own — packages/core/src/services/node_service/schema.rs:616-635; test a_subtype_follows_its_ancestors_paths_and_its_own.
  • ✅ Add/remove on user-defined and core schemas through update_schema (CLI and agent tool) — agent test get_node_context_follows_the_types_context_paths_and_returns_a_version, CLI test at cli_integration.rs:2927.
  • ✅ Survives restart and re-seed — test a_core_schemas_context_paths_survive_a_restart reopens the database.
  • ✅ No paths given follows the type's; explicit ones are followed in addition, deduplicated — node_context_ops.rs:314-325.
  • ✅ Wire SchemaNode carries contextPaths; generated TypeScript updated (three files).

Applicable skills

  • ✅ Node's own, matched queries', reached nodes'; each once with attached_to / matched_queries — node_context_ops.rs:453-563.
  • ✅ Membership only for queries with a skill attached — SkillQueries::load; test asserts query_ids().
  • ✅ Node moves between two queues by a field change — test a_node_carries_the_skills_of_the_queries_it_matches.
  • ✅ Relative date and stored limit evaluated as stored — test membership_is_asked_only_of_queries_with_a_skill_attached; run-time narrowing covered in the run-with-context test.
  • ✅ No type by name; the user-defined workflow fixture in skill_attached_to_test.rs includes query membership.

Run with context

  • ✅ Each item with governing nodes and skills; limit applies to items, capped at 50 in both the daemon and the agent tool.
  • ✅ Shared skill output once (read_node_contexts, asserted in core, agent and CLI tests).

Version

  • ✅ Returned; stable across unchanged reads.
  • ✅ Changes on node, returned node, skill content (child subtree), and skill set — test a_reads_version_follows_what_the_read_returns.
  • ✅ Unchanged for an unrelated node, a non-checkbox child, an unrelated skill, a query with no skill.
  • ✅ Version alone: --version-only / version_only.

General

  • ✅ Agent tools get_node_context and run_query (with_context); tool-surface golden updated.
  • ✅ references/cli.md and SKILL.md document all three; bun run skill:check passes (run during this review).
  • ✅ Docs edited in the nodespace-docs working tree (schema-management.md, node-types.md, seeded-nodes.md, prompt-skill-system.md, relationship-paths.md). Note for the push: that working tree also holds an unrelated edit to decisions/047-ci-gating-policy.md; stage explicit paths.
  • ✅ quality:fix / test:changed — reported passed by the author; not re-run here.

Findings

Critical Issues

None.

Suggested Improvements

1. 🟡 A path can be validated and dead in the same call; nothing guards a relationship a context path names
📁 packages/core/src/schema/mod.rs:2452-2550 (validation), :2597 (call site), packages/core/src/ops/node_context_ops.rs:326-339 (the read that fails)

validate_context_path_changes resolves each added path against the schemas as stored before the call. {"schema_id": "request", "remove_relationships": ["x"], "add_context_paths": ["x"]} therefore passes validation and commits a path that no longer resolves. The same holds for extends re-targeted in the call that adds a path over a relationship the old parent declared.

More broadly, removing a relationship that any schema's context path names is accepted silently (the PR lists this as a decision, and a_context_path_that_no_longer_resolves_names_its_schema pins it). The cost is larger than for a single read: context_of returns the error for the whole call, so one stale path on one type fails query run --with-context for every queue holding an item of that type, and every --version-only poll.

Recommendation: at minimum validate added paths against the relationships the call leaves (close the same-call hole). Better, treat context paths as update_schema already treats plays: a remove_relationships (or extends change) that strands a context path is refused with the schema and path named, unless force. All schemas are small and already loaded on this path, so the scan is cheap. Principle: validate at the write, fail fast where the author is, rather than at every later reader; consistency with the existing play impact check. I verified this by reading the code, not by running a test.

2. 🟡 Per-read and per-item work that is repeated rather than shared
📁 packages/core/src/ops/node_context_ops.rs:142-192 (SkillQueries::load), :273-304 (read_node_contexts), :308-366 (context_of)

  • load reads every target of every attached_to edge and hydrates each node, then filters to queries in Rust. Skills attached to hundreds of tasks or projects means hundreds of full node reads on every context read, to find a handful of queries. Filter in SQL instead (join node with is_a_sql for the query type and the governance participation condition).
  • load also builds a checked definition per query (schema resolution) on every read. With the version meant to be polled during a turn (ADR-093 §5), --version-only pays the full read each time: Q definitions, Q membership statements, the path walks and a render of every skill subtree.
  • In a run with context, each of up to 50 items calls resolve_context_paths (chain plus a schema read per ancestor) and applicable_skills -> fetch_skills, so a procedure shared by 50 items is rendered 50 times, and a governing node shared by all items (one project) is re-read with its children 50 times.

Recommendation: in read_node_contexts, memoize context paths by node type and rendered skills by skill id across items; push the query-type filter into get_edge_target_ids' caller. The N x Q membership statements are what ADR-094 accepts and should stay as they are. Principle: avoid N+1 where the repeated input is identical; none of this changes the output or the version.

3. 🟡 The version is only comparable for the same paths, and two of the three places that teach it do not say so
📁 packages/agent/src/local_agent/tools.rs:2436-2470 (def_get_node_context), packages/skill/SKILL.md:196-199

The digest includes each followed path, so a read with paths: [["project"]] and a later version_only read without it never match. references/cli.md:190 says to pass the same --path flags; the agent tool description ("read again with 'version_only' to learn whether anything moved") and SKILL.md do not. A model following the tool description after a read with extra paths sees "changed" on every poll, re-reads, and sees it again.

Recommendation: add "with the same 'paths'" to the version_only parameter description, and to the SKILL.md sentence if it fits under the size cap (otherwise leave cli.md as the place). Principle: a contract that has a precondition states it where the caller reads it.

4. 🟢 The one-write-path guard covers updates, not creates (unverified)
📁 packages/core/src/services/node_service/crud.rs:146-156

ensure_schema_structure_unchanged runs on the update paths only. A generic create of a schema node with contextPaths in its properties appears to store them unchecked, which the docs' "no path skips the check" rules out. I did not run this; the children / parent rules look to have the same shape, so it may already be handled elsewhere. Worth one test (a generic create with contextPaths is refused or the key is dropped). Impact is small either way: a bad path fails the read with a message naming the schema.

5. 🟢 Two small gaps between the docs and the code on "declared once in a chain"
📁 packages/core/src/schema/mod.rs:2470-2476, packages/core/src/services/node_service/schema.rs:628-632

inherited_from looks up the chain only. Adding to a parent a path a subtype already declares is accepted, leaving it declared twice (the read deduplicates, so nothing breaks). Likewise the 20-path limit is per schema, so a deep chain plus 20 asked paths is unbounded in principle. Either is fine as is; the schema-management.md wording ("A path is declared once in a chain") is slightly stronger than what is enforced.

Nitpicks

  • Nit: packages/agent/src/local_agent/tools.rs:2147-2156 vs :2456-2462 — update_schema documents a path as a dotted string, get_node_context as an array of names, and the read prints "path": ["project"]. Both forms are accepted on write, so this works; one form across the two tool descriptions would be easier on a small model.
  • Nit: packages/core/src/ops/node_context_ops.rs:419-422 — a matched query's title is returned but only its id is digested, so renaming a queue changes the output without changing the version. Defensible (the query node is not a returned node); a one-line comment would record that it is intended.
  • Nit: packages/core/src/schema/mod.rs:3321-3331 — the checks run against schema_before and the list is built from the later re-read schema. Two concurrent adds of one path can both pass and store it twice. Harmless (deduplicated on read, retain removes both); noting it only because the version check sits after this point.

Tests

Coverage is strong and behaviour-led: refusals with their messages, inheritance, restart, the queue move, stored-vs-run-time evaluation, version changes and non-changes, the empty run. Missing cases follow the findings: a same-call relationship removal plus path add (1), and a generic create carrying contextPaths (4).

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

Automated pragmatic-code-review: APPROVE (posted as a comment — GitHub blocks self-approval). Full findings in the review comment above.

@malibio

malibio commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator Author

Review addressed (commit af29c95)

Addressed

  1. 🟡 A path validated and dead in the same call / unguarded relationship removal
    • add_context_paths is refused in a call that also has remove_relationships or extends, so an added path is never checked against a relationship the call takes away.
    • An update that takes a relationship away now reports every context path, on any schema, that no longer resolves: strandedContextPaths ("<schema id>: <path>"), printed by the CLI. It is checked against the committed schemas with the same resolver the save uses, so it is exact.
    • Not done: refusing the removal itself. That needs resolving every schema's paths against a schema set that does not exist yet (the resolver reads stored schemas), which is a second resolver. The author is told at the write, and the read still fails loudly rather than returning less context.
  2. 🟡 Repeated per-item work in a run with context — one ReadCache per call: a type's context paths and each skill set are read and rendered once, not once per item.
  3. 🟡 Version precondition — the version_only parameter description and SKILL.md now say to pass the same paths as the read being compared.
  4. 🟢 Create path — confirmed: a generic create of a schema node stored contextPaths unchecked. It is now refused; test added.
  5. 🟢 "Declared once in a chain" — docs wording changed to what is enforced.
  • Nits: the version now digests a matched query's title; a path already stored is not stored twice by a racing add.

Skipped

  • SQL-side filtering and cached definitions in SkillQueries::load — it reads the targets of attached_to edges, of which there are few, in one batched read. ADR-094 accepts a query per queue with skills; caching definitions across reads would need invalidation on every schema and query write. Revisit if a profile shows it.
  • One path form across tool descriptions — get_node_context keeps its shipped array form; update_schema takes both forms.

bun run test:changed passes on the new commit.

@malibio

malibio commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator Author

Pragmatic Code Review: PR #3614 (issue #3557)

Review Type: Re-Review (commit af29c955e only; first review covered up to a0e04a10b)

Recommendation: APPROVE. No blockers and no regression in the new commit. One 🟡 is worth a two-line change before merge; the rest is optional.

Previous Review Summary

  • Previous recommendations: 5 findings (3 🟡, 2 🟢) and 3 nits.
  • Addressed: 3 (version precondition), 4 (create guard), 5 (docs wording), and two nits (query title in the version, racing add stored once).
  • Partly addressed, remainder reasonably skipped: 1 (same-call hole closed; removal is reported, not refused), 2 (per-call cache added; SQL-side filtering and shared governing-node reads not done).
  • Skipped: the nit on one path form across tool descriptions. Fine as is.
  • New issues: 1 🟡, 2 🟢 / nits, below.

What I checked: I read the diff and the surrounding code in schema/mod.rs, crud.rs, bulk.rs, schema_node.rs and node_context_ops.rs, and ran context_paths_test:: (9 passed). I did not run the other suites; test:changed is reported as passing on this commit.

How each previous finding was handled

1. Path validated and dead in the same call / unguarded removal: addressed in part, and the skip is sound.

  • The same-call refusal (packages/core/src/schema/mod.rs:2492-2504) closes the hole. It runs in Phase 0, before any rename commits, so a refused call changes nothing; the test asserts that. remove_context_paths together with remove_relationships is still allowed, which is the repair path and is right.
  • stranded_context_paths (:2445-2467) scans every schema, so it catches the cases the trigger alone would not suggest: a path on another schema over a reverse name (the test's request: desk), and a subtype's path over a relationship an ancestor drops.
  • previous_extends != updated_extends (:3679) is correct: previous_extends is the Phase 2 re-read value, updated_extends is that value or the trimmed new parent, and the clone is taken before extends moves into updated_schema. Re-sending the current parent compares equal and skips the scan.
  • The reason for not refusing the removal holds. The resolver reads stored schemas, so a pre-commit refusal needs either a resolver over a pending schema set or a scan inside the transaction through a service that does not read through it. Telling the author at the write and failing the read loudly is a reasonable place to stop.

2. Repeated per-item work: addressed where it was cheapest; the rest is a fair YAGNI call.

  • ReadCache (packages/core/src/ops/node_context_ops.rs:557-612) is sound. Guidance is a function of the ordered skill-id list and stored state only; the per-item parts (attached_to, matched_queries) are applied after the lookup at :529-544, outside the cached value. The order is part of the key, so the output order cannot differ from the uncached read. The archived / not-a-skill filter is inside the cached function, unchanged. The cache is created per call and never kept.
  • The version is unaffected: it digests the same values, and a_query_run_with_context_returns_each_item_whole still asserts an item's version equals a lone read's. The only observable difference is under a concurrent write during one run: all items now see one rendering of a shared skill, which is more consistent than before, not less.
  • Not done: a governing node shared by every item is still walked and re-read with its children per item (:330-351), and SkillQueries::load still filters in Rust. Bounded by 50 items and a batched read; fine to leave until a profile says otherwise.

3. Version precondition: addressed. packages/agent/src/local_agent/tools.rs:2466 and packages/skill/SKILL.md:196-200. SKILL.md is 17,916 bytes, under its cap.

4. Create guard: addressed, and it reaches every service create path. The guard sits in ensure_not_creating_core_schema (packages/core/src/services/node_service/crud.rs:567-581), called only from ensure_creatable_in_batch (:245), which the single create (:610) and all three bulk paths (bulk.rs:154, :595, :728) go through. It does not trip legitimate creates: to_properties omits contextPaths when empty (packages/core/src/models/schema_node.rs:205-208), so create_schema never carries the key, and core schema seeding writes straight to the store (node_service/mod.rs:1906) and seeds none.

5. "Declared once in a chain": addressed in the docs working tree (components/schema-management.md, "A path two schemas in a chain both declare is followed once").

Findings in the new commit

Critical Issues

None.

Suggested Improvements

1. 🟡 A failed advisory scan reports a committed update as failed
packages/core/src/schema/mod.rs:3679-3684

stranded_context_paths(node_service).await? runs after the transaction has committed (committed.map_err(..)? at :3647-3673). If the scan fails (get_all_schemas, or a PathResolveError::Lookup at :2459-2461), update_schema returns an internal error for a change that was written. The caller retries and is then refused with "does not declare relationship", which points away from what happened. The scan is a report, not part of the write, so it should not decide the call's result.

Recommendation: on a scan error, log a warning and return the output without the list (unwrap_or_else(|e| { tracing::warn!(..); Vec::new() })). Principle: an advisory step after the commit point must not change the outcome of the committed operation. Likelihood is low (a read failing right after a successful write), which is why this is not a blocker.

Nitpicks

  • Nit: packages/core/src/schema/mod.rs:2496 — params.extends.is_some() refuses add_context_paths even when extends names the current parent, which takes nothing away. The message tells the caller what to do, so this is only a slightly wider refusal than needed.
  • Nit: packages/nodespace-types/src/schema_ops.rs:252-256 — the field comment says "context paths the update left naming a relationship that no longer resolves", but the scan returns every stranded path, including ones stranded by an earlier update. The docs sentence ("reports every context path, on any schema, that no longer resolves") is the accurate one; the wire comment could match it. A gen:types run follows if it changes.
  • Nit: packages/core/src/services/node_service/crud.rs:554-560 — ensure_not_creating_core_schema now also refuses context paths on create; its name and doc comment describe only the first check.

Tests

The two cases the first review asked for are there: the combined call is refused and leaves nothing declared, the removal reports request: desk, and a generic create carrying contextPaths is refused. No test covers the stranded report on an extends re-target; the code path is the same scan, so I would not hold the merge for it.

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

Automated pragmatic-code-review (re-review): APPROVE (posted as a comment — GitHub blocks self-approval). Full findings in the review comment above.

@malibio

malibio commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator Author

Re-review addressed (commit fb9eebe)

  • 🟡 A failed stranded-path check after the commit is now logged and the update returns without the list, so a committed update is never reported as failed.
  • Nits: the wire comment on strandedContextPaths now says it lists every stranded path; the create guard's doc comment covers both checks.
  • Skipped: extends naming the current parent still refuses add_context_paths; the message says what to do.

Re-review decision: NO RE-REVIEW NEEDED — one error-handling change on an advisory step and two comments. context_paths_test passes; the merge gate runs the full pyramid.

malibio and others added 5 commits October 5, 2026 14:06
…sioned context read and query run with context (#3557)

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…n with context and the read version

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…re per-item reads in a run with context, state the version's precondition

- A call that removes relationships or re-points extends adds no context paths
- An update that takes a relationship away reports the context paths it stranded
- A schema node is not created with context paths
- A run with context reads each type's paths and each skill set once
- The version digests a matched query's title; a path is not stored twice

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…mitted schema update

Also corrects the wire comment on stranded_context_paths and the create guard's doc comment.
Skipped: extends naming the current parent still refuses add_context_paths (the message says what to do).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@mstomar125
mstomar125 force-pushed the issue-3557-context-paths branch from fb9eebe to 02b3fe9 Compare October 5, 2026 12:06
@mstomar125
mstomar125 merged commit 471368e into main Oct 5, 2026
@mstomar125
mstomar125 deleted the issue-3557-context-paths branch October 5, 2026 12:06
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.

Declare context paths on a schema, derive a node's skills from the node and the queries it matches, and return a versioned context read

2 participants