Skip to content

knowledge.synthesize answers from datasets with a plan it checks and runs, and abstains when the task is ambiguous - #279

Merged
zlareb1 merged 26 commits into
mainfrom
feat/synthesize-datasets
Nov 6, 2026
Merged

zlareb1 merged 26 commits into
mainfrom
feat/synthesize-datasets

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Related: #157

Changelog category (leave one):

  • New Feature

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):

knowledge.synthesize takes datasets: the model proposes a knowledge.query plan from their schemas, the server checks and runs it over every row, and the answer cites the result as [knowledge.query#1]; when the task is ambiguous the answer abstains with the question to ask.

Why

The issue asks for an answer workflow that can retrieve narrative evidence, execute a validated calculation, or combine both, with optional natural-language planning that "proposes this plan; it does not acquire database privileges", and that requests clarification or abstains when table or column meaning, joins, units or time boundaries are ambiguous, never inventing joins from similar column names or answering an aggregate from retrieved rows. knowledge.query (#278) runs a typed plan; nothing turned a question into one.

Design

  • Input: knowledge.synthesize gains datasets (up to four, as knowledge.query takes them: source_uri, dataset_id, alias); source_uris becomes optional, and a task names documents, datasets or both.
  • The plan (server/knowledge/domain/answer_plan.py): one model call (knowledge_synthesize_plan) reads the task and each dataset's alias, name, row count and stored columns, and answers with a JSON object, {"plan": {...}} (the knowledge.query fields) or {"ambiguous": "<the question to ask>"}. The instructions tell it to join only on columns the question makes clear belong together and to answer ambiguous rather than guess a column, unit, period or relation. A reply wrapped in prose or a code fence, or with braces in a note after the object, still reads (the first JSON object in it holding plan or ambiguous is read; a reply cut at its output cap is told back as incomplete JSON, even after a brace in prose, and a plan or ambiguous of the wrong type is told back as such). The datasets are always the caller's: a plan's own datasets, org or namespace is dropped.
  • No field dropped: the query's models ignore an unknown key, so a misnamed field (filters for where, a group's period for bucket) would vanish and the plan run without it: a count of every row cited as the count asked for. A plan with a key the models do not declare, at any depth, is refused and told back (_unknown_keys); the prompt lists each field's shape, with an example.
  • Bounded prompts: the plan prompt lists at most 100 columns a dataset and 24,000 characters of schema shared among the datasets (and how many columns more there are), each column name whole and as a JSON string, since a plan must name it exactly; the dataset name, which a plan never names (it names the alias), is untrusted source text, so it is a JSON string cut to 200 characters. The evidence note is at most half a window_chars: what it may not cover and the plan that ran come first, the plan taking at most a third of it, then whole rows as many as fit, each value and each column name to 200 characters and the column line to a third of the room (so a wide selection leaves its rows room), saying how many of the rows the calculation returned are shown (and that more matched when it was cut at its limit); a cut never loses a warning.
  • Checked and run (server/knowledge/app/synthesize.py): the plan is validated as a KnowledgeQueryInput in the caller's org and namespace and run by knowledge.query's handler, so it is checked against the schemas, pinned to the live snapshots, bounded by the caller-shaped ceilings and revalidated after the read exactly as a direct query is. The model never writes SQL and reaches nothing the caller could not query. A plan refused by the input model or by the query (an unknown column, a type its aggregate does not take) is proposed once more, the prompt showing the refused reply and the reason; past that the answer abstains.
  • Abstentions: needs_clarification when a dataset's schema needs a person's answer (before any model call; clarification names what to approve), ambiguous when the model would need to ask first (clarification holds the question), invalid_plan when no plan it proposed held (clarification holds why). A dataset that is not live answers 404, as knowledge.query does.
  • Cited evidence: the result's columns and first 50 rows, with what it may not cover (more rows matched, rows rejected, an incomplete extraction), are one more note for the reduce, with the plan that ran less every field left at its default (so the answer can tell what was counted), cited as [knowledge.query#1]. The grounding knowledge.synthesize keeps only the citations of chunks it read, and abstains on an empty answer #225 added for chunk citations covers it: knowledge.query#1 joins the set of what was read, so a citation of a calculation that was not run is removed and listed in unsupported_citations. calculations returns the result (with each row's ids when the plan selects rows, as knowledge.query returns them) with its receipt (the plan as validated, the snapshots, the statement and its parameters). With no documents, the reduce runs over the calculation alone. One calculation is run an answer; with datasets, source_uris refuses knowledge.query, the calculation's citation source.
  • Python SDK: knowledge.synthesize(source_uris, task, *, datasets=...): source_uris may be None when datasets are named, and every call it took still works; datasets is sent only when named, so a documents-only call to an older server names nothing as ignored.
  • Order: the calculation runs before the documents are read, so an answer that abstains on its datasets reads none. SynthesizeCalculation is knowledge.query's output plus its citation, so the two cannot drift apart.
  • Cost: one plan call (two when the first is refused) plus the reduce; the namespace's cost cap still gates the whole call. The plan's output cap is an LLM_MAX_TOKENS entry (1,200).
  • Fake provider: answers the plan prompt with a plan that counts the rows, so the path runs end to end in tests and local development with no provider.
  • The handler's fold loop moved into _reduce, unchanged, to keep the handler under the function-length gate.

Alternatives considered: the model writing knowledge.query calls as a tool loop (several model turns per answer, and a loop to bound); retrieving rows and letting the model total them (the issue rules it out: an aggregate from retrieved rows is not over every row).

