Skip to content

Commit cd21d0c

Browse files
committed
fix(plugin,scripts): apply council audit post-ship follow-up fixes
Four follow-up findings from the v0.18.0 pre-release council audit. None were release-blocking on their own; bundled here to reach a fully-clean audit state before v0.18.0 publishes. Finding #2 — TUI keymap.registerLayer try-catch (tui/index.tsx) When OpenCode TUI 1.14.42-1.14.43 exposes `keymap.registerLayer` as a function but throws at call time, the unguarded call previously made the legacy `command.register` fallback (~20 lines down) unreachable. Wrap the call in try-catch, log a debug line, and fall through to the legacy path so older command.register-only flows still get the Magic Context palette entries. Finding #7 — ctx_memory getAllowedActions fail-closed default (tools.ts) Previously when a caller omitted `allowedActions`, the helper defaulted to the dreamer-permissive set (all 6 actions). Production wiring in tool-registry.ts always passes `["write", "delete"]` explicitly, but a future caller forgetting the field would have inadvertently let primary agents run `list/update/merge/archive`. Change the default to `["write", "delete"]` — dreamer child sessions are still allowed full access via the runtime `toolContext.agent === DREAMER_AGENT` gate, which bypasses `allowedActions` entirely. The 4 ctx-memory tests that exercised `list/update/merge/archive` in the default test setup are updated to use `DREAMER_AGENT` as their `toolContext.agent`, which is the realistic production shape (only dreamer calls those actions). Finding #9 — release-dashboard.sh non-tty guard (scripts) The non-main-branch confirmation prompt at the top of the script used `read -rp` without a TTY guard, which would hang forever in CI or background invocations. Add an explicit `have_tty` check that refuses the release with a clear error message when no controlling terminal is available. Use `< /dev/tty` on the actual read for consistency with other prompts in the script. Finding #10 — historian-editor fallback policy documentation (compartment-runner-historian.ts) The two-pass editor path deliberately does NOT receive `fallbackModels` from the caller — if the editor model fails, falling back to the already-validated draft is cheaper and safer than iterating multiple fallback models for a pure polish step. Behavior is correct; just add a docblock to make the design choice explicit. Skipped (false positives on close inspection): - Finding #3 (defer-pass byte-equality test mutation): already covered by adjacent test 4 which proves snapshot isolation by mutating last_todo_state between cache-bust T0 and defer T1. - Finding #4 (ctx_memory schema enum narrowing): the existing code comment at tools.ts:185-188 explicitly documents that the full action enum is intentional for the shared tool-definition model; dreamer child sessions need the full enum and runtime gating is correct. Narrowing per-deps would require duplicate tool definitions.
1 parent e2353d3 commit cd21d0c

5 files changed

Lines changed: 72 additions & 28 deletions

File tree

