diff --git a/.agents/skills/reverse-engineer-anything/SKILL.md b/.agents/skills/reverse-engineer-anything/SKILL.md index 3eaa239b8..dd4527664 100644 --- a/.agents/skills/reverse-engineer-anything/SKILL.md +++ b/.agents/skills/reverse-engineer-anything/SKILL.md @@ -31,7 +31,7 @@ When tools are absent or registration is stale: `npx -y rea-agents@latest doctor --client codex --json`. Substitute the current supported client: `claude_code`, `claude_desktop`, `codex`, `cursor`, `gemini_cli`, `windsurf`, `devin`, `opencode`, `antigravity`, `copilot_cli`, - `commandcode`, `vscode`, `grok_build`, `omp`, or `grok_bot`. `grok_bot` has no local + `commandcode`, `qwen_code`, `vscode`, `grok_build`, `omp`, `pi`, `hermes`, or `grok_bot`. `grok_bot` has no local file registration: doctor reports the Grok Bot chat step, and setup does not write `mcp.json`. If the client is unknown, use `doctor --json` and inspect its registration results before choosing a setup scope. diff --git a/docs/installation.md b/docs/installation.md index 18d4ba165..da726182b 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -208,10 +208,53 @@ Windows, `~/.hermes` elsewhere — including any `HERMES_DATA_DIR_SUFFIX`. Herme connects an entry unless `enabled` is false, so setup states `enabled: true`. Hermes personal skills install under the active Hermes home's `skills` directory. +Setup, doctor and uninstall follow the root's `active_profile` selection, as +plain Hermes CLI commands do. An explicit `HERMES_HOME` pointing to +`profiles/` selects that named profile directly. An invalid, missing or +deleted sticky profile is reported instead of writing into the default home; +repair the profile or run `hermes profile use default` before retrying. REA follows `HERMES_HOME` whitespace trimming, environment-variable expansion, and home expansion before resolving that profile. Setup previews the resolved configuration and skill destinations; doctor and uninstall use those same paths. +### DeepSeek Harness + +DeepSeek Harness uses a Cordis YAML patch sequence rather than a `mcpServers` +object. REA's `setup --client` does not edit this format. For Harness +`0.2.0-rc.2`, append the following entry to `$DSH_HOME/cordis.patch.yml` +(`~/.dsh/cordis.patch.yml` when `DSH_HOME` is unset). Preserve existing patches +and replace the executable paths with absolute paths from your installation: + +```yaml +- insert: + - id: rea-mcp + name: "@deepseek-ai/dsh-mcp-client" + config: + serverName: rea + transport: stdio + command: /absolute/path/to/node + args: ["/absolute/path/to/rea/scripts/rea.mjs", mcp] + cwd: !!js process.cwd() +``` + +For a global npm installation, the launcher is under +`$(npm root -g)/rea-agents/scripts/rea.mjs`. Check the Node executable with +`command -v node`. Register `serverName: rea` only once in each active profile, +then restart Harness. It exposes tools as `mcp__rea__`. +Run `rea setup --skill --dry-run`, review the destination, then +`rea setup --skill --yes` to install the workflow in `~/.agents/skills`. +Harness discovers that directory, or `$DSH_AGENTS_HOME/skills` when configured; +its native `skill` tool can load `reverse-engineer-anything`. + +REA's optional [native client verification](testing.md#end-to-end-integration-and-golden-evidence) +checks headless chat, skill loading, all tool input schemas and actual analysis +using the installed Harness and a local model fixture. This establishes the +Harness integration, with live DeepSeek API compatibility remaining unverified. +See the [Harness MCP guide](https://github.com/deepseek-ai/deepseek-harness/blob/639ed015397290b3745d163aafe02ffee4aa3f84/packages/mcp/mcp-client/README.md) +for its native configuration contract. + +### Other client settings + For OpenCode, setup writes the V1 `mcp.rea` entry, which OpenCode V1 and V2 both load. If the configuration already uses OpenCode V2's native `mcp.servers` table, setup registers REA there instead and replaces any earlier diff --git a/docs/testing.md b/docs/testing.md index 82b529929..031baa824 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -102,6 +102,23 @@ cleanup. These POSIX lanes use a deterministic loopback model, so they do not prove live model-provider or native Windows compatibility. Qwen and Pi use caller-configured skill directories to isolate the fixture account from the OS home; this does not prove their default home-directory discovery. +Set `REA_VERIFY_HERMES_STICKY_PROFILE=1` to exercise native Hermes selection of +a named sticky profile with `HERMES_HOME` still pointing to its root. + +`verify:deepseek-client` requires an installed DeepSeek Harness (`dsh`; +verified with `@deepseek-ai/dsh@0.2.0-rc.2`) and Git. Set +`REA_VERIFY_DEEPSEEK_COMMAND` to select its executable. This optional POSIX lane +installs the skill through `rea setup --skill`, writes the native Cordis MCP +patch into an isolated `DSH_HOME`, and uses `DSH_AGENTS_HOME` for native personal +skill discovery. A separate Git root prevents inherited project skills from +masking a missing installation. The default `call` mode checks skill loading, +complete catalog discovery, the actual forwarded input schemas and regexes, +and full JavaScript Evidence for a Unicode path. Use `-- chat` for ordinary +chat with REA enabled, or `REA_VERIFY_RUNTIME_ROOT` for an installed package. +The fixture uses Harness's custom OpenAI adapter and the one-million-token +context capacity declared by its default DeepSeek Flash model; it does not +establish live DeepSeek API or native Windows coverage. Receipts include +request artifacts, result digests and owned-process lineage. Full E2E tests invoke the production command dispatcher and real providers, without fake launchers, runners or responses. `verify:keyed-archive` writes an diff --git a/package.json b/package.json index 0c47e5901..663055d64 100644 --- a/package.json +++ b/package.json @@ -182,6 +182,7 @@ "verify:qwen-client": "npm run build:cached && node scripts/verify-qwen-client.mjs", "verify:pi-client": "npm run build:cached && node scripts/verify-native-agent-client.mjs pi", "verify:hermes-client": "npm run build:cached && node scripts/verify-native-agent-client.mjs hermes", + "verify:deepseek-client": "npm run build:cached && node scripts/verify-deepseek-client.mjs", "probe:windows-capabilities": "npm run build:cached && node scripts/windows-capabilities.mjs", "verify:published": "node scripts/verify-published-package.mjs", "config:print": "node scripts/print-mcp-config.mjs", diff --git a/scripts/verify-deepseek-client.mjs b/scripts/verify-deepseek-client.mjs new file mode 100644 index 000000000..d53131a88 --- /dev/null +++ b/scripts/verify-deepseek-client.mjs @@ -0,0 +1,346 @@ +import assert from "node:assert/strict"; +import { createHash } from "node:crypto"; +import { mkdir, mkdtemp, readFile, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; +import { Ajv2020 } from "ajv/dist/2020.js"; +import { + TOOL_CONTRACTS, + toolContract, +} from "../dist/contracts/toolContracts.js"; +import { exec } from "./lib/verify-package-core.mjs"; +import { createVerifierRun, completeVerifierRun } from "./lib/verifier-run.mjs"; +import { createOpenAiModelFixture } from "./verify/clients/model-fixture.mjs"; + +const mode = process.argv[2] ?? "call"; +assert(["chat", "call"].includes(mode), "Select chat or call."); +assert(process.platform !== "win32", "Native Windows coverage is unverified."); +const repo = fileURLToPath(new URL("..", import.meta.url)); +const runtimeRoot = process.env.REA_VERIFY_RUNTIME_ROOT ?? repo; +const command = process.env.REA_VERIFY_DEEPSEEK_COMMAND ?? "dsh"; +const lab = await mkdtemp(join(tmpdir(), "rea-deepseek-client-")); +const account = join(lab, "account"); +const profile = join(lab, "selected profile"); +const workspace = join(lab, "workspace"); +const target = join(workspace, "fixture α测试"); +const skillPath = join( + account, + ".agents", + "skills", + "reverse-engineer-anything", + "SKILL.md", +); +const cli = join(runtimeRoot, "scripts", "rea.mjs"); +const verifier = createVerifierRun(); +await Promise.all([ + mkdir(account), + mkdir(profile), + mkdir(target, { recursive: true }), +]); +const environment = { + PATH: process.env.PATH ?? "", + USERPROFILE: account, + DSH_HOME: profile, + DSH_AGENTS_HOME: join(account, ".agents"), + OPENAI_API_KEY: "local-fixture-key", + REA_PROCESS_RUN_ID: verifier.run_id, +}; +// A temporary directory under the checkout must not inherit its project skills or AGENTS.md. +await exec( + "git", + ["-c", "init.defaultBranch=main", "init", "--quiet", workspace], + { + env: environment, + timeout: 30_000, + }, +); +await writeFile( + join(target, "app.js"), + 'export function result() { return { clientCompatibility: "REA_CLIENT_FIXTURE", count: 7 }; }\n', +); +const clientVersion = ( + await exec(command, ["--version"], { + env: environment, + cwd: workspace, + timeout: 30_000, + }) +).stdout.trim(); +for (const args of [ + ["setup", "--skill", "--dry-run"], + ["setup", "--skill", "--yes"], +]) { + const result = await exec(process.execPath, [cli, ...args, "--json"], { + env: environment, + cwd: workspace, + timeout: 60_000, + }); + const parsed = JSON.parse(result.stdout); + assert.equal(parsed.status, args.includes("--dry-run") ? "planned" : "ready"); + if (args.includes("--dry-run")) { + assert.equal(parsed.plannedActions.length, 1); + assert.equal(parsed.plannedActions[0].kind, "install_skill"); + assert.equal(parsed.plannedActions[0].target, dirname(skillPath)); + } + await writeFile( + join(lab, args.includes("--dry-run") ? "setup-plan.json" : "setup.json"), + result.stdout, + ); +} +assert( + (await readFile(skillPath, "utf8")).includes( + "name: reverse-engineer-anything", + ), +); + +let catalog; +let patternCount = 0; +let skillRequested = false; +let skillLoaded = false; +let evidenceSeen = false; +let finalSeen = false; +let artifactDigest; +let mainRequests = 0; +const ajv = new Ajv2020({ strict: false, validateFormats: false }); +const patterns = new Set(); +const validatePatterns = (value) => { + if (!value || typeof value !== "object") return; + for (const [key, child] of Object.entries(value)) { + if (key === "pattern" && typeof child === "string") { + // Check the representation the native client actually forwards to its model adapter. + for (const flags of ["", "u", "v"]) new RegExp(child, flags); + patterns.add(child); + patternCount++; + } else validatePatterns(child); + } +}; +const extractEvidence = (value) => { + if (typeof value === "string") { + try { + return extractEvidence(JSON.parse(value)); + } catch { + return undefined; + } + } + if (value && typeof value === "object") { + if ( + typeof value.evidence_id === "string" && + value.normalized_result?.graph?.nodes + ) + return value; + for (const child of Object.values(value)) { + const found = extractEvidence(child); + if (found) return found; + } + } +}; +const fixture = await createOpenAiModelFixture({ + directory: lab, + prefix: `deepseek-${mode}`, + onRequest: async ({ body, tools, requestIndex }) => { + // Harness generates a session title in a separate, tool-free model request. + if (tools.length === 0) + return { role: "assistant", content: "REA validation" }; + assert( + ++mainRequests <= 4, + "Harness did not complete the bounded fixture workflow.", + ); + const rea = tools.filter((tool) => tool.name.startsWith("mcp__rea__")); + const observedCatalog = rea + .map((tool) => tool.name.slice("mcp__rea__".length)) + .sort(); + assert.deepEqual( + observedCatalog, + TOOL_CONTRACTS.map((tool) => tool.name).sort(), + ); + if (catalog === undefined) { + catalog = observedCatalog; + for (const tool of rea) { + assert( + ajv.validateSchema(tool.parameters), + `${tool.name}: ${ajv.errorsText(ajv.errors)}`, + ); + validatePatterns(tool.parameters); + } + assert( + patternCount > 0, + "The advertised regex constraints must reach the model.", + ); + } + const toolMessages = (body.messages ?? []).filter( + (message) => message.role === "tool", + ); + const serialized = JSON.stringify(toolMessages); + skillLoaded ||= + serialized.includes( + '', + ) && serialized.includes(dirname(skillPath)); + for (const message of toolMessages) { + const found = extractEvidence(message.content); + if (found === undefined) continue; + const evidence = toolContract( + "analyze_javascript_application", + ).outputSchema.parse(found); + assert.equal(evidence.subject.local_path, target); + const shape = evidence.normalized_result.graph.nodes + .flatMap((node) => node.observations) + .map((observation) => observation.properties) + .find( + (properties) => + properties.semantic_role === "export-return-shapes" && + properties.exported_name === "result", + ); + assert(shape?.return_shape_coverage?.projection_complete); + const fields = shape.static_return_shapes.flatMap( + (entry) => entry.fields, + ); + assert( + fields.some( + (field) => + field.path === "/clientCompatibility" && + field.state === "literal" && + field.value === "REA_CLIENT_FIXTURE", + ), + ); + assert( + fields.some( + (field) => + field.path === "/count" && + field.state === "literal" && + field.value === 7, + ), + ); + const text = JSON.stringify(evidence); + await writeFile(join(lab, "evidence.json"), text); + artifactDigest = createHash("sha256").update(text).digest("hex"); + evidenceSeen = true; + } + if (mode === "chat" || evidenceSeen) { + finalSeen = true; + return { role: "assistant", content: "REA_CLIENT_COMPATIBILITY_OK" }; + } + const call = (name, args) => ({ + role: "assistant", + content: null, + tool_calls: [ + { + id: `call_rea_${requestIndex}`, + type: "function", + function: { name, arguments: JSON.stringify(args) }, + }, + ], + }); + if (!skillRequested) { + assert(tools.some((tool) => tool.name === "skill")); + skillRequested = true; + return call("skill", { name: "reverse-engineer-anything" }); + } + assert( + skillLoaded, + "Harness must load the installed skill before analysis.", + ); + return call("mcp__rea__analyze_javascript_application", { + input_path: target, + format: "directory", + }); + }, +}); +let failure; +try { + // Use the client's native Cordis patch DSL; generic `rea setup --client` does not edit it. + await writeFile( + join(profile, "cordis.patch.yml"), + ` +- id: llm-pi-ai + config: + providers: + rea-fixture: + apiKeyEnv: OPENAI_API_KEY + api: openai-completions + baseURL: ${fixture.baseUrl} + defaultMaxTokens: 4096 + retryPolicy: + mode: normal + maxRetries: 0 + models: + - id: rea-client-fixture + contextWindow: 1000000 +- id: agent-default-model + config: + provider: rea-fixture + model: rea-client-fixture +- insert: + - id: rea-mcp + name: '@deepseek-ai/dsh-mcp-client' + config: + serverName: rea + transport: stdio + command: ${JSON.stringify(process.execPath)} + args: ${JSON.stringify([cli, "mcp"])} + cwd: !!js process.cwd() + env: + REA_PROCESS_RUN_ID: ${JSON.stringify(verifier.run_id)} +`, + ); + const execution = exec( + command, + [ + "--profile", + "headless", + "--json", + mode === "chat" ? "Say hello briefly." : `Use REA to analyze ${target}.`, + ], + { + env: environment, + cwd: workspace, + timeout: 120_000, + maxBuffer: 10 * 1024 * 1024, + }, + ); + execution.child.stdin.end(); + const result = await execution; + await writeFile(join(lab, "client.stdout"), result.stdout); + await writeFile(join(lab, "client.stderr"), result.stderr); + assert.equal(fixture.failure, undefined); + assert(finalSeen && result.stdout.includes("REA_CLIENT_COMPATIBILITY_OK")); + if (mode === "call") assert(evidenceSeen && skillLoaded); +} catch (cause) { + failure = cause; + if (typeof cause?.stdout === "string") + await writeFile(join(lab, "client.stdout"), cause.stdout); + if (typeof cause?.stderr === "string") + await writeFile(join(lab, "client.stderr"), cause.stderr); +} finally { + await fixture.close(); + const receipt = { + client: "deepseek-harness", + clientVersion, + mode, + passed: failure === undefined, + artifacts: lab, + runtimeRoot, + host: { + platform: process.platform, + architecture: process.arch, + node: process.versions.node, + }, + modelFixture: + "loopback custom OpenAI-compatible adapter; no live DeepSeek provider claim", + modelContextWindow: 1000000, + skillDiscovery: + "native DSH_AGENTS_HOME personal directory; isolated project root", + catalogSize: catalog?.length, + patternCount, + uniquePatterns: patterns.size, + skillLoaded, + evidenceSeen, + artifactDigest, + finalSeen, + requests: fixture.requests, + fixtureFailure: fixture.failure, + verifier: await completeVerifierRun(verifier), + }; + await writeFile(join(lab, "receipt.json"), JSON.stringify(receipt, null, 2)); + console.log(JSON.stringify(receipt)); +} +if (failure !== undefined) throw failure; diff --git a/scripts/verify-native-agent-client.mjs b/scripts/verify-native-agent-client.mjs index f561e353e..5b6600479 100644 --- a/scripts/verify-native-agent-client.mjs +++ b/scripts/verify-native-agent-client.mjs @@ -27,11 +27,16 @@ const command = const lab = await mkdtemp(join(tmpdir(), `rea-${client}-client-`)); const account = join(lab, "account"); const profile = join(lab, "selected profile"); +const stickyHermes = + client === "hermes" && process.env.REA_VERIFY_HERMES_STICKY_PROFILE === "1"; +const selectedProfile = stickyHermes + ? join(profile, "profiles", "coder") + : profile; const workspace = join(lab, "workspace"); const target = join(workspace, "fixture α测试"); const artifactPath = join(workspace, "evidence.json"); const skillPath = join( - client === "pi" ? join(account, ".agents") : profile, + client === "pi" ? join(account, ".agents") : selectedProfile, "skills", "reverse-engineer-anything", "SKILL.md", @@ -39,7 +44,8 @@ const skillPath = join( const verifier = createVerifierRun(); await Promise.all([ mkdir(account), - mkdir(profile), + mkdir(profile, { recursive: true }), + ...(stickyHermes ? [mkdir(selectedProfile, { recursive: true })] : []), mkdir(target, { recursive: true }), ]); await writeFile( @@ -53,7 +59,11 @@ const environment = { PI_OFFLINE: "1", REA_PROCESS_RUN_ID: verifier.run_id, }; -const configPath = join(profile, client === "pi" ? "mcp.json" : "config.yaml"); +const configPath = join( + selectedProfile, + client === "pi" ? "mcp.json" : "config.yaml", +); +if (stickyHermes) await writeFile(join(profile, "active_profile"), "coder\n"); const original = client === "pi" ? JSON.stringify({ @@ -440,6 +450,7 @@ try { client === "pi" ? "caller-configured isolated shared directory; default OS home discovery unverified" : "native HERMES_HOME personal directory", + stickyHermes, catalogSize: catalog?.length, evidenceSeen, skillLoaded, diff --git a/src/application/HermesClientHome.ts b/src/application/HermesClientHome.ts new file mode 100644 index 000000000..c3f148d96 --- /dev/null +++ b/src/application/HermesClientHome.ts @@ -0,0 +1,150 @@ +import { lstatSync, readFileSync, realpathSync, statSync } from "node:fs"; +import { + basename, + dirname, + isAbsolute, + join, + relative, + resolve, + sep, +} from "node:path"; + +/** An unresolved Hermes profile must not be configured as the default home. */ +export class HermesClientPathError extends Error { + constructor( + readonly path: string, + message: string, + ) { + super(message); + this.name = "HermesClientPathError"; + } +} + +const defaultDirectory = ( + home: string, + platform: NodeJS.Platform, + env: Readonly, +): string => { + const suffix = env.HERMES_DATA_DIR_SUFFIX ?? ""; + return platform === "win32" + ? join( + env.LOCALAPPDATA?.trim() || join(home, "AppData", "Local"), + `hermes${suffix}`, + ) + : join(home, `.hermes${suffix}`); +}; +const expandedDirectory = ( + home: string, + platform: NodeJS.Platform, + env: Readonly, +): string | undefined => { + const override = env.HERMES_HOME?.trim(); + if (!override) return undefined; + const variables = override.replace( + /\$(\w+|\{[^}]*\})/gu, + (match, variable: string) => { + const key = variable.startsWith("{") ? variable.slice(1, -1) : variable; + return env[key] ?? match; + }, + ); + const expanded = + platform === "win32" + ? variables.replace( + /%([^%]+)%/gu, + (match, variable: string) => env[variable] ?? match, + ) + : variables; + if (expanded === "~") return home; + if ( + expanded.startsWith("~/") || + (platform === "win32" && expanded.startsWith("~\\")) + ) + return join(home, expanded.slice(2)); + return resolve(expanded); +}; +const physicalPath = (path: string): string => { + try { + return realpathSync(path); + } catch { + return resolve(path); + } +}; +const within = (root: string, path: string): boolean => { + const difference = relative(physicalPath(root), physicalPath(path)); + return ( + difference === "" || + (!isAbsolute(difference) && + difference !== ".." && + !difference.startsWith(`..${sep}`)) + ); +}; +const PROFILE_MARKERS = [ + "config.yaml", + ".env", + "SOUL.md", + "profile.yaml", + "auth.json", + "state.db", +]; +const RESERVED_PROFILES = new Set(["hermes", "test", "tmp", "root", "sudo"]); +const liveProfile = (path: string): boolean => { + try { + if (!statSync(path).isDirectory()) return false; + try { + statSync(join(dirname(path), ".deleted", basename(path))); + return false; + } catch { + /* No tombstone. */ + } + return PROFILE_MARKERS.some((marker) => { + try { + const status = lstatSync(join(path, marker)); + return status.isFile() || status.isSymbolicLink(); + } catch { + return false; + } + }); + } catch { + return false; + } +}; + +/** Match Hermes CLI sticky-profile selection while retaining the configured path spelling. */ +export const resolveHermesClientHome = ( + home: string, + platform: NodeJS.Platform, + env: Readonly, +): string => { + const native = defaultDirectory(home, platform, env); + const root = expandedDirectory(home, platform, env) ?? native; + // An explicit named profile bypasses the root's sticky selection in Hermes CLI. + if (basename(dirname(root)) === "profiles") return root; + const stickyRoot = within(native, root) ? native : root; + let name: string; + try { + // Python uses utf-8-sig and ignores unreadable / invalid-UTF8 active_profile files. + name = new TextDecoder("utf-8", { fatal: true }) + .decode(readFileSync(join(stickyRoot, "active_profile"))) + .trim(); + } catch { + return root; + } + if (name === "" || name === "default") return root; + const canonical = name.toLowerCase(); + if (canonical === "default") return root; + if ( + !/^[a-z0-9][a-z0-9_-]{0,63}$/u.test(canonical) || + RESERVED_PROFILES.has(canonical) + ) + throw new HermesClientPathError( + join(stickyRoot, "active_profile"), + `Invalid saved Hermes profile ${JSON.stringify(name)}. Run 'hermes profile use default' or repair active_profile.`, + ); + const selected = join(root, "profiles", canonical); + if (!liveProfile(selected)) + throw new HermesClientPathError( + selected, + `Saved Hermes profile ${JSON.stringify(name)} is missing, deleted, or has no profile identity. Run 'hermes profile use default' or restore that profile before configuring it.`, + ); + return selected; +}; diff --git a/src/application/SupportedClients.ts b/src/application/SupportedClients.ts index dfeee34b7..c87925f98 100644 --- a/src/application/SupportedClients.ts +++ b/src/application/SupportedClients.ts @@ -1,6 +1,10 @@ import { dirname, isAbsolute, join, resolve, win32 } from "node:path"; import { fileURLToPath } from "node:url"; import { lstatSync } from "node:fs"; +import { + HermesClientPathError, + resolveHermesClientHome, +} from "./HermesClientHome.js"; /** One supported client configuration location. */ export interface SetupClient { @@ -115,49 +119,8 @@ const qwenCodeDirectory = ({ home, env }: ClientPathContext): string => { const grokDirectory = ({ home, env }: ClientPathContext): string => env.GROK_HOME || join(home, ".grok"); -/** - * Hermes Agent home. `HERMES_HOME` wins; otherwise the platform default mirrors - * Hermes's own resolver (`%LOCALAPPDATA%\\hermes` on Windows, `~/.hermes` - * elsewhere), including the optional `HERMES_DATA_DIR_SUFFIX` that is appended - * to the literal `hermes`/`.hermes` directory name. - */ -const hermesDirectory = ({ - home, - platform, - env, -}: ClientPathContext): string => { - const override = env.HERMES_HOME?.trim(); - if (override) { - const expandedVariables = override.replace( - /\$(\w+|\{[^}]*\})/gu, - (match, variable: string) => { - const key = variable.startsWith("{") ? variable.slice(1, -1) : variable; - return env[key] ?? match; - }, - ); - const expanded = - platform === "win32" - ? expandedVariables.replace( - /%([^%]+)%/gu, - (match, variable: string) => env[variable] ?? match, - ) - : expandedVariables; - if (expanded === "~") return home; - if ( - expanded.startsWith("~/") || - (platform === "win32" && expanded.startsWith("~\\")) - ) - return join(home, expanded.slice(2)); - return resolve(expanded); - } - const suffix = env.HERMES_DATA_DIR_SUFFIX ?? ""; - return platform === "win32" - ? join( - env.LOCALAPPDATA?.trim() || join(home, "AppData", "Local"), - `hermes${suffix}`, - ) - : join(home, `.hermes${suffix}`); -}; +const hermesDirectory = ({ home, platform, env }: ClientPathContext): string => + resolveHermesClientHome(home, platform, env); /** Grok Bot uses an absolute SAND_DATA_ROOT; anything else stays ~/.grokbot. */ const grokBotDirectory = ({ home, env }: ClientPathContext): string => { @@ -577,6 +540,14 @@ export const supportedClients = ( format: definition.format, }; } catch (cause: unknown) { + if (cause instanceof HermesClientPathError) + return { + name: definition.name, + displayName: definition.displayName, + format: definition.format, + configPath: cause.path, + configPathError: cause.message, + }; if (definition.name !== "pi") throw cause; return { name: definition.name, @@ -611,14 +582,21 @@ export const clientSkillDirectories = ( const destinations = new Map(); for (const client of selected) { const definition = definitions.find(({ name }) => name === client); - const directory = - definition?.skillPath === undefined - ? join(home, ".agents", "skills") - : resolvePath(definition.skillPath, { - home, - platform, - env: environment, - }); + let directory: string; + try { + directory = + definition?.skillPath === undefined + ? join(home, ".agents", "skills") + : resolvePath(definition.skillPath, { + home, + platform, + env: environment, + }); + } catch (cause: unknown) { + // Registration diagnostics own an unresolved profile; never fall back to another skill root. + if (cause instanceof HermesClientPathError) continue; + throw cause; + } if (!destinations.has(directory)) destinations.set(directory, { client: diff --git a/src/application/javascript/JavaScriptApplicationCancellation.test.ts b/src/application/javascript/JavaScriptApplicationCancellation.test.ts index 1457046ef..5c2b2f005 100644 --- a/src/application/javascript/JavaScriptApplicationCancellation.test.ts +++ b/src/application/javascript/JavaScriptApplicationCancellation.test.ts @@ -42,6 +42,46 @@ describe("JavaScript analysis cancellation before publication", () => { expect(phases).not.toContain("validate_javascript_application_result"); }); + it("handles control turns and cancellation inside one large source file (#1462)", async () => { + const root = await createTestTempDirectory("rea-js-cancel-in-file-"); + await writeFile( + join(root, "index.js"), + Array.from( + { length: 1200 }, + (_, index) => + `export function f${index}(){return {name:'f${index}',value:${index}};}`, + ).join("\n"), + ); + const controller = new AbortController(); + const phases: string[] = []; + let turns = 0; + // Count event-loop turns once the file starts; parsing, analysis and + // projection of this one file must leave room for them. + const tick = (): void => { + turns += 1; + if (turns === 3) controller.abort(); + else if (!controller.signal.aborted) setImmediate(tick); + }; + const result = await analyzeJavaScriptApplication( + { input_path: root, format: "directory" }, + { + signal: controller.signal, + progress: { + async report(event) { + phases.push(event.phase); + if (event.phase === "parse_javascript_source") setImmediate(tick); + }, + }, + }, + ); + if (result.ok) throw new Error("Cancelled analysis must not publish"); + expect(projectAnalysisError(result.error)).toMatchObject({ + code: "cancelled", + }); + expect(phases).toContain("parse_javascript_source"); + expect(phases).not.toContain("build_javascript_application_graph"); + }); + it.each([ "parse_javascript_source", "build_javascript_application_graph", diff --git a/src/application/javascript/JavaScriptArtifactAnalysis.ts b/src/application/javascript/JavaScriptArtifactAnalysis.ts index 84390b873..3fac39dbb 100644 --- a/src/application/javascript/JavaScriptArtifactAnalysis.ts +++ b/src/application/javascript/JavaScriptArtifactAnalysis.ts @@ -33,6 +33,7 @@ import type { JavaScriptSourceMapOriginal, } from "./JavaScriptArtifactAnalysisTypes.js"; import { analyzeJavaScriptJsonModule } from "./JavaScriptJsonModules.js"; +import { completeJavaScriptAnalysisSteps } from "./JavaScriptAnalysisControl.js"; import { htmlArtifactReferences } from "../../domain/javascript/htmlArtifactReferences.js"; interface MutableArtifactAnalysis< @@ -56,34 +57,44 @@ interface ArtifactAnalysisContext< readonly projectSemantics: ( file: JavaScriptArtifactFile, ir: JavaScriptSemanticIr, - ) => SemanticIr; + ) => Generator; } /** Analyze every text source from the artifact without a result quota. */ export const analyzeJavaScriptArtifactFiles = ( fileSet: JavaScriptArtifactFileSet, ): JavaScriptArtifactAnalysis => - analyzeArtifactFiles(fileSet, (_file, ir) => ir); + analyzeArtifactFiles(fileSet, function* (_file, ir) { + // The full IR needs no projection; one step keeps the shared protocol. + yield; + return ir; + }); -/** Project each file's full IR immediately, retaining only later module facts. */ +/** + * Project each file's full IR immediately, retaining only later module facts. + * Parsing, analysis and each projection pass run as separate steps, so control + * messages and cancellation are handled within one large file as well. + */ export const analyzeAndProjectJavaScriptArtifactFiles = async ( fileSet: JavaScriptArtifactFileSet, projectSemantics: ( file: JavaScriptArtifactFile, ir: JavaScriptSemanticIr, - ) => void, + ) => Iterator | void, beforeFile?: ( file: JavaScriptArtifactFile, completed: number, total: number, ) => Promise, + signal?: AbortSignal, ): Promise => { const state = emptyArtifactAnalysis(); - const projectFileSemantics = ( + const projectFileSemantics = function* ( file: JavaScriptArtifactFile, ir: JavaScriptSemanticIr, - ): JavaScriptModuleSemanticIr => { - projectSemantics(file, ir); + ): Generator { + const projection = projectSemantics(file, ir); + if (projection !== undefined) while (projection.next().done !== true) yield; const programScopes = ir.scopes.filter(({ kind }) => kind === "program"); const programScopeId = programScopes[0]?.scopeId; const localNames = new Set( @@ -111,7 +122,10 @@ export const analyzeAndProjectJavaScriptArtifactFiles = async ( // Let progress, cancellation and garbage collection run between files. await setImmediate(); await beforeFile?.(file, index, fileSet.files.length); - analyzeArtifactFile(file, context); + await completeJavaScriptAnalysisSteps( + analyzeArtifactFileSteps(file, context), + signal, + ); } return finalizeArtifactAnalysis(state); }; @@ -122,7 +136,10 @@ const analyzeArtifactFiles = ( ): JavaScriptArtifactAnalysis => { const state = emptyArtifactAnalysis(); const context = { state, projectSemantics }; - for (const file of fileSet.files) analyzeArtifactFile(file, context); + for (const file of fileSet.files) { + const steps = analyzeArtifactFileSteps(file, context); + while (steps.next().done !== true); + } return finalizeArtifactAnalysis(state); }; @@ -148,10 +165,12 @@ const finalizeArtifactAnalysis = < }; }; -const analyzeArtifactFile = ( +function* analyzeArtifactFileSteps< + SemanticIr extends JavaScriptModuleSemanticIr, +>( file: JavaScriptArtifactFile, context: ArtifactAnalysisContext, -): void => { +): Generator { const { state } = context; addStructuredObservations(file, state); if (file.kind === "html" && file.text.included) @@ -162,6 +181,7 @@ const analyzeArtifactFile = ( return; } const parsed = parseJavaScriptSource(file.text.value, file.path); + yield; if (parsed === null) { const analysis = failedJavaScriptStaticAnalysis(); state.files.push({ file, javascript: analysis, semantic: null }); @@ -175,17 +195,19 @@ const analyzeArtifactFile = ( openReceiverFacts, ); const staticFindings = findingCount(analysis); + yield; const semantics = analysis.parse_status === "complete" || analysis.parse_status === "partial" ? analyzeParsedJavaScriptSemantics(parsed) : null; + const projected = + semantics === null + ? null + : yield* context.projectSemantics(file, semantics); state.files.push({ file, javascript: analysis, - semantic: - semantics === null - ? null - : { ir: context.projectSemantics(file, semantics) }, + semantic: projected === null ? null : { ir: projected }, }); state.visitedNodes += analysis.visited_ast_nodes; state.findings += staticFindings + (semantics?.moduleLinks.length ?? 0); @@ -194,7 +216,7 @@ const analyzeArtifactFile = ( 0, ); if (analysis.parse_status === "failed") state.parseFailures += 1; -}; +} const addSourceMap = ( file: JavaScriptArtifactFile, diff --git a/src/application/javascript/JavaScriptArtifactGraphBuilder.ts b/src/application/javascript/JavaScriptArtifactGraphBuilder.ts index bf46a0557..023802a79 100644 --- a/src/application/javascript/JavaScriptArtifactGraphBuilder.ts +++ b/src/application/javascript/JavaScriptArtifactGraphBuilder.ts @@ -1,7 +1,7 @@ import type { ArtifactInventorySnapshot } from "../../domain/artifactInventorySnapshot.js"; import { createJavaScriptApplicationGraph, - createImmutableJavaScriptApplicationGraphSteps, + sealTransferredJavaScriptApplicationGraphSteps, type JavaScriptApplicationGraph, } from "../../domain/javascript/javascriptApplicationGraph.js"; import type { JavaScriptModuleArtifactAnalysis } from "./JavaScriptArtifactAnalysisTypes.js"; @@ -15,9 +15,9 @@ import { addJavaScriptHtmlRoles, addJavaScriptSourceMapOriginals, } from "./JavaScriptArtifactGraphDocuments.js"; -import { addJavaScriptStaticFindings } from "./JavaScriptArtifactGraphFindings.js"; +import { addJavaScriptStaticFindingsSteps } from "./JavaScriptArtifactGraphFindings.js"; import { - addJavaScriptModuleRelationships, + addJavaScriptModuleRelationshipsSteps, type JavaScriptModuleRelationshipOmissions, addJavaScriptSourceModules, } from "./JavaScriptModuleRelationships.js"; @@ -51,24 +51,42 @@ export const buildJavaScriptArtifactGraph = ( analysis: JavaScriptModuleArtifactAnalysis, ): JavaScriptApplicationGraph => createJavaScriptApplicationGraph( - buildJavaScriptArtifactGraphInput(snapshot, fileSet, analysis), + completeSynchronously( + buildJavaScriptArtifactGraphInputSteps(snapshot, fileSet, analysis), + ), ); -/** Build a validated application graph with cooperative immutable ownership. */ -export const buildImmutableJavaScriptArtifactGraphSteps = ( +/** + * Build a validated application graph with cooperative immutable ownership. + * Constructing the graph input also runs in steps: projecting every module + * relationship and finding at once held the event loop for seconds. + */ +export function* buildImmutableJavaScriptArtifactGraphSteps( snapshot: ArtifactInventorySnapshot, fileSet: JavaScriptArtifactFileSet, analysis: JavaScriptModuleArtifactAnalysis, -): Generator => - createImmutableJavaScriptApplicationGraphSteps( - buildJavaScriptArtifactGraphInput(snapshot, fileSet, analysis), +): Generator { + const input = yield* buildJavaScriptArtifactGraphInputSteps( + snapshot, + fileSet, + analysis, ); + // The input was built here and is not retained, so it can be transferred. + return yield* sealTransferredJavaScriptApplicationGraphSteps(input); +} -const buildJavaScriptArtifactGraphInput = ( +const completeSynchronously = (steps: Iterator): Value => { + for (;;) { + const step = steps.next(); + if (step.done === true) return step.value; + } +}; + +function* buildJavaScriptArtifactGraphInputSteps( snapshot: ArtifactInventorySnapshot, fileSet: JavaScriptArtifactFileSet, analysis: JavaScriptModuleArtifactAnalysis, -): unknown => { +): Generator { const accumulator = new JavaScriptArtifactGraphAccumulator(); const root = createJavaScriptArtifactRootNode(accumulator, snapshot); const context: JavaScriptArtifactGraphContext = { @@ -86,15 +104,23 @@ const buildJavaScriptArtifactGraphInput = ( containerNodes: new Map([[snapshot.manifest.root_sha256, root]]), }; addJavaScriptArtifactContainers(context); + yield; addJavaScriptArtifactFiles(context); + yield; const packageRoots = addJavaScriptPackageNodes(context); + yield; addJavaScriptSourceModules(context); + yield; const bundlerLimitations = addJavaScriptBundlerNodes(context); - const relationshipOmissions = addJavaScriptModuleRelationships(context); - const findingLimitations = addJavaScriptStaticFindings(context); + const relationshipOmissions = + yield* addJavaScriptModuleRelationshipsSteps(context); + const findingLimitations = yield* addJavaScriptStaticFindingsSteps(context); addElectronBoundaries(context); + yield; addJavaScriptHtmlRoles(context); + yield; addJavaScriptSourceMapOriginals(context); + yield; const coverage = graphCoverage(context); return { schema: "JavaScriptApplicationGraph", @@ -111,7 +137,7 @@ const buildJavaScriptArtifactGraphInput = ( ...graphLimitations(context, coverage.status, relationshipOmissions), ], }; -}; +} const graphCoverage = (context: JavaScriptArtifactGraphContext) => { const resourceLimits = semanticResourceLimits(context); diff --git a/src/application/javascript/JavaScriptArtifactGraphFindings.ts b/src/application/javascript/JavaScriptArtifactGraphFindings.ts index 41f480daf..e7a002a6a 100644 --- a/src/application/javascript/JavaScriptArtifactGraphFindings.ts +++ b/src/application/javascript/JavaScriptArtifactGraphFindings.ts @@ -44,11 +44,12 @@ interface ResolvedReference { } /** Project imports, workers, roles, endpoints, storage, and source-map links. */ -export const addJavaScriptStaticFindings = ( +export function* addJavaScriptStaticFindingsSteps( context: JavaScriptArtifactGraphContext, -): readonly string[] => { +): Generator { const omissions = { selfReferences: 0 }; for (const analyzed of context.analysis.files) { + yield; const { file, javascript } = analyzed; const asset = context.assetNodes.get(file.path); if (javascript === null || asset === undefined) continue; @@ -69,7 +70,7 @@ export const addJavaScriptStaticFindings = ( ["static reference", "static references"], "the referencing module", ); -}; +} const addReference = ( context: JavaScriptArtifactGraphContext, diff --git a/src/application/javascript/JavaScriptArtifactReconstruction.ts b/src/application/javascript/JavaScriptArtifactReconstruction.ts index ab986608a..25919923c 100644 --- a/src/application/javascript/JavaScriptArtifactReconstruction.ts +++ b/src/application/javascript/JavaScriptArtifactReconstruction.ts @@ -90,7 +90,7 @@ export const reconstructJavaScriptArtifact = async ( const semanticProjection = createJavaScriptSemanticGraphProjection(); const analysis = await analyzeAndProjectJavaScriptArtifactFiles( files, - semanticProjection.projectFile, + semanticProjection.projectFileSteps, async (file, completed, total) => { abortIfNeeded(signal); await progress?.report({ @@ -101,6 +101,7 @@ export const reconstructJavaScriptArtifact = async ( }); abortIfNeeded(signal); }, + signal, ); await checkpointJavaScriptAnalysis(signal); abortIfNeeded(signal); diff --git a/src/application/javascript/JavaScriptModuleRelationships.ts b/src/application/javascript/JavaScriptModuleRelationships.ts index 8e12f3beb..3784141a8 100644 --- a/src/application/javascript/JavaScriptModuleRelationships.ts +++ b/src/application/javascript/JavaScriptModuleRelationships.ts @@ -136,15 +136,17 @@ export interface JavaScriptModuleRelationshipOmissions { type RelationshipOmissionCounter = { selfImports: number }; /** Compose bounded CommonJS and ESM binding relationships across artifact files. */ -export const addJavaScriptModuleRelationships = ( +export function* addJavaScriptModuleRelationshipsSteps( context: JavaScriptArtifactGraphContext, -): JavaScriptModuleRelationshipOmissions => { +): Generator { const omissions = { selfImports: 0 }; for (const analyzed of context.analysis.files) { const { file, semantic } = analyzed; const source = context.sourceModuleNodes.get(file.path); if (semantic === null || source === undefined) continue; for (const link of semantic.ir.moduleLinks) { + // One module can export thousands of bindings, each with return shapes. + yield; const input = { context, file, semantic, source, link }; if (isExportLink(link)) addExportRelationship(input, omissions); else if (link.specifier !== null) @@ -155,7 +157,7 @@ export const addJavaScriptModuleRelationships = ( } } return omissions; -}; +} const addExportRelationship = ( input: RelationshipInput, diff --git a/src/application/javascript/JavaScriptSemanticGraphBuilder.ts b/src/application/javascript/JavaScriptSemanticGraphBuilder.ts index 45ded5051..770930de7 100644 --- a/src/application/javascript/JavaScriptSemanticGraphBuilder.ts +++ b/src/application/javascript/JavaScriptSemanticGraphBuilder.ts @@ -1,6 +1,6 @@ import { createJavaScriptSemanticGraph, - createImmutableJavaScriptSemanticGraphSteps, + sealTransferredJavaScriptSemanticGraphSteps, type JavaScriptSemanticGraph, } from "../../domain/javascript/javascriptSemanticGraph.js"; import type { @@ -130,6 +130,11 @@ export interface JavaScriptSemanticGraphProjection { file: JavaScriptArtifactFile, ir: JavaScriptSemanticIr, ) => void; + /** Project one file in steps between its projection passes. */ + readonly projectFileSteps: ( + file: JavaScriptArtifactFile, + ir: JavaScriptSemanticIr, + ) => Generator; readonly finish: ( rootArtifactSha256: string, applicationGraph: BuilderInput["applicationGraph"], @@ -146,10 +151,10 @@ export const createJavaScriptSemanticGraphProjection = const state = emptyState({ nodes: [] }); const fingerprints: JavaScriptSemanticFingerprint[] = []; let truncatedFiles = 0; - const projectSource = ( + const projectSourceSteps = function* ( file: JavaScriptArtifactFile, ir: JavaScriptSemanticIr, - ): void => { + ): Generator { if (state.nodes.size >= SEMANTIC_GRAPH_NODE_CEILING) { truncatedFiles += 1; return; @@ -161,7 +166,7 @@ export const createJavaScriptSemanticGraphProjection = remainingTreeBudget, ); state.fileNodesDropped = false; - fingerprints.push(...projectFile(file, ir, state)); + fingerprints.push(...(yield* projectFileSteps(file, ir, state))); // Nodes were dropped only if the budget actually blocked creation. A file // that exactly fills its share ends with a zero budget without dropping // anything, so the remaining budget alone cannot decide truncation. @@ -198,7 +203,8 @@ export const createJavaScriptSemanticGraphProjection = evidence_contexts: state.evidenceContexts.contexts, nodes: [...state.nodes.values()], relations, - fingerprints, + // The input is transferred to the factory; `fingerprints` is cleared below. + fingerprints: [...fingerprints], unknowns, coverage: { status: truncatedFiles > 0 ? "partial" : "unknown", @@ -246,7 +252,11 @@ export const createJavaScriptSemanticGraphProjection = return graph; }; return { - projectFile: projectSource, + projectFile: (file, ir) => { + const steps = projectSourceSteps(file, ir); + while (steps.next().done !== true); + }, + projectFileSteps: projectSourceSteps, finish: (rootArtifactSha256, applicationGraph) => finishWith( createJavaScriptSemanticGraph, @@ -255,18 +265,19 @@ export const createJavaScriptSemanticGraphProjection = ), finishImmutableSteps: (rootArtifactSha256, applicationGraph) => finishWith( - createImmutableJavaScriptSemanticGraphSteps, + sealTransferredJavaScriptSemanticGraphSteps, rootArtifactSha256, applicationGraph, ), }; }; -const projectFile = ( +function* projectFileSteps( file: JavaScriptArtifactFile, ir: JavaScriptSemanticIr, state: BuilderState, -): JavaScriptSemanticFingerprint[] => { +): Generator { + yield; const moduleNode = retainNode(state, file, { kind: "module", roleKey: "module", @@ -328,6 +339,7 @@ const projectFile = ( callableOwnerAt, callSiteAt: createSemanticCallSiteLookup(ir, callSiteNodes), }; + yield; projectDefinitionsAndReferences(definitions); const context: FileContext = { ...definitions, @@ -335,26 +347,20 @@ const projectFile = ( definitions.referenceNodes, ), }; - projectSemanticValues(context); - projectSemanticObjects(context); - projectCalls(context); - projectSemanticPromises(context); - projectSemanticEvents(context); - projectSemanticTimers(context); - projectSemanticChildProcesses(context); - projectSemanticConfiguration(context); - projectSemanticRequests(context); - projectSemanticBoundaries(context); - projectSemanticResources(context); - projectSemanticClosureCaptures(context); - projectSemanticFrontiers(context); + // Each pass completes before the next starts, so yielding between passes + // keeps projection order and results unchanged. + for (const project of FILE_PROJECTION_PASSES) { + yield; + project(context); + } + yield; return projectSemanticFunctionFingerprints( file, ir, callableNodes, state.evidenceContexts, ); -}; +} const createReturnSiteNodes = ( file: JavaScriptArtifactFile, @@ -543,3 +549,20 @@ const createArgumentNode = ( : `argument ${String(argument.index)}`, functionNodeId: callNode.function_node_id, }); + +/** Per-file projection passes, run in this order with a yield between each. */ +const FILE_PROJECTION_PASSES: readonly ((context: FileContext) => void)[] = [ + projectSemanticValues, + projectSemanticObjects, + projectCalls, + projectSemanticPromises, + projectSemanticEvents, + projectSemanticTimers, + projectSemanticChildProcesses, + projectSemanticConfiguration, + projectSemanticRequests, + projectSemanticBoundaries, + projectSemanticResources, + projectSemanticClosureCaptures, + projectSemanticFrontiers, +]; diff --git a/src/domain/javascript/javascriptApplicationAnalysisSteps.test.ts b/src/domain/javascript/javascriptApplicationAnalysisSteps.test.ts index c96ccece7..a460de077 100644 --- a/src/domain/javascript/javascriptApplicationAnalysisSteps.test.ts +++ b/src/domain/javascript/javascriptApplicationAnalysisSteps.test.ts @@ -11,11 +11,13 @@ import { createJavaScriptApplicationGraph, createImmutableJavaScriptApplicationGraphSteps, isValidatedImmutableJavaScriptApplicationGraph, + sealTransferredJavaScriptApplicationGraphSteps, } from "./javascriptApplicationGraph.js"; import { createJavaScriptSemanticGraph, createImmutableJavaScriptSemanticGraphSteps, isValidatedImmutableJavaScriptSemanticGraph, + sealTransferredJavaScriptSemanticGraphSteps, } from "./javascriptSemanticGraph.js"; const complete = (steps: Iterator): Value => { @@ -265,3 +267,60 @@ describe("owned JavaScript result bindings", () => { ); }); }); + +describe("graphs transferred to their sealing steps", () => { + it("seal the same validated graphs as the copying factories", () => { + const input = example(); + const { graph_id: _applicationId, ...application } = input.graph; + const { graph_id: _semanticId, ...semantic } = input.semantic_graph; + const expectedApplication = createJavaScriptApplicationGraph(application); + const expectedSemantic = createJavaScriptSemanticGraph(semantic); + const sealedApplication = complete( + sealTransferredJavaScriptApplicationGraphSteps( + structuredClone(application), + ), + ); + const sealedSemantic = complete( + sealTransferredJavaScriptSemanticGraphSteps(structuredClone(semantic)), + ); + expect(sealedApplication).toEqual(expectedApplication); + expect(sealedSemantic).toEqual(expectedSemantic); + expect( + isValidatedImmutableJavaScriptApplicationGraph(sealedApplication), + ).toBe(true); + expect(isValidatedImmutableJavaScriptSemanticGraph(sealedSemantic)).toBe( + true, + ); + }); + + it("report the issues a whole-graph parse reports", () => { + const input = example(); + const { graph_id: _applicationId, ...application } = input.graph; + const { graph_id: _semanticId, ...semantic } = input.semantic_graph; + const node = semantic.nodes[0]; + const applicationNode = application.nodes[0]; + if (node === undefined || applicationNode === undefined) + throw new Error("Expected semantic and application nodes"); + const malformed = { + ...semantic, + nodes: [{ ...node, label: 7 }, ...semantic.nodes.slice(1)], + }; + expect( + issuesFrom(() => + complete(sealTransferredJavaScriptSemanticGraphSteps(malformed)), + ), + ).toEqual(issuesFrom(() => createJavaScriptSemanticGraph(malformed))); + const stale = { + ...application, + nodes: [ + { ...applicationNode, node_id: `jag_node_${"f".repeat(64)}` }, + ...application.nodes.slice(1), + ], + }; + expect( + issuesFrom(() => + complete(sealTransferredJavaScriptApplicationGraphSteps(stale)), + ), + ).toEqual(issuesFrom(() => createJavaScriptApplicationGraph(stale))); + }); +}); diff --git a/src/domain/javascript/javascriptApplicationGraph.ts b/src/domain/javascript/javascriptApplicationGraph.ts index e16aded75..ea84585f1 100644 --- a/src/domain/javascript/javascriptApplicationGraph.ts +++ b/src/domain/javascript/javascriptApplicationGraph.ts @@ -1,4 +1,9 @@ import { z } from "zod"; +import { + arrayContainerSchema, + parseElementSteps, +} from "../zodCollectionSteps.js"; +import { canonicalJsonDigestSteps } from "../canonicalJsonDigestSteps.js"; import { canonicalJson } from "../comparisonSemantics.js"; import { digestCanonicalValue } from "../canonicalDigest.js"; import { uniqueSorted } from "../canonicalOrdering.js"; @@ -93,11 +98,14 @@ type EdgeSemantic = Omit; const edgeId = (edge: EdgeSemantic): string => `jag_edge_${digestCanonicalValue(edge, "JavaScript Application Graph")}`; +/** The issue sink shared by schema refinement and stepwise checking. */ +type GraphIssueContext = Pick; + const sortedUniqueIssue = ( values: readonly string[], path: PropertyKey[], label: string, - context: z.RefinementCtx, + context: GraphIssueContext, ): void => { for (let index = 1; index < values.length; index += 1) { if ( @@ -116,7 +124,7 @@ const sortedUniqueIssue = ( const checkEvidenceNormalization = ( evidence: ApplicationGraphEvidence, path: PropertyKey[], - context: z.RefinementCtx, + context: GraphIssueContext, ): void => { sortedUniqueIssue( evidence.limitations, @@ -226,7 +234,7 @@ const observationMatchesIdentity = ( const checkNode = ( node: ApplicationNode, index: number, - context: z.RefinementCtx, + context: GraphIssueContext, ): void => { if (node.node_id !== nodeId(node)) context.addIssue({ @@ -306,8 +314,16 @@ type GraphRecord = z.infer; const checkGraphContent = ( graph: JavaScriptApplicationGraphInput, - context: z.RefinementCtx, + context: GraphIssueContext, ): void => { + const steps = checkGraphContentSteps(graph, context); + while (steps.next().done !== true); +}; + +function* checkGraphContentSteps( + graph: JavaScriptApplicationGraphInput, + context: GraphIssueContext, +): Generator { sortedUniqueIssue( graph.root_node_ids, ["root_node_ids"], @@ -353,9 +369,12 @@ const checkGraphContent = ( message: "Root identifier must name a graph node", path: ["root_node_ids"], }); - for (const [index, node] of graph.nodes.entries()) + for (const [index, node] of graph.nodes.entries()) { + if (index % 64 === 0) yield; checkNode(node, index, context); + } for (const [index, edge] of graph.edges.entries()) { + if (index % 64 === 0) yield; const { edge_id: identifier, ...semantic } = edge; if (identifier !== edgeId(semantic)) context.addIssue({ @@ -394,11 +413,11 @@ const checkGraphContent = ( context, ); } -}; +} const checkGraphInvariants = ( graph: GraphRecord, - context: z.RefinementCtx, + context: GraphIssueContext, ): void => { checkGraphContent(graph, context); const { graph_id: identifier, ...semantic } = graph; @@ -475,22 +494,9 @@ export const createJavaScriptApplicationEdge = ( export const createJavaScriptApplicationGraph = ( input: unknown, ): JavaScriptApplicationGraph => { - const parsed = javascriptApplicationGraphInputSchema.parse(input); - const semantic: JavaScriptApplicationGraphInput = { - ...parsed, - root_node_ids: uniqueSorted(parsed.root_node_ids), - nodes: [...parsed.nodes].sort((left, right) => - compareUnicodeCodePoints(left.node_id, right.node_id), - ), - edges: [...parsed.edges].sort((left, right) => - compareUnicodeCodePoints(left.edge_id, right.edge_id), - ), - coverage: { - ...parsed.coverage, - limits: normalizeLimits(parsed.coverage.limits), - }, - limitations: uniqueSorted(parsed.limitations), - }; + const semantic = normalizeGraphInput( + javascriptApplicationGraphInputSchema.parse(input), + ); const content = javascriptApplicationGraphContentSchema.parse(semantic); return { ...content, @@ -498,6 +504,24 @@ export const createJavaScriptApplicationGraph = ( }; }; +const normalizeGraphInput = ( + parsed: JavaScriptApplicationGraphInput, +): JavaScriptApplicationGraphInput => ({ + ...parsed, + root_node_ids: uniqueSorted(parsed.root_node_ids), + nodes: [...parsed.nodes].sort((left, right) => + compareUnicodeCodePoints(left.node_id, right.node_id), + ), + edges: [...parsed.edges].sort((left, right) => + compareUnicodeCodePoints(left.edge_id, right.edge_id), + ), + coverage: { + ...parsed.coverage, + limits: normalizeLimits(parsed.coverage.limits), + }, + limitations: uniqueSorted(parsed.limitations), +}); + const validatedImmutableApplicationGraphs = new WeakSet(); /** Clone and validate synchronously, then return steps to seal the owned graph. */ @@ -506,6 +530,65 @@ export const createImmutableJavaScriptApplicationGraphSteps = ( ): Generator => sealValidatedApplicationGraphSteps(createJavaScriptApplicationGraph(input)); +const applicationGraphShape = javascriptApplicationGraphInputSchema.shape; +const applicationGraphContainerSchema = + javascriptApplicationGraphInputSchema.extend({ + nodes: arrayContainerSchema(applicationGraphShape.nodes), + edges: arrayContainerSchema(applicationGraphShape.edges), + }); + +/** + * Seal a graph whose input the caller transfers: the caller neither retains nor + * mutates any part of it afterwards. Only the graph's own fields are validated + * synchronously; nodes and edges are validated, checked, hashed and frozen in + * steps, because doing so at once held the event loop for large applications. + */ +export const sealTransferredJavaScriptApplicationGraphSteps = ( + input: unknown, +): Generator => + parseAndSealApplicationGraphSteps( + applicationGraphContainerSchema.parse(input), + ); + +function* parseAndSealApplicationGraphSteps( + container: z.output, +): Generator { + const issues: z.core.$ZodIssue[] = []; + const parsed: JavaScriptApplicationGraphInput = { + ...container, + nodes: yield* parseElementSteps( + applicationGraphShape.nodes.element, + container.nodes, + ["nodes"], + issues, + ), + edges: yield* parseElementSteps( + applicationGraphShape.edges.element, + container.edges, + ["edges"], + issues, + ), + }; + if (issues.length > 0) throw new z.ZodError(issues); + const content = normalizeGraphInput(parsed); + let invalid = false; + yield* checkGraphContentSteps(content, { + addIssue: () => { + invalid = true; + }, + }); + // An invalid owned graph is a construction defect; report it with the exact + // issues the content schema produces. + if (invalid) { + javascriptApplicationGraphContentSchema.parse(content); + throw new TypeError("Application graph checks disagree with its schema"); + } + return yield* sealValidatedApplicationGraphSteps({ + ...content, + graph_id: `jag_${yield* canonicalJsonDigestSteps(content)}`, + }); +} + function* sealValidatedApplicationGraphSteps( graph: JavaScriptApplicationGraph, ): Generator { diff --git a/src/domain/javascript/javascriptSemanticAnalysis.member_mutations.test.ts b/src/domain/javascript/javascriptSemanticAnalysis.member_mutations.test.ts index bf7d35c47..11bf30d0b 100644 --- a/src/domain/javascript/javascriptSemanticAnalysis.member_mutations.test.ts +++ b/src/domain/javascript/javascriptSemanticAnalysis.member_mutations.test.ts @@ -628,7 +628,7 @@ describe("results of methods on an escaped receiver", () => { }); }); - it("expands a receiver's methods once rather than once per call (#1495)", () => { + it("keeps a shared result uncertain across a wide escaped receiver (#1495)", () => { const count = 2000; const methods = Array.from( { length: count }, @@ -638,11 +638,9 @@ describe("results of methods on an escaped receiver", () => { { length: count }, (_, index) => `box.m${index}().p${index} = ${index};`, ).join(" "); - const start = performance.now(); const value = resultValue( `const shared = { token: "TOKEN" }; const box = { ${methods} }; ${calls} return [shared.token];`, ); - expect(performance.now() - start).toBeLessThan(2000); expect(value).toMatchObject({ status: "array", items: [{ value: { status: "unknown" } }], diff --git a/src/domain/javascript/javascriptSemanticGraph.ts b/src/domain/javascript/javascriptSemanticGraph.ts index 9ab2dd485..6908af2a1 100644 --- a/src/domain/javascript/javascriptSemanticGraph.ts +++ b/src/domain/javascript/javascriptSemanticGraph.ts @@ -6,6 +6,10 @@ import { uniqueSorted } from "../canonicalOrdering.js"; import { compareUnicodeCodePoints } from "../unicodeCodePointOrder.js"; import { canonicalJsonDigestSteps } from "../canonicalJsonDigestSteps.js"; import { freezeOwnedJsonSnapshotSteps } from "../immutableJson.js"; +import { + arrayContainerSchema, + parseElementSteps, +} from "../zodCollectionSteps.js"; import type { ApplicationGraphEvidence, ApplicationGraphEvidenceContext, @@ -765,6 +769,15 @@ export const createJavaScriptSemanticGraph = ( const validatedImmutableSemanticGraphs = new WeakSet(); +const semanticGraphShape = javaScriptSemanticGraphInputSchema.shape; +const semanticGraphContainerSchema = javaScriptSemanticGraphInputSchema.extend({ + evidence_contexts: arrayContainerSchema(semanticGraphShape.evidence_contexts), + nodes: arrayContainerSchema(semanticGraphShape.nodes), + relations: arrayContainerSchema(semanticGraphShape.relations), + fingerprints: arrayContainerSchema(semanticGraphShape.fingerprints), + unknowns: arrayContainerSchema(semanticGraphShape.unknowns), +}); + /** Clone input synchronously; commit, check and seal the owned graph in steps. */ export const createImmutableJavaScriptSemanticGraphSteps = ( input: unknown, @@ -773,6 +786,58 @@ export const createImmutableJavaScriptSemanticGraphSteps = ( normalizeGraphInput(javaScriptSemanticGraphInputSchema.parse(input)), ); +/** + * Seal a graph whose input the caller transfers: the caller neither retains nor + * mutates any part of it afterwards. Only the graph's own fields are validated + * synchronously; collection elements are validated and cloned in steps, because + * a whole-graph parse of a large application held the event loop for seconds. + */ +export const sealTransferredJavaScriptSemanticGraphSteps = ( + input: unknown, +): Generator => + parseAndSealSemanticGraphSteps(semanticGraphContainerSchema.parse(input)); + +function* parseAndSealSemanticGraphSteps( + container: z.output, +): Generator { + const issues: z.core.$ZodIssue[] = []; + const parsed: JavaScriptSemanticGraphInput = { + ...container, + evidence_contexts: yield* parseElementSteps( + semanticGraphShape.evidence_contexts.element, + container.evidence_contexts, + ["evidence_contexts"], + issues, + ), + nodes: yield* parseElementSteps( + semanticGraphShape.nodes.element, + container.nodes, + ["nodes"], + issues, + ), + relations: yield* parseElementSteps( + semanticGraphShape.relations.element, + container.relations, + ["relations"], + issues, + ), + fingerprints: yield* parseElementSteps( + semanticGraphShape.fingerprints.element, + container.fingerprints, + ["fingerprints"], + issues, + ), + unknowns: yield* parseElementSteps( + semanticGraphShape.unknowns.element, + container.unknowns, + ["unknowns"], + issues, + ), + }; + if (issues.length > 0) throw new z.ZodError(issues); + return yield* validateAndSealSemanticGraphSteps(normalizeGraphInput(parsed)); +} + function* validateAndSealSemanticGraphSteps( semantic: JavaScriptSemanticGraphInput, ): Generator { diff --git a/src/domain/zodCollectionSteps.ts b/src/domain/zodCollectionSteps.ts new file mode 100644 index 000000000..a32e8c804 --- /dev/null +++ b/src/domain/zodCollectionSteps.ts @@ -0,0 +1,33 @@ +import { z } from "zod"; + +/** + * An array schema's own constraints, such as a minimum length, without its + * element schema. Pair it with {@link parseElementSteps} to validate a large + * collection in cooperative steps. + */ +export const arrayContainerSchema = ( + array: z.ZodArray, +): z.ZodType => array.clone({ ...array.def, element: z.unknown() }); + +/** Elements validated between yields, in the issues and paths a whole-array parse reports. */ +export function* parseElementSteps( + element: Element, + values: readonly unknown[], + path: readonly PropertyKey[], + issues: z.core.$ZodIssue[], +): Generator[]> { + const parsed: z.output[] = []; + for (const [index, value] of values.entries()) { + if (index % 64 === 0) yield; + const result = element.safeParse(value); + if (result.success) parsed.push(result.data); + else + issues.push( + ...result.error.issues.map((issue) => ({ + ...issue, + path: [...path, index, ...issue.path], + })), + ); + } + return parsed; +} diff --git a/tests/acceptance/applications/javascriptReturnedReferences.test.ts b/tests/acceptance/applications/javascriptReturnedReferences.test.ts index bd9a05c76..d9f4c324b 100644 --- a/tests/acceptance/applications/javascriptReturnedReferences.test.ts +++ b/tests/acceptance/applications/javascriptReturnedReferences.test.ts @@ -47,6 +47,10 @@ const writes = { constructor: "function Box() { return shared; } const t = new Box(); t.x = 2;", callback: "const get = () => shared; [0].forEach(() => { get().x = 2; });", + wideMethods: `const box = { ${Array.from( + { length: 64 }, + (_, index) => `m${index}() { return shared; }`, + ).join(", ")} }; box.m0().x = 2; box.m63().other = 3;`, }; const source = [ ...Object.entries(writes).map( @@ -61,6 +65,8 @@ const source = [ "export function unused(){ const shared = {x: 1}; const get = () => shared; get(); return shared.x; }", "export function nestedCallable(){ const shared = {x: 1}; const get = () => { const inner = () => shared; return {x: 0}; }; get().x = 2; return shared.x; }", "export function unrelated(){ const shared = {x: 1}; const other = {x: 9}; const get = () => shared; get().x = 2; return other.x; }", + "export function receiverDistinctScopes(){ const shared = {x: 1}; const box = {outer(){ return shared; }, inner(){ const shared = {x: 9}; return shared; }, noop(){}}; box.noop(); return {outer: shared.x, inner: box.inner().x}; }", + "export function receiverDistinctPaths(){ const parent = {left: {x: 1}, right: {x: 9}, keep: 7}; const box = {left(){ return parent.left; }, right(){ return parent.right; }, noop(){}}; box.noop(); return {left: parent.left.x, right: parent.right.x, keep: parent.keep}; }", ].join("\n"); const assertReturns = (fields: JavaScriptReturnFields): void => { @@ -91,6 +97,17 @@ const assertReturns = (fields: JavaScriptReturnFields): void => { expect(fields("unrelated")).toContainEqual( expect.objectContaining({ path: "", state: "literal", value: 9 }), ); + for (const path of ["/outer", "/inner"]) + expect(fields("receiverDistinctScopes")).toContainEqual( + expect.objectContaining({ path, state: "unknown" }), + ); + for (const path of ["/left", "/right"]) + expect(fields("receiverDistinctPaths")).toContainEqual( + expect.objectContaining({ path, state: "unknown" }), + ); + expect(fields("receiverDistinctPaths")).toContainEqual( + expect.objectContaining({ path: "/keep", state: "literal", value: 7 }), + ); }; cliTest( diff --git a/tests/acceptance/setup/hermesClientSetup.test.ts b/tests/acceptance/setup/hermesClientSetup.test.ts index bd65d735b..5a7baa333 100644 --- a/tests/acceptance/setup/hermesClientSetup.test.ts +++ b/tests/acceptance/setup/hermesClientSetup.test.ts @@ -22,9 +22,9 @@ cliTest( arguments: arguments_, cwd: home, environment: { - HOME: home, USERPROFILE: home, - HERMES_HOME: " ${HOME}/profile ", + REA_CLIENT_PROFILE_HOME: home, + HERMES_HOME: " ${REA_CLIENT_PROFILE_HOME}/profile ", }, timeoutMs: 20_000, }); @@ -96,3 +96,195 @@ cliTest( await expect(access(skill)).rejects.toMatchObject({ code: "ENOENT" }); }, ); + +for (const selection of ["default", "custom", "suffix", "explicit"] as const) { + cliTest( + `Hermes ${selection} sticky-profile selection preserves the root configuration`, + async ({ cli }) => { + const home = await createTestTempDirectory("rea-hermes-sticky-"); + const suffix = selection === "suffix" ? "-dev" : ""; + const native = + process.platform === "win32" + ? join(home, "AppData", "Local", `hermes${suffix}`) + : join(home, `.hermes${suffix}`); + const root = selection === "custom" ? join(home, "custom root") : native; + const profile = join( + root, + "profiles", + selection === "explicit" ? "other" : "coder", + ); + await mkdir(profile, { recursive: true }); + const original = + "# profile settings\nmodel: custom\nmcp_servers:\n other:\n command: other-server\n"; + const config = join(profile, "config.yaml"); + const rootConfig = join(root, "config.yaml"); + await writeFile(config, original); + await writeFile(rootConfig, "model: root-model\n"); + await writeFile(join(root, "active_profile"), "\uFEFFCoDeR\r\n"); + const run = (args: readonly string[]) => + cli.run({ + arguments: args, + cwd: home, + environment: { + USERPROFILE: home, + LOCALAPPDATA: join(home, "AppData", "Local"), + HERMES_DATA_DIR_SUFFIX: suffix, + HERMES_HOME: + selection === "explicit" + ? profile + : selection === "custom" + ? root + : undefined, + }, + timeoutMs: 20_000, + }); + const plan = await run([ + "setup", + "--client", + "hermes", + "--dry-run", + "--json", + ]); + expect(plan.exitCode).toBe(0); + expect(plan.json).toMatchObject({ + status: "planned", + plannedActions: expect.arrayContaining([ + expect.objectContaining({ target: config }), + expect.objectContaining({ + target: join(profile, "skills", "reverse-engineer-anything"), + }), + ]), + }); + expect( + (await run(["setup", "--client", "hermes", "--yes", "--json"])) + .exitCode, + ).toBe(0); + expect(parse(await readFile(config, "utf8"))).toMatchObject({ + mcp_servers: { + rea: { enabled: true }, + other: { command: "other-server" }, + }, + }); + expect(await readFile(`${config}.rea.backup`, "utf8")).toBe(original); + const skill = join( + profile, + "skills", + "reverse-engineer-anything", + "SKILL.md", + ); + expect(await readFile(skill, "utf8")).toContain( + "name: reverse-engineer-anything", + ); + if (selection === "custom") { + const doctor = await run([ + "doctor", + "--client", + "hermes", + "--skill", + "--json", + ]); + expect(doctor.exitCode).toBe(0); + expect(doctor.json).toMatchObject({ + healthy: true, + identity: { + skill: { state: "aligned" }, + registrations: expect.arrayContaining([ + expect.objectContaining({ + client: "hermes", + config_path: config, + state: "aligned", + }), + ]), + }, + }); + expect( + (await run(["setup", "--client", "hermes", "--yes", "--json"])).json, + ).toMatchObject({ appliedActions: [] }); + expect((await run(["uninstall", "--json"])).exitCode).toBe(0); + expect(parse(await readFile(config, "utf8"))).toEqual(parse(original)); + await expect(access(skill)).rejects.toMatchObject({ code: "ENOENT" }); + } + expect(await readFile(rootConfig, "utf8")).toBe("model: root-model\n"); + await expect(access(`${rootConfig}.rea.backup`)).rejects.toMatchObject({ + code: "ENOENT", + }); + await expect( + access(join(root, "skills", "reverse-engineer-anything")), + ).rejects.toMatchObject({ code: "ENOENT" }); + }, + ); +} +for (const name of ["missing", "deleted", "ghost", "../escape", "root"]) { + cliTest( + `Hermes rejects unusable sticky profile ${name} without writing a fallback home`, + async ({ cli }) => { + const home = await createTestTempDirectory("rea-hermes-invalid-profile-"); + const root = join(home, "hermes root"); + await mkdir(root); + await writeFile(join(root, "active_profile"), name); + if (name === "deleted" || name === "ghost") { + await mkdir(join(root, "profiles", name), { recursive: true }); + if (name === "deleted") { + await writeFile( + join(root, "profiles", name, "config.yaml"), + "model: custom\n", + ); + await mkdir(join(root, "profiles", ".deleted")); + await writeFile( + join(root, "profiles", ".deleted", name), + "deleted\n", + ); + } + } + const run = (args: readonly string[]) => + cli.run({ + arguments: args, + cwd: home, + environment: { USERPROFILE: home, HERMES_HOME: root }, + timeoutMs: 20_000, + }); + const rejected = await run([ + "setup", + "--client", + "hermes", + "--yes", + "--json", + ]); + expect(rejected.exitCode).not.toBe(0); + expect(rejected.json).toMatchObject({ + status: "needs_human", + plannedActions: [], + appliedActions: [], + remediation: expect.stringContaining("Hermes profile"), + }); + await expect(access(join(root, "config.yaml"))).rejects.toMatchObject({ + code: "ENOENT", + }); + await expect(access(join(root, "skills"))).rejects.toMatchObject({ + code: "ENOENT", + }); + if (name !== "missing") return; + const doctor = await run(["doctor", "--client", "hermes", "--json"]); + expect(doctor.exitCode).not.toBe(0); + expect(doctor.json).toMatchObject({ + identity: { + registrations: expect.arrayContaining([ + expect.objectContaining({ + client: "hermes", + state: "invalid", + remediation: expect.stringContaining("Hermes profile"), + }), + ]), + }, + }); + const unrelated = await run([ + "setup", + "--client", + "codex", + "--yes", + "--json", + ]); + expect(unrelated.exitCode).toBe(0); + }, + ); +} diff --git a/tests/boundary/cli/browserCli.test.ts b/tests/boundary/cli/browserCli.test.ts index bfd167a7e..9084e87a9 100644 --- a/tests/boundary/cli/browserCli.test.ts +++ b/tests/boundary/cli/browserCli.test.ts @@ -32,157 +32,156 @@ afterEach(async () => { await Promise.all(browsers.splice(0).map(async (browser) => browser.close())); }); -describe("browser CLI parity", () => { - it( - "returns the same Evidence discovery and inspection contracts", - async () => { - const browser = await startFakeCdpBrowser({ - sessionTimeline: "same_origin", - webMcpTools: true, - sensitiveShapes: true, - commandEvents: partialScriptEvents, - }); - browsers.push(browser); - const listed = await runCli( - ["list-browser-targets", browser.endpoint, "--json"], - process.env, - ); - expect(listed).toMatchObject({ - operation: "list_browser_targets", - provider: { id: "rea-cdp-browser" }, - normalized_result: { - targets: expect.arrayContaining([ - expect.objectContaining({ target_id: "allowed-page" }), - expect.objectContaining({ target_id: "disallowed-page" }), - ]), +it( + "browser CLI parity returns the same Evidence discovery and inspection contracts", + async () => { + const browser = await startFakeCdpBrowser({ + sessionTimeline: "same_origin", + webMcpTools: true, + sensitiveShapes: true, + commandEvents: partialScriptEvents, + eventsBeforeReply: ["Network.enable"], + }); + browsers.push(browser); + const listed = await runCli( + ["list-browser-targets", browser.endpoint, "--json"], + process.env, + ); + expect(listed).toMatchObject({ + operation: "list_browser_targets", + provider: { id: "rea-cdp-browser" }, + normalized_result: { + targets: expect.arrayContaining([ + expect.objectContaining({ target_id: "allowed-page" }), + expect.objectContaining({ target_id: "disallowed-page" }), + ]), + }, + }); + const inspected = await runCli( + [ + "inspect-web-page", + browser.endpoint, + "allowed-page", + "--observation-ms", + "0", + "--include-console-text", + "--include-json-body-shapes", + "--include-websocket-shapes", + "--json", + ], + process.env, + ); + expect(inspected).toMatchObject({ + operation: "inspect_web_page", + provider: { id: "rea-cdp-browser" }, + normalized_result: { + target: { target_id: "allowed-page" }, + scripts: { + items: [{ cdp_hash: null, length: null, is_module: null }], }, - }); - const inspected = await runCli( - [ - "inspect-web-page", - browser.endpoint, - "allowed-page", - "--observation-ms", - "0", - "--include-console-text", - "--include-json-body-shapes", - "--include-websocket-shapes", - "--json", - ], - process.env, - ); - expect(inspected).toMatchObject({ - operation: "inspect_web_page", - provider: { id: "rea-cdp-browser" }, - normalized_result: { - target: { target_id: "allowed-page" }, - scripts: { - items: [{ cdp_hash: null, length: null, is_module: null }], - }, - network: { - prior_activity_available: false, - requests: [ - expect.objectContaining({ - body_shapes: expect.objectContaining({ status: "included" }), - }), - ], - }, - }, - }); - const inspection = normalizedResult(inspected); - const compared = await runCli( - [ - "compare-web-captures", - JSON.stringify({ inspection }), - JSON.stringify({ inspection }), - "--json", - ], - process.env, - ); - expect(compared).toMatchObject({ - operation: "compare_web_captures", - normalized_result: { - overall_status: "unknown", - dimensions: { - network: { status: "unknown", total_changes: 0 }, - }, - }, - }); - const scenarioCapture = completeScenarioCapture(); - const scenarioCompared = await runCli( - [ - "compare-web-captures", - JSON.stringify(scenarioCapture), - JSON.stringify(scenarioCapture), - "--normalization-json", - '{"rules":[]}', - "--json", - ], - process.env, - ); - expect(scenarioCompared).toMatchObject({ - operation: "compare_web_captures", - normalized_result: { - comparison_kind: "browser_scenario", - overall_status: "unchanged", - alignment: { status: "aligned", aligned_steps: 2 }, - }, - }); - const intentionalPolicy = await runCli( - [ - "compare-web-captures", - JSON.stringify(scenarioCapture), - JSON.stringify(scenarioCapture), - "--normalization-json", - JSON.stringify({ - rules: [ - { - rule_id: "domain", - artifacts: ["url"], - match: "app.example.test", - replacement: "normalized.test", - }, - ], - }), - "--json", - ], - process.env, - ); - expect(intentionalPolicy).toMatchObject({ - normalized_result: { - normalization: { rules: [{ rule_id: "domain" }] }, - }, - }); - - const malformedPolicy = await runCli( - [ - "compare-web-captures", - JSON.stringify(scenarioCapture), - JSON.stringify(scenarioCapture), - "--normalization-json", - "{invalid-policy", - "--json", - ], - process.env, - ); - expect(malformedPolicy).toMatchObject({ - error: "Browser observation failed", - code: "invalid_request", - details: { - operation: "compare_web_captures", - issues: [ - { - path: ["normalization"], - reason: "invalid_format", - expected: "JSON", - }, + network: { + prior_activity_available: false, + requests: [ + expect.objectContaining({ + body_shapes: expect.objectContaining({ status: "included" }), + }), ], }, - }); - }, - INTEGRATION_TEST_TIMEOUT_MS, - ); -}); + }, + }); + const inspection = normalizedResult(inspected); + const compared = await runCli( + [ + "compare-web-captures", + JSON.stringify({ inspection }), + JSON.stringify({ inspection }), + "--json", + ], + process.env, + ); + expect(compared).toMatchObject({ + operation: "compare_web_captures", + normalized_result: { + overall_status: "unknown", + dimensions: { + network: { status: "unknown", total_changes: 0 }, + }, + }, + }); + const scenarioCapture = completeScenarioCapture(); + const scenarioCompared = await runCli( + [ + "compare-web-captures", + JSON.stringify(scenarioCapture), + JSON.stringify(scenarioCapture), + "--normalization-json", + '{"rules":[]}', + "--json", + ], + process.env, + ); + expect(scenarioCompared).toMatchObject({ + operation: "compare_web_captures", + normalized_result: { + comparison_kind: "browser_scenario", + overall_status: "unchanged", + alignment: { status: "aligned", aligned_steps: 2 }, + }, + }); + const intentionalPolicy = await runCli( + [ + "compare-web-captures", + JSON.stringify(scenarioCapture), + JSON.stringify(scenarioCapture), + "--normalization-json", + JSON.stringify({ + rules: [ + { + rule_id: "domain", + artifacts: ["url"], + match: "app.example.test", + replacement: "normalized.test", + }, + ], + }), + "--json", + ], + process.env, + ); + expect(intentionalPolicy).toMatchObject({ + normalized_result: { + normalization: { rules: [{ rule_id: "domain" }] }, + }, + }); + + const malformedPolicy = await runCli( + [ + "compare-web-captures", + JSON.stringify(scenarioCapture), + JSON.stringify(scenarioCapture), + "--normalization-json", + "{invalid-policy", + "--json", + ], + process.env, + ); + expect(malformedPolicy).toMatchObject({ + error: "Browser observation failed", + code: "invalid_request", + details: { + operation: "compare_web_captures", + issues: [ + { + path: ["normalization"], + reason: "invalid_format", + expected: "JSON", + }, + ], + }, + }); + }, + INTEGRATION_TEST_TIMEOUT_MS, +); describe("browser CLI capture parity", () => { it( diff --git a/tests/fixtures/fakeCdpBrowser.ts b/tests/fixtures/fakeCdpBrowser.ts index f9d2ddf08..c085b1806 100644 --- a/tests/fixtures/fakeCdpBrowser.ts +++ b/tests/fixtures/fakeCdpBrowser.ts @@ -196,22 +196,28 @@ const replyToCommand = ( ); return; } - socket.send( - JSON.stringify({ - id: command.id, - result, - }), - ); - if (options.malformedEventOnMethod === command.method) - socket.send("{not-json"); - if (options.malformedEventShapeOnMethod === command.method) - socket.send(JSON.stringify({ method: 42 })); + const reply = () => { + socket.send( + JSON.stringify({ + id: command.id, + result, + }), + ); + if (options.malformedEventOnMethod === command.method) + socket.send("{not-json"); + if (options.malformedEventShapeOnMethod === command.method) + socket.send(JSON.stringify({ method: 42 })); + }; + const eventsFirst = + options.eventsBeforeReply?.includes(command.method) === true; + if (!eventsFirst) reply(); const customEvents = options.commandEvents?.( command, `http://127.0.0.1:${String(port)}`, ); if (customEvents === undefined) emitEvents(socket, command, port, options); else for (const event of customEvents) socket.send(JSON.stringify(event)); + if (eventsFirst) reply(); if (options.closeAfterMethod === command.method) setImmediate(() => socket.close()); }; diff --git a/tests/fixtures/fakeCdpBrowserTypes.ts b/tests/fixtures/fakeCdpBrowserTypes.ts index 00d8807c5..54fc76563 100644 --- a/tests/fixtures/fakeCdpBrowserTypes.ts +++ b/tests/fixtures/fakeCdpBrowserTypes.ts @@ -28,6 +28,8 @@ export interface FakeCdpBrowser { } export interface FakeOptions { + /** Send these commands' producer events before acknowledging completion, as a wire barrier. */ + readonly eventsBeforeReply?: readonly string[]; /** Return a native-shaped command rejection through the actual wire parser. */ readonly commandError?: ( command: FakeCdpCommand, diff --git a/website/README.md b/website/README.md index 93faf5e59..298bc4b80 100644 --- a/website/README.md +++ b/website/README.md @@ -43,7 +43,7 @@ HTML files. No URL list needs to be maintained. - `public/first-investigation/index.html`: a guided Notes export investigation, from setup to a checked CSV prediction. - `public/showcase/index.html`: the case-study index. - `public/blog/index.html`: articles about reconstruction methods, ports, mods and reverse engineering. -- `public/blog/touhou-reconstruction/index.html`: a personal essay about agent-driven reconstruction, knowledge that compounds across TH08 and TH095, and the workflow's applicability to PC-98 TH04. +- `public/blog/touhou-reconstruction/index.html`: a personal essay about agent autonomy, oracle quality, knowledge carried between TH08 and TH095, PC-98 TH04, and the path from exact reconstruction to readable source and modern ports. - `public/showcase/aegis/index.html`: Aegis's Android login-code calculation, with an adjustable clock and reference checks. - `public/showcase/dx-ball/index.html`: sound-pan investigation and project status. - `public/showcase/notion/index.html`: Notion's Electron clipboard bridge and rich clipboard format. @@ -136,6 +136,10 @@ quoted README passage and milestone counting rules are in The completed text has normal search metadata and is included in the generated sitemap. +Its opening flywheel connects agent autonomy, reference-based oracles and +repository memory. The essay develops those ideas through the project examples. +Figure labels are selectable HTML; inline SVG supplies the feedback arrows. + Keep the copy direct and specific. Explain the task and the result before listing tool names. Setup commands and runtime requirements should match the released package. Keep the core learning path inside the site. Source, issue and evidence diff --git a/website/evidence/touhou-reconstruction-method.md b/website/evidence/touhou-reconstruction-method.md index 99f3ceb33..9d5b91565 100644 --- a/website/evidence/touhou-reconstruction-method.md +++ b/website/evidence/touhou-reconstruction-method.md @@ -173,3 +173,115 @@ observations, also confirmed by the author in this revision request. It is not attributed to a new automated website test or a fresh runtime replay. The 64-bit port is described as in development. The earlier four-product build and function-count milestones retain their historical scopes. + +## Autonomy, oracles and the compounding flywheel + +The author's requested revision emphasizes two principles: agents have broad +autonomy to investigate, and their hypotheses are tested by oracles with concrete +references and explicit acceptance conditions. These ideas are developed in the +Factory's agent-autonomy and semantic-reconstruction documents listed above. +An oracle result supports the property and inputs actually checked; the essay +retains human responsibility for project milestones and release decisions. + +The flywheel is an explanatory model of this relationship. Its passing branch +retains a checked result and its evidence; a mismatch returns to investigation. +Code, checks and recorded lessons improve the next starting point, with TH08, +TH095 and TH04 illustrating knowledge carried between projects. It does not +depict a measured performance ratio or give REA sole credit for reconstruction. + +The first inline reference to GensokyoClub's public README links to the author's +selected [Internet Archive capture](https://web.archive.org/web/20260909051514/https://github.com/GensokyoClub/th08#important-notice). +The pinned source caption and 23-word excerpt retain their original provenance. +The author approved this revision for publication on 10 October 2026. + +## TH08's floating-literal oracle failure + +The author recalled a floating-point comparison bug. The source history and +public GitHub record identify it as a reference-data verification gap, rather +than a rounding-tolerance problem. The original Japanese TH08 1.00d executable +uses double `128.0` in the power arm of `ItemManager::OnUpdate`'s item-attraction +condition. The reconstructed source used `0.0`. + +- [Issue #10](https://github.com/N0zoM1z0/th08/issues/10), opened on 2 September + 2026, records the downstream Switch-port report, the target instructions and + the constant bytes. Normal power is non-negative, so the erroneous power arm + was effectively unconditional after crossing the collection line. Focus and + shot-type exceptions were separate, correct arms of the condition. +- [PR #11](https://github.com/N0zoM1z0/th08/pull/11), merged on 2 September, + contains commit + [`7148a76b`](https://github.com/N0zoM1z0/th08/commit/7148a76ba224742677895c7160807a68897957d5). + It changes the threshold from `0.0` to `128.0`, fixes the manifest literal + identity, and adds source-symbol/declared-data/target-data checks for + explicitly recorded floating literals. +- [PR #13](https://github.com/N0zoM1z0/th08/pull/13), merged the same day, + contains commit + [`a393f400`](https://github.com/N0zoM1z0/th08/commit/a393f400786e4cc6cbb1000af0dad3276da12c78). + It audits 1,548 configured floating-literal references, corrects twelve more + stale references across five accepted functions, and makes verification + automatic for every 32-bit or 64-bit `__real@...` relocation. Its tests include + deliberately invalid literal declarations and representations. + +The older comparison replayed the target address into the instruction's +relocation field, then compared instruction bytes without checking the +floating-literal data at that address. Source `0.0` could therefore appear +exact against target `128.0`. The essay explains this mechanism in ordinary +language and keeps implementation details in the linked record. The count is +of references, not distinct values or new functions. + +The website work reexamined the historical diffs and retrieved the issue and +PR metadata with `gh`; it did not rerun the game's historical compiler or +runtime. The author supplies the first-person lesson about working as one +human with agents and about responsibility for oracle quality. + +## Readability and the learned phase order + +The semantic discussion is based on the author's requested explanation, +[TH08's semantic playbook](https://github.com/N0zoM1z0/th08/blob/d2a00f4acb4a13916e07a00bd13f714961714628/docs/SEMANTIC_PLAYBOOK.md), +and the Factory's +[semantic-reconstruction workflow](https://github.com/N0zoM1z0/touhou-reconstruction-factory/blob/717e1b8560783ed17a39fdf159d30aa16bb307a7/docs/semantic-reconstruction.md). +The workflow's latest public change was checked using `gh` on 10 October. + +The recommended order is an exact baseline, a working historical-platform +product, semantic reconstruction with both validation paths, then portable +products. The original target/compiler comparisons and the historical +build/runtime checks answer different questions. Both are preserved during +semantic work. Names, ownership and protocol interpretations also need +producer/consumer evidence; a passing check does not establish their English +meaning. Modern ports compare behavior with the established historical build +without requiring the new architecture's machine code to match the old one. + +This is a method learned from the projects. TH08's earlier portable releases +preceded some later historical-platform audits. The essay states that history +explicitly rather than presenting the current order as the original chronology. +The game-RE background and the assembly/raw-offset aside are author-provided +perspective. Transfer to other fields is a proposal to carry the principles +while choosing references appropriate to the new domain. + +## Industrial analogy: instruments develop with machines + +The [Science Museum Group's steam-engine indicator record](https://collection.sciencemuseumgroup.org.uk/objects/co51439/watts-steam-engine-indicator), +read on 10 October 2026, describes Boulton and Watt's introduction of the +instrument in 1796 to assist with valve adjustment. It also explains how adding +a pencil and moving paper made cylinder pressure through the stroke recordable. +The essay paraphrases this description and links it directly. It does not use +the record's images, quote its text, or date the displayed object or pressure +diagram to 1796. + +The connection to reconstruction tools is the author's analogy: rapid +execution needs measurement, and the instruments themselves require improvement. +The claim that this is an early industrial stage is the essay's interpretation +of the project experience. The historical example supports the measurement +comparison rather than a measured prediction about AI productivity. + +## Prose revision + +The later prose pass reduces repeated enumerations and develops each lesson +through its project example. It preserves the milestone figures, quotation, +source links and four-stage method. The two author-selected emphasized sentences +retain their wording. + +The opening takeaway now uses five bullets: agent autonomy, repository/Git +memory, evidence and explicit unknowns, testing the oracle, and the early stage +of the industrial shift. Each has a direct lead and a short explanation. The +historical-build/semantic/port sequence remains in the body rather than becoming +another takeaway item. diff --git a/website/figures.md b/website/figures.md index 534d8ac24..73fc70c42 100644 --- a/website/figures.md +++ b/website/figures.md @@ -96,9 +96,25 @@ formula, default settings, slider outputs and reference cases consistent. ## Touhou reconstruction essay -The essay opens with a takeaway and develops its argument in prose. It has no -opening flow figure. Project dates and the attributed README excerpt support -the narrative; detailed milestone references are available in a native +The essay opens with a takeaway and a flywheel explaining how experience +compounds. The figure uses selectable HTML labels and inline SVG connectors: +an autonomous agent proposes an experiment, an oracle tests it against a +reference, and code, evidence, checks and lessons remain in the repository. +A dashed mismatch branch returns to the agent for revision. The repository +supplies a better starting point for the next investigation; TH08 → TH095 → +TH04 illustrates knowledge carried between projects. Human direction names +the goal and acceptance criteria above the cycle. + +The caption connects reuse with improving the checks themselves. The TH08 +floating-literal case in the essay explains why a passing result can need +reexamination when an oracle's blind spot is discovered. Preserve that link +between the figure's ideal loop and the infrastructure lessons in the prose. + +The shared stylesheet positions the labels and gives narrow screens separate +connector geometry. Keep the arrows clear of the text and retain the same +meaning at every width. This is an explanatory model, with no measured speed +or productivity multiplier. Project dates and the attributed README excerpt +support the narrative; detailed milestone references are available in a native disclosure. Their provenance is documented in [evidence/touhou-reconstruction-method.md](evidence/touhou-reconstruction-method.md). diff --git a/website/public/assets/styles.css b/website/public/assets/styles.css index 542baf892..fe8539d0c 100644 --- a/website/public/assets/styles.css +++ b/website/public/assets/styles.css @@ -692,6 +692,178 @@ pre code { .blog-takeaway p:last-child { margin-bottom: 0; } +.blog-takeaway ul { + margin: 0; + padding-left: 1.15rem; +} +.blog-takeaway li + li { + margin-top: 0.75rem; +} +.reconstruction-flywheel { + max-width: 880px; + margin: 0 auto 4rem; + padding-block: 1.5rem 1.75rem; + border-block: 1px solid var(--line); +} +.flywheel-heading { + text-align: center; +} +.flywheel-heading .eyebrow { + margin-bottom: 0.6rem; + color: var(--accent); +} +.flywheel-heading > p:last-child { + color: var(--muted); + font-size: 0.875rem; +} +.flywheel-diagram { + position: relative; + max-width: 760px; + height: 440px; + margin-inline: auto; +} +.flywheel-arrows { + position: absolute; + inset: 0; + width: 100%; + height: 100%; +} +.flywheel-path-mobile { + display: none; +} +.flywheel-node { + position: absolute; + width: 30%; + text-align: center; + transform: translate(-50%, -50%); +} +.flywheel-node p { + margin: 0.6rem 0 0; + color: var(--muted); + font-size: 0.875rem; + line-height: 1.5; +} +.flywheel-node .flywheel-kicker { + margin: 0 0 0.4rem; + color: var(--accent); + font-size: 0.6875rem; + font-weight: 600; + letter-spacing: 0.09em; + text-transform: uppercase; +} +.flywheel-node h3 { + margin: 0; + font-size: 1.5rem; + line-height: 1.2; +} +.flywheel-agent { + top: 15%; + left: 50%; + width: 38%; +} +.flywheel-oracle { + top: 58%; + left: 79%; +} +.flywheel-memory { + top: 58%; + left: 21%; +} +.flywheel-retry, +.flywheel-center, +.flywheel-reuse, +.flywheel-pass { + position: absolute; + margin: 0; + padding: 0.25rem; + background: var(--page); + color: var(--muted); + font-size: 0.75rem; + line-height: 1.5; + text-align: center; + transform: translate(-50%, -50%); +} +.flywheel-retry { + top: 36%; + left: 68%; + width: 23%; +} +.flywheel-center { + top: 57%; + left: 50%; + width: 20%; + color: var(--accent); + font-size: 0.9375rem; +} +.flywheel-reuse { + top: 33%; + left: 23%; + color: var(--accent); +} +.flywheel-pass { + top: 94%; + left: 50%; + width: 75%; + color: var(--accent); + font-size: 0.8125rem; +} +.flywheel-projects { + display: flex; + justify-content: center; + align-items: center; + gap: 1.4rem; + margin-top: 0.3rem; +} +.flywheel-projects a { + font-size: 0.9375rem; + font-weight: 600; +} +.flywheel-projects span { + color: var(--muted); +} +.reconstruction-flywheel figcaption { + margin: 1rem auto 0; + max-width: 600px; + color: var(--muted); + font-size: 0.875rem; + text-align: center; +} +@media (max-width: 640px) { + .reconstruction-flywheel { + margin-bottom: 2.75rem; + } + .flywheel-diagram { + height: 405px; + } + .flywheel-agent { + width: 52%; + } + .flywheel-path-desktop { + display: none; + } + .flywheel-path-mobile { + display: initial; + } + .flywheel-node h3 { + font-size: 1.125rem; + } + .flywheel-node p { + font-size: 0.75rem; + } + .flywheel-node .flywheel-kicker { + font-size: 0.6rem; + letter-spacing: 0.05em; + } + .flywheel-center { + font-size: 0.75rem; + } + .flywheel-retry { + font-size: 0.625rem; + } + .flywheel-projects { + gap: 1rem; + } +} .blog-article blockquote { margin: 1.6rem 0; padding-left: 1.25rem; diff --git a/website/public/blog/touhou-reconstruction/index.html b/website/public/blog/touhou-reconstruction/index.html index 2afa3f1d9..10201ec25 100644 --- a/website/public/blog/touhou-reconstruction/index.html +++ b/website/public/blog/touhou-reconstruction/index.html @@ -147,21 +147,166 @@

By N0zoM1z0 · 10 October 2026

Takeaway

-

- Agent-driven reverse engineering changes how the work can - accumulate. - An investigation can become a checked result, a failed experiment - can become a reusable test, and one project's experience can become - the next project's starting point. -

-

- Reconstructing TH08, TH095 and TH04 convinced me that RE is entering - its industrial age. -

+
    +
  • + Give agents autonomy over the investigation. Set + a clear goal and acceptance criteria, then let the agent decide + what to try next. +
  • +
  • + Use the repository and Git as working memory. + Keep the reasoning with the code. Git checkpoints let the next + session continue after the conversation ends. +
  • +
  • + Let evidence decide what you claim. A confident + explanation still needs testing. “Unknown” is a valid answer when + the evidence is incomplete. +
  • +
  • + Test the oracle too. Try cases it should reject. + If it misses a bug, revisit the earlier results that depended on + it. +
  • +
  • + We are in the early days of RE's industrial revolution. + The methods are still taking shape. There is plenty left to + explore. +
  • +
+
+
+

How experience compounds

+

Human direction · Goal and acceptance criteria

+
+
+ +
+

Autonomy

+

Agent

+

Choose an experiment.
Propose a hypothesis.

+
+
+

Verification

+

Oracle

+

Test against a
concrete reference.

+
+
+

Repository

+

Working memory

+

Code, evidence,
checks and lessons.

+
+

+ Mismatch
Revise the hypothesis +

+

+ A better
starting point +

+

Reuse

+

+ Pass · Keep the result and its evidence +

+
+
+ TH08TH095TH04 +
+
+ Each project leaves the next with a better starting point. That + includes fixing the checks when we discover a gap. +
+