SkillPedia Skills Encyclopedia
中文 EN

Skills 2026-06-09 01:00 UTC

claude-brain

Committed by omonien. Update Category: Bug Fixes & Stability Improvements. Commit details: fix(import): handle keybindings.json shape mismatch without crashing (#44). This update resolves known runtime errors and enhances system reliability.

omonien 09242f0 toroleapinc/claude-brain

CommitRaw commit message

fix(import): handle keybindings.json shape mismatch without crashing (#44)

* fix(import): handle keybindings.json shape mismatch without crashing

Claude Code's keybindings.json may be either an array of bindings or
an empty `{}` object (when no bindings are configured). The current
union-merge:

    jq -s '.[0] + .[1] | unique_by(.key, .command)' \
        "${CLAUDE_DIR}/keybindings.json" "$tmp_remote_kb" > "$tmp"

requires both inputs to be arrays. When either side is an object, jq
exits with:

    jq: error: object ({}) and array ([]) cannot be sorted,
    as they are not both arrays

Because import.sh runs under `set -euo pipefail`, this crash terminates
import_brain mid-way — the shared-namespace import that follows and the
"Brain import complete." marker never run. The user sees the import
"succeed" (settings, MCP servers, etc. did land) but everything after
keybindings silently drops on the floor.

Fix
---
Detect both shapes before merging:

  * array + array  → union via unique_by (existing behaviour)
  * array remote + non-array local → adopt remote
  * non-array remote → log and skip; nothing to merge

Add an integration test (test_keybindings_shape_mismatch) that builds
a brain whose keybindings.content is `{}`, seeds local keybindings.json
as `{}` too, and asserts:

  1. the import does not emit the "object and array cannot be sorted"
     jq error, AND
  2. "Brain import complete." still appears in the log — proof that
     import_brain ran to the end instead of dying at the keybindings step.

Without the fix the new test fails as expected.

* review fixes: atomic write + early-return on first failure

Address Copilot review comments on #44.

1. Replace the two new `echo "$new_keybindings" > <file>` writes with
   `printf '%s\n' "$x" > $tmp && mv $tmp <file>`. Same atomic-write
   pattern the merge branch already uses. Avoids the `echo -n` /
   non-atomic / escape-handling edge cases the reviewer flagged.

2. In `test_keybindings_shape_mismatch`, return after the first `fail`.
   When the underlying bug fires, both assertions would fail (crash text
   present AND "Brain import complete" missing) and the test would
   report two correlated failures for one root cause. Now reports a
   single clear failure per run.