Skip to content

BrainHost: context-length resolution memo — repeat agent construction ~3.8x faster (flag-gated) - #44236

Closed
claw3649 wants to merge 51 commits into
NousResearch:mainfrom
claw3649:feat/brainhost-schema-cache
Closed

BrainHost: context-length resolution memo — repeat agent construction ~3.8x faster (flag-gated)#44236
claw3649 wants to merge 51 commits into
NousResearch:mainfrom
claw3649:feat/brainhost-schema-cache

Conversation

@claw3649

Copy link
Copy Markdown

Stacked on #43422 (→ #43039) — Draft until the parents merge; review only the top commit (agent/brain_host.py, agent/model_metadata.py, tests/agent/test_brain_host.py).

First measured BrainHost value: context-length resolution memo (flag-gated, default OFF)

Measure-first result that flipped the design: profiling AIAgent construction showed tool definitions — the obvious cache candidate — are already memoized process-wide (model_tools.get_tool_definitions keyed on registry._generation, bumped on every MCP register/deregister). The dominant repeat cost is the context-length resolution: the live probe re-runs for non-Ollama endpoints on every construction (measured 335–788 ms each).

  • BrainHost.build_agent installs a memo (dict owned by the singleton) into agent.model_metadata via install_context_length_cache. Flag off = default-None passthrough, byte-identical, zero allocation — the existing gate tests already pin that agent.brain_host is never even imported.
  • Safety rules: the key captures every resolution input (model, base_url, normalized provider, config override, api_key identity, custom_providers fingerprint); lmstudio and nous are never cached (transient / portal-authoritative — the same exclusions the persistent disk cache encodes); TTL 3600 s matches the catalog horizon; probe-down fallbacks get 60 s so an outage can't freeze an under-reported window; cap 64, plus clear_context_length_cache() for explicit invalidation.

Measured: repeat construction 423 ms → 110 ms (~3.8×); the resolution itself 335–788 ms → 0.02 ms.

11 new tests (hit/miss per key input, exclusions, both TTLs, explicit + MCP-generation invalidation, cap eviction, 3-way full-construction parity); 310 green including all model_metadata suites.

🤖 Generated with Claude Code

claw3649 and others added 30 commits June 10, 2026 02:01
…res in the UI

Model-resolution reliability and picker UX, including the corrections found in
review of the in-flight batch:

- model_switch: discover keyless local endpoints (ollama/llama.cpp/LM Studio)
  so `ollama rm`'d models stop lingering in the picker — but only when no
  explicit `models:` subset is pinned, preserving an intentional narrowing
  (parity with the custom-providers section; fixes 2 test regressions).
- models: bypass the disk cache for custom/local providers (always live),
  falling through to the shared stale-beats-empty path so a transiently
  unreachable endpoint keeps its last-known-good list instead of going empty.
- model_metadata: pin local Gemma 4 26B to its served 64k num_ctx ahead of the
  /api/show GGUF-max probe so the token budget can't over-run and silently
  truncate; reconcile a stale cache entry in place instead of re-probing every
  call (the previous guard never converged).
- web/desktop picker: await the /model switch and surface a rejected switch
  (result.error) inline — keep the dialog open, roll back the optimistic model,
  toast benign advisories — instead of closing as though it worked.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- launchd_restart: try a graceful SIGUSR1 self-restart before terminate, with a
  hard-stop fallback; bind the force-kill to the freshly-resolved live PID (not
  the captured one) so PID reuse can't SIGKILL an unrelated process.
- hermes launcher: re-exec into ./venv/bin/python on Python <3.11, but only
  after verifying that interpreter is itself >=3.11 — a stale venv no longer
  silently bypasses the version guard and crashes later with an opaque error.
- tests: cover the SIGUSR1 graceful path and reconcile the terminate-drain
  fallback test with the new graceful-first behavior.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…ontract

