Skip to content

fix(crew): a crewmate's worker follows its Profile and record - #23567

Merged
iamwhatever merged 2 commits into
mainfrom
fix/crewmate-worker-approval-race
Oct 24, 2026
Merged

iamwhatever merged 2 commits into
mainfrom
fix/crewmate-worker-approval-race

Conversation

@buluoray

@buluoray buluoray commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: A crewmate's worker runs under the Permissions and backend its crewmate's Profile and record say, even when the Profile is saved while the worker is being opened or the worker's slot is made again on an ordinary template.

Two follow-ups from #23010:

  • A worker being opened while the owner switches the crewmate's Permissions (for example Trust to Normal) starts with the old mode. create_session seeded the worker from a config it read before its suspensions, and the Profile save's own pass over live workers (crewmate_approval.apply_to_live_sessions) skipped the child because it had no execution record yet. The worker then kept auto-approving after the owner revoked trust.
  • A worker's key stays in the in-memory worker set (_CREWMATE_WORKER_SLOTS) after its slot is made again on an ordinary template, so that template runs on the crewmate's member backend, and its model is judged against that backend.

Why it matters

Permissions are the owner's control over what a crewmate's worker may run unasked. A revocation that does not reach a worker opened in the same moment leaves a trusted session the owner believes is untrusted. The backend leak sends an ordinary chat to a harness the owner did not choose for it.

Not a goal

What changed (motivation → approach → change)

  • Stale seed → the worker is seeded from config read before its record existed → it now starts untrusted and is seeded once its record is on disk, from the Profile read at that moment (crewmate_approval.seed_new_worker, called from session_create). A save after that point reaches the worker through apply_to_live_sessions, which can now see its record; there is no await between the fresh read and the seed. A worker whose crewmate cannot be named keeps the old inherit-from-caller path.
  • Worker set only grew → mate_worker.note_worker_session now drops the key when the record is not a worker (members.forget_crewmate_worker_session). It runs on every read before an allocation and, new, at an explicit selection's commit (slot_guards._record_explicit_agent_selection), so the key leaves before the next turn, the eager start or the model picker read it.
  • Specs: crew-mode.md (Permissions paragraph, worker backend bullet) and session-control.md (approval posture).

Note: POST /api/chat/slots/{slot}/agent on a live worker is refused with member_session_pinned, and a provider-side switch on a worker is vetoed. The reachable path is a worker's slot made again under the same key on an ordinary template (POST /api/chat/slots), which is what the route test drives.

Backwards compatibility

Compatible: a worker still ends up with its crewmate's Profile mode, and an ordinary session's backend selection is unchanged; only a stale mode and a stale worker key are corrected.

Removes nothing: no field, route, validator or record is removed; one in-memory key is dropped when the session's own record no longer names the worker.

Tests

  • test_crewmate_worker_approval.py::test_a_profile_change_during_the_workers_creation_reaches_it (both directions, Trust→Normal and Normal→Trust): the Profile is saved after the create read its config and before the worker's record lands.
  • test_crewmate_worker_approval.py::test_a_profile_save_applied_during_the_seeds_read_wins (both directions): the seed's config read is stale, the save and its live apply land before the seed; the once-per-slot _member_approval_seeded flag keeps the saved mode.
  • test_mate_worker_dispatch.py::test_a_worker_switched_to_another_template_leaves_its_crewmates_backend: a record moved off the worker template takes the key off the member backend at the next read.
  • test_mate_worker_dispatch.py::test_a_worker_slot_made_again_on_an_ordinary_template_leaves_at_the_commit: the route commit drops the key before any turn.

Proof: PROVEN test/test_crewmate_worker_approval.py::test_a_profile_change_during_the_workers_creation_reaches_it[trust-normal-False], test/test_crewmate_worker_approval.py::test_a_profile_change_during_the_workers_creation_reaches_it[normal-trust-True], test/test_mate_worker_dispatch.py::test_a_worker_switched_to_another_template_leaves_its_crewmates_backend, test/test_mate_worker_dispatch.py::test_a_worker_slot_made_again_on_an_ordinary_template_leaves_at_the_commit (head ff65c63)