‎packages/plugin/src/hooks/magic-context/compartment-runner-historian.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,15 @@ export async function runValidatedHistorianPass(args: {
149149
* editor's validated result if successful; falls back to the draft on any
150150
* failure (editor call, validation, or invalid structure). Editor can never
151151
* regress behavior — worst case we return the same validated draft.
152+
*
153+
* Fallback-chain policy (Audit Finding #10 clarification): the editor pass
154+
* deliberately does NOT receive `fallbackModels`. If the configured editor
155+
* model fails (auth, model-not-found, transient network, or the editor's own
156+
* output fails validation), the function returns the already-validated draft
157+
* unchanged. Iterating through fallback models here would cost extra LLM
158+
* calls per chunk for no compression benefit — the draft is already known to
159+
* be valid and the editor pass is purely a polish step. Letting the editor
160+
* silently no-op back to the draft is the cheaper and safer behavior.
152161
*/
153162
async function runEditorPassOrFallback(args: {
154163
client: PluginContext["client"];

‎packages/plugin/src/tools/ctx-memory/tools.test.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { afterAll, afterEach, beforeEach, describe, expect, it, mock } from "bun:test";
2+
import { DREAMER_AGENT } from "../../agents/dreamer";
23
import { getMemoriesByProject, getMemoryById, insertMemory } from "../../features/magic-context";
34
import { Database } from "../../shared/sqlite";
45
import { closeQuietly } from "../../shared/sqlite-helpers";
@@ -231,7 +232,7 @@ describe("createCtxMemoryTools", () => {
231232

232233
const result = await tools.ctx_memory.execute(
233234
{ action: "list", limit: 10 },
234-
toolContext(),
235+
toolContext("ses-dreamer", DREAMER_AGENT),
235236
);
236237

237238
expect(result).toContain("Found 2 active memories");
@@ -255,7 +256,7 @@ describe("createCtxMemoryTools", () => {
255256
id: memory.id,
256257
content: "cache_ttl=10m",
257258
},
258-
toolContext(),
259+
toolContext("ses-dreamer", DREAMER_AGENT),
259260
);
260261

261262
expect(result).toContain(`Updated memory [ID: ${memory.id}]`);
@@ -282,7 +283,7 @@ describe("createCtxMemoryTools", () => {
282283
ids: [first.id, second.id],
283284
content: "Use bun for all scripts in this repository.",
284285
},
285-
toolContext("ses-dreamer"),
286+
toolContext("ses-dreamer", DREAMER_AGENT),
286287
);
287288

288289
expect(result).toContain("Merged memories");
@@ -308,7 +309,7 @@ describe("createCtxMemoryTools", () => {
308309
id: memory.id,
309310
reason: "Removed subsystem no longer exists",
310311
},
311-
toolContext(),
312+
toolContext("ses-dreamer", DREAMER_AGENT),
312313
);
313314

314315
expect(result).toContain("Archived memory");

‎packages/plugin/src/tools/ctx-memory/tools.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,18 @@ function normalizeLimit(limit?: number): number {
4141
return Math.max(1, Math.floor(limit));
4242
}
4343

44+
// Audit Finding #7 hardening: when a caller omits `allowedActions`, fall back
45+
// to the least-privileged set instead of the dreamer's full action list. The
46+
// only production caller (`tool-registry.ts`) passes `["write", "delete"]`
47+
// explicitly, and dreamer child sessions are gated by the runtime
48+
// `toolContext.agent === DREAMER_AGENT` check below — they bypass
49+
// `allowedActions` entirely. A future caller that forgets the field would
50+
// previously have inadvertently let primary agents run `list/update/merge/
51+
// archive`; fail-closed default prevents that class of regression.
4452
function getAllowedActions(deps: CtxMemoryToolDeps): [CtxMemoryAction, ...CtxMemoryAction[]] {
4553
const allowed = deps.allowedActions?.length
4654
? deps.allowedActions
47-
: [...CTX_MEMORY_DREAMER_ACTIONS];
55+
: (["write", "delete"] as const);
4856
return [...allowed] as [CtxMemoryAction, ...CtxMemoryAction[]];
4957
}
5058

‎packages/plugin/src/tui/index.tsx‎

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -485,30 +485,47 @@ function registerCommandPaletteEntries(api: TuiPluginApi): void {
485485
const apiAny = api as unknown as ApiAny
486486

487487
if (typeof apiAny.keymap?.registerLayer === "function") {
488-
apiAny.keymap.registerLayer({
489-
commands: [
490-
{
491-
namespace: "palette",
492-
name: "magic-context.status",
493-
title: "Magic Context: Status",
494-
category: "Magic Context",
495-
run() {
496-
showStatusDialog(api)
488+
// Audit Finding #2 hardening: even when registerLayer exists as a
489+
// function, the underlying keymap implementation in OpenCode TUI
490+
// 1.14.42-1.14.43 can throw at call time. Without the try-catch the
491+
// `return` below would propagate the throw and the legacy
492+
// `command.register` fallback path (~20 lines down) would be
493+
// unreachable. The cost is one debug log on the rare broken-TUI
494+
// build; the benefit is that older command.register-only TUIs
495+
// running alongside a partially-broken keymap surface still get
496+
// their command palette entries.
497+
try {
498+
apiAny.keymap.registerLayer({
499+
commands: [
500+
{
501+
namespace: "palette",
502+
name: "magic-context.status",
503+
title: "Magic Context: Status",
504+
category: "Magic Context",
505+
run() {
506+
showStatusDialog(api)
507+
},
497508
},
498-
},
499-
{
500-
namespace: "palette",
501-
name: "magic-context.recomp",
502-
title: "Magic Context: Recomp",
503-
category: "Magic Context",
504-
run() {
505-
showRecompDialog(api)
509+
{
510+
namespace: "palette",
511+
name: "magic-context.recomp",
512+
title: "Magic Context: Recomp",
513+
category: "Magic Context",
514+
run() {
515+
showRecompDialog(api)
516+
},
506517
},
507-
},
508-
],
509-
bindings: [],
510-
})
511-
return
518+
],
519+
bindings: [],
520+
})
521+
return
522+
} catch (err) {
523+
console.debug(
524+
"[magic-context-tui] keymap.registerLayer threw; falling back to command.register",
525+
err,
526+
)
527+
// Fall through to legacy registration.
528+
}
512529
}
513530

514531
if (typeof apiAny.command?.register === "function") {

‎scripts/release-dashboard.sh‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,16 @@ fi
9999
BRANCH=$(git branch --show-current)
100100
if [[ "$BRANCH" != "main" && "$BRANCH" != "master" ]]; then
101101
echo "Warning: releasing from '$BRANCH' (not main/master)"
102-
read -rp "Continue? [y/N] " confirm
102+
# Audit Finding #9 hardening: an interactive `read -rp` here would hang
103+
# forever when the script is invoked without a controlling TTY (CI, nohup,
104+
# background task). Refuse the release explicitly in that case — the user
105+
# has to either switch to main/master or re-run from a real terminal.
106+
if ! have_tty; then
107+
echo "Error: refusing to release from non-main branch in non-interactive mode."
108+
echo " Re-run from a TTY, or switch to main/master first."
109+
exit 1
110+
fi
111+
read -rp "Continue? [y/N] " confirm </dev/tty
103112
if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then
104113
echo "Aborted."
105114
exit 1

0 commit comments

Comments
 (0)