Phase 2 of the Central Brain + Action Runtime design (docs/architecture): a
single honest-status schema the gateway's exec handlers funnel through.

- contract: ExecutionTask / ExecutionResult / Status / ErrorType / ExecError /
  SideEffect / NeedsInput / Constraints. `outputs` is a lossless payload bag so
  each handler's native shape round-trips back to its exact wire dict.
- adapters: shell / cli / plugin / slash, each a pure (to_result, to_wire) pair
  with a byte-identical round-trip guarantee.
- tests assert the round-trip identity (the byte-compat oracle).

Pure dataclasses with no dependency on the gateway (server imports this, not
the reverse). Nothing consumes the rich fields yet — that is Phase 4.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
… Runtime

- slash.exec: report a rejected live side-effect (/model, busy) and a raised
  plugin handler as a structured result.error (additive — TUI/web that read
  only output/warning are unaffected), keying off _SlashSideEffect.kind rather
  than prefix-matching the message text.
- shell.exec / cli.exec / slash.exec / plugin path now build an ExecutionResult
  and render it back through the adapters — byte-identical wire, so the three
  frontends need no change. A non-zero exit is an honest FAILED result whose
  wire shape (result.code) is unchanged.
- desktop typed `/model` path: surface result.error instead of rendering a
  rejected switch as a success line; add error? to SlashExecResponse.
- add honest-status regression coverage for the plugin error field and for the
  already-honest shell.exec / cli.exec paths (none existed before).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
A design doc for separating the Orchestration Core (reasoning/planning/memory)
from the Action Runtime (execution/tool-use), grounded in the current Hermes
gateway. Covers the decided terminology (OpenClaw = compat/reference only), the
ExecutionTask/ExecutionResult contract, the phased migration (1a honest-status →
2 unified path → 3 SessionState + Brain-host → 4 intent/idempotency → 5
multi-runtime), cross-cutting rules (additive-first, test-first, observability,
idempotency), and a decision log tracking what has landed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…n sites

Phase 3 Step 1 (Central Brain + Action Runtime design, §11). The per-session
state was an untyped dict at _sessions[sid] (~58 refs, ~30 keys, locks floating
as dict entries). Introduce a SessionState type to anchor the decomposition.

SessionState subclasses `dict`, so every existing session["key"] / .get /
.setdefault / "k" in session call keeps EXACT dict semantics — zero handler
churn. A safety analysis confirmed the gateway has no membership test, bare pop,
or iteration that relies on the session value dict, and that absent keys must
stay absent (a dict subclass gives that for free, so no field defaults need
tuning — notably agent_ready stays absent on the eager path and personality
keeps its config fallback).

Typed @Property accessors (history, history_lock, running, agent, …) let
handlers migrate to attribute access incrementally in later steps; the locks
stay dict items until the subscript sites are converted. Wraps the two real
creation literals (_init_session, session.create); test-constructed plain dicts
still work since SessionState IS a dict.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
SessionState type introduced and committed; note the next step (Step 2 lock-core
methods). Also reflects the verified AIAgent instantiation-site count.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…robe

