Skip to content

fix: repair record_fact summary contracts and asset aliases - #282

Merged
BaaRai merged 3 commits into
BaaRai:mainfrom
renaesop:codex/fix-record-fact-input-contract
Nov 18, 2026
Merged

BaaRai merged 3 commits into
BaaRai:mainfrom
renaesop:codex/fix-record-fact-input-contract

Conversation

@renaesop

@renaesop renaesop commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Problem

record_fact can reject valid batch calls, hide failed items, or silently lose asset associations because its advertised schema and handler disagree.

  • Missing batch summaries: facts.items is currently an unconstrained object. The model is not told that every item needs its own non-empty summary. A legacy/edited catalog schema requiring a top-level summary can also reject a valid batch before the handler runs.
  • Ignored asset IDs: {"summary":"TLS handshake observed","tested_asset_ids":[42]} records an unanchored fact on current main: Go silently discards the unrecognized field. Supplying conflicting asset_ids and tested_asset_ids is silently accepted.
  • Misleading success and unintended writes: {"summary":"do not save","facts":[]} falls back to single mode and writes a fact. A JSON type error in a later item can still save earlier items because the unmarshal error is ignored. Item failures appear inside a nominally successful tool result.

A deployment failure review motivated this change. That deployment additionally validates structured outcomes; upstream main does not contain that extension. This PR fixes the shared input-contract problems without introducing the deployment-specific outcome/revision workflow.

Resulting behavior

  • Publish a plain object schema without root-level anyOf; the handler selects single or batch mode. Single calls require summary; batches require a non-empty facts array with a non-empty summary on each item. Only intent_id inherits from the enclosing object.
  • Keep asset_ids canonical and accept tested_asset_ids as an alias. Normalize ordering/duplicates, retain numeric-string compatibility, reject invalid IDs and conflicting lists, and never infer assets from an intent or combine conflicting coverage.
  • Reject malformed JSON, unknown or mis-cased fields, empty/null batches, and batch-level fields other than intent_id and facts before any writes. Errors name the invalid field. Semantic per-item failures still retain successful results, set IsError, return aligned IDs/errors, and instruct callers to retry only failed indices.
  • Check every fact asset anchor before insertion, holding PostgreSQL key-share locks through commit. Missing IDs produce a clear message naming the IDs, rather than SQLSTATE 23503.
  • Repair existing catalog rows at startup and schemas at tool resolution, so existing installations and cached/edited schemas benefit too. Preserve operator descriptions and defaults for supported fields, plus tool settings; remove unsupported properties so the schema agrees with the strict parser; the database update is idempotent and uses compare-and-swap.

For example, this valid batch needs no redundant top-level summary:

{"facts":[{"summary":"TLS handshake observed","asset_ids":[42]}]}

Benefits

  • Fewer avoidable tool retries, LLM turns and wasted tokens because the model sees the actual required fields.
  • Correct fact-to-asset links instead of silent evidence/coverage association loss.
  • Actionable batch failures that can be retried individually without resubmitting successful items.
  • Existing deployments receive the corrected contract without resetting operator configuration or manually rebuilding the tool catalog.

These are correctness and retry-efficiency benefits; no percentage cost or latency improvement is claimed.

Validation

All work and checks ran on the remote machine against isolated test databases, with CPU/memory limits.

  • The new regression tests fail on unmodified upstream main 278de8b: alias-only inputs lose anchors, conflicting lists are accepted, empty batches write facts, malformed batches partially write, and legacy top-level summary requirements survive.
  • New tests cover actual database anchors, single/batch calls, conflicting/invalid IDs, missing summaries, partial failures, SDK schema round trips, runtime repair, and idempotent catalog migration.
  • go build -p 1 ./... — passed.
  • go vet -p 1 ./... — passed.
  • go test -p 1 ./agent ./server ./db -count=1 — passed after merging upstream ac5e714.
  • Review regressions cover exact key casing, unknown batch-item fields, all forbidden batch-level fields, absent anchors, Unicode whitespace, numeric-string IDs and removal of legacy root anyOf.

@BaaRai BaaRai left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks. The alias conflict handling and the per-item summary contract are right. The schema places anyOf at the root. The handler decodes without DisallowUnknownFields, so unknown keys are dropped. Blocking:

  1. Remove the root-level anyOf from the tool input schema. It reaches the provider unchanged; the handler already enforces "summary or facts".
  2. Refuse unknown keys (DisallowUnknownFields at top level and on items), and in batch mode reject top-level fields other than intent_id and facts, with an error naming the field. Note that encoding/json matches keys case-insensitively today, so the strict check should compare exact names.
  3. Non-existent asset IDs currently surface to the model as a raw foreign-key error (SQLSTATE 23503). Please check that every anchor exists first and return a clear message.