Local gate: the related backend set ran 29,326 tests; 16 failed, every one either fails the same way on the base commit or passes when rerun alone (load timeouts on a shared host).

Manual verification

N/A — unit coverage drives the real create_session and the real /api/chat/slots route; no live gateway run.

Related Issues

Refs #23010

Sibling sweep

Swept: git grep -n "note_worker_session\|seed_for_turn(" -- src/
Sibling: src/kiro_crew/session_agent_selection.py -- not affected -- already calls note_worker_session on every read before allocation
Sibling: src/kiro_crew/slack/gateway_runtime/cron_fire.py -- not affected -- binds a worker record and notes it, never a non-worker
Sibling: src/kiro_crew/dashboard/chat_turn/turn_admission.py -- not affected -- seeds at turn start from the config loaded for that turn

Pattern harvest

Not generalizable: both are one in-memory mirror (slot trust, worker key set) read from config or a record at a moment the record did not exist yet or had changed; no shared shape beyond these two seams.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

Contributions are accepted under the project's Apache-2.0 license.

@buluoray
buluoray requested review from a team and KiroCrewCaptain as code owners October 23, 2026 21:39
@buluoray
buluoray requested a review from iamwhatever October 23, 2026 21:39
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Oct 23, 2026
@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of a84415079e8671d1e076b39ca5bd29454214d27e — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

First-Principles-Verdict: PASS

Nothing to check. Both items fix the mechanism that caused the defect: the seed is re-read once the record exists, and the one writer of the worker set now also removes keys. Each fix has a test that drives it.

What this change ships

Inventory (6 items) — 6 justified

Intent: a crewmate's worker always runs under the Permissions and backend its Profile and record say. FIX.

  1. A worker opened while its crewmate's Profile Permissions are saved now gets the newly saved mode — justified
  2. A Profile worker starts untrusted and only takes its mode once its record is on disk — justified
  3. A worker slot made again on an ordinary template leaves the crewmate's backend as soon as the selection is committed, before any turn runs — justified
  4. Any record read before an allocation that is no longer a worker removes the key from the worker set — justified
  5. New helper members.forget_crewmate_worker_session, with one consumer (note_worker_session) — justified
  6. crew-mode.md and session-control.md updated to describe the seed timing and the key removal — justified

[FIRST-PRINCIPLES-REVIEWED] a844150

Description read

[DESCRIPTION-READ] 9729896931284259335a47bf924589a2726b555addf765d3ed6495582dd88246

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

Revision a84415079e8671d1e076b39ca5bd29454214d27e touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Does not block merge.

@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of a84415079e8671d1e076b39ca5bd29454214d27e — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

Design-Verdict: PASS

No design-level concerns. Both fixes follow the existing seams: the worker is seeded from the Profile once its record exists, and the worker key set now tracks the record in both directions. If seeding is skipped, the worker stays untrusted.

Notes

  • resolve_session_agent_bindings passes the record from read_session_execution(session_key), and that record can be None. The key is now dropped in that case, so a worker whose record read misses once runs that turn on the default backend. Check that a None there only means "no record", never "read failed".

[DESIGN-REVIEWED] a844150

Description read

[DESCRIPTION-READ] 5673cec0d623a781cc6cffc1b4af2ca1610f7786538177aa634e2a1cd0487fab

That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed a84415079e8671d1e076b39ca5bd29454214d27e — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] a844150

Verdict parsed from the review's SHA-scoped output markers for commit a84415079e8671d1e076b39ca5bd29454214d27e.

False positive or not applicable? A repository writer can comment:
/ai-review override fable a84415079e8671d1e076b39ca5bd29454214d27e false-positive: <reason, 20 to 500 characters>

@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of a84415079e8671d1e076b39ca5bd29454214d27e and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] a844150

False positive or not applicable? A repository writer can comment:
/ai-review override gpt a84415079e8671d1e076b39ca5bd29454214d27e false-positive: <reason, 20 to 500 characters>

@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running labels Oct 23, 2026

@cruisercohen cruisercohen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Commenting at ff65c63, not approving: this changes how a crewmate's worker gets its auto-approve posture, so the sign-off is a maintainer's. I found no code defect.

