Skip to content
Open
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
15 changes: 11 additions & 4 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import type { Database } from "bun:sqlite";
import { getWorkflowById } from "./db";
import type { AdapterConfig, BundleArtifactRecord, Profile, ReplayGrade, TaskRecord, RuntimeConfig } from "./types";

const MAX_PAYLOAD_CHARS = 4000;
/**
* Default cap on characters used when summarizing the task payload for the
* prompt. Profiles can override via `context_policy.max_payload_chars` when
* they need to ship larger evidence payloads (e.g., curated PR diffs for
* review tasks). Kept conservative to preserve prior behavior for profiles
* that do not opt in.
*/
export const DEFAULT_MAX_PAYLOAD_CHARS = 4000;

function sanitizeRelativeArtifactPath(value: string): string {
return value
Expand Down Expand Up @@ -40,8 +47,8 @@ function truncateText(value: string, maxChars: number): string {
return `${value.slice(0, Math.max(0, maxChars - 16))}\n[truncated]\n`;
}

function summarizeJson(value: Record<string, unknown>): string {
return truncateText(JSON.stringify(value, null, 2), MAX_PAYLOAD_CHARS);
function summarizeJson(value: Record<string, unknown>, maxChars: number = DEFAULT_MAX_PAYLOAD_CHARS): string {
return truncateText(JSON.stringify(value, null, 2), maxChars);
}

function buildBundleTaskSnapshot(task: TaskRecord): Record<string, unknown> {
Expand Down Expand Up @@ -200,7 +207,7 @@ function buildGoalLoopContextLines(config: RuntimeConfig, task: TaskRecord): str
}

function buildPromptText(config: RuntimeConfig, profile: Profile, task: TaskRecord): string {
const payloadBlock = summarizeJson(task.payload);
const payloadBlock = summarizeJson(task.payload, profile.context_policy.max_payload_chars);
const phaseSkills = Array.isArray(task.payload.phase_skills)
? task.payload.phase_skills.filter((value): value is string => typeof value === "string" && value.trim().length > 0)
: [];
Expand Down
50 changes: 50 additions & 0 deletions src/runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,56 @@ test("goal loop context no longer inlines oversized artifact content", async ()
expect(context).not.toContain("tail");
});

test("payload truncates to default 4000 chars when context_policy does not override max_payload_chars", () => {
const config = testConfig();
const longString = "x".repeat(8000);
const profile = testProfile();
const context = assembleContext(config, profile, testTaskRecord({
payload: { evidence: longString }
}));

expect(context).toContain("[truncated]");
// Default cap is 4000; the payload block should not contain the full 8000-char value.
expect(context).not.toContain("x".repeat(8000));
});

test("payload respects context_policy.max_payload_chars override when profile sets it higher", () => {
const config = testConfig();
const longString = "y".repeat(8000);
const profile = testProfile({
context_policy: {
include_recent_task_memory: true,
max_prompt_chars: 32000,
max_payload_chars: 12000
}
});
const context = assembleContext(config, profile, testTaskRecord({
payload: { evidence: longString }
}));

// With a 12000-char payload cap, the full 8000-char value fits without truncation.
expect(context).toContain("y".repeat(8000));
expect(context).not.toContain("[truncated]");
});

test("payload respects context_policy.max_payload_chars override when profile sets it lower", () => {
const config = testConfig();
const longString = "z".repeat(2000);
const profile = testProfile({
context_policy: {
include_recent_task_memory: true,
max_prompt_chars: 16000,
max_payload_chars: 1000
}
});
const context = assembleContext(config, profile, testTaskRecord({
payload: { evidence: longString }
}));

expect(context).toContain("[truncated]");
expect(context).not.toContain("z".repeat(2000));
});

test("artifact-writing context requires declared managed artifact paths in artifact_paths", () => {
const config = testConfig();
const context = assembleContext(config, testProfile(), testTaskRecord({
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ export type Profile = {
context_policy: {
include_recent_task_memory: boolean;
max_prompt_chars: number;
/**
* Maximum characters used when summarizing the task payload for the prompt.
* Optional; defaults to the runtime-internal `DEFAULT_MAX_PAYLOAD_CHARS`
* (currently 4000) when unset. Profiles that need to ship larger evidence
* payloads (curated diffs, structured review excerpts) can raise this.
*/
max_payload_chars?: number;
};
result_schema: Record<string, boolean | string>;
integration_policies: Record<string, string>;
Expand Down