@omacode/micromanager 0.0.0-stage → 0.1.1
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/LICENSE +201 -0
- package/README.md +27 -2
- package/evolver/mm_evolve/__init__.py +1 -0
- package/evolver/mm_evolve/entry.py +3 -0
- package/evolver/mm_evolve/evaluator.py +119 -0
- package/evolver/mm_evolve/llm.py +76 -0
- package/evolver/mm_evolve/run.py +89 -0
- package/evolver/mm_evolve/spec.py +62 -0
- package/evolver/pyproject.toml +21 -0
- package/evolver/uv.lock +772 -0
- package/package.json +55 -4
- package/skills/THIRD_PARTY.md +58 -0
- package/skills/code-review/SKILL.md +46 -0
- package/skills/deliberate/SKILL.md +39 -0
- package/skills/diagnosing-bugs/SKILL.md +75 -0
- package/skills/receiving-code-review/SKILL.md +36 -0
- package/skills/research/SKILL.md +18 -0
- package/skills/tdd/SKILL.md +56 -0
- package/skills/tdd/mocking.md +59 -0
- package/skills/tdd/tests.md +77 -0
- package/skills/verification-before-completion/SKILL.md +50 -0
- package/src/agenda.ts +56 -0
- package/src/agents.ts +74 -0
- package/src/ask.ts +243 -0
- package/src/backends.ts +144 -0
- package/src/backlog.ts +436 -0
- package/src/bench.ts +163 -0
- package/src/board.ts +1 -0
- package/src/brief.ts +151 -0
- package/src/browser.ts +624 -0
- package/src/budget.ts +73 -0
- package/src/catalog.ts +214 -0
- package/src/checkchange.ts +180 -0
- package/src/checkpoint.ts +99 -0
- package/src/cli.ts +117 -0
- package/src/compact.ts +162 -0
- package/src/config.ts +340 -0
- package/src/context/capture.ts +1 -0
- package/src/context/digest.ts +1 -0
- package/src/context/embed.ts +1 -0
- package/src/context/index.ts +2 -0
- package/src/context/ledger.ts +1 -0
- package/src/context/manager.ts +1 -0
- package/src/context/memory.ts +1 -0
- package/src/context/policy.ts +1 -0
- package/src/context/prices.ts +1 -0
- package/src/context/recall.ts +1 -0
- package/src/context/rules.ts +1 -0
- package/src/context/search.ts +1 -0
- package/src/context/status.ts +1 -0
- package/src/context/store.ts +1 -0
- package/src/context/summary.ts +1 -0
- package/src/context/tokens.ts +1 -0
- package/src/context/tools.ts +1 -0
- package/src/continuation.ts +122 -0
- package/src/contract.ts +342 -0
- package/src/contractround.ts +225 -0
- package/src/control.ts +1 -0
- package/src/dataset.ts +133 -0
- package/src/decide.ts +690 -0
- package/src/eval.ts +66 -0
- package/src/evolver.ts +369 -0
- package/src/followups.ts +50 -0
- package/src/forms.ts +191 -0
- package/src/gate.ts +90 -0
- package/src/grants.ts +239 -0
- package/src/graph.ts +74 -0
- package/src/guard/index.ts +1 -0
- package/src/guard/wire.ts +1 -0
- package/src/helpers.ts +105 -0
- package/src/hooks.ts +199 -0
- package/src/integration.ts +4 -0
- package/src/jev.ts +141 -0
- package/src/journal.ts +1 -0
- package/src/look.ts +390 -0
- package/src/memory.ts +1 -0
- package/src/native.ts +2455 -0
- package/src/nogit.ts +112 -0
- package/src/outcomes.ts +210 -0
- package/src/pick.ts +112 -0
- package/src/pipelines.ts +252 -0
- package/src/plan.ts +449 -0
- package/src/plugin.ts +3187 -0
- package/src/policy.ts +84 -0
- package/src/pools.ts +1 -0
- package/src/profile.ts +186 -0
- package/src/progress.ts +1 -0
- package/src/redact.ts +1 -0
- package/src/registry.ts +207 -0
- package/src/roles.ts +1 -0
- package/src/routing.ts +1 -0
- package/src/runner.ts +1506 -0
- package/src/sandbox.ts +4 -0
- package/src/scheduler.ts +200 -0
- package/src/session.ts +269 -0
- package/src/shell/decide.ts +1 -0
- package/src/shell/index.ts +2 -0
- package/src/shell/sandbox.ts +1 -0
- package/src/shell/wire.ts +1 -0
- package/src/skills.ts +111 -0
- package/src/state.ts +1 -0
- package/src/store.ts +1 -0
- package/src/switcher.ts +1 -0
- package/src/tools.ts +480 -0
- package/src/trials.ts +247 -0
- package/src/usage.ts +1 -0
- package/src/wait.ts +180 -0
- package/src/watchdog.ts +498 -0
- package/src/worktree/git.ts +378 -0
package/src/helpers.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// Helpers: a worker that writes asks an explorer or a researcher one question and gets the answer back as a tool result
|
|
2
|
+
// (the explore and research tools).
|
|
3
|
+
//
|
|
4
|
+
// A writer used to have no way to learn anything its own tools did not reach: no web (a coder that needed an API's
|
|
5
|
+
// docs could not read them) and no sub-agents (only the micromanager delegates, so it can count the workers). Opening
|
|
6
|
+
// the subagent tool to workers would make every such call a new task of the job, with a worktree, a merge and a place
|
|
7
|
+
// on the micromanager's list. A helper is none of that: it reads in the asking writer's own directory (so it sees the
|
|
8
|
+
// writer's uncommitted change), it runs while the writer waits, it cannot ask a helper of its own, and a writer gets a
|
|
9
|
+
// few per task. The plugin treats it like any other worker: the permission hook, the guards on web content, the watchdog
|
|
10
|
+
// and the journal. It is not shown under the writer: the plugin's session.create drops parentID (opencode 2.0.18 passes
|
|
11
|
+
// only id, title, agent, model, metadata, permissions and location on), so the title and the metadata name the writer.
|
|
12
|
+
|
|
13
|
+
import { lastAssistantText } from "./brief.ts"
|
|
14
|
+
import { redact } from "./jev.ts"
|
|
15
|
+
import { roleById, type Role } from "./roles.ts"
|
|
16
|
+
import { toModel, type SessionApi } from "./runner.ts"
|
|
17
|
+
|
|
18
|
+
export type HelperRole = "explore" | "research"
|
|
19
|
+
export const HELPER_ROLES: readonly HelperRole[] = ["explore", "research"]
|
|
20
|
+
export const MAX_HELPERS = 3 // per writer session: enough for the questions a task raises, not a second job
|
|
21
|
+
export const HELPER_STEPS = 60
|
|
22
|
+
const MAX_ANSWER = 12_000
|
|
23
|
+
const TIMEOUT_MS = 10 * 60_000
|
|
24
|
+
|
|
25
|
+
// The writer asking, as the plugin knows it.
|
|
26
|
+
export interface Caller {
|
|
27
|
+
readonly dir: string // where the writer works: the helper reads there
|
|
28
|
+
readonly label: string // "T3 of job-…", for the helper's title and brief
|
|
29
|
+
readonly task: string // what the writer is working on
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface HelperDeps {
|
|
33
|
+
readonly session: SessionApi
|
|
34
|
+
readonly caller: (sessionID: string) => Caller | undefined // undefined: not a writer of the micromanager
|
|
35
|
+
readonly model: (role: Role) => Promise<string>
|
|
36
|
+
// The helper session exists: the plugin registers it with the permission hook and the watchdog before its first turn.
|
|
37
|
+
readonly started: (helper: string, caller: string, dir: string, brief: string) => void
|
|
38
|
+
readonly ended?: (helper: string) => void
|
|
39
|
+
readonly journal?: (entry: Record<string, unknown>) => void
|
|
40
|
+
readonly timeoutMs?: number
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function helperBrief(role: HelperRole, caller: Caller, question: string): string {
|
|
44
|
+
const how = role === "explore"
|
|
45
|
+
? "Answer it from the code and the git history in your directory, which holds the worker's change as it is right now, uncommitted edits included."
|
|
46
|
+
: "Look it up in the documentation and sources on the web, and in your directory where it helps; cite the URLs you used."
|
|
47
|
+
return [
|
|
48
|
+
`A worker on ${caller.label} asks you one question and waits for your answer. Its task: ${caller.task}`,
|
|
49
|
+
"",
|
|
50
|
+
`Question: ${question}`,
|
|
51
|
+
"",
|
|
52
|
+
how,
|
|
53
|
+
"Answer only what was asked, as briefly as the answer allows: paths with line numbers and short excerpts, at most about 40 lines. Change nothing. If you cannot answer it, say what you found and what is missing.",
|
|
54
|
+
].join("\n")
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export class Helpers {
|
|
58
|
+
readonly #d: HelperDeps
|
|
59
|
+
readonly #used = new Map<string, number>()
|
|
60
|
+
|
|
61
|
+
constructor(deps: HelperDeps) {
|
|
62
|
+
this.#d = deps
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async ask(callerSession: string, role: HelperRole, question: string): Promise<string> {
|
|
66
|
+
const caller = this.#d.caller(callerSession)
|
|
67
|
+
if (!caller) return "Only a worker that changes files for the micromanager may ask a helper."
|
|
68
|
+
if (!question.trim()) return "Ask one question, in a sentence."
|
|
69
|
+
const used = this.#used.get(callerSession) ?? 0
|
|
70
|
+
if (used >= MAX_HELPERS) {
|
|
71
|
+
return `You asked ${MAX_HELPERS} helpers in this task already. Work with what you have, and name what you still need in your report.`
|
|
72
|
+
}
|
|
73
|
+
this.#used.set(callerSession, used + 1)
|
|
74
|
+
const r = roleById(role)!
|
|
75
|
+
const model = await this.#d.model(r)
|
|
76
|
+
const brief = helperBrief(role, caller, question.trim())
|
|
77
|
+
const s = await this.#d.session.create({
|
|
78
|
+
title: `${caller.label} · ${r.agent} · ${question.trim().slice(0, 60)}`,
|
|
79
|
+
agent: r.agent,
|
|
80
|
+
model: toModel(model),
|
|
81
|
+
location: { directory: caller.dir },
|
|
82
|
+
metadata: { micromanager: { helperOf: callerSession, task: caller.label } },
|
|
83
|
+
})
|
|
84
|
+
this.#d.started(s.id, callerSession, caller.dir, brief)
|
|
85
|
+
const start = Date.now()
|
|
86
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
87
|
+
let timedOut = false
|
|
88
|
+
try {
|
|
89
|
+
await this.#d.session.prompt({ sessionID: s.id, text: redact(brief) })
|
|
90
|
+
timedOut = await Promise.race([
|
|
91
|
+
this.#d.session.wait({ sessionID: s.id }).then(() => false),
|
|
92
|
+
new Promise<boolean>((resolve) => { timer = setTimeout(() => resolve(true), this.#d.timeoutMs ?? TIMEOUT_MS) }),
|
|
93
|
+
])
|
|
94
|
+
} finally {
|
|
95
|
+
clearTimeout(timer)
|
|
96
|
+
}
|
|
97
|
+
if (timedOut) await this.#d.session.interrupt({ sessionID: s.id }).catch(() => undefined)
|
|
98
|
+
const answer = lastAssistantText(await this.#d.session.context({ sessionID: s.id }).catch(() => undefined))
|
|
99
|
+
this.#d.ended?.(s.id)
|
|
100
|
+
this.#d.journal?.({ kind: "helper", sessionID: callerSession, helper: s.id, role, model, question: question.slice(0, 300), chars: answer.length, ms: Date.now() - start, timedOut })
|
|
101
|
+
if (!answer) return `The ${r.agent} ended without an answer${timedOut ? " (it ran out of time)" : ""}. Go on without it, and name the open question in your report.`
|
|
102
|
+
const cut = answer.length > MAX_ANSWER ? `${answer.slice(0, MAX_ANSWER)}\n[cut at ${MAX_ANSWER} characters]` : answer
|
|
103
|
+
return `${timedOut ? "[the helper ran out of time; this is what it had]\n" : ""}${cut}`
|
|
104
|
+
}
|
|
105
|
+
}
|
package/src/hooks.ts
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
// Before the micromanager's subagent call runs: the routed model goes into the call, and the call runs in the
|
|
2
|
+
// background (OpenCode notifies the micromanager when the worker ends; src/native.ts keeps writers in worktrees).
|
|
3
|
+
|
|
4
|
+
import { chooseModel, guessNeeds, type Available, type Routing, type Spread } from "./routing.ts"
|
|
5
|
+
import { MICROMANAGER, ROLES, roleByAgent, type Role, type RoleId } from "./roles.ts"
|
|
6
|
+
|
|
7
|
+
export interface ToolCall {
|
|
8
|
+
tool: string
|
|
9
|
+
agent?: string // the agent making the call
|
|
10
|
+
sessionID?: string
|
|
11
|
+
input: Record<string, unknown>
|
|
12
|
+
result?: unknown // tool.execute.after: what the model will see
|
|
13
|
+
error?: unknown // tool.execute.after: why the tool failed (a hook's throw lands here too)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// The tool a delegation arrives under.
|
|
17
|
+
//
|
|
18
|
+
// Read in the bundled server (~/.opencode/bin/opencode, the @opencode/Tool service): opencode registers exactly one
|
|
19
|
+
// delegation tool, `subagent`, and it carries `options.codemode:false`. That one flag settles the question of a
|
|
20
|
+
// nested shape — Tool.snapshot builds the code-mode catalog from the tools whose codemode option is not false, so a
|
|
21
|
+
// delegation cannot be issued from inside the code-mode `execute` tool at all. There is no second shape to
|
|
22
|
+
// recognise, and parsing the code string would be a parser for an event that cannot happen.
|
|
23
|
+
//
|
|
24
|
+
// (Nested calls that do exist are surfaced individually with their real tool name, so the hook sees them normally:
|
|
25
|
+
// Tool.execute triggers tool.execute.before per nested call, with that call's own name, input and the outer
|
|
26
|
+
// session's id and agent. The same code path also confirms the fields a call carries: tool, sessionID, agent,
|
|
27
|
+
// messageID, id and input.)
|
|
28
|
+
export const DELEGATION_TOOL = "subagent"
|
|
29
|
+
|
|
30
|
+
// Every tool name the delegation hook recognises, so the setup record can name them (a field operator has to be able
|
|
31
|
+
// to read off the journal what the hook thinks it is looking at).
|
|
32
|
+
export const DELEGATION_TOOLS: readonly string[] = [DELEGATION_TOOL]
|
|
33
|
+
|
|
34
|
+
export interface Routed {
|
|
35
|
+
readonly role: RoleId
|
|
36
|
+
readonly model: string
|
|
37
|
+
readonly reason: string
|
|
38
|
+
// The chain entries the host cannot run. Absent unless the catalog said so, so an ordinary delegation is unchanged.
|
|
39
|
+
readonly skipped?: readonly string[]
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const HEADER = "[micromanager]"
|
|
43
|
+
|
|
44
|
+
// The role a call delegates to, when it delegates to one of this plugin's own role agents; undefined otherwise.
|
|
45
|
+
//
|
|
46
|
+
// The caller is deliberately not part of the question, and that is the whole fix for a live run of 2026-10-06: the
|
|
47
|
+
// caller used to have to be the micromanager agent, the user switched the parent session's agent from `micromanager`
|
|
48
|
+
// to `build`, and the ten delegations that followed (coder, tester, explorer, oracle) all returned early from that
|
|
49
|
+
// one check. No task, no worktree, no claim, no move, no gate, no merge and not one word of it — the workers ran in
|
|
50
|
+
// the user's checkout. The agent is a property of the session the user happens to be sitting in, not a property of
|
|
51
|
+
// the delegation.
|
|
52
|
+
//
|
|
53
|
+
// What makes widening this safe is that the discriminator is the *target*: a target that is one of this plugin's own
|
|
54
|
+
// agents cannot be anything but a delegation, and routing one can only ever narrow what the worker may do. A built-in
|
|
55
|
+
// agent (`explore`, `general`) is not one of ours, so nothing is routed on its behalf and the call is left exactly as
|
|
56
|
+
// its caller wrote it.
|
|
57
|
+
export function delegationRole(call: ToolCall): Role | undefined {
|
|
58
|
+
if (!DELEGATION_TOOLS.includes(call.tool)) return undefined
|
|
59
|
+
return roleByAgent(String(call.input.agent ?? ""))
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Whether this call is a delegation attempt the plugin cannot isolate: the subagent tool, aimed at an agent it does
|
|
63
|
+
// not own or at none. It is worth a journal record of its own, because a delegation that reached neither the routing
|
|
64
|
+
// nor a refusal is precisely how a live run wrote into the user's checkout in silence.
|
|
65
|
+
export function unownedDelegation(call: ToolCall): boolean {
|
|
66
|
+
return call.tool === DELEGATION_TOOL && !roleByAgent(String(call.input.agent ?? ""))
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Rewrites `call.input` in place; returns what was decided, or undefined when the call is not the micromanager's.
|
|
70
|
+
export async function routeSubagentCall(call: ToolCall, routing: Routing, workerModel?: string, available?: Available, spread?: Spread): Promise<Routed | undefined> {
|
|
71
|
+
if (call.tool !== DELEGATION_TOOL || call.agent !== MICROMANAGER.agent) return undefined
|
|
72
|
+
return await route(call, routing, workerModel, available, spread)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// The same routing for a delegation that the micromanager agent did not make. Narrower than the caller alone would
|
|
76
|
+
// suggest and never wider: it returns nothing at all for a tool that is not the delegation tool.
|
|
77
|
+
export async function routeDelegation(call: ToolCall, routing: Routing, workerModel?: string, available?: Available, spread?: Spread): Promise<Routed | undefined> {
|
|
78
|
+
if (call.tool !== DELEGATION_TOOL) return undefined
|
|
79
|
+
return await route(call, routing, workerModel, available, spread)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async function route(call: ToolCall, routing: Routing, workerModel?: string, available?: Available, spread?: Spread): Promise<Routed | undefined> {
|
|
83
|
+
const role = roleByAgent(String(call.input.agent ?? ""))
|
|
84
|
+
if (!role) return undefined // a built-in agent: left as the caller asked
|
|
85
|
+
const prompt = String(call.input.prompt ?? "")
|
|
86
|
+
const choice = await chooseModel(role, guessNeeds(prompt), routing, workerModel, available, spread)
|
|
87
|
+
call.input.model = choice.model
|
|
88
|
+
call.input.background = true
|
|
89
|
+
if (!prompt.startsWith(HEADER)) {
|
|
90
|
+
call.input.prompt = `${HEADER} role: ${role.id} · model: ${choice.model}\n\n${prompt}`
|
|
91
|
+
}
|
|
92
|
+
// skipped travels with the decision so the plugin can journal why this model and not the first of the chain.
|
|
93
|
+
return { role: role.id, model: choice.model, reason: choice.reason, ...(choice.skipped ? { skipped: choice.skipped } : {}) }
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// --- the backstop: a child of a micromanager session that no worktree ever got must not be able to write ---
|
|
97
|
+
|
|
98
|
+
// The write-capable actions a permission evaluation can carry. The names are the ones opencode asserts for its own
|
|
99
|
+
// tools (opencode.tool.edit and opencode.tool.patch both assert action "edit", the shell tool asserts "shell"), plus
|
|
100
|
+
// the code-mode `execute` tool, which reaches everything else: a worker that may run `execute` may run a shell inside
|
|
101
|
+
// it. Anything not listed here is a read, and a read cannot damage the checkout.
|
|
102
|
+
const WRITES = new Set(["shell", "edit", "write", "patch", "execute"])
|
|
103
|
+
|
|
104
|
+
// What the permission hook knows about a session before it lets anything change the tree.
|
|
105
|
+
//
|
|
106
|
+
// `parent` is the session that started it — OpenCode's own Session record, whose parentID is in the SDK's Session
|
|
107
|
+
// type — and `parentIsMicromanager` says whether that parent is a session this plugin owns. The relation is the
|
|
108
|
+
// discriminator because it is the one thing a delegation cannot fake and cannot lose: a plugin-recognised delegation
|
|
109
|
+
// rewrites the child's prompt, and an unrecognised one does not, so the prompt text cannot tell them apart (and a
|
|
110
|
+
// child's prompt is not even visible to the permission hook).
|
|
111
|
+
export interface ChildSession {
|
|
112
|
+
readonly sessionID: string
|
|
113
|
+
readonly directory?: string // where the session would write
|
|
114
|
+
readonly parent?: string // the session that started it
|
|
115
|
+
readonly parentIsMicromanager: boolean // that parent is a session this plugin owns
|
|
116
|
+
readonly job?: string // the job of that parent, for the journal line
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export interface Denial {
|
|
120
|
+
readonly reason: string // greppable, and says the thing that failed
|
|
121
|
+
readonly message: string // what the agent reads instead of its write
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// The refusal an unclaimed child of a micromanager session gets instead of its write, and undefined for every other
|
|
125
|
+
// session and every other action.
|
|
126
|
+
//
|
|
127
|
+
// This is the part that holds when the delegation hook did not engage, which is why it does not depend on knowing how
|
|
128
|
+
// the delegation was made: it asks only whether this session is a child of a session the plugin owns and whether the
|
|
129
|
+
// plugin claimed it as a worker. The live run this exists for had no task, no worktree, no claim and no session move,
|
|
130
|
+
// and nothing complained — the children were ordinary sessions of a micromanager session, and every one of their
|
|
131
|
+
// writes went straight into the user's checkout.
|
|
132
|
+
//
|
|
133
|
+
// It never keys on the agent name. A user who picks a role agent by hand and works in their own checkout is not a
|
|
134
|
+
// delegated child — it is a root session, or a child of a session that is not the plugin's — and keeps working
|
|
135
|
+
// exactly as before. Only a session that was *started by* a micromanager session is ever caught.
|
|
136
|
+
export function backstop(facts: ChildSession, action: string, claimed: boolean): Denial | undefined {
|
|
137
|
+
if (claimed || !facts.parent || !facts.parentIsMicromanager) return undefined
|
|
138
|
+
if (!WRITES.has(action)) return undefined
|
|
139
|
+
return {
|
|
140
|
+
reason: "isolation did not engage: a child of a micromanager session that is not a claimed worker",
|
|
141
|
+
message: `Stopped: you were started as a sub-agent of micromanager session ${facts.parent}${facts.job ? ` (job ${facts.job})` : ""}, but the plugin never gave you a task and never put you in a worktree, so nothing of yours is checked and nothing of yours can be merged. You are in ${facts.directory ?? "a directory the plugin does not know"}; writing there would change the user's own checkout. Do not work around this and do not write anything. Stop now and report: STATUS: NEEDS_CONTEXT — say that the delegation was not isolated, that you were never placed in a worktree, and what you were asked to do.`,
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Why a call of the micromanager was not routed, when it is about the agent name at all; undefined for every other
|
|
146
|
+
// reason routeSubagentCall returns nothing (a call that is not the micromanager's, or one it already routed).
|
|
147
|
+
//
|
|
148
|
+
// routeSubagentCall leaves a built-in agent alone on purpose (it routes nothing it does not own), so the caller got
|
|
149
|
+
// "Not started: use one of the role agents (coder, …)" for `explore` and looked for a `explore` role agent that does
|
|
150
|
+
// not exist. The two cases need different fixes: rename the agent, or install the plugin's agents.
|
|
151
|
+
export function notRouted(call: ToolCall): string | undefined {
|
|
152
|
+
if (call.tool !== "subagent" || call.agent !== MICROMANAGER.agent) return undefined
|
|
153
|
+
const asked = String(call.input.agent ?? "").trim()
|
|
154
|
+
if (roleByAgent(asked)) return undefined // a role agent: routed, or nothing to say about the name
|
|
155
|
+
if (asked) {
|
|
156
|
+
return `"${asked}" is not a role agent, so nothing was started. Use one of this plugin's role agents (${ROLES.map((r) => r.agent).join(", ")}); OpenCode's own agents (explore, general, …) are not role agents here.`
|
|
157
|
+
}
|
|
158
|
+
return `No agent was named, so nothing was started. Name one of this plugin's role agents (${ROLES.map((r) => r.agent).join(", ")}).`
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// The text of a tool result as the model sees it: result.content[] text parts, plus string fields of result.output
|
|
162
|
+
// (shapes seen in the Phase E spike for read, shell, execute and custom tools).
|
|
163
|
+
function textSlots(result: unknown): { get(): string; set(v: string): void }[] {
|
|
164
|
+
const slots: { get(): string; set(v: string): void }[] = []
|
|
165
|
+
const r = result as { content?: unknown; output?: unknown } | undefined
|
|
166
|
+
if (!r || typeof r !== "object") return slots
|
|
167
|
+
if (Array.isArray(r.content)) {
|
|
168
|
+
for (const part of r.content as { text?: unknown }[]) {
|
|
169
|
+
if (part && typeof part.text === "string") slots.push({ get: () => part.text as string, set: (v) => { part.text = v } })
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (typeof r.output === "string") slots.push({ get: () => r.output as string, set: (v) => { r.output = v } })
|
|
173
|
+
else if (r.output && typeof r.output === "object") {
|
|
174
|
+
const o = r.output as Record<string, unknown>
|
|
175
|
+
for (const key of ["output", "content", "text"]) {
|
|
176
|
+
if (typeof o[key] === "string") slots.push({ get: () => o[key] as string, set: (v) => { o[key] = v } })
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return slots
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function resultText(result: unknown): string {
|
|
183
|
+
return textSlots(result).map((s) => s.get()).join("\n")
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Rewrites a tool result in place before the model sees it (tool.execute.after is awaited, Phase E spike); returns
|
|
187
|
+
// whether anything changed.
|
|
188
|
+
export function rewriteResult(result: unknown, fn: (text: string) => string): boolean {
|
|
189
|
+
let changed = false
|
|
190
|
+
for (const slot of textSlots(result)) {
|
|
191
|
+
const before = slot.get()
|
|
192
|
+
const after = fn(before)
|
|
193
|
+
if (after !== before) {
|
|
194
|
+
slot.set(after)
|
|
195
|
+
changed = true
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return changed
|
|
199
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
// The worktree handling lives in src/worktree/ now (the worktree plugin, enable-able on its own). This file keeps the
|
|
2
|
+
// old import path working for the parts of the plugin that are not the worktree plugin: the same names, from one place.
|
|
3
|
+
|
|
4
|
+
export * from "./worktree/git.ts"
|
package/src/jev.ts
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// TypeSafe Jev (System One): program state + typed questions in, typed answers with probabilities out.
|
|
2
|
+
// Wire format: POST https://api.typesafe.ai/v1/systemone {model, state, questions} -> {model, answers, usage}
|
|
3
|
+
// (the same shape the oc-voice project uses in decision/systemone.py).
|
|
4
|
+
|
|
5
|
+
import { execFileSync } from "node:child_process"
|
|
6
|
+
|
|
7
|
+
import { mapStrings, scrub } from "./redact.ts"
|
|
8
|
+
|
|
9
|
+
export type Question =
|
|
10
|
+
| { type: "choice"; instructions: string; criteria: Record<string, string> } // option id -> meaning
|
|
11
|
+
| { type: "score"; instructions: string; criteria: string[] } // rubric levels, lowest first
|
|
12
|
+
| { type: "noul"; instructions: string } // a statement; Jev returns the probability that it is true
|
|
13
|
+
|
|
14
|
+
export type Answer =
|
|
15
|
+
| { type: "choice"; choice: string; confidence: number; probabilities: Record<string, number> }
|
|
16
|
+
| { type: "score"; score: number; confidence: number; probabilities: Record<string, number> }
|
|
17
|
+
| { type: "noul"; noul: number }
|
|
18
|
+
|
|
19
|
+
export interface Decision {
|
|
20
|
+
readonly answers: Record<string, Answer>
|
|
21
|
+
readonly model: string
|
|
22
|
+
readonly latencyMs: number
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class JevError extends Error {}
|
|
26
|
+
|
|
27
|
+
export interface JevOptions {
|
|
28
|
+
readonly url?: string
|
|
29
|
+
readonly model?: string
|
|
30
|
+
readonly timeoutMs?: number
|
|
31
|
+
readonly retries?: number // on 429/529 only
|
|
32
|
+
readonly maxStateChars?: number
|
|
33
|
+
readonly fetch?: typeof fetch
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// The key: JEV_API_KEY, else the desktop keyring (secret-tool store --label="TypeSafe Jev API key" service typesafe key jev).
|
|
37
|
+
export function apiKey(env: NodeJS.ProcessEnv = process.env): string | undefined {
|
|
38
|
+
if (env.JEV_API_KEY) return env.JEV_API_KEY
|
|
39
|
+
try {
|
|
40
|
+
const key = execFileSync("secret-tool", ["lookup", "service", "typesafe", "key", "jev"], {
|
|
41
|
+
encoding: "utf8",
|
|
42
|
+
timeout: 3000,
|
|
43
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
44
|
+
}).trim()
|
|
45
|
+
return key || undefined
|
|
46
|
+
} catch {
|
|
47
|
+
return undefined
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Private data is swapped for the vault's placeholders before the state leaves the machine (src/redact.ts); with
|
|
52
|
+
// redaction off, keys and tokens are still masked.
|
|
53
|
+
export function redact(text: string): string {
|
|
54
|
+
return scrub(text)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// The question texts carry the same private data the state does, so the wire body gets a redacted copy. The caller's
|
|
58
|
+
// questions stay as they are: the ask flow journals them, and the local backends read them too.
|
|
59
|
+
function wireQuestions(questions: Record<string, Question>): Record<string, Question> {
|
|
60
|
+
const copy = JSON.parse(JSON.stringify(questions)) as Record<string, Question>
|
|
61
|
+
mapStrings(copy, redact)
|
|
62
|
+
return copy
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export class Jev {
|
|
66
|
+
readonly #key: string
|
|
67
|
+
readonly #url: string
|
|
68
|
+
readonly #model: string
|
|
69
|
+
readonly #timeoutMs: number
|
|
70
|
+
readonly #retries: number
|
|
71
|
+
readonly #maxStateChars: number
|
|
72
|
+
readonly #fetch: typeof fetch
|
|
73
|
+
|
|
74
|
+
constructor(key: string, options: JevOptions = {}) {
|
|
75
|
+
this.#key = key
|
|
76
|
+
this.#url = options.url ?? "https://api.typesafe.ai/v1/systemone"
|
|
77
|
+
this.#model = options.model ?? "jev-latest"
|
|
78
|
+
this.#timeoutMs = options.timeoutMs ?? 3000
|
|
79
|
+
this.#retries = options.retries ?? 1
|
|
80
|
+
this.#maxStateChars = options.maxStateChars ?? 24000
|
|
81
|
+
this.#fetch = options.fetch ?? fetch
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async ask(state: string, questions: Record<string, Question>): Promise<Decision> {
|
|
85
|
+
// The newest part of the state is at the end; a cut keeps it.
|
|
86
|
+
const cut = state.length > this.#maxStateChars ? state.slice(-this.#maxStateChars) : state
|
|
87
|
+
const body = JSON.stringify({ model: this.#model, state: redact(cut), questions: wireQuestions(questions) })
|
|
88
|
+
const started = Date.now()
|
|
89
|
+
for (let attempt = 0; ; attempt++) {
|
|
90
|
+
let response: Response
|
|
91
|
+
try {
|
|
92
|
+
response = await this.#fetch(this.#url, {
|
|
93
|
+
method: "POST",
|
|
94
|
+
headers: { Authorization: `Bearer ${this.#key}`, "Content-Type": "application/json" },
|
|
95
|
+
body,
|
|
96
|
+
signal: AbortSignal.timeout(this.#timeoutMs),
|
|
97
|
+
})
|
|
98
|
+
} catch (err) {
|
|
99
|
+
throw new JevError(`Jev unreachable: ${err instanceof Error ? err.name : String(err)}`)
|
|
100
|
+
}
|
|
101
|
+
if ((response.status === 429 || response.status === 529) && attempt < this.#retries) {
|
|
102
|
+
await new Promise((resolve) => setTimeout(resolve, 200 * 2 ** attempt))
|
|
103
|
+
continue
|
|
104
|
+
}
|
|
105
|
+
if (response.status === 401) throw new JevError("Jev: API key rejected (401)")
|
|
106
|
+
if (response.status !== 200) throw new JevError(`Jev: HTTP ${response.status}`)
|
|
107
|
+
const payload = (await response.json()) as { model?: string; answers?: Record<string, unknown> }
|
|
108
|
+
return { answers: parseAnswers(questions, payload.answers ?? {}), model: payload.model ?? this.#model, latencyMs: Date.now() - started }
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function num(value: unknown, what: string): number {
|
|
114
|
+
if (typeof value !== "number" || Number.isNaN(value)) throw new JevError(`Jev: missing numeric ${what}`)
|
|
115
|
+
return value
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function probs(value: unknown): Record<string, number> {
|
|
119
|
+
if (!value || typeof value !== "object") return {}
|
|
120
|
+
return Object.fromEntries(Object.entries(value).filter(([, v]) => typeof v === "number")) as Record<string, number>
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function parseAnswers(questions: Record<string, Question>, raw: Record<string, unknown>): Record<string, Answer> {
|
|
124
|
+
const answers: Record<string, Answer> = {}
|
|
125
|
+
for (const [id, question] of Object.entries(questions)) {
|
|
126
|
+
const data = raw[id] as Record<string, unknown> | undefined
|
|
127
|
+
if (!data) continue
|
|
128
|
+
if (question.type === "choice") {
|
|
129
|
+
const choice = data.choice
|
|
130
|
+
if (typeof choice !== "string" || !(choice in question.criteria)) throw new JevError(`Jev: ${id}: choice not offered`)
|
|
131
|
+
answers[id] = { type: "choice", choice, confidence: num(data.confidence, `${id}.confidence`), probabilities: probs(data.probabilities) }
|
|
132
|
+
} else if (question.type === "score") {
|
|
133
|
+
answers[id] = { type: "score", score: num(data.score, `${id}.score`), confidence: num(data.confidence, `${id}.confidence`), probabilities: probs(data.probabilities) }
|
|
134
|
+
} else {
|
|
135
|
+
const noul = num(data.noul, `${id}.noul`)
|
|
136
|
+
if (noul < 0 || noul > 1) throw new JevError(`Jev: ${id}: noul outside 0..1`)
|
|
137
|
+
answers[id] = { type: "noul", noul }
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return answers
|
|
141
|
+
}
|
package/src/journal.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@omacode/core/journal"
|