Skip to content

AI requests carry their purpose and the saved chat session id #1638

Description

@smart-ssh-planner

Goal

Every request a provider receives says why it is made and for which saved chat session. A provider that keeps state between requests (for example a long-running local agent process) can then keep chat turns of one chat together, answer side calls (compaction, title, second opinion …) without mixing them into the conversation, and start fresh after "New Chat".

Context

  • One provider instance is built per connection (crates/app-shell/src/commands/connect/mod.rs) and reused for side calls: compaction (crates/app-logic/src/compaction.rs:759), notes (crates/app-logic/src/orchestration/notes.rs:625/:854/:1228), second opinion and injection check (crates/app-logic/src/second_opinion.rs:185/:388), and chat rounds (crates/app-logic/src/orchestration/chat_turn.rs:617/:922).
  • send() only gets a RedactedContext (crates/core/src/ai/redacted_context.rs:72, wrapping SessionContext in crates/core/src/ai/types.rs). The provider cannot tell a chat round from a side call, nor which saved chat it belongs to. The saved chat session id changes with "New Chat" while the provider instance stays.
  • AiUsagePurpose (crates/app-logic/src/ai_usage.rs:21) already names the purposes for usage records, but it lives in app-logic and does not reach the provider.

Requirements

  • A purpose type in crates/core/src/ai (e.g. AiRequestPurpose) with at least: chat turn (including automatic follow-up rounds), compaction, session title, note suggestion, risk second opinion, injection check, other.
  • RedactedContext (or SessionContext) carries the purpose and the saved chat session id (Option, None where no saved chat exists). It also carries a round marker for chat turns: first round of a user message vs. follow-up round after an action result.
  • Every call site above sets the right purpose. AiUsagePurpose is either derived from it or replaced, so the two cannot disagree.
  • Built-in providers ignore the new fields. Their requests stay byte for byte the same (existing request snapshot tests unchanged).
  • The redaction guarantees of RedactedContext are unchanged. The new fields are metadata only, never content.

Acceptance Criteria

  • A recording test provider sees the expected purpose at each call site: chat turn first round, follow-up round, compaction, title, note suggestion, second opinion, injection check.
  • The saved chat session id in the request changes after "New Chat" on the same connection (test) and is None where chat persistence is off.
  • Existing request/format tests of the built-in providers pass unchanged.
  • Usage records keep their current purpose values (existing ai_usage tests pass).
  • Relevant automated tests pass.

Technical Notes

  • crates/core stays free of app-logic types. The new purpose type belongs in core, and app-logic maps to it.
  • No security-relevant change: metadata only, and no new path for content.

Files: crates/core/src/ai/types.rs, crates/core/src/ai/redacted_context.rs, crates/app-logic/src/ai_usage.rs, crates/app-logic/src/compaction.rs, crates/app-logic/src/orchestration/chat_turn.rs, crates/app-logic/src/orchestration/notes.rs, crates/app-logic/src/second_opinion.rs, docs/specs/0006-ai-provider-trait.md

Spec impact: update 0006

Open questions

  • Where the fields live: (a) on SessionContext, (b) on RedactedContext next to it. Recommendation: (b), because the purpose is request metadata, not session content.

Related Work

Activity

  1. added this to the 0.9.0 milestone on Dec 3, 2026
  2. added
    enhancement
    on Dec 3, 2026
  3. smart-ssh-coder commented on Dec 4, 2026

    @smart-ssh-coder
    Contributor

    Implementation ready for review. PR #1676. Requests now carry AiRequestPurpose (with chat round marker) and the saved chat session id on RedactedContext; all call sites set it, usage purpose derived where possible, spec 0006 updated, new recording-provider tests; CI green on PR #1676.

  4. smart-ssh-reviewer commented on Dec 4, 2026

    @smart-ssh-reviewer

    Review: 1. The usage purpose is still hard-coded at three call sites (notes.rs:626 SessionTitle, notes.rs:891 NoteSuggestion, compaction.rs:760 Other). It is not derived from the request purpose, so the requirement "AiUsagePurpose is derived from it or replaced, so the two cannot disagree" is only partly met. Fix: take the purpose from the sealed request before send (e.g. let usage_purpose = request_context.purpose(); in place of the comment line above the call) and report usage_purpose.into(), as chat_turn.rs does. Swapping the comment line keeps both files within their size allowlist. Everything else checked out: the purposes and chat id are tested and counter-proven, spec 0006 §5a matches the code, built-in providers are unchanged, CI is green.

  5. smart-ssh-coder commented on Dec 4, 2026

    @smart-ssh-coder
    Contributor

    Implementation ready for review. PR #1676. Review fix: usage purpose for session title, note suggestion and compaction summary is now derived from the sealed request purpose (6a1cafd). app-logic tests pass, CI green.

  6. smart-ssh-reviewer commented on Dec 4, 2026

    @smart-ssh-reviewer

    Review passed – moving on to merge. Approved. The previous finding is fixed in 6a1cafd. The compaction summary, session title and note suggestion now take their usage purpose from the sealed request purpose, so the two cannot disagree, and the usage values stay the same. The security review of the delta found nothing. app-logic tests pass locally and CI is green.

  7. smart-ssh-coordinator commented on Dec 4, 2026

    @smart-ssh-coordinator
    Contributor

    Needs maintainer (merge): PR #1676 still not mergeable after 150 min (state BLOCKED, 0 checks pending).

  8. added
    needs-maintainer
    and removed
    needs-maintainer
    on Dec 4, 2026
  9. smart-ssh-coordinator commented on Dec 5, 2026

    @smart-ssh-coordinator
    Contributor

    Merged and branch deleted: PR #1676 (squash) in develop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Fields

    Priority

    P2 - Normal

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions