Skip to content

fix(slack): bound the per-thread override maps together - #18985

Merged
bolichen97 merged 2 commits into
kirodotdev:mainfrom
javenciu:fix/slack-thread-override-maps-are-bounded
Oct 12, 2026
Merged

bolichen97 merged 2 commits into
kirodotdev:mainfrom
javenciu:fix/slack-thread-override-maps-are-bounded

Conversation

@javenciu

@javenciu javenciu commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: the Slack handler's per-thread override state stops growing with every thread the bot has ever heard from, each value it keeps is bounded, and a thread that is evicted gets its !ta and !project choices back on its next message.

src/kiro_crew/slack/handler.py keeps three module-level containers for per-thread state: _hydrated_sessions (the guard that keeps _hydrate_thread_overrides from re-reading a thread's metadata on every message), _thread_agents (the !ta choice) and _thread_projects (the !project choice). Every inbound message for a new thread adds its session key to the guard, and to the two maps when the thread's conversation metadata names an agent or a project (src/kiro_crew/slack/handler_runtime/inbound.py, _hydrate_thread_overrides). Only !ta off and !project off remove an entry from the two maps, and nothing removes a key from the guard. The entries outlive the thread's session: the idle sweep ends the session, and the three entries stay for the life of the process.

Why it matters

A gateway that answers many threads holds one entry per thread forever, about 125 bytes in the guard and more for each override. A busy workspace adds thousands of threads a week, and a long-running gateway never gives the memory back until it restarts.

Not a goal

  • _pending_approvals and _linked_approvals are not touched; they are in-flight registries with their own removal.
  • The persisted overrides in the conversation metadata are unchanged: this bounds the in-memory cache only.
  • No change to what !ta and !project persist or to the hydration order of the inbound paths.
  • Setting a !ta or !project choice in more than 4,095 threads in one process. Pinned threads count inside the bound, at most MAX_PINNED_THREADS = THREAD_SLOTS - 1 of them, so once 4,095 threads are pinned, a command that would pin one more thread is refused with a reply that names the limit and says how to free a slot (!ta off or !project off in a thread), and it sets nothing (thread_override_slots.py:140-158, handler_runtime/commands.py:724, :842). A restart also frees them, and hydration then reads the persisted choices back.
  • Setting or restoring an agent name or project path longer than 4,096 characters. !ta and !project refuse one with a reply that names its length and the limit, and hydration skips one with a warning. On Linux and macOS no directory path that long can be opened, so in practice this refuses a Windows long path, or a project agent whose spec declares a name that long.

What changed

  • This revision restructures the two rules that kept drawing findings on the same spans. Only a hydration whose read the store reports as readable marks a thread hydrated. Hydration reads the metadata line once through read_back (slack/thread_override_slots.py:258), which reads it through get_metadata_status, because get_metadata answers a line the store could not read with empty metadata, the same as a thread with nothing set, and an unreadable read returns before the thread is marked (slack/handler_runtime/inbound.py:253). pin (slack/thread_override_slots.py:194) no longer adds the thread to the hydration guard, and hydration never publishes over a kind a command pinned (kind_is_pinned, slack/thread_override_slots.py:105, checked at slack/handler_runtime/inbound.py:274-279). So a pinned thread whose hydration gave up, or could not read the line, stays out of the guard, and its next message reads its other override back. The cost: a thread whose metadata line stays unreadable is read again on each of its messages. A reset releases its pin only after a read that the store reports as readable: !ta off and !project off read back through the same read_back, and an unreadable read-back keeps the pin (slack/handler_runtime/commands.py:711, :856). Tests: seven new cases fail on the previous head 97a10d4 and pass here, 3 of 3; the file, 35 passed, 3 of 3; the 17 neighbour files, 1,109 passed.
  • New src/kiro_crew/slack/thread_override_slots.py: the three containers are held to THREAD_SLOTS threads together, in the least-recently-used shape already used for the thread-replies watermark (_last_turn in src/kiro_crew/slack/thread_replies.py). THREAD_SLOTS is that watermark's own bound, _WATERMARK_SLOTS (4096), imported rather than written a second time, so the two cannot drift (thread_override_slots.py:46). Past the bound, the oldest thread loses its guard entry and both override entries at once. Its next message finds no guard entry, so _hydrate_thread_overrides reads the overrides back from the conversation metadata as it does for a thread it has never seen.
  • _hydrate_thread_overrides records a thread through note_hydrated instead of _hydrated_sessions.add, and a repeat message for a tracked thread moves it to the fresh end (touch). _get_agent_for_session, the reader the background paths use (monitor ticks, autonudge and subagent notices in src/kiro_crew/slack/gateway.py), also touches the thread, so a thread with background activity stays resident.
  • Design choice, pinning: an override a person sets with !ta <name> or !project <path> in this process is pinned and never evicted while it is set (pin in _bang_thread_agent and _bang_project; !ta off and !project off release it, unless hydration would read the cleared value back, below). Hydration cannot be relied on to read these choices back: the command's metadata write is skipped when there is no conversation log and only logged at debug when it fails, and hydration prefers a recorded execution template over the agent field. An override that came from the metadata is evictable, because the metadata restores it. Pinned threads count inside THREAD_SLOTS: each hydration evicts the oldest unpinned threads until pinned and unpinned together fit (note_hydrated, slack/thread_override_slots.py:176), and pin refuses a new thread once MAX_PINNED_THREADS (THREAD_SLOTS - 1, slack/thread_override_slots.py:51) are pinned (slack/thread_override_slots.py:214). That keeps one slot evictable, so hydrating a new thread never evicts that same thread and then writes its rows outside every count. Both commands call pin before they write the override, so a refused command writes no row (slack/handler_runtime/commands.py:765 and slack/handler_runtime/commands.py:926). Between a message's hydration and the pin, other threads hydrating can evict the command's own thread, its other override included: the inbound path awaits after it hydrates (for example admit_inbound_callback, slack/transport_dispatch.py:350), the dispatcher, _handle_slash_command, awaits the deprecation notice for !ta (slack/handler_runtime/commands.py:95), and !ta awaits its agent lookup (slack/handler_runtime/commands.py:733). So each command runs _hydrate_thread_overrides again just before it pins, with no await in between (slack/handler_runtime/commands.py:762 and slack/handler_runtime/commands.py:923). For a thread still in the guard it returns at once. An evicted thread reads both overrides back from the conversation metadata, and a value set while that read ran is kept, the rule every hydration applies (slack/handler_runtime/inbound.py:251-279). Then pin calls _evict_over_bound (slack/thread_override_slots.py:218), the loop note_hydrated also calls (slack/thread_override_slots.py:191), which evicts the oldest unpinned threads from the guard and both maps until the pins and the rest fit THREAD_SLOTS (_evict_over_bound, slack/thread_override_slots.py:161). pin does not put the thread in the hydration guard: only a hydration that read the metadata does. If the hydration just before the pin gave up, the thread stays out of the guard and its next message reads its other override back, and hydration never publishes over the kind the command pinned (kind_is_pinned), which keeps that message, and any later one, from hydrating over the override. That runs before the command writes its row (_thread_agents, slack/handler_runtime/commands.py:779; _thread_projects, slack/handler_runtime/commands.py:940). The test reset in test/conftest.py now also clears the module's _order and _pinned, so an existing !ta test no longer leaves a pin for the rest of its worker.
  • A reset that hydration would undo keeps its pin. Hydration prefers a recorded execution template (a cron-created thread records one) over the agent field !ta off clears (messaging/session_resume.py:99-104), and reads the old value back when the clear's metadata write failed. So once it has cleared the override, and after the write, !ta off reads back what hydration would restore through read_back (slack/thread_override_slots.py:258, called at slack/handler_runtime/commands.py:701; !project off, :848). It reads the metadata line once through get_metadata_status, because get_metadata answers a line the store could not read with empty metadata, the same as a thread with nothing set, and hydration's reader, _read_thread_overrides, resolves that line. Then, on either branch, the thread hydrates again and its value is cleared again, with no await after the hydration (:708-709, :853-854). A thread evicted while the command awaited its write can meanwhile be hydrated by its own next message from a read made before the write landed, and the value that read published is cleared before the command replies; a read of that kind that is still in flight when the command replies is refused by the hydration rule below. A thread that would get a value back, or whose metadata line cannot be read back, is then pinned with nothing set (:712, :857), so it is never evicted in this process, the same as on origin/main, where nothing evicts it before a restart, and no hydration publishes that kind for it again. Any other thread releases its pin (:716, :861). When every pin is taken, the reset still applies and the reply says it lasts until the thread is evicted (unheld_reset_text, slack/thread_override_slots.py:230). unpin returns a thread to the evictable order only when it releases that thread's last pin, and otherwise only touches a tracked thread (slack/thread_override_slots.py:287-292). So a reset of a thread evicted while its command awaited, one that held no pin, does not track that thread again outside the bound.
  • Hydration rule: hydration publishes nothing it read before a newer !ta or !project metadata write. Eviction is what makes this needed: on origin/main a thread stays in the hydration guard for the life of the process, so a read that began before a command's write finds the guard set and publishes nothing. Here, a read still in flight after !ta off replied could find its thread evicted again by then, and publish the old value. The commands count their metadata writes in one process-wide integer, _command_writes (slack/thread_override_slots.py:73), so there is nothing to bound. Each command bumps it as soon as its write's worker returns or raises, before it awaits anything else (note_command_write; !ta off slack/handler_runtime/commands.py:696, !ta <name> :789, !project off :844, !project <path> :949). _hydrate_thread_overrides takes the count before each read and compares it after; when it moved, it reads again, at most HYDRATION_READ_ATTEMPTS (3) times (slack/thread_override_slots.py:78, slack/handler_runtime/inbound.py:244-263). If every read overlapped a write, it publishes nothing, does not mark the thread hydrated, and logs a warning, so the thread's next message hydrates it and the message whose hydration gave up runs with the defaults (slack/handler_runtime/inbound.py:252-259). The count is process-wide, so a write in another thread also causes a read again; giving up needs a write to land during each of three consecutive reads. If that happens to the hydration a command runs just before its pin, the thread stays unhydrated after the pin, so its next message reads its other override back. There is no bump before the write. A read can only be stale if it began before the write landed, so the count it took precedes the bump: if it is back after the bump, it reads again, and if it is back before, it publishes before the command's own hydrate-and-clear above removes the value. A bump before the write would not help: a read begun after it but before the write landed takes the bumped count and is just as stale. The !ta <name> and !project <path> threads are already pinned when they write, so hydration never publishes over that kind and a stale read cannot reach it; their bump keeps the rule from depending on the pin.
  • Per-value bound: every site that writes the two maps keeps a value only within OVERRIDE_VALUE_MAX_CHARS (4096, thread_override_slots.py:61), so the bound holds whichever path wrote it. With the count bound, the maps keep at most 4096 threads × 2 values × 4096 characters.
    • Hydration, which this change edits, keeps what _read_thread_overrides returns: _hydrate_thread_overrides writes it at handler_runtime/inbound.py:277 and :279, and eviction now sends every evicted thread back through it. That read-back of the conversation metadata is bounded by nothing: origin/main keeps meta["project"] after only is_sensitive_path (inbound.py:203 at 843abe3), and keeps the agent field, or what session_agent_from_metadata returns, as it is (:195). Each value is now kept only when it is text within the limit, through bounded_override (thread_override_slots.py:126, called at inbound.py:198 and :202). A longer or non-text value is skipped with one warning that names its length or type, never its text, and the thread uses the default for that kind. A list-valued project, which made main's hydration raise from is_sensitive_path (executed: TypeError: expected str, bytes or os.PathLike object, not list), is skipped the same way.
    • The two setters refuse a longer value before they pin or write anything, with a reply that names its length and the limit, never the value (setter_refusal, thread_override_slots.py:110). !ta checks the name it resolved (handler_runtime/commands.py:744): a project agent's name is whatever its spec file declares, and nothing else limits it (_resolve_agent_name returns project_agent_name(spec), slack/handler.py:808). !project checks the realpath it would keep (slack/handler_runtime/commands.py:884), before the sensitive-path and directory checks, so no audit record carries an over-limit path; it records project_denied_too_long with the length instead.
    • Every refusal the bound adds writes a SEL command record, as the other !project refusals do: agent_denied_too_long with the name's length, never the name (slack/handler_runtime/commands.py:754), and, when pin refuses, agent_denied_capacity (slack/handler_runtime/commands.py:774) or project_denied_capacity (slack/handler_runtime/commands.py:935). A capacity record names the agent or path, which already passed the per-value limit, as agent_switch and project_denied_invalid do.
    • 4096 is Linux's PATH_MAX, so the limit admits every directory a Linux or macOS (1,024) host can resolve, and Windows without long paths (260).
  • Every reader of the two maps in the inbound paths (handle_message and maybe_handle_keyword_command in handler.py, the two sites in src/kiro_crew/slack/transport_dispatch.py, and the !ta, !project and !agent commands in handler_runtime/commands.py) runs after that turn's _hydrate_thread_overrides, so an evicted entry is rehydrated before it is read. The one reader outside the inbound path, _get_agent_for_session, falls back to the default agent for a thread that was evicted before its background turn, the same fallback it takes for every thread after a gateway restart today.
  • Tests: test/test_slack_thread_override_slots.py, thirty-five tests (one of them run for three refusals, one for both resets, one for a reset whose write lands or fails) that drive the real _hydrate_thread_overrides with a fake conversation log as the one seam: with every pin pin admits taken (4,095), 50 new threads hydrate and every thread with an override row is still counted, and the newest keeps its agent; the bound holds; an evicted thread restores both overrides on its next message; a recently active thread is kept over an idle one; one thread hydrated many times adds one entry; a command-set override survives any number of other threads and is released by its reset; 100 pinned threads count inside the bound; through the real _bang_thread_agent, with every slot but two pinned, a thread that two other threads' hydration evicts while !ta awaits its agent lookup is pinned with every container still within THREAD_SLOTS and its hydration guard back; and, through the real _bang_thread_agent with only the agent lookup replaced, !ta in a new thread when every slot is pinned is refused out loud and sets nothing; a 16 MiB agent name and project path in the metadata are not kept, each logged once by length; a value of exactly 4096 characters is kept and one of 4097 is not; a project that is not text is not kept while the agent beside it is; through the real _bang_thread_agent and _resolve_agent_name, !ta naming a project agent whose spec declares a 4,097-character name is refused out loud, pins nothing and sets nothing, while one declaring 4,096 characters is set; and through the real _bang_project, a directory path one character over the limit is refused out loud with a project_denied_too_long audit record that carries no path, while one at the limit is set (no directory path over 4,096 characters can be opened on Linux, so that test lowers the limit to a length real directories under tmp_path straddle); through the real _bang_thread_agent, a thread whose project came from its conversation metadata, evicted by two other threads while !ta awaits its agent lookup, keeps that project, and !project then reports it; through the real _bang_project, a thread evicted after its message hydrated keeps the agent its metadata names; and each refusal the bound adds (!ta with a name one character over the limit, !ta and !project with every pin taken) writes one SEL command record with its outcome, the over-limit one with the name's length and never the name; through the real _bang_thread_agent, !ta off on a thread whose metadata records an execution template stays reset after 4,096 other threads hydrate and the thread's next message hydrates again, with its project kept; through the real _bang_project, !project off whose metadata write fails stays cleared the same way; and !ta off for a thread evicted before it ran, with every pin but one taken, leaves the pinned and evictable threads within THREAD_SLOTS; and, with every pin but one taken and the command's metadata write held open, !ta off and !project off stay reset when another thread's message evicts the thread and the thread's own next message hydrates it again from a read made before the write landed, whether that hydration publishes before the command reads the metadata back or after the command has replied; !ta off and !project off stay reset when such a read is still in flight after the command replied and the thread is evicted again before it publishes; and, through the real _bang_thread_agent run for another thread during each read, a hydration whose every read a command's metadata write overlaps publishes nothing and leaves the thread unhydrated, and the thread's next message hydrates it; through the real _bang_thread_agent, an evicted thread whose hydration before the pin gives up (a !ta off in another thread writes metadata during each of its reads) is left unhydrated by the pin, so its next message reads its project back, and that hydration does not publish the recorded template over the agent !ta set; and !ta off and !project off whose write fails and whose read-back meets a line the store cannot read keep their pin, and eviction after the store recovers does not bring the value back.

Backwards compatibility

Compatible: a gateway that sees up to 4096 threads behaves as before, entry for entry, and so do !ta and !project until 4,095 threads have a choice set in one process. An override value of up to 4096 characters is restored from the metadata as before. A reset made while the store cannot read the thread's metadata line keeps its pin, as a reset that hydration would undo does, so it holds a slot until a later reset reads the line, or until a restart. A thread whose metadata line the store cannot read is not marked hydrated, so each of its messages reads the line again until a read succeeds; on main its first message marks it hydrated with no overrides.
Removes nothing: no setting, field, command or event is removed.
Reader: nothing visible changes in Slack. An evicted thread's next message costs one metadata read, as its first message did.

Tests

Red on origin/main 843abe3 (a throwaway detached worktree with only the new test file copied in), 3 of 3 serial runs, each:

FAILED test/test_slack_thread_override_slots.py::test_new_threads_do_not_grow_the_override_state_without_bound - AssertionError: 4596 hydrated-session keys for 4596 threads; the guard set ...
FAILED test/test_slack_thread_override_slots.py::test_an_evicted_thread_restores_its_overrides_on_its_next_message - AssertionError: the oldest thread was not evicted
FAILED test/test_slack_thread_override_slots.py::test_a_recently_active_thread_is_kept_over_an_idle_one - AssertionError: assert 'slack:C0SYNTH01:1700000001.000100' not in {'slack:C...
FAILED test/test_slack_thread_override_slots.py::test_an_override_set_by_a_command_in_this_process_is_never_evicted - AssertionError: the bound does not exist
FAILED test/test_slack_thread_override_slots.py::test_pinned_threads_count_inside_the_bound - AssertionError: the bound does not exist
FAILED test/test_slack_thread_override_slots.py::test_a_command_past_the_bound_is_refused_out_loud_and_sets_nothing - AssertionError: the bound does not exist
FAILED test/test_slack_thread_override_slots.py::test_hydration_with_every_pin_taken_keeps_no_untracked_row - AssertionError: the bound does not exist
FAILED test/test_slack_thread_override_slots.py::test_hydration_keeps_no_16_mib_override_from_the_metadata - AssertionError: a 16 MiB agent name was kept
FAILED test/test_slack_thread_override_slots.py::test_an_override_at_the_limit_is_kept_and_one_character_over_is_not - AssertionError: assert 'slack:C0SYNTH01:1700000001.000100' not in {'slack:C...
FAILED test/test_slack_thread_override_slots.py::test_a_project_override_that_is_not_text_is_not_kept - TypeError: expected str, bytes or os.PathLike object, not list
FAILED test/test_slack_thread_override_slots.py::test_ta_refuses_a_project_agent_name_over_the_limit_and_sets_nothing - AssertionError: an over-limit agent name was kept
FAILED test/test_slack_thread_override_slots.py::test_project_refuses_a_path_over_the_limit_and_sets_nothing - AttributeError: None has no attribute 'OVERRIDE_VALUE_MAX_CHARS'
12 failed, 1 passed

What each failure pins: the first three, the guard set and both maps grow with every thread, and nothing evicts the oldest or keeps the recently active one; the next four (the bound does not exist), the pin and refusal rules; the next three, a 16 MiB value, a value one character over the limit and a list-valued project are kept from the metadata (the last makes main's hydration raise); the last two, !ta keeps an over-limit project agent name, and !project has no limit to check against. The passing test is the control: one thread hydrated many times adds one entry. On origin/main 7bb9d57, the base of this revision, the whole file (35 tests) gives 34 failed, 1 passed, 3 of 3; the pass is the same control.

Each guard also has a test that fails without it. This file, run against earlier versions of this change that lacked one or more guards, 3 of 3 serial runs each:

Version Result Failures
pins outside the count 7 failed, 6 passed 4196 threads kept with 100 pinned: pins are outside the bound; the refusal test stops at pin, which answers nothing in that version; the five per-value and setter tests fail as on main
count bound, no per-value bound 7 failed, 6 passed the two all-pinned failures in the last row; a 16 MiB agent name was kept; the 4,097-character value kept; the list-valued project raising TypeError; an over-limit agent name was kept; the !project setter test stops at the missing limit constant (AttributeError)
hydration bounded, setters not 4 failed, 9 passed the two all-pinned failures in the last row; an over-limit agent name was kept; an over-limit project path was kept
every slot may be pinned 2 failed, 11 passed a thread past the bound got an override row; 4146 threads keep override rows with 4096 slots and 4096 pinned (50 untracked by the bound)

The last row is the reason for MAX_PINNED_THREADS: with every slot pinned, hydration evicted each new thread it had just added and still wrote that thread's rows.

The new file's autouse _home fixture pins KIROCREW_HOME and KIROCREW_WORKSPACE through the isolation floor's own _floor_monkeypatch, so a test's monkeypatch.undo() cannot lift the pins.

The red block above is the first commit's 13 tests on main. The second commit's fifteen tests, run against the first commit 9195b87 (a throwaway detached worktree with only this test file copied in), 3 of 3 serial runs, each:

E       AssertionError: 4097 threads keep state with 4096 slots after pinning a thread evicted during its command; over the bound: {'_thread_agents': 4097}
E       AssertionError: the thread evicted during !ta lost the project its conversation metadata names: None
E       AssertionError: the thread evicted before !project lost the agent its conversation metadata names: None
E       AssertionError: !ta refused (name_over_limit) with no audit record
E       AssertionError: !ta refused (every_pin_taken) with no audit record
E       AssertionError: !project refused (every_pin_taken) with no audit record
E       AssertionError: eviction undid !ta off: the thread's next message hydrated the recorded template back as its agent ('reviewer')
E       AssertionError: eviction undid !project off: the thread's next message hydrated the old project back ('<tmp>')
E       AssertionError: 4097 threads are tracked with 4096 slots after !ta off on a thread evicted before it ran
E       AssertionError: !ta off replied '🔄 Thread agent reset.' and wrote agent='', yet the thread still runs 'reviewer'
E       AssertionError: !ta off replied '🔄 Thread agent reset.' and wrote agent='', yet the thread still runs 'reviewer'
E       AssertionError: !project off replied 'Thread project cleared.' and wrote project='', yet the thread still has '<tmp>'
E       AssertionError: !ta off replied '🔄 Thread agent reset.'; a read from before its write, published after a later eviction, left the thread running 'reviewer'
E       AssertionError: !project off replied 'Thread project cleared.'; a read from before its write, published after a later eviction, left the thread with '<tmp>'
E       AssertionError: hydration published 'reviewer' from a read that a command's metadata write overlapped (reads: 1)
15 failed, 13 deselected

What each failure pins: the first, pin admitted a thread evicted during its command without evicting another, so the containers held THREAD_SLOTS + 1 threads; the next two, a thread evicted before its pin lost the other override its conversation metadata names, !ta its project and !project its agent; the next three, the refusals the bound adds wrote no SEL record; the next two, eviction undid a reset that hydration reads back, !ta off on a thread that records an execution template and !project off whose write failed; the ninth, unpin tracked a thread that !ta off reached after it was evicted, with no pin to release, outside the bound; the next three, !ta off and !project off replied that the reset was done while the value that a hydration published from a read made before the write landed stayed live; the next two, a read made before the write and still in flight when the command replied published the old value after the thread was evicted again; the last, hydration published a read that a command's metadata write overlapped. The five tests after the first also fail, with the same messages, 3 of 3, on this branch's previous head 278bddc, which had the first fix but not the other two. The nine after the refusal records fail, with the same messages, 3 of 3, on the previous head d42d0f6 (9 failed, 19 passed). Each of the two mechanisms behind the last six has its own failing test, 3 of 3 serial runs each. On this branch without the write count (no bump, no read again) the last three fail (3 failed, 25 passed). With only the hydration before the release taken out, the two race tests whose hydration publishes before the command reads the metadata back fail (2 failed, 26 passed); the one whose read publishes after the reply passes there, because the count already covers it. Adding a bump before the write to that variant changes nothing (2 failed, 26 passed). The two !ta race tests drive the real _bang_thread_agent: the agent lookup it awaits runs in a worker thread, which hydrates two other threads on the loop before it answers, so the command's own thread is evicted before pin admits it. The three reset race tests hold the command's metadata write on a gate in its worker thread, hydrate another thread and then this one on the loop, and only then open the gate; in the second, this thread's read waits on a second gate until the command has replied. The two later-eviction tests do the same as the second and, after the reply, hydrate a third thread before they open the read's gate. The give-up test's fake log runs a real !ta off for another thread on the loop, through run_coroutine_threadsafe, from inside each read of the thread under test. No test sleeps: each waits on events or futures, within a 10-second bound.

The seven newest cases, run against the previous head 97a10d4 (rebased onto 7bb9d57 as 645315d969, same patch-id; a throwaway detached worktree with only this test file copied in), 3 of 3 serial runs, each:

FAILED test_ta_on_an_evicted_thread_whose_hydration_gives_up_reads_its_project_next - AssertionError: the pin marked the thread hydrated after its hydration gave...
FAILED test_a_reset_whose_metadata_cannot_be_read_back_keeps_its_pin[agent] - AssertionError: !ta off replied '🔄 Thread agent reset.' while the store wa...
FAILED test_a_reset_whose_metadata_cannot_be_read_back_keeps_its_pin[project] - AssertionError: !project off replied 'Thread project cleared.' while the st...
FAILED test_ta_on_an_evicted_thread_while_its_metadata_cannot_be_read_reads_its_project_next - AssertionError: a hydration that could not read the metadata marked the thr...
FAILED test_ta_off_while_its_metadata_cannot_be_read_keeps_its_project_after_recovery - AssertionError: the reset released a pin it could not verify
FAILED test_a_reset_that_cannot_be_held_or_read_back_promises_nothing_about_the_value[True] - AssertionError: 🔄 Thread agent reset.
FAILED test_a_reset_that_cannot_be_held_or_read_back_promises_nothing_about_the_value[False] - AssertionError: 🔄 Thread agent reset.
7 failed, 28 deselected

The last four are this revision's. In the two while_its_metadata_cannot_be_read cases the store cannot read the thread's line while !ta <name> or !ta off runs on an evicted thread and then recovers: the thread's next message must read its project back. With hydration reading through get_metadata again, both fail with a hydration that could not read the metadata marked the thread hydrated (True) .... The two promises_nothing cases take a reset that cannot be held or read back: when the clear's write lands, the old agent never comes back; when it fails on a thread evicted before the reset, it comes back with the thread's next message, with no eviction. With the reply's former wording, that the value "comes back if it is evicted", the first fails with the reply ties the agent's return to an eviction, but here it never came back. With an unreadable hydration read marked hydrated again (one change, on this branch), the two while_its_metadata_cannot_be_read cases and the second promises_nothing case fail, 3 failed, 32 passed, 3 of 3.

Each guard has its own failing case, 3 of 3 serial runs each, on this branch with one change: with pin marking the thread hydrated again, the first case fails as above (1 failed, 2 passed); without hydration's per-kind skip, it fails at the agent, the thread's next message hydrated the recorded template over the agent !ta set: 'reviewer' (1 failed, 2 passed); with the resets reading back through _read_thread_overrides and get_metadata again, both reset cases fail as above (2 failed, 1 passed). The first case runs a real !ta off for another thread on the loop, through run_coroutine_threadsafe, from inside each of the three reads; the store in the other two raises on a write and answers a read as the real conversation log answers an unreadable line, {} from get_metadata and ({}, False) from get_metadata_status.

Green on the branch (on origin/main 7bb9d57): 35 passed, in three serial runs. The control (one thread hydrated many times adds one entry) passes on both sides. test/test_slack_thread_override_slots.py first, then the 16 Slack handler test files that import thread_override_slots, handler_runtime.inbound or handler_runtime.commands (including test_thread_override_persistence.py and the existing !ta off and !project off tests), run serially: 1,109 passed. Their conversation-log fakes now answer get_metadata_status as the real log does, with the same metadata reported readable. Three golden transcripts in test/test_slack_handler_composition_contract.py were re-recorded. Their fake now answers get_metadata_status, as the real log does (history.py:3913), so the naming turn's record read (messaging/auto_title.py:281, reached from slack/handler.py:2913 and :3243) reads the fake's metadata. On the old fake that read raised AttributeError and took its unknown-record branch, which the real log never does. The transcripts gain only those reads: two in linked_dashboard_session, two in pinned_answer, one in turn_with_history. mypy, flake8, isort, black, scripts/check_black_formatting.py, scripts/check_subprocess_encoding.py, scripts/check_agent_sdk_boundary.py, scripts/docs-lint.sh and the comment-history check with COMMENT_HISTORY_BASE_REF=origin/main exit 0.

The Not a goal section's line numbers are the first revision's. On this branch the two pin calls are at slack/handler_runtime/commands.py:765 and :926, and pin's refusal check is at slack/thread_override_slots.py:214.

Manual verification

Not run against a live Slack workspace.

Related Issues

None found for the growth. #8715 (open) is about races in the agent-override persistence and does not change with this PR. Open PRs that edit handler_runtime/inbound.py or handler_runtime/commands.py, checked by hunk: #15273 edits the import block and maybe_route_linked_thread in inbound.py; #14089 edits _bang_stop and #11398 edits _handle_slash_command in commands.py. None of them touches _hydrate_thread_overrides, _bang_thread_agent or _bang_project, where this change lives.

Pattern harvest

Rule candidate: review-prompt
Pattern: a module-level map keyed by a conversation identifier with no eviction path, or a guard set that gates a re-read but is not evicted together with the state it guards.

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

@javenciu
javenciu requested review from a team and KiroCrewCaptain as code owners October 11, 2026 00:27
@javenciu
javenciu requested a review from bolichen97 October 11, 2026 00:27
@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 Oct 11, 2026
@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5, fork) — ⏭️ skipped

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

@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5, fork) — 🟡 CONCERNS

Design-level review of b5ce8924940ac6c1ca322c0c274ddec2e28665f3 via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: CONCERNS

The PR caps a cache of roughly 125 bytes per thread, but the real problem is that in-memory overrides can say something the persisted metadata doesn't, and it stays unfixed under a 292-line concurrency layer.

Watch

  • The root cause stays in place. The description says hydration "cannot be relied on to read these choices back: the command's metadata write is skipped when there is no conversation log and only logged at debug when it fails, and hydration prefers a recorded execution template over the agent field."
    • That gap is the reason for pins, note_command_write counters, HYDRATION_READ_ATTEMPTS retry loops, the re-hydrate-before-pin step and read_back on resets.
    • Each of those was added to close a race the previous one exposed, as the description's "restructures the two rules that kept drawing findings on the same spans" admits.
    • Result: anyone who touches hydration or the bang commands later has to keep about six interlocking ordering rules intact (for example "no await between hydrate and pin", or "note the write before any further await"). These are spread across commands.py and inbound.py.
    • If metadata were authoritative (the command's choice persisted and preferred by hydration, including a cleared value over a template), eviction would be plain LRU with no pins and no write counter.
    • Clears when: the description weighs making the persisted override authoritative, so eviction needs no pins, and says why it was rejected; or the fix is restructured that way.
  • A new user-facing refusal. Once 4,095 threads are pinned, !ta/!project reply "❌ Not set". An unheld !ta off/!project off replies "reset for now … can come back". Today both commands always succeed. This is reversible, but it is a new failure mode that users will see, added to fix a memory cost of a few MB.
    • Clears when: the description's Backwards compatibility section names this behaviour change and why it is acceptable.

Suggestions

  • Bounding only _hydrated_sessions, and dropping all three entries together when the idle sweep ends the session, would tie their lifetime to an existing event instead of a separate LRU.

[DESIGN-REVIEWED] b5ce892

Description read

[DESCRIPTION-READ] 7595160b7d550d9ded4e1b20e112775dcaff0679ccb14b6965997023c1227f16

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 11, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5, fork) — 🟡 CONCERNS

Premise-level review of b5ce8924940ac6c1ca322c0c274ddec2e28665f3 via the fork AI-review pipeline — 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: CONCERNS

The per-value length refusals and the setter-path write counter guard against harms that, by the description's own words and the diff's comments, are already covered.

Not justified as shipped

  • Item 3 — oversized: the !ta/!project length refusals guard values that must already resolve to an existing agent or directory. The description admits that "in practice this refuses a Windows long path, or a project agent whose spec declares a name that long."
    Subtraction: delete the two setter_refusal branches, the *_denied_too_long SEL outcomes and setter_refusal. Keep bounded_override on hydration, where the metadata is unbounded input.
    Clears when: a reported case of an over-limit name or path set through a command is linked, or the branches are deleted.
  • Item 4 — duplicate of kind_is_pinned (thread_override_slots.py): on the two setter paths, the diff's own comment says "The pin above already keeps a stale read off this thread; counting the write keeps the hydration rule independent of the pin." That makes the counter a second spelling of the same protection.
    Subtraction: delete the note_command_write() calls after the !ta <name> and !project <path> writes, and keep only the reset-path calls.
    Clears when: a test is linked that fails with only those two calls removed, or the calls are deleted.

What this change ships

Inventory (8 items) — 6 justified

Intent: stop the Slack gateway's per-thread override cache from growing for the life of the process. This is a FIX.

  1. The hydration guard plus the agent and project maps are held to 4,096 threads, oldest evicted first, and an evicted thread re-reads its metadata — justified
  2. Command-set overrides are pinned, and a 4,096th pin is refused with a reply — justified
  3. !ta/!project refuse a name or path over 4,096 characters — oversized
  4. A process-wide write counter makes hydration re-read up to 3 times — duplicate of kind_is_pinned on setter paths
  5. !ta off/!project off read the metadata back, may keep a pin, and give a new "reset for now" reply — justified
  6. An unreadable metadata line leaves the thread unhydrated, so it is re-read on each message — justified
  7. Background readers keep a thread resident — justified
  8. New SEL outcomes *_denied_capacity and *_denied_too_long — undeclared

[FIRST-PRINCIPLES-REVIEWED] b5ce892

Description read

[DESCRIPTION-READ] 7cad650013f3ae0f6214bd8e39137184c7b7cff36669c7c881c735d477c687a7

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 11, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review (fork) — ✅ no blocking findings

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

Review details

No findings.

[OPUS-REVIEWED] b5ce892

@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review (fork) — ✅ no blocking findings

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

Review details

No findings.
[GPT-REVIEWED] b5ce892

@javenciu

Copy link
Copy Markdown
Contributor Author

Thanks, the finding holds. Fixed in a second commit, 278bddc, on top of 1094535.

What was wrong. !ta and !project await the agent lookup or the directory check before they call pin. Threads hydrating during that await can evict the command's own thread. pin (slack/thread_override_slots.py:148 at 1094535) then added the pin and dropped the thread from _order without evicting anything, so the pins and the unpinned threads together held THREAD_SLOTS + 1 threads. The thread also kept no hydration guard, so its next message would hydrate over the override the command had just set.

The change.

  • pin (slack/thread_override_slots.py:150) takes the shared containers, as note_hydrated does. Both callers pass the guard and both maps (slack/handler_runtime/commands.py:725 and slack/handler_runtime/commands.py:845).
  • It puts the thread back in the hydration guard (slack/thread_override_slots.py:171).
  • It then calls _evict_over_bound (slack/thread_override_slots.py:172), which evicts the oldest unpinned threads from the guard and both maps until the pins and the rest fit THREAD_SLOTS. That happens before the command writes its row (_thread_agents, slack/handler_runtime/commands.py:731; _thread_projects, slack/handler_runtime/commands.py:851).
  • The eviction is the loop note_hydrated already ran, moved into one helper, _evict_over_bound (slack/thread_override_slots.py:117), which both call (slack/thread_override_slots.py:147 and slack/thread_override_slots.py:172). So an evicted thread leaves the guard and both maps together on either path.

Red. test_pinning_a_thread_evicted_during_its_command_keeps_the_bound reproduces your trace through the real _bang_thread_agent. Every slot but two is pinned. The agent lookup it awaits runs in a worker thread, which hydrates two other threads on the loop before it answers, so the command's own thread is evicted before pin admits it. Against 1094535 (only the test file copied in), 3 of 3 runs:

E       AssertionError: 4097 threads keep state with 4096 slots after pinning a thread evicted during its command; over the bound: {'_thread_agents': 4097}

Green. On 278bddc the test file passes, 14 tests, 3 of 3 runs. The new test checks that each container stays within THREAD_SLOTS, that their union does too, and that the pinned thread is back in the guard. The 17 Slack handler test files from the description, run serially: 1,088 passed. mypy, flake8, isort, black, scripts/check_black_formatting.py and the comment-history check with COMMENT_HISTORY_BASE_REF=origin/main exit 0.

@javenciu
javenciu force-pushed the fix/slack-thread-override-maps-are-bounded branch from 278bddc to d42d0f6 Compare October 11, 2026 06:39
@javenciu

Copy link
Copy Markdown
Contributor Author

Thanks, both findings hold. Fixed by amending the second commit, now d42d0f6; both commits are rebased onto main 843abe3, and the first, now 9195b87, has the same patch.

F1 (slack/thread_override_slots.py:171 on 278bddc). pin put a thread evicted during its command back in the guard without the other override its conversation metadata names. Both commands now run _hydrate_thread_overrides just before they pin, with no await in between (slack/handler_runtime/commands.py:734 and slack/handler_runtime/commands.py:865). A thread still in the guard returns at once. An evicted one reads both overrides back, through note_hydrated's own capacity loop, and keeps any value set while the read ran (slack/handler_runtime/inbound.py:236-242), before pin marks it hydrated (slack/thread_override_slots.py:174). Red on 278bddc, 3 of 3: test_ta_on_a_thread_evicted_during_its_lookup_keeps_its_restored_project (your trace through the real _bang_thread_agent, then !project reports the project) and test_project_on_an_evicted_thread_keeps_its_restored_agent (the other call site) fail with lost the project ...: None and lost the agent ...: None.

F2 (slack/handler_runtime/commands.py:848 on 278bddc). The three refusals now write a SEL command record, as the other !project refusals do, with the reason as the outcome: agent_denied_too_long with the name's length, never the name (slack/handler_runtime/commands.py:727), agent_denied_capacity (slack/handler_runtime/commands.py:746) and project_denied_capacity (slack/handler_runtime/commands.py:877). Red on 278bddc, 3 of 3: test_each_refusal_writes_an_audit_record_without_the_over_limit_value, one case per refusal, each refused ... with no audit record. The unknown-agent refusal (slack/handler_runtime/commands.py:709-714) is unaudited on main as well; this PR leaves it unchanged.

Green: the test file passes 19 tests, 3 of 3; the 17 Slack handler test files from the description: 1,093 passed.

@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 labels Oct 11, 2026
@javenciu
javenciu force-pushed the fix/slack-thread-override-maps-are-bounded branch from d42d0f6 to 97a10d4 Compare October 11, 2026 10:05
@javenciu

Copy link
Copy Markdown
Contributor Author

Thanks, both findings hold. Fixed by amending the second commit, now 97a10d4. The first, 9195b87, is unchanged, and both are still on main 843abe3.

F1 (slack/handler_runtime/commands.py:686 on d42d0f6). Hydration prefers a recorded execution template over the agent field !ta off clears (messaging/session_resume.py:99-104), so evicting a cron-created thread brought the template's agent back. !ta off no longer releases the pin unconditionally. After it clears the agent and writes the metadata, it asks _read_thread_overrides what hydration would read back (slack/handler_runtime/commands.py:700). Then, on either branch, the thread hydrates again and its agent is cleared again, with no await after the hydration (slack/handler_runtime/commands.py:707-708). A thread that would get an agent back is then pinned with no agent set (:711). It is never evicted or re-hydrated in this process, the same as on main, where nothing evicts it before a restart. The member and store identity in the execution record is untouched. Any other thread releases its pin (slack/handler_runtime/commands.py:715). The hydration before the release covers a thread evicted while !ta off awaited its write and hydrated again by its own next message from a read made before the write landed: the agent that read published is cleared before the reply. !project off uses the same read-back and the same order, which covers a clear whose metadata write failed (slack/handler_runtime/commands.py:845-858). The reset pin counts inside MAX_PINNED_THREADS. When every pin is taken, the reset still applies, and the reply says it lasts until the thread is evicted (unheld_reset_text, slack/thread_override_slots.py:222). Red on d42d0f6, 3 of 3: test_ta_off_on_a_thread_with_a_recorded_template_stays_reset_after_eviction (your trace through the real _bang_thread_agent, a recorded template, then 4,096 other threads) fails with eviction undid !ta off: ... ('reviewer'). test_project_off_whose_write_failed_stays_cleared_after_eviction fails the same way for the project. The three reset race tests (test_ta_off_stays_reset_when_its_thread_hydrates_again_during_the_write, test_ta_off_stays_reset_when_a_read_from_before_its_write_publishes_after_it and test_project_off_stays_cleared_when_its_thread_hydrates_again_during_the_write) fail there too, with !ta off replied '🔄 Thread agent reset.' and wrote agent='', yet the thread still runs 'reviewer' and its !project off form.

The class behind F1. Each instance so far has been one class: a hydration read that began before a command's metadata write publishes the old value after it, because eviction takes the thread out of the hydration guard that on main stops such a read. So hydration now publishes nothing it read before a newer !ta or !project metadata write. The four commands count their writes in one process-wide integer (_command_writes, slack/thread_override_slots.py:69). Each calls note_command_write as soon as its write returns or raises and before it awaits anything else (slack/handler_runtime/commands.py:696, :787, :842, :945). _hydrate_thread_overrides compares the count before and after each read and reads again when it moved, at most HYDRATION_READ_ATTEMPTS (3) times (slack/handler_runtime/inbound.py:241-251). If every read overlapped a write, it publishes nothing, does not mark the thread hydrated, and logs a warning, so the thread's next message hydrates it (:252-259). There is no bump before the write: a stale read began before the write landed, so its count precedes the bump. If it is back after the bump, it reads again; if it is back before, it publishes before the command's own hydrate-and-clear removes the value. Red on d42d0f6, 3 of 3, and on this branch without the count (3 failed, 25 passed): test_ta_off_stays_reset_when_a_stale_read_publishes_after_a_later_eviction and its !project off form, where a read still in flight after the reply publishes once the thread is evicted again (... published after a later eviction, left the thread running 'reviewer'), and test_hydration_publishes_nothing_while_every_read_overlaps_a_command_write, where a real !ta off in another thread lands during each read (hydration published 'reviewer' from a read that a command's metadata write overlapped).

F2 (slack/thread_override_slots.py:191 on d42d0f6). unpin now returns a thread to the evictable order only when it releases that thread's last existing pin. Otherwise it only touches a thread that is still tracked, so it never re-creates an entry for a thread that was evicted and never pinned (slack/thread_override_slots.py:245-250). A pinned thread is never evicted, so the thread whose last pin is released is still in the guard, and the pins plus the evictable threads stay within THREAD_SLOTS. Red on d42d0f6, 3 of 3: test_ta_off_for_a_thread_evicted_before_it_runs_tracks_no_extra_thread (4,095 pins, the thread evicted before !ta off runs) fails with 4097 threads are tracked with 4096 slots.

Green on 97a10d4: the file's 28 tests pass 3 of 3, and the second commit's fifteen tests fail on the first commit 9195b87, 3 of 3.

The Slack handler kept one entry per thread, for the life of the process,
in the hydration guard (_hydrated_sessions) and the !ta / !project maps
(_thread_agents, _thread_projects). Only the reset commands removed an
override, and nothing removed a guard entry, so a gateway that answered
many threads never gave the memory back.

The three containers are now held to THREAD_SLOTS threads together, in the
least-recently-used shape the thread-replies watermark already uses, and
THREAD_SLOTS is that watermark's own bound, not a second literal. An
evicted thread loses its guard entry and both overrides at once, so its
next message hydrates again and reads the overrides back from the
conversation metadata. An override a person set with !ta or !project in
this process is pinned and never evicted while it is set, because its
metadata write is best-effort and hydration prefers a recorded execution
template over the agent field.

Pinned threads count inside THREAD_SLOTS, at most MAX_PINNED_THREADS
(THREAD_SLOTS - 1) of them, so one slot always stays evictable and
hydrating a new thread never evicts that thread itself. A !ta or !project
that would pin one more thread past that is refused with a reply that
says so, and sets nothing.

Each agent name and project path the maps keep is bounded by
OVERRIDE_VALUE_MAX_CHARS (4096, Linux's PATH_MAX), whichever path writes
it. Hydration keeps a value it reads back from the conversation metadata
only when it is text within the limit, and skips any other with one
warning that names its length or type. !ta and !project refuse a longer
value before they pin or write anything, with a reply that names its
length and the limit: a project agent's name is whatever its spec
declares, and a Windows long path can pass 4096. So the maps keep at most
THREAD_SLOTS threads x 2 values x 4096 characters.
`!ta` and `!project` await agent discovery or a directory check before
they pin. Threads hydrating meanwhile can evict the command's own
thread, and pin() then admitted it without evicting anything, so the
pins and the unpinned threads together held THREAD_SLOTS + 1 threads,
and the evicted thread had no hydration guard, so its next message
hydrated over the override the command had just set.

pin() now takes the shared containers, as note_hydrated does. It evicts
the oldest unpinned threads from the guard and both override maps until
the pins and the rest fit THREAD_SLOTS, through the same loop
note_hydrated uses (_evict_over_bound), before the command publishes its
override. It does not mark the thread hydrated: only a hydration whose
read the store reports as readable does. Hydration reads the line once
through thread_override_slots.read_back (get_metadata_status), and a line
it cannot read leaves the thread unhydrated, so its next message reads it
again; a line that stays unreadable is read on each message. Hydration
never publishes over a kind a command
pinned instead (thread_override_slots.kind_is_pinned), so a pinned
thread's next message cannot hydrate over its override.

An evicted thread also lost its other override: a project or agent
restored from the conversation metadata. Both commands now run
_hydrate_thread_overrides just before pin(), with no await between
them, so an evicted thread reads both overrides back, keeping any value
set meanwhile. If that hydration gives up, the thread stays unhydrated,
and its next message reads the other override back.

The refusals this bound adds now write a SEL command record, as the
other `!project` refusals do: agent_denied_too_long (with the length,
never the name), agent_denied_capacity and project_denied_capacity.

A reset could be undone by eviction. Hydration prefers a recorded
execution template over the `agent` field `!ta off` clears, and reads
the old value back when the clear's metadata write fails. So after the
clear, `!ta off` and `!project off` read back what hydration would
restore (thread_override_slots.read_back). They read the line through
get_metadata_status, because get_metadata answers a line the store could
not read with empty metadata, the same as one with nothing set. A thread
that would get a value back, or whose line cannot be read back, keeps a
pin with nothing set, so it is never evicted in this process, as on
main. Any other thread releases its pin. When every pin is taken, the
reset applies and the reply says the recorded value can come back.

Either way the command first hydrates the thread again and clears its
value again, with no await after the hydration. A thread evicted while
the command awaited its write can be hydrated by its own next message
from a read made before the write landed; the release branch then
replied that the reset was done while the old value stayed live.

Eviction also lets such a read publish later: one still in flight after
the command replied, for a thread evicted again by then, found it
outside the guard and published the old value. Hydration now publishes
nothing it read before a newer `!ta` or `!project` metadata write. The
commands count their writes in one process-wide integer
(thread_override_slots.note_command_write), each as soon as its write
returns; hydration compares the count before and after its read and
reads again when it moved, at most HYDRATION_READ_ATTEMPTS (3) times.
If every read overlapped a write, it publishes nothing and leaves the
thread unhydrated, so the thread's next message hydrates it.

unpin() put any thread back in the evictable order, so `!ta off` on a
thread evicted while the command awaited, one that held no pin, tracked
it again without evicting anything: THREAD_SLOTS + 1 threads. It now
re-tracks a thread only when it releases that thread's last pin, and
otherwise only touches a tracked one.
@javenciu
javenciu force-pushed the fix/slack-thread-override-maps-are-bounded branch from 97a10d4 to b5ce892 Compare October 11, 2026 19:03
@javenciu

Copy link
Copy Markdown
Contributor Author

span=25bf41b83f59 (GPT 6.1 F1, slack/thread_override_slots.py:209 on 97a10d4): fixed for both ways a hydration can end without reading the metadata, not only the one named. pin no longer adds the thread to the hydration guard (slack/thread_override_slots.py:194); only a hydration whose read the store reports as readable marks a thread hydrated. Hydration now reads the metadata line once through read_back (slack/thread_override_slots.py:258), which uses get_metadata_status. A line it cannot read returns before _hydrate_thread_overrides marks the thread (slack/handler_runtime/inbound.py:253), as a hydration that gave up already does. Hydration never publishes over a kind a command pinned (kind_is_pinned, slack/thread_override_slots.py:105, checked at slack/handler_runtime/inbound.py:274-279). So when the hydration that !ta or !project runs just before its pin gives up, or meets a line the store cannot read, the thread stays out of the guard, and its next message reads the persisted project back. The cost is one read per message for a thread whose line stays unreadable. test_ta_on_an_evicted_thread_whose_hydration_gives_up_reads_its_project_next, test_ta_on_an_evicted_thread_while_its_metadata_cannot_be_read_reads_its_project_next and, for the pin a reset keeps, test_ta_off_while_its_metadata_cannot_be_read_keeps_its_project_after_recovery fail on 97a10d4 and pass here, 3 of 3. Marking a thread hydrated after an unreadable read again fails the last two.

span=9c152b799da0 (GPT 6.1 F2, slack/handler_runtime/commands.py:858 on 97a10d4): fixed. !ta off and !project off read back through the same read_back. A read the store reports as unreadable, or one that raises, returns None and keeps the pin (slack/handler_runtime/commands.py:711 for !ta off, :856 for !project off); only a readable read that shows nothing to restore releases it (:716, :861). When every pin is taken the reset cannot be held, and its reply now says only that the recorded value can come back (unheld_reset_text, slack/thread_override_slots.py:230). The earlier wording, that it comes back if the thread is evicted, was wrong both ways: when the clear's write landed nothing comes back, and when it failed on a thread that was not hydrated, the value returns with the thread's next message. test_a_reset_whose_metadata_cannot_be_read_back_keeps_its_pin (both resets) and test_a_reset_that_cannot_be_held_or_read_back_promises_nothing_about_the_value (the write lands, or fails) fail on 97a10d4 and pass here, 3 of 3.

Line references in Not a goal moved with later revisions: the pin refusal it cites as thread_override_slots.py:140-158 is now src/kiro_crew/slack/thread_override_slots.py:214, and the two refusing commands it cites as handler_runtime/commands.py:724 and :842 are now src/kiro_crew/slack/handler_runtime/commands.py:765 (!ta) and :926 (!project). That section is not edited once a pull request is open.

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 11, 2026

@bolichen97 bolichen97 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.

Approved at head b5ce892: PR Readiness is green, no open change requests, and the review lanes passed. (AI-assisted review at bolichen97's request.)

@bolichen97
bolichen97 merged commit b3da4dc into kirodotdev:main Oct 12, 2026
107 checks passed
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 12, 2026
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.

2 participants