Skip to content

perf(dashboard): skip the folder-project thread hop when no folder sets one - #13994

Merged
iamwhatever merged 1 commit into
kirodotdev:mainfrom
Pearcekieser:fix/folder-new-session-latency
Sep 26, 2026
Merged

iamwhatever merged 1 commit into
kirodotdev:mainfrom
Pearcekieser:fix/folder-new-session-latency

Conversation

@Pearcekieser

@Pearcekieser Pearcekieser commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

"New chat" inside a sidebar folder is slower than "New chat" at the top level. On a busy gateway the folder create waits for a worker thread to look up a folder project, even when no folder in the chain sets one.

Why it matters

Starting a chat in a folder is how people keep work organized, so this path runs constantly. In the executor-saturated repro below, a folder create took 161 ms and an unfiled create took 41 ms. This PR removes the wait that produced nothing, which brings the folder create to 81 ms. The rest of the gap is the history save that records the new chat's folder, which has to happen.

What changed (motivation → approach → change)

A new chat inside a folder with no project directory no longer queues on the shared default executor to find that out. The same applies to switching agents in such a folder.

_resolve_folder_project_dir in dashboard/chat_folders.py is now two steps. _folder_declared_project walks the folder chain as plain dicts, which is safe on the event loop. _validate_project_dir then checks the directory on disk. The new resolve_folder_project_dir_off_loop runs the walk on the loop and sends only the validation to a worker thread, and only when a folder in the chain declares a project_dir. Both runtime callers use it: api_chat_slot_create and api_chat_slot_agent in dashboard/chat_handlers.py. There is still one chain walk, so cycle handling, missing ids and which folder wins are unchanged.

The forced history save in the create handler is unchanged. For an owner-dashboard create, _record_explicit_agent_selection has already written the new session's metadata line by then, and this save is what adds folder_id, the folder's tags and the title to it. Without it, a restart before the first message would restore the tab unfiled.

Backwards compatibility

Compatible: request and response shapes are unchanged. Project inheritance and folder_project_invalid behave as before for folders that declare a project.

Tests

test/test_chat_slot_create_folder.py:

  • TestFreshFolderCreateStaysOffTheExecutor checks that a create in a folder with no project never reaches the validator. It also checks that a folder with a project is still validated, and that validation runs on a worker thread only when a project is declared.
  • TestOwnerFolderCreatePersistsTheFiling drives the real create handler as the dashboard owner and asserts folder_id on the metadata line. It fails if the forced save is skipped.

test/test_remote_crew_execution.py::test_an_invalid_folder_project_never_reaches_the_peer now gives its folder a project_dir, so its faked validation error is still reached.

Manual verification

I timed the real handler under aiohttp TestClient, 20 creates per row. The default executor was limited to 4 workers, with a feeder keeping them busy on 40 ms jobs.

Create main median this PR median
unfiled 40.9 ms 40.7 ms
in a folder, no project 161.4 ms 81.3 ms

On an idle pool both paths take about 2 ms before and after.

Related Issues

Fixes #13993

Pattern harvest

Rule candidate: review-prompt
Pattern: an await asyncio.to_thread or run_in_executor call on a request path whose work is a guaranteed no-op for the common input. Check the cheap on-loop condition first, and keep one implementation of that condition.

Checklist

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

@Pearcekieser
Pearcekieser requested review from a team and patrigao September 25, 2026 22:11
@Pearcekieser

Copy link
Copy Markdown
Contributor Author

Intent: Make "New chat" inside a sidebar folder cost the same as an unfiled new chat by removing the two default-executor hops that do nothing for a fresh, message-less slot in a folder without a project.
Not a goal: Changing when or how the slot list is broadcast (that is #13499), the client create flow, or durability of any named/recreated/adopted slot.

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention fork Pull request from a fork (external contributor) readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Sep 25, 2026
@github-actions

github-actions Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

GPT 5.6 Review (fork) — ✅ no blocking findings

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

Review details

No findings.
[GPT-REVIEWED] 56df85e

@github-actions

github-actions Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Fable 5, fork) — ✅ PASS

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

Design-Verdict: PASS

Description says the peer test "now gives its folder a project_dir"; the diff only re-targets the monkeypatch — fix the description sentence.

[DESIGN-REVIEWED] 56df85e

@github-actions

github-actions Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Fable 5, fork) — 🟡 CONCERNS

Premise-level review of 56df85e10b9c827aaf8c57b4f3ed8bf3e7b72f58 via the fork AI-review pipeline — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

First-Principles-Verdict: CONCERNS

The rewritten _resolve_folder_project_dir survives as a second spelling of the new off-loop resolver with zero runtime callers left.

Not justified as shipped

  • Item 4 — zero consumers: after both handler call sites move to resolve_folder_project_dir_off_loop, the kept synchronous composition in chat_folders.py serves only test/test_dashboard_chat.py:15520 (grep _resolve_folder_project_dir in src/: 0 non-defining runtime uses post-diff), so two compositions of the same walk+validate must now stay in sync.
    Subtraction: delete _resolve_folder_project_dir and point the cycle test at _folder_declared_project, whose new tests already cover the cycle case.
    Clears when: the deletion is made, or a runtime consumer of the sync form is named.