Generalizes the root cause behind the Gemma4-26B 64k pin: Ollama's /api/show
reports the GGUF training max, which over-states the window a LOCAL server
actually serves (the user-set Modelfile num_ctx) and silently truncates
conversations once they outgrow it. Steps 2b and 5e now resolve local
endpoints num_ctx-first (query_ollama_num_ctx) while hosted servers keep the
GGUF-first probe (their users can't set num_ctx); discovered values persist
under the same lmstudio cache exclusion the sibling steps use. The explicit
Gemma pin stays as a tested guard above these steps.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Opening the picker live-probes every custom/local endpoint (keyless-local
discovery in sections 3/4 and the live "custom" branch of provider_model_ids);
with a server down each open burns the full HTTP timeout. Add a 30s in-process
memo around fetch_api_models — keyed by endpoint + credential fingerprint and
pinned to the exact function object so monkeypatched tests stay hermetic
(plus an autouse clear in tests/hermes_cli). Empty results and exceptions are
never memoized, so a down server still retries on the next open and the
stale-beats-empty disk-cache semantics are untouched.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…istry

Pre-existing cross-file pollution: the dispatch-pool tests overwrite entries in
the module-global _methods registry (slash.exec, session.compress, …) without
restoring them, so four session.compress/snapshot tests in
test_tui_gateway_server.py fail whenever both files run in one process. The
server fixture now snapshots and restores _methods in place (same dict object)
plus the directly-assigned module globals (_real_stdout, _hermes_home,
_cfg_cache/_cfg_mtime/_cfg_path). Tests-only; both files also stay green alone.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Phase 3 Step 2 (Central Brain + Action Runtime design, §11). The compaction
snapshot + compare-and-swap, the turn-release finally, and the one-shot
agent-build guard now live as SessionState methods — snapshot_history,
commit_compaction, begin_turn, end_turn, build_once — each a verbatim lift of
the server.py block it replaced (same lock, same .get defaults, same order).
Five gateway sites migrated; sites whose critical sections interleave extra
work with the CAS (the prompt.submit busy-gate + truncate, the poller gates,
bare running-flag cleanups, the mid-turn history CAS that logs under the lock)
stay deliberately inline to preserve atomicity, each noted in place.

Test helpers that feed the migrated paths now construct SessionState (a dict
subclass, so semantics are unchanged); 9 unit tests pin the method contracts.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…esult wire

Phase 4 (Central Brain + Action Runtime design, §11): make the rich
ExecutionResult consumable.

- slash.exec accepts an optional task_id and echoes it additively (the wire is
  byte-identical when absent — existing protocol tests pass unmodified).
- result_to_wire_rich renders the full contract: task_id, status, outputs,
  error {type, retryable, message}, side_effects.
- New task.submit RPC: pilot scope intent="slash" only (reversible commands,
  e.g. /model), other intents → 4030. The slash.exec body is extracted into a
  shared _slash_exec_core returning the ExecutionResult; slash.exec renders the
  legacy wire exactly as today, task.submit renders the rich wire.
- Idempotency: a locked in-process store (10-min TTL, 1024-entry cap) replays
  the recorded result with replayed=true instead of re-executing; task_id
  defaults to a fresh uuid4.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…-flight token

Phase 4 race fix (from the PR review): selectModel applied optimistic switches
with no in-flight correlation, so a slow switch A failing after a faster
switch B succeeded rolled the store back to the pre-A model, clobbering B.

Each selectModel call now captures a monotonically-increasing token; once a
newer call starts, the older call is stale — a stale success commits nothing
and a stale failure skips the store/cache rollback (the snapshot predates the
newer switch) while keeping the failure toast so the user still learns their
requested switch failed. refreshCurrentModel gains an optional staleness guard
used by the persistGlobal paths; external callers are unaffected. The slash.exec
request also carries an additive task_id for forward correlation; staleness is
decided purely by the local token. Two race tests cover both interleavings.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…sion log

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…us surfacing

Upstream moved the web model picker's chat path from slash.exec to config.set
and added the expensive-model confirmation flow (pendingConfirm). Resolution
keeps upstream's architecture and folds our honest-status work into it: all
three modes (standalone onApply, gateway config.set, legacy onSubmit fallback)
funnel through one awaited try/catch — a failed switch surfaces inline via
submitError and the dialog stays open; a benign post-switch warning fires the
new onWarning prop, which ChatSidebar toasts. The now-dead slash.exec
onModelSubmit path is removed with upstream.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…ttributes

Phase 3 Step 3: 107 square-bracket sites in tui_gateway/server.py now use the
typed accessors for the 12 hot fields (history_lock=25, session_key=22,
agent=17, running=11, history_version=8, history=7, the rest 2-3 each).
Mechanical and behavior-preserving by rule: only ["x"] reads/writes were
converted — every .get()/.setdefault()/.pop() stays untouched because absent-key
semantics differ (None/default vs KeyError), and non-session dicts (DB rows,
payload/info dicts) are excluded. Tests that inject plain-dict sessions into
the migrated paths now wrap them in SessionState (a dict subclass — no
behavioral change to the tests themselves).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Phase 3 Brain-host proof-of-life (design §11 3c/3d): agent/brain_host.py hosts
AgentSpec + a process-singleton BrainHost whose build_agent() constructs the
SAME AIAgent the call site would (parity is asserted by test, not assumed).
_make_agent now assembles its kwargs once and, only when HERMES_BRAIN_HOST=1,
routes through the host (lazy import inside the branch — default-off adds zero
import cost, proven via sys.modules in tests). V1 is deliberately the seam
only; credential-pool/tool-schema/memory-session sharing arrives as the other
~20 construction sites migrate.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…ision log

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…ns through BrainHost

Second and third construction sites behind the HERMES_BRAIN_HOST flag (default
off, byte-equivalent when unset): gateway/run.py's per-turn LRU-miss site
(intent "gateway-run") and the API server's _create_agent (intent
"api-server"). Same assemble-kwargs-then-gate pattern as _make_agent; seven new
gate tests (the api-server gate is unit-driven; the gateway-run gate is
compile+source asserted — its 1200-line async host method is not unit-drivable).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…gistry)

