@0perator/operator 0.1.283 → 0.1.284
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/dist/autonomy/headless.js +2 -0
- package/dist/moa/shadow.js +61 -0
- package/dist/session.js +38 -0
- package/package.json +1 -1
|
@@ -49,12 +49,14 @@ export async function runHeadless(settings, prompt, opts = {}) {
|
|
|
49
49
|
reporter.event("done", { reason: res.reason, turns: res.turns, toolCalls: res.toolCalls });
|
|
50
50
|
if (res.lastContent)
|
|
51
51
|
reporter.line(res.lastContent);
|
|
52
|
+
await session.flushRouteShadow(); // MOA phase-0: the last turn's shadow record must not die with the process
|
|
52
53
|
return res.reason === "error" ? 1 : res.reason === "interrupted" ? 130 : 0;
|
|
53
54
|
}
|
|
54
55
|
const outcome = await session.send(prompt, handlers);
|
|
55
56
|
reporter.event("done", { rounds: outcome.rounds, toolCalls: outcome.toolCalls, interrupted: outcome.interrupted, durationMs: Math.round(outcome.durationMs ?? 0) });
|
|
56
57
|
if (outcome.content)
|
|
57
58
|
reporter.line(outcome.content);
|
|
59
|
+
await session.flushRouteShadow(); // MOA phase-0: headless one-shot exits before fire-and-forget would land
|
|
58
60
|
return outcome.interrupted ? 130 : 0;
|
|
59
61
|
}
|
|
60
62
|
catch (e) {
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// MOA phase-0 route shadow — pure observability for the mixture-of-agents dispatch plan.
|
|
2
|
+
// A gate-not-voice classifier labels the turn's opening request into one of 9 route labels;
|
|
3
|
+
// we log (label, model-that-actually-ran, cost) so a week of real traffic answers the ONLY
|
|
4
|
+
// question that decides lane placement: what share of turns is actually CHAT/SUMMARIZE/WRITE
|
|
5
|
+
// (fast lane) vs CODE/SECURITY (heavy/top). NOTHING here changes routing, blocks a turn, or
|
|
6
|
+
// leaves the machine: env-gated (KODE_MOA_SHADOW), fire-and-forget, every failure swallowed.
|
|
7
|
+
//
|
|
8
|
+
// The classifier is deliberately strict — parseRouteLabel accepts ONE word and nothing else;
|
|
9
|
+
// prose answers ("This is a coding request") are null, not a guess. That is the same contract
|
|
10
|
+
// the Alfred-class 186M gate will ship under: a gate that rambles is a gate that lies.
|
|
11
|
+
// Node-free by construction (plain fetch + caller-supplied file path) — safe for any graph.
|
|
12
|
+
export const ROUTE_LABELS = ["CHAT", "OPERATE", "VISION", "CODE", "DATA", "SECURITY", "WRITE", "SUMMARIZE", "MATH"];
|
|
13
|
+
/** Planned lane per label (MOA design, 2026-10-12): fast=small resident model, operate=tool-format
|
|
14
|
+
* model, heavy=aurora-class, top=true-MoA fanout (SECURITY only for now), vision=media surface. */
|
|
15
|
+
export const TIER_BY_LABEL = {
|
|
16
|
+
CHAT: "fast", SUMMARIZE: "fast", WRITE: "fast",
|
|
17
|
+
OPERATE: "operate",
|
|
18
|
+
CODE: "heavy", DATA: "heavy", MATH: "heavy",
|
|
19
|
+
SECURITY: "top",
|
|
20
|
+
VISION: "vision",
|
|
21
|
+
};
|
|
22
|
+
/** Teacher prompt: 9 labels, one word, nothing else. Kept tiny on purpose — the shadow call is
|
|
23
|
+
* billed to the session's own model and must stay a rounding error (~1 completion token). */
|
|
24
|
+
export function shadowPrompt(text) {
|
|
25
|
+
const head = text.slice(0, 400);
|
|
26
|
+
return `Classify the REQUEST below into exactly one word: CHAT, OPERATE, VISION, CODE, DATA, SECURITY, WRITE, SUMMARIZE, or MATH.
|
|
27
|
+
Rules: CODE=writing/fixing code. OPERATE=running tools/system actions. WRITE=prose drafting. SUMMARIZE=condensing. DATA=tables/analytics. SECURITY=threats/vulns/auth. MATH=numeric/formal. VISION=images. CHAT=everything else.
|
|
28
|
+
Output ONE word, nothing else.
|
|
29
|
+
|
|
30
|
+
REQUEST: """${head}"""`;
|
|
31
|
+
}
|
|
32
|
+
/** STRICT parse: one word only (punctuation/spacing tolerated), case-insensitive. Multi-word or
|
|
33
|
+
* unknown answers are refusals to gate — return null, never guess. */
|
|
34
|
+
export function parseRouteLabel(out) {
|
|
35
|
+
const w = out.trim().replace(/[.!?,;:"'`]/g, "").toUpperCase();
|
|
36
|
+
if (!w || /\s/.test(w))
|
|
37
|
+
return null;
|
|
38
|
+
return ROUTE_LABELS.includes(w) ? w : null;
|
|
39
|
+
}
|
|
40
|
+
/** Fire-and-forget one shadow record for a completed turn. `classify` is injected so tests run
|
|
41
|
+
* offline and production can use any teacher endpoint (session wires aurora chat). Never throws;
|
|
42
|
+
* never awaits longer than the caller's own abort policy — the whole point is that observability
|
|
43
|
+
* cannot degrade the thing it observes. */
|
|
44
|
+
export async function routeShadowTurn(opts) {
|
|
45
|
+
if (!opts.enabled)
|
|
46
|
+
return;
|
|
47
|
+
const t0 = Date.now();
|
|
48
|
+
try {
|
|
49
|
+
const raw = await opts.classify(opts.text);
|
|
50
|
+
const label = parseRouteLabel(raw);
|
|
51
|
+
const ev = {
|
|
52
|
+
ts: new Date().toISOString(), label, tier: label ? TIER_BY_LABEL[label] : null,
|
|
53
|
+
model: opts.model, promptTokens: opts.promptTokens, genTokens: opts.genTokens,
|
|
54
|
+
turnMs: opts.turnMs, classifyMs: Date.now() - t0,
|
|
55
|
+
};
|
|
56
|
+
await opts.append(JSON.stringify(ev));
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
/* shadow telemetry is allowed to die in silence — never into a turn */
|
|
60
|
+
}
|
|
61
|
+
}
|
package/dist/session.js
CHANGED
|
@@ -12,6 +12,7 @@ import { appendFileSync, mkdirSync } from "node:fs";
|
|
|
12
12
|
import { dirname } from "node:path";
|
|
13
13
|
import { operatorPath } from "./config/home.js";
|
|
14
14
|
import { fanOutProposers, labelDrafts, DEFAULT_AGGREGATOR_PROMPT } from "./engine/moa.js";
|
|
15
|
+
import { routeShadowTurn, shadowPrompt } from "./moa/shadow.js"; // MOA phase-0 observability (KODE_MOA_SHADOW)
|
|
15
16
|
import { AuroraRouter } from "./router/router.js";
|
|
16
17
|
import { operatorAgentPersona } from "./config/operator-harness.js";
|
|
17
18
|
import { modelRegistry, findModel, selectModel, visibleModels, endpointBase, DEFAULT_MODEL_ID, LEAN_HARNESS_CTX } from "./router/models.js";
|
|
@@ -271,6 +272,12 @@ export class Session {
|
|
|
271
272
|
wantRecall;
|
|
272
273
|
recallBlock = ""; // memory recalled on the first turn, folded into the system prompt thereafter
|
|
273
274
|
sessionTokensTotal = 0; // cumulative prefill+output tokens across all turns (Claude-Code-style session total)
|
|
275
|
+
_shadowPending = Promise.resolve(); // MOA phase-0: in-flight route-shadow record (never rejects)
|
|
276
|
+
/** Await the in-flight route-shadow record before a short-lived process exits — fire-and-forget alone
|
|
277
|
+
* would drop the last turn's telemetry silently. Interactive sessions need not call this (the process
|
|
278
|
+
* outlives the 15s-abort classification); `operator run` headless MUST. Safe to call anytime, twice,
|
|
279
|
+
* from anywhere: the pending promise is swapped and failures were already swallowed by the module. */
|
|
280
|
+
async flushRouteShadow() { const p = this._shadowPending; this._shadowPending = Promise.resolve(); await p; }
|
|
274
281
|
planMode = false; // /plan: research read-only + propose a plan, no mutations until the owner approves
|
|
275
282
|
thinkMode = "off"; // /think, /ultrathink — extended reasoning
|
|
276
283
|
recalled = false;
|
|
@@ -917,6 +924,37 @@ export class Session {
|
|
|
917
924
|
turnContinuation: { enabled: process.env.KODE_TURN_CONTINUATION !== "0" && !this.planMode },
|
|
918
925
|
});
|
|
919
926
|
this.telemetry.record({ type: "turn", rounds: res.rounds, toolCalls: res.toolCalls, interrupted: false, durationMs: Math.round(performance.now() - _t0), ctxPct: this.ctxPct(), stopReason: res.stopReason });
|
|
927
|
+
// MOA phase-0 ROUTE SHADOW (observability ONLY — routing is untouched): ON by default, opt out with
|
|
928
|
+
// KODE_MOA_SHADOW=0. Labels
|
|
929
|
+
// the turn with the gate's own 9-label contract and appends {label, model-that-ran, cost} to
|
|
930
|
+
// ~/.axe/operator/route-shadow/<date>.jsonl. The classifier runs as a BACKGROUND-priority call on
|
|
931
|
+
// the endpoint gate (a main turn always outranks it) with a 15s abort, and routeShadowTurn swallows
|
|
932
|
+
// every failure — observability that can degrade the observed thing is not observability.
|
|
933
|
+
if (process.env.KODE_MOA_SHADOW !== "0") {
|
|
934
|
+
const prof = findModel(this.registry, this.modelId);
|
|
935
|
+
const shadowBase = prof?.endpoint ? endpointBase(prof.endpoint) : undefined;
|
|
936
|
+
if (shadowBase) {
|
|
937
|
+
const shadowTok = prof?.byo ? getByoToken(prof.byo) : undefined;
|
|
938
|
+
const shadowModel = prof?.model ?? this.modelId;
|
|
939
|
+
this._shadowPending = routeShadowTurn({
|
|
940
|
+
enabled: true,
|
|
941
|
+
text,
|
|
942
|
+
model: this.currentModel(),
|
|
943
|
+
promptTokens: res.promptTokens,
|
|
944
|
+
genTokens: res.tokens,
|
|
945
|
+
turnMs: Math.round(performance.now() - _t0),
|
|
946
|
+
classify: async (t) => (await chat([{ role: "user", content: shadowPrompt(t) }], {
|
|
947
|
+
base: shadowBase, model: shadowModel, token: shadowTok, transport: prof?.transport,
|
|
948
|
+
maxTokens: 8, temperature: 0, background: true, signal: AbortSignal.timeout(15000),
|
|
949
|
+
})).content,
|
|
950
|
+
append: async (line) => {
|
|
951
|
+
const p = operatorPath("route-shadow", `${new Date().toISOString().slice(0, 10)}.jsonl`);
|
|
952
|
+
mkdirSync(dirname(p), { recursive: true, mode: 0o700 });
|
|
953
|
+
appendFileSync(p, line + "\n", { encoding: "utf8", mode: 0o600 });
|
|
954
|
+
},
|
|
955
|
+
});
|
|
956
|
+
}
|
|
957
|
+
}
|
|
920
958
|
this.sessionTokensTotal += (res.tokens ?? 0) + (res.promptTokens ?? 0); // cumulative session spend
|
|
921
959
|
// Instant compaction: feed token counts to the rolling summarizer. When a round ends without
|
|
922
960
|
// tool calls and we've crossed the threshold, the summarizer triggers a background summary.
|