Suggestions: a whitespace-only summary passes minLength: 1 but the handler rejects it; numeric-string asset IDs are accepted by the handler but rejected by the schema.

@renaesop

Copy link
Copy Markdown
Contributor Author

@BaaRai Addressed all three blockers in c833130, after merging current main.

  1. Removed root-level anyOf from generated, stored and runtime-repaired schemas. The handler enforces the single/batch contract.
  2. Added DisallowUnknownFields and exact-name checks at the top level and on every item. Batch calls accept only intent_id and facts at the top level; other fields are rejected by name before any writes. Unsupported catalog properties are removed rather than advertising fields the handler now rejects; annotations on supported fields remain intact.
  3. Check every fact anchor inside the insertion transaction and hold key-share locks until commit. Missing IDs are listed in an actionable error, without exposing a foreign-key error.

I also aligned the schema with the handler for Unicode-whitespace-only summaries and numeric-string asset IDs. The existing per-item failure/retry behavior remains intact.

Validation on the remote machine: complete agent/server/db suites with isolated PostgreSQL, go build -p 1 ./..., and go vet -p 1 ./... all passed. New regressions cover the cases above and ensure malformed/unknown fields cannot cause partial writes. The PR description has been updated. Ready for re-review.

@BaaRai BaaRai left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Re-reviewed the rework. All three blockers are addressed: the root anyOf is gone from the generated, stored and repaired schemas; unknown keys are refused by exact name and DisallowUnknownFields, and batch mode only accepts intent_id and facts; the anchor check holds FOR KEY SHARE inside the insert transaction and reports the missing IDs instead of a foreign-key error. Aligning the schema with the handler for whitespace-only summaries and numeric-string asset IDs is a welcome extra. Thanks.

@BaaRai
BaaRai merged commit 51e2c60 into BaaRai:main Nov 18, 2026
5 checks passed
zealot00 added a commit to zealot00/ARTEX that referenced this pull request Nov 18, 2026
BaaRai pushed a commit that referenced this pull request Nov 20, 2026
The schema the model sees for record_fact declared every fact field twice:
at the top level (single-fact shape) and inside facts[] (batch shape), with
the handler deciding which shape a call is. A model that fills every
declared property sends the single fact inside facts[] and a copy, or a
placeholder such as "", [] or "x", of each field beside it; since PR #282
every such call is refused by name, and the refusal text ("summary is not
allowed at the top level") leads the model to send a dummy value next, so
the same refusal repeats until the intent ends. Observed with a model over
the OpenAI Responses API: 209 of 212 record_fact calls of two tasks were
refused, 156 of them identical in one intent.

The published schema is now {facts:[{...}]} and nothing else (required:
facts, additionalProperties false). The handler keeps the strict contract
of PR #282: unknown or mis-cased fields, invalid or foreign asset ids,
missing summaries, empty batches and any top-level field beside facts
(other than the legacy intent_id) are refused before anything is written,
with an error that names the fields and the one accepted shape. It still
accepts the legacy single fact and the legacy batch with a top-level
intent_id. An item without intent_id is linked to the intent the worker is
running (SetOwnerNode), as report_finding already does.

record_fact opts out of the harness schema check (norma Spec.RawInput) so
the stricter published schema does not reject the legacy shapes before the
handler sees them; the catalog decorator forwards the opt-out.
RepairRecordFactSchema republishes stored and cached schemas as facts-only
and keeps the annotations of facts and its items. A one-time startup
refresh replaces the stored description and the texts that referred to the
top level when they equal the previous defaults; operator edits stay and
are logged. seedFactSourceSchema adds the source enum on the items.

Tests: the handler contract (TestRecordFactSummaryContract,
TestRecordFactStrictFields, TestRecordFactBatchTopLevelConflicts,
TestRecordFactRefusesTopLevelCopiesBesideFacts), the published schema and
its repair (TestRecordFactSchemaIsFactsOnly, TestRecordFactStoredContractRepair,
TestRecordFactInputSchemaUpgrade, TestRecordFactSourceSchemaUpgrade,
TestWireTools), the running-intent default, the one-time refresh
(TestRefreshRecordFactContract) and a synthetic replay of the call shapes
(TestRecordFactShapesReplay).

Co-Authored-By: Claude Mythos 5.1 <[email protected]>
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.

3 participants