Skip to content

plugin: run the loop guard's judge on Apple's on-device model (local_model=fm) - #185

Open
Michael Avrukin (mavrukin) wants to merge 6 commits into
microsoft:mainfrom
mavrukin:feat/apple-fm-local-side-calls
Open

Michael Avrukin (mavrukin) wants to merge 6 commits into
microsoft:mainfrom
mavrukin:feat/apple-fm-local-side-calls

Conversation

@mavrukin

@mavrukin Michael Avrukin (mavrukin) commented Nov 9, 2026 •

Copy link
Copy Markdown

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 of claude -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's TurnGuard, default thresholds. Each verdict below comes from llm_stuck_judge, through TE's stock claude_call for Haiku and this PR's fm_call for fm. A redirect is stuck with confidence at or above the threshold.

Judge threshold redirected, resolved runs ↓ redirected, context-exhausted runs redirected, other unresolved AUC tokens / call s / call
Haiku 5.5 (current default) 0.70 26/52 (50%) 115/150 (77%) 53/100 (53%) 0.684 6,981 3.9
Haiku 5.5 0.85 10/52 (19%) 63/150 (42%) 21/100 (21%) 0.684 6,981 3.9
fm, this PR (local_judge_confidence 0.9) 0.90 7/52 (13%) 56/150 (37%) 13/100 (13%) 0.639 0 2.0
fm without the floor 0.70 37/52 (71%) 123/150 (82%) 76/100 (76%) 0.639 0 2.0
fm without a schema (free text) 0.70 4/52 (8%) 13/150 (9%) 2/100 (2%) 0.383 0 1.9
  • fm's confidences come in coarse steps (0.65/0.72/0.85/0.92). At the guard's 0.7 it lets nearly every "stuck" verdict through, hence local_judge_confidence=0.9.
  • At that floor fm sits close to Haiku's own trade-off curve, slightly below it (AUC 0.64 vs 0.68). It redirects fewer resolved runs than Haiku at 0.85, and catches fewer loops.
  • The 302 Haiku calls cost 2.1M effective tokens; the same calls on fm cost 0.
  • Without a schema fm can't tell loops from successes: it redirects 9% of exhausted runs' points and 8% of resolved runs' (AUC 0.38). In 182 of its 283 "not stuck" verdicts, the reason contains a loop phrase such as "repeated identical failures". So the judge always passes its verdict schema (fm respond --schema).

Changes

  • plugin/sidecall.py:
    • local_routes: the settings and the platform only, no subprocess.
    • fm_available: runs /usr/bin/fm by its full path, on macOS only.
    • fm_call: prompt on stdin, greedy decoding, optional JSON schema.
    • local_runner, and a role argument to runner_for.
    • Without fm, the call goes to the agent's CLI. A call fm can'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 below local_judge_confidence spares 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 runs fm. The judge's own job checks that the model answers. plugin/fold.py: role-aware runner.
  • plugin/config.py: local_model, local_roles (default judge) and local_judge_confidence (0.9), with TE_* variables.
  • Docs: plugin/README.md (On-device side calls) and one sentence in README.md. The plugin's own text makes no claims of effect.
  • Tests: tests/test_local_model.py (29 tests). fm and the platform are faked, so they run on Linux CI. conftest.py fakes fm like the other side calls.
    • python -m pytest tests experiments/tests: 1,315 passed, 22 skipped on 3.13. tests also passes on 3.10.
    • scripts/dead_code.py reports nothing new compared with main.

After review

main was merged in, with no force push. The conflicts were additions next to #187's per-agent model settings; both are kept. Then two commits:

Not covered

  • Only verified on macOS 27.0.1 on an M2 with Claude Code.
  • The judge comparison uses SWE-agent (Llama) trajectories mapped onto TE's tool names, and a shared envelope rather than a per-repo one. It is not a rerun of the paper's arms; that needs the Linux/Docker harness.
  • In my live Claude Code runs, the fm judge answered, and a paused host still got verdicts. I couldn't produce an organic loop that ended in an fm redirect reaching the model, because Haiku 5.5 stops on its own when tests contradict each other.

🤖 Generated with Claude Code

https://claude.ai/code/session_01UporVpn64UvJqEnurKVwza

…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-jx-li Aaron Li (aaron-jx-li) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

  1. controller.py probes fm available inside 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.
  2. Please call /usr/bin/fm by its full path and only on macOS, instead of looking it up on PATH.
  3. Please point the config.py docstring 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
@mavrukin Michael Avrukin (mavrukin) changed the title plugin: on-device side calls (local_model=fm) and audio transcripts (media=audio) on macOS plugin: run the loop guard's judge on Apple's on-device model (local_model=fm) Nov 10, 2026
@mavrukin

Copy link
Copy Markdown
Author

Thanks for the review. This PR is now just local_model=fm. I merged main into the branch rather than rebasing, so there was no force push, and added two commits:

  • plugin: drop media=audio. Audio is out. We'll consider it as a separate plugin, outside TokenEconomizer. You're right about both bugs:
    • With repomap=on, _inject_map replaced the transcript.
    • Up to 3 files × 15 s of transcription could overrun the prompt hook's 20 s limit, and lose the loop guard's new turn with it.
  • plugin: local judge review fixes:
    1. While the host is paused, controller._judge now reads only the config: sidecall.local_routes checks local_model, local_roles and the platform, with no subprocess. The fm available check runs only in the judge's detached job, and if the model doesn't answer, the turn is spared. A new test runs _judge for a paused host with fm and subprocess.run set to fail if called. It fails on the old code.
    2. /usr/bin/fm by its full path, only on macOS. It's never looked up on PATH.
    3. The config.py docstring points at Apple's on-device model (fm) for TokenEconomizer's side calls and audio prompts: approach, rationale and results #186 instead of quoting the Haiku comparison.

The merge with main only conflicted where #187's per-agent paid_model/effort settings sit next to the new ones; both are kept. tests and experiments/tests pass on 3.13, tests passes on 3.10, and scripts/dead_code.py reports nothing new compared with main. I've updated the PR description to match.

@mavrukin

Copy link
Copy Markdown
Author

Conflict in plugin/sidecall.py only: Factory Droid's side-call helpers
(microsoft#159) and the on-device model's block were added at the same place;
both kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UporVpn64UvJqEnurKVwza
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants