Skip to content

feat(threads): record a Slack thread's anchor on every dispatch - #15410

Closed
chenmingwei23 wants to merge 2 commits into
mainfrom
feat/crew-bot-threads-slack
Closed

chenmingwei23 wants to merge 2 commits into
mainfrom
feat/crew-bot-threads-slack

Conversation

@chenmingwei23

@chenmingwei23 chenmingwei23 commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Stacks on #14928; the Slack anchor recorder for Crew Bot Threads (CREW-18694 part 2)

Problem / Motivation

Goal: a Slack thread records which message it hangs off, so the thread's own
history can name its anchor.

A Slack thread has no parent transcript to hang an anchor index off, the way the
dashboard does. It keeps that statement on its own _thread_anchor metadata line
instead. Nothing writes that line yet.

Why it matters

Without the recorder a Slack thread is a session with no record of what it is a
thread of, so nothing downstream can resolve its anchor.

Not a goal

Slack's native threading, which main already has. This adds the anchor record and
nothing about how Slack threads are created or rendered.

What changed (motivation -> approach -> change)

Both dispatch paths call record_anchor at the same point, immediately after the
is_new set_channel gate, off the loop because it reads and rewrites a metadata
line.

It runs on EVERY dispatch rather than only a new session. The write can fail
transiently and the recorder swallows that by contract, so a call gated on is_new
would leave the anchor missing for the life of the process -- is_new is false for
every later message in the same thread. The recorder is idempotent: a session
already carrying an anchor is returned untouched and writes no second
thread/opened, so the steady state is one metadata read. set_channel stays
gated, because that one is not idempotent.

It answers None rather than an error for a folded 1:1 DM, a dashboard-linked route,
or a channel id it does not recognise. Those are ordinary shapes, not faults.

Backwards compatibility

Additive. A session with no anchor behaves as it does today. This slice adds only
the Slack anchor recorder (5 files, +648).

Because this PR stacks on #14928, its diff against main also carries that
epic's removals, so PR Hygiene's take-away check names them here too. Each removed
public def is from the epic's deletion of the old crewmate side-chat reply path,
and each had exactly one reader, all inside that removed path or its route
registration -- the same reader list the epic's own ## Backwards compatibility
carries:

Reader: src/kiro_crew/dashboard/routes/chat.py:configure_chat_routes -- chat -- the only reader of api_chat_thread_reply was the POST /api/chat/threads/{mid}/reply registration; the epic drops that line and registers the new /open and /close routes instead, so no route resolves the removed handler
Reader: src/kiro_crew/dashboard/chat_threads.py:api_chat_thread_reply -- chat -- thread_session_key, build_thread_message and threads_enabled were called only from api_chat_thread_reply and its helpers in this same file, all removed together in the epic's rewrite, so no caller survives
Reader: src/kiro_crew/session.py:_THREAD_PREFIX -- chat -- the one reference to build_thread_message outside chat_threads.py was a code comment beside the _THREAD_PREFIX session-prefix constant; the comment is updated and nothing imports the removed symbol
Reader: src/kiro_crew/dashboard/chat_threads.py:broadcast_thread_reply -- chat -- broadcast_thread_reply (in ws.py) was imported and called only by the removed reply path in chat_threads.py; the new path broadcasts the chat.thread_anchor frame instead, so the removed def has no reader

Tests

test/test_slack_thread_anchor.py -- 8 tests covering both dispatch paths, the
idempotency guard, and each None case.

Manual verification

Covered by the epic's own verification; this slice has no surface of its own.

Why no screenshot: This slice is backend-only -- the Slack anchor recorder (slack/handler.py, slack/threads.py, slack/transport_dispatch.py), a docs page and a test, with no frontend surface. The frontend files the diff shows against main belong to the stacked epic #14928, which carries its own screenshot evidence; this PR adds no visual delta of its own.

Related Issues

CREW-18694 part 2. Stacks on #14928.

Other notes

Split out of #14928 because the AI review lanes fail closed on a diff over 1 MB and
the epic measured 1,006,837 bytes. The boundary is two one-name imports of
record_anchor, so nothing else moved with it. Draft until #14928 merges: against
main this PR's diff shows the epic as well, and it is reviewable on its own only
once the epic has landed.

@github-actions

github-actions Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — 🔴 BLOCK

Premise-level review of fd2d575cd4d67c9b5a8140d808ee6fc0958d032e — 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/CONCERNS are advisory.

