Skip to content

fix(agents): merge a resources patch element-wise onto the freshly-read spec - #11924

Closed
rnoack1 wants to merge 1 commit into
kirodotdev:mainfrom
rnoack1:fix/merge-agent-resources-delta-element-wise
Closed

rnoack1 wants to merge 1 commit into
kirodotdev:mainfrom
rnoack1:fix/merge-agent-resources-delta-element-wise

Conversation

@rnoack1

@rnoack1 rnoack1 commented Sep 19, 2026

Copy link
Copy Markdown
Contributor

One unit of a split of #7105, which was 6.9x over the 1500 added-line cap. This unit stands
alone: it reads and writes plain dicts and needs nothing from the other units. 226 added lines.

Problem / Motivation

api_agent_detail's PATCH path builds its result document from a snapshot taken before the
spec lock is held, then assigns that document's resources list onto the freshly-read spec
wholesale. Any resources URI a concurrent writer added between the snapshot and the lock is
silently dropped — the losing write looks like it succeeded, and the URI is simply gone.

Two smaller defects sit in the same assignment:

  • a patch that names no resources change still rewrote the key, so a malformed value the
    request never looked at came back normalised;
  • because iterating a string yields characters and each one is itself a str, an unguarded
    comprehension over a malformed resources value would rewrite it as a per-character list.

Why it matters

This is a lost update with no error surface: the request that loses returns success, and nothing
reports which URI vanished. Two sessions editing one agent is the ordinary case, not an exotic
one, and the window is exactly the read-modify-write gap the lock exists to close.

What changed (motivation → approach → change)

The write-back cannot assign a value derived from a stale snapshot, so it must move only what
the request itself named. _merge_resources_delta computes the patch's delta — the URIs it
removed and the ones it added — and applies just that to the freshly-read document:

  • Early return when the patch named no resource change. It may not rewrite the key at all,
    so a malformed value it never read survives untouched rather than normalised.
  • Only the strings this patch named may leave. An entry of any other shape is not something
    this merge has an opinion about, so it is carried through unread rather than dropped.
  • An empty result drops the key rather than writing [], matching the mapping writer,
    because an empty list suppresses the shipped steering defaults.
  • The copy and delete loops in _locked_overwrite now skip resources, so the merge is the
    only thing that writes it.

Tests

Four tests in test/test_agent_detail_model_managed.py, each pinning one behaviour:

test locks in
…keeps_a_uri_a_concurrent_writer_added the lost update itself: a concurrent writer's URI survives a skills patch
…names_no_resource_change_leaves_a_malformed_value_alone the early return
…over_a_malformed_fresh_value_does_not_split_it the non-list guard
…preserves_resource_entries_it_cannot_read non-string entries are carried, not dropped

Mutation controls, all four killed, one test each: assigning after whole; dropping the
early return; dropping the non-list guard; rebuilding the kept list from the string view. Each
mutation failed exactly one test, and a different one — so no test here is vacuous and none is
redundant. The tree was restored byte-identical afterwards and re-run green.

Gates on the touched files: flake8, isort --check-only, black --check, mypy all clean.
Regression sweep over every test file importing this handler (57 files, traversal
positive-controlled): 3179 passed, 0 failed.

Manual verification

N/A — the lost update is reproduced directly in the first test by simulating the concurrent
writer, so unit coverage reaches the actual failure mode.

Related Issues

Part of the split of #7105.

Pattern harvest

Rule candidate: review-prompt

Pattern: a write-back that assigns a value derived from a pre-lock snapshot discards whatever a
concurrent writer changed in the same key — under a lock, write back only the keys the request
itself named, as a delta, never the whole re-derived document.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable) — N/A, no doc surface in this unit
  • No secrets, credentials, or internal references in the diff

…ad spec

The document a PATCH writes back is built from a snapshot taken before the spec lock, so assigning its `resources` list whole dropped any URI a concurrent writer had added in between; only the URIs this patch itself named may move.
A patch that names no resource change now leaves a malformed `resources` value untouched rather than normalising a key it never read.
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: action required A blocking check or review needs attention labels Sep 19, 2026
@dwu96

dwu96 commented Sep 19, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! This PR is currently in draft status. Workflow runs won't be auto-approved until it's marked as ready for review.

When you're ready, click "Ready for review" and the workflows will be approved on the next cycle automatically.

@rnoack1
rnoack1 marked this pull request as ready for review September 19, 2026 01:15
@rnoack1
rnoack1 requested review from a team and krishdhasmana September 19, 2026 01:15
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Sep 19, 2026
@github-actions

Copy link
Copy Markdown
Contributor

First Principles Review (Fable 5, fork) — ⚠️ could not complete

The first-principles review did not produce a verdict for a74b3906564ffc8dcec4ff288553b1a5228010b3 (the review step failed). See the Fork First Principles Review job logs. Advisory — does not block merge.

@github-actions

Copy link
Copy Markdown
Contributor

Opus 4.8 Review (fork) — ⚠️ review incomplete

Reviewed a74b3906564ffc8dcec4ff288553b1a5228010b3 via the fork AI-review pipeline; updated in place on each push.

No completed Opus verdict for this commit; see the Fork Opus 4.8 Review job logs.

@github-actions

Copy link
Copy Markdown
Contributor

Design Review (Fable 5, fork) — ⚠️ could not complete

The design review did not produce a verdict for a74b3906564ffc8dcec4ff288553b1a5228010b3 (the review step failed). See the Fork Design Review job logs. Advisory — does not block merge.

@github-actions

Copy link
Copy Markdown
Contributor

GPT 5.6 Review (fork) — ⚠️ review incomplete

Reviewed a74b3906564ffc8dcec4ff288553b1a5228010b3 via the fork AI-review pipeline; updated in place on each push.

No completed GPT verdict for this commit; see the Fork GPT 5.6 Review job logs.

Captured review output -- no verdict stamp for this commit, so this is NOT a verdict

Incomplete review: pass(es) 1 2 did not complete.

The pass-2 verdict was not published because both calls are required. Re-run the workflow for a complete review.

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Sep 19, 2026
@iamwhatever iamwhatever added the drive-to-green PR claimed by drive-to-green pipeline label Sep 25, 2026
@iamwhatever iamwhatever self-assigned this Sep 25, 2026
@iamwhatever

Copy link
Copy Markdown
Contributor

Thank you for this fix, @rnoack1 — the element-wise resources-patch merge is a real lost-update fix and the reasoning is sound.

Since the head branch is on a fork, the AI review lanes could not run to completion here, so I opened an in-repo replacement that carries your change (and credits you via Co-authored-by): #13624. It is now fully green (all review lanes pass, no blocking findings) and awaiting maintainer review/merge.

Closing this one as superseded by #13624. Your branch is untouched. Thanks again!

@github-actions github-actions Bot removed the readiness: action required A blocking check or review needs attention label Sep 25, 2026
@dwu96 dwu96 removed the drive-to-green PR claimed by drive-to-green pipeline label Sep 25, 2026
@rnoack1
rnoack1 deleted the fix/merge-agent-resources-delta-element-wise branch November 9, 2026 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants