Skip to content

Use Garry Tan's exact Claude Code setup: 23 opinionated tools that serve as CEO, Designer, Eng Manager, Release Manager, Doc Engineer, and QA

TypeScript
7.4 KB
Raw Blame
/**
 * /autoplan publication guard, live (gate tier, rule kind, one trial, haiku).
 *
 * On the pinned Claude Code, in an interactive PTY session with default
 * settings, a project skill carrying /autoplan's own hook block enters Phase 1,
 * dispatches the CEO reviewer (each dispatch is a declared token the driver's relay replaces with the exact
 * dispatch, so the actor never retypes it), reads a prepared close packet, publishes the Phase 1 report in a message
 * whose only tool call is `true autoplan-published ceo`, enters Phase 2 in a
 * later message and dispatches the Phase 2 reviewer. The reviewer dispatch sends run_in_background, which the
 * 2.1.29x default fork-subagent schema strips. Claude Code 2.1.29x can write a tool call to the journal only
 * after PreToolUse returns, so this is where the guard's payload path and its
 * flush rule meet the real CLI. The outcome is read from the guard decision
 * log, never the screen.
 *
 * The reviewer runs in the background, so its completion notice starts a new
 * turn and Claude Code stops applying the skill's allowed tools. Every later
 * Read of /autoplan's own files (the close packet and the Phase 2 reviewer's
 * input in the project's .gstack/tmp/autoplan/, the installed Phase 2 section)
 * must still raise no permission card.
 *
 * The actor is Claude Haiku 5.5. It acts out a scripted turn shape the guard
 * depends on and is not a model under test, so the wait for the CEO reviewer's
 * notice is the relay's, not the model's (GST-3441): the relay ends the actor's
 * turn after the background dispatch until that notice arrives. Without it, Haiku
 * 5.5 read the close packet and dispatched Phase 2 before the notice, so the
 * notice-turn path never ran: 8 of 8 attempts failed in release gate runs
 * 44952328945, 44955253712 and 44956497475 (#4639 pinned Haiku 4.5 meanwhile).
 *
 * The relay is a gateway to Claude Code, so auto mode's server-side checks cannot reach the session and the first
 * reviewer action the local classifier checks waits on the "auto mode classifier billing" notice for Enter. The
 * reviewer then never finishes and its notice never comes (run gsta796-guard-1). CLAUDE_CODE_AUTO_MODE_SERVER=0 is
 * the documented setting for a gateway that cannot carry those checks: the same local classifier, with no notice.
 * https://code.claude.com/docs/en/auto-mode-classifier-billing
 */
import { expect } from 'bun:test';
import * as path from 'node:path';
import { describeIfSelected, testIfSelected } from './helpers/e2e-helpers';
import { launchClaudePty, selectPtyNumberedOption } from './helpers/claude-pty-runner';
import { closePacketReadAfterNotice, guardRunNeedsJournal, prepareGuardSession, startDispatchRelay, stoppedAtPhaseBoundary } from './helpers/autoplan-guard-pty';

const MODEL = 'claude-haiku-5-5';