First-Principles-Verdict: BLOCK

The Slack anchor is write-only: nothing outside its own writer reads _thread_anchor or the Slack thread/opened entry, so shipping nothing changes nothing.

Not justified as shipped

  • Item 1 — zero consumers: the Goal is "the thread's own history can name its anchor", but no code reads the name. Grepping "_thread_anchor", THREAD_ANCHOR_META and slack.threads across src/ and website/src finds 0 readers outside slack/threads.py. The only read is the writer's own idempotency guard.
    Subtraction: delete slack/threads.py and both record_thread_anchor call sites. Ship them with the first reader.
    Clears when: a reader of _thread_anchor ships in this PR or is already on base.
  • Item 2 — zero consumers: thread_projection projects only the parent's log, and none is a Slack thread's own log. The entry also goes against on_thread_opened's contract: "Record thread/opened on the PARENT conversation's log".
    Subtraction: delete _emit_opened.
    Clears when: a reader of a Slack thread's own thread/opened is named.

What this change ships

Inventory (5 items) — 2 justified

Intent: let a Slack thread say which message it hangs off (ADDITION).

  1. Every Slack thread's history now records an anchor in its metadata — zero consumers
  2. Each new Slack thread's own crew log gets a thread/opened entry — zero consumers
  3. Every Slack message now costs one more metadata read, on both dispatch paths — rides along, follows from item 1
  4. Incognito and temporary sessions get no anchor — justified
  5. The diff against main also carries the epic from feat(threads): a thread is a full session anchored to one message, openable mid-stream #14928 — justified, declared and reviewed there

Blockers

  • The zero option costs nobody anything (items 1–2). I found 0 readers, by the grep above. The description says "nothing downstream can resolve its anchor", and that is still true after this PR.
    Subtraction: drop the recorder until a reader lands with it.
    Clears when: a reader of the Slack anchor ships in this PR or is already on base.

[FIRST-PRINCIPLES-REVIEWED] fd2d575

Description read

[DESCRIPTION-READ] b195f5ac55df8dc62f5a32a62a34b5eb40b77f5329a3b9c460923c85526cf0bd

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 Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — 🔴 changes requested (blocking)

GPT 6.1 found at least one blocking issue that must be resolved before merging fd2d575cd4d67c9b5a8140d808ee6fc0958d032e. 1 of 1 blocking finding(s) are security-class and were withheld from adjudication, so the blocking verdict stands.

This comment is updated in place on each push.

BLOCKING -- src/kiro_crew/dashboard/chat_threads.py:588 -- Retraction can delete a replacement transcript (origin: validation)
await asyncio.to_thread(log.delete_session, thread_key)
Concurrent thread opens → loser’s _retract reads an empty transcript after closing its slot → a same-key /api/chat send persists before deletion → the replacement’s messages are permanently deleted.
Anchor: residual/crash-data-loss-corruption
Fix: Recheck transcript emptiness and perform deletion under the same transcript lock, rather than in separate executor calls.

[BLOCK-MERGE] fd2d575
[GPT-REVIEWED] fd2d575

Adjudication (Opus 5.5) — is blocking on each finding proportionate?

I checked the one fenced finding against the code and marked it FLAG. It keeps blocking the merge either way. FLAG only adds a pre-written rationale in case a repository writer decides to override it with /ai-review. There were no regular findings to rule on.

F1 (unbounded harm: the deleted messages cannot be recovered)

  • How the race is reached: _retract only runs when the anchor write fails or is refused, mainly when two people open a thread on the same message at once and one loses (src/kiro_crew/dashboard/chat_threads.py:634, :639). It deletes only after the slot has been closed and archived (:529), the in-memory slot has no rows (:552), and a fresh read of the saved transcript also comes back empty (:569, :578). The race is real because that read and the delete at :588 are two separate thread-pool calls with no lock held between them. delete_session (src/kiro_crew/history.py:3941) deletes without re-checking whether the transcript is still empty.

  • Recovery and rarity: there is no recovery once the delete runs. To lose anything, someone has to send a message into the losing side's session, which was just created and is already closed, and the message has to be saved inside the gap between two back-to-back thread-pool calls. Even then, the loss is that one message.

    [ADJUDICATION] fd2d575 total=0 uphold=0 downgrade=0
    [GPT-ADJUDICATED] fd2d575

    [ADJUDICATION-FENCED] fd2d575 fenced=1 flagged=1
    FLAG F1 src/kiro_crew/dashboard/chat_threads.py:588 -- Loss needs a lost duplicate-open race (:634/:639), then a same-key send into the loser's just-created, already-archived session (:529) that persists exactly between two back-to-back to_thread calls (:569 read, :588 delete); the window is one executor hop and at most one just-typed message is lost, so a human could plausibly accept the residual risk.
    [GPT-ADJUDICATED-FENCED] fd2d575

🏷️ Fenced finding(s) machine-flagged as likely edge case

The security fence keeps these findings blocking regardless of adjudication; the only clearance path is a human override. The annotate-only pass judged the condition combination each one requires extreme, and pre-drafted the override rationale below. A repository writer must independently verify a rationale before posting it — it is machine-authored, and a wrong override on a security-class finding ships exactly the class the fence exists to stop.

  • F1 src/kiro_crew/dashboard/chat_threads.py:588 — Loss needs a lost duplicate-open race (:634/:639), then a same-key send into the loser's just-created, already-archived session (:529) that persists exactly between two back-to-back to_thread calls (:569 read, :588 delete); the window is one executor hop and at most one just-typed message is lost, so a human could plausibly accept the residual risk.

    /ai-review override gpt fd2d575cd4d67c9b5a8140d808ee6fc0958d032e: Loss needs a lost duplicate-open race (:634/:639), then a same-key send into the loser's just-created, already-archived session (:529) that persists exactly between two back-to-back to_thread calls (:569 read, :588 delete); the window is one executor hop and at most one just-typed message is lost, so a human could plausibly accept the residual risk.
    

False positive or not applicable? A repository writer can comment:
/ai-review override gpt fd2d575cd4d67c9b5a8140d808ee6fc0958d032e: <one-sentence reason>

@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 30, 2026
@github-actions

github-actions Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

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

Review details

No findings.

FINDING — src/kiro_crew/dashboard/thread_projection.py:960 — the fold stops only after spent >= budget_chars, so any stopped fold renders over budget, sets truncated, and cursor_seq = window.start - 1 holds the cursor in place. Once a parent's backlog is larger than one budget, every later turn re-summarizes the same earliest groups, and the thread never sees newer parent rows. A direct build_projection run with cursor 10 and rows 11..200 returns cursor_seq=10, truncated=True. A single failed group of long rows does the same, because its fallback digest is up to 8×600 chars on its own → Fix: stop fold_chunks before a group that would cross the budget, so render never trims. Or, on a trim, keep the first group's rows retired.
FINDING — src/kiro_crew/dashboard/thread_projection.py:475 — after a restart, a thread's new log session has no parent edge and no cursor yet, so minted_as_thread is False and cursor is None adds it to _NOT_A_THREAD. On every later turn the early return drops the cursor that project_for_turn recorded in that same session. Each turn is then a first turn: the preamble is re-sent and the parent is re-read from the anchor → Fix: in project_for_turn, call _NOT_A_THREAD.discard(thread_sid) once lineage or the anchor confirms a parent.
FINDING — src/kiro_crew/dashboard/thread_projection.py:944 — the trigger is a thread opened by a bare click, then a restart, then a first message in the thread before the parent has run. parent_sid is then empty and latest = 0, so the empty window with a preamble persists cursor_seq = window.end = 0, with cursor_source_sid empty. On the next turn the window starts at seq 1, and the parent's whole history is projected as "New … since the last update" (a failed tail probe at :1297 does the same) → Fix: when latest is unknown (no parent_sid, or the probe raised), publish the preamble without persisting a cursor, the way window_unread already does.
FINDING — src/kiro_crew/slack/threads.py:221 — no crew log is ever opened for a Slack session: only chat_runner and messaging/dispatch call on_session_opened, and Slack uses neither. So on_thread_opened(sid, …) silently writes nothing for every Slack thread. The _thread_anchor guard at :184 is persisted first, so later dispatches return early and never retry → Fix: open the session's crew log on the Slack dispatch path before emitting (this touches code outside the diff), or drop the claim that Slack records thread/opened.
FINDING — website/src/pages/members/MembersPage.tsx:4444 — the Crewmates page mounts <ThreadPanel> with no threadSlot and no onStartNew, and the panel's old reply composer was removed. With dashboard.crewmate_threads on, "Reply in thread" on a message with no thread shows "These replies can't be shown yet." and offers no way to start one → Fix: use useThreads here as ChatPage does, and pass threadSlot, onStartNew and onEnd.
FINDING — website/src/pages/ChatPage.tsx:974 — after a thread opens, consumedThreadMid holds its mid. Closing the drawer with X, Escape or End leaves openThreadMid empty, so if (!openThreadMid && consumedThreadMid.current && !threads.openError) return fires and ?thread= is never removed. A reload or a copied link then reopens the dismissed thread → Fix: return early only while an open is actually pending (threads.opening), or clear the param in threads.close.

