Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
fab320e
fix(native-chat): preserve literal image markers
brennanb2025 Aug 14, 2026
7803de9
fix(native-chat): key optimistic echoes on literal image markers
brennanb2025 Aug 14, 2026
94f1cc6
fix(native-chat): require complete image echo evidence
brennanb2025 Aug 15, 2026
c51e8e6
fix(mobile): isolate image echo occurrences by count
brennanb2025 Aug 15, 2026
794e07c
fix(mobile): retire launch drafts on literal marker turns
brennanb2025 Aug 15, 2026
9b9aa6f
fix(mobile): use array shorthand in draft reconciliation
brennanb2025 Aug 15, 2026
6b736e8
Merge remote-tracking branch 'origin/main' into brennanb2025/sta-4363…
brennanb2025 Aug 15, 2026
d49d6ad
fix(native-chat): isolate literal marker match keys
brennanb2025 Aug 15, 2026
bf74570
fix(native-chat): separate literal and image echo identity
brennanb2025 Aug 15, 2026
48f175a
fix(native-chat): fold prompt-first image transcripts
brennanb2025 Aug 15, 2026
b5ba22e
fix(native-chat): retain Claude image source metadata
brennanb2025 Aug 15, 2026
bdf1bd2
Merge remote-tracking branch 'origin/main' into brennanb2025/sta-4363…
brennanb2025 Aug 15, 2026
8246492
fix(ci): satisfy PR LoC type-aware audit
brennanb2025 Aug 15, 2026
e9b692f
fix(native-chat): decode multi-image Claude metadata
brennanb2025 Aug 15, 2026
f74297c
fix(native-chat): preserve image preview ownership across version skew
brennanb2025 Aug 15, 2026
bc1beba
fix(native-chat): negotiate image source wire projection
brennanb2025 Aug 15, 2026
604748f
fix(native-chat): keep legacy image turns atomic
brennanb2025 Aug 15, 2026
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: 1 addition & 1 deletion config/scripts/pr-test-loc-summary.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('PR test LoC summary', () => {
fetchImpl: async (url) => {
const page = pages[url]
if (page == null) {
throw new Error(`unexpected url ${url}`)
throw new Error(`unexpected url ${String(url)}`)
}
return {
ok: true,
Expand Down
119 changes: 119 additions & 0 deletions mobile/src/session/mobile-native-chat-draft-reconcile.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest'
import type { NativeChatMessage } from '../../../src/shared/native-chat-types'
import {
countUserTextOccurrences,
findLandedImagePreviewEchoes,
findLandedUnconfirmedSends,
migrateImagePreviewMessageIds,
Expand Down Expand Up @@ -37,6 +38,29 @@ describe('mobile native chat image preview reconciliation', () => {
pendingKey: 'pending-key',
text: 'look at this',
normalizedText: 'look at this',
imageCount: 1,
baselineTailMessageId: null,
deadline: null
}

expect(findLandedUnconfirmedSends(messages, [unconfirmed])).toEqual([unconfirmed])
expect(findLandedImagePreviewEchoes(messages, [preview])).toEqual([
{ pendingId: 'pending', messageId: 'prompt', images: ['file:///a.jpg'] }
])
})

it('reconciles prompt-before-source and keeps the prompt as preview owner', () => {
const messages = [
userText('prompt', 'look at this[Image #1]'),
userText('source', '[Image: source: /tmp/a.png]')
]
const preview = { ...pending('pending', ['file:///a.jpg']), text: 'look at this' }
const unconfirmed: UnconfirmedSend = {
draftKey: 'draft',
pendingKey: 'pending-key',
text: 'look at this',
normalizedText: 'look at this',
imageCount: 1,
baselineTailMessageId: null,
deadline: null
}
Expand All @@ -58,6 +82,7 @@ describe('mobile native chat image preview reconciliation', () => {
pendingKey: 'pending-key',
text: 'look here',
normalizedText: 'look here',
imageCount: 1,
baselineTailMessageId: null,
deadline: null
}
Expand All @@ -68,6 +93,54 @@ describe('mobile native chat image preview reconciliation', () => {
])
})

it('does not treat markerless text as an unknown-ack literal marker echo', () => {
const unconfirmed: UnconfirmedSend = {
draftKey: 'draft',
pendingKey: 'pending-key',
text: 'keep [Image #1] literal',
normalizedText: 'keep [Image #1] literal',
imageCount: 0,
baselineTailMessageId: null,
deadline: null
}

expect(findLandedUnconfirmedSends([userText('wrong', 'keep literal')], [unconfirmed])).toEqual(
[]
)
expect(
findLandedUnconfirmedSends([userText('right', 'keep [Image #1] literal')], [unconfirmed])
).toEqual([unconfirmed])
})

it('does not treat literal marker text as a markerless unknown-ack echo', () => {
const unconfirmed: UnconfirmedSend = {
draftKey: 'draft',
pendingKey: 'pending-key',
text: 'keep literal',
normalizedText: 'keep literal',
imageCount: 0,
baselineTailMessageId: null,
deadline: null
}

expect(
findLandedUnconfirmedSends([userText('wrong', 'keep [Image #1] literal')], [unconfirmed])
).toEqual([])
expect(findLandedUnconfirmedSends([userText('right', 'keep literal')], [unconfirmed])).toEqual([
unconfirmed
])
})

it('keeps no-image occurrence baselines separate from attached-image turns', () => {
const messages = [
userText('source', '[Image: source: /tmp/a.png]'),
userText('prompt', 'keep [Image #1] literal')
]

expect(countUserTextOccurrences(messages, 'keep literal')).toBe(0)
expect(countUserTextOccurrences(messages, 'keep literal', 1)).toBe(1)
})

it('reconciles multiple transcript text blocks with desktop separators', () => {
const prompt: NativeChatMessage = {
...userText('prompt', 'unused'),
Expand All @@ -84,6 +157,24 @@ describe('mobile native chat image preview reconciliation', () => {
])
})

it('hands a local preview to a marker-only transcript echo', () => {
expect(
findLandedImagePreviewEchoes(
[userText('prompt', '[Image #1]')],
[pending('pending', ['file:///a.jpg'])]
)
).toEqual([{ pendingId: 'pending', messageId: 'prompt', images: ['file:///a.jpg'] }])
})

it('hands a captioned preview to a marker-only compatibility echo', () => {
expect(
findLandedImagePreviewEchoes(
[userText('prompt', 'look [Image #1] here')],
[{ ...pending('pending', ['file:///a.jpg']), text: 'look here' }]
)
).toEqual([{ pendingId: 'pending', messageId: 'prompt', images: ['file:///a.jpg'] }])
})

it('keeps separate adjacent image-only sends independently reconcilable', () => {
const landed = findLandedImagePreviewEchoes(
[
Expand Down Expand Up @@ -164,4 +255,32 @@ describe('mobile native chat image preview reconciliation', () => {
])
).toEqual({ [sessionKey]: { prompt: ['file:///a.jpg'] } })
})

it.each([
['source before prompt', ['sources', 'prompt']],
['prompt before source', ['prompt', 'sources']]
])('moves a multi-source preview only with marker parity: %s', (_label, order) => {
const sessionKey = 'host\0worktree\0tab\0session'
const previous = {
[sessionKey]: { sources: ['file:///a.jpg', 'file:///b.jpg', 'file:///c.jpg'] }
}
const byId = {
sources: {
...userText('sources', 'unused'),
blocks: [
{ type: 'text' as const, text: '[Image: source: /tmp/a.png]' },
{ type: 'text' as const, text: '[Image: source: C:\\Users\\me\\b.png]' },
{ type: 'text' as const, text: '[Image: source: /ssh/workspace/c.png]' }
]
},
prompt: userText('prompt', '[Image #1] [Image #2] [Image #3]')
}
const orderedMessages = () => order.map((id) => byId[id as keyof typeof byId])

expect(migrateImagePreviewMessageIds(previous, sessionKey, orderedMessages())).toEqual({
[sessionKey]: { prompt: ['file:///a.jpg', 'file:///b.jpg', 'file:///c.jpg'] }
})
byId.prompt = userText('prompt', '[Image #1]')
expect(migrateImagePreviewMessageIds(previous, sessionKey, orderedMessages())).toBe(previous)
})
})
151 changes: 106 additions & 45 deletions mobile/src/session/mobile-native-chat-draft-reconcile.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { isImageRefBlock, type NativeChatMessage } from '../../../src/shared/native-chat-types'
import {
hasImagePromptMarker,
countImagePromptMarkers,
isImageSourceUserTurn,
nativeChatUserMessageImageEvidenceCount,
nativeChatUserMessageMatchText,
nativeChatUserTextMatchText,
normalizeImageTranscriptMessages,
normalizeNativeChatUserText,
normalizedNativeChatUserMessageText
} from './mobile-native-chat-image-transcript-markers'
export { normalizeNativeChatUserText as normalizeReconcileText } from './mobile-native-chat-image-transcript-markers'
Expand All @@ -15,6 +17,7 @@ export type UnconfirmedSend = {
pendingKey: string | null
text: string
normalizedText: string
imageCount: number
baselineTailMessageId: string | null
deadline: ReturnType<typeof setTimeout> | null
}
Expand All @@ -25,11 +28,21 @@ export function normalizedUserText(message: NativeChatMessage): string | null {

export function countUserTextOccurrences(
messages: readonly NativeChatMessage[],
text: string
text: string,
imageCount = 0
): number {
let count = 0
for (const message of messages) {
if (normalizedUserText(message) === text) {
for (const message of normalizeImageTranscriptMessages(messages)) {
const hasImageRefs = message.blocks.some(isImageRefBlock)
const matchText =
imageCount > 0
? normalizedNativeChatUserMessageText(message)
: nativeChatUserMessageMatchText(message)
if (
matchText === text &&
(imageCount > 0 || !hasImageRefs) &&
nativeChatUserMessageImageEvidenceCount(message) >= imageCount
) {
count++
}
}
Expand Down Expand Up @@ -94,29 +107,46 @@ export function mergeLandedImagePreviewEchoes(
return next
}

function imagePreviewReplacementMessageId(
messages: readonly NativeChatMessage[],
sourceIndex: number
): string | null {
const source = messages[sourceIndex]
if (!source || !isImageSourceUserTurn(source)) {
return null
}
let nextIndex = sourceIndex + 1
while (
messages[nextIndex]?.source === source.source &&
isImageSourceUserTurn(messages[nextIndex]!)
) {
nextIndex++
function imagePreviewReplacementIds(
messages: readonly NativeChatMessage[]
): ReadonlyMap<string, string> {
const replacements = new Map<string, string>()
for (let index = 0; index < messages.length; index++) {
const source = messages[index]
if (!source || !isImageSourceUserTurn(source)) {
continue
}
let afterSourceIndex = index
let sourceCount = 0
while (afterSourceIndex < messages.length) {
const candidate = messages[afterSourceIndex]
if (!candidate || candidate.source !== source.source || !isImageSourceUserTurn(candidate)) {
break
}
sourceCount += candidate.blocks.length
afterSourceIndex++
}
const prompt = [messages[index - 1], messages[afterSourceIndex]].find(
(candidate) =>
candidate?.role === 'user' &&
candidate.source === source.source &&
countImagePromptMarkers(candidate) === sourceCount
)
if (prompt) {
for (let sourceIndex = index; sourceIndex < afterSourceIndex; sourceIndex++) {
const sourceId = messages[sourceIndex]?.id
if (sourceId) {
replacements.set(sourceId, prompt.id)
}
}
}
index = afterSourceIndex - 1
}
const prompt = messages[nextIndex]
return prompt?.role === 'user' && prompt.source === source.source && hasImagePromptMarker(prompt)
? prompt.id
: null
return replacements
}

/** Moves previews forward when a progressive source-only transcript frame later
* folds into the marker-bearing prompt with a different authoritative id. */
/** Moves previews when a progressive source-only frame later folds into its
* marker-bearing prompt with a different authoritative id. */
export function migrateImagePreviewMessageIds(
previous: Record<string, Record<string, string[]>>,
sessionKey: string,
Expand All @@ -126,14 +156,10 @@ export function migrateImagePreviewMessageIds(
if (!sessionPreviews) {
return previous
}
const messageIndexById = new Map(messages.map((message, index) => [message.id, index]))
const replacementIdBySourceId = imagePreviewReplacementIds(messages)
let nextSession: Record<string, string[]> | null = null
for (const [messageId, images] of Object.entries(sessionPreviews)) {
const sourceIndex = messageIndexById.get(messageId)
if (sourceIndex === undefined) {
continue
}
const replacementId = imagePreviewReplacementMessageId(messages, sourceIndex)
const replacementId = replacementIdBySourceId.get(messageId)
if (!replacementId) {
continue
}
Expand All @@ -160,16 +186,27 @@ export function findLandedImagePreviewEchoes(
if (!entry.images?.length) {
continue
}
const targetText = normalizeNativeChatUserText(entry.text)
const targetText = nativeChatUserTextMatchText(entry.text, true)
const candidates = normalized.filter((message) => {
if (message.role !== 'user') {
return false
}
const imageCount = nativeChatUserMessageImageEvidenceCount(message)
if (targetText) {
return normalizedUserText(message) === targetText
return (
normalizedNativeChatUserMessageText(message) === targetText &&
imageCount >= entry.images!.length
)
}
const imageCount = message.blocks.filter(isImageRefBlock).length
return message.blocks.length === 0 || imageCount >= entry.images!.length
const imageRefCount = message.blocks.filter(isImageRefBlock).length
// Why: a marker-only echo proves delivery per marker, so a partially
// rendered turn must not claim (and rebind) previews it cannot account for.
return (
message.blocks.length === 0 ||
imageRefCount >= entry.images!.length ||
(countImagePromptMarkers(message) >= entry.images!.length &&
normalizedUserText(message) === null)
)
})
const tailIndex = entry.baselineTailMessageId
? messageIndexById.get(entry.baselineTailMessageId)
Expand Down Expand Up @@ -202,17 +239,35 @@ export function findLandedUnconfirmedSends(
// captured tail prove new echoes. User turns are keyed by text; an image echo
// (`[Image: source: …]` or no text) keys under '' so an empty-text send can
// claim it.
const messageIndexById = new Map<string, number>()
const userMessagesByText = new Map<string, Array<{ id: string; index: number }>>()
for (const [index, message] of messages.entries()) {
messageIndexById.set(message.id, index)
const messageIndexById = new Map(messages.map((message, index) => [message.id, index]))
const literalMessagesByText = new Map<
string,
{ id: string; index: number; imageCount: number }[]
>()
const imageMessagesByText = new Map<string, { id: string; index: number; imageCount: number }[]>()
for (const message of normalizeImageTranscriptMessages(messages)) {
const index = messageIndexById.get(message.id)
if (index === undefined) {
continue
}
if (message.role !== 'user') {
continue
}
const key = isImageSourceUserTurn(message) ? '' : (normalizedUserText(message) ?? '')
const current = userMessagesByText.get(key) ?? []
current.push({ id: message.id, index })
userMessagesByText.set(key, current)
const isImageSource = isImageSourceUserTurn(message)
const imageCount = nativeChatUserMessageImageEvidenceCount(message)
const candidate = { id: message.id, index, imageCount }
if (!message.blocks.some(isImageRefBlock)) {
const literalKey = isImageSource ? '' : (nativeChatUserMessageMatchText(message) ?? '')
const current = literalMessagesByText.get(literalKey) ?? []
current.push(candidate)
literalMessagesByText.set(literalKey, current)
}
if (imageCount > 0) {
const imageKey = isImageSource ? '' : (normalizedUserText(message) ?? '')
const current = imageMessagesByText.get(imageKey) ?? []
current.push(candidate)
imageMessagesByText.set(imageKey, current)
}
}

const claimedMessageIds = new Set<string>()
Expand All @@ -224,9 +279,15 @@ export function findLandedUnconfirmedSends(
if (tailIndex === undefined) {
continue
}
const echo = userMessagesByText
const messagesByText = entry.imageCount > 0 ? imageMessagesByText : literalMessagesByText
const echo = messagesByText
.get(entry.normalizedText)
?.find((message) => message.index > tailIndex && !claimedMessageIds.has(message.id))
?.find(
(message) =>
message.index > tailIndex &&
message.imageCount >= entry.imageCount &&
!claimedMessageIds.has(message.id)
)
if (echo) {
claimedMessageIds.add(echo.id)
landed.push(entry)
Expand Down
Loading