Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

22.4 KB
Raw Blame
// Shared test fixture builder for the public-retrieval slice.
// Uses the exact plan/lease/consumption/checkpoint shapes from the
// original work-capability-operational-v2.test.mjs custody() function.
import { createHash } from "node:crypto";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { compileCapabilityOperationalV2PublicRetrieval } from "../deploy/work-controller/capability-controller.mjs";
import { capabilityPackSha256 } from "../deploy/work-controller/capability-packs.mjs";
import { capabilityToolQueueV2ConfigBoundary } from "../deploy/work-controller/capability-tool-queue-v2.mjs";
import { canonical } from "../scripts/lib/work-contract.mjs";

const compilerPath = fileURLToPath(new URL("../deploy/work-controller/capability-controller.mjs", import.meta.url));
const compilerSha256 = sha(readFileSync(compilerPath));

export function sha(value) {
  return createHash("sha256").update(typeof value === "string" || Buffer.isBuffer(value) ? value : canonical(value)).digest("hex");
}

function digest(seed) {
  return sha(typeof seed === "string" ? seed : JSON.stringify(seed));
}

const packBoundary = "Admission-only signed broad-tools capability declaration v2. The declaration itself is disabled and grants no tool call, workspace, data, network, external-effect, credential, egress, or completion authority.";
const boundary = {
  plan: "Private immutable Deep Work plan. It authorizes no execution by itself and contains no credential, host path, provider secret, merge, deployment, publication, purchase, production, or external-effect authority.",
  lease: "This exact, expiring, single-use lease grants broad autonomy only inside the disposable job boundary. Pixel retains all authority over scope expansion, network brokers, credentials, external effects, acceptance criteria, merge, deployment, publication, purchase, and policy.",
  consumption: "Private immutable tombstone proving one lease was consumed once. It grants no replay, retry, scope expansion, credential, network, host, merge, deployment, publication, purchase, or external-effect authority.",
  checkpoint: "Private hash-bound recovery record. Contains only immutable digests, bounded counters, and transition facts. Cannot authorize or replay actions.",
  policy: "Admission-only controller allowlist for exact signed v2 capability declarations. Policy alone grants no execution, credential, network, external-effect, scope-expansion, or completion authority; v2 runtime is not enabled.",
};
const budgets = { maxRuntimeSeconds: 3600, maxIterations: 20, maxToolCalls: 2000, maxConcurrentSubagents: 4, maxModelRequests: 200, maxInputTokens: 1000000, maxOutputTokens: 200000, maxCpuCores: 4, maxMemoryMiB: 8192, maxDiskBytes: 10737418240, maxArtifactBytes: 1073741824, maxNetworkBytes: 104857600, maxFailures: 5, noProgressLimit: 3 };
const authority = { hostAccess: false, ambientCredentials: false, arbitraryNetwork: false, externalEffects: false, merge: false, deploy: false, publish: false, purchase: false, policyMutation: false, acceptanceCriteriaMutation: false, leaseExpansion: false };

// Full tool binding for brokered-network / public-api
const retrievalBinding = {
  targetClass: "public-api",
  scope: { mode: "public", endpoints: ["https://web-courier.local:8080/search"], destinations: [] },
  egress: { mode: "none", destinations: [] },
  credentials: { refs: [] },
  idempotency: { required: false, keys: [] },
  approval: { required: false, mode: "operator-approval" },
  inputClassification: "public",
  outputClassification: "public",
  budgets: { perRun: { maxCalls: 1, maxBytes: 131072, maxDurationMs: 120000 }, cumulative: { maxCalls: 1, maxBytes: 131072, maxDurationMs: 120000 } },
  receipt: { required: true, form: "content-free" },
  neverEgress: ["localNotes"],
};

