fix(session): restrict attachment discovery to declared event content

Attachment reads and ZIP export previously inferred ownership from common
field names on arbitrary events. Opaque ignorable plugin payloads could
therefore cause storage reads, while declared compaction content was missed.

Select attachment-bearing fields by current built-in event type in both
readers. Read direct image and file blocks, including flat V4 tool results
and developer messages, without interpreting extra block fields. Leave
unknown payloads and malformed inbox entries opaque. The archive preserves
the complete logical log even when its payload does not authorize a read.

V4 validates title requests as one text block, so do not treat them as an
attachment carrier. Compaction keeps its declared ContentBlock fields for
valid custom producers; the basic producer's text-only output is documented.
Historical wrappers remain the adjacent converters' responsibility rather
than a second compatibility path in attachment readers.

Adapt the original policy change to master after V4 landed, retaining its
author credit and scope. Cover tool-result export explicitly, keep Team
fixtures free of invented message roles, and verify that denied carriers
never read storage. The 889 owning tests pass with 100% statement, branch,
function, and line coverage for both changed runtime files.

Co-authored-by: Tianyi Cui <[email protected]>
This commit is contained in:
Tianyi Cui committed 2026-09-19 21:08:29 +08:00
1 parent b1dbe92356
commit 0c44e5461d
13 files changed
+338 -62

No files matched your search

+39 -18
View File
@@ -620,19 +620,16 @@ function imageBlockIn(
if (!Array.isArray(content)) return undefined
for (const value of content) {
if (typeof value !== 'object' || value === null || Array.isArray(value)) continue
const block = value as { readonly type?: unknown; readonly attachment?: unknown; readonly content?: unknown }
const block = value as { readonly type?: unknown; readonly attachment?: unknown }
if (block.type === 'image' && typeof block.attachment === 'object' && block.attachment !== null) {
const ref = block.attachment as ImageAttachmentRef
if (match(ref)) return ref
}
if (block.type === 'tool-result') {
const nested = imageBlockIn(block.content, match)
if (nested !== undefined) return nested
}
}
return undefined
}
/** Read only first-party declared content fields; unknown event payloads stay opaque. */
function imageInEvent(
event: SessionEvent,
match: (ref: ImageAttachmentRef) => boolean,
@@ -640,21 +637,45 @@ function imageInEvent(
const data = event.data as {
readonly content?: unknown
readonly message?: { readonly content?: unknown }
readonly inserted?: readonly { readonly content?: unknown }[]
readonly inserted?: unknown
readonly summary?: unknown
readonly rawOutput?: unknown
}
const direct = imageBlockIn(data.content, match)
if (direct !== undefined) return direct
const message = imageBlockIn(data.message?.content, match)
if (message !== undefined) return message
for (const inserted of data.inserted ?? []) {
const found = imageBlockIn(inserted.content, match)
if (found !== undefined) return found
}
if (event.type === 'assistant/message' || event.type === 'assistant/attempt') {
for (const chunk of assistantStreamChunks(event.data.stream, 'block-end')) {
const found = imageBlockIn([chunk.block], match)
if (found !== undefined) return found
// First-party event payloads can be present without their producer plugin mounted.
const type: string = event.type
switch (type) {
case 'user/message':
case 'tool/ptc-dispatch':
return imageBlockIn(data.content, match)
case 'system/message':
case 'developer/message':
case 'tool/result':
case 'team/message/queued':
return imageBlockIn(data.message?.content, match)
case 'agent/inbox/spliced': {
const messages = data.inserted
if (!Array.isArray(messages)) return undefined
for (const message of messages as readonly unknown[]) {
if (typeof message !== 'object' || message === null || Array.isArray(message)) continue
const found = imageBlockIn((message as { readonly content?: unknown }).content, match)
if (found !== undefined) return found
}
return undefined
}
case 'compaction/summary':
return imageBlockIn(data.summary, match) ?? imageBlockIn(data.rawOutput, match)
case 'assistant/message': {
const found = imageBlockIn(data.message?.content, match)
if (found !== undefined) return found
break
}
case 'assistant/attempt': break
default: return undefined
}
const assistant = event as SessionEvent<'assistant/message' | 'assistant/attempt'>
for (const chunk of assistantStreamChunks(assistant.data.stream, 'block-end')) {
const found = imageBlockIn([chunk.block], match)
if (found !== undefined) return found
}
return undefined
}