Skip to content

fix(apps): limit an app's send on a user session to the turn - #16213

Merged
chenmingwei23 merged 1 commit into
mainfrom
fix/app-chat-cross-slot-scope
Oct 4, 2026
Merged

chenmingwei23 merged 1 commit into
mainfrom
fix/app-chat-cross-slot-scope

Conversation

@bolichen97

@bolichen97 bolichen97 commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: An app that holds permissions.sessionApproval can send a turn to a user's session and nothing more; it can no longer change that session's settings or read past its own turn.

permissions.sessionApproval lets an app send a turn to a user-owned dashboard session through POST /api/chat. The same request could also change the session around that turn. It could bind an agent onto an agent-less slot, write the persona fields (color_theme, theme_consent, theme_consent_sha), or run a harness slash command from the message text. It could also skip the sub-agent hold with steer, supply its own row id, or drive the session's automatic title. And it could keep streaming the user's own queued follow-ups while the user's dashboard frames were suppressed.

Why it matters

The grant is described as "send a turn". A session's agent binding, persona consent and title belong to the user, and the user's later turns are theirs. An app with the grant should not be able to change them or read them through its own stream.

Not a goal

  • Changing what the dashboard user or an app on its own slot may do through POST /api/chat.
  • Re-judging slot ownership at session acquisition, or the other session routes (regenerate, rename, generate-title, Slack link). That is a separate slot-ownership change.
  • A per-member gate on bind-on-send.
  • Filtering the user's own manual "Regenerate title". It still reads every row.

What changed (motivation → approach → change)

Root cause. api_chat asked one yes/no question, "may this app use this slot?". After that, every setting the body carried was applied the same way for a dashboard user, the owning app and a grant holder.

Approach. The caller's access is now an explicit SlotAccess value: DASHBOARD, OWNER, GRANT or NONE. Only DASHBOARD and OWNER may configure the slot. On the GRANT path:

  • Refused, before anything is written or queued: an agent binding, the persona fields and a harness slash command. The response is 403 app_session_settings_forbidden and an SEL denial is logged. The checks reuse the write sites' predicates (_send_binds_agent, _send_writes_persona) and the runner's own is_harness_slash_command.
  • After the permission and config reads, the slot must still be registered, not closing and still a local user session. Otherwise the request gets the usual 404.
  • Turn-level changes: steer is ignored and a caller-supplied meta.mid is dropped. turnActor becomes a reserved row-meta key. The app's user row is always echoed to the user's open tabs.
  • Titling: an app turn on a slot the app does not own starts no auto-title and does not count as a user turn. Its rows are left out of the title and refresh prompts, the fallback and the low-signal check (chat_title._counts_as_user_turn, _titling_messages). The rehydrate refresh-mark rebase uses the same predicate. A queued-message merge stops where the turn actor changes.
  • Restored queue entries: a restart drops the actor stamp, so who sent a restored entry is unknown. Such an entry drains alone, never merged with fresh words, and its row carries turnProvenanceRestored. Titling skips that row on a slot no app owns.
  • The stream ends with the app's turn. chat_runner._mark_turn_end pushes a turn_end wire frame at the turn's end, before the drain or the cycle end writes anything. It is not pushed before a recovery that the same turn queued for itself. The recovery carries that turn's own id (recoveryTurnId), so another turn's held retry, or a restored one, still gets the boundary. Only a grant-path SSE reader stops at this frame. That reader also stops at any user row that is not its own gateway-minted row, so a missed boundary fails closed. It skips queued placeholder rows and never suppresses the user's broadcast. A grant-path reader never blocks the user's SSE broadcast (_has_reader stays false), and resp.prepare runs inside the pending-consumer scope so a failed prepare releases it.
  • Queue classification: a queued app entry, or one restored from disk, is never classified as a cron or sub-agent event from its text.

The code is in dashboard/chat_handlers.py, dashboard/chat_runner.py (with its chat_turn/steer_queue.py owner), dashboard/chat_title.py, dashboard/chat_utils.py and slot_persistence/restored_metadata.py.

Grant-path POST /api/chat carrying… Before After
plain text 🟦 turn runs 🟦 turn runs
agent on an agent-less slot 🟥 agent bound 🟩 403, SEL denial
color_theme / consent 🟥 persona written 🟩 403, SEL denial
leading harness slash command 🟥 command runs 🟩 403, SEL denial
steer 🟥 skips the sub-agent hold 🟨 ignored
SSE after the app's turn 🟥 streams the user's later turns 🟨 ends at turn_end

🟩 added · 🟨 changed · 🟥 removed · 🟦 unchanged

Backwards compatibility

Breaking: on a user session that an app reaches only through permissions.sessionApproval, a send that names an agent for an agent-less slot, carries color_theme, or opens with a harness slash command now gets a 403 instead of being applied. Writer sweep on origin/main @ 21999ee: the dashboard SPA and ChatEmbed use the cookie (DASHBOARD), and an app on its own slot is OWNER, so neither changes. No bundled app sends these fields onto a user session.