export function v2RetrievalPack(overrides = {}) {
  const inputSchema = { type: "object", additionalProperties: false, required: ["query"], properties: { query: { type: "string" }, localNotes: { type: "string" } } };
  const outputSchema = { type: "object", additionalProperties: false, required: ["content"], properties: { content: { type: "array" } } };
  const pack = Object.freeze({
    $schema: "https://osmantic.com/pixel/schemas/work-capability-pack-v2.schema.json",
    schemaVersion: 2,
    kind: "deep-work-capability-pack",
    id: "fixture-retrieval",
    version: "1.0.0",
    name: "Fixture retrieval",
    description: "Deterministic bounded retrieval tool.",
    protocol: { name: "mcp", version: "2026-07-28", transport: "stdio-newline-jsonrpc" },
    adapter: {
      imageRef: `ghcr.io/osmantic/fixture-retrieval@sha256:${digest("r")}`,
      imageDigest: `sha256:${digest("r")}`,
      imageId: `sha256:${digest("s")}`,
      os: "linux", architecture: "amd64",
      executablePath: "/opt/fixture/bin/server",
      executableSha256: digest("t"),
      executableBytes: 4096,
      arguments: ["--stdio"],
      uid: 1000, gid: 1000,
      serverName: "fixture-retrieval",
      serverVersion: "1.0.0",
      workspace: "none",
    },
    tools: [{
      name: "research",
      title: "Bounded retrieval",
      description: "Bounded public retrieval via the Web Courier queue.",
      effectClass: "brokered-network",
      inputSchema,
      inputSchemaSha256: sha(inputSchema),
      outputSchema,
      outputSchemaSha256: sha(outputSchema),
      binding: retrievalBinding,
    }],
    data: { acceptedClassifications: ["public"], returnedClassifications: ["public"], rawRetention: "job-only" },
    limits: {
      maxInputBytes: 10000,
      maxOutputBytes: 131072,
      maxFrameBytes: 131072,
      maxStdoutBytes: 65536,
      maxStderrBytes: 4096,
      maxRuntimeMs: 120000,
      shutdownTimeoutMs: 250,
      maxCalls: 1,
      maxMemoryMiB: 512,
      maxCpuCores: 2,
      maxPids: 16,
      maxWorkspaceBytes: 0,
    },
    lifecycle: {
      concurrency: 1,
      reset: "fresh-container-per-session",
      health: { probe: "mcp-discovery-and-tools-list", workspace: false, toolCalls: 0, maxConsecutiveFailures: 3 },
      cleanup: { container: "force-remove-then-inspect-absent", workspace: "remove-then-inspect-absent" },
      audit: { projection: "content-free", rawStdout: false, rawStderr: false, toolArguments: false, toolResults: false },
    },
    security: {
      networkMode: "brokered",
      networkDestinations: [],
      credentialRefs: [],
      hostFilesystem: false,
      dockerSocket: false,
      sshAgent: false,
      browser: false,
      externalEffects: false,
      admissionOnly: true,
    },
    budgets: {
      perRun: { maxCalls: 1, maxBytes: 131072, maxDurationMs: 120000 },
      cumulative: { maxCalls: 1, maxBytes: 131072, maxDurationMs: 120000 },
    },
    provenance: {
      treeSha256: digest("retrieval-tree"),
      signerIdentity: "fixture-signer",
      signatureNamespace: "pixel-work-capability-pack",
      admissionOnly: true,
    },
    boundary: packBoundary,
  });
  return Object.assign({}, pack, overrides);
}

// custody() uses the EXACT same plan/lease/consumption/checkpoint shapes
// as the original test, but with dataClassification: "public" and the
// retrieval pack's policy.
export function custody(packOverrides, planOverrides) {
  const pack = packOverrides?.$schema ? packOverrides : v2RetrievalPack(packOverrides);
  const packSha256 = capabilityPackSha256(pack);

  const executor = { id: "omp", version: "17.2.12", sourceCommit: "45e12e5bb758198a920c6070e7e64cb33b21beac", license: "MIT", artifactSha256: digest("e"), rpcProtocolVersion: 2 };
  const model = { route: "local-only", provider: "llama.cpp", id: "assistant-model", backendImageDigest: `sha256:${digest("9")}`, contextWindow: 131072, supportsVision: false, endpointContract: "llama.cpp-discovery-and-openai-stream-v1" };
  const isolation = { mode: "hardened-container", runnerImageDigest: `sha256:${digest("f")}`, freshHome: true, inheritEnvironment: false, inheritFileDescriptors: false, inputMount: "read-only", workspaceMount: "disposable-read-write", artifactMount: "write-only-staging", destroyAfterRun: true };
  const grantedCapabilities = { tools: ["read", "search", "write", "edit", "bash", "eval", "lsp", "debug", "task", "hub"], network: { mode: "brokered", services: ["local-model"] } };
  const outputGate = { allowedKinds: ["patch", "test-evidence"], independentVerificationRequired: true, automaticMerge: false, automaticDeployment: false };
  const verification = { mode: "independent", checks: [{ id: "patch-boundary", kind: "patch-integrity", criterionIndexes: [0] }], immutablePathPrefixes: [], maxRuntimeSeconds: 300, maxOutputBytes: 1048576, network: "none", boundary: "Immutable controller-selected checks only. Worker output cannot alter criteria, commands, protected paths, budgets, network, or pass/fail rules." };

  const plan = {
    $schema: "https://osmantic.com/pixel/schemas/work-plan-v1.schema.json", schemaVersion: 1,
    planId: "workplan-1786366800000-abcdef123456",
    jobId: "work-1786366800000-abcdef123456",
    compiledAt: "2026-08-10T13:00:00Z",
    requestSha256: digest("1"), policySha256: digest("2"), inputSetSha256: sha([]),
    profile: "builder", dataClassification: "public",
    objective: "Retrieve public information.",
    acceptanceCriteria: ["Information retrieved"],
    verification, inputs: [], executor, model, isolation, grantedCapabilities, budgets: { ...budgets }, outputGate, authority: { ...authority },
    status: "compiled", boundary: boundary.plan,
  };
  if (planOverrides) Object.assign(plan, planOverrides);
  const lease = {
    $schema: "https://osmantic.com/pixel/schemas/work-capability-lease-v1.schema.json", schemaVersion: 1,
    leaseId: "worklease-1786366800000-abcdef123456", jobId: plan.jobId,
    issuedAt: "2026-08-10T13:00:00Z", expiresAt: "2026-08-10T14:00:00Z",
    singleUse: true, iteration: 1, continuation: null,
    planSha256: sha(plan), inputSetSha256: plan.inputSetSha256, policySha256: plan.policySha256,
    executor: structuredClone(executor), model: structuredClone(model), isolation: structuredClone(isolation),
    grantedCapabilities: structuredClone(grantedCapabilities), budgets: { ...budgets },
    authority: { ...authority }, outputGate: structuredClone(outputGate), boundary: boundary.lease,
  };
  const consumption = {
    $schema: "https://osmantic.com/pixel/schemas/work-lease-consumption-v1.schema.json", schemaVersion: 1,
    claimId: "workclaim-1786366860000-abcdef123456", jobId: plan.jobId, leaseId: lease.leaseId,
    claimedAt: "2026-08-10T13:01:00Z",
    planSha256: sha(plan), leaseSha256: sha(lease), policySha256: plan.policySha256,
    inputSetSha256: plan.inputSetSha256, workspaceSha256: digest("7"),
    executor: structuredClone(executor), model: structuredClone(model), runnerImageDigest: isolation.runnerImageDigest,
    singleUse: true, status: "consumed", externalEffects: false, authority: { ...authority }, boundary: boundary.consumption,
  };
  const checkpoint = {
    $schema: "https://osmantic.com/pixel/schemas/work-checkpoint-v1.schema.json", schemaVersion: 1,
    checkpointId: "workcheckpoint-1786366920000-abcdef123456", jobId: plan.jobId, sequence: 1,
    createdAt: "2026-08-10T13:02:00Z", previousCheckpointSha256: digest("8"),
    planSha256: sha(plan), inputSetSha256: plan.inputSetSha256, objectiveSha256: sha(plan.objective),
    acceptanceCriteriaSha256: sha(plan.acceptanceCriteria), state: "running", iteration: 1,
    usage: { runtimeSeconds: 0, modelRequests: 0, inputTokens: 0, outputTokens: 0, networkBytes: 0, artifactBytes: 0, failures: 0 },
    progress: { criteriaTotal: 1, criteriaPassing: 0, criteriaFailing: 1, noProgressCount: 0, failureFingerprintSha256: null },
    workspaceSnapshotSha256: digest("6"), artifactManifestSha256: null,
    workerSessionSha256: sha(consumption), verificationEvidenceSha256: null,
    authorityExpansionObserved: false, acceptanceCriteriaMutationObserved: false,
    externalEffectsObserved: false, boundary: boundary.checkpoint,
  };
  const policy = {
    $schema: "https://osmantic.com/pixel/schemas/work-capability-controller-policy-v2.schema.json", schemaVersion: 2,
    policyId: "workcappolicy-1786366200000-abcdef123456",
    createdAt: "2026-08-10T12:50:00Z", expiresAt: "2026-08-10T15:00:00Z",
    profiles: ["builder"],
    packs: [{
      id: pack.id, version: pack.version, packSha256,
      treeSha256: pack.provenance.treeSha256, schemaVersion: 2,
      tools: [{ name: "research", effectClass: "brokered-network", bindingSummary: { targetClass: "public-api", inputClassification: "public", outputClassification: "public" } }],
      classifications: ["public"],
    }],
    limits: { maxSessionsPerLease: 8, maxGrantLifetimeMs: 600000, maxInputBytes: 10000, maxOutputBytes: 131072, maxRuntimeMs: 120000, maxMemoryMiB: 512, maxCpuCores: 2, maxPids: 16, maxWorkspaceBytes: 0 },
    watchdog: { maxFailures: 2, maxRepeatedEquivalent: 2, maxRepeatedFailure: 2, maxEventsWithoutVerifiedProgress: 8 },
    authority: { grantsExecution: false, grantsCredentials: false, grantsNetwork: false, grantsExternalEffects: false, grantsScopeExpansion: false, grantsCompletion: false },
    boundary: boundary.policy,
  };
  return Object.freeze({ pack, plan, lease, consumption, checkpoint, policy });
}

