feat(context): implement observation masking for tool outputs (#18389)

This commit is contained in:
Abhi
2026-02-05 20:53:11 -05:00
committed by GitHub
parent 289769f544
commit 8ec176e005
15 changed files with 1151 additions and 7 deletions
+10 -5
View File
@@ -572,6 +572,14 @@ export async function fileExists(filePath: string): Promise<boolean> {
const MAX_TRUNCATED_LINE_WIDTH = 1000;
const MAX_TRUNCATED_CHARS = 4000;
/**
* Sanitizes a string for use as a filename part by removing path traversal
* characters and other non-alphanumeric characters.
*/
export function sanitizeFilenamePart(part: string): string {
return part.replace(/[^a-zA-Z0-9_-]/g, '_');
}
/**
* Formats a truncated message for tool output, handling multi-line and single-line (elephant) cases.
*/
@@ -623,11 +631,8 @@ export async function saveTruncatedToolOutput(
id: string | number, // Accept string (callId) or number (truncationId)
projectTempDir: string,
): Promise<{ outputFile: string; totalLines: number }> {
const safeToolName = toolName.replace(/[^a-z0-9]/gi, '_').toLowerCase();
const safeId = id
.toString()
.replace(/[^a-z0-9]/gi, '_')
.toLowerCase();
const safeToolName = sanitizeFilenamePart(toolName).toLowerCase();
const safeId = sanitizeFilenamePart(id.toString()).toLowerCase();
const fileName = `${safeToolName}_${safeId}.txt`;
const toolOutputDir = path.join(projectTempDir, TOOL_OUTPUT_DIR);
const outputFile = path.join(toolOutputDir, fileName);