Reader: chat_handlers.api_chat SSE loop -- app -- stops at turn_end on the grant path only (test_app_sse_stream_skips_a_row_queued_for_a_later_turn)
Reader: dashboard WS / global SSE readers -- chat -- skip the turn_end frame, which is never a row (test_the_drain_marks_the_turn_end_before_a_successor_turn)
Reader: chat_title._maybe_auto_title / maybe_refresh_title -- chat -- an app row on its own slot still counts (test_the_title_fallback_never_takes_an_apps_text)

Tests

Tests are in test/test_chat_mode_security.py:

  • The 403 plus SEL denial for each settings trigger, with no allowed row, on agent-bound and agent-less slots.
  • The 404 when the slot closes or is re-linked during the permission or command reads.
  • steer ignored, mid dropped, and the echo to the user's tabs.
  • Titling exclusion: the fallback, the refresh prompt, the reply to an app turn, and the rehydrate rebase.
  • turn_end placement around recoveries, held notes, synthesis and the cycle end; an older or restored app retry cannot extend a later app's stream.
  • The SSE backstop: a human, app or id-less successor row without turn_end never reaches the app.
  • A restored app entry titles only an app-owned slot, and restored and fresh entries never merge.
  • The queued-row skip, the merge stop at an actor change, and the restored-entry classification.
  • test_every_api_chat_body_key_is_classified, an AST pin, so a new body key fails until it is classified.

test_active_turn_session_key, test_dashboard_chat and test_steer_requeue fakes take the new predecessor_actor keyword.

Manual verification

N/A. The unit coverage drives api_chat and the runner drain with real slots.

Related Issues

None.

Pattern harvest

Rule candidate: review-prompt
Pattern: a route that authorizes an action with a boolean, then applies every body field the same way for every caller.

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): app-kit-platform.md, app-platform-trust-model.md, manifest-reference.md, history.md, learn-cron-dashboard.md
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

Closes #16642

@bolichen97
bolichen97 requested a review from a team as a code owner October 2, 2026 17:32
@bolichen97
bolichen97 requested a review from buluoray October 2, 2026 17:32
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Oct 2, 2026
@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — 🟡 CONCERNS

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

Rows from any user's restored queue are now left out of auto-titling on every non-app slot. That costs all users, apps or not, to close a narrow app-after-restart case.

Not justified as shipped

  • Item 7 — oversized: on a slot no app owns, every restored queued row is dropped from titling. _counts_as_user_turn returns false when meta.get(RESTORED_TURN_META_KEY) is True and slot._app is empty. So a user's own words queued before a gateway restart no longer count toward, or name, their session. The harm it closes needs a grant-path send that was queued and then a restart before the queue drained. The description gives no report of that happening.
    Subtraction: delete the RESTORED_TURN_META_KEY arm from _counts_as_user_turn and drop the _drained_meta[RESTORED_TURN_META_KEY] stamp in chat_runner._start_next_queued_turn. Restored rows then count as they do on base.
    Clears when: the restored-row exclusion is removed, or a reported case shows an app's restored queued send naming a user's session.

What this change ships

Inventory (9 items) — 8 justified

Intent: an app with sessionApproval can send a turn to a user's session but cannot reconfigure it or read past its own turn. FIX.

  1. Grant-path send naming an agent for an agent-less slot, setting persona fields, or opening with a harness slash command gets 403 plus an SEL denial — justified
  2. Grant-path send against a slot that closed or was re-linked mid-read gets 404 — justified
  3. Grant-path steer ignored; it waits behind running sub-agents — justified
  4. Caller-supplied meta.mid dropped; turnActor becomes a reserved meta key — justified
  5. An app's row is always echoed to the user's open tabs, and the user's broadcast is no longer suppressed — justified
  6. App turns on a user's session neither start nor feed auto-title or refresh — justified
  7. Restored queued rows excluded from titling on slots no app owns, and drained alone — oversized: costs every user's restored prompt
  8. App SSE stream ends at a new turn_end frame, with a backstop at any user row that isn't its own — justified
  9. App or restored queue entries never classified as cron or sub-agent events from their text — justified

[FIRST-PRINCIPLES-REVIEWED] afb0d86

Description read

[DESCRIPTION-READ] 517db56985e8032d5f7e902ce9b05ab96314905d0b0124118f0fb03086b9f53f

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of afb0d868419f99615615bea9d8c97007fee078d6 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: PASS

Check that every turn-ending path in chat_runner (recovery, synthesis, cycle end) pushes turn_end. If one is missed, the app's stream stays open until the foreign-row backstop or done.

[DESIGN-REVIEWED] afb0d86

Description read

[DESCRIPTION-READ] c75081f393cf20d3d2f03b959820ab89343b1c23448cd7c99cd36d02fed55e52

