feat: add /debug runtime overrides

This commit is contained in:
Peter Steinberger
2026-01-09 16:38:52 +01:00
parent 36bdec0f2c
commit c643ce2a7a
14 changed files with 412 additions and 2 deletions

View File

@@ -26,6 +26,7 @@ describe("commands registry", () => {
expect(detection.regex.test("/status:")).toBe(true);
expect(detection.regex.test("/stop")).toBe(true);
expect(detection.regex.test("/send:")).toBe(true);
expect(detection.regex.test("/debug set foo=bar")).toBe(true);
expect(detection.regex.test("/models")).toBe(true);
expect(detection.regex.test("/models list")).toBe(true);
expect(detection.regex.test("try /status")).toBe(false);

View File

@@ -27,6 +27,13 @@ const CHAT_COMMANDS: ChatCommandDefinition[] = [
description: "Show current status.",
textAliases: ["/status"],
},
{
key: "debug",
nativeName: "debug",
description: "Set runtime debug overrides.",
textAliases: ["/debug"],
acceptsArgs: true,
},
{
key: "cost",
nativeName: "cost",

View File

@@ -40,6 +40,12 @@ import { enqueueSystemEvent } from "../../infra/system-events.js";
import { parseAgentSessionKey } from "../../routing/session-key.js";
import { resolveSendPolicy } from "../../sessions/send-policy.js";
import { normalizeE164 } from "../../utils.js";
import {
getConfigOverrides,
resetConfigOverrides,
setConfigOverride,
unsetConfigOverride,
} from "../../config/runtime-overrides.js";
import { resolveCommandAuthorization } from "../command-auth.js";
import {
normalizeCommandBody,
@@ -65,6 +71,7 @@ import type {
} from "../thinking.js";
import type { ReplyPayload } from "../types.js";
import { isAbortTrigger, setAbortMemory } from "./abort.js";
import { parseDebugCommand } from "./debug-commands.js";
import type { InlineDirectives } from "./directive-handling.js";
import { stripMentions, stripStructuralPrefixes } from "./mentions.js";
import { getFollowupQueueDepth, resolveQueueSettings } from "./queue.js";
@@ -609,6 +616,81 @@ export async function handleCommands(params: {
return { shouldContinue: false, reply };
}
const debugCommand = allowTextCommands
? parseDebugCommand(command.commandBodyNormalized)
: null;
if (debugCommand) {
if (!command.isAuthorizedSender) {
logVerbose(
`Ignoring /debug from unauthorized sender: ${command.senderE164 || "<unknown>"}`,
);
return { shouldContinue: false };
}
if (debugCommand.action === "error") {
return { shouldContinue: false, reply: { text: `⚠️ ${debugCommand.message}` } };
}
if (debugCommand.action === "show") {
const overrides = getConfigOverrides();
const hasOverrides = Object.keys(overrides).length > 0;
if (!hasOverrides) {
return {
shouldContinue: false,
reply: { text: "⚙️ Debug overrides: (none)" },
};
}
const json = JSON.stringify(overrides, null, 2);
return {
shouldContinue: false,
reply: { text: `⚙️ Debug overrides (memory-only):\n\`\`\`json\n${json}\n\`\`\`` },
};
}
if (debugCommand.action === "reset") {
resetConfigOverrides();
return {
shouldContinue: false,
reply: { text: "⚙️ Debug overrides cleared; using config on disk." },
};
}
if (debugCommand.action === "unset") {
const result = unsetConfigOverride(debugCommand.path);
if (!result.ok) {
return {
shouldContinue: false,
reply: { text: `⚠️ ${result.error ?? "Invalid path."}` },
};
}
if (!result.removed) {
return {
shouldContinue: false,
reply: { text: `⚙️ No debug override found for ${debugCommand.path}.` },
};
}
return {
shouldContinue: false,
reply: { text: `⚙️ Debug override removed for ${debugCommand.path}.` },
};
}
if (debugCommand.action === "set") {
const result = setConfigOverride(debugCommand.path, debugCommand.value);
if (!result.ok) {
return {
shouldContinue: false,
reply: { text: `⚠️ ${result.error ?? "Invalid override."}` },
};
}
const valueLabel =
typeof debugCommand.value === "string"
? `"${debugCommand.value}"`
: JSON.stringify(debugCommand.value);
return {
shouldContinue: false,
reply: {
text: `⚙️ Debug override set: ${debugCommand.path}=${valueLabel ?? "null"}`,
},
};
}
}
const stopRequested = command.commandBodyNormalized === "/stop";
if (allowTextCommands && stopRequested) {
if (!command.isAuthorizedSender) {

View File

@@ -0,0 +1,21 @@
import { describe, expect, it } from "vitest";
import { parseDebugCommand } from "./debug-commands.js";
describe("parseDebugCommand", () => {
it("parses show/reset", () => {
expect(parseDebugCommand("/debug")).toEqual({ action: "show" });
expect(parseDebugCommand("/debug show")).toEqual({ action: "show" });
expect(parseDebugCommand("/debug reset")).toEqual({ action: "reset" });
});
it("parses set with JSON", () => {
const cmd = parseDebugCommand('/debug set foo={"a":1}');
expect(cmd).toEqual({ action: "set", path: "foo", value: { a: 1 } });
});
it("parses unset", () => {
const cmd = parseDebugCommand("/debug unset foo.bar");
expect(cmd).toEqual({ action: "unset", path: "foo.bar" });
});
});

View File

@@ -0,0 +1,98 @@
export type DebugCommand =
| { action: "show" }
| { action: "reset" }
| { action: "set"; path: string; value: unknown }
| { action: "unset"; path: string }
| { action: "error"; message: string };
function parseDebugValue(raw: string): { value?: unknown; error?: string } {
const trimmed = raw.trim();
if (!trimmed) return { error: "Missing value." };
if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
try {
return { value: JSON.parse(trimmed) };
} catch (err) {
return { error: `Invalid JSON: ${String(err)}` };
}
}
if (trimmed === "true") return { value: true };
if (trimmed === "false") return { value: false };
if (trimmed === "null") return { value: null };
if (/^-?\d+(\.\d+)?$/.test(trimmed)) {
const num = Number(trimmed);
if (Number.isFinite(num)) return { value: num };
}
if (
(trimmed.startsWith("\"") && trimmed.endsWith("\"")) ||
(trimmed.startsWith("'") && trimmed.endsWith("'"))
) {
try {
return { value: JSON.parse(trimmed) };
} catch {
const unquoted = trimmed.slice(1, -1);
return { value: unquoted };
}
}
return { value: trimmed };
}
export function parseDebugCommand(raw: string): DebugCommand | null {
const trimmed = raw.trim();
if (!trimmed.toLowerCase().startsWith("/debug")) return null;
const rest = trimmed.slice("/debug".length).trim();
if (!rest) return { action: "show" };
const match = rest.match(/^(\S+)(?:\s+([\s\S]+))?$/);
if (!match) return { action: "error", message: "Invalid /debug syntax." };
const action = match[1].toLowerCase();
const args = (match[2] ?? "").trim();
switch (action) {
case "show":
return { action: "show" };
case "reset":
return { action: "reset" };
case "unset": {
if (!args) return { action: "error", message: "Usage: /debug unset path" };
return { action: "unset", path: args };
}
case "set": {
if (!args) {
return {
action: "error",
message: "Usage: /debug set path=value",
};
}
const eqIndex = args.indexOf("=");
if (eqIndex <= 0) {
return {
action: "error",
message: "Usage: /debug set path=value",
};
}
const path = args.slice(0, eqIndex).trim();
const rawValue = args.slice(eqIndex + 1);
if (!path) {
return {
action: "error",
message: "Usage: /debug set path=value",
};
}
const parsed = parseDebugValue(rawValue);
if (parsed.error) {
return { action: "error", message: parsed.error };
}
return { action: "set", path, value: parsed.value };
}
default:
return {
action: "error",
message: "Usage: /debug show|set|unset|reset",
};
}
}

View File

@@ -357,6 +357,6 @@ export function buildHelpMessage(): string {
return [
" Help",
"Shortcuts: /new reset | /compact [instructions] | /restart relink (if enabled)",
"Options: /think <level> | /verbose on|off | /reasoning on|off | /elevated on|off | /model <id> | /cost on|off",
"Options: /think <level> | /verbose on|off | /reasoning on|off | /elevated on|off | /model <id> | /cost on|off | /debug show",
].join("\n");
}