@hua-labs/tap 0.1.1 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/engine/termination.ts","../../src/engine/review.ts","../../src/engine/headless-loop.ts","../../src/bridges/codex-bridge-runner.ts","../../src/config/resolve.ts","../../src/runtime/resolve-node.ts"],"sourcesContent":["/**\n * Termination engine — decides when a review session should stop.\n *\n * Strategies are evaluated in priority order. First non-\"continue\" verdict wins.\n * Default order: manual-stop → round-cap → repetition → quality → diff-insignificance\n */\nimport * as fs from \"node:fs\";\nimport * as crypto from \"node:crypto\";\n\n// ── Types ──────────────────────────────────────────────────────────\n\nexport type TerminationStrategy =\n | \"diff-insignificance\"\n | \"repetition-detection\"\n | \"quality-threshold\"\n | \"round-cap\"\n | \"manual-stop\";\n\nexport type TerminationVerdict = \"continue\" | \"stop\" | \"escalate\";\n\nexport type FindingSeverity =\n | \"critical\"\n | \"high\"\n | \"medium\"\n | \"low\"\n | \"nitpick\";\n\nexport interface ReviewFinding {\n severity: FindingSeverity;\n category: string;\n description: string;\n file?: string;\n line?: number;\n}\n\nexport interface ReviewRound {\n round: number;\n timestamp: string;\n findingCount: number;\n findings: ReviewFinding[];\n suggestedDiffLines: number;\n findingHash: string;\n}\n\nexport interface TerminationConfig {\n strategies: TerminationStrategy[];\n maxRounds: number;\n diffThreshold: number;\n repetitionThreshold: number;\n qualitySeverityFloor: FindingSeverity;\n}\n\nexport interface TerminationContext {\n round: number;\n rounds: ReviewRound[];\n stopSignalPath: string;\n config: TerminationConfig;\n}\n\nexport interface TerminationResult {\n verdict: TerminationVerdict;\n reason: string;\n strategy: TerminationStrategy;\n summary: string;\n}\n\n// ── Defaults ───────────────────────────────────────────────────────\n\nexport const DEFAULT_TERMINATION_CONFIG: TerminationConfig = {\n strategies: [\n \"manual-stop\",\n \"round-cap\",\n \"repetition-detection\",\n \"quality-threshold\",\n \"diff-insignificance\",\n ],\n maxRounds: 5,\n diffThreshold: 3,\n repetitionThreshold: 2,\n qualitySeverityFloor: \"high\",\n};\n\n// ── Severity ranking ───────────────────────────────────────────────\n\nconst SEVERITY_RANK: Record<FindingSeverity, number> = {\n critical: 5,\n high: 4,\n medium: 3,\n low: 2,\n nitpick: 1,\n};\n\nfunction isAtOrAbove(\n severity: FindingSeverity,\n floor: FindingSeverity,\n): boolean {\n return SEVERITY_RANK[severity] >= SEVERITY_RANK[floor];\n}\n\n// ── Finding hash ───────────────────────────────────────────────────\n\nexport function computeFindingHash(findings: ReviewFinding[]): string {\n const normalized = findings\n .filter((f) => isAtOrAbove(f.severity, \"high\"))\n .map((f) => `${f.category}:${f.description.slice(0, 100)}`)\n .sort()\n .join(\"|\");\n\n if (!normalized) return \"empty\";\n\n return crypto\n .createHash(\"sha256\")\n .update(normalized)\n .digest(\"hex\")\n .slice(0, 16);\n}\n\n// ── Strategy evaluators ────────────────────────────────────────────\n\nfunction evalManualStop(ctx: TerminationContext): TerminationResult | null {\n if (fs.existsSync(ctx.stopSignalPath)) {\n return {\n verdict: \"stop\",\n reason: `Manual stop signal found at ${ctx.stopSignalPath}`,\n strategy: \"manual-stop\",\n summary: `Review stopped manually at round ${ctx.round}`,\n };\n }\n return null;\n}\n\nfunction evalRoundCap(ctx: TerminationContext): TerminationResult | null {\n if (ctx.round >= ctx.config.maxRounds) {\n return {\n verdict: \"stop\",\n reason: `Round cap reached (${ctx.round}/${ctx.config.maxRounds})`,\n strategy: \"round-cap\",\n summary: `Review stopped at round cap (${ctx.config.maxRounds})`,\n };\n }\n return null;\n}\n\nfunction evalRepetition(ctx: TerminationContext): TerminationResult | null {\n if (ctx.rounds.length < 2) return null;\n\n const latest = ctx.rounds[ctx.rounds.length - 1];\n if (!latest) return null;\n\n let count = 0;\n for (const round of ctx.rounds) {\n if (round.findingHash === latest.findingHash) count++;\n }\n\n if (count >= ctx.config.repetitionThreshold) {\n return {\n verdict: \"stop\",\n reason: `Same finding hash repeated ${count} times (threshold: ${ctx.config.repetitionThreshold})`,\n strategy: \"repetition-detection\",\n summary: `Review going in circles — same findings repeated ${count}x`,\n };\n }\n\n return null;\n}\n\nfunction evalQualityThreshold(\n ctx: TerminationContext,\n): TerminationResult | null {\n if (ctx.rounds.length === 0) return null;\n\n const latest = ctx.rounds[ctx.rounds.length - 1];\n if (!latest) return null;\n\n // Guard: if parser extracted nothing at all (0 findings + 0 diff lines),\n // treat as inconclusive — not \"clean\". The parser may have failed to\n // extract from malformed output.\n if (\n latest.findingCount === 0 &&\n latest.suggestedDiffLines === 0 &&\n latest.findings.length === 0\n ) {\n return null; // inconclusive — continue to next strategy\n }\n\n const significantFindings = latest.findings.filter((f) =>\n isAtOrAbove(f.severity, ctx.config.qualitySeverityFloor),\n );\n\n if (significantFindings.length === 0) {\n return {\n verdict: \"stop\",\n reason: `No findings at ${ctx.config.qualitySeverityFloor}+ severity in round ${ctx.round}`,\n strategy: \"quality-threshold\",\n summary: `Review clean — no ${ctx.config.qualitySeverityFloor}+ findings in round ${ctx.round}`,\n };\n }\n\n return null;\n}\n\nfunction evalDiffInsignificance(\n ctx: TerminationContext,\n): TerminationResult | null {\n if (ctx.rounds.length === 0) return null;\n\n const latest = ctx.rounds[ctx.rounds.length - 1];\n if (!latest) return null;\n\n // Guard: same as quality-threshold — empty output is inconclusive, not trivial\n if (\n latest.findingCount === 0 &&\n latest.suggestedDiffLines === 0 &&\n latest.findings.length === 0\n ) {\n return null;\n }\n\n if (latest.suggestedDiffLines < ctx.config.diffThreshold) {\n return {\n verdict: \"stop\",\n reason: `Suggested diff (${latest.suggestedDiffLines} lines) below threshold (${ctx.config.diffThreshold})`,\n strategy: \"diff-insignificance\",\n summary: `Review suggestions are trivial (${latest.suggestedDiffLines} lines)`,\n };\n }\n\n return null;\n}\n\nconst STRATEGY_EVALUATORS: Record<\n TerminationStrategy,\n (ctx: TerminationContext) => TerminationResult | null\n> = {\n \"manual-stop\": evalManualStop,\n \"round-cap\": evalRoundCap,\n \"repetition-detection\": evalRepetition,\n \"quality-threshold\": evalQualityThreshold,\n \"diff-insignificance\": evalDiffInsignificance,\n};\n\n// ── Main evaluator ─────────────────────────────────────────────────\n\nexport function evaluate(ctx: TerminationContext): TerminationResult {\n for (const strategy of ctx.config.strategies) {\n const evaluator = STRATEGY_EVALUATORS[strategy];\n if (!evaluator) continue;\n\n const result = evaluator(ctx);\n if (result) return result;\n }\n\n return {\n verdict: \"continue\",\n reason: \"All strategies passed — review continues\",\n strategy:\n ctx.config.strategies[ctx.config.strategies.length - 1] ?? \"round-cap\",\n summary: `Round ${ctx.round} complete, continuing`,\n };\n}\n","/**\n * Review engine — detects review requests, builds prompts, parses output.\n *\n * This module handles the \"what\" of review sessions.\n * The termination engine handles the \"when to stop.\"\n * The bridge handles the \"how to deliver.\"\n */\nimport * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport * as crypto from \"node:crypto\";\nimport type {\n ReviewFinding,\n ReviewRound,\n FindingSeverity,\n TerminationConfig,\n} from \"./termination.js\";\nimport { computeFindingHash } from \"./termination.js\";\n\n// ── Types ──────────────────────────────────────────────────────────\n\nexport type AgentRole = \"reviewer\" | \"validator\" | \"long-running\";\n\nexport interface ReviewRequest {\n sourcePath: string;\n sender: string;\n recipient: string;\n prNumber: number;\n branch?: string;\n generation: string;\n isReReview: boolean;\n round: number;\n}\n\nexport interface ReviewSession {\n request: ReviewRequest;\n agentName: string;\n role: AgentRole;\n rounds: ReviewRound[];\n startedAt: string;\n terminatedAt?: string;\n reviewFilePath: string;\n}\n\nexport interface ReviewEngineConfig {\n role: AgentRole;\n generation: string;\n commsDir: string;\n repoRoot: string;\n agentName: string;\n termination: TerminationConfig;\n}\n\nexport interface HeadlessConfig {\n enabled: boolean;\n role: AgentRole;\n termination: TerminationConfig;\n}\n\n// ── Request Detection ──────────────────────────────────────────────\n\nconst REVIEW_KEYWORDS = [/리뷰\\s*요청/, /review[- ]?request/i];\n\nconst REREVIEW_KEYWORDS = [/재리뷰/, /re-?review/i];\n\nconst PR_NUMBER_PATTERNS = [\n /PR\\s*#?\\s*(\\d+)/i,\n /pull\\/(\\d+)/,\n /review[-_ ]?(\\d+)/i,\n];\n\n/**\n * Parse inbox filename to extract routing info.\n * Format: YYYYMMDD-sender-recipient-subject.md\n */\nexport function parseInboxFilename(filename: string): {\n date: string;\n sender: string;\n recipient: string;\n subject: string;\n} | null {\n const base = path.basename(filename, \".md\");\n const match = base.match(/^(\\d{8})-([^-]+)-([^-]+)-(.+)$/);\n if (!match) return null;\n\n return {\n date: match[1],\n sender: match[2],\n recipient: match[3],\n subject: match[4],\n };\n}\n\n/**\n * Extract PR number from text content.\n */\nexport function extractPrNumber(text: string): number | null {\n for (const pattern of PR_NUMBER_PATTERNS) {\n const match = text.match(pattern);\n if (match?.[1]) return parseInt(match[1], 10);\n }\n return null;\n}\n\n/**\n * Detect if a file represents a review request.\n * Returns a ReviewRequest if detected, null otherwise.\n */\nexport function detectReviewRequest(\n filePath: string,\n content: string,\n generation: string,\n): ReviewRequest | null {\n const parsed = parseInboxFilename(filePath);\n if (!parsed) return null;\n\n const fullText = `${parsed.subject} ${content}`;\n\n // Check for review keywords\n const isReview = REVIEW_KEYWORDS.some((re) => re.test(fullText));\n const isReReview = REREVIEW_KEYWORDS.some((re) => re.test(fullText));\n\n if (!isReview && !isReReview) return null;\n\n // Extract PR number\n const prNumber = extractPrNumber(fullText);\n if (!prNumber) return null;\n\n return {\n sourcePath: filePath,\n sender: parsed.sender,\n recipient: parsed.recipient,\n prNumber,\n generation,\n isReReview,\n round: isReReview ? 2 : 1, // Will be adjusted by session tracking\n };\n}\n\n// ── Review Prompt ──────────────────────────────────────────────────\n\nexport function buildReviewPrompt(\n request: ReviewRequest,\n agentName: string,\n round: number,\n): string {\n const roundLabel = round > 1 ? ` (re-review round ${round})` : \"\";\n\n return [\n `You are a code reviewer for the HUA Platform monorepo.`,\n ``,\n `## Task`,\n `Review PR #${request.prNumber}${roundLabel}.`,\n ``,\n `## Instructions`,\n `1. Run: gh pr diff ${request.prNumber}`,\n `2. Read changed files for understanding`,\n `3. Apply review checklist: security > data integrity > performance > error handling > code quality`,\n `4. Write structured findings`,\n ``,\n `## Output`,\n `Write review to: ${path.join(\"reviews\", request.generation, `review-PR${request.prNumber}-${agentName}.md`)}`,\n ``,\n `### Review File Format`,\n `\\`\\`\\`markdown`,\n `---`,\n `date: ${new Date().toISOString().split(\"T\")[0]}`,\n `reviewer: ${agentName}`,\n `pr: ${request.prNumber}`,\n `round: ${round}`,\n `status: clean | p1-Nitems | p2-Nitems`,\n `merge: merge | fix-then-merge | hold`,\n `---`,\n ``,\n `## Findings`,\n ``,\n `### Critical / High`,\n `- [severity] [category] file:line — description`,\n ``,\n `### Medium / Low`,\n `- [severity] [category] file:line — description`,\n ``,\n `## Checks`,\n `- [ ] Build verified`,\n `- [ ] Typecheck passed`,\n `- [ ] Scope check (only expected files changed)`,\n ``,\n `## Suggested Diff Lines`,\n `{number of lines the author should change to address findings}`,\n ``,\n `## Decision`,\n `{one-line merge recommendation}`,\n `\\`\\`\\``,\n ``,\n `## After Review`,\n `- Update reviews/INDEX.md`,\n `- Write inbox reply to ${request.sender}`,\n `- Commit and push comms changes`,\n ].join(\"\\n\");\n}\n\n// ── Review Output Parsing ──────────────────────────────────────────\n\nconst SEVERITY_PATTERNS: Record<FindingSeverity, RegExp> = {\n critical: /\\bcritical\\b/i,\n high: /\\bhigh\\b/i,\n medium: /\\bmedium\\b/i,\n low: /\\blow\\b/i,\n nitpick: /\\bnitpick\\b/i,\n};\n\nconst CATEGORY_PATTERNS = [\n \"security\",\n \"performance\",\n \"correctness\",\n \"data-integrity\",\n \"error-handling\",\n \"code-quality\",\n \"style\",\n];\n\n/**\n * Parse frontmatter from review file.\n */\nexport function parseFrontmatter(\n content: string,\n): Record<string, string> | null {\n const match = content.match(/^---\\n([\\s\\S]*?)\\n---/);\n if (!match?.[1]) return null;\n\n const fields: Record<string, string> = {};\n for (const line of match[1].split(\"\\n\")) {\n const kv = line.match(/^(\\w+):\\s*(.+)$/);\n if (kv?.[1] && kv[2]) {\n fields[kv[1]] = kv[2].trim();\n }\n }\n return fields;\n}\n\n/**\n * Extract suggested diff lines from review content.\n */\nexport function extractSuggestedDiffLines(content: string): number {\n const match = content.match(/## Suggested Diff Lines\\s*\\n\\s*(\\d+)/i);\n if (match?.[1]) return parseInt(match[1], 10);\n\n // Fallback: count lines in code blocks that look like suggestions\n const codeBlocks = content.match(/```[\\s\\S]*?```/g) ?? [];\n let totalLines = 0;\n for (const block of codeBlocks) {\n totalLines += block.split(\"\\n\").length - 2; // minus fences\n }\n return totalLines;\n}\n\n/**\n * Extract findings from review content.\n * Best-effort parsing — reviews may not follow exact format.\n */\nexport function extractFindings(content: string): ReviewFinding[] {\n const findings: ReviewFinding[] = [];\n\n // Match lines that look like finding entries\n const lines = content.split(\"\\n\");\n for (const line of lines) {\n const trimmed = line.trim();\n if (!trimmed.startsWith(\"-\") && !trimmed.startsWith(\"*\")) continue;\n\n // Detect severity\n let severity: FindingSeverity = \"medium\";\n for (const [sev, pattern] of Object.entries(SEVERITY_PATTERNS)) {\n if (pattern.test(trimmed)) {\n severity = sev as FindingSeverity;\n break;\n }\n }\n\n // Detect category\n let category = \"general\";\n for (const cat of CATEGORY_PATTERNS) {\n if (trimmed.toLowerCase().includes(cat)) {\n category = cat;\n break;\n }\n }\n\n // Extract file:line if present\n const fileMatch = trimmed.match(/([a-zA-Z0-9_/.-]+\\.[a-zA-Z]+):(\\d+)/);\n\n // Only include if it looks like an actual finding (has severity keyword or file ref)\n const hasSeverityKeyword = Object.values(SEVERITY_PATTERNS).some((p) =>\n p.test(trimmed),\n );\n if (hasSeverityKeyword || fileMatch) {\n findings.push({\n severity,\n category,\n description: trimmed.replace(/^[-*]\\s*/, \"\").slice(0, 200),\n file: fileMatch?.[1],\n line: fileMatch?.[2] ? parseInt(fileMatch[2], 10) : undefined,\n });\n }\n }\n\n return findings;\n}\n\n/**\n * Parse a review output file into a ReviewRound.\n */\nexport function parseReviewOutput(\n reviewFilePath: string,\n round: number,\n): ReviewRound | null {\n if (!fs.existsSync(reviewFilePath)) return null;\n\n const content = fs.readFileSync(reviewFilePath, \"utf-8\");\n const findings = extractFindings(content);\n const suggestedDiffLines = extractSuggestedDiffLines(content);\n\n return {\n round,\n timestamp: new Date().toISOString(),\n findingCount: findings.length,\n findings,\n suggestedDiffLines,\n findingHash: computeFindingHash(findings),\n };\n}\n\n// ── Review File Path ───────────────────────────────────────────────\n\nexport function reviewFilePath(\n commsDir: string,\n generation: string,\n prNumber: number,\n agentName: string,\n): string {\n return path.join(\n commsDir,\n \"reviews\",\n generation,\n `review-PR${prNumber}-${agentName}.md`,\n );\n}\n\n// ── Stale Detection ────────────────────────────────────────────────\n\n/**\n * Check if a review request is stale (already handled).\n * Mirrors PS1 Test-IsStaleRequest logic.\n */\nexport function isStaleReviewRequest(\n request: ReviewRequest,\n commsDir: string,\n agentName: string,\n): boolean {\n // 1. Check if review file exists and is newer than request\n const revPath = reviewFilePath(\n commsDir,\n request.generation,\n request.prNumber,\n agentName,\n );\n if (fs.existsSync(revPath) && fs.existsSync(request.sourcePath)) {\n const reviewStat = fs.statSync(revPath);\n const requestStat = fs.statSync(request.sourcePath);\n if (reviewStat.mtimeMs > requestStat.mtimeMs) return true;\n }\n\n return false;\n}\n\n// ── Processed Marker ───────────────────────────────────────────────\n\nexport function computeRequestMarkerId(filePath: string): string {\n const stat = fs.statSync(filePath);\n const input = `${filePath}|${stat.mtimeMs}`;\n return crypto.createHash(\"sha1\").update(input).digest(\"hex\");\n}\n\nexport function isAlreadyProcessed(\n stateDir: string,\n filePath: string,\n): boolean {\n const markerId = computeRequestMarkerId(filePath);\n return fs.existsSync(path.join(stateDir, \"processed\", `${markerId}.done`));\n}\n\nexport function unmarkProcessed(\n stateDir: string,\n request: ReviewRequest,\n): void {\n const markerId = computeRequestMarkerId(request.sourcePath);\n const markerPath = path.join(stateDir, \"processed\", `${markerId}.done`);\n if (fs.existsSync(markerPath)) {\n fs.unlinkSync(markerPath);\n }\n}\n\nexport function markAsProcessed(\n stateDir: string,\n request: ReviewRequest,\n): void {\n const markerId = computeRequestMarkerId(request.sourcePath);\n const markerDir = path.join(stateDir, \"processed\");\n fs.mkdirSync(markerDir, { recursive: true });\n const markerPath = path.join(markerDir, `${markerId}.done`);\n const payload = {\n prNumber: request.prNumber,\n sourcePath: request.sourcePath,\n processedAt: new Date().toISOString(),\n };\n const tmp = `${markerPath}.tmp.${process.pid}`;\n fs.writeFileSync(tmp, JSON.stringify(payload, null, 2), \"utf-8\");\n fs.renameSync(tmp, markerPath);\n}\n\n// ── Bridge Receipt ─────────────────────────────────────────────────\n\n/**\n * Write immediate inbox acknowledgment before review starts.\n * Mirrors PS1 Write-BridgeReceipt pattern.\n */\nexport function writeReviewReceipt(\n commsDir: string,\n request: ReviewRequest,\n agentName: string,\n): string {\n const date = new Date().toISOString().split(\"T\")[0].replace(/-/g, \"\");\n const filename = `${date}-${agentName}-${request.sender}-PR${request.prNumber}-ack.md`;\n const content = [\n `## ${agentName} > ${request.sender}`,\n ``,\n `- PR #${request.prNumber} review request received.`,\n `- headless reviewer processing.`,\n `- request: ${path.basename(request.sourcePath)}`,\n ].join(\"\\n\");\n\n const inboxDir = path.join(commsDir, \"inbox\");\n fs.mkdirSync(inboxDir, { recursive: true });\n const inboxPath = path.join(inboxDir, filename);\n const tmp = `${inboxPath}.tmp.${process.pid}`;\n fs.writeFileSync(tmp, content, \"utf-8\");\n fs.renameSync(tmp, inboxPath);\n return inboxPath;\n}\n\n// ── Orchestrator Entry Point ───────────────────────────────────\n\n/**\n * Check if the current bridge process is running in headless reviewer mode.\n * Reads from env vars set by engine/bridge.ts startBridge().\n */\nexport function isHeadlessReviewer(): boolean {\n return process.env.TAP_HEADLESS === \"true\";\n}\n\n/**\n * Get headless reviewer configuration from env vars.\n * Returns null if not in headless mode.\n */\nexport function getHeadlessEnvConfig(): {\n role: string;\n maxRounds: number;\n qualityFloor: string;\n} | null {\n if (!isHeadlessReviewer()) return null;\n return {\n role: process.env.TAP_AGENT_ROLE ?? \"reviewer\",\n maxRounds: parseInt(process.env.TAP_MAX_REVIEW_ROUNDS ?? \"5\", 10),\n qualityFloor: process.env.TAP_QUALITY_FLOOR ?? \"high\",\n };\n}\n\n/**\n * Scan inbox for pending review requests.\n * This is the entry point for the headless review loop.\n *\n * Phase 3 will wire this into the bridge runner's poll cycle:\n * 1. scanInboxForReviews() → detect pending requests\n * 2. For each: writeReviewReceipt() → dispatch to bridge → parseReviewOutput()\n * 3. evaluate() termination → continue or stop\n */\nexport function scanInboxForReviews(\n commsDir: string,\n stateDir: string,\n generation: string,\n agentName: string,\n): ReviewRequest[] {\n const inboxDir = path.join(commsDir, \"inbox\");\n if (!fs.existsSync(inboxDir)) return [];\n\n const files = fs.readdirSync(inboxDir).filter((f) => f.endsWith(\".md\"));\n const requests: ReviewRequest[] = [];\n\n for (const file of files) {\n const filePath = path.join(inboxDir, file);\n const content = fs.readFileSync(filePath, \"utf-8\");\n const request = detectReviewRequest(filePath, content, generation);\n\n if (!request) continue;\n\n // Only process requests addressed to this agent or broadcast (\"전체\"/\"all\")\n const to = request.recipient.toLowerCase();\n if (\n to !== agentName.toLowerCase() &&\n to !== \"전체\" &&\n to !== \"all\" &&\n to !== \"\"\n ) {\n continue;\n }\n\n if (isStaleReviewRequest(request, commsDir, agentName)) continue;\n if (isAlreadyProcessed(stateDir, filePath)) continue;\n\n requests.push(request);\n }\n\n return requests;\n}\n","/**\n * Headless review loop — poll-based review orchestrator for bridge processes.\n *\n * Runs alongside the bridge script. When TAP_HEADLESS=true:\n * 1. Periodically scans inbox for review requests\n * 2. Writes review dispatch files that the bridge picks up\n * 3. Monitors review output for completion\n * 4. Evaluates termination conditions\n * 5. Continues or stops the review session\n *\n * This is a control loop, not a WebSocket client — the bridge handles dispatch.\n */\nimport * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport {\n scanInboxForReviews,\n isHeadlessReviewer,\n getHeadlessEnvConfig,\n buildReviewPrompt,\n writeReviewReceipt,\n parseReviewOutput,\n reviewFilePath,\n markAsProcessed,\n unmarkProcessed,\n type ReviewRequest,\n type ReviewSession,\n} from \"./review.js\";\nimport {\n evaluate,\n DEFAULT_TERMINATION_CONFIG,\n type TerminationContext,\n type TerminationConfig,\n type FindingSeverity,\n} from \"./termination.js\";\n\n// ── Types ──────────────────────────────────────────────────────────\n\nexport interface HeadlessLoopOptions {\n commsDir: string;\n stateDir: string;\n repoRoot: string;\n agentName: string;\n generation: string;\n pollIntervalMs: number;\n}\n\nexport interface HeadlessLoopState {\n running: boolean;\n activeSession: ReviewSession | null;\n completedSessions: number;\n lastPollAt: string | null;\n}\n\n// ── Loop implementation ────────────────────────────────────────────\n\nexport function createHeadlessLoop(options: HeadlessLoopOptions): {\n start: () => void;\n stop: () => void;\n getState: () => HeadlessLoopState;\n} {\n const envConfig = getHeadlessEnvConfig();\n const terminationConfig: TerminationConfig = {\n ...DEFAULT_TERMINATION_CONFIG,\n maxRounds: envConfig?.maxRounds ?? DEFAULT_TERMINATION_CONFIG.maxRounds,\n qualitySeverityFloor:\n (envConfig?.qualityFloor as FindingSeverity) ??\n DEFAULT_TERMINATION_CONFIG.qualitySeverityFloor,\n };\n\n const state: HeadlessLoopState = {\n running: false,\n activeSession: null,\n completedSessions: 0,\n lastPollAt: null,\n };\n\n let timer: ReturnType<typeof setInterval> | null = null;\n\n function log(msg: string): void {\n const ts = new Date().toISOString();\n console.error(`[${ts}] [headless-loop] ${msg}`);\n }\n\n function pollOnce(): void {\n state.lastPollAt = new Date().toISOString();\n\n // Skip if already processing a review\n if (state.activeSession) {\n checkActiveSession();\n return;\n }\n\n // Scan for new review requests\n const requests = scanInboxForReviews(\n options.commsDir,\n options.stateDir,\n options.generation,\n options.agentName,\n );\n\n if (requests.length === 0) return;\n\n // Process first request (sequential — one at a time)\n const request = requests[0];\n startReviewSession(request);\n }\n\n function startReviewSession(request: ReviewRequest): void {\n log(`Starting review for PR #${request.prNumber}`);\n\n // Mark as processed EAGERLY to prevent race with generic bridge.\n // If anything fails after this point, we roll back the marker.\n markAsProcessed(options.stateDir, request);\n\n try {\n // Write receipt\n writeReviewReceipt(options.commsDir, request, options.agentName);\n\n // Build review prompt\n const prompt = buildReviewPrompt(request, options.agentName, 1);\n\n // Write dispatch file to commsDir/inbox/ — the bridge watches this\n // directory and will inject it as a turn/start\n const date = new Date().toISOString().split(\"T\")[0].replace(/-/g, \"\");\n const dispatchFilename = `${date}-headless-${options.agentName}-review-PR${request.prNumber}.md`;\n const inboxDir = path.join(options.commsDir, \"inbox\");\n fs.mkdirSync(inboxDir, { recursive: true });\n const dispatchFile = path.join(inboxDir, dispatchFilename);\n const tmp = `${dispatchFile}.tmp.${process.pid}`;\n fs.writeFileSync(tmp, prompt, \"utf-8\");\n fs.renameSync(tmp, dispatchFile);\n\n state.activeSession = {\n request,\n agentName: options.agentName,\n role:\n (envConfig?.role as \"reviewer\" | \"validator\" | \"long-running\") ??\n \"reviewer\",\n rounds: [],\n startedAt: new Date().toISOString(),\n reviewFilePath: reviewFilePath(\n options.commsDir,\n request.generation,\n request.prNumber,\n options.agentName,\n ),\n };\n\n log(`Dispatched review prompt for PR #${request.prNumber} (round 1)`);\n } catch (err) {\n // Roll back processed marker so request can be retried on next poll\n log(\n `Failed to start review for PR #${request.prNumber}: ${err instanceof Error ? err.message : String(err)}`,\n );\n unmarkProcessed(options.stateDir, request);\n }\n }\n\n function checkActiveSession(): void {\n if (!state.activeSession) return;\n\n const session = state.activeSession;\n const revPath = session.reviewFilePath;\n\n // Check if review output file has been updated since last check\n if (!fs.existsSync(revPath)) return;\n\n const stat = fs.statSync(revPath);\n const lastRound = session.rounds[session.rounds.length - 1];\n const lastCheck = lastRound?.timestamp ?? session.startedAt;\n\n // Only process if file is newer than our last check\n if (stat.mtime.toISOString() <= lastCheck) return;\n\n // Parse the review output\n const roundNum = session.rounds.length + 1;\n const round = parseReviewOutput(revPath, roundNum);\n if (!round) return;\n\n session.rounds.push(round);\n log(\n `PR #${session.request.prNumber} round ${roundNum}: ${round.findingCount} findings, ${round.suggestedDiffLines} suggested diff lines`,\n );\n\n // Evaluate termination\n const stopSignalPath = path.join(options.stateDir, \"stop-signal\");\n const ctx: TerminationContext = {\n round: roundNum,\n rounds: session.rounds,\n stopSignalPath,\n config: terminationConfig,\n };\n\n const result = evaluate(ctx);\n\n if (result.verdict === \"stop\") {\n log(\n `PR #${session.request.prNumber} terminated: ${result.reason} (${result.strategy})`,\n );\n completeSession(session);\n } else {\n log(`PR #${session.request.prNumber} continues to round ${roundNum + 1}`);\n dispatchFollowUp(session, roundNum + 1);\n }\n }\n\n function dispatchFollowUp(session: ReviewSession, round: number): void {\n const prompt = buildReviewPrompt(session.request, options.agentName, round);\n\n // Write follow-up dispatch to commsDir/inbox/ for bridge to steer\n const date = new Date().toISOString().split(\"T\")[0].replace(/-/g, \"\");\n const dispatchFilename = `${date}-headless-${options.agentName}-review-PR${session.request.prNumber}-r${round}.md`;\n const inboxDir = path.join(options.commsDir, \"inbox\");\n fs.mkdirSync(inboxDir, { recursive: true });\n const dispatchFile = path.join(inboxDir, dispatchFilename);\n const tmp = `${dispatchFile}.tmp.${process.pid}`;\n fs.writeFileSync(tmp, prompt, \"utf-8\");\n fs.renameSync(tmp, dispatchFile);\n }\n\n function completeSession(session: ReviewSession): void {\n session.terminatedAt = new Date().toISOString();\n\n // Note: request was already marked as processed eagerly in startReviewSession()\n\n // Clean up dispatch files from inbox\n const inboxDir = path.join(options.commsDir, \"inbox\");\n if (fs.existsSync(inboxDir)) {\n const prefix = `headless-${options.agentName}-review-PR${session.request.prNumber}`;\n const files = fs.readdirSync(inboxDir).filter((f) => f.includes(prefix));\n for (const f of files) {\n fs.unlinkSync(path.join(inboxDir, f));\n }\n }\n\n state.activeSession = null;\n state.completedSessions++;\n log(\n `PR #${session.request.prNumber} review complete (${session.rounds.length} rounds)`,\n );\n }\n\n return {\n start() {\n if (!isHeadlessReviewer()) {\n log(\"Not in headless mode — loop not started\");\n return;\n }\n\n state.running = true;\n log(\n `Headless review loop started (${envConfig?.role ?? \"reviewer\"}, poll ${options.pollIntervalMs}ms, max ${terminationConfig.maxRounds} rounds)`,\n );\n\n // Initial poll\n pollOnce();\n\n // Set up interval\n timer = setInterval(pollOnce, options.pollIntervalMs);\n },\n\n stop() {\n state.running = false;\n if (timer) {\n clearInterval(timer);\n timer = null;\n }\n log(\"Headless review loop stopped\");\n },\n\n getState() {\n return { ...state };\n },\n };\n}\n","import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport { spawn } from \"node:child_process\";\nimport { fileURLToPath } from \"node:url\";\nimport {\n resolveConfig,\n SHARED_CONFIG_FILE,\n LOCAL_CONFIG_FILE,\n} from \"../config/index.js\";\nimport { resolveNodeRuntime, buildRuntimeEnv } from \"../runtime/index.js\";\n\n// ─── Repo root discovery (fallback for unbundled runs) ─────────\n\nfunction findRepoRootFromRunner(): string | null {\n let dir = path.resolve(path.dirname(fileURLToPath(import.meta.url)));\n\n while (true) {\n if (fs.existsSync(path.join(dir, SHARED_CONFIG_FILE))) return dir;\n if (fs.existsSync(path.join(dir, LOCAL_CONFIG_FILE))) return dir;\n if (fs.existsSync(path.join(dir, \"scripts\", \"codex-app-server-bridge.ts\")))\n return dir;\n const parent = path.dirname(dir);\n if (parent === dir) return null;\n dir = parent;\n }\n}\n\n// ─── Headless review loop integration ──────────────────────────\n\nfunction maybeStartHeadlessLoop(\n repoRoot: string,\n commsDir: string,\n stateDir: string | undefined,\n): void {\n if (process.env.TAP_HEADLESS !== \"true\") return;\n\n // Dynamic import to avoid loading review/termination engines in non-headless mode\n import(\"../engine/headless-loop.js\")\n .then(({ createHeadlessLoop }) => {\n const agentName =\n process.env.TAP_AGENT_NAME ??\n process.env.CODEX_TAP_AGENT_NAME ??\n \"reviewer\";\n const generation = process.env.TAP_REVIEW_GENERATION ?? \"gen11\";\n const resolvedStateDir = stateDir ?? path.join(repoRoot, \".tap-comms\");\n\n const loop = createHeadlessLoop({\n commsDir,\n stateDir: resolvedStateDir,\n repoRoot,\n agentName,\n generation,\n pollIntervalMs: 3_000, // Poll faster than generic bridge (5s) for review priority\n });\n\n loop.start();\n\n // Clean shutdown\n process.on(\"SIGTERM\", () => loop.stop());\n process.on(\"SIGINT\", () => loop.stop());\n })\n .catch((err) => {\n console.error(\"[headless-loop] Failed to start:\", err);\n });\n}\n\n// ─── Main ──────────────────────────────────────────────────────\n\nasync function main(): Promise<void> {\n const repoRootHint = findRepoRootFromRunner() ?? undefined;\n const { config } = resolveConfig({}, repoRootHint);\n\n const repoRoot = config.repoRoot;\n const commsDir = config.commsDir;\n let appServerUrl = config.appServerUrl;\n\n // Multi-instance: override port from TAP_BRIDGE_PORT env var\n const instancePort = process.env.TAP_BRIDGE_PORT;\n if (instancePort) {\n try {\n const url = new URL(appServerUrl);\n url.port = instancePort;\n appServerUrl = url.toString().replace(/\\/$/, \"\");\n } catch {\n // Invalid URL — fall through to config default\n }\n }\n\n // Multi-instance: derive instance-specific state dir\n const instanceId = process.env.TAP_BRIDGE_INSTANCE_ID;\n const stateDir = instanceId\n ? path.join(repoRoot, \".tmp\", `codex-app-server-bridge-${instanceId}`)\n : undefined;\n\n // Honor pre-resolved node from parent (2-stage spawn: engine → runner → daemon)\n // TAP_STRIP_TYPES preserves metadata so bun doesn't get --experimental-strip-types.\n const preResolved = process.env.TAP_RESOLVED_NODE;\n const resolved = preResolved\n ? {\n command: preResolved,\n supportsStripTypes: process.env.TAP_STRIP_TYPES === \"1\",\n source: \"env\" as const,\n majorVersion: null,\n }\n : resolveNodeRuntime(config.runtimeCommand, repoRoot);\n\n const command = resolved.command;\n\n // Locate bridge script\n const scriptPath = path.join(\n repoRoot,\n \"scripts\",\n \"codex-app-server-bridge.ts\",\n );\n if (!fs.existsSync(scriptPath)) {\n throw new Error(\n `Bridge script not found: ${scriptPath}\\n` +\n `Ensure scripts/codex-app-server-bridge.ts exists in repo root.`,\n );\n }\n\n // Build args\n const args: string[] = [];\n if (resolved.supportsStripTypes) {\n args.push(\"--experimental-strip-types\");\n }\n args.push(\n scriptPath,\n `--repo-root=${repoRoot}`,\n `--comms-dir=${commsDir}`,\n `--app-server-url=${appServerUrl}`,\n );\n if (stateDir) {\n args.push(`--state-dir=${stateDir}`);\n }\n\n // Forward bridge operational flags from env (set by engine/bridge.ts)\n const busyMode = process.env.TAP_BUSY_MODE;\n if (busyMode) args.push(`--busy-mode=${busyMode}`);\n\n const pollSeconds = process.env.TAP_POLL_SECONDS;\n if (pollSeconds) args.push(`--poll-seconds=${pollSeconds}`);\n\n const reconnectSeconds = process.env.TAP_RECONNECT_SECONDS;\n if (reconnectSeconds) args.push(`--reconnect-seconds=${reconnectSeconds}`);\n\n const lookbackMinutes = process.env.TAP_MESSAGE_LOOKBACK_MINUTES;\n if (lookbackMinutes)\n args.push(`--message-lookback-minutes=${lookbackMinutes}`);\n\n const threadId = process.env.TAP_THREAD_ID;\n if (threadId) args.push(`--thread-id=${threadId}`);\n\n if (process.env.TAP_EPHEMERAL === \"true\") args.push(\"--ephemeral\");\n if (process.env.TAP_PROCESS_EXISTING === \"true\")\n args.push(\"--process-existing-messages\");\n\n // Spawn with fnm-aware PATH so any further child spawns also find the right Node\n const runtimeEnv = buildRuntimeEnv(repoRoot);\n\n const child = spawn(command, args, {\n cwd: repoRoot,\n env: runtimeEnv,\n stdio: \"inherit\",\n });\n\n // Start headless review loop if in headless mode\n maybeStartHeadlessLoop(repoRoot, commsDir, stateDir);\n\n child.on(\"exit\", (code: number | null, signal: NodeJS.Signals | null) => {\n if (signal) {\n process.kill(process.pid, signal);\n return;\n }\n process.exit(code ?? 0);\n });\n\n child.on(\"error\", (error: Error) => {\n console.error(String(error));\n process.exit(1);\n });\n}\n\nmain().catch((error) => {\n console.error(error instanceof Error ? error.message : String(error));\n process.exit(1);\n});\n","import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport type {\n TapSharedConfig,\n TapLocalConfig,\n TapResolvedConfig,\n ConfigSource,\n ConfigResolution,\n} from \"./types.js\";\n\n// ─── File names ────────────────────────────────────────────────\n\nexport const SHARED_CONFIG_FILE = \"tap-config.json\";\nexport const LOCAL_CONFIG_FILE = \"tap-config.local.json\";\n\n// ─── Defaults ──────────────────────────────────────────────────\n\nconst DEFAULT_RUNTIME_COMMAND = \"node\";\nconst DEFAULT_APP_SERVER_URL = \"ws://127.0.0.1:4501\";\n\n// ─── Repo root discovery ───────────────────────────────────────\n\nexport function findRepoRoot(startDir: string = process.cwd()): string {\n let dir = path.resolve(startDir);\n while (true) {\n if (fs.existsSync(path.join(dir, \".git\"))) return dir;\n if (fs.existsSync(path.join(dir, \"package.json\"))) return dir;\n const parent = path.dirname(dir);\n if (parent === dir) break;\n dir = parent;\n }\n return process.cwd();\n}\n\n// ─── JSON file loading ─────────────────────────────────────────\n\nfunction loadJsonFile<T>(filePath: string): T | null {\n if (!fs.existsSync(filePath)) return null;\n try {\n const raw = fs.readFileSync(filePath, \"utf-8\");\n return JSON.parse(raw) as T;\n } catch {\n return null;\n }\n}\n\nexport function loadSharedConfig(repoRoot: string): TapSharedConfig | null {\n return loadJsonFile<TapSharedConfig>(path.join(repoRoot, SHARED_CONFIG_FILE));\n}\n\nexport function loadLocalConfig(repoRoot: string): TapLocalConfig | null {\n return loadJsonFile<TapLocalConfig>(path.join(repoRoot, LOCAL_CONFIG_FILE));\n}\n\n// ─── CLI overrides ─────────────────────────────────────────────\n\nexport interface ConfigOverrides {\n commsDir?: string;\n stateDir?: string;\n runtimeCommand?: string;\n appServerUrl?: string;\n}\n\n// ─── Resolution ────────────────────────────────────────────────\n\n/**\n * Resolve config with priority: CLI flag > env > local config > shared config > auto.\n */\nexport function resolveConfig(\n overrides: ConfigOverrides = {},\n startDir?: string,\n): ConfigResolution {\n const repoRoot = findRepoRoot(startDir);\n const shared = loadSharedConfig(repoRoot) ?? {};\n const local = loadLocalConfig(repoRoot) ?? {};\n\n const sources: Record<keyof TapResolvedConfig, ConfigSource> = {\n repoRoot: \"auto\",\n commsDir: \"auto\",\n stateDir: \"auto\",\n runtimeCommand: \"auto\",\n appServerUrl: \"auto\",\n };\n\n // ─── commsDir ──────────────────────────────────────────────\n let commsDir: string;\n if (overrides.commsDir) {\n commsDir = path.resolve(overrides.commsDir);\n sources.commsDir = \"cli-flag\";\n } else if (process.env.TAP_COMMS_DIR) {\n commsDir = path.resolve(process.env.TAP_COMMS_DIR);\n sources.commsDir = \"env\";\n } else if (local.commsDir) {\n commsDir = resolvePath(repoRoot, local.commsDir);\n sources.commsDir = \"local-config\";\n } else if (shared.commsDir) {\n commsDir = resolvePath(repoRoot, shared.commsDir);\n sources.commsDir = \"shared-config\";\n } else {\n commsDir = path.join(path.dirname(repoRoot), \"tap-comms\");\n }\n\n // ─── stateDir ──────────────────────────────────────────────\n let stateDir: string;\n if (overrides.stateDir) {\n stateDir = path.resolve(overrides.stateDir);\n sources.stateDir = \"cli-flag\";\n } else if (process.env.TAP_STATE_DIR) {\n stateDir = path.resolve(process.env.TAP_STATE_DIR);\n sources.stateDir = \"env\";\n } else if (local.stateDir) {\n stateDir = resolvePath(repoRoot, local.stateDir);\n sources.stateDir = \"local-config\";\n } else if (shared.stateDir) {\n stateDir = resolvePath(repoRoot, shared.stateDir);\n sources.stateDir = \"shared-config\";\n } else {\n stateDir = path.join(repoRoot, \".tap-comms\");\n }\n\n // ─── runtimeCommand ────────────────────────────────────────\n let runtimeCommand: string;\n if (overrides.runtimeCommand) {\n runtimeCommand = overrides.runtimeCommand;\n sources.runtimeCommand = \"cli-flag\";\n } else if (process.env.TAP_RUNTIME_COMMAND) {\n runtimeCommand = process.env.TAP_RUNTIME_COMMAND;\n sources.runtimeCommand = \"env\";\n } else if (local.runtimeCommand) {\n runtimeCommand = local.runtimeCommand;\n sources.runtimeCommand = \"local-config\";\n } else if (shared.runtimeCommand) {\n runtimeCommand = shared.runtimeCommand;\n sources.runtimeCommand = \"shared-config\";\n } else {\n runtimeCommand = DEFAULT_RUNTIME_COMMAND;\n }\n\n // ─── appServerUrl ──────────────────────────────────────────\n let appServerUrl: string;\n if (overrides.appServerUrl) {\n appServerUrl = overrides.appServerUrl;\n sources.appServerUrl = \"cli-flag\";\n } else if (process.env.TAP_APP_SERVER_URL) {\n appServerUrl = process.env.TAP_APP_SERVER_URL;\n sources.appServerUrl = \"env\";\n } else if (local.appServerUrl) {\n appServerUrl = local.appServerUrl;\n sources.appServerUrl = \"local-config\";\n } else if (shared.appServerUrl) {\n appServerUrl = shared.appServerUrl;\n sources.appServerUrl = \"shared-config\";\n } else {\n appServerUrl = DEFAULT_APP_SERVER_URL;\n }\n\n return {\n config: { repoRoot, commsDir, stateDir, runtimeCommand, appServerUrl },\n sources,\n };\n}\n\n// ─── Save helpers ──────────────────────────────────────────────\n\nexport function saveSharedConfig(\n repoRoot: string,\n config: TapSharedConfig,\n): void {\n const filePath = path.join(repoRoot, SHARED_CONFIG_FILE);\n const tmp = `${filePath}.tmp.${process.pid}`;\n fs.writeFileSync(tmp, JSON.stringify(config, null, 2) + \"\\n\", \"utf-8\");\n fs.renameSync(tmp, filePath);\n}\n\nexport function saveLocalConfig(\n repoRoot: string,\n config: TapLocalConfig,\n): void {\n const filePath = path.join(repoRoot, LOCAL_CONFIG_FILE);\n const tmp = `${filePath}.tmp.${process.pid}`;\n fs.writeFileSync(tmp, JSON.stringify(config, null, 2) + \"\\n\", \"utf-8\");\n fs.renameSync(tmp, filePath);\n}\n\n// ─── Helpers ───────────────────────────────────────────────────\n\n/** Resolve a path relative to repoRoot, or keep absolute as-is. */\nfunction resolvePath(repoRoot: string, p: string): string {\n return path.isAbsolute(p) ? p : path.resolve(repoRoot, p);\n}\n","/**\n * Common Node.js runtime resolver for all tap-comms child processes.\n *\n * Resolution chain:\n * .node-version + fnm probe → configured command → tsx fallback\n *\n * Extracted from codex-bridge-runner.ts (M69) to share across:\n * - bridge engine spawn\n * - bridge runner spawn\n * - future CLI commands\n */\n\nimport * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport { execSync } from \"node:child_process\";\n\n// ─── Types ─────────────────────────────────────────────────────\n\nexport type RuntimeSource = \"fnm\" | \"config\" | \"path\" | \"tsx-fallback\" | \"bun\";\n\nexport interface ResolvedRuntime {\n /** Absolute path or command name for the resolved runtime. */\n command: string;\n /** Whether --experimental-strip-types is supported and should be used. */\n supportsStripTypes: boolean;\n /** Where the runtime was resolved from (for diagnostics). */\n source: RuntimeSource;\n /** Detected major version, if available. */\n majorVersion: number | null;\n}\n\n// ─── .node-version ─────────────────────────────────────────────\n\nexport function readNodeVersion(repoRoot: string): string | null {\n const nvFile = path.join(repoRoot, \".node-version\");\n if (!fs.existsSync(nvFile)) return null;\n try {\n const raw = fs.readFileSync(nvFile, \"utf-8\").trim();\n return raw.length > 0 ? raw.replace(/^v/, \"\") : null;\n } catch {\n return null;\n }\n}\n\n// ─── fnm probe ─────────────────────────────────────────────────\n\nfunction fnmCandidateDirs(): string[] {\n if (process.platform === \"win32\") {\n return [\n process.env.FNM_DIR,\n process.env.APPDATA ? path.join(process.env.APPDATA, \"fnm\") : null,\n process.env.LOCALAPPDATA\n ? path.join(process.env.LOCALAPPDATA, \"fnm\")\n : null,\n process.env.USERPROFILE\n ? path.join(process.env.USERPROFILE, \"scoop\", \"persist\", \"fnm\")\n : null,\n ].filter(Boolean) as string[];\n }\n // macOS / Linux\n return [\n process.env.FNM_DIR,\n process.env.HOME\n ? path.join(process.env.HOME, \".local\", \"share\", \"fnm\")\n : null,\n process.env.HOME ? path.join(process.env.HOME, \".fnm\") : null,\n process.env.XDG_DATA_HOME\n ? path.join(process.env.XDG_DATA_HOME, \"fnm\")\n : null,\n ].filter(Boolean) as string[];\n}\n\nfunction nodeExecutableName(): string {\n return process.platform === \"win32\" ? \"node.exe\" : \"node\";\n}\n\nexport function probeFnmNode(desiredVersion: string): string | null {\n const dirs = fnmCandidateDirs();\n const exe = nodeExecutableName();\n\n for (const baseDir of dirs) {\n const candidate = path.join(\n baseDir,\n \"node-versions\",\n `v${desiredVersion}`,\n \"installation\",\n exe,\n );\n if (!fs.existsSync(candidate)) continue;\n\n try {\n const v = execSync(`\"${candidate}\" --version`, {\n encoding: \"utf-8\",\n timeout: 5000,\n }).trim();\n if (v.startsWith(`v${desiredVersion.split(\".\")[0]}.`)) {\n return candidate;\n }\n } catch {\n // candidate exists but doesn't work — skip\n }\n }\n\n return null;\n}\n\n// ─── Version detection ─────────────────────────────────────────\n\nexport function detectNodeMajorVersion(command: string): number | null {\n try {\n const version = execSync(`\"${command}\" --version`, {\n encoding: \"utf-8\",\n timeout: 5000,\n }).trim();\n const match = version.match(/^v?(\\d+)\\./);\n return match ? parseInt(match[1], 10) : null;\n } catch {\n return null;\n }\n}\n\nexport function checkStripTypesSupport(command: string): boolean {\n const major = detectNodeMajorVersion(command);\n if (major !== null && major >= 22) return true;\n try {\n execSync(`\"${command}\" --experimental-strip-types -e \"\"`, {\n timeout: 5000,\n stdio: \"pipe\",\n });\n return true;\n } catch {\n return false;\n }\n}\n\n// ─── tsx fallback ──────────────────────────────────────────────\n\nexport function findTsxFallback(repoRoot: string): string | null {\n const candidates = [\n path.join(repoRoot, \"node_modules\", \".bin\", \"tsx.exe\"),\n path.join(repoRoot, \"node_modules\", \".bin\", \"tsx.CMD\"),\n path.join(repoRoot, \"node_modules\", \".bin\", \"tsx\"),\n ];\n for (const c of candidates) {\n if (fs.existsSync(c)) return c;\n }\n return null;\n}\n\n// ─── fnm bin directory (for PATH prepending) ───────────────────\n\n/**\n * Returns the directory containing the fnm-managed node binary,\n * suitable for prepending to PATH in child processes.\n */\nexport function getFnmBinDir(repoRoot: string): string | null {\n const desiredVersion = readNodeVersion(repoRoot);\n if (!desiredVersion) return null;\n\n const nodePath = probeFnmNode(desiredVersion);\n if (!nodePath) return null;\n\n return path.dirname(nodePath);\n}\n\n// ─── Main resolver ─────────────────────────────────────────────\n\n/**\n * Resolve the Node.js runtime to use for spawning child processes.\n *\n * Priority: bun passthrough → .node-version + fnm → configured command → tsx fallback\n */\nexport function resolveNodeRuntime(\n configCommand: string,\n repoRoot: string,\n): ResolvedRuntime {\n // Bun: native TS support, no strip-types needed\n if (configCommand === \"bun\" || configCommand.endsWith(\"bun.exe\")) {\n return {\n command: configCommand,\n supportsStripTypes: false,\n source: \"bun\",\n majorVersion: null,\n };\n }\n\n // .node-version + fnm discovery\n const desiredVersion = readNodeVersion(repoRoot);\n if (desiredVersion) {\n const fnmNode = probeFnmNode(desiredVersion);\n if (fnmNode) {\n const major = detectNodeMajorVersion(fnmNode);\n return {\n command: fnmNode,\n supportsStripTypes: checkStripTypesSupport(fnmNode),\n source: \"fnm\",\n majorVersion: major,\n };\n }\n }\n\n // Configured command (from config or PATH)\n const major = detectNodeMajorVersion(configCommand);\n if (major !== null) {\n return {\n command: configCommand,\n supportsStripTypes: checkStripTypesSupport(configCommand),\n source: major === detectNodeMajorVersion(\"node\") ? \"path\" : \"config\",\n majorVersion: major,\n };\n }\n\n // tsx fallback\n const tsx = findTsxFallback(repoRoot);\n if (tsx) {\n return {\n command: tsx,\n supportsStripTypes: false,\n source: \"tsx-fallback\",\n majorVersion: null,\n };\n }\n\n // Last resort\n return {\n command: configCommand,\n supportsStripTypes: false,\n source: \"path\",\n majorVersion: null,\n };\n}\n\n// ─── Env builder for child processes ───────────────────────────\n\n/**\n * Build an env object with fnm Node prepended to PATH.\n * Use this when spawning child processes that need the correct Node.\n */\nexport function buildRuntimeEnv(\n repoRoot: string,\n baseEnv: NodeJS.ProcessEnv = process.env,\n): NodeJS.ProcessEnv {\n const fnmBin = getFnmBinDir(repoRoot);\n if (!fnmBin) return { ...baseEnv };\n\n const pathKey = process.platform === \"win32\" ? \"Path\" : \"PATH\";\n const currentPath = baseEnv[pathKey] ?? baseEnv.PATH ?? \"\";\n\n return {\n ...baseEnv,\n [pathKey]: `${fnmBin}${path.delimiter}${currentPath}`,\n };\n}\n"],"mappings":";;;;;;;;;;;AAMA,YAAYA,SAAQ;AACpB,YAAY,YAAY;AAqFxB,SAAS,YACP,UACA,OACS;AACT,SAAO,cA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OAAO;AAAA,EACT,CAAC;AAGD,yBAAuB,UAAU,UAAU,QAAQ;AAEnD,QAAM,GAAG,QAAQ,CAAC,MAAqB,WAAkC;AACvE,QAAI,QAAQ;AACV,cAAQ,KAAK,QAAQ,KAAK,MAAM;AAChC;AAAA,IACF;AACA,YAAQ,KAAK,QAAQ,CAAC;AAAA,EACxB,CAAC;AAED,QAAM,GAAG,SAAS,CAAC,UAAiB;AAClC,YAAQ,MAAM,OAAO,KAAK,CAAC;AAC3B,YAAQ,KAAK,CAAC;AAAA,EAChB,CAAC;AACH;AAEA,KAAK,EAAE,MAAM,CAAC,UAAU;AACtB,UAAQ,MAAM,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,CAAC;AACpE,UAAQ,KAAK,CAAC;AAChB,CAAC;","names":["fs","fs","path","crypto","reviewFilePath","fs","path","fs","path","fs","path","major","createHeadlessLoop"]}
1
+ {"version":3,"sources":["../../src/engine/termination.ts","../../src/engine/review.ts","../../src/engine/headless-loop.ts","../../src/bridges/codex-bridge-runner.ts","../../src/config/resolve.ts","../../src/utils.ts","../../src/engine/bridge.ts","../../src/runtime/resolve-node.ts"],"sourcesContent":["/**\n * Termination engine — decides when a review session should stop.\n *\n * Strategies are evaluated in priority order. First non-\"continue\" verdict wins.\n * Default order: manual-stop → round-cap → repetition → quality → diff-insignificance\n */\nimport * as fs from \"node:fs\";\nimport * as crypto from \"node:crypto\";\n\n// ── Types ──────────────────────────────────────────────────────────\n\nexport type TerminationStrategy =\n | \"diff-insignificance\"\n | \"repetition-detection\"\n | \"quality-threshold\"\n | \"round-cap\"\n | \"manual-stop\";\n\nexport type TerminationVerdict = \"continue\" | \"stop\" | \"escalate\";\n\nexport type FindingSeverity =\n | \"critical\"\n | \"high\"\n | \"medium\"\n | \"low\"\n | \"nitpick\";\n\nexport interface ReviewFinding {\n severity: FindingSeverity;\n category: string;\n description: string;\n file?: string;\n line?: number;\n}\n\nexport interface ReviewRound {\n round: number;\n timestamp: string;\n findingCount: number;\n findings: ReviewFinding[];\n suggestedDiffLines: number;\n findingHash: string;\n}\n\nexport interface TerminationConfig {\n strategies: TerminationStrategy[];\n maxRounds: number;\n diffThreshold: number;\n repetitionThreshold: number;\n qualitySeverityFloor: FindingSeverity;\n}\n\nexport interface TerminationContext {\n round: number;\n rounds: ReviewRound[];\n stopSignalPath: string;\n config: TerminationConfig;\n}\n\nexport interface TerminationResult {\n verdict: TerminationVerdict;\n reason: string;\n strategy: TerminationStrategy;\n summary: string;\n}\n\n// ── Defaults ───────────────────────────────────────────────────────\n\nexport const DEFAULT_TERMINATION_CONFIG: TerminationConfig = {\n strategies: [\n \"manual-stop\",\n \"round-cap\",\n \"repetition-detection\",\n \"quality-threshold\",\n \"diff-insignificance\",\n ],\n maxRounds: 5,\n diffThreshold: 3,\n repetitionThreshold: 2,\n qualitySeverityFloor: \"high\",\n};\n\n// ── Severity ranking ───────────────────────────────────────────────\n\nconst SEVERITY_RANK: Record<FindingSeverity, number> = {\n critical: 5,\n high: 4,\n medium: 3,\n low: 2,\n nitpick: 1,\n};\n\nfunction isAtOrAbove(\n severity: FindingSeverity,\n floor: FindingSeverity,\n): boolean {\n return SEVERITY_RANK[severity] >= SEVERITY_RANK[floor];\n}\n\n// ── Finding hash ───────────────────────────────────────────────────\n\nexport function computeFindingHash(findings: ReviewFinding[]): string {\n const normalized = findings\n .filter((f) => isAtOrAbove(f.severity, \"high\"))\n .map((f) => `${f.category}:${f.description.slice(0, 100)}`)\n .sort()\n .join(\"|\");\n\n if (!normalized) return \"empty\";\n\n return crypto\n .createHash(\"sha256\")\n .update(normalized)\n .digest(\"hex\")\n .slice(0, 16);\n}\n\n// ── Strategy evaluators ────────────────────────────────────────────\n\nfunction evalManualStop(ctx: TerminationContext): TerminationResult | null {\n if (fs.existsSync(ctx.stopSignalPath)) {\n return {\n verdict: \"stop\",\n reason: `Manual stop signal found at ${ctx.stopSignalPath}`,\n strategy: \"manual-stop\",\n summary: `Review stopped manually at round ${ctx.round}`,\n };\n }\n return null;\n}\n\nfunction evalRoundCap(ctx: TerminationContext): TerminationResult | null {\n if (ctx.round >= ctx.config.maxRounds) {\n return {\n verdict: \"stop\",\n reason: `Round cap reached (${ctx.round}/${ctx.config.maxRounds})`,\n strategy: \"round-cap\",\n summary: `Review stopped at round cap (${ctx.config.maxRounds})`,\n };\n }\n return null;\n}\n\nfunction evalRepetition(ctx: TerminationContext): TerminationResult | null {\n if (ctx.rounds.length < 2) return null;\n\n const latest = ctx.rounds[ctx.rounds.length - 1];\n if (!latest) return null;\n\n let count = 0;\n for (const round of ctx.rounds) {\n if (round.findingHash === latest.findingHash) count++;\n }\n\n if (count >= ctx.config.repetitionThreshold) {\n return {\n verdict: \"stop\",\n reason: `Same finding hash repeated ${count} times (threshold: ${ctx.config.repetitionThreshold})`,\n strategy: \"repetition-detection\",\n summary: `Review going in circles — same findings repeated ${count}x`,\n };\n }\n\n return null;\n}\n\nfunction evalQualityThreshold(\n ctx: TerminationContext,\n): TerminationResult | null {\n if (ctx.rounds.length === 0) return null;\n\n const latest = ctx.rounds[ctx.rounds.length - 1];\n if (!latest) return null;\n\n // Guard: if parser extracted nothing at all (0 findings + 0 diff lines),\n // treat as inconclusive — not \"clean\". The parser may have failed to\n // extract from malformed output.\n if (\n latest.findingCount === 0 &&\n latest.suggestedDiffLines === 0 &&\n latest.findings.length === 0\n ) {\n return null; // inconclusive — continue to next strategy\n }\n\n const significantFindings = latest.findings.filter((f) =>\n isAtOrAbove(f.severity, ctx.config.qualitySeverityFloor),\n );\n\n if (significantFindings.length === 0) {\n return {\n verdict: \"stop\",\n reason: `No findings at ${ctx.config.qualitySeverityFloor}+ severity in round ${ctx.round}`,\n strategy: \"quality-threshold\",\n summary: `Review clean — no ${ctx.config.qualitySeverityFloor}+ findings in round ${ctx.round}`,\n };\n }\n\n return null;\n}\n\nfunction evalDiffInsignificance(\n ctx: TerminationContext,\n): TerminationResult | null {\n if (ctx.rounds.length === 0) return null;\n\n const latest = ctx.rounds[ctx.rounds.length - 1];\n if (!latest) return null;\n\n // Guard: same as quality-threshold — empty output is inconclusive, not trivial\n if (\n latest.findingCount === 0 &&\n latest.suggestedDiffLines === 0 &&\n latest.findings.length === 0\n ) {\n return null;\n }\n\n if (latest.suggestedDiffLines < ctx.config.diffThreshold) {\n return {\n verdict: \"stop\",\n reason: `Suggested diff (${latest.suggestedDiffLines} lines) below threshold (${ctx.config.diffThreshold})`,\n strategy: \"diff-insignificance\",\n summary: `Review suggestions are trivial (${latest.suggestedDiffLines} lines)`,\n };\n }\n\n return null;\n}\n\nconst STRATEGY_EVALUATORS: Record<\n TerminationStrategy,\n (ctx: TerminationContext) => TerminationResult | null\n> = {\n \"manual-stop\": evalManualStop,\n \"round-cap\": evalRoundCap,\n \"repetition-detection\": evalRepetition,\n \"quality-threshold\": evalQualityThreshold,\n \"diff-insignificance\": evalDiffInsignificance,\n};\n\n// ── Main evaluator ─────────────────────────────────────────────────\n\nexport function evaluate(ctx: TerminationContext): TerminationResult {\n for (const strategy of ctx.config.strategies) {\n const evaluator = STRATEGY_EVALUATORS[strategy];\n if (!evaluator) continue;\n\n const result = evaluator(ctx);\n if (result) return result;\n }\n\n return {\n verdict: \"continue\",\n reason: \"All strategies passed — review continues\",\n strategy:\n ctx.config.strategies[ctx.config.strategies.length - 1] ?? \"round-cap\",\n summary: `Round ${ctx.round} complete, continuing`,\n };\n}\n","/**\n * Review engine — detects review requests, builds prompts, parses output.\n *\n * This module handles the \"what\" of review sessions.\n * The termination engine handles the \"when to stop.\"\n * The bridge handles the \"how to deliver.\"\n */\nimport * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport * as crypto from \"node:crypto\";\nimport type {\n ReviewFinding,\n ReviewRound,\n FindingSeverity,\n TerminationConfig,\n} from \"./termination.js\";\nimport { computeFindingHash } from \"./termination.js\";\n\n// ── Types ──────────────────────────────────────────────────────────\n\nexport type AgentRole = \"reviewer\" | \"validator\" | \"long-running\";\n\nexport interface ReviewRequest {\n sourcePath: string;\n sender: string;\n recipient: string;\n prNumber: number;\n branch?: string;\n generation: string;\n isReReview: boolean;\n round: number;\n}\n\nexport interface ReviewSession {\n request: ReviewRequest;\n agentName: string;\n role: AgentRole;\n rounds: ReviewRound[];\n startedAt: string;\n terminatedAt?: string;\n reviewFilePath: string;\n}\n\nexport interface ReviewEngineConfig {\n role: AgentRole;\n generation: string;\n commsDir: string;\n repoRoot: string;\n agentName: string;\n termination: TerminationConfig;\n}\n\nexport interface HeadlessConfig {\n enabled: boolean;\n role: AgentRole;\n termination: TerminationConfig;\n}\n\n// ── Request Detection ──────────────────────────────────────────────\n\nconst REVIEW_KEYWORDS = [/리뷰\\s*요청/, /review[- ]?request/i];\n\nconst REREVIEW_KEYWORDS = [/재리뷰/, /re-?review/i];\n\nconst PR_NUMBER_PATTERNS = [\n /PR\\s*#?\\s*(\\d+)/i,\n /pull\\/(\\d+)/,\n /review[-_ ]?(\\d+)/i,\n];\n\n/**\n * Parse inbox filename to extract routing info.\n * Format: YYYYMMDD-sender-recipient-subject.md\n */\nexport function parseInboxFilename(filename: string): {\n date: string;\n sender: string;\n recipient: string;\n subject: string;\n} | null {\n const base = path.basename(filename, \".md\");\n const match = base.match(/^(\\d{8})-([^-]+)-([^-]+)-(.+)$/);\n if (!match) return null;\n\n return {\n date: match[1],\n sender: match[2],\n recipient: match[3],\n subject: match[4],\n };\n}\n\n/**\n * Extract PR number from text content.\n */\nexport function extractPrNumber(text: string): number | null {\n for (const pattern of PR_NUMBER_PATTERNS) {\n const match = text.match(pattern);\n if (match?.[1]) return parseInt(match[1], 10);\n }\n return null;\n}\n\n/**\n * Detect if a file represents a review request.\n * Returns a ReviewRequest if detected, null otherwise.\n */\nexport function detectReviewRequest(\n filePath: string,\n content: string,\n generation: string,\n): ReviewRequest | null {\n const parsed = parseInboxFilename(filePath);\n if (!parsed) return null;\n\n const fullText = `${parsed.subject} ${content}`;\n\n // Check for review keywords\n const isReview = REVIEW_KEYWORDS.some((re) => re.test(fullText));\n const isReReview = REREVIEW_KEYWORDS.some((re) => re.test(fullText));\n\n if (!isReview && !isReReview) return null;\n\n // Extract PR number\n const prNumber = extractPrNumber(fullText);\n if (!prNumber) return null;\n\n return {\n sourcePath: filePath,\n sender: parsed.sender,\n recipient: parsed.recipient,\n prNumber,\n generation,\n isReReview,\n round: isReReview ? 2 : 1, // Will be adjusted by session tracking\n };\n}\n\n// ── Review Prompt ──────────────────────────────────────────────────\n\nexport function buildReviewPrompt(\n request: ReviewRequest,\n agentName: string,\n round: number,\n): string {\n const roundLabel = round > 1 ? ` (re-review round ${round})` : \"\";\n\n return [\n `You are a code reviewer for the HUA Platform monorepo.`,\n ``,\n `## Task`,\n `Review PR #${request.prNumber}${roundLabel}.`,\n ``,\n `## Instructions`,\n `1. Run: gh pr diff ${request.prNumber}`,\n `2. Read changed files for understanding`,\n `3. Apply review checklist: security > data integrity > performance > error handling > code quality`,\n `4. Write structured findings`,\n ``,\n `## Output`,\n `Write review to: ${path.join(\"reviews\", request.generation, `review-PR${request.prNumber}-${agentName}.md`)}`,\n ``,\n `### Review File Format`,\n `\\`\\`\\`markdown`,\n `---`,\n `date: ${new Date().toISOString().split(\"T\")[0]}`,\n `reviewer: ${agentName}`,\n `pr: ${request.prNumber}`,\n `round: ${round}`,\n `status: clean | p1-Nitems | p2-Nitems`,\n `merge: merge | fix-then-merge | hold`,\n `---`,\n ``,\n `## Findings`,\n ``,\n `### Critical / High`,\n `- [severity] [category] file:line — description`,\n ``,\n `### Medium / Low`,\n `- [severity] [category] file:line — description`,\n ``,\n `## Checks`,\n `- [ ] Build verified`,\n `- [ ] Typecheck passed`,\n `- [ ] Scope check (only expected files changed)`,\n ``,\n `## Suggested Diff Lines`,\n `{number of lines the author should change to address findings}`,\n ``,\n `## Decision`,\n `{one-line merge recommendation}`,\n `\\`\\`\\``,\n ``,\n `## After Review`,\n `- Update reviews/INDEX.md`,\n `- Write inbox reply to ${request.sender}`,\n `- Commit and push comms changes`,\n ].join(\"\\n\");\n}\n\n// ── Review Output Parsing ──────────────────────────────────────────\n\nconst SEVERITY_PATTERNS: Record<FindingSeverity, RegExp> = {\n critical: /\\bcritical\\b/i,\n high: /\\bhigh\\b/i,\n medium: /\\bmedium\\b/i,\n low: /\\blow\\b/i,\n nitpick: /\\bnitpick\\b/i,\n};\n\nconst CATEGORY_PATTERNS = [\n \"security\",\n \"performance\",\n \"correctness\",\n \"data-integrity\",\n \"error-handling\",\n \"code-quality\",\n \"style\",\n];\n\n/**\n * Parse frontmatter from review file.\n */\nexport function parseFrontmatter(\n content: string,\n): Record<string, string> | null {\n const match = content.match(/^---\\n([\\s\\S]*?)\\n---/);\n if (!match?.[1]) return null;\n\n const fields: Record<string, string> = {};\n for (const line of match[1].split(\"\\n\")) {\n const kv = line.match(/^(\\w+):\\s*(.+)$/);\n if (kv?.[1] && kv[2]) {\n fields[kv[1]] = kv[2].trim();\n }\n }\n return fields;\n}\n\n/**\n * Extract suggested diff lines from review content.\n */\nexport function extractSuggestedDiffLines(content: string): number {\n const match = content.match(/## Suggested Diff Lines\\s*\\n\\s*(\\d+)/i);\n if (match?.[1]) return parseInt(match[1], 10);\n\n // Fallback: count lines in code blocks that look like suggestions\n const codeBlocks = content.match(/```[\\s\\S]*?```/g) ?? [];\n let totalLines = 0;\n for (const block of codeBlocks) {\n totalLines += block.split(\"\\n\").length - 2; // minus fences\n }\n return totalLines;\n}\n\n/**\n * Extract findings from review content.\n * Best-effort parsing — reviews may not follow exact format.\n */\nexport function extractFindings(content: string): ReviewFinding[] {\n const findings: ReviewFinding[] = [];\n\n // Match lines that look like finding entries\n const lines = content.split(\"\\n\");\n for (const line of lines) {\n const trimmed = line.trim();\n if (!trimmed.startsWith(\"-\") && !trimmed.startsWith(\"*\")) continue;\n\n // Detect severity\n let severity: FindingSeverity = \"medium\";\n for (const [sev, pattern] of Object.entries(SEVERITY_PATTERNS)) {\n if (pattern.test(trimmed)) {\n severity = sev as FindingSeverity;\n break;\n }\n }\n\n // Detect category\n let category = \"general\";\n for (const cat of CATEGORY_PATTERNS) {\n if (trimmed.toLowerCase().includes(cat)) {\n category = cat;\n break;\n }\n }\n\n // Extract file:line if present\n const fileMatch = trimmed.match(/([a-zA-Z0-9_/.-]+\\.[a-zA-Z]+):(\\d+)/);\n\n // Only include if it looks like an actual finding (has severity keyword or file ref)\n const hasSeverityKeyword = Object.values(SEVERITY_PATTERNS).some((p) =>\n p.test(trimmed),\n );\n if (hasSeverityKeyword || fileMatch) {\n findings.push({\n severity,\n category,\n description: trimmed.replace(/^[-*]\\s*/, \"\").slice(0, 200),\n file: fileMatch?.[1],\n line: fileMatch?.[2] ? parseInt(fileMatch[2], 10) : undefined,\n });\n }\n }\n\n return findings;\n}\n\n/**\n * Parse a review output file into a ReviewRound.\n */\nexport function parseReviewOutput(\n reviewFilePath: string,\n round: number,\n): ReviewRound | null {\n if (!fs.existsSync(reviewFilePath)) return null;\n\n const content = fs.readFileSync(reviewFilePath, \"utf-8\");\n const findings = extractFindings(content);\n const suggestedDiffLines = extractSuggestedDiffLines(content);\n\n return {\n round,\n timestamp: new Date().toISOString(),\n findingCount: findings.length,\n findings,\n suggestedDiffLines,\n findingHash: computeFindingHash(findings),\n };\n}\n\n// ── Review File Path ───────────────────────────────────────────────\n\nexport function reviewFilePath(\n commsDir: string,\n generation: string,\n prNumber: number,\n agentName: string,\n): string {\n return path.join(\n commsDir,\n \"reviews\",\n generation,\n `review-PR${prNumber}-${agentName}.md`,\n );\n}\n\n// ── Stale Detection ────────────────────────────────────────────────\n\n/**\n * Check if a review request is stale (already handled).\n * Mirrors PS1 Test-IsStaleRequest logic.\n */\nexport function isStaleReviewRequest(\n request: ReviewRequest,\n commsDir: string,\n agentName: string,\n): boolean {\n // 1. Check if review file exists and is newer than request\n const revPath = reviewFilePath(\n commsDir,\n request.generation,\n request.prNumber,\n agentName,\n );\n if (fs.existsSync(revPath) && fs.existsSync(request.sourcePath)) {\n const reviewStat = fs.statSync(revPath);\n const requestStat = fs.statSync(request.sourcePath);\n if (reviewStat.mtimeMs > requestStat.mtimeMs) return true;\n }\n\n return false;\n}\n\n// ── Processed Marker ───────────────────────────────────────────────\n\nexport function computeRequestMarkerId(filePath: string): string {\n const stat = fs.statSync(filePath);\n const input = `${filePath}|${stat.mtimeMs}`;\n return crypto.createHash(\"sha1\").update(input).digest(\"hex\");\n}\n\nexport function isAlreadyProcessed(\n stateDir: string,\n filePath: string,\n): boolean {\n const markerId = computeRequestMarkerId(filePath);\n return fs.existsSync(path.join(stateDir, \"processed\", `${markerId}.done`));\n}\n\nexport function unmarkProcessed(\n stateDir: string,\n request: ReviewRequest,\n): void {\n const markerId = computeRequestMarkerId(request.sourcePath);\n const markerPath = path.join(stateDir, \"processed\", `${markerId}.done`);\n if (fs.existsSync(markerPath)) {\n fs.unlinkSync(markerPath);\n }\n}\n\nexport function markAsProcessed(\n stateDir: string,\n request: ReviewRequest,\n): void {\n const markerId = computeRequestMarkerId(request.sourcePath);\n const markerDir = path.join(stateDir, \"processed\");\n fs.mkdirSync(markerDir, { recursive: true });\n const markerPath = path.join(markerDir, `${markerId}.done`);\n const payload = {\n prNumber: request.prNumber,\n sourcePath: request.sourcePath,\n processedAt: new Date().toISOString(),\n };\n const tmp = `${markerPath}.tmp.${process.pid}`;\n fs.writeFileSync(tmp, JSON.stringify(payload, null, 2), \"utf-8\");\n fs.renameSync(tmp, markerPath);\n}\n\n// ── Bridge Receipt ─────────────────────────────────────────────────\n\n/**\n * Write immediate inbox acknowledgment before review starts.\n * Mirrors PS1 Write-BridgeReceipt pattern.\n */\nexport function writeReviewReceipt(\n commsDir: string,\n request: ReviewRequest,\n agentName: string,\n): string {\n const date = new Date().toISOString().split(\"T\")[0].replace(/-/g, \"\");\n const filename = `${date}-${agentName}-${request.sender}-PR${request.prNumber}-ack.md`;\n const content = [\n `## ${agentName} > ${request.sender}`,\n ``,\n `- PR #${request.prNumber} review request received.`,\n `- headless reviewer processing.`,\n `- request: ${path.basename(request.sourcePath)}`,\n ].join(\"\\n\");\n\n const inboxDir = path.join(commsDir, \"inbox\");\n fs.mkdirSync(inboxDir, { recursive: true });\n const inboxPath = path.join(inboxDir, filename);\n const tmp = `${inboxPath}.tmp.${process.pid}`;\n fs.writeFileSync(tmp, content, \"utf-8\");\n fs.renameSync(tmp, inboxPath);\n return inboxPath;\n}\n\n// ── Orchestrator Entry Point ───────────────────────────────────\n\n/**\n * Check if the current bridge process is running in headless reviewer mode.\n * Reads from env vars set by engine/bridge.ts startBridge().\n */\nexport function isHeadlessReviewer(): boolean {\n return process.env.TAP_HEADLESS === \"true\";\n}\n\n/**\n * Get headless reviewer configuration from env vars.\n * Returns null if not in headless mode.\n */\nexport function getHeadlessEnvConfig(): {\n role: string;\n maxRounds: number;\n qualityFloor: string;\n} | null {\n if (!isHeadlessReviewer()) return null;\n return {\n role: process.env.TAP_AGENT_ROLE ?? \"reviewer\",\n maxRounds: parseInt(process.env.TAP_MAX_REVIEW_ROUNDS ?? \"5\", 10),\n qualityFloor: process.env.TAP_QUALITY_FLOOR ?? \"high\",\n };\n}\n\n/**\n * Scan inbox for pending review requests.\n * This is the entry point for the headless review loop.\n *\n * Phase 3 will wire this into the bridge runner's poll cycle:\n * 1. scanInboxForReviews() → detect pending requests\n * 2. For each: writeReviewReceipt() → dispatch to bridge → parseReviewOutput()\n * 3. evaluate() termination → continue or stop\n */\nexport function scanInboxForReviews(\n commsDir: string,\n stateDir: string,\n generation: string,\n agentName: string,\n): ReviewRequest[] {\n const inboxDir = path.join(commsDir, \"inbox\");\n if (!fs.existsSync(inboxDir)) return [];\n\n const files = fs.readdirSync(inboxDir).filter((f) => f.endsWith(\".md\"));\n const requests: ReviewRequest[] = [];\n\n for (const file of files) {\n const filePath = path.join(inboxDir, file);\n const content = fs.readFileSync(filePath, \"utf-8\");\n const request = detectReviewRequest(filePath, content, generation);\n\n if (!request) continue;\n\n // Only process requests addressed to this agent or broadcast (\"전체\"/\"all\")\n const to = request.recipient.toLowerCase();\n if (\n to !== agentName.toLowerCase() &&\n to !== \"전체\" &&\n to !== \"all\" &&\n to !== \"\"\n ) {\n continue;\n }\n\n if (isStaleReviewRequest(request, commsDir, agentName)) continue;\n if (isAlreadyProcessed(stateDir, filePath)) continue;\n\n requests.push(request);\n }\n\n return requests;\n}\n","/**\n * Headless review loop — poll-based review orchestrator for bridge processes.\n *\n * Runs alongside the bridge script. When TAP_HEADLESS=true:\n * 1. Periodically scans inbox for review requests\n * 2. Writes review dispatch files that the bridge picks up\n * 3. Monitors review output for completion\n * 4. Evaluates termination conditions\n * 5. Continues or stops the review session\n *\n * This is a control loop, not a WebSocket client — the bridge handles dispatch.\n */\nimport * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport {\n scanInboxForReviews,\n isHeadlessReviewer,\n getHeadlessEnvConfig,\n buildReviewPrompt,\n writeReviewReceipt,\n parseReviewOutput,\n reviewFilePath,\n markAsProcessed,\n unmarkProcessed,\n type ReviewRequest,\n type ReviewSession,\n} from \"./review.js\";\nimport {\n evaluate,\n DEFAULT_TERMINATION_CONFIG,\n type TerminationContext,\n type TerminationConfig,\n type FindingSeverity,\n} from \"./termination.js\";\n\n// ── Types ──────────────────────────────────────────────────────────\n\nexport interface HeadlessLoopOptions {\n commsDir: string;\n stateDir: string;\n repoRoot: string;\n agentName: string;\n generation: string;\n pollIntervalMs: number;\n}\n\nexport interface HeadlessLoopState {\n running: boolean;\n activeSession: ReviewSession | null;\n completedSessions: number;\n lastPollAt: string | null;\n}\n\n// ── Loop implementation ────────────────────────────────────────────\n\nexport function createHeadlessLoop(options: HeadlessLoopOptions): {\n start: () => void;\n stop: () => void;\n getState: () => HeadlessLoopState;\n} {\n const envConfig = getHeadlessEnvConfig();\n const terminationConfig: TerminationConfig = {\n ...DEFAULT_TERMINATION_CONFIG,\n maxRounds: envConfig?.maxRounds ?? DEFAULT_TERMINATION_CONFIG.maxRounds,\n qualitySeverityFloor:\n (envConfig?.qualityFloor as FindingSeverity) ??\n DEFAULT_TERMINATION_CONFIG.qualitySeverityFloor,\n };\n\n const state: HeadlessLoopState = {\n running: false,\n activeSession: null,\n completedSessions: 0,\n lastPollAt: null,\n };\n\n let timer: ReturnType<typeof setInterval> | null = null;\n\n function log(msg: string): void {\n const ts = new Date().toISOString();\n console.error(`[${ts}] [headless-loop] ${msg}`);\n }\n\n function pollOnce(): void {\n state.lastPollAt = new Date().toISOString();\n\n // Skip if already processing a review\n if (state.activeSession) {\n checkActiveSession();\n return;\n }\n\n // Scan for new review requests\n const requests = scanInboxForReviews(\n options.commsDir,\n options.stateDir,\n options.generation,\n options.agentName,\n );\n\n if (requests.length === 0) return;\n\n // Process first request (sequential — one at a time)\n const request = requests[0];\n startReviewSession(request);\n }\n\n function startReviewSession(request: ReviewRequest): void {\n log(`Starting review for PR #${request.prNumber}`);\n\n // Mark as processed EAGERLY to prevent race with generic bridge.\n // If anything fails after this point, we roll back the marker.\n markAsProcessed(options.stateDir, request);\n\n try {\n // Write receipt\n writeReviewReceipt(options.commsDir, request, options.agentName);\n\n // Build review prompt\n const prompt = buildReviewPrompt(request, options.agentName, 1);\n\n // Write dispatch file to commsDir/inbox/ — the bridge watches this\n // directory and will inject it as a turn/start\n const date = new Date().toISOString().split(\"T\")[0].replace(/-/g, \"\");\n const dispatchFilename = `${date}-headless-${options.agentName}-review-PR${request.prNumber}.md`;\n const inboxDir = path.join(options.commsDir, \"inbox\");\n fs.mkdirSync(inboxDir, { recursive: true });\n const dispatchFile = path.join(inboxDir, dispatchFilename);\n const tmp = `${dispatchFile}.tmp.${process.pid}`;\n fs.writeFileSync(tmp, prompt, \"utf-8\");\n fs.renameSync(tmp, dispatchFile);\n\n state.activeSession = {\n request,\n agentName: options.agentName,\n role:\n (envConfig?.role as \"reviewer\" | \"validator\" | \"long-running\") ??\n \"reviewer\",\n rounds: [],\n startedAt: new Date().toISOString(),\n reviewFilePath: reviewFilePath(\n options.commsDir,\n request.generation,\n request.prNumber,\n options.agentName,\n ),\n };\n\n log(`Dispatched review prompt for PR #${request.prNumber} (round 1)`);\n } catch (err) {\n // Roll back processed marker so request can be retried on next poll\n log(\n `Failed to start review for PR #${request.prNumber}: ${err instanceof Error ? err.message : String(err)}`,\n );\n unmarkProcessed(options.stateDir, request);\n }\n }\n\n function checkActiveSession(): void {\n if (!state.activeSession) return;\n\n const session = state.activeSession;\n const revPath = session.reviewFilePath;\n\n // Check if review output file has been updated since last check\n if (!fs.existsSync(revPath)) return;\n\n const stat = fs.statSync(revPath);\n const lastRound = session.rounds[session.rounds.length - 1];\n const lastCheck = lastRound?.timestamp ?? session.startedAt;\n\n // Only process if file is newer than our last check\n if (stat.mtime.toISOString() <= lastCheck) return;\n\n // Parse the review output\n const roundNum = session.rounds.length + 1;\n const round = parseReviewOutput(revPath, roundNum);\n if (!round) return;\n\n session.rounds.push(round);\n log(\n `PR #${session.request.prNumber} round ${roundNum}: ${round.findingCount} findings, ${round.suggestedDiffLines} suggested diff lines`,\n );\n\n // Evaluate termination\n const stopSignalPath = path.join(options.stateDir, \"stop-signal\");\n const ctx: TerminationContext = {\n round: roundNum,\n rounds: session.rounds,\n stopSignalPath,\n config: terminationConfig,\n };\n\n const result = evaluate(ctx);\n\n if (result.verdict === \"stop\") {\n log(\n `PR #${session.request.prNumber} terminated: ${result.reason} (${result.strategy})`,\n );\n completeSession(session);\n } else {\n log(`PR #${session.request.prNumber} continues to round ${roundNum + 1}`);\n dispatchFollowUp(session, roundNum + 1);\n }\n }\n\n function dispatchFollowUp(session: ReviewSession, round: number): void {\n const prompt = buildReviewPrompt(session.request, options.agentName, round);\n\n // Write follow-up dispatch to commsDir/inbox/ for bridge to steer\n const date = new Date().toISOString().split(\"T\")[0].replace(/-/g, \"\");\n const dispatchFilename = `${date}-headless-${options.agentName}-review-PR${session.request.prNumber}-r${round}.md`;\n const inboxDir = path.join(options.commsDir, \"inbox\");\n fs.mkdirSync(inboxDir, { recursive: true });\n const dispatchFile = path.join(inboxDir, dispatchFilename);\n const tmp = `${dispatchFile}.tmp.${process.pid}`;\n fs.writeFileSync(tmp, prompt, \"utf-8\");\n fs.renameSync(tmp, dispatchFile);\n }\n\n function completeSession(session: ReviewSession): void {\n session.terminatedAt = new Date().toISOString();\n\n // Note: request was already marked as processed eagerly in startReviewSession()\n\n // Clean up dispatch files from inbox\n const inboxDir = path.join(options.commsDir, \"inbox\");\n if (fs.existsSync(inboxDir)) {\n const prefix = `headless-${options.agentName}-review-PR${session.request.prNumber}`;\n const files = fs.readdirSync(inboxDir).filter((f) => f.includes(prefix));\n for (const f of files) {\n fs.unlinkSync(path.join(inboxDir, f));\n }\n }\n\n state.activeSession = null;\n state.completedSessions++;\n log(\n `PR #${session.request.prNumber} review complete (${session.rounds.length} rounds)`,\n );\n }\n\n return {\n start() {\n if (!isHeadlessReviewer()) {\n log(\"Not in headless mode — loop not started\");\n return;\n }\n\n state.running = true;\n log(\n `Headless review loop started (${envConfig?.role ?? \"reviewer\"}, poll ${options.pollIntervalMs}ms, max ${terminationConfig.maxRounds} rounds)`,\n );\n\n // Initial poll\n pollOnce();\n\n // Set up interval\n timer = setInterval(pollOnce, options.pollIntervalMs);\n },\n\n stop() {\n state.running = false;\n if (timer) {\n clearInterval(timer);\n timer = null;\n }\n log(\"Headless review loop stopped\");\n },\n\n getState() {\n return { ...state };\n },\n };\n}\n","import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport { spawn } from \"node:child_process\";\nimport { fileURLToPath, pathToFileURL } from \"node:url\";\nimport {\n resolveConfig,\n SHARED_CONFIG_FILE,\n LOCAL_CONFIG_FILE,\n} from \"../config/index.js\";\nimport { resolveAppServerUrl } from \"../engine/bridge.js\";\nimport { resolveNodeRuntime, buildRuntimeEnv } from \"../runtime/index.js\";\n\n// ─── Repo root discovery (fallback for unbundled runs) ─────────\n\nfunction findRepoRootFromRunner(): string | null {\n let dir = path.resolve(path.dirname(fileURLToPath(import.meta.url)));\n\n while (true) {\n if (fs.existsSync(path.join(dir, SHARED_CONFIG_FILE))) return dir;\n if (fs.existsSync(path.join(dir, LOCAL_CONFIG_FILE))) return dir;\n if (fs.existsSync(path.join(dir, \"scripts\", \"codex-app-server-bridge.ts\")))\n return dir;\n const parent = path.dirname(dir);\n if (parent === dir) return null;\n dir = parent;\n }\n}\n\n// ─── Headless review loop integration ──────────────────────────\n\nfunction maybeStartHeadlessLoop(\n repoRoot: string,\n commsDir: string,\n stateDir: string | undefined,\n): void {\n if (process.env.TAP_HEADLESS !== \"true\") return;\n\n // Dynamic import to avoid loading review/termination engines in non-headless mode\n import(\"../engine/headless-loop.js\")\n .then(({ createHeadlessLoop }) => {\n const agentName =\n process.env.TAP_AGENT_NAME ??\n process.env.CODEX_TAP_AGENT_NAME ??\n \"reviewer\";\n const generation = process.env.TAP_REVIEW_GENERATION ?? \"gen11\";\n const resolvedStateDir = stateDir ?? path.join(repoRoot, \".tap-comms\");\n\n const loop = createHeadlessLoop({\n commsDir,\n stateDir: resolvedStateDir,\n repoRoot,\n agentName,\n generation,\n pollIntervalMs: 3_000, // Poll faster than generic bridge (5s) for review priority\n });\n\n loop.start();\n\n // Clean shutdown\n process.on(\"SIGTERM\", () => loop.stop());\n process.on(\"SIGINT\", () => loop.stop());\n })\n .catch((err) => {\n console.error(\"[headless-loop] Failed to start:\", err);\n });\n}\n\n// ─── Main ──────────────────────────────────────────────────────\n\ninterface BridgeScriptArgsOptions {\n repoRoot: string;\n commsDir: string;\n appServerUrl: string;\n gatewayTokenFile?: string;\n stateDir?: string;\n agentName?: string;\n}\n\nexport function resolveBridgeDaemonScript(\n repoRoot: string,\n runnerUrl: string = import.meta.url,\n fileExists: (candidate: string) => boolean = fs.existsSync,\n): string | null {\n const moduleDir = path.dirname(fileURLToPath(runnerUrl));\n const candidates = [\n // 1. Bundled standalone/npm install\n path.join(moduleDir, \"codex-app-server-bridge.mjs\"),\n // 2. Source run from monorepo package\n path.join(moduleDir, \"codex-app-server-bridge.ts\"),\n // 3. Built monorepo package dist\n path.join(\n repoRoot,\n \"packages\",\n \"tap-comms\",\n \"dist\",\n \"bridges\",\n \"codex-app-server-bridge.mjs\",\n ),\n // 4. Monorepo source wrapper\n path.join(\n repoRoot,\n \"packages\",\n \"tap-comms\",\n \"src\",\n \"bridges\",\n \"codex-app-server-bridge.ts\",\n ),\n // 5. Legacy monorepo root script\n path.join(repoRoot, \"scripts\", \"codex-app-server-bridge.ts\"),\n ];\n\n for (const candidate of candidates) {\n if (fileExists(candidate)) {\n return candidate;\n }\n }\n\n return null;\n}\n\nexport function buildBridgeScriptArgs(\n scriptPath: string,\n options: BridgeScriptArgsOptions,\n): string[] {\n const args = [\n scriptPath,\n `--repo-root=${options.repoRoot}`,\n `--comms-dir=${options.commsDir}`,\n `--app-server-url=${options.appServerUrl}`,\n ];\n\n if (options.agentName) {\n args.push(`--agent-name=${options.agentName}`);\n }\n\n if (options.gatewayTokenFile) {\n args.push(`--gateway-token-file=${options.gatewayTokenFile}`);\n }\n\n if (options.stateDir) {\n args.push(`--state-dir=${options.stateDir}`);\n }\n\n return args;\n}\n\nasync function main(): Promise<void> {\n const repoRootHint = findRepoRootFromRunner() ?? undefined;\n const { config } = resolveConfig({}, repoRootHint);\n\n const repoRoot = config.repoRoot;\n const commsDir = config.commsDir;\n const instancePortRaw = process.env.TAP_BRIDGE_PORT;\n const instancePort = instancePortRaw\n ? Number.parseInt(instancePortRaw, 10)\n : undefined;\n const envAppServerUrl = process.env.CODEX_APP_SERVER_URL?.trim();\n const gatewayTokenFile = process.env.TAP_GATEWAY_TOKEN_FILE?.trim();\n const appServerUrl =\n envAppServerUrl ||\n resolveAppServerUrl(\n config.appServerUrl,\n Number.isFinite(instancePort) ? instancePort : undefined,\n );\n\n // Multi-instance: derive instance-specific state dir\n // Honor TAP_STATE_DIR env (set by config resolver) before falling back to .tmp/\n const instanceId = process.env.TAP_BRIDGE_INSTANCE_ID;\n const envStateDir = process.env.TAP_STATE_DIR;\n const stateDir = envStateDir\n ? envStateDir\n : instanceId\n ? path.join(repoRoot, \".tmp\", `codex-app-server-bridge-${instanceId}`)\n : undefined;\n\n // Honor pre-resolved node from parent (2-stage spawn: engine → runner → daemon)\n // TAP_STRIP_TYPES preserves metadata so bun doesn't get --experimental-strip-types.\n const preResolved = process.env.TAP_RESOLVED_NODE;\n const resolved = preResolved\n ? {\n command: preResolved,\n supportsStripTypes: process.env.TAP_STRIP_TYPES === \"1\",\n source: \"env\" as const,\n majorVersion: null,\n }\n : resolveNodeRuntime(config.runtimeCommand, repoRoot);\n\n const command = resolved.command;\n const agentName =\n process.env.TAP_AGENT_NAME?.trim() ||\n process.env.CODEX_TAP_AGENT_NAME?.trim() ||\n undefined;\n\n // Locate bridge script\n const scriptPath = resolveBridgeDaemonScript(repoRoot);\n if (!scriptPath) {\n throw new Error(\n `Bridge script not found for repo root ${repoRoot}.\\n` +\n `Expected a packaged dist/bridges/codex-app-server-bridge.mjs or monorepo bridge script.`,\n );\n }\n\n // Build args\n const args: string[] = [];\n if (resolved.supportsStripTypes) {\n args.push(\"--experimental-strip-types\");\n }\n args.push(\n ...buildBridgeScriptArgs(scriptPath, {\n repoRoot,\n commsDir,\n appServerUrl,\n gatewayTokenFile,\n stateDir,\n agentName,\n }),\n );\n\n // Forward bridge operational flags from env (set by engine/bridge.ts)\n const busyMode = process.env.TAP_BUSY_MODE;\n if (busyMode) args.push(`--busy-mode=${busyMode}`);\n\n const pollSeconds = process.env.TAP_POLL_SECONDS;\n if (pollSeconds) args.push(`--poll-seconds=${pollSeconds}`);\n\n const reconnectSeconds = process.env.TAP_RECONNECT_SECONDS;\n if (reconnectSeconds) args.push(`--reconnect-seconds=${reconnectSeconds}`);\n\n const lookbackMinutes = process.env.TAP_MESSAGE_LOOKBACK_MINUTES;\n if (lookbackMinutes)\n args.push(`--message-lookback-minutes=${lookbackMinutes}`);\n\n const threadId = process.env.TAP_THREAD_ID;\n if (threadId) args.push(`--thread-id=${threadId}`);\n\n if (process.env.TAP_EPHEMERAL === \"true\") args.push(\"--ephemeral\");\n if (process.env.TAP_PROCESS_EXISTING === \"true\")\n args.push(\"--process-existing-messages\");\n\n // Spawn with fnm-aware PATH so any further child spawns also find the right Node\n const runtimeEnv = buildRuntimeEnv(repoRoot);\n\n const child = spawn(command, args, {\n cwd: repoRoot,\n env: runtimeEnv,\n stdio: \"inherit\",\n });\n\n // Start headless review loop if in headless mode\n maybeStartHeadlessLoop(repoRoot, commsDir, stateDir);\n\n child.on(\"exit\", (code: number | null, signal: NodeJS.Signals | null) => {\n if (signal) {\n process.kill(process.pid, signal);\n return;\n }\n process.exit(code ?? 0);\n });\n\n child.on(\"error\", (error: Error) => {\n console.error(String(error));\n process.exit(1);\n });\n}\n\nfunction isDirectExecution(): boolean {\n const entry = process.argv[1];\n if (!entry) return false;\n return import.meta.url === pathToFileURL(path.resolve(entry)).href;\n}\n\nif (isDirectExecution()) {\n main().catch((error) => {\n console.error(error instanceof Error ? error.message : String(error));\n process.exit(1);\n });\n}\n","import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport type {\n TapSharedConfig,\n TapLocalConfig,\n TapResolvedConfig,\n ConfigSource,\n ConfigResolution,\n} from \"./types.js\";\n\n// ─── File names ────────────────────────────────────────────────\n\nexport const SHARED_CONFIG_FILE = \"tap-config.json\";\nexport const LOCAL_CONFIG_FILE = \"tap-config.local.json\";\nexport const LEGACY_CONFIG_FILE = \".tap-config\";\n\n// ─── Defaults ──────────────────────────────────────────────────\n\nconst DEFAULT_RUNTIME_COMMAND = \"node\";\nconst DEFAULT_APP_SERVER_URL = \"ws://127.0.0.1:4501\";\n\n// ─── Repo root discovery ───────────────────────────────────────\n\nimport { _noGitWarned, _setNoGitWarned } from \"../utils.js\";\n\nexport function findRepoRoot(startDir: string = process.cwd()): string {\n let dir = path.resolve(startDir);\n while (true) {\n if (fs.existsSync(path.join(dir, \".git\"))) return dir;\n if (fs.existsSync(path.join(dir, \"package.json\"))) {\n if (!_noGitWarned) {\n _setNoGitWarned();\n console.error(\n \"[tap] warning: No .git directory found. Resolved via package.json. Use --comms-dir to specify explicitly.\",\n );\n }\n return dir;\n }\n const parent = path.dirname(dir);\n if (parent === dir) break;\n dir = parent;\n }\n if (!_noGitWarned) {\n _setNoGitWarned();\n console.error(\n \"[tap] warning: No git repository found. Using cwd as root. Run 'git init' or use --comms-dir.\",\n );\n }\n return process.cwd();\n}\n\n// ─── JSON file loading ─────────────────────────────────────────\n\nfunction loadJsonFile<T>(filePath: string): T | null {\n if (!fs.existsSync(filePath)) return null;\n try {\n const raw = fs.readFileSync(filePath, \"utf-8\");\n return JSON.parse(raw) as T;\n } catch {\n return null;\n }\n}\n\nexport function loadSharedConfig(repoRoot: string): TapSharedConfig | null {\n return loadJsonFile<TapSharedConfig>(path.join(repoRoot, SHARED_CONFIG_FILE));\n}\n\nexport function loadLocalConfig(repoRoot: string): TapLocalConfig | null {\n return loadJsonFile<TapLocalConfig>(path.join(repoRoot, LOCAL_CONFIG_FILE));\n}\n\nfunction readLegacyShellValue(configText: string, key: string): string | null {\n const match = configText.match(new RegExp(`^${key}=\"?(.+?)\"?$`, \"m\"));\n return match?.[1]?.trim() || null;\n}\n\nfunction loadLegacyShellConfig(repoRoot: string): TapSharedConfig | null {\n const filePath = path.join(repoRoot, LEGACY_CONFIG_FILE);\n if (!fs.existsSync(filePath)) return null;\n\n try {\n const raw = fs.readFileSync(filePath, \"utf-8\");\n const commsDir = readLegacyShellValue(raw, \"TAP_COMMS_DIR\");\n if (!commsDir) return null;\n return { commsDir };\n } catch {\n return null;\n }\n}\n\n// ─── CLI overrides ─────────────────────────────────────────────\n\nexport interface ConfigOverrides {\n commsDir?: string;\n stateDir?: string;\n runtimeCommand?: string;\n appServerUrl?: string;\n}\n\n// ─── Resolution ────────────────────────────────────────────────\n\n/**\n * Resolve config with priority: CLI flag > env > local config > shared config > auto.\n */\nexport function resolveConfig(\n overrides: ConfigOverrides = {},\n startDir?: string,\n): ConfigResolution {\n const repoRoot = findRepoRoot(startDir);\n const shared = loadSharedConfig(repoRoot) ?? {};\n const local = loadLocalConfig(repoRoot) ?? {};\n const legacy = loadLegacyShellConfig(repoRoot) ?? {};\n\n const sources: Record<keyof TapResolvedConfig, ConfigSource> = {\n repoRoot: \"auto\",\n commsDir: \"auto\",\n stateDir: \"auto\",\n runtimeCommand: \"auto\",\n appServerUrl: \"auto\",\n };\n\n // ─── commsDir ──────────────────────────────────────────────\n let commsDir: string;\n if (overrides.commsDir) {\n commsDir = resolvePath(repoRoot, overrides.commsDir);\n sources.commsDir = \"cli-flag\";\n } else if (process.env.TAP_COMMS_DIR) {\n commsDir = resolvePath(repoRoot, process.env.TAP_COMMS_DIR);\n sources.commsDir = \"env\";\n } else if (local.commsDir) {\n commsDir = resolvePath(repoRoot, local.commsDir);\n sources.commsDir = \"local-config\";\n } else if (shared.commsDir) {\n commsDir = resolvePath(repoRoot, shared.commsDir);\n sources.commsDir = \"shared-config\";\n } else if (legacy.commsDir) {\n commsDir = resolvePath(repoRoot, legacy.commsDir);\n sources.commsDir = \"legacy-shell-config\";\n } else {\n commsDir = path.join(repoRoot, \"tap-comms\");\n }\n\n // ─── stateDir ──────────────────────────────────────────────\n let stateDir: string;\n if (overrides.stateDir) {\n stateDir = resolvePath(repoRoot, overrides.stateDir);\n sources.stateDir = \"cli-flag\";\n } else if (process.env.TAP_STATE_DIR) {\n stateDir = resolvePath(repoRoot, process.env.TAP_STATE_DIR);\n sources.stateDir = \"env\";\n } else if (local.stateDir) {\n stateDir = resolvePath(repoRoot, local.stateDir);\n sources.stateDir = \"local-config\";\n } else if (shared.stateDir) {\n stateDir = resolvePath(repoRoot, shared.stateDir);\n sources.stateDir = \"shared-config\";\n } else {\n stateDir = path.join(repoRoot, \".tap-comms\");\n }\n\n // ─── runtimeCommand ────────────────────────────────────────\n let runtimeCommand: string;\n if (overrides.runtimeCommand) {\n runtimeCommand = overrides.runtimeCommand;\n sources.runtimeCommand = \"cli-flag\";\n } else if (process.env.TAP_RUNTIME_COMMAND) {\n runtimeCommand = process.env.TAP_RUNTIME_COMMAND;\n sources.runtimeCommand = \"env\";\n } else if (local.runtimeCommand) {\n runtimeCommand = local.runtimeCommand;\n sources.runtimeCommand = \"local-config\";\n } else if (shared.runtimeCommand) {\n runtimeCommand = shared.runtimeCommand;\n sources.runtimeCommand = \"shared-config\";\n } else {\n runtimeCommand = DEFAULT_RUNTIME_COMMAND;\n }\n\n // ─── appServerUrl ──────────────────────────────────────────\n let appServerUrl: string;\n if (overrides.appServerUrl) {\n appServerUrl = overrides.appServerUrl;\n sources.appServerUrl = \"cli-flag\";\n } else if (process.env.TAP_APP_SERVER_URL) {\n appServerUrl = process.env.TAP_APP_SERVER_URL;\n sources.appServerUrl = \"env\";\n } else if (local.appServerUrl) {\n appServerUrl = local.appServerUrl;\n sources.appServerUrl = \"local-config\";\n } else if (shared.appServerUrl) {\n appServerUrl = shared.appServerUrl;\n sources.appServerUrl = \"shared-config\";\n } else {\n appServerUrl = DEFAULT_APP_SERVER_URL;\n }\n\n return {\n config: { repoRoot, commsDir, stateDir, runtimeCommand, appServerUrl },\n sources,\n };\n}\n\n// ─── Save helpers ──────────────────────────────────────────────\n\nexport function saveSharedConfig(\n repoRoot: string,\n config: TapSharedConfig,\n): void {\n const filePath = path.join(repoRoot, SHARED_CONFIG_FILE);\n const tmp = `${filePath}.tmp.${process.pid}`;\n fs.writeFileSync(tmp, JSON.stringify(config, null, 2) + \"\\n\", \"utf-8\");\n fs.renameSync(tmp, filePath);\n}\n\nexport function saveLocalConfig(\n repoRoot: string,\n config: TapLocalConfig,\n): void {\n const filePath = path.join(repoRoot, LOCAL_CONFIG_FILE);\n const tmp = `${filePath}.tmp.${process.pid}`;\n fs.writeFileSync(tmp, JSON.stringify(config, null, 2) + \"\\n\", \"utf-8\");\n fs.renameSync(tmp, filePath);\n}\n\n// ─── Helpers ───────────────────────────────────────────────────\n\n/** Resolve a path relative to repoRoot, or keep absolute as-is. */\nfunction resolvePath(repoRoot: string, p: string): string {\n const normalized = normalizeTapPath(p);\n return path.isAbsolute(normalized)\n ? normalized\n : path.resolve(repoRoot, normalized);\n}\n\nfunction normalizeTapPath(input: string): string {\n const trimmed = input.trim().replace(/^[\"'`]+|[\"'`]+$/g, \"\");\n if (/^[A-Za-z]:[\\\\/]/.test(trimmed)) {\n return trimmed;\n }\n\n // MSYS/Git Bash `/c/...` → `C:\\...` conversion — Windows only.\n // On POSIX, `/d/...` is a legitimate absolute path and must not be rewritten.\n if (process.platform === \"win32\") {\n const match = trimmed.match(/^\\/([A-Za-z])\\/(.*)$/);\n if (match) {\n return `${match[1].toUpperCase()}:\\\\${match[2].replace(/\\//g, \"\\\\\")}`;\n }\n }\n\n return trimmed;\n}\n","import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport type {\n AdapterContext,\n CommandCode,\n InstanceId,\n Platform,\n RuntimeName,\n TapState,\n} from \"./types.js\";\nimport { resolveConfig } from \"./config/index.js\";\n\nconst VALID_RUNTIMES: RuntimeName[] = [\"claude\", \"codex\", \"gemini\"];\n\nexport function isValidRuntime(name: string): name is RuntimeName {\n return VALID_RUNTIMES.includes(name as RuntimeName);\n}\n\nexport function detectPlatform(): Platform {\n return process.platform as Platform;\n}\n\n/** Shared flag: suppress duplicate no-git warnings across modules. */\nexport let _noGitWarned = false;\n\nexport function _setNoGitWarned() {\n _noGitWarned = true;\n}\n\nexport function findRepoRoot(startDir: string = process.cwd()): string {\n let dir = path.resolve(startDir);\n while (true) {\n if (fs.existsSync(path.join(dir, \".git\"))) return dir;\n if (fs.existsSync(path.join(dir, \"package.json\"))) {\n if (!_noGitWarned) {\n _setNoGitWarned();\n logWarn(\n \"No .git directory found. Resolved repo root via package.json — \" +\n \"comms directory may be created in an unexpected location. \" +\n \"Use --comms-dir to specify explicitly.\",\n );\n }\n return dir;\n }\n const parent = path.dirname(dir);\n if (parent === dir) break;\n dir = parent;\n }\n if (!_noGitWarned) {\n _setNoGitWarned();\n logWarn(\n \"No git repository or package.json found. Using current directory as root. \" +\n \"Run 'git init' first, or use --comms-dir to specify the comms path.\",\n );\n }\n return process.cwd();\n}\n\nexport function resolveCommsDir(args: string[], repoRoot: string): string {\n // Check --comms-dir flag\n const idx = args.indexOf(\"--comms-dir\");\n if (idx !== -1 && args[idx + 1]) {\n return path.resolve(args[idx + 1]);\n }\n\n // Delegate to config resolution (env > local > shared > auto)\n const { config } = resolveConfig({}, repoRoot);\n return config.commsDir;\n}\n\nexport function createAdapterContext(\n commsDir: string,\n repoRoot: string,\n): AdapterContext {\n // Use config-resolved stateDir if available\n const { config } = resolveConfig({}, repoRoot);\n return {\n commsDir: path.resolve(commsDir),\n repoRoot: path.resolve(repoRoot),\n stateDir: config.stateDir,\n platform: detectPlatform(),\n };\n}\n\nexport function parseArgs(args: string[]): {\n positional: string[];\n flags: Record<string, string | boolean>;\n} {\n const positional: string[] = [];\n const flags: Record<string, string | boolean> = {};\n\n for (let i = 0; i < args.length; i++) {\n const arg = args[i];\n if (arg.startsWith(\"--\")) {\n const key = arg.slice(2);\n const next = args[i + 1];\n if (next && !next.startsWith(\"--\")) {\n flags[key] = next;\n i++;\n } else {\n flags[key] = true;\n }\n } else if (arg.startsWith(\"-\")) {\n flags[arg.slice(1)] = true;\n } else {\n positional.push(arg);\n }\n }\n\n return { positional, flags };\n}\n\n// ─── JSON mode suppression ──────────────────────────────────────\n\nlet _jsonMode = false;\n\nexport function setJsonMode(enabled: boolean): void {\n _jsonMode = enabled;\n}\n\nexport function log(message: string): void {\n if (!_jsonMode) console.log(` ${message}`);\n}\n\nexport function logSuccess(message: string): void {\n if (!_jsonMode) console.log(` + ${message}`);\n}\n\nexport function logWarn(message: string): void {\n if (!_jsonMode) console.log(` ! ${message}`);\n}\n\nexport function logError(message: string): void {\n if (!_jsonMode) console.error(` x ${message}`);\n}\n\nexport function logHeader(message: string): void {\n if (!_jsonMode) console.log(`\\n ${message}\\n`);\n}\n\n// ─── Instance ID utilities ─────────────────────────────────────\n\nexport type ResolveResult =\n | { ok: true; instanceId: InstanceId }\n | { ok: false; code: CommandCode; message: string };\n\n/**\n * Resolve a user-provided identifier to an instance ID.\n * Accepts either an exact instance ID or a runtime name (if unambiguous).\n */\nexport function resolveInstanceId(\n identifier: string,\n state: TapState,\n): ResolveResult {\n // Exact match\n if (state.instances[identifier]) {\n return { ok: true, instanceId: identifier };\n }\n\n // Runtime name → find matching instances\n if (isValidRuntime(identifier)) {\n const matches = Object.values(state.instances).filter(\n (inst) => inst.runtime === identifier,\n );\n\n if (matches.length === 1) {\n return { ok: true, instanceId: matches[0].instanceId };\n }\n\n if (matches.length > 1) {\n const ids = matches.map((m) => m.instanceId).join(\", \");\n return {\n ok: false,\n code: \"TAP_INSTANCE_AMBIGUOUS\",\n message: `Multiple ${identifier} instances found: ${ids}. Specify one explicitly.`,\n };\n }\n }\n\n return {\n ok: false,\n code: \"TAP_INSTANCE_NOT_FOUND\",\n message: `Instance not found: ${identifier}`,\n };\n}\n\n/** Build an instance ID from runtime + optional name. */\nexport function buildInstanceId(\n runtime: RuntimeName,\n name?: string,\n): InstanceId {\n return name ? `${runtime}-${name}` : runtime;\n}\n\n/** Extract the runtime name from an instance ID. */\nexport function extractRuntimeFromInstanceId(id: InstanceId): RuntimeName {\n for (const r of VALID_RUNTIMES) {\n if (id === r || id.startsWith(`${r}-`)) return r;\n }\n throw new Error(`Cannot extract runtime from instance ID: ${id}`);\n}\n\n/** Check if a port is already claimed by another instance. */\nexport function findPortConflict(\n state: TapState,\n port: number,\n excludeInstanceId?: InstanceId,\n): InstanceId | null {\n for (const [id, inst] of Object.entries(state.instances)) {\n if (id !== excludeInstanceId && inst.port === port) return id;\n }\n return null;\n}\n\n/** Find the next available port starting from basePort (default 4501). */\nexport function findNextAvailablePort(\n state: TapState,\n basePort: number = 4501,\n excludeInstanceId?: InstanceId,\n): number {\n let port = basePort;\n while (findPortConflict(state, port, excludeInstanceId) !== null) {\n port++;\n }\n return port;\n}\n","import * as fs from \"node:fs\";\nimport * as net from \"node:net\";\nimport * as path from \"node:path\";\nimport { randomBytes } from \"node:crypto\";\nimport { spawn, spawnSync, execSync } from \"node:child_process\";\nimport { fileURLToPath } from \"node:url\";\nimport type {\n RuntimeName,\n InstanceId,\n BridgeState,\n AppServerState,\n AppServerAuthState,\n HeadlessConfig,\n Platform,\n TapState,\n} from \"../types.js\";\nimport { probeCommand } from \"../adapters/common.js\";\nimport { resolveNodeRuntime, buildRuntimeEnv } from \"../runtime/index.js\";\n\nexport interface BridgeStartOptions {\n instanceId: InstanceId;\n runtime: RuntimeName;\n stateDir: string;\n commsDir: string;\n bridgeScript: string;\n platform: Platform;\n agentName?: string;\n runtimeCommand?: string;\n appServerUrl?: string;\n repoRoot?: string;\n port?: number;\n /** Headless configuration. Passed as env vars to the bridge process. */\n headless?: HeadlessConfig | null;\n /** Bridge script operational flags (forwarded to codex-app-server-bridge.ts) */\n busyMode?: \"steer\" | \"wait\";\n pollSeconds?: number;\n reconnectSeconds?: number;\n messageLookbackMinutes?: number;\n threadId?: string;\n ephemeral?: boolean;\n processExistingMessages?: boolean;\n manageAppServer?: boolean;\n /** Skip auth gateway — app-server listens directly on the public port (localhost only). */\n noAuth?: boolean;\n}\n\nexport interface BridgeStopOptions {\n instanceId: InstanceId;\n stateDir: string;\n platform: Platform;\n}\n\ninterface EnsureCodexAppServerOptions {\n instanceId: InstanceId;\n stateDir: string;\n repoRoot: string;\n platform: Platform;\n appServerUrl: string;\n existingAppServer?: AppServerState | null;\n noAuth?: boolean;\n}\n\ninterface ManagedAppServerGatewayOptions {\n instanceId: InstanceId;\n stateDir: string;\n repoRoot: string;\n platform: Platform;\n publicUrl: string;\n}\n\ninterface WebSocketLike {\n addEventListener(\n type: \"open\" | \"error\" | \"close\",\n listener: () => void,\n options?: { once?: boolean },\n ): void;\n close(code?: number, reason?: string): void;\n}\n\ntype WebSocketCtor = new (url: string) => WebSocketLike;\n\nconst DEFAULT_APP_SERVER_URL = \"ws://127.0.0.1:4501\";\nconst APP_SERVER_HEALTH_TIMEOUT_MS = 1_500;\nconst APP_SERVER_START_TIMEOUT_MS = 20_000;\nconst APP_SERVER_GATEWAY_START_TIMEOUT_MS = 5_000;\nconst APP_SERVER_HEALTH_RETRY_MS = 250;\nconst APP_SERVER_AUTH_QUERY_PARAM = \"tap_token\";\nconst APP_SERVER_AUTH_FILE_MODE = 0o600;\n\nfunction appServerLogFilePath(\n stateDir: string,\n instanceId: InstanceId,\n): string {\n return path.join(stateDir, \"logs\", `app-server-${instanceId}.log`);\n}\n\nfunction appServerGatewayLogFilePath(\n stateDir: string,\n instanceId: InstanceId,\n): string {\n return path.join(stateDir, \"logs\", `app-server-gateway-${instanceId}.log`);\n}\n\nfunction appServerGatewayTokenFilePath(\n stateDir: string,\n instanceId: InstanceId,\n): string {\n return path.join(\n stateDir,\n \"secrets\",\n `app-server-gateway-${instanceId}.token`,\n );\n}\n\nfunction stderrLogFilePath(logPath: string): string {\n return `${logPath}.stderr`;\n}\n\nfunction writeProtectedTextFile(filePath: string, content: string): void {\n fs.mkdirSync(path.dirname(filePath), { recursive: true });\n const tmp = `${filePath}.tmp.${process.pid}`;\n fs.writeFileSync(tmp, content, {\n encoding: \"utf-8\",\n mode: APP_SERVER_AUTH_FILE_MODE,\n });\n fs.chmodSync(tmp, APP_SERVER_AUTH_FILE_MODE);\n fs.renameSync(tmp, filePath);\n fs.chmodSync(filePath, APP_SERVER_AUTH_FILE_MODE);\n}\n\nfunction removeFileIfExists(filePath: string | null | undefined): void {\n if (!filePath || !fs.existsSync(filePath)) {\n return;\n }\n\n try {\n fs.unlinkSync(filePath);\n } catch {\n // Best-effort cleanup only.\n }\n}\n\nfunction getWebSocketCtor(): WebSocketCtor | null {\n const candidate = (globalThis as { WebSocket?: unknown }).WebSocket;\n return typeof candidate === \"function\" ? (candidate as WebSocketCtor) : null;\n}\n\nfunction delay(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\n\nfunction isLoopbackHost(hostname: string): boolean {\n return hostname === \"127.0.0.1\" || hostname === \"localhost\";\n}\n\nfunction resolveCodexCommand(platform: Platform): string | null {\n const candidates =\n platform === \"win32\"\n ? [\"codex.cmd\", \"codex.exe\", \"codex\", \"codex.ps1\"]\n : [\"codex\"];\n return probeCommand(candidates).command;\n}\n\nfunction formatCodexAppServerCommand(command: string, url: string): string {\n return `${command} app-server --listen ${url}`;\n}\n\nfunction resolvePowerShellCommand(): string {\n return (\n probeCommand([\"pwsh\", \"powershell\", \"powershell.exe\"]).command ??\n \"powershell\"\n );\n}\n\nfunction resolveAuthGatewayScript(repoRoot: string): string | null {\n const moduleDir = path.dirname(fileURLToPath(import.meta.url));\n const candidates = [\n path.join(moduleDir, \"..\", \"bridges\", \"codex-app-server-auth-gateway.mjs\"),\n path.join(moduleDir, \"..\", \"bridges\", \"codex-app-server-auth-gateway.ts\"),\n path.join(\n repoRoot,\n \"packages\",\n \"tap-comms\",\n \"dist\",\n \"bridges\",\n \"codex-app-server-auth-gateway.mjs\",\n ),\n path.join(\n repoRoot,\n \"packages\",\n \"tap-comms\",\n \"src\",\n \"bridges\",\n \"codex-app-server-auth-gateway.ts\",\n ),\n ];\n\n for (const candidate of candidates) {\n if (fs.existsSync(candidate)) {\n return candidate;\n }\n }\n\n return null;\n}\n\nexport function getBridgeRuntimeStateDir(\n repoRoot: string,\n instanceId: InstanceId,\n): string {\n return path.join(repoRoot, \".tmp\", `codex-app-server-bridge-${instanceId}`);\n}\n\nasync function allocateLoopbackPort(hostname: string): Promise<number> {\n const bindHost = hostname === \"localhost\" ? \"127.0.0.1\" : hostname;\n return await new Promise<number>((resolve, reject) => {\n const server = net.createServer();\n server.unref();\n server.once(\"error\", reject);\n server.listen(0, bindHost, () => {\n const address = server.address();\n if (!address || typeof address === \"string\") {\n server.close(() => {\n reject(new Error(\"Failed to allocate a loopback port\"));\n });\n return;\n }\n\n const port = address.port;\n server.close((error) => {\n if (error) {\n reject(error);\n return;\n }\n resolve(port);\n });\n });\n });\n}\n\nfunction buildProtectedAppServerUrl(publicUrl: string, token: string): string {\n const url = new URL(publicUrl);\n url.searchParams.set(APP_SERVER_AUTH_QUERY_PARAM, token);\n return url.toString().replace(/\\/(?=\\?|$)/, \"\");\n}\n\nfunction readGatewayTokenFromPath(tokenPath: string): string {\n return fs.readFileSync(tokenPath, \"utf8\").trim();\n}\n\nfunction readGatewayToken(\n auth: AppServerAuthState | null | undefined,\n): string | null {\n if (!auth) {\n return null;\n }\n\n const legacyToken = (auth as AppServerAuthState & { token?: string }).token;\n if (legacyToken?.trim()) {\n return legacyToken.trim();\n }\n\n if (!auth.tokenPath || !fs.existsSync(auth.tokenPath)) {\n return null;\n }\n\n const fileToken = readGatewayTokenFromPath(auth.tokenPath);\n return fileToken || null;\n}\n\nfunction materializeGatewayTokenFile(\n stateDir: string,\n instanceId: InstanceId,\n publicUrl: string,\n auth: AppServerAuthState,\n): AppServerAuthState {\n if (auth.tokenPath && fs.existsSync(auth.tokenPath)) {\n return auth;\n }\n\n const token = readGatewayToken(auth);\n if (!token) {\n throw new Error(`Missing auth gateway token for ${instanceId}`);\n }\n\n const tokenPath = appServerGatewayTokenFilePath(stateDir, instanceId);\n writeProtectedTextFile(tokenPath, `${token}\\n`);\n return {\n ...auth,\n protectedUrl: buildProtectedAppServerUrl(publicUrl, \"***\"),\n tokenPath,\n };\n}\n\nasync function createManagedAppServerAuth(\n options: ManagedAppServerGatewayOptions,\n): Promise<AppServerAuthState> {\n const publicUrl = new URL(options.publicUrl);\n const upstreamUrl = new URL(options.publicUrl);\n upstreamUrl.port = String(await allocateLoopbackPort(publicUrl.hostname));\n upstreamUrl.search = \"\";\n upstreamUrl.hash = \"\";\n\n const gatewayScript = resolveAuthGatewayScript(options.repoRoot);\n if (!gatewayScript) {\n throw new Error(\"Auth gateway script not found\");\n }\n\n const token = randomBytes(24).toString(\"base64url\");\n const tokenPath = appServerGatewayTokenFilePath(\n options.stateDir,\n options.instanceId,\n );\n writeProtectedTextFile(tokenPath, `${token}\\n`);\n const protectedUrl = buildProtectedAppServerUrl(options.publicUrl, \"***\");\n\n const gatewayLogPath = appServerGatewayLogFilePath(\n options.stateDir,\n options.instanceId,\n );\n fs.mkdirSync(path.dirname(gatewayLogPath), { recursive: true });\n rotateLog(gatewayLogPath);\n\n const runtime = resolveNodeRuntime(process.execPath, options.repoRoot);\n const gatewayArgs: string[] = [];\n if (gatewayScript.endsWith(\".ts\")) {\n if (!runtime.supportsStripTypes) {\n throw new Error(\n \"Current Node runtime cannot start the auth gateway from TypeScript source. Rebuild @hua-labs/tap or use Node 22.6+.\",\n );\n }\n gatewayArgs.push(\"--experimental-strip-types\");\n }\n gatewayArgs.push(gatewayScript);\n\n const gatewayEnv = {\n ...buildRuntimeEnv(options.repoRoot),\n TAP_GATEWAY_LISTEN_URL: options.publicUrl,\n TAP_GATEWAY_UPSTREAM_URL: upstreamUrl.toString().replace(/\\/$/, \"\"),\n TAP_GATEWAY_TOKEN_FILE: tokenPath,\n };\n\n let gatewayPid: number | null;\n {\n let logFd: number | null = null;\n try {\n if (options.platform === \"win32\") {\n gatewayPid = startWindowsDetachedProcess(\n runtime.command,\n gatewayArgs,\n options.repoRoot,\n gatewayLogPath,\n gatewayEnv,\n );\n } else {\n logFd = fs.openSync(gatewayLogPath, \"a\");\n const child = spawn(runtime.command, gatewayArgs, {\n cwd: options.repoRoot,\n detached: true,\n stdio: [\"ignore\", logFd, logFd],\n env: gatewayEnv,\n windowsHide: true,\n });\n child.unref();\n gatewayPid = child.pid ?? null;\n }\n } catch (error) {\n removeFileIfExists(tokenPath);\n throw error;\n } finally {\n if (logFd != null) {\n fs.closeSync(logFd);\n }\n }\n }\n\n if (gatewayPid == null) {\n removeFileIfExists(tokenPath);\n throw new Error(\"Failed to spawn app-server auth gateway\");\n }\n\n return {\n mode: \"query-token\",\n protectedUrl,\n upstreamUrl: upstreamUrl.toString().replace(/\\/$/, \"\"),\n tokenPath,\n gatewayPid,\n gatewayLogPath,\n };\n}\n\nfunction canReuseManagedAppServer(\n appServer: AppServerState | null | undefined,\n): boolean {\n if (!appServer?.managed) {\n return false;\n }\n\n // App-server process must be alive\n if (appServer.pid != null && !isProcessAlive(appServer.pid)) {\n return false;\n }\n\n const auth = appServer.auth;\n if (auth) {\n // Auth mode: verify gateway token and process are intact\n if (!auth.protectedUrl) {\n return false;\n }\n if (!readGatewayToken(auth)) {\n return false;\n }\n if (auth.gatewayPid != null && !isProcessAlive(auth.gatewayPid)) {\n return false;\n }\n }\n // No-auth mode (auth is null): only the app-server process check above is needed\n\n return true;\n}\n\nfunction markAppServerHealthy(appServer: AppServerState): AppServerState {\n const checkedAt = new Date().toISOString();\n return {\n ...appServer,\n healthy: true,\n lastCheckedAt: checkedAt,\n lastHealthyAt: checkedAt,\n };\n}\n\nfunction findReusableManagedAppServer(\n stateDir: string,\n publicUrl: string,\n): AppServerState | null {\n const pidDir = path.join(stateDir, \"pids\");\n if (!fs.existsSync(pidDir)) {\n return null;\n }\n\n for (const name of fs.readdirSync(pidDir)) {\n if (!name.startsWith(\"bridge-\") || !name.endsWith(\".json\")) {\n continue;\n }\n\n try {\n const raw = fs.readFileSync(path.join(pidDir, name), \"utf-8\");\n const parsed = JSON.parse(raw) as BridgeState;\n if (parsed.appServer?.url !== publicUrl) {\n continue;\n }\n if (canReuseManagedAppServer(parsed.appServer)) {\n return markAppServerHealthy(parsed.appServer!);\n }\n } catch {\n // Ignore stale or corrupted bridge state.\n }\n }\n\n return null;\n}\n\nfunction startWindowsDetachedProcess(\n command: string,\n args: string[],\n repoRoot: string,\n logPath: string,\n env: NodeJS.ProcessEnv = process.env,\n): number | null {\n const ext = path.extname(command).toLowerCase();\n const stderrLogPath = stderrLogFilePath(logPath);\n const stdoutFd = fs.openSync(logPath, \"a\");\n const stderrFd = fs.openSync(stderrLogPath, \"a\");\n\n try {\n const child =\n ext === \".ps1\"\n ? spawn(\n resolvePowerShellCommand(),\n [\"-NoLogo\", \"-NoProfile\", \"-File\", command, ...args],\n {\n cwd: repoRoot,\n detached: true,\n stdio: [\"ignore\", stdoutFd, stderrFd],\n env,\n windowsHide: true,\n },\n )\n : spawn(command, args, {\n cwd: repoRoot,\n detached: true,\n stdio: [\"ignore\", stdoutFd, stderrFd],\n env,\n windowsHide: true,\n shell: ext === \".cmd\" || ext === \".bat\",\n });\n\n child.unref();\n return child.pid ?? null;\n } finally {\n fs.closeSync(stdoutFd);\n fs.closeSync(stderrFd);\n }\n}\n\nfunction startWindowsCodexAppServer(\n command: string,\n url: string,\n repoRoot: string,\n logPath: string,\n): number | null {\n return startWindowsDetachedProcess(\n command,\n [\"app-server\", \"--listen\", url],\n repoRoot,\n logPath,\n );\n}\n\nfunction findListeningProcessId(\n url: string,\n platform: Platform,\n): number | null {\n if (platform !== \"win32\") {\n return null;\n }\n\n let port: number | null;\n try {\n const parsed = new URL(url);\n port = parsed.port ? Number.parseInt(parsed.port, 10) : null;\n } catch {\n return null;\n }\n\n if (port == null || !Number.isFinite(port)) {\n return null;\n }\n\n const result = spawnSync(\n resolvePowerShellCommand(),\n [\n \"-NoLogo\",\n \"-NoProfile\",\n \"-Command\",\n [\n `$port = ${port}`,\n \"$processId = Get-NetTCPConnection -LocalPort $port -State Listen -ErrorAction SilentlyContinue | Select-Object -First 1 -ExpandProperty OwningProcess\",\n \"if ($processId) { $processId }\",\n ].join(\"; \"),\n ],\n {\n encoding: \"utf-8\",\n windowsHide: true,\n },\n );\n\n if (result.status !== 0) {\n return null;\n }\n\n const parsedPid = Number.parseInt((result.stdout ?? \"\").trim(), 10);\n return Number.isFinite(parsedPid) ? parsedPid : null;\n}\n\nexport function resolveAppServerUrl(\n baseUrl: string | undefined,\n port?: number,\n): string {\n const resolvedBase = (baseUrl ?? DEFAULT_APP_SERVER_URL).replace(/\\/$/, \"\");\n if (port == null) {\n return resolvedBase;\n }\n\n try {\n const parsed = new URL(resolvedBase);\n parsed.port = String(port);\n return parsed.toString().replace(/\\/$/, \"\");\n } catch {\n return resolvedBase;\n }\n}\n\nexport async function isTcpPortAvailable(\n hostname: string,\n port: number,\n): Promise<boolean> {\n const bindHost = hostname === \"localhost\" ? \"127.0.0.1\" : hostname;\n return await new Promise<boolean>((resolve) => {\n const server = net.createServer();\n server.unref();\n server.once(\"error\", () => resolve(false));\n server.listen(port, bindHost, () => {\n server.close((error) => resolve(!error));\n });\n });\n}\n\nexport async function findNextAvailableAppServerPort(\n state: TapState,\n baseUrl: string | undefined,\n basePort: number = 4501,\n excludeInstanceId?: InstanceId,\n): Promise<number> {\n let hostname = \"127.0.0.1\";\n try {\n hostname = new URL(baseUrl ?? DEFAULT_APP_SERVER_URL).hostname;\n } catch {\n // Fall back to the default loopback host.\n }\n\n const maxAttempts = 1000;\n let port = basePort;\n for (let attempt = 0; attempt < maxAttempts; attempt += 1, port += 1) {\n const claimedInState = Object.entries(state.instances).some(\n ([id, inst]) => id !== excludeInstanceId && inst.port === port,\n );\n if (claimedInState) {\n continue;\n }\n\n if (!isLoopbackHost(hostname)) {\n return port;\n }\n\n if (await isTcpPortAvailable(hostname, port)) {\n return port;\n }\n }\n\n throw new Error(\n `Failed to find a free app-server port starting at ${basePort}`,\n );\n}\n\nexport async function checkAppServerHealth(\n url: string,\n timeoutMs: number = APP_SERVER_HEALTH_TIMEOUT_MS,\n): Promise<boolean> {\n const WebSocket = getWebSocketCtor();\n if (!WebSocket) {\n return false;\n }\n\n return new Promise<boolean>((resolve) => {\n let settled = false;\n let socket: WebSocketLike | null = null;\n\n const finish = (healthy: boolean) => {\n if (settled) {\n return;\n }\n settled = true;\n clearTimeout(timer);\n try {\n socket?.close();\n } catch {\n // Best-effort cleanup only.\n }\n resolve(healthy);\n };\n\n const timer = setTimeout(() => finish(false), timeoutMs);\n\n try {\n socket = new WebSocket(url);\n socket.addEventListener(\"open\", () => finish(true), { once: true });\n socket.addEventListener(\"error\", () => finish(false), { once: true });\n socket.addEventListener(\"close\", () => finish(false), { once: true });\n } catch {\n finish(false);\n }\n });\n}\n\nasync function waitForAppServerHealth(\n url: string,\n timeoutMs: number,\n): Promise<boolean> {\n const deadline = Date.now() + timeoutMs;\n\n while (Date.now() < deadline) {\n if (await checkAppServerHealth(url)) {\n return true;\n }\n await delay(APP_SERVER_HEALTH_RETRY_MS);\n }\n\n return false;\n}\n\nasync function terminateProcess(\n pid: number,\n platform: Platform,\n): Promise<boolean> {\n if (!isProcessAlive(pid)) {\n return false;\n }\n\n try {\n if (platform === \"win32\") {\n execSync(`taskkill /PID ${pid} /F /T`, { stdio: \"pipe\" });\n } else {\n process.kill(pid, \"SIGTERM\");\n await delay(2_000);\n if (isProcessAlive(pid)) {\n process.kill(pid, \"SIGKILL\");\n }\n }\n } catch {\n // Best effort. The caller only needs a boolean outcome.\n }\n\n return !isProcessAlive(pid);\n}\n\nexport async function stopManagedAppServer(\n appServer: AppServerState,\n platform: Platform,\n): Promise<boolean> {\n if (!appServer.managed) {\n return false;\n }\n\n let stopped = false;\n if (appServer.auth?.gatewayPid != null) {\n stopped =\n (await terminateProcess(appServer.auth.gatewayPid, platform)) || stopped;\n }\n if (appServer.pid != null) {\n stopped = (await terminateProcess(appServer.pid, platform)) || stopped;\n }\n removeFileIfExists(appServer.auth?.tokenPath);\n return stopped;\n}\n\nexport async function ensureCodexAppServer(\n options: EnsureCodexAppServerOptions,\n): Promise<AppServerState> {\n const effectiveUrl = resolveAppServerUrl(options.appServerUrl);\n const fallbackManualCommand = formatCodexAppServerCommand(\n \"codex\",\n effectiveUrl,\n );\n if (\n options.existingAppServer?.url === effectiveUrl &&\n canReuseManagedAppServer(options.existingAppServer)\n ) {\n return markAppServerHealthy(options.existingAppServer);\n }\n\n const sharedManaged = findReusableManagedAppServer(\n options.stateDir,\n effectiveUrl,\n );\n if (sharedManaged) {\n return sharedManaged;\n }\n\n let parsedUrl: URL;\n try {\n parsedUrl = new URL(effectiveUrl);\n } catch {\n throw new Error(\n `Invalid app-server URL: ${effectiveUrl}\\nStart it manually:\\n ${fallbackManualCommand}`,\n );\n }\n\n if (!isLoopbackHost(parsedUrl.hostname)) {\n throw new Error(\n `Auto-start only supports loopback app-server URLs. Current URL: ${effectiveUrl}\\nStart it manually:\\n ${fallbackManualCommand}`,\n );\n }\n\n if (await checkAppServerHealth(effectiveUrl)) {\n const hint = options.noAuth\n ? \"Stop it first or use --no-server for an unmanaged external app-server.\"\n : \"A listener is already running, so tap cannot insert the auth gateway there.\\nStop it first or use --no-server for an unmanaged external app-server.\";\n throw new Error(`${effectiveUrl}: ${hint}`);\n }\n\n const resolvedCommand = resolveCodexCommand(options.platform);\n if (!resolvedCommand) {\n throw new Error(\n `Codex CLI not found in PATH.\\nStart the app-server manually:\\n ${fallbackManualCommand}`,\n );\n }\n\n const logPath = appServerLogFilePath(options.stateDir, options.instanceId);\n fs.mkdirSync(path.dirname(logPath), { recursive: true });\n rotateLog(logPath);\n\n // --no-auth: start app-server directly on the public URL (no gateway).\n // TUI and bridge both connect to the same port without token auth.\n if (options.noAuth) {\n const manualCommand = formatCodexAppServerCommand(\"codex\", effectiveUrl);\n let pid: number | null;\n\n if (options.platform === \"win32\") {\n try {\n pid = startWindowsCodexAppServer(\n resolvedCommand,\n effectiveUrl,\n options.repoRoot,\n logPath,\n );\n } catch (err) {\n throw new Error(\n `Failed to spawn Codex app-server: ${err instanceof Error ? err.message : String(err)}\\nStart it manually:\\n ${manualCommand}`,\n { cause: err },\n );\n }\n } else {\n const logFd = fs.openSync(logPath, \"a\");\n try {\n const child = spawn(\n resolvedCommand,\n [\"app-server\", \"--listen\", effectiveUrl],\n {\n cwd: options.repoRoot,\n detached: true,\n stdio: [\"ignore\", logFd, logFd],\n env: process.env,\n windowsHide: true,\n },\n );\n child.unref();\n pid = child.pid ?? null;\n } catch (err) {\n throw new Error(\n `Failed to spawn Codex app-server: ${err instanceof Error ? err.message : String(err)}\\nStart it manually:\\n ${manualCommand}`,\n { cause: err },\n );\n } finally {\n fs.closeSync(logFd);\n }\n }\n\n if (pid == null) {\n throw new Error(\n `Failed to spawn Codex app-server.\\nStart it manually:\\n ${manualCommand}`,\n );\n }\n\n const healthy = await waitForAppServerHealth(\n effectiveUrl,\n APP_SERVER_START_TIMEOUT_MS,\n );\n if (!healthy) {\n await terminateProcess(pid, options.platform);\n throw new Error(\n `Codex app-server did not become healthy at ${effectiveUrl}.\\nCheck ${logPath}\\nOr start it manually:\\n ${manualCommand}`,\n );\n }\n\n pid = findListeningProcessId(effectiveUrl, options.platform) ?? pid;\n const healthyAt = new Date().toISOString();\n return {\n url: effectiveUrl,\n pid,\n managed: true,\n healthy: true,\n lastCheckedAt: healthyAt,\n lastHealthyAt: healthyAt,\n logPath,\n manualCommand,\n auth: null,\n };\n }\n\n // Default: auth gateway mode — gateway on publicUrl, app-server on random upstream port\n const auth = await createManagedAppServerAuth({\n instanceId: options.instanceId,\n stateDir: options.stateDir,\n repoRoot: options.repoRoot,\n platform: options.platform,\n publicUrl: effectiveUrl,\n });\n const manualCommand = formatCodexAppServerCommand(\"codex\", auth.upstreamUrl);\n\n let pid: number | null;\n\n if (options.platform === \"win32\") {\n try {\n pid = startWindowsCodexAppServer(\n resolvedCommand,\n auth.upstreamUrl,\n options.repoRoot,\n logPath,\n );\n } catch (err) {\n if (auth.gatewayPid != null) {\n await terminateProcess(auth.gatewayPid, options.platform);\n }\n removeFileIfExists(auth.tokenPath);\n throw new Error(\n `Failed to spawn Codex app-server: ${err instanceof Error ? err.message : String(err)}\\nStart it manually:\\n ${manualCommand}`,\n { cause: err },\n );\n }\n } else {\n const logFd = fs.openSync(logPath, \"a\");\n\n try {\n const child = spawn(\n resolvedCommand,\n [\"app-server\", \"--listen\", auth.upstreamUrl],\n {\n cwd: options.repoRoot,\n detached: true,\n stdio: [\"ignore\", logFd, logFd],\n env: process.env,\n windowsHide: true,\n },\n );\n\n child.unref();\n pid = child.pid ?? null;\n } catch (err) {\n if (auth.gatewayPid != null) {\n await terminateProcess(auth.gatewayPid, options.platform);\n }\n removeFileIfExists(auth.tokenPath);\n throw new Error(\n `Failed to spawn Codex app-server: ${err instanceof Error ? err.message : String(err)}\\nStart it manually:\\n ${manualCommand}`,\n { cause: err },\n );\n } finally {\n fs.closeSync(logFd);\n }\n }\n\n if (pid == null) {\n if (auth.gatewayPid != null) {\n await terminateProcess(auth.gatewayPid, options.platform);\n }\n removeFileIfExists(auth.tokenPath);\n throw new Error(\n `Failed to spawn Codex app-server.\\nStart it manually:\\n ${manualCommand}`,\n );\n }\n\n const healthy = await waitForAppServerHealth(\n auth.upstreamUrl,\n APP_SERVER_START_TIMEOUT_MS,\n );\n\n if (!healthy) {\n await terminateProcess(pid, options.platform);\n if (auth.gatewayPid != null) {\n await terminateProcess(auth.gatewayPid, options.platform);\n }\n removeFileIfExists(auth.tokenPath);\n throw new Error(\n `Codex app-server did not become healthy at ${auth.upstreamUrl}.\\nCheck ${logPath}\\nOr start it manually:\\n ${manualCommand}`,\n );\n }\n\n const gatewayToken = readGatewayToken(auth);\n if (!gatewayToken) {\n await terminateProcess(pid, options.platform);\n if (auth.gatewayPid != null) {\n await terminateProcess(auth.gatewayPid, options.platform);\n }\n removeFileIfExists(auth.tokenPath);\n throw new Error(\"Tap auth gateway token is missing after startup.\");\n }\n\n const gatewayHealthy = await waitForAppServerHealth(\n buildProtectedAppServerUrl(effectiveUrl, gatewayToken),\n APP_SERVER_GATEWAY_START_TIMEOUT_MS,\n );\n if (!gatewayHealthy) {\n await terminateProcess(pid, options.platform);\n if (auth.gatewayPid != null) {\n await terminateProcess(auth.gatewayPid, options.platform);\n }\n removeFileIfExists(auth.tokenPath);\n throw new Error(\n `Tap auth gateway did not become healthy at ${effectiveUrl}.\\nCheck ${auth.gatewayLogPath ?? \"the gateway log\"} and ${logPath}.`,\n );\n }\n\n const healthyAt = new Date().toISOString();\n pid = findListeningProcessId(auth.upstreamUrl, options.platform) ?? pid;\n return {\n url: effectiveUrl,\n pid,\n managed: true,\n healthy: true,\n lastCheckedAt: healthyAt,\n lastHealthyAt: healthyAt,\n logPath,\n manualCommand,\n auth,\n };\n}\n\nfunction pidFilePath(stateDir: string, instanceId: InstanceId): string {\n return path.join(stateDir, \"pids\", `bridge-${instanceId}.json`);\n}\n\nfunction logFilePath(stateDir: string, instanceId: InstanceId): string {\n return path.join(stateDir, \"logs\", `bridge-${instanceId}.log`);\n}\n\nfunction runtimeHeartbeatFilePath(runtimeStateDir: string): string {\n return path.join(runtimeStateDir, \"heartbeat.json\");\n}\n\nfunction loadRuntimeHeartbeatTimestamp(\n runtimeStateDir: string | null | undefined,\n): string | null {\n if (!runtimeStateDir) {\n return null;\n }\n\n const heartbeatPath = runtimeHeartbeatFilePath(runtimeStateDir);\n if (!fs.existsSync(heartbeatPath)) {\n return null;\n }\n\n try {\n const raw = fs.readFileSync(heartbeatPath, \"utf-8\");\n const parsed = JSON.parse(raw) as { updatedAt?: string };\n return typeof parsed.updatedAt === \"string\" ? parsed.updatedAt : null;\n } catch {\n return null;\n }\n}\n\nfunction resolveHeartbeatTimestamp(\n state: BridgeState | null | undefined,\n): string | null {\n return (\n loadRuntimeHeartbeatTimestamp(state?.runtimeStateDir) ??\n state?.lastHeartbeat ??\n null\n );\n}\n\nexport function loadBridgeState(\n stateDir: string,\n instanceId: InstanceId,\n): BridgeState | null {\n const pidPath = pidFilePath(stateDir, instanceId);\n if (!fs.existsSync(pidPath)) return null;\n\n try {\n const raw = fs.readFileSync(pidPath, \"utf-8\");\n return JSON.parse(raw) as BridgeState;\n } catch {\n return null;\n }\n}\n\nexport function saveBridgeState(\n stateDir: string,\n instanceId: InstanceId,\n state: BridgeState,\n): void {\n const pidPath = pidFilePath(stateDir, instanceId);\n const serializable = JSON.parse(JSON.stringify(state)) as BridgeState & {\n appServer?: { auth?: { token?: string } | null } | null;\n };\n if (serializable.appServer?.auth) {\n delete serializable.appServer.auth.token;\n }\n writeProtectedTextFile(pidPath, JSON.stringify(serializable, null, 2));\n}\n\nexport function clearBridgeState(\n stateDir: string,\n instanceId: InstanceId,\n): void {\n const pidPath = pidFilePath(stateDir, instanceId);\n if (fs.existsSync(pidPath)) {\n fs.unlinkSync(pidPath);\n }\n}\n\nexport function isProcessAlive(pid: number): boolean {\n try {\n process.kill(pid, 0);\n return true;\n } catch {\n return false;\n }\n}\n\nexport function isBridgeRunning(\n stateDir: string,\n instanceId: InstanceId,\n): boolean {\n const state = loadBridgeState(stateDir, instanceId);\n if (!state) return false;\n return isProcessAlive(state.pid);\n}\n\nexport async function startBridge(\n options: BridgeStartOptions,\n): Promise<BridgeState> {\n const {\n instanceId,\n runtime,\n stateDir,\n commsDir,\n bridgeScript,\n agentName,\n port,\n } = options;\n\n // Resolve agent name: explicit > env > error\n const resolvedAgent =\n agentName || process.env.TAP_AGENT_NAME || process.env.CODEX_TAP_AGENT_NAME;\n\n if (!resolvedAgent) {\n throw new Error(\n `No agent name for ${instanceId} bridge. ` +\n `Set TAP_AGENT_NAME env var or pass --agent-name flag.`,\n );\n }\n\n // Check if already running\n if (isBridgeRunning(stateDir, instanceId)) {\n const existing = loadBridgeState(stateDir, instanceId)!;\n throw new Error(\n `Bridge for ${instanceId} is already running (PID: ${existing.pid})`,\n );\n }\n\n const previousBridgeState = loadBridgeState(stateDir, instanceId);\n const previousAppServer = previousBridgeState?.appServer ?? null;\n\n // Clear stale PID\n clearBridgeState(stateDir, instanceId);\n\n const logPath = logFilePath(stateDir, instanceId);\n fs.mkdirSync(path.dirname(logPath), { recursive: true });\n\n // Log rotation: rename existing log to .prev\n rotateLog(logPath);\n\n let logFd: number | null = null;\n\n // Use explicit repoRoot (not derived from stateDir — stateDir may be external)\n const repoRoot = options.repoRoot ?? path.resolve(stateDir, \"..\");\n const runtimeStateDir = getBridgeRuntimeStateDir(repoRoot, instanceId);\n const resolved = resolveNodeRuntime(\n options.runtimeCommand ?? \"node\",\n repoRoot,\n );\n const command = resolved.command;\n\n // Build env with fnm Node prepended to PATH so the bridge runner's\n // 2nd-stage spawn also finds the correct Node (결 finding: 2-stage spawn)\n const runtimeEnv = buildRuntimeEnv(repoRoot);\n const effectiveAppServerUrl = resolveAppServerUrl(options.appServerUrl, port);\n let appServer: AppServerState | null = null;\n let bridgeAppServerUrl = effectiveAppServerUrl;\n\n if (runtime === \"codex\" && options.manageAppServer) {\n appServer = await ensureCodexAppServer({\n instanceId,\n stateDir,\n repoRoot,\n platform: options.platform,\n appServerUrl: effectiveAppServerUrl,\n existingAppServer: previousAppServer,\n noAuth: options.noAuth,\n });\n if (appServer.auth) {\n appServer = {\n ...appServer,\n auth: materializeGatewayTokenFile(\n stateDir,\n instanceId,\n effectiveAppServerUrl,\n appServer.auth,\n ),\n };\n }\n bridgeAppServerUrl = effectiveAppServerUrl;\n }\n\n // Spawn detached process — pass both command and strip-types metadata\n // so the runner doesn't re-guess (avoids bun + --experimental-strip-types)\n try {\n const bridgeEnv = {\n ...runtimeEnv,\n TAP_COMMS_DIR: commsDir,\n TAP_STATE_DIR: runtimeStateDir,\n TAP_BRIDGE_RUNTIME: runtime,\n TAP_BRIDGE_INSTANCE_ID: instanceId,\n TAP_AGENT_ID: instanceId,\n TAP_AGENT_NAME: resolvedAgent,\n CODEX_TAP_AGENT_NAME: resolvedAgent,\n TAP_RESOLVED_NODE: resolved.command,\n TAP_STRIP_TYPES: resolved.supportsStripTypes ? \"1\" : \"0\",\n ...(bridgeAppServerUrl\n ? { CODEX_APP_SERVER_URL: bridgeAppServerUrl }\n : {}),\n ...(appServer?.auth?.tokenPath\n ? { TAP_GATEWAY_TOKEN_FILE: appServer.auth.tokenPath }\n : {}),\n ...(port != null ? { TAP_BRIDGE_PORT: String(port) } : {}),\n ...(options.headless?.enabled\n ? {\n TAP_HEADLESS: \"true\",\n TAP_AGENT_ROLE: options.headless.role,\n TAP_MAX_REVIEW_ROUNDS: String(options.headless.maxRounds),\n TAP_QUALITY_FLOOR: options.headless.qualitySeverityFloor,\n }\n : {}),\n ...(options.busyMode ? { TAP_BUSY_MODE: options.busyMode } : {}),\n ...(options.pollSeconds != null\n ? { TAP_POLL_SECONDS: String(options.pollSeconds) }\n : {}),\n ...(options.reconnectSeconds != null\n ? { TAP_RECONNECT_SECONDS: String(options.reconnectSeconds) }\n : {}),\n ...(options.messageLookbackMinutes != null\n ? {\n TAP_MESSAGE_LOOKBACK_MINUTES: String(\n options.messageLookbackMinutes,\n ),\n }\n : {}),\n ...(options.threadId ? { TAP_THREAD_ID: options.threadId } : {}),\n ...(options.ephemeral ? { TAP_EPHEMERAL: \"true\" } : {}),\n ...(options.processExistingMessages\n ? { TAP_PROCESS_EXISTING: \"true\" }\n : {}),\n };\n\n let bridgePid: number | null = null;\n\n if (options.platform === \"win32\") {\n bridgePid = startWindowsDetachedProcess(\n command,\n [bridgeScript],\n repoRoot,\n logPath,\n bridgeEnv,\n );\n } else {\n logFd = fs.openSync(logPath, \"a\");\n const child = spawn(command, [bridgeScript], {\n detached: true,\n stdio: [\"ignore\", logFd, logFd],\n env: bridgeEnv,\n windowsHide: true,\n });\n\n child.unref();\n bridgePid = child.pid ?? null;\n }\n\n if (logFd != null) {\n fs.closeSync(logFd);\n logFd = null;\n }\n\n if (!bridgePid) {\n throw new Error(`Failed to spawn bridge process for ${instanceId}`);\n }\n\n const state: BridgeState = {\n pid: bridgePid,\n statePath: pidFilePath(stateDir, instanceId),\n lastHeartbeat: new Date().toISOString(),\n appServer,\n runtimeStateDir,\n };\n\n saveBridgeState(stateDir, instanceId, state);\n\n // NOTE: Heartbeat updates are the bridge process's responsibility.\n // The bridge script should periodically write to the PID file's lastHeartbeat field.\n // CLI only records the initial heartbeat at spawn time.\n\n return state;\n } catch (err) {\n if (logFd != null) {\n try {\n fs.closeSync(logFd);\n } catch {\n // Best-effort cleanup only.\n }\n }\n if (appServer?.managed) {\n await stopManagedAppServer(appServer, options.platform);\n }\n throw err;\n }\n}\n\nexport async function stopBridge(options: BridgeStopOptions): Promise<boolean> {\n const { instanceId, stateDir, platform } = options;\n const state = loadBridgeState(stateDir, instanceId);\n\n if (!state) {\n return false; // No PID file\n }\n\n if (!isProcessAlive(state.pid)) {\n clearBridgeState(stateDir, instanceId);\n return false; // Already dead\n }\n\n try {\n await terminateProcess(state.pid, platform);\n } catch {\n // Process may have already exited\n }\n\n clearBridgeState(stateDir, instanceId);\n return true;\n}\n\n// ─── Log rotation ──────────────────────────────────────────────\n\nexport function rotateLog(logPath: string): void {\n if (!fs.existsSync(logPath)) return;\n try {\n const stats = fs.statSync(logPath);\n if (stats.size === 0) return;\n const prevPath = `${logPath}.prev`;\n fs.renameSync(logPath, prevPath);\n } catch {\n // Best-effort: don't fail bridge start if rotation fails\n }\n}\n\n// ─── Heartbeat ─────────────────────────────────────────────────\n\n/**\n * Update the heartbeat timestamp for a running bridge.\n * Bridge processes should call this periodically.\n *\n * Only the owning process (matching PID) can update the heartbeat.\n * This prevents state dir collision when multiple writers exist.\n * See: 묵 finding — bridge-heartbeat-state-dir-collision\n */\nexport function updateBridgeHeartbeat(\n stateDir: string,\n instanceId: InstanceId,\n): void {\n const state = loadBridgeState(stateDir, instanceId);\n if (!state) return;\n\n // Guard: only the owning process may update heartbeat\n if (state.pid !== process.pid) return;\n\n state.lastHeartbeat = new Date().toISOString();\n saveBridgeState(stateDir, instanceId, state);\n}\n\n/**\n * Get heartbeat age in seconds. Returns null if no state or no heartbeat.\n */\nexport function getHeartbeatAge(\n stateDir: string,\n instanceId: InstanceId,\n): number | null {\n const state = loadBridgeState(stateDir, instanceId);\n const heartbeat = resolveHeartbeatTimestamp(state);\n if (!heartbeat) return null;\n const heartbeatTime = new Date(heartbeat).getTime();\n if (isNaN(heartbeatTime)) return null;\n return Math.floor((Date.now() - heartbeatTime) / 1000);\n}\n\nexport function getBridgeHeartbeatTimestamp(\n stateDir: string,\n instanceId: InstanceId,\n): string | null {\n return resolveHeartbeatTimestamp(loadBridgeState(stateDir, instanceId));\n}\n\nexport function getBridgeStatus(\n stateDir: string,\n instanceId: InstanceId,\n): \"running\" | \"stopped\" | \"stale\" {\n const state = loadBridgeState(stateDir, instanceId);\n if (!state) return \"stopped\";\n\n // Primary check: is the process actually alive?\n if (!isProcessAlive(state.pid)) {\n clearBridgeState(stateDir, instanceId);\n return \"stale\";\n }\n\n // Process is alive → running.\n // Heartbeat staleness is informational only — the bridge process\n // is responsible for updating lastHeartbeat. If it doesn't,\n // PID alive is still the authoritative signal.\n return \"running\";\n}\n","/**\n * Common Node.js runtime resolver for all tap-comms child processes.\n *\n * Resolution chain:\n * .node-version + fnm probe → configured command → tsx fallback\n *\n * Extracted from codex-bridge-runner.ts (M69) to share across:\n * - bridge engine spawn\n * - bridge runner spawn\n * - future CLI commands\n */\n\nimport * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport { execSync } from \"node:child_process\";\n\n// ─── Types ─────────────────────────────────────────────────────\n\nexport type RuntimeSource = \"fnm\" | \"config\" | \"path\" | \"tsx-fallback\" | \"bun\";\n\nexport interface ResolvedRuntime {\n /** Absolute path or command name for the resolved runtime. */\n command: string;\n /** Whether --experimental-strip-types is supported and should be used. */\n supportsStripTypes: boolean;\n /** Where the runtime was resolved from (for diagnostics). */\n source: RuntimeSource;\n /** Detected major version, if available. */\n majorVersion: number | null;\n}\n\n// ─── .node-version ─────────────────────────────────────────────\n\nexport function readNodeVersion(repoRoot: string): string | null {\n const nvFile = path.join(repoRoot, \".node-version\");\n if (!fs.existsSync(nvFile)) return null;\n try {\n const raw = fs.readFileSync(nvFile, \"utf-8\").trim();\n return raw.length > 0 ? raw.replace(/^v/, \"\") : null;\n } catch {\n return null;\n }\n}\n\n// ─── fnm probe ─────────────────────────────────────────────────\n\nfunction fnmCandidateDirs(): string[] {\n if (process.platform === \"win32\") {\n return [\n process.env.FNM_DIR,\n process.env.APPDATA ? path.join(process.env.APPDATA, \"fnm\") : null,\n process.env.LOCALAPPDATA\n ? path.join(process.env.LOCALAPPDATA, \"fnm\")\n : null,\n process.env.USERPROFILE\n ? path.join(process.env.USERPROFILE, \"scoop\", \"persist\", \"fnm\")\n : null,\n ].filter(Boolean) as string[];\n }\n // macOS / Linux\n return [\n process.env.FNM_DIR,\n process.env.HOME\n ? path.join(process.env.HOME, \".local\", \"share\", \"fnm\")\n : null,\n process.env.HOME ? path.join(process.env.HOME, \".fnm\") : null,\n process.env.XDG_DATA_HOME\n ? path.join(process.env.XDG_DATA_HOME, \"fnm\")\n : null,\n ].filter(Boolean) as string[];\n}\n\nfunction nodeExecutableName(): string {\n return process.platform === \"win32\" ? \"node.exe\" : \"node\";\n}\n\nexport function probeFnmNode(desiredVersion: string): string | null {\n const dirs = fnmCandidateDirs();\n const exe = nodeExecutableName();\n\n for (const baseDir of dirs) {\n const candidate = path.join(\n baseDir,\n \"node-versions\",\n `v${desiredVersion}`,\n \"installation\",\n exe,\n );\n if (!fs.existsSync(candidate)) continue;\n\n try {\n const v = execSync(`\"${candidate}\" --version`, {\n encoding: \"utf-8\",\n timeout: 5000,\n }).trim();\n if (v.startsWith(`v${desiredVersion.split(\".\")[0]}.`)) {\n return candidate;\n }\n } catch {\n // candidate exists but doesn't work — skip\n }\n }\n\n return null;\n}\n\n// ─── Version detection ─────────────────────────────────────────\n\nexport function detectNodeMajorVersion(command: string): number | null {\n try {\n const version = execSync(`\"${command}\" --version`, {\n encoding: \"utf-8\",\n timeout: 5000,\n }).trim();\n const match = version.match(/^v?(\\d+)\\./);\n return match ? parseInt(match[1], 10) : null;\n } catch {\n return null;\n }\n}\n\nexport function checkStripTypesSupport(command: string): boolean {\n const major = detectNodeMajorVersion(command);\n if (major !== null && major >= 22) return true;\n try {\n execSync(`\"${command}\" --experimental-strip-types -e \"\"`, {\n timeout: 5000,\n stdio: \"pipe\",\n });\n return true;\n } catch {\n return false;\n }\n}\n\n// ─── tsx fallback ──────────────────────────────────────────────\n\nexport function findTsxFallback(repoRoot: string): string | null {\n const candidates = [\n path.join(repoRoot, \"node_modules\", \".bin\", \"tsx.exe\"),\n path.join(repoRoot, \"node_modules\", \".bin\", \"tsx.CMD\"),\n path.join(repoRoot, \"node_modules\", \".bin\", \"tsx\"),\n ];\n for (const c of candidates) {\n if (fs.existsSync(c)) return c;\n }\n return null;\n}\n\n// ─── fnm bin directory (for PATH prepending) ───────────────────\n\n/**\n * Returns the directory containing the fnm-managed node binary,\n * suitable for prepending to PATH in child processes.\n */\nexport function getFnmBinDir(repoRoot: string): string | null {\n const desiredVersion = readNodeVersion(repoRoot);\n if (!desiredVersion) return null;\n\n const nodePath = probeFnmNode(desiredVersion);\n if (!nodePath) return null;\n\n return path.dirname(nodePath);\n}\n\n// ─── Main resolver ─────────────────────────────────────────────\n\n/**\n * Resolve the Node.js runtime to use for spawning child processes.\n *\n * Priority: bun passthrough → .node-version + fnm → configured command → tsx fallback\n */\nexport function resolveNodeRuntime(\n configCommand: string,\n repoRoot: string,\n): ResolvedRuntime {\n // Bun: native TS support, no strip-types needed\n if (configCommand === \"bun\" || configCommand.endsWith(\"bun.exe\")) {\n return {\n command: configCommand,\n supportsStripTypes: false,\n source: \"bun\",\n majorVersion: null,\n };\n }\n\n // .node-version + fnm discovery\n const desiredVersion = readNodeVersion(repoRoot);\n if (desiredVersion) {\n const fnmNode = probeFnmNode(desiredVersion);\n if (fnmNode) {\n const major = detectNodeMajorVersion(fnmNode);\n return {\n command: fnmNode,\n supportsStripTypes: checkStripTypesSupport(fnmNode),\n source: \"fnm\",\n majorVersion: major,\n };\n }\n }\n\n // Configured command (from config or PATH)\n const major = detectNodeMajorVersion(configCommand);\n if (major !== null) {\n return {\n command: configCommand,\n supportsStripTypes: checkStripTypesSupport(configCommand),\n source: major === detectNodeMajorVersion(\"node\") ? \"path\" : \"config\",\n majorVersion: major,\n };\n }\n\n // tsx fallback\n const tsx = findTsxFallback(repoRoot);\n if (tsx) {\n return {\n command: tsx,\n supportsStripTypes: false,\n source: \"tsx-fallback\",\n majorVersion: null,\n };\n }\n\n // Last resort\n return {\n command: configCommand,\n supportsStripTypes: false,\n source: \"path\",\n majorVersion: null,\n };\n}\n\n// ─── Env builder for child processes ───────────────────────────\n\n/**\n * Build an env object with fnm Node prepended to PATH.\n * Use this when spawning child processes that need the correct Node.\n */\nexport function buildRuntimeEnv(\n repoRoot: string,\n baseEnv: NodeJS.ProcessEnv = process.env,\n): NodeJS.ProcessEnv {\n const fnmBin = getFnmBinDir(repoRoot);\n if (!fnmBin) return { ...baseEnv };\n\n const pathKey = process.platform === \"win32\" ? \"Path\" : \"PATH\";\n const currentPath =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B,QAAQ,MAAM,UAAU,OAAO,KAAK,CAAC;AACpE,YAAQ,KAAK,CAAC;AAAA,EAChB,CAAC;AACH;","names":["fs","fs","path","crypto","reviewFilePath","fs","path","fs","path","spawn","fileURLToPath","fs","path","fs","path","execSync","fs","path","major","DEFAULT_APP_SERVER_URL","DEFAULT_APP_SERVER_URL","fileURLToPath","createHeadlessLoop","spawn"]}