Scope and risk

  • Read path only: no table, migration or setting beyond the token cap's table entry.
  • Prompt injection: column names, dataset names and the task are untrusted text in the plan prompt. Whatever plan comes back is only a knowledge.query input in the caller's scope, checked against the schemas; it cannot name another dataset, namespace or org, and it runs as the reader user where one is set.
  • Left for later in the issue's plan: more than one calculation per answer, and units, which dataset schemas do not record yet.

Compatibility, rollout and rollback

  • Rolling upgrade: a build N-1 replica answers a call with datasets and no source_uris with 422, and one with both as it answers today, naming datasets an ignored argument; retried on a build N replica it calculates.
  • Wire: knowledge.synthesize gains datasets (input) and calculations, clarification and three abstain_reason values (output); source_uris loses its minimum of one, refused only when datasets is empty too. No field is removed.
  • Operator steps: none. A deployment that caps output tokens per call can override knowledge_synthesize_plan in LOOM_LLM_MAX_TOKENS_OVERRIDES.
  • Rollback: reverting removes the fields; nothing is stored.

Tests

  • Unit (tests/unit/test_synthesize_datasets.py, 22): a reply read as a plan, a question or neither (a fenced reply, prose, braces or another object in prose before it or a note after it, a reply cut at its cap, a plan or ambiguous of the wrong type, a plan beside an empty ambiguous (null, a blank string) read as the plan and beside any other value told back, no object, an object with neither key), a plan's own datasets, org and namespace dropped; the prompt listing each dataset's stored columns and the reason a plan was refused; the evidence showing the result, its first 50 rows and what it may not cover; the handler running the plan on the caller's datasets and scope (the model named another dataset), citing [knowledge.query#1] and removing a citation of #2, which was not run; an ambiguous task abstaining with the question and running nothing, a long question cut to 1,000 characters and ending …; an unreadable reply and a refused plan proposed again with the reason, and two refusals abstaining invalid_plan; a schema that needs an answer abstaining before any model call (model empty); a dataset that is not live answering 404; a column name written as an instruction shown to the model and granting the plan nothing (it runs on the caller's dataset, org and namespace); the input refusing a task with neither documents nor datasets, a repeated alias, or, with datasets, a document named knowledge.query (it would cite as the calculation does); a misnamed plan field (filters, a group's period) refused and told back, never run without it; a wide schema listed only to its bound, each name whole, and four datasets sharing the schema's room; a valid plan of every shape the prompt lists passing the unknown-key check; an answer that abstains on its dataset reading no document; the fake provider recognising the plan prompt; the evidence showing the plan that ran, and a note cut to fit keeping its warnings before the rows and dropping whole rows, a truncated result saying how many of its rows are shown, a long plan leaving the rows room; a value or column name holding a line break or | escaped so it stays on its row, a null shown as NULL apart from an empty string, and non-ASCII text shown unescaped in the plan as in the columns; a column name holding a line break or a comma listed in the plan prompt as one JSON string, and a dataset name written as another dataset line kept on its own line and cut; a dataset line that fits its room exactly showing every column; a plan past the query's ceilings proposed again with how to narrow it; documents and a dataset answered together (chunk and calculation citations, cited_chunks holding only the chunk); model named on the abstentions that called one.
  • Unit (tests/unit/test_sdk_agent_call.py): the SDK's synthesize sends datasets alone and with documents, and the call it always took without datasets.
  • Unit, updated: the operation-transport contract's body for knowledge.synthesize, which now needs source_uris or datasets.
  • Integration (tests/integration/test_knowledge_query_live.py): with the fake provider (the server's, as its /v1/health reports it: a new require_fake_llm fixture beside require_real_llm), an answer over an ingested CSV runs the proposed plan over the live snapshot (5 rows, its receipt naming the snapshot knowledge.dataset_get reports) and cites knowledge.query#1; the other query tests pass (6 in all).
  • Existing synthesize tests (tests/unit/test_corpus_synthesis.py) pass unchanged.
  • No real provider was called: the plan's quality with a real model is the issue's later qualification step (P12).
  • Full unit suite: run on the rebased head before the merge.

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

  • AI-generated (an agent wrote most of the diff): Claude Code. Checked: the plan path end to end against a real server and ClickHouse with the fake provider, each abstention and the citation grounding in the tests above, and a review at every push.

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

Checklist

  • make format-check, make lint, make typecheck and make test-unit pass; the integration suite passes if server behaviour changed.
  • Generated files are regenerated in this PR, not by hand: the SDK op tables, docs/reference/configuration.md and the docs/reference/ tables (python scripts/codegen/docs_reference.py), the reach manifest, schemas/schema.sql (make schema-dump) and the Codex plugin copies (python3 scripts/codegen/agent_plugin_sync.py).
  • A new migration is additive and numbered after the latest on main; no merged migration is edited.
  • A behaviour change to any file a plugin ships (hooks/, lib/, bin/, skills/, .mcp.json, the Codex plugin-defaults.json) bumps both plugin manifests' version, and no other open PR uses that version.
  • The frontend builds (npm --prefix frontend run build) and its tests pass, if frontend/ changed.
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers; the history goes in this description (tests/unit/test_public_copy.py).
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code

zlareb1 and others added 26 commits November 5, 2026 21:15
…runs, and abstains when the task is ambiguous

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…e no model when none was called

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…d the evidence, and show the plan that ran

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ch column name whole

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…e answer test on the server's fake LLM

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…umn line, and keep its plan once

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…send datasets only when named

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…s output, and say what citations cover

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…documents or datasets

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…all it checks

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 merged commit ad167ed into main Nov 6, 2026
22 of 24 checks passed
@zlareb1
zlareb1 deleted the feat/synthesize-datasets branch November 6, 2026 00:47
@zlareb1 zlareb1 mentioned this pull request Nov 12, 2026
3 tasks done
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