Grounded scoping of delegation.*/spawn_tree.*/delegate_tool state (registries,
locks, persistence, interrupt trees) and the unified design: one
AgentTaskRegistry keyed by task_id speaking ExecutionTask/ExecutionResult,
with a 6-step dual-write migration, risks R1-R7 (notably: the AIAgent-internal
_active_children interrupt chain must not be replaced), and the maintainer
decisions Q1-Q6 required before implementation starts.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
build_agent(intent, **kwargs) is the compact form of the HERMES_BRAIN_HOST
gate for migrating the remaining AIAgent construction sites. It lives in
its own light module (imports only os) so the flag-off path never imports
agent.brain_host — the zero-footprint invariant the gate tests assert —
and run_agent is only imported at call time, matching the lazy-import
style of the call sites it replaces.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…n_host_gate

Converts the 14 construction sites that were still calling AIAgent()
directly to build_agent("<intent>", ...) — exact same kwargs, callable
swap only — and collapses the three pre-existing inline HERMES_BRAIN_HOST
gate blocks (tui_gateway/_make_agent, gateway-run LRU-miss,
api_server._create_agent) into single build_agent calls with their
original intent strings.

New intents: prompt-size, oneshot, cli-background, cli, background-review,
curator, acp, cron, compress, history-hygiene, gateway-background,
feishu-comment, tui-background, preview-restart, batch, run-agent-cli,
delegate.

Deliberately untouched: cli.py's lazy AIAgent wrapper (re-export shim),
scripts/tool_search_livetest.py (dev script), and everything around
_active_subagents / spawn trees / interrupt handling in delegate_tool.

Tests: 4 unit tests for the gate helper (flag off/zero/on + kwargs
parity) and a parametrized site-table source check covering all 16
files / 20 intents, replacing the old grep-based gate-exists test whose
marker strings the gate collapse removed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…scripts to typed attributes

Adds 10 property accessors to SessionState (attached_images,
pending_title, image_counter, show_reasoning, tool_progress_mode,
tool_started_at, personality, model_override, explicit_cwd,
edit_snapshots) with exact self["key"] read/write semantics, and
converts every server.py subscript on those keys to attribute access.

Two sites deliberately stay as subscripts: session["model_override"] in
_apply_model_switch (one caller passes a plain {"agent": None} dict on
the no-session global-switch path — the isinstance guard there exists
for exactly this) and session["_finalized"] (single private marker).