What this change ships

Inventory (6 items) — 4 justified

Intent: make "New chat" (and agent switch) inside a project-less folder stop waiting on a saturated worker pool — a FIX (issue #13993, measured 161→81 ms).

  1. Folder create with no declared project no longer queues on the shared executor — justified
  2. Agent switch inside such a folder skips the same wait — justified
  3. Both handlers call one new on-loop resolver (2 consumers counted) — justified
  4. The old synchronous resolver stays behind with no runtime caller — zero consumers
  5. A new test pins that an owner's folder create survives restart still filed — rides along
  6. The remote-crew invalid-project test now declares a project so its faked error still fires — justified

[FIRST-PRINCIPLES-REVIEWED] 56df85e

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Sep 25, 2026
@github-actions

github-actions Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5 Review (fork) — ✅ no blocking findings

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

Review details

No findings.

[OPUS-REVIEWED] 56df85e

@Pearcekieser Pearcekieser changed the title perf(dashboard): skip no-op executor hops when creating a chat in a folder perf(dashboard): skip the folder-project thread hop when no folder sets one Sep 25, 2026
@Pearcekieser
Pearcekieser force-pushed the fix/folder-new-session-latency branch from 1b6aa65 to 5b026b6 Compare September 25, 2026 23:12
@Pearcekieser

Copy link
Copy Markdown
Contributor Author
  • span=a7e1ff0be717: forced save skipped for a fresh folder create

Outcome: fixed in 5b026b6.
Verified: on the owner-dashboard path, _record_explicit_agent_selection writes the new key's metadata line before the save, so the save does persist folder_id, tags and title. The fresh_empty_slot skip is removed and the save guard is back to its main form.
Regression: TestOwnerFolderCreatePersistsTheFiling drives the create handler as the owner and asserts folder_id on the metadata line. It fails with the skip and passes without it. The save.call_count == 0 assertion is gone.

@Pearcekieser

Copy link
Copy Markdown
Contributor Author
  • span=4bc452f479ac: duplicated chain walk, agent-switch hop unfixed

Outcome: fixed in 5b026b6.
_folder_chain_declares_project is deleted. _resolve_folder_project_dir now calls one on-loop walk, _folder_declared_project, then _validate_project_dir. resolve_folder_project_dir_off_loop sends only the validation to a thread and only when a project is declared. Both runtime callers, the create handler and the agent-switch handler, use it.

@Pearcekieser

Copy link
Copy Markdown
Contributor Author
  • span=e7f75c8b6a8f: derive the predicate and the resolver from one walk

Outcome: fixed in 5b026b6.
The separate predicate is deleted. The resolver and its async wrapper both take their answer from _folder_declared_project, so the "does this chain declare a project" check can no longer drift from the validator.

@Pearcekieser

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (d8272b6) with no code change: the new head is 2851c1c and has the same diff as 5b026b6. main had moved 51 commits in modules this PR's files import. The changed and related test files pass on the new base (1161 tests), and the black check passes. Round 2's workflows were still waiting for approval, so this head needs the same one approval.

@Pearcekieser
Pearcekieser force-pushed the fix/folder-new-session-latency branch from 5b026b6 to 2851c1c Compare September 25, 2026 23:21
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 25, 2026
…ts one

Creating a chat inside a sidebar folder, and switching agents in one, sent
_resolve_folder_project_dir to a worker thread even when no folder in the
chain declares a project_dir. That hop queues on the shared default executor
before the response, so on a busy gateway a folder create was slower than an
unfiled one for no result.

Split the resolver into an on-loop chain walk (_folder_declared_project) and
the directory validation, and add resolve_folder_project_dir_off_loop, which
hops to a thread only for a declared project. Both runtime callers use it.
The create handler's forced history save stays: for an owner-dashboard create
it is what writes folder_id, tags and title to the new metadata line.
@Pearcekieser

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (4a12bc8), no code change. The only red on the previous head, Backend Tests (3.12, 8), was test_sandbox_protected_name_holds failing on main itself; #14056 fixed it there. On the new base, the changed test files plus that ratchet test pass (252) and the black gate passes.

@Pearcekieser
Pearcekieser force-pushed the fix/folder-new-session-latency branch from 2851c1c to 56df85e Compare September 26, 2026 19:47
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 26, 2026
@github-actions github-actions Bot added the readiness: passed Eligible automated validation passed for the current revision label Sep 26, 2026
@iamwhatever
iamwhatever enabled auto-merge (squash) September 26, 2026 22:51
@iamwhatever
iamwhatever merged commit 7d8eab1 into kirodotdev:main Sep 26, 2026
99 checks passed
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New chat inside a sidebar folder is slower than an unfiled new chat

2 participants