hermes-agent/agent
sasquatch9818 6def7ce1df fix(models): write context-length cache atomically
save_context_length() and _invalidate_cached_context_length() did an
unguarded read-modify-write into $HERMES_HOME/context_length_cache.yaml.
The plain `open(path, "w")` truncates the file before the dump runs. If
the process is killed mid-dump, the file is left empty or partial. The
next _load_context_cache() swallows the YAML error and returns {} —
silently wiping every persisted context length. A concurrent process
reading between truncate and dump-complete also sees a torn file.

After the cache is lost, every model re-probes the network, and when a
probe fails it falls back to the generic 256K default — so a user on a
1M-window model ends up with a wrong, short context window.

Hermes routinely runs several processes against one shared $HERMES_HOME
(a cron agent plus an interactive session, multiple gateway sessions),
so this is hit in normal use.

Switch both writers to the existing utils.atomic_yaml_write helper
(temp file + fsync + os.replace, symlink- and mode-preserving). The real
file is only ever swapped from a fully written temp file, so an
interrupted write leaves the previous cache intact and readers never see
a partial file. Matches the atomic-write pattern already used for
auth.json, config.yaml, and other persisted state.

Makes the persistent model context-length cache write crash-safe. The
old non-atomic write could truncate or wipe the entire cache on an
interrupted or concurrent write, which then forces models onto the wrong
fallback context window. The fix routes both cache writers through the
repo's atomic temp-file + os.replace helper.

N/A

- [x] 🐛 Bug fix (non-breaking change that fixes an issue)
- [ ] ✨ New feature (non-breaking change that adds functionality)
- [ ] 🔒 Security fix
- [ ] 📝 Documentation update
- [ ] ✅ Tests (adding or improving test coverage)
- [ ] ♻️ Refactor (no behavior change)
- [ ] 🎯 New skill (bundled or hub)

- `agent/model_metadata.py`: `save_context_length()` and
  `_invalidate_cached_context_length()` now write via
  `utils.atomic_yaml_write` instead of a truncating `open(path, "w")`.
  Added the `atomic_yaml_write` import.
- `tests/agent/test_model_metadata.py`: added
  `test_write_failure_leaves_existing_cache_intact` — simulates a crash
  during the atomic swap and asserts the existing cache survives
  byte-for-byte with no stray temp file.

1. `pytest tests/agent/test_model_metadata.py -q` — 98 pass, including
   the new crash-safety test.
2. The new test seeds a valid cache, forces the swap step to raise, and
   confirms the file is not truncated and no `.cache_*.tmp` is left.
3. `ruff check agent/model_metadata.py` passes.

- [x] I've read the Contributing Guide
- [x] My commit messages follow Conventional Commits (`fix(scope):`, etc.)
- [x] I searched for existing PRs to make sure this isn't a duplicate
- [x] My PR contains **only** changes related to this fix
- [x] I've run the affected tests (`pytest tests/agent/test_model_metadata.py -q`) and they pass
- [x] I've added tests for my changes
- [x] I've tested on my platform: macOS 15 (Darwin 25.5)

