Reverse engineer anything with agents, from app behavior down to native binaries.
TypeScript agent-skillsai-agentsbinary-analysisclaude-codeclicodexcordisdecompilerdeveloper-toolsdshdsh-pluginghidrahopperllmmcpmodel-context-protocolreareverse-engineeringstatic-analysistouhou
refactor(process): extract capture journal recorder
morlutoa32b988a35a3
diff --git a/src/application/ProcessCaptureJournal.ts b/src/application/ProcessCaptureJournal.tsnew file mode 100644index 000000000..8de674232--- /dev/null+++ b/src/application/ProcessCaptureJournal.ts@@ -0,0 +1,89 @@+import type { IPty } from "@lydell/node-pty";++import type {+ InteractionEvent,+ ProcessCaptureEventJournalEntry,+ ProcessScenario,+ RecordProcessCaptureEvent,+} from "../domain/processCapture.js";+import { normalizeProcessText } from "./ProcessNormalization.js";+import type { TerminalRenderer } from "./TerminalRenderer.js";++/** Mutable observation-order ledger whose writer is shared by capture producers. */+export interface ProcessCaptureJournal {+ readonly entries: readonly ProcessCaptureEventJournalEntry[];+ readonly recordEvent: RecordProcessCaptureEvent;+}++/** Create one monotonic event journal for a process capture. */+export const createProcessCaptureJournal = (): ProcessCaptureJournal => {+ const entries: ProcessCaptureEventJournalEntry[] = [];+ return {+ entries,+ recordEvent: (collection, index) => {+ entries.push({ capture_order: entries.length, collection, index });+ },+ };+};++interface ScenarioInteractionOptions {+ readonly scenario: ProcessScenario;+ readonly getTerminal: () => IPty | undefined;+ readonly timers: Set<NodeJS.Timeout>;+ readonly interactions: InteractionEvent[];+ readonly renderer: TerminalRenderer;+ readonly started: number;+ readonly dispatchedEventIndexes: Set<number>;+ readonly recordEvent: RecordProcessCaptureEvent;+}++/** Schedule scenario interactions and record each observed dispatch atomically. */+export const scheduleScenarioInteractions = (+ options: ScenarioInteractionOptions,+): void => {+ const { scenario, getTerminal, timers, interactions, renderer, started } =+ options;+ for (const [eventIndex, event] of scenario.events.entries()) {+ const timer = setTimeout(() => {+ options.dispatchedEventIndexes.add(eventIndex);+ const terminal = getTerminal();+ const dispatchedAt = Math.max(0, Date.now() - started);+ let outcome: InteractionEvent["outcome"] = "dispatched";+ if (terminal === undefined) outcome = "target_exited";+ else {+ try {+ if (event.type === "input") terminal.write(event.data);+ else if (event.type === "resize") {+ terminal.resize(event.columns, event.rows);+ renderer.resize(event.columns, event.rows, dispatchedAt);+ } else terminal.kill(event.signal);+ } catch {+ outcome = "failed";+ }+ }+ const sequence = interactions.length;+ interactions.push({+ sequence,+ scheduled_at_ms: event.at_ms,+ dispatched_at_ms: dispatchedAt,+ type: event.type,+ data:+ event.type === "input"+ ? event.sensitive+ ? `<redacted-input:${String(Buffer.byteLength(event.data))}-bytes>`+ : normalizeProcessText(+ event.data,+ scenario,+ "<no-temporary-root>",+ -1,+ )+ : event.type === "resize"+ ? `${String(event.columns)}x${String(event.rows)}`+ : event.signal,+ outcome,+ });+ options.recordEvent("interaction_events", sequence);+ }, event.at_ms);+ timers.add(timer);+ }+};diff --git a/src/application/ProcessHarness.ts b/src/application/ProcessHarness.tsindex 098adcaa8..e67c4ed9b 100644--- a/src/application/ProcessHarness.ts+++ b/src/application/ProcessHarness.ts@@ -39,6 +39,10 @@ import {resolveProcessResult,} from "./ProcessCaptureLifecycle.js";import { assertNotCancelled } from "./ProcessCaptureAuthority.js";+import {+ createProcessCaptureJournal,+ scheduleScenarioInteractions,+} from "./ProcessCaptureJournal.js";export { probeProcessCaptureCapability } from "./ProcessCaptureCapability.js";@@ -77,65 +81,6 @@ const makeEnvironment = (return environment;};-interface ScenarioEventOptions {- readonly scenario: ProcessScenario;- readonly getTerminal: () => IPty | undefined;- readonly timers: Set<NodeJS.Timeout>;- readonly interactions: InteractionEvent[];- readonly renderer: TerminalRenderer;- readonly started: number;- readonly dispatchedEventIndexes: Set<number>;- readonly recordEvent: RecordProcessCaptureEvent;-}--const scheduleScenarioEvents = (options: ScenarioEventOptions): void => {- const { scenario, getTerminal, timers, interactions, renderer, started } =- options;- for (const [eventIndex, event] of scenario.events.entries()) {- const timer = setTimeout(() => {- options.dispatchedEventIndexes.add(eventIndex);- const terminal = getTerminal();- const dispatchedAt = Math.max(0, Date.now() - started);- let outcome: InteractionEvent["outcome"] = "dispatched";- if (terminal === undefined) outcome = "target_exited";- else {- try {- if (event.type === "input") terminal.write(event.data);- else if (event.type === "resize") {- terminal.resize(event.columns, event.rows);- renderer.resize(event.columns, event.rows, dispatchedAt);- } else terminal.kill(event.signal);- } catch {- outcome = "failed";- }- }- const sequence = interactions.length;- interactions.push({- sequence,- scheduled_at_ms: event.at_ms,- dispatched_at_ms: dispatchedAt,- type: event.type,- data:- event.type === "input"- ? event.sensitive- ? `<redacted-input:${String(Buffer.byteLength(event.data))}-bytes>`- : normalizeProcessText(- event.data,- scenario,- "<no-temporary-root>",- -1,- )- : event.type === "resize"- ? `${String(event.columns)}x${String(event.rows)}`- : event.signal,- outcome,- });- options.recordEvent("interaction_events", sequence);- }, event.at_ms);- timers.add(timer);- }-};-interface StartedCaptureRuntime {readonly replay: LoopbackReplay;readonly renderer: TerminalRenderer;@@ -248,7 +193,7 @@ const startCaptureRuntime = async (options: {renderer,checkpoints,});- scheduleScenarioEvents({+ scheduleScenarioInteractions({...options,getTerminal: () => terminal,renderer,@@ -443,14 +388,7 @@ const runProcessScenario = async ();const frames: TerminalFrame[] = [];const samples: ProcessSample[] = [];- const eventJournal: ProcessCaptureEventJournalEntry[] = [];- const recordEvent: RecordProcessCaptureEvent = (collection, index) => {- eventJournal.push({- capture_order: eventJournal.length,- collection,- index,- });- };+ const { entries: eventJournal, recordEvent } = createProcessCaptureJournal();let runtime: StartedCaptureRuntime | undefined;const timers = new Set<NodeJS.Timeout>();let capture: ProcessCapture | undefined;diff --git a/tests/processCapture.test.ts b/tests/processCapture.test.tsindex ad25d6176..f67eaa5de 100644--- a/tests/processCapture.test.ts+++ b/tests/processCapture.test.ts@@ -20,6 +20,7 @@ import {type ProcessPreparationHost,} from "../src/application/ProcessCaptureLifecycle.js";import { ProcessCheckpoints } from "../src/application/ProcessCheckpoints.js";+import { createProcessCaptureJournal } from "../src/application/ProcessCaptureJournal.js";import { normalizeProcessSamples } from "../src/application/ProcessNormalization.js";import {isInitializedPtyRoot,@@ -491,6 +492,14 @@ describe("process capture domain", () => {});it("accepts old captures without a journal and validates complete journals", () => {+ const journal = createProcessCaptureJournal();+ journal.recordEvent("lifecycle", 0);+ journal.recordEvent("lifecycle", 1);+ expect(journal.entries).toEqual([+ { capture_order: 0, collection: "lifecycle", index: 0 },+ { capture_order: 1, collection: "lifecycle", index: 1 },+ ]);+const capture = emptyCapture();const { event_journal: _eventJournal, ...oldCapture } = capture;expect(parseProcessCapture(oldCapture).event_journal).toEqual([]);