Skip to content
Myr

results.ts

This file

Path

scripts/lib/swarm/results.ts

105 lines · 2.9 KiB · TypeScript

Last changed by

Merge build/comments-ops into mainoscabrielbefore adoptione4c407e

History of this file

Take it

Close W7: cross-compile the release again, guard pi's edits, and keep the swarm out of this checkoutoscabrielbefore adoption842f042

lines 1–2why, one step back
1import { Brand } from "@repo/brand";
2import { Context, Effect, FileSystem, Option, Schema } from "effect";

Merge branch 'build/702-swarm-plan'oscabrielbefore adoption8496778

lines 3–4why, one step back
3import type { PlatformError } from "effect";
4

Close W7: cross-compile the release again, guard pi's edits, and keep the swarm out of this checkoutoscabrielbefore adoption842f042

lines 5–9why, one step back
5const StateEnv = Schema.Struct({
6 HOME: Schema.optionalKey(Schema.NonEmptyString),7 XDG_STATE_HOME: Schema.optionalKey(Schema.NonEmptyString),8});9

Merge build/comments-ops into mainoscabrielbefore adoptione4c407e

line 10why, one step back
10// Never inside a checkout: a harness reads every AGENTS.md and CLAUDE.md above its folder.

Close W7: cross-compile the release again, guard pi's edits, and keep the swarm out of this checkoutoscabrielbefore adoption842f042

lines 11–20why, one step back
11export const swarmDirOf = (env: typeof StateEnv.Type) => {
12 const home = env.HOME === undefined ? "/tmp" : `${env.HOME}/.local/state`;1314 return `${env.XDG_STATE_HOME ?? home}/${Brand.slug}/swarm`;15};1617const GlobalScope = Schema.Struct({18 process: Schema.Struct({ env: StateEnv }),19});20

Merge branch 'build/702-swarm-plan'oscabrielbefore adoption8496778

line 21why, one step back
21export const SwarmDir = Context.Reference<string>("swarm/SwarmDir", {

Close W7: cross-compile the release again, guard pi's edits, and keep the swarm out of this checkoutoscabrielbefore adoption842f042

lines 22–32why, one step back
22 defaultValue: () => {
23 // Unknown on purpose: the runtime is whatever started this process.24 const scope: unknown = globalThis;2526 return swarmDirOf(27 Option.match(Schema.decodeUnknownOption(GlobalScope)(scope), {28 onNone: () => ({}),29 onSome: ({ process }) => process.env,30 })31 );32 },

Merge branch 'build/702-swarm-plan'oscabrielbefore adoption8496778

lines 33–105why, one step back
33});
3435export const runDir = (run: string) =>36 Effect.map(SwarmDir, (dir) => `${dir}/${run}`);3738export const Harness = Schema.Literals([39 "claude-code",40 "opencode",41 "amp",42 "pi",43]);4445export type Harness = typeof Harness.Type;4647export const AgentOutcome = Schema.Literals([48 "landed",49 "refused",50 "waiting",51 "noAnswer",52 "timedOut",53 "failed",54]);5556export type AgentOutcome = typeof AgentOutcome.Type;5758export const AgentRun = Schema.Struct({59 adapter: Schema.Boolean,60 ask: Schema.NonEmptyString,61 attempts: Schema.Int,62 durationMs: Schema.Int,63 handle: Schema.NonEmptyString,64 harness: Harness,65 landed: Schema.optionalKey(Schema.NonEmptyString),66 model: Schema.NonEmptyString,67 outcome: AgentOutcome,68 refusals: Schema.Record(Schema.String, Schema.Int),69});7071export type AgentRun = typeof AgentRun.Type;7273export const AgentsResult = Schema.Struct({74 at: Schema.DateTimeUtcFromString,75 commit: Schema.NonEmptyString,76 project: Schema.NonEmptyString,77 runs: Schema.Array(AgentRun),78 scenario: Schema.NonEmptyString,79 stage: Schema.NonEmptyString,80 v: Schema.Literal(1),81});8283export type AgentsResult = typeof AgentsResult.Type;8485export const agentsFile = (dir: string) => `${dir}/agents.json`;8687export const writeAgentsResult = (input: {88 readonly run: string;89 readonly result: AgentsResult;90}): Effect.Effect<void, PlatformError.PlatformError, FileSystem.FileSystem> =>91 Effect.gen(function* () {92 const fs = yield* FileSystem.FileSystem;93 const dir = yield* runDir(input.run);9495 // An `AgentsResult` value always encodes: strings, numbers and a DateTime.96 const encoded = yield* Schema.encodeEffect(AgentsResult)(input.result).pipe(97 Effect.orDie98 );99100 yield* fs.makeDirectory(dir, { recursive: true });101 yield* fs.writeFileString(102 agentsFile(dir),103 `${JSON.stringify(encoded, null, 2)}\n`104 );105 });