#!/usr/bin/env node import assert from "node:assert/strict"; import crypto from "node:crypto"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { loadAdapterIdentity } from "../skills/dirtyloops/adapters/adapter-identity.mjs"; import { createActivationRequirement } from "../skills/dirtyloops/adapters/pi/activation.mjs"; import { registerDirtyloopsPiAdapter } from "../skills/dirtyloops/adapters/pi/index.ts"; import { GENERATED_CONTRACT_PATHS, GENERATED_REMOVAL_PATHS, generationIdFromHashes, } from "../skills/dirtyloops/scripts/generated-contract.mjs"; const root = fs.mkdtempSync(path.join(os.tmpdir(), "dirtyloops-pi-integration-")); const docRoot = path.join(root, "docs", "implementation", "loop"); const skillRoot = path.resolve("skills/dirtyloops"); const artifactContents = Object.fromEntries(GENERATED_CONTRACT_PATHS.map((relative) => [ relative, GENERATED_REMOVAL_PATHS.includes(relative) ? null : `integration-artifact:${relative}\n`, ])); const artifacts = Object.fromEntries(Object.entries(artifactContents).map(([relative, value]) => [ relative, value === null ? null : crypto.createHash("sha256").update(value).digest("hex"), ])); const generation = { schema_version: 1, operation: "install-generated-contract", artifacts, metadata: { target: "pi", adapter_contract: "dirtyloops-harness/1", adapter_identity: loadAdapterIdentity(skillRoot, "pi"), execution_readiness: "ready", }, generation_id: "", }; generation.generation_id = generationIdFromHashes(generation.artifacts, generation.metadata); function writeRequirement() { for (const [relative, value] of Object.entries(artifactContents)) { const file = path.join(docRoot, relative); if (value === null) fs.rmSync(file, { force: true }); else { fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync(file, value); } } fs.writeFileSync(path.join(docRoot, "runtime/generation.json"), `${JSON.stringify(generation, null, 2)}\n`); const activation = createActivationRequirement({ operation: "convert-harness", generation, sourceCoordinatorId: "source-coordinator", }); fs.writeFileSync(path.join(docRoot, "runtime/activation.json"), `${JSON.stringify(activation, null, 2)}\n`); } const tools = new Map(); const lifecycle = new Map(); const eventSubscribers = new Map void>>(); const branch: any[] = []; const visible: any[] = []; let sendMode: "success" | "failure" = "success"; const events = { on(name: string, callback: (payload: any) => void) { const callbacks = eventSubscribers.get(name) ?? new Set(); callbacks.add(callback); eventSubscribers.set(name, callbacks); return () => callbacks.delete(callback); }, emit(name: string, payload: any) { if (name === "subagents:rpc:v1:request") { queueMicrotask(() => events.emit(`subagents:rpc:v1:reply:${payload.requestId}`, { success: false, error: { code: "unavailable", message: "optional backend unavailable in integration fixture" }, })); } for (const callback of eventSubscribers.get(name) ?? []) callback(payload); }, }; const pi: any = { events, registerTool(tool: any) { tools.set(tool.name, tool); }, on(event: string, handler: any) { lifecycle.set(event, handler); }, appendEntry(customType: string, data: unknown) { branch.push({ type: "custom", customType, data: structuredClone(data) }); }, sendMessage(message: any) { if (sendMode === "failure") throw new Error("injected visible emission failure"); visible.push(structuredClone(message)); }, getThinkingLevel() { return "low"; }, }; let sessionSequence = 0; class FakeSessionManager { id = `child-session-${++sessionSequence}`; static create() { return new FakeSessionManager(); } static inMemory() { return new FakeSessionManager(); } appendSessionInfo() {} getSessionId() { return this.id; } getSessionFile() { return path.join(root, `${this.id}.jsonl`); } } class FakeResourceLoader { async reload() { if (loaderMode === "failure") throw new Error("injected resource loader failure"); } } let createMode: "success" | "failure" = "success"; let loaderMode: "success" | "failure" = "success"; let promptMode: "success" | "pending" = "success"; let clock = 1_000_000; const dependencies: any = { SessionManager: FakeSessionManager, DefaultResourceLoader: FakeResourceLoader, getAgentDir: () => root, now: () => clock, recoveryMinSilenceMs: 1_000, recoveryCooldownMs: 500, renewalCheckout: () => ({ branch: "lavender/recovery", head: "a".repeat(40), dirty: false, }), createAgentSession: async () => { if (createMode === "failure") throw new Error("injected createAgentSession failure"); const state: any = { messages: [], errorMessage: undefined }; const session = { state, async prompt(mission: string) { if (promptMode === "pending") await new Promise(() => undefined); const expected = mission.match(/^Return exactly (.+) and nothing else\.$/)?.[1] ?? "ordinary done"; state.messages.push({ role: "assistant", content: [{ type: "text", text: expected }] }); }, async steer() {}, async abort() {}, subscribe() { return () => undefined; }, dispose() {}, }; return { session }; }, }; let coordinatorId = "fresh-coordinator"; const context = ( model: any = { provider: "fake", id: "fake-model" }, sessionId = coordinatorId, ) => ({ sessionManager: { getSessionId: () => sessionId, getSessionFile: () => path.join(root, "parent.jsonl"), getBranch: () => branch, }, modelRegistry: { authStorage: {}, find: () => model }, model, getContextUsage: () => ({ tokens: 100, contextWindow: 1_000, percent: 10 }), }); const execute = (tool: any, params: any, ctx = context()) => tool.execute("call", params, new AbortController().signal, () => undefined, ctx); try { writeRequirement(); registerDirtyloopsPiAdapter(pi, dependencies); let runtime = tools.get("dirtyloops_runtime"); let child = tools.get("dirtyloops_child"); const bypass = await execute(child, { action: "launch", kind: "session", backend: "vanilla", mutation: "read-only", mission: "ordinary", description: "ordinary", cwd: root, }); assert.match(bypass.content[0].text, /doc_root/); let activation = await execute(runtime, { action: "activate", doc_root: docRoot }); let launch = activation.details.activation.launch; let failed = await execute(child, { ...launch, cwd: path.join(root, "missing"), doc_root: docRoot }); assert.match(failed.content[0].text, /existing directory/); assert.equal(JSON.parse(fs.readFileSync(path.join(docRoot, "runtime/activation.json"))).failure.boundary, "launch"); writeRequirement(); activation = await execute(runtime, { action: "activate", doc_root: docRoot }); launch = activation.details.activation.launch; failed = await execute(child, { ...launch, cwd: root, doc_root: docRoot }, context(null)); assert.match(failed.content[0].text, /no Pi model/); assert.equal(JSON.parse(fs.readFileSync(path.join(docRoot, "runtime/activation.json"))).failure.boundary, "launch"); writeRequirement(); activation = await execute(runtime, { action: "activate", doc_root: docRoot }); launch = activation.details.activation.launch; loaderMode = "failure"; failed = await execute(child, { ...launch, cwd: root, doc_root: docRoot }); assert.match(failed.content[0].text, /injected resource loader failure/); assert.equal(JSON.parse(fs.readFileSync(path.join(docRoot, "runtime/activation.json"))).failure.boundary, "launch"); writeRequirement(); activation = await execute(runtime, { action: "activate", doc_root: docRoot }); launch = activation.details.activation.launch; loaderMode = "success"; createMode = "failure"; failed = await execute(child, { ...launch, cwd: root, doc_root: docRoot }); assert.match(failed.content[0].text, /injected createAgentSession failure/); assert.equal(JSON.parse(fs.readFileSync(path.join(docRoot, "runtime/activation.json"))).failure.boundary, "launch"); writeRequirement(); activation = await execute(runtime, { action: "activate", doc_root: docRoot }); launch = activation.details.activation.launch; createMode = "success"; const launched = await execute(child, { ...launch, cwd: root, doc_root: docRoot }); await new Promise((resolve) => setTimeout(resolve, 0)); const completion = visible.at(-1).details; assert.equal(completion.handle, launched.details.handle); assert.equal(completion.backend, "vanilla"); assert.equal(completion.lifecycle_source, "vanilla-session"); assert.equal(completion.doc_root, docRoot); await lifecycle.get("session_shutdown")(); registerDirtyloopsPiAdapter(pi, dependencies); runtime = tools.get("dirtyloops_runtime"); child = tools.get("dirtyloops_child"); const wrongNonce = await execute(child, { action: "ack", handle: completion.handle, nonce: "wrong-nonce", doc_root: docRoot, }); assert.match(wrongNonce.content[0].text, /nonce mismatch/); assert.equal(JSON.parse(fs.readFileSync(path.join(docRoot, "runtime/activation.json"))).state, "probing"); const acknowledged = await execute(child, { action: "ack", handle: completion.handle, nonce: completion.delivery.nonce, doc_root: docRoot, }); assert.equal(acknowledged.details.activation.status, "ready"); assert.equal(JSON.parse(fs.readFileSync(path.join(docRoot, "runtime/activation.json"))).state, "ready"); const duplicateAcknowledged = await execute(child, { action: "ack", handle: completion.handle, nonce: completion.delivery.nonce, doc_root: docRoot, }); assert.equal(duplicateAcknowledged.details.activation.status, "ready"); assert.equal(duplicateAcknowledged.details.activation.idempotent, true); assert.equal(JSON.parse(fs.readFileSync(path.join(docRoot, "runtime/activation.json"))).state, "ready"); const ordinary = await execute(child, { action: "launch", kind: "session", backend: "vanilla", mutation: "read-only", mission: "ordinary", description: "ordinary", cwd: root, doc_root: docRoot, }); await new Promise((resolve) => setTimeout(resolve, 0)); const ordinaryCompletion = visible.at(-1).details; assert.equal(ordinaryCompletion.handle, ordinary.details.handle); await execute(child, { action: "ack", handle: ordinaryCompletion.handle, nonce: ordinaryCompletion.delivery.nonce, doc_root: docRoot, }); writeRequirement(); activation = await execute(runtime, { action: "activate", doc_root: docRoot }); launch = activation.details.activation.launch; const supersededLaunch = await execute(child, { ...launch, cwd: root, doc_root: docRoot }); await new Promise((resolve) => setTimeout(resolve, 0)); const supersededCompletion = visible.at(-1).details; assert.equal(supersededCompletion.handle, supersededLaunch.details.handle); writeRequirement(); const clearedSuperseded = await execute(child, { action: "ack", handle: supersededCompletion.handle, nonce: supersededCompletion.delivery.nonce, doc_root: docRoot, }); assert.equal(clearedSuperseded.details.acknowledgement.ok, true); assert.equal(clearedSuperseded.details.activation.status, "blocked"); activation = await execute(runtime, { action: "activate", doc_root: docRoot }); launch = activation.details.activation.launch; const recoveryLaunch = await execute(child, { ...launch, cwd: root, doc_root: docRoot }); await new Promise((resolve) => setTimeout(resolve, 0)); const recoveryCompletion = visible.at(-1).details; assert.equal(recoveryCompletion.handle, recoveryLaunch.details.handle); const recoveryAck = await execute(child, { action: "ack", handle: recoveryCompletion.handle, nonce: recoveryCompletion.delivery.nonce, doc_root: docRoot, }); assert.equal(recoveryAck.details.activation.status, "ready"); promptMode = "pending"; const heartbeatChild = await execute(child, { action: "launch", kind: "session", backend: "vanilla", mutation: "read-only", mission: "heartbeat-child", description: "heartbeat-child", cwd: root, doc_root: docRoot, }); const unreasonedStatus = await execute(child, { action: "status", handle: heartbeatChild.details.handle, }); assert.match(unreasonedStatus.content[0].text, /requires a recovery reason/); const prematureRecovery = await execute(child, { action: "recover", handle: heartbeatChild.details.handle, reason: "missed-heartbeat", }); assert.equal(prematureRecovery.details.ok, false); assert.match(prematureRecovery.details.error, /premature recovery/); clock += 1_000; const heartbeatRecovery = await execute(child, { action: "recover", handle: heartbeatChild.details.handle, reason: "missed-heartbeat", }); assert.equal(heartbeatRecovery.details.ok, true); assert.equal(heartbeatRecovery.details.recovery.outcome, "observed"); const rateLimitedRecovery = await execute(child, { action: "recover", handle: heartbeatChild.details.handle, reason: "operator-request", }); assert.equal(rateLimitedRecovery.details.ok, false); assert.match(rateLimitedRecovery.details.error, /rate-limited/); await execute(child, { action: "cancel", handle: heartbeatChild.details.handle }); const cancelledCompletion = visible.at(-1).details; assert.equal(cancelledCompletion.handle, heartbeatChild.details.handle); assert.equal(cancelledCompletion.status, "cancelled"); await execute(child, { action: "ack", handle: cancelledCompletion.handle, nonce: cancelledCompletion.delivery.nonce, doc_root: docRoot, }); promptMode = "success"; const deliveryChild = await execute(child, { action: "launch", kind: "session", backend: "vanilla", mutation: "read-only", mission: "delivery-child", description: "delivery-child", cwd: root, doc_root: docRoot, }); await new Promise((resolve) => setTimeout(resolve, 0)); const deliveryCompletion = visible.at(-1).details; assert.equal(deliveryCompletion.handle, deliveryChild.details.handle); const deliveryAttempts = deliveryCompletion.delivery.emission_attempts; clock += 500; sendMode = "failure"; const deliveryRecovery = await execute(child, { action: "recover", handle: deliveryChild.details.handle, reason: "delivery-recovery", }); assert.equal(deliveryRecovery.details.ok, false); assert.equal(deliveryRecovery.details.recovery.outcome, "emission-failed"); assert.match(deliveryRecovery.details.error, /injected visible emission failure/); assert.ok(branch.some((entry) => entry.customType === "dirtyloops-child-emission" && entry.data.handle === deliveryChild.details.handle && entry.data.attempt > deliveryAttempts)); assert.equal(visible.at(-1).details.handle, deliveryChild.details.handle); sendMode = "success"; await execute(child, { action: "ack", handle: deliveryCompletion.handle, nonce: deliveryCompletion.delivery.nonce, doc_root: docRoot, }); const branchBeforeRecovery = branch.filter((entry) => entry.customType !== "dirtyloops-recovery"); const rewindContext = context(); await lifecycle.get("session_tree")({}, { ...rewindContext, sessionManager: { ...rewindContext.sessionManager, getBranch: () => branchBeforeRecovery, }, }); const degradedLaunch = await execute(child, { action: "launch", kind: "session", backend: "vanilla", mutation: "read-only", mission: "blocked-by-degraded-delivery", description: "blocked-by-degraded-delivery", cwd: root, doc_root: docRoot, }); assert.match(degradedLaunch.content[0].text, /requires coordinator renewal/); const renewalRequest = await execute(runtime, { action: "renew", doc_root: docRoot, reason: "degraded-delivery", handle: deliveryChild.details.handle, safe_boundary: true, phase_issue_id: "agents-lwt.6", turn_doc: "docs/implementation/dirtyloops-lifecycle-hardening/turn-docs/06-recovery-and-renewal.md", pr: "none", expected_branch: "lavender/recovery", }); assert.equal(renewalRequest.details.renewal.status, "renewal-required"); let activationFile = JSON.parse(fs.readFileSync(path.join(docRoot, "runtime/activation.json"), "utf8")); assert.equal(activationFile.state, "required"); assert.equal(activationFile.renewal.reason, "degraded-delivery"); const renewedContext = context(undefined, "renewed-coordinator"); activation = await execute(runtime, { action: "activate", doc_root: docRoot }, renewedContext); launch = activation.details.activation.launch; const renewalProbe = await execute(child, { ...launch, cwd: root, doc_root: docRoot }, renewedContext); await new Promise((resolve) => setTimeout(resolve, 0)); const renewalCompletion = visible.at(-1).details; assert.equal(renewalCompletion.handle, renewalProbe.details.handle); const renewalAck = await execute(child, { action: "ack", handle: renewalCompletion.handle, nonce: renewalCompletion.delivery.nonce, doc_root: docRoot, }, renewedContext); assert.equal(renewalAck.details.activation.status, "ready"); assert.equal(renewalAck.details.renewal.status, "completed"); activationFile = JSON.parse(fs.readFileSync(path.join(docRoot, "runtime/activation.json"), "utf8")); assert.equal(activationFile.renewal.destination_coordinator_id, "renewed-coordinator"); coordinatorId = "renewed-coordinator"; for (let index = branch.length - 1; index >= 0; index -= 1) { if (branch[index].customType === "dirtyloops-recovery-renewed") branch.splice(index, 1); } await lifecycle.get("session_shutdown")(); registerDirtyloopsPiAdapter(pi, dependencies); runtime = tools.get("dirtyloops_runtime"); child = tools.get("dirtyloops_child"); const postRenewal = await execute(child, { action: "launch", kind: "session", backend: "vanilla", mutation: "read-only", mission: "post-renewal", description: "post-renewal", cwd: root, doc_root: docRoot, }); await new Promise((resolve) => setTimeout(resolve, 0)); const postRenewalCompletion = visible.at(-1).details; assert.equal(postRenewalCompletion.handle, postRenewal.details.handle); assert.ok(branch.some((entry) => entry.customType === "dirtyloops-recovery-renewed")); await execute(child, { action: "ack", handle: postRenewalCompletion.handle, nonce: postRenewalCompletion.delivery.nonce, doc_root: docRoot, }); promptMode = "pending"; for (let index = 0; index < 4; index += 1) { const active = await execute(child, { action: "launch", kind: "session", backend: "vanilla", mutation: "read-only", mission: `active-${index}`, description: `active-${index}`, cwd: root, doc_root: docRoot, }); assert.match(active.content[0].text, /"status": "running"/); } writeRequirement(); activation = await execute(runtime, { action: "activate", doc_root: docRoot }); launch = activation.details.activation.launch; failed = await execute(child, { ...launch, cwd: root, doc_root: docRoot }); assert.match(failed.content[0].text, /certified limit is 4 active children/); assert.equal(JSON.parse(fs.readFileSync(path.join(docRoot, "runtime/activation.json"))).failure.boundary, "launch"); assert.ok(branch.some((entry) => entry.customType === "dirtyloops-child-record")); assert.ok(branch.some((entry) => entry.customType === "dirtyloops-child-emission")); assert.ok(branch.some((entry) => entry.customType === "dirtyloops-child-ack")); await lifecycle.get("session_shutdown")(); } finally { fs.rmSync(root, { recursive: true, force: true }); } console.log("dirtyloops Pi adapter integration tests passed");