fix(outbound): strip internal runtime scaffolding

This commit is contained in:
Peter Steinberger
2026-04-28 20:34:35 +01:00
parent c5c08c074a
commit c2d31a5e59
6 changed files with 308 additions and 7 deletions

View File

@@ -72,6 +72,26 @@ describe("discordOutbound", () => {
});
});
it("sanitizes internal runtime scaffolding before Discord delivery", () => {
expect(
discordOutbound.sanitizeText?.({
text: "<previous_response>null</previous_response>visible",
payload: { text: "<previous_response>null</previous_response>visible" },
}),
).toBe("visible");
});
it("preserves Discord-native angle markup while stripping internal scaffolding", () => {
expect(
discordOutbound.sanitizeText?.({
text: "soon <t:1710000000:R> run </deploy:123> <previous_response>null</previous_response>",
payload: {
text: "soon <t:1710000000:R> run </deploy:123> <previous_response>null</previous_response>",
},
}),
).toBe("soon <t:1710000000:R> run </deploy:123> ");
});
it("forwards explicit formatting options to Discord text sends", async () => {
await discordOutbound.sendText?.({
cfg: {},

View File

@@ -26,6 +26,19 @@ import {
} from "./outbound-send-context.js";
export const DISCORD_TEXT_CHUNK_LIMIT = 2000;
const DISCORD_INTERNAL_RUNTIME_SCAFFOLDING_BLOCK_RE =
/<\s*(system-reminder|previous_response)\b[^>]*>[\s\S]*?<\s*\/\s*\1\s*>/gi;
const DISCORD_INTERNAL_RUNTIME_SCAFFOLDING_SELF_CLOSING_RE =
/<\s*(?:system-reminder|previous_response)\b[^>]*\/\s*>/gi;
const DISCORD_INTERNAL_RUNTIME_SCAFFOLDING_TAG_RE =
/<\s*\/?\s*(?:system-reminder|previous_response)\b[^>]*>/gi;
function stripDiscordInternalRuntimeScaffolding(text: string): string {
return text
.replace(DISCORD_INTERNAL_RUNTIME_SCAFFOLDING_BLOCK_RE, "")
.replace(DISCORD_INTERNAL_RUNTIME_SCAFFOLDING_SELF_CLOSING_RE, "")
.replace(DISCORD_INTERNAL_RUNTIME_SCAFFOLDING_TAG_RE, "");
}
type DiscordThreadBindingsModule = typeof import("./monitor/thread-bindings.js");
@@ -97,6 +110,7 @@ export const discordOutbound: ChannelOutboundAdapter = {
maxLines: ctx?.formatting?.maxLinesPerMessage,
}),
textChunkLimit: DISCORD_TEXT_CHUNK_LIMIT,
sanitizeText: ({ text }) => stripDiscordInternalRuntimeScaffolding(text),
pollMaxOptions: 10,
normalizePayload: ({ payload }) => normalizeDiscordApprovalPayload(payload),
presentationCapabilities: {