SLOPSHOPPER

com.infiquetra.claude

Portable Agent Skills, Agent Plugins, and vendor adapters for Infiquetra workflows

panebandguardcommandtoaststatus
A shopper browsing a rack in a slop shop
Preview · a replayed session in a sandbox
claude · ~/work/app · com.infiquetra.claude
│ ┃ Setup ✕ › fix the failing auth╭────────────────────────────────────────────╮ │ ┃ The setup survey failed. │ com.infiquetra.claude │ │ ┃ saga_setup survey failed (unreadable): saga_ ● com.infiquetra.claud│ saga: could not read the machine record │ │ ┃ printed no JSON object ● com.infiquetra.claud│ (unreadable): saga_setup offer-status │ │ ┃ ⏺ Read(src/auth.ts) │ printed no JSON object │ │ ┃ [ Close ] ⎿ Read 6 lines ╰────────────────────────────────────────────╯ │ ⏺ Update(src/auth.ts) │ ⎿ Added 2 lines, removed 1 line │ ⏺ Bash(bun test) │ ⎿ 3 pass, 1 fail │ │ ● Done. refresh now rejects expired claims and logs an audit event. │ │ ✻ Worked for 42s · done 4:20 PM │ │ › /plan-view │ ⎿ com.infiquetra.claude: plan-view: could not read the saga run (u │ │ ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── › ? for shortcuts

Draws

Pane · Setup
The setup survey failed. saga_setup survey failed (unreadable): saga_setup survey printed no JSON object [ Close ]
Pane · saga-plan
No plan loaded. Run /plan-view, /plan-view #issue or /plan-view path.
Pane · saga-admission
No admission review is open.
Pane · saga-merge
No merge confirmation is open.
Pane · saga-review
No review loaded. Run /review-view or /review-view #issue.
Pane · saga-record
No run record loaded. Press Record on the saga run band.
README

Infiquetra Agent Plugins

Portable Agent Skills, Agent Plugins, and vendor adapters for Infiquetra engineering workflows.

This public repository is the design and source catalog for behavior that does not belong to one coding-agent vendor. It complements, and is intended to eventually generate parts of, the existing Claude Code, Codex, Antigravity, OpenCode, and Hermes plugin repositories. Those repositories remain the runtime sources of truth until a recorded custody decision moves that authority here, and no such decision has been made.

Status

Custody moved here on 2026-09-22. This repository is the source of truth for every Infiquetra plugin. The thirteen live plugins of infiquetra-claude-plugins (commit acc99fe7; the fourteenth, team-execution, had already been archived upstream) were imported once, package by package, and are authored here from that commit: there is no upstream pin and no provenance manifest any more. The decision, its rationale, and the four rules it changed are the 2026-09-22 entry in DECISIONS.md; the run plan is docs/plans/2026-09-22-custody-move-and-claude-plugins-retirement-plan.md.

What every package looks like now:

  • Vendor-neutral core at the package root (plugin.json, skills/, scripts/, references/, tests/). A package whose upstream shipped only Claude commands now also carries a portable skill over its scripts, so the three skill-scoped harnesses (OpenCode, Gemini CLI, Hermes) can reach them.
  • Claude-only behaviour under com.infiquetra.claude/ (commands, agents, hooks, MCP registration, output styles, Claude Code mods: TypeScript hooks modules under mods/ and a types/index.d.ts state contract). The root .claude-plugin/plugin.json and the generated root marketplace carry paths and identity only (scripts/sync_marketplace.py, checked by check_repo.py).
  • Codex packaging at plugins/<pkg>/.codex-plugin/plugin.json and the root .agents/plugins/marketplace.json, both generated (scripts/sync_codex_packaging.py), for the same reason and under the same paths-only rule.
  • Muse installs plugin bundles through a staged marketplace at build/muse-marketplace/ (scripts/sync_marketplace.py). The installer selects the 12 applicable packages, projects their Claude-compatible manifests, and installs via muse plugins, registered under the compatibility name infiquetra-claude-plugins but built from this repository. fleet-core and house-style are excluded. See the Muse install workflow.
  • Fleet Core is a library, bundled at build time into each consumer's _bundled/ directory (scripts/bundle_fleet_module.py); it is not installed as a plugin by any harness. The upstream discovery shim (fleet_commons_shim.py) does not cross the boundary.
  • Tests travel with the package under plugins/<pkg>/tests/ and run in CI with pytest's importlib import mode; repository-level tests stay standard-library-only.

How a harness on a machine installs the catalog is docs/runbooks/install-clients.md (scripts/install_client.py, with --check readback and a legacy uninstall for placements that recorded the old repository).

Compatibility evidence binds to a released package version rather than to every tree (2026-09-22 decision). The ten-client matrices under docs/evidence/ that predate the import are superseded and kept as historical context; fresh assessments for the authored versions are queued work, not a claim this README makes.

The record of the work, in the order a new reader should take it:

Packages

PackageVersionDescriptionInstallable surfaces
plugins/agent-launcher/1.7.1Create one verified coding-agent session through the agents wrapper and HerdrClaude, Codex, Muse, 1 skill
plugins/agy/0.6.2Antigravity-backed coder reviewer bridge agents that run in a disposable clone and…Claude, Codex, Muse, 1 skill
plugins/codex/0.1.5Portable Codex delegation wrapperClaude, Codex, Muse, 1 skill
plugins/deploy/0.2.3Tag-promotion deployment operations for Infiquetra repositoriesClaude, Codex, Muse, 1 skill
plugins/fleet-core/0.32.0Authored Fleet Core library: staffing (work shape, role, and lens to model and…Codex
plugins/hermes-profile-evolution/0.1.5Portable request adapter for target-sovereign Hermes profile evolutionClaude, Codex, Muse, 1 skill
plugins/home-lab-ops/1.2.2Proxmox VE cluster operations, Ansible pre-flight validation, Ceph management,…Claude, Codex, Muse, 6 skills
plugins/house-style/0.1.1Claude-only packageClaude, Codex
plugins/mission-control/2.21.1SDLC management for Operations, Asgard, and CAMPPS: prepared issue drafts, live schema…Claude, Codex, Muse, 8 skills
plugins/orchestrate/6.0.1Run one piece of work across several herdr agent sessions, one git worktree per unitClaude, Codex, Muse, 1 skill
plugins/redis-channel/0.5.3Portable Redis Streams bridgeClaude, Codex, Muse, 1 skill
plugins/saga/1.2.1Infiquetra lifecycle plugin: one automatic run per issue — admission, plan, plan…Claude, Codex, Muse, 14 skills
plugins/unifi/2.0.7Portable UniFi Network and Protect package: two Agent Skills with their bundled Python…Claude, Codex, Muse, 2 skills
plugins/voice/0.4.0Portable voice package: a spoken conversational loop for one explicitly bound,…Claude, Codex, Muse, 1 skill

Portable Fleet Core scope

The full Fleet Core module set at the import commit is here (plugins/fleet-core/scripts/fleet_commons/, plus scripts/jev.py and references/). What is deliberately absent is stated in plugins/fleet-core/DEFERRED.md: the Claude-specific discovery shim, and one residual data file kept only while a consumer still declares it.

Operator site profile

The portable UniFi package can read an optional operator site profile — a JSON file describing one site's topology and intent — from a machine-local path. The profile is optional in the strong sense: a runtime with no profile anywhere loads successfully in discovery-only mode and infers no trust role, criticality, or ownership rather than guessing a default.

How a profile is authored and deployed is the operator's business. The Infiquetra instance keeps its profile in a private repository and renders JSON at deployment time through an existing Ansible harness. That arrangement is one operator's custody instance and is not required by the portable contract, which knows only a path. See plugins/unifi/references/site-profile.md for the contract itself.

Repository layout

PathPurpose
plugins/Portable packages, authored here since the 2026-09-22 custody move
ports/One port descriptor per package: identity, custody, and assessment settings
schemas/JSON Schemas for the contracts this repository validates
scripts/Validation, synchronization, bundling, and inventory tools
docs/runbooks/install-clients.mdPlace this catalog on each harness, read the placement back, and remove the old marketplace
docs/Architecture, public guidance, and durable repository knowledge
docs/plans/Approved implementation plans
docs/evidence/Assessment records, written under the public evidence rules
docs/engineering-journal/Learnings, decisions, queued work, and archive
.github/Pull request, issue, and validation workflow configuration

Client-specific material lives in an explicit adapter directory inside the package that needs it, never at the package root. plugins/unifi/com.infiquetra.claude/ and plugins/mission-control/com.infiquetra.claude/ are Claude adapters; the portable manifests, skills, schemas, and scripts beside them carry no client-specific assumption.

Validation

Run the same checks used by continuous integration (CI):

python3 scripts/check_repo.py
python3 -m unittest discover -s tests -v
git diff --check

The validation script checks the repository baseline, local Markdown links, the Agent Plugin manifests under plugins/, each package's provenance manifest, the build declarations and generated bundle stamps, the portable skills' frontmatter, and that no TypeScript or JavaScript module source (nor the tsconfig.json Claude Code writes at a package root) sits outside a Claude adapter. It installs nothing and makes no network call, so this baseline cannot be broken by a package index outage. A second continuous integration job pins the catalog's declared floor, python>=3.12, installs requests, urllib3, and pytest, and runs the ported plugin tests. The pin is the floor itself rather than the newest interpreter, because a floor that is never exercised is not a floor. The floor is a single value with a single owner, tests/test_python_floor.py, and every place the catalog states it is checked against that owner.

A third continuous integration job, claude-mods, installs Claude Code at the recorded floor build (2.1.286) and at the pinned build (2.1.289) and, for every package whose Claude adapter names a hooks module, runs claude plugin validate --strict and claude plugin test. Neither command needs a login.

Development

  • Read AGENTS.md before changing the repository.
  • Use conventional commit messages.
  • Use pull requests after the initial repository bootstrap.
  • Update the engineering journal when work creates a durable learning, repository decision, or deferred item.
  • Do not commit credentials, generated installed copies, or local agent state.

Public development guidance is summarized in docs/public-safe-summary.md.

Source 14 files
mods/index.ts 28 lines
1// Saga's one hooks module, named from `../hooks/hooks.json` under `modules`.
2//
3// The engine loads exactly one module per plugin, so every saga mod registers
4// from here: each mod lives in its own file beside this one and exports a
5// function this `register` calls. Mods display saga state and collect answers;
6// saga's scripts own that state and enforce every policy (see `run-record.ts`).
7
8import type { Register } from 'claude-code'
9import { registerAdmissionReview } from './admission-review.tsx'
10import { registerAgentTypes } from './agent-types.ts'
11import { registerMergeConfirmation } from './merge-confirmation.tsx'
12import { registerPlanViewer } from './plan-viewer.tsx'
13import { registerReviewPane } from './review-pane.tsx'
14import { registerSetupPane } from './setup-pane.tsx'
15import { registerUsageCapture } from './usage-capture.ts'
16import { registerRunBand } from './run-band.tsx'
17
18export const register: Register = (on) => {
19  registerPlanViewer(on)
20  registerAdmissionReview(on)
21  registerMergeConfirmation(on)
22  registerReviewPane(on)
23  registerSetupPane(on)
24  registerUsageCapture(on)
25  registerRunBand(on)
26  registerAgentTypes(on)
27}
28
mods/admission-review.tsx 510 lines
1// The admission review pane (issue #103): admission's staffing answer
2// (question 4, `staffing_overrides`) and lens answer (question 5,
3// `lens_declaration`) as two tables with a control per row.
4//
5// The model calls the tool `mcp__saga__review_admission` with an issue number.
6// The tool runs `scripts/admission.py --dry-run --render json`, draws the pane
7// from that document alone (schema `admission_review.v1`), and waits for the
8// operator. Submit hands the answers to `admission.py --answers -` on standard
9// input, so the script validates and records them; the tool then returns them
10// to the model as its result. Closing the pane returns `dismissed`, and the plan
11// skill prints admission's fixed tables instead.
12//
13// The mod decides nothing. Every choice a Select offers comes from the palette
14// the script printed, never a list written here; the script is the one place an
15// answer is checked, and a refusal it prints is shown in the pane as printed.
16//
17// Waiting for a person: a hook's own time is capped at ten seconds per
18// dispatch, and the clock stops only while a `$` call is in flight. Awaiting a
19// bare Promise therefore fails the hook at ten seconds; the tool waits on
20// `$.process.run(['sleep', '1'])` instead, one second at a time, up to
21// WAIT_CAP_SECONDS (LEARNINGS.md, 2026-10-04).
22
23import { atom, read, update } from 'claude-code'
24import type { On, ProcessRunResult } from 'claude-code'
25import type {
26  SagaAdmissionAnswers,
27  SagaAdmissionPalette,
28  SagaAdmissionReviewData,
29  SagaAdmissionReviewOutcome,
30  SagaAdmissionSelections,
31  SagaAdmissionTier,
32} from '../types/index.d.ts'
33import { paneResult } from './pane-result.ts'
34import { sagaScriptArgv } from './run-record.ts'
35
36/** The pane's id. */
37export const PANE = 'saga-admission'
38
39/** The tool's short name; the model calls it as `mcp__saga__review_admission`. */
40export const TOOL = 'review_admission'
41
42/** The review document version this module reads; `REVIEW_SCHEMA` in `scripts/admission.py`. */
43export const REVIEW_SCHEMA = 'admission_review.v1'
44
45/** The two questions the pane answers. With neither outstanding there is nothing to review. */
46export const REVIEWED_QUESTIONS = ['staffing_overrides', 'lens_declaration'] as const
47
48/** How long the tool waits for the operator before it returns `timed-out`: thirty minutes. */
49export const WAIT_CAP_SECONDS = 1800
50
51/** How long one admission run may take; it reads the issue with `gh`. */
52const ADMISSION_TIMEOUT_MS = 120_000
53
54/** The two decisions a conditional lens takes. */
55const INCLUDE_CHOICES = ['yes', 'no'] as const
56
57const review = atom({ plugin: 'saga', key: 'admissionReview' } as const, null)
58
59/**
60 * The outcome of the review in flight, by issue. A module value, not `$.state`:
61 * the waiting hook reads it between its sleeps, and a state read inside one
62 * dispatch sees one frozen moment. A hot reload drops it with the pane.
63 */
64const outcomes = new Map<number, SagaAdmissionReviewOutcome>()
65
66/** The issue whose review is open, or null. One review at a time. */
67let active: number | null = null
68
69// ---------------------------------------------------------------------------
70// Pure helpers: no `$`, so a test calls them directly
71// ---------------------------------------------------------------------------
72
73/** The argv that prints the review document for an issue without writing the record. */
74export function reviewArgv(pluginRoot: string, issue: number, repo: string | null): string[] {
75  return sagaScriptArgv(pluginRoot, 'admission.py', [
76    '--issue',
77    String(issue),
78    ...(repo ? ['--repo', repo] : []),
79    '--dry-run',
80    '--render',
81    'json',
82  ])
83}
84
85/** The argv that records the answers read from standard input. */
86export function answersArgv(pluginRoot: string, issue: number, repo: string | null): string[] {
87  return sagaScriptArgv(pluginRoot, 'admission.py', [
88    '--issue',
89    String(issue),
90    ...(repo ? ['--repo', repo] : []),
91    '--answers',
92    '-',
93  ])
94}
95
96type Ran = Pick<ProcessRunResult, 'exitCode' | 'stdout' | 'stderr' | 'isStdoutTruncated'>
97
98/** Turn what `admission.py --render json` did into the review document, or why there is none. */
99export function parseReview(
100  ran: Ran,
101): { ok: true; data: SagaAdmissionReviewData } | { ok: false; reason: string; exitCode?: number; stderr?: string } {
102  const stderr = ran.stderr.trim()
103  if (ran.exitCode !== 0) {
104    return { ok: false, reason: `admission.py exited ${ran.exitCode}`, exitCode: ran.exitCode, stderr }
105  }
106  if (ran.isStdoutTruncated) {
107    return { ok: false, reason: 'admission.py printed more than the engine keeps (4 MiB); the review was cut off' }
108  }
109  let parsed: unknown
110  try {
111    parsed = JSON.parse(ran.stdout)
112  } catch (err) {
113    return { ok: false, reason: `admission.py did not print JSON: ${String(err)}` }
114  }
115  if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
116    return { ok: false, reason: 'admission.py did not print a JSON object' }
117  }
118  const schema = (parsed as { schema?: unknown }).schema
119  if (schema !== REVIEW_SCHEMA) {
120    return { ok: false, reason: `review version ${JSON.stringify(schema)} is not ${REVIEW_SCHEMA}` }
121  }
122  return { ok: true, data: parsed as SagaAdmissionReviewData }
123}
124
125/** The efforts the palette allows for one model, in palette order. */
126export function effortsFor(palette: SagaAdmissionPalette, model: string): string[] {
127  return palette.pairs.filter((pair) => pair.model === model).map((pair) => pair.effort)
128}
129
130/** A tier the palette allows: the effort kept when the model takes it, else the model's ceiling. */
131export function clampTier(palette: SagaAdmissionPalette, model: string, effort: string | null): { model: string; effort: string } {
132  const allowed = effortsFor(palette, model)
133  if (effort !== null && allowed.includes(effort)) return { model, effort }
134  const ceiling = palette.effort_ceilings[model]
135  if (ceiling !== undefined && allowed.includes(ceiling)) return { model, effort: ceiling }
136  return { model, effort: allowed[allowed.length - 1] ?? '' }
137}
138
139function pickFrom(palette: SagaAdmissionPalette, tier: SagaAdmissionTier | null): { model: string; effort: string } {
140  if (tier && palette.models.includes(tier.model)) return clampTier(palette, tier.model, tier.effort)
141  const first = palette.pairs[0]
142  return first ? { model: first.model, effort: first.effort } : { model: '', effort: '' }
143}
144
145/**
146 * The picks the pane opens with: each role's
147 * proposed tier, and each conditional lens as declared; an undeclared lens is
148 * included when Jev pre-checked it (issue #110) and left out, with no reason
149 * yet, otherwise.
150 */
151export function initialSelections(data: SagaAdmissionReviewData): SagaAdmissionSelections {
152  const palette = data.palette
153  const staffing: SagaAdmissionSelections['staffing'] = {}
154  if (palette) {
155    for (const row of data.staffing.rows) staffing[row.role] = pickFrom(palette, row.proposed ?? row.default)
156  }
157  const lenses: SagaAdmissionSelections['lenses'] = {}
158  for (const row of data.lenses.rows) {
159    if (row.always_on) continue
160    if (row.include === 'yes' || row.include === 'no') {
161      lenses[row.lens] = { include: row.include, reason: row.reason }
162    } else {
163      lenses[row.lens] = { include: row.jev.band === 'pre-checked' ? 'yes' : 'no', reason: '' }
164    }
165  }
166  return { staffing, lenses }
167}
168
169/**
170 * The picks "Accept all" submits: every proposal that exists, applied over the
171 * operator's current picks. Each role takes its proposed tier, and each lens
172 * that is declared or that Jev pre-checked takes that answer; a lens with no
173 * proposal keeps the operator's own include choice and reason.
174 */
175export function acceptAllSelections(
176  data: SagaAdmissionReviewData,
177  current: SagaAdmissionSelections,
178): SagaAdmissionSelections {
179  const proposed = initialSelections(data)
180  const lenses: SagaAdmissionSelections['lenses'] = { ...proposed.lenses }
181  for (const row of data.lenses.rows) {
182    if (row.always_on) continue
183    const hasProposal = row.include === 'yes' || row.include === 'no' || row.jev.band === 'pre-checked'
184    const own = current.lenses[row.lens]
185    if (!hasProposal && own) lenses[row.lens] = own
186  }
187  return { staffing: proposed.staffing, lenses }
188}
189
190/** The conditional lenses left out with no reason yet, in table order. */
191export function unexplainedExclusions(selections: SagaAdmissionSelections): string[] {
192  return Object.entries(selections.lenses)
193    .filter(([, pick]) => pick.include === 'no' && pick.reason.trim() === '')
194    .map(([lens]) => lens)
195}
196
197/**
198 * The answers to record: the complete role map, so every role's source becomes
199 * the operator, and the lens declaration in the shape the review reads.
200 */
201export function buildAnswers(data: SagaAdmissionReviewData, selections: SagaAdmissionSelections): SagaAdmissionAnswers {
202  const vendor = data.palette?.vendor ?? ''
203  const staffing_overrides: SagaAdmissionAnswers['staffing_overrides'] = {}
204  for (const row of data.staffing.rows) {
205    const pick = selections.staffing[row.role]
206    if (!pick) continue
207    staffing_overrides[row.role] = {
208      vendor: row.vendor ?? row.proposed?.vendor ?? row.default?.vendor ?? vendor,
209      model: pick.model,
210      effort: pick.effort,
211    }
212  }
213  const conditional_applies: Record<string, string> = {}
214  const conditional_does_not_apply: Record<string, string> = {}
215  for (const row of data.lenses.rows) {
216    const pick = selections.lenses[row.lens]
217    if (row.always_on || !pick) continue
218    if (pick.include === 'yes') conditional_applies[row.lens] = pick.reason.trim()
219    else conditional_does_not_apply[row.lens] = pick.reason.trim()
220  }
221  return {
222    staffing_overrides,
223    lens_declaration: {
224      always_on: data.lenses.rows.filter((row) => row.always_on).map((row) => row.lens),
225      conditional_applies,
226      conditional_does_not_apply,
227    },
228  }
229}
230
231function tierText(tier: SagaAdmissionTier | null): string {
232  if (!tier) return 'not configured'
233  return `${tier.vendor ? `${tier.vendor} ` : ''}${tier.model}/${tier.effort ?? 'default'}`
234}
235
236function issueOf(input: unknown): number | null {
237  const issue = (input as { issue?: unknown }).issue
238  return typeof issue === 'number' && Number.isInteger(issue) && issue > 0 ? issue : null
239}
240
241function repoOf(input: unknown): string | null {
242  const repo = (input as { repo?: unknown }).repo
243  return typeof repo === 'string' && /^[\w.-]+\/[\w.-]+$/.test(repo) ? repo : null
244}
245
246// ---------------------------------------------------------------------------
247// The hooks
248// ---------------------------------------------------------------------------
249
250export function registerAdmissionReview(on: On): void {
251  on('session.start', async ($, e, next) => {
252    await $.tool.register({
253      name: TOOL,
254      description:
255        "Opens saga's admission review pane for an issue: the staffing table (admission question 4, " +
256        'staffing_overrides) and the lens table (question 5, lens_declaration), each row with its ' +
257        "own control. The operator's answers are recorded through scripts/admission.py, which " +
258        'validates them. Returns status submitted (with the answers recorded), dismissed, ' +
259        'not-placed, unavailable, nothing-to-review, timed-out or error; on anything but ' +
260        'submitted, print admission.py --render tables and collect the answers in the conversation.',
261      inputSchema: {
262        type: 'object',
263        properties: {
264          issue: { type: 'integer', minimum: 1, description: 'The issue number admission runs for.' },
265          repo: { type: 'string', description: 'owner/name; defaults to the origin remote.' },
266        },
267        required: ['issue'],
268      },
269    })
270    return next(e)
271  })
272
273  on('tool.call', { tool: 'mcp__saga__review_admission' }, async ($, e, next) => {
274    const issue = issueOf(e)
275    const repo = repoOf(e)
276    if (issue === null) {
277      return { result: paneResult({ status: 'error', reason: 'issue must be a positive whole number' }) }
278    }
279    if (active !== null) {
280      return { result: paneResult({ status: 'error', reason: `the review pane is already open for issue ${active}` }) }
281    }
282    const surfaces = await $.session.surfaces()
283    if (!surfaces.some((surface) => surface === 'terminal' || surface === 'desktop')) {
284      return {
285        result: paneResult({ status: 'unavailable', reason: 'no terminal or desktop surface is attached to draw the pane' }),
286      }
287    }
288
289    let ran: ProcessRunResult
290    try {
291      ran = await $.process.run(reviewArgv($.plugin.root, issue, repo), { timeoutMs: ADMISSION_TIMEOUT_MS })
292    } catch (err) {
293      return { result: paneResult({ status: 'error', reason: `admission.py did not run: ${String(err)}` }) }
294    }
295    const parsed = parseReview(ran)
296    if (!parsed.ok) {
297      const { ok: _ok, ...failure } = parsed
298      return { result: paneResult({ status: 'error', ...failure }) }
299    }
300    const data = parsed.data
301    if (!REVIEWED_QUESTIONS.some((question) => data.pending_questions.includes(question))) {
302      return { result: paneResult({ status: 'nothing-to-review' }) }
303    }
304    if (data.palette === null) {
305      return { result: paneResult({ status: 'error', reason: 'admission could not load the tier palette' }) }
306    }
307    if (data.staffing.rows.length === 0) {
308      return { result: paneResult({ status: 'error', reason: 'admission could not reach the staffing component' }) }
309    }
310
311    active = issue
312    outcomes.delete(issue)
313    await update($, review, () => ({ issue, repo, data, selections: initialSelections(data), error: null }))
314    let outcome: SagaAdmissionReviewOutcome | undefined
315    try {
316      const opened = await $.ui.open({ id: PANE, title: `Admission #${issue}: staffing and lenses`, focus: true })
317      if (!opened.isPlaced) {
318        return { result: paneResult({ status: 'not-placed', reason: opened.reason }) }
319      }
320      let waited = 0
321      while (!outcomes.has(issue) && !next.signal.aborted && waited < WAIT_CAP_SECONDS) {
322        await $.process.run(['sleep', '1'])
323        waited += 1
324      }
325      outcome = outcomes.get(issue)
326      return { result: paneResult(outcome ?? { status: next.signal.aborted ? 'dismissed' : 'timed-out' }) }
327    } catch (err) {
328      return { result: paneResult({ status: 'error', reason: `the review pane failed: ${String(err)}` }) }
329    } finally {
330      active = null
331      outcomes.delete(issue)
332      await $.ui.close({ id: PANE })
333      await update($, review, () => null)
334    }
335  })
336
337  on('ui.close', { id: PANE }, ($, e, next) => {
338    // Every close but the tool's own after an outcome: the operator's close mark or Escape.
339    if (active !== null && !outcomes.has(active)) outcomes.set(active, { status: 'dismissed' })
340    return next(e)
341  })
342
343  on('ui.render', { component: 'Pane', requestId: PANE }, async ($, e) => {
344    const state = await read($, review)
345    if (e.surface !== 'terminal' && e.surface !== 'desktop') {
346      const { Text } = $.ui.resolve(e)
347      return <Text>Answer the admission review in the terminal or the desktop app.</Text>
348    }
349    const { Box, Button, Input, Select, Text } = $.ui.resolve(e)
350    if (state === null || state.data.palette === null) {
351      return <Text dimColor>No admission review is open.</Text>
352    }
353    const { data, selections } = state
354    const palette = state.data.palette
355    const issue = state.issue
356
357    const pickModel = (role: string, model: string) =>
358      update($, review, (current) => {
359        if (!current || !palette.models.includes(model)) return current
360        const effort = current.selections.staffing[role]?.effort ?? null
361        const staffing = { ...current.selections.staffing, [role]: clampTier(palette, model, effort) }
362        return { ...current, selections: { ...current.selections, staffing }, error: null }
363      })
364    const pickEffort = (role: string, effort: string) =>
365      update($, review, (current) => {
366        const pick = current?.selections.staffing[role]
367        if (!current || !pick || !effortsFor(palette, pick.model).includes(effort)) return current
368        const staffing = { ...current.selections.staffing, [role]: { model: pick.model, effort } }
369        return { ...current, selections: { ...current.selections, staffing }, error: null }
370      })
371    const setLens = (lens: string, change: { include?: string; reason?: string }) =>
372      update($, review, (current) => {
373        const pick = current?.selections.lenses[lens]
374        if (!current || !pick) return current
375        const include = change.include ?? pick.include
376        if (include !== 'yes' && include !== 'no') return current
377        const lenses = { ...current.selections.lenses, [lens]: { include, reason: change.reason ?? pick.reason } }
378        return { ...current, selections: { ...current.selections, lenses }, error: null }
379      })
380    const setError = (error: string) => update($, review, (current) => current && { ...current, error })
381
382    const submit = async (acceptAll: boolean) => {
383      const current = await read($, review)
384      if (!current || current.issue !== issue) return
385      const picks = acceptAll ? acceptAllSelections(current.data, current.selections) : current.selections
386      const missing = unexplainedExclusions(picks)
387      if (missing.length > 0) {
388        await setError(`Give a reason for each lens left out: ${missing.join(', ')}`)
389        return
390      }
391      if (acceptAll) await update($, review, (now) => now && { ...now, selections: picks })
392      const answers = buildAnswers(current.data, picks)
393      let ran: ProcessRunResult
394      try {
395        ran = await $.process.run(answersArgv($.plugin.root, issue, current.repo), {
396          stdin: JSON.stringify(answers),
397          timeoutMs: ADMISSION_TIMEOUT_MS,
398        })
399      } catch (err) {
400        await setError(`admission.py did not run: ${String(err)}`)
401        return
402      }
403      if (ran.exitCode !== 0) {
404        await setError(ran.stderr.trim() || `admission.py exited ${ran.exitCode}`)
405        return
406      }
407      outcomes.set(issue, { status: 'submitted', issue, answers, source: 'operator', summary: ran.stdout.trim() })
408      await $.ui.close({ id: PANE })
409    }
410    const dismiss = async () => {
411      outcomes.set(issue, { status: 'dismissed' })
412      await $.ui.close({ id: PANE })
413    }
414
415    const modelOptions = palette.models.map((value) => ({ value }))
416    const includeOptions = INCLUDE_CHOICES.map((value) => ({ value }))
417
418    return (
419      <Box flexDirection="column" width={e.props.bodyColumns}>
420        <Text bold>Staffing (answer: staffing_overrides)</Text>
421        {data.staffing.rows.map((row) => {
422          const pick = selections.staffing[row.role] ?? { model: '', effort: '' }
423          return (
424            <Box flexDirection="column" marginTop={1}>
425              <Text>
426                <Text bold>{row.role}</Text> default {tierText(row.default)}, proposed {tierText(row.proposed)}
427              </Text>
428              <Box key={`staff:${row.role}:jev`}>
429                <Text>Jev: {row.jev.cell}</Text>
430              </Box>
431              <Box flexDirection="row" flexWrap="wrap" columnGap={2}>
432                <Select
433                  key={`staff:${row.role}:model`}
434                  label="model "
435                  options={modelOptions}
436                  value={pick.model}
437                  onSelect={(value) => pickModel(row.role, value)}
438                />
439                <Select
440                  key={`staff:${row.role}:effort`}
441                  label="effort "
442                  options={effortsFor(palette, pick.model).map((value) => ({ value }))}
443                  value={pick.effort}
444                  onSelect={(value) => pickEffort(row.role, value)}
445                />
446              </Box>
447              <Text dimColor wrap="wrap">
448                {row.why}
449              </Text>
450            </Box>
451          )
452        })}
453        <Box marginTop={1}>
454          <Text bold>Lenses (answer: lens_declaration)</Text>
455        </Box>
456        {data.lenses.rows.map((row) => {
457          if (row.always_on) {
458            return (
459              <Box flexDirection="row" columnGap={2}>
460                <Text bold>{row.lens}</Text>
461                <Box key={`lens:${row.lens}:always-on`}>
462                  <Text>always on</Text>
463                </Box>
464              </Box>
465            )
466          }
467          const pick = selections.lenses[row.lens] ?? { include: 'no', reason: '' }
468          return (
469            <Box flexDirection="column" marginTop={1}>
470              <Box flexDirection="row" flexWrap="wrap" columnGap={2}>
471                <Text bold>{row.lens}</Text>
472                <Box key={`lens:${row.lens}:jev`}>
473                  <Text>Jev: {row.jev.cell}</Text>
474                </Box>
475                <Select
476                  key={`lens:${row.lens}:include`}
477                  label="include "
478                  options={includeOptions}
479                  value={pick.include}
480                  onSelect={(value) => setLens(row.lens, { include: value })}
481                />
482              </Box>
483              <Input
484                key={`lens:${row.lens}:reason`}
485                label="reason "
486                placeholder={pick.include === 'no' ? 'why it does not apply (required)' : 'why it applies'}
487                value={pick.reason}
488                onInput={(value) => setLens(row.lens, { reason: value })}
489                onSubmit={(value) => setLens(row.lens, { reason: value })}
490              />
491            </Box>
492          )
493        })}
494        {state.error !== null && (
495          <Box key="error" marginTop={1}>
496            <Text color="red" wrap="wrap">
497              {state.error}
498            </Text>
499          </Box>
500        )}
501        <Box flexDirection="row" columnGap={2} marginTop={1}>
502          <Button key="accept-all" label="Accept all" hotkey="a" onPress={() => submit(true)} />
503          <Button key="submit" label="Submit" hotkey="s" variant="primary" onPress={() => submit(false)} />
504          <Button key="dismiss" label="Dismiss" hotkey="d" role="dismiss" onPress={dismiss} />
505        </Box>
506      </Box>
507    )
508  })
509}
510
mods/agent-types.ts 203 lines
1// Saga's roles as Claude Code agent types that carry a real model and effort (issue #106).
2//
3// The Agent tool takes a model per call but no effort, so a plain subagent can
4// only be asked to try harder in its prompt (the effort rider, a labeled proxy;
5// `plugins/fleet-core/references/staffing.md`). An agent type registered here
6// carries both. For each role of this checkout's active saga run the mod
7// registers `saga:<role>` (`saga:worker`, `saga:planner`, ...) with the role's
8// prompt and the model and effort the run is staffed at.
9//
10// What to register is the script's answer, never the mod's:
11// `scripts/role_agent_types.py --json` decides whether a run is active, reads
12// each role's tier (the run record's staffing, completed by the staffing
13// resolver) and reads each prompt from the roles library file at that moment.
14// The mod registers what it printed, when it printed something new.
15//
16// With no active run the script answers no types and nothing is registered, so
17// a session with no saga run sees no new agent type. A type cannot be
18// unregistered, so once a run closes the mod hides its types from the model.
19//
20// A subagent of a registered type is checked on every request it makes: when
21// the model or effort actually sent is not the type's resolved tier, the mod
22// shows a toast and writes a `tiering-drift[claude-agent-type]` line into the
23// transcript (`$.ui.log`, drawn as a notice the model never reads). It never
24// rewrites the request: the mod reports, and policy stays in the scripts.
25
26import type { EngineInterface, On, ProcessRunResult } from 'claude-code'
27import type { SagaAgentTypesState, SagaRegisteredAgentType, SagaRoleAgentTypes } from '../types/index.d.ts'
28import { sagaScriptArgv } from './run-record.ts'
29
30/** The spawn kind `fleet_commons.effort_rider` names this route by; it leads every drift line. */
31export const SPAWN_KIND = 'claude-agent-type'
32
33/** How long the script may take before the mod gives up on this refresh. */
34const REFRESH_TIMEOUT_MS = 30_000
35
36/** How long a drift toast stays up. */
37const DRIFT_TOAST_MS = 8_000
38
39const AGENT_TYPES = { plugin: 'saga', key: 'agentTypes' } as const
40
41/** The argv that prints this checkout's role agent types. */
42export function roleAgentTypesArgv(pluginRoot: string, cwd: string): string[] {
43  return sagaScriptArgv(pluginRoot, 'role_agent_types.py', ['--cwd', cwd, '--json'])
44}
45
46export type RoleAgentTypesRead = { ok: true; answer: SagaRoleAgentTypes } | { ok: false; detail: string }
47
48/** Turn what `role_agent_types.py` did into its answer, or the reason there is none. */
49export function parseRoleAgentTypes(ran: Pick<ProcessRunResult, 'exitCode' | 'stdout' | 'stderr'>): RoleAgentTypesRead {
50  if (ran.exitCode !== 0) {
51    return { ok: false, detail: `role_agent_types exited ${ran.exitCode}: ${ran.stderr.trim()}` }
52  }
53  let parsed: unknown
54  try {
55    parsed = JSON.parse(ran.stdout)
56  } catch (err) {
57    return { ok: false, detail: `role_agent_types printed no JSON: ${String(err)}` }
58  }
59  const answer = parsed as Partial<SagaRoleAgentTypes> | null
60  if (!answer || answer.schema !== 'saga_role_agent_types.v1' || !Array.isArray(answer.types)) {
61    return { ok: false, detail: 'role_agent_types printed an answer this mod does not know' }
62  }
63  if (answer.error) return { ok: false, detail: answer.error }
64  return { ok: true, answer: answer as SagaRoleAgentTypes }
65}
66
67/**
68 * Whether a request's model and effort are a registered type's resolved tier.
69 * A request names a resolved model id (`claude-opus-5-5`) and a type an alias
70 * (`opus`), so the model matches when the id carries the alias. A request that
71 * sends no effort does not match.
72 */
73export function matchesTier(resolved: SagaRegisteredAgentType, model: string, effort: string | number | undefined): boolean {
74  return model.toLowerCase().includes(resolved.model.toLowerCase()) && effort === resolved.effort
75}
76
77/** The drift line: the same `tiering-drift[<spawn kind>]` name `effort_rider.reconcile_effort` uses. */
78export function driftLine(
79  type: string,
80  resolved: SagaRegisteredAgentType,
81  model: string,
82  effort: string | number | undefined,
83  agentId: string,
84): string {
85  const sent = `${model}/${effort === undefined ? 'no effort sent' : String(effort)}`
86  return `tiering-drift[${SPAWN_KIND}]: ${type} resolved ${resolved.model}/${resolved.effort}, request sent ${sent} (agent ${agentId})`
87}
88
89/**
90 * What this load of the module knows. A reload starts it afresh, and also
91 * unloads the plugin's types while the session's state keeps the last answer,
92 * so `registered` (not the state) says whether the types are in place.
93 */
94const loaded = {
95  /** The answer this load registered, as `<active>:<fingerprint>`. */
96  registered: null as string | null,
97  /** Every type this load registered; one the current answer no longer names is hidden. */
98  everRegistered: new Set<string>(),
99  /** A listed subagent's registered type, looked up once; null for one of no saga type. */
100  typeOfAgent: new Map<string, string | null>(),
101  /** Agents already reported, so a drifted subagent raises one toast, not one per request. */
102  reported: new Set<string>(),
103}
104
105/** Register what the script answers for `cwd`, when it answers something this load has not. */
106async function refresh($: EngineInterface, cwd: string): Promise<void> {
107  let read: RoleAgentTypesRead
108  try {
109    read = parseRoleAgentTypes(
110      await $.process.run(roleAgentTypesArgv($.plugin.root, cwd), { timeoutMs: REFRESH_TIMEOUT_MS }),
111    )
112  } catch (err) {
113    read = { ok: false, detail: `role_agent_types did not run: ${err instanceof Error ? err.message : String(err)}` }
114  }
115  if (!read.ok) {
116    $.ui.log(`saga agent types: none registered; ${read.detail}`)
117    return
118  }
119  const { answer } = read
120  const answerKey = `${answer.active}:${answer.fingerprint}`
121  if (loaded.registered === answerKey) return
122
123  // A closed run keeps its types' tiers, so a subagent still running is still checked.
124  const { value: held } = await $.state.get(AGENT_TYPES)
125  const byType: Record<string, SagaRegisteredAgentType> = answer.active ? {} : { ...held?.byType }
126  for (const t of answer.active ? answer.types : []) {
127    try {
128      const { agent } = await $.agent.register({
129        name: t.role,
130        description: t.description,
131        prompt: t.prompt,
132        model: t.model,
133        effort: t.effort,
134      })
135      byType[agent] = { role: t.role, model: t.model, effort: t.effort }
136      loaded.everRegistered.add(agent)
137    } catch (err) {
138      $.ui.log(`saga agent types: saga:${t.role} not registered; ${err instanceof Error ? err.message : String(err)}`)
139    }
140  }
141  const state: SagaAgentTypesState = {
142    active: answer.active,
143    issue: answer.issue,
144    fingerprint: answer.fingerprint,
145    byType,
146  }
147  await $.state.set(AGENT_TYPES, state)
148  loaded.registered = answerKey
149}
150
151export function registerAgentTypes(on: On): void {
152  // A matcher that takes every directory: the engine refuses a second matcherless
153  // `session.start` in one plugin, and the admission review mod holds that slot.
154  on('session.start', { cwd: /^/ }, async ($, e, next) => {
155    await refresh($, e.cwd)
156    return next(e)
157  })
158
159  // The run record's staffing changes between turns (admission, an operator's
160  // answer); a registered type takes effect from the next turn on, so the end
161  // of each main-loop turn is the earliest a change can matter.
162  on('turn.complete', async ($, e, next) => {
163    const result = await next(e)
164    if (!e.agentId) await refresh($, await $.session.cwd())
165    return result
166  })
167
168  // A type cannot be unregistered, so one the current answer does not name (the
169  // run closed, or the role is no longer staffed on this vendor) is kept from the model.
170  on('agent.offer', async ($, e, next) => {
171    if (!loaded.everRegistered.has(e.agent)) return next(e)
172    const { value: held } = await $.state.get(AGENT_TYPES)
173    if (held?.active && held.byType[e.agent]) return next(e)
174    return { isOffered: false }
175  })
176
177  on('turn.step', async function* ($, e, next) {
178    if (e.agentId && !loaded.reported.has(e.agentId)) {
179      const { value: held } = await $.state.get(AGENT_TYPES)
180      if (held && Object.keys(held.byType).length > 0) {
181        let type = loaded.typeOfAgent.get(e.agentId)
182        if (type === undefined) {
183          const agents = await $.agent.list()
184          const found = agents.find((a) => a.id === e.agentId)?.type
185          type = found !== undefined && held.byType[found] ? found : null
186          // An agent not listed yet is looked up again on its next request.
187          if (found !== undefined) loaded.typeOfAgent.set(e.agentId, type)
188        }
189        const resolved = type ? held.byType[type] : undefined
190        if (type && resolved && !matchesTier(resolved, e.model, e.effort)) {
191          loaded.reported.add(e.agentId)
192          const line = driftLine(type, resolved, e.model, e.effort, e.agentId)
193          $.ui.toast(line, { timeoutMs: DRIFT_TOAST_MS })
194          // A transcript line drawn as a notice: the model never reads it, and a
195          // headless host receives it as `ui_log`.
196          $.ui.log(line)
197        }
198      }
199    }
200    return yield* next(e)
201  })
202}
203
mods/merge-confirmation.tsx 464 lines
1// The merge-confirmation pane (issue #165): the grades, the blocking items
2// left at an early stop or the round limit, disputed declarations,
3// consequence disagreements, the fix-later list, degraded inputs and the cost.
4//
5// The model calls the tool `mcp__saga__review_merge` with an issue number and
6// the repository. The tool reads the `review_state.v1` document through
7// `scripts/run_status.py review` and draws the pane from that document alone.
8// Submit hands the answers to `review_state.py answers` on standard input, so
9// the script validates and records them; the tool then returns them to the
10// model as its result. Closing the pane returns `dismissed`, and the work
11// skill prints the script's numbered questions instead. A thirty-minute wait
12// submits `{"answers": {}, "pane_timeout": true}` to the script — the
13// unattended rules file only security-guard items — and returns `timed-out`,
14// which the skill never re-asks.
15//
16// The mod decides nothing. Every choice a Select offers is one of the script's
17// fixed answers; the script is the one place an answer is checked, and a
18// refusal it prints is shown in the pane as printed. The pane itself files and
19// merges nothing: every durable effect is the script's.
20//
21// Waiting for a person: a hook's own time is capped at ten seconds per
22// dispatch, and the clock stops only while a `$` call is in flight. The tool
23// waits on `$.process.run(['sleep', '1'])` instead, one second at a time, up
24// to WAIT_CAP_SECONDS (LEARNINGS.md, 2026-10-04).
25
26import { atom, read, update } from 'claude-code'
27import type { EngineInterface, On } from 'claude-code'
28import type {
29  SagaMergeAnswers,
30  SagaMergeReviewOutcome,
31  SagaMergeSelections,
32  SagaReviewState,
33} from '../types/index.d.ts'
34import { costLine, gradeLabel } from './review-findings.ts'
35import { paneResult } from './pane-result.ts'
36import { readStateReviewWith, sagaScriptArgv } from './run-record.ts'
37
38/** The pane's id. */
39export const PANE = 'saga-merge'
40
41/** The tool's short name; the model calls it as `mcp__saga__review_merge`. */
42export const TOOL = 'review_merge'
43
44/** How long the tool waits for the operator before it submits the timeout: thirty minutes. */
45export const WAIT_CAP_SECONDS = 1800
46
47/** How long one answers run may take; it reads the issue and may file through mission-control. */
48const ANSWERS_TIMEOUT_MS = 120_000
49
50/** The blocking question's choice key in `pending_choices`. */
51export const MERGE_CHOICE = 'merge-blocking'
52
53/** The fix-later choice keys start with this prefix, then the finding identity. */
54export const FIX_LATER_PREFIX = 'fix-later:'
55
56/** The only choices a fix-later item takes; the script refuses anything else. */
57export const FIX_LATER_CHOICES = ['fix-now', 'file-as-issue', 'leave'] as const
58
59/** The only merge decisions; the script refuses anything else. */
60export const MERGE_DECISIONS = ['merge-with-reason', 'stop-card'] as const
61
62const merge = atom({ plugin: 'saga', key: 'mergeReview' } as const, null)
63
64/**
65 * The outcome of the confirmation in flight, by issue. A module value, not
66 * `$.state`: the waiting hook reads it between its sleeps, and a state read
67 * inside one dispatch sees one frozen moment. A hot reload drops it with the pane.
68 */
69const outcomes = new Map<number, SagaMergeReviewOutcome>()
70
71/** The issue whose confirmation is open, or null. One confirmation at a time. */
72let active: number | null = null
73
74// ---------------------------------------------------------------------------
75// Pure helpers: no `$`, so a test calls them directly
76// ---------------------------------------------------------------------------
77
78/**
79 * The argv that records the answers read from standard input. The repository
80 * always rides along: the script refuses answers without both issue and
81 * repository.
82 */
83export function answersArgv(pluginRoot: string, issue: number, repo: string): string[] {
84  return sagaScriptArgv(pluginRoot, 'review_state.py', [
85    'answers',
86    '--issue',
87    String(issue),
88    '--repo',
89    repo,
90    '--answers',
91    '-',
92  ])
93}
94
95/** Whether the document still has the blocking question pending. */
96export function mergeQuestionPending(data: SagaReviewState): boolean {
97  return data.pending_choices.includes(MERGE_CHOICE)
98}
99
100/** The fix-later finding identities with pending choices, in document order. */
101export function fixLaterIds(data: SagaReviewState): string[] {
102  return data.pending_choices
103    .filter((key) => key.startsWith(FIX_LATER_PREFIX))
104    .map((key) => key.slice(FIX_LATER_PREFIX.length))
105}
106
107/** A recorded outcome back to the choice that made it, or '' when unanswered. */
108function choiceForOutcome(outcome: Record<string, unknown> | null): string {
109  switch (outcome?.outcome) {
110    case 'fixed-now':
111      return 'fix-now'
112    case 'filed':
113      return 'file-as-issue'
114    case 'left':
115      return 'leave'
116    default:
117      return ''
118  }
119}
120
121/**
122 * The picks the pane opens with: each pending fix-later item's recorded
123 * choice, or unanswered; re-sending a recorded choice is idempotent and never
124 * refiles. The merge decision always opens unanswered: only the operator can
125 * merge with a reason or stop the card.
126 */
127export function initialSelections(data: SagaReviewState): SagaMergeSelections {
128  const byId = new Map(data.findings.map((finding) => [finding.id, finding]))
129  const fix_later: SagaMergeSelections['fix_later'] = {}
130  for (const id of fixLaterIds(data)) {
131    fix_later[id] = choiceForOutcome(byId.get(id)?.merge_outcome ?? null)
132  }
133  return { fix_later, merge_blocking: { decision: '', reason: '' } }
134}
135
136/**
137 * The answers to record: only the answered keys. A fix-later pick of '' is
138 * unanswered and left out; the merge answer rides only when the operator
139 * chose a decision. `pane_timeout` is always false here; the wait, not the
140 * pane, sends the timeout.
141 */
142export function buildAnswers(selections: SagaMergeSelections): SagaMergeAnswers {
143  const answers: SagaMergeAnswers['answers'] = {}
144  for (const [id, choice] of Object.entries(selections.fix_later)) {
145    if (choice !== '') answers[`${FIX_LATER_PREFIX}${id}`] = choice
146  }
147  const { decision, reason } = selections.merge_blocking
148  if (decision === 'merge-with-reason') answers[MERGE_CHOICE] = { decision, reason: reason.trim() }
149  else if (decision === 'stop-card') answers[MERGE_CHOICE] = { decision }
150  return { answers, pane_timeout: false }
151}
152
153function issueOf(input: unknown): number | null {
154  const issue = (input as { issue?: unknown }).issue
155  return typeof issue === 'number' && Number.isInteger(issue) && issue > 0 ? issue : null
156}
157
158function repoOf(input: unknown): string | null {
159  const repo = (input as { repo?: unknown }).repo
160  return typeof repo === 'string' && /^[\w.-]+\/[\w.-]+$/.test(repo) ? repo : null
161}
162
163// ---------------------------------------------------------------------------
164// The hooks
165// ---------------------------------------------------------------------------
166
167export function registerMergeConfirmation(on: On): void {
168  // The matcher is load-bearing: the engine refuses a second matcher-less
169  // `session.start` registration, and admission's holds that one slot.
170  on('session.start', { cwd: /^/ }, async ($, e, next) => {
171    await $.tool.register({
172      name: TOOL,
173      description:
174        "Opens saga's merge-confirmation pane for an issue: the lens grades, blocking items left at an " +
175        'early stop or the round limit, disputed declarations, consequence disagreements, the fix-later ' +
176        "list with three choices per item, degraded inputs and the cost. The operator's answers are recorded " +
177        'through scripts/review_state.py, which validates them. Returns status submitted (with the answers ' +
178        'recorded), dismissed, not-placed, unavailable, nothing-to-review, timed-out or error; on timed-out ' +
179        "the unattended rules already ran and the merge follows the run's merge setting, so do not re-ask; " +
180        'on anything but submitted or timed-out, run review_state.py render --render markdown and collect ' +
181        'the answers in the conversation.',
182      inputSchema: {
183        type: 'object',
184        properties: {
185          issue: { type: 'integer', minimum: 1, description: 'The issue number to confirm the merge for.' },
186          repo: { type: 'string', description: "owner/name; must match the review document's repository, else the call is refused." },
187        },
188        required: ['issue'],
189      },
190    })
191    return next(e)
192  })
193
194  on('tool.call', { tool: 'mcp__saga__review_merge' }, async ($, e, next) => {
195    const issue = issueOf(e)
196    if (issue === null) {
197      return { result: paneResult({ status: 'error', reason: 'issue must be a positive whole number' }) }
198    }
199    if (active !== null) {
200      return { result: paneResult({ status: 'error', reason: `a merge confirmation is already open for issue ${active}` }) }
201    }
202    const surfaces = await $.session.surfaces()
203    if (!surfaces.some((surface) => surface === 'terminal' || surface === 'desktop')) {
204      return {
205        result: paneResult({ status: 'unavailable', reason: 'no terminal or desktop surface is attached to draw the pane' }),
206      }
207    }
208
209    const cwd = await $.session.cwd()
210    const read = await readStateReviewWith((argv) => $.process.run(argv), $.plugin.root, { repoRoot: cwd, issue })
211    if (!read.ok) {
212      return { result: paneResult({ status: 'error', reason: `run_status review failed (${read.reason}): ${read.detail}` }) }
213    }
214    const data = read.review.state
215    if (data.pending_choices.length === 0) {
216      return { result: paneResult({ status: 'nothing-to-review' }) }
217    }
218    // The document owns the repository: `answers` reads the intent envelope from
219    // `gh issue view --repo`, so a model-supplied repo would select the policy
220    // behind the operator's back. A present input that differs is refused.
221    const inputRepo = (e as { repo?: unknown }).repo
222    const documentRepo = repoOf({ repo: data.repo })
223    if (inputRepo !== undefined && inputRepo !== documentRepo) {
224      return {
225        result: paneResult({
226          status: 'error',
227          reason: `the tool repo ${JSON.stringify(inputRepo)} does not match the review document's repository ${JSON.stringify(data.repo)}`,
228        }),
229      }
230    }
231    const repo = documentRepo
232    if (repo === null) {
233      return { result: paneResult({ status: 'error', reason: 'could not determine the owner/name repository for the answers call' }) }
234    }
235
236    active = issue
237    outcomes.delete(issue)
238    await update($, merge, () => ({ issue, repo, data, selections: initialSelections(data), error: null }))
239    let outcome: SagaMergeReviewOutcome | undefined
240    try {
241      const opened = await $.ui.open({ id: PANE, title: `Merge confirmation · #${issue}`, focus: true })
242      if (!opened.isPlaced) {
243        return { result: paneResult({ status: 'not-placed', reason: opened.reason }) }
244      }
245      let waited = 0
246      while (!outcomes.has(issue) && !next.signal.aborted && waited < WAIT_CAP_SECONDS) {
247        await $.process.run(['sleep', '1'])
248        waited += 1
249      }
250      outcome = outcomes.get(issue)
251      if (outcome === undefined && !next.signal.aborted) {
252        outcome = await submitTimeout($, issue, repo)
253      }
254      return { result: paneResult(outcome ?? { status: next.signal.aborted ? 'dismissed' : 'timed-out' }) }
255    } catch (err) {
256      return { result: paneResult({ status: 'error', reason: `the merge confirmation failed: ${String(err)}` }) }
257    } finally {
258      active = null
259      outcomes.delete(issue)
260      await $.ui.close({ id: PANE })
261      await update($, merge, () => null)
262    }
263  })
264
265  on('ui.close', { id: PANE }, ($, e, next) => {
266    // Every close but the tool's own after an outcome: the operator's close mark or Escape.
267    if (active !== null && !outcomes.has(active)) outcomes.set(active, { status: 'dismissed' })
268    return next(e)
269  })
270
271  on('ui.render', { component: 'Pane', requestId: PANE }, async ($, e) => {
272    const state = await read($, merge)
273    if (e.surface !== 'terminal' && e.surface !== 'desktop') {
274      const { Text } = $.ui.resolve(e)
275      return <Text>Answer the merge confirmation in the terminal or the desktop app.</Text>
276    }
277    const { Box, Button, Input, Select, Text } = $.ui.resolve(e)
278    if (state === null) {
279      return <Text dimColor>No merge confirmation is open.</Text>
280    }
281    const { data, selections } = state
282    const issue = state.issue
283    const repo = state.repo
284
285    const setFixLater = (id: string, choice: string) =>
286      update($, merge, (current) => {
287        if (!current || !(FIX_LATER_CHOICES as readonly string[]).includes(choice)) return current
288        return { ...current, selections: { ...current.selections, fix_later: { ...current.selections.fix_later, [id]: choice } }, error: null }
289      })
290    const setDecision = (decision: string) =>
291      update($, merge, (current) => {
292        if (!current || !(MERGE_DECISIONS as readonly string[]).includes(decision)) return current
293        const blocking = { ...current.selections.merge_blocking, decision }
294        return { ...current, selections: { ...current.selections, merge_blocking: blocking }, error: null }
295      })
296    const setReason = (reason: string) =>
297      update($, merge, (current) => {
298        if (!current) return current
299        const blocking = { ...current.selections.merge_blocking, reason }
300        return { ...current, selections: { ...current.selections, merge_blocking: blocking }, error: null }
301      })
302    const setError = (error: string) => update($, merge, (current) => current && { ...current, error })
303
304    const submit = async () => {
305      const current = await read($, merge)
306      if (!current || current.issue !== issue) return
307      const picks = current.selections
308      if (picks.merge_blocking.decision === 'merge-with-reason' && picks.merge_blocking.reason.trim() === '') {
309        await setError('Give a reason for the merge, or stop the card instead.')
310        return
311      }
312      const answers = buildAnswers(picks)
313      let ran
314      try {
315        ran = await $.process.run(answersArgv($.plugin.root, issue, repo ?? ''), {
316          stdin: JSON.stringify(answers),
317          timeoutMs: ANSWERS_TIMEOUT_MS,
318        })
319      } catch (err) {
320        await setError(`review_state.py did not run: ${String(err)}`)
321        return
322      }
323      if (ran.exitCode !== 0) {
324        await setError(ran.stderr.trim() || `review_state.py exited ${ran.exitCode}`)
325        return
326      }
327      outcomes.set(issue, { status: 'submitted', issue, answers, source: 'operator', summary: ran.stdout.trim() })
328      await $.ui.close({ id: PANE })
329    }
330    const dismiss = async () => {
331      outcomes.set(issue, { status: 'dismissed' })
332      await $.ui.close({ id: PANE })
333    }
334
335    const byId = new Map(data.findings.map((finding) => [finding.id, finding]))
336    const disagreements = data.consequence_disagreements
337    return (
338      <Box flexDirection="column" width={e.props.bodyColumns}>
339        <Text bold>Grades</Text>
340        {data.lenses.map((lens) => (
341          <Box key={`grade:${lens.lens}`}>
342            <Text>{gradeLabel(lens)}</Text>
343          </Box>
344        ))}
345        {mergeQuestionPending(data) && (
346          <Box flexDirection="column" marginTop={1}>
347            <Text bold>Blocking items (answer: merge-blocking)</Text>
348            {data.merge_blocking.map((id) => (
349              <Box key={`blocking:${id}`}>
350                <Text>{`  ${id} — ${byId.get(id)?.statement ?? '(no statement recorded)'}`}</Text>
351              </Box>
352            ))}
353            <Select
354              key="merge:decision"
355              label="Decision"
356              value={selections.merge_blocking.decision}
357              options={MERGE_DECISIONS.map((value) => ({ value }))}
358              onSelect={(value: string) => setDecision(value)}
359            />
360            <Input
361              key="merge:reason"
362              label="reason "
363              placeholder="why the merge is safe (required)"
364              value={selections.merge_blocking.reason}
365              onInput={setReason}
366              onSubmit={setReason}
367            />
368          </Box>
369        )}
370        {data.disputes.length > 0 && (
371          <Box flexDirection="column" marginTop={1}>
372            <Text bold>Disputed declarations</Text>
373            {data.disputes.map((id) => (
374              <Box key={`dispute:${id}`}>
375                <Text>{`  ${id}`}</Text>
376              </Box>
377            ))}
378          </Box>
379        )}
380        {disagreements.length > 0 && (
381          <Box flexDirection="column" marginTop={1}>
382            <Text bold>Consequence disagreements</Text>
383            {disagreements.map((row) => (
384              <Box key={`disagreement:${row.id}`}>
385                <Text>{`  ${row.id}: model ${row.llm}, classifier ${row.jev}`}</Text>
386              </Box>
387            ))}
388          </Box>
389        )}
390        {data.unconfirmed.length > 0 && (
391          <Box flexDirection="column" marginTop={1}>
392            <Text bold>Unconfirmed consequences</Text>
393            {data.unconfirmed.map((id) => (
394              <Box key={`unconfirmed:${id}`}>
395                <Text>{`  ${id}`}</Text>
396              </Box>
397            ))}
398          </Box>
399        )}
400        <Box flexDirection="column" marginTop={1}>
401          <Text bold>Fix later</Text>
402          {fixLaterIds(data).map((id) => (
403            <Box key={`fix:${id}`} flexDirection="column">
404              <Text>{`${id} — ${byId.get(id)?.statement ?? '(no statement recorded)'}`}</Text>
405              <Select
406                key={`fix:${id}:choice`}
407                label="Choice"
408                value={selections.fix_later[id] ?? ''}
409                options={FIX_LATER_CHOICES.map((value) => ({ value }))}
410                onSelect={(value: string) => setFixLater(id, value)}
411              />
412            </Box>
413          ))}
414        </Box>
415        {data.degraded_inputs.length > 0 && (
416          <Box flexDirection="column" marginTop={1}>
417            <Text bold>Degraded inputs</Text>
418            {data.degraded_inputs.map((input, i) => (
419              <Box key={`degraded:${i}`}>
420                <Text>{`  ${JSON.stringify(input)}`}</Text>
421              </Box>
422            ))}
423          </Box>
424        )}
425        <Box marginTop={1}>
426          <Text dimColor>{costLine(data.cost)}</Text>
427        </Box>
428        {state.error !== null && (
429          <Box key="error" marginTop={1}>
430            <Text color="red" wrap="wrap">
431              {state.error}
432            </Text>
433          </Box>
434        )}
435        <Box key="actions" flexDirection="row" columnGap={2} marginTop={1}>
436          <Button key="submit" label="Submit" hotkey="s" variant="primary" onPress={() => submit()} />
437          <Button key="dismiss" label="Dismiss" hotkey="d" role="dismiss" onPress={() => dismiss()} />
438        </Box>
439      </Box>
440    )
441  })
442}
443
444/**
445 * Submit the timeout the wait expired on: empty answers with the sentinel, so
446 * the script applies the unattended rules itself. A failed submit returns an
447 * error the skill falls back from, never a timed-out the script never saw.
448 */
449async function submitTimeout($: EngineInterface, issue: number, repo: string): Promise<SagaMergeReviewOutcome> {
450  let ran
451  try {
452    ran = await $.process.run(answersArgv($.plugin.root, issue, repo), {
453      stdin: JSON.stringify({ answers: {}, pane_timeout: true }),
454      timeoutMs: ANSWERS_TIMEOUT_MS,
455    })
456  } catch (err) {
457    return { status: 'error', reason: `the timeout submit did not run: ${String(err)}` }
458  }
459  if (ran.exitCode !== 0) {
460    return { status: 'error', reason: ran.stderr.trim() || `the timeout submit exited ${ran.exitCode}` }
461  }
462  return { status: 'timed-out' }
463}
464
mods/plan-viewer.tsx 328 lines
1// The plan viewer pane (issue #104): `/plan-view` reads a saga plan one
2// section at a time inside Claude Code.
3//
4// Read-only. The plan file is the one thing it reads besides the run view
5// `scripts/run_status.py summary` prints; the only thing it writes is the
6// operator's own prompt, when they press a file reference or "Discuss this".
7// The plain fallback on every other harness is the plan file itself, whose path
8// `run_status.py summary` prints.
9//
10// - `/plan-view` opens the run's plan; `/plan-view #N` issue N's;
11//   `/plan-view <path>` any Markdown file, relative to the session's directory.
12// - The pane lists the sections by heading; choosing one draws it as Markdown,
13//   paged under the engine's 10,000-character limit.
14// - An Edit or Write of the plan file reloads it, and so does a change of its
15//   modification time, polled every three seconds while the pane is open.
16// - When `/plan` saves a plan tick, the pane opens unasked. The engine places
17//   an unasked pane only from 144 columns; below that it is closed again and a
18//   toast names the command instead.
19// - The run status band's Plan button (issue #105) opens the run's plan here:
20//   this module answers the press on `band-plan-<issue>` itself, because a
21//   plugin's own `$.command.run` never reaches its own command hook.
22
23import { atom, read, update } from 'claude-code'
24import type { EngineInterface, On } from 'claude-code'
25
26import type { SagaPlanView } from '../types/index.d.ts'
27import {
28  discussPromptText,
29  fitTables,
30  linkifyRefs,
31  pageText,
32  planSavePath,
33  refFromHref,
34  refPromptText,
35  splitSections,
36} from './plan-sections.ts'
37import type { PlanRef } from './plan-sections.ts'
38import { readRunStatusWith } from './run-record.ts'
39
40/** The pane's id, and the `requestId` its `ui.render` hook matches. */
41export const PLAN_PANE = 'saga-plan'
42
43/** The run status band's Plan button, `band-plan-<issue>` (issue #105). */
44export const BAND_PLAN_ELEMENT = /^band-plan-\d+$/
45
46/** How often an open pane checks the plan file's modification time. */
47export const PLAN_POLL_MS = 3_000
48
49/** The most reference Buttons drawn under a page; the links in the page are all pressable. */
50const MAX_REF_BUTTONS = 20
51
52const planView = atom({ plugin: 'saga', key: 'planView' } as const, null)
53const planSelected = atom({ plugin: 'saga', key: 'planSelected' } as const, -1)
54const planPage = atom({ plugin: 'saga', key: 'planPage' } as const, 0)
55
56type Loaded = { ok: true; view: SagaPlanView } | { ok: false; detail: string }
57
58/** `path` taken from `cwd` unless it is absolute. */
59export function absolutePath(cwd: string, path: string): string {
60  return path.startsWith('/') ? path : `${cwd.replace(/\/+$/, '')}/${path.replace(/^\.\//, '')}`
61}
62
63function errorText(err: unknown): string {
64  return err instanceof Error ? err.message : String(err)
65}
66
67/** Read and split the plan at `file`; a failure is a reason, never a throw. */
68async function loadPlan($: EngineInterface, path: string, file: string, repoRoot: string): Promise<Loaded> {
69  try {
70    const stat = await $.fs.stat(file, { resolve: true })
71    if (stat.kind !== 'file') return { ok: false, detail: `${path} is not a file` }
72    const absPath = stat.realPath ?? file
73    const text = await $.fs.read(absPath)
74    return { ok: true, view: { path, absPath, repoRoot, mtimeMs: stat.mtimeMs, sections: splitSections(text) } }
75  } catch (err) {
76    return { ok: false, detail: `could not read ${path}: ${errorText(err)}` }
77  }
78}
79
80/** Show `view` from its section list. */
81async function showPlan($: EngineInterface, view: SagaPlanView): Promise<void> {
82  await update($, planView, () => view)
83  await update($, planSelected, () => -1)
84  await update($, planPage, () => 0)
85}
86
87/** Read the shown plan again, keeping the reader on the section they were reading. */
88async function reloadPlan($: EngineInterface, view: SagaPlanView): Promise<void> {
89  const loaded = await loadPlan($, view.path, view.absPath, view.repoRoot)
90  // A file caught mid-write or deleted keeps its last good version on screen.
91  if (!loaded.ok) return
92  const selected = await read($, planSelected)
93  const was = view.sections[selected]
94  const sections = loaded.view.sections
95  let now = -1
96  if (was !== undefined) {
97    const same = sections.findIndex((one) => one.level === was.level && one.title === was.title)
98    now = same >= 0 ? same : Math.min(selected, sections.length - 1)
99  }
100  await update($, planView, () => loaded.view)
101  if (now !== selected) {
102    await update($, planSelected, () => now)
103    await update($, planPage, () => 0)
104  }
105}
106
107/** Put text in the operator's prompt, saying so when the prompt cannot take it. */
108async function fillPrompt($: EngineInterface, text: string): Promise<void> {
109  const filled = await $.prompt.fill({ text, mode: 'insert' })
110  if (!filled.isFilled) $.ui.toast('plan-view: the prompt cannot take text right now')
111}
112
113/**
114 * Open the pane on `args` as `/plan-view` takes them: nothing, `#N` or a path.
115 * Says what it did; `isOpened` is false when there was nothing to open.
116 */
117async function openPlanView($: EngineInterface, args: string): Promise<{ text: string; isOpened: boolean }> {
118  const cwd = await $.session.cwd()
119  const target = args.trim()
120  let path: string
121  let file: string
122  let repoRoot = cwd
123
124  const issueArg = /^#?(\d+)$/.exec(target)
125  if (target === '' || issueArg !== null) {
126    const issue = issueArg === null ? undefined : Number(issueArg[1])
127    const ran = await readRunStatusWith((argv) => $.process.run(argv), $.plugin.root, { repoRoot: cwd, issue })
128    if (!ran.ok) return { text: `plan-view: could not read the saga run (${ran.reason}): ${ran.detail}`, isOpened: false }
129    const run = ran.view.runs[0]
130    if (run === undefined) {
131      const which = issue === undefined ? 'for this checkout' : `for #${issue} in this checkout`
132      return { text: `plan-view: no saga run ${which}. Name the plan instead: /plan-view docs/plans/<file>.md`, isOpened: false }
133    }
134    if (run.plan_file === null || run.plan_path === null) {
135      return { text: `plan-view: #${run.issue} has no plan recorded yet. Name one: /plan-view docs/plans/<file>.md`, isOpened: false }
136    }
137    path = run.plan_path
138    file = run.plan_file
139    repoRoot = ran.view.repo_root
140  } else {
141    path = target
142    file = absolutePath(cwd, target)
143  }
144
145  const loaded = await loadPlan($, path, file, repoRoot)
146  if (!loaded.ok) return { text: `plan-view: ${loaded.detail}`, isOpened: false }
147  await showPlan($, loaded.view)
148  await $.ui.open({ id: PLAN_PANE, title: `Plan · ${path.split('/').pop()}` })
149  const count = loaded.view.sections.length
150  return { text: `plan-view: ${path}, ${count} section${count === 1 ? '' : 's'}.`, isOpened: true }
151}
152
153export function registerPlanViewer(on: On): void {
154  on('session.start', { cwd: /^/ }, async ($, e, next) => {
155    await $.command.register({
156      name: 'plan-view',
157      description: 'Read the saga plan one section at a time in a pane',
158      argumentHint: '[path | #issue]',
159      immediate: true,
160    })
161
162    // Catches what no tool call shows: an edit from another session, an editor, git.
163    async function poll(): Promise<void> {
164      const view = await read($, planView)
165      if (view === null) return
166      const panes = await $.ui.panes()
167      if (!panes.some((pane) => pane.id === PLAN_PANE)) return
168      try {
169        const stat = await $.fs.stat(view.absPath)
170        if (stat.mtimeMs === view.mtimeMs) return
171      } catch {
172        return
173      }
174      await reloadPlan($, view)
175    }
176    $.clock.every(PLAN_POLL_MS, () => {
177      void poll()
178    })
179
180    return next(e)
181  })
182
183  on('command.run', { command: 'plan-view' }, async ($, e) => {
184    const opened = await openPlanView($, e.args)
185    return { text: opened.text }
186  })
187
188  // The run status band's Plan button (issue #105): a press on `band-plan-N` opens #N's plan.
189  on('ui.press', { plugin: 'saga', element: BAND_PLAN_ELEMENT }, async ($, e) => {
190    const issue = /(\d+)$/.exec(e.element)?.[1] ?? ''
191    const opened = await openPlanView($, `#${issue}`)
192    if (!opened.isOpened) $.ui.toast(opened.text)
193    return { element: e.element }
194  })
195
196  // An edit of the plan file reloads the pane.
197  on('tool.call', { tool: 'Edit' }, async ($, e, next) => {
198    const ran = await next(e)
199    if (ran.deny !== undefined || ran.isError === true) return ran
200    const view = await read($, planView)
201    if (view === null) return ran
202    try {
203      const stat = await $.fs.stat(e.file_path, { resolve: true })
204      if (stat.realPath === view.absPath) await reloadPlan($, view)
205    } catch {
206      // The poll catches what this misses.
207    }
208    return ran
209  })
210
211  on('tool.call', { tool: 'Write' }, async ($, e, next) => {
212    const ran = await next(e)
213    if (ran.deny !== undefined || ran.isError === true) return ran
214    const view = await read($, planView)
215    if (view === null) return ran
216    try {
217      const stat = await $.fs.stat(e.file_path, { resolve: true })
218      if (stat.realPath === view.absPath) await reloadPlan($, view)
219    } catch {
220      // The poll catches what this misses.
221    }
222    return ran
223  })
224
225  // `/plan` saving a plan tick opens the pane unasked, where there is room.
226  on('tool.call', { tool: 'Bash' }, async ($, e, next) => {
227    const ran = await next(e)
228    if (ran.deny !== undefined || ran.isError === true) return ran
229    const saved = planSavePath(e.command)
230    if (saved === null) return ran
231    const cwd = await $.session.cwd()
232    const loaded = await loadPlan($, saved, absolutePath(cwd, saved), cwd)
233    if (!loaded.ok) return ran
234    await showPlan($, loaded.view)
235    const opened = await $.ui.open({ id: PLAN_PANE, title: `Plan · ${saved.split('/').pop()}` })
236    if (!opened.isPlaced) {
237      // A pane that appears later, when the terminal widens, would be a surprise.
238      await $.ui.close({ id: PLAN_PANE })
239      $.ui.toast('Plan saved: run /plan-view to read it here')
240    }
241    return ran
242  })
243
244  on('ui.render', { component: 'Pane', requestId: PLAN_PANE }, async ($, e) => {
245    const { Box, Button, Markdown, Text } = $.ui.resolve(e)
246    const view = await read($, planView)
247    if (view === null) {
248      return (
249        <Box flexDirection="column">
250          <Text dimColor>No plan loaded. Run /plan-view, /plan-view #issue or /plan-view path.</Text>
251        </Box>
252      )
253    }
254
255    const selected = await read($, planSelected)
256    const section = view.sections[selected]
257    if (section === undefined) {
258      const count = view.sections.length
259      return (
260        <Box flexDirection="column">
261          <Text dimColor>
262            {view.path} · {count} section{count === 1 ? '' : 's'}
263          </Text>
264          {count === 0 && <Text>This plan has no headings.</Text>}
265          {view.sections.map((one, i) => (
266            <Button
267              key={`sec-${i}`}
268              label={`${'  '.repeat(Math.max(0, one.level - 1))}${one.title}`}
269              plain
270              onPress={async () => {
271                await update($, planSelected, () => i)
272                await update($, planPage, () => 0)
273              }}
274            />
275          ))}
276        </Box>
277      )
278    }
279
280    const pages = pageText(section.text)
281    const page = Math.min(Math.max(0, await read($, planPage)), pages.length - 1)
282    const linked = linkifyRefs(pages[page] ?? '', view.repoRoot)
283    // Link first, then measure: a file reference drawn without OSC 8 is wider than its label.
284    // bodyColumns is the pane. viewport.columns is the transcript beside a docked pane.
285    const text = e.surface === 'terminal' ? fitTables(linked.text, e.props.bodyColumns) : linked.text
286    const { refs } = linked
287    const pressRef = async (ref: PlanRef | null) => {
288      if (ref !== null) await fillPrompt($, refPromptText(ref))
289    }
290
291    return (
292      <Box flexDirection="column">
293        <Box flexDirection="row" gap={1}>
294          <Button key="back" label="Sections" onPress={() => update($, planSelected, () => -1)} />
295          {page > 0 && <Button key="page-prev" label="Previous page" onPress={() => update($, planPage, () => page - 1)} />}
296          {page < pages.length - 1 && (
297            <Button key="page-next" label="Next page" onPress={() => update($, planPage, () => page + 1)} />
298          )}
299          <Button
300            key="discuss"
301            label="Discuss this"
302            variant="primary"
303            onPress={() => fillPrompt($, discussPromptText(view.path, section, page, pages.length))}
304          />
305        </Box>
306        <Text dimColor>
307          {section.title} · page {page + 1} of {pages.length}
308        </Text>
309        {refs.length > 0 ? (
310          <Markdown
311            key="section"
312            text={text}
313            pressableLinks={refs.map((ref) => ref.href)}
314            onLinkPress={(link) => {
315              void pressRef(refFromHref(link.href, view.repoRoot, refs))
316            }}
317          />
318        ) : (
319          <Markdown key="section" text={text} />
320        )}
321        {refs.slice(0, MAX_REF_BUTTONS).map((ref, k) => (
322          <Button key={`ref-${k}`} label={ref.label} plain dimColor onPress={() => pressRef(ref)} />
323        ))}
324      </Box>
325    )
326  })
327}
328
mods/review-pane.tsx 343 lines
1// The review findings pane (issue #108, live in issue #165): `/review-view`
2// shows the run's latest code review inside Claude Code.
3//
4// Read-only. It reads the review through `scripts/run_status.py review`. For a
5// record with C1 review runs the view embeds the whole `review_state.v1`
6// document, and the pane draws that alone: grades, where-to-look states,
7// tools, round and cost, and per-round deltas. For an older record it draws
8// today's lens list from the latest `review_result.v2` entry. The pane shows
9// those answers and never recomputes them. The only thing it writes is the
10// operator's own prompt, when they press a finding's Quote. No acceptance
11// decision and no finding edits happen here. The plain fallback on every other
12// harness is `run_status.py review`'s table.
13//
14// - `/review-view` opens the checkout's run; `/review-view #N` issue N's.
15// - A state review shows each lens's grade with its blocking and fix-later
16//   counts. An old-shape lens row reads met, not met, not run or unscored,
17//   with its finding count and its top findings; a lens that did not run, or
18//   has no recorded result, says so and never shows a number that could read
19//   as a low score.
20// - Choosing a lens lists its findings; Quote puts one in the prompt.
21// - A Bash call that ran a script which writes review runs reloads the open
22//   pane, and so does a change of the run record's modification time, polled
23//   every five seconds while the pane is open.
24// - The run status band's Review button (issue #105) opens this pane: this
25//   module answers the press on `band-review-<issue>` itself, because a
26//   plugin's own `$.command.run` never reaches its own command hook.
27
28import { atom, read, update } from 'claude-code'
29import type { EngineInterface, On } from 'claude-code'
30
31import type { SagaReviewView } from '../types/index.d.ts'
32import { fitTables } from './plan-sections.ts'
33import {
34  costLine,
35  findingHeading,
36  findingLine,
37  findingsFor,
38  findingText,
39  gradeLabel,
40  lensLabel,
41  OTHER_FINDINGS,
42  quoteText,
43  reviewHeading,
44  roundLabel,
45  stateFindingHeading,
46  stateFindingText,
47  stateHeading,
48  stateLensFindings,
49  stateQuoteText,
50  whereToLookLabel,
51} from './review-findings.ts'
52import { isStateReview, readEitherReviewWith } from './run-record.ts'
53import type { ReviewViewQuery } from './run-record.ts'
54
55/** The pane's id, and the `requestId` its `ui.render` hook matches. */
56export const REVIEW_PANE = 'saga-review'
57
58/** The run status band's Review button, `band-review-<issue>` (issue #105). */
59export const BAND_REVIEW_ELEMENT = /^band-review-\d+$/
60
61/** How often an open pane checks the run record's modification time. */
62export const REVIEW_POLL_MS = 5_000
63
64/** A Bash command that ran a script which writes review runs or answers. */
65const WRITES_REVIEW = /\b(review_result|review_command|review_state)\.py\b/
66
67const reviewView = atom({ plugin: 'saga', key: 'reviewView' } as const, null)
68const reviewLens = atom({ plugin: 'saga', key: 'reviewLens' } as const, null)
69const reviewMtimeMs = atom({ plugin: 'saga', key: 'reviewMtimeMs' } as const, 0)
70
71/** The run record's modification time, or 0 when it cannot be read. */
72async function recordMtime($: EngineInterface, view: SagaReviewView): Promise<number> {
73  if (view.record_path === null) return 0
74  try {
75    return (await $.fs.stat(view.record_path)).mtimeMs
76  } catch {
77    return 0
78  }
79}
80
81/** Read the review again for the shown issue, keeping the reader on their lens when it is still there. */
82async function reloadReview($: EngineInterface, shown: SagaReviewView): Promise<void> {
83  const query: ReviewViewQuery = { repoRoot: shown.repo_root }
84  if (shown.issue !== null) query.issue = shown.issue
85  const ran = await readEitherReviewWith((argv) => $.process.run(argv), $.plugin.root, query)
86  // A record caught mid-write keeps its last good review on screen.
87  if (!ran.ok || ran.view.review === null) return
88  const view = ran.view
89  await update($, reviewView, () => view)
90  const mtime = await recordMtime($, view)
91  await update($, reviewMtimeMs, () => mtime)
92  const lens = await read($, reviewLens)
93  const review = view.review
94  if (lens !== null && review !== null && lens !== OTHER_FINDINGS && !review.lenses.some((one) => one.lens === lens)) {
95    await update($, reviewLens, () => null)
96  }
97}
98
99async function isOpen($: EngineInterface): Promise<boolean> {
100  const panes = await $.ui.panes()
101  return panes.some((pane) => pane.id === REVIEW_PANE)
102}
103
104/** Put text in the operator's prompt, saying so when the prompt cannot take it. */
105async function fillPrompt($: EngineInterface, text: string): Promise<void> {
106  const filled = await $.prompt.fill({ text, mode: 'insert' })
107  if (!filled.isFilled) $.ui.toast('review-view: the prompt cannot take text right now')
108}
109
110/**
111 * Open the pane on `args` as `/review-view` takes them: nothing or `#N`.
112 * Says what it did; `isOpened` is false when there was nothing to open.
113 */
114async function openReviewView($: EngineInterface, args: string): Promise<{ text: string; isOpened: boolean }> {
115  const cwd = await $.session.cwd()
116  const target = args.trim()
117  const issueArg = /^#?(\d+)$/.exec(target)
118  if (target !== '' && issueArg === null) return { text: 'review-view: name an issue, as in /review-view #108', isOpened: false }
119  const query: ReviewViewQuery = { repoRoot: cwd }
120  if (issueArg !== null) query.issue = Number(issueArg[1])
121
122  const ran = await readEitherReviewWith((argv) => $.process.run(argv), $.plugin.root, query)
123  if (!ran.ok) return { text: `review-view: could not read the review (${ran.reason}): ${ran.detail}`, isOpened: false }
124  const view = ran.view
125  const review = view.review
126  if (view.issue === null) return { text: 'review-view: no saga run for this checkout. Name one: /review-view #N', isOpened: false }
127  if (review === null) return { text: `review-view: no review result recorded for #${view.issue}.`, isOpened: false }
128
129  await update($, reviewView, () => view)
130  await update($, reviewLens, () => null)
131  const mtime = await recordMtime($, view)
132  await update($, reviewMtimeMs, () => mtime)
133  if (isStateReview(review)) {
134    await $.ui.open({ id: REVIEW_PANE, title: `Review · #${view.issue} round ${review.round}` })
135    const blocking = review.lenses.reduce((sum, lens) => sum + lens.blocking, 0)
136    const fixLater = review.lenses.reduce((sum, lens) => sum + lens.fix_later, 0)
137    return {
138      text: `review-view: #${view.issue} round ${review.round}, ${review.outcome}, ${review.lenses.length} lenses, ${blocking} blocking, ${fixLater} fix later.`,
139      isOpened: true,
140    }
141  }
142  await $.ui.open({ id: REVIEW_PANE, title: `Review · #${view.issue} cycle ${review.cycle}` })
143  const findings = review.lenses.reduce((sum, lens) => sum + lens.finding_count, 0) + review.unattributed_findings.length
144  return {
145    text: `review-view: #${view.issue} cycle ${review.cycle}, ${review.outcome}, ${review.lenses.length} lenses, ${findings} findings.`,
146    isOpened: true,
147  }
148}
149
150export function registerReviewPane(on: On): void {
151  on('session.start', { cwd: /^/ }, async ($, e, next) => {
152    await $.command.register({
153      name: 'review-view',
154      description: 'Show the latest saga code review, lens by lens, in a pane',
155      argumentHint: '[#issue]',
156      immediate: true,
157    })
158
159    // Catches a review written by another session: a stat, never a read of the record.
160    async function poll(): Promise<void> {
161      const view = await read($, reviewView)
162      if (view === null || view.record_path === null) return
163      if (!(await isOpen($))) return
164      const mtime = await recordMtime($, view)
165      if (mtime === 0 || mtime === (await read($, reviewMtimeMs))) return
166      await reloadReview($, view)
167    }
168    $.clock.every(REVIEW_POLL_MS, () => {
169      void poll()
170    })
171
172    return next(e)
173  })
174
175  on('command.run', { command: 'review-view' }, async ($, e) => {
176    const opened = await openReviewView($, e.args)
177    return { text: opened.text }
178  })
179
180  // The run status band's Review button (issue #105): a press on `band-review-N` opens #N's review.
181  on('ui.press', { plugin: 'saga', element: BAND_REVIEW_ELEMENT }, async ($, e) => {
182    const issue = /(\d+)$/.exec(e.element)?.[1] ?? ''
183    const opened = await openReviewView($, `#${issue}`)
184    if (!opened.isOpened) $.ui.toast(opened.text)
185    return { element: e.element }
186  })
187
188  // `review_result.py` writing a result reloads the open pane at once.
189  on('tool.call', { tool: 'Bash' }, async ($, e, next) => {
190    const ran = await next(e)
191    if (ran.deny !== undefined || ran.isError === true) return ran
192    if (!WRITES_REVIEW.test(e.command)) return ran
193    const view = await read($, reviewView)
194    if (view === null || !(await isOpen($))) return ran
195    await reloadReview($, view)
196    return ran
197  })
198
199  on('ui.render', { component: 'Pane', requestId: REVIEW_PANE }, async ($, e) => {
200    const { Box, Button, Markdown, Text } = $.ui.resolve(e)
201    const view = await read($, reviewView)
202    const review = view?.review ?? null
203    if (view === null || review === null) {
204      return (
205        <Box flexDirection="column">
206          <Text dimColor>No review loaded. Run /review-view or /review-view #issue.</Text>
207        </Box>
208      )
209    }
210
211    const lens = await read($, reviewLens)
212    if (isStateReview(review)) {
213      const state = review.state
214      if (lens === null) {
215        const others = stateLensFindings(state, OTHER_FINDINGS)
216        const ran = Object.entries(state.tools.ran)
217        return (
218          <Box flexDirection="column">
219            <Text dimColor>{stateHeading(view.issue, review)}</Text>
220            <Text bold>Grades</Text>
221            {state.lenses.map((one) => (
222              <Box key={`row-${one.lens}`} flexDirection="column">
223                <Button key={`lens-${one.lens}`} label={gradeLabel(one)} plain onPress={() => update($, reviewLens, () => one.lens)} />
224              </Box>
225            ))}
226            {others.length > 0 && (
227              <Button
228                key="lens-other"
229                label={`${OTHER_FINDINGS}  ${others.length} finding${others.length === 1 ? '' : 's'}`}
230                plain
231                onPress={() => update($, reviewLens, () => OTHER_FINDINGS)}
232              />
233            )}
234            <Text bold>Where to look</Text>
235            {state.where_to_look.length === 0 && <Text dimColor>None.</Text>}
236            {state.where_to_look.map((item, i) => (
237              <Box key={`wtl-${i}`}>
238                <Text dimColor>{whereToLookLabel(item)}</Text>
239              </Box>
240            ))}
241            <Text bold>Tools</Text>
242            {ran.map(([name, version]) => (
243              <Box key={`tool-${name}`}>
244                <Text dimColor>{`${name} ${version}`}</Text>
245              </Box>
246            ))}
247            {state.tools.missing_notice !== null && <Text>{state.tools.missing_notice}</Text>}
248            {state.tools.missing_notice === null && state.tools.missing_tools.length > 0 && (
249              <Text dimColor>{`missing: ${state.tools.missing_tools.join(', ')}`}</Text>
250            )}
251            <Text bold>{`Round ${state.round}`}</Text>
252            <Text dimColor>{costLine(state.cost)}</Text>
253            {state.rounds.map((round) => (
254              <Box key={`round-${round.round}`} flexDirection="column">
255                <Text>{roundLabel(round)}</Text>
256                {round.new_blocking.map((id) => (
257                  <Box key={`new-${id}`}>
258                    <Text dimColor>{`  + ${id}`}</Text>
259                  </Box>
260                ))}
261                {round.cleared_blocking.map((id) => (
262                  <Box key={`cleared-${id}`}>
263                    <Text dimColor>{`  - ${id}`}</Text>
264                  </Box>
265                ))}
266                {round.new_blocking.length === 0 && round.cleared_blocking.length === 0 && (
267                  <Text dimColor>  nothing added or cleared</Text>
268                )}
269              </Box>
270            ))}
271          </Box>
272        )
273      }
274      const stateFindings = stateLensFindings(state, lens)
275      const shownGrade = state.lenses.find((one) => one.lens === lens)
276      return (
277        <Box flexDirection="column">
278          <Button key="back" label="Grades" onPress={() => update($, reviewLens, () => null)} />
279          <Text bold>{shownGrade === undefined ? lens : gradeLabel(shownGrade)}</Text>
280          {stateFindings.length === 0 && <Text dimColor>No findings.</Text>}
281          {stateFindings.map((finding, i) => (
282            <Box key={`finding-${i}`} flexDirection="column">
283              <Text key={`heading-${i}`}>{stateFindingHeading(finding)}</Text>
284              <Markdown
285                key={`text-${i}`}
286                text={e.surface === 'terminal' ? fitTables(stateFindingText(finding), e.props.bodyColumns) : stateFindingText(finding)}
287              />
288              <Button key={`quote-${i}`} label="Quote" onPress={() => fillPrompt($, stateQuoteText(finding))} />
289            </Box>
290          ))}
291        </Box>
292      )
293    }
294    if (lens === null) {
295      const others = review.unattributed_findings
296      return (
297        <Box flexDirection="column">
298          <Text dimColor>{reviewHeading(view.issue, review)}</Text>
299          {review.lenses.length === 0 && <Text>This review has no lens results.</Text>}
300          {review.lenses.map((one) => (
301            <Box key={`row-${one.lens}`} flexDirection="column">
302              <Button key={`lens-${one.lens}`} label={lensLabel(one)} plain onPress={() => update($, reviewLens, () => one.lens)} />
303              {one.top.map((finding, i) => (
304                <Text key={`top-${one.lens}-${i}`} dimColor>
305                  {`  ${findingLine(finding)}`}
306                </Text>
307              ))}
308            </Box>
309          ))}
310          {others.length > 0 && (
311            <Button
312              key="lens-other"
313              label={`${OTHER_FINDINGS}  ${others.length} finding${others.length === 1 ? '' : 's'}`}
314              plain
315              onPress={() => update($, reviewLens, () => OTHER_FINDINGS)}
316            />
317          )}
318        </Box>
319      )
320    }
321
322    const findings = findingsFor(review, lens)
323    const shown = review.lenses.find((one) => one.lens === lens)
324    return (
325      <Box flexDirection="column">
326        <Button key="back" label="Lenses" onPress={() => update($, reviewLens, () => null)} />
327        <Text bold>{shown === undefined ? lens : lensLabel(shown)}</Text>
328        {findings.length === 0 && <Text dimColor>No findings.</Text>}
329        {findings.map((finding, i) => (
330          <Box key={`finding-${i}`} flexDirection="column">
331            <Text key={`heading-${i}`}>{findingHeading(finding)}</Text>
332            <Markdown
333              key={`text-${i}`}
334              text={e.surface === 'terminal' ? fitTables(findingText(finding), e.props.bodyColumns) : findingText(finding)}
335            />
336            <Button key={`quote-${i}`} label="Quote" onPress={() => fillPrompt($, quoteText(lens, finding))} />
337          </Box>
338        ))}
339      </Box>
340    )
341  })
342}
343
mods/setup-pane.tsx 659 lines
1// The setup pane (issue #165): C3's survey rows with a checkbox each, an
2// Install button that runs the ticked tools, the survey's optional steps with
3// a Run button each, and the first-session offer.
4//
5// The model calls the tool `mcp__saga__setup` with the checkout. The tool runs
6// `saga_setup.py survey --format json` and draws the pane from that document
7// alone: each tool's id, status, lens and pinned version with a checkbox,
8// pre-ticked when the status is not installed. A row with no install command
9// has no checkbox and shows the script's install sentence instead. Install
10// runs one `install --tools <id>` call per ticked tool, in pane order,
11// showing each row's progress, and stops at the first failure; a step runs
12// one `step --name <name>` call from the saga plugin root with its own progress.
13// Done returns the installed and failed lists to the model. Dismissed,
14// timed-out and the error paths match the merge pane: closing records
15// nothing, and a timeout returns timed-out with whatever already installed
16// left installed. Every answer leaves as a JSON string (issue #218): the
17// session rejects an object result.
18//
19// The offer: when C3's machine record shows setup has never run — and neither
20// the machine record nor the mod's `$.store` remembers the offer — the first
21// session start toasts `Run /saga:setup to check this machine's tools.`, then
22// records the offer in the machine record through `record-offer` and in the
23// store, machine first. The offer check never runs `survey`: the survey
24// persists the machine record with `ran` set, so checking with it would mark
25// setup run. Any store failure suppresses the offer path without throwing, so
26// a suite or build without the store stays silent and green.
27//
28// Waiting for a person: the tool waits on `$.process.run(['sleep', '1'])`, one
29// second at a time, up to WAIT_CAP_SECONDS (LEARNINGS.md, 2026-10-04).
30//
31// The command path (issue #221): `/setup-view`, registered beside the tool,
32// opens this same pane with no tool call and no agent turn. One shared open
33// helper reads the survey and probes the checkout for both openers; the pane
34// state remembers which opener drew it (`openedBy`). The command hook answers
35// `{text}` at once and never waits; closing a command-opened pane clears the
36// state and leaves no pending tool call, while a tool-opened close still sets
37// `dismissed` for the waiting call. After an Install run or a step settles,
38// the shared runners re-survey so done states are current, and a failed row
39// offers Ask Claude through the session prompt.
40
41import { atom, read, update } from 'claude-code'
42import type { EngineInterface, On } from 'claude-code'
43import type { SagaSetupOutcome, SagaSetupStep, SagaSetupSurvey, SagaSetupToolRow } from '../types/index.d.ts'
44import { paneResult } from './pane-result.ts'
45import {
46  readOfferStatusWith,
47  readSetupSurveyWith,
48  setupInstallArgv,
49  setupRecordOfferArgv,
50  setupScriptExistsWith,
51  setupStepArgv,
52} from './run-record.ts'
53
54/** The pane's id. */
55export const PANE = 'saga-setup'
56
57/** The tool's short name; the model calls it as `mcp__saga__setup`. */
58export const TOOL = 'setup'
59
60/** The setup command's name; the operator runs it as `/setup-view` (issue #221). */
61export const COMMAND = 'setup-view'
62
63/** The survey a command-opened pane carries when the survey failed: drawn never, read never. */
64const EMPTY_SURVEY: SagaSetupSurvey = { schema: 'setup_survey.v1', tools: [], steps: [] }
65
66/** How long the tool waits for the operator before it returns timed-out: thirty minutes. */
67export const WAIT_CAP_SECONDS = 1800
68
69/** How long one tool install may take. */
70const INSTALL_TIMEOUT_MS = 300_000
71
72/** How long one step run may take: past the script's own 30-second child timeout. */
73const STEP_TIMEOUT_MS = 60_000
74
75/** The `$.store` key remembering the setup offer. */
76export const OFFER_STORE_KEY = 'setup_offered'
77
78/** The offer toasted once, on the first session of a machine setup never ran on. */
79export const OFFER_TEXT = "Run /saga:setup to check this machine's tools."
80
81const setup = atom({ plugin: 'saga', key: 'setupReview' } as const, null)
82
83/**
84 * The outcome of the setup in flight. A module value, not `$.state`: the
85 * waiting hook reads it between its sleeps, and a state read inside one
86 * dispatch sees one frozen moment. A hot reload drops it with the pane.
87 */
88let outcome: SagaSetupOutcome | undefined
89
90/** Whether a setup pane is open. One setup at a time. */
91let active = false
92
93/**
94 * Which opener drew the active setup, set beside `active` so the close handler
95 * reads it with no await. The pane state carries the same answer for the render.
96 */
97let activeOpener: 'tool' | 'command' | null = null
98
99/**
100 * The Install run's epoch. Each Install bumps it and orphans any older loop,
101 * so a loop outlived by its pane can never mark another setup's rows.
102 */
103let installEpoch = 0
104
105/**
106 * The step runs' epoch, Install's for steps: bumped when the tool call ends,
107 * so a run outlived by its pane can never mark another setup's steps.
108 */
109let stepEpoch = 0
110
111// ---------------------------------------------------------------------------
112// Pure helpers: no `$`, so a test calls them directly
113// ---------------------------------------------------------------------------
114
115/** The ticks the pane opens with: every installable row whose status is not installed. */
116export function initialTicks(survey: SagaSetupSurvey): Record<string, boolean> {
117  const ticked: Record<string, boolean> = {}
118  for (const row of survey.tools) {
119    ticked[row.id] = row.has_install && row.status !== 'installed'
120  }
121  return ticked
122}
123
124/** The ticked installable ids, in survey order: exactly what Install passes, one argv each. */
125export function tickedIds(survey: SagaSetupSurvey, ticked: Record<string, boolean>): string[] {
126  return survey.tools.filter((row) => row.has_install && ticked[row.id] === true).map((row) => row.id)
127}
128
129/** One row's checkbox label: the tick box, id, status and lens. */
130export function rowLabel(row: SagaSetupToolRow, ticked: boolean): string {
131  return `${ticked ? '[x]' : '[ ]'} ${row.id}  ${row.status}  ${row.lens}`
132}
133
134/** The survey's optional steps: a missing or non-array field reads as none. */
135export function surveySteps(survey: SagaSetupSurvey): SagaSetupStep[] {
136  return Array.isArray(survey.steps) ? survey.steps : []
137}
138
139/** The last lines of a row's install output that the pane shows. */
140export function shownOutput(output: string): string {
141  return output.length > 2000 ? `\u2026${output.slice(-2000)}` : output
142}
143
144// ---------------------------------------------------------------------------
145// Shared open: one survey read and checkout probe for both openers (issue #221)
146// ---------------------------------------------------------------------------
147
148/** What the shared open read: the survey and the step checkout, or why there is no pane. */
149type SetupOpen =
150  | { ok: true; survey: SagaSetupSurvey; checkout: string | null }
151  | { ok: false; reason: string; empty: boolean }
152
153/**
154 * Read the survey and probe the step checkout for `repo`. Both the tool call
155 * and the setup-view command open through this alone, so the survey
156 * implementation is one function and its failure words are one string.
157 */
158async function prepareSetup($: EngineInterface, repo: string): Promise<SetupOpen> {
159  const read = await readSetupSurveyWith((argv) => $.process.run(argv), $.plugin.root, repo)
160  if (!read.ok) {
161    return { ok: false, reason: `saga_setup survey failed (${read.reason}): ${read.detail}`, empty: false }
162  }
163  if (read.survey.tools.length === 0 && surveySteps(read.survey).length === 0) {
164    return { ok: false, reason: 'the survey lists no tools and no steps', empty: true }
165  }
166  const runnable = await setupScriptExistsWith((path) => $.fs.exists(path), $.plugin.root)
167  return { ok: true, survey: read.survey, checkout: runnable ? $.plugin.root : null }
168}
169
170/**
171 * Clear a command-opened setup: no call waits on it, so `active`, the epochs
172 * and the state go now. The tool path never calls this; its `finally` owns
173 * that cleanup.
174 */
175async function clearCommandSetup($: EngineInterface): Promise<void> {
176  active = false
177  activeOpener = null
178  installEpoch += 1
179  stepEpoch += 1
180  await update($, setup, () => null)
181}
182
183// ---------------------------------------------------------------------------
184// The hooks
185// ---------------------------------------------------------------------------
186
187export function registerSetupPane(on: On): void {
188  // The matcher is load-bearing: the engine refuses a second matcher-less
189  // `session.start` registration, and admission's holds that one slot.
190  on('session.start', { cwd: /^/ }, async ($, e, next) => {
191    await $.tool.register({
192      name: TOOL,
193      description:
194        "Opens saga's setup pane for a checkout: C3's surveyed tool rows with a checkbox each, and an " +
195        'Install button that runs the ticked tools through scripts/saga_setup.py and shows live progress. ' +
196        'Returns status submitted (with the installed and failed ids), dismissed, not-placed, unavailable, ' +
197        'nothing-to-review, timed-out or error; on anything but submitted, print the survey table and ask ' +
198        'which tools to install instead.',
199      inputSchema: {
200        type: 'object',
201        properties: {
202          repo: { type: 'string', description: 'The checkout to set up; defaults to the session directory.' },
203        },
204      },
205    })
206    await $.command.register({
207      name: COMMAND,
208      description: "Open the setup pane: survey this checkout's tools, install them, and run the optional steps",
209      immediate: true,
210    })
211    const out = await next(e)
212    await maybeOfferSetup($)
213    return out
214  })
215
216  on('tool.call', { tool: 'mcp__saga__setup' }, async ($, e, next) => {
217    if (active) {
218      return { result: paneResult({ status: 'error', reason: 'a setup pane is already open' }) }
219    }
220    const surfaces = await $.session.surfaces()
221    if (!surfaces.some((surface) => surface === 'terminal' || surface === 'desktop')) {
222      return {
223        result: paneResult({ status: 'unavailable', reason: 'no terminal or desktop surface is attached to draw the pane' }),
224      }
225    }
226    const cwd = await $.session.cwd()
227    const input = (e as { repo?: unknown }).repo
228    const repo = typeof input === 'string' && input !== '' ? input : cwd
229
230    const prepared = await prepareSetup($, repo)
231    if (!prepared.ok && prepared.empty) {
232      return { result: paneResult({ status: 'nothing-to-review' }) }
233    }
234    if (!prepared.ok) {
235      return { result: paneResult({ status: 'error', reason: prepared.reason }) }
236    }
237
238    active = true
239    activeOpener = 'tool'
240    outcome = undefined
241    await update($, setup, () => ({
242      repo,
243      survey: prepared.survey,
244      ticked: initialTicks(prepared.survey),
245      progress: null,
246      checkout: prepared.checkout,
247      stepProgress: {},
248      error: null,
249      openedBy: 'tool',
250      surveyError: null,
251    }))
252    try {
253      const opened = await $.ui.open({ id: PANE, title: 'Setup', focus: true })
254      if (!opened.isPlaced) {
255        return { result: paneResult({ status: 'not-placed', reason: opened.reason }) }
256      }
257      let waited = 0
258      while (outcome === undefined && !next.signal.aborted && waited < WAIT_CAP_SECONDS) {
259        await $.process.run(['sleep', '1'])
260        waited += 1
261      }
262      return { result: paneResult(outcome ?? { status: next.signal.aborted ? 'dismissed' : 'timed-out' }) }
263    } catch (err) {
264      return { result: paneResult({ status: 'error', reason: `the setup pane failed: ${String(err)}` }) }
265    } finally {
266      active = false
267      activeOpener = null
268      outcome = undefined
269      installEpoch += 1
270      stepEpoch += 1
271      await $.ui.close({ id: PANE })
272      await update($, setup, () => null)
273    }
274  })
275
276  on('command.run', { command: 'setup-view' }, async ($, e) => {
277    if (active) {
278      return { text: 'setup-view: a setup pane is already open' }
279    }
280    const surfaces = await $.session.surfaces()
281    if (!surfaces.some((surface) => surface === 'terminal' || surface === 'desktop')) {
282      return { text: 'setup-view: no terminal or desktop surface is attached to draw the pane' }
283    }
284    const repo = await $.session.cwd()
285    const prepared = await prepareSetup($, repo)
286    if (!prepared.ok && prepared.empty) {
287      return { text: `setup-view: nothing to set up: ${prepared.reason}.` }
288    }
289    active = true
290    activeOpener = 'command'
291    outcome = undefined
292    let openedPane = false
293    try {
294      if (!prepared.ok) {
295        await update($, setup, () => ({
296          repo,
297          survey: EMPTY_SURVEY,
298          ticked: {},
299          progress: null,
300          checkout: null,
301          stepProgress: {},
302          error: null,
303          openedBy: 'command',
304          surveyError: prepared.reason,
305        }))
306      } else {
307        await update($, setup, () => ({
308          repo,
309          survey: prepared.survey,
310          ticked: initialTicks(prepared.survey),
311          progress: null,
312          checkout: prepared.checkout,
313          stepProgress: {},
314          error: null,
315          openedBy: 'command',
316          surveyError: null,
317        }))
318      }
319      const opened = await $.ui.open({ id: PANE, title: 'Setup', focus: true })
320      if (!opened.isPlaced) {
321        await $.ui.close({ id: PANE })
322        return { text: `setup-view: the pane could not be placed: ${opened.reason}` }
323      }
324      openedPane = true
325      if (!prepared.ok) {
326        return { text: `setup-view: the survey failed: ${prepared.reason}` }
327      }
328      return {
329        text: `setup-view: ${prepared.survey.tools.length} tools, ${surveySteps(prepared.survey).length} steps.`,
330      }
331    } catch (err) {
332      return { text: `setup-view: the setup pane failed: ${String(err)}` }
333    } finally {
334      if (!openedPane) await clearCommandSetup($)
335    }
336  })
337
338  on('ui.close', { id: PANE }, async ($, e, next) => {
339    // Every close but the tool's own after an outcome: the operator's close mark or Escape.
340    // A tool-opened close answers the waiting call; a command-opened close clears the
341    // state, since no call waits on it. The tool call's own `finally` clears first.
342    // The command branch clears synchronously, before the first await, so no later
343    // opener answers "already open" for a pane that is already gone.
344    if (active && outcome === undefined) {
345      if (activeOpener === 'command') {
346        active = false
347        activeOpener = null
348        installEpoch += 1
349        stepEpoch += 1
350        await update($, setup, () => null)
351      } else {
352        outcome = { status: 'dismissed' }
353      }
354    }
355    return next(e)
356  })
357
358  on('ui.render', { component: 'Pane', requestId: PANE }, async ($, e) => {
359    const state = await read($, setup)
360    if (e.surface !== 'terminal' && e.surface !== 'desktop') {
361      const { Text } = $.ui.resolve(e)
362      return <Text>Answer the setup pane in the terminal or the desktop app.</Text>
363    }
364    const { Box, Button, Text } = $.ui.resolve(e)
365    if (state === null) {
366      return <Text dimColor>No setup is open.</Text>
367    }
368    const { repo, survey } = state
369
370    const toggle = (id: string) =>
371      update($, setup, (current) => {
372        if (!current || current.progress !== null) return current
373        const row = current.survey.tools.find((one) => one.id === id)
374        if (!row || !row.has_install) return current
375        return { ...current, ticked: { ...current.ticked, [id]: current.ticked[id] !== true }, error: null }
376      })
377    const setError = (error: string) => update($, setup, (current) => current && { ...current, error })
378    const markRow = (id: string, progress: { state: 'queued' | 'installing' | 'done' | 'failed'; output: string }) =>
379      update($, setup, (current) => {
380        if (!current || current.progress === null) return current
381        return { ...current, progress: { ...current.progress, [id]: progress } }
382      })
383    const markStep = (name: string, progress: { state: 'running' | 'done' | 'failed'; output: string }) =>
384      update($, setup, (current) => {
385        if (!current) return current
386        return { ...current, stepProgress: { ...current.stepProgress, [name]: progress } }
387      })
388
389    /**
390     * Re-read the survey after an action, keeping the operator's ticks for surviving rows.
391     * `stillCurrent` is the caller's epoch check, run again after the read: a close and a
392     * reopen of the same checkout landing mid-read must not write into the new pane.
393     */
394    const refreshSurvey = async (stillCurrent: () => boolean) => {
395      const before = await read($, setup)
396      if (!before || before.repo !== repo) return
397      const reread = await readSetupSurveyWith((argv) => $.process.run(argv), $.plugin.root, before.repo)
398      if (!stillCurrent()) return
399      const current = await read($, setup)
400      if (!current || current.repo !== repo) return
401      if (!reread.ok) {
402        $.ui.toast(`setup-view: the survey refresh failed (${reread.reason}): ${reread.detail}`)
403        return
404      }
405      await update($, setup, (shown) => {
406        if (!shown || shown.repo !== repo) return shown
407        const ticked: Record<string, boolean> = {}
408        for (const row of reread.survey.tools) {
409          ticked[row.id] = shown.ticked[row.id] ?? (row.has_install && row.status !== 'installed')
410        }
411        return { ...shown, survey: reread.survey, ticked }
412      })
413    }
414
415    /** Hand a failed row to the session prompt: the row, the command as run, and the output tail. */
416    const askClaude = async (id: string, command: string, output: string) => {
417      const filled = await $.prompt.fill({ text: `setup ${id} failed: ${command}\n${shownOutput(output)}`, mode: 'insert' })
418      if (!filled.isFilled) $.ui.toast('setup-view: the prompt cannot take text right now')
419    }
420
421    /** Close a command-opened pane: nothing waits on it, so the state goes with it. */
422    const closePane = async () => {
423      await clearCommandSetup($)
424      await $.ui.close({ id: PANE })
425    }
426
427    const install = async () => {
428      const current = await read($, setup)
429      if (!current || current.repo !== repo || current.progress !== null) return
430      // Bumped after the guards, so a second press that finds work started
431      // returns without orphaning the loop already running.
432      const epoch = ++installEpoch
433      const ids = tickedIds(current.survey, current.ticked)
434      if (ids.length === 0) {
435        await setError('Tick at least one tool with an install command.')
436        return
437      }
438      const progress: Record<string, { state: 'queued' | 'installing' | 'done' | 'failed'; output: string }> = {}
439      for (const id of ids) progress[id] = { state: 'queued', output: '' }
440      await update($, setup, (shown) => (shown ? { ...shown, progress, error: null } : shown))
441      for (const id of ids) {
442        if (epoch !== installEpoch) return
443        await markRow(id, { state: 'installing', output: '' })
444        let ran
445        try {
446          ran = await $.process.run(setupInstallArgv($.plugin.root, id, repo), { timeoutMs: INSTALL_TIMEOUT_MS })
447        } catch (err) {
448          if (epoch !== installEpoch) return
449          await markRow(id, { state: 'failed', output: String(err) })
450          if (epoch === installEpoch) await refreshSurvey(() => epoch === installEpoch)
451          return
452        }
453        if (epoch !== installEpoch) return
454        const output = `${ran.stdout}${ran.stderr}`.trim()
455        if (ran.exitCode !== 0) {
456          await markRow(id, { state: 'failed', output: output === '' ? `install exited ${ran.exitCode}` : output })
457          if (epoch === installEpoch) await refreshSurvey(() => epoch === installEpoch)
458          return
459        }
460        await markRow(id, { state: 'done', output })
461      }
462      if (epoch === installEpoch) await refreshSurvey(() => epoch === installEpoch)
463    }
464    const runStep = async (name: string) => {
465      const current = await read($, setup)
466      if (!current || current.repo !== repo || current.checkout === null) return
467      if (!surveySteps(current.survey).some((step) => step.name === name)) return
468      if (current.stepProgress[name]?.state === 'running') return
469      const epoch = stepEpoch
470      const cwd = current.checkout
471      await markStep(name, { state: 'running', output: '' })
472      let ran
473      try {
474        ran = await $.process.run(setupStepArgv($.plugin.root, name), { cwd, timeoutMs: STEP_TIMEOUT_MS })
475      } catch (err) {
476        if (epoch !== stepEpoch) return
477        await markStep(name, { state: 'failed', output: String(err) })
478        if (epoch === stepEpoch) await refreshSurvey(() => epoch === stepEpoch)
479        return
480      }
481      if (epoch !== stepEpoch) return
482      const output = `${ran.stdout}${ran.stderr}`.trim()
483      if (ran.exitCode !== 0) {
484        await markStep(name, { state: 'failed', output: output === '' ? `step exited ${ran.exitCode}` : output })
485        if (epoch === stepEpoch) await refreshSurvey(() => epoch === stepEpoch)
486        return
487      }
488      await markStep(name, { state: 'done', output })
489      if (epoch === stepEpoch) await refreshSurvey(() => epoch === stepEpoch)
490    }
491    const done = async () => {
492      const current = await read($, setup)
493      if (!current) return
494      const installed: string[] = []
495      const failed: string[] = []
496      for (const [id, row] of Object.entries(current.progress ?? {})) {
497        if (row.state === 'done') installed.push(id)
498        else if (row.state === 'failed') failed.push(id)
499      }
500      outcome = { status: 'submitted', installed, failed }
501      await $.ui.close({ id: PANE })
502    }
503    const dismiss = async () => {
504      outcome = { status: 'dismissed' }
505      await $.ui.close({ id: PANE })
506    }
507
508    if (state.surveyError !== null) {
509      return (
510        <Box flexDirection="column" width={e.props.bodyColumns}>
511          <Text>The setup survey failed.</Text>
512          <Text dimColor>{shownOutput(state.surveyError)}</Text>
513          <Box key="actions" flexDirection="row" columnGap={2} marginTop={1}>
514            <Button key="close" label="Close" onPress={() => closePane()} />
515          </Box>
516        </Box>
517      )
518    }
519
520    const running = state.progress !== null && Object.values(state.progress).some((row) => row.state === 'installing' || row.state === 'queued')
521    const installable = survey.tools.some((row) => row.has_install)
522    return (
523      <Box flexDirection="column" width={e.props.bodyColumns}>
524        <Text bold>Tools</Text>
525        {survey.tools.map((row) => (
526          <Box key={`row:${row.id}`} flexDirection="column">
527            {row.has_install ? (
528              <Button
529                key={`tick:${row.id}`}
530                label={rowLabel(row, state.ticked[row.id] === true)}
531                plain
532                onPress={() => toggle(row.id)}
533              />
534            ) : (
535              <Text dimColor>{`${row.id}  ${row.status}  ${row.lens}`}</Text>
536            )}
537            <Text dimColor>
538              {`  ${row.version ?? 'no version'}  pinned ${row.pinned_version}${row.has_install ? '' : `  ${row.install ?? 'no install command'}`}`}
539            </Text>
540            {state.progress?.[row.id] && (
541              <Text key={`progress:${row.id}`} dimColor>
542                {`  ${state.progress[row.id]?.state}: ${shownOutput(state.progress[row.id]?.output ?? '')}`}
543              </Text>
544            )}
545            {state.progress?.[row.id]?.state === 'failed' && (
546              <Button
547                key={`ask:${row.id}`}
548                label="Ask Claude"
549                plain
550                onPress={async () => {
551                  const current = await read($, setup)
552                  await askClaude(row.id, setupInstallArgv($.plugin.root, row.id, repo).join(' '), current?.progress?.[row.id]?.output ?? '')
553                }}
554              />
555            )}
556          </Box>
557        ))}
558        {surveySteps(survey).length > 0 && (
559          <Box flexDirection="column" marginTop={1}>
560            <Text bold>Steps</Text>
561            {surveySteps(survey).map((step) => (
562              <Box key={`step:${step.name}`} flexDirection="column">
563                <Text>{`${step.name}: ${step.done ? 'done' : 'not done'} — ${step.summary}`}</Text>
564                {state.checkout !== null ? (
565                  <Button key={`run-step:${step.name}`} label={`Run ${step.name}`} plain onPress={() => runStep(step.name)} />
566                ) : (
567                  <Text dimColor>Steps run only from a saga checkout.</Text>
568                )}
569                {state.stepProgress[step.name] && (
570                  <Text key={`step-progress:${step.name}`} dimColor>
571                    {`  ${state.stepProgress[step.name]?.state}: ${shownOutput(state.stepProgress[step.name]?.output ?? '')}`}
572                  </Text>
573                )}
574                {state.stepProgress[step.name]?.state === 'failed' && (
575                  <Button
576                    key={`ask-step:${step.name}`}
577                    label="Ask Claude"
578                    plain
579                    onPress={async () => {
580                      const current = await read($, setup)
581                      await askClaude(step.name, setupStepArgv($.plugin.root, step.name).join(' '), current?.stepProgress[step.name]?.output ?? '')
582                    }}
583                  />
584                )}
585              </Box>
586            ))}
587          </Box>
588        )}
589        <Box key="questions" marginTop={1}>
590          <Text dimColor>The repository questions live in /saga:setup.</Text>
591        </Box>
592        {!installable && (
593          <Box key="no-install" marginTop={1}>
594            <Text wrap="wrap">Nothing here installs: no row has an install command.</Text>
595          </Box>
596        )}
597        {state.error !== null && (
598          <Box key="error" marginTop={1}>
599            <Text color="red" wrap="wrap">
600              {state.error}
601            </Text>
602          </Box>
603        )}
604        <Box key="actions" flexDirection="row" columnGap={2} marginTop={1}>
605          {installable && (
606            <Button key="install" label={running ? 'Installing…' : 'Install'} variant="primary" onPress={() => install()} />
607          )}
608          {state.openedBy === 'command' ? (
609            <Button key="close" label="Close" onPress={() => closePane()} />
610          ) : (
611            <Button key="done" label="Done" hotkey="d" onPress={() => done()} />
612          )}
613          {state.openedBy === 'tool' && installable && <Button key="dismiss" label="Dismiss" role="dismiss" onPress={() => dismiss()} />}
614        </Box>
615      </Box>
616    )
617  })
618}
619
620/**
621 * Toast the setup offer once, on the first session of a machine setup never
622 * ran on. The store is read first: when it remembers the offer the script is
623 * never called. Any store failure suppresses the path without throwing.
624 */
625async function maybeOfferSetup($: EngineInterface): Promise<void> {
626  let remembered: unknown
627  try {
628    remembered = await $.store.get(OFFER_STORE_KEY)
629  } catch {
630    return
631  }
632  if (remembered === true) return
633  const read = await readOfferStatusWith((argv) => $.process.run(argv), $.plugin.root)
634  if (!read.ok) {
635    await $.ui.toast(`saga: could not read the machine record (${read.reason}): ${read.detail}`)
636    return
637  }
638  if (read.status.ran || read.status.offered) return
639  await $.ui.toast(OFFER_TEXT)
640  let recorded
641  try {
642    recorded = await $.process.run(setupRecordOfferArgv($.plugin.root))
643  } catch (err) {
644    await $.ui.toast(`saga: could not record the setup offer: ${String(err)}`)
645    return
646  }
647  if (recorded.exitCode !== 0) {
648    const stderr = recorded.stderr.trim()
649    await $.ui.toast(`saga: could not record the setup offer: ${stderr === '' ? `record-offer exited ${recorded.exitCode}` : stderr}`)
650    return
651  }
652  try {
653    await $.store.set(OFFER_STORE_KEY, true)
654  } catch {
655    // The machine record already remembers; a store that fails just costs one
656    // offer-status call next session.
657  }
658}
659
mods/usage-capture.ts 319 lines
1// Live token capture (issue #107): what each model request of a unit session
2// costs, appended to that unit's row of the run record as the session runs.
3//
4// Saga's scripts own the record; this mod only collects and hands over.
5//
6// - At session start it asks `run_status.py unit-for` which unit row the
7//   session's directory is working: the row whose worktree it is, else the row
8//   whose branch is checked out there. No row, and the mod records nothing:
9//   no timer, no queue, no write. The coordinator in the primary checkout is
10//   such a session.
11// - Every model request (`turn.step`), on the main thread and in every
12//   subagent's loop, adds its usage to a bucket in `$.state`: one bucket per
13//   model session, role, model and effort, the identity of a `usage add` entry.
14//   `turn.step` rather than `turn.complete`, because only the request carries
15//   the effort it asked for, and a subagent's turns complete inside the
16//   parent's.
17// - Every minute, and at session end, each bucket is written through
18//   `run_record.py usage add`, the one portable writer, which takes the
19//   record's lock. One process per bucket per minute, never one per request.
20//   A failed write keeps its bucket for the next tick; a written one is
21//   subtracted, so requests that landed during the write are kept.
22//
23// Claude Code reports cache writes as one count, without the five-minute and
24// one-hour split the record prices apart. They are recorded as one-hour writes,
25// the dearer rate, so a report built on them can overstate cache-write spend
26// but never understate it (`references/run-record.md`).
27//
28// The plain fallback on every other harness is the same writer, called by the
29// harness's own integration with the counts it reads (`usage add --help`).
30
31import { atom, read, update } from 'claude-code'
32import type { EngineInterface, On } from 'claude-code'
33
34import type { SagaUsageBucket, SagaUsageTarget } from '../types/index.d.ts'
35import { KNOWN_RUN_STATUS_SCHEMA, sagaScriptArgv } from './run-record.ts'
36import type { ProcessResult } from './run-record.ts'
37
38/** How often the queue is written. */
39export const USAGE_FLUSH_MS = 60_000
40
41/** How long one timed write may run before it is abandoned and retried next tick. */
42const FLUSH_TIMEOUT_MS = 10_000
43
44/** One write at session end, inside the engine's 1.5-second budget for every hook. */
45const END_TIMEOUT_MS = 1_000
46
47/** The vendor every entry this mod writes names. */
48export const USAGE_VENDOR = 'claude'
49
50/** The `usage add` flag Claude Code's single cache-write count goes under (see the header). */
51export const CACHE_WRITE_FLAG = '--cache-write-1h'
52
53/** The effort a request recorded when its model takes none: `usage add` needs a name. */
54export const NO_EFFORT = 'none'
55
56/** A saga role registered as a Claude agent type (`saga:<role>`) records as that role. */
57const SAGA_AGENT_TYPE = /^saga:([a-z][a-z0-9-]*)$/
58
59const usageTarget = atom({ plugin: 'saga', key: 'usageTarget' } as const, null)
60const usageQueue = atom({ plugin: 'saga', key: 'usageQueue' } as const, [])
61
62/** One request's usage, as `addStep` folds it into the queue. */
63export type UsageStep = {
64  sessionId: string
65  agentId: string | null
66  role: string
67  model: string
68  effort: string
69  uncachedInput: number
70  cacheRead: number
71  cacheWrite: number
72  output: number
73}
74
75/** The argv that asks which unit row a session at `cwd` is working. */
76export function unitForArgv(pluginRoot: string, cwd: string): string[] {
77  return sagaScriptArgv(pluginRoot, 'run_status.py', ['--repo-root', cwd, 'unit-for', '--json'])
78}
79
80/**
81 * The unit `run_status.py unit-for --json` matched, or null. Anything but a
82 * clean match of the known view version is null: a mod that cannot tell which
83 * unit a session works records nothing rather than guessing.
84 */
85export function parseUnitFor(ran: ProcessResult): SagaUsageTarget | null {
86  if (ran.exitCode !== 0 || ran.isStdoutTruncated) return null
87  let parsed: unknown
88  try {
89    parsed = JSON.parse(ran.stdout)
90  } catch {
91    return null
92  }
93  if (typeof parsed !== 'object' || parsed === null) return null
94  const view = parsed as { schema?: unknown; match?: unknown }
95  if (view.schema !== KNOWN_RUN_STATUS_SCHEMA) return null
96  const match = view.match as Partial<SagaUsageTarget> | null | undefined
97  if (typeof match !== 'object' || match === null) return null
98  const { issue, unit, role, store_root } = match
99  if (!Number.isInteger(issue) || (issue as number) <= 0) return null
100  if (typeof unit !== 'string' || unit === '' || typeof role !== 'string' || role === '') return null
101  if (typeof store_root !== 'string' || store_root === '') return null
102  return match as SagaUsageTarget
103}
104
105/** The effort a request asked for, as `usage add --effort` takes it. */
106export function effortName(effort: string | number | undefined): string {
107  return effort === undefined ? NO_EFFORT : String(effort)
108}
109
110/** The role a subagent's requests record: its saga role, else the session's. */
111export function subagentRole(agentType: string | undefined, sessionRole: string): string {
112  const saga = agentType === undefined ? null : SAGA_AGENT_TYPE.exec(agentType)
113  return saga === null ? sessionRole : saga[1]
114}
115
116/** The session id an entry records: the session's own, or `<session>/<agent>` for a subagent's loop. */
117export function entrySessionId(bucket: Pick<SagaUsageBucket, 'sessionId' | 'agentId'>): string {
118  return bucket.agentId === null ? bucket.sessionId : `${bucket.sessionId}/${bucket.agentId}`
119}
120
121function sameEntry(a: Omit<UsageStep, 'uncachedInput' | 'cacheRead' | 'cacheWrite' | 'output'>, b: typeof a): boolean {
122  return (
123    a.sessionId === b.sessionId &&
124    a.agentId === b.agentId &&
125    a.role === b.role &&
126    a.model === b.model &&
127    a.effort === b.effort
128  )
129}
130
131/** The queue with one request's usage summed into its bucket. */
132export function addStep(queue: readonly SagaUsageBucket[], step: UsageStep): SagaUsageBucket[] {
133  const at = queue.findIndex((bucket) => sameEntry(bucket, step))
134  if (at < 0) return [...queue, { ...step, steps: 1 }]
135  const was = queue[at]
136  const sum: SagaUsageBucket = {
137    ...was,
138    uncachedInput: was.uncachedInput + step.uncachedInput,
139    cacheRead: was.cacheRead + step.cacheRead,
140    cacheWrite: was.cacheWrite + step.cacheWrite,
141    output: was.output + step.output,
142    steps: was.steps + 1,
143  }
144  return queue.map((bucket, i) => (i === at ? sum : bucket))
145}
146
147/**
148 * The queue after `written` reached the record: its counts subtracted from its
149 * bucket, which goes once nothing is left in it. What a request added while the
150 * write ran stays.
151 */
152export function subtractWritten(queue: readonly SagaUsageBucket[], written: SagaUsageBucket): SagaUsageBucket[] {
153  const out: SagaUsageBucket[] = []
154  for (const bucket of queue) {
155    if (!sameEntry(bucket, written)) {
156      out.push(bucket)
157      continue
158    }
159    const left: SagaUsageBucket = {
160      ...bucket,
161      uncachedInput: bucket.uncachedInput - written.uncachedInput,
162      cacheRead: bucket.cacheRead - written.cacheRead,
163      cacheWrite: bucket.cacheWrite - written.cacheWrite,
164      output: bucket.output - written.output,
165      steps: bucket.steps - written.steps,
166    }
167    if (left.steps > 0) out.push(left)
168  }
169  return out
170}
171
172/** The argv that appends one bucket to the target unit's usage block. */
173export function usageAddArgv(pluginRoot: string, target: SagaUsageTarget, bucket: SagaUsageBucket): string[] {
174  return sagaScriptArgv(pluginRoot, 'run_record.py', [
175    '--store-root',
176    target.store_root,
177    'usage',
178    'add',
179    String(target.issue),
180    '--unit',
181    target.unit,
182    '--session-id',
183    entrySessionId(bucket),
184    '--role',
185    bucket.role,
186    '--vendor',
187    USAGE_VENDOR,
188    '--model',
189    bucket.model,
190    '--effort',
191    bucket.effort,
192    '--uncached-input',
193    String(bucket.uncachedInput),
194    '--cache-read',
195    String(bucket.cacheRead),
196    CACHE_WRITE_FLAG,
197    String(bucket.cacheWrite),
198    '--output',
199    String(bucket.output),
200  ])
201}
202
203function errorText(err: unknown): string {
204  return err instanceof Error ? err.message : String(err)
205}
206
207// Module variables, on purpose: a hot reload stops the module's timers and
208// resets these with them, so the next request starts the timer again. The
209// queue itself is in `$.state` and survives the reload.
210let timerRunning = false
211let flushChain: Promise<void> = Promise.resolve()
212let flushPending = false
213const agentRoles = new Map<string, string>()
214
215/** Write every bucket in the queue, one `usage add` each, stopping at the first failure. */
216async function writeQueue($: EngineInterface, timeoutMs: number): Promise<void> {
217  const target = await read($, usageTarget)
218  if (target === null) return
219  const queue = await read($, usageQueue)
220  for (const bucket of queue ?? []) {
221    let ran: ProcessResult
222    try {
223      ran = await $.process.run(usageAddArgv($.plugin.root, target, bucket), { timeoutMs })
224    } catch (err) {
225      $.ui.log(`usage-capture: usage add did not run: ${errorText(err)}`, { to: 'debug' })
226      return
227    }
228    if (ran.exitCode !== 0) {
229      $.ui.log(`usage-capture: usage add exited ${ran.exitCode}: ${ran.stderr.trim()}`, { to: 'debug' })
230      return
231    }
232    await update($, usageQueue, (now) => subtractWritten(now ?? [], bucket))
233  }
234}
235
236/** Queue one write of the queue after any already running, so two never interleave. */
237function flushUsage($: EngineInterface, timeoutMs: number): Promise<void> {
238  flushPending = true
239  flushChain = flushChain
240    .then(() => {
241      flushPending = false
242      return writeQueue($, timeoutMs)
243    })
244    .catch((err) => $.ui.log(`usage-capture: flush failed: ${errorText(err)}`, { to: 'debug' }))
245  return flushChain
246}
247
248function startTimer($: EngineInterface): void {
249  if (timerRunning) return
250  timerRunning = true
251  $.clock.every(USAGE_FLUSH_MS, () => {
252    // A tick while a write is still waiting adds nothing: that write takes the whole queue.
253    if (!flushPending) void flushUsage($, FLUSH_TIMEOUT_MS)
254  })
255}
256
257/** The role a subagent's requests record, looked up once per subagent. */
258async function roleOf($: EngineInterface, agentId: string, sessionRole: string): Promise<string> {
259  const known = agentRoles.get(agentId)
260  if (known !== undefined) return known
261  let agentType: string | undefined
262  try {
263    agentType = (await $.agent.list()).find((agent) => agent.id === agentId)?.type
264  } catch {
265    agentType = undefined
266  }
267  const role = subagentRole(agentType, sessionRole)
268  agentRoles.set(agentId, role)
269  return role
270}
271
272export function registerUsageCapture(on: On): void {
273  on('session.start', { cwd: /^/ }, async ($, e, next) => {
274    let target: SagaUsageTarget | null = null
275    try {
276      target = parseUnitFor(await $.process.run(unitForArgv($.plugin.root, e.cwd), { timeoutMs: FLUSH_TIMEOUT_MS }))
277    } catch (err) {
278      $.ui.log(`usage-capture: unit-for did not run: ${errorText(err)}`, { to: 'debug' })
279    }
280    await update($, usageTarget, () => target)
281    if (target !== null) {
282      if (target.ambiguous) {
283        $.ui.log(`usage-capture: several unit rows match; recording to #${target.issue} ${target.unit}`, { to: 'debug' })
284      }
285      startTimer($)
286    }
287    return next(e)
288  })
289
290  on('turn.step', { model: /^/ }, async function* ($, e, next) {
291    const result = yield* next(e)
292    const usage = result.usage
293    // No response, or one without usage (interrupted, an API error): nothing was billed to read.
294    if (usage === null) return result
295    const target = await read($, usageTarget)
296    if (target === null) return result
297    const agentId = e.agentId ?? null
298    const step: UsageStep = {
299      sessionId: await $.session.id(),
300      agentId,
301      role: agentId === null ? target.role : await roleOf($, agentId, target.role),
302      model: usage.model,
303      effort: effortName(e.effort),
304      uncachedInput: usage.input_tokens,
305      cacheRead: usage.cache_read_input_tokens,
306      cacheWrite: usage.cache_creation_input_tokens,
307      output: usage.output_tokens,
308    }
309    await update($, usageQueue, (queue) => addStep(queue ?? [], step))
310    startTimer($)
311    return result
312  })
313
314  on('session.end', { sessionId: /^/ }, async ($, e, next) => {
315    await flushUsage($, END_TIMEOUT_MS)
316    return next(e)
317  })
318}
319
mods/run-band.tsx 219 lines
1// The run status band (issue #105, review words and notice in issue #165): one
2// line per active saga run above the prompt, and the issue, the phase and
3// C3's missing-tools notice in saga's status-line entry.
4//
5// Read-only. Every word comes from `scripts/run_status.py summary --all-active`,
6// whose `band_line` the script renders once for every harness: the build loop's
7// latest pass, the review cycle against its allowance, and how many lenses met
8// their bar by the verdict's own rule. The band never recomputes any of it and
9// no button here changes the run. The plain fallback on every other harness is
10// `run_status.py summary --band`.
11//
12// - Refreshes after the session starts, every minute, after each main-loop turn
13//   completes, and after a Bash call that ran one of saga's or orchestrate's
14//   state scripts. A refresh is queued on the clock so it never holds up a turn.
15// - With no active run the band passes and the status line is cleared.
16// - Plan and Review open the plan viewer (issue #104) and the review findings
17//   pane (issue #108) for the first run; those modules answer the press. Record
18//   shows the raw record as `run_record.py show` prints it; Hide hides the band
19//   for the session.
20// - This is the only saga mod that sets saga's status line: the engine keeps
21//   one line per plugin, so a second writer would overwrite this one.
22
23import { atom, read, update } from 'claude-code'
24import type { EngineInterface, On } from 'claude-code'
25
26import type { SagaRunStatus } from '../types/index.d.ts'
27import { readRunRecordWith, readRunStatusWith } from './run-record.ts'
28
29/** The raw record pane's id, and the `requestId` its `ui.render` hook matches. */
30export const RECORD_PANE = 'saga-record'
31
32/** How often the band reads the runs again with nothing else prompting it. */
33export const BAND_REFRESH_MS = 60_000
34
35/** A Code block holds at most 10,000 characters; a page of the record stays under it. */
36export const RECORD_PAGE_CHARS = 9_000
37
38/** A Bash command that ran a script which writes saga run state. */
39const WRITES_RUN_STATE = /\b(run_record|build_loop|review_result|admission|merge_turn|release_step|saga|orchestrate)\.py\b/
40
41const runStatus = atom({ plugin: 'saga', key: 'runStatus' } as const, [])
42const bandHidden = atom({ plugin: 'saga', key: 'bandHidden' } as const, false)
43const recordView = atom({ plugin: 'saga', key: 'recordView' } as const, null)
44
45/**
46 * Saga's status-line entry for the first run: the issue, its phase, and C3's
47 * missing-tools notice verbatim when the run has one. This stays the only saga
48 * writer of the status line: the engine keeps one line per plugin.
49 */
50export function statusText(runs: readonly SagaRunStatus[]): string | undefined {
51  const run = runs[0]
52  if (run === undefined) return undefined
53  const base = run.phase ? `saga #${run.issue} · ${run.phase}` : `saga #${run.issue}`
54  const notice = run.setup_notice?.text
55  return notice ? `${base} · ${notice}` : base
56}
57
58/** A line cut to `columns` cells, an ellipsis marking the cut. */
59export function fitLine(line: string, columns: number): string {
60  if (columns <= 0) return ''
61  const chars = [...line]
62  return chars.length <= columns ? line : `${chars.slice(0, columns - 1).join('')}…`
63}
64
65/** A record's JSON cut into pages a Code block can hold, breaking at a newline where one is near. */
66export function recordPages(json: string, limit = RECORD_PAGE_CHARS): string[] {
67  const pages: string[] = []
68  let rest = json
69  while (rest.length > limit) {
70    const cut = rest.lastIndexOf('\n', limit - 1)
71    let end = cut > limit / 2 ? cut + 1 : limit
72    // Never split a surrogate pair across two pages.
73    if (end === limit && /[\uD800-\uDBFF]/.test(rest[end - 1] ?? '')) end -= 1
74    pages.push(rest.slice(0, end))
75    rest = rest.slice(end)
76  }
77  pages.push(rest)
78  return pages
79}
80
81/**
82 * Read the runs again and redraw. A read that fails keeps what the band showed:
83 * a record caught mid-write must not blank it, and the next refresh retries.
84 */
85async function refreshBand($: EngineInterface): Promise<void> {
86  const cwd = await $.session.cwd()
87  const ran = await readRunStatusWith((argv) => $.process.run(argv), $.plugin.root, { repoRoot: cwd, allActive: true })
88  if (!ran.ok) return
89  const runs = ran.view.runs
90  await update($, runStatus, () => runs)
91  $.ui.status(statusText(runs))
92}
93
94/**
95 * The Plan and Review buttons' keys. The plan viewer and the review pane answer
96 * a press on these themselves (`BAND_PLAN_ELEMENT`, `BAND_REVIEW_ELEMENT`): a
97 * plugin's own `$.command.run` never reaches its own command hook, while a
98 * press is raised by the engine and reaches every hook.
99 */
100export function bandPlanKey(issue: number): string {
101  return `band-plan-${issue}`
102}
103
104export function bandReviewKey(issue: number): string {
105  return `band-review-${issue}`
106}
107
108/** The bottom of a press the pane modules answer; it runs only if neither is registered. */
109function paneNotLoaded(): void {}
110
111/** Load issue `issue`'s raw run record into the record pane and open it. */
112async function openRecord($: EngineInterface, issue: number): Promise<void> {
113  const loaded = await readRunRecordWith((argv) => $.process.run(argv), $.plugin.root, issue)
114  if (!loaded.ok) {
115    $.ui.toast(`saga: could not read the run record for #${issue} (${loaded.reason}): ${loaded.detail}`)
116    return
117  }
118  const pages = recordPages(JSON.stringify(loaded.record, null, 2))
119  await update($, recordView, () => ({ issue, pages, page: 0 }))
120  await $.ui.open({ id: RECORD_PANE, title: `Run record · #${issue}` })
121}
122
123/** Whether a refresh is running, and whether another was asked for while it ran. */
124let refreshing = false
125let refreshAgain = false
126
127/**
128 * Queue a refresh on the clock, so the dispatch that asked is not held up. One
129 * runs at a time; requests made meanwhile fold into one more read after it.
130 */
131function requestRefresh($: EngineInterface): void {
132  $.clock.after(0, async () => {
133    if (refreshing) {
134      refreshAgain = true
135      return
136    }
137    refreshing = true
138    try {
139      do {
140        refreshAgain = false
141        await refreshBand($)
142      } while (refreshAgain)
143    } finally {
144      refreshing = false
145    }
146  })
147}
148
149export function registerRunBand(on: On): void {
150  on('session.start', { cwd: /^/ }, async ($, e, next) => {
151    requestRefresh($)
152    $.clock.every(BAND_REFRESH_MS, () => {
153      requestRefresh($)
154    })
155    return next(e)
156  })
157
158  // A subagent's turn ending is not the run moving; the main loop's is.
159  on('turn.complete', { turnId: /^/ }, async ($, e, next) => {
160    if (e.agentId === undefined) requestRefresh($)
161    return next(e)
162  })
163
164  on('tool.call', { tool: 'Bash' }, async ($, e, next) => {
165    const ran = await next(e)
166    if (ran.deny === undefined && WRITES_RUN_STATE.test(e.command)) requestRefresh($)
167    return ran
168  })
169
170  on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
171    if (e.props.hasSurvey || (await read($, bandHidden))) return next(e)
172    const runs = await read($, runStatus)
173    const first = runs[0]
174    if (first === undefined) return next(e)
175
176    const { Box, Button, Text } = $.ui.resolve(e)
177    const shown = runs.slice(0, Math.max(1, e.props.maxRows - 1))
178    return (
179      <Box flexDirection="column">
180        {shown.map((run) => (
181          <Text key={`run-${run.issue}`}>{fitLine(run.band_line, e.props.bodyColumns)}</Text>
182        ))}
183        <Box key="actions" flexDirection="row" gap={1}>
184          <Button key={bandPlanKey(first.issue)} label="Plan" hotkey="p" onPress={paneNotLoaded} />
185          <Button key={bandReviewKey(first.issue)} label="Review" hotkey="r" onPress={paneNotLoaded} />
186          <Button key="record" label="Record" hotkey="o" onPress={() => openRecord($, first.issue)} />
187          <Button key="hide" label="Hide" hotkey="h" onPress={() => update($, bandHidden, () => true)} />
188        </Box>
189      </Box>
190    )
191  })
192
193  on('ui.render', { component: 'Pane', requestId: RECORD_PANE }, async ($, e) => {
194    const { Box, Button, Code, Text } = $.ui.resolve(e)
195    const view = await read($, recordView)
196    if (view === null) {
197      return (
198        <Box flexDirection="column">
199          <Text dimColor>No run record loaded. Press Record on the saga run band.</Text>
200        </Box>
201      )
202    }
203    const page = Math.min(view.page, view.pages.length - 1)
204    const turn = (to: number) => update($, recordView, (now) => (now === null ? now : { ...now, page: to }))
205    return (
206      <Box flexDirection="column">
207        <Text dimColor>{`#${view.issue} · run_record.py show · page ${page + 1} of ${view.pages.length}`}</Text>
208        <Code source={view.pages[page] ?? ''} language="json" />
209        {view.pages.length > 1 && (
210          <Box key="pages" flexDirection="row" gap={1}>
211            {page > 0 && <Button key="prev" label="Prev" onPress={() => turn(page - 1)} />}
212            {page < view.pages.length - 1 && <Button key="next" label="Next" onPress={() => turn(page + 1)} />}
213          </Box>
214        )}
215      </Box>
216    )
217  })
218}
219
types/index.d.ts 693 lines
1// Saga's state contract for Claude Code mods.
2//
3// Saga's scripts own its state; a mod only reads it, by running
4// `scripts/run_record.py show <issue>` and parsing the JSON it prints. These
5// types describe that JSON. The authority is `plugins/saga/scripts/run_record.py`
6// (`SCHEMA` and `TOP_LEVEL_KEYS`); when that file adds a record version, this
7// contract and `mods/run-record.ts` change with it.
8//
9// It also declares the session state the saga mods keep (`PluginState`, at the
10// end). Self-contained on purpose: no import and no reference, as the engine
11// requires of a plugin's `types` file. Every exported name is led by `Saga`.
12
13/** The one record version this contract reads. */
14export type SagaRunRecordSchema = 'run_record.v1'
15
16/**
17 * One issue's saga run record, as `run_record.py show` prints it. `to_dict`
18 * always writes all twelve keys, so every one is required here;
19 * `plugins/saga/tests/test_mod_run_record_contract.py` checks this list against
20 * `TOP_LEVEL_KEYS`. Fields a newer writer added are kept under the index
21 * signature.
22 */
23export type SagaRunRecord = {
24  schema: SagaRunRecordSchema
25  issue: number
26  repo: string
27  created_at: string
28  updated_at: string
29  admission: Record<string, unknown>
30  run_configuration: Record<string, unknown>
31  approval_scope: Record<string, unknown>
32  roster: unknown[]
33  units: unknown[]
34  review_cycles: unknown[]
35  next_step: string
36  [field: string]: unknown
37}
38
39/** Why a read did not produce a record. */
40export type SagaRunReadFailure =
41  /** The script ran and found no record for the issue. */
42  | 'no-record'
43  /** The record (or the script's loader) names a version this contract does not know. */
44  | 'unknown-version'
45  /** The script exited 0 but did not print one JSON object. */
46  | 'unreadable'
47  /** Any other failure, such as running outside a git repository. */
48  | 'error'
49
50/** The outcome of reading one run record. A mod shows `detail` rather than guessing. */
51export type SagaRunRead =
52  | { ok: true; record: SagaRunRecord }
53  | { ok: false; reason: SagaRunReadFailure; detail: string }
54
55/** The version token `scripts/run_status.py summary --json` prints. */
56export type SagaRunStatusSchema = 'run_status.v1'
57
58/**
59 * Where a run's build loop stands (issue #105). One unit is described (`unit`
60 * and the iteration fields set) when its worktree is the checkout or it is the
61 * only unit that ran the loop; otherwise only the green count is.
62 */
63export type SagaRunBuildLoop = {
64  unit: string | null
65  /** The latest iteration's number. */
66  pass: number | null
67  failing: number | null
68  /** Checks that could not execute, never counted as failing. */
69  could_not_execute: number | null
70  green: boolean | null
71  units_total: number
72  units_green: number
73}
74
75/**
76 * The latest code review cycle against its allowance, and how many lenses met
77 * their bar by the verdict's own rule (issue #105). A record with C1 review
78 * runs carries the round and grades instead (issue #165); the cycle keys stay
79 * unset and `round` and `grades` carry the state branch.
80 */
81export type SagaRunReviewProgress = {
82  unit: string | null
83  cycle: number | null
84  standard_allowance: number
85  escalated_allowance: number
86  /** The cycle is past the standard allowance. */
87  is_escalated: boolean
88  outcome: string | null
89  lenses_met: number
90  lenses_total: number
91  lenses_not_run: number
92  /** The newest review round, or null for an old-shape record. */
93  round: number | null
94  /** One `{lens, grade}` pair per lens in document order; empty for an old-shape record. */
95  grades: { lens: string; grade: string }[]
96}
97
98/**
99 * One run as `run_status.py summary --json` prints it. The run record supplies
100 * `next_step`, `updated_at`, `record_path`, `build_loop` and `review`; the saga
101 * envelope supplies `phase`, `plan_path` (as recorded, relative to the
102 * checkout) and `plan_file` (absolute). Each is null when its store does not
103 * know the run. `band_line` is the status band's line, rendered by the script
104 * so every harness shows the same words. `setup_notice` is the one admission
105 * notice for missing tools and the reproduction sandbox, or null when the
106 * record has none.
107 */
108export type SagaSetupNotice = {
109  text: string
110  missing_tools: string[]
111  sandbox_unavailable: boolean
112}
113
114export type SagaRunStatus = {
115  issue: number
116  repo: string | null
117  next_step: string
118  updated_at: string | null
119  record_path: string | null
120  phase: string | null
121  plan_path: string | null
122  plan_file: string | null
123  build_loop: SagaRunBuildLoop | null
124  review: SagaRunReviewProgress | null
125  setup_notice: SagaSetupNotice | null
126  band_line: string
127}
128
129/** What `run_status.py summary --json` prints. */
130export type SagaRunStatusView = {
131  schema: SagaRunStatusSchema
132  repo_root: string
133  runs: SagaRunStatus[]
134}
135
136/**
137 * One section of a plan as the plan viewer splits it: the frontmatter
138 * (`level` 0), or a heading of level 1 to 3 and the text up to the next
139 * heading of the same or a higher level. `text` starts with the heading line.
140 */
141export type SagaPlanSection = {
142  level: 0 | 1 | 2 | 3
143  title: string
144  text: string
145}
146
147/** The plan the viewer pane shows: where it is, when it was read, its sections. */
148export type SagaPlanView = {
149  /** The path as the operator or the run named it. */
150  path: string
151  /** The file, every link resolved; what an edit's `file_path` is compared with. */
152  absPath: string
153  /** The checkout a `path:line` reference in the plan is relative to. */
154  repoRoot: string
155  mtimeMs: number
156  sections: SagaPlanSection[]
157}
158
159// ---------------------------------------------------------------------------
160// The admission review pane (issue #103)
161// ---------------------------------------------------------------------------
162//
163// `scripts/admission.py --dry-run --render json` prints one document of schema
164// `admission_review.v1`; the pane draws from it and nothing else. The authority
165// is `review_data` in that script. A field the pane does not draw is left
166// untyped under the index signatures.
167
168/** The one review document version the pane reads. */
169export type SagaAdmissionReviewSchema = 'admission_review.v1'
170
171/** One tier as admission prints it: vendor, model and effort. */
172export type SagaAdmissionTier = { vendor: string | null; model: string; effort: string | null }
173
174/** A Jev cell: the text to show, and the state a pane branches on instead of the text. */
175export type SagaAdmissionJevCell = {
176  cell: string
177  state: string
178  [field: string]: unknown
179}
180
181/** One staffing row: a role, its default, Jev's cell, the tier proposed, and why. */
182export type SagaAdmissionStaffingRow = {
183  role: string
184  vendor: string | null
185  default: SagaAdmissionTier | null
186  proposed: SagaAdmissionTier | null
187  jev: SagaAdmissionJevCell
188  why: string
189}
190
191/** One lens row. `include` is `always on`, `yes`, `no` or `undeclared`. */
192export type SagaAdmissionLensRow = {
193  lens: string
194  always_on: boolean
195  include: string
196  reason: string
197  /** `band` is `pre-checked`, `consider` or null (issue #110's lens proposal). */
198  jev: SagaAdmissionJevCell & { probability: number | null; band?: string | null }
199}
200
201/** The tiers an operator may pick: models, efforts, and the pairs the palette allows. */
202export type SagaAdmissionPalette = {
203  vendor: string
204  models: string[]
205  efforts: string[]
206  effort_ceilings: Record<string, string>
207  pairs: { model: string; effort: string }[]
208}
209
210/** What `admission.py --render json` prints. */
211export type SagaAdmissionReviewData = {
212  schema: SagaAdmissionReviewSchema
213  issue: number
214  repo: string
215  pending_questions: string[]
216  staffing: { source: string; status: string; rows: SagaAdmissionStaffingRow[] }
217  lenses: { source: string; status: string; catalogue_version: string | null; rows: SagaAdmissionLensRow[] }
218  /** Null when the bundled tier palette could not be loaded. */
219  palette: SagaAdmissionPalette | null
220  [field: string]: unknown
221}
222
223/** The operator's picks while the pane is open: per role a tier, per conditional lens a decision. */
224export type SagaAdmissionSelections = {
225  staffing: Record<string, { model: string; effort: string }>
226  lenses: Record<string, { include: 'yes' | 'no'; reason: string }>
227}
228
229/** The pane's session state: the document it draws, the picks, and the last refusal shown. */
230export type SagaAdmissionReview = {
231  issue: number
232  repo: string | null
233  data: SagaAdmissionReviewData
234  selections: SagaAdmissionSelections
235  error: string | null
236}
237
238/** The answers the pane hands to `admission.py --answers -`. */
239export type SagaAdmissionAnswers = {
240  staffing_overrides: Record<string, { vendor: string; model: string; effort: string }>
241  lens_declaration: {
242    always_on: string[]
243    conditional_applies: Record<string, string>
244    conditional_does_not_apply: Record<string, string>
245  }
246}
247
248/** What `mcp__saga__review_admission` returns to the model. */
249export type SagaAdmissionReviewOutcome =
250  | { status: 'submitted'; issue: number; answers: SagaAdmissionAnswers; source: 'operator'; summary: string }
251  | { status: 'dismissed' | 'timed-out' | 'nothing-to-review' }
252  | { status: 'not-placed' | 'unavailable'; reason: string }
253  | { status: 'error'; reason: string; exitCode?: number; stderr?: string }
254
255/** The version token `scripts/run_status.py review --json` prints. */
256export type SagaReviewViewSchema = 'review_view.v1'
257
258/**
259 * Where a lens stands against its bar. `not_run` (the lens did not execute, or
260 * the result has no row for a selected lens) and `unscored` (it ran and
261 * reported findings but sets no bar) carry no score, so neither can read as a
262 * low one.
263 */
264export type SagaReviewLensState = 'met' | 'not_met' | 'not_run' | 'unscored'
265
266/** One finding as `run_status.py review` prints it; `FINDING_FIELDS` there. */
267export type SagaReviewFinding = {
268  id: string
269  severity: string
270  path: string
271  line: number | string
272  category: string
273  dimension: string
274  evidence: string
275  impact: string
276  status: string
277  confidence: string
278}
279
280/** One lens of the review: its state, why when it has no usable result, and its findings. */
281export type SagaReviewLens = {
282  lens: string
283  state: SagaReviewLensState
284  reason: string | null
285  /** Only a `met` or `not_met` lens carries its score. */
286  derived_overall: number | null
287  finding_count: number
288  /** The first few findings, most severe first. */
289  top: SagaReviewFinding[]
290  findings: SagaReviewFinding[]
291}
292
293/** The latest review result in one loop of the run record. */
294export type SagaReview = {
295  unit: string
296  cycle: number
297  loop: string
298  revision: string
299  outcome: string
300  reason: string | null
301  lenses: SagaReviewLens[]
302  /** Findings whose lens is not one of `lenses`. */
303  unattributed_findings: SagaReviewFinding[]
304  advisory_count: number
305  duplicate_count: number
306}
307
308/** What `run_status.py review --json` prints. `review` is null when no result is recorded. */
309export type SagaReviewView = {
310  schema: SagaReviewViewSchema
311  repo_root: string
312  issue: number | null
313  record_path: string | null
314  legacy_entries: number
315  review: SagaReview | SagaStateReview | null
316}
317
318// ---------------------------------------------------------------------------
319// The review-state document (issue #165)
320//
321// `scripts/review_state.py` renders a review's state and pending choices as one
322// document of schema `review_state.v1`; `run_status.py review` embeds it under
323// `state` for records with C1 review runs. A pane draws from it and nothing
324// else. The authority is `build_document` in that script. A field the panes do
325// not draw is left untyped under the index signatures.
326// ---------------------------------------------------------------------------
327
328/** The one review-state document version the panes read. */
329export type SagaReviewStateSchema = 'review_state.v1'
330
331/** One lens grade as the document prints it. */
332export type SagaStateLens = {
333  lens: string
334  grade: string
335  blocking: number
336  fix_later: number
337  [field: string]: unknown
338}
339
340/** One finding of the newest run, as the document prints it. */
341export type SagaStateFinding = {
342  id: string
343  lens: string
344  severity: string
345  statement: string
346  guard: boolean
347  merge_outcome: Record<string, unknown> | null
348  [field: string]: unknown
349}
350
351/** One where-to-look item: `file:start-end`, its state, and the fired question ids. */
352export type SagaStateWhereToLook = {
353  lens: string
354  location: string
355  questions: string[]
356  state: 'answered' | 'cleared'
357  finding_id?: string | null
358  reason?: string | null
359  [field: string]: unknown
360}
361
362/** Which tools ran, and C3's missing-tools notice verbatim. */
363export type SagaStateTools = {
364  ran: Record<string, string>
365  missing_notice: string | null
366  missing_tools: string[]
367  [field: string]: unknown
368}
369
370/** What one round added and cleared, against the round before. */
371export type SagaStateRound = {
372  round: number
373  new_blocking: string[]
374  cleared_blocking: string[]
375  [field: string]: unknown
376}
377
378/** Tokens, dollars and seconds summed across the stored runs. */
379export type SagaStateCost = {
380  tokens_in: number
381  tokens_out: number
382  cost_usd: number
383  seconds: number
384  [field: string]: unknown
385}
386
387/** Whether the merge waits, and why. */
388export type SagaStateMerge = {
389  waiting: boolean
390  reason: string
391  [field: string]: unknown
392}
393
394/** One consequence the LLM and the Jev classifier disagreed on. */
395export type SagaConsequenceDisagreement = {
396  id: string
397  llm: string
398  jev: string
399  [field: string]: unknown
400}
401
402/** The `review_state.v1` document. */
403export type SagaReviewState = {
404  schema: SagaReviewStateSchema
405  card: number
406  repo: string
407  round: number
408  lenses: SagaStateLens[]
409  findings: SagaStateFinding[]
410  pending_choices: string[]
411  merge_blocking: string[]
412  merge: SagaStateMerge
413  disputes: string[]
414  consequence_disagreements: SagaConsequenceDisagreement[]
415  unconfirmed: string[]
416  where_to_look: SagaStateWhereToLook[]
417  tools: SagaStateTools
418  degraded_inputs: unknown[]
419  rounds: SagaStateRound[]
420  cost: SagaStateCost
421  unattended: boolean | null
422  [field: string]: unknown
423}
424
425/** The state review as `run_status.py review` prints it for records with C1 runs. */
426export type SagaStateReview = {
427  state_schema: SagaReviewStateSchema
428  round: number
429  loop: string
430  revision: string
431  outcome: string
432  lenses: SagaStateLens[]
433  pending_choices: string[]
434  merge: SagaStateMerge
435  state: SagaReviewState
436}
437
438/** The answers the merge pane hands to `review_state.py answers --answers -`. */
439export type SagaMergeAnswers = {
440  answers: Record<string, string | { decision: 'merge-with-reason' | 'stop-card'; reason?: string }>
441  pane_timeout: boolean
442}
443
444/** The operator's picks while the merge pane is open. */
445export type SagaMergeSelections = {
446  fix_later: Record<string, string>
447  merge_blocking: { decision: string; reason: string }
448}
449
450/** The merge pane's session state: the document it draws, the picks, and the last refusal shown. */
451export type SagaMergeReview = {
452  issue: number
453  repo: string | null
454  data: SagaReviewState
455  selections: SagaMergeSelections
456  error: string | null
457}
458
459/** What `mcp__saga__review_merge` returns to the model. */
460export type SagaMergeReviewOutcome =
461  | { status: 'submitted'; issue: number; answers: SagaMergeAnswers; source: 'operator'; summary: string }
462  | { status: 'dismissed' | 'timed-out' | 'nothing-to-review' }
463  | { status: 'not-placed' | 'unavailable'; reason: string }
464  | { status: 'error'; reason: string; exitCode?: number; stderr?: string }
465
466// ---------------------------------------------------------------------------
467// The setup survey and the machine offer (issue #165)
468//
469// `scripts/saga_setup.py survey --format json` prints one document of schema
470// `setup_survey.v1`; the setup pane draws its tool rows and nothing else. The
471// authority is `collect` in that script. `offer-status` prints the machine
472// record's `ran` and `offered` without writing.
473// ---------------------------------------------------------------------------
474
475/** The one survey document version the setup pane reads. */
476export type SagaSetupSurveySchema = 'setup_survey.v1'
477
478/** The one machine-record version the offer reads. */
479export type SagaMachineRecordSchema = 'machine_record.v1'
480
481/** One surveyed tool row as the setup pane draws it. */
482export type SagaSetupToolRow = {
483  id: string
484  lens: string
485  status: string
486  version: string | null
487  pinned_version: string
488  has_install: boolean
489  install: string | null
490  [field: string]: unknown
491}
492
493/** One optional setup step as the survey reports it: its name, summary and done state. */
494export type SagaSetupStep = {
495  name: string
496  summary: string
497  done: boolean
498  [field: string]: unknown
499}
500
501/** What `saga_setup.py survey --format json` prints. */
502export type SagaSetupSurvey = {
503  schema: SagaSetupSurveySchema
504  tools: SagaSetupToolRow[]
505  /** The optional steps; absent on survey documents from before issue #214. */
506  steps?: SagaSetupStep[]
507  [field: string]: unknown
508}
509
510/** What `saga_setup.py offer-status` prints. */
511export type SagaOfferStatus = {
512  schema: SagaMachineRecordSchema
513  ran: boolean
514  offered: boolean
515}
516
517/** One tool's install progress while Install runs. */
518export type SagaSetupRowProgress = {
519  state: 'queued' | 'installing' | 'done' | 'failed'
520  output: string
521}
522
523/** One step's run progress while its verb runs. Kept apart from Install's progress, which Done submits. */
524export type SagaSetupStepProgress = {
525  state: 'running' | 'done' | 'failed'
526  output: string
527}
528
529/** The setup pane's session state: the survey it draws, the ticks, the step checkout, and both progresses. */
530export type SagaSetupReview = {
531  repo: string
532  survey: SagaSetupSurvey
533  ticked: Record<string, boolean>
534  progress: Record<string, SagaSetupRowProgress> | null
535  /** The saga plugin root steps run in, or null when its setup script is missing: steps then render unrunnable. */
536  checkout: string | null
537  stepProgress: Record<string, SagaSetupStepProgress>
538  error: string | null
539  /** Which opener drew this pane: the tool call, or the setup-view command (issue #221). */
540  openedBy: 'tool' | 'command'
541  /** The survey failure the command path renders, or null when the survey read (issue #221). */
542  surveyError: string | null
543}
544
545/** What `mcp__saga__setup` returns to the model. */
546export type SagaSetupOutcome =
547  | { status: 'submitted'; installed: string[]; failed: string[] }
548  | { status: 'dismissed' | 'timed-out' | 'nothing-to-review' }
549  | { status: 'not-placed' | 'unavailable'; reason: string }
550  | { status: 'error'; reason: string; exitCode?: number; stderr?: string }
551
552/**
553 * The unit row a session is working, as `run_status.py --repo-root <cwd>
554 * unit-for --json` prints it under `match` (issue #107). The token-capture mod
555 * appends this session's usage to that row through `run_record.py usage add`.
556 */
557export type SagaUsageTarget = {
558  issue: number
559  /** The row's identity as `usage add --unit` takes it: `id`, else `name`, else `unit_id`. */
560  unit: string
561  /** The staffing role the session records: the row's `role`, else `worker` (`merging-worker` in a merge turn). */
562  role: string
563  /** How the row matched: its worktree, or only its branch. */
564  matched_by: 'worktree' | 'branch'
565  record_path: string
566  /** The record store, passed back as `--store-root` so a write resolves nothing again. */
567  store_root: string
568  /** More than one row matched; this is the strongest, active and newest. */
569  ambiguous: boolean
570}
571
572/** What `run_status.py unit-for --json` prints. */
573export type SagaUnitForView = {
574  schema: SagaRunStatusSchema
575  repo_root: string
576  branch: string
577  match: SagaUsageTarget | null
578}
579
580/**
581 * Token counts not yet written to the run record, summed per model session:
582 * one bucket per session id (a subagent's loop is its own), role, model and
583 * effort, the identity of a `usage add` entry. The four counts are the ones a
584 * Claude Code `turn.step` result reports.
585 */
586export type SagaUsageBucket = {
587  sessionId: string
588  /** The subagent's id, or null on the main thread. */
589  agentId: string | null
590  role: string
591  model: string
592  effort: string
593  uncachedInput: number
594  cacheRead: number
595  cacheWrite: number
596  output: number
597  /** How many model requests the counts sum. */
598  steps: number
599}
600
601/** The raw run record pane: the record as JSON, cut into pages a Code block can hold. */
602export type SagaRecordView = {
603  issue: number
604  pages: string[]
605  page: number
606}
607
608/**
609 * One role's agent type, as `scripts/role_agent_types.py` prints it: the role's
610 * prompt (a saga hosting preamble, then the roles-library file's body) and the
611 * model and effort the run is staffed at.
612 */
613export type SagaRoleAgentType = {
614  /** The staffing role (`worker`); the registered type is `saga:<role>`. */
615  role: string
616  /** The roles library's id for it (`implementer`). */
617  role_id: string
618  readable_role: string
619  prompt_path: string
620  prompt: string
621  model: string
622  effort: string
623  /** The resolver's tier source: `operator`, `overlay`, `jev-raise` or `policy`. */
624  source: string
625  description: string
626}
627
628/** What `scripts/role_agent_types.py --json` prints: `saga_role_agent_types.v1`. */
629export type SagaRoleAgentTypes = {
630  schema: 'saga_role_agent_types.v1'
631  /** Whether this checkout has an active run; with none, `types` is empty. */
632  active: boolean
633  issue: number | null
634  types: SagaRoleAgentType[]
635  skipped: { role: string; reason: string }[]
636  /** A hash of every type's role, tier and prompt; it changes only when one of them does. */
637  fingerprint: string
638  /** Why no types could be answered, when that is the case. */
639  error?: string
640}
641
642/** One registered agent type's resolved tier, by its full name (`saga:worker`). */
643export type SagaRegisteredAgentType = {
644  role: string
645  model: string
646  effort: string
647}
648
649/** What the agent-types mod last registered, kept in the session's state. */
650export type SagaAgentTypesState = {
651  active: boolean
652  issue: number | null
653  fingerprint: string
654  byType: Record<string, SagaRegisteredAgentType>
655}
656
657declare module 'claude-code' {
658  interface PluginState {
659    saga: {
660      /** The plan the `/plan-view` pane shows, or null before one is loaded. */
661      planView: SagaPlanView | null
662      /** The section shown, by index into `planView.sections`; -1 is the section list. */
663      planSelected: number
664      /** The page of the shown section, from 0. */
665      planPage: number
666      /** The admission review pane's state (issue #103), or null when no review is open. */
667      admissionReview: SagaAdmissionReview | null
668      /** The review the `/review-view` pane shows, or null before one is loaded. */
669      reviewView: SagaReviewView | null
670      /** The lens whose findings are listed; null is the lens list. */
671      reviewLens: string | null
672      /** The run record's modification time when `reviewView` was read. */
673      reviewMtimeMs: number
674      /** The unit this session's usage goes to; null when it works none (or before it is resolved). */
675      usageTarget: SagaUsageTarget | null
676      /** Usage read from `turn.step` and not yet written through `usage add`. */
677      usageQueue: SagaUsageBucket[]
678      /** This checkout's active runs as the status band last read them (issue #105). */
679      runStatus: SagaRunStatus[]
680      /** The operator pressed the band's Hide; it stays hidden for the session. */
681      bandHidden: boolean
682      /** The raw run record the band's Record button opened, or null. */
683      recordView: SagaRecordView | null
684      /** What the agent-types mod last registered (issue #106). */
685      agentTypes: SagaAgentTypesState
686      /** The merge-confirmation pane's state (issue #165), or null when no confirmation is open. */
687      mergeReview: SagaMergeReview | null
688      /** The setup pane's state (issue #165), or null when no setup is open. */
689      setupReview: SagaSetupReview | null
690    }
691  }
692}
693
mods/pane-result.ts 11 lines
1// One writer for every pane tool's answer (issue #218): the session
2// validates a `tool.call` hook's answer against the tool's output schema,
3// which for a registered tool admits a string, an array of content blocks,
4// or nothing — never an object. Each pane keeps its documented status object
5// and sends it as compact JSON text, which the caller parses back.
6
7/** The status object as the session accepts it: compact JSON text. */
8export function paneResult(outcome: unknown): string {
9  return JSON.stringify(outcome)
10}
11
mods/run-record.ts 566 lines
1// The shared way a saga mod reads saga state and writes answers back.
2//
3// Saga's scripts own the run record. A mod never opens the files under
4// `.claude/saga/runs/` and never parses prose: it runs
5// `python3 <plugin root>/scripts/run_record.py show <issue>` and parses the
6// JSON that prints. (One pinned stderr prefix is the single exception; see
7// `EXIT_RECORD_ERROR`.) An answer goes back the same way, through a script's
8// command line, built with `sagaScriptArgv`.
9//
10// The engine's validator follows `$` only into functions declared in the same
11// file as the hook, never across an import, so nothing here takes `$` itself.
12// A closure over `$.process.run` can cross the import, though, so the guarded
13// reader lives here once and a mod's hook passes it the runner:
14//
15//   import { readRunRecordWith } from './run-record.ts'
16//
17//   const read = await readRunRecordWith((argv) => $.process.run(argv), $.plugin.root, issue)
18//
19// `readRunRecordWith` owns the catch: `$.process.run` rejects when the command
20// cannot start (no `python3` on the session's PATH) or outlasts its timeout (30
21// seconds by default), and the reader turns that into reason 'error' so the mod
22// falls back to its plain behaviour instead of throwing. No mod writes its own
23// try/catch around the run.
24//
25// A mod's test can drive the real path too: stub the engine's process runner
26// with `on('process.run', async (_$, e) => ({ value: { exitCode, stdout, stderr,
27// isStdoutTruncated, isStderrTruncated } }))`, see the argv the reader built in
28// `e.argv`, and drive the mod's hook. `plugins/saga/tests/test_mod_run_record_contract.py`
29// pins what the real script prints and exits with.
30
31import type { ProcessRunResult } from 'claude-code'
32import type {
33  SagaMachineRecordSchema,
34  SagaOfferStatus,
35  SagaReview,
36  SagaReviewStateSchema,
37  SagaReviewView,
38  SagaReviewViewSchema,
39  SagaRunRead,
40  SagaRunRecord,
41  SagaRunRecordSchema,
42  SagaRunStatusSchema,
43  SagaRunStatusView,
44  SagaSetupSurvey,
45  SagaSetupSurveySchema,
46  SagaStateReview,
47} from '../types/index.d.ts'
48
49/** The record version this module reads; `SCHEMA` in `scripts/run_record.py`. */
50export const KNOWN_SCHEMA: SagaRunRecordSchema = 'run_record.v1'
51
52/** What `$.process.run` resolves to, narrowed to the fields the parser reads. */
53export type ProcessResult = Pick<ProcessRunResult, 'exitCode' | 'stdout' | 'stderr' | 'isStdoutTruncated'>
54
55/** `run_record.py show` exits 3 when its loader meets a record version it does not know. */
56const EXIT_UNKNOWN_VERSION = 3
57
58/**
59 * Exit 2 covers both "no record" and every other loader failure, and the script
60 * has no exit code of its own for a missing record, so the start of its stderr
61 * message tells them apart. This is the one place the reader matches script
62 * text; `test_mod_run_record_contract.py` pins the prefix against the real
63 * script (DECISIONS.md, 2026-10-04).
64 */
65const EXIT_RECORD_ERROR = 2
66const NO_RECORD_PREFIX = 'run_record: no record for issue '
67
68/** A script under `scripts/` is named by its file name alone. */
69const SCRIPT_NAME = /^[A-Za-z0-9_][A-Za-z0-9_.-]*\.py$/
70
71function requireIssue(issue: number): void {
72  if (!Number.isInteger(issue) || issue <= 0) {
73    throw new RangeError(`not an issue number: ${issue}`)
74  }
75}
76
77/**
78 * The argv that runs one of saga's scripts. `$.process.run` takes argv with no
79 * shell, so nothing here is quoted; a script name that could leave `scripts/`
80 * is refused.
81 */
82export function sagaScriptArgv(pluginRoot: string, script: string, args: readonly string[]): string[] {
83  if (!SCRIPT_NAME.test(script) || script.includes('..')) {
84    throw new RangeError(`not a saga script name: ${script}`)
85  }
86  return ['python3', `${pluginRoot}/scripts/${script}`, ...args]
87}
88
89/** The argv that prints one issue's run record as JSON. */
90export function runRecordShowArgv(pluginRoot: string, issue: number): string[] {
91  requireIssue(issue)
92  return sagaScriptArgv(pluginRoot, 'run_record.py', ['show', String(issue)])
93}
94
95/** The read a mod reports when `$.process.run` rejected, so the script never ran to an exit. */
96export function runRecordRunFailed(err: unknown): SagaRunRead {
97  const detail = err instanceof Error ? err.message : String(err)
98  return { ok: false, reason: 'error', detail: `run_record show did not run: ${detail}` }
99}
100
101/** Turn what `run_record.py show` did into a record, or the reason there is none. */
102export function parseRunRecordShow(ran: ProcessResult): SagaRunRead {
103  const detail = ran.stderr.trim()
104  if (ran.exitCode === EXIT_UNKNOWN_VERSION) return { ok: false, reason: 'unknown-version', detail }
105  if (ran.exitCode === EXIT_RECORD_ERROR && detail.startsWith(NO_RECORD_PREFIX)) {
106    return { ok: false, reason: 'no-record', detail }
107  }
108  if (ran.exitCode !== 0) return { ok: false, reason: 'error', detail }
109  if (ran.isStdoutTruncated) {
110    // The engine keeps only the first 4 MiB of standard output; the JSON is cut.
111    return { ok: false, reason: 'unreadable', detail: 'run_record show printed more than the engine keeps (4 MiB); the record was cut off' }
112  }
113
114  let parsed: unknown
115  try {
116    parsed = JSON.parse(ran.stdout)
117  } catch (err) {
118    return { ok: false, reason: 'unreadable', detail: String(err) }
119  }
120  if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
121    return { ok: false, reason: 'unreadable', detail: 'run_record show did not print a JSON object' }
122  }
123  const schema = (parsed as { schema?: unknown }).schema
124  if (schema !== KNOWN_SCHEMA) {
125    return { ok: false, reason: 'unknown-version', detail: `record version ${JSON.stringify(schema)} is not ${KNOWN_SCHEMA}` }
126  }
127  return { ok: true, record: parsed as SagaRunRecord }
128}
129
130/** Runs one argv and resolves to its result; a mod passes `(argv) => $.process.run(argv)`. */
131export type ProcessRunner = (argv: string[]) => Promise<ProcessResult>
132
133/**
134 * Read one issue's run record through `run`. Never rejects: a run that could
135 * not start or timed out, and an issue number that is not one, come back as
136 * reason 'error', so every mod keeps its plain fallback without a catch of its own.
137 */
138export async function readRunRecordWith(run: ProcessRunner, pluginRoot: string, issue: number): Promise<SagaRunRead> {
139  try {
140    return parseRunRecordShow(await run(runRecordShowArgv(pluginRoot, issue)))
141  } catch (err) {
142    return runRecordRunFailed(err)
143  }
144}
145
146// ---------------------------------------------------------------------------
147// `run_status.py summary`: the read-only run view (issue #104)
148//
149// The run record has no plan path and no lifecycle phase; the saga envelope
150// does. `scripts/run_status.py summary --json` joins the two, so a mod reads
151// both through one script, the same guarded way it reads the record.
152// ---------------------------------------------------------------------------
153
154/** The view version this module reads; `SCHEMA` in `scripts/run_status.py`. */
155export const KNOWN_RUN_STATUS_SCHEMA: SagaRunStatusSchema = 'run_status.v1'
156
157/** Which runs `run_status.py summary` reports, and from which checkout. */
158export type RunStatusQuery = {
159  /** The checkout whose saga envelopes are read: the session's directory. */
160  repoRoot: string
161  /** One issue; left out, the issue the checkout's active saga or `issue/N` branch names. */
162  issue?: number
163  /** Every run with a next step, the resolved issue first. */
164  allActive?: boolean
165}
166
167/** The outcome of reading the run view. A mod shows `detail` rather than guessing. */
168export type RunStatusRead =
169  | { ok: true; view: SagaRunStatusView }
170  | { ok: false; reason: 'unknown-version' | 'unreadable' | 'error'; detail: string }
171
172/** The argv that prints the run view as JSON. */
173export function runStatusSummaryArgv(pluginRoot: string, query: RunStatusQuery): string[] {
174  const args = ['--repo-root', query.repoRoot, 'summary']
175  if (query.issue !== undefined) {
176    requireIssue(query.issue)
177    args.push('--issue', String(query.issue))
178  }
179  if (query.allActive === true) args.push('--all-active')
180  args.push('--json')
181  return sagaScriptArgv(pluginRoot, 'run_status.py', args)
182}
183
184/** Why a `run_status.py` command produced no view. */
185type RunStatusFailure = { ok: false; reason: 'unknown-version' | 'unreadable' | 'error'; detail: string }
186
187type RunStatusObjectRead = { ok: true; value: Record<string, unknown> } | RunStatusFailure
188
189/**
190 * The JSON object a `run_status.py` command printed, or why there is none.
191 * `command` names it in every detail, as in "run_status summary".
192 */
193function parseRunStatusObject(ran: ProcessResult, command: string): RunStatusObjectRead {
194  const detail = ran.stderr.trim()
195  if (ran.exitCode === EXIT_UNKNOWN_VERSION) return { ok: false, reason: 'unknown-version', detail }
196  if (ran.exitCode !== 0) return { ok: false, reason: 'error', detail }
197  if (ran.isStdoutTruncated) {
198    return { ok: false, reason: 'unreadable', detail: `${command} printed more than the engine keeps (4 MiB)` }
199  }
200  let parsed: unknown
201  try {
202    parsed = JSON.parse(ran.stdout)
203  } catch (err) {
204    return { ok: false, reason: 'unreadable', detail: String(err) }
205  }
206  if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
207    return { ok: false, reason: 'unreadable', detail: `${command} did not print a JSON object` }
208  }
209  return { ok: true, value: parsed as Record<string, unknown> }
210}
211
212/** Turn what `run_status.py summary --json` did into the view, or the reason there is none. */
213export function parseRunStatusSummary(ran: ProcessResult): RunStatusRead {
214  const parsed = parseRunStatusObject(ran, 'run_status summary')
215  if (!parsed.ok) return parsed
216  const view = parsed.value
217  if (view.schema !== KNOWN_RUN_STATUS_SCHEMA) {
218    return {
219      ok: false,
220      reason: 'unknown-version',
221      detail: `run view version ${JSON.stringify(view.schema)} is not ${KNOWN_RUN_STATUS_SCHEMA}`,
222    }
223  }
224  if (!Array.isArray(view.runs)) {
225    return { ok: false, reason: 'unreadable', detail: 'run_status summary printed no runs list' }
226  }
227  return { ok: true, view: view as unknown as SagaRunStatusView }
228}
229
230/** Read the run view through `run`. Never rejects, as `readRunRecordWith`. */
231export async function readRunStatusWith(
232  run: ProcessRunner,
233  pluginRoot: string,
234  query: RunStatusQuery,
235): Promise<RunStatusRead> {
236  try {
237    return parseRunStatusSummary(await run(runStatusSummaryArgv(pluginRoot, query)))
238  } catch (err) {
239    const detail = err instanceof Error ? err.message : String(err)
240    return { ok: false, reason: 'error', detail: `run_status summary did not run: ${detail}` }
241  }
242}
243
244// ---------------------------------------------------------------------------
245// `run_status.py review`: the latest code review, lens by lens (issue #108)
246//
247// Whether a lens met its bar is the verdict's own rule
248// (`review_consensus.lens_outcomes_for_result`); the script applies it and a
249// mod shows the answer, never recomputing it.
250// ---------------------------------------------------------------------------
251
252/** The view version this module reads; `REVIEW_SCHEMA` in `scripts/run_status.py`. */
253export const KNOWN_REVIEW_VIEW_SCHEMA: SagaReviewViewSchema = 'review_view.v1'
254
255/** Which review `run_status.py review` reports, and from which checkout. */
256export type ReviewViewQuery = {
257  /** The checkout whose run is read: the session's directory. */
258  repoRoot: string
259  /** One issue; left out, the issue the checkout's active saga or `issue/N` branch names. */
260  issue?: number
261}
262
263/** The outcome of reading the review view. A mod shows `detail` rather than guessing. */
264export type ReviewViewRead =
265  | { ok: true; view: SagaReviewView }
266  | { ok: false; reason: 'unknown-version' | 'unreadable' | 'error'; detail: string }
267
268/** The argv that prints the latest code review as JSON. */
269export function runStatusReviewArgv(pluginRoot: string, query: ReviewViewQuery): string[] {
270  const args = ['--repo-root', query.repoRoot, 'review']
271  if (query.issue !== undefined) {
272    requireIssue(query.issue)
273    args.push('--issue', String(query.issue))
274  }
275  args.push('--json')
276  return sagaScriptArgv(pluginRoot, 'run_status.py', args)
277}
278
279/** Turn what `run_status.py review --json` did into the view, or the reason there is none. */
280export function parseRunStatusReview(ran: ProcessResult): ReviewViewRead {
281  const parsed = parseRunStatusObject(ran, 'run_status review')
282  if (!parsed.ok) return parsed
283  const view = parsed.value
284  if (view.schema !== KNOWN_REVIEW_VIEW_SCHEMA) {
285    return {
286      ok: false,
287      reason: 'unknown-version',
288      detail: `review view version ${JSON.stringify(view.schema)} is not ${KNOWN_REVIEW_VIEW_SCHEMA}`,
289    }
290  }
291  const review = view.review as { lenses?: unknown } | null | undefined
292  if (review === undefined || (review !== null && !Array.isArray(review.lenses))) {
293    return { ok: false, reason: 'unreadable', detail: 'run_status review printed no lens list' }
294  }
295  return { ok: true, view: view as unknown as SagaReviewView }
296}
297
298/** Read the review view through `run`. Never rejects, as `readRunRecordWith`. */
299export async function readReviewViewWith(
300  run: ProcessRunner,
301  pluginRoot: string,
302  query: ReviewViewQuery,
303): Promise<ReviewViewRead> {
304  try {
305    return parseRunStatusReview(await run(runStatusReviewArgv(pluginRoot, query)))
306  } catch (err) {
307    const detail = err instanceof Error ? err.message : String(err)
308    return { ok: false, reason: 'error', detail: `run_status review did not run: ${detail}` }
309  }
310}
311
312// ---------------------------------------------------------------------------
313// The review-state document embedded in `run_status.py review` (issue #165)
314//
315// For records with C1 review runs the review view carries the whole
316// `review_state.v1` document under `state`. The panes read the document
317// through this reader alone, never by running the review-state script.
318// ---------------------------------------------------------------------------
319
320/** The document version this module reads; `SCHEMA` in `scripts/review_state.py`. */
321export const KNOWN_REVIEW_STATE_SCHEMA: SagaReviewStateSchema = 'review_state.v1'
322
323/** The outcome of reading the state review. A mod shows `detail` rather than guessing. */
324export type StateReviewRead =
325  | { ok: true; view: SagaReviewView; review: SagaStateReview }
326  | { ok: false; reason: 'unknown-version' | 'unreadable' | 'error'; detail: string }
327
328/** Whether the review is a state review carrying the embedded document. */
329export function isStateReview(review: SagaReview | SagaStateReview | null): review is SagaStateReview {
330  return (
331    review !== null &&
332    typeof (review as SagaStateReview).state_schema === 'string' &&
333    typeof (review as SagaStateReview).state === 'object' &&
334    (review as SagaStateReview).state !== null
335  )
336}
337
338/** Turn what `run_status.py review --json` did into the state review, or the reason there is none. */
339export function parseRunStatusStateReview(ran: ProcessResult): StateReviewRead {
340  const parsed = parseRunStatusObject(ran, 'run_status review')
341  if (!parsed.ok) return parsed
342  const view = parsed.value
343  if (view.schema !== KNOWN_REVIEW_VIEW_SCHEMA) {
344    return {
345      ok: false,
346      reason: 'unknown-version',
347      detail: `review view version ${JSON.stringify(view.schema)} is not ${KNOWN_REVIEW_VIEW_SCHEMA}`,
348    }
349  }
350  const review = view.review as SagaStateReview | null | undefined
351  if (review === null || review === undefined) {
352    return { ok: false, reason: 'unreadable', detail: 'run_status review recorded no review' }
353  }
354  if (!isStateReview(review)) {
355    return { ok: false, reason: 'unreadable', detail: 'run_status review printed no state review' }
356  }
357  if (review.state_schema !== KNOWN_REVIEW_STATE_SCHEMA || review.state.schema !== KNOWN_REVIEW_STATE_SCHEMA) {
358    return {
359      ok: false,
360      reason: 'unknown-version',
361      detail: `review-state document version ${JSON.stringify(review.state_schema)} is not ${KNOWN_REVIEW_STATE_SCHEMA}`,
362    }
363  }
364  return { ok: true, view: view as unknown as SagaReviewView, review }
365}
366
367/** Read the state review through `run`. Never rejects, as `readRunRecordWith`. */
368export async function readStateReviewWith(
369  run: ProcessRunner,
370  pluginRoot: string,
371  query: ReviewViewQuery,
372): Promise<StateReviewRead> {
373  try {
374    return parseRunStatusStateReview(await run(runStatusReviewArgv(pluginRoot, query)))
375  } catch (err) {
376    const detail = err instanceof Error ? err.message : String(err)
377    return { ok: false, reason: 'error', detail: `run_status review did not run: ${detail}` }
378  }
379}
380
381/**
382 * Read the review in either shape through `run`, enforcing the document
383 * version for a state review. One run, two parses: the legacy parse validates
384 * the envelope for both shapes, and a state review is then checked against
385 * the known document token before anything draws from it. Never rejects.
386 */
387export async function readEitherReviewWith(
388  run: ProcessRunner,
389  pluginRoot: string,
390  query: ReviewViewQuery,
391): Promise<ReviewViewRead> {
392  let completed: ProcessResult
393  try {
394    completed = await run(runStatusReviewArgv(pluginRoot, query))
395  } catch (err) {
396    const detail = err instanceof Error ? err.message : String(err)
397    return { ok: false, reason: 'error', detail: `run_status review did not run: ${detail}` }
398  }
399  const parsed = parseRunStatusReview(completed)
400  if (!parsed.ok) return parsed
401  const review = parsed.view.review
402  if (review !== null && isStateReview(review)) {
403    const checked = parseRunStatusStateReview(completed)
404    if (!checked.ok) return checked
405  }
406  return parsed
407}
408
409// ---------------------------------------------------------------------------
410// The setup survey and the machine offer (issue #165)
411//
412// The setup pane draws `saga_setup.py survey --format json` through these
413// readers alone, and the first-session offer reads `offer-status` through one.
414// Recording the offer rides `record-offer`'s exit code, which needs no reader.
415// ---------------------------------------------------------------------------
416
417/** The survey version this module reads; `SURVEY_SCHEMA` in `scripts/saga_setup.py`. */
418export const KNOWN_SETUP_SURVEY_SCHEMA: SagaSetupSurveySchema = 'setup_survey.v1'
419
420/** The machine-record version the offer reads; `MACHINE_SCHEMA` in `scripts/saga_setup.py`. */
421export const KNOWN_MACHINE_RECORD_SCHEMA: SagaMachineRecordSchema = 'machine_record.v1'
422
423/** The outcome of reading the setup survey. A mod shows `detail` rather than guessing. */
424export type SetupSurveyRead =
425  | { ok: true; survey: SagaSetupSurvey }
426  | { ok: false; reason: 'unknown-version' | 'unreadable' | 'error'; detail: string }
427
428/** The outcome of reading the machine offer status. */
429export type OfferStatusRead =
430  | { ok: true; status: SagaOfferStatus }
431  | { ok: false; reason: 'unknown-version' | 'unreadable' | 'error'; detail: string }
432
433/**
434 * The argv that surveys the checkout's tools as JSON. The survey persists the
435 * machine record with `ran` set, so it never serves as the offer check.
436 */
437export function setupSurveyArgv(pluginRoot: string, repo: string): string[] {
438  return sagaScriptArgv(pluginRoot, 'saga_setup.py', ['survey', '--repo', repo, '--format', 'json'])
439}
440
441/** The argv that installs one tool. Install runs one call per ticked tool, in order. */
442export function setupInstallArgv(pluginRoot: string, tool: string, repo: string): string[] {
443  return sagaScriptArgv(pluginRoot, 'saga_setup.py', ['install', '--tools', tool, '--repo', repo])
444}
445
446/**
447 * The argv that runs one named optional step. No `--repo`: the step's vectors
448 * resolve against the saga plugin root, so the pane runs from `$.plugin.root`
449 * (issue #218).
450 */
451export function setupStepArgv(pluginRoot: string, name: string): string[] {
452  return sagaScriptArgv(pluginRoot, 'saga_setup.py', ['step', '--name', name])
453}
454
455/** The setup script's path under a plugin root, in source and installed layouts alike. */
456export const SETUP_SCRIPT_SUFFIX = 'scripts/saga_setup.py'
457
458/**
459 * Whether the plugin root holds the setup script. A failed probe reads as absent, so a build or
460 * suite without `$.fs.exists` leaves the steps unrunnable instead of throwing.
461 */
462export async function setupScriptExistsWith(
463  exists: (path: string) => Promise<boolean>,
464  pluginRoot: string,
465): Promise<boolean> {
466  try {
467    return await exists(`${pluginRoot}/${SETUP_SCRIPT_SUFFIX}`)
468  } catch {
469    return false
470  }
471}
472
473/** The argv that prints the machine record's `ran` and `offered` without writing. */
474export function setupOfferStatusArgv(pluginRoot: string): string[] {
475  return sagaScriptArgv(pluginRoot, 'saga_setup.py', ['offer-status'])
476}
477
478/** The argv that stamps `offered` on the machine record. */
479export function setupRecordOfferArgv(pluginRoot: string): string[] {
480  return sagaScriptArgv(pluginRoot, 'saga_setup.py', ['record-offer'])
481}
482
483/** The JSON object one setup verb printed, or why there is none. */
484function setupOutput(
485  ran: ProcessResult,
486  command: string,
487): { ok: true; value: Record<string, unknown> } | { ok: false; reason: 'unreadable' | 'error'; detail: string } {
488  if (ran.exitCode !== 0) {
489    const stderr = ran.stderr.trim()
490    return { ok: false, reason: 'error', detail: stderr === '' ? `${command} exited ${ran.exitCode}` : stderr }
491  }
492  if (ran.isStdoutTruncated) {
493    return { ok: false, reason: 'unreadable', detail: `${command} printed past the stdout cap` }
494  }
495  let value: unknown
496  try {
497    value = JSON.parse(ran.stdout)
498  } catch {
499    value = undefined
500  }
501  if (typeof value !== 'object' || value === null || Array.isArray(value)) {
502    return { ok: false, reason: 'unreadable', detail: `${command} printed no JSON object` }
503  }
504  return { ok: true, value: value as Record<string, unknown> }
505}
506
507/** Turn what `saga_setup.py survey --format json` did into the survey, or the reason there is none. */
508export function parseSetupSurvey(ran: ProcessResult): SetupSurveyRead {
509  const parsed = setupOutput(ran, 'saga_setup survey')
510  if (!parsed.ok) return parsed
511  const survey = parsed.value
512  if (survey.schema !== KNOWN_SETUP_SURVEY_SCHEMA) {
513    return {
514      ok: false,
515      reason: 'unknown-version',
516      detail: `setup survey version ${JSON.stringify(survey.schema)} is not ${KNOWN_SETUP_SURVEY_SCHEMA}`,
517    }
518  }
519  if (!Array.isArray(survey.tools)) {
520    return { ok: false, reason: 'unreadable', detail: 'saga_setup survey printed no tool rows' }
521  }
522  return { ok: true, survey: survey as unknown as SagaSetupSurvey }
523}
524
525/** Turn what `saga_setup.py offer-status` did into the offer status, or the reason there is none. */
526export function parseOfferStatus(ran: ProcessResult): OfferStatusRead {
527  const parsed = setupOutput(ran, 'saga_setup offer-status')
528  if (!parsed.ok) return parsed
529  const status = parsed.value
530  if (status.schema !== KNOWN_MACHINE_RECORD_SCHEMA) {
531    return {
532      ok: false,
533      reason: 'unknown-version',
534      detail: `machine record version ${JSON.stringify(status.schema)} is not ${KNOWN_MACHINE_RECORD_SCHEMA}`,
535    }
536  }
537  if (typeof status.ran !== 'boolean' || typeof status.offered !== 'boolean') {
538    return { ok: false, reason: 'unreadable', detail: 'saga_setup offer-status printed no ran and offered flags' }
539  }
540  return { ok: true, status: status as unknown as SagaOfferStatus }
541}
542
543/** Read the setup survey through `run`. Never rejects, as `readRunRecordWith`. */
544export async function readSetupSurveyWith(
545  run: ProcessRunner,
546  pluginRoot: string,
547  repo: string,
548): Promise<SetupSurveyRead> {
549  try {
550    return parseSetupSurvey(await run(setupSurveyArgv(pluginRoot, repo)))
551  } catch (err) {
552    const detail = err instanceof Error ? err.message : String(err)
553    return { ok: false, reason: 'error', detail: `saga_setup survey did not run: ${detail}` }
554  }
555}
556
557/** Read the machine offer status through `run`. Never rejects, as `readRunRecordWith`. */
558export async function readOfferStatusWith(run: ProcessRunner, pluginRoot: string): Promise<OfferStatusRead> {
559  try {
560    return parseOfferStatus(await run(setupOfferStatusArgv(pluginRoot)))
561  } catch (err) {
562    const detail = err instanceof Error ? err.message : String(err)
563    return { ok: false, reason: 'error', detail: `saga_setup offer-status did not run: ${detail}` }
564  }
565}
566