What I checked, on the PR merged with current main:

  • Seed timing. A Profile worker now starts with _trust and _trust_reads off and is seeded only after bind_session_execution has written its record, from a fresh config read. If the crewmate can't be named, it keeps the old inherit-from-caller path. A failed birth, or a config that loads as defaults, leaves the worker untrusted.
  • Races with a Profile save. I traced each ordering. A save written before the fresh read is seen by the read. A save written after it reaches the worker through apply_to_live_sessions, because the record is already on disk. The remaining case is a save whose apply runs while the fresh read is still in flight, so the read is stale. That case is covered by the once-per-slot _member_approval_seeded flag: the apply sets it, so the stale seed skips, and if the seed ran first, the apply overwrites it. I checked this with a scratch test that does a stale read, then the save and its apply, then the seed. Trust→Normal ends untrusted and Normal→Trust ends trusted.
  • Worker key. note_worker_session now drops the key for any non-worker record. read_session_execution returns None only for no record or a plain record: an unreadable record, or a member record missing its identity, raises. So a real worker can't lose its key to a transient read.
  • Tests. 395 pass across the worker, approval, dispatch, agent-switch and create-path suites. With the six source files put back to main, all 4 new cases fail.

One optional suggestion, not blocking: the seed_new_worker comment credits the no-await gap between the read and the seed. The read itself runs in a thread, though, so the in-flight case above is actually covered by _member_approval_seeded. A sentence saying so, plus a case like my scratch test, would keep a later change to that flag from reopening the window.

CrysisDeu
CrysisDeu previously approved these changes Oct 23, 2026
@iamwhatever

Copy link
Copy Markdown
Contributor

HOLD (policy-sensitive) at ff65c63.

I found no defect. A Profile worker now starts untrusted and is seeded once its record is on disk, so a Trust to Normal save made while the worker is being opened reaches it; the stale-read window is closed by the once-per-slot _member_approval_seeded guard in seed_for_turn. A non-worker record now drops its key from the worker set, so a re-made slot leaves the crewmate's backend. All AI lanes pass, every current check is green, and there is no change request.

I am not approving because the diff changes a crewmate worker's auto-approve posture (crewmate_approval.py, session_create.py), which the owner reserves for a human reviewer. What clears it: a maintainer signs off on the permission change. Optional: correct the "No await between this read and the seed" comment to credit _member_approval_seeded, as Design and cruisercohen noted.

— crew-pr-reviewer (r123)

A worker took its crewmate's approval mode from a config read before the
create's awaits, and a Profile save in that window skipped it because it
had no record yet. It now starts untrusted and is seeded once its record
is on disk, so a save from then on reaches it through the live apply.
… one

The crewmate-worker key set only grew, so a session whose record moved
off the worker template kept the crewmate's member backend. A read of a
record that is no longer the worker now removes the key.
@buluoray
buluoray force-pushed the fix/crewmate-worker-approval-race branch from ff65c63 to a844150 Compare October 24, 2026 02:20
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: passed Eligible automated validation passed for the current revision labels Oct 24, 2026
@buluoray

Copy link
Copy Markdown
Contributor Author

Maintainer sign-off (buluoray) at a84415079e8671d1e076b39ca5bd29454214d27e on the policy-sensitive HOLD (crew-pr-reviewer r123): I accept the change to a crewmate worker's auto-approve posture in crewmate_approval.py and session_create.py. A Profile worker starts untrusted and is seeded from a fresh config read once its record is on disk; the in-flight stale-read case is covered by the once-per-slot _member_approval_seeded flag, now named in the seed_new_worker comment and pinned by a test (stale read, then save + apply, then seed: Trust->Normal ends untrusted, Normal->Trust ends trusted). No reviewer found a defect; this clears the HOLD.

@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running labels Oct 24, 2026
@iamwhatever
iamwhatever merged commit a6dcc47 into main Oct 24, 2026
110 of 119 checks passed
@iamwhatever
iamwhatever deleted the fix/crewmate-worker-approval-race branch October 24, 2026 06:13
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 24, 2026
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.

4 participants