diff --git a/docs/architecture/mcp.md b/docs/architecture/mcp.md index 3ba0fc583d7b..28619021967c 100644 --- a/docs/architecture/mcp.md +++ b/docs/architecture/mcp.md @@ -1362,7 +1362,7 @@ Managed servers, registered by `agent._MANAGED_MCP_SERVERS` and installed into | `kirocrew-cron` | `kirocrew mcp-cron` (`mcp_cron.py`) | `cron_add`, `cron_list`, `cron_update`, `cron_remove`, `cron_remove_all`, `cron_pause`, `cron_resume`, `cron_trigger`, `cron_secret_request` | | `kirocrew-core` | `kirocrew mcp-core` (`mcp_core.py` + `mcp_tools/`) | spawn/subagent, learn, task, messaging, artifact, workflow, knowledge and session-directive tools (see below) | | `kirocrew-computer` | `kirocrew mcp-computer` (`mcp_computer.py`) | `computer_list_apps`, `computer_launch_app`, `computer_get_state`, `computer_click`, `computer_drag`, `computer_type_text`, `computer_press_key`, `computer_set_value`, `computer_scroll`, `computer_perform_action`, `computer_end_turn` | -| `kirocrew-dashboard` | `kirocrew mcp-dashboard` (`mcp_dashboard.py`) | `chat_folder_tree`, `chat_folder_create`, `chat_folder_move`, `chat_folder_move_session`, `chat_folder_file_self`, `chat_tag_list`, `chat_tag_create`, `chat_tag_update`, `chat_tag_assign`, `chat_tag_column_list`, `chat_tag_column_create`, `chat_tag_column_move`, `chat_session_pin`, `session_create`, `session_fork`, `session_stop`, `session_end_wait`, `session_set_model`, `session_reload`, `session_close`, `session_revive`, `session_send`, `session_broadcast`, `session_status`, `session_adopt`, `session_release`, `session_read_message`, `session_summary` | +| `kirocrew-dashboard` | `kirocrew mcp-dashboard` (`mcp_dashboard.py`) | `chat_folder_tree`, `chat_folder_create`, `chat_folder_delete`, `chat_folder_move`, `chat_folder_move_session`, `chat_folder_file_self`, `chat_tag_list`, `chat_tag_create`, `chat_tag_update`, `chat_tag_assign`, `chat_tag_column_list`, `chat_tag_column_create`, `chat_tag_column_move`, `chat_session_pin`, `session_create`, `session_fork`, `session_stop`, `session_end_wait`, `session_set_model`, `session_reload`, `session_close`, `session_revive`, `session_send`, `session_broadcast`, `session_status`, `session_adopt`, `session_release`, `session_read_message`, `session_summary` | | `kirocrew-work` | `kirocrew mcp-work` (`mcp_work.py`) | `work_brief`, `work_report`, `work_ledger_read`, `work_ledger_record` | | `kirocrew-crew-log` | `kirocrew mcp-crew-log` (`mcp_crew_log.py`) | `crew_log_list`, `crew_log_read`, `crew_log_projection` | | `kirocrew-debug` | `kirocrew mcp-debug` (`mcp_debug.py`) | `debug_gateway`, `debug_refusals`, `debug_threads`, `debug_processes`, `debug_snapshots` | @@ -1700,11 +1700,13 @@ neither sharing a lock with it -- so "is this folder empty?" cannot be establish atomically with the removal. Successively narrower rules each leaked through another seam: a session filed while the archive scan awaited, a child created while the lock was acquired, a session closing after the scan and writing its `folder_id` -on the way out. Each was closable alone; the class was not, so the verb is withheld -instead. Nothing shipped loses a capability -- no MCP tool exposes deletion and the -only client of the route is the dashboard UI -- and an app organizes its work by -creating, renaming and reparenting its own folders and filing its own sessions. The -person deletes a full folder exactly as before. +on the way out. Each was closable alone; the class was not, so the verb is refused +for every agent principal instead. The person's own sessions delete a full folder +exactly as before -- through the dashboard UI, or through `chat_folder_delete`, a +thin wrapper over the same route under the caller's own session identity, so an app +agent's call gets the endpoint's refusal and a crew member's is refused by the +member gate (no DELETE is admitted on the folder routes). An app organizes its work +by creating, renaming and reparenting its own folders and filing its own sessions. The policy lives in the endpoints, not in the MCP server. Only the endpoint holds the store lock and sees the authoritative tree, so a second copy of the rule in diff --git a/docs/system-specs/modules/learn-cron-dashboard.md b/docs/system-specs/modules/learn-cron-dashboard.md index 9964e4dfcf92..720d50536d54 100644 --- a/docs/system-specs/modules/learn-cron-dashboard.md +++ b/docs/system-specs/modules/learn-cron-dashboard.md @@ -2825,7 +2825,7 @@ never rereads message history or overwrites a newer hydration. **Context meter survives a reopen**: `context_usage` WS frames are turn-scoped, so a session opened in a tab that did not witness its turns had no usage to render and the bar sat at 0% until the next message. `GET /api/chat/slots/{slot}` therefore carries `{context_pct, context_stale, context_window_tokens?, context_used_tokens?}`, computed by `_context_snapshot_fields` in two tiers: a provider still resident in the session pool is authoritative, and a session whose ACP process is gone (idle timeout or a gateway restart) falls back to a stored snapshot flagged `context_stale`. **`context_pct` is the load-bearing field and the token counts are optional enrichment** — kiro-cli commonly reports `contextUsagePercentage` with no `usage_update`, so a resident session routinely knows it is 13% full while knowing neither token count; gating the reading on a known window no-ops the feature for the majority of sessions, and the frontend already supplies a model-derived window. Fields are OMITTED, never zeroed, when there is no reading at all — a 0% pct with no window is indistinguishable from a session that never had a turn, and both render an empty bar, so it is reported as absent rather than as a measurement. A **stale** reading additionally withholds `context_used_tokens`: no process measured that count, so shipping it would make any consumer render a never-measured figure as measured; the tooltip derives a `~` approximation from pct instead. A snapshot whose recorded model differs from the slot's current one is discarded, because its pct and counts are denominated in the old model's window. Every failure path degrades to `{}`: the fields ride on the response that carries the whole transcript, so a non-numeric provider reading is gated by `_finite_number` and any exception is swallowed rather than turning a display nicety into a 500 that blanks the conversation. `DashboardState.broadcast_context_usage(slot_key, payload)` is the SINGLE writer for both the frame and the snapshot — every producer (end-of-turn, both compaction paths, the auto-compact callback, and the model-switch/session reset) routes through it, it records a pct-only frame on its own, and an unchanged reading leaves the map clean so the next flush has nothing to write. A post-compaction frame legitimately records `pct: 0`: that is the new truth, not an absence. **All snapshot file IO is off the event loop**, which is what the `no-blocking-call-on-event-loop` rule requires and what `_persist_open_slots` already does for its own sidecar: `broadcast_context_usage` only mutates the in-memory map and sets a dirty flag, `_persist_context_snapshots` runs from `_flush_dirty_slots` (the periodic executor pass) plus once more on graceful shutdown so a restart — the case the reopen seed exists to serve — does not lose the last reading, and the first disk read reaches `ensure_context_snapshots_loaded` through `asyncio.to_thread` from the async slot-detail handler. Deferring the write puts the map on a thread boundary, so every access — event-loop writers, the flush executor, the shutdown thread, the handler's copy-out read (`context_snapshot_for`) — holds `_context_snapshots_lock`; the lock covers dict work and serialization only, never the file write, so a stalled disk cannot block the loop's writers. The loaded flag flips only after the disk merge is in the map, so a concurrent flush can never write a memory-only view over prior-process readings it has not merged; disk fills gaps and never overwrites a live entry. Any flush failure re-arms the dirty flag and is swallowed — the flush loop treats a raising callee as fatal, and the next 5s pass retries. Snapshots live in `/context_snapshots.json` (slot key → `{pct, model, window_tokens?, used_tokens?}`, `0o600`, pruned to the open-slot set on every write) and NOT in the session's metadata line: `ConversationLog.update_metadata` rewrites the whole transcript to edit its first line, so paying that per turn would scale a turn's I/O with transcript size while holding the cross-process lock. Ephemeral (incognito/temporary) slots are excluded, matching `_persist_open_slots`. Frontend: `seedContextUsage` in `website/src/store/chat/slotCache.ts` is the single hydration path for all three slot-detail reducers (`switchSlot`/`refreshSlot`/`warmSlotCache`) and seeds ONLY a slot with no entry yet — the server broadcasts over WS before the HTTP response lands, so an unconditional write would clobber a live measurement with the older snapshot the request was built from. A token entry is written only when a window is actually known. **Follow-up suggestions** (`suggest_followup` MCP tool → card above the composer): POST `/api/chat/slots/{slot}/followup` with `{items: [{title, description, prompt, branch?}]}` — broadcast-only (nothing persisted server-side), 404 unknown slot, re-validates `SUGGEST_FOLLOWUP_SCHEMA` server-side (≤3 items; title ≤120, description ≤600, prompt ≤8000, branch ≤80 chars full-matching `FOLLOWUP_BRANCH_RE`; hidden-Unicode stripped), redacts every field through `redact_exfiltration_urls` + `redact_credentials` — including `branch`, which is DROPPED rather than mangled when redaction alters it — and emits `followup_card` over WS. The card goes out on the OWNER-only websocket channel (`deliver_ws_owners`), never the all-clients broadcast, because an app caller can open `/api/ws` and would otherwise receive another user's complete handoff prompts. Returns `{ok, delivered}` where `delivered` is the number of owner-socket sends that actually COMPLETED — the send is awaited (`deliver_ws_owners`), not fire-and-forget, because a socket count is taken before any send runs and a window that drops in that gap would be reported as delivered; failed sends are dropped from the owner set. A card with no listener is reported to the model as not shown instead of a false success. Dashboard sessions only, at both layers: the MCP tool rejects non-`dashboard:` session keys, and BOTH endpoints require the OWNER's own identity via `is_owner_dashboard_request` — the same predicate the source-provider mutations use: an `app` claim of `""` is necessary but not sufficient, since a dashboard credential minted for a different subject carries it too and would raise cards in the owner's composer and create branches in the owner's repositories; when no owner is configured only the signed local bootstrap subjects (`local-app`, `local-startup`) are accepted, which is the standalone-local case. One carve-out: the loopback internal-secret grant sets `request["internal_auth"] = True` and NO app claim (that is the path every MCP call arrives on), so it is permitted. A request with neither marker really did skip authentication and is refused, SEL-audited. POST `/api/worktree/create` with `{repo, branch}` creates `/-wt-` on a new branch off `origin/HEAD` (falling back to `HEAD`) and returns `{ok, path, branch, base, reused}`. Security boundary: `repo` must name or sit inside a directory an existing chat slot is scoped to (the submitted path AND the resolved git toplevel are both checked, and only the server-held root is ever used as a path); sensitive paths refused; git is routed through the `sandboxed_spawn_argv` chokepoint in **strict** mode (OS isolation + scrubbed env; strict because the filter probe passes `--includes` and repo-controlled `include.path` could otherwise make git read `~/.aws/credentials` as config); a host with no sandbox backend and no `agent.sandbox_allow_unsandboxed_exec` opt-in gets a 503 instead of an unisolated spawn. On top of that: an argv list with no shell, `run_limited` (resource limits applied after `exec`), 120s timeout, and `-c core.hooksPath= -c core.fsmonitor=false` so no repo-supplied program executes (a non-directory device has no hook to find and nowhere to plant one; an in-repo path is repo-writable and a gateway-owned temp dir is still same-uid writable between calls) — a repo declaring a `filter.*.{process,smudge,clean}` driver in EITHER repository config scope (`--local`, and `--worktree` whenever `extensions.worktreeConfig` is on — probe first, classify a failed probe after per `kiro_crew.git_worktree_scope`: only a probe that failed because the per-worktree `config.worktree` was never created counts as a healthy empty scope) is refused (409) because `-c` cannot disable an arbitrary filter name; the branch must also be a ref git accepts (`foo..bar`, trailing `.`/`.lock`, and `HEAD` are rejected up front rather than after the claim); both probes pass `--includes` (git defaults it OFF for a specific-scope query, so a driver reached via `include.path` was invisible yet still ran on checkout), and an unreadable scope is refused too. The allow-list collection and the sensitive-path/`isdir` screens run on a worker thread, so a slot project on stalled storage cannot block the event loop. Concurrency: the branch is claimed atomically (`update-ref ""`) and the destination by `os.mkdir`, both before anything else, so cleanup after a failure deletes only what the request proved it created, pruning before the branch delete so an `rmtree` fallback cannot leave the claimed branch behind, and skipping the delete entirely when the post-prune listing shows another worktree holding that branch or cannot be read at all (`update-ref -d` lacks `branch -D`'s used-by-worktree guard, so deleting an adopted branch would strand that worktree on a dangling ref); a failed `worktree list` is treated as unknown (503), never as "nothing registered". Reuse requires the destination to be registered on the REQUESTED branch (`_dir_slug` keeps only a branch's last segment, so `feat/foo` and `fix/foo` collide). Both endpoints are SEL-audited. Frontend: both card actions PRE-FILL a composer and never send, appending below an unsent draft rather than replacing it (the pending-input path also persists the draft, so a plain set destroyed in-progress user text); the worktree action creates the tree, opens a session WITHOUT activating it, scopes it, and only then activates it and prefills — so the composer is never live in the default directory (and fails closed rather than prefilling an unrelated one), and deletes the session it just made if scoping fails. Per-slot card state is pruned on slot delete and in stale-slot pruning. User-facing guide: `src/kiro_crew/docs/followup-suggestions.md`. **Pull-request sources**: POST `/api/source/pull-request` with `{url, refresh?}` returns the normalized full GitHub PR or GitLab MR payload. POST `/api/source/pull-request/checks` with `{url}` returns `{checks}` through a lightweight one-call GitHub or at-most-two-call GitLab path without rewriting the full-source cache. POST `/api/source/pull-request/status` with `{urls: [...]}` (bounded to `STATUS_URLS_MAX` = 64 canonicalized URLs, non-PR/MR URLs dropped, non-list bodies 400) returns `{statuses: {url: {state?, ci?, mergeable?, mergeStateStatus?}}, refreshing: [...], ttlSecs}` read straight from the same short-TTL chip-status cache the sidebar uses — it never blocks on a provider call, and schedules the bounded background refresh for stale entries, so unknown URLs are simply absent until a later poll. `refreshing` is the scheduler's own report of URLs whose value is expected to change shortly (started or already in flight; pending-cap deferrals excluded) and `ttlSecs` is the cache TTL, so a client paces its steady state by the server's TTL and re-polls within seconds of a refresh landing instead of up to one extra interval later. The Changes-tab source strip polls it to mark every PR/MR tab with its lifecycle state and CI rollup (bounded fast follow-ups, then TTL pacing; failing polls back off exponentially to a 5-minute ceiling rather than stopping, and re-poll on reconnect), and drives the selected tab from the full payload instead. The strip polls rather than riding the WS slots channel that already carries cached `state`/`ci` for sidebar chips: slot `source_links` are capped at a handful per slot while the strip shows up to 64 sources, so widening every slots push for one panel would cost every client more than one bounded request per TTL costs this one. **One truth for two surfaces**: the full-payload cache and the lightweight chip cache are kept coherent instead of expiring independently (which let the sidebar chip and the detail panel render different lifecycles for the same PR while both were nominally "fresh"). A completed full fetch projects its `state`/`draft`/`checks` onto the chip cache via `status_from_full_payload` (`record_full_payload_status`), and a chip refresh that observes a *changed* status conversely drops the full payload for that URL, so the panel's next read cannot serve a lifecycle the chip has already moved past. The chip entry carries the normalized **merge pair** alongside `{state, ci}` — free on the GitHub chip read and already present in GitLab's payload — and `status_from_full_payload` projects it too, because a write-through that dropped it would make every full fetch look like a chip change and drive exactly the mutual-invalidation loop below. Each field is recorded independently and only once it is real, never as `unknown`: independently because GitLab settles `need_rebase` and its branch-protection gates in the detail field while `mergeable` stays `unknown`, and never-as-`unknown` because a still-computing read must not read as a change away from a real answer. Because the pair participates in the chip cache's change detection, a branch that starts conflicting while the panel is open drops the full payload and pushes a delta like any other status change — the panel re-reads and banners it without a manual refresh. A structural loop-breaker guards the two comment-mirrored projections: `_refresh_check_status` records each URL's *changed* transition, and once the identical `(previous → new)` transition repeats past `_CHECK_FLAP_DAMP_THRESHOLD` consecutive refreshes — the signature of a chip↔full vocabulary divergence — it stops invalidating the full payload and emitting deltas for that URL and logs loudly once, degrading a would-be unbounded provider-polling loop to a stale glyph; a genuinely changing PR produces distinct transitions, which resets the counter and clears the damp. **Turn-boundary refresh**: when a chat slot goes idle, `DashboardState.refresh_slot_source_status` re-reads that slot's serialized chip URLs through `request_check_refresh_now`, which bypasses the chip TTL — an agent turn that opened a PR, pushed a revision, or drove a review round is the moment the remote state most likely moved, and TTL rotation alone can lag it by minutes once there are more PR-linked slots than `CHECK_STATUS_PENDING_MAX`. It is gated on at least one owner websocket (status is credential-backed and nobody else can render it, so a headless gateway spawns no provider work), scoped to the one slot that finished, floored to one forced read per URL per `_CHECK_FORCE_MIN_INTERVAL_SECS` (URLs inside the floor fall back to plain TTL pacing), and best-effort — a failure is logged and can never break turn completion. **Status deltas push instead of polling**: whenever a URL's cached status — `{ci, state}` or the merge pair — changes, the owner-only `source_status` WS event carries `{url, origin, ci?, state?, mergeable?, mergeStateStatus?}` to owner sockets (`DashboardState.push_source_status`, registered once as a delta sink at app wiring and unregistered on `on_cleanup`). `origin` (`"chip"` = the lightweight path learned it, `"detail"` = a full fetch's write-through produced it) is **diagnostic only** — the client patches its cached status batch AND invalidates `['pull-request-source', url]`/`['pull-request-checks', url]` for *every* changed delta regardless of origin. It must invalidate on `"detail"` too, because that delta is emitted by the single window whose full fetch ran; only that window received the fresh HTTP payload, so the other owner windows (whose detail query is `staleTime: Infinity`) would otherwise keep rendering the pre-change lifecycle. The initiating window's resulting refetch is harmless and cannot loop: `record_full_payload_status` runs only in the *uncached* fetch path, so the refetch hits the warm 30s cache and emits no further delta. The `origin` field is retained on the wire for diagnostics and possible future requester-aware routing; no consumer branches on it today. The client additionally invalidates pull-request detail queries on `chat_done`, because lifecycle/CI deltas do not cover review comments or mergeability and the detail query is otherwise `staleTime: Infinity` (it would never refetch after mount). The invalidation is **scoped to the finished slot**: for the ACTIVE slot it refetches the MOUNTED detail query with `refetchQueries({queryKey: ['pull-request-source'], type: 'active'})` (the PR on screen, which may lie outside the serialized chip subset; `refetchQueries` marks nothing else stale, whereas `invalidateQueries` with `refetchType: 'active'` would still mark every cached PR stale) and marks that slot's own `source_links` change URLs stale for their next mount; for a BACKGROUND slot it only marks that slot's own URLs stale (`refetchType: 'none'`, never a refetch of an off-screen PR). It never invalidates the whole `['pull-request-source']` family — doing so marked every session's PR stale on every turn anywhere, so a panel reopened while any chat was running always paid the full provider fanout even when nothing about that PR had changed — and the panel's own mutation success handlers invalidate only `['pull-request-source', url]` for the same reason. A background slot with more PRs than serialized chips has its overflow left to the status-delta path. The detail queries (PR and issue) also keep an unmounted payload for `SOURCE_DETAIL_GC_MS` (one hour) instead of React Query's five-minute default, and both detail queries revalidate on mount whenever the retained data is older than the gateway's cache window (`SOURCE_REMOUNT_REVALIDATE_MS`, 30s — `refetchOnMount` answers `'always'` past it, since with `staleTime: Infinity` a plain `true` would never refetch), so a reopened panel renders the retained payload at once and refetches in the background — stale-while-revalidate — rather than presenting an hour-old discussion as current when the only events this gateway sees are its own turns and status deltas. Younger data is not refetched: the gateway would return the same cached bytes, and Code Review Sage mounts its pane and this panel on the same key, which must stay one provider read per open. Past the window the refetch is cheap because the gateway revalidates with conditional GETs. A background revalidation that fails over a loaded payload (expired provider login, registry outage) renders a compact `role="status"` notice above the retained content — the last loaded version is showing, with the login command and a retry — never the full-height "could not load" card, which is reserved for a panel with nothing to show. Polling remains the safety net for a missed event. POST `/api/source/pull-request/resolve` with `{url, threadId}`, POST `/api/source/pull-request/auto-merge` with `{url, confirmImmediateMerge?}`, and POST `/api/source/pull-request/ready` with `{url}` are the three lifecycle mutations, and POST `/api/source/pull-request/submit-review` with `{url, reviewId, event}` is the fourth. Alongside it POST `/api/source/pull-request/pending-review` with `{url}` is a fourth READ, returning `{reviewId, body, commitId, headSha, stale, contentRedacted}` for the caller's own unsubmitted (PENDING) review or empty strings when there is none. The pair exists so a draft review can be published from the dashboard instead of only in the provider UI: GitHub scopes a PENDING review to its author and allows one per user per pull request, so the single PENDING entry the read returns is necessarily the caller's own — there is no way to observe, or therefore publish, someone else's. `event` must be `APPROVE`, `REQUEST_CHANGES`, or `COMMENT` (`DISMISS` and the other review endpoints are deliberately unreachable: this path publishes a draft, it does not act on reviews others submitted), `reviewId` must be a bare positive integer since it is interpolated into the REST path, and both are GitHub-only — a GitLab merge request is a 400 rather than a silently different code path. `reviewId` is REQUIRED rather than resolved server-side: a pending review may equally be one the human started by hand in the provider UI, so submitting whatever draft happens to exist could publish an unfinished review the caller never saw; the id the caller was shown is re-checked against the still-pending one and a mismatch is a 400, which turns a concurrent submit-or-replace into a rejection instead of a surprise. The id is necessary but NOT sufficient: it identifies the review OBJECT, and GitHub lets a pending review's body be edited and its inline comments added or removed under the same id, so an id match alone cannot prove the caller read what is about to go out. The read therefore also returns `contentDigest` -- a sha256 over the review body plus every inline comment's `(id, path, line, body)`, sorted by comment id so a re-ordered read of identical content yields the same digest while an edited body, a moved line, or an added/removed comment all change it -- and the submit REJECTS a digest that no longer matches the current content. `contentDigest` is REQUIRED, not optional: a digest compared only when present would be a one-parameter bypass of the whole binding, so an empty value is a 400 rather than a skipped check. The digest is taken over the RAW text, because raw text is what GitHub publishes. Two further guards fail the submit CLOSED, and the read reports both so the UI withholds the buttons rather than offering a doomed click. **Stale head**: the draft's `commit_id` must equal the pull request's live `head.sha`, because a repository without stale-approval dismissal counts a stale `APPROVE` as a live approval of code nobody read; every event is refused on a moved head, not just the verdicts, since inline comments anchor to lines that may be gone, and an unknown draft or head sha counts as stale. **Redaction mismatch**: submission publishes the draft GitHub stored, NOT the redacted copy the read returned, so a draft whose body or any of its inline comments is ALTERED by `_redact_provider_data` is refused outright -- otherwise the dashboard shows `[REDACTED]` while the published review carries the secret verbatim, which is worse than having no publish button. Inline comment bodies are read separately (`.../reviews/{id}/comments`) because submission publishes every comment the draft holds, not only the body this app can see -- and BOTH that call and the reviews list itself run `--paginate --slurp`, because each returns 30 per page: an unpaginated comment scan clears a draft whose 31st comment carries the credential and then publishes it, and an unpaginated reviews read reports "no draft" for a pending review sitting past page one. **Post-submit head re-check**: GitHub's submit-review API accepts no expected-head parameter, so the staleness check and the submit cannot be one atomic operation. For a GATING verdict (`APPROVE` / `REQUEST_CHANGES` -- a `COMMENT` carries no verdict) the head is re-read AFTER publishing, and a head that moved in that window makes the just-published review get dismissed again (`.../reviews/{id}/dismissals`) with the reason recorded; a dismissal that itself fails raises a louder error naming the review, because an undismissable stale approval is the one state a human must be told about. This is a compensating action, not atomicity: it converts an invisible race into a visible, self-reverting one -- with ONE case it cannot repair, which is why the read also reports `autoMergeArmed` and `APPROVE` is REFUSED while auto-merge is armed on the pull request: an approval then satisfies branch protection and GitHub can merge the unreviewed head BEFORE the dismissal lands, and nothing repairs a merge. The refusal is scoped to that combination rather than removing the verdict, because `COMMENT` and `REQUEST_CHANGES` cannot let a merge through, and an unrecognised `auto_merge` shape counts as armed (fail-closed). **Stale-approval dismissal is the setting that closes the class**: every remaining variant of the race -- auto-merge armed before or after the check, a force-push inside the submit round trip, a manual merge in that window -- needs the base branch to KEEP stale approvals to do harm, because with `dismiss_stale_reviews` on GitHub retracts the approval itself the moment the head moves. So `APPROVE` additionally requires `required_pull_request_reviews.dismiss_stale_reviews` on the pull request's base branch, read from `repos/{o}/{r}/branches/{base}/protection` and reported to the client as `staleDismissalEnabled`. Unreadable protection (no admin rights, or no protection at all) withholds `APPROVE` -- fail-closed, since an unanswerable question about safety is not an answer of safe. The UI withholds the Approve button and states the reason instead of offering a click that 400s. Submitting is irreversible and immediately visible to everyone on the pull request, so it is the most consequential of the provider writes — the LLM review poster is separately forbidden from ever submitting (see the Code Review Sage app), and this endpoint is what makes the HUMAN's own click the only path to a published verdict.. All three go through one auth/audit/error wrapper (`_owner_mutation_response`), so a client disconnect, a rejected request, and a provider failure are recorded identically across them. Every credential-backed endpoint requires the explicit dashboard-user claim `request["app"] == ""`. When `state.owner_id` is configured, every `_authorize_owner_request` read and every `_owner_mutation_response` mutation requires an exact `request["user"] == owner_id` match. When no owner is configured, the signed machine-local bootstrap subjects `local-app` and `local-startup` ARE the owner across both wrappers — reads and mutations alike — which is the standalone-local case. App tokens, non-owners, and unrelated or missing subjects fail closed. Every direct source request makes a best-effort SEL audit attempt with only the caller, operation, coarse outcome/reason, never URL, thread id, provider output, or credentials. SEL write failure cannot weaken a denial or replace the request's response or exception. Provider CLI calls separately emit coarse `invoked`, `completed`, `denied`, or `failed` tool-invocation events with no argv or provider-controlled text. Cancellation while reading a request or awaiting a provider is recorded as `failed/request_cancelled` when SEL is available, then the original cancellation is re-raised; because a remote mutation may already have landed, mutation cancellation is intentionally an uncertain failure outcome. Cache removal, generation advancement, and stale in-flight detachment complete before provider mutation dispatch, so a cancellation cannot preserve or repopulate pre-mutation data. A mutation invalidates **both** caches: the full-source payload and the separate short-TTL chip-status cache the sidebar and `/api/source/pull-request/status` read, which would otherwise keep serving pre-mutation `draft`/CI state for up to one TTL. The chip cache carries its own per-URL generation counter for the same reason resolve advances the full-fetch generation: a status refresh that started before the mutation captures the generation at entry and discards its result if it changed, so an in-flight fetch cannot restore superseded state. The resolve endpoint validates thread ownership and resolves a supported review thread, returning `{resolved: true}`. The auto-merge endpoint arms provider auto-merge and returns `{autoMerge: true, mergeMethod}`: on GitHub it reads the pull request node plus the repository's allowed merge methods, refuses a draft or an already-armed pull request without dispatching, picks the first repository-allowed method in squash/merge/rebase order, and calls `enablePullRequestAutoMerge`; GitLab has no separate switch, so it reads the merge request first, refuses a draft or an already-armed merge request, and otherwise issues the merge call flagged `merge_when_pipeline_succeeds`; because that call merges immediately when no pipeline is pending, the GitLab path is a merge authorization, so with no pending head pipeline the request is refused unless the body carries `confirmImmediateMerge: true`. The field must be a real JSON boolean -- coercing it would let any truthy value, notably the string `"false"`, read as consent -- and any other value is a 400. That refusal is raised as `ConfirmationRequired` and answers with `{error, confirmationRequired: true}`, which is what makes the guard live rather than a constant the client asserts: the dashboard's confirming click sends `false`, and only the server's own refusal escalates the panel to a third, explicitly-worded `Merge now` step that sends `true` and quotes the server's reason. Confirm and Cancel are separate buttons, with Cancel standing where the arming button was, so the second half of an accidental double-click backs out instead of authorizing a merge. The ready endpoint clears draft state and returns `{ready: true}`: `markPullRequestReadyForReview` on GitHub, and on GitLab the GraphQL `mergeRequestSetDraft(draft: false)` mutation, refusing when the merge request is not a draft (`draft`, or legacy `work_in_progress`). Neither ready path rewrites the title: the draft-prefix grammar stays the provider's concern, a concurrent retitle cannot be lost, and a title that merely begins with a draft-like word (`Drafting widgets`) is never mangled. GraphQL reports refusals in the body with HTTP 200, so every mutation response is inspected for transport-level `errors` and the per-mutation `errors` field and raises rather than reading as success. Both read the current draft/auto-merge state before mutating so an inapplicable action is a 400 instead of a provider error, and both invalidate the cache before dispatch on the same rule as resolve. The full payload carries `autoMerge` (GitHub `autoMergeRequest`, GitLab `merge_when_pipeline_succeeds`) so the panel renders armed auto-merge as state rather than an available action. Invalid URLs/thread IDs return 400; provider CLI, authentication, secure-spawn, audit, or direct-fetch-capacity failures return 503. Sidebar source-link extraction ignores every non-durable message role (`chunk`, `done`, `streaming`, `queued`, and `permission`) and indexes only durable message content, preventing partial output, queue placeholders, and permission prompts from scheduling credential-backed provider work. Sidebar provider refresh and cached `state`/`ci` serialization use the same read-only boundary: an exact configured-owner request, or a signed `local-app`/`local-startup` dashboard request when no owner is configured. Non-owner and app-token slot responses retain the source URL/provider/number but cannot trigger or observe credential-backed status. -**Chat Folders**: GET `/api/chat/folders` (list project folders, each enriched with a computed non-persisted `history_count` — the authoritative on-disk archived-session count per folder from `ConversationLog.list_sessions()`), POST `/api/chat/folders` (create; body `{name, parent_id?, project_dir?, default_agent?, color?, icon?, tags?}` — `color` is allowlisted against the folder palette (`_FOLDER_COLOR_PALETTE`, pinned to the frontend catalog by test), rejected with 400 `code: "color_invalid"` otherwise; `tags` is an array of tag ids filtered against the live tag vocabulary (deduped preserving order; only a non-array payload is rejected with 400 `code: "tags_invalid"` — unknown and non-string ids are silently FILTERED, not 400ed, matching the slot-tags endpoint, since a dangling id can legitimately exist on a folder and a strict endpoint would brick every subsequent save; the intersection is skipped when the vocabulary is not authoritative (`tags.json` unreadable at boot), failing open so a save cannot wipe stored tags; no count cap), persisted on the folder record only when non-empty (absent means no tags); `icon` is an optional explicit emoji (grapheme-exact single emoji, validated in `create_folder_record` next to `color`, 400 `code: "icon_invalid"` otherwise) — create NEVER generates: absent, null, and explicit empty-string `icon` are equivalent (no icon stored, the folder keeps the default glyph, no model call). Generation is explicit-only, via `regenerate_icon` on PATCH below (the Auto-generate button); that path's fire-and-forget background task (`_spawn_chat_folder_icon_task`, the chat-folder sibling of the artifact-library generator) derives an emoji from the folder name on the governed cheap-model one-liner path and writes it back through `mutate_folders` under the store lock, re-finding the folder by id (a folder deleted mid-generation is never resurrected) and only while the folder's icon epoch is unchanged since schedule time (a per-folder in-memory counter bumped under the store lock by every icon set, icon clear, and rename — any of those landing while the LLM runs invalidates the stale result); the generated icon arrives via the slots push), PATCH `/api/chat/folders/{id}` (update — accepts `hidden` (bool), `color` (allowlisted; empty string or null clears back to default) and `tags` (validated as on create; an empty array clears the key) alongside `name`/`collapsed`/`order`/`default_agent`/`project_dir`; a non-object JSON body is 400 `code: "invalid_json"`; `icon` (single emoji sets it; empty string or null clears back to the default glyph — absent-means-default stays the one on-disk representation, mirroring `color`), `regenerate_icon` (strict boolean, 400 `code: "regenerate_icon_invalid"` on a non-bool; `true` runs the background generator from the folder's current name after the update applies — mutually exclusive with `icon` in the same request, 400 `code: "icon_conflict"`); a folder's emoji `icon` replaces only the default glyph shape while the palette `color` keeps tinting the default glyph, rename never regenerates, and `icon`/`regenerate_icon` ride the validate-then-apply changes dict so app-ownership enforcement applies unchanged (the artifact-library folder emoji generator is the same underlying engine, wired separately); moving or reviving a session into a folder auto-unhides it via `_unhide_folder`), DELETE `/api/chat/folders/{id}` (delete + ungroup its slots), POST `/api/project-scaffold/scan` (dry-run preview of the folder tree a project directory would produce — read-only, creates nothing), POST `/api/project-scaffold/create` (create the previewed selection; both are specified in "Chat Folder Scaffolding" below) Folders may be linked to a project directory (`project_dir`, validated server-side: absolute, existing, non-sensitive path): `POST /api/chat/slots` resolves the effective directory from the server-held folder tree by walking to the nearest ancestor with `project_dir` set (cycle-guarded), validates the stored path again, and initializes a new slot with it before the first broadcast. The dashboard still carries its client-resolved directory through the existing explicit project endpoint as compatibility defense, but correctness does not depend on the React Query folder cache. **A slot is born filed**: `POST /api/chat/slots` accepts `folder_id` (validated against the folder list, 400 on unknown) and the create handler runs the whole set-up — folder, title, artifact binding, project — inside `DashboardState.suspend_slots_push()`, so exactly ONE coalesced `slots` broadcast is emitted and it already carries the folder. A genuinely NEW slot filed into a folder that carries `tags` also inherits them at creation (copy-by-value onto `slot.tags`, ids re-validated against the live vocabulary, non-string entries skipped; addressing an EXISTING slot never re-tags, and moving a session into a folder later never retro-tags — creation-only, mirroring fork tag inheritance). Inheritance is DIRECT-FOLDER-ONLY: unlike `project_dir`, which resolves by walking up to the nearest ancestor that sets it, a chat created in a subfolder does not inherit an ancestor folder's tags — tags mark the folder itself, not its subtree. The per-channel default-filing path (`channel_slots.surface_channel_session`) applies the same copy on its first-filing branch, so a channel chat born into a tagged folder inherits identically; its restore branch (a persisted `folder_id`) does not. **`default_agent` follows the `project_dir` rule rather than the tag rule**: an empty `default_agent` on a subfolder means "inherit", so `website/src/utils/folderAgent.ts::resolveFolderAgent` walks up to the nearest ancestor that pins one (sharing `resolveFolderProjectDir`'s `Set`-based cycle guard through one `nearestFolderValue` helper) before falling back to the global default, and `FolderConfigModal`'s empty "Inherit (…)" option names that resolved ancestor agent — naming the global default there contradicted the agent the subfolder's chats actually run. That walk is FRONTEND-ONLY, which is the one place this rule is weaker than `project_dir`'s: the sidebar create path sends the resolved name as the request's explicit `agent`, and the handler has no folder-agent counterpart to `_resolve_folder_project_dir` — an agent-less create falls straight through to `cfg.default_agent`. So every non-sidebar create-in-folder path (cron folder assignment, `channel_slots.surface_channel_session`, MCP slot creates) still gets the global default and ignores the folder's agent, a DIRECTLY set one included; unlike `project_dir`, correctness here does depend on the React Query folder cache. The create ordering is load-bearing: `get_or_create_slot` requests a slots push while the handler's `suspend_slots_push()` context is active, and metadata-bearing, nonpersistent, app-owned, remote, unknown-body-key and pre-handoff error paths publish synchronously when that context exits. One narrow exception applies to a genuinely new local plain persistent-memory create whose body contains only the explicitly known create keys and no folder, title, artifact or mode metadata: its POST response already carries the complete slot, so the operation-only suspension callback requests a state-owned fixed 10 ms handoff of the full-list publication; callers cannot choose the delay. A successfully scheduled handoff cancels and clears any armed trailing timer under the coalescing lock, and the callback repeats that cancellation before stamping the coalescing window, so neither an older nor a newly armed trailing callback duplicates the full-list frame. A delayed publication failure is logged and retried once; a callback that fires during another active suspension transfers its publication debt to that suspension, while any overlapping suspensions force the depth-zero publication back to the synchronous path. The former client-side `setSlotFolder` PATCH could not provide this ordering: it ran only after the response and therefore after an unfiled slots frame, rendering the new session at the top level for ~200ms before it jumped into its folder. `_folder_changed` is deliberately NOT set at creation: the first turn is `is_new`, which already injects the `[FOLDER]` breadcrumb. +**Chat Folders**: GET `/api/chat/folders` (list project folders, each enriched with a computed non-persisted `history_count` — the authoritative on-disk archived-session count per folder from `ConversationLog.list_sessions()`), POST `/api/chat/folders` (create; body `{name, parent_id?, project_dir?, default_agent?, color?, icon?, tags?}` — `color` is allowlisted against the folder palette (`_FOLDER_COLOR_PALETTE`, pinned to the frontend catalog by test), rejected with 400 `code: "color_invalid"` otherwise; `tags` is an array of tag ids filtered against the live tag vocabulary (deduped preserving order; only a non-array payload is rejected with 400 `code: "tags_invalid"` — unknown and non-string ids are silently FILTERED, not 400ed, matching the slot-tags endpoint, since a dangling id can legitimately exist on a folder and a strict endpoint would brick every subsequent save; the intersection is skipped when the vocabulary is not authoritative (`tags.json` unreadable at boot), failing open so a save cannot wipe stored tags; no count cap), persisted on the folder record only when non-empty (absent means no tags); `icon` is an optional explicit emoji (grapheme-exact single emoji, validated in `create_folder_record` next to `color`, 400 `code: "icon_invalid"` otherwise) — create NEVER generates: absent, null, and explicit empty-string `icon` are equivalent (no icon stored, the folder keeps the default glyph, no model call). Generation is explicit-only, via `regenerate_icon` on PATCH below (the Auto-generate button); that path's fire-and-forget background task (`_spawn_chat_folder_icon_task`, the chat-folder sibling of the artifact-library generator) derives an emoji from the folder name on the governed cheap-model one-liner path and writes it back through `mutate_folders` under the store lock, re-finding the folder by id (a folder deleted mid-generation is never resurrected) and only while the folder's icon epoch is unchanged since schedule time (a per-folder in-memory counter bumped under the store lock by every icon set, icon clear, and rename — any of those landing while the LLM runs invalidates the stale result); the generated icon arrives via the slots push), PATCH `/api/chat/folders/{id}` (update — accepts `hidden` (bool), `color` (allowlisted; empty string or null clears back to default) and `tags` (validated as on create; an empty array clears the key) alongside `name`/`collapsed`/`order`/`default_agent`/`project_dir`; a non-object JSON body is 400 `code: "invalid_json"`; `icon` (single emoji sets it; empty string or null clears back to the default glyph — absent-means-default stays the one on-disk representation, mirroring `color`), `regenerate_icon` (strict boolean, 400 `code: "regenerate_icon_invalid"` on a non-bool; `true` runs the background generator from the folder's current name after the update applies — mutually exclusive with `icon` in the same request, 400 `code: "icon_conflict"`); a folder's emoji `icon` replaces only the default glyph shape while the palette `color` keeps tinting the default glyph, rename never regenerates, and `icon`/`regenerate_icon` ride the validate-then-apply changes dict so app-ownership enforcement applies unchanged (the artifact-library folder emoji generator is the same underlying engine, wired separately); moving or reviving a session into a folder auto-unhides it via `_unhide_folder`), DELETE `/api/chat/folders/{id}?delete_contents=` (default: delete this one folder, unfile its live slots and re-parent its direct child folders to the top level, answering `{ok}`; `delete_contents=true` removes the whole subtree in one step -- the subtree is computed once under the store lock and FROZEN for the request's duration (a frozen folder refuses a child create with 400 `folder_parent_deleting`, a rename/reparent/reorder or a reparent into it and a second delete with 409 `folder_deleting`, and the filing paths that consult the store -- `_unhide_folder`, `arrival_folder_exists`, the session-control create/fork existence checks, the cron placement's `chat_folder_exists` -- report it absent to a NEW filing, so nothing files into it, while the two callers holding a STORED filing (the resume, the import repair) ask with `frozen_is_present=True` and keep it -- frozen is not gone, and the delete's commit decides; the removal callback itself sweeps live slots under the store lock and unfiles any still naming a removed folder, so the commit can never leave a dangling assignment), every folder in the frozen set deleted, every live session filed in it ARCHIVED through the tab-✕ `close_slot` path (saved to history as closed, never a hard delete; a session moved out of the subtree while its close awaited is left live where it went, via `close_slot`'s synchronous `pre_pop_check`), and archived sessions still filed under the subtree unfiled by a compare-and-set `update_metadata_if` -- answering `{ok, delete_contents, deleted_folder_ids, unfiled_sessions, archived_sessions, unfiled_history_sessions}`; a session whose close refuses stops the cascade with that close's own `code` and the folder tree untouched; the app-caller 403s apply to both modes, and the MCP verb `chat_folder_delete` is a thin wrapper over this route under the caller's own session identity, so an app agent gets the same refusal), POST `/api/project-scaffold/scan` (dry-run preview of the folder tree a project directory would produce — read-only, creates nothing), POST `/api/project-scaffold/create` (create the previewed selection; both are specified in "Chat Folder Scaffolding" below) Folders may be linked to a project directory (`project_dir`, validated server-side: absolute, existing, non-sensitive path): `POST /api/chat/slots` resolves the effective directory from the server-held folder tree by walking to the nearest ancestor with `project_dir` set (cycle-guarded), validates the stored path again, and initializes a new slot with it before the first broadcast. The dashboard still carries its client-resolved directory through the existing explicit project endpoint as compatibility defense, but correctness does not depend on the React Query folder cache. **A slot is born filed**: `POST /api/chat/slots` accepts `folder_id` (validated against the folder list, 400 on unknown) and the create handler runs the whole set-up — folder, title, artifact binding, project — inside `DashboardState.suspend_slots_push()`, so exactly ONE coalesced `slots` broadcast is emitted and it already carries the folder. A genuinely NEW slot filed into a folder that carries `tags` also inherits them at creation (copy-by-value onto `slot.tags`, ids re-validated against the live vocabulary, non-string entries skipped; addressing an EXISTING slot never re-tags, and moving a session into a folder later never retro-tags — creation-only, mirroring fork tag inheritance). Inheritance is DIRECT-FOLDER-ONLY: unlike `project_dir`, which resolves by walking up to the nearest ancestor that sets it, a chat created in a subfolder does not inherit an ancestor folder's tags — tags mark the folder itself, not its subtree. The per-channel default-filing path (`channel_slots.surface_channel_session`) applies the same copy on its first-filing branch, so a channel chat born into a tagged folder inherits identically; its restore branch (a persisted `folder_id`) does not. **`default_agent` follows the `project_dir` rule rather than the tag rule**: an empty `default_agent` on a subfolder means "inherit", so `website/src/utils/folderAgent.ts::resolveFolderAgent` walks up to the nearest ancestor that pins one (sharing `resolveFolderProjectDir`'s `Set`-based cycle guard through one `nearestFolderValue` helper) before falling back to the global default, and `FolderConfigModal`'s empty "Inherit (…)" option names that resolved ancestor agent — naming the global default there contradicted the agent the subfolder's chats actually run. That walk is FRONTEND-ONLY, which is the one place this rule is weaker than `project_dir`'s: the sidebar create path sends the resolved name as the request's explicit `agent`, and the handler has no folder-agent counterpart to `_resolve_folder_project_dir` — an agent-less create falls straight through to `cfg.default_agent`. So every non-sidebar create-in-folder path (cron folder assignment, `channel_slots.surface_channel_session`, MCP slot creates) still gets the global default and ignores the folder's agent, a DIRECTLY set one included; unlike `project_dir`, correctness here does depend on the React Query folder cache. The create ordering is load-bearing: `get_or_create_slot` requests a slots push while the handler's `suspend_slots_push()` context is active, and metadata-bearing, nonpersistent, app-owned, remote, unknown-body-key and pre-handoff error paths publish synchronously when that context exits. One narrow exception applies to a genuinely new local plain persistent-memory create whose body contains only the explicitly known create keys and no folder, title, artifact or mode metadata: its POST response already carries the complete slot, so the operation-only suspension callback requests a state-owned fixed 10 ms handoff of the full-list publication; callers cannot choose the delay. A successfully scheduled handoff cancels and clears any armed trailing timer under the coalescing lock, and the callback repeats that cancellation before stamping the coalescing window, so neither an older nor a newly armed trailing callback duplicates the full-list frame. A delayed publication failure is logged and retried once; a callback that fires during another active suspension transfers its publication debt to that suspension, while any overlapping suspensions force the depth-zero publication back to the synchronous path. The former client-side `setSlotFolder` PATCH could not provide this ordering: it ran only after the response and therefore after an unfiled slots frame, rendering the new session at the top level for ~200ms before it jumped into its folder. `_folder_changed` is deliberately NOT set at creation: the first turn is `is_new`, which already injects the `[FOLDER]` breadcrumb. **Chat Folder Scaffolding** (`dashboard/chat_folder_scaffold.py` + the pure scanner `project_scan.py`): a monorepo, or a directory of sibling repositories, needs one folder per package before per-package steering loads at all — and assembling N sub-folders by hand is work nobody does, so the two endpoints above detect the packages and then build the tree through the ordinary folder create path. The split into preview-then-create is the safety property, not a convenience: `scan` is read-only, so the destructive-sounding half of "build me twenty folders" only ever runs against a selection the user has seen. `POST /api/project-scaffold/scan` — body `{root}`. The root goes through the SAME `_validate_project_dir` a manual folder create uses, so the scan refuses exactly what creating a folder by hand refuses and answers with the identical prose (`project_dir must be an absolute path` / `refers to a sensitive path` / `must be an existing directory`) under `code: "folder_scan_root_invalid"`; an empty root is `folder_scan_root_required`, which has to be caught separately because the validator ACCEPTS `""` (a folder is allowed to have no project directory) and a rootless scan would otherwise fall through and scan nothing. The root is additionally refused when it CONTAINS a protected location (`path_contains_sensitive` — the reverse direction of the validator's own check, because the scan sweeps everything below the root), and as defense in depth the scanner's two file reads (`declared_patterns`, `_gitignore_spec`) refuse a path inside a protected location whatever the root was. Root resolution (`realpath`/`isdir` against a path the user merely named), the config read (both `asyncio.to_thread`) and the walk (`discovery_executor()`, the pool purpose-built for browser-triggerable read-only filesystem discovery — not the subprocess pool, which must stay free for PTY teardown and wedge recovery, and not the maintenance pool, whose periodic sweeps must stay responsive) all run OFF the event loop, because the cost scales with a tree the user merely pointed at and one stalled network mount would otherwise stall every chat, WS push and heartbeat behind it. Response: `{root, root_existing, status, candidates: [{path, name, parent_path, tier, signals, existing, selected}], warnings}`. Zero candidates is HTTP 200 with `status: "empty"` — an answer (this tree holds no packages the scanner recognizes), not a failure a surface has to render as an error; anything else is `"ok"`. Each candidate carries its `parent_path`, and a surface derives whatever grouping it needs from that — the flat `candidates` list stays the single copy of each candidate. **Detection is two-tiered because the confidence differs.** A `.git` or `.kiro` directory is unambiguous at any depth → `auto` (ticked by default): a nested repository is its own package, and a directory already used with Kiro is one the user has already treated as a project root. A recognized manifest — `package.json`, `pyproject.toml`, `Cargo.toml`, `go.mod`, `pom.xml`, `build.gradle`, `build.gradle.kts`, `build.sbt`, `pubspec.yaml`, `composer.json`, `Gemfile`, `mix.exs`, `Package.swift`, `deno.json`/`deno.jsonc`, plus the deploy-root markers `firebase.json`, `vercel.json`, `netlify.toml`, `amplify.yml`, `serverless.yml`/`.yaml`, `cdk.json`, `wrangler.toml`/`.jsonc`, `fly.toml`, `render.yaml`, and `Procfile` (a deploy config marks a deployable unit that often carries no build manifest at its own level — the package.json files of a Firebase app live in its functions/web children; deliberately generic names like `template.yaml`, `app.yaml` or `Dockerfile` are excluded) — is the ambiguous case and POSITION decides it: outside any detected package it names the package itself (`auto`), inside one it may just as easily name a build fixture as a real sub-package (`offered`, shown unticked) — EXCEPT the deploy-root markers, which stay `auto` at any depth, because a deploy config never names a build fixture: someone deploys that directory, so a monorepo's apps stay ticked even though the workspace root wraps them in a package. A directory named by a workspace declaration (`workspaces` in a package.json, including yarn's `{"packages": [...]}`; `pnpm-workspace.yaml`; Cargo `[workspace] members`; `go.work` `use`, both spellings) is `offered`, and being named in a list never DEMOTES a candidate that already earned `auto` — it only gains the `member` signal, so a preview can show both reasons. `!pattern` exclusions are honoured and subtracted, because an excluded directory is one the project has already said is not a member. A directory with no signal at all is absent from the tree entirely rather than carrying a third tier, so a surface that forgot to filter cannot render an ignored directory. The scan root itself is never a candidate (its folder is created by the scaffold step, from the root path directly), but its own signals still count: a root holding a manifest makes everything below it *nested*, which is what puts a monorepo's packages on the unticked tier while a directory of unrelated repositories puts them on the ticked one. **Prune beats every signal, one-directionally**: `node_modules`, `dist`, `build`, `target`, `env`, `venv`, `.venv`, `__pycache__`, `DerivedData`, plus any dot-directory except `.kiro`, are rejected BEFORE classification (they never enter the walk stack at all), so a vendored `package.json` under `node_modules` cannot become a candidate however well it matches — the opposite ordering (classify, then filter) leaks a candidate the moment a signal is added without a matching filter, so member-pattern expansion walks segment by segment under the same limits rather than calling `glob.glob`, which honours none of them. **Gitignored is pruned with the same precedence**: the project's `.gitignore` files (at or below the scan root only — the scanner never reads outside the tree the user pointed at) are honoured with git semantics via `pathspec` (nested files stack and the deeper wins, dir-only `pattern/` forms, `!negation` re-includes), an ignored directory is never entered or classified — a `.git` inside cannot rescue it, which is what defeats SwiftPM's `derived_data/SourcePackages/checkouts` store of full dependency clones — an ignored file is neither a manifest signal nor a workspace declaration, and a declared member inside an ignored directory is dropped; an unreadable or oversized `.gitignore` costs a warning and that file's layer, never the scan. Linked directories are never traversed, and the gate is the property rather than a list of link kinds: an entry whose name resolves somewhere other than itself — a POSIX symlink (`is_dir(follow_symlinks=False)`), a Windows junction (whose listing-time reparse tag carries the name-surrogate bit; `mklink /J` needs no elevation, so a privilege argument never covered it), or whatever redirecting reparse kind ships next — is kept out of the walk, while in-place reparse decorations like cloud placeholders still scan. Because listing settles what a child is while the descent re-resolves it by NAME, the descent is descriptor-pinned where the platform allows — opened `O_NOFOLLOW`/`O_DIRECTORY` and checked against the `(st_dev, st_ino)` the parent recorded, so a directory swapped for a link in between is refused rather than read through (a path-based read would yield entries from outside the root under a path string that still looks inside it, which string containment cannot catch). A platform with neither the flags nor a per-entry inode (Windows reports a zero inode from a directory listing, recorded as no identity at all) verifies resolution instead: at descent the directory's real path must equal its parent's real path plus its own name, so a pre-planted link of any kind is refused no matter when it was created — inductively up the walk, since every ancestor passed the same check when it was the leaf being descended, with the scan root the caller's to validate (the endpoint realpaths it). What that branch still cannot close is a swap landing between the check and the read; it has no way to make them one operation, and states that window plainly rather than reasoning it away. A pre-existing link's target is therefore never read on any platform, whether it points inside the root, outside it, or back at an ancestor, and a declaration file is read only when found as a regular file — following a link would read a file outside the tree the user pointed at and quote its content back in a parse warning. The scanner's own `DEFAULT_DEPTH_CAP` (5) bounds the descent; a declaration larger than 512 KiB is refused rather than parsed truncated; and `pnpm-workspace.yaml` is parsed by a SafeLoader subclass that refuses YAML ALIASES outright, since a small file can otherwise compose a graph orders of magnitude larger than itself and no real member list needs one. An unreadable subtree or an unparsable declaration becomes a `warnings` entry and the scan continues — one unreadable directory must not cost the user the other twenty packages. A `.gitignore` pattern the grammar refuses (a lone `!`) is converted to the same refusal an oversized file gets, costing the warning and that file's layer rather than surfacing as a 500. And warning REASONS are redacted at construction (`redact_exfiltration_urls` + `redact_credentials` in `_warning_reason`): a parse error quotes the offending source line back, and that line is tree content — a credential in a malformed declaration must not ride the warning into the scan response. **Reconcile marking is an overlay, not a detection rule**, which is what keeps the scanner's output a function of the filesystem plus its two arguments and nothing else (two scans of an unchanged tree compare equal): the endpoint marks a candidate `existing: true` when its path EXACTLY equals a `project_dir` some folder already holds. Exact equality is the whole comparison — both sides come out of the same `realpath` — where prefix matching or re-resolving a stored path could mark a sibling directory as taken. An existing candidate is still REPORTED ("already set up" is information the user wants) but is never ticked whatever its tier, so a re-scan offers additions only; the store is read AFTER the walk, so a folder created while the scan ran is reflected rather than offered again. `POST /api/project-scaffold/create` — body `{root, selected: ["", ...]}`. **The selection is re-derived, never trusted**: the tree is scanned AGAIN here, a selected path is created only if THIS scan offers it as a candidate, and the folder is created on the *scanner's* path rather than the string the request carried — so a client picks from what the server just found and can never name the directory a folder lands on. That makes the difference between a stale preview (the user's tree changed under them) and a forged one (a path nobody was ever offered) irrelevant, since one test refuses both: anything outside the fresh candidate set is a 400 `folder_scaffold_selection_stale` carrying up to `MAX_REPORTED_UNKNOWN` (20) `unknown` paths — capped because the request body it comes from is caller-controlled and a response must not grow with it — plus a SEL `denied` audit, because the request asked for a folder on a directory this server never offered even when the cause was a preview left open too long. A `selected` that is not a list of strings is `folder_scaffold_selection_invalid`; an absent or null `selected` is legitimate and means "the root folder, none of the packages". Creation order is the scan root's folder first, then candidates in the tree's path-sorted order, which puts every ancestor before its descendants (an ancestor's path is a prefix of theirs) and is therefore parent-before-child by construction. Every folder goes through `create_folder_record`, one call per folder, so each is name/path-validated and appended under the folders lock exactly as a hand-created folder is — no new persistence, no second write path. **One folder per directory is atomic**: the scaffold opts into `create_folder_record(unique_project_dir=True)`, whose check runs inside the locked append — two concurrent scaffolds of the same tree cannot both observe a path unclaimed and both persist it; the loser's path lands in `skipped_existing` and the winner's folder is still the parent its children hang off. **A path must still name the directory the scan confirmed at create time**: the scaffold also opts into `require_resolved_project_dir=True`, which refuses (`folder_project_dir_moved`, audited as a SEL denial) any `project_dir` whose validation resolves away from the scanner's own canonical path — a component swapped for a link in the scan-to-create window would otherwise bind the folder to a directory outside the confirmed root. A swapped root costs the whole call; a swapped candidate costs only its own path. The folder API proper leaves the flag off, since a person naming their own `~` or symlinked path is resolution working as intended. **The scaffold write carries the same three guards the folder-create route applies**: an unattributable caller (a `dashboard:` session key naming a popped slot) is refused 403 `caller_unattributable` before anything is created; an internal caller consumes ONE `FOLDER_CREATE` rate-budget unit per scaffold call (the browser is never throttled) so this route is not the loophole around the create route's limiter; and the caller's app identity — `_effective_request_app`, never the body — is passed to every `create_folder_record`, so a scaffolded folder is stamped `owner_app` exactly as a hand-created one and an app's create under a folder it does not own is refused. An ownership refusal is a `FolderCreateError` subclass, so it lands in `failed` for that path like any other per-folder refusal. A child whose parent was left unselected or whose parent failed hangs off the nearest ancestor that DOES have a folder, ending at the root's, keeping a partially-selected tree shaped like the tree the user saw rather than flattening it. Response: `{root, created: [{path, folder_id, name}], skipped_existing, failed: [{path, error, code}], warnings}`, with exactly one `slots` push and only when something was created. **Additive only**: no existing folder is deleted, renamed, or modified and no existing folder's settings change; an already-scaffolded candidate is reported in `skipped_existing` and is still the parent its own children hang off; and a failure partway through is REPORTED rather than rolled back, because rolling back would delete folders that may already hold conversations by the time the next creation fails. The one short-circuit is the root folder itself: if IT cannot be created the call returns immediately with that single failure, since the whole selection would otherwise land as unrelated top-level folders — a worse thing to hand back than "nothing was created". Both endpoints SEL-audit (`chat.folder_scan`, `chat.folder_scaffold`) with the root plus COUNTS (candidates, or created/skipped/failed) and never the candidate paths, so the audit trail does not become a copy of the user's directory layout. The scanner reads no configuration at all — its bounds are its own defaults, which is what keeps a scan a function of the filesystem and the root alone. The shipped surface today is the `project-scaffolder` App Store app page (off by default); the two endpoints are deliberately the repackaging boundary, so a CLI, a core folder-UI action, or another delivery shape would call these rather than re-implement the scan. **Agents**: GET `/api/agents` (Kiro Crew agent roster ordered **most-used-first** — config agents plus, scoped to the requesting session's project directory via `X-Session-Key`, that project's `.kiro/agents` discoveries, each row tagged `scope: "global" | "project"`; a name in both scopes lists once as the alias, since dispatch resolves aliases first; ordering reorders by `ConversationLog.agent_usage()` (turn count, then recency), falling back gracefully to config-insertion order on any failure so the dropdown never breaks or drops agents), GET `/api/agents/installed` (list all kiro-cli agents from `~/.kiro/agents/` — deliberately global-only, because every consumer is an agent CRUD/editor surface whose "Set as default" persists the selected name into `cfg.agents`, where a project-only name would not resolve; project-scope discovery instead reaches the dispatch surfaces — per-turn resolution, spawn validation, and Slack — with `scope` on each entry reporting which scope won there; `package` field extracted from filename), GET/PATCH/DELETE `/api/agents/detail/{name}` (full agent config JSON; PATCH writes `model` and `skills` for any user-level spec — the crew pane's private-copy edits — where `skills` lands on the freshly-read spec under the spec lock element-wise: only the `skill://` URIs the request adds, removes or reorders move, a reorder is applied within the managed `skill://` entries' own slots so a `file://` glob or hand-written wildcard interleaved between two skills keeps its index, a URI a concurrent writer removed in between is never put back, a request naming no resource change leaves the key untouched, and the response's `skills` reports what the written spec maps — the order persisted, including a skill a concurrent writer added in between — rather than the order requested; and, for the Agent templates tab, the definition keys `description`, `prompt`, `tools`, `allowedTools`, which are shape-checked (`400 invalid_definition`), refused on a package, runtime, markdown or private-copy spec (`409 template_read_only`, `reason` names which) and refused when more than one file claims the name (`409 ambiguous_template_name`); DELETE removes a template the user owns, `409 template_read_only` for the same four, `409 ambiguous_template_name` when more than one file reaches the name (by declared `name` or stem — the first hit is never unlinked), `503 schedule_store_unreadable` when `crons.json` is present but unreadable (nothing unlinked; a repaired store brings its jobs back), `409 template_referenced` with `references: [{kind: crew|default|schedule|folder|webhook|private_copy, id, label}]` (ids and labels through `_roster_mask`) while anything still names it; a row whose `filename` is not a plain basename resolving to a regular file directly under the agents directory is `404 template_not_found`, never unlinked — `folder` is a chat folder whose `default_agent` pins the template, `webhook` a webhook token whose `agent` does; DELETE and PATCH are owner-only — see Agent Config), GET `/api/agents/templates` (the templates tab's roster: every global discovery row with its package-controlled strings — description, model, source, package, lineage, skills, MCP server names, reference ids and labels — through `_roster_mask`, a row whose name or filename would be masked omitted, plus `read_only` — `null` or the reason — and `used_by`, the same reference list the delete guard evaluates), POST `/api/agents/templates` (`{name, description?, from?}` creates `~/.kiro/agents/.json`, blank or as a lineage-free copy of `from`; `400 invalid_template_name` / `template_name_reserved` / `template_name_reserved_by_engine` (an id the KAS engine keeps for itself, `agent_files.KAS_RESERVED_AGENT_IDS`), `404 template_not_found`, `409 name_taken` / `name_bound` (a binding in `config.json` or only in the `config.local.json` overlay, both checked under their own locks) / `ambiguous_template_name`; enrols nothing) diff --git a/src/kiro_crew/dashboard/arrival_folders.py b/src/kiro_crew/dashboard/arrival_folders.py index 8a738a537e0f..44f7d3156390 100644 --- a/src/kiro_crew/dashboard/arrival_folders.py +++ b/src/kiro_crew/dashboard/arrival_folders.py @@ -55,7 +55,11 @@ from collections.abc import Sequence from typing import TYPE_CHECKING, Any, NamedTuple -from kiro_crew.dashboard.chat_folders import MAX_CHAT_FOLDERS, folder_ids_filed_into +from kiro_crew.dashboard.chat_folders import ( + MAX_CHAT_FOLDERS, + folder_ids_filed_into, + folder_is_deleting, +) from kiro_crew.sel import sel if TYPE_CHECKING: # pragma: no cover - typing only @@ -357,10 +361,26 @@ def _resolve(folders: list[dict[str, Any]]) -> tuple[bool, str]: # an adopted row already happens. hidden_rows.append(str(group.get("id", ""))) group_id = str(group.get("id", "")) + if folder_is_deleting(state, group_id): + # A delete running on the group has frozen its subtree: a child + # appended now would be born outside the set that delete walks and + # commits, and outlive it re-parented to the top level with this + # arrival filed into it -- the same rule create_folder_record applies + # to a frozen parent. Nothing was appended on this branch (a group + # created above is new and cannot be frozen), so the empty filing + # leaves the tree as it was -- including the group's hidden flag: an + # un-hide deferred above is owed only by a filing that lands. + hidden_rows.clear() + return changed, "" if not child_name: # No sender to name: file directly under the group. return changed, group_id child = _find(folders, child_name, group_id) + if child is not None and folder_is_deleting(state, str(child.get("id", ""))): + # The sender's own folder is going; the delete decides it, not a + # filing made now. No filing, no deferred un-hide either. + hidden_rows.clear() + return changed, "" if child is None: if len(folders) >= MAX_CHAT_FOLDERS: # Reachable only when ``Imported`` already existed and the store @@ -601,9 +621,18 @@ def _discard(folders: list[dict[str, Any]]) -> tuple[bool, list[str]]: logger.info("Rolled back arrival folder %s after a failed import", fid) -async def arrival_folder_exists(state: "DashboardState", folder_id: str) -> bool: +async def arrival_folder_exists( + state: "DashboardState", folder_id: str, *, frozen_is_present: bool = False +) -> bool: """Whether *folder_id* is still a folder in COMMITTED store state. + A folder a running delete has frozen (``chat_folders._deleting_folder_ids``) + exists but is going, and reads as *frozen_is_present* says: absent for a + caller about to make a NEW filing (it would outlive the folder as a dangling + id), present for the import repair that would otherwise ERASE a stored filing + from a folder the delete may yet leave standing -- if the delete commits, its + own commit-time sweep unfiles the slot. + Read through ``read_folders`` so it sees only committed rows: an unlocked read can land mid-transaction and report a folder whose write is then rolled back, which is exactly the dangling id this check exists to prevent. @@ -621,6 +650,8 @@ async def arrival_folder_exists(state: "DashboardState", folder_id: str) -> bool return False def _read(folders: list[dict[str, Any]]) -> bool: + if folder_is_deleting(state, folder_id): + return frozen_is_present return any(str(f.get("id", "")) == folder_id for f in folders) try: diff --git a/src/kiro_crew/dashboard/channel_slots.py b/src/kiro_crew/dashboard/channel_slots.py index a834bd210c7e..0e5c10298a71 100644 --- a/src/kiro_crew/dashboard/channel_slots.py +++ b/src/kiro_crew/dashboard/channel_slots.py @@ -57,6 +57,7 @@ import logging import time import weakref +from collections.abc import Callable from itertools import islice from typing import TYPE_CHECKING, Any @@ -451,6 +452,133 @@ def needs_backfill_filing(meta: dict[str, Any]) -> bool: return not (meta.get("folder_id") or meta.get("channel_folder_filed")) +def _filing_write_guard( + state: "DashboardState", + folder_id: str, + placement_guard: Callable[[dict[str, Any]], bool], +) -> Callable[[dict[str, Any]], bool]: + """The ``update_metadata_if`` guard for a filing write into *folder_id*. + + *placement_guard* is the placement rule (``needs_default_filing`` / + ``needs_backfill_filing``), re-made against the locked record as before. On + top of it, the folder must still STAND at the write's commit boundary: + neither frozen by a running delete nor gone from the store. The write takes + the transcript's cross-process lock and can stall on it; a folder delete + committing while it waits -- its commit-time sweep of live slots and its + history pass over archived rows both already done -- would otherwise be + followed by this write landing the deleted id as a durable assignment that no + later pass corrects. Asked under the lock, right before the write, so the + answer is the newest the writer can have; a refusal skips the write, and the + conversation surfaces unfiled (a frozen folder that then aborts its delete is + filed by the next pass, since the record carries no marker). + + Runs off the loop, where the write runs, reading a list and a set the loop + mutates: a read here can trail the loop by one step, so a stale answer costs + at most one skipped filing the next pass retries -- never a filing into a + folder the store has already committed away. The frozen set is RESOLVED HERE, + on the loop, and only read by the worker: ``_deleting_folder_ids`` attaches + the set lazily, and a first attach from a worker thread racing the loop's own + first freeze would leave two sets, one of them empty and winning. + """ + # circular import: chat_folders -> chat_persistence -> this module. + from kiro_crew.dashboard.chat_folders import _deleting_folder_ids + + deleting = _deleting_folder_ids(state) + + def _guard(current: dict[str, Any]) -> bool: + if not placement_guard(current): + return False + if folder_id in deleting: + return False + return any(str(f.get("id")) == folder_id for f in getattr(state, "_folders", None) or ()) + + return _guard + + +# How often a landed filing re-asks a FROZEN folder for the running delete's +# verdict (see _filing_stands_after_the_write). The delete's own duration bounds +# the wait, so this only sets how promptly the thaw is noticed. +_FREEZE_VERDICT_POLL_SECS = 0.05 + + +async def _filing_stands_after_the_write( + state: "DashboardState", log: Any, key: str, folder_id: str +) -> bool | None: + """Whether a filing write that just LANDED still names a standing folder. + + :func:`_filing_write_guard` reads the folder under the record's lock right + before the write, but the folder store has its own lock, and the write itself + -- a transcript rewrite, off the loop -- takes long enough for a delete to + freeze, commit and finish its archived-row pass on the loop meanwhile. That + pass lists rows before the write lands, so it never sees this filing, and the + record would keep a folder id that is gone beside the marker that stops every + later pass from filing it again. So the folder is asked once more, on the + loop, the moment the write is known to have landed and before anything else + runs. Gone, the filing is WITHDRAWN: a compare-and-set on the very value this + pass wrote clears the folder and takes the marker back, leaving the + conversation unfiled and eligible, and a committed delete never sees a filing + to sweep. FROZEN is not gone: the running delete decides, so its verdict is + WAITED for rather than guessed -- withdrawing on the freeze would erase a + filing the abort keeps for every other writer, leaving it would strand the id + if the delete commits. The route thaws its ids in a ``finally`` whichever way + it ends, so the wait is bounded by the delete itself. + + Returns True when the filing stands, False when it is withdrawn or another + writer settled the row, and None when the withdrawal could not be CONFIRMED: + the record may still name the folder, and the caller carries that as a + failure rather than as a filing. + """ + # circular import: chat_folders -> chat_persistence -> this module. + from kiro_crew.dashboard.chat_folders import folder_is_deleting, folder_is_gone + + while folder_is_deleting(state, folder_id): + await asyncio.sleep(_FREEZE_VERDICT_POLL_SECS) + if not folder_is_gone(state, folder_id): + return True + + def _ours(current: dict[str, Any]) -> bool: + return current.get("folder_id") == folder_id + + # The withdrawal is a conditional write, and a refusal is not a commit: the + # verdict is read back from the row, never from the boolean alone. A row + # that names another folder, or none, was settled by another writer (the user's + # own move, or the delete's archived-row pass clearing it), and that writer's + # outcome stands -- withdrawing over it would be wrong. A row that still names + # it is still this pass's to fix (the write raised, or the store could not be + # read), so the withdrawal is tried once more; a filing that stands after + # that is logged at error level by key, since nothing later revisits it. + for attempt in range(2): + try: + if await asyncio.to_thread( + log.update_metadata_if, + key, + {"folder_id": "", "channel_folder_filed": False}, + _ours, + ): + return False + current = await asyncio.to_thread(log.get_metadata, key) + except Exception: + if attempt == 0: + logger.warning( + "channel filing: withdrawing the filing of %s failed once; retrying", + key, + exc_info=True, + ) + continue + break + if not _ours(current): + # Another writer settled the row; its outcome stands. + return False + logger.error( + "channel filing: %s still names folder %s, which a delete took while the " + "filing wrote, and the withdrawal could not be confirmed; the record keeps " + "a folder id no folder carries", + key, + folder_id, + ) + return None + + def _rebind_unbound_channel_slot( state: "DashboardState", slot: "_ChatSlot", session_key: str ) -> bool: @@ -659,8 +787,22 @@ def surface_channel_session( if tid not in slot.tags: slot.tags.append(tid) tags_changed = True + # Both assignments below are the "channel restore" half of the late-assignment + # window: the existence of a STORED ``folder_id`` was never asked here, and the + # default filing's folder was resolved before the reconcile's record write + # awaited. A folder delete committing in either gap ran its commit-time sweep + # before this slot existed. So the folder is re-asked synchronously at the + # assignment: GONE, the session surfaces unfiled and its next save replaces + # the stored id. FROZEN is not gone -- the filing is applied, the slot + # registers with it for the delete's commit-time sweep to unfile if the delete + # commits, the payload projects it unfiled meanwhile, and an abort leaves it + # standing. Local import: chat_folders reaches this module through + # chat_persistence, so a module-level import would be circular. + from kiro_crew.dashboard.chat_folders import folder_is_gone + if meta.get("folder_id"): - slot.folder_id = meta["folder_id"] + if not folder_is_gone(state, str(meta["folder_id"])): + slot.folder_id = meta["folder_id"] elif folder_id and needs_default_filing(meta): # Per-channel filing (off by default), applied on the pass that first # surfaces the conversation. Re-tested here rather than trusting the @@ -670,16 +812,21 @@ def surface_channel_session( # user moved it. `folder_id` is omitted from the metadata line when # empty, so the marker is the only thing that distinguishes "moved to the # top level" from "never filed". - slot.folder_id = folder_id + # + # The marker is set even when the folder is gone: the reconcile's record + # write already carries it, and a slot without it would be filed again by + # the next pass. slot._channel_folder_filed = True - # First filing = this chat's birth into the folder: copy the folder's - # tags by value, the same creation-only inheritance the dashboard - # slot-create path applies. The restore branch above deliberately does - # not — a persisted folder_id means the filing already happened. - for tid in folder_tags or []: - if tid not in slot.tags: - slot.tags.append(tid) - tags_changed = True + if not folder_is_gone(state, folder_id): + slot.folder_id = folder_id + # First filing = this chat's birth into the folder: copy the folder's + # tags by value, the same creation-only inheritance the dashboard + # slot-create path applies. The restore branch above deliberately does + # not — a persisted folder_id means the filing already happened. + for tid in folder_tags or []: + if tid not in slot.tags: + slot.tags.append(tid) + tags_changed = True # "tags changed => revision changed": the slot was constructed with an empty # list under its birth revision, and a concurrent slots GET may already have # snapshotted that; the surfaced list must carry a revision of its own. @@ -1286,7 +1433,10 @@ def _read_folder_tags( log.update_metadata_if, key, filing_meta, - needs_default_filing, + # The folder is re-asked at the commit boundary too, under + # the record's lock: a delete committing while this write + # waited must not be followed by its id landing here. + _filing_write_guard(state, to_file, needs_default_filing), ) except Exception: # Could not record it, so do not apply it in memory either: @@ -1300,6 +1450,20 @@ def _read_folder_tags( ) to_file = "" else: + if ( + filed + and await _filing_stands_after_the_write(state, log, key, to_file) is not True + ): + # The write landed, but the folder went away while it was in + # flight; the filing is withdrawn again (or its withdrawal is + # unconfirmed, which the helper logs), so this pass surfaces + # the conversation unfiled and eligible either way. + logger.debug( + "channel reconcile: the folder of %s was deleted while the filing " + "wrote; filing withdrawn", + key, + ) + filed = False if not filed: # The guard rejected it under the lock: the record gained a # placement or a filing marker while this write queued. That @@ -1654,9 +1818,24 @@ def _read_folder_tags(folders: list[dict[str, Any]], fid: str = folder_id) -> li log.update_metadata_if, key, filing_meta, - needs_backfill_filing, + # Placement rule AND the folder still standing, both read at + # the commit boundary under the record's lock: a stalled write + # must not land a folder id a delete committed away meanwhile. + _filing_write_guard(state, folder_id, needs_backfill_filing), require_existing=True, ) + # Landed, but into a folder a delete removed while the write was + # in flight? Then the filing is withdrawn again and this + # conversation is neither mirrored nor counted as moved. A + # withdrawal that could not be confirmed leaves a record that may + # still name the folder: that is a failed write in the report, + # not a silent success. + if filed: + stands = await _filing_stands_after_the_write(state, log, key, folder_id) + if stands is not True: + filed = False + if stands is None: + write_failures += 1 # Mirror the persisted placement onto the open tab, INSIDE the # lock and against a freshly read slot. Without this the tab keeps # showing the conversation at the top level until a restart @@ -1695,12 +1874,15 @@ def _read_folder_tags(folders: list[dict[str, Any]], fid: str = folder_id) -> li write_failures += 1 continue if not filed: - # Two refusals reach here and neither is retried. The guard saw a - # placement or a filing marker that landed while this pass ran -- - # the user's own action, so it stands. Or the conversation was - # DELETED while this pass ran, in which case there is nothing left - # to file. Neither is counted as a failure: both are decisions, not - # errors, and no write was attempted. + # Three refusals reach here and none is retried by this click. The + # guard saw a placement or a filing marker that landed while this pass + # ran -- the user's own action, so it stands. Or the conversation was + # DELETED while this pass ran, in which case there is nothing left to + # file. Or the write landed but the folder was frozen or gone by then + # and the filing was withdrawn again: the folder this button files + # into is going or gone, so another click against it would be a dead + # end (the pass above reports ``folder_gone`` once it sees that). + # None is counted as a failure: all are decisions, not errors. continue report["moved"].append( { diff --git a/src/kiro_crew/dashboard/chat_folders.py b/src/kiro_crew/dashboard/chat_folders.py index d1705df79d64..e44d901fcfcd 100644 --- a/src/kiro_crew/dashboard/chat_folders.py +++ b/src/kiro_crew/dashboard/chat_folders.py @@ -10,6 +10,7 @@ import unicodedata import uuid import weakref +from collections.abc import Callable from pathlib import Path from typing import Any @@ -236,6 +237,12 @@ async def generate_emoji_for_name(state: DashboardState, name: str) -> str: #: to survive a restart for. Entries are dropped on folder delete. _CHAT_FOLDER_ICON_EPOCHS: dict[str, int] = {} +#: The ``SlotCloseError`` code the cascade's own pre-pop check raises when a +#: session left the subtree while its close awaited. Internal to the cascade: it +#: is caught in the same loop and never reaches a response, so it is not part of +#: the route's error-code contract. +_CODE_LEFT_SUBTREE = "folder_subtree_left" + def _bump_icon_epoch(folder_id: str) -> None: """Invalidate any in-flight icon generation for this folder. @@ -404,23 +411,139 @@ def folder_ids_filed_into(state: DashboardState) -> set[str]: return set(ids) if isinstance(ids, set) else set() -async def _unhide_folder(state: DashboardState, folder_id: str) -> bool: +def _deleting_folder_ids(state: DashboardState) -> set[str]: + """The folder ids a running delete has frozen -- its whole subtree, on the + cascade -- for the request's duration. + + Only ever touched on the event loop: frozen inside a folder-store callback + (so the subtree it records is the committed tree, read under the store lock), + thawed synchronously when the delete's request ends, and consulted by the + store's own callbacks and by synchronous on-loop checks -- none of which can + interleave with a thaw, so a check never sees the set half-updated. A frozen + folder refuses every mutation that would move the ground under the delete: + filing a session into it (:func:`_unhide_folder` and the session-birth + existence checks report it absent), creating a child under it, reparenting it + or reparenting into it, reordering it, deleting it again. Attached lazily like + ``_folders_filed_into`` because it is this module's bookkeeping, not the + store's; empty between deletes. + """ + ids = getattr(state, "_folders_deleting", None) + if not isinstance(ids, set): + ids = set() + setattr(state, "_folders_deleting", ids) + return ids + + +def is_folder_id(folder_id: object) -> bool: + """Whether *folder_id* has the shape of a folder id: a non-empty string. + + A stored ``folder_id`` comes back from a session's metadata line, in a file an + agent's tools can edit, so it can be anything JSON holds. Only a non-empty + string can name a folder; every other shape is unfiled to a reader and absent + to a verdict, so the caller's own clear path heals the record. The freeze + predicates below hash their argument, and a list or a dict would raise there + on every resume of that session instead. + """ + return isinstance(folder_id, str) and bool(folder_id) + + +def folder_is_deleting(state: DashboardState, folder_id: str) -> bool: + """Whether a running delete has frozen *folder_id*. Call on the event loop. + + Anything that is not a folder id (see :func:`is_folder_id`) is not frozen. + """ + return is_folder_id(folder_id) and folder_id in _deleting_folder_ids(state) + + +def _filed_in(slot: Any, folder_ids: set[str]) -> bool: + """Whether *slot*'s filing names one of *folder_ids*. + + The delete route's membership tests all go through here: ``slot.folder_id`` + is restored from a metadata line anyone can edit, and a list or a dict there + would raise on the set lookup and fail every folder delete while that session + is live. A value that is not a folder id is filed nowhere. + """ + return is_folder_id(slot.folder_id) and slot.folder_id in folder_ids + + +def folder_is_gone(state: DashboardState, folder_id: str) -> bool: + """Whether *folder_id* names a folder that is GONE: not in the committed tree + and not frozen by a running delete. Only a gone folder may clear a stored filing. + + The synchronous last word for an assignment whose folder verdict was earned + before an await -- the resume's ``_unhide_folder`` reading, the channel + surface's stored filing. A delete that committed inside that await swept the + live slot table before this slot was in it, so the verdict has to be asked + again with nothing able to run between the answer and the assignment. A + folder that is gone leaves the assignment dangling, so the caller publishes + the session unfiled (and its next save persists that, as for any folder + deleted while the session was archived). + + FROZEN IS NOT GONE. A folder a running delete has frozen still exists, and + the delete may yet abort (a refused close answers 500 with the tree + untouched) -- so a stored filing into it is KEPT: the caller publishes the + session filed, in memory, and :meth:`DashboardState.serialize_slot` projects + the filing as unfiled for as long as the folder is frozen. The delete's own + commit-time sweep (``_remove``, under the store lock) unfiles every live slot + naming a removed folder if the delete commits; an abort thaws and the filing + renders again. Nothing but that sweep unfiles a stored filing on the strength + of a freeze. Frozen is checked FIRST, so a folder missing from the in-memory + list while its removal's disk write is still in flight (the store rolls the + list back if that write fails) also reads as frozen, not gone. + + Read off the loaded store, a membership test on a list this loop owns + (folder mutations run on it), like the cron placement's + ``chat_folder_exists``. Call on the event loop, with no await between the + call and the assignment it guards. + """ + if not folder_id: + return False + if not is_folder_id(folder_id): + # Not an id at all (a list, a dict): no folder carries it and no delete + # can have frozen it, so it is gone, and the caller's clear heals it. + return True + if folder_is_deleting(state, folder_id): + return False + return not any(str(f.get("id")) == folder_id for f in getattr(state, "_folders", None) or ()) + + +async def _unhide_folder( + state: DashboardState, folder_id: str, *, frozen_is_present: bool = False +) -> bool: """Clear a folder's `hidden` flag when a session re-engages it. Model-B semantics: reviving or moving a session into a folder un-hides it so it stays visible until the user hides it again. Persists on change; the caller is responsible for pushing the slots update. - Returns whether the folder EXISTS. Existence is reported from inside the - store lock, which is the only place it can be checked without a race: a - caller that validated against ``state._folders`` beforehand and then assigned - can have the folder deleted in between, and would persist a placement into a - folder that is gone. + Returns whether the folder EXISTS for the caller's purpose. Existence is + reported from inside the store lock, which is the only place it can be checked + without a race: a caller that validated against ``state._folders`` beforehand + and then assigned can have the folder deleted in between, and would persist a + placement into a folder that is gone. + + A folder a running delete has FROZEN (:func:`_deleting_folder_ids`) still + exists, is never written here, and reads two ways, which is what + *frozen_is_present* says. A caller about to make a NEW filing leaves it False + and reads the folder as absent: it is going, and a placement into it would + outlive it as a dangling id. A caller holding a STORED filing it would erase on + "absent" -- the resume, the import repair -- passes True and reads the folder + as present, keeping the filing: erasing it would unfile the session from a + folder that still exists if the delete then aborts, and if the delete commits, + its own commit-time sweep unfiles the slot. Frozen is the delete's to decide. """ if not folder_id: return True + if not is_folder_id(folder_id): + # Not an id at all: absent, whichever reading the caller asked for. The + # frozen check below hashes its argument, and this value came from a + # metadata line anyone can edit; reporting it absent lets the resume's + # clear path unfile the session instead of failing on every attempt. + return False def _clear(folders: list[dict[str, Any]]) -> tuple[bool, bool]: + if folder_is_deleting(state, folder_id): + return False, frozen_is_present for f in folders: if f["id"] == folder_id: if f.get("hidden"): @@ -1081,6 +1204,59 @@ def _is_descendant(folders: list[dict], *, ancestor_id: str, folder_id: str) -> return False +def _subtree_folder_ids(folders: list[dict[str, Any]], root_id: str) -> set[str]: + """``root_id`` plus the id of every folder anywhere under it. + + Empty when ``root_id`` is not in *folders*, so a caller that computes the set + under the store lock learns the root was deleted meanwhile from the result + rather than from an exception. Same cycle-guarded walk as + :func:`_is_descendant`. + """ + return { + str(f["id"]) + for f in folders + if f.get("id") and _is_descendant(folders, ancestor_id=root_id, folder_id=str(f["id"])) + } + + +def _clear_archived_folder_assignments(conversation_log: Any, folder_ids: set[str]) -> int: + """Unfile every ARCHIVED session filed under one of *folder_ids*; count them. + + Synchronous and lock-taking (``update_metadata_if`` enters the transcript's + cross-process flock), so the caller runs it off the event loop. Each write is + a compare-and-set on the on-disk record: it lands only while the transcript + still names one of these folders, and ``require_existing`` keeps a session + deleted since the listing from being recreated as a metadata-only stub. A + session whose write fails is logged and skipped -- its dangling ``folder_id`` + degrades to unfiled everywhere a reader meets it (the resume path drops it), + so a partial pass leaves nothing broken, only a row this delete did not tidy. + """ + cleared = 0 + for session in conversation_log.list_sessions(): + if str(session.get("folder_id") or "") not in folder_ids: + continue + key = str(session.get("key") or "") + if not key: + continue + try: + applied = conversation_log.update_metadata_if( + key, + {"folder_id": ""}, + lambda meta: str(meta.get("folder_id") or "") in folder_ids, + require_existing=True, + ) + except Exception: + logger.warning( + "folder delete: could not unfile archived session %s from a deleted folder", + key, + exc_info=True, + ) + continue + if applied: + cleared += 1 + return cleared + + class FolderCreateError(ValueError): """A folder could not be created because the request was refused. @@ -1325,6 +1501,11 @@ def _append(folders: list[dict[str, Any]]) -> tuple[bool, str]: parent = next((f for f in folders if f["id"] == parent_id), None) if parent_id else None if parent_id and parent is None: return False, "parent_not_found" + if parent_id and folder_is_deleting(state, parent_id): + # A delete running on that subtree has frozen it: a child created now + # would be born under a folder that is going, with sessions no pass of + # that delete has seen. + return False, "parent_deleting" # The ceiling is tested here, under the lock, for the same reason the parent # is re-checked here: `len(folders)` is only authoritative while the lock is # held, so a pre-lock test lets concurrent creators each pass a cap that is @@ -1385,6 +1566,8 @@ def _append(folders: list[dict[str, Any]]) -> tuple[bool, str]: if create_err == "parent_not_found": # The parent was deleted while this request waited for the lock. raise FolderCreateError("parent folder not found", "folder_parent_not_found") + if create_err == "parent_deleting": + raise FolderCreateError("parent folder is being deleted", "folder_parent_deleting") if create_err == "forbidden_parent": raise FolderOwnershipError() if create_err == "project_dir_exists": @@ -1743,6 +1926,15 @@ def _apply(folders: list[dict[str, Any]]) -> tuple[bool, str]: target = next((f for f in folders if f["id"] == fid), None) if target is None: return False, "not_found" + # A delete running on this folder's subtree has frozen it: nothing about + # it may change, and nothing may be moved into it, until that delete + # ends -- a reparent out would carry sessions that delete has already + # committed to archiving, a reparent in would hand it sessions it has not + # seen. + if folder_is_deleting(state, fid) or ( + reparenting and new_parent and folder_is_deleting(state, new_parent) + ): + return False, "deleting" # Ownership, decided here for the same reason the cycle rule is: a # concurrent reparent can change who the target or the destination # belongs to between validation and the write. @@ -1821,6 +2013,11 @@ def _bump_epoch_on_commit() -> None: if err == "not_found": # Deleted between the validation above and acquiring the store lock. return web.json_response({"error": "not found", "code": "folder_not_found"}, status=404) + if err == "deleting": + # Frozen by a delete that is still running; the tree it describes is going. + return web.json_response( + {"error": "folder is being deleted", "code": "folder_deleting"}, status=409 + ) if err in ("not_owned", "forbidden_parent", "foreign_descendant"): # Distinguished in the audit, not to the caller: one code for all three # keeps the response from reporting which folder was foreign. @@ -2036,6 +2233,8 @@ def _apply(folders: list[dict[str, Any]]) -> tuple[bool, str]: target = by_id.get(fid) if target is None: return False, "not_found" + if folder_is_deleting(state, fid): + return False, "deleting" if request_app and _folder_owner_app(target) != request_app: return False, "not_owned" # Ownership of the row itself is not the whole rule: repositioning a @@ -2090,6 +2289,13 @@ def _apply(folders: list[dict[str, Any]]) -> tuple[bool, str]: }, status=409, ) + if err == "deleting": + # Same reading for a folder a running delete has frozen: the tree the + # reorder describes is changing under it, so none of it lands. + return web.json_response( + {"error": "a folder in the reorder is being deleted", "code": "folder_deleting"}, + status=409, + ) if err == "not_owned": # One row named a folder this app does not own. Refused whole, and # distinguished only in the audit -- the same one code for the caller @@ -2139,7 +2345,23 @@ def _apply(folders: list[dict[str, Any]]) -> tuple[bool, str]: async def api_chat_folder_delete(request: web.Request) -> web.Response: - """DELETE /api/chat/folders/{id} — delete a folder, ungroup its slots.""" + """DELETE /api/chat/folders/{id}?delete_contents= — delete a folder. + + Default (``delete_contents`` falsy) is the SAFE path and today's behaviour: + unfile the folder's live sessions to the top level, re-parent its direct + child folders to the top level, and remove only this folder. Nothing but the + one folder row is destroyed. + + ``delete_contents=true`` removes the whole subtree in one step -- the case is + a shift folder holding a hundred per-ticket subfolders, which the safe path + can only dissolve one level at a time. Every descendant folder is deleted; + every live session filed anywhere in the subtree is ARCHIVED through + :func:`chat_handlers.close_slot`, the exact path the tab ✕ takes (saved to + history as closed, resumable later -- never a hard delete of a transcript); + and archived sessions still filed under the subtree have that assignment + cleared so no transcript keeps naming a folder that is gone. The response + carries the counts. Same shape as ``DELETE /api/artifact-folders/{id}``. + """ state: DashboardState = request.app["state"] if (refusal := _refuse_unattributable_caller(state, request)) is not None: @@ -2148,6 +2370,8 @@ async def api_chat_folder_delete(request: web.Request) -> web.Response: target = next((f for f in state._folders if f["id"] == fid), None) if target is None: return web.json_response({"error": "not found"}, status=404) + raw_cascade = (request.query.get("delete_contents") or "").strip().lower() + delete_contents = raw_cascade in ("1", "true", "yes") request_app = _effective_request_app(state, request) # Answered before a single slot is unfiled, so the common refusal costs no # rollback. Sound pre-lock because ``owner_app`` is stamped at create and no @@ -2178,12 +2402,14 @@ async def api_chat_folder_delete(request: web.Request) -> web.Response: # session closing after the scan and writing its folder_id on the way out. # Each was closable in isolation; the class was not. # - # Nothing shipped loses a capability: no MCP tool exposes folder deletion - # (the set is chat_folder_tree / chat_folder_create / chat_folder_move / - # chat_folder_move_session), and the only client of this route is the - # dashboard UI, which is the person. An app organizes its own work by - # creating, renaming and reparenting its folders and filing its sessions -- - # cleanup is the person's, who can delete a full folder as they always could. + # No agent principal loses a capability it had: the MCP verb + # ``chat_folder_delete`` (``mcp_dashboard``) is a thin wrapper over this + # route under the caller's OWN session identity, so an app agent's call + # lands here and gets this refusal, and a crew member's is refused by the + # member gate before it (no DELETE is admitted on the folder routes). An app + # organizes its own work by creating, renaming and reparenting its folders + # and filing its sessions -- cleanup is the person's, whose own sessions + # (the dashboard UI, or their agent through the verb) delete a full folder. if request_app: sel().log_api_access( caller=request_app, @@ -2203,6 +2429,162 @@ async def api_chat_folder_delete(request: web.Request) -> web.Response: }, status=403, ) + # The folders this delete empties: the one row on the safe path, the whole + # subtree on the cascade. Computed ONCE, under the store lock, and FROZEN + # there for the request's duration (``_deleting_folder_ids``). The sessions + # live in a different store from the folders, so no reading of "what is in + # this folder" can be atomic with its removal -- instead the tree is held + # still: while the set is frozen no folder in it can be reparented, reordered, + # deleted again or given a child, nothing can be reparented into it, and + # every filing path that consults the store (``_unhide_folder``, + # ``arrival_folder_exists``) refuses to file a session into it. So the set the + # archive pass walks IS the set ``_remove`` deletes, and a session moved OUT + # of it meanwhile is a rescue this delete honours (the pre-pop check below), + # not a race it loses. Thawed in ``finally``, whichever way the request ends. + deleting = _deleting_folder_ids(state) + taken: set[str] = set() + + # The freeze goes through ``mutate_folders`` reporting "no change", the way + # ``_unhide_folder`` does for a folder already visible: it runs under the + # store lock -- so the subtree it reads is the committed tree and no callback + # can see the set half-updated -- without rewriting the store. + def _freeze(folders: list[dict[str, Any]]) -> tuple[bool, set[str] | None]: + if not any(f.get("id") == fid for f in folders): + return False, set() + subtree = _subtree_folder_ids(folders, fid) if delete_contents else {fid} + if subtree & deleting: + # Another delete holds part of this tree; two cascades over one + # subtree would each archive what the other is committed to. + return False, None + taken.update(subtree) + deleting.update(subtree) + return False, subtree + + def _thaw(ids: set[str]) -> None: + # Release *ids* from the freeze -- called by ``_delete_frozen`` the + # moment ``_remove`` has committed, with exactly the ids it removed. + # Synchronous and lock-free like the ``finally`` below, and for the same + # reason. ``taken`` follows so the ``finally`` releases only what this + # request still holds. + deleting.difference_update(ids) + taken.difference_update(ids) + + try: + frozen = await state.mutate_folders(_freeze) + if frozen is None: + return web.json_response( + {"error": "folder is being deleted", "code": "folder_deleting"}, status=409 + ) + if not frozen: + # Deleted between the lookup above and the lock. + return web.json_response({"error": "not found", "code": "folder_not_found"}, status=404) + return await _delete_frozen(request, state, fid, delete_contents, frozen, thaw=_thaw) + finally: + # Synchronous and lock-free on purpose: the set is only ever touched on + # this loop, the store callbacks that read it are synchronous, so nothing + # can be mid-read here -- and a thaw that awaited the store would leave + # the tree frozen for good if the store were the thing that just failed. + # ``taken`` is what the callback actually took, so a freeze call that + # raised after its callback ran is released too. + deleting.difference_update(taken) + if taken: + # Something was still frozen at the end, so this request did not + # commit it: a refused close, a failed commit, a store failure. The + # slot payload projected every filing into those folders as unfiled + # while they were frozen (``serialize_slot``); thawed, the filings + # stand as stored, so publish once more and they render filed again. + state.push_slots_update() + + +async def _delete_frozen( + request: web.Request, + state: DashboardState, + fid: str, + delete_contents: bool, + target_ids: set[str], + *, + thaw: Callable[[set[str]], None], +) -> web.Response: + """The body of :func:`api_chat_folder_delete`, run while *target_ids* is frozen. + + Archive (cascade) or unfile (safe path) the live sessions filed in + *target_ids*, commit the removal of exactly that set, then tidy the archived + rows it covered. Split from the route so the freeze around it is one + ``try``/``finally`` rather than a thaw at every exit. *thaw* releases ids from + that freeze early: it is called with the committed ids the moment the removal + lands, so the post-commit tidy runs against folders that read as gone rather + than as frozen (see the call below). + """ + archived: list[str] = [] + if delete_contents: + # Archive, do not unfile: "delete everything in it" means the sessions + # leave the sidebar too, and the only sanctioned way a session leaves it + # is the tab-✕ close -- saved to history as closed, with every rollback + # and resurrection guard that path carries. Nothing here is a hard + # delete; every one of these conversations is still in History. + # + # A close that refuses stops the cascade: the folder tree is untouched, + # the sessions archived so far are in History still filed under their + # (still existing) folders, so the person sees exactly the state the + # tab-✕ would have left and retries. Archiving is not undone -- there is + # no "un-close", and a refused close is the recoverable outcome. + from kiro_crew.dashboard.chat_handlers import ( # circular: chat_handlers imports this module + SlotCloseError, + close_slot, + ) + + for name, slot in list(state._slots.items()): + # Re-check the registration: each close awaits, and a name can be + # popped and re-minted for another conversation meanwhile. + if state._slots.get(name) is not slot or not _filed_in(slot, target_ids): + continue + if slot.is_closing: + # Another retraction owns this slot (a ✕ mid-flight, the idle + # sweep); it lands archived without a second close racing it. + continue + + def _still_in_subtree(name: str = name, slot: Any = slot) -> None: + # close_slot's point of no return, SYNCHRONOUS: the membership + # read above went stale across its awaits (nudge retirement, the + # app hook), and a move committed meanwhile (a drag out of the + # subtree, or a re-mint of the name) makes this a session the + # person just placed elsewhere. Raising unwinds the close and + # leaves it live where it now sits; the code is ours to catch. + if state._slots.get(name) is not slot or not _filed_in(slot, target_ids): + raise SlotCloseError( + "session left the folder subtree during the close", + code=_CODE_LEFT_SUBTREE, + ) + + try: + await close_slot(state, slot, name, pre_pop_check=_still_in_subtree) + except SlotCloseError as exc: + if exc.code == _CODE_LEFT_SUBTREE: + # Not ours to archive any more; the cascade goes on without it. + continue + source, caller = _audit_origin(request) + sel().log_api_access( + caller=caller, + operation="chat.folder_delete", + outcome="error", + source=source, + resources=f"{fid} delete_contents session={name}", + error=exc.code, + ) + return web.json_response( + { + "error": ( + f"folder not deleted: session {name} could not be archived " + f"({exc.message}); {len(archived)} session(s) were archived " + "before it and are in History" + ), + "code": exc.code, + "archived_sessions": len(archived), + "session": name, + }, + status=500, + ) + archived.append(name) # Unfile the folder's slots first, then commit the folder removal. If that # commit fails, put the slots back: otherwise the delete half-lands — # conversations persistently unfiled while the folder they came from is @@ -2210,33 +2592,62 @@ async def api_chat_folder_delete(request: web.Request) -> web.Response: # ordering leaves a partial-commit window on its own (folder-first strands a # dangling folder_id; slots-first strands unfiled conversations), and only # undoing the half that did land closes both. + # + # On the cascade this loop normally finds nothing -- the archive pass above + # closed every session in the subtree -- and catches the two it could not: + # a slot another close already owned, and one a path that does not consult + # the store (a fork inheriting its parent's folder, a channel or cron + # placement) filed into the frozen subtree meanwhile. Those stay live, + # unfiled, rather than being archived by a second pass that would race the + # first. + # + # A list snapshot, re-checked per entry, like the archive pass: the save + # below awaits, and a close landing meanwhile (a ✕ mid-flight, the stale-slot + # sweep) pops the table. Iterating the live view raises ``RuntimeError`` on + # the step after that pop -- outside the rollback ``try`` below, with slots + # already unfiled and the folder still present. A name popped or re-minted + # since the snapshot is skipped; its own close carries it. + # + # A slot an active close owns is left to that close, here and in the commit's + # sweep: its archival save is the close's own and unguarded, while every save + # THIS route makes for it is refused by the close fence and only marks the + # slot dirty -- a flush that never comes for a slot the close pops. Unfiling + # it in memory would make the close persist "" and, should the commit then + # fail, leave the rollback's restore refused too: an archived conversation + # durably unfiled from a folder that still exists. Left alone, its closing + # save carries the folder it sat in; on a committed cascade the post-commit + # pass clears that row with the rest, and until then a dangling id is inert + # at every reader. unfiled: list[tuple[Any, str]] = [] - for slot in state._slots.values(): - if slot.folder_id == fid: - unfiled.append((slot, slot.folder_id)) - # Pin the write to the transcript this iteration's membership - # check covered: the save awaits inside the loop, so a rebind can - # land mid-persist and the save would otherwise resolve its - # target from the moved routing at write time. No await between - # this capture and the unfile below. - authorized_history_key = slot_history_key(slot) - slot.folder_id = "" - if not await save_slot_off_loop( - state, slot, force=True, expected_history_key=authorized_history_key - ): - # Refused without writing (session permanently deleted or - # rebound mid-persist). The in-memory unfile stands — the - # folder is being removed — so mark dirty and let the - # periodic flush persist wherever the slot now routes; a - # dangling folder_id left on the old transcript is ignored - # on the next load. - slot._dirty = True - logger.warning( - "folder delete: unfile save refused for %s " - "(session deleted or rebound); marked dirty for " - "periodic-flush retry", - getattr(slot, "key", "?"), - ) + for name, slot in list(state._slots.items()): + if state._slots.get(name) is not slot or not _filed_in(slot, target_ids): + continue + if slot.is_closing: + continue + unfiled.append((slot, slot.folder_id)) + # Pin the write to the transcript this iteration's membership + # check covered: the save awaits inside the loop, so a rebind can + # land mid-persist and the save would otherwise resolve its + # target from the moved routing at write time. No await between + # this capture and the unfile below. + authorized_history_key = slot_history_key(slot) + slot.folder_id = "" + if not await save_slot_off_loop( + state, slot, force=True, expected_history_key=authorized_history_key + ): + # Refused without writing (session permanently deleted or + # rebound mid-persist). The in-memory unfile stands — the + # folder is being removed — so mark dirty and let the + # periodic flush persist wherever the slot now routes; a + # dangling folder_id left on the old transcript is ignored + # on the next load. + slot._dirty = True + logger.warning( + "folder delete: unfile save refused for %s " + "(session deleted or rebound); marked dirty for " + "periodic-flush retry", + getattr(slot, "key", "?"), + ) async def _restore_unfiled() -> None: for slot, previous in unfiled: @@ -2282,20 +2693,109 @@ async def _restore_unfiled() -> None: ) state.push_slots_update() - def _remove(folders: list[dict[str, Any]]) -> tuple[bool, None]: + swept: list[tuple[str, Any]] = [] + + def _remove(folders: list[dict[str, Any]]) -> tuple[bool, set[str]]: + # Exactly the frozen set, on both paths: it is the set the archive and + # unfile passes walked, and while it was frozen no folder could join it, + # leave it or be removed by another request, so the subtree under the + # lock now IS the snapshot. Present ids only, so a torn row cannot make + # the commit report an id it never removed. + removed = {str(f["id"]) for f in folders if f.get("id") in target_ids} + # The safe path re-parents the row's direct children to the top level; + # on the cascade nothing should hang off a removed folder any more, and + # the same line keeps the tree acyclic-and-rooted if something does. for f in folders: - if f.get("parent_id") == fid: + parent = f.get("parent_id") + if f.get("id") not in removed and isinstance(parent, str) and parent in removed: f["parent_id"] = "" + # The last word on live assignments, in the same synchronous step as the + # removal: a filing path that never consulted the store or the freeze (a + # cron placement, a fork inheriting its parent's folder, a channel + # default) can have set a slot's ``folder_id`` since the unfile loop's + # snapshot. Nothing can interleave between this sweep and the commit -- + # both run inside this callback -- so the commit cannot leave a live + # slot naming a folder it removes, save one an active close owns (its + # closing save and the post-commit pass carry that one). The persist + # follows the commit (below); the rollback covers these entries like + # the loop's own. + for name, slot in list(state._slots.items()): + if state._slots.get(name) is slot and _filed_in(slot, removed): + if slot.is_closing: + # The close's own save carries this slot (see the unfile + # loop): a pre-commit unfile here could not be rolled back. + continue + unfiled.append((slot, slot.folder_id)) + swept.append((name, slot)) + slot.folder_id = "" # In place, not a rebind: mutate_folders snapshots the list object it # was given, and other holders of state._folders must see the removal. - folders[:] = [f for f in folders if f["id"] != fid] - return True, None + folders[:] = [f for f in folders if f.get("id") not in removed] + return True, removed try: - await state.mutate_folders(_remove) - except Exception: + removed_ids = await state.mutate_folders(_remove) + except Exception as exc: await _restore_unfiled() + if delete_contents: + # The archive pass is not undone -- there is no "un-close" -- and + # the folder store rolled its list back, so this is the state a + # refused close leaves: the sessions closed so far are in History, + # still filed under folders that exist. A retry finds nothing live + # left to archive and commits. Record what landed, since the + # ``allowed`` row below never will. + source, caller = _audit_origin(request) + sel().log_api_access( + caller=caller, + operation="chat.folder_delete", + outcome="error", + source=source, + resources=f"{fid} delete_contents archived={len(archived)}", + error=type(exc).__name__, + ) raise + # The commit's own sweep ran inside ``_remove``; between it and here the + # store's disk write awaited, and a session published in that window -- a + # resume that built while the folder was frozen -- keeps its filing (frozen + # is not gone: a filing is never dropped on the strength of a freeze), so it + # now names a folder that is committed away. Sweep once more, synchronously, + # before the thaw: nothing can interleave between this pass and the thaw, so + # after it no live slot names a removed folder. Persisted with the swept saves + # below, and counted with them. + for name, slot in list(state._slots.items()): + if state._slots.get(name) is slot and _filed_in(slot, removed_ids): + unfiled.append((slot, slot.folder_id)) + swept.append((name, slot)) + slot.folder_id = "" + # Thaw the committed ids NOW, before the post-commit tidy awaits (the swept + # saves, the history scan below): the freeze's "frozen is present" reading + # exists so a resume or an import landing mid-cascade keeps a stored filing + # for the commit-time sweep to decide -- but that sweep has just run, and + # cannot see a slot published after it. Left frozen, a removed folder would + # keep answering "present" to those two readers across these awaits, and a + # resume landing here would publish a live slot filed under a folder that is + # already gone -- one the sidebar renders in no lane. Thawed, the same + # readers find the folder absent and drop the filing, which is the ordinary + # dangling-id path. Only the committed ids: a frozen id the commit did not + # remove stays held until the route's ``finally``. + thaw(removed_ids) + for name, slot in swept: + # Persist what the sweep unfiled, the way the loop persists its own: a + # slot closed meanwhile carried the unfile in its closing save; one that + # is still live gets the pinned forced save, and a refused save marks it + # dirty for the periodic flush, exactly as above. + if state._slots.get(name) is not slot: + continue + authorized_history_key = slot_history_key(slot) + if not await save_slot_off_loop( + state, slot, force=True, expected_history_key=authorized_history_key + ): + slot._dirty = True + logger.warning( + "folder delete: commit-time unfile save refused for %s " + "(session deleted or rebound); marked dirty for periodic-flush retry", + getattr(slot, "key", "?"), + ) # Pop the epoch only after the removal is confirmed persisted. Popping # inside the callback would be a module-level side effect that survives a # failed store write: the folder would still exist while its epoch read 0 @@ -2303,27 +2803,61 @@ def _remove(folders: list[dict[str, Any]]) -> tuple[bool, None]: # a confirmed delete the entry has nothing left to guard (the write-back # already drops results for a folder it cannot re-find); popping keeps the # dict from growing with every deleted-folder id over the process lifetime. - _CHAT_FOLDER_ICON_EPOCHS.pop(fid, None) - # Cancel the folder's pending icon generation and drop its registry entry. - # Without this, an owner looping create->delete accumulates one queued + # + # Cancel each removed folder's pending icon generation and drop its registry + # entry. Without this, an owner looping create->delete accumulates one queued # task per deleted folder behind the serialized generator — each holds a # strong reference and a slot in the one-at-a-time model queue. The cancel # is safe after a confirmed delete: a write already started finishes under # the shield, and its write-back re-finds the folder by id, which no # longer exists, so nothing lands. - pending = _CHAT_FOLDER_PENDING_ICON_TASKS.pop(fid, None) - if pending is not None and not pending.done(): - pending.cancel() + for gone in removed_ids: + _CHAT_FOLDER_ICON_EPOCHS.pop(gone, None) + pending = _CHAT_FOLDER_PENDING_ICON_TASKS.pop(gone, None) + if pending is not None and not pending.done(): + pending.cancel() + cleared = 0 + if delete_contents and removed_ids and state.conversation_log is not None: + # After the commit, never before: an archived session filed under a + # folder that still exists is correctly filed, and a commit that failed + # above must leave those rows exactly as they were. The pass covers the + # sessions the archive pass just closed (their closing save carried the + # folder they sat in) and anything archived there earlier. Off the loop: + # each write takes the transcript's flock. + cleared = await asyncio.to_thread( + _clear_archived_folder_assignments, state.conversation_log, removed_ids + ) state.push_slots_update() + if cleared: + state.push_refresh("history") source, caller = _audit_origin(request) sel().log_api_access( caller=caller, operation="chat.folder_delete", outcome="allowed", source=source, - resources=fid, + resources=( + f"{fid} delete_contents folders={len(removed_ids)} archived={len(archived)} " + f"unfiled={len(unfiled)} history_unfiled={cleared}" + if delete_contents + else fid + ), + ) + if not delete_contents: + # The safe path answers ``{ok}`` only: its one client ignores the body, + # and the counts are the cascade's contract (its second confirm step + # shows them). + return web.json_response({"ok": True}) + return web.json_response( + { + "ok": True, + "delete_contents": True, + "deleted_folder_ids": sorted(removed_ids), + "unfiled_sessions": len(unfiled), + "archived_sessions": len(archived), + "unfiled_history_sessions": cleared, + } ) - return web.json_response({"ok": True}) # The authorization-identity helper is homed in ``token_auth`` beside the rule it @@ -2471,12 +3005,30 @@ async def api_chat_slot_folder(request: web.Request) -> web.Response: # compare-and-set below stays as defense for the non-endpoint writers # (the folder-delete unfile loop) that do not take this lock. async with _slot_meta_txn_lock(state): - # Re-authorize after the awaits above (body parse, lock acquisition): - # same slot OBJECT still registered under the name, routing still on - # the transcript captured before the first await. No await between - # this check and the mutation below; the _unhide_folder and persist - # awaits after it are covered by the save's pin. The generation token - # is checked in the same breath: a mismatch means the caller resolved a + # The destination's verdict FIRST, before anything is assigned. The + # existence check above read the store unlocked, so a delete can land + # between it and here; _unhide_folder re-checks under the store lock, + # which is the only place the answer cannot go stale, and reads a + # folder a running delete has frozen as absent to this new filing. + # Nothing on the slot is touched until it has answered: a provisional + # ``folder_id`` written before this await would be live for its + # duration, and a delete cascade running meanwhile reads the live field + # in its membership pass (and again, synchronously, at close_slot's + # point of no return) -- it would archive a session on a move this + # route then rejects. Reject rather than persist a placement into a + # folder that is gone. When the verdict is True the coroutine resumes + # without suspending, so nothing can change the answer before the + # assignment below. + if not await _unhide_folder(state, folder_id): + return web.json_response( + {"error": "folder not found", "code": "folder_not_found"}, status=400 + ) + # Re-authorize after the awaits above (body parse, lock acquisition, + # the verdict): same slot OBJECT still registered under the name, + # routing still on the transcript captured before the first await. No + # await between this check and the mutation below; the persist await + # after it is covered by the save's pin. The generation token is + # checked in the same breath: a mismatch means the caller resolved a # slot that has since been replaced under its key. if ( state._slots.get(name) is not slot @@ -2501,16 +3053,6 @@ async def api_chat_slot_folder(request: web.Request) -> web.Response: if folder_id != slot.folder_id: slot._folder_changed = True # re-inject [FOLDER] breadcrumb on next turn slot.folder_id = folder_id - # The check above reads the store unlocked, so a delete can land between it - # and here. _unhide_folder re-checks existence under the store lock, which - # is the only place the answer cannot go stale — reject rather than persist a - # placement into a folder that no longer exists. - if not await _unhide_folder(state, folder_id): - slot.folder_id = previous - slot._folder_changed = previous_changed - return web.json_response( - {"error": "folder not found", "code": "folder_not_found"}, status=400 - ) if not await save_slot_off_loop( state, slot, force=True, expected_history_key=authorized_history_key ): diff --git a/src/kiro_crew/dashboard/chat_handlers.py b/src/kiro_crew/dashboard/chat_handlers.py index dd3e9046a60a..4bf2ccadf272 100644 --- a/src/kiro_crew/dashboard/chat_handlers.py +++ b/src/kiro_crew/dashboard/chat_handlers.py @@ -58,6 +58,9 @@ ) from kiro_crew.dashboard.chat_folders import ( _unhide_folder, + folder_is_deleting, + folder_is_gone, + is_folder_id, resolve_folder_project_dir_off_loop, ) from kiro_crew.dashboard.chat_persistence import ( @@ -4490,21 +4493,22 @@ async def api_chat_slot_create(request: web.Request) -> web.Response: # model believing the session is still in its old folder. # Harmless on the new-slot path: that turn is `is_new`, so the # breadcrumb fires regardless and the flag is consumed there. - previous_folder = slot.folder_id - previous_changed = slot._folder_changed - if folder_id != slot.folder_id: - slot._folder_changed = True - slot.folder_id = folder_id - # Existence is only reliable inside the store lock. If the folder - # went away, abandon THIS assignment and leave the slot as it was — - # `name` can address an already-used slot, so clearing outright would - # unfile a conversation that was sitting in a perfectly good folder - # of its own. This is a chat turn, so declining the move beats - # failing the turn. - if not await _unhide_folder(state, folder_id): - slot.folder_id = previous_folder - slot._folder_changed = previous_changed - else: + # Existence is only reliable inside the store lock, and the verdict + # comes FIRST: nothing on the slot is assigned until the store has + # answered. `name` can address an already-used, live slot, and a + # provisional folder_id written before this await would be read by + # a delete cascade running meanwhile (its membership pass, and + # close_slot's synchronous point of no return) -- it would archive + # a session on a filing this turn then declines. If the folder went + # away, abandon THIS assignment and leave the slot as it was: + # clearing outright would unfile a conversation that was sitting in + # a perfectly good folder of its own. This is a chat turn, so + # declining the move beats failing the turn. On True the coroutine + # resumes without suspending, so the answer holds at the assignment. + if await _unhide_folder(state, folder_id): + if folder_id != slot.folder_id: + slot._folder_changed = True + slot.folder_id = folder_id folder_applied = True if is_new_slot: # Folder-tag inheritance, creation-only. A brand-new @@ -12975,12 +12979,27 @@ def _hydrate_slot_from_history( # made outside it can go stale, so re-deriving one here against # ``state._folders`` is the race its own docstring warns about; and it # cannot simply be re-run, because a second await here would reopen the - # publish-to-hydrate window this ordering exists to close. Holding no - # verdict for a newly filed id, we KEEP it: a dangling id is visible and - # self-corrects on the next folder operation, whereas erasing a live - # filing is silent and indistinguishable from the user unfiling the - # session -- and the dirty-slot flush would then persist that erasure. - if not folder_unhidden and meta["folder_id"] == folder_checked_id: + # publish-to-hydrate window this ordering exists to close. The resume + # re-asks synchronously about the id it binds right before this build + # (``folder_is_gone``) and passes that as the verdict, so a folder that + # went away during its awaits lands here as ``folder_unhidden=False`` for + # this very id. Holding no verdict for the id (the import path passes + # none), we KEEP it: erasing a live filing is silent and + # indistinguishable from the user unfiling the session, and the + # dirty-slot flush would then persist that erasure; a dangling id renders + # unfiled and is replaced by the slot's next folder write. + # + # And NEVER for a FROZEN folder, whatever the verdict says: a delete is + # running on it and may abort with the tree untouched, so the stored + # filing is the delete's to unfile (its commit-time sweep, on success), + # not this hydrate's. The slot payload projects the filing as unfiled + # while the folder is frozen; ``folder_id`` is a clearable field the + # next save writes, so clearing it here would erase the filing durably. + if ( + not folder_unhidden + and meta["folder_id"] == folder_checked_id + and not folder_is_deleting(state, meta["folder_id"]) + ): slot.folder_id = "" if meta.get("pinned"): slot.pinned = True @@ -13421,7 +13440,15 @@ async def resume_slot_from_history( folder_checked_id = "" if meta.get("folder_id"): folder_checked_id = meta["folder_id"] - folder_unhidden = await _unhide_folder(state, folder_checked_id) + # ``frozen_is_present``: a folder a running delete has frozen still + # exists, so this verdict does not erase the stored filing -- a delete + # that then aborts leaves a folder this session is still rightly in. + # Whether the folder is still THERE is asked again synchronously, right + # before the build below (``folder_is_gone``): gone by then, the session + # publishes unfiled; frozen, it publishes with its filing kept, which the + # slot payload projects as unfiled while the delete runs and the delete's + # own commit-time sweep unfiles if the delete commits. + folder_unhidden = await _unhide_folder(state, folder_checked_id, frozen_is_present=True) cleared_closed: bool = False cleared_closed_at: Any = meta.get("closed_at") # With a containment hook the clear is DEFERRED until the hook has passed: @@ -13792,6 +13819,25 @@ def _same_transcript(current: dict) -> bool: "this session is being resumed elsewhere; try again", "resume_in_progress", 409 ) ) + # The folder verdict above was earned BEFORE the awaits between it and here + # (the reopen write, the agent restore, the binding read). A folder delete + # committing inside one of them ran its commit-time sweep over the live slot + # table while this slot did not exist yet, so a filing it would have unfiled + # is about to be built and published with a folder that is gone. Ask again + # HERE, synchronously, about the id the hydrate binds (the post-await + # snapshot's, which a reconciliation may have changed since the verdict): + # GONE, the session publishes unfiled. FROZEN is not gone: the filing is kept + # -- the slot registers with it, so the delete's commit-time sweep unfiles it + # if the delete commits, the payload projects it unfiled meanwhile, and an + # abort leaves it standing. Nothing suspends between this read and the build. + # Only a folder id is re-asked about. A stored value of another shape (a + # hand-edited metadata line) already holds the ``_unhide_folder`` verdict + # False for that very value, which the hydrate clears; coercing it to a + # string here would make the two ids differ and keep the value instead. + stored_folder_id = meta.get("folder_id") + bound_folder_id = stored_folder_id if is_folder_id(stored_folder_id) else "" + if bound_folder_id and folder_is_gone(state, bound_folder_id): + folder_checked_id, folder_unhidden = bound_folder_id, False slot = _materialise_slot_from_history( state, name=name, @@ -14037,6 +14083,13 @@ def _identity_refusal(post: dict, readable: bool) -> ResumeRefusal | None: refusal = final_check(slot) if refusal is not None: return ResumeOutcome(refusal=(await _discard()) or refusal) + # The slot sat OUTSIDE the table across the hook's awaits, so a folder + # delete committing meanwhile swept without seeing it. Same synchronous + # re-ask as before the build, on the folder the built slot carries: + # gone, it publishes unfiled; frozen, it keeps the filing for the + # delete's sweep to decide. + if slot.folder_id and folder_is_gone(state, slot.folder_id): + slot.folder_id = "" state._slots[slot.key] = slot except BaseException: await asyncio.shield(_discard()) diff --git a/src/kiro_crew/dashboard/cron_inject.py b/src/kiro_crew/dashboard/cron_inject.py index ab3aa04b0ed7..064ec57e2e01 100644 --- a/src/kiro_crew/dashboard/cron_inject.py +++ b/src/kiro_crew/dashboard/cron_inject.py @@ -398,6 +398,16 @@ def chat_folder_exists(state: DashboardState, folder_id: str) -> bool: """ if not folder_id: return False + # circular import: chat_folders -> chat_tags -> handlers (its package init + # imports this module) -> here, so chat_folders cannot be imported at module + # level; the same reason ``_commit_cron_tab_placement`` imports it locally. + from kiro_crew.dashboard.chat_folders import folder_is_deleting + + # A folder a running delete has frozen is going: a placement into it would + # outlive it as a dangling id, so it reads as absent here, as it does for + # every other filing path that consults the store. + if folder_is_deleting(state, folder_id): + return False return any(str(f.get("id")) == folder_id for f in getattr(state, "_folders", None) or ()) @@ -468,6 +478,21 @@ async def _commit_cron_tab_placement( from kiro_crew.dashboard.chat_utils import slot_history_key async with _slot_meta_txn_lock(state): + # Re-asked SYNCHRONOUSLY at the assignment, after the lock's await: the + # target was resolved before this task was scheduled, and a delete of + # that folder (or the freeze that precedes one) can have landed since. + # Nothing suspends between this answer and the assignment, and the + # delete's own commit unfiles under the folder-store lock whatever it + # still finds, so the two can never leave a placement into a folder + # that is gone. + if placed and not chat_folder_exists(state, placed): + logger.info( + "Cron '%s': folder %s is gone or being deleted; %s stays where it is", + job.name, + placed, + slot.key, + ) + return False slot.folder_id = placed authorized_history_key = slot_history_key(slot) committed = False diff --git a/src/kiro_crew/dashboard/session_control.py b/src/kiro_crew/dashboard/session_control.py index f94275eab4f2..65a3e57d35f4 100644 --- a/src/kiro_crew/dashboard/session_control.py +++ b/src/kiro_crew/dashboard/session_control.py @@ -67,6 +67,7 @@ _resolve_folder_project_dir, _slot_meta_txn_lock, _unhide_folder, + folder_is_deleting, note_folder_filed, ) from kiro_crew.dashboard.chat_fork import ( @@ -2446,7 +2447,11 @@ def _current_folder_project( folders: list[dict[str, Any]], ) -> tuple[bool, str | None, str | None]: tree = _safe_folder_tree(folders) - exists = any(str(folder.get("id") or "") == folder_id for folder in tree) + # A folder a running delete has frozen reads as absent: filing into + # it would outlive it as a dangling id (``chat_folders``' freeze). + exists = not folder_is_deleting(state, folder_id) and any( + str(folder.get("id") or "") == folder_id for folder in tree + ) raw_project, error = _folder_declared_project(tree, folder_id) return exists, raw_project, error @@ -3000,6 +3005,10 @@ async def fork_session( # `create_session` does and for the same reasons; the Model-B un-hide runs # only once the filing has landed on the child. def _exists(folders: list[dict[str, Any]]) -> bool: + # A folder a running delete has frozen reads as absent: filing into + # it would outlive it as a dangling id (``chat_folders``' freeze). + if folder_is_deleting(state, folder_id): + return False return any(str(f.get("id") or "") == folder_id for f in _safe_folder_tree(folders)) if not await state.read_folders(_exists): @@ -3075,6 +3084,8 @@ def _folder_exists_now() -> bool: # this loop, so between this read and the assignment `_stamp` makes there # is no point at which a delete can land. Same value `read_folders` hands # its reader; the lock there exists for readers that hop off the loop. + if folder_is_deleting(state, folder_id): + return False # frozen by a running delete: going, so absent here too return any(str(f.get("id") or "") == folder_id for f in _safe_folder_tree(state._folders)) def _recheck() -> None: @@ -5878,8 +5889,6 @@ def _audit_allowed(filed_now: bool) -> None: # Re-read under the lock: filed there meanwhile by another writer # means nothing to do, and nothing this call may later roll back. if folder_id != previous: - slot.folder_id = folder_id - slot._folder_changed = True def _roll_back() -> None: if slot.folder_id == folder_id: @@ -5887,26 +5896,35 @@ def _roll_back() -> None: slot._folder_changed = previous_changed try: - if not await _unhide_folder(state, folder_id): - _roll_back() - elif await save_slot_off_loop( - state, - slot, - force=True, - # Strict, not best-effort: the default swallows a raised - # save and answers True (marking the slot dirty for the - # periodic flush), which would report ``filed: true`` for - # a placement that is not on disk and is lost if the - # gateway restarts before that flush. A raise takes the - # rollback arm below and reports ``filed: false``. - best_effort=False, - expected_history_key=slot_history_key(slot), - ): - note_folder_filed(state, folder_id) - filed = True - else: - _roll_back() - slot._dirty = True + # The destination's verdict before anything is assigned: + # the revived slot is live and published, and a + # provisional folder_id here would be read by a delete + # cascade's membership pass (and close_slot's synchronous + # point of no return) while this await is pending -- a + # session archived on a filing then refused. On True the + # coroutine resumes without suspending before the + # assignment. + if await _unhide_folder(state, folder_id): + slot.folder_id = folder_id + slot._folder_changed = True + if await save_slot_off_loop( + state, + slot, + force=True, + # Strict, not best-effort: the default swallows a raised + # save and answers True (marking the slot dirty for the + # periodic flush), which would report ``filed: true`` for + # a placement that is not on disk and is lost if the + # gateway restarts before that flush. A raise takes the + # rollback arm below and reports ``filed: false``. + best_effort=False, + expected_history_key=slot_history_key(slot), + ): + note_folder_filed(state, folder_id) + filed = True + else: + _roll_back() + slot._dirty = True except Exception: logger.warning( "revive_session: %s revived but filing it into %s failed", diff --git a/src/kiro_crew/dashboard/session_transfer.py b/src/kiro_crew/dashboard/session_transfer.py index a56c86b70167..2d445025fa3b 100644 --- a/src/kiro_crew/dashboard/session_transfer.py +++ b/src/kiro_crew/dashboard/session_transfer.py @@ -3020,7 +3020,11 @@ async def _refuse_as_deleted(witness: str) -> web.Response: status=409, ) - if slot.folder_id and not await arrival_folder_exists(state, slot.folder_id): + # ``frozen_is_present``: a folder a running delete has frozen still exists; + # the stored filing is kept and that delete's commit decides it. + if slot.folder_id and not await arrival_folder_exists( + state, slot.folder_id, frozen_is_present=True + ): logger.info( "session_transfer: arrival folder %s went away during import of %s; " "leaving the session unfiled", diff --git a/src/kiro_crew/dashboard/state.py b/src/kiro_crew/dashboard/state.py index b1cbbd0b6354..396465af454f 100644 --- a/src/kiro_crew/dashboard/state.py +++ b/src/kiro_crew/dashboard/state.py @@ -9183,17 +9183,51 @@ def append_link( return links, True, visible_slack_channel, slack_ts or "" return links, False, "", "" + def _known_folder_ids(self) -> set[str]: + """The ids a slot's ``folder_id`` must be in to project as filed: the ids + the loaded folder store carries, minus the ones a running delete has frozen + (``chat_folders._deleting_folder_ids``, attached to this state as + ``_folders_deleting``). ``getattr``: read on ``__new__``-built states.""" + frozen = getattr(self, "_folders_deleting", None) or () + return {str(f.get("id")) for f in getattr(self, "_folders", None) or ()} - set(frozen) + def serialize_slot( self, slot: _ChatSlot, *, include_check_status: bool = False, dashboard_user: bool = False, + known_folder_ids: set[str] | None = None, ) -> dict[str, Any]: - """Serialize one slot with state-backed channel-link metadata.""" + """Serialize one slot with state-backed channel-link metadata. + + ``known_folder_ids`` lets a caller serializing many slots compute the + folder-id set once (:meth:`serialize_slots`); omitted, it is read here. + """ payload = slot.to_dict( include_check_status=include_check_status, dashboard_user=dashboard_user ) + # A dangling folder id -- one no folder record carries -- projects as + # UNFILED, and so does a filing into a folder a running delete has + # FROZEN. Every slot payload the sidebar reads passes through here (the + # full list, the ``slot_patch`` frame, the single-slot answers), so this + # is the one place the reading is made harmless: the sidebar files a + # session by its folder id and would otherwise draw a dangling one in no + # lane at all (its folder block does not exist, and the unfiled lane + # excludes it). For a frozen folder this is what "the session surfaces + # unfiled while the delete runs" means: the stored filing is untouched + # -- the delete's commit-time sweep unfiles it if the delete commits, and + # an abort thaws and re-publishes, after which the filing renders again. + # A projection only: ``slot.folder_id`` is left as stored, nothing is + # written from this read path, and a dangling id is replaced by the slot's + # next folder write. ``getattr``: this runs on ``__new__``-built states too. + # The stored value is restored from a file an agent's tools can edit, so a + # shape that is not a string is a dangling id too, not a crash of the feed. + folder_id = payload.get("folder_id") + if folder_id: + known = self._known_folder_ids() if known_folder_ids is None else known_folder_ids + if not isinstance(folder_id, str) or folder_id not in known: + payload["folder_id"] = "" links, slack_linked, slack_channel, slack_thread_ts = self._slot_links(slot) payload.update( { @@ -9234,6 +9268,7 @@ def serialize_slots( from kiro_crew.dashboard.chat_utils import effective_session_key under_construction = getattr(self, "_slots_under_construction", None) or () + known_folder_ids = self._known_folder_ids() for s in self._slots.values(): if s.key in under_construction: continue @@ -9242,6 +9277,7 @@ def serialize_slots( s, include_check_status=include_check_status, dashboard_user=dashboard_user, + known_folder_ids=known_folder_ids, ) d["subagents_running"] = bool( subs and subs.running_agents_for(effective_session_key(s)) diff --git a/src/kiro_crew/data/mcp_tool_titles.json b/src/kiro_crew/data/mcp_tool_titles.json index 1d63be545a9f..cc6cd8d6f3c8 100644 --- a/src/kiro_crew/data/mcp_tool_titles.json +++ b/src/kiro_crew/data/mcp_tool_titles.json @@ -87,6 +87,7 @@ "kirocrew-dashboard": { "chat_folder_tree": "Read folder tree", "chat_folder_create": "Create folder", + "chat_folder_delete": "Delete folder", "chat_folder_move": "Move folder", "chat_folder_move_session": "Move session to folder", "chat_folder_file_self": "File this session", diff --git a/src/kiro_crew/docs/index.md b/src/kiro_crew/docs/index.md index 8a27a4134040..0a342044978f 100644 --- a/src/kiro_crew/docs/index.md +++ b/src/kiro_crew/docs/index.md @@ -49,7 +49,7 @@ index, first-time setup, and connecting messaging channels. | [Artifacts](artifacts.md) | Save, version, and revert generated UI and documents so they outlive the chat scrollback | | [Monitor Loops](monitor-loops.md) | Keep one session checking something on an interval — a pull request, a CI run, a deployment — until an exit condition fires | | [Session Ledger](session-ledger.md) | A durable per-session record of goal, phase, and next step that survives context compaction | -| [Session Control](session-control.md) | Let one chat session open, fork, seed, watch, broadcast to, stop, switch models on, reload, close and revive another one — the 28 `kirocrew-dashboard` MCP tools, plus the sidebar folders, tags, board columns and pins that keep them findable | +| [Session Control](session-control.md) | Let one chat session open, fork, seed, watch, broadcast to, stop, switch models on, reload, close and revive another one — the 29 `kirocrew-dashboard` MCP tools, plus the sidebar folders, tags, board columns and pins that keep them findable | | [Work Ledger](work-ledger.md) | Split a goal across one session per item: a conductor dispatches workers, reads their status as data, and settles every completion claim against an acceptance condition | | [Browser Control](browser-control.md) | Drive a real web page from the dashboard's Browser panel: navigate, snapshot, click, type, screenshot | | [Computer Use](computer-use.md) | Read and drive native desktop applications through the accessibility layer; opt-in and off by default | diff --git a/src/kiro_crew/docs/session-control.md b/src/kiro_crew/docs/session-control.md index 8f078e65787d..55c63641dec3 100644 --- a/src/kiro_crew/docs/session-control.md +++ b/src/kiro_crew/docs/session-control.md @@ -5,7 +5,7 @@ change its model, reload its agent process, and take another one under itself in the sidebar. The tools come from the `kirocrew-dashboard` MCP server, so an agent that does not mount that server never has them — exactly like any other MCP server. This page is the reference -for all 28 of its tools, written for the agent that is about to use them. +for all 29 of its tools, written for the agent that is about to use them. The server is defined in `src/kiro_crew/mcp_dashboard.py`. Two halves: @@ -13,7 +13,7 @@ The server is defined in `src/kiro_crew/mcp_dashboard.py`. Two halves: `session_read_message`, `session_summary`, `session_stop`, `session_end_wait`, `session_set_model`, `session_reload`, `session_close`, `session_revive`, `session_broadcast`, `session_status`, `session_adopt`, `session_release`. These reach another session. -- **Sidebar shape** — `chat_folder_tree`, `chat_folder_create`, +- **Sidebar shape** — `chat_folder_tree`, `chat_folder_create`, `chat_folder_delete`, `chat_folder_move`, `chat_folder_move_session`, `chat_folder_file_self`, `chat_tag_list`, `chat_tag_create`, `chat_tag_update`, `chat_tag_assign`, `chat_tag_column_list`, `chat_tag_column_create`, `chat_tag_column_move`, @@ -519,6 +519,7 @@ The sidebar tree the person organizes their sessions in. |---|---|---| | `chat_folder_tree` | none | Every folder (id, human path, project dir, default agent) with the live sessions nested under it, plus an `(unfiled)` group. Listed in **sidebar order**, not alphabetically | | `chat_folder_create` | `name` (required), `parent` | Create a folder. `parent` is an id or a `/`-separated path; missing segments are created (`mkdir -p`). Omit or pass `root` for top level. Creating never moves anything | +| `chat_folder_delete` | `folder` (required), `delete_contents` | Delete a folder. Default: unfile its live sessions and re-parent its child folders to the top level, remove the one row. `delete_contents=true` removes the whole subtree and ARCHIVES every live session in it (the tab-close path, resumable from History) — echo the affected count to the person first. The person's own sessions may delete; an app agent or crew member is refused by the endpoint | | `chat_folder_move` | `folder` (required), `new_parent`, `before`, `after` | Reparent a folder and/or set its position among siblings. Moves everything inside it; cycle-guarded | | `chat_folder_move_session` | `session` (required), `folder` | File another live session into a folder, or omit `folder` to unfile it to the top level | | `chat_folder_file_self` | `folder` | File **this** session — the caller — into a folder. Writes only its own placement | diff --git a/src/kiro_crew/mcp_core.py b/src/kiro_crew/mcp_core.py index 688150945e72..3f99dd82d94e 100644 --- a/src/kiro_crew/mcp_core.py +++ b/src/kiro_crew/mcp_core.py @@ -1768,14 +1768,29 @@ def _put(path: str, body: dict | None = None, session_key: str | None = None) -> return _send(path, data=data, headers=headers, method="PUT") -def _delete(path: str, body: dict | None = None) -> dict: +def _delete( + path: str, + body: dict | None = None, + *, + session_key: str | None = None, + timeout: float = 10, +) -> dict: + """DELETE a loopback gateway path with the internal-secret handshake. + + ``session_key``: as in :func:`_patch`. A caller gated on + :func:`_resolve_session_key_strict` must send the key it verified -- the + chat-folder delete does, since the endpoint judges the write against the + identity that reaches it. ``None`` keeps the lenient resolution the artifact + tools rely on. ``timeout``: a cascade delete archives every session in the + subtree before it answers, so its caller raises it. + """ data = json.dumps(body or {}).encode() if body else None headers = { "X-Internal-Secret": _internal_secret(), **_caller_header(), **_session_token_header(), } - sk = _resolve_session_key() + sk = _resolve_session_key() if session_key is None else session_key _sk_err = _session_key_header_error(sk) if _sk_err: return {"error": _sk_err} @@ -1783,7 +1798,7 @@ def _delete(path: str, body: dict | None = None) -> dict: headers["X-Session-Key"] = sk if data: headers["Content-Type"] = "application/json" - return _send(path, data=data, headers=headers, method="DELETE", timeout=10) + return _send(path, data=data, headers=headers, method="DELETE", timeout=timeout) def _autonudge_binding_key(sk: str) -> str | None: diff --git a/src/kiro_crew/mcp_dashboard.py b/src/kiro_crew/mcp_dashboard.py index 746c9611dcd4..ed8a092a9d32 100644 --- a/src/kiro_crew/mcp_dashboard.py +++ b/src/kiro_crew/mcp_dashboard.py @@ -87,6 +87,7 @@ _subtree_holds_foreign_folder, ) from kiro_crew.mcp_core import ( + _delete, _get, _patch, _post, @@ -102,6 +103,7 @@ BROADCAST_RESPONSE_MARGIN_SECS, BROADCAST_TARGET_ALLOWANCE_SECS, CHAT_FOLDER_CREATE_SCHEMA, + CHAT_FOLDER_DELETE_SCHEMA, CHAT_FOLDER_FILE_SELF_SCHEMA, CHAT_FOLDER_MOVE_SCHEMA, CHAT_FOLDER_MOVE_SESSION_SCHEMA, @@ -235,6 +237,46 @@ def _tool_definitions() -> list[dict[str, Any]]: "required": ["name"], }, }, + { + "name": "chat_folder_delete", + "description": ( + "Delete a sidebar chat folder. By default (delete_contents=false) " + "this is SAFE: the folder's live sessions are unfiled to the top " + "level, its direct child folders are re-parented to the top level, " + "and only the folder itself is removed. Pass delete_contents=true to " + "remove the entire subtree in one step, INCLUDING its sessions: every " + "descendant folder is deleted and every live session filed anywhere " + "in it is ARCHIVED to History (the tab-close path -- closed and " + "resumable, never a deleted transcript), archived sessions filed " + "there are unfiled -- echo the affected count to the user before " + "doing so: chat_folder_tree shows the subtree and the live sessions " + "filed in each folder (it lists no archived counts, so say the " + "archived sessions filed there are unfiled as well). ``folder`` = " + "folder id or human path from chat_folder_tree. The person's own " + "sessions may delete; an app agent or a crew member is refused, " + "whoever created the folder -- move your sessions out and leave the " + "folder, or ask the person. The sidebar's own delete dialog is " + "separate; this is the agent-facing consumer of the route." + ), + "inputSchema": { + "type": "object", + "properties": { + "folder": { + "type": "string", + "description": "Folder to delete (id or human path).", + }, + "delete_contents": { + "type": "boolean", + "description": ( + "false (default) = unfile the folder's sessions and re-parent " + "its children to the top level, remove the one folder. true = " + "remove the whole subtree and archive every session in it." + ), + }, + }, + "required": ["folder"], + }, + }, { "name": "chat_folder_move", "description": ( @@ -3038,6 +3080,48 @@ def _session_line(row: dict, indent: str) -> str: new_path = _chat_folder_paths(chat_folders).get(new_id) or str(d.get("name") or "?") return redact(f"Created folder `{new_path}` (id={new_id}).{made_note}") + if name == "chat_folder_delete": + args = validate_tool_args(args, CHAT_FOLDER_DELETE_SCHEMA) + caller_key, _caller_app, gate = _refuse_tree_shaping_if_unverifiable("deleting a folder") + if gate: + return gate + chat_folders, folders_err = _get_rows("/api/chat/folders") + if folders_err: + return f"Error: {folders_err}" + fld_id, fld_err = _resolve_chat_folder_id(args["folder"], chat_folders) + if fld_err: + return f"Error: {fld_err}" + if not fld_id: + return "Error: 'root' is not a folder — name the folder to delete." + cascade = bool(args.get("delete_contents")) + fld_path = _chat_folder_paths(chat_folders).get(fld_id) or fld_id + # A thin wrapper: the endpoint owns every rule (the 404, the app and + # member refusals, the frozen-subtree cascade), so nothing is decided + # here but the caller's verifiability above. The verified key is passed + # through unchanged, as every tree-shaping write here does. The cascade + # archives each live session in the subtree before it answers, so it + # gets a longer wait than a one-row delete. + d = _delete( + f"/api/chat/folders/{quote(fld_id, safe='')}{'?delete_contents=true' if cascade else ''}", + session_key=caller_key, + timeout=120 if cascade else 10, + ) + if d.get("error"): + return redact(f"Error: {d['error']}") + if not cascade: + return redact( + f"Deleted folder `{fld_path}` (id={fld_id}); its live sessions are " + "unfiled and its child folders now sit at the top level." + ) + n_folders = len(d.get("deleted_folder_ids") or []) + return redact( + f"Deleted folder `{fld_path}` (id={fld_id}) and everything in it: " + f"{n_folders} folder{'' if n_folders == 1 else 's'} removed, " + f"{d.get('archived_sessions', 0)} live session(s) archived to History, " + f"{d.get('unfiled_history_sessions', 0)} stored session filing(s) cleared, " + f"{d.get('unfiled_sessions', 0)} filing(s) cleared from live sessions during the deletion." + ) + if name == "chat_folder_move": args = validate_tool_args(args, CHAT_FOLDER_MOVE_SCHEMA) caller_key, caller_app, gate = _refuse_tree_shaping_if_unverifiable("moving a folder") diff --git a/src/kiro_crew/validation.py b/src/kiro_crew/validation.py index 47f0161ff215..d52ee5631be0 100644 --- a/src/kiro_crew/validation.py +++ b/src/kiro_crew/validation.py @@ -2441,6 +2441,16 @@ def _validate_webapp_metadata_shape(am: dict) -> None: ], ) +# Same two fields as ``artifact_folder_delete``: the chat-folder delete mirrors +# the artifact-folder one (``DELETE /api/chat/folders/{id}?delete_contents=``). +CHAT_FOLDER_DELETE_SCHEMA = ToolSchema( + tool_name="chat_folder_delete", + fields=[ + FieldSpec("folder", str, required=True, max_len=_ARTIFACT_FOLDER_REF_MAX), + FieldSpec("delete_contents", bool), + ], +) + CHAT_FOLDER_MOVE_SESSION_SCHEMA = ToolSchema( tool_name="chat_folder_move_session", fields=[ @@ -3932,6 +3942,7 @@ def _cu_coord_field(name: str, *, required: bool = False) -> FieldSpec: "chat_folder_tree": CHAT_FOLDER_TREE_SCHEMA, "chat_folder_create": CHAT_FOLDER_CREATE_SCHEMA, "chat_folder_move": CHAT_FOLDER_MOVE_SCHEMA, + "chat_folder_delete": CHAT_FOLDER_DELETE_SCHEMA, "chat_folder_move_session": CHAT_FOLDER_MOVE_SESSION_SCHEMA, "chat_folder_file_self": CHAT_FOLDER_FILE_SELF_SCHEMA, "chat_tag_list": CHAT_TAG_LIST_SCHEMA, diff --git a/test/test_arrival_folders.py b/test/test_arrival_folders.py index 737399e3a3f4..49a8e891ba5f 100644 --- a/test/test_arrival_folders.py +++ b/test/test_arrival_folders.py @@ -133,6 +133,60 @@ async def test_a_first_arrival_creates_the_group_and_the_sender_folder(): assert folder_id == str(child["id"]) +@pytest.mark.asyncio +async def test_a_frozen_group_takes_no_new_sender_folder(): + """A delete running on ``Imported`` has frozen its subtree. A sender folder + appended under it now would be outside the set that delete walks and + commits, and would outlive it re-parented to the top level with the arrival + filed into it. Under the lock the freeze is authoritative, so the arrival is + refused a folder -- the empty filing every refusal path answers -- and the + tree is left exactly as it was.""" + from kiro_crew.dashboard.chat_folders import _deleting_folder_ids + + group = _row("Imported") + state = _store([group]) + _deleting_folder_ids(state).add(str(group["id"])) + filing = await af.arrival_folder_id(state, origin="mac", request_app="") + assert filing.folder_id == "" + assert filing.created_ids == () + assert _tree(state) == {"Imported": ""} + + +@pytest.mark.asyncio +async def test_a_frozen_hidden_group_owes_no_unhide(): + """The adopted branch defers the group's un-hide to the landed path. A + refusal on the freeze lands nothing, so it must not leave that un-hide + owed: an aborted delete would otherwise find the folder flipped visible.""" + from kiro_crew.dashboard.chat_folders import _deleting_folder_ids + + group = _row("Imported", hidden=True) + state = _store([group]) + _deleting_folder_ids(state).add(str(group["id"])) + filing = await af.arrival_folder_id(state, origin="mac", request_app="") + assert filing.folder_id == "" + assert filing.hidden_ids == () + assert group["hidden"] is True + + +@pytest.mark.asyncio +async def test_a_frozen_sender_folder_is_the_deletes_to_decide(): + """The sender's folder exists and a delete has frozen it (alone, or as part + of a subtree): the filing that would land in it is not made now.""" + from kiro_crew.dashboard.chat_folders import _deleting_folder_ids + + group = _row("Imported") + child = _row("from mac", str(group["id"])) + state = _store([group, child]) + _deleting_folder_ids(state).add(str(child["id"])) + filing = await af.arrival_folder_id(state, origin="mac", request_app="") + assert filing.folder_id == "" + assert _tree(state) == {"Imported": "", "from mac": str(group["id"])} + # Thawed, the same arrival is filed into that folder again. + _deleting_folder_ids(state).discard(str(child["id"])) + filing = await af.arrival_folder_id(state, origin="mac", request_app="") + assert filing.folder_id == str(child["id"]) + + @pytest.mark.asyncio async def test_both_levels_are_settled_in_one_transaction(): """Constraint 3. Two transactions would leave a window in which a delete of diff --git a/test/test_channel_folders.py b/test/test_channel_folders.py index 5dc4e8ec3bd0..6fee1db7dd5a 100644 --- a/test/test_channel_folders.py +++ b/test/test_channel_folders.py @@ -478,6 +478,56 @@ def _quiet_push(self, dashboard_state: Any) -> None: # Surfacing pushes a slots update, which serializes the whole state. dashboard_state.push_slots_update = lambda: None # type: ignore[method-assign] + @pytest.fixture(autouse=True) + def _folders_exist(self, dashboard_state: Any) -> None: + # The assignment re-asks whether the folder exists (and is not frozen by + # a running delete) right before it lands, so the ids this class files + # into are real rows; a dangling one surfaces unfiled, tested below. + for order, fid in enumerate(("f1", "user-choice")): + dashboard_state._folders.append( + {"id": fid, "name": fid, "parent_id": "", "order": order} + ) + + def test_a_stored_filing_into_a_folder_that_is_gone_surfaces_unfiled( + self, dashboard_state: Any + ) -> None: + """The channel-restore half of the late-assignment window: the stored + ``folder_id`` was never checked here, and a delete that committed while the + reconcile awaited swept the live table before this slot existed. Re-asked at + the assignment, a folder that is gone leaves the session unfiled; its next + save replaces the stored id.""" + slot = channel_slots.surface_channel_session( + dashboard_state, self._info(), {"folder_id": "gone-folder"}, [] + ) + assert slot is not None + assert slot.folder_id == "" + + def test_a_filing_into_a_folder_a_running_delete_has_frozen_is_kept_for_the_sweep( + self, dashboard_state: Any + ) -> None: + """Frozen is not gone. The folder still exists and its delete may abort, so + both the stored and the just-written filing are applied: the slot registers + with the folder, the delete's commit-time sweep unfiles it if the delete + commits, and the payload projects it as unfiled while the folder is frozen + -- filed again once an abort thaws it.""" + chat_folders._deleting_folder_ids(dashboard_state).add("f1") + try: + stored = channel_slots.surface_channel_session( + dashboard_state, self._info("discord:kirocrew:direct:U2"), {"folder_id": "f1"}, [] + ) + fresh = channel_slots.surface_channel_session( + dashboard_state, self._info(), {}, [], folder_id="f1", folder_tags=["t1"] + ) + assert stored is not None and stored.folder_id == "f1" + assert fresh is not None and fresh.folder_id == "f1" + assert fresh._channel_folder_filed is True and fresh.tags == ["t1"] + assert dashboard_state.serialize_slot(stored)["folder_id"] == "" + assert dashboard_state.serialize_slot(fresh)["folder_id"] == "" + finally: + chat_folders._deleting_folder_ids(dashboard_state).discard("f1") + assert dashboard_state.serialize_slot(stored)["folder_id"] == "f1" + assert dashboard_state.serialize_slot(fresh)["folder_id"] == "f1" + def test_files_a_newly_surfaced_session(self, dashboard_state: Any) -> None: slot = channel_slots.surface_channel_session( dashboard_state, self._info(), {}, [], folder_id="f1" @@ -879,6 +929,183 @@ def read_then_resume(k: str) -> list[dict[str, Any]]: ) assert not log.get_metadata(key).get("channel_folder_filed") + def test_a_filing_write_stalled_across_the_folders_delete_lands_no_deleted_id( + self, dashboard_state: Any + ) -> None: + """The filing write waits on the transcript's cross-process lock, and a + folder delete can commit while it waits -- its live-slot sweep and its + archived-row pass both done before this write ever runs. The folder is + therefore re-asked at the write's commit boundary, under the lock: gone, + the write is skipped, so no deleted id lands as a durable assignment and + the record carries no marker either (the next pass may file it into + whatever folder the channel then has).""" + _write_config("discord", "Discord") + key = "discord:kirocrew:direct:U1" + fid = asyncio.run( + channel_folders.ensure_channel_folder(dashboard_state, "discord", "Discord") + ) + log = _FakeLog([key]) + dashboard_state.conversation_log = log + dashboard_state.push_slots_update = lambda: None # type: ignore[method-assign] + real_write = log.update_metadata_if + + def _delete_commits_while_the_write_waits(key_: str, fields: dict, guard: Any) -> bool: + # What a committed cascade leaves behind by the time the stalled write + # acquires the lock: the folder out of the store, the freeze released. + dashboard_state._folders[:] = [f for f in dashboard_state._folders if f["id"] != fid] + return real_write(key_, fields, guard) + + log.update_metadata_if = _delete_commits_while_the_write_waits # type: ignore[assignment] + + asyncio.run(channel_slots.reconcile_channel_slots(dashboard_state, 0)) + + assert "folder_id" not in log.get_metadata(key), "the deleted folder id landed durably" + assert "channel_folder_filed" not in log.get_metadata(key) + slot = dashboard_state._slots[channel_slots.channel_slot_name(key)] + assert slot.folder_id == "" + + def test_a_filing_write_stalled_across_a_running_delete_is_skipped( + self, dashboard_state: Any + ) -> None: + """Same boundary, the delete still running: a frozen folder refuses the + write too. A delete that then aborts leaves the folder and a record that + carries no marker, still eligible for default filing; a delete that + commits never sees a filing to sweep.""" + _write_config("discord", "Discord") + key = "discord:kirocrew:direct:U1" + fid = asyncio.run( + channel_folders.ensure_channel_folder(dashboard_state, "discord", "Discord") + ) + log = _FakeLog([key]) + dashboard_state.conversation_log = log + dashboard_state.push_slots_update = lambda: None # type: ignore[method-assign] + real_write = log.update_metadata_if + + def _freeze_while_the_write_waits(key_: str, fields: dict, guard: Any) -> bool: + chat_folders._deleting_folder_ids(dashboard_state).add(fid) + try: + return real_write(key_, fields, guard) + finally: + chat_folders._deleting_folder_ids(dashboard_state).discard(fid) + + log.update_metadata_if = _freeze_while_the_write_waits # type: ignore[assignment] + + asyncio.run(channel_slots.reconcile_channel_slots(dashboard_state, 0)) + + assert "folder_id" not in log.get_metadata(key) + assert "channel_folder_filed" not in log.get_metadata(key) + assert dashboard_state._slots[channel_slots.channel_slot_name(key)].folder_id == "" + # No marker was written, so the record stays eligible for default filing + # the next time this conversation is surfaced (the reconcile files pending + # conversations, not live slots): a delete that aborts costs nothing durable. + assert channel_slots.needs_default_filing(log.get_metadata(key)) + + @pytest.mark.parametrize("what", ["gone", "frozen-then-committed", "frozen-then-aborted"]) + def test_a_filing_that_landed_as_its_folder_was_deleted_is_withdrawn( + self, dashboard_state: Any, what: str + ) -> None: + """The guard reads the folder under the record's lock right before the + write, but the folder store has its own lock: a delete can freeze, commit + and finish its archived-row pass while the write itself is in flight, and + that pass lists rows before this one lands. Re-asked on the loop the moment + the write is known to have landed, a gone folder withdraws the filing: the + record names no folder and carries no marker, so the conversation surfaces + unfiled and stays eligible. A FROZEN folder is not gone: the pass waits for + the running delete's verdict -- a commit withdraws the filing exactly like + gone, an abort keeps it, since the filing is as good as any other the + abort retains.""" + _write_config("discord", "Discord") + key = "discord:kirocrew:direct:U1" + fid = asyncio.run( + channel_folders.ensure_channel_folder(dashboard_state, "discord", "Discord") + ) + log = _FakeLog([key]) + dashboard_state.conversation_log = log + dashboard_state.push_slots_update = lambda: None # type: ignore[method-assign] + real_write = log.update_metadata_if + landed: list[dict[str, Any]] = [] + + def _remove_the_folder() -> None: + dashboard_state._folders[:] = [f for f in dashboard_state._folders if f["id"] != fid] + + def _write_then_the_delete_lands(key_: str, fields: dict, guard: Any) -> bool: + # The guard passes and the write lands with the folder still standing... + ok = real_write(key_, fields, guard) + if ok and not landed: + landed.append(dict(log.get_metadata(key_))) + # ...and the delete gets in before the loop resumes this pass. + if what == "gone": + _remove_the_folder() + else: + chat_folders._deleting_folder_ids(dashboard_state).add(fid) + return ok + + log.update_metadata_if = _write_then_the_delete_lands # type: ignore[assignment] + + async def _run() -> None: + pass_ = asyncio.ensure_future(channel_slots.reconcile_channel_slots(dashboard_state, 0)) + if what != "gone": + # The pass is parked on the freeze; the delete then ends one way or + # the other and thaws, which is what lets it decide. + for _ in range(20): + await asyncio.sleep(0.01) + if landed: + break + assert landed and not pass_.done(), "the pass waited for the verdict" + if what == "frozen-then-committed": + _remove_the_folder() + chat_folders._deleting_folder_ids(dashboard_state).discard(fid) + await pass_ + + try: + asyncio.run(_run()) + finally: + chat_folders._deleting_folder_ids(dashboard_state).discard(fid) + + assert landed and landed[0].get("folder_id") == fid, "fixture: the filing did land" + slot = dashboard_state._slots[channel_slots.channel_slot_name(key)] + if what == "frozen-then-aborted": + assert log.get_metadata(key).get("folder_id") == fid, "an abort lost the filing" + assert log.get_metadata(key).get("channel_folder_filed") + assert slot.folder_id == fid + return + assert not log.get_metadata(key).get("folder_id"), "a filing into a lost folder stood" + assert not log.get_metadata(key).get("channel_folder_filed") + assert channel_slots.needs_default_filing(log.get_metadata(key)) + assert slot.folder_id == "" + + def test_the_withdrawal_is_a_compare_and_set_on_what_this_pass_wrote( + self, dashboard_state: Any + ) -> None: + """A placement the user made in between is not the one withdrawn.""" + _write_config("discord", "Discord") + key = "discord:kirocrew:direct:U1" + fid = asyncio.run( + channel_folders.ensure_channel_folder(dashboard_state, "discord", "Discord") + ) + dashboard_state._folders.append( + {"id": "theirs", "name": "Theirs", "parent_id": "", "order": 9} + ) + log = _FakeLog([key]) + dashboard_state.conversation_log = log + dashboard_state.push_slots_update = lambda: None # type: ignore[method-assign] + real_write = log.update_metadata_if + once: list[int] = [] + + def _write_then_user_moves_then_delete(key_: str, fields: dict, guard: Any) -> bool: + ok = real_write(key_, fields, guard) + if ok and not once: + once.append(1) + log.update_metadata(key_, {"folder_id": "theirs"}) + dashboard_state._folders[:] = [ + f for f in dashboard_state._folders if f["id"] != fid + ] + return ok + + log.update_metadata_if = _write_then_user_moves_then_delete # type: ignore[assignment] + asyncio.run(channel_slots.reconcile_channel_slots(dashboard_state, 0)) + assert log.get_metadata(key).get("folder_id") == "theirs" + def test_a_session_surfaced_before_filing_was_on_is_not_filed_later( self, dashboard_state: Any ) -> None: @@ -1184,6 +1411,121 @@ def _racing(key_: str, fields: dict[str, Any], guard: Any) -> bool: assert slot.folder_id == "" +class TestFilingWriteBoundary: + """The two halves of the filing writers' commit boundary, on their own.""" + + def test_the_write_guard_reads_the_freeze_the_loop_owns(self, dashboard_state: Any) -> None: + """The frozen set is attached lazily; the guard resolves it on the loop when + it is built and the worker only reads it -- a first attach from a worker + thread racing the loop's first freeze would otherwise leave two sets.""" + dashboard_state._folders.append({"id": "f1", "name": "F1", "parent_id": "", "order": 0}) + guard = channel_slots._filing_write_guard(dashboard_state, "f1", lambda current: True) + assert guard({}) is True + # The loop freezes the folder through its own accessor: the guard sees it. + chat_folders._deleting_folder_ids(dashboard_state).add("f1") + try: + assert guard({}) is False + finally: + chat_folders._deleting_folder_ids(dashboard_state).discard("f1") + assert guard({}) is True + # The worker side never attaches a set of its own. + del dashboard_state._folders_deleting + assert guard({}) is True + assert not hasattr(dashboard_state, "_folders_deleting") + # Gone reads as refused, frozen or not. + dashboard_state._folders.clear() + assert guard({}) is False + # The placement rule still decides first. + assert ( + channel_slots._filing_write_guard(dashboard_state, "f1", lambda c: False)({}) is False + ) + + def test_a_withdrawal_reads_its_verdict_back_from_the_row(self, dashboard_state: Any) -> None: + """A refusal is not a commit. The conditional write's False is not taken + at face value: the row is re-read, and a row still naming this folder + (a transient refusal) is withdrawn on the retry.""" + key = "discord:kirocrew:direct:U1" + log = _FakeLog([key], {key: {"folder_id": "gone", "channel_folder_filed": True}}) + real = log.update_metadata_if + refusals: list[int] = [] + + def _refuse_once(k: str, fields: dict[str, Any], guard: Any, **kw: Any) -> bool: + if not refusals: + refusals.append(1) + return False # the store balked once; the row is still ours + return real(k, fields, guard, **kw) + + log.update_metadata_if = _refuse_once # type: ignore[method-assign] + stands = asyncio.run( + channel_slots._filing_stands_after_the_write(dashboard_state, log, key, "gone") + ) + assert stands is False + assert log.get_metadata(key) == {"folder_id": "", "channel_folder_filed": False} + + def test_a_row_settled_by_another_writer_is_left_alone(self, dashboard_state: Any) -> None: + key = "discord:kirocrew:direct:U1" + # The delete's archived-row pass already cleared the folder; the marker is + # its business, as for every archived session the cascade unfiles. + log = _FakeLog([key], {key: {"folder_id": "", "channel_folder_filed": True}}) + stands = asyncio.run( + channel_slots._filing_stands_after_the_write(dashboard_state, log, key, "gone") + ) + assert stands is False + assert log.get_metadata(key) == {"folder_id": "", "channel_folder_filed": True} + + def test_a_withdrawal_that_cannot_land_is_logged_by_key( + self, dashboard_state: Any, caplog: Any + ) -> None: + key = "discord:kirocrew:direct:U1" + log = _FakeLog([key], {key: {"folder_id": "gone", "channel_folder_filed": True}}) + log.update_metadata_if = lambda *a, **k: False # type: ignore[method-assign] + with caplog.at_level("ERROR"): + stands = asyncio.run( + channel_slots._filing_stands_after_the_write(dashboard_state, log, key, "gone") + ) + assert stands is None, "an unconfirmed withdrawal is not reported as one" + assert any(key in r.getMessage() and "gone" in r.getMessage() for r in caplog.records) + + def test_a_frozen_folder_is_waited_for_not_guessed(self, dashboard_state: Any) -> None: + """Frozen is not gone. The helper parks on the freeze and reads the verdict + the thaw reveals: the folder still there -> the filing stands; the folder + removed -> withdrawn like gone.""" + dashboard_state._folders.append({"id": "f1", "name": "F1", "parent_id": "", "order": 0}) + key = "discord:kirocrew:direct:U1" + log = _FakeLog([key], {key: {"folder_id": "f1", "channel_folder_filed": True}}) + + async def _run(commit: bool) -> bool | None: + chat_folders._deleting_folder_ids(dashboard_state).add("f1") + task = asyncio.ensure_future( + channel_slots._filing_stands_after_the_write(dashboard_state, log, key, "f1") + ) + await asyncio.sleep(0.12) + assert not task.done(), "the verdict was guessed while the folder was frozen" + if commit: + dashboard_state._folders[:] = [ + f for f in dashboard_state._folders if f["id"] != "f1" + ] + chat_folders._deleting_folder_ids(dashboard_state).discard("f1") + return await task + + try: + assert asyncio.run(_run(commit=False)) is True + assert log.get_metadata(key)["folder_id"] == "f1" + assert asyncio.run(_run(commit=True)) is False + assert log.get_metadata(key) == {"folder_id": "", "channel_folder_filed": False} + finally: + chat_folders._deleting_folder_ids(dashboard_state).discard("f1") + + def test_a_standing_folder_leaves_the_filing_alone(self, dashboard_state: Any) -> None: + dashboard_state._folders.append({"id": "f1", "name": "F1", "parent_id": "", "order": 0}) + key = "discord:kirocrew:direct:U1" + log = _FakeLog([key], {key: {"folder_id": "f1", "channel_folder_filed": True}}) + assert asyncio.run( + channel_slots._filing_stands_after_the_write(dashboard_state, log, key, "f1") + ) + assert log.get_metadata(key)["folder_id"] == "f1" + + class TestStampIsTheIdentity: """The channel's folder is found by its stamp, not by its configured name.""" @@ -1639,6 +1981,75 @@ def _explode(k: str, fields: dict[str, Any], guard: Any, **kwargs: Any) -> bool: assert [m["key"] for m in report["moved"]] == [good] + def test_a_filing_that_landed_as_its_folder_was_deleted_is_withdrawn_and_not_counted( + self, dashboard_state: Any + ) -> None: + """The backfill's write can land while a delete of the folder is committing on + the loop; re-asked the moment the write is known to have landed, a gone folder + withdraws the filing (no folder, marker taken back) and the conversation is + neither mirrored onto its tab nor reported as moved.""" + fid = self._folder(dashboard_state) + key = "discord:kirocrew:direct:U1" + log = _ModifiedLog([key], {key: {"channel_origin": True}}) + dashboard_state.conversation_log = log + tab = dashboard_state.get_or_create_slot(channel_slots.channel_slot_name(key)) + original = log.update_metadata_if + once: list[int] = [] + + def _write_then_delete(k: str, fields: dict[str, Any], guard: Any, **kwargs: Any) -> bool: + ok = original(k, fields, guard, **kwargs) + if ok and not once: + once.append(1) + dashboard_state._folders[:] = [ + f for f in dashboard_state._folders if f["id"] != fid + ] + return ok + + log.update_metadata_if = _write_then_delete # type: ignore[method-assign] + + report = asyncio.run(channel_slots.backfill_channel_folder(dashboard_state, "discord")) + + assert report["moved"] == [] + assert not log.get_metadata(key).get("folder_id") + assert not log.get_metadata(key).get("channel_folder_filed") + assert tab.folder_id == "" and tab._channel_folder_filed is False + + def test_an_unconfirmed_withdrawal_is_a_failed_write_in_the_report( + self, dashboard_state: Any + ) -> None: + """The filing landed, the folder is gone, and the compensating write cannot + be confirmed: the record may still name the folder, so the report carries + it as a failure rather than as a move -- an operator sees it, nothing + pretends the filing settled.""" + fid = self._folder(dashboard_state) + key = "discord:kirocrew:direct:U1" + log = _ModifiedLog([key], {key: {"channel_origin": True}}) + dashboard_state.conversation_log = log + real_write = log.update_metadata_if + landed: list[str] = [] + + def _land_then_lose_the_folder_and_the_store( + key_: str, fields: dict, guard: Any, **kw: Any + ) -> bool: + if landed: + return False # the withdrawal is refused, and refused again + ok = real_write(key_, fields, guard, **kw) + if ok: + landed.append(key_) + dashboard_state._folders[:] = [ + f for f in dashboard_state._folders if f["id"] != fid + ] + return ok + + log.update_metadata_if = _land_then_lose_the_folder_and_the_store # type: ignore[assignment] + report = asyncio.run(channel_slots.backfill_channel_folder(dashboard_state, "discord")) + assert landed, "fixture: the filing did land" + assert report["moved"] == [] + assert report["failed"] == 1 + assert report["remaining"] >= 1 + # The record still names the folder: the helper could not take it back. + assert log.get_metadata(key).get("folder_id") == fid + def test_a_folder_deleted_mid_pass_strands_nothing(self, dashboard_state: Any) -> None: """A dead ``folder_id`` beside the filing marker has NO recovery. @@ -1667,9 +2078,12 @@ def _delete_then_write(k: str, fields: dict[str, Any], guard: Any, **kwargs: Any report = asyncio.run(channel_slots.backfill_channel_folder(dashboard_state, "discord")) # Whatever it managed before the deletion is reported; nothing after it - # is stamped with the dead id. Its OWN reason: a folder DELETED mid-pass - # needs the opposite sentence from one that was never created, and the - # panel must not have to infer which it had from the counts. + # is stamped with the dead id -- including the very write the deletion + # landed in front of: the guard re-asks the folder at the commit boundary, + # under the record's lock, and skips a write into a folder that is gone. + # Its OWN reason: a folder DELETED mid-pass needs the opposite sentence + # from one that was never created, and the panel must not have to infer + # which it had from the counts. assert report["reason"] == "folder_gone" stranded = [ k @@ -1680,7 +2094,7 @@ def _delete_then_write(k: str, fields: dict[str, Any], guard: Any, **kwargs: Any for f in dashboard_state._folders ) ] - assert len(stranded) <= 1, stranded + assert stranded == [], stranded assert len(report["moved"]) == len(stranded) def test_a_drag_during_the_write_is_not_reverted_in_memory(self, dashboard_state: Any) -> None: diff --git a/test/test_channel_slots.py b/test/test_channel_slots.py index bbc6adb4784e..00e6b634abf6 100644 --- a/test/test_channel_slots.py +++ b/test/test_channel_slots.py @@ -493,6 +493,9 @@ def test_ignores_non_channel_keys(self, dashboard_state: Any) -> None: assert dashboard_state._slots == {} def test_applies_metadata(self, dashboard_state: Any) -> None: + # A stored filing is restored only for a folder that exists (a dangling + # id surfaces unfiled), so the folder is part of the fixture. + dashboard_state._folders.append({"id": "f1", "name": "F1", "parent_id": "", "order": 0}) slot = channel_slots.surface_channel_session( dashboard_state, _session("slack:1.1"), diff --git a/test/test_chat_folder_dangling_id.py b/test/test_chat_folder_dangling_id.py new file mode 100644 index 000000000000..6f2d8e477e13 --- /dev/null +++ b/test/test_chat_folder_dangling_id.py @@ -0,0 +1,209 @@ +"""A dangling ``folder_id`` -- one no folder record carries -- is inert. + +The sidebar files a session by its folder id, so a live slot naming a folder that +is gone would otherwise render in no lane at all: not in the folder (no block +exists for the id) and not in the unfiled lane (the id is non-empty). Every slot +payload passes through ``DashboardState.serialize_slot`` -- the full list, the +``slot_patch`` frame, the single-slot answers -- so that is the ONE place the +reading is made harmless: the payload says unfiled. It is a projection only: the +read path writes nothing, ``slot.folder_id`` keeps the stored value, and the +slot's next folder write replaces it. +""" + +from __future__ import annotations + +import pytest +from aiohttp.test_utils import TestClient, TestServer +from chat_test_helpers import _make_folder_app, _make_state + + +def _folder(state, fid: str, name: str) -> None: + state._folders.append({"id": fid, "name": name, "parent_id": "", "order": len(state._folders)}) + + +def test_a_dangling_folder_id_projects_as_unfiled_and_is_not_written(tmp_path, monkeypatch): + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + _folder(state, "fld-live", "Live") + filed = state.get_or_create_slot("filed") + filed.folder_id = "fld-live" + dangling = state.get_or_create_slot("dangling") + dangling.folder_id = "fld-gone" + + rows = {row["key"]: row for row in state.serialize_slots()} + assert rows["filed"]["folder_id"] == "fld-live", "a filing into a real folder projects as is" + assert rows["dangling"]["folder_id"] == "", "a dangling id projects as unfiled" + # Every audience view is built through the same chokepoint. + bare, dashboard_user, owner = state.serialize_slot_views(owner=True) + for view in (bare, dashboard_user, owner or []): + assert {r["key"]: r["folder_id"] for r in view}["dangling"] == "" + # The single-slot projection agrees, with and without a precomputed id set. + assert state.serialize_slot(dangling)["folder_id"] == "" + assert state.serialize_slot(dangling, known_folder_ids={"fld-gone"})["folder_id"] == "fld-gone" + # A projection only: the stored value stands and nothing was persisted. + assert dangling.folder_id == "fld-gone" + assert state.conversation_log.get_metadata("dashboard:dangling").get("folder_id", "") == "" + + +def test_a_stored_folder_id_of_the_wrong_shape_projects_as_unfiled(tmp_path, monkeypatch): + """The stored value comes back from a file an agent's tools can edit, so a + list or a dict where the id should be is a dangling id too: the projection + reads it as unfiled instead of the membership test raising and taking the + whole slots feed with it.""" + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + _folder(state, "fld-live", "Live") + odd = state.get_or_create_slot("odd") + odd.folder_id = ["fld-live"] # type: ignore[assignment] + other = state.get_or_create_slot("other") + other.folder_id = {"id": "fld-live"} # type: ignore[assignment] + + rows = {row["key"]: row for row in state.serialize_slots()} + assert rows["odd"]["folder_id"] == "" and rows["other"]["folder_id"] == "" + assert state.serialize_slot(odd, known_folder_ids={"fld-live"})["folder_id"] == "" + assert odd.folder_id == ["fld-live"], "a projection only" + + +def test_the_freeze_predicates_never_hash_a_value_that_is_not_an_id(tmp_path, monkeypatch): + """A list or a dict where the id should be is not frozen, is gone, and reads + absent to the un-hide -- none of them raises.""" + import asyncio + + from kiro_crew.dashboard import chat_folders + + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + _folder(state, "fld-live", "Live") + chat_folders._deleting_folder_ids(state).add("fld-live") + try: + for odd in (["fld-live"], {"id": "fld-live"}, 7): + assert chat_folders.is_folder_id(odd) is False + assert chat_folders.folder_is_deleting(state, odd) is False # type: ignore[arg-type] + assert chat_folders.folder_is_gone(state, odd) is True # type: ignore[arg-type] + assert asyncio.run(chat_folders._unhide_folder(state, odd)) is False # type: ignore[arg-type] + assert ( + asyncio.run(chat_folders._unhide_folder(state, odd, frozen_is_present=True)) # type: ignore[arg-type] + is False + ) + assert chat_folders.is_folder_id("fld-live") and not chat_folders.is_folder_id("") + assert chat_folders.folder_is_deleting(state, "fld-live") is True + assert chat_folders.folder_is_gone(state, "") is False + finally: + chat_folders._deleting_folder_ids(state).discard("fld-live") + + +@pytest.mark.asyncio +@pytest.mark.parametrize("flag", ["", "?delete_contents=true"], ids=["default", "cascade"]) +async def test_a_folder_delete_survives_a_live_slot_whose_folder_id_is_not_an_id( + tmp_path, monkeypatch, flag +): + """A live slot restored with ``folder_id: ["x"]`` (a hand-edited metadata + line) must not fail every folder delete on the set lookup: the route reads + such a slot as filed nowhere and deletes the asked folder, both modes. A + folder row whose ``parent_id`` is not a string is left as it is.""" + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + _folder(state, "fld-doomed", "Doomed") + state._folders.append({"id": "fld-odd-parent", "name": "Odd", "parent_id": ["x"], "order": 7}) + odd = state.get_or_create_slot("odd") + odd.folder_id = ["x"] # type: ignore[assignment] + filed = state.get_or_create_slot("filed") + filed.folder_id = "fld-doomed" + monkeypatch.setattr("kiro_crew.dashboard.chat_folders.save_slot_off_loop", _saved_ok) + + async def _close(state_, slot, name, **_kw): + state_._slots.pop(name, None) + + monkeypatch.setattr("kiro_crew.dashboard.chat_handlers.close_slot", _close) + async with TestClient(TestServer(_make_folder_app(state))) as client: + resp = await client.delete(f"/api/chat/folders/fld-doomed{flag}") + assert resp.status == 200, await resp.text() + assert not any(f["id"] == "fld-doomed" for f in state._folders) + assert odd.folder_id == ["x"], "a value that is not an id is not this delete's to touch" + assert "odd" in state._slots + if flag: + assert "filed" not in state._slots, "the cascade archived the filed session" + else: + assert filed.folder_id == "" + assert next(f for f in state._folders if f["id"] == "fld-odd-parent")["parent_id"] == ["x"] + + +async def _saved_ok(*_a, **_k): + return True + + +def test_the_slot_patch_frame_carries_the_projected_reading(tmp_path, monkeypatch): + """The patch frame is built from ``serialize_slot`` too, so a patch naming + ``folder_id`` on a dangling slot says unfiled rather than the stored id.""" + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + slot = state.get_or_create_slot("dangling") + slot.folder_id = "fld-gone" + frames: list[dict] = [] + monkeypatch.setattr(state, "_has_legacy_slots_audience", lambda: False) + monkeypatch.setattr(state, "_has_slot_patch_clients", lambda: True) + monkeypatch.setattr(state, "_send_slot_patch", lambda data: frames.append(data)) + + state.push_slot_patch("dangling", ("folder_id",)) + + assert frames == [{"slots": [{"key": "dangling", "folder_id": ""}]}] + + +@pytest.mark.asyncio +async def test_a_dangling_folder_id_is_replaced_by_the_slots_next_folder_write( + tmp_path, monkeypatch +): + """Cleared on the slot's next folder write, never by a read: the GET renders + the session unfiled while the stored id stands; filing the session (or + unfiling it) through the slot-folder route writes the new placement over it, + in memory and on disk.""" + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + log = state.conversation_log + _folder(state, "fld-live", "Live") + slot = state.get_or_create_slot("dangling") + slot.folder_id = "fld-gone" + slot.append("user", "hello") + slot.drain() + log.update_metadata("dashboard:dangling", {"folder_id": "fld-gone"}) + + async with TestClient(TestServer(_make_folder_app(state))) as client: + listed = await client.get("/api/chat/slots") + assert listed.status == 200 + assert next(r for r in await listed.json() if r["key"] == "dangling")["folder_id"] == "" + # The read wrote nothing. + assert slot.folder_id == "fld-gone" + assert log.get_metadata("dashboard:dangling").get("folder_id") == "fld-gone" + + moved = await client.patch( + "/api/chat/slots/dangling/folder", json={"folder_id": "fld-live"} + ) + assert moved.status == 200, await moved.text() + + assert slot.folder_id == "fld-live" + assert log.get_metadata("dashboard:dangling").get("folder_id") == "fld-live" + assert state.serialize_slot(slot)["folder_id"] == "fld-live" + + +def test_a_filing_into_a_frozen_folder_projects_unfiled_only_while_frozen(tmp_path, monkeypatch): + """Frozen is not gone. While a delete holds the folder frozen the payload says + unfiled -- the session surfaces unfiled while the delete runs -- and the + moment the freeze lifts (an abort) the same stored filing renders again. + Nothing is written either way.""" + from kiro_crew.dashboard import chat_folders + + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + _folder(state, "fld-going", "Going") + slot = state.get_or_create_slot("filed") + slot.folder_id = "fld-going" + assert state.serialize_slot(slot)["folder_id"] == "fld-going" + chat_folders._deleting_folder_ids(state).add("fld-going") + try: + assert state.serialize_slot(slot)["folder_id"] == "" + assert {r["key"]: r["folder_id"] for r in state.serialize_slots()}["filed"] == "" + assert slot.folder_id == "fld-going", "a projection only" + finally: + chat_folders._deleting_folder_ids(state).discard("fld-going") + assert state.serialize_slot(slot)["folder_id"] == "fld-going" + assert state.conversation_log.get_metadata("dashboard:filed").get("folder_id", "") == "" diff --git a/test/test_chat_folder_delete_contents.py b/test/test_chat_folder_delete_contents.py new file mode 100644 index 000000000000..b6733d7d188d --- /dev/null +++ b/test/test_chat_folder_delete_contents.py @@ -0,0 +1,972 @@ +"""``DELETE /api/chat/folders/{id}?delete_contents=``. + +The default is the safe path the sidebar always had: unfile the folder's live +sessions, re-parent its direct children to the top level, remove the one row. +``delete_contents=true`` removes the whole subtree in one step: descendant folders +deleted, live sessions ARCHIVED through the same ``close_slot`` the tab ✕ uses +(never a hard delete), archived sessions unfiled from the folders that are gone. +""" + +from __future__ import annotations + +import pytest +from aiohttp.test_utils import TestClient, TestServer +from chat_test_helpers import _make_folder_app, _make_state + +from kiro_crew.dashboard import chat_folders, chat_handlers +from kiro_crew.dashboard.chat_handlers import SlotCloseError + + +async def _create(client: TestClient, name: str, parent_id: str = "") -> str: + body = {"name": name} + if parent_id: + body["parent_id"] = parent_id + resp = await client.post("/api/chat/folders", json=body) + assert resp.status in (200, 201), await resp.text() + return (await resp.json())["id"] + + +def _folder_ids(state) -> set[str]: + return {f["id"] for f in state._folders} + + +def _live_slot(state, key: str, folder_id: str): + slot = state.get_or_create_slot(key) + slot.folder_id = folder_id + slot.append("user", f"hello from {key}") + slot.drain() + return slot + + +def _archived_session(state, key: str, folder_id: str) -> None: + log = state.conversation_log + log.append(key, "user", f"archived {key}") + log.update_metadata(key, {"folder_id": folder_id, "closed": True}) + + +@pytest.mark.parametrize("flag", ["", "?delete_contents=false", "?delete_contents=0"]) +@pytest.mark.asyncio +async def test_default_reparents_children_and_unfiles_slots_as_before(tmp_path, monkeypatch, flag): + """Without the flag (or with it off) nothing changes: one row goes, the + child survives at the top level, the live session stays live, unfiled.""" + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + async with TestClient(TestServer(_make_folder_app(state))) as client: + parent = await _create(client, "Shift") + child = await _create(client, "TICKET-1", parent) + slot = _live_slot(state, "in-shift", parent) + resp = await client.delete(f"/api/chat/folders/{parent}{flag}") + assert resp.status == 200, await resp.text() + body = await resp.json() + + assert body == {"ok": True}, "the safe path's body carries no counts" + assert _folder_ids(state) == {child} + assert next(f for f in state._folders if f["id"] == child)["parent_id"] == "" + assert state._slots.get("in-shift") is slot, "the safe path must not archive" + assert slot.folder_id == "" + + +@pytest.mark.asyncio +async def test_delete_contents_removes_nested_folders_and_archives_sessions(tmp_path, monkeypatch): + """The shift-folder case: a subtree three deep with live and archived + sessions at every level goes in ONE request, and nothing outside it moves.""" + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + log = state.conversation_log + async with TestClient(TestServer(_make_folder_app(state))) as client: + shift = await _create(client, "Sep 25 - Oct 2 shift") + ticket_a = await _create(client, "TICKET-A", shift) + ticket_b = await _create(client, "TICKET-B", shift) + nested = await _create(client, "TICKET-A/notes", ticket_a) + other = await _create(client, "Unrelated") + + in_shift = _live_slot(state, "live-shift", shift) + in_a = _live_slot(state, "live-a", ticket_a) + in_nested = _live_slot(state, "live-nested", nested) + outside = _live_slot(state, "live-outside", other) + _archived_session(state, "dashboard:old-b", ticket_b) + _archived_session(state, "dashboard:old-outside", other) + + resp = await client.delete(f"/api/chat/folders/{shift}?delete_contents=true") + assert resp.status == 200, await resp.text() + body = await resp.json() + + # Folders: the whole subtree is gone, the unrelated folder untouched. + assert _folder_ids(state) == {other} + assert body["ok"] is True + assert body["delete_contents"] is True + assert body["deleted_folder_ids"] == sorted({shift, ticket_a, ticket_b, nested}) + # Live sessions in the subtree were ARCHIVED -- gone from the live table, + # present in history as closed -- and unfiled there. + for key in ("live-shift", "live-a", "live-nested"): + assert key not in state._slots + meta = log.get_metadata(f"dashboard:{key}") + assert meta.get("closed") is True, f"{key} was not archived: {meta}" + assert meta.get("folder_id", "") == "" + assert log.read_messages(f"dashboard:{key}"), f"{key}'s transcript must survive" + assert body["archived_sessions"] == 3 + assert body["unfiled_sessions"] == 0 + # The session archived earlier under TICKET-B is unfiled, still closed. + old_b = log.get_metadata("dashboard:old-b") + assert old_b.get("folder_id", "") == "" + assert old_b.get("closed") is True + # Three closing saves carried their folder plus the one archived earlier. + assert body["unfiled_history_sessions"] == 4 + # Outside the subtree nothing changed, live or archived. + assert state._slots.get("live-outside") is outside + assert outside.folder_id == other + assert log.get_metadata("dashboard:old-outside").get("folder_id") == other + del in_shift, in_a, in_nested + + +@pytest.mark.asyncio +async def test_delete_contents_refuses_an_unknown_folder(tmp_path, monkeypatch): + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + async with TestClient(TestServer(_make_folder_app(state))) as client: + keep = await _create(client, "Keep") + slot = _live_slot(state, "kept", keep) + resp = await client.delete("/api/chat/folders/fldr00000000?delete_contents=true") + assert resp.status == 404 + assert _folder_ids(state) == {keep} + assert state._slots.get("kept") is slot and slot.folder_id == keep + + +@pytest.mark.asyncio +async def test_a_refused_close_stops_the_cascade_and_leaves_the_tree(tmp_path, monkeypatch): + """A session that cannot be archived is the tab-✕'s own failure, surfaced + with that close's code; the folder tree is not touched and the session stays + live and filed, so the person retries from a state they can see.""" + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + + async def refuse(state_, slot, name, *, pre_pop_check=None): + raise SlotCloseError("a history write is still running", code="history_write_running") + + monkeypatch.setattr(chat_handlers, "close_slot", refuse) + async with TestClient(TestServer(_make_folder_app(state))) as client: + shift = await _create(client, "Shift") + ticket = await _create(client, "TICKET-1", shift) + slot = _live_slot(state, "stuck", ticket) + resp = await client.delete(f"/api/chat/folders/{shift}?delete_contents=true") + assert resp.status == 500 + body = await resp.json() + + assert body["code"] == "history_write_running" + assert body["archived_sessions"] == 0 + assert body["session"] == "stuck" + assert _folder_ids(state) == {shift, ticket} + assert state._slots.get("stuck") is slot + assert slot.folder_id == ticket + + +class _Interleave: + """Run one HTTP request from inside the cascade's first ``close_slot`` await + window -- after the subtree was frozen, before any folder is removed -- and + record its outcome, then let the real close proceed.""" + + def __init__(self, monkeypatch, client: TestClient, send): + self.results: list[tuple[int, dict]] = [] + real_close = chat_handlers.close_slot + + async def close_after_interleaving(state_, slot, name, *, pre_pop_check=None): + if not self.results: + resp = await send(client) + body = await resp.json() if resp.content_type == "application/json" else {} + self.results.append((resp.status, body)) + await real_close(state_, slot, name, pre_pop_check=pre_pop_check) + + monkeypatch.setattr(chat_handlers, "close_slot", close_after_interleaving) + + +def _deleting(state) -> set[str]: + return set(chat_folders._deleting_folder_ids(state)) + + +@pytest.mark.asyncio +async def test_a_child_created_under_the_frozen_subtree_is_refused(tmp_path, monkeypatch): + """The subtree is frozen for the cascade's duration, so a folder cannot be + born under it with sessions no pass of this delete has seen: the create is + refused with a typed error and the cascade removes exactly its snapshot.""" + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + async with TestClient(TestServer(_make_folder_app(state))) as client: + shift = await _create(client, "Shift") + ticket = await _create(client, "TICKET-1", shift) + _live_slot(state, "early", ticket) + race = _Interleave( + monkeypatch, + client, + lambda c: c.post("/api/chat/folders", json={"name": "TICKET-2", "parent_id": ticket}), + ) + resp = await client.delete(f"/api/chat/folders/{shift}?delete_contents=true") + assert resp.status == 200, await resp.text() + body = await resp.json() + + assert race.results == [ + (400, {"error": "parent folder is being deleted", "code": "folder_parent_deleting"}) + ] + assert body["deleted_folder_ids"] == sorted({shift, ticket}) + assert body["archived_sessions"] == 1 + assert _folder_ids(state) == set() + assert _deleting(state) == set(), "the freeze must be released" + + +@pytest.mark.asyncio +async def test_reparenting_a_child_out_of_the_frozen_subtree_is_refused(tmp_path, monkeypatch): + """A child cannot leave the subtree while the cascade runs: the set the + archive pass walks is the set ``_remove`` deletes, so a session archived from + that child never ends up filed under a folder that survived.""" + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + log = state.conversation_log + async with TestClient(TestServer(_make_folder_app(state))) as client: + shift = await _create(client, "Shift") + ticket = await _create(client, "TICKET-1", shift) + _live_slot(state, "in-ticket", ticket) + race = _Interleave( + monkeypatch, + client, + lambda c: c.patch(f"/api/chat/folders/{ticket}", json={"parent_id": ""}), + ) + resp = await client.delete(f"/api/chat/folders/{shift}?delete_contents=true") + assert resp.status == 200, await resp.text() + body = await resp.json() + + assert race.results == [(409, {"error": "folder is being deleted", "code": "folder_deleting"})] + assert body["deleted_folder_ids"] == sorted({shift, ticket}) + assert _folder_ids(state) == set() + assert "in-ticket" not in state._slots + meta = log.get_metadata("dashboard:in-ticket") + assert meta.get("closed") is True and meta.get("folder_id", "") == "" + assert _deleting(state) == set() + + +@pytest.mark.asyncio +async def test_reparenting_a_folder_into_the_frozen_subtree_is_refused(tmp_path, monkeypatch): + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + async with TestClient(TestServer(_make_folder_app(state))) as client: + shift = await _create(client, "Shift") + ticket = await _create(client, "TICKET-1", shift) + outside = await _create(client, "Outside") + _live_slot(state, "in-ticket", ticket) + race = _Interleave( + monkeypatch, + client, + lambda c: c.patch(f"/api/chat/folders/{outside}", json={"parent_id": ticket}), + ) + resp = await client.delete(f"/api/chat/folders/{shift}?delete_contents=true") + assert resp.status == 200, await resp.text() + + assert race.results == [(409, {"error": "folder is being deleted", "code": "folder_deleting"})] + assert _folder_ids(state) == {outside} + assert next(f for f in state._folders if f["id"] == outside)["parent_id"] == "" + + +@pytest.mark.asyncio +async def test_filing_a_session_into_the_frozen_subtree_is_refused_and_nothing_dangles( + tmp_path, monkeypatch +): + """A filing whose existence check would pass but whose save would land after + the removal is refused up front: the store reports a frozen folder absent, so + no live or persisted session names a folder that is gone.""" + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + log = state.conversation_log + async with TestClient(TestServer(_make_folder_app(state))) as client: + shift = await _create(client, "Shift") + ticket = await _create(client, "TICKET-1", shift) + _live_slot(state, "in-ticket", ticket) + outside = _live_slot(state, "outside", "") + race = _Interleave( + monkeypatch, + client, + lambda c: c.patch("/api/chat/slots/outside/folder", json={"folder_id": ticket}), + ) + resp = await client.delete(f"/api/chat/folders/{shift}?delete_contents=true") + assert resp.status == 200, await resp.text() + body = await resp.json() + + # Filing into the deleted folder AFTER the cascade is the plain 400. + late = await client.patch("/api/chat/slots/outside/folder", json={"folder_id": ticket}) + assert late.status == 400, await late.text() + + assert race.results == [(400, {"error": "folder not found", "code": "folder_not_found"})] + assert state._slots.get("outside") is outside and outside.folder_id == "" + assert log.get_metadata("dashboard:outside").get("folder_id", "") == "" + assert body["archived_sessions"] == 1 and body["unfiled_sessions"] == 0 + assert _folder_ids(state) == set() + + +@pytest.mark.asyncio +async def test_a_cascade_racing_a_move_into_its_subtree_never_archives_the_moved_session( + tmp_path, monkeypatch +): + """The move's verdict is earned before anything is assigned. While the PATCH + awaits the store's answer about its destination, a cascade on that very + folder runs to completion: it must find the moving session filed nowhere -- + a provisional ``folder_id`` written before the verdict would be read by the + cascade's membership pass and at close_slot's point of no return, and a LIVE + session would be archived on a move the route then rejects.""" + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + log = state.conversation_log + async with TestClient(TestServer(_make_folder_app(state))) as client: + shift = await _create(client, "Shift") + _live_slot(state, "in-shift", shift) + mover = _live_slot(state, "mover", "") + real_unhide = chat_folders._unhide_folder + cascade: list[tuple[int, dict]] = [] + seen_while_deciding: list[str] = [] + + async def cascade_runs_inside_the_verdict(state_, folder_id, **kw): + if folder_id == shift and not cascade: + seen_while_deciding.append(mover.folder_id) + resp = await client.delete(f"/api/chat/folders/{shift}?delete_contents=true") + cascade.append((resp.status, await resp.json())) + return await real_unhide(state_, folder_id, **kw) + + monkeypatch.setattr(chat_folders, "_unhide_folder", cascade_runs_inside_the_verdict) + resp = await client.patch("/api/chat/slots/mover/folder", json={"folder_id": shift}) + assert resp.status == 400, await resp.text() + assert (await resp.json())["code"] == "folder_not_found" + + assert seen_while_deciding == [""], "the move was live before its verdict" + assert cascade and cascade[0][0] == 200, cascade + assert cascade[0][1]["archived_sessions"] == 1, "only the session filed there is archived" + assert state._slots.get("mover") is mover, "a rejected move archived a live session" + assert mover.folder_id == "" + assert log.get_metadata("dashboard:mover").get("folder_id", "") == "" + assert "in-shift" not in state._slots + assert _folder_ids(state) == set() + + +@pytest.mark.asyncio +async def test_a_confirmed_move_still_lands_after_its_verdict(tmp_path, monkeypatch): + """The other half of the reorder: a destination the store confirms is + assigned right after the verdict, persisted, and the row is un-hidden.""" + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + log = state.conversation_log + async with TestClient(TestServer(_make_folder_app(state))) as client: + dest = await _create(client, "Dest") + next(f for f in state._folders if f["id"] == dest)["hidden"] = True + mover = _live_slot(state, "mover", "") + resp = await client.patch("/api/chat/slots/mover/folder", json={"folder_id": dest}) + assert resp.status == 200, await resp.text() + assert (await resp.json()) == {"ok": True, "folder_id": dest} + + assert mover.folder_id == dest + assert log.get_metadata("dashboard:mover")["folder_id"] == dest + assert next(f for f in state._folders if f["id"] == dest).get("hidden") is False + + +@pytest.mark.asyncio +async def test_a_second_delete_of_a_frozen_folder_is_refused(tmp_path, monkeypatch): + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + async with TestClient(TestServer(_make_folder_app(state))) as client: + shift = await _create(client, "Shift") + ticket = await _create(client, "TICKET-1", shift) + _live_slot(state, "in-ticket", ticket) + race = _Interleave(monkeypatch, client, lambda c: c.delete(f"/api/chat/folders/{ticket}")) + resp = await client.delete(f"/api/chat/folders/{shift}?delete_contents=true") + assert resp.status == 200, await resp.text() + + assert race.results == [(409, {"error": "folder is being deleted", "code": "folder_deleting"})] + assert _folder_ids(state) == set() + + +@pytest.mark.asyncio +@pytest.mark.parametrize("flag", ["", "?delete_contents=true"]) +async def test_the_commit_itself_unfiles_a_slot_filed_after_the_unfile_snapshot( + tmp_path, monkeypatch, flag +): + """A filing path that consults neither the store nor the freeze (a cron + placement, an inherited fork folder) can set a slot's ``folder_id`` after the + unfile loop's snapshot. The removal callback sweeps live slots in the same + synchronous step as the commit, so no live or persisted slot names a folder + the commit removed.""" + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + log = state.conversation_log + real_mutate = state.mutate_folders + late: dict[str, object] = {} + + async def file_late_then_commit(mutate, on_committed=None): + if getattr(mutate, "__name__", "") == "_remove" and "slot" not in late: + # Lands after every pre-commit pass, as a detached placement would. + late["slot"] = _live_slot(state, "late", str(late["folder"])) + return await real_mutate(mutate) + + async with TestClient(TestServer(_make_folder_app(state))) as client: + shift = await _create(client, "Shift") + ticket = await _create(client, "TICKET-1", shift) + late["folder"] = ticket if flag else shift + monkeypatch.setattr(state, "mutate_folders", file_late_then_commit) + resp = await client.delete(f"/api/chat/folders/{shift}{flag}") + assert resp.status == 200, await resp.text() + + slot = late["slot"] + assert state._slots.get("late") is slot, "swept, not archived: it stays live" + assert slot.folder_id == "" + assert log.get_metadata("dashboard:late").get("folder_id", "") == "" + assert not any(s.folder_id in {shift, ticket} for s in state._slots.values()) + assert shift not in _folder_ids(state) + + +@pytest.mark.asyncio +async def test_a_frozen_folder_reads_absent_to_a_new_filing_and_present_to_a_stored_one( + tmp_path, monkeypatch +): + """The two readings of a frozen folder, and that neither writes it.""" + from kiro_crew.dashboard.arrival_folders import arrival_folder_exists + + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + async with TestClient(TestServer(_make_folder_app(state))) as client: + folder = await _create(client, "Going") + hidden = await client.patch(f"/api/chat/folders/{folder}", json={"hidden": True}) + assert hidden.status == 200, await hidden.text() + chat_folders._deleting_folder_ids(state).add(folder) + try: + assert await chat_folders._unhide_folder(state, folder) is False + assert await chat_folders._unhide_folder(state, folder, frozen_is_present=True) is True + assert await arrival_folder_exists(state, folder) is False + assert await arrival_folder_exists(state, folder, frozen_is_present=True) is True + finally: + chat_folders._deleting_folder_ids(state).discard(folder) + row = next(f for f in state._folders if f["id"] == folder) + assert row.get("hidden") is True, "neither reading writes a frozen folder" + # Thawed, the ordinary reading resumes: present, and the un-hide lands. + assert await chat_folders._unhide_folder(state, folder) is True + assert not next(f for f in state._folders if f["id"] == folder).get("hidden") + + +@pytest.mark.asyncio +async def test_the_freeze_is_released_when_a_refused_close_aborts_the_cascade( + tmp_path, monkeypatch +): + """An aborted cascade leaves the tree intact AND writable: the frozen set is + emptied in ``finally``, so the folders it could not delete accept mutations.""" + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + + async def refuse(state_, slot, name, *, pre_pop_check=None): + raise SlotCloseError("a history write is still running", code="history_write_running") + + monkeypatch.setattr(chat_handlers, "close_slot", refuse) + async with TestClient(TestServer(_make_folder_app(state))) as client: + shift = await _create(client, "Shift") + ticket = await _create(client, "TICKET-1", shift) + _live_slot(state, "stuck", ticket) + resp = await client.delete(f"/api/chat/folders/{shift}?delete_contents=true") + assert resp.status == 500 + assert _deleting(state) == set() + moved = await client.patch(f"/api/chat/folders/{ticket}", json={"parent_id": ""}) + assert moved.status == 200, await moved.text() + child = await _create(client, "TICKET-2", ticket) + + assert _folder_ids(state) == {shift, ticket, child} + assert next(f for f in state._folders if f["id"] == ticket)["parent_id"] == "" + + +async def _cascade_with_a_slot_mid_close( + state, client, monkeypatch, *, commit_fails: bool, lands: str = "before-the-commit" +): + """Run a cascade while one session in the subtree is owned by ANOTHER close + (a ✕ mid-flight): the archive pass skips it, and that close lands its + archival save -- unguarded, writing the slot as it stands -- either right + before the route's commit callback runs (``before-the-commit``: after the + unfile loop, before ``_remove``) or during the store's disk write + (``during-the-write``: after ``_remove``'s sweep ran over the still + registered slot, before the write's verdict). Returns (status, folder id).""" + log = state.conversation_log + shift = await _create(client, "Shift") + _live_slot(state, "archived-by-pass", shift) + mid = _live_slot(state, "mid-close", shift) + mid.begin_close() # the ✕ has fenced the slot; its close is suspended in an await + landed: list[str] = [] + + def the_close_lands() -> None: + # The other close resumes: its archival save persists the slot's LIVE + # folder_id -- the route had no say in that write -- and pops it. + landed.append(mid.folder_id) + log.update_metadata("dashboard:mid-close", {"folder_id": mid.folder_id, "closed": True}) + state._slots.pop("mid-close", None) + mid.cancel_close() + + real_mutate = state.mutate_folders + real_write = state._write_folders_confirmed + + async def before_the_commit(mutate): + if getattr(mutate, "__name__", "") == "_remove" and not landed: + the_close_lands() + if commit_fails: + raise OSError("folders.json: short write") + return await real_mutate(mutate) + + def during_the_write(path, snapshot): + # ``_remove`` has run (its sweep saw the slot, still registered); the + # disk write is where the close lands and, on this path, where the + # commit fails -- the repository restores the list, the route rolls back. + if not landed: + the_close_lands() + if commit_fails: + raise OSError("folders.json: short write") + return real_write(path, snapshot) + + if lands == "before-the-commit": + monkeypatch.setattr(state, "mutate_folders", before_the_commit) + else: + monkeypatch.setattr(state, "_write_folders_confirmed", during_the_write) + resp = await client.delete(f"/api/chat/folders/{shift}?delete_contents=true") + assert landed, "fixture: the other close did land during the route" + return resp.status, shift + + +@pytest.mark.asyncio +@pytest.mark.parametrize("lands", ["before-the-commit", "during-the-write"]) +async def test_a_failed_commit_leaves_a_mid_close_sessions_folder_on_its_transcript( + tmp_path, monkeypatch, lands +): + """The route never unfiles a slot an active close owns -- neither the unfile + loop nor the commit callback's sweep. Every save the route could make for it + is refused by the close fence (only marking it dirty, for a flush that never + comes once the close pops it), while the close's own archival save is + unguarded: an in-memory unfile at either site would be persisted by that + save and, the commit failing, could not be restored -- an archived + conversation durably unfiled from a folder that still exists.""" + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + log = state.conversation_log + async with TestClient(TestServer(_make_folder_app(state))) as client: + status, shift = await _cascade_with_a_slot_mid_close( + state, client, monkeypatch, commit_fails=True, lands=lands + ) + assert status == 500 + assert _folder_ids(state) == {shift}, "the store rolled the removal back" + assert ( + log.get_metadata("dashboard:mid-close").get("folder_id") == shift + ), "the archived transcript lost its folder while the folder survives" + assert log.get_metadata("dashboard:mid-close").get("closed") is True + # The session the pass archived is likewise still filed under the surviving folder. + assert log.get_metadata("dashboard:archived-by-pass").get("folder_id") == shift + + +@pytest.mark.asyncio +async def test_a_committed_cascade_clears_a_mid_close_sessions_row_after_the_commit( + tmp_path, monkeypatch +): + """The other half: on a committed cascade the mid-close session's closing + save carries the folder it sat in, and the post-commit pass over the + archived rows clears it with the rest -- nothing stays filed under a folder + that is gone, and nothing the route did had to be rolled back.""" + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + log = state.conversation_log + async with TestClient(TestServer(_make_folder_app(state))) as client: + status, shift = await _cascade_with_a_slot_mid_close( + state, client, monkeypatch, commit_fails=False + ) + assert status == 200 + assert _folder_ids(state) == set() + # The close's save carried the folder the session sat in (the route did not + # touch a slot the close owned); the post-commit pass cleared that row. + assert not log.get_metadata("dashboard:mid-close").get("folder_id") + assert log.get_metadata("dashboard:mid-close").get("closed") is True + assert not log.get_metadata("dashboard:archived-by-pass").get("folder_id") + + +@pytest.mark.asyncio +async def test_a_session_moved_out_of_the_subtree_during_its_close_is_not_archived( + tmp_path, monkeypatch +): + """The archive pass read the session's folder before ``close_slot`` awaited; a + move committed inside that window makes it a session the person just placed + elsewhere. The synchronous pre-pop re-check unwinds that close, the session + stays live in its new folder, and the cascade goes on without it.""" + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + log = state.conversation_log + real_close = chat_handlers.close_slot + moved: list[str] = [] + + async with TestClient(TestServer(_make_folder_app(state))) as client: + shift = await _create(client, "Shift") + ticket = await _create(client, "TICKET-1", shift) + keep = await _create(client, "Keep") + moving = _live_slot(state, "moving", ticket) + _live_slot(state, "staying", ticket) + + async def move_out_then_close(state_, slot, name, *, pre_pop_check=None): + if name == "moving" and not moved: + # The drag-out lands while this close is suspended in its awaits, + # after the loop's membership read and before the pop. + slot.folder_id = keep + moved.append(name) + await real_close(state_, slot, name, pre_pop_check=pre_pop_check) + + monkeypatch.setattr(chat_handlers, "close_slot", move_out_then_close) + resp = await client.delete(f"/api/chat/folders/{shift}?delete_contents=true") + assert resp.status == 200, await resp.text() + body = await resp.json() + + assert moved == ["moving"] + assert state._slots.get("moving") is moving, "the moved session must stay live" + assert moving.folder_id == keep + assert not moving.is_closing, "the aborted close must release its fence" + assert log.get_metadata("dashboard:moving").get("closed") is not True + assert "staying" not in state._slots + assert log.get_metadata("dashboard:staying").get("closed") is True + assert body["archived_sessions"] == 1 + assert body["unfiled_sessions"] == 0 + assert body["deleted_folder_ids"] == sorted({shift, ticket}) + assert _folder_ids(state) == {keep} + + +@pytest.mark.asyncio +async def test_a_slot_popped_while_the_unfile_loop_awaits_does_not_crash_the_cascade( + tmp_path, monkeypatch +): + """A slot another retraction already owns is skipped by the archive pass and + reached by the unfile loop, whose save awaits; that retraction's pop lands + meanwhile. The loop walks a snapshot, so the cascade completes and the folders + go instead of a ``RuntimeError`` escaping with the tree intact.""" + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + real_save = chat_folders.save_slot_off_loop + popped: list[str] = [] + + async def save_after_a_concurrent_pop(state_, slot, **kwargs): + if slot.key == "closing" and not popped: + # The other close reaches its point of no return while this save + # awaits. + state._slots.pop("closing") + popped.append("closing") + return await real_save(state_, slot, **kwargs) + + monkeypatch.setattr(chat_folders, "save_slot_off_loop", save_after_a_concurrent_pop) + async with TestClient(TestServer(_make_folder_app(state))) as client: + shift = await _create(client, "Shift") + ticket = await _create(client, "TICKET-1", shift) + closing = _live_slot(state, "closing", ticket) + closing.begin_close() + resp = await client.delete(f"/api/chat/folders/{shift}?delete_contents=true") + assert resp.status == 200, await resp.text() + body = await resp.json() + + assert popped == ["closing"] + assert _folder_ids(state) == set() + assert body["deleted_folder_ids"] == sorted({shift, ticket}) + assert body["archived_sessions"] == 0 + assert body["unfiled_sessions"] == 1 + assert "closing" not in state._slots + assert closing.folder_id == "" + + +def test_the_archived_unfile_is_a_compare_and_set_that_recreates_nothing(): + """The pass lists sessions, then writes each one later. Between the two a + session can be refiled elsewhere (the guard must refuse) or deleted (the + write must not recreate it as a metadata-only stub).""" + listed = [ + {"key": "still", "folder_id": "gone-1"}, + {"key": "refiled", "folder_id": "gone-1"}, + {"key": "deleted", "folder_id": "gone-2"}, + {"key": "", "folder_id": "gone-1"}, + ] + on_disk: dict[str, dict] = { + "still": {"folder_id": "gone-1", "closed": True}, + "refiled": {"folder_id": "kept", "closed": True}, + } + calls: list[tuple[str, dict, bool]] = [] + + class _Log: + def list_sessions(self): + return list(listed) + + def update_metadata_if(self, key, fields, guard, *, require_existing=False): + calls.append((key, fields, require_existing)) + meta = on_disk.get(key) + if meta is None: + if require_existing: + return False + on_disk[key] = dict(fields) # the stub the flag exists to prevent + return True + if not guard(meta): + return False + meta.update(fields) + return True + + cleared = chat_folders._clear_archived_folder_assignments(_Log(), {"gone-1", "gone-2"}) + + assert cleared == 1 + assert [c[0] for c in calls] == ["still", "refiled", "deleted"], "an empty key is skipped" + assert all(c[1] == {"folder_id": ""} and c[2] is True for c in calls) + assert on_disk["still"] == {"folder_id": "", "closed": True} + assert on_disk["refiled"] == {"folder_id": "kept", "closed": True} + assert "deleted" not in on_disk + + +@pytest.mark.asyncio +async def test_a_failed_folder_commit_after_the_archive_pass_is_audited_and_retryable( + tmp_path, monkeypatch +): + """The archive pass cannot be undone, so a folders.json write that fails + after it leaves the state a refused close leaves: the sessions are in + History, filed under folders that still exist. The escape is audited with the + archive count, and a retry finds nothing live left and commits.""" + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + log = state.conversation_log + events: list[dict] = [] + + class _Sel: + def log_api_access(self, **kw): + events.append(kw) + + def __getattr__(self, _name): + return lambda *a, **k: None + + monkeypatch.setattr(chat_folders, "sel", lambda: _Sel()) + real_mutate = state.mutate_folders + failures: list[str] = [] + + async def fail_once(mutate): + # Only the removal commit fails; the freeze and thaw callbacks that share + # the store lock go through. + if getattr(mutate, "__name__", "") == "_remove" and not failures: + failures.append("torn write") + raise OSError("folders.json: short write") + return await real_mutate(mutate) + + async with TestClient(TestServer(_make_folder_app(state))) as client: + shift = await _create(client, "Shift") + ticket = await _create(client, "TICKET-1", shift) + _live_slot(state, "live", ticket) + monkeypatch.setattr(state, "mutate_folders", fail_once) + first = await client.delete(f"/api/chat/folders/{shift}?delete_contents=true") + assert first.status == 500 + # Archived by the pass, still filed under the folder that still exists. + assert "live" not in state._slots + assert log.get_metadata("dashboard:live").get("closed") is True + assert log.get_metadata("dashboard:live").get("folder_id") == ticket + assert _folder_ids(state) == {shift, ticket} + error_rows = [e for e in events if e.get("outcome") == "error"] + assert len(error_rows) == 1 + assert error_rows[0]["operation"] == "chat.folder_delete" + assert error_rows[0]["resources"] == f"{shift} delete_contents archived=1" + assert error_rows[0]["error"] == "OSError" + + retry = await client.delete(f"/api/chat/folders/{shift}?delete_contents=true") + assert retry.status == 200, await retry.text() + body = await retry.json() + + assert body["deleted_folder_ids"] == sorted({shift, ticket}) + assert body["archived_sessions"] == 0 + assert body["unfiled_history_sessions"] == 1 + assert _folder_ids(state) == set() + assert log.get_metadata("dashboard:live").get("folder_id", "") == "" + + +@pytest.mark.asyncio +async def test_delete_contents_on_a_leaf_folder_reports_zero_counts(tmp_path, monkeypatch): + """The counts are the contract the sidebar's second confirm step will show, + so an empty folder answers zeros rather than omitting them.""" + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + async with TestClient(TestServer(_make_folder_app(state))) as client: + leaf = await _create(client, "Empty") + resp = await client.delete(f"/api/chat/folders/{leaf}?delete_contents=true") + assert resp.status == 200, await resp.text() + body = await resp.json() + assert body == { + "ok": True, + "delete_contents": True, + "deleted_folder_ids": [leaf], + "unfiled_sessions": 0, + "archived_sessions": 0, + "unfiled_history_sessions": 0, + } + assert _folder_ids(state) == set() + + +@pytest.mark.parametrize("flag", ["", "?delete_contents=true"]) +@pytest.mark.asyncio +async def test_the_committed_ids_are_thawed_before_the_post_commit_tidy( + tmp_path, monkeypatch, flag +): + """The "frozen is present" reading exists for the commit-time sweep to decide + a stored filing; once ``_remove`` has committed that sweep has run and cannot + see a slot published later. So the committed ids leave the freeze the moment + the commit lands, before the post-commit tidy awaits (the swept saves, the + history scan) -- and a resume landing in that window reads the removed folder + as ABSENT and drops its filing, instead of keeping a filing into a folder + that is gone.""" + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + real_mutate = state.mutate_folders + real_save = chat_folders.save_slot_off_loop + late: dict[str, object] = {} + seen: dict[str, object] = {} + + async def file_late_then_commit(mutate, on_committed=None): + if getattr(mutate, "__name__", "") == "_remove" and "slot" not in late: + # A detached placement landing after every pre-commit pass, so the + # commit-time sweep has something to persist AFTER the commit. + late["slot"] = _live_slot(state, "late", str(late["folder"])) + return await real_mutate(mutate) + + async def read_during_the_tidy(state_, slot, **kwargs): + if slot is late.get("slot") and "frozen" not in seen: + # Inside the post-commit tidy: the swept slot's save is awaiting. + seen["frozen"] = _deleting(state_) + seen["resume_reading"] = await chat_folders._unhide_folder( + state_, str(late["folder"]), frozen_is_present=True + ) + return await real_save(state_, slot, **kwargs) + + async with TestClient(TestServer(_make_folder_app(state))) as client: + shift = await _create(client, "Shift") + late["folder"] = shift + monkeypatch.setattr(state, "mutate_folders", file_late_then_commit) + monkeypatch.setattr(chat_folders, "save_slot_off_loop", read_during_the_tidy) + resp = await client.delete(f"/api/chat/folders/{shift}{flag}") + assert resp.status == 200, await resp.text() + + assert seen["frozen"] == set(), "the committed id left the freeze with the commit" + assert seen["resume_reading"] is False, "a removed folder reads absent, not frozen-present" + assert _deleting(state) == set() + assert shift not in _folder_ids(state) + assert late["slot"].folder_id == "" + + +async def _resume_parked(state, name: str): + """Resume archived session *name* the way the History tab does, mid-cascade.""" + outcome = await chat_handlers.resume_slot_from_history( + state, name=name, history_key=f"dashboard:{name}", caller_label="test" + ) + assert outcome.refusal is None, outcome.refusal + return outcome.slot + + +@pytest.mark.asyncio +async def test_a_cascade_that_aborts_retains_a_filing_resumed_while_it_ran(tmp_path, monkeypatch): + """Frozen is not gone. A session resumed from History while the cascade holds + its subtree frozen keeps its STORED filing: the slot publishes with it (the + payload projects it unfiled while the folder is frozen), nothing is written, + and when the cascade aborts by its designed 500 (a refused close) the folder + is still there and the filing stands -- in memory, on disk, and in the payload + once the thaw re-publishes. Only a commit may unfile it.""" + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + log = state.conversation_log + seen: dict[str, object] = {} + + async def resume_then_refuse(state_, slot, name, *, pre_pop_check=None): + # Inside the archive pass: the subtree is frozen, the commit has not run. + parked = await _resume_parked(state_, "parked") + seen["frozen"] = _deleting(state_) + seen["live_folder"] = parked.folder_id + seen["projected"] = state_.serialize_slot(parked)["folder_id"] + seen["stored"] = log.get_metadata("dashboard:parked").get("folder_id") + raise SlotCloseError("a history write is still running", code="history_write_running") + + monkeypatch.setattr(chat_handlers, "close_slot", resume_then_refuse) + pushes: list[int] = [] + monkeypatch.setattr(state, "push_slots_update", lambda *a, **k: pushes.append(1)) + async with TestClient(TestServer(_make_folder_app(state))) as client: + shift = await _create(client, "Shift") + ticket = await _create(client, "TICKET-1", shift) + _live_slot(state, "stuck", ticket) + _archived_session(state, "dashboard:parked", ticket) + resp = await client.delete(f"/api/chat/folders/{shift}?delete_contents=true") + assert resp.status == 500, await resp.text() + + assert seen["frozen"] == {shift, ticket} + assert seen["live_folder"] == ticket, "the stored filing was dropped at the publish" + assert seen["projected"] == "", "while frozen, the payload projects the filing as unfiled" + assert seen["stored"] == ticket + parked = state._slots["parked"] + assert parked.folder_id == ticket, "the abort left the session unfiled in memory" + assert log.get_metadata("dashboard:parked").get("folder_id") == ticket, "durably erased" + assert _deleting(state) == set() + assert state.serialize_slot(parked)["folder_id"] == ticket, "not rendered filed after the thaw" + assert pushes, "the thaw re-publishes so the hidden filings render again" + assert _folder_ids(state) == {shift, ticket} + + +@pytest.mark.asyncio +async def test_a_cascade_that_commits_unfiles_a_filing_resumed_while_it_ran(tmp_path, monkeypatch): + """The other arm: the same mid-cascade resume, and the cascade commits. The + resumed slot registered with its filing, so the delete's own passes unfile it + -- live, unfiled, persisted -- and the folders are gone.""" + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + log = state.conversation_log + real_close = chat_handlers.close_slot + + async def resume_then_close(state_, slot, name, *, pre_pop_check=None): + if "parked" not in state_._slots: + await _resume_parked(state_, "parked") + return await real_close(state_, slot, name, pre_pop_check=pre_pop_check) + + monkeypatch.setattr(chat_handlers, "close_slot", resume_then_close) + async with TestClient(TestServer(_make_folder_app(state))) as client: + shift = await _create(client, "Shift") + ticket = await _create(client, "TICKET-1", shift) + _live_slot(state, "stuck", ticket) + _archived_session(state, "dashboard:parked", ticket) + resp = await client.delete(f"/api/chat/folders/{shift}?delete_contents=true") + assert resp.status == 200, await resp.text() + + parked = state._slots.get("parked") + assert ( + parked is not None + ), "the resumed session stays live: the cascade unfiles, it does not re-archive" + assert parked.folder_id == "" + assert log.get_metadata("dashboard:parked").get("folder_id", "") == "" + assert _folder_ids(state) == set() + assert _deleting(state) == set() + assert not any(s.folder_id in {shift, ticket} for s in state._slots.values()) + + +@pytest.mark.parametrize("flag", ["", "?delete_contents=true"]) +@pytest.mark.asyncio +async def test_a_slot_published_during_the_commits_disk_write_is_swept_before_the_thaw( + tmp_path, monkeypatch, flag +): + """Between the removal callback's sweep and the thaw the store's disk write + awaits. A session published in that window keeps its filing (frozen is not + gone), so it names a folder the callback already removed. The second, + synchronous sweep right before the thaw unfiles it, and it is persisted with + the swept saves: after the thaw no live slot names a removed folder.""" + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + log = state.conversation_log + real_mutate = state.mutate_folders + late: dict[str, object] = {} + + async def commit_then_publish(mutate, on_committed=None): + result = await real_mutate(mutate) + if getattr(mutate, "__name__", "") == "_remove" and "slot" not in late: + # After the callback (and its sweep) ran and the write landed, before + # ``_delete_frozen`` resumes: the folder is out of the list, still frozen. + assert _deleting(state) >= {str(late["folder"])} + late["slot"] = _live_slot(state, "late", str(late["folder"])) + return result + + async with TestClient(TestServer(_make_folder_app(state))) as client: + shift = await _create(client, "Shift") + late["folder"] = shift + monkeypatch.setattr(state, "mutate_folders", commit_then_publish) + resp = await client.delete(f"/api/chat/folders/{shift}{flag}") + assert resp.status == 200, await resp.text() + + slot = late["slot"] + assert state._slots.get("late") is slot + assert slot.folder_id == "" + assert log.get_metadata("dashboard:late").get("folder_id", "") == "" + assert _deleting(state) == set() + assert shift not in _folder_ids(state) diff --git a/test/test_chat_folder_ownership.py b/test/test_chat_folder_ownership.py index f1f03387a91d..8a227a08ca2e 100644 --- a/test/test_chat_folder_ownership.py +++ b/test/test_chat_folder_ownership.py @@ -476,6 +476,31 @@ async def test_an_app_cannot_delete_even_an_empty_folder_it_owns(self) -> None: assert body["code"] == "folder_delete_forbidden" assert _by_id(state, RADAR) is not None + @pytest.mark.asyncio + async def test_the_cascade_flag_opens_no_second_door_for_an_app(self) -> None: + """``?delete_contents=true`` archives sessions, which is MORE than the + relocation the rule above forbids; it is refused before any session is + touched, and the flag is not read by the refusal at all.""" + mine = _app_slot("chat-9-900", "issue-radar") + mine.folder_id = RADAR + state = _state(_app_slot("chat-1-100", "issue-radar"), mine) + with ( + patch("kiro_crew.dashboard.chat_folders.save_slot_off_loop", AsyncMock()) as save, + patch("kiro_crew.dashboard.chat_handlers.close_slot", AsyncMock()) as close, + ): + async with TestClient(TestServer(_make_app(state))) as client: + resp = await client.delete( + f"/api/chat/folders/{RADAR}?delete_contents=true", + headers={"X-Session-Key": "dashboard:chat-1-100"}, + ) + body = await resp.json() + assert resp.status == 403 + assert body["code"] == "folder_delete_forbidden" + assert _by_id(state, RADAR) is not None + assert mine.folder_id == RADAR + save.assert_not_awaited() + close.assert_not_awaited() + @pytest.mark.asyncio async def test_no_session_is_touched_by_the_refusal(self) -> None: """Refused before the unfile loop, so nothing is written and there is diff --git a/test/test_chat_slot_create_folder.py b/test/test_chat_slot_create_folder.py index 04be348cc605..f3f11f9fd0b3 100644 --- a/test/test_chat_slot_create_folder.py +++ b/test/test_chat_slot_create_folder.py @@ -127,6 +127,33 @@ async def test_create_with_folder_files_the_slot(self, tmp_path): assert (await resp.json())["folder_id"] == FOLDER_ID assert state._slots["s1"].folder_id == FOLDER_ID + @pytest.mark.asyncio + async def test_the_folder_is_assigned_only_after_its_verdict(self, tmp_path, monkeypatch): + """``name`` can address a live slot. While the destination's verdict is + pending, no provisional folder_id may be live for a delete cascade to + read; on True the assignment follows without a suspension.""" + from kiro_crew.dashboard import chat_handlers + + state = _make_state(tmp_path) + real = chat_handlers._unhide_folder + seen: list[str] = [] + + async def _record_then_answer(state_, folder_id, **kw): + seen.append(state._slots["s1"].folder_id) + return await real(state_, folder_id, **kw) + + monkeypatch.setattr(chat_handlers, "_unhide_folder", _record_then_answer) + async with TestClient(TestServer(_make_app(state))) as client: + resp = await client.post("/api/chat/slots", json={"name": "s1"}) + assert resp.status == 200 + resp = await client.post( + "/api/chat/slots", json={"name": "s1", "folder_id": FOLDER_ID} + ) + assert resp.status == 200 + assert (await resp.json())["folder_id"] == FOLDER_ID + assert seen == [""], "the filing was live before its verdict" + assert state._slots["s1"].folder_id == FOLDER_ID + @pytest.mark.asyncio async def test_unknown_folder_is_rejected(self, tmp_path): state = _make_state(tmp_path) diff --git a/test/test_cron_chat_folder.py b/test/test_cron_chat_folder.py index 26af54607525..ccd4d22f0940 100644 --- a/test/test_cron_chat_folder.py +++ b/test/test_cron_chat_folder.py @@ -670,6 +670,34 @@ async def _save(state, slot, *a, **kw): await asyncio.gather(*list(state._background_tasks)) assert state.push_slots_update.call_count == 2 + @pytest.mark.asyncio + async def test_a_folder_frozen_by_a_running_delete_is_not_placed_into( + self, monkeypatch + ) -> None: + """The placement re-asks whether the folder exists at the assignment, + synchronously, and a folder a ``delete_contents`` cascade has frozen reads + as absent: the tab stays where it is and nothing is written.""" + import kiro_crew.dashboard.chat_persistence as persistence + from kiro_crew.dashboard import chat_folders + + saves: list[str] = [] + + async def _save(state, slot, *a, **kw): + saves.append(slot.key) + return True + + monkeypatch.setattr(persistence, "save_slot_off_loop", _save) + state = _make_state() + chat_folders._deleting_folder_ids(state).add(FOLDER) + try: + slot = _deliver(state, _job(chat_folder_id=FOLDER), "the brief", history=[]) + assert slot is not None + await asyncio.gather(*list(state._background_tasks)) + finally: + chat_folders._deleting_folder_ids(state).discard(FOLDER) + assert slot.folder_id == "" + assert saves == [] + @pytest.mark.asyncio async def test_a_failed_write_retracts_the_placement(self, monkeypatch) -> None: """So memory and disk agree on "not filed" rather than disagreeing.""" diff --git a/test/test_dashboard_chat.py b/test/test_dashboard_chat.py index 7c8d604780b9..1588fd3bd377 100644 --- a/test/test_dashboard_chat.py +++ b/test/test_dashboard_chat.py @@ -14587,6 +14587,9 @@ async def test_slots_include_pinned(self, tmp_path, monkeypatch): async def test_slots_include_folder_id(self, tmp_path, monkeypatch): monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) state = _make_state(tmp_path) + # The payload carries a folder id only while a folder record carries it + # (a dangling id projects as unfiled), so the folder is part of the fixture. + state._folders.append({"id": "f-abc", "name": "ABC", "parent_id": "", "order": 0}) slot = state.get_or_create_slot("s1") slot.folder_id = "f-abc" app = _make_folder_app(state) diff --git a/test/test_folder_store_writer.py b/test/test_folder_store_writer.py index f57344e5b908..211579b7ae06 100644 --- a/test/test_folder_store_writer.py +++ b/test/test_folder_store_writer.py @@ -511,7 +511,7 @@ async def test_a_vanished_target_leaves_the_previous_folder_intact( # The target is deleted between the handler's unlocked check and the store # lock, which is what _unhide_folder reports from inside that lock. - async def _gone(state_: Any, folder_id: str) -> bool: + async def _gone(state_: Any, folder_id: str, **_kw: Any) -> bool: return folder_id != "target" monkeypatch.setattr(chat_handlers, "_unhide_folder", _gone) diff --git a/test/test_forced_save_history_key_pin.py b/test/test_forced_save_history_key_pin.py index 9642fff7bf62..26ec4584b01e 100644 --- a/test/test_forced_save_history_key_pin.py +++ b/test/test_forced_save_history_key_pin.py @@ -243,11 +243,20 @@ async def test_restore_unfiled_refusal_marks_dirty_and_keeps_restored_field( folder = await (await client.post("/api/chat/folders", json={"name": "F"})).json() slot = state.get_or_create_slot("s1") slot.folder_id = folder["id"] - # Force the folder-store commit to fail so the rollback runs and - # its restore save is also refused. + + # Force the folder-store COMMIT to fail so the rollback runs and its + # restore save is also refused. Only the removal callback fails: the + # delete first freezes the folder through the same store method with a + # no-write callback, and that has to go through for the unfile to run. + async def commit_fails(fn, on_committed=None): + if getattr(fn, "__name__", "") == "_remove": + raise OSError("disk full") + _changed, value = fn(state._folders) + return value + with ( patch("kiro_crew.dashboard.chat_folders.save_slot_off_loop", refusing), - patch.object(state, "mutate_folders", AsyncMock(side_effect=OSError("disk full"))), + patch.object(state, "mutate_folders", AsyncMock(side_effect=commit_fails)), ): resp = await client.delete(f"/api/chat/folders/{folder['id']}") # The commit failure propagates (the delete did NOT land)… diff --git a/test/test_mcp_dashboard_folders.py b/test/test_mcp_dashboard_folders.py index 2891173855a8..6a2c8836c55f 100644 --- a/test/test_mcp_dashboard_folders.py +++ b/test/test_mcp_dashboard_folders.py @@ -346,6 +346,150 @@ def test_name_is_required(self) -> None: _call_tool_inner("chat_folder_create", {"parent": "kirocrew"}) +class TestFolderDelete: + """``chat_folder_delete`` is a thin wrapper over ``DELETE /api/chat/folders/{id}``: + the endpoint owns the rules (the frozen-subtree cascade, the app and member + refusals); this server resolves the reference, passes the flag through and + reports the endpoint's counts.""" + + def test_default_is_the_safe_delete_and_names_what_moved(self) -> None: + with ( + patch("kiro_crew.mcp_dashboard._get", side_effect=_rows), + patch("kiro_crew.mcp_dashboard._delete", return_value={"ok": True}) as mock_delete, + ): + out = _call_tool_inner("chat_folder_delete", {"folder": "kirocrew/0811"}) + assert mock_delete.call_args.args == ("/api/chat/folders/bbbbbbbbbbbb",) + # The verified caller key rides on the write, as on every tree-shaping + # write here: the endpoint judges the delete against it. + assert mock_delete.call_args.kwargs["session_key"] == "dashboard:chat-1-100" + assert "kirocrew/0811" in out and "bbbbbbbbbbbb" in out + assert "unfiled" in out and "top level" in out + + def test_delete_contents_passes_the_flag_and_reports_the_counts(self) -> None: + answer = { + "ok": True, + "delete_contents": True, + "deleted_folder_ids": ["aaaaaaaaaaaa", "bbbbbbbbbbbb"], + "unfiled_sessions": 0, + "archived_sessions": 2, + "unfiled_history_sessions": 3, + } + with ( + patch("kiro_crew.mcp_dashboard._get", side_effect=_rows), + patch("kiro_crew.mcp_dashboard._delete", return_value=answer) as mock_delete, + ): + out = _call_tool_inner( + "chat_folder_delete", {"folder": "aaaaaaaaaaaa", "delete_contents": True} + ) + assert mock_delete.call_args.args == ( + "/api/chat/folders/aaaaaaaaaaaa?delete_contents=true", + ) + assert mock_delete.call_args.kwargs["session_key"] == "dashboard:chat-1-100" + # A cascade archives every session in the subtree before answering. + assert mock_delete.call_args.kwargs["timeout"] > 10 + assert "2 folders removed" in out + assert "2 live session(s) archived" in out + assert "3 stored session filing(s) cleared" in out + assert "0 filing(s) cleared from live sessions" in out + + def test_the_resolved_id_travels_as_one_path_segment(self) -> None: + """A stored id is a string the store minted, but the file it lives in can + be edited: an id carrying ``?`` or ``/`` must not rewrite the request's + path or query -- a safe delete must never become a cascade.""" + odd = dict(_FOLDERS[0], id="victim?delete_contents=true", name="Odd") + rows = [odd] + [dict(f) for f in _FOLDERS] + + def _get(path: str) -> list[dict]: + return rows if path == "/api/chat/folders" else _rows(path) + + with ( + patch("kiro_crew.mcp_dashboard._get", side_effect=_get), + patch("kiro_crew.mcp_dashboard._delete", return_value={"ok": True}) as mock_delete, + ): + _call_tool_inner( + "chat_folder_delete", + {"folder": "victim?delete_contents=true", "delete_contents": False}, + ) + assert mock_delete.call_args.args == ("/api/chat/folders/victim%3Fdelete_contents%3Dtrue",) + + def test_a_false_flag_is_the_safe_delete(self) -> None: + with ( + patch("kiro_crew.mcp_dashboard._get", side_effect=_rows), + patch("kiro_crew.mcp_dashboard._delete", return_value={"ok": True}) as mock_delete, + ): + _call_tool_inner("chat_folder_delete", {"folder": "Travel", "delete_contents": False}) + assert mock_delete.call_args.args == ("/api/chat/folders/cccccccccccc",) + + def test_root_is_not_deletable(self) -> None: + with ( + patch("kiro_crew.mcp_dashboard._get", side_effect=_rows), + patch("kiro_crew.mcp_dashboard._delete") as mock_delete, + ): + out = _call_tool_inner("chat_folder_delete", {"folder": "root"}) + assert out.startswith("Error:") + mock_delete.assert_not_called() + + def test_an_unknown_folder_is_refused_before_the_write(self) -> None: + with ( + patch("kiro_crew.mcp_dashboard._get", side_effect=_rows), + patch("kiro_crew.mcp_dashboard._delete") as mock_delete, + ): + out = _call_tool_inner("chat_folder_delete", {"folder": "kirocrew/nope"}) + assert out.startswith("Error:") + mock_delete.assert_not_called() + + @pytest.mark.parametrize( + "refusal", + [ + { + "error": "an app cannot delete folders - ask the person, or move your " + "sessions out and leave the folder", + "code": "folder_delete_forbidden", + }, + { + "error": "This operation requires the owner. Use the member's scoped tools instead.", + "code": "member_scope_denied", + }, + ], + ids=["app-agent", "crew-member"], + ) + def test_the_ownership_refusal_is_the_endpoints_and_is_surfaced(self, refusal: dict) -> None: + """An app agent and a crew member are refused by the endpoint whoever created + the folder (an app's 403 ``folder_delete_forbidden``; the member gate admits no + DELETE on the folder routes). The wrapper decides nothing of its own and + hands the refusal back verbatim, in both modes.""" + for args in ({"folder": "Travel"}, {"folder": "Travel", "delete_contents": True}): + with ( + patch("kiro_crew.mcp_dashboard._get", side_effect=_rows), + patch("kiro_crew.mcp_dashboard._delete", return_value=refusal), + ): + out = _call_tool_inner("chat_folder_delete", args) + assert out == f"Error: {refusal['error']}" + + def test_an_unverifiable_caller_is_refused_before_any_read_or_write(self) -> None: + with ( + patch("kiro_crew.mcp_core._resolve_session_key_strict", return_value=""), + patch("kiro_crew.mcp_dashboard._get") as mock_get, + patch("kiro_crew.mcp_dashboard._delete") as mock_delete, + ): + out = _call_tool_inner("chat_folder_delete", {"folder": "Travel"}) + assert out.startswith("Error:") and "deleting a folder" in out + mock_get.assert_not_called() + mock_delete.assert_not_called() + + def test_folder_is_required_and_the_flag_must_be_a_boolean(self) -> None: + with pytest.raises(ValidationError): + _call_tool_inner("chat_folder_delete", {"delete_contents": True}) + with pytest.raises(ValidationError): + _call_tool_inner("chat_folder_delete", {"folder": "Travel", "delete_contents": "yes"}) + + def test_the_description_tells_the_agent_to_echo_the_count_first(self) -> None: + tool = next(t for t in _list_tools() if t["name"] == "chat_folder_delete") + assert "echo the affected count" in tool["description"] + assert tool["inputSchema"]["required"] == ["folder"] + assert tool["inputSchema"]["properties"]["delete_contents"]["type"] == "boolean" + + class TestAmbiguousFolderPaths: """Folder names are not unique within a parent, so a path can be ambiguous. @@ -1878,6 +2022,7 @@ def test_the_whole_set_is_advertised(self) -> None: assert names == { "chat_folder_tree", "chat_folder_create", + "chat_folder_delete", "chat_folder_move", "chat_folder_move_session", "chat_folder_file_self", diff --git a/test/test_mcp_dashboard_registration.py b/test/test_mcp_dashboard_registration.py index 250f65024fb1..c0e704dd6109 100644 --- a/test/test_mcp_dashboard_registration.py +++ b/test/test_mcp_dashboard_registration.py @@ -324,9 +324,16 @@ class TestWhatThisSetGrants: A spec that references this server gets every tool in it — there is no per-tool granularity in the mount. That is sound for the current tools: they grant no read the agent lacks (``list_sessions`` in core already returns every - session's title and key) and delete nothing. It stops being sound the moment a - capability with real blast radius is added to this same set, because granting - the folder tools would silently grant that too. + session's title and key), and the one delete among them -- + ``chat_folder_delete`` -- is bounded by its endpoint rather than by this set: + an app agent and a crew member are refused outright (``folder_delete_forbidden`` + / the member gate admits no DELETE on the folder routes), so for every agent + principal it deletes nothing, and for the person's own sessions its + ``delete_contents`` cascade removes folder rows and ARCHIVES sessions through + the same ``close_slot`` path ``session_close`` in this very set already grants + -- never a transcript. It stops being sound the moment a capability with real + blast radius for an agent principal is added to this same set, because + granting the folder tools would silently grant that too. This ratchet pins the set, so such a capability fails here until the author puts it in a server of its own with the gate it actually needs. @@ -336,6 +343,7 @@ class TestWhatThisSetGrants: "chat_folder_tree", "chat_folder_create", "chat_folder_move", + "chat_folder_delete", "chat_folder_move_session", "chat_folder_file_self", } diff --git a/test/test_resume_publishes_hydrated_slot.py b/test/test_resume_publishes_hydrated_slot.py index ce5ba5cfa840..6e39324a2fe1 100644 --- a/test/test_resume_publishes_hydrated_slot.py +++ b/test/test_resume_publishes_hydrated_slot.py @@ -161,7 +161,7 @@ async def test_slot_is_not_reachable_unhydrated_during_the_folder_unhide(tmp_pat unhide_entered = asyncio.Event() may_finish = asyncio.Event() - async def slow_unhide(_state, _folder_id): + async def slow_unhide(_state, _folder_id, **_kw): unhide_entered.set() await may_finish.wait() return True @@ -562,6 +562,172 @@ async def test_a_dangling_folder_id_unchanged_across_the_read_is_still_dropped( ) +@pytest.mark.asyncio +@pytest.mark.parametrize("stored", [["x"], {"a": 1}, 7], ids=["list", "dict", "int"]) +async def test_a_stored_folder_id_of_the_wrong_shape_resumes_unfiled_and_is_cleared( + tmp_path, monkeypatch, stored +): + """The metadata line is a file an agent's tools can edit. A ``folder_id`` that + is not a string names no folder: the resume answers 200, the session publishes + unfiled, and the stored value is cleared by the hydrate's own drop -- instead + of the freeze predicates hashing a list and failing the resume every time.""" + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + log = state.conversation_log + key = "dashboard:foldershape1" + log.append(key, "user", "history-1") + log.update_metadata(key, {"folder_id": stored}) + assert log.get_metadata(key)["folder_id"] == stored, "fixture: the odd shape was stored" + + async with TestClient(TestServer(_make_app(state))) as client: + resp = await client.post("/api/chat/slots/foldershape1/resume", json={"key": key}) + + assert resp.status == 200, f"resume did not publish (status {resp.status})" + slot = state._slots.get("foldershape1") + assert slot is not None, "the slot was never published" + assert slot.folder_id == "", f"the odd value survived resume (folder_id={slot.folder_id!r})" + assert state.serialize_slot(slot)["folder_id"] == "" + # The clear is durable: the slot's own save writes the unfiled record. + from kiro_crew.dashboard.chat_persistence import save_slot_off_loop + + assert await save_slot_off_loop(state, slot, force=True) + assert not log.get_metadata(key).get("folder_id"), "the stored value was not cleared" + + +@pytest.mark.asyncio +async def test_a_resume_into_a_folder_a_running_delete_has_frozen_keeps_the_stored_filing( + tmp_path, monkeypatch +): + """Frozen is not gone. A folder a ``delete_contents`` cascade has frozen still + exists and the delete may abort, so a resume landing inside the cascade KEEPS + the stored filing: the slot publishes with it (registered, so the delete's + commit-time sweep unfiles it if the delete commits), the slot payload projects + it as unfiled for as long as the folder is frozen, and nothing is written -- + the metadata line still names the folder, the frozen row keeps its ``hidden`` + flag. Thawed (the abort), the payload renders the filing again.""" + from kiro_crew.dashboard import chat_folders + + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + log = state.conversation_log + key = "dashboard:frozenresume1" + log.append(key, "user", "history-1") + frozen_id = "fldrFROZEN01" + state._folders.append( + {"id": frozen_id, "name": "Going", "parent_id": "", "order": 0, "hidden": True} + ) + log.update_metadata(key, {"folder_id": frozen_id}) + chat_folders._deleting_folder_ids(state).add(frozen_id) + try: + async with TestClient(TestServer(_make_app(state))) as client: + resp = await client.post("/api/chat/slots/frozenresume1/resume", json={"key": key}) + assert resp.status == 200, f"resume did not publish (status {resp.status})" + slot = state._slots.get("frozenresume1") + assert slot is not None, "the slot was never published" + assert slot.folder_id == frozen_id, ( + f"a filing into a FROZEN folder was dropped (folder_id={slot.folder_id!r}); only " + "the delete's own commit may unfile it" + ) + assert state.serialize_slot(slot)["folder_id"] == "", "projected unfiled while frozen" + finally: + chat_folders._deleting_folder_ids(state).discard(frozen_id) + + # Thawed by the abort: the filing stands, stored and rendered. + assert state.serialize_slot(slot)["folder_id"] == frozen_id + assert log.get_metadata(key).get("folder_id") == frozen_id, "the stored filing was erased" + row = next(f for f in state._folders if f["id"] == frozen_id) + assert row.get("hidden") is True, "a frozen folder is never written" + + +@pytest.mark.asyncio +async def test_the_hydrate_never_clears_a_frozen_folders_id_whatever_the_verdict_says( + tmp_path, monkeypatch +): + """The hydrate's clear is for a folder that is GONE. Forced to an 'absent' + verdict while the folder is frozen, it still keeps the id: the stored filing + is the running delete's to unfile, and ``folder_id`` is a clearable field the + next save would otherwise write out as erased.""" + from kiro_crew.dashboard import chat_folders, chat_handlers + + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + log = state.conversation_log + key = "dashboard:frozenverdict1" + log.append(key, "user", "history-1") + frozen_id = "fldrFROZEN02" + state._folders.append({"id": frozen_id, "name": "Going", "parent_id": "", "order": 0}) + log.update_metadata(key, {"folder_id": frozen_id}) + + async def absent_verdict(state_, folder_id, **kwargs): + return False + + monkeypatch.setattr(chat_handlers, "_unhide_folder", absent_verdict) + chat_folders._deleting_folder_ids(state).add(frozen_id) + try: + async with TestClient(TestServer(_make_app(state))) as client: + resp = await client.post("/api/chat/slots/frozenverdict1/resume", json={"key": key}) + finally: + chat_folders._deleting_folder_ids(state).discard(frozen_id) + + assert resp.status == 200, f"resume did not publish (status {resp.status})" + slot = state._slots.get("frozenverdict1") + assert slot is not None and slot.folder_id == frozen_id + + +@pytest.mark.asyncio +async def test_a_folder_deleted_between_the_verdict_and_the_build_is_not_published( + tmp_path, monkeypatch +): + """The late-assignment window itself. The folder verdict is earned before the + awaits that separate it from the build; a delete committing inside one of + them runs its commit-time sweep over the live slot table while this slot does + not exist yet, so nothing unfiles the filing it is about to bind. The + synchronous re-ask adjacent to the build catches it: the session publishes + unfiled, with nothing live naming the removed folder.""" + from kiro_crew.dashboard import chat_handlers + + monkeypatch.setattr("kiro_crew.dashboard.state.config_dir", lambda: tmp_path) + state = _make_state(tmp_path) + log = state.conversation_log + key = "dashboard:lateverdict1" + log.append(key, "user", "history-1") + going_id = "fldrGOING001" + state._folders.append({"id": going_id, "name": "Going", "parent_id": "", "order": 0}) + log.update_metadata(key, {"folder_id": going_id}) + real_unhide = chat_handlers._unhide_folder + seen: dict[str, object] = {} + + async def verdict_then_delete(state_, folder_id, **kwargs): + # The verdict is earned against a folder that exists... + verdict = await real_unhide(state_, folder_id, **kwargs) + seen["verdict"] = verdict + + def _remove(folders): + folders[:] = [f for f in folders if f.get("id") != going_id] + return True, None + + # ...and the folder is committed away inside the resume's await window, + # before the slot exists for any sweep to find. + await state_.mutate_folders(_remove) + return verdict + + monkeypatch.setattr(chat_handlers, "_unhide_folder", verdict_then_delete) + async with TestClient(TestServer(_make_app(state))) as client: + resp = await client.post("/api/chat/slots/lateverdict1/resume", json={"key": key}) + + assert ( + seen["verdict"] is True + ), "fixture: the pre-await verdict must have read the folder present" + assert resp.status == 200, f"resume did not publish (status {resp.status})" + slot = state._slots.get("lateverdict1") + assert slot is not None, "the slot was never published" + assert slot.folder_id == "", ( + f"the slot published filed under a folder deleted during the resume's awaits " + f"(folder_id={slot.folder_id!r})" + ) + assert going_id not in {f["id"] for f in state._folders} + + @pytest.mark.asyncio @pytest.mark.parametrize("stamp_closed_at", [True, False]) async def test_a_close_landing_during_the_read_is_not_cleared_by_the_stale_snapshot( diff --git a/test/test_session_control.py b/test/test_session_control.py index 2e60e6d3f3e8..09a87cd37b23 100644 --- a/test/test_session_control.py +++ b/test/test_session_control.py @@ -3717,6 +3717,33 @@ def test_create_refuses_an_unbounded_or_bad_charset_model(tmp_path, bad_model): assert state.live_slot_count() == before, "a refused create must not leave a slot behind" +def test_create_refuses_a_folder_frozen_by_a_running_delete(tmp_path): + """A folder a ``delete_contents`` cascade has frozen is going; a session + filed into it now would outlive it as a dangling id, so the existence check + reads it as absent for the request's duration.""" + from kiro_crew.dashboard import chat_folders + + state = _make_state(tmp_path) + caller = _slot(state, "chat-1") + _folder(state, "fold00000001", "Goal") + before = state.live_slot_count() + chat_folders._deleting_folder_ids(state).add("fold00000001") + try: + with pytest.raises(sc.SessionControlError) as exc: + asyncio.run( + sc.create_session(state, caller_session_key=_key(caller), folder_id="fold00000001") + ) + finally: + chat_folders._deleting_folder_ids(state).discard("fold00000001") + assert exc.value.code == "folder_not_found" + assert state.live_slot_count() == before + # Thawed, the same folder accepts the create. + result = asyncio.run( + sc.create_session(state, caller_session_key=_key(caller), folder_id="fold00000001") + ) + assert state.get_slot(result["target"]).folder_id == "fold00000001" + + def test_a_folder_deleted_mid_create_is_refused_under_the_lock(tmp_path, monkeypatch): """Folder existence is decided under the folder-store lock, late. diff --git a/test/test_session_control_fork.py b/test/test_session_control_fork.py index bc0d8c7f4a7f..62baa9f2a120 100644 --- a/test/test_session_control_fork.py +++ b/test/test_session_control_fork.py @@ -284,6 +284,23 @@ def test_without_a_folder_the_child_inherits_the_sources_folder(tmp_path): assert state.get_slot(result["target"]).folder_id == folder_id +def test_a_folder_frozen_by_a_running_delete_refuses_the_whole_fork(tmp_path): + from kiro_crew.dashboard import chat_folders + + state = _make_state(tmp_path) + caller = _seed(state, "chat-1") + folder_id = _folder(state, "fold00000003", "Going") + before = set(state._slots) + chat_folders._deleting_folder_ids(state).add(folder_id) + try: + with pytest.raises(sc.SessionControlError) as exc: + _fork(state, caller, folder_id=folder_id) + finally: + chat_folders._deleting_folder_ids(state).discard(folder_id) + assert exc.value.code == "folder_not_found" + assert set(state._slots) == before + + def test_an_unknown_folder_refuses_the_whole_fork(tmp_path): state = _make_state(tmp_path) caller = _seed(state, "chat-1") diff --git a/test/test_session_control_revive.py b/test/test_session_control_revive.py index 9fa580187da7..21985ca68fa1 100644 --- a/test/test_session_control_revive.py +++ b/test/test_session_control_revive.py @@ -298,6 +298,39 @@ def test_revive_files_into_the_requested_folder(tmp_path): assert state.conversation_log.get_metadata(f"dashboard:{key}").get("folder_id") == "f1" +def test_revive_assigns_the_folder_only_after_its_verdict(tmp_path, monkeypatch): + """While the destination's verdict is pending the revived slot is live and + published; a provisional folder_id there would be read by a delete cascade's + membership pass and at close_slot's point of no return. So nothing is assigned + until the store has answered, and a refusal leaves nothing to roll back.""" + state = _make_state(tmp_path) + _folder(state, "f1", "Gamma") + caller = _slot(state, "chat-1") + key = _archive(state, caller, _slot(state, "chat-2")) + real = sc._unhide_folder + seen: list[str] = [] + verdict = {"answer": True} + + async def _record_then_answer(state_, folder_id, **kw): + seen.append(state._slots[key].folder_id) + if not verdict["answer"]: + return False + return await real(state_, folder_id, **kw) + + monkeypatch.setattr(sc, "_unhide_folder", _record_then_answer) + result = _revive(state, caller, key, folder_id="f1") + assert seen == [""], "the filing was live before its verdict" + assert result["filed"] is True and state._slots[key].folder_id == "f1" + + # And a refusal at the verdict: nothing assigned, nothing rolled back, revived. + key2 = _archive(state, caller, _slot(state, "chat-3")) + verdict["answer"] = False + result = _revive(state, caller, key2, folder_id="f1") + assert result["filed"] is False and result["folder_id"] == "" + assert state._slots[key2].folder_id == "" + assert not state.conversation_log.get_metadata(f"dashboard:{key2}").get("folder_id") + + def test_revive_refuses_an_unknown_folder_before_touching_history(tmp_path): state = _make_state(tmp_path) caller = _slot(state, "chat-1") @@ -326,6 +359,47 @@ def test_revive_without_a_folder_keeps_the_previous_placement(tmp_path): assert state._slots[key].folder_id == "f1" +def test_a_folder_deleted_while_the_hook_awaits_is_not_published_on_the_revived_slot( + tmp_path, monkeypatch +): + """The hook path of the late-assignment window. The core builds the slot with + its stored filing, RETRACTS it from the table while the containment hook + awaits, and publishes it afterwards. A folder delete committing inside the + hook's awaits sweeps the live table without seeing the retracted slot, so the + publish is re-asked synchronously about the folder the built slot carries: + gone, the session publishes unfiled.""" + state = _make_state(tmp_path) + _folder(state, "f1", "Gamma") + caller = _slot(state, "chat-1") + peer = _slot(state, "chat-2") + peer.folder_id = "f1" + key = _archive(state, caller, peer) + real_probe = sc._has_channel_mirror + seen: dict[str, object] = {} + + def delete_the_folder_then_probe(state_, built): + # Inside the hook's await window: the slot is built (its filing bound) + # and out of the table. ``_has_channel_mirror(state, built)`` is the one + # probe the hook alone runs on the BUILT slot (the caller-surface check + # runs it on the caller's slot too, hence the key test). + if built.key == key and "built_folder" not in seen: + seen["built_folder"] = built.folder_id + assert ( + key not in state_._slots + ), "fixture: the built slot is retracted while the hook runs" + state_._folders[:] = [f for f in state_._folders if f.get("id") != "f1"] + return real_probe(state_, built) + + monkeypatch.setattr(sc, "_has_channel_mirror", delete_the_folder_then_probe) + + result = _revive(state, caller, key) + + assert seen["built_folder"] == "f1", "fixture: the slot was built with its stored filing" + assert result["folder_id"] == "" + assert state._slots[key].folder_id == "", "a filing into a deleted folder was published" + assert "f1" not in {f["id"] for f in state._folders} + + # ── Resolution refusals ────────────────────────────────────────────────────── diff --git a/test/test_session_transfer.py b/test/test_session_transfer.py index e23426fd8ae3..efdee31fe045 100644 --- a/test/test_session_transfer.py +++ b/test/test_session_transfer.py @@ -4867,8 +4867,8 @@ async def _count_then_delete_the_folder(_state, slot, *_a, **_k): state = _stub_state(st, monkeypatch, save=_count_then_delete_the_folder) real_exists = st.arrival_folder_exists - async def _a_close_lands_inside_the_check(_state, folder_id): - answer = await real_exists(_state, folder_id) + async def _a_close_lands_inside_the_check(_state, folder_id, **kw): + answer = await real_exists(_state, folder_id, **kw) if not answer: # The folder is gone, so this is the call that arms the repair. The # person closes the tab in the same window: a close pops the slot @@ -4894,6 +4894,40 @@ async def _a_close_lands_inside_the_check(_state, folder_id): ) +@pytest.mark.asyncio +async def test_an_arrival_folder_frozen_during_the_save_keeps_the_filing(monkeypatch): + """Frozen is not gone. A ``delete_contents`` cascade freezing the arrival + folder while the durable save is in flight must not arm the repair: the folder + still exists, and the cascade's own commit decides the filing. The repair's + existence read is asked with ``frozen_is_present`` for exactly this reason.""" + from kiro_crew.dashboard import chat_folders + from kiro_crew.dashboard import session_transfer as st + + saves: list[str] = [] + frozen: list[str] = [] + + async def _freeze_the_folder_during_the_save(_state, slot, *_a, **_k): + saves.append(getattr(slot, "folder_id", "")) + if slot.folder_id and not frozen: + chat_folders._deleting_folder_ids(state).add(slot.folder_id) + frozen.append(slot.folder_id) + return True + + state = _stub_state(st, monkeypatch, save=_freeze_the_folder_during_the_save) + try: + resp = await st.api_chat_slot_import(_make_request(state, _valid(origin="mac"))) + finally: + for folder_id in frozen: + chat_folders._deleting_folder_ids(state).discard(folder_id) + + assert saves and saves[0], "the premise: the durable save carried a folder_id" + assert frozen == [saves[0]] + assert len(saves) == 1, "no repair save: the filing is kept, not erased" + assert state._imported_slot.folder_id == saves[0] + assert resp.status == 200, resp.body + assert json.loads(resp.body)["ok"] is True + + @pytest.mark.asyncio async def test_a_delete_during_the_folder_check_is_not_reported_as_landed(monkeypatch): """The folder-gone guard alone leaves the COMMON case unchecked. @@ -4921,8 +4955,8 @@ async def test_a_delete_during_the_folder_check_is_not_reported_as_landed(monkey real_exists = st.arrival_folder_exists - async def _watch_exists(_state, folder_id): - answer = await real_exists(_state, folder_id) + async def _watch_exists(_state, folder_id, **kw): + answer = await real_exists(_state, folder_id, **kw) existed.append(answer) return answer