Accessor-sync and write-through tests extended to cover the 10 new
accessors.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…3 Step 4 in the decision log

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…stions Q1-Q2, Q4-Q6

Q3 (Status.RUNNING placement) stays open pending an impact sweep the
maintainer requested: a consumer census of Status/ExecutionResult and
proof that record-level TaskStatus loses no running-task visibility.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…sus, visibility map, lifecycle)

Three-agent sweep: option A touches ~8 sites with one genuine breakage
(the idempotency replay store would cache a non-terminal snapshot as the
permanent answer) plus silent-success hazards on every legacy wire;
option B touches zero existing sites and loses no running-task
visibility, since every in-flight view (delegation.status, the
subagent.*/tool.* event stream, bg counters) lives outside
ExecutionResult. Recommendation recorded as option B, pending the
maintainer's ruling.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
… unblocked

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…itive)

The in-process ledger for live and completed agent tasks, encoding the
Q1-Q6 rulings: standalone module (not a BrainHost tenant), ephemeral
records and replay store, a record-level TaskStatus enum carrying
RUNNING while contract.Status stays terminal-only, and method-private
storage so a shared backend can replace the dicts later.

Nothing imports it yet — Step 2 dual-writes from delegate_tool. The
interrupt path resolves the stored agent (weakref or direct) and calls
agent.interrupt() outside the registry lock, the same mechanic as
interrupt_subagent today. The replay store mirrors _TASK_RESULTS
semantics (600s TTL, 1024 cap) ahead of the Step 5 fold-in.

17 unit tests, including two Q3-ruling pins: contract.Status has no
RUNNING, and TaskStatus terminal values mirror contract.Status exactly.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…d the landed Step 1 path

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
… Phase 5 Step 2

Each child registers an AgentTaskRecord (intent="delegate", weakref
agent_ref) right after _register_subagent and completes it in the same
finally block that unregisters the legacy dict entry, mapping the
existing per-child entry to a terminal ExecutionResult: completed →
SUCCEEDED, interrupted/timeout → FAILED + ErrorType.TRANSPORT
(non-retryable, per the doc ruling), failed → PROVIDER_ERROR, escaped
exception → INTERNAL, died-without-entry → complete(None) → FAILED.

Both registry calls are try/except-guarded so a registry bug can never
break a real delegate run. _active_subagents, _active_children,
heartbeat and timeout paths are untouched — the registry is a parallel
ledger until the Step 7 cutover.

8 new tests; delegate suites 172 green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
claw3649 and others added 21 commits June 10, 2026 21:37
…pt and pause — Phase 5 Steps 3-4

Step 3: subagent.interrupt tries registry.interrupt first and falls
back to the legacy interrupt_subagent on a miss; delegation.pause
dual-writes the registry flag alongside the still-authoritative
set_spawn_paused. Both wire shapes unchanged.

Step 4: three additive RPCs backed by the registry — task.status
(snapshot + found, query misses are found:false not errors), task.cancel
(registry.interrupt), task.list (active records only). The bg_* and
preview-restart background paths now register records (intent
"tui-background"/"preview-restart") and complete them with honest
terminal results, closing the visibility gap where a background task
was unqueryable after a client restart. No agent_ref is held for
inline-built agents, so task.cancel reports found:false for them — as
designed in the doc's Step 4.

8 new protocol tests; protocol suite green, server suite green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…l — Phase 5 Step 6a

AgentTaskRecord gains an optional session_id; complete() captures it
under the lock, then snapshots and appends one JSON line to
$HERMES_HOME/spawn-trees/<session_id>/_tasks.jsonl outside the lock —
same single-write append pattern as the legacy _append_spawn_tree_index
(doc risk R4). Persistence is wholly guarded: a write failure can never
break complete(). snapshot() emits session_id only when set, so
task.status/task.list wire shapes for pre-existing records are
unchanged.

