chat: separate create operation identity

Use an explicit chat_create operation_id as the durable bootstrap key while retaining envelope IDs for reply correlation. Legacy requests receive fresh server-scoped identities, so independent cli-1/android-1 requests do not collide; clients that retry retain one operation_id.

Update Android creation state and repository calls to preserve an operation identity across retries, and cover durable reconnect retries, duplicate barriers, changed input, legacy correlation reuse, and protocol serialization.

Checked packaged templates: none describe chat_create. Checked packaged authoring-skill references: the existing description is stale, but shared references and README must not be edited by an item; docs/notes/bootstrap-create-identity.md records the required docs-fold update and rollout compatibility.
This commit is contained in:
2026-10-18 16:50:12 +01:00
parent 134bd0611a
commit 5946f1abc8
14 changed files with 203 additions and 46 deletions
@@ -287,11 +287,15 @@ class PipelinesRepository(
* The server reports its own error when it does not broker creation; the
* caller surfaces that message rather than guessing at a session id.
*/
suspend fun chatCreate(title: String, directory: String): ChatSession {
suspend fun chatCreate(
title: String,
directory: String,
operationId: String = newChatCreateOperationId(),
): ChatSession {
val response = client.call(
Message(
type = Protocol.CHAT_CREATE,
chatCreate = ChatCreateRequest(title = title, directory = directory),
chatCreate = ChatCreateRequest(title = title, directory = directory, operationId = operationId),
),
)
return response.chatSession
@@ -383,5 +387,8 @@ class PipelinesRepository(
* the same start intent so the server can dedupe idempotent repeats.
*/
fun newRequestKey(): String = "android-" + UUID.randomUUID().toString()
/** A durable key for one chat-create intent; retain it when retrying. */
fun newChatCreateOperationId(): String = "android-chat-" + UUID.randomUUID().toString()
}
}
@@ -59,6 +59,7 @@ data class ChatCloseResult(
data class ChatCreateRequest(
val title: String = "",
val directory: String = "",
@SerialName("operation_id") val operationId: String = "",
)
/** The session-scoped request body for chat_messages and chat_interrupt. */
@@ -1055,7 +1055,7 @@ class AppViewModel(
* create a session wherever the server happens to be.
*/
fun startNewChat() {
chatStore.createOpened()
chatStore.createOpened(PipelinesRepository.newChatCreateOperationId())
refreshChatDirectories()
}
@@ -1076,7 +1076,11 @@ class AppViewModel(
chatStore.creating()
viewModelScope.launch {
try {
val created = repository.chatCreate(title = "New chat", directory = create.directory)
val created = repository.chatCreate(
title = "New chat",
directory = create.directory,
operationId = create.operationId,
)
chatStore.createSucceeded(created)
chatStore.history(created.id, emptyList())
} catch (t: Throwable) {
@@ -224,6 +224,8 @@ data class ChatThread(
*/
data class ChatCreateState(
val directory: String = HOME_DIRECTORY,
/** Durable identity for this one create intent; retained when the request is retried. */
val operationId: String,
val creating: Boolean = false,
val error: String? = null,
)
@@ -461,8 +463,8 @@ class ChatStore(
}
/** Opens the directory picker on its default (the operator's home). */
fun createOpened() {
_state.value = _state.value.copy(create = ChatCreateState())
fun createOpened(operationId: String) {
_state.value = _state.value.copy(create = ChatCreateState(operationId = operationId))
}
/** Picks a directory in the picker; the home stays the default until this is called. */
@@ -0,0 +1,26 @@
package net.awan.pipelines.data
import net.awan.pipelines.data.protocol.ChatCreateRequest
import net.awan.pipelines.data.protocol.Message
import net.awan.pipelines.data.protocol.PipelinesJson
import net.awan.pipelines.data.protocol.Protocol
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Test
class ChatCreateIdentityTest {
@Test
fun aRetriedCreateKeepsItsDurableIdentityWhileNewCreatesDoNotReuseIt() {
val operationId = PipelinesRepository.newChatCreateOperationId()
val retry = Message(
type = Protocol.CHAT_CREATE,
id = "android-2", // Transport correlation may change after reconnect.
chatCreate = ChatCreateRequest(title = "New chat", directory = "/work", operationId = operationId),
)
val decoded = PipelinesJson.decodeFromString(Message.serializer(), PipelinesJson.encodeToString(Message.serializer(), retry))
assertEquals(operationId, decoded.chatCreate!!.operationId)
assertNotEquals(operationId, PipelinesRepository.newChatCreateOperationId())
}
}
@@ -218,12 +218,13 @@ class ChatProtocolTest {
val create = Message(
type = Protocol.CHAT_CREATE,
id = "android-1",
chatCreate = ChatCreateRequest(title = "New chat", directory = "/work"),
chatCreate = ChatCreateRequest(title = "New chat", directory = "/work", operationId = "create-1"),
)
val createJson = PipelinesJson.encodeToString(Message.serializer(), create)
assertTrue(createJson.contains("\"chat_create\""))
val decodedCreate = PipelinesJson.decodeFromString(Message.serializer(), createJson)
assertEquals("New chat", decodedCreate.chatCreate!!.title)
assertEquals("create-1", decodedCreate.chatCreate!!.operationId)
val prompt = Message(
type = Protocol.CHAT_PROMPT,
@@ -33,8 +33,9 @@ class ChatSessionManagementTest {
@Test
fun theCreateFlowDefaultsToHomeAndCreatesInTheChosenDirectory() {
val store = ChatStore()
store.createOpened()
store.createOpened("create-1")
assertEquals(HOME_DIRECTORY, store.value.create!!.directory)
assertEquals("create-1", store.value.create!!.operationId)
store.createDirectoryChosen("/home/op/work")
assertEquals("/home/op/work", store.value.create!!.directory)
@@ -50,12 +51,23 @@ class ChatSessionManagementTest {
fun cancellingTheCreateFlowLeavesTheListUntouched() {
val store = ChatStore()
store.sessionsLoaded(listOf(session("a")))
store.createOpened()
store.createOpened("create-2")
store.createCancelled()
assertNull(store.value.create)
assertEquals(listOf("a"), store.value.visibleSessions.map { it.id })
}
@Test
fun aFailedCreateKeepsItsOperationIdentityForRetry() {
val store = ChatStore()
store.createOpened("create-retry")
store.creating()
store.createFailed("connection lost")
assertEquals("create-retry", store.value.create!!.operationId)
assertTrue(!store.value.create!!.creating)
}
@Test
fun renameWinsOverTheServersTitleAndSurvivesARefresh() {
val store = ChatStore()