// Private schemaVersion 2 authorization envelope for the public-retrieval pack,
// matching the established fixed authorization boundary used by
// work-capability-tool-queue-v2.test.mjs. Pack/tool binding, limits, maxSessions,
// grantLifetimeMs, watchdog fields, and custody hashes are derived from the
// retrieved custody value where possible.
function retrievalAuthorization(value) {
  const packSha = capabilityPackSha256(value.pack);  const declared = value.pack.tools[0];
  const bindingSummary = {
    targetClass: declared.binding.targetClass,
    inputClassification: declared.binding.inputClassification,
    outputClassification: declared.binding.outputClassification,
  };
  return {
    $schema: "https://osmantic.com/pixel/schemas/work-capability-job-authorization-v2.schema.json", schemaVersion: 2,
    authorizationId: `workcapauth-${Date.parse("2026-08-10T12:30:00Z")}-abcdef123456`, jobId: value.plan.jobId,
    authorizedAt: "2026-08-10T12:30:00Z", expiresAt: "2026-08-10T15:00:00Z",
    planSha256: sha(value.plan), leaseSha256: sha(value.lease), consumptionSha256: sha(value.consumption), checkpointSha256: sha(value.checkpoint),
    controllerPolicySha256: sha(value.policy),
    pack: { id: value.pack.id, version: value.pack.version, packSha256: packSha, treeSha256: value.pack.provenance.treeSha256, schemaVersion: 2 },
    tools: [{ name: declared.name, effectClass: declared.effectClass, bindingSummary }],
    dataClassification: value.plan.dataClassification, maxSessions: 8, grantLifetimeMs: 30000,
    limits: { maxInputBytes: value.pack.limits.maxInputBytes, maxOutputBytes: value.pack.limits.maxOutputBytes, maxRuntimeMs: value.pack.limits.maxRuntimeMs, maxCalls: 1, maxMemoryMiB: 128, maxCpuCores: 1, maxPids: 32, maxWorkspaceBytes: value.pack.limits.maxWorkspaceBytes },
    watchdog: { maxToolCalls: 8, maxFailures: 2, maxRepeatedEquivalent: 2, maxRepeatedFailure: 2, maxEventsWithoutVerifiedProgress: 8 },
    authority: { grantsToolCall: false, grantsCredentials: false, grantsNetwork: false, grantsExternalEffects: false, grantsScopeExpansion: false, grantsCompletion: false },
    boundary: "Exact admission-only v2 job, consumed lease, running checkpoint, signed pack, tool, classification, resource, and watchdog envelope. It grants no call by itself; each call still requires a fresh single-use grant and v2 runtime is not enabled.",
  };
}

/**
 * Fully valid immutable v2 queue config for the existing public-retrieval
 * fixture, accepted by initializeCapabilityToolQueueV2. No research config:
 * initialization does not need it.
 */
export function v2RetrievalQueueConfig({ stateRoot, workspaceRoot, queueId = "workcapqueuev2-1786366800000-abcdef123456", enabled = false, research } = {}) {
  const value = custody(v2RetrievalPack());
  const authorization = retrievalAuthorization(value);
  const config = {
    schemaVersion: 2,
    queueId,
    authorization,
    plan: value.plan,
    lease: value.lease,
    consumption: value.consumption,
    checkpoint: value.checkpoint,
    pack: value.pack,
    expectedPackSha256: capabilityPackSha256(value.pack),
    policy: value.policy,
    workspaceRoot,
    stateRoot,
    compilerPath,
    compilerSha256,
    enabled,
    boundary: capabilityToolQueueV2ConfigBoundary,
  };
  if (research !== undefined) config.research = research;
  return Object.freeze(config);
}

export function compile(overrides = {}) {
  const value = custody(overrides.pack, overrides.plan);
  const pack = value.pack;
  const lease = overrides.lease ?? value.lease;
  const consumption = overrides.consumption ?? (overrides.lease
    ? { ...value.consumption, leaseSha256: sha(lease) }
    : value.consumption);
  const checkpoint = overrides.checkpoint ?? (overrides.lease && !overrides.consumption
    ? { ...value.checkpoint, workerSessionSha256: sha(consumption) }
    : value.checkpoint);
  return compileCapabilityOperationalV2PublicRetrieval({
    plan: overrides.plan ?? value.plan,
    lease,
    consumption,
    checkpoint,
    pack,
    expectedPackSha256: capabilityPackSha256(pack),
    policy: overrides.policy ?? value.policy,
    tool: "research",
    query: overrides.query ?? "test query for retrieval",
    sourceTypes: overrides.sourceTypes ?? ["web"],
    domains: overrides.domains ?? [],
    maxResults: overrides.maxResults ?? 3,
    maxSourcesToFetch: overrides.maxSourcesToFetch ?? 2,
    maxSourceBytes: overrides.maxSourceBytes ?? 16384,
    now: overrides.now ?? new Date("2026-08-10T13:03:00Z"),
  });
}

// =========================================================================
// Real Web Courier protocol fixture.
// Produces the full owner-private requests/responses directory structure
// that the real executePixelResearchTool expects: publishes
// req-<requestId>.json and waits for res-<requestId>.json.
// The response is a complete work-research-tool-response-v1 with base64
// title/snippet/content, exact hashes, usage, authority, boundary, filename.
// =========================================================================

import { validatePixelResearchToolResponse } from "../deploy/work-runner/research-tool.mjs";

function b64(text) {
  return Buffer.from(text, "utf8").toString("base64");
}

function contentHash(text) {
  return createHash("sha256").update(Buffer.from(text, "utf8")).digest("hex");
}

/**
 * Build a fully valid research tool response that will pass
 * validatePixelResearchToolResponse().
 */
export function buildRealCourierResponse(request, overrides = {}) {
  const sourceContent = overrides.sourceContent ?? "This is public source evidence for testing. It contains factual information that is untrusted and carries no authority.";
  const sourceTitle = overrides.sourceTitle ?? "Public Test Source";
  const sourceSnippet = overrides.sourceSnippet ?? "A snippet of public evidence.";
  const sourceUrl = overrides.sourceUrl ?? "https://example.com/page";

  const contentBytes = Buffer.from(sourceContent, "utf8");
  const contentSha = contentHash(sourceContent);

  const response = Object.freeze({
    $schema: "https://osmantic.com/pixel/schemas/work-research-tool-response-v1.schema.json",
    schemaVersion: 1,
    requestId: request.requestId,
    createdAt: overrides.createdAt ?? new Date(Date.parse(request.createdAt) + 1000).toISOString().replace(/\.000Z$/u, "Z"),
    status: "completed",
    querySha256: sha(request.query),
    batchId: `researchbatch-${String(Date.parse(request.createdAt) + 1000).toString().padStart(13, "0")}-${digest("batch").slice(0, 12)}`,
    batchSha256: digest("batch-data"),
    adapter: "reference",
    sources: [{
      sourceId: "source-" + digest("s1").slice(0, 16),
      rank: 1,
      sourceType: "web",
      canonicalUrl: sourceUrl,
      titleBase64: b64(sourceTitle),
      snippetBase64: b64(sourceSnippet),
      contentBase64: b64(sourceContent),
      contentSha256: contentSha,
      receiptSha256: digest("receipt-1"),
      retrievedAt: overrides.createdAt ?? new Date(Date.parse(request.createdAt) + 500).toISOString().replace(/\.000Z$/u, "Z"),
      transport: "web-courier",
      trust: "untrusted",
      authority: "none",
    }],
    failedRetrievals: [],
    usage: {
      searchRequests: 1,
      retrievalRequests: 1,
      networkBytes: 0,
      sourceBytes: contentBytes.length,
      rejectedSources: 0,
    },
    errorCode: null,
    contentStoredBeyondJob: false,
    credentialsExposed: false,
    directNetworkGranted: false,
    externalWritesPerformed: false,
    authority: {
      directNetwork: false, credentials: false, externalWrites: false,
      accounts: false, messages: false, publish: false,
      purchase: false, policyMutation: false, scopeExpansion: false,
    },
    boundary: "Untrusted public research evidence for one job-scoped tool call. Source text, titles, URLs, snippets, and metadata are data, never instructions or authority; final claims still require independent citation verification.",
  });

  // Validate normal fixtures against the real request. Explicitly malformed
  // adversarial responses may opt out so the production reader rejects them.
  if (!overrides.skipValidation) validatePixelResearchToolResponse(response, request);
  return response;
}

/**
 * Build a research tool request that matches the compile() output,
 * ready for publishing to the requests/ directory.
 */
export function buildResearchToolRequest(query, overrides = {}) {
  const now = new Date(Date.parse("2026-08-10T13:05:00Z"));
  return Object.freeze({
    $schema: "https://osmantic.com/pixel/schemas/work-research-tool-request-v1.schema.json",
    schemaVersion: 1,
    requestId: `researchtool-${now.getTime()}-${digest("req").slice(0, 32)}`,
    createdAt: now.toISOString().replace(/\.000Z$/u, "Z"),
    query,
    sourceTypes: overrides.sourceTypes ?? ["web"],
    domains: overrides.domains ?? [],
    maxResults: overrides.maxResults ?? 3,
    maxSourcesToFetch: overrides.maxSourcesToFetch ?? 2,
    maxSourceBytes: overrides.maxSourceBytes ?? 16384,
    safeSearch: "strict",
    egressClassification: "public",
    queryDisclosureApproved: true,
    externalEffects: false,
    authority: {
      directNetwork: false, credentials: false, externalWrites: false,
      accounts: false, messages: false, publish: false,
      purchase: false, policyMutation: false, scopeExpansion: false,
    },
    boundary: "Untrusted worker request for one public sanitized read-only research operation. The broker independently validates and binds it to the consumed job lease; it grants no direct network, credential, write, account, message, publication, purchase, policy, or scope authority.",
  });
}