Skip to content

Extract WTA prompt building and turn metrics - #529

Open
vanzue wants to merge 2 commits into
mainfrom
dev/kaitao/wta-acp-prompt-metrics
Open

Extract WTA prompt building and turn metrics#529
vanzue wants to merge 2 commits into
mainfrom
dev/kaitao/wta-acp-prompt-metrics

Conversation

@vanzue

@vanzue vanzue commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Summary

  • extract ACP prompt templates, assembly, pane/context resolution, and prompt audit logging into focused prompt modules
  • extract concurrent per-turn timing, ACP timing logs, and response-latency telemetry into turn_metrics.rs
  • establish one-way dependencies from client to prompt_builder to prompt_context, while keeping callbacks and session orchestration in client.rs
  • reduce client.rs by roughly 1,670 lines and update the architecture refactor plan

Validation

  • cargo test --manifest-path tools/wta/Cargo.toml --quiet (1213 passed)
  • cargo build --manifest-path tools/wta/Cargo.toml --quiet
  • manual Debug Intelligent Terminal smoke testing of normal prompts, pane context, autofix, multi-tab routing, and permission/tool-call sequencing
  • cargo clippy --manifest-path tools/wta/Cargo.toml --all-targets --quiet remains blocked by the pre-existing clippy::never_loop error in src/ui/recommendations.rs; this PR does not modify that file

Move ACP prompt assembly, pane context resolution, and prompt audit logging into focused prompt modules. Move per-turn timing and response telemetry into a neutral metrics module while preserving session and callback orchestration in the ACP client.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: f0b2a188-f62a-48b7-b316-6fc31ed3880c
Copilot AI review requested due to automatic review settings July 30, 2026 08:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the WTA ACP client implementation by extracting prompt construction/context resolution and per-turn timing/telemetry into dedicated modules, reducing client.rs size and clarifying module dependencies within tools/wta/src/protocol/acp/.

Changes:

  • Added prompt_builder.rs to centralize prompt template selection, per-session template memoization, prompt assembly, and per-turn audit logging.
  • Expanded prompt_context.rs to own pane/context resolution (active vs. source pane for autofix), shell identity detection, and shared terminal-buffer capture helpers/providers.
  • Added turn_metrics.rs to own prompt timing state, timing logs, and response-latency telemetry; updated call sites and tests accordingly.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tools/wta/src/protocol/acp/turn_metrics.rs New module for per-turn timing state/logs and response-latency telemetry.
tools/wta/src/protocol/acp/prompt_context.rs Moves terminal context assembly and pane/shell resolution helpers into a focused module with tests.
tools/wta/src/protocol/acp/prompt_builder.rs New module for prompt template memoization, prompt assembly, and turn audit logging.
tools/wta/src/protocol/acp/mod.rs Wires new prompt/timing modules into the ACP module tree.
tools/wta/src/protocol/acp/mock_agent_tests.rs Updates imports to use the new prompt/timing modules.
tools/wta/src/protocol/acp/client.rs Switches prompt assembly/timing responsibilities to the extracted modules.
tools/wta/src/app.rs Updates import of prompt_timing_log to the new module.
doc/specs/wta-architecture-refactor.md Updates the refactor plan/status to reflect the new module boundaries.

Comment thread tools/wta/src/protocol/acp/prompt_builder.rs
Comment thread tools/wta/src/protocol/acp/prompt_builder.rs
Comment thread tools/wta/src/protocol/acp/prompt_context.rs
Comment thread tools/wta/src/protocol/acp/turn_metrics.rs
Comment thread tools/wta/src/protocol/acp/prompt_builder.rs
Use bounded iterators for prompt snippets, compact session IDs, and pane-output truncation while preserving the existing output.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: f0b2a188-f62a-48b7-b316-6fc31ed3880c
Copilot AI review requested due to automatic review settings July 30, 2026 09:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (2)

tools/wta/src/protocol/acp/prompt_builder.rs:243

  • log_turn_trace calls prompt_text.chars().count() to decide whether to include a tail snippet. Since prompts can be large (template + captured context), this forces an O(n) full scan of the prompt on every turn just for logging. You can keep the same behavior with a bounded scan by checking whether there is any character after HEAD_LEN + TAIL_LEN.
    let tail = if prompt_text.chars().count() > HEAD_LEN + TAIL_LEN {
        snippet(prompt_text, TAIL_LEN, false)
    } else {
        String::new()
    };

tools/wta/src/protocol/acp/prompt_context.rs:38

  • truncate_for_prompt always allocates truncated and then (when the input is already within budget) allocates again via text.to_string(). Returning truncated in the non-truncation case avoids an extra allocation/copy on every call.
    if chars.next().is_none() {
        text.to_string()
    } else {
        format!("{truncated}\n...<truncated>")
    }

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (2)

tools/wta/src/protocol/acp/prompt_context.rs:38

  • truncate_for_prompt always allocates truncated, and when the input is already within budget it allocates again via text.to_string(). This doubles the work on the common non-truncation path; you can return truncated directly (it already contains the full text when no extra char remains).
fn truncate_for_prompt(text: &str, max_chars: usize) -> String {
    let mut chars = text.chars();
    let truncated: String = chars.by_ref().take(max_chars).collect();
    if chars.next().is_none() {
        text.to_string()
    } else {
        format!("{truncated}\n...<truncated>")
    }

tools/wta/src/protocol/acp/prompt_builder.rs:243

  • log_turn_trace uses prompt_text.chars().count() to decide whether to include a tail snippet, which scans the entire (potentially large) prompt every turn. You can make this check bounded by looking for the (HEAD_LEN+TAIL_LEN+1)th character via skip(...).next() instead.
    let head = snippet(prompt_text, HEAD_LEN, true);
    let tail = if prompt_text.chars().count() > HEAD_LEN + TAIL_LEN {
        snippet(prompt_text, TAIL_LEN, false)
    } else {
        String::new()
    };

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.

2 participants