import { createHash } from "node:crypto"; import { PiAgentRunner } from "../src/agents/pi.js"; import { defaultOutputContractIdentity } from "../src/agents/output-contracts.js"; import type { ThoughtAgentDeclaration } from "../src/agents/types.js"; import type { ThoughtEvent } from "../src/events/types.js"; if (process.env.THOUGHTSTREAM_RUN_TINKER_PROPOSAL_CANARY !== "1") { throw new Error("Set THOUGHTSTREAM_RUN_TINKER_PROPOSAL_CANARY=1 to run the credentialed proposal canary"); } if (!process.env.TINKER_API_KEY) throw new Error("TINKER_API_KEY is required for the Tinker proposal canary"); const model = process.env.THOUGHTSTREAM_TINKER_CANARY_MODEL ?? "thinkingmachines/Inkling-Small"; const eventId = "evt_tinker_proposal_canary"; const targetOutput = "evt_tinker_proposal_target"; const outputContract = defaultOutputContractIdentity(); const declaration: ThoughtAgentDeclaration = { id: "tinker-proposal-canary", version: 1, name: "Tinker proposal canary", description: "One credentialed inference-only Tinker proposal canary", mode: "pi", provider: "tinker", providerProfile: "tinker-default", model, outputMode: "conversation-text", eventTypes: ["stream.thought.source.telegram.message"], compiledEventTypes: ["stream.thought.source.telegram.message"], sourcePatterns: ["telegram:tinker-proposal-canary"], acceptedPrivacy: ["sensitive"], outputEventType: "stream.thought.derived.message.observation", emit: ["stream.thought.derived.message.observation"], promptRef: "scripts/tinker-proposal-canary.ts", systemPrompt: "Use the supplied correction function exactly once with the exact values requested by the user. Return no visible text.", enabled: true, maxEvents: 1, maxInputChars: 4_000, contextStrategy: "telegram-conversation", maxOutputTokens: 512, timeoutMs: 120_000, tools: [], proposals: ["self-correction"], externalActions: false, }; const event: ThoughtEvent = { id: eventId, sourceSequence: 1, type: "stream.thought.source.telegram.message", schemaVersion: 1, source: "telegram:tinker-proposal-canary", sourceKind: "telegram", externalId: "tinker-proposal-canary", idempotencyKey: "tinker-proposal-canary", occurredAt: new Date().toISOString(), observedAt: new Date().toISOString(), actor: "canary", rootEventId: eventId, correlationId: "tinker-proposal-canary", privacy: "sensitive", payload: { chatId: "canary", senderId: "canary", text: "Propose the exact correction described in the transcript." }, payloadHash: sha256("tinker-proposal-canary"), createdByRuntime: "tinker-proposal-canary", }; const output = await new PiAgentRunner().run({ runId: "run_tinker_proposal_canary", declaration, event, context: { text: [ '', JSON.stringify([{ role: "user", content: `Call submit_correction with target_output ${targetOutput}, replacement "Corrected answer.", reason "The prior answer was wrong.", and evidence_event_ids ["${eventId}"].` }]), "", ].join("\n"), manifest: { contextSnapshot: { id: "snapshot_tinker_proposal_canary" }, proposalCapabilities: { enabled: ["self-correction"], evidenceEventIds: [eventId], correctionTargets: [{ runId: "run_tinker_proposal_target", outputEventId: targetOutput, deliveryReceiptEventId: "evt_tinker_proposal_delivery", sourceRootEventId: "evt_tinker_proposal_source", outputContract, }], }, }, }, }, async () => undefined); const proposal = output.proposals?.[0]; if (!proposal || proposal.kind !== "self-correction" || proposal.arguments.target_output !== targetOutput) { throw new Error(`Tinker proposal canary returned an unexpected proposal hash ${sha256(JSON.stringify(output.proposals ?? []))}`); } process.stdout.write(`${JSON.stringify({ status: "passed", provider: output.model?.provider, model: output.model?.id, proposalKind: proposal.kind, targetMatches: true, usage: output.usage, })}\n`); function sha256(value: string): string { return createHash("sha256").update(value).digest("hex"); }