Skip to content

ai-providers: export the shared history-entry renderer for text-only providers #1757

In review

Description

@smart-ssh-planner

Goal

Let an embedding application that contributes its own text-only AiProvider (wrapped in TextActionFallback) render the conversation history exactly as the built-in providers do, instead of re-implementing the rendering and its fencing.

Context

  • TextActionFallback (crates/ai-providers/src/fallback.rs, public, Extension point: register AI provider kinds through Wiring (storage, factory, commands) #1637) covers the system-prompt addition and the action-marker parsing, but not the rendering of history entries.
  • The model-facing text of a history entry is produced by message_content_text (crates/ai-providers/src/common/prompt_format.rs:23) and the wire role by role_str (:10). Both are pub(crate), and mod common is private (crates/ai-providers/src/lib.rs:9).
  • format_command_result and format_action_rejected in the same file are also not public. They are the single place where untrusted remote output is fenced (ai-providers: one shared formatter for command results and rejected actions (single fencing call site) #845, Spec 0039). A provider outside this crate can only call the public parts (WebActivity::to_model_text, SealedContent::text_for_model, ask_user_model_text), so it would have to copy the fencing logic, and a later hardening there would not reach it.

Requirements

  • Export from ai-providers a function history_entry_text(content: &MessageContent) -> String with the behaviour of today's message_content_text.
  • Export a function role_label(role: Role) -> &'static str with the behaviour of today's role_str.
  • Both are thin public re-exports/wrappers. Internal call sites keep using one implementation; no logic is duplicated or changed.
  • Doc comments state that the text contains fenced untrusted content and must be passed to a model unmodified.

Acceptance Criteria

  • ai_providers::history_entry_text and ai_providers::role_label are public and documented
  • A test calls the public functions for every MessageContent variant and asserts the same output as the existing internal rendering, including the fence and security_notice for a command result
  • Output of the built-in providers is unchanged (existing provider tests pass without edits)
  • Without any external caller the behaviour is unchanged (test covering the default path: the Anthropic/OpenAI request bodies stay byte-identical)
  • Relevant automated tests pass

Technical Notes

  • Affected: crates/ai-providers/src/common/prompt_format.rs, crates/ai-providers/src/lib.rs.
  • Touches the single fencing call site only by exposing it; it must not change what is fenced or how (security invariant: fencing at exactly one place). No filter, risk or confirmation code is involved. No data model change.
  • Update the extension points list in docs/codemap.md (npm run check-codemap).

Files: crates/ai-providers/src/common/prompt_format.rs, crates/ai-providers/src/lib.rs, docs/codemap.md

Spec impact: none

Open questions

  • Export a single render_history(&[ChatMessage]) -> Vec<(&'static str, String)> instead of two functions? Option A: two functions (smallest, matches the existing internals). Option B: one list function (less to get wrong for callers, but a new shape). Recommendation: A.

Related Work

Activity

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

    @smart-ssh-coder
    Contributor

    Implementation ready for review. PR #1758. Exported ai_providers::history_entry_text and role_label as re-exports of the single internal renderer (no logic change); added an all-variants test, updated the codemap. CI green on PR #1758.

  3. smart-ssh-reviewer commented on Dec 5, 2026

    @smart-ssh-reviewer

    Review: 1. docs/codemap.md: the entry is in §6 'Large files' (prompt_format.rs has 428 lines). The issue asks for the §4 'Extension points' list. The entry also overclaims ('the one place untrusted output is fenced'; only remote command output is fenced there). Move it to §4, name history_entry_text/role_label backticked, and say 'remote command output'. 2. The pub use … as … aliases give one function two names. Rustdoc then says role_label is 're-exported as role_label', check-codemap cannot see the names, and the test compares a function with itself. Rename the functions in prompt_format.rs to history_entry_text/role_label, update the three provider use lines, the comments and the compaction.rs:826 reference, and re-export with a plain pub use. 3. The AC2 test has no evidential value: it runs inside the crate (stays green with pub(crate)) and compares the function with itself. Move it to crates/ai-providers/tests/ and assert every MessageContent variant against fixed expected text or the public core renderers. Keep the fence/security_notice and role checks. 4. AC4: add a test that pins the Anthropic, OpenAI Chat Completions and Responses request bodies for a history with every variant and role to the bodies produced by the base.

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

    @smart-ssh-coder
    Contributor

    Implementation ready for review. PR #1758. Renamed the provider functions to history_entry_text/role_label with a plain pub use, moved the codemap entry to section 4, and replaced the in-crate test with black-box tests in tests/. Added pinned request-body tests for Anthropic, Chat Completions and Responses, checked against a clean develop. CI is green.

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

    @smart-ssh-reviewer

    Review passed – moving on to merge. Approved. All four findings from the last review are fixed: the codemap entry is in §4 with backticked symbols, the functions are renamed with a plain re-export, black-box tests in tests/ check every variant against fixed text, and pinned request bodies for Anthropic, Chat Completions and Responses pass on the base and fail on a rendering change. Fencing is unchanged and stays in one place. CI is green. Non-blocking: the in-crate test test_role_str_maps_action_result_to_user still has the old name.

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

    enhancementsecurity

    Fields

    Priority

    P2 - Normal

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions