import { spawn } from "node:child_process"; import { chmodSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { dirname, isAbsolute, join, relative } from "node:path"; import { childEnv, stopOwnedProcess, trackGroup } from "./child-process.ts"; import { hubGitEnv, hubGitSync } from "./git.ts"; import { foldSegment } from "./permission-mode.ts"; import { realPath } from "./project.ts"; import { releaseTrackedClean } from "./release-worktree.ts"; export interface CodeSymbol { name: string; kind: string; path: string; line: number; endLine?: number; ambiguous: boolean; change: "unknown" } export type CodeFactResult = { state: "known"; value: T } | { state: "unknown" }; export interface CodeLocation { repo: string; head: string; private?: boolean; priority?: "optional" } export const OPTIONAL_CODE_UNKNOWN_TTL_MS = 5000; const unknown = (): {state:"unknown"} => ({state:"unknown"}); const SHA = /^[a-f0-9]{40}$/; /** These host anchor spellings have no indexed-name lookup attestation in ripwire 0.6.7. */ export const unindexedSymbolSyntax = (name: string): boolean => /#|:|->/.test(name); // Keep the owned group leader alive after the CLI exits, until the host has stopped every group member. const LAUNCHER = String.raw` const {writeSync,closeSync}=require("node:fs"); setInterval(()=>{},1000); let code=null; try { code=await Bun.spawn(process.argv.slice(1),{stdin:"ignore",stdout:"inherit",stderr:"ignore"}).exited; } catch {} try { closeSync(1); } catch {} writeSync(3,JSON.stringify({code})+"\n"); `; const MAX_OUTPUT = 256 * 1024, MAX_SYMBOLS = 256, MAX_FILES = 4096, MAX_TREE_BYTES = 32 * 1024 * 1024; const within = (root:string,file:string) => {const r=relative(root,file);return r !== ".." && !r.startsWith("../") && !isAbsolute(r);}; const text = (value:unknown,cap=512): value is string => typeof value === "string" && !!value && Buffer.byteLength(value)<=cap && !/[\p{Cc}\p{Cf}]/u.test(value); const pathName = (value:unknown): value is string => text(value,2048) && !isAbsolute(value) && !value.includes("\\") && !value.split("/").some(p=>!p || [".","..",".git"].includes(foldSegment(p))); const integer = (value:unknown) => typeof value === "string" && /^\d+$/.test(value) && Number.isSafeInteger(Number(value)); interface Xml { tag:string; attrs:Record; children:Xml[] } /** Small bounded XML data reader: no DTD, entities, processing instructions or external resources. */ function xml(raw:string): Xml { let root:Xml|undefined;const stack:Xml[]=[];let count=0; const decode=(s:string)=>s.replace(/&(amp|lt|gt|quot|apos);/g,(_,e:string)=>({amp:"&",lt:"<",gt:">",quot:'"',apos:"'"})[e]!); const tokens=raw.match(/|<[^>]*>|[^<]+/g)??[];if(tokens.join("")!==raw)throw Error(); for(const part of tokens) { if(part.startsWith("