Skip to content

fix(crew): destroy a deleted crewmate's thread session entries - #28613

Closed
cruisercohen wants to merge 1 commit into
mainfrom
fix/22486-delete-clears-slot
Closed

cruisercohen wants to merge 1 commit into
mainfrom
fix/22486-delete-clears-slot

Conversation

@cruisercohen

@cruisercohen cruisercohen commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: deleting a crewmate also clears its pinned thread's session_map.json entries, so a bad slot record is not left behind with no way to remove it.

The thread is keyed dashboard:member-<slug> (V1) or dashboard:member-<slug>.memory-<store> (private store). DELETE /api/agents/{name} removes the config row, crew log, dashboard lock, avatar, goal check-ins, worktrees and private template copy, but never the session-map entry. Fresh start rotates the sid and keeps the key, a restart restores it, and member threads are not listed where a session can be deleted, so the entry outlives the crewmate.

Why it matters

The issue reports a crewmate whose thread mounted only one MCP server because of a bad slot record. The only recovery was to make a new crewmate, leaving the dead record in place for good.

Not a goal

  • The reserved member id. A deleted member's retained store and DM binding reserve its slug on purpose (memory_stores._allocate_member_id); that is the companion issue, and fix(members): let a new crewmate start from a deleted one's memory #22947 builds on it.
  • kirocrew agent delete. Only the gateway's in-memory map may rewrite session_map.json (it rewrites the whole file from memory), so the CLI path leaves the entries as before.
  • Repairing a record in place.

What changed (motivation → approach → change)

  • Symptom: after a delete, dashboard:member-<slug>.memory-<store> is still in session_map.json.
  • Cause: the shared teardown crew_removal._remove_member_leftovers has no step for the thread's session.
  • Change: new dashboard/member_thread_teardown.destroy_member_thread_sessions, called from _remove_member_leftovers when the route passes the gateway's session manager. It resolves the slug from the config captured before the delete, skips everything when a live crew still derives the slug (_member_slug_is_claimed, the rule the other steps use), and calls sessions.destroy(key) for each entry the map holds: the V1 key, the current store's key (named outright, so an entry a Fresh start left without a sid is reached), and earlier generations matched by the exact .memory- boundary from mapped_sids_by_key() (so member-mate-0a26… is not caught by member-mate). destroy ends a live process for the thread and deletes the map entry in one step. A failure is reported as a failed thread session row and never fails the committed delete.
  • crew-mode.md says what the delete does with the entries and why the CLI path does not.

The new function lives outside agent_admin/ so the agents facade (at the 2,000-line cap) does not need another binding; it takes the claimed-slug predicate as an argument, so a patch of the facade's _member_slug_is_claimed still reaches it.

Backwards compatibility

Compatible: no request is refused and no response shape changes. The only new effect is that a deleted crewmate's thread session is destroyed. Its transcript file stays. Note for #22947 (adopting a deleted member's store): an adopted thread starts a new native session from the kept transcript instead of resuming the old sid.

Reader: src/kiro_crew/session.py:SessionManager.get_or_create -- dashboard -- a deleted crewmate's thread key no longer maps to a sid, so nothing resumes it; test_delete_destroys_the_departed_crewmates_thread_entries
Reader: src/kiro_crew/dashboard/server_runtime/crewmate_prune.py:_claimed_dashboard_slots -- dashboard boot -- the slot is no longer claimed as a dashboard session; same test

Tests

test/test_crew_delete_forgets_member_thread_session.py, through the real route with a real SessionMap:

  • the departed crewmate's V1 and V2 keys are destroyed; a sibling member-mate-0a2620b15f41… key and an unrelated chat stay (fails on main);
  • an entry with its sid cleared by a Fresh start is destroyed too (fails on main);
  • a slug another live crew still derives keeps every entry;
  • a destroy that raises still answers 200 with the row gone;
  • _remove_member_leftovers with no session manager (the CLI) leaves the map file byte-identical.

Run locally (Linux, Python 3.12): the new file plus every test file naming the agents facade, crew_removal, _remove_member_leftovers or a composition contract (81 files): 5375 passed, 9 skipped. black, isort, flake8 and mypy clean on the touched modules; file-line-cap, comment-history and docs_lint pass.

