Commit 16c0e08
authored
feat(scripts): YOLO transcript analyzer for permissions.allow seeding (#281)
* feat(scripts): YOLO transcript analyzer for permissions.allow seeding
Walks Claude Code JSONL session transcripts under
~/.claude-work/projects/ and ~/.claude/projects/, extracts paired-success
Bash invocations, classifies them by safety category (read-only,
search/inspect, build/test, file/cache, local-git mutation, MUTATING),
and emits a proposed permissions.allow array as both stdout summary
and machine-readable JSON at
~/.local/spellbook/state/proposed_allow_list.json.
The script never writes settings.json -- the orchestrator (or user) is
expected to review the proposal before piping it through
install_permissions.
Mutating commands (git push, gh pr merge, acli jira workitem
transition/edit) are recorded under rejected_mutating and never
promoted to the allowlist regardless of frequency.
Bash invocations are yielded only when paired with an explicit
non-error tool_result; unpaired tool_uses (interrupted sessions where
the user-side confirmation never landed) are dropped, since we cannot
confirm the command actually succeeded.
* fix(installer): handle null values in settings.json reads
A settings.json file containing the literal JSON value ``null`` (or any
non-object top-level value) used to crash the installer with
``TypeError: dict(None)`` when ``read_settings`` returned the parsed
``None`` and downstream callers tried to wrap it in a dict.
Fixes three null-propagation paths surfaced by Gemini code review:
- ``_settings_io.read_settings`` now returns ``{}`` for any non-dict
top-level JSON value, treating it the same as a missing or empty
file.
- ``install_permissions`` now uses ``... or {}`` / ``... or []``
guards on ``existing_settings.get("permissions")`` and
``perms_section.get(bucket)`` so an explicit ``"permissions": null``
or ``"allow": null`` in the user's settings.json no longer crashes
the install path.
- ``uninstall_permissions`` carries the same guards on its own read of
the permissions section.
* test(installer): migrate managed_permissions_state test to tripwire
The wiring tests in test_claude_code_wires_default_mode_and_permissions
used ``monkeypatch.setattr`` to redirect the
``managed_permissions_state._STATE_FILE_PATH`` constant to a tmp_path,
which violates the repo style rule that ``monkeypatch`` is reserved for
env vars, cwd, and sys.path.
To support tripwire-based mocking of the state-file path, introduce a
``_state_file_path()`` accessor on ``managed_permissions_state`` and
route every internal callsite through it. The accessor reads the
existing ``_STATE_FILE_PATH`` constant dynamically so the legacy
monkeypatch-based tests in test_install_default_mode.py continue to
work unchanged; new tests should mock the accessor instead.
The five wiring tests now register a tripwire mock for
``_state_file_path`` with an empirically probed call budget
(9 per install, 6 per uninstall) and assert the calls in the same
``in_any_order`` block as the existing mocks. If the SUT changes the
number of state-file accesses, the assertion will fail loudly and the
constants ``_STATE_PATH_CALLS_PER_INSTALL`` and
``_STATE_PATH_CALLS_PER_UNINSTALL`` must be updated together with the
new probed counts.
* test(installer): replace _FlakyReplace stub with tripwire sequential mock
The hooks atomic-write tests used a hand-rolled ``_FlakyReplace``
class to stub ``os.replace``: first call raises ``PermissionError``,
second call performs the real rename. Hand-rolled stubs are forbidden
by the repo style guide -- mocks must go through tripwire so the
verifier can assert every interaction.
Replace the stub with tripwire's sequential FIFO of side effects:
``mock_replace.__call__.raises(PermissionError(...)).calls(real_os_replace)``.
The retry contract is now expressed in the assertion order itself --
``assert_call(..., raised=AnyThing)`` followed by
``assert_call(..., returned=AnyThing)`` -- which is strictly more
verifiable than the previous ``flaky.count == 2`` check, since the
order of the raise-then-succeed sequence is now part of the assertion.
Also drops the import-time ``_REAL_OS_REPLACE`` capture; the real
``os.replace`` is captured inside the helper instead so the test file
has no module-global state.
* test(installer): cover read_settings non-dict JSON guard
Adds 12 tests for read_settings in installer/components/_settings_io.py:
absent file, empty file, whitespace-only file, valid object, malformed
JSON (raises), and the non-object top-level cases (null, empty array,
populated array, number, string, true, false) that the isinstance guard
collapses to {}.
Closes Momus BOT-A2 (review on axiomantic/spellbook PR review pass 2).
---------
Co-authored-by: elijahr <153711+elijahr@users.noreply.github.com>1 parent 854b7ca commit 16c0e08
13 files changed
Lines changed: 1318 additions & 65 deletions
File tree
- installer/components
- scripts
- tests
- installer
- test_scripts
- fixtures/yolo_transcripts/-Users-fake-project
- aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/subagents
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
8 | 33 | | |
9 | 34 | | |
10 | 35 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
18 | 23 | | |
19 | 24 | | |
20 | 25 | | |
21 | 26 | | |
22 | 27 | | |
23 | 28 | | |
24 | | - | |
| 29 | + | |
| 30 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
41 | | - | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
42 | 43 | | |
43 | 44 | | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
44 | 57 | | |
45 | 58 | | |
46 | 59 | | |
| |||
59 | 72 | | |
60 | 73 | | |
61 | 74 | | |
62 | | - | |
| 75 | + | |
63 | 76 | | |
64 | 77 | | |
65 | 78 | | |
| |||
149 | 162 | | |
150 | 163 | | |
151 | 164 | | |
152 | | - | |
| 165 | + | |
153 | 166 | | |
154 | 167 | | |
155 | 168 | | |
| |||
184 | 197 | | |
185 | 198 | | |
186 | 199 | | |
187 | | - | |
| 200 | + | |
188 | 201 | | |
189 | 202 | | |
190 | 203 | | |
| |||
211 | 224 | | |
212 | 225 | | |
213 | 226 | | |
214 | | - | |
| 227 | + | |
| 228 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
105 | | - | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
106 | 112 | | |
107 | 113 | | |
108 | 114 | | |
109 | 115 | | |
110 | 116 | | |
111 | 117 | | |
112 | 118 | | |
| 119 | + | |
113 | 120 | | |
114 | | - | |
| 121 | + | |
115 | 122 | | |
116 | 123 | | |
117 | 124 | | |
| |||
125 | 132 | | |
126 | 133 | | |
127 | 134 | | |
128 | | - | |
| 135 | + | |
| 136 | + | |
129 | 137 | | |
130 | 138 | | |
131 | 139 | | |
| |||
335 | 343 | | |
336 | 344 | | |
337 | 345 | | |
338 | | - | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
339 | 350 | | |
340 | 351 | | |
341 | 352 | | |
342 | | - | |
| 353 | + | |
343 | 354 | | |
344 | 355 | | |
345 | 356 | | |
| |||
0 commit comments