Repository navigation
plugin: run the loop guard's judge on Apple's on-device model (local_model=fm) - #185
Michael Avrukin (mavrukin) wants to merge 5 commits into
Conversation
…model=fm) macOS 27 ships Apple's Foundation Models CLI at /usr/bin/fm: a ~3B model on the Neural Engine and GPU, 4,096-token window, no login or key. With local_model=fm the side calls local_roles names (default: judge) run on it instead of the agent's CLI, and are charged 0 tokens. - sidecall: fm_available, fm_call (prompt on stdin, greedy, optional JSON schema via `fm respond --schema`), local_runner, and a role argument to runner_for. A local call that fails (no fm, model not ready, prompt over the window, no reply) falls back to the agent's CLI, so the guard never loses a verdict; while that CLI is paused (signed out), the local judge still answers. - judge: the judge passes its verdict schema; under it the model returns the object every time, where in free text it often wraps or contradicts it. - local_judge_confidence (default 0.9): a stuck verdict from the local judge counts only at this floor. Its confidences come in coarse steps (0.65/0.72/0.85/0.92), most above judge_confidence (0.7). - Settings local_model, local_roles, local_judge_confidence (and TE_* vars); documented in plugin/README.md. Off by default. - tests/test_local_model.py; conftest fakes fm like the other side calls. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…t names An agent cannot read audio: Claude Code's Read rejects an .m4a or .aiff as binary before any hook sees the call, so a prompt like "implement what the PM asks in notes/memo.m4a" sends the agent hunting for a transcriber. With media=audio, the prompt hook finds the audio paths the prompt names, transcribes each on the Mac with Apple's Speech framework (SpeechAnalyzer), and adds the transcripts to that prompt's context, as the repository map is added. No model call, nothing leaves the machine; transcripts are cached by content hash. - plugin/media.py: path detection (up to 3 files, 50 MB each), the cache, and the helper's build. - plugin/scripts/te-transcribe.swift: the transcriber, compiled once with swiftc into <state root>/bin by `te media setup`, or in the background by the first prompt that needs it (that prompt goes without). Without macOS 26+ and swiftc the mechanism does nothing and says so once. - controller.user_prompt adds the block on hosts with prompt context; shadow logs it without injecting. `te media [status|setup]`. - Setting media (off|audio), TE_MEDIA; documented in plugin/README.md. Off by default. tests/test_media.py. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Aaron Li (aaron-jx-li)
left a comment
There was a problem hiding this comment.
Thanks for the careful write-up in #186. We'd like to take the two options separately.
On-device judge (local_model=fm): we can take it with small changes.
controller.pyprobesfm availableinside the PostToolUse hook while the host is signed out. That runs on every tool call, because each hook is a new process. Please check only the config there.- Please call
/usr/bin/fmby its full path and only on macOS, instead of looking it up on PATH. - Please point the
config.pydocstring at #186 instead of quoting the comparison with Haiku.
Audio transcripts (media=audio): we're not taking this one. The plugin reads finished sessions to change what later sessions cost, and transcribing files at prompt time is outside that. It would also be our first compiled helper, which CI can't build or test. If you'd like to keep it, it could live as a separate plugin. For reference, two bugs we found:
- With
repomap=on,outputs = self._inject_map(...)replaces the transcript on the first prompt. - Transcription can take up to 45 s inside a prompt hook whose limit is 20 s, and a killed hook also loses the loop guard's new turn.
Could you split the first commit into its own PR, rebased on main?
# Conflicts: # plugin/README.md # tokeneconomizer/plugin/config.py
Per review on microsoft#185: transcribing files at prompt time is outside what the plugin does (it reads finished sessions to change what later sessions cost), and it would be the first compiled helper, which CI cannot build or test. It may come back as a separate plugin. Removes plugin/media.py, plugin/scripts/te-transcribe.swift, `te media`, the `media` setting and its docs, and tests/test_media.py. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UporVpn64UvJqEnurKVwza
…on macOS only) Per review on microsoft#185: - controller: while the agent's CLI is paused, PostToolUse picks the judge from the settings alone (sidecall.local_routes: local_model, local_roles and the platform; no subprocess). Each hook is a new process, so the old `fm available` probe ran on every tool call. The judge's own job checks that the model answers, and with none spares the turn. - sidecall: FM_CLI is /usr/bin/fm, run by its full path (os.access), never looked up on PATH, and only on macOS. - config: the local_judge_confidence docstring points at microsoft#186 instead of quoting the comparison with Haiku. - plugin/README.md: without fm the call goes to the agent's CLI without an errors.log line; only a call fm cannot answer is logged. The setting does nothing off macOS. - tests: local_routes runs no subprocess; a paused host's _judge never probes fm (fails on the old code); fm_available runs /usr/bin/fm on macOS only and never searches PATH. A `_macos` seam lets them run on Linux CI. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UporVpn64UvJqEnurKVwza
|
Thanks for the review. This PR is now just
The merge with |
|
Michael Avrukin (@mavrukin) please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
Adds
local_model=fm, off by default: the loop guard's judge runs on Apple's Foundation Models CLI (/usr/bin/fm, macOS 27 with Apple Intelligence) instead ofclaude -p/codex exec/ …. It costs 0 tokens, and it still answers while the agent's CLI is signed out. On other systems the setting does nothing.Rationale, method and every result, including what did not work: #186.
Results
Judge, replayed on 302 real loop-guard decision points. I took 502 public SWE-agent trajectories (
nebius/SWE-agent-trajectories) and replayed them through this repo'sTurnGuard, default thresholds. Each verdict below comes fromllm_stuck_judge, through TE's stockclaude_callfor Haiku and this PR'sfm_callfor fm. A redirect isstuckwith confidence at or above the threshold.local_judge_confidence0.9)local_judge_confidence=0.9.fm respond --schema).Changes
plugin/sidecall.py:local_routes: the settings and the platform only, no subprocess.fm_available: runs/usr/bin/fmby its full path, on macOS only.fm_call: prompt on stdin, greedy decoding, optional JSON schema.local_runner, and aroleargument torunner_for.fm, the call goes to the agent's CLI. A callfmcan't answer is logged and goes there too.plugin/judge.py: the judge and the redirect writer each get their own runner. A local stuck verdict belowlocal_judge_confidencespares the turn. A local call is charged 0 tokens.plugin/controller.py: while the host is paused, PostToolUse picks the judge from the settings alone, so no hook runsfm. The judge's own job checks that the model answers.plugin/fold.py: role-aware runner.plugin/config.py:local_model,local_roles(defaultjudge) andlocal_judge_confidence(0.9), withTE_*variables.plugin/README.md(On-device side calls) and one sentence inREADME.md. The plugin's own text makes no claims of effect.tests/test_local_model.py(29 tests).fmand the platform are faked, so they run on Linux CI.conftest.pyfakesfmlike the other side calls.python -m pytest tests experiments/tests: 1,315 passed, 22 skipped on 3.13.testsalso passes on 3.10.scripts/dead_code.pyreports nothing new compared withmain.After review
mainwas merged in, with no force push. The conflicts were additions next to #187's per-agent model settings; both are kept. Then two commits:plugin: drop media=audio. It's out of scope here; we'll consider it as a separate plugin.plugin: local judge review fixes:fmcheck in a hook;/usr/bin/fmon macOS only;Not covered
🤖 Generated with Claude Code
https://claude.ai/code/session_01UporVpn64UvJqEnurKVwza