[OPUS-REVIEWED] fd2d575

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

False positive or not applicable? A repository writer can comment:
/ai-review override fable fd2d575cd4d67c9b5a8140d808ee6fc0958d032e: <one-sentence reason>

@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Sep 30, 2026
@chenmingwei23
chenmingwei23 force-pushed the feat/crew-bot-threads-slack branch from 73b9518 to 5661946 Compare October 7, 2026 11:59
@chenmingwei23

Copy link
Copy Markdown
Contributor Author

Rebased onto the current #14928 epic head (547c7e565b) so the branch carries only this slice's own commit, and main is now mergeable rather than conflicting.

GPT 5.6 blocking finding (restricted-mode persistence) — fixed. Both record_thread_anchor dispatch call sites are now gated on not _is_slack_restricted(session_key):

  • src/kiro_crew/slack/handler.py (the handle_message path)
  • src/kiro_crew/slack/transport_dispatch.py (the transport path)

An !incognito / !temporary session no longer persists _thread_anchor / agent metadata, matching the five sibling write-side gates in the same function. Added test_a_restricted_session_is_not_anchored to pin it: the turn still runs, but no anchor metadata is written for a restricted session.

Opus 5 findings (thread_projection.py cursor, useThreads.ts recovery, i18n diacritics) are advisory/non-blocking and live in the epic's own files, not this slice — out of scope here, best addressed on #14928.

First Principles (CONCERNS) is advisory and non-blocking.

Local gate green on the rebased tree: test_slack_thread_anchor.py (9) plus the related slack handler / transport_dispatch / privacy_mode / golden-transcript suites (392 total), black/isort/flake8/mypy clean. New head 566194669aa3a6cd922928513f80abdddfbb2999.

@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Sensitive Change Review

Sensitive files changed:

  • src/kiro_crew/security_posture.py

  • No approval on fd2d575 yet.

A human reviewer with write access must approve the CURRENT head and
paste this template into the approval, every field filled
(N/A needs a reason):

## Sensitive change review
- What rule changed:
- Before -> after (what was allowed/blocked, now):
- Worst case if wrong (breaks commands / leaks secret / locks user out):
- Evidence checked (which sandbox result proves it):
- No regression:
  - Tools still work (which tools/commands were run, result):
  - Backward compatible (old config / old data / old callers still work, how checked):
  - Existing tests (which suites passed on this head):
- How to undo:

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running merge conflict Branch has merge conflicts with its base — author must resolve before merge labels Oct 7, 2026
@chenmingwei23
chenmingwei23 force-pushed the feat/crew-bot-threads-slack branch from 5661946 to a9395ce Compare October 7, 2026 13:26
@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 Oct 7, 2026
@chenmingwei23
chenmingwei23 force-pushed the feat/crew-bot-threads-slack branch from a9395ce to 46a2b5c Compare October 7, 2026 14:08
@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 Oct 7, 2026
wave1 verify added 2 commits October 7, 2026 15:40
A reply thread on a crewmate's chat was a bounded reply list: replies in a
sidecar, every reply in a throwaway `thread:<slot>:<mid>` session that could
never resume, a forced read-only tool posture, no memory, the whole envelope
re-seeded each turn, and a one-reply-in-flight lock. It lived only on the
Crewmates page, only behind the `dashboard.crewmate_threads` flag, and only on
a message whose turn had already finished.

A thread is now an ordinary chat session plus an anchor to one message.

Anchor is (surface, conversation, message id): (dashboard, parent slot, mid) and
(slack, channel id, thread_ts). It is stored on the thread session's own metadata
and in a per-parent index -- the old reply sidecar, at version 2. Version 1 files
are read in place and render as a read-only fold, because their replies came from
a cold read-only session with no memory and replaying them as a full session's
transcript would assert a history that session never had.

