@omacode/micromanager 0.1.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -4
- package/THIRD_PARTY.md +100 -0
- package/decider/.python-version +1 -0
- package/decider/README.md +35 -0
- package/decider/decider/__init__.py +1 -0
- package/decider/decider/server.py +327 -0
- package/decider/pyproject.toml +34 -0
- package/decider/uv.lock +1255 -0
- package/dist/agenda.js +51 -0
- package/dist/agents.js +58 -0
- package/dist/artifact/handoff.js +281 -0
- package/dist/ask.js +200 -0
- package/dist/backends.js +219 -0
- package/dist/backlog.js +414 -0
- package/dist/bench.js +427 -0
- package/dist/board.js +1 -0
- package/dist/brief.js +202 -0
- package/dist/browser.js +679 -0
- package/dist/budget.js +61 -0
- package/dist/catalog.js +187 -0
- package/dist/checkchange.js +145 -0
- package/dist/checkpoint.js +94 -0
- package/dist/cli.js +764 -0
- package/dist/comfy-gate.js +258 -0
- package/dist/compact.js +142 -0
- package/dist/config.js +344 -0
- package/dist/context/capture.js +1 -0
- package/dist/context/digest.js +1 -0
- package/dist/context/embed.js +1 -0
- package/dist/context/index.js +2 -0
- package/dist/context/ledger.js +1 -0
- package/dist/context/manager.js +1 -0
- package/dist/context/memory.js +1 -0
- package/dist/context/policy.js +1 -0
- package/dist/context/prices.js +1 -0
- package/dist/context/recall.js +1 -0
- package/dist/context/rules.js +1 -0
- package/dist/context/search.js +1 -0
- package/dist/context/status.js +1 -0
- package/dist/context/store.js +1 -0
- package/dist/context/summary.js +1 -0
- package/dist/context/tokens.js +1 -0
- package/dist/context/tools.js +1 -0
- package/dist/continuation.js +184 -0
- package/dist/contract.js +294 -0
- package/dist/contractround.js +178 -0
- package/dist/control.js +1 -0
- package/dist/creative-tools.js +237 -0
- package/dist/dataset.js +175 -0
- package/dist/decide.js +676 -0
- package/dist/decider.js +171 -0
- package/dist/design-shell.js +448 -0
- package/dist/design.js +1410 -0
- package/dist/desktop.js +44 -0
- package/dist/egress.js +156 -0
- package/dist/eval.js +60 -0
- package/dist/evolver.js +302 -0
- package/dist/features.js +193 -0
- package/dist/followups.js +32 -0
- package/dist/forms.js +195 -0
- package/dist/gate.js +108 -0
- package/dist/gatebox.js +46 -0
- package/dist/grants.js +189 -0
- package/dist/graph.js +65 -0
- package/dist/guard/index.js +1 -0
- package/dist/guard/wire.js +1 -0
- package/dist/helpers.js +85 -0
- package/dist/hooks.js +158 -0
- package/dist/image-models.js +117 -0
- package/{src/integration.ts → dist/integration.js} +1 -2
- package/dist/journal.js +1 -0
- package/dist/look.js +363 -0
- package/dist/memory.js +1 -0
- package/dist/mode.js +97 -0
- package/dist/native.js +3348 -0
- package/dist/nogit.js +259 -0
- package/dist/outcomes.js +148 -0
- package/dist/permission.js +35 -0
- package/dist/pick.js +89 -0
- package/dist/pipelines.js +262 -0
- package/dist/plan.js +641 -0
- package/dist/plandoc.js +211 -0
- package/dist/plugin.js +4736 -0
- package/dist/policy.js +58 -0
- package/dist/pools.js +1 -0
- package/dist/preview.js +274 -0
- package/dist/profile.js +165 -0
- package/dist/progress.js +1 -0
- package/dist/promote.js +313 -0
- package/dist/redact.js +1 -0
- package/dist/registry.js +179 -0
- package/dist/riskscore.js +126 -0
- package/dist/roles.js +1 -0
- package/dist/routing.js +1 -0
- package/dist/runner.js +1892 -0
- package/{src/sandbox.ts → dist/sandbox.js} +1 -2
- package/dist/sandboxgate.js +90 -0
- package/dist/scheduler.js +170 -0
- package/dist/services-main.js +131 -0
- package/dist/services.js +265 -0
- package/dist/session.js +241 -0
- package/dist/setup.js +423 -0
- package/dist/shell/decide.js +1 -0
- package/dist/shell/index.js +2 -0
- package/dist/shell/sandbox.js +1 -0
- package/dist/shell/wire.js +1 -0
- package/dist/skills.js +103 -0
- package/dist/state.js +1 -0
- package/dist/store.js +1 -0
- package/dist/switcher.js +1 -0
- package/dist/synth.js +418 -0
- package/dist/tools.js +477 -0
- package/dist/trials.js +223 -0
- package/dist/usage.js +1 -0
- package/dist/wait.js +167 -0
- package/dist/watchdog.js +476 -0
- package/dist/web-guidance.js +43 -0
- package/dist/which.js +20 -0
- package/dist/worktree/git.js +545 -0
- package/dist/writeguard.js +311 -0
- package/package.json +36 -17
- package/skills/deck/SKILL.md +98 -0
- package/skills/design/SKILL.md +150 -0
- package/skills/image/SKILL.md +66 -0
- package/skills/orchestrator/SKILL.md +107 -0
- package/skills/video/SKILL.md +97 -0
- package/skills/web/SKILL.md +60 -0
- package/src/agenda.ts +0 -56
- package/src/agents.ts +0 -74
- package/src/ask.ts +0 -243
- package/src/backends.ts +0 -144
- package/src/backlog.ts +0 -436
- package/src/bench.ts +0 -163
- package/src/board.ts +0 -1
- package/src/brief.ts +0 -151
- package/src/browser.ts +0 -624
- package/src/budget.ts +0 -73
- package/src/catalog.ts +0 -214
- package/src/checkchange.ts +0 -180
- package/src/checkpoint.ts +0 -99
- package/src/cli.ts +0 -117
- package/src/compact.ts +0 -162
- package/src/config.ts +0 -340
- package/src/context/capture.ts +0 -1
- package/src/context/digest.ts +0 -1
- package/src/context/embed.ts +0 -1
- package/src/context/index.ts +0 -2
- package/src/context/ledger.ts +0 -1
- package/src/context/manager.ts +0 -1
- package/src/context/memory.ts +0 -1
- package/src/context/policy.ts +0 -1
- package/src/context/prices.ts +0 -1
- package/src/context/recall.ts +0 -1
- package/src/context/rules.ts +0 -1
- package/src/context/search.ts +0 -1
- package/src/context/status.ts +0 -1
- package/src/context/store.ts +0 -1
- package/src/context/summary.ts +0 -1
- package/src/context/tokens.ts +0 -1
- package/src/context/tools.ts +0 -1
- package/src/continuation.ts +0 -122
- package/src/contract.ts +0 -342
- package/src/contractround.ts +0 -225
- package/src/control.ts +0 -1
- package/src/dataset.ts +0 -133
- package/src/decide.ts +0 -690
- package/src/eval.ts +0 -66
- package/src/evolver.ts +0 -369
- package/src/followups.ts +0 -50
- package/src/forms.ts +0 -191
- package/src/gate.ts +0 -90
- package/src/grants.ts +0 -239
- package/src/graph.ts +0 -74
- package/src/guard/index.ts +0 -1
- package/src/guard/wire.ts +0 -1
- package/src/helpers.ts +0 -105
- package/src/hooks.ts +0 -199
- package/src/jev.ts +0 -141
- package/src/journal.ts +0 -1
- package/src/look.ts +0 -390
- package/src/memory.ts +0 -1
- package/src/native.ts +0 -2455
- package/src/nogit.ts +0 -112
- package/src/outcomes.ts +0 -210
- package/src/pick.ts +0 -112
- package/src/pipelines.ts +0 -252
- package/src/plan.ts +0 -449
- package/src/plugin.ts +0 -3187
- package/src/policy.ts +0 -84
- package/src/pools.ts +0 -1
- package/src/profile.ts +0 -186
- package/src/progress.ts +0 -1
- package/src/redact.ts +0 -1
- package/src/registry.ts +0 -207
- package/src/roles.ts +0 -1
- package/src/routing.ts +0 -1
- package/src/runner.ts +0 -1506
- package/src/scheduler.ts +0 -200
- package/src/session.ts +0 -269
- package/src/shell/decide.ts +0 -1
- package/src/shell/index.ts +0 -2
- package/src/shell/sandbox.ts +0 -1
- package/src/shell/wire.ts +0 -1
- package/src/skills.ts +0 -111
- package/src/state.ts +0 -1
- package/src/store.ts +0 -1
- package/src/switcher.ts +0 -1
- package/src/tools.ts +0 -480
- package/src/trials.ts +0 -247
- package/src/usage.ts +0 -1
- package/src/wait.ts +0 -180
- package/src/watchdog.ts +0 -498
- package/src/worktree/git.ts +0 -378
package/src/eval.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
// The eval harness (design.md §15): replays journaled decisions that have a label against a floor and reports,
|
|
2
|
-
// per node and question, how often the model would have decided alone, how often it would have been wrong, and
|
|
3
|
-
// how well its confidence matches its accuracy. Floors change only together with a run of this.
|
|
4
|
-
|
|
5
|
-
import type { Answer } from "./jev.ts"
|
|
6
|
-
import { labeled, options, type Labeled } from "./dataset.ts"
|
|
7
|
-
import type { Entry } from "./journal.ts"
|
|
8
|
-
|
|
9
|
-
export interface Report {
|
|
10
|
-
readonly key: string // "node.qid"
|
|
11
|
-
readonly labeled: number
|
|
12
|
-
readonly automatic: number // share decided alone at this floor
|
|
13
|
-
readonly wrong: number // share of the automatic ones that disagree with the label
|
|
14
|
-
readonly ece: number // expected calibration error over 5 bins
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// The chosen option and its confidence; a noul is decided when it is clearly yes or clearly no.
|
|
18
|
-
export function verdict(answer: Answer): { choice: string; confidence: number } {
|
|
19
|
-
if (answer.type === "noul") return answer.noul >= 0.5 ? { choice: "yes", confidence: answer.noul } : { choice: "no", confidence: 1 - answer.noul }
|
|
20
|
-
if (answer.type === "choice") return { choice: answer.choice, confidence: answer.confidence }
|
|
21
|
-
return { choice: String(Math.round(answer.score)), confidence: answer.confidence }
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function truth(row: Labeled): string | undefined {
|
|
25
|
-
const v = row.label?.value
|
|
26
|
-
if (v === undefined) return undefined
|
|
27
|
-
if (row.question.type === "noul") return v === "yes" || v === 1 ? "yes" : "no"
|
|
28
|
-
if (row.question.type === "score") return String(Math.round(Number(v)))
|
|
29
|
-
return options(row.question).includes(String(v)) ? String(v) : undefined
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function evaluate(entries: readonly Entry[], floor: number, backend?: string): Report[] {
|
|
33
|
-
const groups = new Map<string, { confidence: number; correct: boolean }[]>()
|
|
34
|
-
for (const row of labeled(entries)) {
|
|
35
|
-
if (!row.answer || (backend && row.entry.backend !== backend)) continue
|
|
36
|
-
const t = truth(row)
|
|
37
|
-
if (t === undefined) continue
|
|
38
|
-
const v = verdict(row.answer)
|
|
39
|
-
const key = `${String(row.entry.node)}.${row.qid}`
|
|
40
|
-
groups.set(key, [...(groups.get(key) ?? []), { confidence: v.confidence, correct: v.choice === t }])
|
|
41
|
-
}
|
|
42
|
-
return [...groups.entries()].sort(([a], [b]) => a.localeCompare(b)).map(([key, rows]) => {
|
|
43
|
-
const auto = rows.filter((r) => r.confidence >= floor)
|
|
44
|
-
const bins = Array.from({ length: 5 }, (_, i) => rows.filter((r) => Math.min(4, Math.floor(r.confidence * 5)) === i))
|
|
45
|
-
const ece = bins.reduce((sum, bin) => {
|
|
46
|
-
if (bin.length === 0) return sum
|
|
47
|
-
const conf = bin.reduce((s, r) => s + r.confidence, 0) / bin.length
|
|
48
|
-
const acc = bin.filter((r) => r.correct).length / bin.length
|
|
49
|
-
return sum + (bin.length / rows.length) * Math.abs(conf - acc)
|
|
50
|
-
}, 0)
|
|
51
|
-
return {
|
|
52
|
-
key,
|
|
53
|
-
labeled: rows.length,
|
|
54
|
-
automatic: auto.length / rows.length,
|
|
55
|
-
wrong: auto.length === 0 ? 0 : auto.filter((r) => !r.correct).length / auto.length,
|
|
56
|
-
ece,
|
|
57
|
-
}
|
|
58
|
-
})
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export function formatReport(reports: readonly Report[], floor: number): string {
|
|
62
|
-
if (reports.length === 0) return "no labeled decisions in the journal yet"
|
|
63
|
-
const pct = (x: number) => `${(x * 100).toFixed(0)}%`.padStart(5)
|
|
64
|
-
const lines = reports.map((r) => `${r.key.padEnd(28)} ${String(r.labeled).padStart(5)} ${pct(r.automatic)} ${pct(r.wrong)} ${r.ece.toFixed(3)}`)
|
|
65
|
-
return [`floor ${floor}`, `${"node.question".padEnd(28)} label auto wrong ece`, ...lines].join("\n")
|
|
66
|
-
}
|
package/src/evolver.ts
DELETED
|
@@ -1,369 +0,0 @@
|
|
|
1
|
-
// The evolver (design.md §26): OpenEvolve run against a task's fitness contract, in the Python sidecar evolver/.
|
|
2
|
-
// This side prepares the run and watches it:
|
|
3
|
-
// - the evolving file with EVOLVE-BLOCK markers around the one block that may change (in a copy, never the worktree);
|
|
4
|
-
// - the spec the sidecar reads: the task's checks, the contract's evaluator, scores, guards and baseline;
|
|
5
|
-
// - a bridge: an OpenAI-compatible endpoint on 127.0.0.1 with a one-run token, through which OpenEvolve's own client
|
|
6
|
-
// reaches the models. The plugin answers it with its session API (a tool-less session per call), so the evolver
|
|
7
|
-
// needs no API key and every call is charged to the job like any worker's;
|
|
8
|
-
// - the sidecar process (uv run mm-evolve), its progress from evals.jsonl, and its result.json.
|
|
9
|
-
// The best candidate then goes through the task's gate like any writer's change (src/native.ts finishEvolved).
|
|
10
|
-
|
|
11
|
-
import { spawn, type ChildProcess } from "node:child_process"
|
|
12
|
-
import { randomBytes } from "node:crypto"
|
|
13
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"
|
|
14
|
-
import { createServer, type Server } from "node:http"
|
|
15
|
-
import { extname, join } from "node:path"
|
|
16
|
-
import { fileURLToPath } from "node:url"
|
|
17
|
-
|
|
18
|
-
import type { Contract } from "./contract.ts"
|
|
19
|
-
|
|
20
|
-
// The evolver's state travels with its task in the job file (the sidebar and job_status read it there); state.ts owns
|
|
21
|
-
// the interface, the field is declared here as contract.ts does.
|
|
22
|
-
declare module "./state.ts" {
|
|
23
|
-
interface Task {
|
|
24
|
-
evolve?: {
|
|
25
|
-
state: "running" | "done" | "failed"
|
|
26
|
-
iterations: number
|
|
27
|
-
evaluations: number
|
|
28
|
-
valid: number
|
|
29
|
-
best?: Record<string, number>
|
|
30
|
-
calls: number
|
|
31
|
-
tokens: number
|
|
32
|
-
cost: number
|
|
33
|
-
started: string
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function evolverDir(): string {
|
|
39
|
-
return fileURLToPath(new URL("../evolver", import.meta.url))
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// --- the block that may change ------------------------------------------------------------------------------------
|
|
43
|
-
|
|
44
|
-
const COMMENT: Record<string, string> = { ".py": "#", ".sh": "#", ".rb": "#", ".r": "#", ".jl": "#", ".lua": "--", ".sql": "--", ".hs": "--" }
|
|
45
|
-
|
|
46
|
-
// The file with EVOLVE-BLOCK markers around the block: a line range ("12-40", 1-based, inclusive) or a function's
|
|
47
|
-
// name, found by its definition and, for brace languages, its balanced braces (Python and the like by indentation).
|
|
48
|
-
export function markBlock(text: string, file: string, block: string): { text?: string; lines?: [number, number]; problem?: string } {
|
|
49
|
-
const lines = text.split("\n")
|
|
50
|
-
const c = COMMENT[extname(file).toLowerCase()] ?? "//"
|
|
51
|
-
let range: [number, number] | undefined
|
|
52
|
-
const r = block.trim().match(/^(\d+)\s*-\s*(\d+)$/)
|
|
53
|
-
if (r) {
|
|
54
|
-
range = [Number(r[1]), Number(r[2])]
|
|
55
|
-
if (range[0] < 1 || range[1] > lines.length || range[0] > range[1]) return { problem: `lines ${block} are not inside ${file} (${lines.length} lines)` }
|
|
56
|
-
} else {
|
|
57
|
-
const name = block.trim().replace(/[^\w$]/g, "")
|
|
58
|
-
if (!name) return { problem: "block: a function's name or a line range like 12-40" }
|
|
59
|
-
const def = new RegExp(`(function\\*?\\s+${name}\\b|\\b${name}\\s*[:=]\\s*(async\\s*)?(function|\\()|^\\s*(async\\s+)?def\\s+${name}\\b|\\bfn\\s+${name}\\b|\\bfunc\\s+${name}\\b|^\\s*(export\\s+)?(async\\s+)?(static\\s+)?${name}\\s*\\([^)]*\\)\\s*[:{])`)
|
|
60
|
-
const start = lines.findIndex((l) => def.test(l))
|
|
61
|
-
if (start < 0) return { problem: `no definition of ${name} in ${file}; give the block as a line range instead` }
|
|
62
|
-
let end = start
|
|
63
|
-
if (c === "#") {
|
|
64
|
-
const indent = lines[start]!.search(/\S/)
|
|
65
|
-
end = start + 1
|
|
66
|
-
while (end < lines.length && (lines[end]!.trim() === "" || lines[end]!.search(/\S/) > indent)) end++
|
|
67
|
-
while (end > start + 1 && lines[end - 1]!.trim() === "") end--
|
|
68
|
-
end--
|
|
69
|
-
} else {
|
|
70
|
-
let depth = 0
|
|
71
|
-
let opened = false
|
|
72
|
-
for (end = start; end < lines.length; end++) {
|
|
73
|
-
for (const ch of lines[end]!.replace(/(["'`])(?:\\.|(?!\1).)*\1/g, "")) {
|
|
74
|
-
if (ch === "{") { depth++; opened = true }
|
|
75
|
-
if (ch === "}") depth--
|
|
76
|
-
}
|
|
77
|
-
if (opened && depth <= 0) break
|
|
78
|
-
}
|
|
79
|
-
if (!opened || end >= lines.length) return { problem: `could not find where ${name} ends in ${file}; give the block as a line range instead` }
|
|
80
|
-
}
|
|
81
|
-
range = [start + 1, end + 1]
|
|
82
|
-
}
|
|
83
|
-
const [a, b] = range
|
|
84
|
-
const out = [...lines.slice(0, a - 1), `${c} EVOLVE-BLOCK-START`, ...lines.slice(a - 1, b), `${c} EVOLVE-BLOCK-END`, ...lines.slice(b)]
|
|
85
|
-
return { text: out.join("\n"), lines: range }
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// --- the spec ------------------------------------------------------------------------------------------------------
|
|
89
|
-
|
|
90
|
-
export interface EvolveSpec {
|
|
91
|
-
readonly workdir: string
|
|
92
|
-
readonly file: string
|
|
93
|
-
readonly initial: string
|
|
94
|
-
readonly checks: readonly string[]
|
|
95
|
-
readonly evaluator: string
|
|
96
|
-
readonly scores: readonly { name: string; direction: string }[]
|
|
97
|
-
readonly guards: readonly { name: string; direction: string; tolerance: number }[]
|
|
98
|
-
readonly baseline: Readonly<Record<string, number>>
|
|
99
|
-
readonly output: string
|
|
100
|
-
readonly models: readonly { name: string; weight: number }[]
|
|
101
|
-
readonly iterations: number
|
|
102
|
-
readonly timeout_s: number
|
|
103
|
-
readonly target: Readonly<Record<string, number>>
|
|
104
|
-
readonly full_rewrite: boolean
|
|
105
|
-
readonly held_out_dir: string | null
|
|
106
|
-
readonly runner: readonly string[]
|
|
107
|
-
readonly api_base: string
|
|
108
|
-
readonly api_key: string
|
|
109
|
-
readonly quick_evaluator: string | null
|
|
110
|
-
readonly quick_baseline: Readonly<Record<string, number>>
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export function specOf(o: {
|
|
114
|
-
workdir: string; file: string; initial: string; checks: readonly string[]; contract: Contract; output: string; models: readonly string[]
|
|
115
|
-
iterations: number; heldOutDir?: string; runner: readonly string[]; bridge: { url: string; token: string }; fullRewrite?: boolean
|
|
116
|
-
}): EvolveSpec {
|
|
117
|
-
const c = o.contract
|
|
118
|
-
return {
|
|
119
|
-
workdir: o.workdir,
|
|
120
|
-
file: o.file,
|
|
121
|
-
initial: o.initial,
|
|
122
|
-
checks: [...o.checks],
|
|
123
|
-
evaluator: c.evaluator,
|
|
124
|
-
scores: c.scores.map((s) => ({ name: s.name, direction: s.direction })),
|
|
125
|
-
guards: c.guards.map((g) => ({ name: g.name, direction: g.direction, tolerance: g.tolerance })),
|
|
126
|
-
baseline: { ...(c.baseline?.values ?? {}) },
|
|
127
|
-
output: o.output,
|
|
128
|
-
models: o.models.map((name) => ({ name, weight: 1 })),
|
|
129
|
-
iterations: o.iterations,
|
|
130
|
-
timeout_s: Math.round(c.timeoutMs / 1000),
|
|
131
|
-
target: { ...(c.stop.target ?? {}) },
|
|
132
|
-
full_rewrite: o.fullRewrite ?? false,
|
|
133
|
-
held_out_dir: o.heldOutDir ?? null,
|
|
134
|
-
runner: [...o.runner],
|
|
135
|
-
api_base: `${o.bridge.url}/v1`,
|
|
136
|
-
api_key: o.bridge.token,
|
|
137
|
-
quick_evaluator: c.quick && c.baseline?.quick ? c.quick.evaluator : null,
|
|
138
|
-
quick_baseline: { ...(c.baseline?.quick ?? {}) },
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
// --- the bridge ----------------------------------------------------------------------------------------------------
|
|
143
|
-
|
|
144
|
-
export interface Generated {
|
|
145
|
-
readonly text: string
|
|
146
|
-
readonly tokens: number
|
|
147
|
-
readonly cost: number
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export interface Bridge {
|
|
151
|
-
readonly url: string
|
|
152
|
-
readonly token: string
|
|
153
|
-
readonly calls: () => { calls: number; failed: number; tokens: number; cost: number }
|
|
154
|
-
close(): Promise<void>
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// An OpenAI-compatible /v1/chat/completions on 127.0.0.1, for one run: a random token, and every call answered by
|
|
158
|
-
// generate (a model id and the system and user text). Anything else is 404; a wrong token 401.
|
|
159
|
-
export async function startBridge(generate: (model: string, system: string, prompt: string) => Promise<Generated>): Promise<Bridge> {
|
|
160
|
-
const token = randomBytes(24).toString("hex")
|
|
161
|
-
const used = { calls: 0, failed: 0, tokens: 0, cost: 0 }
|
|
162
|
-
const server: Server = createServer((req, res) => {
|
|
163
|
-
const send = (code: number, body: unknown) => {
|
|
164
|
-
res.writeHead(code, { "content-type": "application/json" })
|
|
165
|
-
res.end(JSON.stringify(body))
|
|
166
|
-
}
|
|
167
|
-
if (req.method !== "POST" || !req.url?.endsWith("/chat/completions")) return send(404, { error: { message: "not found" } })
|
|
168
|
-
if (req.headers.authorization !== `Bearer ${token}`) return send(401, { error: { message: "wrong token" } })
|
|
169
|
-
let raw = ""
|
|
170
|
-
req.on("data", (chunk: Buffer) => (raw += chunk.toString("utf8")))
|
|
171
|
-
req.on("end", () => {
|
|
172
|
-
void (async () => {
|
|
173
|
-
try {
|
|
174
|
-
const body = JSON.parse(raw) as { model?: string; messages?: { role: string; content: unknown }[] }
|
|
175
|
-
const text = (m: { content: unknown }) => (typeof m.content === "string" ? m.content : Array.isArray(m.content) ? m.content.map((p: { text?: string }) => p.text ?? "").join("") : "")
|
|
176
|
-
const messages = body.messages ?? []
|
|
177
|
-
const system = messages.filter((m) => m.role === "system").map(text).join("\n\n")
|
|
178
|
-
const prompt = messages.filter((m) => m.role !== "system").map(text).join("\n\n")
|
|
179
|
-
const g = await generate(String(body.model ?? ""), system, prompt)
|
|
180
|
-
used.calls++
|
|
181
|
-
used.tokens += g.tokens
|
|
182
|
-
used.cost += g.cost
|
|
183
|
-
send(200, {
|
|
184
|
-
id: `mm-${used.calls}`,
|
|
185
|
-
object: "chat.completion",
|
|
186
|
-
created: Math.floor(Date.now() / 1000),
|
|
187
|
-
model: body.model,
|
|
188
|
-
choices: [{ index: 0, message: { role: "assistant", content: g.text }, finish_reason: "stop" }],
|
|
189
|
-
usage: { prompt_tokens: 0, completion_tokens: g.tokens, total_tokens: g.tokens },
|
|
190
|
-
})
|
|
191
|
-
} catch (err) {
|
|
192
|
-
used.failed++
|
|
193
|
-
send(500, { error: { message: err instanceof Error ? err.message : String(err) } })
|
|
194
|
-
}
|
|
195
|
-
})()
|
|
196
|
-
})
|
|
197
|
-
})
|
|
198
|
-
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve))
|
|
199
|
-
const port = (server.address() as { port: number }).port
|
|
200
|
-
return {
|
|
201
|
-
url: `http://127.0.0.1:${port}`,
|
|
202
|
-
token,
|
|
203
|
-
calls: () => ({ ...used }),
|
|
204
|
-
close: () => new Promise<void>((resolve) => server.close(() => resolve())),
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// Why the evolved best may not be merged although it won on the visible corpus: invalid on the held-out cases, or worse
|
|
209
|
-
// there than today's code on the deciding score. The evolution never saw those cases, so this is where it shows
|
|
210
|
-
// whether it learned the task or the corpus. Undefined when it may go on to the gate.
|
|
211
|
-
export function heldOutProblem(c: Contract, held: Readonly<Record<string, number | string>> | null | undefined): string | undefined {
|
|
212
|
-
if (!held) return undefined
|
|
213
|
-
if (held.valid !== 1) return `the best candidate is invalid on the held-out cases (${held.error ?? "no valid result"})`
|
|
214
|
-
const first = c.scores[0]
|
|
215
|
-
const base = first && c.baseline?.heldOut?.[first.name]
|
|
216
|
-
const now = first && held[first.name]
|
|
217
|
-
if (base === undefined || typeof now !== "number") return undefined
|
|
218
|
-
const worse = first!.direction === "min" ? now > base : now < base
|
|
219
|
-
return worse ? `on the held-out cases the best candidate is worse than today's code (${first!.name} ${now} against ${base})` : undefined
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
// --- the offer ----------------------------------------------------------------------------------------------------
|
|
223
|
-
|
|
224
|
-
// Whether the evolver suits a writer measured against this contract: the code's half of the rule (Jev's local_block is
|
|
225
|
-
// the other). A frozen contract, a full measurement of at most maxEvalSeconds (the "evolver.maxEvalSeconds" setting,
|
|
226
|
-
// 600 by default: multi-step algorithms of minutes are what is worth speeding up), and one or two files.
|
|
227
|
-
export const EVOLVER_MAX_EVAL_S = 600
|
|
228
|
-
|
|
229
|
-
export function evolverFits(c: Contract, files: readonly string[], maxEvalSeconds = EVOLVER_MAX_EVAL_S): boolean {
|
|
230
|
-
return c.status === "frozen" && c.costPerEval !== undefined && c.costPerEval * c.repeats <= maxEvalSeconds && files.length >= 1 && files.length <= 2
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
// The share of candidates that pass a contract's quick stage and get the full measurement, before a run has measured
|
|
234
|
-
// it: most model-written variants are no better than their parent.
|
|
235
|
-
export const QUICK_PASS = 0.3
|
|
236
|
-
|
|
237
|
-
// What a run would take: iterations from the contract's stop rule (30 at most by default), each a model call (about
|
|
238
|
-
// 25 s) and a measurement: the full one, or with a quick stage that one and the full one for the share that pass it.
|
|
239
|
-
// The call is priced at what earlier evolver calls cost.
|
|
240
|
-
export function estimate(c: Contract, perCall: number, quickPass = QUICK_PASS): { iterations: number; minutes: number; cost: number } {
|
|
241
|
-
const iterations = Math.min(c.stop.evaluations ?? 30, 30)
|
|
242
|
-
const full = (c.costPerEval ?? 1) * c.repeats
|
|
243
|
-
const measure = c.quick?.costPerEval !== undefined ? c.quick.costPerEval + quickPass * full : full
|
|
244
|
-
return { iterations, minutes: Math.max(1, Math.round((iterations * (25 + measure)) / 60)), cost: Math.round(iterations * perCall * 1000) / 1000 }
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
// Minutes as the form says them: "45 min", "about 3.5 h".
|
|
248
|
-
export function duration(minutes: number): string {
|
|
249
|
-
return minutes < 90 ? `${minutes} min` : `${Math.round(minutes / 30) / 2} h`
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
const METHOD = { evolver: "Evolver", trial: "Trial", worker: "One worker" } as const
|
|
253
|
-
|
|
254
|
-
// The question when a task suits the evolver: the user decides how it is done, with what each way costs.
|
|
255
|
-
export function evolverForm(id: string, title: string, files: readonly string[], c: Contract, e: { iterations: number; minutes: number; cost: number }): { header: string; question: string; options: { label: string; description: string }[] }[] {
|
|
256
|
-
return [{
|
|
257
|
-
header: `Method ${id}`,
|
|
258
|
-
question: `"${title.slice(0, 100)}" improves ${files.join(", ")} against contract ${c.id}, which measures in ${c.costPerEval}s${c.quick?.costPerEval !== undefined ? ` (its quick stage in ${c.quick.costPerEval}s)` : ""}, and its gain lies in one function or block. How should it be done?`,
|
|
259
|
-
options: [
|
|
260
|
-
{ label: `${METHOD.evolver} (Recommended)`, description: `OpenEvolve tries about ${e.iterations} variants in the sandbox${c.quick ? `, each on the quick stage first and in full only when it is better there` : ""}, the best goes through the checks; about ${duration(e.minutes)} and ${e.cost} in model calls` },
|
|
261
|
-
{ label: METHOD.trial, description: "Two or more workers on different models at once; the best score wins" },
|
|
262
|
-
{ label: METHOD.worker, description: "One worker, as usual" },
|
|
263
|
-
],
|
|
264
|
-
}]
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
export function readMethod(header: string, answers: readonly string[]): { id: string; method?: "evolver" | "trial" | "worker"; said?: string } | undefined {
|
|
268
|
-
const m = header.match(/^Method (M\d+)$/)
|
|
269
|
-
if (!m) return undefined
|
|
270
|
-
const clean = answers.map((a) => a.trim().replace(/\s*\(recommended\)\s*$/i, "").toLowerCase())
|
|
271
|
-
const method = (Object.keys(METHOD) as (keyof typeof METHOD)[]).find((k) => clean.includes(METHOD[k].toLowerCase()))
|
|
272
|
-
if (method) return { id: m[1]!, method }
|
|
273
|
-
const said = answers.find((a) => a.trim())
|
|
274
|
-
return { id: m[1]!, ...(said ? { said: said.trim() } : {}) }
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
// --- the run -------------------------------------------------------------------------------------------------------
|
|
278
|
-
|
|
279
|
-
export interface Progress {
|
|
280
|
-
readonly evaluations: number
|
|
281
|
-
readonly valid: number
|
|
282
|
-
readonly best?: Readonly<Record<string, number>> // the best valid evaluation's metrics so far
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
export interface EvolveResult {
|
|
286
|
-
readonly best: string // the best program's file
|
|
287
|
-
readonly metrics: Readonly<Record<string, number>>
|
|
288
|
-
readonly heldOut?: Readonly<Record<string, number | string>> | null
|
|
289
|
-
readonly seconds: number
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
// What evals.jsonl says so far: how many candidates were measured, how many were valid, and the best by
|
|
293
|
-
// combined_score.
|
|
294
|
-
export function progressOf(evals: string): Progress {
|
|
295
|
-
let evaluations = 0
|
|
296
|
-
let valid = 0
|
|
297
|
-
let best: Record<string, number> | undefined
|
|
298
|
-
for (const line of evals.split("\n")) {
|
|
299
|
-
if (!line.trim()) continue
|
|
300
|
-
let row: Record<string, number>
|
|
301
|
-
try {
|
|
302
|
-
row = JSON.parse(line) as Record<string, number>
|
|
303
|
-
} catch {
|
|
304
|
-
continue
|
|
305
|
-
}
|
|
306
|
-
evaluations++
|
|
307
|
-
if (row.valid === 1) {
|
|
308
|
-
valid++
|
|
309
|
-
if (!best || (row.combined_score ?? 0) > (best.combined_score ?? 0)) best = row
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
return { evaluations, valid, ...(best ? { best } : {}) }
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
export interface Running {
|
|
316
|
-
readonly done: Promise<{ result?: EvolveResult; problem?: string }>
|
|
317
|
-
stop(): void
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
// The sidecar on a spec: `uv run --project evolver mm-evolve spec.json`, stdin closed (a child that reads stdin waits
|
|
321
|
-
// for an end that never comes). onProgress is called whenever evals.jsonl grew. stop() ends the process group.
|
|
322
|
-
export function runEvolver(spec: EvolveSpec, onProgress: (p: Progress) => void, opts: { uv?: string; dir?: string; pollMs?: number } = {}): Running {
|
|
323
|
-
mkdirSync(spec.output, { recursive: true })
|
|
324
|
-
const specPath = join(spec.output, "spec.json")
|
|
325
|
-
writeFileSync(specPath, JSON.stringify(spec, null, 2))
|
|
326
|
-
const child: ChildProcess = spawn(opts.uv ?? "uv", ["run", "--quiet", "--project", opts.dir ?? evolverDir(), "mm-evolve", specPath], { stdio: ["ignore", "pipe", "pipe"], detached: true })
|
|
327
|
-
let tail = ""
|
|
328
|
-
child.stdout?.on("data", (d: Buffer) => (tail = (tail + d.toString("utf8")).slice(-20_000)))
|
|
329
|
-
child.stderr?.on("data", (d: Buffer) => (tail = (tail + d.toString("utf8")).slice(-20_000)))
|
|
330
|
-
let seen = 0
|
|
331
|
-
const evals = join(spec.output, "evals.jsonl")
|
|
332
|
-
const poll = setInterval(() => {
|
|
333
|
-
if (!existsSync(evals)) return
|
|
334
|
-
const text = readFileSync(evals, "utf8")
|
|
335
|
-
if (text.length === seen) return
|
|
336
|
-
seen = text.length
|
|
337
|
-
onProgress(progressOf(text))
|
|
338
|
-
}, opts.pollMs ?? 5000)
|
|
339
|
-
poll.unref?.()
|
|
340
|
-
const done = new Promise<{ result?: EvolveResult; problem?: string }>((resolve) => {
|
|
341
|
-
child.on("error", (err) => {
|
|
342
|
-
clearInterval(poll)
|
|
343
|
-
resolve({ problem: `the evolver did not start: ${err.message}` })
|
|
344
|
-
})
|
|
345
|
-
child.on("close", (code) => {
|
|
346
|
-
clearInterval(poll)
|
|
347
|
-
if (existsSync(evals)) onProgress(progressOf(readFileSync(evals, "utf8")))
|
|
348
|
-
const out = join(spec.output, "result.json")
|
|
349
|
-
if (code === 0 && existsSync(out)) {
|
|
350
|
-
try {
|
|
351
|
-
return resolve({ result: JSON.parse(readFileSync(out, "utf8")) as EvolveResult })
|
|
352
|
-
} catch {
|
|
353
|
-
// fall through: a result that cannot be read is no result
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
resolve({ problem: `the evolver ended with code ${code}: ${tail.trim().split("\n").slice(-3).join(" | ").slice(0, 400)}` })
|
|
357
|
-
})
|
|
358
|
-
})
|
|
359
|
-
return {
|
|
360
|
-
done,
|
|
361
|
-
stop: () => {
|
|
362
|
-
try {
|
|
363
|
-
if (child.pid) process.kill(-child.pid, "SIGTERM")
|
|
364
|
-
} catch {
|
|
365
|
-
child.kill("SIGTERM")
|
|
366
|
-
}
|
|
367
|
-
},
|
|
368
|
-
}
|
|
369
|
-
}
|
package/src/followups.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
// N7, a worker's follow-ups (design.md §8): what the decision model is asked, and how code turns its answers into
|
|
2
|
-
// a verdict per item. Pure, so the runner and the N7 bench (npm run bench-n7) decide the same way.
|
|
3
|
-
|
|
4
|
-
import { CHOICE_FLOOR, followUpQuestions, NOUL_FLOOR } from "./catalog.ts"
|
|
5
|
-
import type { Answer } from "./jev.ts"
|
|
6
|
-
import type { FollowUp } from "./state.ts"
|
|
7
|
-
|
|
8
|
-
export interface N7Context {
|
|
9
|
-
readonly goal: string
|
|
10
|
-
readonly acceptance: readonly string[]
|
|
11
|
-
readonly task: string // the finder: "<id> <title>"
|
|
12
|
-
readonly otherTasks: readonly string[] // "<id> <title> (files: …)", every other live task of the job
|
|
13
|
-
readonly openFiles: ReadonlySet<string> // files of tasks still open (not the finder)
|
|
14
|
-
readonly doneFiles: ReadonlySet<string> // files of done tasks and the finder
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export type Verdict =
|
|
18
|
-
| { readonly kind: "task"; readonly priority: number }
|
|
19
|
-
| { readonly kind: "planned" }
|
|
20
|
-
| { readonly kind: "later"; readonly reason: "user_should_decide" | "out_of_scope" | "no_files" }
|
|
21
|
-
|
|
22
|
-
export function n7State(ctx: N7Context, items: readonly FollowUp[]): Record<string, unknown> {
|
|
23
|
-
return { goal: ctx.goal, acceptance: ctx.acceptance, task: ctx.task, otherTasks: ctx.otherTasks, followUps: items }
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export const n7Questions = followUpQuestions
|
|
27
|
-
|
|
28
|
-
const PRIORITY: Record<string, number> = { high: 1, normal: 3, low: 5 }
|
|
29
|
-
|
|
30
|
-
// answers: the model's, or empty (no model, or a rule preset). Without answers, files decide: a follow-up only on
|
|
31
|
-
// files of tasks still open is already planned, one on files of done tasks is in scope, anything else is not.
|
|
32
|
-
export function triage(answers: Readonly<Record<string, Answer>>, items: readonly FollowUp[], ctx: N7Context): Verdict[] {
|
|
33
|
-
const noul = (q: string) => (answers[q]?.type === "noul" ? (answers[q] as { noul: number }).noul : undefined)
|
|
34
|
-
return items.map((item, i): Verdict => {
|
|
35
|
-
const planned = noul(`planned.${i}`)
|
|
36
|
-
const onOpen = item.files.length > 0 && item.files.every((f) => ctx.openFiles.has(f))
|
|
37
|
-
if (planned !== undefined ? planned >= NOUL_FLOOR : onOpen) return { kind: "planned" }
|
|
38
|
-
// "Needed or in scope" goes before "the user's call": on the N7 bench the latter is high for nearly everything
|
|
39
|
-
// outside the job's text (0.6 to 0.8), real work included, while needed and in_scope stay low for what is not.
|
|
40
|
-
const needed = noul(`needed.${i}`)
|
|
41
|
-
const inScope = noul(`in_scope.${i}`)
|
|
42
|
-
const take = needed !== undefined || inScope !== undefined
|
|
43
|
-
? Math.max(needed ?? 0, inScope ?? 0) >= NOUL_FLOOR
|
|
44
|
-
: item.files.length > 0 && item.files.every((f) => ctx.doneFiles.has(f))
|
|
45
|
-
if (!take) return { kind: "later", reason: (noul(`user.${i}`) ?? 0) >= NOUL_FLOOR ? "user_should_decide" : "out_of_scope" }
|
|
46
|
-
if (item.files.length === 0) return { kind: "later", reason: "no_files" }
|
|
47
|
-
const p = answers[`priority.${i}`]
|
|
48
|
-
return { kind: "task", priority: p?.type === "choice" && p.confidence >= CHOICE_FLOOR ? PRIORITY[p.choice] ?? 3 : 3 }
|
|
49
|
-
})
|
|
50
|
-
}
|