fix(chat): pin existing-session sends to captured target (#2424)

* fix(chat): pin sends to captured session

* fix(chat): handle runtime cancellation consistently
This commit is contained in:
Wsyjq
2026-08-07 00:46:21 +03:00
committed by GitHub
parent 834d2edb87
commit 70226149ce
10 changed files with 222 additions and 31 deletions
+16 -3
View File
@@ -1257,6 +1257,7 @@ function ascendingId(prefix: string): string {
* handles deduplication when the server echoes back the real message.
*/
export async function optimisticSend(input: {
runtimeKey?: string
sessionId: string
content: string
providerID: string
@@ -1273,9 +1274,20 @@ export async function optimisticSend(input: {
if (!_optimisticAdd || !_optimisticRemove) {
throw new Error("Optimistic refs not set — is useSync() mounted?")
}
const optimisticAdd = _optimisticAdd
const optimisticRemove = _optimisticRemove
const optimisticConfirm = _optimisticConfirm
const assertRuntimeUnchanged = () => {
if (input.runtimeKey && input.runtimeKey !== getRuntimeKey()) {
throw new Error("Message was not sent because the runtime changed.")
}
}
assertRuntimeUnchanged()
await waitForConnectionOrThrow()
input.beforeOptimisticInsert?.()
assertRuntimeUnchanged()
const targetDirectory = input.directory ?? dir()
const store = targetDirectory ? dirStoreForDirectory(targetDirectory) : dirStore()
@@ -1341,7 +1353,7 @@ export async function optimisticSend(input: {
} as unknown as Message
// Insert into store + register in shadow Map (for mergeOptimisticPage cleanup)
_optimisticAdd({
optimisticAdd({
sessionID: input.sessionId,
directory: targetDirectory,
message: optimisticMessage,
@@ -1359,6 +1371,7 @@ export async function optimisticSend(input: {
})
try {
assertRuntimeUnchanged()
await input.send(messageID)
} catch (error) {
const status = getErrorStatus(error)
@@ -1369,7 +1382,7 @@ export async function optimisticSend(input: {
if (acceptedRecords) {
materializeConfirmedSendRecords(store, input.sessionId, messageID, acceptedRecords)
_optimisticConfirm?.({
optimisticConfirm?.({
sessionID: input.sessionId,
directory: targetDirectory,
messageID,
@@ -1396,7 +1409,7 @@ export async function optimisticSend(input: {
console.warn("[session-actions] prompt send rejected; rolling back optimistic message", failureRecord)
// Rollback via optimistic infrastructure
_optimisticRemove({
optimisticRemove({
sessionID: input.sessionId,
directory: targetDirectory,
messageID,