Skip to content

fix(contexts): durably persist writeable-context writeback (bucket + summary/updated_at) - #2324

Merged
federicodeponte merged 1 commit into
mainfrom
fix/contexts-writeback-persist
Jul 22, 2026
Merged

fix(contexts): durably persist writeable-context writeback (bucket + summary/updated_at)#2324
federicodeponte merged 1 commit into
mainfrom
fix/contexts-writeback-persist

Conversation

@federicodeponte

Copy link
Copy Markdown
Member

Problem

When a worker run writes files into a writeable local context, the writeback overlays the run's edits onto local disk (context_dir(name)) but never refreshes the pack's cached summary. Operator listings (GET /contexts/{name}/files and the Library rows) read file_count / size / updated_at from metadata[name]["summary"] via _context_summary's fast path, so a run's edits leave those fields stale until the next explicit write.

The durable object-storage upload half of this (files lost on container recycle) is already handled on current main by the sync_refreshed_context_pack refresh hook (#2320). The remaining gap was the local summary / updated_at refresh, so the run's writeback stayed invisible to the operator listing.

Fix

  • Add one shared best-effort helper refresh_context_summary_after_writeback(name, *, log_fn=None) in contexts.py.
  • Call it from both drivers' _persist_writeable_contexts (e2b_driver, agent_driver), once per successfully-persisted writeable context, inside the active use_context_scope(...) block, right after the successful durable sync.
  • Mirrors the refresh-then-sync ordering the system-asset refresh path (services/system_assets.py) already uses.
  • The helper never raises: a summary-refresh failure is logged but must not fail the run or undo the already-completed durable persistence.
  • Deletion semantics unchanged.

Tests

  • apps/api/tests/test_agent_driver_contexts.py: writeback refreshes summary + triggers durable sync (mocked); an end-to-end assertion that the cached summary (file_count, updated_at) is actually written; and a read-only context triggers neither.
  • tests/test_e2b_artifact_collection.py: e2b writeback refreshes the cached summary end-to-end.
python3 -m pytest tests/test_e2b_artifact_collection.py apps/api/tests/test_agent_driver_contexts.py -q
# 54 passed

Context-suite regression: test_worker_context_attach.py test_contexts_sensitive.py test_1433_context_mount_selection.py -> 47 passed.

…summary/updated_at)

Writeable-context writeback overlaid a run's edits onto local disk and
triggered the durable object-storage sync (sync_refreshed_context_pack
hook), but never refreshed the pack's cached summary. Operator listings
(GET /contexts/{name}/files and the Library rows) read file_count / size
/ updated_at from metadata[name]["summary"] via _context_summary's fast
path, so a run's edits left those fields stale until the next explicit
write.

Add a shared best-effort helper refresh_context_summary_after_writeback()
in contexts.py and call it from both drivers' _persist_writeable_contexts
(e2b + agent), inside the active use_context_scope block, right after a
successful sync. Mirrors the refresh-then-sync ordering the system-asset
refresh path already uses. The helper never raises: a summary-refresh
failure is logged but never fails the run or undoes durable persistence.

Tests: assert the writeback refreshes the cached summary + updated_at for
a writeable pack (e2b + agent drivers, mocked and end-to-end) and that a
read-only context triggers neither refresh nor durable sync.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@federicodeponte
federicodeponte merged commit 378fcd2 into main Jul 22, 2026
6 of 7 checks passed
@federicodeponte
federicodeponte deleted the fix/contexts-writeback-persist branch July 22, 2026 13:45
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.

1 participant