5 new tests (tmp-home write, no-session no-op, open-failure resilience,
two-line append, existing 17 untouched).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…lds into the registry — Phase 5 Step 5

_task_submit_delegate validates inputs.tasks, resolves the session like
slash, registers a guarded parent AgentTaskRecord (weakref agent_ref —
task.status sees the batch, task.cancel interrupts into the engine's
interrupted-partial path) and calls the existing delegate_task engine
unchanged. _delegate_aggregate_to_result maps the aggregate per the Q4
ruling: all completed → SUCCEEDED, mixed/interrupted → PARTIAL with the
per-child breakdown verbatim in outputs (Core re-plans only unfinished
children), none → FAILED typed by the Step-2 convention; engine
exceptions are FAILED results, never protocol errors. No dispatch
change needed — task.submit was already in _LONG_HANDLERS, so
intent="slash" ordering is untouched; the R6 synchronous-blocking
constraint is documented on the handler.

Idempotency fold: _task_result_replay/_task_result_store now delegate
to registry.recall/remember (same 600s TTL / 1024 cap), each guarded to
degrade to a cache miss rather than a broken run. Protocol tests for
both this and Step 6b land in the next commit (single contiguous test
hunk).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…h the legacy index — Phase 5 Step 6b

_read_spawn_tree_tasks mirrors the legacy reader's lenient line
handling; _registry_task_entries dedupes by task_id preferring the
legacy TUI-assembled entry (Q5 ruling) and maps registry snapshots onto
the existing list-entry shape. Sessions without _tasks.jsonl produce
byte-identical output (pinned by test).

Carries the protocol tests for both Step 5 (7 tests: delegate happy
path, Q4 PARTIAL pin, engine exception, param errors, no-agent error,
delegate idempotency replay) and Step 6b (4 tests: legacy-only
byte-identical, registry-only, dedupe preference, corrupt-line skip).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
… 7 findings closed

R1 terminal-state guard: a second complete() returns False without
mutation, double persist, or replay overwrite (a late bg failure can no
longer falsify a recorded success). R2 bounded retention: terminal
records evict oldest-first past RECORDS_TERMINAL_CAP=1024; RUNNING
records never evicted. R3 interrupt(task_id, reason=None): forwards the
reason (AIAgent.interrupt accepts an optional message) and swallows
agent exceptions, restoring legacy interrupt_subagent parity — a bare
exception could previously kill the inline stdio dispatcher. R4 replay
timebase back to time.monotonic(), matching the _TASK_RESULTS store the
fold replaced. R5 the idempotency fold stores the rich result wire, not
the record snapshot. R6 complete() builds one snapshot under the lock
and reuses it for replay and persistence; new locked readers
get_snapshot/list_active_snapshots for the RPC layer (torn-read fix).
R7 _tasks.jsonl appends are single unbuffered byte writes so large
rich-result lines cannot interleave between threads. Plus
update_progress(task_id, tool_count, last_tool) for the live delegate
progress relay (G8).

8 new unit tests; one fixture realigned to the R5 shape change (ids now
come from the ExecutionResult, as in production).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…ep 6 wiring — 9 findings

G1 intent=delegate now claims the session busy gate (begin_turn/
end_turn, same 4009 'session busy' as prompt.submit) — a batch and a
user turn can no longer run concurrently on the same AIAgent with
cross-coupled interrupt state. G2 a retry of a still-RUNNING task_id is
rejected (4034) instead of re-executed. G3 the parent agent's interrupt
flag is cleared after the batch (verified NOT auto-reset at
conversation start), so a task.cancel mid-batch cannot abort the user's
next turn. G4 session_id is now wired at all three record sites (bg/
preview, delegate parent, delegate children via parent_agent.session_id)
— Step 6 persistence actually runs in production, proven end-to-end by
new _tasks.jsonl tests. G5 the idempotency-test isolation helper resets
the registry singleton (the old helper poked a dormant dict). G6 the
registry-first subagent.interrupt forwards the legacy 'Interrupted via
TUI' reason. G7 only the spawn-pause rejection maps to DENIED/
retryable; other engine errors are INTERNAL non-retryable. G8 the
TASK_TOOL_STARTED relay mirrors tool_count/last_tool into the registry
so task.status shows live progress. G9 task.status/task.list read
locked snapshots.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…outcome, and the deferred gaps

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…ry ledger paths

