Files
Duriin-API/src/autonomy/settings.js
T
ImBenjiandClaude Opus 5 0a5b30ab63 feat: new ops console for the admin surface
The old admin was five separate html pages, so every navigation was a full
reload and the operational picture was scattered across all of them. Worth
saying: the api was never the problem, every endpoint answers in under 200ms.
It felt slow because of the architecture, not the backend.

This is a single page console. React and htm from a cdn, no bundler and no
babel-in-the-browser, because a runtime transpiler on every load is exactly the
slowness we are trying to get rid of. Hash routing, polling that keeps the last
good payload on screen instead of flashing a spinner, and stale responses are
dropped so a slow request cannot overwrite a newer one.

/admin/api/ops/overview answers the whole dashboard in one call rather than
making the browser fan out and stitch. It carries the things that actually
matter and were not visible anywhere before: when live evidence matures, why a
cohort does or does not clear the trade gate, which stage of the pipeline has
gone quiet, and what is sitting in dead letters.

Controls, all of which change production and all of which ask twice:
- requeue dead letters, which only ever moves dead_letter back to pending
- execution mode and a kill switch, now read from autonomy_settings on every
  poll instead of only from AUTONOMY_EXECUTION_MODE, so halting no longer needs
  a redeploy first
- run the reaction analysis and read its output

The d3 graph is framed rather than ported. It works, and rewriting it would risk
something valuable for nothing the operator can see.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WnNxwxfXSbeNtjvtz5gayb
2026-09-01 20:17:36 +01:00

68 lines
2.4 KiB
JavaScript

// Runtime settings for the autonomy stack. The env vars stay the default, the
// table is the override, so nothing changes behaviour until somebody deliberately
// writes a row. Kept deliberately tiny -- this is a control plane, not a config
// system, and every key here can move real money or stop the pipeline.
const EXECUTION_MODES = ['shadow', 'paper'];
const KEYS = {
executionMode: 'execution_mode',
killSwitch: 'execution_kill_switch',
};
function readSetting(db, key) {
try {
const row = db.prepare('SELECT value FROM autonomy_settings WHERE key = ?').get(key);
return row ? row.value : null;
} catch (error) {
// A missing table means an older schema, which should behave like "no override"
// rather than taking the worker down with it.
console.error(`[settings] could not read ${key}:`, error.message);
return null;
}
}
function writeSetting(db, key, value, updatedBy = 'admin') {
db.prepare(`
INSERT INTO autonomy_settings(key, value, updated_by) VALUES (?, ?, ?)
ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated_by = excluded.updated_by,
updated_at = datetime('now')
`).run(key, String(value), updatedBy);
}
// Truthy strings people actually type, rather than only accepting 'true'
function isOn(value) {
return ['1', 'true', 'on', 'yes', 'engaged'].includes(String(value || '').trim().toLowerCase());
}
function getExecutionControls(db, env = process.env) {
const stored = readSetting(db, KEYS.executionMode);
const fallback = env.AUTONOMY_EXECUTION_MODE || 'shadow';
const mode = EXECUTION_MODES.includes(String(stored)) ? String(stored) : fallback;
return {
mode: EXECUTION_MODES.includes(mode) ? mode : 'shadow',
modeSource: EXECUTION_MODES.includes(String(stored)) ? 'settings' : 'env',
killSwitch: isOn(readSetting(db, KEYS.killSwitch)),
};
}
function setExecutionMode(db, mode, updatedBy) {
if (!EXECUTION_MODES.includes(mode)) {
throw new Error(`unsupported execution mode: ${mode} (expected ${EXECUTION_MODES.join(' or ')})`);
}
writeSetting(db, KEYS.executionMode, mode, updatedBy);
return getExecutionControls(db);
}
function setKillSwitch(db, engaged, updatedBy) {
writeSetting(db, KEYS.killSwitch, engaged ? 'true' : 'false', updatedBy);
return getExecutionControls(db);
}
module.exports = {
EXECUTION_MODES, KEYS, isOn,
readSetting, writeSetting,
getExecutionControls, setExecutionMode, setKillSwitch,
};