mirror of
https://github.com/garrytan/gstack.git
synced 2026-10-04 10:26:52 +02:00
* refactor(resolvers): split review.ts into MECE resolver modules (pure move) Move every function from scripts/resolvers/review.ts, unchanged, into: - review-dashboard.ts: review dashboard, plan-file review report - plan-gates.ts: approval check, exit-plan-mode gate, plan-file discovery, plan-completion audit/gate (ship + review), plan verification exec - spec-review.ts: both spec review loops, benefits-from, anti-shortcut clause - outside-voice-steps.ts: Codex second opinion, adversarial step, Codex plan review, Codex doc review, disabled-outside record - review-scope.ts: scope drift, cross-review dedup, shared-code reuse review.ts is deleted; index.ts imports the new modules. gen-skill-docs output is byte-identical for every host (--host all). Test imports and source-path references are re-pointed; the two source-text report/gate tests in gen-skill-docs.test.ts become behavioral renders across every consuming skill and host. All 46 touchfile entries that named review.ts now name all five modules, guarded by a recorded selection golden. * test(browse): black-box auth matrix for every server route and both surfaces Drives buildFetchHandler fetchLocal/fetchTunnel with no token, wrong token, root token, scoped token and the SSE cookie for all 33 routes, plus unmatched paths and wrong methods. Denials assert today's exact status, body and content type; allowed credentials assert the handler was reached. Written against the unchanged if-chain server so the W3 route-table refactor must keep it green. * refactor(shard-engine): move scripts/test-strict-output.ts to scripts/lib/shard-engine.ts The shared shard engine grows from the existing strict-output module (runShardChild, killProcessGroup, signal forwarding, strict classifier). scripts/test-strict-output.ts stays as a re-export so existing importers, mock.module paths and the strict-output/run-shard-child tests are unchanged. The engine inherits the global touchfile entry; the free runner's CLI-routing fixture copies the new module. * refactor(resolvers): decompose the three >150-line review resolvers (output-neutral) Split generateAdversarialStep, generateCodexPlanReview and generatePlanCompletionAuditInner into per-section helpers whose template literals are copied verbatim, so every function in the new modules is at or under 150 lines. gen-skill-docs output is byte-identical for every host (--host all, compared against96764e80with a fixed --link-root). * refactor(resolvers): one outside-voice failure policy (deliberate prose unification) outsideVoiceFailurePolicy(ctx, opts) in outside-voice.ts now renders the auth / timeout / empty-response bullets for all four call sites that hand-typed them (Codex second opinion, adversarial step, Codex plan review, design outside voices). Options are explicit per site (timeoutMinutes, onTimeout, stderrOnEmpty, fallback, escape) with no defaults. Deliberate generated-prose changes (every host): - office-hours: 'Fall back to <native> subagent.' becomes 'Fall back to the <native> subagent below.' - plan-devex-review: the plain 'Auth failure (stderr contains ...)' bullets become the canonical bold bullets; auth also triggers on 'API key'; 'auth failed' becomes 'authentication failed'. - review/ship adversarial: 'exceeded 9 minutes and was terminated' becomes 'timed out after 9 minutes and was terminated'; the timeout is still MISSING COVERAGE. - design outside voices: unchanged. Adds ratchet (d) (test/outside-voice-failure-policy.test.ts) with a reasoned allowlist for /codex's own CLI errors, the MISSING COVERAGE retention test, refreshed codex/factory ship goldens, and outside-voice.ts in every touchfile entry of review.ts and design.ts (selection golden extended). * test(pty): fake PTY session driver with an injectable clock through the runner launch seam The three plan-skill runners take an optional PtyDriver (launch, now, monotonic, sleep); omitted, they use the real launcher and clocks exactly as before. test/helpers/pty/fake-session.ts feeds scripted frames through that seam, and claude-pty-runner.runners.unit.test.ts runs observation, counting and floor for success, deadline timeout, permission prompt and plan-ready outcomes with no CLI or real timers. These cases must stay green unchanged through the W4 split and the runPtySession extraction. Touchfiles: every entry that lists claude-pty-runner.ts or pty-screen.ts now also lists test/helpers/pty/**. * refactor(shard-engine): run both lanes on the shared engine; lane policy injected Engine (scripts/lib/shard-engine.ts) gains the W2 primitives: per-shard tmp/Chromium sandbox + async cleanup backstop, log-path allocation and full-stream log capture, one duration-seed reader/writer with a lane predicate, LanePolicy (seed predicate + zero-execution verdict), strictShardStatus, and the shared CLI flag loop. runShardChild takes an optional companion (signal/settle) and waits a bounded 250ms to reap a wall-killed child. Free lane stops spawning shards itself: runFreeShard uses runShardChild with trackShardBrowser as the companion (win32 path unchanged: no process group, no negative-pid kill). Its sync state-dir removal stays lane policy. Paid lane uses the sandbox, log, seed, verdict and flag primitives; the hollow-shard guard applies PAID_LANE_POLICY. Lane outcomes are unchanged (free keeps >= 0 seeds and file-count zero-exec rule; paid keeps > 0 seeds, warning under selection and passed-empty under EVALS_ALL). paid-free-boundary's closure assertion now names the engine module, where the strict classifier lives. * test(shard-engine): engine unit tests, fixture-corpus equivalence, per-lane CLI parity - test/shard-engine.test.ts: failing/unhandled/module-load output fails both lanes, per-lane zero-execution and seed rules, whole-group kill on a wall timeout (both lanes), mocked-win32 path with no negative-pid kill, companion settle order, log capture, sandbox isolation, flag loop. - test/shard-engine-equivalence.test.ts + test/fixtures/shard-equivalence: seven outcome fixtures plus one real shard, run through both lanes and compared with classifications recorded from the base runners (96764e80). - test/shard-cli-parity.test.ts + test/fixtures/shard-cli-parity: flag set, defaults, validation errors and the Unknown argument error per lane match the base runners. * refactor(shard-engine): decompose runFreeShard and runPaidShard to <= 150 lines Output-neutral extraction under the fixture-corpus equivalence and runner tests: captureFreeStream, explainFreeVerdict and logFreeRecovery (free); paidShardCommand, settleShardSpool, settleBootstrapRetention and printLogTail (paid). The bootstrap scope-creation block that bootstrap-retention.test.ts evaluates stays verbatim. * refactor(pty): split claude-pty-runner.ts into test/helpers/pty/* behind a barrel Pure move: every line of the former 5,047-line runner lands verbatim in one module (four private helpers gain `export` for cross-module use): binary, screen (absorbs test/helpers/pty-screen.ts, which now re-exports it), launch, session (PtyDriver), judge, classify, auq, plan-native, boundaries, runners/{observation,counting,floor}. claude-pty-runner.ts re-exports the original public surface by name; pty/ modules import siblings directly. Tests that read the runner's source text: - rewritten as behavioral: the unit test's model-pin tripwire (fake CLI argv: fallback chain, --model before extraArgs, hermetic --strict-mcp-config), pty-skill-seeding-wiring (runners through the fake driver; launcher through a fake CLI reporting CLAUDE_CONFIG_DIR). The "three wrappers forward model" grep is replaced by the runners' fake-driver launch assertions. - pty-screen-session / pty-screen-supervision: stop copying runner source; they mock.module the real pty/screen.ts (and the fixture cleanup) instead. - re-pointed to the owning module (they execute a sliced runner body with injected boundaries; no seam exists for those boundaries yet): eng-seeded-completion-ai, plan-floor-permission, plan-create-prepublication, plan-count-completion; hermetic-wiring's source guard now reads pty/launch.ts and scans every pty/ module for raw process.env spreads. - plan-count-timeout and pty-output-wake mock the viewport at pty/screen.ts. * test(ratchet-c): enforcing module/function size ratchet and moved-code touchfile coverage Ratchet (c) ships enforcing: test/helpers/module-size.ts counts file and top-level function lengths by brace matching over masked source (strings, comments, regex literals and template text masked; ${} expressions kept), covering function declarations, arrow functions assigned to consts and route-table handler properties, with no parser dependency. Its self-test uses template literals and code-fence braces copied from scripts/resolvers/review.ts and design.ts. test/fixtures/module-size-ratchet.json binds scripts/lib/shard-engine.ts (<= 800 lines, <= 150 per function) and records the residual runner sizes (free 2352, paid 1921) as non-growth caps; allowlist entries are keyed on file plus matched text and need a reason. Failure output lists file:line, the rule, Fix: and the allowlist path. touchfiles.test.ts gains the moved-code superset check over test/fixtures/touchfile-move-goldens/ (W2 golden recorded at96764e80: test-strict-output.ts and test-paid-shards.ts global, test-free-shards.ts none). * refactor(browse): declared route table replaces the buildFetchHandler if-chain The ~1,300-line if-chain in buildFetchHandler becomes a route table: each entry declares method, path, auth kind and surfaces, and one auth gate in browse/src/routes/table.ts returns the per-kind denial (root-bearer, scoped, root-or-sse-cookie: 401 Unauthorized; root-token: 403 Root token required; extension-origin: 403 Forbidden). Unmatched requests take the declared fallthrough (root-bearer check, then plain-text 404). Handlers move to browse/src/routes/{core,pairing,pty,tokens,tunnel,activity,commands,files, inspector}.ts and receive a RouteContext with auth checks as functions instead of closing over factory locals. Dispatch order is unchanged: tunnel filter, beforeRoute overlay, gate, handler. TUNNEL_PATHS stays a literal in server.ts. Behavior-preserving: the black-box auth matrix from the previous commit passes unchanged. /memory and /inspector/events are declared root-bearer because the blanket check always ran before their SSE-cookie branch. Source-text route tests are rewritten as behavioral tests through buildFetchHandler or a route's real handler with a stub RouteContext (browse/test/route-test-harness.ts). Checks with no runtime seam are re-pointed to the route modules: Surface type, /inspector/events SSE helper, sanitizeReplacer imports, /pty-inject-scan sidecar-client import, and the ngrok config lookup and startTunnel wiring that stay in server.ts. * test(browse): stubbed-handler auth matrix and route inventory for the route table Every ROUTES entry runs through the real dispatcher and gate with stub handlers on each declared surface and six credentials; denials assert the exact status and body each auth kind returned at96764e8, admitted credentials assert the handler ran (with the gate's TokenInfo for scoped routes). Also pins the reviewed route inventory (method, path, auth kind, surfaces), that every entry declares auth and surfaces, that the table's tunnel paths equal the TUNNEL_PATHS literal with GET /connect admitted, the unmatched fallthrough, and that the root token is rejected on every tunnel route through buildFetchHandler. * test(browse): ratchet (b) keeps route dispatch inside the route table Scans browse/src/server.ts and browse/src/routes/*.ts for pathname comparisons; only the table matcher and the tunnel-surface filter are allowed, listed with reasons in browse/test/fixtures/route-dispatch-allowlist.json (keyed on file plus line text). Also checks every entry declares auth and surfaces and that gstack registers no beforeRoute overlay itself. Self-tests plant a violation and assert the file:line, Fix: and allowlist path in the message, that a shifted line stays allowlisted, and that a reasonless entry is rejected. * test: touchfile superset check for modules moved out of browse/src/server.ts Records the paid evals selected by touching browse/src/server.ts at96764e80(17 E2E, 1 LLM judge) and asserts every browse/src/routes/*.ts module selects a superset. The test reads every golden in test/fixtures/moved-module-selection/ so other moved-code goldens can sit beside it. * test(shard-engine): give non-timeout corpus fixtures CI headroom; keep the POSIX golden off the Windows lane Only the wall-timeout fixture keeps a 3s wall; the rest get 60s so a loaded host cannot turn a pass into a timeout. Base and branch runners still agree on every classification under the new walls. The Windows exclusion entry moves the free runner's ratchet (c) residual cap to 2356 lines. * refactor(pty): one runPtySession loop drives observation, counting and floor test/helpers/pty/session.ts owns launch -> start -> (poll -> tick)* -> timeout and the failure contract the three runners each hand-rolled: the run's own error wins over capture and close errors, close always runs, owned fixture cleanup runs last (also when launch fails). Each runner now supplies a PtySessionPlan: its boot/command step, poll cadence (2s observation/floor sleep; counting's output wake + 250ms coalesce), tick policy (permission handling, native identity, terminal rules stay per runner because they differ) and capture hooks. The runner bodies are decomposed into top-level steps so no function exceeds 150 lines; behavior is unchanged and the fake-driver cases from the first W4 commit pass unmodified. The counting capture step and the native completion-summary predicate are now named functions (countingCapture, isNativeCompletionSummary), so plan-create-prepublication and plan-count-completion call them directly instead of executing sliced source. The two harnesses that still execute a sliced runner body with injected boundaries (eng-seeded-completion-ai, plan-floor-permission) pass the PtyDriver seam instead of overriding Date/Bun.sleep. * test(ratchet-c): register route modules, review resolver modules and server.ts residual cap * refactor(pty): decompose launchClaudePty and engNumberedFindingAUQ under 150 lines launchClaudePty (349 lines) becomes launch preparation (args, hermetic child env, owned state roots), recorder creation, spawn, the trust-dialog watcher, close, and the session handle over one PtyProcess state object. The failure order is unchanged: abort the viewport, dispose any recorders created so far, dispose the viewport, rethrow. The --model / --strict-mcp-config ordering and seedSkills wiring stay pinned by the behavioral fake-CLI tests. engNumberedFindingAUQ (345 lines) keeps its guards and dispatch; each self-contained issue family (declared cache, library retry hooks, cache owner, injected singleton, shared writers, injected export) moves verbatim into its own function. Every pty/ module is now <= 800 lines and every top-level function <= 150 lines. * test(pty): split claude-pty-runner.unit.test.ts along the pty/ module seams The 188 unit tests move verbatim into claude-pty-runner.{screen,classify, auq,launch,plan-native,boundaries}.unit.test.ts (test names unchanged; each file imports only what it uses from the barrel). The five files that no longer read a SKILL.md template join the test-of-test ratchet baseline with a reason. * test(touchfiles): moved PTY modules keep their paid-eval selection test/fixtures/touchfile-selection/w4-pty.json records, at96764e8, the paid evals selected by touching test/helpers/claude-pty-runner.ts (20) and test/helpers/pty-screen.ts (20). touchfiles.test.ts now asserts every .ts file under test/helpers/pty/ (and pty/screen.ts for both sources) selects a superset, reading every golden in that directory so later moves can add one; a planted-violation case pins the report and its Fix line. * fix(browse): unexchanged pair setup keys no longer authenticate bearer requests validateToken accepted a gsk_setup_ key as a bearer on /command, /batch and /file (found while building the W3 auth matrix). A setup key now only authenticates the /connect exchange. * W1: one state-root owner (lib/state-root.ts + bin/gstack-state-root.sh), gstack-paths --explain and fail-stop, parity tests * W1: guarded migration of every executable state-root site; uninstall deletes only ~/.gstack Bins, careful/freeze hooks, setup, upgrade migrations, browse/src, design, ios-qa daemon, lib and scripts resolve the state root through bin/gstack-state-root.sh (bash) or lib/state-root.ts (TS). Bins source the twin and stop with a reinstall message when it is missing; hooks source it and never spawn gstack-paths. browse/src/config.ts and lib/cso/state.ts delegate to resolveStateRoot. Analytics writers and readers move together so the usage log stays one file. gstack-uninstall deletes state only at ~/.gstack, refuses (exit 2) when it resolves to /, $HOME or an ancestor, the checkout or the git root, and leaves any other resolved root in place with the removal command. Fixtures that copy single bins now copy the twin. * W1: privacy keys and trust-policy deny tiers merge across state roots; gstack-config reporting; test hermeticity readConfigKey / gstack_read_config_key return the most restrictive telemetry, memorable_recall, codex_reviews and update_check across the resolved root and ~/.gstack; other keys read the resolved root only. gstack-config set reports an overriding root with the exact override command, list shows the winning root and a root-variable disagreement line. gstack-gbrain-repo-policy get merges deny/read-only tiers. gstack-egress reads through readConfigKey. test-setup.ts strips inherited GSTACK_STATE_ROOT/GSTACK_STATE_DIR and redirects the legacy root. * W1: shared hook logging helper (hosts/claude/hooks/hook-log.ts) One hook-errors.log writer: root from resolveStateRoot, 0600 on every append, opt-in rate limit used only by memorable-user-prompt. The five hooks route through it. * W1: docs/state-root.md and README troubleshooting pointer Precedence table, a real --explain example, the move-your-state recipe, merged privacy keys, the uninstall rule, the resolver-failure fix, and the plugin-mode note (evidence gate: no official plugin distribution). * W1b: template and resolver prose resolve state through guarded gstack-paths; ratchet (a) Every gstack-paths eval in templates and resolvers carries the fail-stop guard; executable ~/.gstack paths in bash blocks (context recovery preamble, eureka log, analytics, project artifacts, upgrade snooze, setup-gbrain lock, retro snapshots, ship consent marker) use $GSTACK_STATE_ROOT, and the writer prose that pairs with them points at the printed PROJECT_DIR / RETRO_FILE. ship drops export GSTACK_STATE_ROOT. SKILL.md regenerated (claude + codex), ship goldens re-pinned, parity and context-budget caps raised to the measured sizes with notes. test/state-root-ratchet.test.ts enforces the rule with a reasoned allowlist; W1 touchfile entries plus a superset golden. * refactor: apply W1 state-root edits in W2/W3/W5-owned files; one moved-code touchfile golden for all workstreams * test: fold the moved-code touchfile golden into touchfiles.test.ts; fix integration fixture closure and caps * v1.91.11.0: CHANGELOG, TODOS, docs and conventions for the refactor wave * test: re-measure plan-ceo/design-consultation caps and ship goldens after the guarded plan-discovery and spec-review blocks; add the state-root twin to the workflow-boundaries fixture * fix(windows): migrations resolve their directory with either path separator; state-root parity compares under the HOME Git Bash actually sees * fix(review,ship): state plan-check timing after smoke expiry and test_stub Skip semantics (review workflow judge clarity) * test(qa-eval): webhook fix eval asks for the fix loop's post-repair probes; eight-scenario coverage stays in the report-only case and the harness recheck * test(qa-eval): re-pin the webhook prompt contract to the fix-loop stage; R29 coverage omissions stay bound by the report-only case * fix(review,ship): plan checks publish a checkpoint before each probe; only the smoke expiry stop is skipped * fix(qa): carry #2999's checkpoint receipt link, report-template line and full-revision placeholder (identical hunks) * test(qa-callers): disable git auto maintenance in the caller fixture Git 2.47+ runs auto maintenance detached after commit; on the CI runner's git 2.55 it rewrote .git/objects fan-out directories while the write observer was running, which surfaced as unauthorized mutations. Same gc.auto=0 / maintenance.auto=false guard the shared-libs fixture already uses. * test(plan-mode-no-op): require prose evidence for the prose-fallback members so a spinner-frame judge verdict cannot end the run as asked * test(ship-docsync): carry #2999's seeded-attempt docsync harness (identical files) The doc-sync fault cases replayed attempt 1 before reaching their gate and ran out of their 285s budget. The fixture now seeds attempt 1 and the parent starts at the gate under test. Taken byte-identical from origin/capy/audit-fix-wave (fb526898,e6ac813d,6ce10ff7,d0c53577,77cce3be). Local: stale-before, recovery and late-result 6/6 PASS (97-164s); the whole file 12/12 PASS.
723 lines
41 KiB
TypeScript
723 lines
41 KiB
TypeScript
/**
|
|
* runPlanSkillCounting. Moved from claude-pty-runner.ts.
|
|
* Import through test/helpers/claude-pty-runner.ts from tests; pty/ modules import siblings directly.
|
|
*/
|
|
import * as path from 'node:path';
|
|
import { createPlanCountFixture } from '../../plan-count-fixture';
|
|
import { createPlanCountSnapshotWriter } from '../../plan-count-artifacts';
|
|
import { readPlanCountTranscript, unresolvedPlanQuestionCalls, type NativePlanQuestionCall, type PlanCountTranscript, type NativePublicToolEvent } from '../../plan-count-transcript';
|
|
import { withPendingExit } from '../../plan-count-pending-exit';
|
|
import { readPendingQuestion } from '../../plan-count-pending-question';
|
|
import { currentFilePermissionBinding, readPendingWriteInput, type FilePermissionEpoch } from '../../plan-count-file-permission';
|
|
import { autoplanArtifactRecorderStatus, autoplanArtifactApprovalBoundary } from '../../autoplan-artifact-recorder';
|
|
import { stripVTControlCharacters } from 'node:util';
|
|
import { capturePlanCountQuestion, createPlanCountPermissionGuard, matchesNativePlanQuestion, nativePlanCallFingerprint, planCountPrerequisitePick, planCountQuestionInput, planCountQuestionPhase } from '../auq';
|
|
import type { AskUserQuestionFingerprint, Step0BoundaryPredicate } from '../auq';
|
|
import { SANCTIONED_WRITE_SUBSTRINGS, classifyPlanCountFrame, isProseAUQVisible, planCountSubmissionInput } from '../classify';
|
|
import { selectPtyNumberedOption } from '../launch';
|
|
import type { ClaudePtySession } from '../launch';
|
|
import { evaluateOwnedNativePlanTerminal, hasNativePlanCompletion, hasNativePlanTerminal, isQuestionlessNativePlanExit } from '../plan-native';
|
|
import type { NativePlanTerminalEvaluator } from '../plan-native';
|
|
import { isNumberedOptionListVisible, isPermissionDialogVisible, isRejectedSlashCommand } from '../screen';
|
|
import { realPtyDriver, runPtySession, type PtyDriver, type PtyStep } from '../session';
|
|
|
|
// ────────────────────────────────────────────────────────────────────────────
|
|
// runPlanSkillCounting — drives a plan-* skill end-to-end through Step 0 then
|
|
// counts completed review-phase AskUserQuestion calls. The actual
|
|
// product asserted by the per-finding-count tests.
|
|
// ────────────────────────────────────────────────────────────────────────────
|
|
|
|
/**
|
|
* Result of a `runPlanSkillCounting` run. Includes both the count summary
|
|
* (`step0Count`, `reviewCount`, `administrativeCount`) and the full fingerprint list for diagnostic
|
|
* dumps when an assertion fails.
|
|
*/
|
|
export interface PlanSkillCountObservation {
|
|
/** Durable full raw/visible PTY output plus JSON observation, when EVALS_RUN_ID or GSTACK_EVAL_DIR is set. */
|
|
artifactDir?: string;
|
|
artifactError?: string;
|
|
outcome:
|
|
| 'plan_ready'
|
|
| 'completion_summary'
|
|
| 'collection_complete'
|
|
| 'ceiling_reached'
|
|
| 'silent_write'
|
|
| 'transcript_unavailable'
|
|
| 'artifact_permission_failed'
|
|
| 'no_review_questions'
|
|
| 'exited'
|
|
| 'timeout';
|
|
summary: string;
|
|
/** Visible terminal text at terminal time (last 3KB). */
|
|
evidence: string;
|
|
/** Wall time (ms) until the outcome was decided. */
|
|
elapsedMs: number;
|
|
/** All distinct AskUserQuestions observed, in observation order. */
|
|
fingerprints: AskUserQuestionFingerprint[];
|
|
/** Actual native calls, including unanswered/failed ones that add no coverage. */
|
|
transcript: PlanCountTranscript;
|
|
/** Setup questions; administrative calls are excluded. */
|
|
step0Count: number;
|
|
/** Review questions; administrative calls are excluded. */
|
|
reviewCount: number;
|
|
/** Answered administrative handoffs and artifact rendering, preserved separately. */
|
|
administrativeCount: number;
|
|
}
|
|
|
|
/** Options for runPlanSkillCounting. */
|
|
export interface PlanSkillCountingOptions {
|
|
/** Skill name, e.g. 'plan-ceo-review'. Used for diagnostic strings only. */
|
|
skillName: string;
|
|
/** Slash command to send alone, e.g. '/plan-ceo-review'. No trailing args. */
|
|
slashCommand: string;
|
|
/** Fixture request seeded in initial project context before the slash command. */
|
|
followUpPrompt: string;
|
|
/** Observe this caller-owned disposable plan for permission identity only.
|
|
* Does not impose the expectedPlanPath terminal-report contract. */
|
|
permissionPlanPath?: string;
|
|
/** Declared actor support for the required QA artifact; no other state path gains approval. */
|
|
approveEngTestPlanEdits?: boolean;
|
|
/** Per-skill predicate: which answered AUQ is the last Step-0 question. */
|
|
isLastStep0AUQ: Step0BoundaryPredicate;
|
|
/** Optional positive identity for a first finding when no final setup AUQ was emitted. */
|
|
isFirstReviewAUQ?: Step0BoundaryPredicate;
|
|
/** Optional native setup classifier; late/reordered setup must not become a finding. */
|
|
isSetupAUQ?: Step0BoundaryPredicate;
|
|
/** Optional native completed-review handoff identity, excluded from both count bands. */
|
|
isCompletionHandoffAUQ?: Step0BoundaryPredicate;
|
|
/** Opt-in one-shot semantic assessment at a real native Exit. Existing callers
|
|
* retain their synchronous navigation and terminal policy. */
|
|
evaluateTerminal?: NativePlanTerminalEvaluator;
|
|
/** Accepted artifact rendering is not a finding; its answer still requires a fresh report. */
|
|
isArtifactGenerationAUQ?: Step0BoundaryPredicate;
|
|
/** Optional issue classifier across phases; receives full native call metadata. */
|
|
isReviewAUQ?: (fp: AskUserQuestionFingerprint, priorCalls?: readonly NativePlanQuestionCall[]) => boolean;
|
|
/** Stop a collection-only fixture once its acknowledged inputs are complete.
|
|
* This is not review completion or a passing verdict; the caller still validates them. */
|
|
isCollectionComplete?: (transcript: PlanCountTranscript, fingerprints: readonly AskUserQuestionFingerprint[]) => boolean;
|
|
/** Narrow caller-specific selection; null retains the normal answer policy.
|
|
* The first argument retains full pending metadata for existing callers.
|
|
* Native-bound selection uses activeCapture, whose metadata is present only
|
|
* when capturePlanCountQuestion matched the currently visible native question. */
|
|
pickAUQ?: (fp: AskUserQuestionFingerprint, activeCapture: AskUserQuestionFingerprint,
|
|
context: Readonly<{ cwd: string; deadlineAt: number }>) => number | null;
|
|
/** Opt-in declared actor: wait for a complete current native tab, then require
|
|
* its picker answer. Unbound redraws never consume seen state or default to 1. */
|
|
requireNativePicker?: boolean;
|
|
/** Observe owned pending AUQs for callers that need identity before answering. */
|
|
observeSetupQuestions?: boolean;
|
|
/** Bind the declared Design board actor and renderer to one fixture-owned daemon state. */
|
|
bindDesignBoardState?: boolean;
|
|
/** Require native completion plus this caller-owned final report before accepting a soft terminal. */
|
|
expectedPlanPath?: string;
|
|
/** Additional versioned files available in the isolated fixture before the skill starts. */
|
|
fixtureFiles?: Record<string, string>;
|
|
/** Fixture actor already declined routing setup and cross-project learnings. */
|
|
preconfiguredReviewActor?: boolean;
|
|
/** Hard cap on review-phase count; helper returns when reached. Should be
|
|
* set ABOVE the test's assertion ceiling so the test sees the cap as a
|
|
* failure rather than a silent stop. */
|
|
reviewCountCeiling: number;
|
|
/** Numbered option to press by default. Defaults to 1 (recommended). */
|
|
defaultPick?: number;
|
|
/**
|
|
* Optional override for the FIRST AUQ observed. Receives the fingerprint;
|
|
* returns the option index to press. Subsequent review AUQs use defaultPick;
|
|
* only the recognized optional office-hours prerequisite is declined by label.
|
|
*
|
|
* Skill-specific routing helper: /plan-ceo-review's first AUQ asks "what
|
|
* scope?" with options like "branch diff" / "describe inline" / "skip
|
|
* interview". Pressing the default 1 routes to "branch diff" (the wrong
|
|
* review target for a seeded fixture). firstAUQPick lets the test pick
|
|
* "Skip interview" or "describe inline" so the agent reviews the
|
|
* fixture plan content, not the git diff.
|
|
*/
|
|
firstAUQPick?: (fp: AskUserQuestionFingerprint) => number;
|
|
/** Total budget including startup and cleanup. Must exceed the 5s cleanup reserve. Default 1_500_000. */
|
|
timeoutMs?: number;
|
|
startupReadyMarker?: string;
|
|
/** Extra env merged into the spawned `claude` process. */
|
|
env?: Record<string, string>;
|
|
/** Override the spawned model. Defaults via launchClaudePty's chain. */
|
|
model?: string;
|
|
/** Launch seam and clock; tests pass the fake driver. Default: real launcher and clocks. */
|
|
driver?: PtyDriver;
|
|
}
|
|
|
|
/**
|
|
* Drive a plan-* skill in plan mode and count distinct native review-phase
|
|
* AskUserQuestions until a terminal signal fires. Each run disables the
|
|
* extra outside review in its own gstack config: independent reviewers can
|
|
* add valid findings unrelated to the seeded-N cadence band. These counts
|
|
* do not assert outside-review dispatch or its approval-question cadence.
|
|
*
|
|
* Flow:
|
|
* 1. Seed the complete fixture request in an isolated git repository's
|
|
* PLAN.md and initial CLAUDE.md context, with owned native-only config,
|
|
* then boot the PTY in that cwd
|
|
* (8s grace + auto-trust dialog). Skills remain registered in user scope.
|
|
* 2. Send `slashCommand` alone. The fixture is already in context; sending
|
|
* it later can queue it behind the skill's first question while the
|
|
* review incorrectly starts against the operator's live branch.
|
|
* 3. Poll loop:
|
|
* - Skip permission dialogs (auto-grant with `defaultPick`).
|
|
* - Read fixture-scoped native JSONL. Count each AskUserQuestion call
|
|
* once after its matching successful answer record, regardless of
|
|
* how many questions the call batches. Full native metadata feeds
|
|
* phase predicates; ANSI redraws and permissions cannot add counts.
|
|
* - On a new numbered-option list, keep the existing PTY answer driver.
|
|
* Decline only the recognized optional office-hours
|
|
* prerequisite by label so a different skill does not rewrite the
|
|
* seeded plan before this review begins.
|
|
* - After the native answer, evaluate `isLastStep0AUQ(fingerprint)`. If true,
|
|
* subsequent AUQs are review-phase unless the caller positively identifies native setup.
|
|
* - Hard ceiling: if `reviewCount >= reviewCountCeiling`, return
|
|
* `ceiling_reached`. This bounds runaway counts; tests should set
|
|
* the ceiling above their assertion CEILING.
|
|
* - Soft terminals: `COMPLETION_SUMMARY_RE` match → `completion_summary`;
|
|
* plan-ready confirmation → `plan_ready`; silent write outside
|
|
* sanctioned dirs → `silent_write`; process exited → `exited`;
|
|
* wall clock exceeded → `timeout`.
|
|
*
|
|
* Boundary detection (D14): event-based, fired against the answered AUQ's
|
|
* fingerprint, not against later rendered content. This avoids the race
|
|
* where Step-0-final and Section-1-first AUQs straddle a section header
|
|
* regex match.
|
|
*
|
|
* UI fingerprints still dedupe redraws. Counted fingerprints use the
|
|
* native session/tool call IDs, so shared answer labels never collapse
|
|
* different calls and one multi-question call remains one finding.
|
|
*/
|
|
export async function runPlanSkillCounting(opts: PlanSkillCountingOptions): Promise<PlanSkillCountObservation> {
|
|
const driver = opts.driver ?? realPtyDriver;
|
|
if (opts.requireNativePicker && !opts.pickAUQ)
|
|
throw Error('Native picker binding requires a declared picker');
|
|
if (opts.bindDesignBoardState && (opts.skillName !== 'plan-design-review' || !opts.pickAUQ))
|
|
throw Error('Design board state binding requires the Design caller and its declared picker');
|
|
if (opts.approveEngTestPlanEdits && (opts.skillName !== 'plan-eng-review' || !opts.expectedPlanPath))
|
|
throw Error('Eng test-plan approval requires the Eng caller and its explicit report');
|
|
if (opts.isCollectionComplete && opts.expectedPlanPath)
|
|
throw Error('Collection-only completion cannot replace the final report contract');
|
|
const budgetStarted = driver.monotonic();
|
|
const startedAt = driver.now();
|
|
const timeoutMs = opts.timeoutMs ?? 1_500_000;
|
|
if (opts.startupReadyMarker !== undefined && !opts.startupReadyMarker.length) {
|
|
throw new RangeError('Plan counting startup-ready marker must not be empty');
|
|
}
|
|
// The caller may use this same limit as its Bun timeout. Leave room for
|
|
// close()'s 2s graceful + 1s forced exit waits and artifact/fixture cleanup.
|
|
// A second work window after boot lets Bun retry while this body is alive.
|
|
const cleanupReserveMs = 5_000;
|
|
if (!Number.isFinite(timeoutMs) || timeoutMs <= cleanupReserveMs) {
|
|
throw new RangeError('Plan counting timeout must exceed the 5000ms cleanup reserve');
|
|
}
|
|
const workDeadline = budgetStarted + timeoutMs - cleanupReserveMs;
|
|
const fixture = createPlanCountFixture(opts.followUpPrompt, { nativeReviewOnly: true,
|
|
files: opts.fixtureFiles, preconfiguredReviewActor: opts.preconfiguredReviewActor });
|
|
const run: CountingRun = {
|
|
opts, driver, startedAt, timeoutMs, cleanupReserveMs, workDeadline, fixture, defaultPick: opts.defaultPick ?? 1,
|
|
pickerContext: Object.freeze({cwd: fixture.cwd, deadlineAt: startedAt + timeoutMs - cleanupReserveMs}),
|
|
saveSnapshot: createPlanCountSnapshotWriter(), fingerprints: [], planningDirectory: undefined,
|
|
seen: new Set(), countedCalls: new Set(), filePermission: createPlanCountPermissionGuard(), ownedFilePermissions: [],
|
|
lastMatchedNativeQuestion: undefined, transcript: { status: 'missing', calls: [], assistantMessages: [] },
|
|
boundaryFired: false, step0Count: 0, reviewCount: 0, administrativeCount: 0, isFirstAUQ: true,
|
|
lastCheckpointAt: 0, viewport: '', observedOutput: 0, lastObservationAt: -Infinity,
|
|
};
|
|
const permissionPaths = [
|
|
...(opts.expectedPlanPath ? [opts.expectedPlanPath, path.join(fixture.cwd, 'PLAN.md')] : []),
|
|
...(opts.permissionPlanPath ? [opts.permissionPlanPath] : []),
|
|
];
|
|
return runPtySession<PlanSkillCountObservation>({
|
|
driver,
|
|
cleanup: () => fixture.cleanup(),
|
|
launch: {
|
|
permissionMode: 'plan',
|
|
cwd: fixture.cwd,
|
|
// Stop new output at the work cutoff so screen drain cannot consume
|
|
// the reserve while the CLI continues streaming.
|
|
timeoutMs: Math.max(1, remainingWork(run)),
|
|
screenDeadlineAt: workDeadline,
|
|
env: { ...opts.env, ...fixture.env,
|
|
// The renderer may cd into its artifact directory before starting the daemon.
|
|
...(opts.bindDesignBoardState ? { DESIGN_DAEMON_STATE_FILE: path.join(fixture.cwd, '.gstack', 'design.json') } : {}),
|
|
},
|
|
model: opts.model,
|
|
seedSkills: true,
|
|
observeScreen: true,
|
|
observePlanReady: true,
|
|
observeSetupQuestions: opts.observeSetupQuestions,
|
|
observeFilePermissions: permissionPaths.length ? [...new Set(permissionPaths)] : undefined,
|
|
...(opts.approveEngTestPlanEdits ? { observeAutoplanArtifacts: true, approveAutoplanArtifactEdits: true, engTestPlanArtifactOnly: true } : {}),
|
|
},
|
|
start: session => countingStart(run, session),
|
|
poll: session => countingPoll(run, session),
|
|
tick: session => countingTick(run, session),
|
|
timeout: session => countingSnapshot(run, session, 'timeout',
|
|
`no terminal outcome within ${timeoutMs}ms total budget (including startup and ${cleanupReserveMs}ms cleanup reserve; step0=${run.step0Count}, review=${run.reviewCount})`,
|
|
run.viewport),
|
|
onError: (session, error) => countingFailure(run, session, error),
|
|
onCloseError: (session, error) => {
|
|
countingCapture(run, session, { state: 'cleanup_failed', error: String(error), transcript: run.transcript, fingerprints: run.fingerprints });
|
|
},
|
|
});
|
|
}
|
|
|
|
/** Mutable state of one counting attempt, shared by its session-loop steps. */
|
|
export interface CountingRun {
|
|
opts: PlanSkillCountingOptions;
|
|
driver: PtyDriver;
|
|
startedAt: number;
|
|
timeoutMs: number;
|
|
cleanupReserveMs: number;
|
|
/** Monotonic work cutoff: budget start + timeout - cleanup reserve. */
|
|
workDeadline: number;
|
|
fixture: { cwd: string };
|
|
defaultPick: number;
|
|
pickerContext: Readonly<{ cwd: string; deadlineAt: number }>;
|
|
saveSnapshot: ReturnType<typeof createPlanCountSnapshotWriter>;
|
|
fingerprints: AskUserQuestionFingerprint[];
|
|
planningDirectory: string | undefined;
|
|
seen: Set<string>;
|
|
countedCalls: Set<string>;
|
|
filePermission: ReturnType<typeof createPlanCountPermissionGuard>;
|
|
// Each owned path keeps its own grant history when the workflow switches
|
|
// between the active plan and the separate caller-owned final report.
|
|
ownedFilePermissions: Array<{ expected: string; file: string; guard: ReturnType<typeof createPlanCountPermissionGuard> }>;
|
|
lastMatchedNativeQuestion: NativePlanQuestionCall | undefined;
|
|
transcript: PlanCountTranscript;
|
|
boundaryFired: boolean;
|
|
step0Count: number;
|
|
reviewCount: number;
|
|
administrativeCount: number;
|
|
isFirstAUQ: boolean;
|
|
lastCheckpointAt: number;
|
|
viewport: string;
|
|
observedOutput: number;
|
|
lastObservationAt: number;
|
|
}
|
|
|
|
function remainingWork(run: CountingRun): number {
|
|
return Math.max(0, run.workDeadline - run.driver.monotonic());
|
|
}
|
|
|
|
async function waitForWork(run: CountingRun, ms: number): Promise<boolean> {
|
|
const remaining = remainingWork(run);
|
|
if (remaining <= 0) return false;
|
|
const clipped = ms >= remaining;
|
|
await run.driver.sleep(Math.min(ms, remaining));
|
|
// A clipped wait cannot finish the requested interval. Timers may wake
|
|
// just before the fractional deadline; that is no license to advance.
|
|
return !clipped && remainingWork(run) > 0;
|
|
}
|
|
|
|
/** Durable observation with public tool events and pending owned write inputs. */
|
|
export function countingCapture(run: CountingRun, session: Pick<ClaudePtySession, 'hermeticConfigDir' | 'rawOutput' | 'visibleText'>,
|
|
observation: object) {
|
|
const publicTools: NativePublicToolEvent[] = [];
|
|
if (session.hermeticConfigDir) readPlanCountTranscript(session.hermeticConfigDir, run.fixture.cwd,
|
|
event => publicTools.push(event));
|
|
return run.saveSnapshot({
|
|
skillName: run.opts.skillName, observation: { ...observation,
|
|
publicTools,
|
|
pendingWriteInputs: run.ownedFilePermissions.flatMap(binding => {
|
|
const input = readPendingWriteInput(binding.file, binding.expected, run.fixture.cwd, session.hermeticConfigDir, run.startedAt);
|
|
return input ? [input] : [];
|
|
}) }, raw: session.rawOutput(), visible: session.visibleText(), viewport: run.viewport,
|
|
cwd: run.fixture.cwd, claudeConfigDir: session.hermeticConfigDir,
|
|
});
|
|
}
|
|
|
|
function countingSnapshot(
|
|
run: CountingRun,
|
|
session: ClaudePtySession,
|
|
outcome: PlanSkillCountObservation['outcome'],
|
|
summary: string,
|
|
visible: string,
|
|
): PlanSkillCountObservation {
|
|
const clean = (text: string) => stripVTControlCharacters(text)
|
|
.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, '');
|
|
const failed = outcome === 'exited' || outcome === 'timeout';
|
|
const observation: PlanSkillCountObservation = {
|
|
outcome,
|
|
summary,
|
|
evidence: failed
|
|
? `exitCode=${session.exitCode()}\n--- post-command evidence (last 3KB) ---\n${clean(visible).slice(-3000)}` +
|
|
`\n--- full-session evidence, including startup (last 6KB) ---\n${clean(session.visibleText()).slice(-6000)}`
|
|
: visible.slice(-3000),
|
|
elapsedMs: run.driver.now() - run.startedAt,
|
|
fingerprints: run.fingerprints,
|
|
transcript: run.transcript,
|
|
step0Count: run.step0Count,
|
|
reviewCount: run.reviewCount,
|
|
administrativeCount: run.administrativeCount,
|
|
};
|
|
const artifacts = countingCapture(run, session, observation);
|
|
Object.assign(observation, artifacts);
|
|
if (artifacts.artifactDir) observation.evidence += `\nFull PTY artifacts: ${artifacts.artifactDir}`;
|
|
if (artifacts.artifactError) observation.evidence += `\nPTY artifact write failed: ${artifacts.artifactError}`;
|
|
return observation;
|
|
}
|
|
|
|
async function countingStart(run: CountingRun, session: ClaudePtySession): Promise<undefined> {
|
|
const { opts } = run;
|
|
run.planningDirectory = session.hermeticConfigDir ? path.join(session.hermeticConfigDir, 'plans') : undefined;
|
|
run.ownedFilePermissions = (session.pendingFilePermissionFiles ?? []).map(binding =>
|
|
({ ...binding, guard: createPlanCountPermissionGuard() }));
|
|
run.lastCheckpointAt = run.driver.now();
|
|
run.observedOutput = session.mark();
|
|
let startupReady: boolean;
|
|
if (opts.startupReadyMarker !== undefined) {
|
|
await session.waitFor(opts.startupReadyMarker, { timeoutMs: Math.min(8000, remainingWork(run)) });
|
|
startupReady = remainingWork(run) > 0;
|
|
} else {
|
|
startupReady = await waitForWork(run, 8000);
|
|
}
|
|
if (startupReady) {
|
|
run.observedOutput = session.mark();
|
|
if (opts.approveEngTestPlanEdits) {
|
|
if (!session.startAutoplanArtifactEditApproval) throw Error('Owned Eng test-plan approval hook unavailable');
|
|
session.startAutoplanArtifactEditApproval(run.driver.now());
|
|
}
|
|
session.send(`${opts.slashCommand}\r`);
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
/** Wake on output (or 2s), coalesce redraws for 250ms, then read the viewport. */
|
|
async function countingPoll(run: CountingRun, session: ClaudePtySession): Promise<boolean> {
|
|
if (remainingWork(run) <= 0) return false;
|
|
await session.waitForOutput(run.observedOutput, Math.min(2000, remainingWork(run)));
|
|
if (remainingWork(run) <= 0) return false;
|
|
const coalesceMs = session.rawOutput().length > run.observedOutput
|
|
? 250 : 250 - (run.driver.monotonic() - run.lastObservationAt);
|
|
if (coalesceMs > 0 && !await waitForWork(run, coalesceMs)) return false;
|
|
run.observedOutput = session.mark();
|
|
run.lastObservationAt = run.driver.monotonic();
|
|
run.viewport = await session.currentScreen();
|
|
return remainingWork(run) > 0;
|
|
}
|
|
|
|
async function countingTick(run: CountingRun, session: ClaudePtySession): Promise<PtyStep<PlanSkillCountObservation>> {
|
|
const visible = run.viewport;
|
|
const observed = countingObserve(run, session, visible);
|
|
if (observed) return observed;
|
|
const { opts } = run;
|
|
const transcript = run.transcript;
|
|
// A native AUQ can render before its JSONL tool-use record is flushed.
|
|
// The opt-in hook supplies pending identity only; answered counts above
|
|
// still come exclusively from the published native transcript.
|
|
const pending = transcript.calls.find(c => !c.answered && !c.failed)
|
|
?? readPendingQuestion(session.pendingQuestionFile, run.fixture.cwd,
|
|
session.hermeticConfigDir, run.startedAt, transcript);
|
|
// Native ACKs → complete collection → caller validation. A process failure
|
|
// above or a pending native question still prevents this early collection stop.
|
|
if (opts.isCollectionComplete && !pending && transcript.status === 'ready' &&
|
|
transcript.calls.length > 0 && transcript.calls.every(call => call.answered && !call.failed) &&
|
|
!unresolvedPlanQuestionCalls(transcript.calls).length && remainingWork(run) > 0 &&
|
|
opts.isCollectionComplete(transcript, run.fingerprints) && remainingWork(run) > 0) {
|
|
return { done: countingSnapshot(run, session, 'collection_complete', 'Caller-defined native collection is complete; final validation remains required', visible) };
|
|
}
|
|
const terminal = await countingTerminal(run, visible, pending);
|
|
if ('done' in terminal) return { done: countingSnapshot(run, session, terminal.done[0], terminal.done[1], visible) };
|
|
return countingRespond(run, session, visible, pending, terminal);
|
|
}
|
|
|
|
/** Read and count native calls; process-level terminals. Undefined keeps routing. */
|
|
function countingObserve(run: CountingRun, session: ClaudePtySession, visible: string): PtyStep<PlanSkillCountObservation> | undefined {
|
|
const { opts, fixture, startedAt } = run;
|
|
const finish = (outcome: PlanSkillCountObservation['outcome'], summary: string) =>
|
|
({ done: countingSnapshot(run, session, outcome, summary, visible) });
|
|
let transcript: PlanCountTranscript = session.hermeticConfigDir
|
|
? readPlanCountTranscript(session.hermeticConfigDir, fixture.cwd)
|
|
: { status: 'error', calls: [], assistantMessages: [], error: 'Claude count session has no isolated transcript directory' };
|
|
transcript = run.transcript = withPendingExit(transcript, session.pendingPlanReadyFile, fixture.cwd,
|
|
session.hermeticConfigDir, startedAt, visible);
|
|
if (opts.approveEngTestPlanEdits) {
|
|
const status = autoplanArtifactRecorderStatus(session.pendingAutoplanArtifactFile, fixture.cwd,
|
|
session.hermeticConfigDir, session.autoplanArtifactStateRoot);
|
|
const boundary = autoplanArtifactApprovalBoundary(status);
|
|
if (boundary === 'failed') return finish('artifact_permission_failed',
|
|
`Owned Eng QA test-plan approval failed: ${JSON.stringify(status)}`);
|
|
// Native approval owns this Edit. Never answer its repaint or count a
|
|
// metadata-only pending request; resume only after its actual result.
|
|
if (boundary === 'pending') {
|
|
if (run.driver.now() - run.lastCheckpointAt >= 30_000) {
|
|
run.lastCheckpointAt = run.driver.now();
|
|
const saved = countingCapture(run, session, { state: 'artifact_pending', elapsedMs: run.driver.now() - startedAt,
|
|
fingerprints: run.fingerprints, step0Count: run.step0Count, reviewCount: run.reviewCount,
|
|
administrativeCount: run.administrativeCount, transcript, artifactStatus: status });
|
|
if (saved.artifactError) console.error(`PTY artifact write failed: ${saved.artifactError}`);
|
|
}
|
|
return 'continue';
|
|
}
|
|
}
|
|
if (transcript.status === 'error') return finish('transcript_unavailable', transcript.error!);
|
|
countAnsweredCalls(run, transcript);
|
|
if (run.reviewCount >= opts.reviewCountCeiling) {
|
|
if (unresolvedPlanQuestionCalls(transcript.calls).length) {
|
|
return finish('transcript_unavailable', 'Question count reached its ceiling with unresolved failed native calls');
|
|
}
|
|
return finish('ceiling_reached', `review-phase AUQ count reached ceiling (${opts.reviewCountCeiling})`);
|
|
}
|
|
// An outer test timeout/cancellation may prevent a terminal snapshot.
|
|
// Keep bounded-cadence evidence without adding a timer to clean up.
|
|
if (run.driver.now() - run.lastCheckpointAt >= 30_000) {
|
|
run.lastCheckpointAt = run.driver.now();
|
|
const saved = countingCapture(run, session, { state: 'in_progress', elapsedMs: run.driver.now() - startedAt,
|
|
fingerprints: run.fingerprints, step0Count: run.step0Count, reviewCount: run.reviewCount,
|
|
administrativeCount: run.administrativeCount, transcript });
|
|
if (saved.artifactError) console.error(`PTY artifact write failed: ${saved.artifactError}`);
|
|
}
|
|
if (session.exited()) {
|
|
return finish('exited', `claude exited (code=${session.exitCode()}) during counting (step0=${run.step0Count}, review=${run.reviewCount})`);
|
|
}
|
|
if (isRejectedSlashCommand(visible, opts.slashCommand)) {
|
|
return finish('exited', `claude rejected ${opts.slashCommand} as unknown command (skill not registered in this cwd)`);
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
/** Count each answered native call once; phase comes from the caller's predicates. */
|
|
function countAnsweredCalls(run: CountingRun, transcript: PlanCountTranscript): void {
|
|
const { opts } = run;
|
|
for (const [callIndex, call] of transcript.calls.entries()) {
|
|
const signature = `${call.sessionId}:${call.toolUseId}`;
|
|
if (!call.answered || run.countedCalls.has(signature)) continue;
|
|
const fp = nativePlanCallFingerprint(call, run.driver.now() - run.startedAt, !run.boundaryFired);
|
|
const phase = planCountQuestionPhase(fp, run.boundaryFired, opts.isLastStep0AUQ, opts.isFirstReviewAUQ, opts.isSetupAUQ, opts.isCompletionHandoffAUQ, opts.isArtifactGenerationAUQ);
|
|
if (phase.administrative) {
|
|
fp.preReview = false;
|
|
fp.administrative = phase.administrative;
|
|
run.administrativeCount += 1;
|
|
} else {
|
|
fp.preReview = opts.isReviewAUQ ? !opts.isReviewAUQ(fp, transcript.calls.slice(0, callIndex)) : phase.preReview;
|
|
if (fp.preReview) run.step0Count += 1;
|
|
else run.reviewCount += 1;
|
|
}
|
|
fp.promptSnippet = fp.promptSnippet.replace(/\s+/g, ' ').slice(0, 240);
|
|
run.fingerprints.push(fp);
|
|
run.countedCalls.add(signature);
|
|
run.boundaryFired = phase.reviewStarted;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* A long completed summary may scroll its heading off the viewport. With no
|
|
* active input UI, retain the existing native/report validator; neither
|
|
* display text nor a missing heading supplies completion evidence.
|
|
*/
|
|
export function isNativeCompletionSummary(input: { expectedPlanPath?: string; nativeCompletion: boolean;
|
|
renderedFrame: ReturnType<typeof classifyPlanCountFrame>; visible: string; transcript: PlanCountTranscript; startedAt: number;
|
|
administrative: ReadonlySet<string> }): boolean {
|
|
return Boolean(!input.nativeCompletion && input.renderedFrame === null && input.expectedPlanPath &&
|
|
!isNumberedOptionListVisible(input.visible) && !isPermissionDialogVisible(input.visible) && !isProseAUQVisible(input.visible) &&
|
|
hasNativePlanTerminal(input.transcript, input.expectedPlanPath, input.startedAt, 'completion_summary', input.administrative));
|
|
}
|
|
|
|
interface CountingTerminal {
|
|
newlyMatched: boolean;
|
|
nativeCompletion: boolean;
|
|
verifiedTerminal: boolean;
|
|
nativeQuestionVisible: boolean;
|
|
terminalHint: 'permission' | 'completion_summary' | 'plan_ready' | null;
|
|
frame: 'permission' | 'completion_summary' | 'plan_ready' | null;
|
|
}
|
|
|
|
/** Terminal evidence for this frame, before any input is routed. */
|
|
async function countingTerminal(run: CountingRun, visible: string, pending: NativePlanQuestionCall | undefined):
|
|
Promise<CountingTerminal | { done: [PlanSkillCountObservation['outcome'], string] }> {
|
|
const { opts, startedAt, transcript, fingerprints } = run;
|
|
const newlyMatched = Boolean(pending && matchesNativePlanQuestion(visible, pending, run.planningDirectory));
|
|
if (newlyMatched) run.lastMatchedNativeQuestion = pending;
|
|
const renderedFrame = classifyPlanCountFrame(visible);
|
|
const administrative = new Set(fingerprints.filter(fp => fp.administrative === 'completion-handoff').map(fp => fp.signature));
|
|
if (opts.evaluateTerminal && opts.expectedPlanPath && renderedFrame === 'plan_ready' && !newlyMatched) {
|
|
const reviewed = await evaluateOwnedNativePlanTerminal(transcript, opts.expectedPlanPath, startedAt,
|
|
startedAt + run.timeoutMs - run.cleanupReserveMs, opts.evaluateTerminal);
|
|
if (reviewed) {
|
|
// Once semantics are validated, lexical phase labels have no veto.
|
|
// The earlier progress snapshots remain unchanged diagnostic evidence.
|
|
administrative.clear();
|
|
for (const identity of reviewed.administrative) administrative.add(identity);
|
|
for (const fp of fingerprints) {
|
|
fp.preReview = !reviewed.substantive.has(fp.signature) && !administrative.has(fp.signature);
|
|
if (administrative.has(fp.signature)) fp.administrative = 'completion-handoff';
|
|
else delete fp.administrative;
|
|
}
|
|
run.reviewCount = reviewed.substantive.size;
|
|
run.administrativeCount = administrative.size;
|
|
run.step0Count = fingerprints.length - run.reviewCount - run.administrativeCount;
|
|
}
|
|
}
|
|
const nativeCompletion = Boolean(opts.expectedPlanPath && hasNativePlanCompletion(transcript, opts.expectedPlanPath, startedAt));
|
|
const nativeSummary = isNativeCompletionSummary({ expectedPlanPath: opts.expectedPlanPath, nativeCompletion,
|
|
renderedFrame, visible, transcript, startedAt, administrative });
|
|
const terminalFrame = nativeSummary ? 'completion_summary' : renderedFrame;
|
|
const isTerminalHint = terminalFrame === 'completion_summary' || terminalFrame === 'plan_ready';
|
|
const verifiedTerminal = nativeSummary || Boolean(opts.expectedPlanPath && isTerminalHint &&
|
|
hasNativePlanTerminal(transcript, opts.expectedPlanPath, startedAt, terminalFrame, administrative));
|
|
if (run.reviewCount === 0 && fingerprints.some(fp => fp.administrative === 'artifact-generation') &&
|
|
(nativeCompletion || verifiedTerminal)) {
|
|
return { done: ['no_review_questions', 'Completed artifact generation supplied no review finding decisions'] };
|
|
}
|
|
// A streamed heading cannot dismiss the last bound native question.
|
|
// Only an accepted terminal may supersede its answered redraw; otherwise
|
|
// permission wording inside that question could queue a stray answer.
|
|
const acceptedTerminal = isTerminalHint && (!opts.expectedPlanPath || verifiedTerminal);
|
|
const nativeQuestionVisible = newlyMatched || (!acceptedTerminal &&
|
|
Boolean(run.lastMatchedNativeQuestion && matchesNativePlanQuestion(visible, run.lastMatchedNativeQuestion, run.planningDirectory)));
|
|
const terminalHint = nativeQuestionVisible ? null : terminalFrame;
|
|
let frame = terminalHint;
|
|
// Clear unverified hints before routing active permissions and Submit.
|
|
if (opts.expectedPlanPath && isTerminalHint && !verifiedTerminal) frame = null;
|
|
// A real approval gate is never an AUQ or a file permission. Wait for
|
|
// its report/native evidence before input routing; verified gates still
|
|
// pass the existing silent-write check below. A positively matched
|
|
// native question above takes precedence over gate text.
|
|
const nonReviewCalls = new Set(fingerprints.filter(fp => fp.preReview || fp.administrative)
|
|
.map(fp => fp.signature));
|
|
if (opts.expectedPlanPath && terminalHint === 'plan_ready' && run.reviewCount === 0 &&
|
|
isQuestionlessNativePlanExit(transcript, opts.expectedPlanPath, startedAt, visible, nonReviewCalls)) {
|
|
return { done: ['no_review_questions',
|
|
'Native plan approval reached with zero review-phase AskUserQuestion calls; review coverage is missing'] };
|
|
}
|
|
return { newlyMatched, nativeCompletion, verifiedTerminal, nativeQuestionVisible, terminalHint, frame };
|
|
}
|
|
|
|
/** Route permissions, Submit and the current question; then terminal outcomes. */
|
|
async function countingRespond(run: CountingRun, session: ClaudePtySession, visible: string,
|
|
pending: NativePlanQuestionCall | undefined, t: CountingTerminal): Promise<PtyStep<PlanSkillCountObservation>> {
|
|
const { opts, fixture, startedAt, transcript } = run;
|
|
const finish = (outcome: PlanSkillCountObservation['outcome'], summary: string) =>
|
|
({ done: countingSnapshot(run, session, outcome, summary, visible) });
|
|
const { frame } = t;
|
|
if (opts.expectedPlanPath && t.terminalHint === 'plan_ready' && !t.verifiedTerminal) return 'continue';
|
|
let permissionGuard = run.filePermission;
|
|
let permissionEpoch: FilePermissionEpoch | null | undefined;
|
|
const currentBinding = currentFilePermissionBinding(run.ownedFilePermissions, fixture.cwd,
|
|
session.hermeticConfigDir, startedAt, transcript, visible);
|
|
if (currentBinding) { permissionGuard = currentBinding.binding.guard; permissionEpoch = currentBinding.epoch; }
|
|
else permissionEpoch = currentBinding;
|
|
const permission = t.nativeQuestionVisible || t.terminalHint === 'plan_ready'
|
|
? null : permissionGuard(visible, session.visibleText(), permissionEpoch);
|
|
if (frame === 'permission' || (permission === 'grant' && frame === null)) {
|
|
if (remainingWork(run) <= 0) return 'break';
|
|
if (permission !== 'handled') session.send(`${run.defaultPick}\r`);
|
|
await waitForWork(run, 1500);
|
|
return 'continue';
|
|
}
|
|
|
|
const submissionInput = frame === null ? planCountSubmissionInput(visible) : null;
|
|
if (submissionInput !== null) {
|
|
if (remainingWork(run) <= 0) return 'break';
|
|
session.send(submissionInput);
|
|
await waitForWork(run, 1500);
|
|
return 'continue';
|
|
}
|
|
|
|
// Silent write detection — only fires if no numbered prompt is on
|
|
// screen (otherwise the write is gated by a permission/AUQ).
|
|
const writeRe = /⏺\s*(?:Write|Edit)\(([^)]+)\)/g;
|
|
let m: RegExpExecArray | null;
|
|
while ((m = writeRe.exec(visible)) !== null) {
|
|
const target = m[1] ?? '';
|
|
const sanctioned = SANCTIONED_WRITE_SUBSTRINGS.some((s) =>
|
|
target.includes(s),
|
|
);
|
|
if (!sanctioned && !isNumberedOptionListVisible(visible)) {
|
|
return finish('silent_write', `Write/Edit to ${target} fired before any AskUserQuestion`);
|
|
}
|
|
}
|
|
|
|
if (opts.expectedPlanPath && t.verifiedTerminal && (frame === 'completion_summary' || frame === 'plan_ready')) {
|
|
return finish(frame, `native ${frame} and final report verified (step0=${run.step0Count}, review=${run.reviewCount})`);
|
|
}
|
|
|
|
// Legacy callers without a report contract retain their terminal policy.
|
|
if (frame === 'completion_summary' || frame === 'plan_ready') {
|
|
if (transcript.status !== 'ready' || transcript.calls.some(c => !c.answered && !c.failed) ||
|
|
unresolvedPlanQuestionCalls(transcript.calls).length) {
|
|
return finish('transcript_unavailable', frame === 'completion_summary'
|
|
? 'Completion has no complete native question transcript'
|
|
: 'Plan-ready gate has no complete native question transcript');
|
|
}
|
|
return finish(frame, frame === 'completion_summary'
|
|
? `skill emitted completion summary / verdict / status line (step0=${run.step0Count}, review=${run.reviewCount})`
|
|
: `skill emitted plan-mode "Ready to execute" confirmation (step0=${run.step0Count}, review=${run.reviewCount})`);
|
|
}
|
|
|
|
if (opts.expectedPlanPath && !transcript.planReadyRequests?.length && !isNumberedOptionListVisible(visible) &&
|
|
hasNativePlanCompletion(transcript, opts.expectedPlanPath, startedAt)) {
|
|
return finish('completion_summary',
|
|
`native review completion and final report verified (step0=${run.step0Count}, review=${run.reviewCount})`);
|
|
}
|
|
|
|
// A dismissed or repainted permission is never a native question.
|
|
if (permission === 'handled') return 'continue';
|
|
return countingAnswer(run, session, visible, pending, t.newlyMatched);
|
|
}
|
|
|
|
/** Dedupe the complete question and press its policy-selected option. */
|
|
async function countingAnswer(run: CountingRun, session: ClaudePtySession, visible: string,
|
|
pending: NativePlanQuestionCall | undefined, newlyMatched: boolean): Promise<PtyStep<PlanSkillCountObservation>> {
|
|
const { opts, startedAt } = run;
|
|
// Dedupe the complete question, not just its answer labels: separate
|
|
// findings often reuse the same Add to plan / Defer / Skip menu.
|
|
if (opts.requireNativePicker && !newlyMatched) return 'continue';
|
|
const capturedSeen = opts.requireNativePicker ? new Set(run.seen) : run.seen;
|
|
const fp = capturePlanCountQuestion(visible, capturedSeen, run.driver.now() - startedAt, !run.boundaryFired, pending, run.planningDirectory);
|
|
if (!fp) return 'continue';
|
|
const boundNativeTab = pending && fp.nativeCall === pending && fp.nativeQuestionIndex !== undefined;
|
|
if (opts.requireNativePicker && !boundNativeTab) return 'continue';
|
|
// Press to advance — first AUQ may use the override pick.
|
|
const routing = pending?.questions.length === 1
|
|
? nativePlanCallFingerprint(pending, fp.observedAtMs, fp.preReview) : fp;
|
|
const prerequisitePick = planCountPrerequisitePick(routing, fp);
|
|
// Native tool records may flush only after the answer. Let a guarded
|
|
// caller recognize that visible menu. A known packet needs a positively
|
|
// matched active tab before a caller can change that tab's choice.
|
|
// The captured fingerprint alone proves whether native metadata matched
|
|
// this active UI; an unrelated pending record is not a routing identity.
|
|
let callerPick: number | null = null;
|
|
if ((!opts.requireNativePicker || prerequisitePick === null) &&
|
|
(!pending || pending.questions.length === 1 || boundNativeTab)) {
|
|
callerPick = opts.pickAUQ?.(routing, fp, run.pickerContext) ?? null;
|
|
}
|
|
if (opts.requireNativePicker && prerequisitePick === null && callerPick === null)
|
|
throw Error('Declared native picker returned no authorized choice');
|
|
const pickIdx = prerequisitePick ?? callerPick ??
|
|
(run.isFirstAUQ && opts.firstAUQPick ? opts.firstAUQPick(routing) : run.defaultPick);
|
|
if (opts.requireNativePicker) for (const signature of capturedSeen) run.seen.add(signature);
|
|
run.isFirstAUQ = false;
|
|
const questionInput = planCountQuestionInput(visible, fp, pickIdx);
|
|
if (remainingWork(run) <= 0) return 'break';
|
|
if (questionInput.includes('\r')) {
|
|
// The helper separates digit and Enter by 500ms. Do not let that
|
|
// delayed confirmation send input after this counting window closes.
|
|
await selectPtyNumberedOption({ send: input => {
|
|
if (remainingWork(run) > 0) session.send(input);
|
|
} }, pickIdx);
|
|
} else session.send(questionInput);
|
|
|
|
// Give the agent a beat to advance to the next state.
|
|
await waitForWork(run, 2000);
|
|
return 'continue';
|
|
}
|
|
|
|
/** Caller/actor errors used to leave only the preceding 30s checkpoint.
|
|
* Retain the actual throw frame and public native state before close()
|
|
* removes the hook and fixture, without replacing the original failure. */
|
|
function countingFailure(run: CountingRun, session: ClaudePtySession, error: unknown): void {
|
|
try {
|
|
// Keep the exact frame/transcript that the throwing caller observed;
|
|
// awaiting a redraw here would erase that ordering evidence.
|
|
const saved = countingCapture(run, session, { state: 'threw', error: error instanceof Error ? error.message : String(error),
|
|
elapsedMs: run.driver.now() - run.startedAt, fingerprints: run.fingerprints, step0Count: run.step0Count,
|
|
reviewCount: run.reviewCount, administrativeCount: run.administrativeCount, transcript: run.transcript,
|
|
pendingQuestion: readPendingQuestion(session.pendingQuestionFile, run.fixture.cwd,
|
|
session.hermeticConfigDir, run.startedAt, run.transcript) });
|
|
if (saved.artifactDir) console.error(`Full PTY artifacts: ${saved.artifactDir}`);
|
|
if (saved.artifactError) console.error(`PTY artifact write failed: ${saved.artifactError}`);
|
|
} catch (captureError) { console.error(`PTY failure capture failed: ${String(captureError)}`); }
|
|
}
|