Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions scripts/pgso/corpus.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
{"name": "verify-tui-transcript-window", "argv": ["bun", "test", "--max-concurrency", "1", "./tui-transcript-window.test.ts"], "test_file": "tui-transcript-window.test.ts"},
{"name": "verify-tui-full-detail", "argv": ["bun", "test", "--max-concurrency", "1", "./tui-full-detail.test.ts"], "test_file": "tui-full-detail.test.ts"},
{"name": "verify-compaction-policy", "argv": ["bun", "test", "--max-concurrency", "1", "./compaction-policy.test.ts"], "test_file": "compaction-policy.test.ts", "requires_tmux": false},
{"name": "verify-provider-compaction", "argv": ["bun", "test", "--max-concurrency", "1", "./provider-compaction.test.ts"], "test_file": "provider-compaction.test.ts"},
{"name": "verify-tui-composer-edit-contracts", "argv": ["bun", "test", "--max-concurrency", "1", "./tui-composer-edit-contracts.test.ts"], "test_file": "tui-composer-edit-contracts.test.ts"},
{"name": "verify-tui-permissions-panel", "argv": ["bun", "test", "--max-concurrency", "1", "./tui-permissions-panel.test.ts"], "test_file": "tui-permissions-panel.test.ts"},
{"name": "verify-tui-effort-menu", "argv": ["bun", "test", "--max-concurrency", "1", "./tui-effort-menu.test.ts"], "test_file": "tui-effort-menu.test.ts"},
Expand Down
3 changes: 2 additions & 1 deletion scripts/pgso/tests/test_corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"tui-transcript-window.test.ts",
"tui-full-detail.test.ts",
"compaction-policy.test.ts",
"provider-compaction.test.ts",
"tui-composer-edit-contracts.test.ts",
"tui-permissions-panel.test.ts",
"tui-effort-menu.test.ts",
Expand Down Expand Up @@ -425,7 +426,7 @@ def test_production_manifest_classifies_every_e2e_file(self) -> None:
tuple(test_file for test_file, _ in corpus.intentional_exclusions),
)
self.assertEqual(37, len(corpus.scenarios))
self.assertEqual(74, len(corpus.candidate_scenarios))
self.assertEqual(75, len(corpus.candidate_scenarios))
self.assertEqual(
{
"direct-help": 100,
Expand Down
2 changes: 2 additions & 0 deletions src/builtins/gateway.zig
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ pub const agent_stream_provider = agent_stream_provider_contract.Provider{
.stream_fn = streamAgentCompletion,
.build_request_fn = buildAgentRequestForProvider,
.project_replay_fn = vercel_protocol.selectReplayParts,
.provider_compaction_fn = vercel_model_policy.providerCompaction,
.holds_compaction_block_fn = vercel_protocol.holdsCompactionBlock,
};

pub const provider_bundle = provider_set.Bundle{
Expand Down
5 changes: 5 additions & 0 deletions src/core/agent/runtime/agent.zig
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ pub const Agent = struct {
/// A request size limit learned from a provider rejection, kept for later
/// requests to the same model in this conversation. Plain value state.
request_byte_limit: ?RequestByteLimitState = null,
/// `/compact` asked for a compaction the provider writes: the next
/// request compacts first, as if it were due. Plain value state.
compaction_requested: bool = false,
/// The toolbox menu this conversation sent first. Later requests send the
/// same bytes until the conversation is cleared, compactions included,
/// whatever the skills and MCP servers do meanwhile
Expand Down Expand Up @@ -136,6 +139,8 @@ pub const Agent = struct {
self.dropHistory(alloc);
// A new conversation lists what the toolbox holds now.
self.toolbox_menu = .{};
if (self.compaction_requested) debug_trace.logf("agent", "dropping the requested compaction: the conversation was cleared", .{});
self.compaction_requested = false;
}

/// Frees every saved turn so a compaction can put its checkpoint in their
Expand Down
40 changes: 25 additions & 15 deletions src/core/agent/runtime/orchestrator.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3926,7 +3926,7 @@ fn build_provider_prompt_with_response_language_control(
origin: runtime_config.TurnOrigin,
enforce_response_language: bool,
correction_attempted: bool,
compaction_handoff: ?[]const u8,
compaction_handoff: ?ChatMessage,
compaction_history_tail: []const ChatMessage,
compacted_suffix_len: usize,
) !ProviderPromptProjection {
Expand Down Expand Up @@ -3981,7 +3981,7 @@ test "compacted request keeps the pending user prompt after the handoff" {
.subagent,
false,
false,
"handoff",
.{ .role = .user, .content = "handoff", .context_origin = .handoff },
&.{.{ .role = .assistant, .content = "retained tail" }},
1,
);
Expand Down Expand Up @@ -7323,7 +7323,7 @@ fn buildProviderPromptForCompactionWindow(
durable_history: []const ChatMessage,
current_user_message: ChatMessage,
within_turn_suffix: []const ChatMessage,
handoff: ?[]const u8,
handoff: ?ChatMessage,
retained_history_tail: []const ChatMessage,
compacted_suffix_len: usize,
) !runtime_prompt_context.ProviderPrompt {
Expand All @@ -7337,10 +7337,7 @@ fn buildProviderPromptForCompactionWindow(
);
var compacted_history: std.ArrayList(ChatMessage) = .empty;
defer compacted_history.deinit(alloc);
try compacted_history.append(alloc, .{
.role = .user,
.content = handoff.?,
});
try compacted_history.append(alloc, handoff.?);
try compacted_history.appendSlice(alloc, retained_history_tail);
return runtime_prompt_context.buildProviderPrompt(
alloc,
Expand All @@ -7352,6 +7349,14 @@ fn buildProviderPromptForCompactionWindow(
);
}

/// The runtime context's texts, which a provider's compaction block keeps
/// (`compactor.Request.runtime_context`).
fn runtimeContextTexts(arena: Allocator, messages: []const ChatMessage) Allocator.Error![]const []const u8 {
const texts = try arena.alloc([]const u8, messages.len);
for (messages, texts) |message, *text| text.* = message.content orelse "";
return texts;
}

fn commitContextCompaction(
deps: *const AgentRuntimeDeps,
summary: types.CompactedSummaryHistoryTurn,
Expand Down Expand Up @@ -7984,7 +7989,7 @@ fn processQueuedPromptLoop(
var identical_failure_escalation: runtime_tool_admission.IdenticalFailureEscalationState = .{};
defer identical_failure_escalation.deinit(arena);
var malformed_arguments_retry: runtime_tool_admission.MalformedArgumentsRetryState = .{};
var active_compaction_handoff: ?[]const u8 = null;
var active_compaction_handoff: ?ChatMessage = null;
var active_compaction_history_tail: []const ChatMessage = &.{};
// The provider's count of the first request after a compaction is traced,
// so its size can be checked against the room compaction had.
Expand Down Expand Up @@ -8800,7 +8805,7 @@ fn processQueuedPromptLoop(
if (just_rebuilt_request) compactor.traceLog(false, "request after compaction estimated_tokens={d} fixed_tokens={any} usable_tokens={any} after_tokens={d}", .{ request_cost.estimated_input_tokens, size.fixed_tokens, size.usable_tokens, size.afterTokens() });
const usable_tokens = size.usable_tokens;
const compact_at_tokens = size.compact_at_tokens;
const wants_compaction = has_new_compactable_context and (overflow_pending or size.due());
const wants_compaction = has_new_compactable_context and (overflow_pending or size.due() or agent.compaction_requested);
compactor.traceEventIf(
wants_compaction,
step_ctx,
Expand Down Expand Up @@ -8831,6 +8836,7 @@ fn processQueuedPromptLoop(
return error.ContextCapacityExceeded;
};
} else {
agent.compaction_requested = false;
// The pending user is source too, even before the first tool.
const active_prefix: types.AssistantHistoryTurn = .{
.user = .{ .text = job.prompt, .images = job.images, .work_id = job.work_id, .author = job.author },
Expand Down Expand Up @@ -8890,6 +8896,7 @@ fn processQueuedPromptLoop(
.records = if (config.session_child_capability) |capability| result_store.compactorStore(capability) else null,
.cancel_flag = config.cancel_flag,
.trace_ctx = step_ctx,
.runtime_context = try runtimeContextTexts(arena, runtime_context.messages()),
},
});
// The turn's duration leaves out the compaction, as its
Expand Down Expand Up @@ -8937,15 +8944,18 @@ fn processQueuedPromptLoop(
} else .{};
compacted_suffix_len += try runtime_execution_memory.retainedMessageOffset(within_turn_suffix.items[compacted_suffix_len..], active_cut);
explicit_note_index = try keepExplicitSkillNote(arena, &within_turn_suffix, skills.explicit, explicit_note_index, compacted_suffix_len);
var retained_messages: std.ArrayList(ChatMessage) = .empty;
try session_runtime.appendHistoryChatMessages(arena, &retained_messages, outcome.retained_history);
try projectEmptyHistoryReplay(arena, deps.agent_stream_provider, .{ .provider = job.provider, .model = gateway_model }, retained_messages.items);
active_compaction_handoff = outcome.model_text;
trace_count_after_compaction = true;
active_compaction_history_tail = retained_messages.items;
const next_history = try arena.alloc(HistoryTurn, outcome.retained_history.len + 1);
next_history[0] = .{ .compacted_summary = outcome.checkpoint };
@memcpy(next_history[1..], outcome.retained_history);
// The rest of the turn sends the checkpoint as every
// later turn will: a provider's block to the model that
// wrote it, fx's text otherwise, then the kept turns.
var compacted_messages: std.ArrayList(ChatMessage) = .empty;
try session_runtime.appendHistoryChatMessages(arena, &compacted_messages, next_history);
try projectEmptyHistoryReplay(arena, deps.agent_stream_provider, .{ .provider = job.provider, .model = gateway_model }, compacted_messages.items);
active_compaction_handoff = compacted_messages.items[0];
trace_count_after_compaction = true;
active_compaction_history_tail = compacted_messages.items[1..];
compaction_history = next_history;
finalization.compacted_execution = .{
.tool_steps = finalization.compacted_execution.tool_steps + active_cut.tool_steps,
Expand Down
10 changes: 9 additions & 1 deletion src/core/agent/runtime/runtime_context.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

const std = @import("std");
const types = @import("../../shared/types.zig");
const compactor = @import("../../compactor/compactor.zig");
const model_context_encoding = @import("../../shared/model_context_encoding.zig");

const Allocator = std.mem.Allocator;
Expand Down Expand Up @@ -58,7 +59,14 @@ pub const State = struct {
while (index > 0) {
index -= 1;
const execution = switch (history[index]) {
.compacted_summary => break,
// A provider's compaction block keeps the copy the turns after
// it were sent with, so nothing before them changes; fx's own
// summary starts a new one.
.compacted_summary => |entry| {
const block = (try compactor.providerBlock(arena, entry.summary)) orelse break;
if (block.runtime_context.len == 0) break;
return .{ .frozen = try messagesFrom(arena, block.runtime_context), .known_update = known_update };
},
.assistant => |entry| entry.execution,
.interrupted => |entry| entry.execution,
};
Expand Down
Loading
Loading