- [x] I've updated relevant documentation (README, `docs/`, docstrings) — or N/A
- [x] I've updated `cli-config.yaml.example` if I added/changed config keys — or N/A
- [x] I've updated `CONTRIBUTING.md` or `AGENTS.md` if I changed architecture or workflows — or N/A
- [x] I've considered cross-platform impact (Windows, macOS) — the helper uses os.replace, which is atomic on both
- [x] I've updated tool descriptions/schemas if I changed tool behavior — or N/A
2026-08-13 11:08:26 -07:00
..
lsp fix(runtime): resolve Hermes-managed Node and uv before bare PATH 2026-08-01 21:17:51 -04:00
monitoring fix(otlp): span exporter now inherits configured resource_attributes 2026-08-08 18:11:02 -07:00
pet
…
proxy_sources
…
secret_sources fix(plugins): preserve force-path platform sweep + scoped plugin-source listing after rebase 2026-08-12 19:13:32 -07:00
transports fix(xai): preserve Grok 4.6 wire capabilities 2026-08-13 11:07:37 -07:00
verify Integrate verify subsystem with the existing verification stack 2026-08-07 10:11:05 -07:00
__init__.py
…
account_usage.py
…
agent_init.py fix: security + efficiency review fixes for salvaged PR #74379 2026-08-13 23:15:25 +05:30
agent_runtime_helpers.py refactor: hoist preflight clear into restart handler, single-source qwen predicate 2026-08-13 15:24:47 +05:30
anthropic_adapter.py fix: send Hermes Agent attribution headers to OpenCode Zen and Go 2026-08-13 02:03:40 -07:00
async_utils.py
…
aux_accounting.py
…
auxiliary_client.py fix(auxiliary): keep /anthropic base_url for anthropic_messages custom endpoints 2026-08-13 10:32:51 -07:00
azure_identity_adapter.py fix(secrets): scope-aware Azure credential presence reads in identity diagnostics 2026-08-02 10:02:33 -07:00
backend_identity.py refactor(fallback): single owner for backend identity and failure-scoped skips 2026-07-26 23:41:54 -07:00
background_review.py simplify: match delegate_tool.py hasattr pattern, drop change-detector test 2026-08-09 16:13:52 +05:30
battery.py
…
bedrock_adapter.py
…
billing_links.py
…
billing_usage.py chore: remove unused imports and dead locals (ruff F401/F841 sweep) 2026-07-29 11:53:39 -07:00
billing_view.py chore: remove unused imports and dead locals (ruff F401/F841 sweep) 2026-07-29 11:53:39 -07:00
bounded_response.py
…
browser_provider.py feat(browser): integrate Browser Use CLI 3.0 2026-08-10 10:45:44 -07:00
browser_registry.py fix(plugins): isolate ownership by profile 2026-08-12 19:13:32 -07:00
chat_completion_helpers.py feat(plugins): add streaming output observer hooks 2026-08-12 18:38:25 -07:00
codex_responses_adapter.py fix(compression): prune pre-checkpoint history on native compaction replay 2026-08-13 01:51:24 -07:00
codex_runtime.py fix: widen APIConnectionError handling to finalization drain loop 2026-08-12 12:42:39 +05:30
coding_context.py perf: use load_config_readonly() at read-only call sites in agent/ 2026-07-29 15:28:15 -07:00
context_breakdown.py
…
context_compressor.py fix: double-paren bug in dropped-tools prefix + add empty-response nudge sibling 2026-08-09 16:20:48 +05:30
context_engine.py Revert "fix: rewire DCP context engine to current main architecture" 2026-08-08 23:09:45 +05:30
context_references.py feat(plugins): allow plugins to register custom @-prefix context references 2026-08-12 18:41:59 -07:00
conversation_compression.py feat(relay): session-span segmentation for continuous sessions 2026-08-13 10:45:15 -07:00
conversation_loop.py fix: restore stale-base revert hunks in conversation_loop.py and moa_loop.py 2026-08-13 23:10:16 +05:30
copilot_acp_client.py fix(file-safety): approval-gate ~/.ssh/config writes instead of hard-denying (#84663) 2026-08-12 11:04:45 -07:00
credential_persistence.py
…
credential_pool.py fix(agent): rebind pool entry id after env credential refresh 2026-08-08 19:17:02 -07:00
credential_sources.py fix(auth): read .env as utf-8-sig in the dotenv-vs-shell detector 2026-08-08 12:32:23 -07:00
credits_tracker.py fix(credits): reintroduce grant_spent, gated on an in-session crossing (#73634) 2026-07-28 21:36:23 +00:00
curator.py perf(curator): trim dead tool-schema from the LLM review fork 2026-08-03 18:21:17 +05:30
curator_backup.py refactor: fold simplify findings — 6th copy in update_cmd, drop dead wrapper + speculative kwarg, behavior-contract tests 2026-08-08 15:10:35 +05:30
delegation_context.py fix(cron): don't let a cron job inherit a kanban worker's dispatcher identity 2026-08-06 03:16:26 +05:30
display.py feat(delegation): live orchestration of running subagents via delegate_task action param 2026-08-13 09:34:36 -07:00
error_classifier.py feat(plugins): rename hook to transform_api_error_classification per #64231 verdict 2026-08-13 09:36:02 -07:00
errors.py
…
estop.py fix: post-merge audit follow-ups for #81138/#81139/#81141/#81148 2026-08-08 05:21:09 -07:00
file_safety.py fix(file-safety): approval-gate ~/.ssh/config writes instead of hard-denying (#84663) 2026-08-12 11:04:45 -07:00
gemini_native_adapter.py fix(gemini): interpose placeholder model turn between tool result and user text 2026-08-05 17:21:01 -07:00
gemini_schema.py
…
i18n.py perf: use load_config_readonly() at read-only call sites in agent/ 2026-07-29 15:28:15 -07:00
image_gen_provider.py feat(media): opt-in upscale pass for image_generate and video_generate across FAL and Krea 2026-08-08 14:49:28 -07:00
image_gen_registry.py fix(plugins): isolate ownership by profile 2026-08-12 19:13:32 -07:00
image_routing.py
…
insights.py refactor(insights): drop consumer-less get_skill_breakdown alias (simplify-pass) 2026-08-03 18:45:43 +05:30
interrupt_compat.py fix(compression): let explicit interrupts cancel safely 2026-08-02 22:15:20 +05:30
iteration_budget.py
…
jiter_preload.py
…
kanban_stop.py
…
learn_prompt.py fix(learn): process large sources incrementally 2026-08-08 05:09:43 -07:00
learning_graph.py
…
learning_graph_render.py chore: remove unused imports and dead locals (ruff F401/F841 sweep) 2026-07-29 11:53:39 -07:00
learning_mutations.py
…
lmstudio_reasoning.py
…
manual_compression_feedback.py
…
markdown_tables.py
…
memory_manager.py fix: security + efficiency review fixes for salvaged PR #74379 2026-08-13 23:15:25 +05:30
memory_provider.py fix: security + efficiency review fixes for salvaged PR #74379 2026-08-13 23:15:25 +05:30
message_content.py
…
message_sanitization.py fix(agent): preserve destroyed tool-call argument bytes in the WARNING log 2026-08-07 16:57:11 +05:30
moa_loop.py fix: restore stale-base revert hunks in conversation_loop.py and moa_loop.py 2026-08-13 23:10:16 +05:30
moa_trace.py feat(langfuse): widen tracing to errors, sessions, subagents, and MoA fan-out 2026-08-13 23:10:16 +05:30
model_metadata.py fix(models): write context-length cache atomically 2026-08-13 11:08:26 -07:00
models_dev.py fix(models_dev): map meta-ai provider to models.dev 'meta' id (salvage #81418) (#84679) 2026-08-12 14:42:00 -04:00
moonshot_schema.py
…
native_compaction.py fix(agent): keep native compaction checkpoints out of the thinking-only drop 2026-08-13 03:04:45 -07:00
nous_rate_guard.py
…
onboarding.py
…
oneshot.py
…
outbound_webhooks.py fix(hooks): flush outbound queue at interpreter exit 2026-08-02 15:01:11 -07:00
plugin_llm.py fix(plugins): complete task-routed LLM integration 2026-08-12 16:25:20 -07:00
plugin_stream_hooks.py feat(plugins): add streaming output observer hooks 2026-08-12 18:38:25 -07:00
portal_tags.py
…
process_bootstrap.py
…
prompt_builder.py feat(desktop): setup_mcp tool — inline MCP consent card over the clarify-style blocking bridge 2026-08-13 01:06:51 -05:00
prompt_cache_boundary.py refactor(cache): share the boundary-declaration helper and simplify the registry 2026-08-09 15:25:12 +05:30
prompt_caching.py refactor: hoist preflight clear into restart handler, single-source qwen predicate 2026-08-13 15:24:47 +05:30
rate_limit_tracker.py
…
reactions.py
…
reasoning_summaries.py fix(reasoning): keep gpt-5.x summary parts as separate blocks on the chat wire 2026-08-06 22:02:37 -05:00
reasoning_timeouts.py add grok 4.6 (#84661) 2026-08-12 13:37:04 -04:00
redact.py fix: reuse redact_sensitive_text, fix leaky abstraction, fix test data 2026-08-13 23:10:16 +05:30
relay_llm.py fix(relay): bypass managed execution for nested calls inside managed callbacks 2026-08-08 23:46:17 -07:00
relay_runtime.py fix(relay): defer rotating-compaction session close while a turn is live 2026-08-13 10:45:15 -07:00
relay_tools.py fix(relay): bypass managed execution for nested calls inside managed callbacks 2026-08-08 23:46:17 -07:00
replay_cleanup.py
…
retry_utils.py refactor: single shared Retry-After parser 2026-07-29 10:13:50 -07:00
runtime_cwd.py
…
secret_scope.py fix(secrets): allowlist API_SERVER listener settings as global deployment env 2026-08-02 09:59:52 -07:00
session_activity.py refactor(agent): pin session activity heartbeat cadence + harden best-effort write 2026-08-02 16:16:36 -07:00
shell_hooks.py fix(plugins): synthetic hook fixture, shell-hook exclusion, docs per review 2026-08-13 09:36:02 -07:00
skill_bundles.py feat(observability): add Relay skill metrics 2026-07-29 12:20:34 -07:00
skill_commands.py refactor(cache): share the boundary-declaration helper and simplify the registry 2026-08-09 15:25:12 +05:30
skill_preprocessing.py perf: use load_config_readonly() at read-only call sites in agent/ 2026-07-29 15:28:15 -07:00
skill_utils.py fix(cron): don't let a cron job inherit a kanban worker's dispatcher identity 2026-08-06 03:16:26 +05:30
ssl_guard.py fix(ssl_guard): tolerate truststore SSLContext.get_ca_certs() NotImplementedError on Windows 2026-08-08 15:10:24 +05:30
ssl_verify.py
…
stream_diag.py
…
stream_single_writer.py
…
subagent_lifecycle.py fix(compression): let explicit interrupts cancel safely 2026-08-02 22:15:20 +05:30
subdirectory_hints.py fix(context): dedupe subdirectory hints by content digest and skip backup/vendor dirs 2026-08-03 20:43:34 +05:30
subscription_view.py
…
system_prompt.py feat(plugins): add cache-safe system prompt sections 2026-08-12 16:34:58 -07:00
think_scrubber.py
…
thinking_timeout_guidance.py
…
thread_scoped_output.py fix: preserve session history when a turn crashes 2026-08-06 23:08:23 -05:00
title_generator.py refactor(title): decide on the stored title and the real turns behind it 2026-08-09 04:33:58 -05:00
tool_dispatch_helpers.py feat(image): parallelize image_generate batches 2026-08-03 22:53:32 +05:30
tool_executor.py feat(delegation): live orchestration of running subagents via delegate_task action param 2026-08-13 09:34:36 -07:00
tool_guardrails.py feat(delegation): live orchestration of running subagents via delegate_task action param 2026-08-13 09:34:36 -07:00
tool_result_classification.py
…
trace_upload.py
…
trajectory.py
…
transcription_provider.py feat(plugins): add pre_transcription hook and STT prompt threading 2026-08-12 19:01:30 -07:00
transcription_registry.py fix(plugins): isolate ownership by profile 2026-08-12 19:13:32 -07:00
tts_provider.py
…
tts_registry.py fix(plugins): isolate ownership by profile 2026-08-12 19:13:32 -07:00
turn_context.py feat(hindsight): memory provider improvements — recall_sync, retain_source, setup templates, memory indicators, error hints 2026-08-13 23:15:25 +05:30
turn_finalizer.py fix(agent,gateway): class-level lone-surrogate chokepoints (#80366 #55143 #55309 #50959 #19819) 2026-08-08 12:31:19 -07:00
turn_retry_state.py feat(compression): native OpenAI Responses server-side compaction for gpt-5.6 2026-08-08 11:24:45 -07:00
turn_summary.py
…
usage_pricing.py fix: close provider-anthropic MiniMax proxy bypass + rework cache observability 2026-08-12 13:58:26 +05:30
verification_evidence.py Integrate verify subsystem with the existing verification stack 2026-08-07 10:11:05 -07:00
verification_stop.py fix: make verify_on_stop opt-in everywhere (default False, not auto) (#84383) 2026-08-12 01:15:25 -07:00
verify_hooks.py
…
vertex_adapter.py
…
video_gen_provider.py feat(media): opt-in upscale pass for image_generate and video_generate across FAL and Krea 2026-08-08 14:49:28 -07:00
video_gen_registry.py fix(plugins): isolate ownership by profile 2026-08-12 19:13:32 -07:00
web_search_provider.py
…
web_search_registry.py fix(plugins): isolate ownership by profile 2026-08-12 19:13:32 -07:00