UNPKG

kestrel.markets

Version:

A typed, token-efficient language + runtime for agentic trading: agents author bounded plans, the runtime fires them at the tick. CLI + typed library + MCP server.

119 lines (117 loc) 4.2 kB
import { runVerify, verifyProofSignature } from "./bin-e4mpazjy.js"; import { HostedFunnel, loadStrategy, resolveCopyToken, resolveScenarioSlug, resolveSimBase, runHostedProof } from "./bin-69rn2kv7.js"; import"./bin-awvz75fn.js"; import { LEGACY_CWD_HOSTED_STORE, defaultHostedStorePath, readHostedRuns, readHostedRunsMerged } from "./bin-m9a64j9t.js"; import"./bin-qewrvqtv.js"; import"./bin-x2cbtyjz.js"; import { parseArgs } from "./bin-1gc4zavq.js"; import { CliError, EXIT } from "./bin-t9ggwnv5.js"; import"./bin-29be75ss.js"; import"./bin-8pchjxn2.js"; import"./bin-2ywrx58g.js"; import"./bin-wckvcay0.js"; // src/cli/commands/replay.ts function findRun(rows, id) { return rows.find((r) => r.grade_artifact_id === id || r.operation_id === id || r.proof_url.endsWith(`/proof/${id}`)); } async function replayCommand(argv, ctx, globals, deps = {}) { const { positionals, flags } = parseArgs(argv, new Set, new Set(["copy-token"])); if (positionals.length === 0) throw new CliError({ code: "USAGE", exit: EXIT.USAGE, message: "replay needs a proof id", hint: "usage: kestrel replay <proofId> (a proof this machine produced re-runs; any other is verified)" }); const id = positionals[0]; const env = deps.env ?? process.env; const storePath = deps.hostedStorePath ?? defaultHostedStorePath(env); const copyToken = resolveCopyToken(flags, env); const forward = copyToken !== undefined ? { copyToken } : {}; const base = resolveSimBase(globals, env); const client = new HostedFunnel({ baseUrl: base, ...deps.fetch !== undefined ? { fetch: deps.fetch } : {} }); const rows = deps.hostedStorePath !== undefined ? readHostedRuns(storePath) : readHostedRunsMerged({ home: storePath, fallbacks: [LEGACY_CWD_HOSTED_STORE] }); const row = findRun(rows, id); if (row === undefined) { return runVerify(client, id, ctx, forward, "no local strategy to replay — verifying the published proof instead"); } const { entries } = await client.catalog(); const resolved = resolveScenarioSlug(row.slug, entries); if (resolved === null) { return runVerify(client, row.grade_artifact_id, ctx, forward, `the recorded scenario ${JSON.stringify(row.slug)} is no longer in the catalog — verifying the recorded proof instead`); } const strategy = loadStrategy(undefined); let outcome; const code = await runHostedProof({ client, resolved, strategy, ctx, hostedStorePath: storePath, quiet: true, onProof: (o) => { outcome = o; }, ...forward }); if (code !== 0) return code; if (outcome === undefined) throw new CliError({ code: "GENERIC", exit: EXIT.GENERIC, message: "replay re-ran the scenario, but the fresh grade proof is still settling (transient R2 write lag) — its signature cannot be re-verified yet", hint: "retry `kestrel replay` shortly; the settling warning on stderr names the re-run's operation id and grade artifact id (the proof URL is not yet readable — that is what settling means)" }); const keys = await client.verifyKeys(forward); const sig = verifyProofSignature({ proof_id: outcome.proofId, root: outcome.root, signature: outcome.signature, kid: outcome.kid, epoch: outcome.epoch, verificationClaim: true }, keys); const metricsReproduced = Number.isFinite(outcome.metrics.orderCount) && Number.isFinite(outcome.metrics.fillCount) && Number.isFinite(outcome.metrics.realizedPnl); const reproduced = sig === "VERIFIED" && metricsReproduced; const verdict = reproduced ? "REPRODUCED" : "NOT_REPRODUCED"; if (ctx.mode === "json") { process.stdout.write(JSON.stringify({ schema: "kestrel.replay/v1", verdict, scenario: row.slug, original_proof: row.proof_url, new_proof: outcome.proofUrl, signature: sig, metrics: outcome.metrics }) + ` `); } else { process.stdout.write([ verdict, `scenario=${row.slug}`, `original=${row.proof_url}`, `reproduced=${outcome.proofUrl}`, `signature=${sig}` ].join("\t") + ` `); } return reproduced ? 0 : EXIT.GENERIC; } export { replayCommand, findRun };