import { appendFileSync, openSync, closeSync, fsyncSync, mkdirSync, fchmodSync, constants, existsSync, readFileSync, writeFileSync, renameSync } from 'node:fs'; import { dirname, join } from 'node:path'; import { createHash } from 'node:crypto'; // Future-only lossless audit. Load AFTER the unchanged collab-google.js hook. // Never records headers/auth and never changes a payload, message, tool, or model. export default function (pi) { const path = process.env.CI_PI_PROVIDER_PRIVATE_AUDIT; if (!path) throw new Error('Explicit private provider audit path required'); mkdirSync(dirname(path), { recursive: true, mode: 0o700 }); const record = value => { const fd = openSync(path, constants.O_CREAT | constants.O_APPEND | constants.O_WRONLY | constants.O_NOFOLLOW, 0o600); try { fchmodSync(fd, 0o600); appendFileSync(fd, JSON.stringify(value) + '\n'); fsyncSync(fd); } finally { closeSync(fd); } }; let completedTurns = Number(process.env.CI_PI_RESTORED_COMPLETED_TURNS || '0'); if (!Number.isSafeInteger(completedTurns) || completedTurns < 0) throw new Error('Invalid restored turn counter'); let filesystemCheckpointRequired = false; let observedToolCalls = 0; const writeState = ctx => { const state = { schema_version: 1, completed_model_turns: completedTurns, observed_tool_calls: observedToolCalls, session_file: ctx.sessionManager.getSessionFile(), leaf_entry_id: ctx.sessionManager.getLeafId(), agent_filesystem_checkpoint_required: filesystemCheckpointRequired, filesystem_state: filesystemCheckpointRequired ? 'host_checkpoint_or_independent_no-mutation-proof_required' : 'no_mutating_or_unknown_tools_observed', provider_audit_file: path, model_or_private_referee_feedback_added: false }; const target = join(dirname(path), 'pi-restoration-state.json'); const temporary = `${target}.${process.pid}.tmp`; writeFileSync(temporary, JSON.stringify(state) + '\n', { mode: 0o600 }); renameSync(temporary, target); }; pi.on('tool_call', (event, ctx) => { observedToolCalls++; // General bash is deliberately NOT assumed read-only from a command prefix. // The host must capture owned main-container files or independently prove // the concrete commands caused no persistent mutation. No credentials/config // files are copied by this in-agent hook. if (!['read', 'grep', 'find', 'ls'].includes(event.toolName)) filesystemCheckpointRequired = true; record({ event: 'local_tool_call', tool_name: event.toolName, tool_call_id: event.toolCallId, input: event.input, agent_filesystem_checkpoint_required: filesystemCheckpointRequired }); writeState(ctx); }); pi.on('turn_end', (_event, ctx) => { completedTurns++; writeState(ctx); }); pi.on('before_provider_request', event => { record({ event: 'provider_request', payload: event.payload }); return event.payload; }); pi.on('provider_stream_event', event => { record({ event: 'provider_stream_event', api: event.api, model: event.model, data: event.data }); }); pi.on('agent_end', (_event, ctx) => { writeState(ctx); const source = join(process.env.PI_CODING_AGENT_DIR || '/tmp/harbor-pi-agent', 'google-thought-signatures.json'); if (!existsSync(source)) { record({ event: 'signature_snapshot', source_file_present: false }); return; } const fd = openSync(source, constants.O_RDONLY | constants.O_NOFOLLOW); let bytes; try { bytes = readFileSync(fd); } finally { closeSync(fd); } let values; try { values = JSON.parse(bytes); } catch { throw new Error('Private signature snapshot has invalid JSON'); } if (!Array.isArray(values) || values.some(v => !Array.isArray(v) || v.length !== 2 || typeof v[0] !== 'string' || typeof v[1]?.google?.thought_signature !== 'string')) throw new Error('Private signature snapshot has invalid schema'); const target = join(dirname(path), 'google-thought-signatures.json'); const temporary = `${target}.${process.pid}.tmp`; writeFileSync(temporary, bytes, { mode: 0o600 }); renameSync(temporary, target); record({ event: 'signature_snapshot', source_file_present: true, sha256: createHash('sha256').update(bytes).digest('hex') }); }); }