describeIfSelected('autoplan publication guard live PTY', ['autoplan-guard-pty'], () => {
  testIfSelected('autoplan-guard-pty', async () => {
    if (!process.env.ANTHROPIC_API_KEY) throw new Error('autoplan-guard-pty requires ANTHROPIC_API_KEY; refusing to skip');
    const s = prepareGuardSession(process.env.ANTHROPIC_API_KEY);
    const relay = startDispatchRelay(path.join(s.project, '.gstack', 'tmp', 'autoplan'));
    const session = await launchClaudePty({ permissionMode: null, model: MODEL, cwd: s.project, env: { ...s.env, ANTHROPIC_BASE_URL: relay.url, CLAUDE_CODE_AUTO_MODE_SERVER: '0' },
      timeoutMs: 540_000, observeScreen: true }).catch(error => { relay.stop(); throw error; });
    const started = performance.now();
    const prompts: string[] = [];
    let phase1Ms: number | null = null;
    try {
      await Bun.sleep(5_000);
      session.send(s.prompt); await Bun.sleep(400); session.send('\r');
      const deadline = performance.now() + 480_000;
      while (performance.now() < deadline) {
        await Bun.sleep(1_000);
        const log = s.readLog();
        if (phase1Ms === null && log.length) phase1Ms = Math.round(performance.now() - started);
        // A permission card is recorded and approved so the run can finish; any card fails the case.
        const screen = await session.currentScreen();
        if (/Do you want to proceed\?/.test(screen) && /❯\s*1\.\s*Yes/.test(screen)) {
          prompts.push(screen);
          await selectPtyNumberedOption(session, 1);
          await Bun.sleep(1_500);
        }
        if (log.some(e => e.decision === 'deny') || (log.length >= 4 && s.noticeEvidence().notices >= 2)) break;
        // The actor ended its turn at the Phase 1 publication; no input is coming, so it cannot reach Phase 2.
        if (log.length < 4 && stoppedAtPhaseBoundary(screen)) break;
        if (session.exited()) break;
      }
      await Bun.sleep(3_000);
      const log = s.readLog(), evidence = s.noticeEvidence();
      const summary = { prompts: prompts.length, substituted: relay.substituted, noticeWaits: relay.noticeWaits, noticeWithLaunch: relay.noticeWithLaunch, phase1Ms, totalMs: Math.round(performance.now() - started), evidence,
        decisions: log.map(e => ({ decision: e.decision, code: e.code, path: e.path, version: e.claude_code_version, ts: e.ts })) };
      console.log(`[autoplan-guard-pty] ${JSON.stringify(summary)}`);
      if (log.length < 4 && stoppedAtPhaseBoundary(session.visibleText())) console.log('[autoplan-guard-pty] PHASE_BOUNDARY_STOP: the actor ended its turn after "Phase 1 complete." instead of going on to step 7');
      if (!guardRunNeedsJournal(log, prompts.length) && !closePacketReadAfterNotice(evidence.afterNotice))
        console.log(`[autoplan-guard-pty] journal (no close packet read after the notice):\n${s.journalSummary().join('\n')}`);
      if (guardRunNeedsJournal(log, prompts.length))
        console.log(`[autoplan-guard-pty] journal:\n${s.journalSummary().join('\n')}\n[autoplan-guard-pty] screen:\n${session.visibleText().slice(-2_000)}\n[autoplan-guard-pty] last non-blank screen:\n${session.lastScreen?.() ?? ''}`);
      if (log.some(e => e.code === 'dispatch_prompt')) console.log(`[autoplan-guard-pty] dispatch prompt deltas:\n${s.dispatchPromptDeltas().join('\n')}`);
      for (const prompt of prompts) console.log(`[autoplan-guard-pty] permission card approved:\n${prompt}`);
      expect(log.filter(e => e.decision === 'deny')).toEqual([]);
      // The relay held the actor until the hand-back, or the hand-back came with the launch result and there was nothing to hold.
      expect(relay.noticeWaits + relay.noticeWithLaunch).toBeGreaterThan(0);
      // Phase 1 entry, the CEO reviewer dispatch (Cause A: run_in_background sent, stripped from the payload),
      // the Phase 2 transition and the Phase 2 reviewer dispatch: four guarded allows, each verified.
      expect(log.map(e => [e.decision, e.disposition])).toEqual(Array(4).fill(['allow', 'allow']));
      // The CEO reviewer's notice started a new turn before the close packet, the Phase 2 entry and the Phase 2 dispatch.
      expect(closePacketReadAfterNotice(evidence.afterNotice), evidence.afterNotice.join('\n')).toBe(true);
      expect(evidence.afterNotice.filter(c => /^Read .*autoplan\/sections\/design-phase\.md$/.test(c))).not.toEqual([]);
      expect(evidence.afterNotice).toContain('Agent Design review');
      expect(evidence.reviewerReads.filter(f => /\.gstack\/tmp\/autoplan\/autoplan-design-[^/]+\/native-prompt\.md$/.test(f))).not.toEqual([]);
      expect(prompts).toEqual([]);
    } finally {
      await session.close();
      relay.stop();
      s.cleanup();
    }
  }, 600_000);
});