From b805e121459b841c98df0cccebf2b69015fdd0be Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 30 Aug 2026 22:33:16 +0000 Subject: [PATCH] fix(publisher): extract publish blocks even when agents mistype the closer Agents were leaking full run narration to Slack because a missing or wrong closer (repeated , ) made extraction fail and fall back to the entire stdout. Once an opener is present, only the block is posted; empty/unrecoverable blocks skip publish. Co-authored-by: Doug Rathbone --- README.md | 2 + src/server/publisher.test.ts | 102 +++++++++++++++++++++++++++++++++++ src/server/publisher.ts | 72 ++++++++++++++++++++----- src/server/utils.ts | 2 +- 4 files changed, 165 insertions(+), 13 deletions(-) create mode 100644 src/server/publisher.test.ts diff --git a/README.md b/README.md index 091ec5d..22e6f0f 100644 --- a/README.md +++ b/README.md @@ -205,6 +205,8 @@ Your formatted summary here (supports **markdown** and [links](url)) ``` +Parsing accepts a few common agent mistakes (HTML-escaped comments, ``, a repeated opening tag as the closer, or a missing closer). Once an opening marker appears, surrounding run narration is never used as a fallback. + For Slack, markdown is converted to mrkdwn; for email, to HTML. Webhooks receive `{ content, agent, runId, timestamp }`. ### GitHub App authentication diff --git a/src/server/publisher.test.ts b/src/server/publisher.test.ts new file mode 100644 index 0000000..359e30a --- /dev/null +++ b/src/server/publisher.test.ts @@ -0,0 +1,102 @@ +import { describe, it, expect } from 'vitest' +import { resolveRunPublishMessage } from './publisher' + +const SUMMARY = [ + '## Run Summary — AIA-607: Hoist route prefetch above subdomain gate', + '', + 'Moved RoutePrefetchBootstrap above the subdomain gate.', +].join('\n') + +describe('resolveRunPublishMessage', () => { + it('returns the full output when no publish markers are present', () => { + const stdout = ':chart_with_upwards_trend: *FE-Perf loop — advanced AIA-389*\nJob: advance-PR' + expect(resolveRunPublishMessage(stdout)).toBe(stdout) + }) + + it('extracts the canonical publish block and drops surrounding narration', () => { + const stdout = [ + 'Starting the FE-perf loop.', + 'Draft PR created.', + '', + SUMMARY, + '', + 'Iteration complete.', + ].join('\n') + expect(resolveRunPublishMessage(stdout)).toBe(SUMMARY) + }) + + it('treats a repeated opening tag as a closer (FE-perf loop leak)', () => { + const stdout = [ + 'Starting the FE-perf loop. I will validate tooling.', + 'Yarn install succeeded.', + '', + SUMMARY, + '', + ].join('\n') + expect(resolveRunPublishMessage(stdout)).toBe(SUMMARY) + }) + + it('accepts CONDUIT:END as a closer (Sentrypede loop-30 leak)', () => { + const stdout = [ + 'Starting a Sentry Patrol iteration.', + 'Now emitting the Phase 6 Slack summary.', + '', + '### Sentrypede — loop 30 (2026-08-02)', + '*Scan:* 0 new issues.', + '', + 'Iteration complete. Summary: no new Sentry issues warranted action.', + ].join('\n') + expect(resolveRunPublishMessage(stdout)).toBe( + '### Sentrypede — loop 30 (2026-08-02)\n*Scan:* 0 new issues.' + ) + }) + + it('accepts slash-after-colon and HTML-escaped comment forms', () => { + const escaped = [ + 'narration', + '<!--CONDUIT:PUBLISH-->', + 'escaped summary', + '<!--/CONDUIT:PUBLISH-->', + ].join('\n') + expect(resolveRunPublishMessage(escaped)).toBe('escaped summary') + + const slashAfter = '\nslash close\n' + expect(resolveRunPublishMessage(slashAfter)).toBe('slash close') + }) + + it('is tolerant of whitespace and case in the markers', () => { + const stdout = '\nspaced\n' + expect(resolveRunPublishMessage(stdout)).toBe('spaced') + }) + + it('uses content after an unclosed opener instead of the full transcript', () => { + const stdout = [ + 'Starting the FE-perf loop.', + 'Let me babysit CI.', + '', + SUMMARY, + ].join('\n') + expect(resolveRunPublishMessage(stdout)).toBe(SUMMARY) + expect(resolveRunPublishMessage(stdout)).not.toContain('Starting the FE-perf loop') + }) + + it('prefers the last non-empty open block when the prompt is quoted earlier', () => { + const stdout = [ + 'To publish, wrap the summary in:', + '', + 'Your formatted summary here', + '', + 'Done. Real summary follows.', + '', + 'actual summary', + '', + ].join('\n') + expect(resolveRunPublishMessage(stdout)).toBe('actual summary') + }) + + it('skips publishing when an opener is present but the block is empty', () => { + expect(resolveRunPublishMessage('\n')).toBeNull() + expect(resolveRunPublishMessage('\n')).toBeNull() + expect(resolveRunPublishMessage('')).toBeNull() + }) +}) diff --git a/src/server/publisher.ts b/src/server/publisher.ts index 40507e5..9ef1340 100644 --- a/src/server/publisher.ts +++ b/src/server/publisher.ts @@ -7,23 +7,69 @@ import { readRunOutputText } from './utils' /** * Publish target is a dumb delivery channel — the agent controls the content. * - * Convention: if the agent's stdout contains a block delimited by + * Convention: if the agent's stdout contains a publish block, only that + * content is posted. Canonical delimiters: * * ...content... * - * then only that content is posted. Otherwise the full stdout output is sent. + * + * Parsing is intentionally forgiving: HTML-escaped comments, extra + * whitespace, ``, and a repeated opening tag as a closer + * all count. Once an opening marker is present the surrounding narration is + * never used as a fallback — a malformed block skips publishing rather than + * dumping the full run. + * + * Agents that never emit a publish marker still deliver their full stdout. + */ + +const PUBLISH_MARKER_RE = + /(?:|-->)/gi + +interface PublishMarker { + index: number + end: number + kind: 'open' | 'close' +} + +function findPublishMarkers(text: string): PublishMarker[] { + const markers: PublishMarker[] = [] + const re = new RegExp(PUBLISH_MARKER_RE.source, 'gi') + let match: RegExpExecArray | null + while ((match = re.exec(text)) !== null) { + const slashBefore = match[1] === '/' + const slashAfter = match[2] === '/' + const name = match[3].toUpperCase() + const kind: PublishMarker['kind'] = + name === 'END' || slashBefore || slashAfter ? 'close' : 'open' + markers.push({ index: match.index, end: match.index + match[0].length, kind }) + if (match[0].length === 0) re.lastIndex += 1 + } + return markers +} + +/** + * Text to deliver to publish targets, or `null` to skip delivery. + * + * Exported for unit tests. */ +export function resolveRunPublishMessage(stdout: string): string | null { + if (!stdout) return null -const PUBLISH_START = '' -const PUBLISH_END = '' + const markers = findPublishMarkers(stdout) + if (markers.length === 0) return stdout -function extractPublishContent(stdout: string): string | null { - const startIdx = stdout.indexOf(PUBLISH_START) - if (startIdx === -1) return null - const contentStart = startIdx + PUBLISH_START.length - const endIdx = stdout.indexOf(PUBLISH_END, contentStart) - if (endIdx === -1) return null - return stdout.slice(contentStart, endIdx).trim() + const opens = markers.filter((m) => m.kind === 'open') + if (opens.length === 0) return stdout + + for (let i = opens.length - 1; i >= 0; i--) { + const open = opens[i] + const next = markers.find((m) => m.index >= open.end) + const raw = next ? stdout.slice(open.end, next.index) : stdout.slice(open.end) + const content = raw.trim() + if (content) return content + } + + return null } // ── Slack ──────────────────────────────────────────────────────────────────── @@ -299,7 +345,9 @@ export async function publishRunResult( if (!fullStdout) return - const publishContent = extractPublishContent(fullStdout) ?? fullStdout + const publishContent = resolveRunPublishMessage(fullStdout) + if (!publishContent) return + const message = publishContent.length > 39000 ? publishContent.slice(0, 39000) + '\n…(truncated)' : publishContent diff --git a/src/server/utils.ts b/src/server/utils.ts index 53e689c..b530513 100644 --- a/src/server/utils.ts +++ b/src/server/utils.ts @@ -60,7 +60,7 @@ function stripAnsi(text: string): string { /** * The run's textual output for publish-target delivery. For structured logs this - * is the assistant narration (which carries any `` markers), + * is the assistant narration (which carries any publish-block markers), * falling back to raw stdout/system text; for old terminal logs it's the * ANSI-stripped stdout — matching the pre-structured behaviour. */