Closes deferred review gap 3: a registry-only spawn_tree.list entry
points its path at the session's _tasks.jsonl, which the legacy load
path could only reject. The handler now detects the ledger basename,
parses it leniently, selects by the optional task_id param (else the
last line), and synthesizes a payload in the exact spawn_tree.save
shape — status vocabulary and field mapping derived from the actual TUI
consumer (spawnHistoryStore/agentsOverlay): succeeded→completed,
partial→interrupted, result text into the rendered summary, ms-epoch
startedAt. Legacy snapshot paths flow through the pre-existing code
byte-identically (pinned by test). 5 new protocol tests.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
/tasks lists active records via task.list (short id, intent, status,
age, tool progress, truncated goal); /tasks status <id> renders a full
snapshot incl. terminal result; /tasks cancel <id> reports the registry
verdict honestly (bg_*/preview_* runs phrase found:false as
'don't accept cancel yet', not as an error).

Behavior note: 'tasks' was previously an alias of /agents (spawn-tree
overlay); it now resolves to this registry board — /agents itself is
untouched and keeps the overlay. Additive wire types mirror
task_registry.snapshot()/result_to_wire_rich. 10 vitest tests follow
the existing slashParity pattern; type-check and eslint clean, full
ui-tui suite at its pre-existing baseline.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…servability branch, Step 7 prep

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…sh refactors, keep honest-status surfacing

Upstream restructured executeSlashCommand into a slash-action table with
a generic runExec path (and then made slash commands registry-driven in
#42351); /model-with-args now rides runExec, so our result.error
honest-status check lives there — covering every exec-style command
instead of just the typed /model path. The refreshCurrentModel guard
keeps BOTH protections: upstream's live-session-owns-the-footer guard
and our in-flight staleness token. The model-controls test file unions
upstream's two refreshCurrentModel tests with our four selectModel
rollback/race tests (6/6 green under --environment jsdom).

Desktop src/ suite shows the byte-identical failure set to pure
origin/main (6 pre-existing); python gateway suites green.

Note: this replaces an earlier broken push of the same resolution — a
mid-merge stash for a baseline measurement silently dropped MERGE_HEAD,
so 46a668be2 landed as a single-parent commit with partial upstream
content. Reset and redone as a true merge; baselines are measured in
detached worktrees from now on.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…e up the stack

Single conflict: config.set reasoning_effort gained
_persist_live_session_runtime + a session.info emit upstream while this
branch had converted the same lines to SessionState attribute access —
resolution keeps both (attribute style per Phase 3 Step 3).

Suites: 489 green (single known browser-hint env failure).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…inHost value

Profiling AIAgent construction showed tool definitions are already
memoized process-wide (model_tools keyed on registry._generation, bumped
on MCP changes) — the dominant repeat cost is the context-length
resolution: the live probe re-runs for non-Ollama endpoints on every
construction (measured 335-788 ms). build_agent now installs a memo
(dict owned by the BrainHost singleton) into agent.model_metadata;
flag off = default-None passthrough, byte-identical, zero allocation.

Safety rules: the key captures every resolution input (model, base_url,
normalized provider, config override, api_key identity, custom_providers
fingerprint); lmstudio and nous are never cached (transient /
portal-authoritative — the same exclusions the disk cache encodes);
TTL 3600 s matches the catalog horizon; probe-down fallbacks get 60 s so
an outage cannot freeze an under-reported window; cap 64.

Measured: repeat construction 423 ms -> 110 ms (~3.8x); the resolution
itself 335-788 ms -> 0.02 ms. 11 new tests (hit/miss per key input,
exclusions, TTLs, explicit + MCP-generation invalidation, cap eviction,
3-way full-construction parity); 310 green incl. model_metadata suites.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…op board, measured BrainHost cache

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…eview system

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…ent wrapper

Three CLI tests stubbed construction by patching cli.AIAgent — a pure
pass-through wrapper the BrainHost migration no longer routes through —
so the real AIAgent ran and each test failed on its environment (CI
caught this; our targeted macOS runs never covered these files).
Investigated each as a potential real regression first: the MCP
wait-before-build ordering, the background-thread callback registration
order, and the single resolve_runtime_provider call per init are all
unchanged in production (the extra counted resolutions came from real
AIAgent internals that the old patch merely prevented from running).
Test-only fix: patch agent.brain_host_gate.build_agent — the seam
production actually uses — with every behavioral assertion unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
… idle reaper

test_ws_disconnect_preserves_and_repoints_reconnectable_session seeds
bare session dicts with no created_at/last_active. The module-level
idle reaper reads missing timestamps as 0.0 — idle since the epoch — so
the moment handle_ws's finally closes the transport, the session is
evictable, and a reaper tick landing in the close-to-assert window pops
it: KeyError 'plain'. The 300s tick makes this a once-in-many-runs
flake that only bites long-lived pytest processes (CI's per-file
subprocesses exit before the first tick), which is why it resisted
plain-loop reproduction.

A probe driving the production scan body at a fast tick reproduced the
exact failure 199/200 before the fix and 0/200 after. Production is
immune: every real creation site stamps both timestamps.

Test-only: a _fresh_session helper stamps time.time() on both seeded
sessions.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…tyle repoint works

After Phase 3 Step 3 the WS-disconnect repoint path sets
session.transport = _detached_ws_transport (attribute style); on a
plain-dict seed that raises AttributeError, silently skipping the
repoint, so test_ws_disconnect_preserves_and_repoints_reconnectable_session
saw the session keep its live WSTransport (CI caught this — our targeted
runs never ran this file's slice). _fresh_session now returns a
SessionState, matching every real creation site. Folds the two reasons
the seed must be a SessionState (timestamps + attribute access) into one
docstring.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@alt-glitch alt-glitch added type/perf Performance improvement or optimization P3 Low — cosmetic, nice to have comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint labels Jun 11, 2026
@claw3649

Copy link
Copy Markdown
Author

Carrier re-run triage update:

  • Fork carrier run 27347261828 attempt 3 failed in test (4) on the voice source-scan family, not on the vanishing-file infra race.
  • I checked current origin/main in a fresh worktree at a09343cc9 with PYTHONPATH pointing at that worktree and verified imports resolved to /private/tmp/hermes-voice-command-source-scan/..., not the live checkout.
  • On current origin/main, the targeted voice checks now pass:
    • TestAutoTtsTempFileCleanup::test_source_has_finally_remove
    • TestSendVoiceReplyCleanup::test_cleanup_in_finally
    • TestSendVoiceReplyFilename::test_filename_uses_uuid
  • Full file validation also passes locally on that worktree: tests/gateway/test_voice_command.py163 passed, 21 skipped.

So I am not opening a voice source-scan goodwill PR: there is no remaining diff to make against current origin/main; the carrier branch is stale relative to upstream main for this failure family and should pick it up on the next appropriate rebase.

The separate carrier attribution noise is addressed by #44290 (fix(release): map carrier contributor emails). Keeping this PR draft and not treating the carrier voice failure as a schema-cache regression.

@claw3649 claw3649 closed this Jun 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P3 Low — cosmetic, nice to have type/perf Performance improvement or optimization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants