Skip to content

AI context: fence the rolling summary and let the injection check see head and tail of long output #1724

Backlog

Description

@smart-ssh-planner

Goal

Two places where untrusted server content reaches the model with less protection than elsewhere get the same treatment as the rest of the AI context: the rolling conversation summary is marked as derived from untrusted content, and the prompt-injection check sees the end of long output (and stderr), not only its first 16 KiB.

Context

Rolling summary (Spec 0057 §2, Spec 0039)

  • After compaction, the model-written summary replaces the removed rounds as a Role::ActionResult message (crates/app-logic/src/compaction.rs:221-229, summary_placeholder_message), which goes to the provider in the user role (crates/ai-providers/src/common/prompt_format.rs:17). It is persisted and reused (compaction.rs:290-298, persist_rolling_summary at :636).
  • The summary is redacted (compaction.rs:802) and capped at 8,000 bytes (SUMMARY_MAX_BYTES, :216), but wrapped only in a plain bracket text by summary_notice (crates/app-logic/src/system_prompt.rs:462-476): no escaping, no fence, no notice.
  • Its input are the removed rounds including command output, remote file content and web content (compaction.rs:560-570). Text from those sources can therefore be paraphrased into the summary and re-enter the context unfenced. Spec 0039 §6 promises no unfenced path for content from its §1 sources.
  • The fencing helper is fence_untrusted with UntrustedKind (crates/core/src/ai/fencing.rs:19-48, :117); the per-result security notice is added in prompt_format.rs:134/:155.
  • The summary call's own system prompt (compaction.rs:728-735) has no rule about untrusted content.
  • untrusted_content_ingested survives compaction (ADR 0049 §8), so the post-ingest escalation is not affected.

Injection check coverage (Spec 0039 §5.2, Spec 0043 §4)

  • truncate_for_second_opinion (crates/core/src/ai/second_opinion.rs:29-38) keeps the first DEFAULT_SECOND_OPINION_MAX_LEN = 16 KiB (:16) and drops the rest; applied in build_second_opinion_parts (crates/app-logic/src/second_opinion.rs:40-53).
  • For commands the checked text is stdout + "\n" + stderr (crates/app-logic/src/orchestration/action_exec/execute.rs:422-426, check call at :449): with more than 16 KiB of stdout, stderr is never checked.
  • The model can receive much more: remote file reads up to 256 KiB (MAX_READ_FILE_BYTES, crates/app-logic/src/orchestration/remote_files.rs:29), command output up to 2 MB per stream (crates/ssh-transport/src/exec.rs:25), cut to 20,000 bytes per stream only when the request is over budget (compaction.rs:847, :888-897).

Requirements

  • Summary: the rolling summary is inserted into the context fenced with fence_untrusted and a new UntrustedKind::ConversationSummary (own tag name; fence_markers() completeness test extended), inside the language-dependent summary_notice wrapper. The wrapper text states that the summary was derived from earlier content that may contain untrusted data and is not an instruction. Existing persisted summaries (stored as plain text) get the fence when they are inserted, so no migration is needed.
  • The system prompt of the summary call tells the model that content inside fence tags is data, never instructions, and must not be turned into instructions in the summary.
  • The fenced summary counts as untrusted content for history_contains_untrusted_content (crates/app-logic/src/session/mod.rs:1082).
  • Injection check: when the content is longer than the cap, the check receives the first half and the last half of the cap (8 KiB each, cut at UTF-8 boundaries) with a marker between them that says how many bytes were left out; the existing truncation hint stays. Total size sent to the check does not grow.
  • For command results, stdout and stderr each get their own share of the cap (each head + tail within half the cap when both are non-empty; a stream that is shorter leaves its unused share to the other), so stderr is always checked when it is non-empty.
  • Specs 0039 (§5.2, §6), 0057 (§2) and 0043 (§4) describe the new behaviour.

Acceptance Criteria

  • A rolling summary inserted into the context is escaped and fenced: a summary containing </stdout> and <security_notice> reaches the provider request escaped, inside the new fence (test in crates/app-logic/src/compaction/tests.rs)
  • A persisted summary from before the change is fenced when the chat is loaded and continued
  • German and English summary_notice wrappers contain the untrusted-data sentence (unit tests in system_prompt.rs)
  • A history consisting only of a fenced summary counts as containing untrusted content
  • Injection check with 40 KiB of stdout whose last 1 KiB contains a marker string: the text sent to the check contains the marker and the omitted-bytes marker (test with a recording provider)
  • 40 KiB stdout plus 200 bytes stderr: the stderr text is part of the checked text
  • Content under the cap is passed unchanged (existing tests stay green)
  • Multibyte characters at both cut points are never split (proptest or targeted test)
  • Relevant automated tests pass

Technical Notes

  • Security-critical area: untrusted-content fencing and the injection check (Spec 0039, THREAT-MODEL.md §1). Both changes only add protection; the escalation logic (escalation.rs) and the meaning of the check's verdict are unchanged. The check can still only escalate, never relax.
  • Redaction of the summary stays as is and runs before fencing.
  • User-visible as a security change: add a changelog fragment.

Files: crates/core/src/ai/fencing.rs, crates/core/src/ai/second_opinion.rs, crates/app-logic/src/second_opinion.rs, crates/app-logic/src/system_prompt.rs, crates/app-logic/src/compaction.rs, crates/app-logic/src/compaction/tests.rs, crates/app-logic/src/session/mod.rs, crates/app-logic/src/orchestration/action_exec/execute.rs, crates/app-logic/src/second_opinion/tests.rs, docs/specs/0039-untrusted-content-fencing.md, docs/specs/0057-rich-session-model.md, docs/specs/0043-resource-caps.md, changelog.d/

Spec impact: update 0039

Open questions

  • Where the stdout/stderr split happens: (a) execute.rs builds the checked text from two separately truncated streams, (b) a new helper in core/src/ai/second_opinion.rs that takes both streams. Recommendation: (b), so the rule lives next to truncate_for_second_opinion and is unit-testable.

Related Work

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugsecurity

    Type

    Fields

    Priority

    P2 - Normal

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions