Skip to content

Commit 53f771c

Browse files
committed
refactor(agents): consolidate context budgets and compaction recovery
1 parent a1b41d5 commit 53f771c

15 files changed

Lines changed: 1049 additions & 2072 deletions

‎src/agents/agent-hooks/compaction-safeguard-quality.ts‎

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,16 @@ function resolveExactIdentifierSectionInstruction(
3939
if (policy === "off") {
4040
return POLICY_OFF_EXACT_IDENTIFIERS_INSTRUCTION;
4141
}
42-
if (policy === "custom") {
43-
const custom = summarizationInstructions?.identifierInstructions?.trim();
44-
if (custom) {
45-
// Operator text is runtime data, not prompt authority. Wrap it as
46-
// untrusted data before inserting it into compaction instructions.
47-
const customBlock = wrapUntrustedInstructionBlock(
42+
const custom =
43+
policy === "custom" ? summarizationInstructions?.identifierInstructions?.trim() : undefined;
44+
if (custom) {
45+
// Operator text is runtime data, never prompt authority.
46+
return (
47+
wrapUntrustedInstructionBlock(
4848
"For ## Exact identifiers, apply this operator-defined policy text",
4949
custom,
50-
);
51-
if (customBlock) {
52-
return customBlock;
53-
}
54-
}
50+
) || STRICT_EXACT_IDENTIFIERS_INSTRUCTION
51+
);
5552
}
5653
return STRICT_EXACT_IDENTIFIERS_INSTRUCTION;
5754
}
@@ -71,14 +68,9 @@ export function buildCompactionStructureInstructions(
7168
"When prior compaction summaries are present, re-distill them with new messages and remove stale duplicate detail.",
7269
].join("\n");
7370
const custom = customInstructions?.trim();
74-
if (!custom) {
75-
return sectionsTemplate;
76-
}
77-
const customBlock = wrapUntrustedInstructionBlock("Additional context from /compact", custom);
78-
if (!customBlock) {
79-
return sectionsTemplate;
80-
}
81-
return `${sectionsTemplate}\n\n${customBlock}`;
71+
const customBlock =
72+
custom && wrapUntrustedInstructionBlock("Additional context from /compact", custom);
73+
return customBlock ? `${sectionsTemplate}\n\n${customBlock}` : sectionsTemplate;
8274
}
8375

8476
function normalizedSummaryLines(summary: string): string[] {
@@ -110,26 +102,18 @@ export function buildStructuredFallbackSummary(
110102
if (trimmedPreviousSummary && hasRequiredSummarySections(trimmedPreviousSummary)) {
111103
return trimmedPreviousSummary;
112104
}
113-
const exactIdentifiersSummary = "None captured.";
114-
return [
115-
"## Decisions",
105+
const values = [
116106
trimmedPreviousSummary || "No prior history.",
117-
"",
118-
"## Open TODOs",
119107
"None.",
120-
"",
121-
"## Constraints/Rules",
122108
"None.",
123-
"",
124-
"## Pending user asks",
125109
"None.",
126-
"",
127-
"## Exact identifiers",
128-
exactIdentifiersSummary,
129-
].join("\n");
110+
"None captured.",
111+
];
112+
return REQUIRED_SUMMARY_SECTIONS.map((heading, index) => `${heading}\n${values[index]}`).join(
113+
"\n\n",
114+
);
130115
}
131116

132-
/** Append an already-formatted summary section without disturbing empty summaries. */
133117
/** Appends a bounded post-compaction section to an existing summary. */
134118
export function appendSummarySection(summary: string, section: string): string {
135119
if (!section) {
@@ -172,8 +156,7 @@ export function extractOpaqueIdentifiers(text: string): string[] {
172156
return Array.from(
173157
new Set(
174158
matches
175-
.map((value) => sanitizeExtractedIdentifier(value))
176-
.map((value) => normalizeOpaqueIdentifier(value))
159+
.map((value) => normalizeOpaqueIdentifier(sanitizeExtractedIdentifier(value)))
177160
.filter((value) => value.length >= 4),
178161
),
179162
).slice(0, MAX_EXTRACTED_IDENTIFIERS);
@@ -205,31 +188,16 @@ function hasAskOverlap(summary: string, latestAsk: string | null): boolean {
205188
if (askTokens.length === 0) {
206189
return true;
207190
}
208-
const meaningfulAskTokens = askTokens.filter((token) => {
209-
if (token.length <= 1) {
210-
return false;
211-
}
212-
if (isQueryStopWordToken(token)) {
213-
return false;
214-
}
215-
return true;
216-
});
191+
const meaningfulAskTokens = askTokens.filter(
192+
(token) => token.length > 1 && !isQueryStopWordToken(token),
193+
);
217194
const tokensToCheck = meaningfulAskTokens.length > 0 ? meaningfulAskTokens : askTokens;
218-
if (tokensToCheck.length === 0) {
219-
return true;
220-
}
221195
const summaryTokens = new Set(tokenizeAskOverlapText(summary));
222-
let overlapCount = 0;
223-
for (const token of tokensToCheck) {
224-
if (summaryTokens.has(token)) {
225-
overlapCount += 1;
226-
}
227-
}
196+
const overlapCount = tokensToCheck.filter((token) => summaryTokens.has(token)).length;
228197
const requiredMatches = tokensToCheck.length >= MIN_ASK_OVERLAP_TOKENS_FOR_DOUBLE_MATCH ? 2 : 1;
229198
return overlapCount >= requiredMatches;
230199
}
231200

232-
/** Audit summary structure, exact identifier preservation, and latest-ask coverage. */
233201
/** Audits a candidate summary for required sections, pending asks, and identifier preservation. */
234202
export function auditSummaryQuality(params: {
235203
summary: string;

0 commit comments

Comments
 (0)