Skip to content

agent-guards: facts, one state table per place (#1063) - #1065

Merged
scuttletoby merged 2 commits into
mainfrom
facts-1063
Nov 23, 2026
Merged

scuttletoby merged 2 commits into
mainfrom
facts-1063

Conversation

@scuttletoby

@scuttletoby scuttletoby commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

Closes #1063. The daily gardener that runs facts --check is wired in #1064, not in this PR.

What

After a compaction, a handoff or a new session, an agent does not know what was already made or why things are the way they are. It spends usage finding out again, and sometimes gets it wrong. This PR adds a state table per place: one Markdown table with one row per thing (Thing | What we made / how to use it | State now | Why | Updated). It also adds the tools to read the tables:

  • facts <words> prints the table rows that hold every word, each under its table's header, with Thing-column hits first. If no row holds all the words, it shows the rows that hold some of them and says so. After the tables, it searches the record repo, when one is configured (see below).
  • facts --index prints every Thing. facts --dirs shows where it looked, record repos included.
  • facts --all also reads every project's tables and record folders. FACTS.md warns: never post --all output to a public place.
  • facts --check is the gardener's check, report only, and exits 1 when it finds something. It checks this folder's tables only (every project's with --check --all) and never reads the record repo. It reports:
    • rows whose Updated date is stale, in the future, or not readable;
    • tables over maxRows;
    • tables with no Updated column;
    • broken rows and missing tables.
  • facts-hook:
    • SessionStart adds the Thing column and "Check the state table before diagnosing or redoing something: facts <words> prints the matching rows". Then it adds the project's text and one line, Records: <path> (facts <words> searches it), when a record repo is there; the repo's content is never shown. The whole text is capped at indexChars (default 4000) and indexLines. When it is cut, it ends with +N more: run facts --index.
    • PreCompact asks the summary to keep the names of the things, not their rows.
  • Upkeep rule (in FACTS.md): a change that alters a thing's state edits its row in the same change.

Record repo search

The config is facts.records: [{path, projectsDir: "projects", shared: ["machines", "orch", "shared"]}].

  • What it searches: the text files (.md .txt .json .yml .yaml .sh) in the shared folders and in the current project's projects/<name>/ folder. <name> is the project row's new optional name key, or else the last part of its remote or folder. With --all, it searches every project's folder.
  • Output: each hit is one path:line: text line, at most 160 characters. The default search shows at most 10 hits, then +N more (facts --records <words>).
  • Modes: --records <words> searches the repo only and shows up to 50 hits. --no-records searches the tables only.
  • What it skips:
    • .git, links, and files that are not regular files;
    • files over 1 MiB;
    • files whose bytes match a table it already read, so a mirrored STATE.md is not shown twice.
  • Limits: the search is a Python file walk with a 10 s limit (no subprocess grep).
  • Missing repo: one note: line in the command; the hook says nothing about it.

Example (a fixture with a mirrored table, another project's notes, and a shared runbook):

$ facts gpu
/srv/example/facts/STATE.md:
| Thing | What we made / how to use it | State now | Why | Updated |
|---|---|---|---|---|
| Example GPU | `gpu on\|off\|status` helper; off at boot | OFF on purpose; the vendor tool fails while off | quiet fans | 11-23 |
Records in /srv/example/records:
shared/runbook.md:1: Example GPU: switch it on with `gpu on` before a render job
projects/project/notes.md:2: The example GPU fan curve lives in gpu-fan.sh (set by hand, 11-20)

Config

Config lives in the machine file's agent-guards section, part facts:

  • dirs lists the tables for every session on the machine.
  • projects rows are {match, dirs, text, name}. match is a remote (host/owner/repo) or a folder, never a glob.
  • staleDays, maxRows, indexLines, indexChars, records.
  • FACTS_DIRS replaces the table lists for one command or session.

A broken config part is named, and the defaults are used. A bad projects or records row is dropped and the good rows are kept.

Review round 1 fixes

  • Tests: dates are fixed with FACTS_TODAY or a fixed today, so no test depends on the day it runs. Before, the fixed dates would have started failing on 12-02.
  • Privacy: --check reads only the current project, unless --all is given.
  • agent-guards check: facts-hook now runs under the same HOME that the expected answer is computed with. Before, ~/ dirs gave a false FAIL.
  • git: the hook asks git at most once per answer.
  • Reading: only regular files are read, opened with O_NONBLOCK and checked with fstat. A FIFO STATE.md gives a note and never hangs.
  • Index:
    • no file header is printed without its things;
    • the whole session text, project text included, is capped by indexChars.
  • Parser:
    • short |-|-| separators are accepted;
    • a second header row starts a new table;
    • a file cut at the size limit gets a (cut at N MB) note.
  • Dates:
    • future dates are flagged;
    • 11-23 (checked) is read;
    • 02-29 in a non-leap year means the last leap year.
  • Places: each place is read once, deduplicated by its real path.
  • Installer: if /usr/local/bin/facts is not this kit's link, it is left alone with a warning. The install still succeeds, and uninstall does not remove it. This uses a generic OPTIONAL_LINKS in the shared kit_install.py.

Why Python in agent-guards, not a separate Node script

The agent-guards kit already has what this needs:

  • the root-owned machine file loader;
  • per-part config with safe defaults;
  • the installer and the managed settings drop-in;
  • agent-guards check.

A separate script would mean a second config path and a second way to wire hooks.

General vs project

  • General (this PR): the format, the command, the hook, the check, the record repo search (OPTIONAL_LINKS in the installer), the config keys and the docs (fork-kit/agent-guards/FACTS.md). No project, person, path or machine is named. Examples use made-up names and /srv/example/... paths.
  • Project / machine (not in this PR): the tables, the record repo, and where each one is. Each machine's file sets them (facts.dirs, facts.projects, facts.records).

Tests

  • fork-kit/agent-guards/tests/facts.test.py: 42 tests, up from 24. New cases:
    • Table files: a FIFO, non-UTF-8 bytes, a cut file, short separators, a second header.
    • Dates: future dates, the (checked) suffix, 02-29.
    • Projects and git: realpath dedupe; a git that hangs or fails while a project matches by remote, both in-process and through the hook; git asked once.
    • Session text cap: many tables under the cap.
    • Config errors: an end-to-end SessionStart with a config problem plus table output; bad records and name rows.
    • --check: current project only vs --all; it never reads records.
    • Record repo search: the hit cap and line trim, --records and --no-records, the mirrored-table skip, .git, big files, links and FIFOs skipped, the current project vs --all, a missing repo, the hook's Records line, and --dirs.
  • wiring.test.py: agent-guards check with a ~/ table dir passes.
  • install.test.py: an unrelated /usr/local/bin/facts is left alone through install, update and uninstall, and our own link is kept. common/tests/kit_install.test.py covers OPTIONAL_LINKS for every kit.

These pass locally:

  • fork-ci-tests for fork-kit/agent-guards/ and fork-kit/common/;
  • every other kit that uses the shared installer;
  • the seam check, the upstream-files check and fork-tests-drift.

🤖 Generated with Claude Code

A state table holds one row per thing (Thing | What we made / how to use
it | State now | Why | Updated). The `facts` command prints the rows that
match some words, with their header; `--index` lists the things;
`--check` is the gardener's check (stale Updated, too many rows, broken
rows, missing tables). The `facts-hook` hook shows the things at session
start with "check the state table before diagnosing or redoing
something", and before compaction asks to keep names, not rows.

Tables are configured in the machine file (agent-guards part "facts":
dirs, projects matched by origin remote or folder, staleDays, maxRows,
indexLines); FACTS_DIRS overrides. The kit ships only the tool, the
format and the hook; tables live with the machine or project.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fixes from the review of #1065:
- tests: dates fixed by FACTS_TODAY / a fixed today, so no test depends on the day it runs
- --check reads this folder's tables only; every project's only with --check --all (FACTS.md: never post --all
  output to a public place); --check never reads record repos
- agent-guards check: facts-hook runs with the same HOME the wanted answer is computed under (no false FAIL with ~/)
- git is asked at most once per hook answer (projects computed once and passed in)
- only regular files are read (O_NONBLOCK + fstat): a FIFO STATE.md is a note, never a wait
- index: no file line with none of its things; the whole session text is capped (indexChars, default 4000) with
  "+N more: run facts --index"
- parser: short |-|-| separators, a second header row starts a new table, "(cut at N MB)" note
- dates: future dates flagged, "11-23 (checked)" read, 02-29 in a non-leap year means the last leap year
- places deduped by real path
- installer: an unrelated /usr/local/bin/facts is left alone with a warning (OPTIONAL_LINKS in kit_install)

New: facts.records ([{path, projectsDir, shared}]): `facts WORDS` searches the record repo's text files after the
tables (this project's projects/<name>/ and the shared folders; --all every project), at most 10 hits, skipping
.git, links, files over 1 MiB and copies of tables already read. --records WORDS (50 hits), --no-records. The
session hook names the repo in one line and never shows its content.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@scuttletoby
scuttletoby merged commit fccee4e into main Nov 23, 2026
32 checks passed
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.

Facts: current state + why per topic, looked up instead of re-derived

1 participant