Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ Your formatted summary here (supports **markdown** and [links](url))
<!--/CONDUIT:PUBLISH-->
```

Parsing accepts a few common agent mistakes (HTML-escaped comments, `<!--CONDUIT:END-->`, 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
Expand Down
102 changes: 102 additions & 0 deletions src/server/publisher.test.ts
Original file line number Diff line number Diff line change
@@ -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.',
'<!--CONDUIT:PUBLISH-->',
SUMMARY,
'<!--/CONDUIT:PUBLISH-->',
'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.',
'<!--CONDUIT:PUBLISH-->',
SUMMARY,
'<!--CONDUIT:PUBLISH-->',
].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.',
'<!--CONDUIT:PUBLISH-->',
'### Sentrypede — loop 30 (2026-08-02)',
'*Scan:* 0 new issues.',
'<!--CONDUIT:END-->',
'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',
'&lt;!--CONDUIT:PUBLISH--&gt;',
'escaped summary',
'&lt;!--/CONDUIT:PUBLISH--&gt;',
].join('\n')
expect(resolveRunPublishMessage(escaped)).toBe('escaped summary')

const slashAfter = '<!--CONDUIT:PUBLISH-->\nslash close\n<!--CONDUIT:/PUBLISH-->'
expect(resolveRunPublishMessage(slashAfter)).toBe('slash close')
})

it('is tolerant of whitespace and case in the markers', () => {
const stdout = '<!-- conduit:publish -->\nspaced\n<!-- /CONDUIT:publish -->'
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.',
'<!--CONDUIT:PUBLISH-->',
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:',
'<!--CONDUIT:PUBLISH-->',
'Your formatted summary here',
'<!--/CONDUIT:PUBLISH-->',
'Done. Real summary follows.',
'<!--CONDUIT:PUBLISH-->',
'actual summary',
'<!--/CONDUIT:PUBLISH-->',
].join('\n')
expect(resolveRunPublishMessage(stdout)).toBe('actual summary')
})

it('skips publishing when an opener is present but the block is empty', () => {
expect(resolveRunPublishMessage('<!--CONDUIT:PUBLISH-->\n<!--/CONDUIT:PUBLISH-->')).toBeNull()
expect(resolveRunPublishMessage('<!--CONDUIT:PUBLISH-->\n<!--CONDUIT:PUBLISH-->')).toBeNull()
expect(resolveRunPublishMessage('')).toBeNull()
})
})
72 changes: 60 additions & 12 deletions src/server/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
* <!--CONDUIT:PUBLISH-->
* ...content...
* <!--/CONDUIT:PUBLISH-->
* then only that content is posted. Otherwise the full stdout output is sent.
*
* Parsing is intentionally forgiving: HTML-escaped comments, extra
* whitespace, `<!--CONDUIT:END-->`, 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 =
/(?:<!--|&lt;!--)\s*(\/?)\s*CONDUIT\s*:\s*(\/?)\s*(PUBLISH|END)\s*(?:-->|--&gt;)/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 = '<!--CONDUIT:PUBLISH-->'
const PUBLISH_END = '<!--/CONDUIT:PUBLISH-->'
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 ────────────────────────────────────────────────────────────────────
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<!--CONDUIT:PUBLISH-->` 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.
*/
Expand Down
Loading