Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .modonome/snapshot/badge.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"color":"blue","label":"snapshot","message":"888 files, 117853 tok","schemaVersion":1}
{"color":"blue","label":"snapshot","message":"889 files, 118404 tok","schemaVersion":1}
2 changes: 1 addition & 1 deletion .modonome/snapshot/map.json

Large diffs are not rendered by default.

142 changes: 76 additions & 66 deletions .modonome/snapshot/map.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .modonome/snapshot/signature.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"commands":{"build":"","lint":"","test":"npm test"},"entrypoints":["bin/modonome.mjs"],"generated_at":"2026-07-06T09:01:11.324Z","generated_for":"modonome","governance":{"armed":false,"autonomy_enabled":false,"dry_run":true},"instructions":["AGENTS.md","CODEX.md","CONTRIBUTING.md","README.md"],"language_mix":{"(none)":11,"css":50,"diff":15,"html":9,"js":19,"json":127,"jsonl":7,"md":122,"mjs":270,"patch":37,"png":5,"py":4,"sh":1,"svg":1,"tf":4,"toml":2,"ts":60,"tsx":98,"txt":7,"webmanifest":1,"webp":1,"woff2":9,"xml":1,"yaml":7,"yml":20},"merkle_root":"sha256:7edf100f776a7d6c795c1c7133f029d6ef646f37a73774a9ef65bfcd32abe643","preamble":"Modonome snapshot. Read this before reading the repo. Tier 0 (signature.json) is the fingerprint: if merkle_root matches your last read, nothing changed. Tier 1 (map.json / map.md) lists modules, public API signatures, import edges, and attention ranking. Cite anchors (F: for files, S: for symbols); each resolves to a path and line so you can act without re-reading the whole repo.","protected_paths":[".github",".github/CODEOWNERS","package-lock.json"],"schema_version":1,"size":{"bytes":3349933,"files":888},"snapshot_version":2,"stack":{"name":"Node or TypeScript","pm":"npm"},"tier_hashes":{"map":"sha256:935fedc9536bcc8ab3b331ce5958fc75c7dabe3be01402192ed6ad558c47ba99","map_md":"sha256:f30e1e9accf7ff3c58f111c7ad749e94877dc463469c9390cf1bf1566bda83dc"}}
{"commands":{"build":"","lint":"","test":"npm test"},"entrypoints":["bin/modonome.mjs"],"generated_at":"2026-07-07T02:40:29.394Z","generated_for":"modonome","governance":{"armed":false,"autonomy_enabled":false,"dry_run":true},"instructions":["AGENTS.md","CODEX.md","CONTRIBUTING.md","README.md"],"language_mix":{"(none)":11,"css":50,"diff":15,"html":9,"js":19,"json":127,"jsonl":7,"md":122,"mjs":271,"patch":37,"png":5,"py":4,"sh":1,"svg":1,"tf":4,"toml":2,"ts":60,"tsx":98,"txt":7,"webmanifest":1,"webp":1,"woff2":9,"xml":1,"yaml":7,"yml":20},"merkle_root":"sha256:4cea131917d0dd424ed64ccaeb7d97411fcd964fcfdc66f436ef7e1304cf1f14","preamble":"Modonome snapshot. Read this before reading the repo. Tier 0 (signature.json) is the fingerprint: if merkle_root matches your last read, nothing changed. Tier 1 (map.json / map.md) lists modules, public API signatures, import edges, and attention ranking. Cite anchors (F: for files, S: for symbols); each resolves to a path and line so you can act without re-reading the whole repo.","protected_paths":[".github",".github/CODEOWNERS","package-lock.json"],"schema_version":1,"size":{"bytes":3367444,"files":889},"snapshot_version":5,"stack":{"name":"Node or TypeScript","pm":"npm"},"tier_hashes":{"map":"sha256:223eabbda69dbcaa25c3007597453f5d462a5c470afc1e6fec91f94b034b1075","map_md":"sha256:ec944f27b5c94082f150322541c201fd078bcefc7c1e6e1a27934b5fd8a0dd51"}}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"schema_version": 1,
"id": "WI-041-agent-model-fallback-runtime",
"state": "queued",
"state": "done",
"type": "develop-feature",
"assigned_role": "maker",
"attempts": 0,
Expand Down
9 changes: 8 additions & 1 deletion ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,14 @@ default until an owner arms it.
`apply-patch.mjs` (diff application), `parse-checker-telemetry.mjs` (checker feedback
parsing), and `tool-loop-adapter.mjs` (spawns an external agentic CLI for the tool-loop
execution mode, ADR-032). This is what `npm run demo:agent` and the maker/checker jobs in
`.github/workflows/modonome-auto.yml` actually run. `review-diff.mjs` (ADR-038 spike) is
`.github/workflows/modonome-auto.yml` actually run. A role with a prioritized `models` list
(ADR-039) also gets a runtime fallback chain (`buildFallbackChain` in `run-cycle.mjs`,
WI-041): if the resolved primary model turns out unreachable at invocation time (a
network-layer failure or a timeout), the openai-http transport retries the next model in
the role's chain rather than failing the whole cycle, skipping any candidate the current
budget cannot afford. A real answer from a reachable endpoint, an auth failure, a bad
status, a malformed response, is never retried; only unreachability is. `review-diff.mjs`
(ADR-038 spike) is
the same checker decoupled from the maker: an author-agnostic, review-only pass over any
diff (a human's, an agent session's, or the internal maker's), run on every pull request by
`.github/workflows/modonome-review.yml` so a change gets an independent review before merge
Expand Down
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ or CVE identifier where one exists.

## Unreleased

### Runtime model fallback for a role's prioritized model chain (WI-041)

- `scripts/agent/run-cycle.mjs` now walks a role's prioritized `models` chain (ADR-039) at
invocation time: when the resolved primary model turns out unreachable (a network-layer
failure, or a timeout), the openai-http transport (`invokeRoleOpenAI`) falls back to the
next model in the chain instead of failing the whole cycle. A candidate the current daily
budget cannot afford is skipped without an attempt, so a fallback can never silently spend
past the budget. A real answer from a reachable endpoint, an auth failure, a bad status, a
malformed response, is never retried, only genuine unreachability is, so a broken diff or a
rejected request never gets silently re-tried against a different model.
`resolveRoleModelChain`/`selectUsableModel` (ADR-039) already picked the best model a
budget affords, statically, before any call; this is what that explicitly left out.
The metric recorded for a fallback run reflects the model that actually answered, plus a
`model_fallback_from` field listing every unreachable candidate tried first, so the audit
trail never misattributes a diff to the originally planned model.
- Fully backward compatible: a plan built without a runtime chain (any hand-built plan, or a
role with a single configured model) takes the exact original one-shot code path, no new
checks, no behavior change.

### Full CRUD for maker/checker wiring, agent capability profiles, and two governed checkpoints

- Added full add/edit/remove UI in the control panel for `roles`, `models`, `providers`, and
Expand Down
6 changes: 3 additions & 3 deletions RELEASE-EVIDENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ be added here once the owner arms the loop on this repo.
|---|---|
| Drift guard | pass |
| Style check | pass |
| Repo hygiene | FAIL |
| Repo hygiene | pass |
| Self-application conformance | pass |
| Learning traceability | pass |
| Capability promotion readiness | pass |
Expand All @@ -29,8 +29,8 @@ be added here once the owner arms the loop on this repo.

## Work queue

- done: 37
- queued: 6
- done: 38
- queued: 5

## Sample-app captures

Expand Down
116 changes: 93 additions & 23 deletions scripts/agent/run-cycle.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { mkdirSync, writeFileSync, appendFileSync, readFileSync, existsSync } fr
import { fileURLToPath } from "node:url";
import { dirname, join, basename, resolve } from "node:path";
import { loadConfig } from "../validate-config.mjs";
import { resolveRole } from "./resolve-role.mjs";
import { resolveRole, resolveRoleModelChain } from "./resolve-role.mjs";
import { isBillable, resolveProvider } from "./providers.mjs";
import { renderPrompt, snapshotContext } from "./render-prompt.mjs";
import { readPromotedLearnings } from "../lib/learnings.mjs";
Expand Down Expand Up @@ -60,6 +60,37 @@ export function resolveExecMode(cfg, model) {
return mode === "tool-loop" ? "tool-loop" : "patch";
}

// Build a role's ordered runtime fallback chain: the resolved primary first (so a
// --maker-model/--checker-model CLI override always wins as chain[0]), then the rest of
// the role's configured `models` list with the primary's own entry (if repeated there)
// removed to avoid a duplicate attempt. Only registry-known models are usable fallback
// candidates; the primary already passed the pinning check the caller runs beforehand,
// so this only prunes additional entries, never throws. A role with no configured
// fallback list still gets a valid one-entry chain (the primary alone).
export function buildFallbackChain(cfg, role, resolved, known) {
const primary = {
model: resolved.model,
modelProvider: resolved.modelProvider,
modelBaseUrl: resolved.modelBaseUrl,
transport: resolved.transport,
costClass: resolved.costClass,
authEnv: resolved.authEnv,
};
const rest = resolveRoleModelChain(cfg, role).filter((c) => c.model !== primary.model);
const chain = [primary, ...rest];
return known.size > 0 ? chain.filter((c) => known.has(c.model)) : chain;
}

// Conservative classifier: only a network-level failure (connection refused/reset, DNS
// failure, a request timeout) counts as "unreachable" and is safe to retry against the
// next model in a role's fallback chain. Anything else, an auth failure, a malformed
// response, a real non-2xx answer, came from a reachable endpoint and must propagate,
// never trigger a silent fallback to a different model.
export function isUnreachableError(err) {
const msg = String((err && err.message) || err || "");
return /ECONNREFUSED|ECONNRESET|ENOTFOUND|EAI_AGAIN|fetch failed|timed out after \d+ms/i.test(msg);
}

export function parseArgs(argv) {
const opts = { execute: false };
for (let i = 0; i < argv.length; i++) {
Expand Down Expand Up @@ -117,6 +148,12 @@ export function planCycle(opts, cfg, runId) {
}
}

// Runtime fallback chain: a role whose primary model turns out unreachable at
// invocation time falls back to the next entry here rather than failing the whole
// cycle. See invokeRoleOpenAI and isUnreachableError.
maker.chain = buildFallbackChain(cfg, "maker", maker, known);
checker.chain = buildFallbackChain(cfg, "checker", checker, known);

// Turn cap.
const maxTurns = opts.maxTurns ?? DEFAULT_MAX_TURNS;
if (!Number.isInteger(maxTurns) || maxTurns <= 0) throw new Error("max-turns must be a positive integer.");
Expand Down Expand Up @@ -160,6 +197,7 @@ export function planCycle(opts, cfg, runId) {
if (known.size > 0 && !known.has(crew.model)) {
throw new Error(`${role} model "${crew.model}" is not in the models registry; pin it in .modonome/config.yaml.`);
}
crew.chain = buildFallbackChain(cfg, role, crew, known);
const route = resolveExecutionTarget(crew, cfg);
plan[role] = { ...crew, id: `${role}:${appName}:${runId}:${crew.model}`, route, execMode: resolveExecMode(cfg, crew.model) };
if (isBillable(crew.costClass)) plan.usesRemote = true;
Expand Down Expand Up @@ -257,32 +295,64 @@ function invokeRoleClaudeCli(plan, role, env) {
export async function invokeRoleOpenAI(plan, role, env, deps = {}) {
const chatCompletionImpl = deps.chatCompletionImpl ?? chatCompletion;
const applyPatchImpl = deps.applyPatchImpl ?? applyPatch;

const r = plan[role];
const prompt = buildRolePrompt(plan, role, env);
const baseUrl = r.modelBaseUrl ?? deps.defaultBaseUrl ?? resolveProvider(r.modelProvider).defaultBaseUrl;
const authToken = r.authEnv ? env[r.authEnv] : undefined;

const result = await chatCompletionImpl({
baseUrl,
authToken,
model: r.model,
messages: [{ role: "user", content: prompt }],
maxTokens: deps.maxTokens,
timeoutMs: deps.timeoutMs,
});
const attemptOnce = async (er, extraMetric) => {
const prompt = buildRolePrompt({ ...plan, [role]: er }, role, env);
const baseUrl = er.modelBaseUrl ?? deps.defaultBaseUrl ?? resolveProvider(er.modelProvider).defaultBaseUrl;
const authToken = er.authEnv ? env[er.authEnv] : undefined;

const result = await chatCompletionImpl({
baseUrl,
authToken,
model: er.model,
messages: [{ role: "user", content: prompt }],
maxTokens: deps.maxTokens,
timeoutMs: deps.timeoutMs,
});

const diff = extractDiff(result.text);
const patch = diff
? applyPatchImpl(diff, resolve(root, plan.target))
: { applied: false, reason: "no diff found in model response." };
const diff = extractDiff(result.text);
const patch = diff
? applyPatchImpl(diff, resolve(root, plan.target))
: { applied: false, reason: "no diff found in model response." };

const transcript = `${result.text}\n\n[patch applied: ${patch.applied}] ${patch.reason}\n`;
writeTranscriptAndMetric(plan, role, r, transcript, {
patch_applied: patch.applied,
patch_reason: patch.reason,
});
return 0;
const transcript = `${result.text}\n\n[patch applied: ${patch.applied}] ${patch.reason}\n`;
writeTranscriptAndMetric(plan, role, er, transcript, {
patch_applied: patch.applied,
patch_reason: patch.reason,
...extraMetric,
});
};

// No runtime fallback chain (a hand-built plan, or a role with a single-model chain):
// exactly the original one-shot behavior. Any error propagates uncaught, as before.
const chain = Array.isArray(r.chain) && r.chain.length > 1 ? r.chain : null;
if (!chain) {
await attemptOnce(r, {});
return 0;
}

const attempted = [];
let lastErr;
for (let i = 0; i < chain.length; i++) {
const candidate = chain[i];
// Never silently spend past the budget: skip a paid candidate this run cannot
// afford exactly as if it were never in the chain, no attempt, no error.
if (isBillable(candidate.costClass) && !plan.remoteAllowed) continue;

attempted.push(candidate.model);
const er = { ...r, ...candidate, id: `${role}:${plan.appName}:${plan.runId}:${candidate.model}` };
try {
await attemptOnce(er, attempted.length > 1 ? { model_fallback_from: attempted.slice(0, -1) } : {});
return 0;
} catch (err) {
const isLast = i === chain.length - 1;
if (!isUnreachableError(err) || isLast) throw err;
lastErr = err;
}
}
// Every candidate was unaffordable under the current budget; none was attempted.
throw lastErr ?? new Error(`${role}: no model in the fallback chain is affordable under the current budget.`);
}

// Load the single agentic-CLI adapter entry from adapters.json for the tool-loop
Expand Down
2 changes: 1 addition & 1 deletion site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ <h2 style="font-family:'Space Grotesk',sans-serif;font-weight:600;font-size:clam
netGeo: null, netActive: -1, netPhase: 'idle', netSeq: 0, netShared: 128, activeSection: 'top',
live: null, syncStatus: 'cached', relIndex: 0 };
this.repoBase = { tests: 248, coverage: '81%', deps: 3, debt: 12 };
this.engineBase = { lessons: 3, rules: 0, gates: 8, queue: 6, version: 'v0.1.0-alpha', armed: false };
this.engineBase = { lessons: 3, rules: 0, gates: 7, queue: 5, version: 'v0.1.0-alpha', armed: false };
this.STAGES = [
{title:'Maker', sub:'builds one small change', scope:'AGENT', live:'The maker is writing one small, scoped change and pinning it with a test.'},
{title:'Checker', sub:'reviews it independently', scope:'AGENT', live:'A separate checker reviews the diff. The maker never approves its own work.'},
Expand Down
Loading
Loading