2098 lines
81 KiB
TypeScript
2098 lines
81 KiB
TypeScript
import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http';
|
|
import { execFile } from 'node:child_process';
|
|
import { mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promises';
|
|
import { tmpdir } from 'node:os';
|
|
import path from 'node:path';
|
|
import { promisify } from 'node:util';
|
|
|
|
import type { CodingProductComposition } from '../../api/coding-product-services';
|
|
import { CodingConversationServiceError } from '../conversation-service';
|
|
import { getRecentLogs } from '../../utils/logger';
|
|
import { createCodingConversationStore } from '../../coding-projects/conversation-store';
|
|
import { createCodingProjectAgent } from '../../coding-projects/project-config';
|
|
import {
|
|
createCodingProjectStore,
|
|
createLocalCodingProject,
|
|
createMemoryCodingProjectStorage,
|
|
type CodingProjectStore,
|
|
} from '../../coding-projects/project-store';
|
|
import type { ProviderAccount } from '../../shared/providers/types';
|
|
import { getProviderService } from '../../services/providers/provider-service';
|
|
import {
|
|
clearWorksSquareAIGatewayCredential,
|
|
seedWorksSquareAIGatewayCredential,
|
|
} from '../../services/works-square-ai-gateway';
|
|
import type { ConversationPatchEnvelope, PrepareConversationInput } from '../contracts';
|
|
import { PiManagedExtensionHost } from './extension-host';
|
|
import { PiManagedInputRevisionCoordinator } from './managed-input-revision';
|
|
import { runPiReleasePressureCleanup } from './release-proof-cleanup';
|
|
import { ensurePiManagedPaths } from './resource-loader';
|
|
import type { PiRpcEvent } from './rpc-client';
|
|
import { createPiManagedWorkerOpener, PiConversationRuntime } from './runtime';
|
|
import { PiSessionRegistry } from './session-registry';
|
|
import { createPiManagedSubagentChildOpener } from './subagent-child';
|
|
import { PiSubagentScheduler } from './subagent';
|
|
import type { PiRuntimeTelemetryEvent } from './telemetry';
|
|
import { PiWorkerProcess, type PiWorkerProcessOptions } from './worker-process';
|
|
import { PiProcessBudget, PiWorkerPool, type PiWorkerPoolEvent } from './worker-pool';
|
|
import { PiProjectWriteLeaseCoordinator, type PiProjectWriteLease } from './write-lease';
|
|
|
|
type ProofWorkerRole = 'parent' | 'child';
|
|
type ProofProviderMode = 'subagent' | 'pressure' | 'resilience' | 'submit-rejection';
|
|
type ProofMilestone = PiRuntimeTelemetryEvent['milestone'] | 'agent.start' | 'provider.first_event';
|
|
type ProofMilestoneSource = 'main.telemetry' | 'pi.agent_start' | 'pi.assistant_message_start';
|
|
|
|
type TrackedProcess = {
|
|
role: ProofWorkerRole;
|
|
process: PiWorkerProcess;
|
|
};
|
|
|
|
type ObservedWorkerEvent = {
|
|
conversationId: string;
|
|
generation: number;
|
|
event: PiRpcEvent;
|
|
at: number;
|
|
};
|
|
|
|
type ProviderRequest = {
|
|
role: ProofWorkerRole;
|
|
toolNames: string[];
|
|
hasToolResult: boolean;
|
|
firstMessageRole: string | null;
|
|
outcome: 'accepted' | 'developer_role_rejection';
|
|
};
|
|
|
|
type HeldProviderResponse = {
|
|
role: ProofWorkerRole;
|
|
response: ServerResponse;
|
|
};
|
|
|
|
type LocalProofProvider = {
|
|
baseUrl: string;
|
|
requests: ProviderRequest[];
|
|
roleContract: { developerRejected: boolean; systemAccepted: boolean } | null;
|
|
activeCounts(): { parent: number; child: number };
|
|
releaseChildren(): void;
|
|
releaseParents(): void;
|
|
releaseAll(): void;
|
|
armDelayedPrompt(): void;
|
|
armDelayedCompaction(): void;
|
|
close(): Promise<void>;
|
|
};
|
|
|
|
type ProofProject = {
|
|
input: PrepareConversationInput;
|
|
projectPath: string;
|
|
};
|
|
|
|
type RealProofComposition = {
|
|
projects: ProofProject[];
|
|
processBudget: PiProcessBudget;
|
|
tracked: TrackedProcess[];
|
|
telemetry: PiRuntimeTelemetryEvent[];
|
|
observedEvents: ObservedWorkerEvent[];
|
|
extensionHost: PiManagedExtensionHost;
|
|
pool: PiWorkerPool;
|
|
scheduler: PiSubagentScheduler;
|
|
revisions: PiManagedInputRevisionCoordinator;
|
|
};
|
|
|
|
type PressureRun = {
|
|
active: PiReleasePressureSnapshot;
|
|
finish(options?: { injectFailureAt?: 'parents.settle' }): Promise<PiReleasePressureSnapshot>;
|
|
};
|
|
|
|
type ProxyCompositionRun = {
|
|
composition: CodingProductComposition;
|
|
provider: LocalProofProvider;
|
|
projectId: string;
|
|
projectPath: string;
|
|
sourceConversationId?: string;
|
|
hostToken: string;
|
|
originalAcceptPrompt: CodingProductComposition['conversations']['acceptPrompt'];
|
|
submissionInjection: { definiteRejections: number };
|
|
activeStatus?: PiReleaseProxyCompositionStatus;
|
|
settledStatus?: PiReleaseProxyCompositionStatus;
|
|
};
|
|
|
|
type ResilienceCompositionRun = {
|
|
composition: CodingProductComposition;
|
|
provider: LocalProofProvider;
|
|
projectId: string;
|
|
projectPath: string;
|
|
targetConversationId: string;
|
|
otherConversationId: string;
|
|
targetBinding: { piSessionId: string; sessionKey: string };
|
|
otherBinding: { piSessionId: string; sessionKey: string };
|
|
hostToken: string;
|
|
};
|
|
|
|
type InspectedPiProcess = {
|
|
processId: number;
|
|
role: ProofWorkerRole;
|
|
commandLineContainsHostToken: boolean;
|
|
};
|
|
|
|
export interface PiReleaseManagedTurnProof {
|
|
runRef: string;
|
|
cold: boolean;
|
|
workerGeneration: number;
|
|
milestones: Array<{
|
|
milestone: ProofMilestone;
|
|
source: ProofMilestoneSource;
|
|
durationMs: number;
|
|
at: number;
|
|
}>;
|
|
}
|
|
|
|
export interface PiReleasePressureSnapshot {
|
|
parentWorkers: number;
|
|
childWorkers: number;
|
|
parentProcessIds: number[];
|
|
childProcessIds: number[];
|
|
liveProcessIds: number[];
|
|
providerRequests: { parent: number; child: number };
|
|
processBudget: { active: number; waiting: number };
|
|
childPermits: { active: number; waiting: number };
|
|
dispatches: { active: number; parents: number };
|
|
writeLeases: { active: number; waiting: number };
|
|
}
|
|
|
|
export interface PiReleaseExtensionProof {
|
|
parentToolNames: string[];
|
|
childToolNames: string[];
|
|
parentProcessIds: number[];
|
|
childProcessIds: number[];
|
|
providerRequests: { parent: number; child: number };
|
|
subagentStatus: string;
|
|
subagentSummary: string;
|
|
materializedExtension: string;
|
|
providerFirstEventDelayMs: number;
|
|
managedTurns: PiReleaseManagedTurnProof[];
|
|
managedWorkerMilestones: PiRuntimeTelemetryEvent[];
|
|
released: {
|
|
processBudget: { active: number; waiting: number };
|
|
childPermits: { active: number; waiting: number };
|
|
dispatches: { active: number; parents: number };
|
|
liveProcessIds: number[];
|
|
};
|
|
}
|
|
|
|
export interface PiReleaseProxyCompositionStatus {
|
|
conversationCount: number;
|
|
providerRequests: { parent: number; child: number };
|
|
activeProviderRequests: { parent: number; child: number };
|
|
workers: ReturnType<CodingProductComposition['runtime']['getDiagnostics']>['workers'];
|
|
processes: {
|
|
supported: boolean;
|
|
parent: number[];
|
|
child: number[];
|
|
};
|
|
currentHostTokenAvailable: boolean;
|
|
tokenSafety: {
|
|
argv: boolean;
|
|
modelsJson: boolean;
|
|
logs: boolean;
|
|
diagnostics: boolean;
|
|
};
|
|
providerCompatibility: {
|
|
roleContract: { developerRejected: boolean; systemAccepted: boolean };
|
|
requestRoles: string[];
|
|
developerRoleRequests: number;
|
|
systemRoleRequests: number;
|
|
controlledDefiniteRejections: number;
|
|
};
|
|
realTurnVerified: false;
|
|
}
|
|
|
|
export interface PiReleaseProxyCompositionProof extends PiReleaseProxyCompositionStatus {
|
|
providerMode: 'loopback-through-authenticated-host-proxy';
|
|
firstConversation: {
|
|
count: number;
|
|
bindingEstablished: boolean;
|
|
workerStatus: string;
|
|
runStatus: string;
|
|
userInputProjected: boolean;
|
|
parentResponseProjected: boolean;
|
|
definiteRejectionObserved: boolean;
|
|
retryAccepted: boolean;
|
|
};
|
|
fork: {
|
|
conversationCount: number;
|
|
sameAgent: boolean;
|
|
targetBindingEstablished: boolean;
|
|
distinctSessionBinding: boolean;
|
|
workerStatus: string;
|
|
runStatus: string;
|
|
sourceEntryRole: 'user';
|
|
targetHydratedBeforeSourceEntry: boolean;
|
|
sourceUnchanged: boolean;
|
|
promptReplayObserved: boolean;
|
|
};
|
|
currentHostTokenUsedBy: ProofWorkerRole[];
|
|
released: { workers: number };
|
|
}
|
|
|
|
export interface PiReleaseResilienceStatus {
|
|
target: {
|
|
conversationId: string;
|
|
workerStatus: string;
|
|
workerGeneration: number;
|
|
runStatus: string;
|
|
errorCode: string | null;
|
|
recoverable: boolean;
|
|
bindingPreserved: boolean;
|
|
contextCompaction: 'idle' | 'running';
|
|
completedCompactions: number;
|
|
};
|
|
other: {
|
|
conversationId: string;
|
|
workerStatus: string;
|
|
workerGeneration: number;
|
|
runStatus: string;
|
|
bindingPreserved: boolean;
|
|
};
|
|
providerRequests: { parent: number; child: number };
|
|
activeProviderRequests: { parent: number; child: number };
|
|
resources: ReturnType<PiConversationRuntime['getResilienceProofDiagnostics']>;
|
|
processes: { supported: boolean; parent: number[]; child: number[] };
|
|
realTurnVerified: false;
|
|
}
|
|
|
|
export interface PiReleaseResilienceProof extends PiReleaseResilienceStatus {
|
|
lifecycle: {
|
|
unexpectedExit: boolean;
|
|
protocolInvalidation: boolean;
|
|
intentionalStop: boolean;
|
|
everyStopHasReason: boolean;
|
|
everyReplacementHasReason: boolean;
|
|
diagnosticRedacted: boolean;
|
|
promptFree: boolean;
|
|
projectPathFree: boolean;
|
|
};
|
|
released: {
|
|
workers: number;
|
|
processes: number;
|
|
resources: ReturnType<PiConversationRuntime['getResilienceProofDiagnostics']>;
|
|
};
|
|
realTurnVerified: false;
|
|
}
|
|
|
|
export interface PiReleaseResilienceIdleStatus {
|
|
workers: ReturnType<CodingProductComposition['runtime']['getDiagnostics']>['workers'];
|
|
resources: ReturnType<PiConversationRuntime['getResilienceProofDiagnostics']>;
|
|
processes: { supported: boolean; parent: number[]; child: number[] };
|
|
backgroundSleepReasoned: boolean;
|
|
realTurnVerified: false;
|
|
}
|
|
|
|
export interface PiReleaseIntentionalDisposeProof {
|
|
terminal: { observed: boolean; errorCode: string | null; recoverable: boolean };
|
|
other: { runStatus: string; bindingPreserved: boolean };
|
|
targetBindingPreserved: boolean;
|
|
providerRequestsUnchanged: boolean;
|
|
resources: ReturnType<PiConversationRuntime['getResilienceProofDiagnostics']>;
|
|
realTurnVerified: false;
|
|
}
|
|
|
|
const PROOF_ACCOUNT_ID = 'release-proof-account';
|
|
const PROOF_AGENT_ID = 'release-proof-agent';
|
|
const PROOF_MODEL_ID = 'release-proof-model';
|
|
const PROXY_PROOF_MODEL_ID = 'deepseek-v4-pro';
|
|
const PROOF_PROVIDER_FIRST_EVENT_DELAY_MS = 75;
|
|
const PROOF_MUTATION_CONFIRMATION_DELAY_MS = 12_000;
|
|
const EXPECTED_TURN_MILESTONES: readonly ProofMilestone[] = [
|
|
'worker.queue_wait',
|
|
'resources.ready',
|
|
'worker.spawn',
|
|
'rpc.ready',
|
|
'session.open',
|
|
'prompt.accepted',
|
|
'agent.start',
|
|
'provider.first_event',
|
|
'agent.settled',
|
|
];
|
|
|
|
let pressureRun: PressureRun | null = null;
|
|
let proxyCompositionRun: ProxyCompositionRun | null = null;
|
|
let resilienceCompositionRun: ResilienceCompositionRun | null = null;
|
|
const execFileAsync = promisify(execFile);
|
|
|
|
async function waitFor(predicate: () => boolean, message: string): Promise<void> {
|
|
const deadline = Date.now() + 30_000;
|
|
while (Date.now() < deadline) {
|
|
if (predicate()) return;
|
|
await new Promise((resolveWait) => setTimeout(resolveWait, 20));
|
|
}
|
|
throw new Error(message);
|
|
}
|
|
|
|
async function delay(durationMs: number): Promise<void> {
|
|
await new Promise((resolve) => setTimeout(resolve, durationMs));
|
|
}
|
|
|
|
function isAssistantMessageStart(event: PiRpcEvent): boolean {
|
|
if (event.type !== 'message_start') return false;
|
|
const message = event.message;
|
|
return Boolean(message)
|
|
&& typeof message === 'object'
|
|
&& !Array.isArray(message)
|
|
&& (message as { role?: unknown }).role === 'assistant';
|
|
}
|
|
|
|
function shortRef(value: string): string {
|
|
return value.replace(/[^A-Za-z0-9]/g, '').slice(-8).toLowerCase() || 'unknown';
|
|
}
|
|
|
|
function readRequestBody(request: IncomingMessage): Promise<Record<string, unknown>> {
|
|
return new Promise((resolve, reject) => {
|
|
const chunks: Buffer[] = [];
|
|
request.on('data', (chunk: Buffer) => chunks.push(chunk));
|
|
request.once('end', () => {
|
|
try {
|
|
const parsed: unknown = JSON.parse(Buffer.concat(chunks).toString('utf8'));
|
|
resolve(parsed && typeof parsed === 'object' && !Array.isArray(parsed)
|
|
? parsed as Record<string, unknown>
|
|
: {});
|
|
} catch (error) {
|
|
reject(error);
|
|
}
|
|
});
|
|
request.once('error', reject);
|
|
});
|
|
}
|
|
|
|
function toolNamesFrom(body: Record<string, unknown>): string[] {
|
|
if (!Array.isArray(body.tools)) return [];
|
|
return body.tools.flatMap((entry) => {
|
|
if (!entry || typeof entry !== 'object' || Array.isArray(entry)) return [];
|
|
const function_ = (entry as { function?: unknown }).function;
|
|
if (!function_ || typeof function_ !== 'object' || Array.isArray(function_)) return [];
|
|
const name = (function_ as { name?: unknown }).name;
|
|
return typeof name === 'string' ? [name] : [];
|
|
}).sort();
|
|
}
|
|
|
|
function hasToolResult(body: Record<string, unknown>): boolean {
|
|
return Array.isArray(body.messages) && body.messages.some((message) => (
|
|
message && typeof message === 'object' && !Array.isArray(message)
|
|
&& (message as { role?: unknown }).role === 'tool'
|
|
));
|
|
}
|
|
|
|
function firstMessageRole(body: Record<string, unknown>): string | null {
|
|
if (!Array.isArray(body.messages)) return null;
|
|
const first = body.messages[0];
|
|
if (!first || typeof first !== 'object' || Array.isArray(first)) return null;
|
|
const role = (first as { role?: unknown }).role;
|
|
return typeof role === 'string' ? role : null;
|
|
}
|
|
|
|
function rejectProofRequest(response: ServerResponse, message: string): void {
|
|
response.writeHead(400, { 'content-type': 'application/json' });
|
|
response.end(JSON.stringify({ error: { message } }));
|
|
}
|
|
|
|
function writeChunk(
|
|
response: ServerResponse,
|
|
model: string,
|
|
delta: Record<string, unknown>,
|
|
finishReason: string | null,
|
|
): void {
|
|
if (response.destroyed || response.writableEnded) return;
|
|
response.write(`data: ${JSON.stringify({
|
|
id: `chatcmpl-release-proof-${Date.now()}`,
|
|
object: 'chat.completion.chunk',
|
|
created: Math.floor(Date.now() / 1_000),
|
|
model,
|
|
choices: [{ index: 0, delta, finish_reason: finishReason }],
|
|
...(finishReason ? { usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 } } : {}),
|
|
})}\n\n`);
|
|
}
|
|
|
|
function finishResponse(response: ServerResponse, model: string): void {
|
|
if (response.destroyed || response.writableEnded) return;
|
|
writeChunk(response, model, {}, 'stop');
|
|
response.end('data: [DONE]\n\n');
|
|
}
|
|
|
|
function respondWithText(response: ServerResponse, model: string, text: string): void {
|
|
response.writeHead(200, { 'content-type': 'text/event-stream' });
|
|
writeChunk(response, model, { role: 'assistant', content: text }, null);
|
|
finishResponse(response, model);
|
|
}
|
|
|
|
function respondWithSubagentCall(
|
|
response: ServerResponse,
|
|
model: string,
|
|
toolProfile: 'read-only' | 'coding' = 'read-only',
|
|
): void {
|
|
response.writeHead(200, { 'content-type': 'text/event-stream' });
|
|
writeChunk(response, model, {
|
|
role: 'assistant',
|
|
tool_calls: [{
|
|
index: 0,
|
|
id: 'release-proof-subagent-call',
|
|
type: 'function',
|
|
function: {
|
|
name: 'subagent',
|
|
arguments: JSON.stringify({
|
|
mode: 'single',
|
|
tasks: [{
|
|
agentId: PROOF_AGENT_ID,
|
|
task: 'Return REAL_CHILD_COMPLETE for final packaged qualification.',
|
|
toolProfile,
|
|
}],
|
|
}),
|
|
},
|
|
}],
|
|
}, null);
|
|
writeChunk(response, model, {}, 'tool_calls');
|
|
response.end('data: [DONE]\n\n');
|
|
}
|
|
|
|
function respondWithHoldingBashCall(response: ServerResponse, model: string): void {
|
|
response.writeHead(200, { 'content-type': 'text/event-stream' });
|
|
writeChunk(response, model, {
|
|
role: 'assistant',
|
|
tool_calls: [{
|
|
index: 0,
|
|
id: 'release-proof-holding-bash',
|
|
type: 'function',
|
|
function: {
|
|
name: 'bash',
|
|
arguments: JSON.stringify({
|
|
command: 'powershell.exe -NoProfile -NonInteractive -Command "Start-Sleep -Seconds 120"',
|
|
}),
|
|
},
|
|
}],
|
|
}, null);
|
|
writeChunk(response, model, {}, 'tool_calls');
|
|
response.end('data: [DONE]\n\n');
|
|
}
|
|
|
|
function latestUserMessageContains(body: Record<string, unknown>, marker: string): boolean {
|
|
if (!Array.isArray(body.messages)) return false;
|
|
for (let index = body.messages.length - 1; index >= 0; index -= 1) {
|
|
const message = body.messages[index];
|
|
if (!message || typeof message !== 'object' || Array.isArray(message)) continue;
|
|
const record = message as { role?: unknown; content?: unknown };
|
|
if (record.role !== 'user') continue;
|
|
return JSON.stringify(record.content ?? '').includes(marker);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
async function startLocalProofProvider(mode: ProofProviderMode): Promise<LocalProofProvider> {
|
|
const requests: ProviderRequest[] = [];
|
|
const held = new Set<HeldProviderResponse>();
|
|
let closed = false;
|
|
let closeFlight: Promise<void> | null = null;
|
|
let delayedPromptArmed = false;
|
|
let delayedCompactionArmed = false;
|
|
const server: Server = createServer(async (request, response) => {
|
|
response.once('error', () => undefined);
|
|
try {
|
|
if (request.method !== 'POST' || request.url !== '/v1/chat/completions') {
|
|
response.writeHead(404).end();
|
|
return;
|
|
}
|
|
const body = await readRequestBody(request);
|
|
const model = typeof body.model === 'string' ? body.model : PROOF_MODEL_ID;
|
|
const leadingRole = firstMessageRole(body);
|
|
if (request.headers['x-makelore-release-proof-role-canary'] === '1') {
|
|
if (leadingRole === 'developer') {
|
|
rejectProofRequest(response, 'developer role is unsupported by the controlled endpoint');
|
|
} else if (leadingRole === 'system') {
|
|
response.writeHead(204).end();
|
|
} else {
|
|
rejectProofRequest(response, 'role canary requires a leading system or developer message');
|
|
}
|
|
return;
|
|
}
|
|
const toolNames = toolNamesFrom(body);
|
|
const role: ProofWorkerRole = mode === 'resilience' && delayedCompactionArmed
|
|
? 'parent'
|
|
: toolNames.includes('subagent') ? 'parent' : 'child';
|
|
const toolResult = hasToolResult(body);
|
|
const providerRequest: ProviderRequest = {
|
|
role,
|
|
toolNames,
|
|
hasToolResult: toolResult,
|
|
firstMessageRole: leadingRole,
|
|
outcome: 'accepted',
|
|
};
|
|
requests.push(providerRequest);
|
|
|
|
if (mode === 'submit-rejection' && leadingRole === 'developer') {
|
|
providerRequest.outcome = 'developer_role_rejection';
|
|
rejectProofRequest(response, 'developer role is unsupported by the controlled endpoint');
|
|
return;
|
|
}
|
|
if (mode === 'resilience') {
|
|
if (role === 'child') {
|
|
if (toolResult) respondWithText(response, model, 'RESILIENCE_CHILD_COMPLETE');
|
|
else respondWithHoldingBashCall(response, model);
|
|
return;
|
|
}
|
|
if (latestUserMessageContains(body, 'RESILIENCE_RECOVERED_TURN')) {
|
|
respondWithText(response, model, 'RESILIENCE_RECOVERED_COMPLETE');
|
|
return;
|
|
}
|
|
if (latestUserMessageContains(body, 'RESILIENCE_SETTLED_BEFORE_CLOSE')) {
|
|
respondWithText(response, model, 'RESILIENCE_SETTLED_COMPLETE');
|
|
return;
|
|
}
|
|
if (latestUserMessageContains(body, 'RESILIENCE_PROTOCOL_ACTIVE')) {
|
|
response.writeHead(200, { 'content-type': 'text/event-stream' });
|
|
writeChunk(response, model, { role: 'assistant', content: 'RESILIENCE_ACTIVE' }, null);
|
|
const entry = { role, response };
|
|
held.add(entry);
|
|
response.once('close', () => held.delete(entry));
|
|
return;
|
|
}
|
|
if (latestUserMessageContains(body, 'RESILIENCE_TARGET_ACTIVE')) {
|
|
respondWithSubagentCall(response, model, 'coding');
|
|
return;
|
|
}
|
|
if (delayedCompactionArmed) {
|
|
delayedCompactionArmed = false;
|
|
const entry = { role, response } satisfies HeldProviderResponse;
|
|
held.add(entry);
|
|
response.once('close', () => held.delete(entry));
|
|
await delay(PROOF_MUTATION_CONFIRMATION_DELAY_MS);
|
|
if (!held.delete(entry)) return;
|
|
respondWithText(response, model, 'RESILIENCE_COMPACTION_SUMMARY');
|
|
return;
|
|
}
|
|
if (role === 'parent' && delayedPromptArmed) {
|
|
delayedPromptArmed = false;
|
|
const entry = { role, response } satisfies HeldProviderResponse;
|
|
held.add(entry);
|
|
response.once('close', () => held.delete(entry));
|
|
await delay(PROOF_MUTATION_CONFIRMATION_DELAY_MS);
|
|
if (!held.has(entry)) return;
|
|
response.writeHead(200, { 'content-type': 'text/event-stream' });
|
|
writeChunk(response, model, { role: 'assistant', content: 'RESILIENCE_ACTIVE' }, null);
|
|
return;
|
|
}
|
|
response.writeHead(200, { 'content-type': 'text/event-stream' });
|
|
writeChunk(response, model, { role: 'assistant', content: 'RESILIENCE_ACTIVE' }, null);
|
|
const entry = { role, response };
|
|
held.add(entry);
|
|
response.once('close', () => held.delete(entry));
|
|
return;
|
|
}
|
|
|
|
if ((mode === 'subagent' || mode === 'submit-rejection') && role === 'parent' && !toolResult) {
|
|
await delay(PROOF_PROVIDER_FIRST_EVENT_DELAY_MS);
|
|
respondWithSubagentCall(response, model);
|
|
return;
|
|
}
|
|
if ((mode === 'subagent' || mode === 'submit-rejection') && role === 'parent') {
|
|
await delay(PROOF_PROVIDER_FIRST_EVENT_DELAY_MS);
|
|
respondWithText(response, model, 'REAL_PARENT_COMPLETE');
|
|
return;
|
|
}
|
|
|
|
response.writeHead(200, { 'content-type': 'text/event-stream' });
|
|
writeChunk(
|
|
response,
|
|
model,
|
|
{ role: 'assistant', content: role === 'child' ? 'REAL_CHILD_COMPLETE' : 'PRESSURE_ACTIVE' },
|
|
null,
|
|
);
|
|
const entry = { role, response };
|
|
held.add(entry);
|
|
response.once('close', () => held.delete(entry));
|
|
} catch (error) {
|
|
if (!response.headersSent) response.writeHead(400, { 'content-type': 'application/json' });
|
|
if (!response.destroyed && !response.writableEnded) {
|
|
response.end(JSON.stringify({ error: error instanceof Error ? error.message : String(error) }));
|
|
}
|
|
}
|
|
});
|
|
await new Promise<void>((resolve, reject) => {
|
|
server.once('error', reject);
|
|
server.listen(0, '127.0.0.1', resolve);
|
|
});
|
|
const address = server.address();
|
|
if (!address || typeof address === 'string') {
|
|
throw new Error('Release proof Provider did not bind a loopback port');
|
|
}
|
|
const baseUrl = `http://127.0.0.1:${address.port}/v1`;
|
|
let roleContract: LocalProofProvider['roleContract'] = null;
|
|
if (mode === 'submit-rejection') {
|
|
const probe = async (role: 'developer' | 'system'): Promise<number> => {
|
|
const response = await fetch(`${baseUrl}/chat/completions`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'content-type': 'application/json',
|
|
'x-makelore-release-proof-role-canary': '1',
|
|
},
|
|
body: JSON.stringify({
|
|
model: PROXY_PROOF_MODEL_ID,
|
|
messages: [{ role, content: 'role canary' }],
|
|
}),
|
|
});
|
|
await response.text();
|
|
return response.status;
|
|
};
|
|
const [developerStatus, systemStatus] = await Promise.all([
|
|
probe('developer'),
|
|
probe('system'),
|
|
]);
|
|
roleContract = {
|
|
developerRejected: developerStatus === 400,
|
|
systemAccepted: systemStatus === 204,
|
|
};
|
|
}
|
|
const release = (role?: ProofWorkerRole) => {
|
|
for (const entry of [...held]) {
|
|
if (role && entry.role !== role) continue;
|
|
held.delete(entry);
|
|
finishResponse(entry.response, PROOF_MODEL_ID);
|
|
}
|
|
};
|
|
return {
|
|
baseUrl,
|
|
requests,
|
|
roleContract,
|
|
activeCounts: () => ({
|
|
parent: [...held].filter(({ role }) => role === 'parent').length,
|
|
child: [...held].filter(({ role }) => role === 'child').length,
|
|
}),
|
|
releaseChildren: () => release('child'),
|
|
releaseParents: () => release('parent'),
|
|
releaseAll: () => release(),
|
|
armDelayedPrompt: () => { delayedPromptArmed = true; },
|
|
armDelayedCompaction: () => { delayedCompactionArmed = true; },
|
|
close: async () => {
|
|
if (closed) return;
|
|
if (!closeFlight) {
|
|
closeFlight = (async () => {
|
|
release();
|
|
await new Promise<void>((resolve, reject) => {
|
|
server.close((error) => (error ? reject(error) : resolve()));
|
|
server.closeIdleConnections?.();
|
|
});
|
|
closed = true;
|
|
})().finally(() => {
|
|
closeFlight = null;
|
|
});
|
|
}
|
|
await closeFlight;
|
|
},
|
|
};
|
|
}
|
|
|
|
function providerAccount(baseUrl: string): ProviderAccount {
|
|
const now = new Date().toISOString();
|
|
return {
|
|
id: PROOF_ACCOUNT_ID,
|
|
vendorId: 'custom',
|
|
label: 'Release proof account',
|
|
authMode: 'api_key',
|
|
apiProtocol: 'openai-completions',
|
|
baseUrl,
|
|
model: PROOF_MODEL_ID,
|
|
enabled: true,
|
|
isDefault: true,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
};
|
|
}
|
|
|
|
async function createProofProjects(
|
|
root: string,
|
|
count: number,
|
|
projectStore: CodingProjectStore,
|
|
): Promise<ProofProject[]> {
|
|
const now = new Date().toISOString();
|
|
const projects: ProofProject[] = [];
|
|
for (let index = 0; index < count; index += 1) {
|
|
const number = index + 1;
|
|
const projectPath = path.join(root, `project-${number}`);
|
|
const { project } = await createLocalCodingProject({ projectPath, now }, projectStore);
|
|
await createCodingProjectAgent(projectPath, {
|
|
id: PROOF_AGENT_ID,
|
|
avatarId: 'avatar-01',
|
|
roleName: 'Release proof',
|
|
name: 'Release proof agent',
|
|
model: {
|
|
accountId: PROOF_ACCOUNT_ID,
|
|
modelId: PROOF_MODEL_ID,
|
|
thinkingLevel: 'medium',
|
|
},
|
|
modelResolution: 'resolved',
|
|
responsibility: {
|
|
mission: 'Exercise final packaged Pi composition',
|
|
owns: [],
|
|
boundaries: [],
|
|
collaborators: [],
|
|
principles: [],
|
|
},
|
|
prompt: 'Follow the controlled release qualification Provider.',
|
|
skillIds: [],
|
|
}, { now });
|
|
const conversationStore = createCodingConversationStore(projectPath, {
|
|
createId: () => `f47ac10b-58cc-4372-a567-${String(number).padStart(12, '0')}`,
|
|
now: () => now,
|
|
});
|
|
const conversation = await conversationStore.create({
|
|
agentId: PROOF_AGENT_ID,
|
|
title: `Release proof ${number}`,
|
|
model: {
|
|
accountId: PROOF_ACCOUNT_ID,
|
|
modelId: PROOF_MODEL_ID,
|
|
thinkingLevel: 'medium',
|
|
},
|
|
modelResolution: 'resolved',
|
|
});
|
|
projects.push({
|
|
projectPath,
|
|
input: {
|
|
conversationId: conversation.id,
|
|
projectId: project.id,
|
|
agentId: PROOF_AGENT_ID,
|
|
title: conversation.title,
|
|
model: { model: conversation.model, modelResolution: conversation.modelResolution },
|
|
},
|
|
});
|
|
}
|
|
return projects;
|
|
}
|
|
|
|
function trackedProcessFactory(role: ProofWorkerRole, tracked: TrackedProcess[]) {
|
|
return (options: PiWorkerProcessOptions) => {
|
|
const process = new PiWorkerProcess(options);
|
|
tracked.push({ role, process });
|
|
return process;
|
|
};
|
|
}
|
|
|
|
async function createRealProofComposition(
|
|
root: string,
|
|
provider: LocalProofProvider,
|
|
projectCount: number,
|
|
): Promise<RealProofComposition> {
|
|
const projectIds = Array.from({ length: projectCount }, (_, index) => `release-proof-project-${index + 1}`);
|
|
let projectIdIndex = 0;
|
|
const projectStore = createCodingProjectStore(createMemoryCodingProjectStorage(), {
|
|
createId: () => projectIds[projectIdIndex++] ?? `release-proof-project-${projectIdIndex}`,
|
|
});
|
|
const projects = await createProofProjects(root, projectCount, projectStore);
|
|
const processBudget = new PiProcessBudget(8);
|
|
const tracked: TrackedProcess[] = [];
|
|
const telemetry: PiRuntimeTelemetryEvent[] = [];
|
|
const observedEvents: ObservedWorkerEvent[] = [];
|
|
const revisions = new PiManagedInputRevisionCoordinator();
|
|
const extensionHost = new PiManagedExtensionHost();
|
|
const account = providerAccount(provider.baseUrl);
|
|
const loadProviderInput = async () => ({ accounts: [account], modelSummaries: [] });
|
|
const registry = new PiSessionRegistry({ projectStore });
|
|
const pool = new PiWorkerPool({
|
|
maxRunning: 4,
|
|
maxIdle: 4,
|
|
processBudget,
|
|
revisionCoordinator: revisions,
|
|
onTelemetry: (event) => telemetry.push(event),
|
|
openWorker: createPiManagedWorkerOpener({
|
|
registry,
|
|
executablePath: process.execPath,
|
|
cliPath: path.join(process.resourcesPath, 'pi-runtime', 'dist', 'cli.js'),
|
|
userDataDir: path.join(root, 'user-data'),
|
|
bundledSkillsDir: path.join(process.resourcesPath, 'resources', 'coding-skills'),
|
|
extensionHost,
|
|
loadProviderInput,
|
|
resolveCredential: async () => 'release-proof-local-only',
|
|
createProcess: trackedProcessFactory('parent', tracked),
|
|
onTelemetry: (event) => telemetry.push(event),
|
|
}),
|
|
});
|
|
pool.subscribe((event) => {
|
|
if (event.type !== 'worker.event') return;
|
|
observedEvents.push({
|
|
conversationId: event.conversationId,
|
|
generation: event.generation,
|
|
event: event.event,
|
|
at: Date.now(),
|
|
});
|
|
});
|
|
const childOpener = createPiManagedSubagentChildOpener({
|
|
projectStore,
|
|
executablePath: process.execPath,
|
|
cliPath: path.join(process.resourcesPath, 'pi-runtime', 'dist', 'cli.js'),
|
|
userDataDir: path.join(root, 'user-data'),
|
|
bundledSkillsDir: path.join(process.resourcesPath, 'resources', 'coding-skills'),
|
|
extensionHost,
|
|
loadProviderInput,
|
|
resolveCredential: async () => 'release-proof-local-only',
|
|
getRevision: () => revisions.current,
|
|
createProcess: trackedProcessFactory('child', tracked),
|
|
});
|
|
const scheduler = new PiSubagentScheduler({
|
|
processBudget,
|
|
openChild: childOpener,
|
|
reclaimProcessCapacity: (signal) => pool.reclaimIdleWorker(signal),
|
|
});
|
|
extensionHost.configureSubagents({ scheduler });
|
|
return {
|
|
projects,
|
|
processBudget,
|
|
tracked,
|
|
telemetry,
|
|
observedEvents,
|
|
extensionHost,
|
|
pool,
|
|
scheduler,
|
|
revisions,
|
|
};
|
|
}
|
|
|
|
function processIds(tracked: TrackedProcess[], role: ProofWorkerRole, runningOnly: boolean): number[] {
|
|
return tracked
|
|
.filter((entry) => entry.role === role && (!runningOnly || entry.process.isRunning))
|
|
.flatMap(({ process }) => process.processId === undefined ? [] : [process.processId])
|
|
.sort((left, right) => left - right);
|
|
}
|
|
|
|
function providerRequestCounts(provider: LocalProofProvider): { parent: number; child: number } {
|
|
return {
|
|
parent: provider.requests.filter(({ role }) => role === 'parent').length,
|
|
child: provider.requests.filter(({ role }) => role === 'child').length,
|
|
};
|
|
}
|
|
|
|
async function fileContainsValue(filePath: string, value: string): Promise<boolean> {
|
|
try {
|
|
return (await readFile(filePath)).includes(Buffer.from(value));
|
|
} catch (error) {
|
|
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return false;
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async function directoryContainsValue(directory: string, value: string): Promise<boolean> {
|
|
let entries;
|
|
try {
|
|
entries = await readdir(directory, { withFileTypes: true });
|
|
} catch (error) {
|
|
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return false;
|
|
throw error;
|
|
}
|
|
for (const entry of entries) {
|
|
const entryPath = path.join(directory, entry.name);
|
|
if (entry.isDirectory()) {
|
|
if (await directoryContainsValue(entryPath, value)) return true;
|
|
} else if (entry.isFile() && await fileContainsValue(entryPath, value)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
async function inspectWindowsPiProcesses(hostToken: string): Promise<{
|
|
supported: boolean;
|
|
processes: InspectedPiProcess[];
|
|
}> {
|
|
if (process.platform !== 'win32') return { supported: false, processes: [] };
|
|
const script = `$parentPid = ${process.pid}; `
|
|
+ '$items = @(Get-CimInstance Win32_Process -Filter "ParentProcessId = $parentPid" '
|
|
+ '| Where-Object { $_.CommandLine -match "pi-runtime[\\\\/]dist[\\\\/]cli\\.js" } '
|
|
+ '| Select-Object ProcessId, CommandLine); '
|
|
+ '$items | ConvertTo-Json -Compress';
|
|
const { stdout } = await execFileAsync(
|
|
'powershell.exe',
|
|
['-NoProfile', '-NonInteractive', '-Command', script],
|
|
{ windowsHide: true, maxBuffer: 1024 * 1024 },
|
|
);
|
|
const text = stdout.trim();
|
|
const parsed = text ? JSON.parse(text) as unknown : [];
|
|
const items = Array.isArray(parsed) ? parsed : [parsed];
|
|
const processes = items.flatMap((item): InspectedPiProcess[] => {
|
|
if (!item || typeof item !== 'object' || Array.isArray(item)) return [];
|
|
const record = item as { ProcessId?: unknown; CommandLine?: unknown };
|
|
const processId = Number(record.ProcessId);
|
|
const commandLine = typeof record.CommandLine === 'string' ? record.CommandLine : '';
|
|
if (!Number.isSafeInteger(processId) || !commandLine) return [];
|
|
const role = commandLine.includes('--no-session') ? 'child' : 'parent';
|
|
return [{
|
|
processId,
|
|
role,
|
|
commandLineContainsHostToken: commandLine.includes(hostToken),
|
|
}];
|
|
});
|
|
return { supported: true, processes };
|
|
}
|
|
|
|
function proxyProviderAccount(baseUrl: string, modelId = PROOF_MODEL_ID): ProviderAccount {
|
|
return {
|
|
...providerAccount(baseUrl),
|
|
model: modelId,
|
|
label: 'Authenticated Host proxy release proof',
|
|
metadata: {
|
|
worksSquareCredentialMode: 'works_square_ai_gateway_proxy',
|
|
customModels: [modelId],
|
|
},
|
|
};
|
|
}
|
|
|
|
async function proxyCompositionStatus(
|
|
run: ProxyCompositionRun,
|
|
): Promise<PiReleaseProxyCompositionStatus> {
|
|
const diagnostics = run.composition.runtime.getDiagnostics();
|
|
const processInspection = await inspectWindowsPiProcesses(run.hostToken);
|
|
const modelsFile = path.join(
|
|
path.dirname(run.projectPath),
|
|
'coding-runtime',
|
|
'pi',
|
|
'config',
|
|
'models.json',
|
|
);
|
|
const logsDirectory = path.join(path.dirname(run.projectPath), 'logs');
|
|
const [modelsContainToken, logsContainToken] = await Promise.all([
|
|
fileContainsValue(modelsFile, run.hostToken),
|
|
directoryContainsValue(logsDirectory, run.hostToken),
|
|
]);
|
|
const providerRequests = providerRequestCounts(run.provider);
|
|
const providerCompatibility = {
|
|
roleContract: run.provider.roleContract ?? {
|
|
developerRejected: false,
|
|
systemAccepted: false,
|
|
},
|
|
requestRoles: run.provider.requests.flatMap(({ firstMessageRole }) => (
|
|
firstMessageRole ? [firstMessageRole] : []
|
|
)),
|
|
developerRoleRequests: run.provider.requests.filter(({ firstMessageRole }) => (
|
|
firstMessageRole === 'developer'
|
|
)).length,
|
|
systemRoleRequests: run.provider.requests.filter(({ firstMessageRole }) => (
|
|
firstMessageRole === 'system'
|
|
)).length,
|
|
controlledDefiniteRejections: run.submissionInjection.definiteRejections,
|
|
};
|
|
const parent = processInspection.processes
|
|
.filter(({ role }) => role === 'parent')
|
|
.map(({ processId }) => processId)
|
|
.sort((left, right) => left - right);
|
|
const child = processInspection.processes
|
|
.filter(({ role }) => role === 'child')
|
|
.map(({ processId }) => processId)
|
|
.sort((left, right) => left - right);
|
|
const conversations = await run.composition.conversations.listConversations(run.projectId);
|
|
if (!run.sourceConversationId && conversations.length === 1) {
|
|
run.sourceConversationId = conversations[0]?.id;
|
|
}
|
|
return {
|
|
conversationCount: conversations.length,
|
|
providerRequests,
|
|
activeProviderRequests: run.provider.activeCounts(),
|
|
workers: diagnostics.workers,
|
|
processes: { supported: processInspection.supported, parent, child },
|
|
currentHostTokenAvailable: run.hostToken.length > 0,
|
|
tokenSafety: {
|
|
argv: processInspection.processes.every(({ commandLineContainsHostToken }) => !commandLineContainsHostToken),
|
|
modelsJson: !modelsContainToken,
|
|
logs: !logsContainToken,
|
|
diagnostics: !JSON.stringify(diagnostics).includes(run.hostToken),
|
|
},
|
|
providerCompatibility,
|
|
realTurnVerified: false,
|
|
};
|
|
}
|
|
|
|
function timelineForTurn(
|
|
composition: RealProofComposition,
|
|
conversationId: string,
|
|
runId: string,
|
|
generation: number,
|
|
cold: boolean,
|
|
): PiReleaseManagedTurnProof {
|
|
const conversationRef = shortRef(conversationId);
|
|
const runRef = shortRef(runId);
|
|
const runTelemetry = composition.telemetry.filter((event) => (
|
|
event.conversationRef === conversationRef
|
|
&& event.workerGeneration === generation
|
|
&& event.cold === cold
|
|
));
|
|
const byMilestone = new Map(runTelemetry.map((event) => [event.milestone, event]));
|
|
const observed = composition.observedEvents.filter((event) => (
|
|
event.conversationId === conversationId && event.generation === generation
|
|
));
|
|
const agentStart = observed.find(({ event }) => event.type === 'agent_start');
|
|
const agentSettled = [...observed].reverse().find(({ event }) => event.type === 'agent_settled');
|
|
const firstProviderEvent = agentStart && observed.find((entry) => (
|
|
entry.at >= agentStart.at && isAssistantMessageStart(entry.event)
|
|
));
|
|
const promptAccepted = byMilestone.get('prompt.accepted');
|
|
if (!agentStart || !agentSettled || !firstProviderEvent || !promptAccepted) {
|
|
throw new Error(`Managed ${cold ? 'cold' : 'warm'} turn did not expose provider lifecycle events`);
|
|
}
|
|
const proofEvents = new Map<ProofMilestone, {
|
|
milestone: ProofMilestone;
|
|
source: ProofMilestoneSource;
|
|
durationMs: number;
|
|
at: number;
|
|
}>();
|
|
for (const event of runTelemetry) {
|
|
proofEvents.set(event.milestone, {
|
|
milestone: event.milestone,
|
|
source: 'main.telemetry',
|
|
durationMs: event.durationMs,
|
|
at: event.at,
|
|
});
|
|
}
|
|
proofEvents.set('agent.start', {
|
|
milestone: 'agent.start',
|
|
source: 'pi.agent_start',
|
|
durationMs: Math.max(0, agentStart.at - promptAccepted.at),
|
|
at: agentStart.at,
|
|
});
|
|
proofEvents.set('provider.first_event', {
|
|
milestone: 'provider.first_event',
|
|
source: 'pi.assistant_message_start',
|
|
durationMs: Math.max(0, firstProviderEvent.at - agentStart.at),
|
|
at: firstProviderEvent.at,
|
|
});
|
|
const milestones = EXPECTED_TURN_MILESTONES.map((milestone) => proofEvents.get(milestone));
|
|
if (milestones.some((event) => event === undefined)) {
|
|
const present = [...proofEvents.keys()].join(',');
|
|
throw new Error(`Managed ${cold ? 'cold' : 'warm'} timeline is incomplete: ${present}`);
|
|
}
|
|
return {
|
|
runRef,
|
|
cold,
|
|
workerGeneration: generation,
|
|
milestones: milestones as PiReleaseManagedTurnProof['milestones'],
|
|
};
|
|
}
|
|
|
|
async function runManagedTurn(
|
|
composition: RealProofComposition,
|
|
project: ProofProject,
|
|
runId: string,
|
|
expectedCold: boolean,
|
|
): Promise<PiReleaseManagedTurnProof> {
|
|
const settled = new Promise<number>((resolve) => {
|
|
const unsubscribe = composition.pool.subscribe((event: PiWorkerPoolEvent) => {
|
|
if (event.type !== 'worker.event'
|
|
|| event.conversationId !== project.input.conversationId
|
|
|| event.event.type !== 'agent_settled') return;
|
|
unsubscribe();
|
|
resolve(event.generation);
|
|
});
|
|
});
|
|
const ticket = composition.pool.startTopLevel({
|
|
conversationId: project.input.conversationId,
|
|
runId,
|
|
command: { type: 'prompt', message: `Run ${runId} through the final packaged Pi composition.` },
|
|
});
|
|
const accepted = await ticket.accepted;
|
|
if (!accepted.success) throw new Error(`Managed ${expectedCold ? 'cold' : 'warm'} prompt was rejected`);
|
|
const generation = await settled;
|
|
return timelineForTurn(
|
|
composition,
|
|
project.input.conversationId,
|
|
runId,
|
|
generation,
|
|
expectedCold,
|
|
);
|
|
}
|
|
|
|
function pressureSnapshot(
|
|
composition: RealProofComposition,
|
|
provider: LocalProofProvider,
|
|
leases: PiProjectWriteLeaseCoordinator,
|
|
): PiReleasePressureSnapshot {
|
|
const subagents = composition.scheduler.getDiagnostics();
|
|
const parentProcessIds = processIds(composition.tracked, 'parent', true);
|
|
const childProcessIds = processIds(composition.tracked, 'child', true);
|
|
return {
|
|
parentWorkers: composition.pool.getDiagnostics().workers
|
|
.filter(({ state }) => state === 'running').length,
|
|
childWorkers: subagents.activeChildPermits,
|
|
parentProcessIds,
|
|
childProcessIds,
|
|
liveProcessIds: [...parentProcessIds, ...childProcessIds].sort((left, right) => left - right),
|
|
providerRequests: provider.activeCounts(),
|
|
processBudget: {
|
|
active: composition.processBudget.activeCount,
|
|
waiting: composition.processBudget.waitingCount,
|
|
},
|
|
childPermits: {
|
|
active: subagents.activeChildPermits,
|
|
waiting: subagents.waitingChildPermits,
|
|
},
|
|
dispatches: {
|
|
active: subagents.activeDispatches,
|
|
parents: subagents.activeParents,
|
|
},
|
|
writeLeases: { active: leases.activeCount, waiting: leases.waitingCount() },
|
|
};
|
|
}
|
|
|
|
async function startPressureRun(): Promise<PressureRun> {
|
|
const root = await mkdtemp(path.join(tmpdir(), 'makelore-pi-final-pressure-'));
|
|
const provider = await startLocalProofProvider('pressure');
|
|
const composition = await createRealProofComposition(root, provider, 4);
|
|
const writeLeases = new PiProjectWriteLeaseCoordinator();
|
|
const heldWriteLeases: PiProjectWriteLease[] = [];
|
|
const dispatches: Array<Promise<unknown>> = [];
|
|
try {
|
|
await Promise.all(composition.projects.map(async ({ input }) => await composition.pool.prepare(input)));
|
|
await Promise.all(composition.projects.map(async ({ input }, index) => {
|
|
const generation = composition.pool.getState(input.conversationId)?.generation;
|
|
if (!generation) throw new Error('Persistent Pi parent generation is unavailable');
|
|
const runId = `release-proof-parent-run-${index + 1}`;
|
|
await composition.extensionHost.bindRun(input.conversationId, generation, runId);
|
|
const ticket = composition.pool.startTopLevel({
|
|
conversationId: input.conversationId,
|
|
runId,
|
|
command: { type: 'prompt', message: 'Hold a real persistent Pi parent for release pressure.' },
|
|
});
|
|
const accepted = await ticket.accepted;
|
|
if (!accepted.success) throw new Error('Persistent Pi parent pressure prompt was rejected');
|
|
}));
|
|
await waitFor(
|
|
() => provider.activeCounts().parent === 4
|
|
&& processIds(composition.tracked, 'parent', true).length === 4,
|
|
'Four real persistent Pi parents did not become active',
|
|
);
|
|
for (let index = 0; index < composition.projects.length; index += 1) {
|
|
const project = composition.projects[index] as ProofProject;
|
|
const generation = composition.pool.getState(project.input.conversationId)?.generation;
|
|
if (!generation) throw new Error('Persistent Pi parent disappeared before child dispatch');
|
|
const runId = `release-proof-parent-run-${index + 1}`;
|
|
heldWriteLeases.push(await writeLeases.acquire(
|
|
`release-proof-write-project-${index + 1}`,
|
|
`release-proof-holder-${index + 1}`,
|
|
));
|
|
dispatches.push(composition.scheduler.dispatch({
|
|
conversationId: project.input.conversationId,
|
|
workerGeneration: generation,
|
|
runId,
|
|
projectId: project.input.projectId,
|
|
request: {
|
|
mode: 'single',
|
|
tasks: [{
|
|
agentId: PROOF_AGENT_ID,
|
|
task: 'Hold a real ephemeral Pi child for release pressure.',
|
|
toolProfile: 'read-only',
|
|
}],
|
|
},
|
|
}));
|
|
}
|
|
try {
|
|
await waitFor(
|
|
() => composition.processBudget.activeCount === 8
|
|
&& composition.scheduler.getDiagnostics().activeChildPermits === 4
|
|
&& provider.activeCounts().child === 4
|
|
&& processIds(composition.tracked, 'child', true).length === 4,
|
|
'Four real ephemeral Pi children did not become active',
|
|
);
|
|
} catch (error) {
|
|
const childDiagnostics = composition.tracked
|
|
.filter(({ role }) => role === 'child')
|
|
.map(({ process }) => ({
|
|
pid: process.processId ?? null,
|
|
running: process.isRunning,
|
|
diagnostic: process.stderrDiagnostic,
|
|
}));
|
|
throw new Error(
|
|
`${error instanceof Error ? error.message : String(error)}: ${JSON.stringify({
|
|
snapshot: pressureSnapshot(composition, provider, writeLeases),
|
|
scheduler: composition.scheduler.getDiagnostics(),
|
|
providerRequests: providerRequestCounts(provider),
|
|
childDiagnostics,
|
|
})}`,
|
|
{ cause: error },
|
|
);
|
|
}
|
|
const active = pressureSnapshot(composition, provider, writeLeases);
|
|
if (active.parentProcessIds.length !== 4
|
|
|| active.childProcessIds.length !== 4
|
|
|| active.liveProcessIds.length !== 8) {
|
|
throw new Error(`Expected 4 real parent and 4 real child processes: ${JSON.stringify(active)}`);
|
|
}
|
|
return {
|
|
active,
|
|
finish: (() => {
|
|
let flight: Promise<PiReleasePressureSnapshot> | null = null;
|
|
const cleanup = async (injectFailureAt?: 'parents.settle') => {
|
|
const steps = [
|
|
{ name: 'provider.release', run: () => provider.releaseAll() },
|
|
{
|
|
name: 'dispatches.settle',
|
|
run: async () => {
|
|
const results = await Promise.allSettled(dispatches);
|
|
const failures = results.flatMap((result) => (
|
|
result.status === 'rejected' ? [result.reason] : []
|
|
));
|
|
if (failures.length > 0) {
|
|
throw new AggregateError(failures, 'PI release pressure dispatch failed');
|
|
}
|
|
},
|
|
},
|
|
{
|
|
name: 'parents.settle',
|
|
run: async () => await waitFor(
|
|
() => composition.pool.getDiagnostics().workers.every(({ state }) => state === 'idle'),
|
|
'Persistent Pi parents did not settle after pressure release',
|
|
),
|
|
},
|
|
{ name: 'scheduler.close', run: async () => await composition.scheduler.close() },
|
|
{ name: 'pool.shutdown', run: async () => await composition.pool.shutdown() },
|
|
{
|
|
name: 'leases.release',
|
|
run: () => {
|
|
for (const lease of heldWriteLeases.splice(0)) lease.release();
|
|
},
|
|
},
|
|
{ name: 'extension.close', run: async () => await composition.extensionHost.close() },
|
|
{ name: 'provider.close', run: async () => await provider.close() },
|
|
{
|
|
name: 'scratch.remove',
|
|
run: async () => await rm(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 }),
|
|
},
|
|
];
|
|
if (injectFailureAt) {
|
|
const target = steps.find(({ name }) => name === injectFailureAt);
|
|
if (!target) throw new Error(`Unknown PI release cleanup failure injection: ${injectFailureAt}`);
|
|
target.run = () => {
|
|
throw new Error(`Injected PI release cleanup failure at ${injectFailureAt}`);
|
|
};
|
|
}
|
|
await runPiReleasePressureCleanup(steps);
|
|
return pressureSnapshot(composition, provider, writeLeases);
|
|
};
|
|
return (options?: { injectFailureAt?: 'parents.settle' }) => {
|
|
if (!flight) {
|
|
flight = cleanup(options?.injectFailureAt).finally(() => {
|
|
flight = null;
|
|
});
|
|
}
|
|
return flight;
|
|
};
|
|
})(),
|
|
};
|
|
} catch (error) {
|
|
provider.releaseAll();
|
|
await composition.scheduler.close().catch(() => undefined);
|
|
await composition.pool.shutdown().catch(() => undefined);
|
|
for (const lease of heldWriteLeases.splice(0)) lease.release();
|
|
await composition.extensionHost.close().catch(() => undefined);
|
|
await provider.close().catch(() => undefined);
|
|
await rm(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
export async function runFinalAsarExtensionProof(): Promise<PiReleaseExtensionProof> {
|
|
const root = await mkdtemp(path.join(tmpdir(), 'makelore-pi-final-asar-extension-'));
|
|
const provider = await startLocalProofProvider('subagent');
|
|
const composition = await createRealProofComposition(root, provider, 1);
|
|
const project = composition.projects[0] as ProofProject;
|
|
let managedTurns: PiReleaseManagedTurnProof[];
|
|
let parentProcessIds: number[];
|
|
let childProcessIds: number[];
|
|
let parentToolNames: string[];
|
|
let childToolNames: string[];
|
|
let requestCounts: { parent: number; child: number };
|
|
try {
|
|
const prepared = await composition.pool.prepare(project.input);
|
|
await composition.extensionHost.bindRun(
|
|
project.input.conversationId,
|
|
prepared.generation,
|
|
'release-proof-cold-run',
|
|
);
|
|
const coldFlight = runManagedTurn(
|
|
composition,
|
|
project,
|
|
'release-proof-cold-run',
|
|
true,
|
|
);
|
|
await waitFor(
|
|
() => composition.scheduler.getDiagnostics().activeChildPermits === 1
|
|
&& composition.processBudget.activeCount === 2
|
|
&& provider.activeCounts().child === 1
|
|
&& processIds(composition.tracked, 'child', true).length === 1,
|
|
'Final packaged parent did not dispatch a real ephemeral Pi child',
|
|
);
|
|
parentProcessIds = processIds(composition.tracked, 'parent', false);
|
|
childProcessIds = processIds(composition.tracked, 'child', false);
|
|
parentToolNames = provider.requests.find(({ role }) => role === 'parent')?.toolNames ?? [];
|
|
childToolNames = provider.requests.find(({ role }) => role === 'child')?.toolNames ?? [];
|
|
provider.releaseChildren();
|
|
const cold = await coldFlight;
|
|
await composition.extensionHost.clearRun(
|
|
project.input.conversationId,
|
|
cold.workerGeneration,
|
|
'release-proof-cold-run',
|
|
);
|
|
|
|
composition.revisions.markResourcesStale();
|
|
const warm = await runManagedTurn(
|
|
composition,
|
|
project,
|
|
'release-proof-warm-run',
|
|
false,
|
|
);
|
|
managedTurns = [cold, warm];
|
|
parentProcessIds = processIds(composition.tracked, 'parent', false);
|
|
childProcessIds = processIds(composition.tracked, 'child', false);
|
|
requestCounts = providerRequestCounts(provider);
|
|
|
|
if (!parentToolNames.includes('subagent')) {
|
|
throw new Error('Final packaged Pi parent did not load the materialized subagent tool');
|
|
}
|
|
if (childToolNames.includes('subagent') || childToolNames.some((name) => (
|
|
['ask_user', 'agent_browser', 'game_asset_browser', 'game_asset_review', 'task_state', 'changed_file', 'runtime_context']
|
|
.includes(name)
|
|
))) {
|
|
throw new Error('Final packaged Pi child exposed parent-only product tools');
|
|
}
|
|
if (requestCounts.child !== 1
|
|
|| !provider.requests.some(({ role, hasToolResult: result }) => role === 'parent' && result)) {
|
|
throw new Error('Final packaged Pi subagent dispatch did not complete through the parent tool result');
|
|
}
|
|
} finally {
|
|
provider.releaseAll();
|
|
await composition.scheduler.close().catch(() => undefined);
|
|
await composition.pool.shutdown().catch(() => undefined);
|
|
await composition.extensionHost.close().catch(() => undefined);
|
|
await provider.close().catch(() => undefined);
|
|
}
|
|
const diagnostics = composition.scheduler.getDiagnostics();
|
|
const released = {
|
|
processBudget: {
|
|
active: composition.processBudget.activeCount,
|
|
waiting: composition.processBudget.waitingCount,
|
|
},
|
|
childPermits: {
|
|
active: diagnostics.activeChildPermits,
|
|
waiting: diagnostics.waitingChildPermits,
|
|
},
|
|
dispatches: {
|
|
active: diagnostics.activeDispatches,
|
|
parents: diagnostics.activeParents,
|
|
},
|
|
liveProcessIds: [
|
|
...processIds(composition.tracked, 'parent', true),
|
|
...processIds(composition.tracked, 'child', true),
|
|
].sort((left, right) => left - right),
|
|
};
|
|
await rm(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
return {
|
|
parentToolNames,
|
|
childToolNames,
|
|
parentProcessIds,
|
|
childProcessIds,
|
|
providerRequests: requestCounts,
|
|
subagentStatus: 'complete',
|
|
subagentSummary: 'REAL_CHILD_COMPLETE',
|
|
materializedExtension: 'makelore-runtime-v3.mjs',
|
|
providerFirstEventDelayMs: PROOF_PROVIDER_FIRST_EVENT_DELAY_MS,
|
|
managedTurns,
|
|
managedWorkerMilestones: composition.telemetry,
|
|
released,
|
|
};
|
|
}
|
|
|
|
async function cleanupProxyCompositionRun(run: ProxyCompositionRun): Promise<void> {
|
|
run.composition.conversations.acceptPrompt = run.originalAcceptPrompt;
|
|
run.provider.releaseAll();
|
|
await run.composition.projects.removeProject(run.projectId).catch(() => undefined);
|
|
await run.provider.close().catch(() => undefined);
|
|
clearWorksSquareAIGatewayCredential();
|
|
await getProviderService().deleteAccount(PROOF_ACCOUNT_ID).catch(() => undefined);
|
|
}
|
|
|
|
export async function startFinalAsarProxyCompositionProof(input: {
|
|
composition: CodingProductComposition;
|
|
projectPath: string;
|
|
hostProxyBaseUrl: string;
|
|
hostToken: string;
|
|
}): Promise<{
|
|
projectId: string;
|
|
agentId: string;
|
|
providerMode: 'works_square_ai_gateway_proxy';
|
|
currentHostTokenAvailable: true;
|
|
realTurnVerified: false;
|
|
}> {
|
|
if (proxyCompositionRun) throw new Error('PI proxy composition proof is already running');
|
|
const hostToken = input.hostToken.trim();
|
|
if (!hostToken) throw new Error('Current Main Host token is unavailable');
|
|
const provider = await startLocalProofProvider('submit-rejection');
|
|
let projectId: string | null = null;
|
|
try {
|
|
seedWorksSquareAIGatewayCredential({
|
|
accessToken: 'release-proof-upstream-only',
|
|
oneApiBaseUrl: provider.baseUrl,
|
|
});
|
|
const providerService = getProviderService();
|
|
await providerService.createAccount(proxyProviderAccount(
|
|
input.hostProxyBaseUrl,
|
|
PROXY_PROOF_MODEL_ID,
|
|
));
|
|
await providerService.setDefaultAccount(PROOF_ACCOUNT_ID);
|
|
const project = await input.composition.projects.createProject({ projectPath: input.projectPath });
|
|
projectId = project.project.id;
|
|
await createCodingProjectAgent(input.projectPath, {
|
|
id: PROOF_AGENT_ID,
|
|
avatarId: 'avatar-01',
|
|
roleName: 'Packaged proxy proof',
|
|
name: 'Packaged proxy proof agent',
|
|
model: {
|
|
accountId: PROOF_ACCOUNT_ID,
|
|
modelId: PROXY_PROOF_MODEL_ID,
|
|
thinkingLevel: 'high',
|
|
},
|
|
modelResolution: 'resolved',
|
|
responsibility: {
|
|
mission: 'Exercise the real packaged Main proxy composition',
|
|
owns: [],
|
|
boundaries: [],
|
|
collaborators: [],
|
|
principles: [],
|
|
},
|
|
prompt: 'Follow the controlled loopback Provider and complete its subagent request.',
|
|
skillIds: [],
|
|
});
|
|
const originalAcceptPrompt = input.composition.conversations.acceptPrompt
|
|
.bind(input.composition.conversations);
|
|
const submissionInjection = { definiteRejections: 0 };
|
|
let rejectNextSubmission = true;
|
|
input.composition.conversations.acceptPrompt = async (promptInput) => {
|
|
if (rejectNextSubmission) {
|
|
rejectNextSubmission = false;
|
|
submissionInjection.definiteRejections += 1;
|
|
throw new CodingConversationServiceError(
|
|
503,
|
|
'CODING_MODEL_UNAVAILABLE',
|
|
'Controlled packaged submission rejection',
|
|
);
|
|
}
|
|
return await originalAcceptPrompt(promptInput);
|
|
};
|
|
proxyCompositionRun = {
|
|
composition: input.composition,
|
|
provider,
|
|
projectId,
|
|
projectPath: input.projectPath,
|
|
hostToken,
|
|
originalAcceptPrompt,
|
|
submissionInjection,
|
|
};
|
|
return {
|
|
projectId,
|
|
agentId: PROOF_AGENT_ID,
|
|
providerMode: 'works_square_ai_gateway_proxy',
|
|
currentHostTokenAvailable: true,
|
|
realTurnVerified: false,
|
|
};
|
|
} catch (error) {
|
|
provider.releaseAll();
|
|
if (projectId) await input.composition.projects.removeProject(projectId).catch(() => undefined);
|
|
await provider.close().catch(() => undefined);
|
|
clearWorksSquareAIGatewayCredential();
|
|
await getProviderService().deleteAccount(PROOF_ACCOUNT_ID).catch(() => undefined);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
export async function getFinalAsarProxyCompositionStatus(): Promise<PiReleaseProxyCompositionStatus> {
|
|
const run = proxyCompositionRun;
|
|
if (!run) throw new Error('PI proxy composition proof is not running');
|
|
const status = await proxyCompositionStatus(run);
|
|
if (status.activeProviderRequests.child > 0
|
|
&& status.processes.child.length > 0
|
|
&& status.processes.parent.length > 0) {
|
|
run.activeStatus = status;
|
|
}
|
|
if (status.conversationCount === 1
|
|
&& status.activeProviderRequests.parent === 0
|
|
&& status.activeProviderRequests.child === 0
|
|
&& status.providerCompatibility.systemRoleRequests >= 2) {
|
|
run.settledStatus = status;
|
|
}
|
|
return status;
|
|
}
|
|
|
|
export function releaseFinalAsarProxyCompositionChild(): void {
|
|
const run = proxyCompositionRun;
|
|
if (!run) throw new Error('PI proxy composition proof is not running');
|
|
run.provider.releaseChildren();
|
|
}
|
|
|
|
export async function finishFinalAsarProxyCompositionProof(): Promise<PiReleaseProxyCompositionProof> {
|
|
const run = proxyCompositionRun;
|
|
if (!run) throw new Error('PI proxy composition proof is not running');
|
|
try {
|
|
const conversations = await run.composition.conversations.listConversations(run.projectId);
|
|
if (conversations.length !== 2 || !run.sourceConversationId) {
|
|
throw new Error(`Expected source and forked proxy Conversations, received ${conversations.length}`);
|
|
}
|
|
const conversation = conversations.find(({ id }) => id === run.sourceConversationId);
|
|
const forked = conversations.find(({ id }) => id !== run.sourceConversationId);
|
|
if (!conversation || !forked) throw new Error('Proxy fork source or target Conversation is unavailable');
|
|
let snapshot = await run.composition.conversations.getSnapshot(conversation.id);
|
|
const deadline = Date.now() + 30_000;
|
|
while (Date.now() < deadline && (
|
|
snapshot.run.status !== 'idle'
|
|
|| !JSON.stringify(snapshot.nodes).includes('REAL_PARENT_COMPLETE')
|
|
)) {
|
|
await delay(20);
|
|
snapshot = await run.composition.conversations.getSnapshot(conversation.id);
|
|
}
|
|
const serializedNodes = JSON.stringify(snapshot.nodes);
|
|
if (snapshot.run.status !== 'idle' || !serializedNodes.includes('REAL_PARENT_COMPLETE')) {
|
|
throw new Error('First proxy Conversation did not settle with the controlled parent response');
|
|
}
|
|
const forkedSnapshot = await run.composition.conversations.getSnapshot(forked.id);
|
|
const sourceEntry = snapshot.nodes.find((node) => (
|
|
node.kind === 'message'
|
|
&& node.role === 'user'
|
|
&& node.blocks.some((block) => block.kind === 'text'
|
|
&& block.text.includes('Retry the packaged Main proxy Conversation'))
|
|
));
|
|
if (!sourceEntry || sourceEntry.kind !== 'message' || !sourceEntry.sourceEntryId) {
|
|
throw new Error('Proxy source user entry is not durable after the controlled turn');
|
|
}
|
|
const [binding, forkedBinding] = await Promise.all([
|
|
run.composition.projects.conversationStore(run.projectPath).get(conversation.id),
|
|
run.composition.projects.conversationStore(run.projectPath).get(forked.id),
|
|
]);
|
|
const finalStatus = await proxyCompositionStatus(run);
|
|
const activeStatus = run.activeStatus ?? finalStatus;
|
|
const settledStatus = run.settledStatus;
|
|
if (!settledStatus) throw new Error('Proxy source Conversation did not record a settled pre-fork baseline');
|
|
const currentHostTokenUsedBy: ProofWorkerRole[] = [
|
|
...(activeStatus.providerRequests.parent > 0 ? ['parent' as const] : []),
|
|
...(activeStatus.providerRequests.child > 0 ? ['child' as const] : []),
|
|
];
|
|
if (currentHostTokenUsedBy.join(',') !== 'parent,child') {
|
|
throw new Error('Authenticated Host proxy did not receive both parent and child Provider requests');
|
|
}
|
|
if (!binding?.piSessionId || !binding.sessionKey) {
|
|
throw new Error('First proxy Conversation did not persist its Pi session binding');
|
|
}
|
|
if (!forkedBinding?.piSessionId || !forkedBinding.sessionKey) {
|
|
throw new Error('Forked proxy Conversation did not persist its Pi session binding');
|
|
}
|
|
const distinctSessionBinding = binding.piSessionId !== forkedBinding.piSessionId
|
|
&& binding.sessionKey !== forkedBinding.sessionKey;
|
|
const targetHydratedBeforeSourceEntry = forkedSnapshot.nodes.every((node) => (
|
|
node.kind !== 'message' || node.sourceEntryId !== sourceEntry.sourceEntryId
|
|
)) && !JSON.stringify(forkedSnapshot.nodes).includes('REAL_PARENT_COMPLETE');
|
|
if (forked.agentId !== conversation.agentId
|
|
|| !distinctSessionBinding
|
|
|| forkedSnapshot.worker.status !== 'ready'
|
|
|| forkedSnapshot.run.status !== 'idle'
|
|
|| !targetHydratedBeforeSourceEntry) {
|
|
throw new Error(`Proxy user-entry fork proof failed: ${JSON.stringify({
|
|
sameAgent: forked.agentId === conversation.agentId,
|
|
distinctSessionBinding,
|
|
workerStatus: forkedSnapshot.worker.status,
|
|
runStatus: forkedSnapshot.run.status,
|
|
targetHydratedBeforeSourceEntry,
|
|
})}`);
|
|
}
|
|
if (!finalStatus.providerCompatibility.roleContract.developerRejected
|
|
|| !finalStatus.providerCompatibility.roleContract.systemAccepted
|
|
|| finalStatus.providerCompatibility.developerRoleRequests !== 0
|
|
|| finalStatus.providerCompatibility.systemRoleRequests < 2
|
|
|| finalStatus.providerCompatibility.controlledDefiniteRejections !== 1) {
|
|
throw new Error(
|
|
`Works Provider role compatibility proof failed: ${JSON.stringify(finalStatus.providerCompatibility)}`,
|
|
);
|
|
}
|
|
const tokenSafety = {
|
|
argv: activeStatus.tokenSafety.argv && finalStatus.tokenSafety.argv,
|
|
modelsJson: activeStatus.tokenSafety.modelsJson && finalStatus.tokenSafety.modelsJson,
|
|
logs: activeStatus.tokenSafety.logs && finalStatus.tokenSafety.logs,
|
|
diagnostics: activeStatus.tokenSafety.diagnostics && finalStatus.tokenSafety.diagnostics,
|
|
};
|
|
if (Object.values(tokenSafety).some((safe) => !safe)) {
|
|
throw new Error(`Current Main Host token escaped its credential boundary: ${JSON.stringify(tokenSafety)}`);
|
|
}
|
|
const proof: Omit<PiReleaseProxyCompositionProof, 'released'> = {
|
|
...finalStatus,
|
|
providerRequests: activeStatus.providerRequests,
|
|
activeProviderRequests: activeStatus.activeProviderRequests,
|
|
processes: activeStatus.processes,
|
|
tokenSafety,
|
|
providerMode: 'loopback-through-authenticated-host-proxy',
|
|
firstConversation: {
|
|
count: 1,
|
|
bindingEstablished: true,
|
|
workerStatus: snapshot.worker.status,
|
|
runStatus: snapshot.run.status,
|
|
userInputProjected: serializedNodes.includes('Retry the packaged Main proxy Conversation'),
|
|
parentResponseProjected: serializedNodes.includes('REAL_PARENT_COMPLETE'),
|
|
definiteRejectionObserved: finalStatus.providerCompatibility.controlledDefiniteRejections === 1,
|
|
retryAccepted: finalStatus.providerCompatibility.systemRoleRequests >= 2,
|
|
},
|
|
fork: {
|
|
conversationCount: conversations.length,
|
|
sameAgent: forked.agentId === conversation.agentId,
|
|
targetBindingEstablished: true,
|
|
distinctSessionBinding,
|
|
workerStatus: forkedSnapshot.worker.status,
|
|
runStatus: forkedSnapshot.run.status,
|
|
sourceEntryRole: 'user',
|
|
targetHydratedBeforeSourceEntry,
|
|
sourceUnchanged: serializedNodes.includes('Retry the packaged Main proxy Conversation')
|
|
&& serializedNodes.includes('REAL_PARENT_COMPLETE'),
|
|
promptReplayObserved: finalStatus.providerRequests.parent !== settledStatus.providerRequests.parent
|
|
|| finalStatus.providerRequests.child !== settledStatus.providerRequests.child,
|
|
},
|
|
currentHostTokenUsedBy,
|
|
};
|
|
await cleanupProxyCompositionRun(run);
|
|
proxyCompositionRun = null;
|
|
return {
|
|
...proof,
|
|
released: { workers: run.composition.runtime.getDiagnostics().workers.length },
|
|
};
|
|
} catch (error) {
|
|
await cleanupProxyCompositionRun(run);
|
|
proxyCompositionRun = null;
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
function resilienceRuntime(run: ResilienceCompositionRun): PiConversationRuntime {
|
|
if (!(run.composition.runtime instanceof PiConversationRuntime)) {
|
|
throw new Error('Packaged Main does not own the Pi Conversation runtime');
|
|
}
|
|
return run.composition.runtime;
|
|
}
|
|
|
|
async function resilienceBindingPreserved(
|
|
run: ResilienceCompositionRun,
|
|
conversationId: string,
|
|
expected: { piSessionId: string; sessionKey: string },
|
|
): Promise<boolean> {
|
|
const binding = await run.composition.projects.conversationStore(run.projectPath).get(conversationId);
|
|
return binding?.piSessionId === expected.piSessionId && binding.sessionKey === expected.sessionKey;
|
|
}
|
|
|
|
export async function getFinalAsarResilienceStatus(): Promise<PiReleaseResilienceStatus> {
|
|
const run = resilienceCompositionRun;
|
|
if (!run) throw new Error('PI resilience proof is not running');
|
|
const runtime = resilienceRuntime(run);
|
|
const [target, other, processInspection, targetBindingPreserved, otherBindingPreserved] = await Promise.all([
|
|
run.composition.conversations.getSnapshot(run.targetConversationId),
|
|
run.composition.conversations.getSnapshot(run.otherConversationId),
|
|
inspectWindowsPiProcesses(run.hostToken),
|
|
resilienceBindingPreserved(run, run.targetConversationId, run.targetBinding),
|
|
resilienceBindingPreserved(run, run.otherConversationId, run.otherBinding),
|
|
]);
|
|
const targetError = target.run.error ?? target.worker.error;
|
|
return {
|
|
target: {
|
|
conversationId: run.targetConversationId,
|
|
workerStatus: target.worker.status,
|
|
workerGeneration: target.cursor.workerGeneration,
|
|
runStatus: target.run.status,
|
|
errorCode: targetError?.code ?? null,
|
|
recoverable: targetError?.recoverable ?? false,
|
|
bindingPreserved: targetBindingPreserved,
|
|
contextCompaction: target.context.compaction,
|
|
completedCompactions: target.nodes.filter((node) => (
|
|
node.kind === 'compaction' && node.status === 'complete'
|
|
)).length,
|
|
},
|
|
other: {
|
|
conversationId: run.otherConversationId,
|
|
workerStatus: other.worker.status,
|
|
workerGeneration: other.cursor.workerGeneration,
|
|
runStatus: other.run.status,
|
|
bindingPreserved: otherBindingPreserved,
|
|
},
|
|
providerRequests: providerRequestCounts(run.provider),
|
|
activeProviderRequests: run.provider.activeCounts(),
|
|
resources: runtime.getResilienceProofDiagnostics(),
|
|
processes: {
|
|
supported: processInspection.supported,
|
|
parent: processInspection.processes
|
|
.filter(({ role }) => role === 'parent')
|
|
.map(({ processId }) => processId)
|
|
.sort((left, right) => left - right),
|
|
child: processInspection.processes
|
|
.filter(({ role }) => role === 'child')
|
|
.map(({ processId }) => processId)
|
|
.sort((left, right) => left - right),
|
|
},
|
|
realTurnVerified: false,
|
|
};
|
|
}
|
|
|
|
async function waitForResilienceStatus(
|
|
predicate: (status: PiReleaseResilienceStatus) => boolean,
|
|
message: string,
|
|
): Promise<PiReleaseResilienceStatus> {
|
|
const deadline = Date.now() + 30_000;
|
|
let latest: PiReleaseResilienceStatus | null = null;
|
|
while (Date.now() < deadline) {
|
|
latest = await getFinalAsarResilienceStatus();
|
|
if (predicate(latest)) return latest;
|
|
await delay(20);
|
|
}
|
|
throw new Error(`${message}: ${JSON.stringify(latest)}`);
|
|
}
|
|
|
|
export async function startFinalAsarResilienceProof(input: {
|
|
composition: CodingProductComposition;
|
|
projectPath: string;
|
|
userDataDir: string;
|
|
hostProxyBaseUrl: string;
|
|
hostToken: string;
|
|
}): Promise<{
|
|
projectId: string;
|
|
targetConversationId: string;
|
|
otherConversationId: string;
|
|
bindingsEstablished: true;
|
|
otherRunAccepted: true;
|
|
realTurnVerified: false;
|
|
}> {
|
|
if (resilienceCompositionRun) throw new Error('PI resilience proof is already running');
|
|
const hostToken = input.hostToken.trim();
|
|
if (!hostToken) throw new Error('Current Main Host token is unavailable');
|
|
const provider = await startLocalProofProvider('resilience');
|
|
let projectId: string | null = null;
|
|
try {
|
|
seedWorksSquareAIGatewayCredential({
|
|
accessToken: 'release-proof-upstream-only',
|
|
oneApiBaseUrl: provider.baseUrl,
|
|
});
|
|
const providerService = getProviderService();
|
|
await providerService.createAccount(proxyProviderAccount(input.hostProxyBaseUrl));
|
|
await providerService.setDefaultAccount(PROOF_ACCOUNT_ID);
|
|
const managedPaths = await ensurePiManagedPaths(input.userDataDir);
|
|
await writeFile(path.join(managedPaths.configDir, 'settings.json'), JSON.stringify({
|
|
compaction: {
|
|
reserveTokens: 128,
|
|
keepRecentTokens: 1,
|
|
},
|
|
}, null, 2), 'utf8');
|
|
const project = await input.composition.projects.createProject({ projectPath: input.projectPath });
|
|
projectId = project.project.id;
|
|
await createCodingProjectAgent(input.projectPath, {
|
|
id: PROOF_AGENT_ID,
|
|
avatarId: 'avatar-01',
|
|
roleName: 'Resilience proof',
|
|
name: 'Resilience proof',
|
|
model: {
|
|
accountId: PROOF_ACCOUNT_ID,
|
|
modelId: PROOF_MODEL_ID,
|
|
thinkingLevel: 'medium',
|
|
},
|
|
modelResolution: 'resolved',
|
|
responsibility: {
|
|
mission: 'Exercise packaged worker failure convergence',
|
|
owns: [],
|
|
boundaries: [],
|
|
collaborators: [],
|
|
principles: [],
|
|
},
|
|
prompt: 'Follow the controlled loopback resilience qualification Provider.',
|
|
skillIds: [],
|
|
});
|
|
const other = await input.composition.conversations.createConversation({
|
|
projectId,
|
|
agentId: PROOF_AGENT_ID,
|
|
title: 'Resilience isolation control',
|
|
});
|
|
const target = await input.composition.conversations.createConversation({
|
|
projectId,
|
|
agentId: PROOF_AGENT_ID,
|
|
title: 'Resilience fault target',
|
|
});
|
|
await Promise.all([
|
|
input.composition.conversations.getSnapshot(other.id),
|
|
input.composition.conversations.getSnapshot(target.id),
|
|
]);
|
|
const store = input.composition.projects.conversationStore(input.projectPath);
|
|
const [targetStored, otherStored] = await Promise.all([store.get(target.id), store.get(other.id)]);
|
|
if (!targetStored?.piSessionId || !targetStored.sessionKey
|
|
|| !otherStored?.piSessionId || !otherStored.sessionKey) {
|
|
throw new Error('Resilience proof did not establish both Pi session bindings');
|
|
}
|
|
resilienceCompositionRun = {
|
|
composition: input.composition,
|
|
provider,
|
|
projectId,
|
|
projectPath: input.projectPath,
|
|
targetConversationId: target.id,
|
|
otherConversationId: other.id,
|
|
targetBinding: { piSessionId: targetStored.piSessionId, sessionKey: targetStored.sessionKey },
|
|
otherBinding: { piSessionId: otherStored.piSessionId, sessionKey: otherStored.sessionKey },
|
|
hostToken,
|
|
};
|
|
const accepted = await input.composition.conversations.acceptPrompt({
|
|
conversationId: other.id,
|
|
clientRequestId: 'release-proof-other-active',
|
|
mode: 'prompt',
|
|
text: 'RESILIENCE_OTHER_ACTIVE',
|
|
attachments: [],
|
|
});
|
|
if (!accepted.accepted) throw new Error('Resilience isolation run was not accepted');
|
|
await waitForResilienceStatus(
|
|
(status) => status.other.runStatus === 'running'
|
|
&& status.activeProviderRequests.parent === 1,
|
|
'Resilience isolation run did not become active',
|
|
);
|
|
return {
|
|
projectId,
|
|
targetConversationId: target.id,
|
|
otherConversationId: other.id,
|
|
bindingsEstablished: true,
|
|
otherRunAccepted: true,
|
|
realTurnVerified: false,
|
|
};
|
|
} catch (error) {
|
|
provider.releaseAll();
|
|
if (projectId) await input.composition.projects.removeProject(projectId).catch(() => undefined);
|
|
await provider.close().catch(() => undefined);
|
|
clearWorksSquareAIGatewayCredential();
|
|
await getProviderService().deleteAccount(PROOF_ACCOUNT_ID).catch(() => undefined);
|
|
resilienceCompositionRun = null;
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
export async function injectFinalAsarResilienceFailure(
|
|
failure: 'unexpected_exit' | 'protocol_invalidation',
|
|
): Promise<{ generation: number; terminalizationMs: number; status: PiReleaseResilienceStatus }> {
|
|
const run = resilienceCompositionRun;
|
|
if (!run) throw new Error('PI resilience proof is not running');
|
|
const startedAt = Date.now();
|
|
const injected = await resilienceRuntime(run).injectWorkerFailureForProof(
|
|
run.targetConversationId,
|
|
failure,
|
|
);
|
|
const deadline = Date.now() + 2_000;
|
|
let target = await run.composition.conversations.getSnapshot(run.targetConversationId);
|
|
while (Date.now() < deadline && (
|
|
target.worker.status !== 'error'
|
|
|| target.cursor.workerGeneration !== injected.generation
|
|
)) {
|
|
await delay(10);
|
|
target = await run.composition.conversations.getSnapshot(run.targetConversationId);
|
|
}
|
|
const terminalizationMs = Date.now() - startedAt;
|
|
if (target.worker.status !== 'error'
|
|
|| target.cursor.workerGeneration !== injected.generation
|
|
|| terminalizationMs > 2_000) {
|
|
throw new Error(`Injected Pi worker failure exceeded the 2s convergence bound: ${terminalizationMs}ms`);
|
|
}
|
|
const status = await getFinalAsarResilienceStatus();
|
|
return { ...injected, terminalizationMs, status };
|
|
}
|
|
|
|
export async function abortFinalAsarResilienceTarget(): Promise<PiReleaseResilienceStatus> {
|
|
const run = resilienceCompositionRun;
|
|
if (!run) throw new Error('PI resilience proof is not running');
|
|
await run.composition.conversations.abort(run.targetConversationId);
|
|
return await getFinalAsarResilienceStatus();
|
|
}
|
|
|
|
export function releaseFinalAsarResilienceParents(): void {
|
|
const run = resilienceCompositionRun;
|
|
if (!run) throw new Error('PI resilience proof is not running');
|
|
run.provider.releaseParents();
|
|
}
|
|
|
|
export function armFinalAsarResilienceCompactDelay(): { delayMs: number } {
|
|
const run = resilienceCompositionRun;
|
|
if (!run) throw new Error('PI resilience proof is not running');
|
|
run.provider.armDelayedCompaction();
|
|
resilienceRuntime(run).delayNextTopLevelConfirmationForProof(
|
|
run.targetConversationId,
|
|
'compact',
|
|
PROOF_MUTATION_CONFIRMATION_DELAY_MS,
|
|
);
|
|
return { delayMs: PROOF_MUTATION_CONFIRMATION_DELAY_MS };
|
|
}
|
|
|
|
export function armFinalAsarResiliencePromptDelay(): { delayMs: number } {
|
|
const run = resilienceCompositionRun;
|
|
if (!run) throw new Error('PI resilience proof is not running');
|
|
run.provider.armDelayedPrompt();
|
|
resilienceRuntime(run).delayNextTopLevelConfirmationForProof(
|
|
run.targetConversationId,
|
|
'prompt',
|
|
PROOF_MUTATION_CONFIRMATION_DELAY_MS,
|
|
);
|
|
return { delayMs: PROOF_MUTATION_CONFIRMATION_DELAY_MS };
|
|
}
|
|
|
|
export async function restartFinalAsarResilienceOther(): Promise<PiReleaseResilienceStatus> {
|
|
const run = resilienceCompositionRun;
|
|
if (!run) throw new Error('PI resilience proof is not running');
|
|
const accepted = await run.composition.conversations.acceptPrompt({
|
|
conversationId: run.otherConversationId,
|
|
clientRequestId: 'release-proof-other-active-restarted',
|
|
mode: 'prompt',
|
|
text: 'RESILIENCE_OTHER_ACTIVE_RESTARTED',
|
|
attachments: [],
|
|
});
|
|
if (!accepted.accepted) throw new Error('Resilience isolation restart was not accepted');
|
|
return await waitForResilienceStatus(
|
|
(status) => status.other.runStatus === 'running'
|
|
&& status.activeProviderRequests.parent === 1,
|
|
'Resilience isolation restart did not become active',
|
|
);
|
|
}
|
|
|
|
export async function getFinalAsarResilienceIdleStatus(): Promise<PiReleaseResilienceIdleStatus> {
|
|
const run = resilienceCompositionRun;
|
|
if (!run) throw new Error('PI resilience proof is not running');
|
|
const runtime = resilienceRuntime(run);
|
|
const processInspection = await inspectWindowsPiProcesses(run.hostToken);
|
|
const lifecycleLogs = getRecentLogs().filter((line) => (
|
|
line.includes('[PiWorkerLifecycle]')
|
|
&& (line.includes(run.targetConversationId) || line.includes(run.otherConversationId))
|
|
));
|
|
return {
|
|
workers: runtime.getDiagnostics().workers,
|
|
resources: runtime.getResilienceProofDiagnostics(),
|
|
processes: {
|
|
supported: processInspection.supported,
|
|
parent: processInspection.processes
|
|
.filter(({ role }) => role === 'parent')
|
|
.map(({ processId }) => processId)
|
|
.sort((left, right) => left - right),
|
|
child: processInspection.processes
|
|
.filter(({ role }) => role === 'child')
|
|
.map(({ processId }) => processId)
|
|
.sort((left, right) => left - right),
|
|
},
|
|
backgroundSleepReasoned: lifecycleLogs.some((line) => (
|
|
line.includes('"classification": "intentional_stop"')
|
|
&& line.includes('"reason": "background_sleep"')
|
|
)),
|
|
realTurnVerified: false,
|
|
};
|
|
}
|
|
|
|
export async function disposeFinalAsarResilienceTarget(): Promise<PiReleaseIntentionalDisposeProof> {
|
|
const run = resilienceCompositionRun;
|
|
if (!run) throw new Error('PI resilience proof is not running');
|
|
const runtime = resilienceRuntime(run);
|
|
const beforeRequests = providerRequestCounts(run.provider);
|
|
let errorCode: string | null = null;
|
|
let recoverable = false;
|
|
const unsubscribe = runtime.subscribe((envelope: ConversationPatchEnvelope) => {
|
|
if (envelope.conversationId !== run.targetConversationId
|
|
|| envelope.patch.op !== 'run.state'
|
|
|| envelope.patch.run.status !== 'error') return;
|
|
errorCode = envelope.patch.run.error?.code ?? null;
|
|
recoverable = envelope.patch.run.error?.recoverable ?? false;
|
|
});
|
|
try {
|
|
await runtime.dispose(run.targetConversationId, 'test_injection');
|
|
} finally {
|
|
unsubscribe();
|
|
}
|
|
const [other, targetBindingPreserved, otherBindingPreserved] = await Promise.all([
|
|
run.composition.conversations.getSnapshot(run.otherConversationId),
|
|
resilienceBindingPreserved(run, run.targetConversationId, run.targetBinding),
|
|
resilienceBindingPreserved(run, run.otherConversationId, run.otherBinding),
|
|
]);
|
|
return {
|
|
terminal: { observed: errorCode !== null, errorCode, recoverable },
|
|
other: { runStatus: other.run.status, bindingPreserved: otherBindingPreserved },
|
|
targetBindingPreserved,
|
|
providerRequestsUnchanged: JSON.stringify(beforeRequests)
|
|
=== JSON.stringify(providerRequestCounts(run.provider)),
|
|
resources: runtime.getResilienceProofDiagnostics(),
|
|
realTurnVerified: false,
|
|
};
|
|
}
|
|
|
|
export async function finishFinalAsarResilienceProof(): Promise<PiReleaseResilienceProof> {
|
|
const run = resilienceCompositionRun;
|
|
if (!run) throw new Error('PI resilience proof is not running');
|
|
try {
|
|
const status = await getFinalAsarResilienceStatus();
|
|
run.provider.releaseAll();
|
|
await run.composition.projects.removeProject(run.projectId);
|
|
const processInspection = await inspectWindowsPiProcesses(run.hostToken);
|
|
const resources = resilienceRuntime(run).getResilienceProofDiagnostics();
|
|
const lifecycleLogs = getRecentLogs().filter((line) => (
|
|
line.includes('[PiWorkerLifecycle]')
|
|
&& (line.includes(run.targetConversationId) || line.includes(run.otherConversationId))
|
|
));
|
|
const stopLogs = lifecycleLogs.filter((line) => line.includes('"stage": "stop_requested"'));
|
|
const replacementLogs = lifecycleLogs.filter((line) => line.includes('worker.replacement_'));
|
|
const lifecycle = {
|
|
unexpectedExit: lifecycleLogs.some((line) => line.includes('"classification": "unexpected_exit"')),
|
|
protocolInvalidation: lifecycleLogs.some((line) => line.includes('"classification": "protocol_invalidation"')),
|
|
intentionalStop: lifecycleLogs.some((line) => line.includes('"classification": "intentional_stop"')),
|
|
everyStopHasReason: stopLogs.length > 0 && stopLogs.every((line) => line.includes('"reason":')),
|
|
everyReplacementHasReason: replacementLogs.every((line) => line.includes('"reason":')),
|
|
diagnosticRedacted: lifecycleLogs.some((line) => line.includes('[REDACTED]'))
|
|
&& lifecycleLogs.every((line) => !line.includes('packaged-proof-secret')),
|
|
promptFree: lifecycleLogs.every((line) => !line.includes('RESILIENCE_')),
|
|
projectPathFree: lifecycleLogs.every((line) => !line.includes(run.projectPath)),
|
|
};
|
|
if (Object.values(lifecycle).some((value) => value !== true)) {
|
|
throw new Error(`Packaged Pi lifecycle evidence is incomplete: ${JSON.stringify(lifecycle)}`);
|
|
}
|
|
const released = {
|
|
workers: run.composition.runtime.getDiagnostics().workers.length,
|
|
processes: processInspection.processes.length,
|
|
resources,
|
|
};
|
|
if (released.workers !== 0
|
|
|| released.processes !== 0
|
|
|| released.resources.pool.processBudget.active !== 0
|
|
|| released.resources.pool.processBudget.waiting !== 0
|
|
|| released.resources.pool.runs.active !== 0
|
|
|| released.resources.pool.runs.waiting !== 0
|
|
|| released.resources.backgroundLeases.active !== 0
|
|
|| released.resources.subagents?.activeChildPermits !== 0
|
|
|| released.resources.subagents?.waitingChildPermits !== 0
|
|
|| released.resources.subagents?.activeDispatches !== 0
|
|
|| released.resources.subagents?.activeParents !== 0
|
|
|| released.resources.extension?.registrations.parent !== 0
|
|
|| released.resources.extension?.registrations.child !== 0
|
|
|| released.resources.extension?.writeLeases.active !== 0
|
|
|| released.resources.extension?.writeLeases.waiting !== 0) {
|
|
throw new Error(`Packaged Pi resources were not fully released: ${JSON.stringify(released)}`);
|
|
}
|
|
await run.provider.close();
|
|
clearWorksSquareAIGatewayCredential();
|
|
await getProviderService().deleteAccount(PROOF_ACCOUNT_ID).catch(() => undefined);
|
|
resilienceCompositionRun = null;
|
|
return { ...status, lifecycle, released, realTurnVerified: false };
|
|
} catch (error) {
|
|
run.provider.releaseAll();
|
|
await run.composition.projects.removeProject(run.projectId).catch(() => undefined);
|
|
await run.provider.close().catch(() => undefined);
|
|
clearWorksSquareAIGatewayCredential();
|
|
await getProviderService().deleteAccount(PROOF_ACCOUNT_ID).catch(() => undefined);
|
|
resilienceCompositionRun = null;
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
export async function startFinalAsarPressureProof(): Promise<PiReleasePressureSnapshot> {
|
|
if (pressureRun) throw new Error('PI release pressure proof is already running');
|
|
pressureRun = await startPressureRun();
|
|
return pressureRun.active;
|
|
}
|
|
|
|
export async function finishFinalAsarPressureProof(
|
|
options?: { injectFailureAt?: 'parents.settle' },
|
|
): Promise<PiReleasePressureSnapshot> {
|
|
const current = pressureRun;
|
|
if (!current) throw new Error('PI release pressure proof is not running');
|
|
const released = await current.finish(options);
|
|
if (pressureRun === current) pressureRun = null;
|
|
return released;
|
|
}
|