Opening never touches the running turn: no semaphore, no `_handle_busy` branch,
no `messaging.queue_mode` value. It reads a non-consuming snapshot of the parent's
partial text, so every messaging invariant holds. A streaming row has no mid, so
the anchor resolves to the user message that started the turn, which is already
persisted.

Two openers, one coroutine: `POST /api/chat/threads/{mid}/open` for a person
clicking a message, and MCP `thread_open` for an agent opening one on the message
it is answering. `open_thread` calls `create_session` directly and lets its
refusals through verbatim rather than adding a bypass.

Slack already keyed one session per thread_ts and never recorded it; that session
is now recorded as anchored, with nothing re-keyed and the 1:1 DM fold and the
dashboard mirror binding untouched.

`thread/opened` and `thread/closed` join `session/opened` in the crew log, so the
ledger is the existing one. `dashboard.crewmate_threads` is removed, not defaulted
on. Retired with it: `thread_session_key`, the `thread:` stateless prefix,
`_run_thread_turn`, `build_thread_message` and its envelope constants, the
read-only spec posture, the per-reply byte caps, and `409 not_crewmate_chat`.

Specs updated alongside: history.md (new Surfaces section), session.md,
slack-gateway.md, crew-mode.md, docs/feature-map/README.md.
Stacks on the Crew Bot Threads epic. A Slack thread has no parent transcript to
hang an anchor index off, so it keeps its own `_thread_anchor` metadata line, and
this is the recorder that writes it.

Both dispatch paths call it at the same point, immediately after the `is_new`
`set_channel` gate, off the loop because it reads and rewrites a metadata line.
It is called on EVERY dispatch rather than only a new session: the write can fail
transiently and the recorder swallows that by contract, so a call gated on
`is_new` would leave the anchor missing for the life of the process, `is_new`
being false for every later message in the same thread. The recorder is
idempotent -- a session already carrying an anchor is returned untouched and
writes no second `thread/opened` -- so the steady-state cost is one metadata read.

It answers None, not an error, for a folded 1:1 DM, a dashboard-linked route, or
a channel id it does not recognise: those are ordinary shapes, not faults.

Split from the epic because the review lanes cap a diff at 1 MB and the epic sat
over it. The boundary is two one-name imports, so nothing else moves.
@chenmingwei23
chenmingwei23 force-pushed the feat/crew-bot-threads-slack branch from 46a2b5c to fd2d575 Compare October 7, 2026 16:07
@chenmingwei23

Copy link
Copy Markdown
Contributor Author

Both GPT 6.1 blockers are fixed in code on the epic (#14928, now head 04fcc5f8bd) that this PR stacks on, and carried here by the restack to head fd2d575cd4 — not overridden:

  • chat_threads.py:561 (retraction deletes a replacement's transcript): the retraction now re-reads the PERSISTED transcript (log.read_messages) immediately before delete_session and keeps it when any row landed — the shape of a same-key replacement that persisted during close_slot's awaits — failing safe (keep) on an unreadable transcript. Two regression tests added.
  • thread_projection.py:1216 (parent-log reset skips its initial rows): resetting the cursor was not enough — the stale anchor seq counts in the OLD log, so a first-turn window reached back past the new tail (empty window, rows 1..N skipped). On a reset the anchor position is now treated as UNKNOWN, so the new log is projected from its start. Regression test added.

Slice-local: the turn_with_history golden transcript digest was updated to reflect this slice's intended per-dispatch thread-anchor record (the scenario's own second-turn failure is unchanged); that fixes the Backend Tests 3.12 / Windows shard 6 reds. Design/UX/Opus here are inherited from the stacked epic and re-evaluate on this restacked head (the epic's ThreadPanel UX fix is carried).

@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Oct 7, 2026
@chenmingwei23

Copy link
Copy Markdown
Contributor Author

Closing this after an audit.

This is additive plumbing (recording a Slack thread's anchor on every dispatch) for the unshipped thread feature in #14928. With #14928 closed, this has no standalone value — it only feeds a feature that isn't landing in its current form.

The branch (feat/crew-bot-threads-slack) is kept so it can be revived alongside a future, smaller thread proposal.

@github-actions github-actions Bot removed the readiness: action required A blocking check or review needs attention label Oct 7, 2026
@bolichen97
bolichen97 deleted the feat/crew-bot-threads-slack branch November 6, 2026 07:23
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.

1 participant