From 9acb0b34c98c47aa2a086fe1ba5deebcf8ac5b85 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Fri, 30 Oct 2026 10:54:29 -0500 Subject: [PATCH] fix(tokens): name the request as the source of its built-in tokens MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit matcha tokens listed req as '— (nothing provides it)'. An agent reading that would write a provider for req. A needed token with no producing node must come from the request (req, params, body, …): app.ready() has already validated the graph, so a truly missing token would have failed it. Mark those source: "request" in the JSON and 'the request (built in)' in the text. Signed-off-by: Diego Resendez --- src/cmd_graph.rs | 15 ++++++++++++++- src/introspect.rs | 7 +++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/cmd_graph.rs b/src/cmd_graph.rs index 54e2160..aa10ecf 100644 --- a/src/cmd_graph.rs +++ b/src/cmd_graph.rs @@ -220,7 +220,11 @@ fn tokens_text(data: &Value) -> String { "{}\n provided by: {}\n needed by: {}", str_of(&t["token"]), if provided.is_empty() { - "— (nothing provides it)" + if t["source"] == "request" { + "the request (built in)" + } else { + "— (nothing provides it)" + } } else { &provided }, @@ -287,6 +291,15 @@ mod tests { ); } + #[test] + fn tokens_text_names_the_request_for_its_built_in_tokens() { + let data = json!([{ "token": "req", "source": "request", "providedBy": [], "neededBy": [{ "kind": "step", "name": "user" }] }]); + assert_eq!( + tokens_text(&data), + "req\n provided by: the request (built in)\n needed by: user" + ); + } + #[test] fn tokens_text_says_when_nothing_provides_a_token() { let data = json!([{ "token": "user", "providedBy": [], "neededBy": [{ "kind": "handler", "name": "Me.get", "route": "/me" }] }]); diff --git a/src/introspect.rs b/src/introspect.rs index 6fb4a73..9c2df82 100644 --- a/src/introspect.rs +++ b/src/introspect.rs @@ -170,6 +170,12 @@ fn body(query: &Query) -> String { /// Tokens, from the graph's nodes plus each route's handler (only `explain` /// knows what a handler needs). Sorted, so the answer is stable across runs. +/// +/// A token that something needs and no node provides is one the request itself +/// carries (`req`, `params`, `body`, …). That is an inference, and a sound one: +/// `app.ready()` has validated the graph, so a needed token with no producer +/// would already have failed it. Marking those `"source": "request"` keeps an +/// agent from reading "nothing provides it" and writing a provider for `req`. const TOKENS: &str = r#"const tokens = new Map(); const entry = (t) => tokens.get(t) ?? tokens.set(t, { token: t, providedBy: [], neededBy: [] }).get(t); const graph = app.graph(); @@ -183,6 +189,7 @@ for (const pattern of new Set(graph.routes.map((r) => r.pattern))) { for (const t of n.needs) entry(t).neededBy.push({ kind: 'handler', name: n.name, origin: n.origin, route: pattern }); } } +for (const t of tokens.values()) t.source = t.providedBy.length ? 'graph' : 'request'; data = [...tokens.values()].sort((a, b) => a.token.localeCompare(b.token));"#; fn resolve_entry(target: &Target) -> std::io::Result {