That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

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

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] afb0d86

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

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

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

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

Review details

No findings.

[OPUS-REVIEWED] afb0d86

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

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

@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Oct 3, 2026
@bolichen97
bolichen97 enabled auto-merge (squash) October 3, 2026 20:44
@bolichen97
bolichen97 force-pushed the fix/app-chat-cross-slot-scope branch from 865710e to c76d06b Compare October 4, 2026 06:39
@bolichen97

bolichen97 commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor Author
  • span=db37e66c9e96 Recovery ownership conflates separate app turns

fixed in c76d06b: _queue_recovery stamps a per-invocation recoveryTurnId; _is_own_recovery requires a single non-restored recovery whose id equals the ending turn's id, so an older held app retry or a restored one always gets turn_end.
pinned: test_a_held_app_recovery_cannot_extend_a_later_apps_stream, test_restored_recovery_identity_cannot_continue_a_live_stream.

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

bolichen97 commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor Author
  • span=01dd9f0739bd Restored app turns can rename the user's session

fixed in c76d06b: the restored stamp is not re-trusted. A restored entry drains alone (no merge with fresh words) and its user row carries turnProvenanceRestored; _counts_as_user_turn skips it unless the slot is app-owned, so auto-title, refresh and the rehydrate rebase all exclude it.
pinned: test_restored_app_turn_titles_only_an_app_owned_slot, test_restored_and_fresh_queue_rows_never_merge.

@bolichen97

bolichen97 commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor Author
  • span=4c46bf4bf491 prepare-failure path / _has_reader rides along undeclared

fixed in c76d06b (body): What changed now states that a grant-path reader never sets _has_reader and that resp.prepare runs inside the pending-consumer scope so a failed prepare releases it.

@bolichen97

bolichen97 commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor Author
  • span=0c58021500d2 turn_end confidentiality rests on call-site discipline

fixed in c76d06b: the grant-path SSE loop in api_chat also stops at the first user-role row that is not its own gateway-minted row, so a missed turn_end fails closed.
pinned: test_app_sse_backstop_rejects_a_foreign_user_row_without_turn_end (human, app and id-less successors).

@bolichen97

bolichen97 commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor Author
  • span=97a4a1267eb8 fail-closed backstop in api_chat

fixed in c76d06b: adopted as suggested; same change as the Watch item (stop on the first foreign user row).

@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 merge conflict Branch has merge conflicts with its base — author must resolve before merge labels Oct 4, 2026
permissions.sessionApproval lets an app send a turn to a user-owned
session through POST /api/chat. The same request could also change the
session around that turn: bind an agent onto an agent-less slot, write
the persona fields (color_theme, theme_consent, theme_consent_sha), run
a harness slash command from the message text, skip the sub-agent hold
with steer, supply its own row id, drive the session's auto-title, or
keep streaming the user's own queued follow-ups while suppressing the
user's dashboard frames.

api_chat now derives the caller's access positively (dashboard, owning
app, or grant). On the grant path it refuses an agent binding, the
persona fields and a harness slash command with 403
app_session_settings_forbidden and an SEL denial before anything is
written or queued, using the same predicates as the write sites. The
send ignores steer, has its row id minted by the gateway, is echoed to
the user's open tabs, and neither the send nor the reply to it starts,
counts toward or feeds the session's auto-title or its refresh; a merge
of queued messages stops where the turn actor changes. An SSE stream
ends at the app's own turn through a turn_end wire frame pushed at the
turn's end before the drain or the cycle's end writes anything else,
never before a recovery the ending turn queued for itself; the stream
skips rows queued for a later turn, and every other reader skips the
frame. The grant path re-checks that the slot is still registered, not
closing and still a local user session after its off-loop reads. A queued app entry, or one restored
from disk, is never classified as a cron or sub-agent event by its
text, and the rehydrate refresh-mark rebase counts turns the way the
refresh does.

An AST pin classifies every body key api_chat reads as turn-only or
settings-bearing, so a new key fails until it is classified. The app
platform spec, trust model, manifest reference, history spec and
dashboard spec describe the narrowed grant.
@bolichen97
bolichen97 force-pushed the fix/app-chat-cross-slot-scope branch from c76d06b to afb0d86 Compare October 4, 2026 08:00
@bolichen97

Copy link
Copy Markdown
Contributor Author

Rebased: main fixed the two inherited reds (#16695) since c76d06b (efd0181 -> af2619f); CI re-running.

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 4, 2026
@chenmingwei23
chenmingwei23 merged commit 1d2fdd5 into main Oct 4, 2026
98 checks passed
@chenmingwei23
chenmingwei23 deleted the fix/app-chat-cross-slot-scope branch October 4, 2026 09:30
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Apps: permissions.sessionApproval lets an app do more than send a turn to a user session

2 participants