UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

206 lines • 9.61 kB
function isRecord(value) { return typeof value === "object" && value !== null && !Array.isArray(value); } function isFiniteNumber(value) { return typeof value === "number" && Number.isFinite(value); } function hasTextOnlyContent(content, allowString) { if (typeof content === "string") return allowString; if (!Array.isArray(content)) return false; return content.every((block) => isRecord(block) && block.type === "text" && typeof block.text === "string"); } function isWellFormedImageBlock(block) { return (block.type === "image" && typeof block.mimeType === "string" && block.mimeType.startsWith("image/") && typeof block.data === "string" && block.data.length > 0); } function hasSafeToolResultContent(content) { if (!Array.isArray(content)) return false; return content.every((block) => isRecord(block) && ((block.type === "text" && typeof block.text === "string") || isWellFormedImageBlock(block))); } function isUsage(value) { if (!isRecord(value) || !isRecord(value.cost)) return false; for (const field of ["input", "output", "cacheRead", "cacheWrite", "totalTokens"]) { if (!isFiniteNumber(value[field])) return false; } for (const field of ["input", "output", "cacheRead", "cacheWrite", "total"]) { if (!isFiniteNumber(value.cost[field])) return false; } return ((value.cacheWrite1h === undefined || isFiniteNumber(value.cacheWrite1h)) && (value.reasoning === undefined || isFiniteNumber(value.reasoning))); } const base64SignaturePattern = /^[A-Za-z0-9+/]+={0,2}$/; /** Validate an opaque provider signature without exposing or logging content. */ function isValidOpaqueSignature(sig) { return typeof sig === "string" && sig.length > 0 && sig.length <= 65_536; } /** Gemini signatures are base64; other providers use bounded opaque strings. */ function isValidProviderSignature(sig, providerIsGoogle) { if (!isValidOpaqueSignature(sig)) return false; return !providerIsGoogle || (sig.length % 4 === 0 && base64SignaturePattern.test(sig)); } function hasSafeAssistantContent(content, providerIsGoogle) { if (!Array.isArray(content)) return false; for (const block of content) { if (!isRecord(block) || typeof block.type !== "string") return false; switch (block.type) { case "text": if (typeof block.text !== "string") return false; if (block.textSignature !== undefined && !isValidProviderSignature(block.textSignature, providerIsGoogle)) { return false; } break; case "thinking": if (typeof block.thinking !== "string" || (block.startedAt !== undefined && !isFiniteNumber(block.startedAt)) || (block.endedAt !== undefined && !isFiniteNumber(block.endedAt)) || (block.redacted !== undefined && typeof block.redacted !== "boolean")) { return false; } // Redacted thinking requires a bounded opaque signature for replay. if ((block.thinkingSignature !== undefined && !isValidProviderSignature(block.thinkingSignature, providerIsGoogle)) || (block.redacted === true && !isValidOpaqueSignature(block.thinkingSignature))) { return false; } break; case "toolCall": if (typeof block.id !== "string" || typeof block.name !== "string" || !isRecord(block.arguments) || (block.incomplete !== undefined && block.incomplete !== true) || (block.errorMessage !== undefined && typeof block.errorMessage !== "string")) { return false; } if (block.thoughtSignature !== undefined && !isValidProviderSignature(block.thoughtSignature, providerIsGoogle)) { return false; } break; default: return false; } } return true; } function hasSafeAssistantEnvelope(message) { const provider = typeof message.provider === "string" ? message.provider : ""; const providerIsGoogle = provider === "google" || provider === "google-vertex"; const stopReason = message.stopReason; const stopDetails = message.stopDetails; const safeStopDetails = stopDetails === undefined || (isRecord(stopDetails) && (stopDetails.type === "sensitive" || (stopDetails.type === "refusal" && (stopDetails.explanation === undefined || typeof stopDetails.explanation === "string")))); return (typeof message.api === "string" && typeof message.provider === "string" && typeof message.model === "string" && isUsage(message.usage) && (stopReason === "pending" || stopReason === "stop" || stopReason === "length" || stopReason === "toolUse" || stopReason === "error" || stopReason === "aborted") && safeStopDetails && isFiniteNumber(message.timestamp) && (message.responseModel === undefined || typeof message.responseModel === "string") && (message.responseId === undefined || typeof message.responseId === "string") && (message.diagnostics === undefined || Array.isArray(message.diagnostics)) && (message.errorMessage === undefined || typeof message.errorMessage === "string") && (message.rawStopReason === undefined || typeof message.rawStopReason === "string") && hasSafeAssistantContent(message.content, providerIsGoogle)); } function hasSafeToolResultEnvelope(message) { return (typeof message.toolCallId === "string" && message.toolCallId.length > 0 && typeof message.toolName === "string" && message.toolName.length > 0 && hasSafeToolResultContent(message.content) && typeof message.isError === "boolean" && isFiniteNumber(message.timestamp) && (message.usage === undefined || isUsage(message.usage)) && (message.addedToolNames === undefined || (Array.isArray(message.addedToolNames) && message.addedToolNames.every((name) => typeof name === "string")))); } /** Reasoning-effort values a `configuration_update` entry can carry: the producer (`appendConfigurationUpdate`) writes a ThinkingLevel. */ const CONFIGURATION_UPDATE_EFFORTS = new Set(["off", "minimal", "low", "medium", "high", "xhigh", "max"]); function hasSafeConfigurationUpdate(message) { return (typeof message.effort === "string" && CONFIGURATION_UPDATE_EFFORTS.has(message.effort) && Array.isArray(message.content) && message.content.length === 0 && isFiniteNumber(message.timestamp)); } /** Reject any retained message that cannot be safely replayed through normalized provider conversion. */ export function hasUnsafeRetainedContent(messages) { for (const value of messages) { if (!isRecord(value) || typeof value.role !== "string") return true; switch (value.role) { case "user": if (!isFiniteNumber(value.timestamp) || !hasTextOnlyContent(value.content, true)) return true; break; case "assistant": if (!hasSafeAssistantEnvelope(value)) return true; break; case "toolResult": if (!hasSafeToolResultEnvelope(value)) return true; break; case "configurationUpdate": // A reasoning-effort change is a valid runtime message: normal request // conversion accepts it, so a bounded one is replay-safe (senpi#8566). if (!hasSafeConfigurationUpdate(value)) return true; break; case "custom": if (typeof value.customType !== "string" || typeof value.display !== "boolean" || !isFiniteNumber(value.timestamp) || !hasTextOnlyContent(value.content, true)) { return true; } break; case "bashExecution": if (typeof value.command !== "string" || typeof value.output !== "string" || (value.exitCode !== undefined && !isFiniteNumber(value.exitCode)) || typeof value.cancelled !== "boolean" || typeof value.truncated !== "boolean" || (value.fullOutputPath !== undefined && typeof value.fullOutputPath !== "string") || (value.excludeFromContext !== undefined && typeof value.excludeFromContext !== "boolean") || !isFiniteNumber(value.timestamp)) { return true; } break; case "compactionSummary": if (typeof value.summary !== "string" || !isFiniteNumber(value.tokensBefore) || !isFiniteNumber(value.timestamp)) { return true; } break; default: return true; } } return false; } //# sourceMappingURL=retained-message-safety.js.map