✨ feat(sarathi): research before you ask, the rule and its gate

Merged
AT
14d ago
+869-31

Slice: #177

Work item #176 (approved by Alok, 2026-11-17).

What

"Research before you ask" in @veda-hq/sarathi: an agent settles what an authoritative source can settle before it asks the owner, and asks only what no source can answer.

  • skills/working-rules/SKILL.md: a short "Research before you ask" section (source order, read the state you can read, cite instead of "docs say X, OK?", state a cheap reversible assumption and go on, the owner-only kinds, check which options work before offering them). "Stop and ask when" now starts with "Research first (above)". This file is always imported.

  • skills/research-first/SKILL.md (new plugin skill sarathi:research-first): the full rule, the owner-only table, the source order, and how to ask. Each of its 10 sources has a URL and the date it was read (2026-11-16). The researcher agent checked every URL (Model Spec pinned at 2026-10-29; the Amazon 2015 letter is cited from SEC EDGAR, because no Amazon-hosted copy resolves).

  • agents/researcher.md: returns a verdict (Settled / Conflicting / Not settled). Each claim comes with a URL, version and date read, in the source order above. It also says when the caller should act instead of asking.

  • hooks/ask-gate.sh (PreToolUse, AskUserQuestion|mcp__ccd_turn__wrap_up): checks that a look happened, not what it found.

    • It refuses (exit 2) an AskUserQuestion, or a wrap_up ask that offers a choice, when nothing since the owner's last message counts as research. Research is a WebSearch, a WebFetch, a browser page read, or the launch of a research subagent.
    • A choice means two or more reply buttons that are not plain yes/no, or an ask that names alternatives. A bare A/B counts; a path, URL or owner/repo#N does not.
    • A wrap_up with no ask, a go-ahead, or an app action first passes.
    • A second gated ask in the same turn passes. The key is the turn, not the text, and it is kept in $XDG_STATE_HOME/sarathi/ask-gate/<session>.
    • A pass is a silent exit 0. Anything it cannot read passes.
  • How the gate finds the owner's last message. It checks the marks Claude Code writes on user-role entries:

    • origin.kind is task-notification or peer → not the owner;
    • turnOrigin is task_notification, peer or auto_continuation → not the owner;
    • isMeta, isCompactSummary, tool results and sidechains → not the owner;
    • entries without marks fall back to their text: an entry that is only harness tags (<system-reminder>, <task-notification>, <side-sessions-event>, <ci-monitor-event> …) is not the owner.

    I checked this against the entry shapes in real local transcripts. No transcript text is in the repo: every fixture is synthetic. In that sample, host_synthetic is either a side session's starting task, which has text outside the tags and counts as the owner, or a <side-sessions-event>, which has only tags and does not count.

  • README: a handler row, a "Research before you ask" section with the .claude/sarathi.json entry and the wider settings matcher, and the skill in "The working rules" and "The plugin". Plugin manifest 0.6.6 → 0.7.1; the marketplace description is updated. No repository rule pins the plugin's version. I bumped it so installed plugins pick up the new skill, and chose 0.7.1 to match the next legal package version. package.json stays at 0.7.0.

Does it still catch something (evidence)

The design's sample covered 30 past asks from 40 recent athya_mono/niyam/veda transcripts:

  • 3 could have been settled by research;
  • 9 were mixed: the owner's call, but options were offered before checking which of them work;
  • 18 were owner-only.

All 12 researchable or mixed asks went through wrap_up (161 asks in all) or AskUserQuestion (12 calls). None went through GitHub comments or plain text (3 plain-text questions in total). The usual pattern was offering A/B before reading the docs that rule one out: cloud image contents, which branch a cloud session clones, billing steps, credential options. The other was asking about state the agent could read: has the PR merged, does the label clear itself, did another session already fix it. Research came only after the owner pushed back. So the gate covers those two channels, only choice-shaped asks, and has no Stop gate.

Tests

src/hooks/ask-gate.test.ts, in the style of shell-hooks.test.ts, with synthetic fixtures. It covers:

  • research in the turn passes, for each research tool and subagent type;
  • non-research does not count: Read, Bash curl, a worker agent, a browser click, a subagent's own sidechain search;
  • research before the owner's last message does not count;
  • a background agent's hand-back, a peer message, an app event, a reminder, a meta entry, a compaction summary, an interruption or a tool result does not reset the window;
  • a side session's starting task opens the window;
  • wrap_up choice and approval shapes, including owner/repo#N, paths and URLs;
  • the second ask passes, keyed on the turn;
  • the refusal text, a silent pass, garbage input, unwritable state, and the state-folder fallbacks.

src/skills.test.ts covers both skills' frontmatter, the rules and their order, and every source URL.

pnpm check passed (exit 0) across the repository. sarathi: tests 535, pass 532, fail 0 (the 3 that are not counted as passes are the live Docker services cases, which run only with SARATHI_LIVE_SERVICES=required). pariksha's smoke reports boundary: 535 files, none names a denied word and repo-rules: … nothing refused. After the follow-up fix to the A/B pattern, the gate's suite ran again:

✔ ask-gate: research since the owner spoke lets the ask through
✔ ask-gate: what is not research does not count
✔ ask-gate: research before the owner last spoke does not count
✔ ask-gate: a hand-back, a peer's message or an app event does not reset the window
✔ ask-gate: the task that started a side session opens its window
✔ ask-gate: with no owner message, the whole transcript is the window
✔ ask-gate: a wrap_up is gated only when its ask offers a choice
✔ ask-gate: a second gated ask in the same turn passes, whatever it says
✔ ask-gate: the refusal carries the rule and the owner-only kinds
✔ ask-gate: a pass is silent, never an allow answer
✔ ask-gate: anything it cannot read passes
✔ ask-gate: a session without an id is keyed on its transcript
✔ ask-gate: state goes under XDG_STATE_HOME, else ~/.local/state
✔ ask-gate: runs the launcher interpreter when it names one
ℹ tests 14
ℹ pass 14
ℹ fail 0

Run against this session's own transcript (locally, not committed), the gate exits 0, as it should: a researcher launch followed the task, and only notifications, peer messages and a hand-back came after.

An AskUserQuestion event through sarathi hook PreToolUse, with ask-gate.sh listed in a sarathi.json:

first ask: exit 2; stdout ""; stderr "BLOCKED: research before you ask. No WebSearch, WebFetch, browser page read or research subagent (researcher, Explore .."
second ask: exit 0; stdout ""; stderr ""

Not verified in a live session. kavach refuses running claude -p from an agent worktree, as "a program whose writes the guard does not know", so I could not run a headless probe. The facts that the deny reaches the model come from the hooks docs: PreToolUse matches AskUserQuestion and MCP tools, and exit-2 stderr goes to Claude. The run above shows exit 2 with the reason on stderr through sarathi's own runner.

Release and pin bump (not done here)

  1. The next v0.7.x tag publishes @veda-hq/sarathi with the handler and skills. Set packages/sarathi/package.json to that version on main, then push the tag.
  2. A project bumps its @veda-hq/sarathi pin, then runs pnpm install. In veda that install must happen in the main checkout, because the hooks load from there.
  3. It adds { "matcher": "AskUserQuestion|mcp__ccd_turn__wrap_up", "command": "bash \"$SARATHI_HOOKS\"/ask-gate.sh", "timeout": 10 } to .claude/sarathi.json.
  4. It widens the PreToolUse matcher in .claude/settings.json to include AskUserQuestion|mcp__ccd_turn__wrap_up; sarathi check fails until it does. The auto-mode classifier refuses an agent's edit to hook wiring, so Alok makes this edit.
  5. niyam's later share: onboard writes the entry and widens the matcher, and its Needs you: comment rule asks for the research.

🤖 Generated with Claude Code


Summary by cubic

Adds a "research before you ask" rule to @veda-hq/sarathi and a hook that enforces it: agents now settle what authoritative sources can settle before asking the owner, and ask-gate.sh refuses one owner-directed ask per turn when no research happened since the owner's last message.

What changed

  • working-rules gains the research rule; the new sarathi:research-first skill carries the full rule, the owner-only kinds (shared systems included), source order, and dated sources.
  • The researcher agent returns a verdict (Settled, Conflicting, Not settled) with each claim's URL, version, and date read; a settled fact authorizes nothing the owner must still decide, and a claim settles only when cross-checked or sourced from the authority itself (a standards body, or the project's own docs, changelog, or source).
  • ask-gate.sh gates AskUserQuestion and choice-shaped wrap_up asks when the turn has no WebSearch, WebFetch, browser page read, or research subagent.
  • A bare A/B counts as a choice only when the buttons are not all yes/no, so paths and owner/repo#N references pass; a URL is read out of an ask before alternatives are checked, ending at whitespace, brackets, quotes, or , ; | and keeping balanced parentheses and bracketed IPv6 hosts, so words glued to it stay.
  • A second gated ask in the same turn passes, keyed to the turn rather than the question text; an empty tool input and anything unreadable pass silently.
  • Tests cover synthetic transcripts, wrap_up approval shapes, state fallbacks, and the refusal message.

Rollout

  • The next v0.7.x tag publishes the handler and skills; projects must bump their @veda-hq/sarathi pin and run pnpm install in the main checkout.
  • Add the AskUserQuestion|mcp__ccd_turn__wrap_up matcher to .claude/sarathi.json and widen the PreToolUse matcher in .claude/settings.json; sarathi check fails until the settings matcher is widened.