@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/dist/grants.js
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
// A worker's request for a variable or a host the sandbox keeps from it (src/shell/sandbox.ts). A test that needs
|
|
2
|
+
// DATABASE_URL, or a build that fetches from a host beyond the package registries, used to fail with no way to ask.
|
|
3
|
+
// Now the worker calls request_env or request_network with the name and why; the plugin decides in steps:
|
|
4
|
+
// - never: a variable on the never list (sockets into the desktop, agent sockets, the plugin's own credentials), a
|
|
5
|
+
// variable that is not set where the server runs, or a name or host refused before in this job;
|
|
6
|
+
// - the decider: a variable that is a setting, not a secret (NODE_ENV, RUST_LOG), or a public host while the job
|
|
7
|
+
// holds no secret, when the decider is sure;
|
|
8
|
+
// - the user, with a form through the micromanager: everything else, and always a secret-looking variable or a host
|
|
9
|
+
// asked for while the job's commands hold a secret (the form names it: a secret and an open host together are what
|
|
10
|
+
// it takes to send the secret away).
|
|
11
|
+
// A grant holds for the job and is kept in its control file (src/control.ts), names and hosts only. The worker sees a
|
|
12
|
+
// variable's name, never its value, and no model ever does: the decider gets the value's shape at most.
|
|
13
|
+
import { formInput, grantBatchForm, grantForm } from "./forms.js";
|
|
14
|
+
import { ENV_ALLOW, looksSecret, neverHanded, REGISTRIES } from "./sandbox.js";
|
|
15
|
+
export const GRANT_TIMEOUT_MS = 10 * 60_000;
|
|
16
|
+
// How sure the decider must be that a variable is a setting, or a host harmless, to grant it without the user.
|
|
17
|
+
export const GRANT_FLOOR = 0.9;
|
|
18
|
+
const NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
19
|
+
// A host, or "*." and a host of at least two labels ("*.com" is no host).
|
|
20
|
+
const DOMAIN = /^(\*\.)?([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}$/;
|
|
21
|
+
// What is wrong with a requested name or host, or undefined when it is one.
|
|
22
|
+
export function badName(kind, name) {
|
|
23
|
+
if (kind === "env")
|
|
24
|
+
return NAME.test(name) ? undefined : `"${name}" is no variable name: letters, digits and _, not starting with a digit`;
|
|
25
|
+
return DOMAIN.test(name) ? undefined : `"${name}" is no host name: a name like api.example.com or *.example.com, without scheme, port or path`;
|
|
26
|
+
}
|
|
27
|
+
const matches = (name, pattern) => (pattern.endsWith("*") ? name.startsWith(pattern.slice(0, -1)) : name === pattern);
|
|
28
|
+
const covers = (host, pattern) => host === pattern || (pattern.startsWith("*.") && host.endsWith(pattern.slice(1)) && host !== pattern.slice(2));
|
|
29
|
+
export function triage(f) {
|
|
30
|
+
if (f.kind === "env") {
|
|
31
|
+
if (neverHanded(f.name))
|
|
32
|
+
return { verdict: "refused", text: `${f.name} is never handed to a worker command: it opens the user's desktop, an agent socket or the plugin's own credentials. Carry on without it.` };
|
|
33
|
+
if (f.value === undefined)
|
|
34
|
+
return { verdict: "refused", text: `${f.name} is not set where the server runs, so there is nothing to hand on. If your task needs it, say so in your report: the user has to set it.` };
|
|
35
|
+
if ([...ENV_ALLOW, ...f.open].some((p) => matches(f.name, p)))
|
|
36
|
+
return { verdict: "open", text: `${f.name} is handed to your commands already. If a command still misses it, it is not the environment: look at how the command reads it.` };
|
|
37
|
+
if (f.refused.includes(f.name))
|
|
38
|
+
return { verdict: "refused", text: `The user refused ${f.name} for this job. Do not ask again; carry on without it and say in your report what it was for.` };
|
|
39
|
+
if (looksSecret(f.name, f.value))
|
|
40
|
+
return { verdict: "user", reason: "its name or value looks like a secret" };
|
|
41
|
+
return { verdict: "decider", reason: "a setting, if the decider is sure it is no secret" };
|
|
42
|
+
}
|
|
43
|
+
if (!f.sandboxed)
|
|
44
|
+
return { verdict: "open", text: "Your commands run without a sandbox, so the network is not restricted. If a host is unreachable, it is not the plugin." };
|
|
45
|
+
if ([...REGISTRIES, ...f.open].some((p) => covers(f.name, p)))
|
|
46
|
+
return { verdict: "open", text: `${f.name} is open to your commands already. If it is still unreachable, the cause is elsewhere (DNS, the host, a proxy setting of the tool).` };
|
|
47
|
+
if (f.refused.includes(f.name))
|
|
48
|
+
return { verdict: "refused", text: `The user refused ${f.name} for this job. Do not ask again; carry on without it and say in your report what it was for.` };
|
|
49
|
+
if (f.secrets.length)
|
|
50
|
+
return { verdict: "user", reason: `the job's commands hold ${f.secrets.join(", ")}` };
|
|
51
|
+
return { verdict: "decider", reason: "a public host, if the decider is sure it sends nothing private" };
|
|
52
|
+
}
|
|
53
|
+
// What the decider may see of a value: its length and the kinds of characters, never the value itself.
|
|
54
|
+
export function valueShape(value) {
|
|
55
|
+
const kinds = [
|
|
56
|
+
/[a-z]/.test(value) && "lowercase letters", /[A-Z]/.test(value) && "uppercase letters", /\d/.test(value) && "digits",
|
|
57
|
+
/[^A-Za-z0-9]/.test(value) && "other characters",
|
|
58
|
+
].filter(Boolean);
|
|
59
|
+
return `${value.length} characters${kinds.length ? `: ${kinds.join(", ")}` : ""}`;
|
|
60
|
+
}
|
|
61
|
+
// The decider's question about a request it may decide: the statement it gives the probability of.
|
|
62
|
+
export function harmlessQuestion(kind) {
|
|
63
|
+
return kind === "env"
|
|
64
|
+
? { type: "noul", instructions: "This environment variable is a configuration setting (a mode, a log level, a flag, a path or a port), not a secret such as a token, key, password or a URL with credentials." }
|
|
65
|
+
: { type: "noul", instructions: "This host is a public documentation, package, source or API host that a build or a test of a coding project plausibly needs, and reaching it from the project's commands sends nothing private." };
|
|
66
|
+
}
|
|
67
|
+
export function grantState(kind, name, why, task, value) {
|
|
68
|
+
const what = kind === "env" ? `the environment variable ${name} (value: ${valueShape(value ?? "")})` : `network access to the host ${name}`;
|
|
69
|
+
return `A coding agent's shell commands run in a sandbox. Working on "${task}", it asks for ${what}. Its reason: ${why}`;
|
|
70
|
+
}
|
|
71
|
+
// The requests waiting for the user. The worker's tool call waits on its promise; the form's answer, the worker's end
|
|
72
|
+
// or the timeout settles it.
|
|
73
|
+
//
|
|
74
|
+
// The requests of one worker session wait together: a worker that needs a variable and a host asks for both while the
|
|
75
|
+
// first answer is still open, and the user sees one form with a question per item instead of two rounds of waiting.
|
|
76
|
+
// Every item keeps its own answer: the user may allow one and refuse the other.
|
|
77
|
+
export class GrantRequests {
|
|
78
|
+
#pending = new Map();
|
|
79
|
+
#journal;
|
|
80
|
+
#timeoutMs;
|
|
81
|
+
#n = 0;
|
|
82
|
+
constructor(journal = () => { }, timeoutMs = GRANT_TIMEOUT_MS) {
|
|
83
|
+
this.#journal = journal;
|
|
84
|
+
this.#timeoutMs = timeoutMs;
|
|
85
|
+
}
|
|
86
|
+
// A request for the same name of the same job that waits already: the second worker waits on the first one's form.
|
|
87
|
+
open(request) {
|
|
88
|
+
const same = [...this.#pending.values()].find((p) => p.project === request.project && p.jobId === request.jobId && p.kind === request.kind && p.name === request.name);
|
|
89
|
+
if (same)
|
|
90
|
+
return { pending: strip(same), answer: same.answer, fresh: false, batch: this.#batchOf(same.sessionID) };
|
|
91
|
+
const id = `G${++this.#n}`;
|
|
92
|
+
let settle;
|
|
93
|
+
const answer = new Promise((resolve) => (settle = resolve));
|
|
94
|
+
const waiting = { ...request, id, at: Date.now(), answer, settle };
|
|
95
|
+
waiting.timer = setTimeout(() => {
|
|
96
|
+
if (!this.#pending.delete(id))
|
|
97
|
+
return;
|
|
98
|
+
this.#journal({ kind: "grant", event: "timeout", id, grant: request.kind, name: request.name, sessionID: request.sessionID, job: request.jobId });
|
|
99
|
+
settle({ granted: false, by: "timeout" });
|
|
100
|
+
}, this.#timeoutMs);
|
|
101
|
+
waiting.timer.unref?.();
|
|
102
|
+
this.#pending.set(id, waiting);
|
|
103
|
+
this.#journal({ kind: "grant", event: "asked", id, grant: request.kind, name: request.name, why: request.why.slice(0, 300), sessionID: request.sessionID, job: request.jobId });
|
|
104
|
+
// The worker asked for this while another of its requests was still open: the user is asked about both at once, so
|
|
105
|
+
// the micromanager is told again about the whole batch.
|
|
106
|
+
const batch = this.#batchOf(request.sessionID);
|
|
107
|
+
return { pending: strip(waiting), answer, fresh: true, batch };
|
|
108
|
+
}
|
|
109
|
+
// The requests of one worker session that wait right now: the batch the user answers in one form.
|
|
110
|
+
#batchOf(sessionID) {
|
|
111
|
+
return [...this.#pending.values()].filter((p) => p.sessionID === sessionID).map(strip);
|
|
112
|
+
}
|
|
113
|
+
// The requests of one worker session that wait right now.
|
|
114
|
+
batch(sessionID) {
|
|
115
|
+
return this.#batchOf(sessionID);
|
|
116
|
+
}
|
|
117
|
+
get(id) {
|
|
118
|
+
const p = this.#pending.get(id);
|
|
119
|
+
return p && strip(p);
|
|
120
|
+
}
|
|
121
|
+
// The user's answer. Returns the request it settled, or undefined when none waits under this id any more.
|
|
122
|
+
answer(id, granted) {
|
|
123
|
+
const p = this.#take(id);
|
|
124
|
+
if (!p)
|
|
125
|
+
return undefined;
|
|
126
|
+
this.#journal({ kind: "grant", event: "answered", id, grant: p.kind, name: p.name, granted, sessionID: p.sessionID, job: p.jobId, waitedMs: Date.now() - p.at });
|
|
127
|
+
p.settle({ granted, by: "user" });
|
|
128
|
+
return strip(p);
|
|
129
|
+
}
|
|
130
|
+
// The user's answers to a batch: one answer per item, the items that wait under it. Returns the requests it settled.
|
|
131
|
+
answerBatch(answers) {
|
|
132
|
+
const settled = [];
|
|
133
|
+
for (const a of answers) {
|
|
134
|
+
if (a.granted === undefined)
|
|
135
|
+
continue; // typed text is no answer: the item keeps waiting
|
|
136
|
+
const p = this.answer(a.id, a.granted);
|
|
137
|
+
if (p)
|
|
138
|
+
settled.push(p);
|
|
139
|
+
}
|
|
140
|
+
return settled;
|
|
141
|
+
}
|
|
142
|
+
// The requests of workers that ended: refused quietly, nobody waits for them any more.
|
|
143
|
+
drop(sessionIDs) {
|
|
144
|
+
const dropped = [...this.#pending.values()].filter((p) => sessionIDs.includes(p.sessionID));
|
|
145
|
+
for (const p of dropped) {
|
|
146
|
+
this.#take(p.id);
|
|
147
|
+
this.#journal({ kind: "grant", event: "dropped", id: p.id, grant: p.kind, name: p.name, sessionID: p.sessionID, job: p.jobId });
|
|
148
|
+
p.settle({ granted: false, by: "timeout" });
|
|
149
|
+
}
|
|
150
|
+
return dropped.map(strip);
|
|
151
|
+
}
|
|
152
|
+
#take(id) {
|
|
153
|
+
const p = this.#pending.get(id);
|
|
154
|
+
if (!p)
|
|
155
|
+
return undefined;
|
|
156
|
+
this.#pending.delete(id);
|
|
157
|
+
clearTimeout(p.timer);
|
|
158
|
+
return p;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
function strip(p) {
|
|
162
|
+
const { settle: _s, timer: _t, answer: _a, ...shown } = p;
|
|
163
|
+
return shown;
|
|
164
|
+
}
|
|
165
|
+
// The note that wakes the micromanager: the form to put to the user, as it is.
|
|
166
|
+
export function grantNote(p, timeoutMs = GRANT_TIMEOUT_MS) {
|
|
167
|
+
const what = p.kind === "env" ? `the variable ${p.name}` : `the host ${p.name}`;
|
|
168
|
+
return `[micromanager] ${p.id}: worker ${p.task} waits for ${what} (${p.why.slice(0, 300)}). Only the user can grant it: ask them with the question tool, with exactly this input (a form that waits for their answer; it reaches the plugin by itself), then end your turn:\n${formInput(grantForm(p.id, p.kind, p.name, p.task, p.why, p.secrets))}\nWithout an answer it is refused in ${Math.round(timeoutMs / 60_000)} minutes and the worker carries on without it.`;
|
|
169
|
+
}
|
|
170
|
+
// The note that wakes the micromanager for a whole batch: the requests of one worker session that wait together. The
|
|
171
|
+
// user answers them in one form, each on its own, so one round of waiting covers them all.
|
|
172
|
+
export function grantBatchNote(batch, timeoutMs = GRANT_TIMEOUT_MS) {
|
|
173
|
+
if (batch.length === 1)
|
|
174
|
+
return grantNote(batch[0], timeoutMs);
|
|
175
|
+
const what = batch.map((p) => `${p.id}: ${p.kind === "env" ? `the variable ${p.name}` : `the host ${p.name}`} (${p.why.slice(0, 300)})`).join("; ");
|
|
176
|
+
const form = grantBatchForm(batch.map((p) => ({ id: p.id, kind: p.kind, name: p.name, why: p.why, secrets: p.secrets })), batch[0].task);
|
|
177
|
+
return `[micromanager] ${batch.length} requests of worker ${batch[0].task} wait together: ${what}. Only the user can grant them: ask them with the question tool, with exactly this input (one form with a question per request; it waits for their answer and reaches the plugin by itself), then end your turn:\n${formInput(form)}\nWithout an answer they are refused in ${Math.round(timeoutMs / 60_000)} minutes and the worker carries on without them.`;
|
|
178
|
+
}
|
|
179
|
+
// What the worker reads once its request is decided.
|
|
180
|
+
export function grantResult(kind, name, granted, by) {
|
|
181
|
+
if (granted) {
|
|
182
|
+
return kind === "env"
|
|
183
|
+
? `${name} is granted for this job (${by === "decider" ? "a setting" : "the user said yes"}): every shell command you start from now on has it. Run the command again.`
|
|
184
|
+
: `${name} is open for this job (${by === "decider" ? "a public host" : "the user said yes"}): every shell command you start from now on may reach it. Run the command again.`;
|
|
185
|
+
}
|
|
186
|
+
return by === "timeout"
|
|
187
|
+
? `${name} is not granted: nobody answered in time. Carry on without it, and say in your report what you needed it for. Report STATUS: NEEDS_CONTEXT only if you genuinely cannot proceed without it.`
|
|
188
|
+
: `${name} is refused for this job. Do not ask again or try it another way; carry on without it, and say in your report what you needed it for.`;
|
|
189
|
+
}
|
package/dist/graph.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// The code graph an explorer starts from: graphify's graph of the session worktree (graphify-out/), built from the
|
|
2
|
+
// tree-sitter AST without an LLM, so its call and import edges need no checking. `graphify update` re-extracts only
|
|
3
|
+
// what changed, so a later explorer of the same session waits seconds at most (jev-tutor, 92 files: 3.6 s cold).
|
|
4
|
+
//
|
|
5
|
+
// The worktree lives under the state directory, which the user's checkout already ignores; a .gitignore inside
|
|
6
|
+
// graphify-out/ keeps it out of the worktree's own git status too, so no commit of the session picks it up.
|
|
7
|
+
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
import { exec as defaultExec } from "./integration.js";
|
|
10
|
+
export const GRAPH_DIR = "graphify-out";
|
|
11
|
+
// Builds or updates the graph of dir; undefined when graphify is missing or fails (the explorer then works without).
|
|
12
|
+
export async function updateGraph(dir, run = defaultExec) {
|
|
13
|
+
const head = await run("git", ["rev-parse", "--short", "HEAD"], { cwd: dir });
|
|
14
|
+
if (head.code !== 0)
|
|
15
|
+
return undefined;
|
|
16
|
+
// --force: the graph follows the code, also when a refactor removed symbols (graphify keeps the bigger graph otherwise).
|
|
17
|
+
const r = await run("graphify", ["update", ".", "--force"], { cwd: dir, timeoutMs: 120_000 });
|
|
18
|
+
if (r.code !== 0 || r.timedOut || !existsSync(join(dir, GRAPH_DIR, "graph.json")))
|
|
19
|
+
return undefined;
|
|
20
|
+
const ignore = join(dir, GRAPH_DIR, ".gitignore");
|
|
21
|
+
if (!existsSync(ignore)) {
|
|
22
|
+
mkdirSync(join(dir, GRAPH_DIR), { recursive: true });
|
|
23
|
+
writeFileSync(ignore, "*\n");
|
|
24
|
+
}
|
|
25
|
+
return { commit: head.stdout.trim() };
|
|
26
|
+
}
|
|
27
|
+
// One build per worktree and commit: explorers started in one message share it.
|
|
28
|
+
export class Graphs {
|
|
29
|
+
#run;
|
|
30
|
+
#builds = new Map();
|
|
31
|
+
constructor(run = defaultExec) {
|
|
32
|
+
this.#run = run;
|
|
33
|
+
}
|
|
34
|
+
async get(dir) {
|
|
35
|
+
const head = (await this.#run("git", ["rev-parse", "--short", "HEAD"], { cwd: dir })).stdout.trim();
|
|
36
|
+
const known = this.#builds.get(dir);
|
|
37
|
+
if (known && known.commit === head)
|
|
38
|
+
return known.graph;
|
|
39
|
+
const graph = updateGraph(dir, this.#run);
|
|
40
|
+
this.#builds.set(dir, { commit: head, graph });
|
|
41
|
+
return graph;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export const GRAPH_ACTIONS = ["explain", "path", "affected", "query"];
|
|
45
|
+
const MAX_OUTPUT = 12_000;
|
|
46
|
+
// One question to the graph of dir, as the code_graph tool answers it.
|
|
47
|
+
export async function queryGraph(dir, action, symbol, to = "", run = defaultExec) {
|
|
48
|
+
if (!GRAPH_ACTIONS.includes(action))
|
|
49
|
+
return `Unknown action "${action}"; use one of ${GRAPH_ACTIONS.join(", ")}.`;
|
|
50
|
+
if (!symbol.trim())
|
|
51
|
+
return "Name a symbol (or words, for query).";
|
|
52
|
+
if (action === "path" && !to.trim())
|
|
53
|
+
return "path needs both symbol and to.";
|
|
54
|
+
const args = action === "path" ? ["path", symbol, to] : action === "query" ? ["query", symbol, "--budget", "3000"] : [action, symbol];
|
|
55
|
+
const r = await run("graphify", args, { cwd: dir, timeoutMs: 60_000 });
|
|
56
|
+
const out = `${r.stdout}${r.code !== 0 ? `\n${r.stderr}` : ""}`.trim() || "(no answer)";
|
|
57
|
+
return out.length > MAX_OUTPUT ? `${out.slice(0, MAX_OUTPUT)}\n[cut at ${MAX_OUTPUT} characters; ask about a narrower symbol]` : out;
|
|
58
|
+
}
|
|
59
|
+
// The lines of an explorer's brief that point it at the graph.
|
|
60
|
+
export function graphBrief(graph) {
|
|
61
|
+
return `A code graph of this directory (commit ${graph.commit}) is ready. ${GRAPH_DIR}/GRAPH_REPORT.md maps the modules and
|
|
62
|
+
their central symbols. For any question about who calls, uses or imports what, ask the code_graph tool first, before
|
|
63
|
+
grep or reading files: its call and import edges come from the syntax tree of every file, so one call replaces a
|
|
64
|
+
search through the code. Read code only to explain what you found; edges marked INFERRED are guesses to check.`;
|
|
65
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "@omacode/guard";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@omacode/guard/wire";
|
package/dist/helpers.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
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
|
+
import { lastAssistantText } from "./brief.js";
|
|
13
|
+
import { redact } from "./decider.js";
|
|
14
|
+
import { roleById } from "./roles.js";
|
|
15
|
+
import { toModel } from "./runner.js";
|
|
16
|
+
export const HELPER_ROLES = ["explore", "research"];
|
|
17
|
+
export const MAX_HELPERS = 3; // per writer session: enough for the questions a task raises, not a second job
|
|
18
|
+
export const HELPER_STEPS = 60;
|
|
19
|
+
const MAX_ANSWER = 12_000;
|
|
20
|
+
const TIMEOUT_MS = 10 * 60_000;
|
|
21
|
+
export function helperBrief(role, caller, question) {
|
|
22
|
+
const how = role === "explore"
|
|
23
|
+
? "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."
|
|
24
|
+
: "Look it up in the documentation and sources on the web, and in your directory where it helps; cite the URLs you used.";
|
|
25
|
+
return [
|
|
26
|
+
`A worker on ${caller.label} asks you one question and waits for your answer. Its task: ${caller.task}`,
|
|
27
|
+
"",
|
|
28
|
+
`Question: ${question}`,
|
|
29
|
+
"",
|
|
30
|
+
how,
|
|
31
|
+
"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.",
|
|
32
|
+
].join("\n");
|
|
33
|
+
}
|
|
34
|
+
export class Helpers {
|
|
35
|
+
#d;
|
|
36
|
+
#used = new Map();
|
|
37
|
+
constructor(deps) {
|
|
38
|
+
this.#d = deps;
|
|
39
|
+
}
|
|
40
|
+
async ask(callerSession, role, question) {
|
|
41
|
+
const caller = this.#d.caller(callerSession);
|
|
42
|
+
if (!caller)
|
|
43
|
+
return "Only a worker that changes files for the micromanager may ask a helper.";
|
|
44
|
+
if (!question.trim())
|
|
45
|
+
return "Ask one question, in a sentence.";
|
|
46
|
+
const used = this.#used.get(callerSession) ?? 0;
|
|
47
|
+
if (used >= MAX_HELPERS) {
|
|
48
|
+
return `You asked ${MAX_HELPERS} helpers in this task already. Work with what you have, and name what you still need in your report.`;
|
|
49
|
+
}
|
|
50
|
+
this.#used.set(callerSession, used + 1);
|
|
51
|
+
const r = roleById(role);
|
|
52
|
+
const model = await this.#d.model(r);
|
|
53
|
+
const brief = helperBrief(role, caller, question.trim());
|
|
54
|
+
const s = await this.#d.session.create({
|
|
55
|
+
title: `${caller.label} · ${r.agent} · ${question.trim().slice(0, 60)}`,
|
|
56
|
+
agent: r.agent,
|
|
57
|
+
model: toModel(model),
|
|
58
|
+
location: { directory: caller.dir },
|
|
59
|
+
metadata: { micromanager: { helperOf: callerSession, task: caller.label } },
|
|
60
|
+
});
|
|
61
|
+
this.#d.started(s.id, callerSession, caller.dir, brief);
|
|
62
|
+
const start = Date.now();
|
|
63
|
+
let timer;
|
|
64
|
+
let timedOut = false;
|
|
65
|
+
try {
|
|
66
|
+
await this.#d.session.prompt({ sessionID: s.id, text: redact(brief) });
|
|
67
|
+
timedOut = await Promise.race([
|
|
68
|
+
this.#d.session.wait({ sessionID: s.id }).then(() => false),
|
|
69
|
+
new Promise((resolve) => { timer = setTimeout(() => resolve(true), this.#d.timeoutMs ?? TIMEOUT_MS); }),
|
|
70
|
+
]);
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
clearTimeout(timer);
|
|
74
|
+
}
|
|
75
|
+
if (timedOut)
|
|
76
|
+
await this.#d.session.interrupt({ sessionID: s.id }).catch(() => undefined);
|
|
77
|
+
const answer = lastAssistantText(await this.#d.session.context({ sessionID: s.id }).catch(() => undefined));
|
|
78
|
+
this.#d.ended?.(s.id);
|
|
79
|
+
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 });
|
|
80
|
+
if (!answer)
|
|
81
|
+
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.`;
|
|
82
|
+
const cut = answer.length > MAX_ANSWER ? `${answer.slice(0, MAX_ANSWER)}\n[cut at ${MAX_ANSWER} characters]` : answer;
|
|
83
|
+
return `${timedOut ? "[the helper ran out of time; this is what it had]\n" : ""}${cut}`;
|
|
84
|
+
}
|
|
85
|
+
}
|
package/dist/hooks.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
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
|
+
import { chooseModel, guessNeeds } from "./routing.js";
|
|
4
|
+
import { MICROMANAGER, ROLES, roleByAgent } from "./roles.js";
|
|
5
|
+
// The tool a delegation arrives under.
|
|
6
|
+
//
|
|
7
|
+
// Read in the bundled server (~/.opencode/bin/opencode, the @opencode/Tool service): opencode registers exactly one
|
|
8
|
+
// delegation tool, `subagent`, and it carries `options.codemode:false`. That one flag settles the question of a
|
|
9
|
+
// nested shape — Tool.snapshot builds the code-mode catalog from the tools whose codemode option is not false, so a
|
|
10
|
+
// delegation cannot be issued from inside the code-mode `execute` tool at all. There is no second shape to
|
|
11
|
+
// recognise, and parsing the code string would be a parser for an event that cannot happen.
|
|
12
|
+
//
|
|
13
|
+
// (Nested calls that do exist are surfaced individually with their real tool name, so the hook sees them normally:
|
|
14
|
+
// Tool.execute triggers tool.execute.before per nested call, with that call's own name, input and the outer
|
|
15
|
+
// session's id and agent. The same code path also confirms the fields a call carries: tool, sessionID, agent,
|
|
16
|
+
// messageID, id and input.)
|
|
17
|
+
export const DELEGATION_TOOL = "subagent";
|
|
18
|
+
// Every tool name the delegation hook recognises, so the setup record can name them (a field operator has to be able
|
|
19
|
+
// to read off the journal what the hook thinks it is looking at).
|
|
20
|
+
export const DELEGATION_TOOLS = [DELEGATION_TOOL];
|
|
21
|
+
export const HEADER = "[micromanager]";
|
|
22
|
+
// The role a call delegates to, when it delegates to one of this plugin's own role agents; undefined otherwise.
|
|
23
|
+
//
|
|
24
|
+
// The caller is deliberately not part of the question, and that is the whole fix for a live run of 2026-10-06: the
|
|
25
|
+
// caller used to have to be the micromanager agent, the user switched the parent session's agent from `micromanager`
|
|
26
|
+
// to `build`, and the ten delegations that followed (coder, tester, explorer, oracle) all returned early from that
|
|
27
|
+
// one check. No task, no worktree, no claim, no move, no gate, no merge and not one word of it — the workers ran in
|
|
28
|
+
// the user's checkout. The agent is a property of the session the user happens to be sitting in, not a property of
|
|
29
|
+
// the delegation.
|
|
30
|
+
//
|
|
31
|
+
// What makes widening this safe is that the discriminator is the *target*: a target that is one of this plugin's own
|
|
32
|
+
// agents cannot be anything but a delegation, and routing one can only ever narrow what the worker may do. A built-in
|
|
33
|
+
// agent (`explore`, `general`) is not one of ours, so nothing is routed on its behalf and the call is left exactly as
|
|
34
|
+
// its caller wrote it.
|
|
35
|
+
export function delegationRole(call) {
|
|
36
|
+
if (!DELEGATION_TOOLS.includes(call.tool))
|
|
37
|
+
return undefined;
|
|
38
|
+
return roleByAgent(String(call.input.agent ?? ""));
|
|
39
|
+
}
|
|
40
|
+
// Whether this call is a delegation attempt the plugin cannot isolate: the subagent tool, aimed at an agent it does
|
|
41
|
+
// not own or at none. It is worth a journal record of its own, because a delegation that reached neither the routing
|
|
42
|
+
// nor a refusal is precisely how a live run wrote into the user's checkout in silence.
|
|
43
|
+
export function unownedDelegation(call) {
|
|
44
|
+
return call.tool === DELEGATION_TOOL && !roleByAgent(String(call.input.agent ?? ""));
|
|
45
|
+
}
|
|
46
|
+
// Rewrites `call.input` in place; returns what was decided, or undefined when the call is not the micromanager's.
|
|
47
|
+
export async function routeSubagentCall(call, routing, workerModel, available, spread) {
|
|
48
|
+
if (call.tool !== DELEGATION_TOOL || call.agent !== MICROMANAGER.agent)
|
|
49
|
+
return undefined;
|
|
50
|
+
return await route(call, routing, workerModel, available, spread);
|
|
51
|
+
}
|
|
52
|
+
// The same routing for a delegation that the micromanager agent did not make. Narrower than the caller alone would
|
|
53
|
+
// suggest and never wider: it returns nothing at all for a tool that is not the delegation tool.
|
|
54
|
+
export async function routeDelegation(call, routing, workerModel, available, spread) {
|
|
55
|
+
if (call.tool !== DELEGATION_TOOL)
|
|
56
|
+
return undefined;
|
|
57
|
+
return await route(call, routing, workerModel, available, spread);
|
|
58
|
+
}
|
|
59
|
+
async function route(call, routing, workerModel, available, spread) {
|
|
60
|
+
const role = roleByAgent(String(call.input.agent ?? ""));
|
|
61
|
+
if (!role)
|
|
62
|
+
return undefined; // a built-in agent: left as the caller asked
|
|
63
|
+
const prompt = String(call.input.prompt ?? "");
|
|
64
|
+
const choice = await chooseModel(role, guessNeeds(prompt), routing, workerModel, available, spread);
|
|
65
|
+
call.input.model = choice.model;
|
|
66
|
+
call.input.background = true;
|
|
67
|
+
if (!prompt.startsWith(HEADER)) {
|
|
68
|
+
call.input.prompt = `${HEADER} role: ${role.id} · model: ${choice.model}\n\n${prompt}`;
|
|
69
|
+
}
|
|
70
|
+
// skipped travels with the decision so the plugin can journal why this model and not the first of the chain.
|
|
71
|
+
return { role: role.id, model: choice.model, reason: choice.reason, ...(choice.skipped ? { skipped: choice.skipped } : {}) };
|
|
72
|
+
}
|
|
73
|
+
// --- the backstop: a child of a micromanager session that no worktree ever got must not be able to write ---
|
|
74
|
+
// The write-capable actions a permission evaluation can carry. The names are the ones opencode asserts for its own
|
|
75
|
+
// tools (opencode.tool.edit and opencode.tool.patch both assert action "edit", the shell tool asserts "shell"), plus
|
|
76
|
+
// the code-mode `execute` tool, which reaches everything else: a worker that may run `execute` may run a shell inside
|
|
77
|
+
// it. Anything not listed here is a read, and a read cannot damage the checkout.
|
|
78
|
+
const WRITES = new Set(["shell", "edit", "write", "patch", "execute"]);
|
|
79
|
+
// The refusal an unclaimed child of a micromanager session gets instead of its write, and undefined for every other
|
|
80
|
+
// session and every other action.
|
|
81
|
+
//
|
|
82
|
+
// This is the part that holds when the delegation hook did not engage, which is why it does not depend on knowing how
|
|
83
|
+
// the delegation was made: it asks only whether this session is a child of a session the plugin owns and whether the
|
|
84
|
+
// plugin claimed it as a worker. The live run this exists for had no task, no worktree, no claim and no session move,
|
|
85
|
+
// and nothing complained — the children were ordinary sessions of a micromanager session, and every one of their
|
|
86
|
+
// writes went straight into the user's checkout.
|
|
87
|
+
//
|
|
88
|
+
// 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
|
|
89
|
+
// delegated child — it is a root session, or a child of a session that is not the plugin's — and keeps working
|
|
90
|
+
// exactly as before. Only a session that was *started by* a micromanager session is ever caught.
|
|
91
|
+
export function backstop(facts, action, claimed) {
|
|
92
|
+
if (claimed || !facts.parent || !facts.parentIsMicromanager)
|
|
93
|
+
return undefined;
|
|
94
|
+
if (!WRITES.has(action))
|
|
95
|
+
return undefined;
|
|
96
|
+
return {
|
|
97
|
+
reason: "isolation did not engage: a child of a micromanager session that is not a claimed worker",
|
|
98
|
+
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.`,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
// Why a call of the micromanager was not routed, when it is about the agent name at all; undefined for every other
|
|
102
|
+
// reason routeSubagentCall returns nothing (a call that is not the micromanager's, or one it already routed).
|
|
103
|
+
//
|
|
104
|
+
// routeSubagentCall leaves a built-in agent alone on purpose (it routes nothing it does not own), so the caller got
|
|
105
|
+
// "Not started: use one of the role agents (coder, …)" for `explore` and looked for a `explore` role agent that does
|
|
106
|
+
// not exist. The two cases need different fixes: rename the agent, or install the plugin's agents.
|
|
107
|
+
export function notRouted(call) {
|
|
108
|
+
if (call.tool !== "subagent" || call.agent !== MICROMANAGER.agent)
|
|
109
|
+
return undefined;
|
|
110
|
+
const asked = String(call.input.agent ?? "").trim();
|
|
111
|
+
if (roleByAgent(asked))
|
|
112
|
+
return undefined; // a role agent: routed, or nothing to say about the name
|
|
113
|
+
if (asked) {
|
|
114
|
+
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.`;
|
|
115
|
+
}
|
|
116
|
+
return `No agent was named, so nothing was started. Name one of this plugin's role agents (${ROLES.map((r) => r.agent).join(", ")}).`;
|
|
117
|
+
}
|
|
118
|
+
// The text of a tool result as the model sees it: result.content[] text parts, plus string fields of result.output
|
|
119
|
+
// (shapes seen in the Phase E spike for read, shell, execute and custom tools).
|
|
120
|
+
function textSlots(result) {
|
|
121
|
+
const slots = [];
|
|
122
|
+
const r = result;
|
|
123
|
+
if (!r || typeof r !== "object")
|
|
124
|
+
return slots;
|
|
125
|
+
if (Array.isArray(r.content)) {
|
|
126
|
+
for (const part of r.content) {
|
|
127
|
+
if (part && typeof part.text === "string")
|
|
128
|
+
slots.push({ get: () => part.text, set: (v) => { part.text = v; } });
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (typeof r.output === "string")
|
|
132
|
+
slots.push({ get: () => r.output, set: (v) => { r.output = v; } });
|
|
133
|
+
else if (r.output && typeof r.output === "object") {
|
|
134
|
+
const o = r.output;
|
|
135
|
+
for (const key of ["output", "content", "text"]) {
|
|
136
|
+
if (typeof o[key] === "string")
|
|
137
|
+
slots.push({ get: () => o[key], set: (v) => { o[key] = v; } });
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return slots;
|
|
141
|
+
}
|
|
142
|
+
export function resultText(result) {
|
|
143
|
+
return textSlots(result).map((s) => s.get()).join("\n");
|
|
144
|
+
}
|
|
145
|
+
// Rewrites a tool result in place before the model sees it (tool.execute.after is awaited, Phase E spike); returns
|
|
146
|
+
// whether anything changed.
|
|
147
|
+
export function rewriteResult(result, fn) {
|
|
148
|
+
let changed = false;
|
|
149
|
+
for (const slot of textSlots(result)) {
|
|
150
|
+
const before = slot.get();
|
|
151
|
+
const after = fn(before);
|
|
152
|
+
if (after !== before) {
|
|
153
|
+
slot.set(after);
|
|
154
|
+
changed = true;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return changed;
|
|
158
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// The image_models tool: which image models are ready on this machine's ComfyUI, in the user's order, with the files
|
|
2
|
+
// each needs, how to load them, and what their licence allows. An agent calls it before its first picture, so it picks a
|
|
3
|
+
// model by what it is good at and what it may be used for, instead of by what happens to be on disk.
|
|
4
|
+
//
|
|
5
|
+
// The order is the user's: `images.prefer` in omacode.jsonc (family ids), default z-image, flux2-klein, qwen-image
|
|
6
|
+
// (2026-11-23). A family is ready when its diffusion model, text encoder and VAE are all on disk and all permitted by
|
|
7
|
+
// the model registry (the same check the ComfyUI gate makes before a run, src/comfy-gate.ts).
|
|
8
|
+
import { readFileSync } from "node:fs";
|
|
9
|
+
import { MODEL_REGISTRY, readModelRegistry, registryWith, resolveModel } from "@omacode/core/models";
|
|
10
|
+
import { stripComments } from "@omacode/core/jsonc";
|
|
11
|
+
import { configFilePath } from "@omacode/core/paths";
|
|
12
|
+
import { hashFile, modelFiles } from "./comfy-gate.js";
|
|
13
|
+
export const IMAGE_MODELS_ID = "image_models";
|
|
14
|
+
export const FAMILIES = [
|
|
15
|
+
{
|
|
16
|
+
id: "z-image",
|
|
17
|
+
title: "Z-Image-Turbo",
|
|
18
|
+
good: "fast (8 steps), photorealistic scenes, people, products",
|
|
19
|
+
load: "UNETLoader (diffusion_models) + CLIPLoader type lumina2 (text_encoders) + VAELoader; latent EmptySD3LatentImage",
|
|
20
|
+
settings: "8 steps, cfg 1.0, sampler res_multistep, scheduler simple, 1024×1024 or similar",
|
|
21
|
+
unet: /z[-_ ]?image/i,
|
|
22
|
+
encoder: /qwen_?3_4b/i,
|
|
23
|
+
vae: /^(z_image_)?ae\.safetensors$/i,
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
id: "flux2-klein",
|
|
27
|
+
title: "FLUX.2-klein 4B",
|
|
28
|
+
good: "general purpose, illustration and design, editing an existing image",
|
|
29
|
+
load: "UnetLoaderGGUF for .gguf or UNETLoader for .safetensors (diffusion_models) + CLIPLoader type flux2 (text_encoders) + VAELoader; latent EmptyFlux2LatentImage",
|
|
30
|
+
settings: "4 steps for the distilled 4B, cfg 1.0; the base model needs about 20 steps",
|
|
31
|
+
unet: /flux[-_. ]?2/i,
|
|
32
|
+
encoder: /qwen_?3_4b/i,
|
|
33
|
+
vae: /flux2[-_]?vae/i,
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
id: "qwen-image",
|
|
37
|
+
title: "Qwen-Image-2.1",
|
|
38
|
+
good: "text and lettering inside the image (posters, signs, slides), complex compositions",
|
|
39
|
+
load: "UnetLoaderGGUF (diffusion_models) + CLIPLoader type qwen_image (text_encoders) + VAELoader; latent EmptySD3LatentImage",
|
|
40
|
+
settings: "about 20 steps, cfg 2.5–4",
|
|
41
|
+
unet: /qwen[-_. ]?image/i,
|
|
42
|
+
encoder: /qwen_?(2\.?5|3)[-_]?vl/i,
|
|
43
|
+
vae: /qwen[-_]?image.*vae/i,
|
|
44
|
+
caution: "non-commercial only (research or evaluation): use it only when the user has said the picture is not for commercial use; otherwise ask the user first, or pick another model",
|
|
45
|
+
},
|
|
46
|
+
];
|
|
47
|
+
export const DEFAULT_ORDER = ["z-image", "flux2-klein", "qwen-image"];
|
|
48
|
+
// The user's order: images.prefer in omacode.jsonc; unknown ids are dropped, missing families keep their default place.
|
|
49
|
+
export function preferredOrder(config) {
|
|
50
|
+
const asked = config?.images?.prefer;
|
|
51
|
+
const ids = new Set(FAMILIES.map((f) => f.id));
|
|
52
|
+
const first = Array.isArray(asked) ? asked.filter((x) => typeof x === "string" && ids.has(x)) : [];
|
|
53
|
+
return [...new Set([...first, ...DEFAULT_ORDER])];
|
|
54
|
+
}
|
|
55
|
+
export function readImageConfig(path = configFilePath()) {
|
|
56
|
+
try {
|
|
57
|
+
return JSON.parse(stripComments(readFileSync(path, "utf8")));
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const permitted = (c) => c?.verdict.status === "allowed";
|
|
64
|
+
const licenceOf = (c) => (c.verdict.status === "allowed" ? c.verdict.spdx : c.verdict.status);
|
|
65
|
+
export function familyViews(files, order) {
|
|
66
|
+
const byKind = (kinds, re) => files.filter((f) => kinds.includes(f.kind) && re.test(f.name));
|
|
67
|
+
return order.map((id) => FAMILIES.find((f) => f.id === id)).map((family) => {
|
|
68
|
+
const unets = byKind(["diffusion_models", "unet", "checkpoints"], family.unet);
|
|
69
|
+
const encoder = byKind(["text_encoders", "clip"], family.encoder).sort((a, b) => Number(permitted(b)) - Number(permitted(a)))[0];
|
|
70
|
+
const vae = byKind(["vae"], family.vae).sort((a, b) => Number(permitted(b)) - Number(permitted(a)))[0];
|
|
71
|
+
const missing = [];
|
|
72
|
+
if (!unets.some(permitted))
|
|
73
|
+
missing.push(unets.length ? "a diffusion model the registry permits" : "a diffusion model");
|
|
74
|
+
if (!permitted(encoder))
|
|
75
|
+
missing.push(encoder ? `a permitted text encoder (${encoder.name} is ${encoder.verdict.status})` : "its text encoder");
|
|
76
|
+
if (!permitted(vae))
|
|
77
|
+
missing.push(vae ? `a permitted VAE (${vae.name} is ${vae.verdict.status})` : "its VAE");
|
|
78
|
+
const used = [...unets.filter(permitted), ...(encoder ? [encoder] : []), ...(vae ? [vae] : [])];
|
|
79
|
+
return { family, unets, ...(encoder ? { encoder } : {}), ...(vae ? { vae } : {}), ready: missing.length === 0, missing, licences: [...new Set(used.map(licenceOf))] };
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
// What the tool answers: the ready families first in the user's order, then the rest with what they lack.
|
|
83
|
+
export function describe(views) {
|
|
84
|
+
const lines = [
|
|
85
|
+
"Image models on this machine's ComfyUI, in the user's order. Pick the first ready one that fits the request; follow its caution if it has one. Use only the files listed here (the ComfyUI gate checks every model a workflow loads).",
|
|
86
|
+
"",
|
|
87
|
+
];
|
|
88
|
+
let n = 0;
|
|
89
|
+
for (const v of views.filter((x) => x.ready)) {
|
|
90
|
+
n++;
|
|
91
|
+
lines.push(`${n}. ${v.family.title} [${v.family.id}] — ${v.family.good}`);
|
|
92
|
+
lines.push(` licence: ${v.licences.join(" + ")}${v.family.caution ? ` — ${v.family.caution}` : ""}`);
|
|
93
|
+
lines.push(` diffusion model: ${v.unets.filter((u) => u.verdict.status === "allowed").map((u) => u.name).join(" or ")}`);
|
|
94
|
+
lines.push(` text encoder: ${v.encoder.name} · VAE: ${v.vae.name}`);
|
|
95
|
+
lines.push(` load: ${v.family.load}`);
|
|
96
|
+
lines.push(` settings: ${v.family.settings}`);
|
|
97
|
+
}
|
|
98
|
+
if (n === 0)
|
|
99
|
+
lines.push("No image model is ready: say so, and name what is missing below.");
|
|
100
|
+
const rest = views.filter((x) => !x.ready);
|
|
101
|
+
if (rest.length) {
|
|
102
|
+
lines.push("", "Not ready:");
|
|
103
|
+
for (const v of rest)
|
|
104
|
+
lines.push(`- ${v.family.title} [${v.family.id}]: missing ${v.missing.join(", ")}`);
|
|
105
|
+
}
|
|
106
|
+
lines.push("", "Run it with the comfy MCP tools (tools.comfy.run_workflow with a workflow you wrote, or a template via tools.comfy.run_template), then tools.comfy.fetch_outputs into the project.");
|
|
107
|
+
return lines.join("\n");
|
|
108
|
+
}
|
|
109
|
+
export async function imageModels(workspace, o = {}) {
|
|
110
|
+
const registry = registryWith(readModelRegistry(o.registryDir !== undefined ? { dir: o.registryDir } : {}).entries, MODEL_REGISTRY);
|
|
111
|
+
const files = [];
|
|
112
|
+
for (const f of modelFiles(workspace)) {
|
|
113
|
+
const hash = await hashFile(f.file, o.cacheFile).catch(() => undefined);
|
|
114
|
+
files.push({ ...f, verdict: resolveModel({ declared: f.name, ...(hash ? { hash } : {}) }, registry) });
|
|
115
|
+
}
|
|
116
|
+
return describe(familyViews(files, preferredOrder(o.config === undefined ? readImageConfig() : o.config)));
|
|
117
|
+
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
1
|
// The worktree handling lives in src/worktree/ now (the worktree plugin, enable-able on its own). This file keeps the
|
|
2
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"
|
|
3
|
+
export * from "./worktree/git.js";
|
package/dist/journal.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@omacode/core/journal";
|