Manual verification

N/A — unit coverage sufficient: the route test drives the real delete handler against a real session map file and asserts the entries left.

Related Issues

Fixes #22486

Sibling sweep

Swept: git grep -n "_remove_member_leftovers(" -- src/
Siblings: kirocrew agent delete (left on purpose, see Not a goal); the sync and startup prunes call no teardown on main (#28052 adds one; the new parameter is optional, so it composes).

Pattern harvest

Not generalizable: one teardown step missing from the crew delete; the existing steps already follow a shared unclaimed-slug rule, and this one now does too.

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): crew-mode.md, workspace paragraph
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

Contributed under the project's Apache-2.0 license.

@cruisercohen
cruisercohen force-pushed the fix/22486-delete-clears-slot branch from 778054b to f3d6f3b Compare November 2, 2026 22:13
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Nov 2, 2026
@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

Revision 2b2e431f0ab2ddb771bbaed75164254193eaed48 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 Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of 2b2e431f0ab2ddb771bbaed75164254193eaed48 — 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

Fixes a reported defect (#22486) at the teardown that skipped this step, and adds tests that fail on base. One note: the generation matcher re-implements an existing slot-key parser.

Notes

  • Item 2 — duplicate of src/kiro_crew/members_parts/dm_bindings.py:slug_from_dm_slot_key: prefix = exact + MEMORY_STORE_SLOT_SUFFIX / key.startswith(prefix) hand-builds the key→slug parse. That helper's docstring says it is "One spelling here so a third reader cannot drift from the other two." Subtraction: replace the startswith(prefix) filter with slug_from_dm_slot_key(key.removeprefix("dashboard:")) == slug, and delete the MEMORY_STORE_SLOT_SUFFIX import.

What this change ships

Inventory (7 items) — 6 justified

Intent: when someone deletes a crewmate from the dashboard, its broken thread session record goes too, so recovering doesn't mean creating a new crewmate. FIX.

  1. Dashboard delete removes the crewmate's dashboard:member-<slug> map entry, including one left without a sid by a Fresh start — justified
  2. Dashboard delete removes every .memory-<store> generation entry for that slug — duplicate of dm_bindings.slug_from_dm_slot_key matching
  3. A live process for the deleted crewmate's thread is ended on delete — justified
  4. Entries are kept when another live crew still derives the slug — justified
  5. A failed destroy logs a failed "thread session" row and the delete still answers 200 — justified
  6. _remove_member_leftovers takes an optional sessions argument; the CLI path doesn't pass it — justified
  7. crew-mode.md describes what happens to the entries on delete — justified

[FIRST-PRINCIPLES-REVIEWED] 2b2e431

Description read

[DESCRIPTION-READ] 0372f40c2f96e398a373f5b261df2069980c90c6652c69ebe44ec1182f2f6579

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 Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

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

Design-Verdict: PASS

Nothing blocks: the missing teardown step goes into the shared crew-delete teardown, follows the same unclaimed-slug rule as the other steps, and goes through SessionManager.destroy, the call that already ends a session and deletes its map entry.

Notes

  • destroy_member_thread_sessions reads sessions._session_map directly (has_hint, mapped_sids_by_key). The session manager doesn't expose this, so the teardown now depends on its internal attribute. A small public method on the manager, such as "keys held under prefix", would keep the map private if a later PR touches it.

[DESIGN-REVIEWED] 2b2e431

Description read

[DESCRIPTION-READ] 9fa33e17399ae5fb25c19bffa1c964f631d9560620b55221d221ed437c36a292

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 Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

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

Review details

No findings.

[OPUS-REVIEWED] 2b2e431

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

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

DELETE /api/agents/{name} left dashboard:member-<slug>[.memory-<store>]
in session_map.json with no supported way to clear it. The delete's shared
teardown now destroys those entries through the session manager when the
slug is unclaimed.

Fixes #22486
@cruisercohen
cruisercohen force-pushed the fix/22486-delete-clears-slot branch from f3d6f3b to 2b2e431 Compare November 2, 2026 23:07
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

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

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 2b2e431

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

@cruisercohen
cruisercohen marked this pull request as ready for review November 3, 2026 01:13
@cruisercohen
cruisercohen requested review from a team and KiroCrewCaptain as code owners November 3, 2026 01:13
@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 Nov 3, 2026
@bolichen97

Copy link
Copy Markdown
Contributor

Thanks for this PR. The CI run on this head never got runners, so its jobs were cancelled, and the branch is based on an older main. Could you update it onto the latest main (rebase or merge)? That starts a fresh CI run against current main, and we will review it once it is green.

@bolichen97

Copy link
Copy Markdown
Contributor

[operator: bolichen97] Continuing this in #32924; thanks for the start.

bolichen97 added a commit that referenced this pull request Nov 8, 2026
Deleting a crewmate tore down its config row, crew log, avatar, goal
check-ins, worktrees and private template copy, but never its pinned
thread's session_map.json entry. A bad dashboard:member-<slug> record
then outlived the crewmate, and nothing else could clear it.

The shared removal teardown now destroys the thread's keys through the
gateway's session manager, which also ends a live process for the
thread: the V1 key, the current store's key and earlier store
generations on the exact .memory- boundary. A slug another live crew
still derives keeps its entries, and a failure never fails the delete.
The dashboard delete and both prunes pass the manager; the CLI delete
has none and leaves the map alone.

Builds on #28613 by @cruisercohen.

Co-authored-by: Matt Cohen <1832296+cruisercohen@users.noreply.github.com>
@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Nov 8, 2026
buluoray added a commit that referenced this pull request Nov 8, 2026
Deleting a crewmate tore down its config row, crew log, avatar, goal
check-ins, worktrees and private template copy, but never its pinned
thread's session_map.json entry. A bad dashboard:member-<slug> record
then outlived the crewmate, and nothing else could clear it.

The shared removal teardown now destroys the thread's keys through the
gateway's session manager, which also ends a live process for the
thread: the V1 key, the current store's key and earlier store
generations on the exact .memory- boundary. A slug another live crew
still derives keeps its entries, and a failure never fails the delete.
The dashboard delete and both prunes pass the manager; the CLI delete
has none and leaves the map alone.

Builds on #28613 by @cruisercohen.

Co-authored-by: Matt Cohen <1832296+cruisercohen@users.noreply.github.com>
(cherry picked from commit bed3c8a)

Original author: @bolichen97
buluoray added a commit that referenced this pull request Nov 8, 2026
Deleting a crewmate tore down its config row, crew log, avatar, goal
check-ins, worktrees and private template copy, but never its pinned
thread's session_map.json entry. A bad dashboard:member-<slug> record
then outlived the crewmate, and nothing else could clear it.

The shared removal teardown now destroys the thread's keys through the
gateway's session manager, which also ends a live process for the
thread: the V1 key, the current store's key and earlier store
generations on the exact .memory- boundary. A slug another live crew
still derives keeps its entries, and a failure never fails the delete.
The dashboard delete and both prunes pass the manager; the CLI delete
has none and leaves the map alone.

Builds on #28613 by @cruisercohen.

Co-authored-by: Matt Cohen <1832296+cruisercohen@users.noreply.github.com>
(cherry picked from commit bed3c8a)

Original author: @bolichen97
buluoray added a commit that referenced this pull request Nov 8, 2026
Deleting a crewmate tore down its config row, crew log, avatar, goal
check-ins, worktrees and private template copy, but never its pinned
thread's session_map.json entry. A bad dashboard:member-<slug> record
then outlived the crewmate, and nothing else could clear it.

The shared removal teardown now destroys the thread's keys through the
gateway's session manager, which also ends a live process for the
thread: the V1 key, the current store's key and earlier store
generations on the exact .memory- boundary. A slug another live crew
still derives keeps its entries, and a failure never fails the delete.
The dashboard delete and both prunes pass the manager; the CLI delete
has none and leaves the map alone.

Builds on #28613 by @cruisercohen.

Co-authored-by: Matt Cohen <1832296+cruisercohen@users.noreply.github.com>
(cherry picked from commit bed3c8a)

Original author: @bolichen97
buluoray added a commit that referenced this pull request Nov 8, 2026
Deleting a crewmate tore down its config row, crew log, avatar, goal
check-ins, worktrees and private template copy, but never its pinned
thread's session_map.json entry. A bad dashboard:member-<slug> record
then outlived the crewmate, and nothing else could clear it.

The shared removal teardown now destroys the thread's keys through the
gateway's session manager, which also ends a live process for the
thread: the V1 key, the current store's key and earlier store
generations on the exact .memory- boundary. A slug another live crew
still derives keeps its entries, and a failure never fails the delete.
The dashboard delete and both prunes pass the manager; the CLI delete
has none and leaves the map alone.

Builds on #28613 by @cruisercohen.

Co-authored-by: Matt Cohen <1832296+cruisercohen@users.noreply.github.com>
(cherry picked from commit bed3c8a)

Original author: @bolichen97
buluoray added a commit that referenced this pull request Nov 8, 2026
Deleting a crewmate tore down its config row, crew log, avatar, goal
check-ins, worktrees and private template copy, but never its pinned
thread's session_map.json entry. A bad dashboard:member-<slug> record
then outlived the crewmate, and nothing else could clear it.

The shared removal teardown now destroys the thread's keys through the
gateway's session manager, which also ends a live process for the
thread: the V1 key, the current store's key and earlier store
generations on the exact .memory- boundary. A slug another live crew
still derives keeps its entries, and a failure never fails the delete.
The dashboard delete and both prunes pass the manager; the CLI delete
has none and leaves the map alone.

Builds on #28613 by @cruisercohen.

Co-authored-by: Matt Cohen <1832296+cruisercohen@users.noreply.github.com>
(cherry picked from commit bed3c8a)

Original author: @bolichen97
buluoray added a commit that referenced this pull request Nov 8, 2026
Deleting a crewmate tore down its config row, crew log, avatar, goal
check-ins, worktrees and private template copy, but never its pinned
thread's session_map.json entry. A bad dashboard:member-<slug> record
then outlived the crewmate, and nothing else could clear it.

The shared removal teardown now destroys the thread's keys through the
gateway's session manager, which also ends a live process for the
thread: the V1 key, the current store's key and earlier store
generations on the exact .memory- boundary. A slug another live crew
still derives keeps its entries, and a failure never fails the delete.
The dashboard delete and both prunes pass the manager; the CLI delete
has none and leaves the map alone.

Builds on #28613 by @cruisercohen.

Co-authored-by: Matt Cohen <1832296+cruisercohen@users.noreply.github.com>
(cherry picked from commit bed3c8a)

Original author: @bolichen97
buluoray added a commit that referenced this pull request Nov 8, 2026
Deleting a crewmate tore down its config row, crew log, avatar, goal
check-ins, worktrees and private template copy, but never its pinned
thread's session_map.json entry. A bad dashboard:member-<slug> record
then outlived the crewmate, and nothing else could clear it.

The shared removal teardown now destroys the thread's keys through the
gateway's session manager, which also ends a live process for the
thread: the V1 key, the current store's key and earlier store
generations on the exact .memory- boundary. A slug another live crew
still derives keeps its entries, and a failure never fails the delete.
The dashboard delete and both prunes pass the manager; the CLI delete
has none and leaves the map alone.

Builds on #28613 by @cruisercohen.

Co-authored-by: Matt Cohen <1832296+cruisercohen@users.noreply.github.com>
(cherry picked from commit bed3c8a)

Original author: @bolichen97
@iamwhatever

Copy link
Copy Markdown
Contributor

Thanks for this fix! Its issue #22486 was closed by #32924 (#32924, merged 2026-11-08), which clears a removed crewmate's thread session record on main. This PR is superseded by it, so I'm closing it. Please reopen if there is a case #32924 does not cover.

@github-actions github-actions Bot removed the readiness: action required A blocking check or review needs attention label Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge conflict Branch has merge conflicts with its base — author must resolve before merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Corrupt crewmate slot record cannot be cleared by any supported control

3 participants