From 2cc2c0d976c8e089cef422efa9259e41d2f25a2c Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sun, 18 Oct 2026 20:23:14 +0100 Subject: [PATCH 01/14] wip: preserve chat hide notice work --- .../net/awan/pipelines/ui/AppViewModel.kt | 4 +- .../awan/pipelines/ui/chat/ChatListPane.kt | 23 ++++-- .../net/awan/pipelines/ui/chat/ChatStore.kt | 32 +++++++- .../ui/chat/ChatRemovalNoticeTest.kt | 77 +++++++++++++++++++ docs/notes/hide-chat-notice.md | 21 +++++ 5 files changed, 147 insertions(+), 10 deletions(-) create mode 100644 android/app/src/test/java/net/awan/pipelines/ui/chat/ChatRemovalNoticeTest.kt create mode 100644 docs/notes/hide-chat-notice.md diff --git a/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt b/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt index 1309d46b..fdd28cd5 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt @@ -1106,7 +1106,9 @@ class AppViewModel( */ fun removeChat(sessionId: String) = chatStore.remove(sessionId) - fun undoRemoveChat() = chatStore.undoRemove() + fun consumeChatRemovalNotice(noticeId: Long) = chatStore.consumeRemovalNotice(noticeId) + + fun undoRemoveChat(sessionId: String) = chatStore.undoRemove(sessionId) fun loadChatHistory(sessionId: String) { if (chatStore.value.thread(sessionId)?.loading == true) return diff --git a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatListPane.kt b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatListPane.kt index f9e909bf..c8d10fc5 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatListPane.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatListPane.kt @@ -107,15 +107,24 @@ internal fun ChatListPane( // A removal is reported for what it is: a local, undoable change, never a // pretend delete — the server is not asked to remove anything. val snackbarHostState = remember { SnackbarHostState() } - LaunchedEffect(state.lastRemoved) { - if (state.lastRemoved != null && - snackbarHostState.showSnackbar( + val removalNotice = state.removalNotice + LaunchedEffect(removalNotice?.id) { + removalNotice?.let { notice -> + // A rapid second hide replaces, rather than queues behind, the first + // report. Do not consume until showSnackbar finishes: consuming first + // changes this effect's key and cancels the snackbar coroutine. + snackbarHostState.currentSnackbarData?.dismiss() + if (snackbarHostState.showSnackbar( message = CHAT_REMOVED_MESSAGE, actionLabel = "Undo", - duration = SnackbarDuration.Long, - ) == SnackbarResult.ActionPerformed - ) { - vm.undoRemoveChat() + duration = SnackbarDuration.Short, + ) == SnackbarResult.ActionPerformed) { + vm.undoRemoveChat(notice.sessionId) + } + // A completed notice is not replayed after the pane is recreated. If + // another hide replaced it while this coroutine was cancelled, this + // exact-id consume is a no-op and the newer notice remains pending. + vm.consumeChatRemovalNotice(notice.id) } } var renaming by remember { mutableStateOf(null) } diff --git a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt index cb0d241e..71821b0d 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt @@ -254,6 +254,12 @@ data class ChatState( val overrides: ChatSessionOverrides = ChatSessionOverrides(), /** The id of the session most recently removed, while its removal can be undone. */ val lastRemoved: String? = null, + /** + * The one-shot report that a removal was made. It is deliberately separate + * from [lastRemoved]: consuming a rendered notice must not make the local + * hide irreversible, and revisiting the list must not report it again. + */ + val removalNotice: ChatRemovalNotice? = null, /** What the operator has read of the list (see [ChatSeenState]). */ val seen: ChatSeenState = ChatSeenState(), /** @@ -337,6 +343,9 @@ data class ChatState( get() = filteredSessions.filter { it.closed } } +/** A one-shot, in-memory report for the list's local-hide snackbar. */ +data class ChatRemovalNotice(val id: Long, val sessionId: String) + /** * The multi-chat state model: a small, Android-free holder of [ChatState] with * the transitions the chat surface needs. Keeping it free of Compose and of the @@ -378,6 +387,9 @@ class ChatStore( */ private var localMessageSeq = 0 + /** Monotonic identity for one-shot local-hide reports. */ + private var removalNoticeSeq = 0L + // ---- Session list -------------------------------------------------------- fun sessionsLoading() { @@ -533,15 +545,31 @@ class ChatStore( open = _state.value.open.filterNot { it.sessionId == sessionId }, activeId = if (_state.value.activeId == sessionId) null else _state.value.activeId, lastRemoved = sessionId, + // Keep only the newest report. A snackbar host has one visible slot; + // retaining older reports would turn rapid hides into a long queue. + removalNotice = ChatRemovalNotice(++removalNoticeSeq, sessionId), ) } - /** Restores the most recently removed session to the list and forgets it. */ - fun undoRemove() { + /** + * Marks [noticeId] handled after its snackbar finishes. This transient event + * is never persisted, so refreshes, recomposition and re-entry cannot replay + * a finished report. + */ + fun consumeRemovalNotice(noticeId: Long) { + if (_state.value.removalNotice?.id == noticeId) { + _state.value = _state.value.copy(removalNotice = null) + } + } + + /** Restores [sessionId] only when it is still the current undoable removal. */ + fun undoRemove(sessionId: String? = null) { val id = _state.value.lastRemoved ?: return + if (sessionId != null && sessionId != id) return _state.value = _state.value.copy( overrides = _state.value.overrides.restore(id), lastRemoved = null, + removalNotice = null, ) } diff --git a/android/app/src/test/java/net/awan/pipelines/ui/chat/ChatRemovalNoticeTest.kt b/android/app/src/test/java/net/awan/pipelines/ui/chat/ChatRemovalNoticeTest.kt new file mode 100644 index 00000000..53bcc261 --- /dev/null +++ b/android/app/src/test/java/net/awan/pipelines/ui/chat/ChatRemovalNoticeTest.kt @@ -0,0 +1,77 @@ +package net.awan.pipelines.ui.chat + +import net.awan.pipelines.data.protocol.ChatSession +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Test + +/** The transient local-hide report and durable undo state have separate lifetimes. */ +class ChatRemovalNoticeTest { + + private fun store() = ChatStore().also { + it.sessionsLoaded(listOf(ChatSession(id = "a"), ChatSession(id = "b"), ChatSession(id = "c"))) + } + + @Test + fun expirationConsumesTheNoticeButLeavesTheLocalHideInPlace() { + val store = store() + store.remove("a") + val notice = store.value.removalNotice!! + + // This is what the UI does after its finite snackbar expires, without a + // clock or real sleep. Keeping the notice until then avoids cancelling + // showSnackbar by changing its LaunchedEffect key. + store.consumeRemovalNotice(notice.id) + + assertNull(store.value.removalNotice) + assertEquals("a", store.value.lastRemoved) + assertEquals(listOf("b", "c"), store.value.visibleSessions.map { it.id }) + } + + @Test + fun rapidHidesReplaceThePendingNoticeInsteadOfBuildingAQueue() { + val store = store() + store.remove("a") + val first = store.value.removalNotice!! + store.remove("b") + val second = store.value.removalNotice!! + + assertTrue(second.id > first.id) + assertEquals("b", second.sessionId) + store.consumeRemovalNotice(first.id) + assertEquals(second, store.value.removalNotice) + assertEquals(listOf("c"), store.value.visibleSessions.map { it.id }) + } + + @Test + fun undoRestoresOnlyTheRemovalWhoseNoticeWasActioned() { + val store = store() + store.remove("a") + val first = store.value.removalNotice!! + store.remove("b") + val second = store.value.removalNotice!! + + store.undoRemove(first.sessionId) + assertEquals("b", store.value.lastRemoved) + assertFalse("a stays hidden after its replaced snackbar is dismissed", "a" in store.value.visibleSessions.map { it.id }) + + store.undoRemove(second.sessionId) + assertNull(store.value.lastRemoved) + assertNull(store.value.removalNotice) + assertEquals(listOf("b", "c"), store.value.visibleSessions.map { it.id }) + } + + @Test + fun consumedNoticeDoesNotReturnOnListReentryOrSessionRefresh() { + val store = store() + store.remove("a") + store.consumeRemovalNotice(store.value.removalNotice!!.id) + store.showList() + store.sessionsLoaded(listOf(ChatSession(id = "a"), ChatSession(id = "b"), ChatSession(id = "c"))) + + assertNull(store.value.removalNotice) + assertEquals(listOf("b", "c"), store.value.visibleSessions.map { it.id }) + } +} diff --git a/docs/notes/hide-chat-notice.md b/docs/notes/hide-chat-notice.md new file mode 100644 index 00000000..a1543356 --- /dev/null +++ b/docs/notes/hide-chat-notice.md @@ -0,0 +1,21 @@ +# Local hide notice lifetime + +## What this item changed + +`android/app/src/main/java/net/awan/pipelines/ui/chat/ChatListPane.kt` now presents a local-hide +notice once with Material's short, finite snackbar duration. The `ChatStore` keeps the one-shot +notice separate from the undoable local override, and the pane consumes it only after the snackbar +finishes so changing the effect key cannot cancel its display. A later hide replaces the pending +notice rather than queueing it. Returning to the list, +recomposition and a session refresh therefore cannot replay an already-finished notice; Undo still +restores only the current local hide and does not delete server state or history. + +`ChatRemovalNoticeTest` covers finite-notice consumption, rapid hides, Undo and list re-entry using +the pure store, with no real-time wait. + +## Shared documents this change makes stale + +- `android/README.md`'s chat-session behaviour should say that **Hide on this device** has a + short, finite Undo notice, rapid hides replace rather than queue notices, and an expired or + consumed notice is not replayed when returning to the list. It should name + `ChatRemovalNoticeTest` as unit-test evidence and leave on-device timing to visual review. From 6521bf87e9cee1590c87901cd1a71b8b7051e1de Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sun, 18 Oct 2026 20:32:36 +0100 Subject: [PATCH 02/14] wip: preserve background task viewer --- .../pipelines/ui/chat/ChatConversationPane.kt | 9 +-- .../net/awan/pipelines/ui/chat/ChatStore.kt | 31 ++++--- .../awan/pipelines/ui/chat/ChatTranscript.kt | 81 +++++++++++++++---- docs/notes/background-tasks-viewer.md | 16 ++++ 4 files changed, 105 insertions(+), 32 deletions(-) create mode 100644 docs/notes/background-tasks-viewer.md diff --git a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatConversationPane.kt b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatConversationPane.kt index 1ee6a729..449f7647 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatConversationPane.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatConversationPane.kt @@ -246,12 +246,9 @@ internal fun ChatConversationPane( ) } } - // The background processes still running, shown whether or not a turn - // is: a shell can outlive the turn that started it, so this is a strip - // of its own rather than part of the turn indicator, and it clears the - // moment each shell exits. - if (thread.runningShells.isNotEmpty()) { - BackgroundProcessBar(thread.runningShells) + // The viewer keeps the bounded exit tail only when this client saw it. + if (thread.observedShells.isNotEmpty()) { + BackgroundProcessBar(thread.observedShells) } if (thread.error != null && thread.loaded) { Text( diff --git a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt index cb0d241e..5548151c 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt @@ -115,12 +115,11 @@ data class ChatThread( */ val lastDeltaAt: Long = 0, /** - * The agent's shell commands still running for this session, keyed by the - * shell id the server reported and in the order they started. A background - * process can outlive the tool call (and the turn) that started it, so this is - * thread state of its own rather than part of the turn: it keeps showing after - * the turn's indicator has gone, and a shell leaves the map the moment its - * exited frame arrives. Kept on the thread so it survives a chat switch. + * Verified shell commands observed for this session, keyed by their server + * id and in observation order. Running shells come from the authoritative + * snapshot/event stream. A small exited tail is retained only after this + * client observed its exit, so a selected task can settle honestly without + * claiming historical shell discovery. */ val shells: Map = emptyMap(), /** The unsent composer text for this chat. */ @@ -201,11 +200,14 @@ data class ChatThread( runningToolElapsedMillis(part, toolStartedAt[part.id] ?: 0L, nowMillis) /** - * The background processes this session is still running, in start order, so - * a conversation shows what is running even when no turn is. + * The background processes currently running for this session, in start + * order, so a conversation shows what is running even when no turn is. */ val runningShells: List get() = runningBackgroundShells(shells) + /** Bounded observed tasks, including a recently observed exit when present. */ + val observedShells: List get() = shells.values.toList() + /** * What the in-flight turn is doing at [nowMillis] (see [turnPhase]): its * running tool calls, and how long ago the last delta arrived. Pure, so the @@ -1049,10 +1051,15 @@ class ChatStore( val shell = frame.shell ?: return if (shell.id.isBlank()) return update(frame.sessionId) { thread -> - val shells = if (shell.status == ChatProtocol.SHELL_EXITED) { - thread.shells - shell.id + val prior = thread.shells[shell.id] + val merged = if (shell.status == ChatProtocol.SHELL_EXITED) { + shell.copy(command = shell.command.ifBlank { prior?.command.orEmpty() }) } else { - thread.shells + (shell.id to shell) + shell.copy(command = shell.command.ifBlank { prior?.command.orEmpty() }) + } + val shells = (thread.shells + (shell.id to merged)).let { observed -> + if (observed.size <= OBSERVED_SHELL_LIMIT) observed + else observed.entries.toList().takeLast(OBSERVED_SHELL_LIMIT).associate { it.toPair() } } if (shells == thread.shells) thread else thread.copy(shells = shells) } @@ -1203,5 +1210,7 @@ class ChatStore( companion object { const val LOCAL_MESSAGE_PREFIX = "local-" const val NOTICE_PREFIX = "notice-" + /** Per-session in-memory observation bound; this is not task history. */ + const val OBSERVED_SHELL_LIMIT = 16 } } diff --git a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatTranscript.kt b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatTranscript.kt index a51c462f..54aa48a1 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatTranscript.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatTranscript.kt @@ -386,35 +386,48 @@ private fun RunningToolLine(part: ChatPart, elapsedMillis: Long) { } } -/** - * The always-visible indicator that the agent has a background process running — - * a dev server, a watching build, a sleep whose tool call has already returned. - * It is deliberately separate from the turn's spinner: a background process can - * outlive the turn, so this is shown whenever a shell is running, whether or not - * a turn is, and each line names the command the way the running-tool line names - * a call. It clears the moment the shell exits, because the exited frame removes - * it from the thread's shells. It survives a chat switch like the other live - * state, since it is read from the thread rather than held here. - */ +/** A read-only, expandable task viewer for verified shells this chat observed. */ @Composable internal fun BackgroundProcessBar(shells: List, modifier: Modifier = Modifier) { + var selectedID by rememberSaveable { mutableStateOf(null) } + val selected = shells.firstOrNull { it.id == selectedID } Column( verticalArrangement = Arrangement.spacedBy(SPACING_XS), modifier = modifier.fillMaxWidth().padding(horizontal = CHAT_GUTTER, vertical = 4.dp), ) { + Text( + text = "Background tasks", + style = MaterialTheme.typography.labelLarge, + color = MaterialTheme.colorScheme.onSurface, + ) + Text( + text = "Running now and recently observed exits; not complete task history.", + style = MaterialTheme.typography.bodySmall, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) shells.forEach { shell -> - key(shell.id) { BackgroundProcessLine(shell) } + key(shell.id) { + BackgroundProcessLine(shell, selectedID == shell.id) { + selectedID = if (selectedID == shell.id) null else shell.id + } + } + } + if (selected != null) { + BackgroundProcessDetail(selected) } } } -/** One background process: a distinct glyph, a `Background process` label and its command. */ +/** One verified shell task; selection reveals its read-only detail. */ @Composable -private fun BackgroundProcessLine(shell: ChatShell) { +private fun BackgroundProcessLine(shell: ChatShell, selected: Boolean, onSelect: () -> Unit) { Row( verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(CHAT_ITEM_GAP), - modifier = Modifier.fillMaxWidth(), + modifier = Modifier + .fillMaxWidth() + .minimumInteractiveComponentSize() + .clickable(role = Role.Button, onClick = onSelect), ) { Icon( Icons.Default.PlayArrow, @@ -423,7 +436,7 @@ private fun BackgroundProcessLine(shell: ChatShell) { modifier = Modifier.size(ICON_STATUS_DP), ) Text( - text = "Background process", + text = if (shell.status == ChatProtocol.SHELL_EXITED) "Exited" else "Running", style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.tertiary, ) @@ -435,6 +448,44 @@ private fun BackgroundProcessLine(shell: ChatShell) { overflow = TextOverflow.Ellipsis, modifier = Modifier.weight(1f), ) + if (selected) { + Text( + text = "Details", + style = MaterialTheme.typography.labelSmall, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + } + } +} + +/** Detail intentionally exposes only state known from verified lifecycle frames. */ +@Composable +private fun BackgroundProcessDetail(shell: ChatShell) { + Surface( + color = MaterialTheme.colorScheme.surfaceContainer, + shape = RoundedCornerShape(CHAT_GUTTER), + modifier = Modifier.fillMaxWidth(), + ) { + SelectionContainer { + Column( + verticalArrangement = Arrangement.spacedBy(SPACING_XS), + modifier = Modifier.padding(CHAT_GUTTER), + ) { + Text("Command", style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.onSurfaceVariant) + Text(shell.command.ifBlank { "Unavailable" }, style = MaterialTheme.typography.bodyMedium) + Text("Task identity", style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.onSurfaceVariant) + Text(shell.id, style = MaterialTheme.typography.bodySmall, fontFamily = FontFamily.Monospace) + Text( + text = if (shell.status == ChatProtocol.SHELL_EXITED) { + "Status: exited" + (shell.exit?.let { " (exit $it)" } ?: "; exit status unavailable") + } else { + "Status: running" + }, + style = MaterialTheme.typography.bodySmall, + ) + Text("Output is unavailable until the server provides verified readback.", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant) + } + } } } diff --git a/docs/notes/background-tasks-viewer.md b/docs/notes/background-tasks-viewer.md new file mode 100644 index 00000000..25c5cab8 --- /dev/null +++ b/docs/notes/background-tasks-viewer.md @@ -0,0 +1,16 @@ +# Background tasks viewer limits + +The chat background-task surface now shows only shell identities verified for the +selected chat session by the current-running snapshot and lifecycle events. It +keeps a bounded in-memory tail of exits observed by this client so a selected task +can settle with an observed exit code; it is not durable task history. + +The upstream shell list is RUNNING-only. Tasks which ended before observation are +not enumerated or backfilled. The current installed chat protocol has no verified +shell-output readback, so the detail explicitly reports output as unavailable +rather than inventing it. + +This changes the chat UI. The packaged OpenCode skill references and chat topic +documentation should describe the running-only, bounded-observation behavior and +the lack of durable history when the manual `docs-fold` pipeline next updates +shared documentation. From 1b5b581de8ec7950b4c7b22e2a5006be417edef7 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sun, 18 Oct 2026 21:48:22 +0100 Subject: [PATCH 03/14] chore: preserve notifications clear-all preparation From dad89493de033f5dfdc0ec21a2928bc3037eda4b Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sun, 18 Oct 2026 23:22:28 +0100 Subject: [PATCH 04/14] Preserve reviewed Clear all change for leased UI verification --- .../src/main/java/net/awan/pipelines/ui/Attention.kt | 6 ++++++ .../awan/pipelines/ui/components/AttentionSurface.kt | 11 +++++++++++ .../test/java/net/awan/pipelines/ui/AttentionTest.kt | 10 ++++++++++ docs/notes/notifications-clear-all.md | 10 ++++++++++ 4 files changed, 37 insertions(+) create mode 100644 docs/notes/notifications-clear-all.md diff --git a/android/app/src/main/java/net/awan/pipelines/ui/Attention.kt b/android/app/src/main/java/net/awan/pipelines/ui/Attention.kt index 4e2b1d6d..69870c4b 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/Attention.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/Attention.kt @@ -179,6 +179,12 @@ internal fun attentionHeadline(entries: List): String = when { else -> "${entries.size} runs need you" } +/** + * Captures the entries shown when the attention sheet opens. Bulk dismissal uses + * this snapshot so entries that arrive while the sheet is open are not removed. + */ +internal fun attentionDismissalSnapshot(entries: List): List = entries.toList() + /** * Parses the run id from a `pipelines://run/` deep link, or null for * anything else. Pure and total, so a malformed link simply opens nothing rather diff --git a/android/app/src/main/java/net/awan/pipelines/ui/components/AttentionSurface.kt b/android/app/src/main/java/net/awan/pipelines/ui/components/AttentionSurface.kt index 8ca63a3e..b8c1f895 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/components/AttentionSurface.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/components/AttentionSurface.kt @@ -17,8 +17,10 @@ import androidx.compose.material3.MaterialTheme import androidx.compose.material3.ModalBottomSheet import androidx.compose.material3.Surface import androidx.compose.material3.Text +import androidx.compose.material3.TextButton import androidx.compose.material3.rememberModalBottomSheetState import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color @@ -27,6 +29,7 @@ import androidx.compose.ui.unit.dp import net.awan.pipelines.ui.AttentionEntry import net.awan.pipelines.ui.AttentionKind import net.awan.pipelines.ui.attentionHeadline +import net.awan.pipelines.ui.attentionDismissalSnapshot import net.awan.pipelines.ui.theme.ICON_INLINE_DP import net.awan.pipelines.ui.theme.SPACING_L import net.awan.pipelines.ui.theme.SPACING_M @@ -105,6 +108,7 @@ fun AttentionSheet( onDismiss: () -> Unit, ) { val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true) + val dismissalSnapshot = remember { attentionDismissalSnapshot(entries) } ModalBottomSheet(onDismissRequest = onDismiss, sheetState = sheetState) { Column( modifier = Modifier.fillMaxWidth().padding(bottom = SPACING_L), @@ -124,6 +128,13 @@ fun AttentionSheet( ) return@Column } + TextButton( + onClick = { dismissalSnapshot.forEach(onDismissEntry) }, + enabled = dismissalSnapshot.isNotEmpty(), + modifier = Modifier.padding(horizontal = SPACING_L), + ) { + Text("Clear all") + } entries.forEach { entry -> AttentionRow( entry = entry, diff --git a/android/app/src/test/java/net/awan/pipelines/ui/AttentionTest.kt b/android/app/src/test/java/net/awan/pipelines/ui/AttentionTest.kt index 50ed94f0..d4fa3d13 100644 --- a/android/app/src/test/java/net/awan/pipelines/ui/AttentionTest.kt +++ b/android/app/src/test/java/net/awan/pipelines/ui/AttentionTest.kt @@ -194,6 +194,16 @@ class AttentionTest { assertEquals("A run failed", attentionHeadline(singleFailed)) } + @Test + fun dismissalSnapshotDoesNotIncludeEntriesThatArriveLater() { + val original = attentionEntries(listOf(run(1, "waiting")), AttentionState()) + val snapshot = attentionDismissalSnapshot(original) + val current = original + attentionEntries(listOf(run(2, "failed")), AttentionState()) + + assertEquals(listOf(1L), snapshot.map { it.runId }) + assertEquals(listOf(1L, 2L), current.map { it.runId }) + } + // --- persistence ----------------------------------------------------------- @Test diff --git a/docs/notes/notifications-clear-all.md b/docs/notes/notifications-clear-all.md new file mode 100644 index 00000000..674600a9 --- /dev/null +++ b/docs/notes/notifications-clear-all.md @@ -0,0 +1,10 @@ +# Needs you: Clear all + +The Needs you bottom sheet now exposes **Clear all** when it has entries. It +dismisses only the entries present when the sheet opened through the existing +individual dismissal path; entries arriving while it is open remain visible. +The action is absent from the empty state. UI verification remains the next +stage's responsibility. + +The packaged author skill/reference documentation should be folded to mention +this user-facing behavior rather than being edited in this item. From 72281716c605fb1e20dbb16bff4ae7c55678026e Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sun, 18 Oct 2026 23:25:16 +0100 Subject: [PATCH 05/14] Preserve bounded background viewer fixes before resumed review --- .../net/awan/pipelines/ui/AppViewModel.kt | 68 ++++++++++++++++++- .../pipelines/ui/chat/ChatConversationPane.kt | 8 ++- .../net/awan/pipelines/ui/chat/ChatStore.kt | 43 ++++++++++++ .../awan/pipelines/ui/chat/ChatTranscript.kt | 36 ++++++++-- .../ui/chat/ChatInFlightStateTest.kt | 36 ++++++++++ docs/notes/background-tasks-viewer.md | 7 +- 6 files changed, 185 insertions(+), 13 deletions(-) diff --git a/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt b/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt index 1309d46b..067cb237 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt @@ -6,6 +6,7 @@ import androidx.lifecycle.viewmodel.initializer import androidx.lifecycle.viewmodel.viewModelFactory import java.util.concurrent.ConcurrentHashMap import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.CoroutineStart import kotlinx.coroutines.Job import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow @@ -169,6 +170,7 @@ class AppViewModel( private val startKeys = ConcurrentHashMap() private val retryKeys = ConcurrentHashMap() + private val shellOutputJobs = ConcurrentHashMap() /** The multi-chat screen state (sessions, open conversations, drafts, scroll). */ private val chatStore = ChatStore( @@ -1009,17 +1011,22 @@ class AppViewModel( * shown on return. */ fun openChatSession(session: ChatSession) { + cancelShellOutputWhenSwitchingTo(session.id) val needsHistory = chatStore.open(session) if (needsHistory) loadChatHistory(session.id) } /** Switches to an already-open chat, re-syncing it when it is running. */ fun activateChat(sessionId: String) { + cancelShellOutputWhenSwitchingTo(sessionId) if (chatStore.activate(sessionId)) loadChatHistory(sessionId) } /** Shows the session list again while keeping every open conversation. */ - fun showChatList() = chatStore.showList() + fun showChatList() { + chatStore.value.activeId?.let(::cancelShellOutput) + chatStore.showList() + } /** * Narrows the session list to what matches [query] over each chat's title and @@ -1029,7 +1036,10 @@ class AppViewModel( */ fun setChatSessionQuery(query: String) = chatStore.searchSessions(query) - fun closeChat(sessionId: String) = chatStore.close(sessionId) + fun closeChat(sessionId: String) { + cancelShellOutput(sessionId) + chatStore.close(sessionId) + } fun updateChatDraft(sessionId: String, text: String) = chatStore.draft(sessionId, text) @@ -1104,7 +1114,10 @@ class AppViewModel( * anything — it exposes no delete for an OpenCode session — so the removal is * local and can be undone from the message that reports it. */ - fun removeChat(sessionId: String) = chatStore.remove(sessionId) + fun removeChat(sessionId: String) { + cancelShellOutput(sessionId) + chatStore.remove(sessionId) + } fun undoRemoveChat() = chatStore.undoRemove() @@ -1121,6 +1134,7 @@ class AppViewModel( try { val page = repository.chatMessages(sessionId, CHAT_HISTORY_LIMIT) chatStore.history(sessionId, page.messages, page.nextCursor, page.hasMore) + loadChatShells(sessionId) } catch (t: Throwable) { if (t is CancellationException) throw t chatStore.failed(sessionId, ErrorMessages.describe(t)) @@ -1128,6 +1142,53 @@ class AppViewModel( } } + private fun loadChatShells(sessionId: String) { + viewModelScope.launch { + try { + chatStore.shellsLoaded(sessionId, repository.chatShells(sessionId)) + } catch (t: Throwable) { + if (t is CancellationException) throw t + // Shell listing is additive; an older server must not make chat history fail. + } + } + } + + fun selectChatShell(sessionId: String, shellId: String?) { + cancelShellOutput(sessionId) + chatStore.selectShell(sessionId, shellId) + if (shellId != null) refreshChatShellOutput(sessionId, shellId) + } + + fun refreshChatShellOutput(sessionId: String, shellId: String) { + cancelShellOutput(sessionId) + chatStore.shellOutputLoading(sessionId, shellId) + // Install before starting so a replacement can never be removed by an + // older request that finishes during launch. + val job = viewModelScope.launch(start = CoroutineStart.LAZY) { + try { + // Always reread from zero: the server's bounded page is authoritative, + // and the client never appends unbounded output to an in-memory buffer. + val page = repository.chatShellOutput(sessionId, shellId, cursor = 0) + chatStore.shellOutputLoaded(sessionId, shellId, page) + } catch (t: Throwable) { + if (t is CancellationException) throw t + chatStore.shellOutputFailed(sessionId, shellId, ErrorMessages.describe(t)) + } finally { + shellOutputJobs.remove(sessionId, coroutineContext[Job]) + } + } + shellOutputJobs[sessionId] = job + job.start() + } + + private fun cancelShellOutputWhenSwitchingTo(sessionId: String) { + chatStore.value.activeId?.takeIf { it != sessionId }?.let(::cancelShellOutput) + } + + private fun cancelShellOutput(sessionId: String) { + shellOutputJobs.remove(sessionId)?.cancel() + } + /** Fetches the next opaque older-history page without replacing live state. */ fun loadOlderChatHistory(sessionId: String) { val thread = chatStore.value.thread(sessionId) ?: return @@ -1325,6 +1386,7 @@ class AppViewModel( */ fun closeChatSession(sessionId: String) { if (sessionId.isBlank()) return + cancelShellOutput(sessionId) viewModelScope.launch { try { val closed = repository.chatClose(sessionId) diff --git a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatConversationPane.kt b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatConversationPane.kt index 449f7647..d585da61 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatConversationPane.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatConversationPane.kt @@ -248,7 +248,13 @@ internal fun ChatConversationPane( } // The viewer keeps the bounded exit tail only when this client saw it. if (thread.observedShells.isNotEmpty()) { - BackgroundProcessBar(thread.observedShells) + BackgroundProcessBar( + shells = thread.observedShells, + selectedID = thread.selectedShellId, + output = thread.shellOutput, + onSelect = { vm.selectChatShell(thread.sessionId, it) }, + onRefresh = { vm.refreshChatShellOutput(thread.sessionId, it) }, + ) } if (thread.error != null && thread.loaded) { Text( diff --git a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt index 5548151c..39f6e29e 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt @@ -10,6 +10,13 @@ import net.awan.pipelines.data.protocol.ChatPermissionFrame import net.awan.pipelines.data.protocol.ChatProtocol import net.awan.pipelines.data.protocol.ChatSession import net.awan.pipelines.data.protocol.ChatShell +import net.awan.pipelines.data.protocol.ChatShellOutputPage + +data class ChatShellOutputState( + val loading: Boolean = false, + val error: String? = null, + val page: ChatShellOutputPage? = null, +) /** * A saved scroll position inside one conversation. Kept as Android-free @@ -122,6 +129,8 @@ data class ChatThread( * claiming historical shell discovery. */ val shells: Map = emptyMap(), + val selectedShellId: String? = null, + val shellOutput: ChatShellOutputState? = null, /** The unsent composer text for this chat. */ val draft: String = "", /** @@ -893,6 +902,40 @@ class ChatStore( fun running(sessionId: String, running: Boolean) = update(sessionId) { setRunning(it, running) } + /** Replaces the running snapshot while retaining only exits observed locally. */ + fun shellsLoaded(sessionId: String, running: List) = update(sessionId) { thread -> + val live = running.filter { it.id.isNotBlank() }.associateBy { it.id } + val observedExits = thread.shells.filterValues { it.status == ChatProtocol.SHELL_EXITED } + thread.copy(shells = (observedExits + live).entries.toList() + .takeLast(OBSERVED_SHELL_LIMIT).associate { it.toPair() }) + } + + fun selectShell(sessionId: String, shellId: String?) = update(sessionId) { thread -> + if (shellId != null && shellId !in thread.shells) thread + else thread.copy(selectedShellId = shellId, shellOutput = null) + } + + fun shellOutputLoading(sessionId: String, shellId: String) = update(sessionId) { thread -> + if (thread.selectedShellId != shellId) thread + else thread.copy(shellOutput = ChatShellOutputState(loading = true)) + } + + fun shellOutputLoaded(sessionId: String, shellId: String, page: ChatShellOutputPage) = update(sessionId) { thread -> + if (thread.selectedShellId != shellId) thread + else { + val reported = page.shell.takeIf { it.id.isNotBlank() } + val shells = if (reported == null) thread.shells else + (thread.shells + (reported.id to reported.copy(command = reported.command.ifBlank { thread.shells[reported.id]?.command.orEmpty() }))) + .entries.toList().takeLast(OBSERVED_SHELL_LIMIT).associate { it.toPair() } + thread.copy(shells = shells, shellOutput = ChatShellOutputState(page = page)) + } + } + + fun shellOutputFailed(sessionId: String, shellId: String, message: String) = update(sessionId) { thread -> + if (thread.selectedShellId != shellId) thread + else thread.copy(shellOutput = ChatShellOutputState(error = message)) + } + /** * Records that the operator's last prompt was accepted into the session's * queue (true) or sent immediately (false), so the composer can say "Queued" diff --git a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatTranscript.kt b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatTranscript.kt index 54aa48a1..6e34614d 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatTranscript.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatTranscript.kt @@ -388,8 +388,14 @@ private fun RunningToolLine(part: ChatPart, elapsedMillis: Long) { /** A read-only, expandable task viewer for verified shells this chat observed. */ @Composable -internal fun BackgroundProcessBar(shells: List, modifier: Modifier = Modifier) { - var selectedID by rememberSaveable { mutableStateOf(null) } +internal fun BackgroundProcessBar( + shells: List, + selectedID: String?, + output: ChatShellOutputState?, + onSelect: (String?) -> Unit, + onRefresh: (String) -> Unit, + modifier: Modifier = Modifier, +) { val selected = shells.firstOrNull { it.id == selectedID } Column( verticalArrangement = Arrangement.spacedBy(SPACING_XS), @@ -408,12 +414,12 @@ internal fun BackgroundProcessBar(shells: List, modifier: Modifier = shells.forEach { shell -> key(shell.id) { BackgroundProcessLine(shell, selectedID == shell.id) { - selectedID = if (selectedID == shell.id) null else shell.id + onSelect(if (selectedID == shell.id) null else shell.id) } } } if (selected != null) { - BackgroundProcessDetail(selected) + BackgroundProcessDetail(selected, output, onRefresh) } } } @@ -460,7 +466,7 @@ private fun BackgroundProcessLine(shell: ChatShell, selected: Boolean, onSelect: /** Detail intentionally exposes only state known from verified lifecycle frames. */ @Composable -private fun BackgroundProcessDetail(shell: ChatShell) { +private fun BackgroundProcessDetail(shell: ChatShell, output: ChatShellOutputState?, onRefresh: (String) -> Unit) { Surface( color = MaterialTheme.colorScheme.surfaceContainer, shape = RoundedCornerShape(CHAT_GUTTER), @@ -483,7 +489,25 @@ private fun BackgroundProcessDetail(shell: ChatShell) { }, style = MaterialTheme.typography.bodySmall, ) - Text("Output is unavailable until the server provides verified readback.", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant) + when { + output?.loading == true -> CircularProgressIndicator(modifier = Modifier.size(ICON_STATUS_DP)) + output?.error != null -> Row(horizontalArrangement = Arrangement.spacedBy(SPACING_XS)) { + Text(output.error, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.error) + Text("Retry", style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.primary, + modifier = Modifier.clickable { onRefresh(shell.id) }) + } + output?.page?.unavailable == true -> Text("Output unavailable for this task.", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant) + output?.page != null && output.page.output.isEmpty() -> Text("No output yet.", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant) + output?.page != null -> { + Text("Output", style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.onSurfaceVariant) + Text(output.page.output, style = MaterialTheme.typography.bodySmall, fontFamily = FontFamily.Monospace) + if (output.page.truncated) { + Text("Output is bounded to 64 KiB.", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant) + } + Text("Refresh", style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.primary, + modifier = Modifier.clickable { onRefresh(shell.id) }) + } + } } } } diff --git a/android/app/src/test/java/net/awan/pipelines/ui/chat/ChatInFlightStateTest.kt b/android/app/src/test/java/net/awan/pipelines/ui/chat/ChatInFlightStateTest.kt index d3c21330..bad873d7 100644 --- a/android/app/src/test/java/net/awan/pipelines/ui/chat/ChatInFlightStateTest.kt +++ b/android/app/src/test/java/net/awan/pipelines/ui/chat/ChatInFlightStateTest.kt @@ -5,6 +5,7 @@ import net.awan.pipelines.data.protocol.ChatPart import net.awan.pipelines.data.protocol.ChatProtocol import net.awan.pipelines.data.protocol.ChatSession import net.awan.pipelines.data.protocol.ChatShell +import net.awan.pipelines.data.protocol.ChatShellOutputPage import net.awan.pipelines.data.protocol.ChatStep import net.awan.pipelines.data.protocol.ChatToolState import org.junit.Assert.assertEquals @@ -208,4 +209,39 @@ class ChatInFlightStateTest { assertEquals(listOf("s"), store.streamResumed()) assertTrue(store.value.thread("s")!!.runningShells.isEmpty()) } + + @Test + fun shellSelectionKeepsObservedExitAndDropsStaleOutputOnSwitch() { + val store = ChatStore(now = { 1_000L }) + store.open(ChatSession(id = "s", title = "s")) + store.shellsLoaded("s", listOf(ChatShell(id = "sh_1", status = ChatProtocol.SHELL_RUNNING, command = "run"))) + store.selectShell("s", "sh_1") + store.shellOutputLoading("s", "sh_1") + store.shellOutputLoaded("s", "sh_1", ChatShellOutputPage(output = "actual", cursor = 6, size = 6)) + store.event(shellFrame("s", "sh_1", ChatProtocol.SHELL_EXITED, exit = 0)) + + val exited = store.value.thread("s")!! + assertEquals(ChatProtocol.SHELL_EXITED, exited.observedShells.single().status) + assertEquals("actual", exited.shellOutput?.page?.output) + + store.shellsLoaded("s", listOf(ChatShell(id = "sh_2", status = ChatProtocol.SHELL_RUNNING, command = "other"))) + store.selectShell("s", "sh_2") + assertEquals("sh_2", store.value.thread("s")!!.selectedShellId) + assertEquals(null, store.value.thread("s")!!.shellOutput) + } + + @Test + fun shellOutputCannotCrossSessionOrSelection() { + val store = ChatStore(now = { 1_000L }) + store.open(ChatSession(id = "a", title = "a")) + store.open(ChatSession(id = "b", title = "b")) + store.shellsLoaded("a", listOf(ChatShell(id = "same", status = ChatProtocol.SHELL_RUNNING))) + store.shellsLoaded("b", listOf(ChatShell(id = "same", status = ChatProtocol.SHELL_RUNNING))) + store.selectShell("a", "same") + store.shellOutputLoaded("b", "same", ChatShellOutputPage(output = "foreign")) + assertEquals(null, store.value.thread("a")!!.shellOutput) + assertEquals(null, store.value.thread("b")!!.shellOutput) + store.shellOutputLoaded("a", "same", ChatShellOutputPage(output = "owned")) + assertEquals("owned", store.value.thread("a")!!.shellOutput?.page?.output) + } } diff --git a/docs/notes/background-tasks-viewer.md b/docs/notes/background-tasks-viewer.md index 25c5cab8..c8e07ca6 100644 --- a/docs/notes/background-tasks-viewer.md +++ b/docs/notes/background-tasks-viewer.md @@ -6,9 +6,10 @@ keeps a bounded in-memory tail of exits observed by this client so a selected ta can settle with an observed exit code; it is not durable task history. The upstream shell list is RUNNING-only. Tasks which ended before observation are -not enumerated or backfilled. The current installed chat protocol has no verified -shell-output readback, so the detail explicitly reports output as unavailable -rather than inventing it. +not enumerated or backfilled. Verified output is read lazily for the selected task +through one bounded 64 KiB page; refresh rereads that page from cursor zero rather +than accumulating client history. A task that exits after observation remains +visible and reports the server's output or an explicit unavailable/empty state. This changes the chat UI. The packaged OpenCode skill references and chat topic documentation should describe the running-only, bounded-observation behavior and From 1ebf0f46dce343b3e4a2e96d49e9d35728fb1261 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sun, 18 Oct 2026 23:29:59 +0100 Subject: [PATCH 06/14] Preserve hide notice re-entry fixes before renewed UI acceptance --- .../net/awan/pipelines/ui/AppViewModel.kt | 2 ++ .../awan/pipelines/ui/chat/ChatListPane.kt | 27 +++++++++---------- .../net/awan/pipelines/ui/chat/ChatStore.kt | 10 +++++++ .../ui/chat/ChatRemovalNoticeTest.kt | 14 ++++++++++ docs/notes/hide-chat-notice.md | 6 ++--- 5 files changed, 42 insertions(+), 17 deletions(-) diff --git a/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt b/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt index fdd28cd5..d35f03d8 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt @@ -1108,6 +1108,8 @@ class AppViewModel( fun consumeChatRemovalNotice(noticeId: Long) = chatStore.consumeRemovalNotice(noticeId) + fun beginChatRemovalNotice(noticeId: Long) = chatStore.beginRemovalNotice(noticeId) + fun undoRemoveChat(sessionId: String) = chatStore.undoRemove(sessionId) fun loadChatHistory(sessionId: String) { diff --git a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatListPane.kt b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatListPane.kt index c8d10fc5..00502cb6 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatListPane.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatListPane.kt @@ -110,21 +110,20 @@ internal fun ChatListPane( val removalNotice = state.removalNotice LaunchedEffect(removalNotice?.id) { removalNotice?.let { notice -> - // A rapid second hide replaces, rather than queues behind, the first - // report. Do not consume until showSnackbar finishes: consuming first - // changes this effect's key and cancels the snackbar coroutine. - snackbarHostState.currentSnackbarData?.dismiss() - if (snackbarHostState.showSnackbar( - message = CHAT_REMOVED_MESSAGE, - actionLabel = "Undo", - duration = SnackbarDuration.Short, - ) == SnackbarResult.ActionPerformed) { - vm.undoRemoveChat(notice.sessionId) + // Claim delivery before showing. Completion remains separate so a + // cancelled effect cannot make this same notice replay on re-entry. + if (vm.beginChatRemovalNotice(notice.id)) { + // A rapid second hide replaces, rather than queues behind, the first. + snackbarHostState.currentSnackbarData?.dismiss() + if (snackbarHostState.showSnackbar( + message = CHAT_REMOVED_MESSAGE, + actionLabel = "Undo", + duration = SnackbarDuration.Short, + ) == SnackbarResult.ActionPerformed) { + vm.undoRemoveChat(notice.sessionId) + } + vm.consumeChatRemovalNotice(notice.id) } - // A completed notice is not replayed after the pane is recreated. If - // another hide replaced it while this coroutine was cancelled, this - // exact-id consume is a no-op and the newer notice remains pending. - vm.consumeChatRemovalNotice(notice.id) } } var renaming by remember { mutableStateOf(null) } diff --git a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt index 71821b0d..6f4e1446 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt @@ -260,6 +260,8 @@ data class ChatState( * hide irreversible, and revisiting the list must not report it again. */ val removalNotice: ChatRemovalNotice? = null, + /** The notice whose snackbar delivery has already started, if any. */ + val removalNoticePresentedId: Long? = null, /** What the operator has read of the list (see [ChatSeenState]). */ val seen: ChatSeenState = ChatSeenState(), /** @@ -562,6 +564,14 @@ class ChatStore( } } + /** Claims a notice before delivery so a cancelled pane cannot replay it. */ + fun beginRemovalNotice(noticeId: Long): Boolean { + val state = _state.value + if (state.removalNotice?.id != noticeId || state.removalNoticePresentedId == noticeId) return false + _state.value = state.copy(removalNoticePresentedId = noticeId) + return true + } + /** Restores [sessionId] only when it is still the current undoable removal. */ fun undoRemove(sessionId: String? = null) { val id = _state.value.lastRemoved ?: return diff --git a/android/app/src/test/java/net/awan/pipelines/ui/chat/ChatRemovalNoticeTest.kt b/android/app/src/test/java/net/awan/pipelines/ui/chat/ChatRemovalNoticeTest.kt index 53bcc261..159264c9 100644 --- a/android/app/src/test/java/net/awan/pipelines/ui/chat/ChatRemovalNoticeTest.kt +++ b/android/app/src/test/java/net/awan/pipelines/ui/chat/ChatRemovalNoticeTest.kt @@ -74,4 +74,18 @@ class ChatRemovalNoticeTest { assertNull(store.value.removalNotice) assertEquals(listOf("b", "c"), store.value.visibleSessions.map { it.id }) } + + @Test + fun presentedNoticeDoesNotReplayWhenLeavingAndReenteringBeforeItExpires() { + val store = store() + store.remove("a") + val notice = store.value.removalNotice!! + + assertTrue(store.beginRemovalNotice(notice.id)) + store.showList() + + assertFalse(store.beginRemovalNotice(notice.id)) + assertEquals(notice, store.value.removalNotice) + assertEquals(listOf("b", "c"), store.value.visibleSessions.map { it.id }) + } } diff --git a/docs/notes/hide-chat-notice.md b/docs/notes/hide-chat-notice.md index a1543356..57594b2e 100644 --- a/docs/notes/hide-chat-notice.md +++ b/docs/notes/hide-chat-notice.md @@ -4,9 +4,9 @@ `android/app/src/main/java/net/awan/pipelines/ui/chat/ChatListPane.kt` now presents a local-hide notice once with Material's short, finite snackbar duration. The `ChatStore` keeps the one-shot -notice separate from the undoable local override, and the pane consumes it only after the snackbar -finishes so changing the effect key cannot cancel its display. A later hide replaces the pending -notice rather than queueing it. Returning to the list, +notice separate from the undoable local override and records delivery separately from snackbar +completion, so leaving and re-entering cannot replay an in-flight notice. A later hide replaces the +pending notice rather than queueing it. Returning to the list, recomposition and a session refresh therefore cannot replay an already-finished notice; Undo still restores only the current local hide and does not delete server state or history. From 58fa528afc5d28490b64f02fb13e2d3eb0936a5f Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sun, 18 Oct 2026 23:30:56 +0100 Subject: [PATCH 07/14] test: retain committed stage diffs --- docs/notes/retained-committed-merge-diffs.md | 7 + internal/server/file_changes_test.go | 152 +++++++++++++++++++ 2 files changed, 159 insertions(+) create mode 100644 docs/notes/retained-committed-merge-diffs.md diff --git a/docs/notes/retained-committed-merge-diffs.md b/docs/notes/retained-committed-merge-diffs.md new file mode 100644 index 00000000..7dacd29d --- /dev/null +++ b/docs/notes/retained-committed-merge-diffs.md @@ -0,0 +1,7 @@ +# Committed and merged stage diffs remain retained + +The server captures workspace contents around each stage visit rather than comparing +Git status or HEAD. Regression coverage now exercises a stage that commits its edit, +a later stage that resolves and commits a merge conflict, and an empty commit-only +stage. After reopening SQLite, the first two patches remain available while the final +stage correctly records no source-content change. diff --git a/internal/server/file_changes_test.go b/internal/server/file_changes_test.go index d9ffb6d8..fe1518a5 100644 --- a/internal/server/file_changes_test.go +++ b/internal/server/file_changes_test.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "os" + "os/exec" "path/filepath" "strings" "testing" @@ -162,3 +163,154 @@ func TestRepeatedStageVisitsRetainIndependentDiffs(t *testing.T) { } } } + +func TestRetainedStageDiffsSurviveCommittedMergeAndNoopVisit(t *testing.T) { + ctx := context.Background() + db := filepath.Join(t.TempDir(), "committed-diffs.db") + store, err := pipeline.Open(db) + if err != nil { + t.Fatal(err) + } + source := pipeline.Template([]string{"commit", "merge", "noop"}, true) + if _, err := store.Submit(ctx, "committed-diffs", source); err != nil { + t.Fatal(err) + } + items, err := store.List(ctx) + if err != nil { + t.Fatal(err) + } + + workspace := t.TempDir() + runGit(t, workspace, "init", "-q") + runGit(t, workspace, "config", "user.email", "test@example.invalid") + runGit(t, workspace, "config", "user.name", "test") + path := filepath.Join(workspace, "source.txt") + if err := os.WriteFile(path, []byte("base\n"), 0600); err != nil { + t.Fatal(err) + } + runGit(t, workspace, "add", "source.txt") + runGit(t, workspace, "commit", "-qm", "base") + branch := runGitOutput(t, workspace, "branch", "--show-current") + + r, err := newRunner(ctx, store) + if err != nil { + t.Fatal(err) + } + git := func(args ...string) error { + cmd := exec.Command("git", append([]string{"-C", workspace}, args...)...) + if output, err := cmd.CombinedOutput(); err != nil { + return fmt.Errorf("git %v: %w: %s", args, err, output) + } + return nil + } + gitOutput := func(args ...string) (string, error) { + cmd := exec.Command("git", append([]string{"-C", workspace}, args...)...) + output, err := cmd.Output() + if err != nil { + return "", fmt.Errorf("git %v: %w", args, err) + } + return strings.TrimSpace(string(output)), nil + } + r.execute = func(_ context.Context, def stage.Definition, _ stage.Values) (stage.Result, error) { + switch def.Key { + case "commit": + if err := os.WriteFile(path, []byte("committed change\n"), 0600); err != nil { + return stage.Result{}, err + } + if err := git("add", "source.txt"); err != nil { + return stage.Result{}, err + } + if err := git("commit", "-qm", "stage commit"); err != nil { + return stage.Result{}, err + } + case "merge": + if err := git("checkout", "-qb", "conflict-side"); err != nil { + return stage.Result{}, err + } + if err := os.WriteFile(path, []byte("side change\n"), 0600); err != nil { + return stage.Result{}, err + } + if err := git("add", "source.txt"); err != nil { + return stage.Result{}, err + } + if err := git("commit", "-qm", "side"); err != nil { + return stage.Result{}, err + } + if err := git("checkout", "-q", branch); err != nil { + return stage.Result{}, err + } + if err := os.WriteFile(path, []byte("main change\n"), 0600); err != nil { + return stage.Result{}, err + } + if err := git("add", "source.txt"); err != nil { + return stage.Result{}, err + } + if err := git("commit", "-qm", "main"); err != nil { + return stage.Result{}, err + } + if err := git("merge", "--no-ff", "conflict-side", "-m", "merge"); err == nil { + return stage.Result{}, errors.New("expected merge conflict") + } + conflicted, err := gitOutput("diff", "--name-only", "--diff-filter=U") + if err != nil || conflicted != "source.txt" { + return stage.Result{}, fmt.Errorf("merge did not leave the expected conflict: %q, %v", conflicted, err) + } + if err := os.WriteFile(path, []byte("resolved merge\n"), 0600); err != nil { + return stage.Result{}, err + } + if err := git("add", "source.txt"); err != nil { + return stage.Result{}, err + } + if err := git("commit", "-qm", "resolve merge"); err != nil { + return stage.Result{}, err + } + case "noop": + if err := git("commit", "--allow-empty", "-qm", "no content change"); err != nil { + return stage.Result{}, err + } + } + return stage.Result{Outcome: "success"}, nil + } + + run, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "committed-diffs", Workspace: workspace, Input: map[string]any{"prompt": "task"}}) + if err != nil { + t.Fatal(err) + } + r.wg.Wait() + if err := store.Close(); err != nil { + t.Fatal(err) + } + store, err = pipeline.Open(db) + if err != nil { + t.Fatal(err) + } + defer store.Close() + + detail, err := store.GetRun(ctx, run.ID) + if err != nil { + t.Fatal(err) + } + if detail.Status != "succeeded" || len(detail.Steps) != 3 { + t.Fatal(detail) + } + for _, assertion := range []struct { + step int + old string + new string + }{ + {step: 0, old: "base", new: "committed change"}, + {step: 1, old: "committed change", new: "resolved merge"}, + } { + changes := detail.Steps[assertion.step].FileChanges + if changes == nil || len(changes.Files) != 1 || !strings.Contains(changes.Files[0].Patch, "-"+assertion.old) || !strings.Contains(changes.Files[0].Patch, "+"+assertion.new) { + t.Fatalf("committed stage patch was not retained after reopen: %+v", changes) + } + } + if changes := detail.Steps[2].FileChanges; changes == nil || len(changes.Files) != 0 { + t.Fatalf("commit-only stage changed source evidence: %+v", changes) + } + data, err := os.ReadFile(path) + if err != nil || string(data) != "resolved merge\n" { + t.Fatalf("merge resolution was not preserved: %q, %v", data, err) + } +} From 31d78cc065317e4b98d0cbff422d040538198862 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sun, 18 Oct 2026 23:33:30 +0100 Subject: [PATCH 08/14] stage: expose resource targets to model harnesses --- docs/notes/resource-pool-runner-handoff.md | 6 +- internal/stage/model_prompt.go | 28 ++++-- internal/stage/model_run.go | 3 +- internal/stage/model_test.go | 103 +++++++++++++++++++++ 4 files changed, 128 insertions(+), 12 deletions(-) diff --git a/docs/notes/resource-pool-runner-handoff.md b/docs/notes/resource-pool-runner-handoff.md index d7c9d091..d92d1e47 100644 --- a/docs/notes/resource-pool-runner-handoff.md +++ b/docs/notes/resource-pool-runner-handoff.md @@ -2,8 +2,10 @@ Stage `resources` declarations now accept exactly one of `exclusive` or `pool`. Pool stages wait on the named pool, then receive the selected concrete member and -bounded metadata through explicit runtime context. The model input remains -unchanged and capability tokens remain private to the store. Retained control +bounded metadata through explicit runtime context. Model execution serializes that +server-granted context as a separate `resource_target` runtime field for both +native and plain harnesses; the task input map remains unchanged and cannot select +the target. Capability tokens remain private to the store. Retained control evidence records the concrete target; queued run state names the pool while an active owner names the selected member. diff --git a/internal/stage/model_prompt.go b/internal/stage/model_prompt.go index f6cc3db2..2a0f9f7f 100644 --- a/internal/stage/model_prompt.go +++ b/internal/stage/model_prompt.go @@ -8,6 +8,8 @@ import ( "sort" "strings" "time" + + "git.a-wan.net/alan/pipelines/internal/protocol" ) var ErrInvalidModelResult = errors.New("invalid model stage result") @@ -36,6 +38,13 @@ func harnessUnreachable(err error) error { } func modelPrompt(def Definition, spec ModelSpec, input Values) (string, error) { + return modelPromptForTarget(def, spec, input, nil) +} + +// modelPromptForTarget carries a server-selected resource target separately from +// task input. The target comes only from the runner context; task input remains +// data and cannot select or replace it. +func modelPromptForTarget(def Definition, spec ModelSpec, input Values, target *protocol.ResourceTarget) (string, error) { outcomes := make([]string, 0, len(def.Edges)) for name := range def.Edges { outcomes = append(outcomes, name) @@ -59,15 +68,16 @@ func modelPrompt(def Definition, spec ModelSpec, input Values) (string, error) { toolsLine = "This call has these stage tools: " + strings.Join(tools, ", ") + "." } data, err := json.Marshal(struct { - Key string `json:"stage"` - Instructions string `json:"instructions"` - OutputContract string `json:"output_contract"` - Outcomes []string `json:"allowed_outcomes"` - Input Values `json:"input"` - ReportFileArtifacts bool `json:"report_file_artifacts"` - ExecutionTime string `json:"execution_time"` - Timezone string `json:"timezone"` - }{def.Key, spec.Prompt, spec.Output, outcomes, input, def.ReportFileArtifacts, time.Now().Format(time.RFC3339), time.Local.String()}) + Key string `json:"stage"` + Instructions string `json:"instructions"` + OutputContract string `json:"output_contract"` + Outcomes []string `json:"allowed_outcomes"` + Input Values `json:"input"` + ResourceTarget *protocol.ResourceTarget `json:"resource_target,omitempty"` + ReportFileArtifacts bool `json:"report_file_artifacts"` + ExecutionTime string `json:"execution_time"` + Timezone string `json:"timezone"` + }{def.Key, spec.Prompt, spec.Output, outcomes, input, target, def.ReportFileArtifacts, time.Now().Format(time.RFC3339), time.Local.String()}) if err != nil { return "", fmt.Errorf("model stage input is not JSON-serializable: %w", err) } diff --git a/internal/stage/model_run.go b/internal/stage/model_run.go index 41300243..2bc5ee34 100644 --- a/internal/stage/model_run.go +++ b/internal/stage/model_run.go @@ -16,7 +16,8 @@ func (m ModelStage) Run(ctx context.Context, def Definition, input Values) (Resu return Result{}, err } spec.Config.ResultSchema = resultSchema(def.Edges) - prompt, err := modelPrompt(def, spec, input) + target, _ := ResourceTarget(ctx) + prompt, err := modelPromptForTarget(def, spec, input, target) if err != nil { return Result{}, err } diff --git a/internal/stage/model_test.go b/internal/stage/model_test.go index 0cd1ac04..a0070755 100644 --- a/internal/stage/model_test.go +++ b/internal/stage/model_test.go @@ -9,6 +9,8 @@ import ( "strings" "testing" "time" + + "git.a-wan.net/alan/pipelines/internal/protocol" ) func modelDefinition() Definition { @@ -194,6 +196,107 @@ func TestModelRunUsesInstructionsAndStructuredOutput(t *testing.T) { } } +func TestModelRunForwardsOnlyServerGrantedResourceTarget(t *testing.T) { + target := &protocol.ResourceTarget{Member: "emulator", Metadata: map[string]string{"serial": "emu-5554", "type": "emulator"}} + budget := NewBudget(0, 0) + input := Values{"resource_target": map[string]any{"member": "user-selected"}, "prompt": "task"} + ctx := WithBudget(WithResourceTarget(context.Background(), target), budget) + + var received ModelConfig + client := generatorFunc(func(_ context.Context, config ModelConfig, prompt string) (string, error) { + received = config + assertPromptResourceTarget(t, prompt, target, input) + return `{"outcome":"success","output":{}}`, nil + }) + if _, err := (ModelStage{Client: client}).Run(ctx, modelDefinition(), input); err != nil { + t.Fatal(err) + } + if received.Budget != budget { + t.Fatal("budget context was not preserved with resource target") + } +} + +func TestModelRunForwardsResourceTargetToNativeOpenCode(t *testing.T) { + target := &protocol.ResourceTarget{Member: "phone", Metadata: map[string]string{"serial": "phone-serial"}} + var prompt string + client := &OpenCodeClient{Directory: "/work/repo", command: func(_ context.Context, _ string, args ...string) ([]byte, error) { + switch args[2] { + case "/api/session": + return []byte(`{"data":{"id":"ses_target"}}`), nil + case "/api/session/ses_target/generate": + var body struct { + Prompt string `json:"prompt"` + } + if err := json.Unmarshal([]byte(args[4]), &body); err != nil { + t.Fatal(err) + } + prompt = body.Prompt + return []byte(`{"data":{"text":"{\"outcome\":\"success\",\"output\":{}}"}}`), nil + default: + t.Fatalf("unexpected fake OpenCode endpoint %q", args[2]) + return nil, nil + } + }} + if _, err := (ModelStage{Client: client}).Run(WithResourceTarget(context.Background(), target), modelDefinition(), Values{"prompt": "task"}); err != nil { + t.Fatal(err) + } + assertPromptResourceTarget(t, prompt, target, Values{"prompt": "task"}) +} + +func TestModelPromptWithoutResourceTargetPreservesLegacyInput(t *testing.T) { + input := Values{"prompt": "task", "count": "1"} + spec, err := (ModelStage{}).Spec(modelDefinition()) + if err != nil { + t.Fatal(err) + } + prompt, err := modelPrompt(modelDefinition(), spec, input) + if err != nil { + t.Fatal(err) + } + var payload struct { + Input Values `json:"input"` + ResourceTarget *protocol.ResourceTarget `json:"resource_target"` + } + if _, data, ok := strings.Cut(prompt, "Stage definition and runtime input:\n"); !ok || json.Unmarshal([]byte(data), &payload) != nil { + t.Fatal("could not decode legacy runtime payload") + } + if payload.ResourceTarget != nil || !reflect.DeepEqual(payload.Input, input) { + t.Fatalf("legacy runtime input changed: %#v", payload) + } +} + +func assertPromptResourceTarget(t *testing.T, prompt string, want *protocol.ResourceTarget, wantInput Values) { + t.Helper() + _, data, ok := strings.Cut(prompt, "Stage definition and runtime input:\n") + if !ok { + t.Fatal("missing runtime payload") + } + var payload struct { + Input Values `json:"input"` + ResourceTarget *protocol.ResourceTarget `json:"resource_target"` + } + if err := json.Unmarshal([]byte(data), &payload); err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(payload.ResourceTarget, want) { + t.Fatalf("resource target = %#v, want %#v", payload.ResourceTarget, want) + } + var fields map[string]json.RawMessage + if err := json.Unmarshal([]byte(data), &fields); err != nil { + t.Fatal(err) + } + var targetFields map[string]json.RawMessage + if err := json.Unmarshal(fields["resource_target"], &targetFields); err != nil { + t.Fatal(err) + } + if len(targetFields) != 2 || targetFields["member"] == nil || targetFields["metadata"] == nil { + t.Fatalf("resource target leaked non-target fields: %s", fields["resource_target"]) + } + if !reflect.DeepEqual(payload.Input, wantInput) { + t.Fatalf("input = %#v, want %#v", payload.Input, wantInput) + } +} + func TestModelResultValidationAndRetryBudget(t *testing.T) { for _, text := range []string{ `not json`, `{"outcome":"missing","output":{}}`, `{"outcome":"success"}`, From 89a2bf1e01543e781cc643240585bbeb48c801c1 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sun, 18 Oct 2026 23:37:46 +0100 Subject: [PATCH 09/14] Add operator resource pool configuration commands --- docs/notes/resource-pool-operator-config.md | 7 ++ internal/cli/app.go | 63 ++++++++++- internal/cli/commands.go | 113 ++++++++++---------- internal/pipeline/resources.go | 15 +++ internal/protocol/message.go | 5 + internal/protocol/resources.go | 1 + internal/server/resource_cli_test.go | 86 +++++++++++++++ internal/server/websocket.go | 14 ++- 8 files changed, 246 insertions(+), 58 deletions(-) create mode 100644 docs/notes/resource-pool-operator-config.md diff --git a/docs/notes/resource-pool-operator-config.md b/docs/notes/resource-pool-operator-config.md new file mode 100644 index 00000000..555e8ca4 --- /dev/null +++ b/docs/notes/resource-pool-operator-config.md @@ -0,0 +1,7 @@ +# Resource-pool operator configuration — 2026-10-18 + +`resource-pool-configure --yes` replaces one bounded pool declaration +through the loopback-only operator transport. `resource-pool-inspect ` reads +the declared members, enabled/quarantine status, and non-secret metadata. Existing +owners and waiters remain under the store's normal ownership, quarantine, and FIFO +rules; configuration does not release active leases. diff --git a/internal/cli/app.go b/internal/cli/app.go index 571f3e2c..92032422 100644 --- a/internal/cli/app.go +++ b/internal/cli/app.go @@ -49,7 +49,7 @@ type options struct { json, source, noOpen, yes, allow, createOnly, force, wait bool version, expected, limit, recoverySequence, retryVisit int recoveryRun int64 - recoveryStage, resource string + recoveryStage, resource, poolFile string timeout, waitTimeout time.Duration stageModels stageModelFlags } @@ -193,6 +193,11 @@ func (a App) flags(command string) (*flag.FlagSet, *options) { f.StringVar(&o.actor, "actor", "operator", "Bounded operator identity") f.BoolVar(&o.allow, "no-mutating-action", false, "Evidence explicitly records that no mutating action occurred") f.BoolVar(&o.yes, "yes", false, "Confirm reconciliation without prompting") + case "resource-pool-inspect": + f.StringVar(&o.resource, "pool", "", "Resource pool name") + case "resource-pool-configure": + f.StringVar(&o.poolFile, "file", "", "JSON pool declaration, or - for stdin") + f.BoolVar(&o.yes, "yes", false, "Confirm this mutation without prompting") case "debug": f.StringVar(&o.harness, "harness", "", "Debugging harness: configured opencode/pi, or nvim") f.BoolVar(&o.yes, "yes", false, "Approve opening a debugging session without prompting") @@ -479,7 +484,7 @@ func (a App) Run(ctx context.Context, args []string) error { requestTimeout = 30 * time.Second } connectURL := o.url - if command == "resource-inspect" || command == "resource-reconcile" { + if command == "resource-inspect" || command == "resource-reconcile" || command == "resource-pool-inspect" || command == "resource-pool-configure" { var transportErr error connectURL, transportErr = operatorTransportURL(o.url) if transportErr != nil { @@ -966,6 +971,60 @@ func (a App) Run(ctx context.Context, args []string) error { } _, err = fmt.Fprintf(a.Out, "Reconciled resource %s for run #%d\n", pos[0], runID) return err + case "resource-pool-inspect": + if len(pos) > 1 { + return fmt.Errorf("resource-pool-inspect takes one pool name") + } + if o.resource == "" && len(pos) == 1 { + o.resource = pos[0] + } + if strings.TrimSpace(o.resource) == "" { + return fmt.Errorf("resource-pool-inspect requires a pool name") + } + r, err := call(protocol.Message{Type: protocol.InspectResourcePool, Resource: o.resource}) + if err != nil { + return err + } + if r.ResourcePool == nil { + return fmt.Errorf("server did not return resource pool") + } + return a.output(r.ResourcePool) + case "resource-pool-configure": + if len(pos) > 1 { + return fmt.Errorf("resource-pool-configure takes one JSON file") + } + file := o.poolFile + if file == "" && len(pos) == 1 { + file = pos[0] + } + if file == "" { + return fmt.Errorf("resource-pool-configure requires a JSON file") + } + var data []byte + if file == "-" { + data, err = io.ReadAll(a.In) + } else { + data, err = os.ReadFile(file) + } + if err != nil { + return err + } + var pool protocol.ResourcePool + if err := json.Unmarshal(data, &pool); err != nil { + return fmt.Errorf("invalid resource pool JSON: %w", err) + } + if err := a.confirmAction(fmt.Sprintf("Configure resource pool %s?", pool.Name), o.yes); err != nil { + return err + } + r, err := call(protocol.Message{Type: protocol.ConfigureResourcePool, ResourcePool: &pool}) + if err != nil { + return err + } + if o.json { + return a.output(r) + } + _, err = fmt.Fprintf(a.Out, "Configured resource pool %s\n", pool.Name) + return err case "inspect", "cancel", "retry": value, err := one() if err != nil { diff --git a/internal/cli/commands.go b/internal/cli/commands.go index 2759d473..89d3e3fd 100644 --- a/internal/cli/commands.go +++ b/internal/cli/commands.go @@ -2,64 +2,67 @@ package cli // Help is also the public command inventory for agents and parity checks. var usage = map[string]string{ - "provider-check": "provider-check --model PROVIDER/MODEL [--yes] [--json]", - "new": "new [name] [prompt] [--template KEY] [--no-open | --json]", - "edit": "edit [name|id] [prompt] [--version N] [--no-open | --json]", - "drafts": "drafts [--json]", - "draft-delete": "draft-delete [--json]", - "validate": "validate [--json]", - "submit": "submit [--name NAME] [--create-only] [--yes] [--json]", - "list": "list [--json]", - "search": "search QUERY [--type KIND] [--limit N] [--json]", - "show": "show [--version N] [--json]", - "versions": "versions [--json]", - "export": "export [name|id ...] [--output FILE.zip] [--all] [--version N]", - "import": "import [FILE.zip] [--all] [--on-conflict ask|keep|both|import] [--yes] [--json]", - "templates": "templates [--json]", - "template": "template [--source | --json]", - "run": "run [name|id] [prompt] [--title TITLE] [--model PROVIDER/MODEL] [--stage-model STAGE=PROVIDER/MODEL ...] [--json [--wait [--wait-timeout DURATION]]]", - "runs": "runs [--pipeline NAME] [--json]", - "inspect": "inspect [--json]", - "wait": "wait [run-id ...] [--timeout DURATION] [--json]", - "cancel": "cancel [--json]", - "decide": "decide [stage] --option KEY [--feedback TEXT] [--json]", - "retry": "retry [--mode resume|restart] [--stage KEY [--visit N]] [--version N] [--input-file FILE|-] [--json [--wait [--wait-timeout DURATION]]]", - "title": "title \"\" [--json]", - "debug": "debug <run-id> [--json | --no-open]", - "chat-adopt": "chat-adopt <session-id> [--json]", - "connection": "connection [--json]", - "ping": "ping [--json]", - "version": "version [--json]", - "status": "status [--json]", - "stats": "stats [--json]", - "resource-inspect": "resource-inspect <resource> [--json]", - "resource-reconcile": "resource-reconcile <resource> <run-id> <stage> <sequence> --evidence TEXT [--no-mutating-action] [--yes] [--json]", - "rpc": "rpc <request.json|->", - "retention": "retention report|apply [--older-than DURATION] [--keep N] [--db FILE] [--artifacts DIR] [--json] [--yes]", - "tui": "tui", + "provider-check": "provider-check --model PROVIDER/MODEL [--yes] [--json]", + "new": "new [name] [prompt] [--template KEY] [--no-open | --json]", + "edit": "edit [name|id] [prompt] [--version N] [--no-open | --json]", + "drafts": "drafts [--json]", + "draft-delete": "draft-delete <draft.md> [--json]", + "validate": "validate <draft.md> [--json]", + "submit": "submit <draft.md> [--name NAME] [--create-only] [--yes] [--json]", + "list": "list [--json]", + "search": "search QUERY [--type KIND] [--limit N] [--json]", + "show": "show <name|id> [--version N] [--json]", + "versions": "versions <name|id> [--json]", + "export": "export [name|id ...] [--output FILE.zip] [--all] [--version N]", + "import": "import [FILE.zip] [--all] [--on-conflict ask|keep|both|import] [--yes] [--json]", + "templates": "templates [--json]", + "template": "template <key> [--source | --json]", + "run": "run [name|id] [prompt] [--title TITLE] [--model PROVIDER/MODEL] [--stage-model STAGE=PROVIDER/MODEL ...] [--json [--wait [--wait-timeout DURATION]]]", + "runs": "runs [--pipeline NAME] [--json]", + "inspect": "inspect <run-id> [--json]", + "wait": "wait <run-id> [run-id ...] [--timeout DURATION] [--json]", + "cancel": "cancel <run-id> [--json]", + "decide": "decide <run-id> [stage] --option KEY [--feedback TEXT] [--json]", + "retry": "retry <run-id> [--mode resume|restart] [--stage KEY [--visit N]] [--version N] [--input-file FILE|-] [--json [--wait [--wait-timeout DURATION]]]", + "title": "title <run-id> \"<title>\" [--json]", + "debug": "debug <run-id> [--json | --no-open]", + "chat-adopt": "chat-adopt <session-id> [--json]", + "connection": "connection [--json]", + "ping": "ping [--json]", + "version": "version [--json]", + "status": "status [--json]", + "stats": "stats [--json]", + "resource-inspect": "resource-inspect <resource> [--json]", + "resource-reconcile": "resource-reconcile <resource> <run-id> <stage> <sequence> --evidence TEXT [--no-mutating-action] [--yes] [--json]", + "resource-pool-inspect": "resource-pool-inspect <pool> [--json]", + "resource-pool-configure": "resource-pool-configure <pool.json|-> --yes [--json]", + "rpc": "rpc <request.json|->", + "retention": "retention report|apply [--older-than DURATION] [--keep N] [--db FILE] [--artifacts DIR] [--json] [--yes]", + "tui": "tui", } var notes = map[string]string{ - "version": "Identify the local CLI binary without connecting to a server. --version is an alias. Use ping --json to identify the running server separately.", - "ping": "Pong includes the running server's compiled build.version, build.commit and build.dirty (null when unknown). This identifies the server, not the client or runtime workspace. Older servers omit build. The client compares its own build/protocol against the server: a same-protocol build mismatch prints one warning to stderr, a server speaking a newer protocol fails the command, and a response carrying a field this client does not model prints a one-time unrecognized-field notice instead of dropping it silently.", - "connection": "Prints the server endpoint and phone notification endpoints. Like ping, it reports the client/server build relationship and warns or fails rather than silently proceeding against a mismatched server.", - "status": "Reports server, host and ntfy publishing health. The client compares its build/protocol against the server and warns once (or fails on a newer protocol) rather than proceeding silently.", - "export": "Without a name, select pipelines with Space and save a ZIP in the current directory. --output writes a portable definition bundle; --all skips selection. One name without --output prints Markdown as before. No runs, credentials or old versions are exported.", - "import": "Select definitions from a ZIP. Identical existing definitions are skipped. For differing names, choose keep existing, keep both under a new name, or use imported as a new version (history retained). --on-conflict supplies an explicit automation policy; --yes only approves final publication. Schedules may activate; no run history is imported.", - "new": "Create an optional template copy or a one-stage starter. Opens the configured default harness; --harness opencode|pi|nvim overrides it. No submission is performed.", - "edit": "Omit the name to open fuzzy selection. Original immutable source is copied to a private draft. The configured harness edits that draft, never the live definition.", - "validate": "Uses the same server parser as submission. Does not persist or activate schedules. Structural validity does not prove runtime/tool availability.", - "submit": "Confirm publication at the prompt, or use --yes for explicit automation; schedules activate on acceptance. Managed drafts provide the name and base-version guard. --force never bypasses validation.", - "run": "Runs in your current directory after action confirmation (--yes skips it). Select a pipeline and compose a multiline task with Ctrl+S. Stage tools are enabled automatically; no file list or typed RUN confirmation. Cancellation does not undo edits. --title sets an explicit run title; without it the title is derived from the prompt. --model PROVIDER/MODEL overrides the model for every stage of that run; --stage-model STAGE=PROVIDER/MODEL overrides one stage and is repeatable, with a repeated key last-wins. Precedence is per-stage > whole-run > the model pinned in the pipeline version; overrides apply to that run only and never change the pinned definition. An unknown stage key or a malformed provider/model is refused before the run starts, naming the valid stage keys. In JSON mode --wait polls until terminal; --wait-timeout bounds only the client wait after admission and is independent of per-request --timeout. A reached wait limit leaves the run active: inspect read-only with `inspect <run-id> --json`, or reattach with --wait reusing the original --request-key (never an automatic replay or cancel).", - "retry": "Resume a failed stage by default, restoring the completed prefix handoff; use --mode=restart to start at entry. --stage selects a recorded stage (use --visit for repeated stages). --input-file accepts a bounded JSON object, merged with replacement at the selected stage; '-' reads stdin. Original history and pinned identity are retained; --version selects an explicit published version of the same pipeline only. --wait-timeout leaves an active retry for later read-only inspection or reattachment with the original --request-key, rather than starting a duplicate retry.", - "title": "Rename a run's operator-facing title after the fact without touching its input, steps or execution. The title is collapsed to a single bounded line and recorded as explicitly set. `pipelines runs` and `pipelines inspect` then show it.", - "wait": "Block until each named run reaches a terminal status - succeeded, failed, canceled or interrupted, and also through a run waiting on a person - then print one line per run with its id, status and outcome, its stage and how long it took, and exit 0 even for a run that finished unsuccessfully. Several ids wait together in one command. The exit is non-zero only for a client-side failure: an unknown run id, a connection that could not be established, or --timeout expiring while a run is still active; a timeout says so plainly, never cancels, retries or otherwise touches a run, and is never reported as its outcome. Read-only: it needs no workspace, is not refused by the workspace guard and writes nothing. --json prints the final run envelope instead of the line (one envelope for one id, an array for several).", - "decide": "Choose an option for a run waiting on a human stage. The run must be `waiting`; the option must be one of that stage's non-error edges. The stage is inferred from the run's current stage unless named. The choice and optional --feedback are recorded as the step's evidence (visible in inspect and the TUI) and the run resumes along the chosen edge. An unknown option is refused with the valid list; a repeated decision for an already-decided visit does not double-apply. Works across a server restart because the wait is persisted.", - "debug": "Open a fresh configured harness conversation to investigate a run using inspect/show/runs CLI queries. --harness opencode|pi|nvim chooses another configured harness or read-only nvim packet. Does not resume implementation or retry. --json returns the prompt without launching anything.", - "rpc": "Accepts a JSON protocol envelope; '-' reads stdin. Every TUI server operation is available here too. Mutating commands are not dry runs. The omni-search command is `search` (fields query, optional kind and limit) and answers with ranked `search_results`.", - "chat-adopt": "Adopt a session that already exists on the configured OpenCode endpoint so it appears in chat_sessions and can be continued. The id must be one the endpoint reports; its directory and title are taken from the endpoint's own record (never guessed), and a missing id records nothing. Adopting is always explicit and does not change the server's endpoint choice.", - "retention": "Local, explicit run-history retention; never automatic and never a side effect of another command. `report` is strictly read-only and prints what a policy would remove; `apply` removes it after approval (--yes for scripted use). Policy: --older-than DURATION selects runs created before now minus that rolling UTC duration (Go durations plus d/w; not a calendar boundary), --keep N keeps the newest N runs by run id and selects older ranks (--keep 0 keeps none). Both together are the conservative intersection; at least one is required. Protected and never removed: queued/running runs, runs waiting on a person (waiting run or step), runs with an active cleanup agent, and any run referenced by a retained run's retry provenance (transitively). A run that becomes protected before deletion aborts the whole apply. Artifacts at <root>/<run> are removed with their run and kept with protected/retained runs; the database is VACUUMed and its WAL checkpointed, with measured reclaimed bytes. Definitions and versions are immutable and never touched. --db must name an existing database (default: the standard data directory); --artifacts defaults to artifacts/ beside --db. Works without a running server.", - "search": "One read-only query over pipelines, versions, runs, steps, field text and artifacts. QUERY is free text, a bare status word (queued, running, succeeded, failed, canceled, interrupted) selecting those runs, or a lone date expression (today, yesterday, this week [Monday 00:00 local], or YYYY-MM-DD) selecting the runs created in that window. --type is the only narrowing mechanism and takes one kind: pipeline, version, run, step, field or artifact; an unknown kind is an error. Ranking: an exact name match (3) beats a name substring (2) beats a body-text match (1); equal tiers break by recency, then a stable kind/target tie-break, and every hit reports score, matched_field and recency so a client can re-rank. Bounds: the result cap defaults to 50 and clamps to 200 (0 = default); every kind scans at most 2000 rows and field-text scanning is additionally capped at 2000 steps / 1 MiB; Truncated plus scanned_steps, scanned_bytes and scan_bound_bytes report when a bound applied. Targets are stable: pipeline:<id>, pipeline:<id>:v<version>, run:<id>, run:<id>:<sequence>, run:<id>:<sequence>:<field>, or an artifact URL.", + "version": "Identify the local CLI binary without connecting to a server. --version is an alias. Use ping --json to identify the running server separately.", + "ping": "Pong includes the running server's compiled build.version, build.commit and build.dirty (null when unknown). This identifies the server, not the client or runtime workspace. Older servers omit build. The client compares its own build/protocol against the server: a same-protocol build mismatch prints one warning to stderr, a server speaking a newer protocol fails the command, and a response carrying a field this client does not model prints a one-time unrecognized-field notice instead of dropping it silently.", + "connection": "Prints the server endpoint and phone notification endpoints. Like ping, it reports the client/server build relationship and warns or fails rather than silently proceeding against a mismatched server.", + "status": "Reports server, host and ntfy publishing health. The client compares its build/protocol against the server and warns once (or fails on a newer protocol) rather than proceeding silently.", + "export": "Without a name, select pipelines with Space and save a ZIP in the current directory. --output writes a portable definition bundle; --all skips selection. One name without --output prints Markdown as before. No runs, credentials or old versions are exported.", + "import": "Select definitions from a ZIP. Identical existing definitions are skipped. For differing names, choose keep existing, keep both under a new name, or use imported as a new version (history retained). --on-conflict supplies an explicit automation policy; --yes only approves final publication. Schedules may activate; no run history is imported.", + "new": "Create an optional template copy or a one-stage starter. Opens the configured default harness; --harness opencode|pi|nvim overrides it. No submission is performed.", + "edit": "Omit the name to open fuzzy selection. Original immutable source is copied to a private draft. The configured harness edits that draft, never the live definition.", + "validate": "Uses the same server parser as submission. Does not persist or activate schedules. Structural validity does not prove runtime/tool availability.", + "submit": "Confirm publication at the prompt, or use --yes for explicit automation; schedules activate on acceptance. Managed drafts provide the name and base-version guard. --force never bypasses validation.", + "run": "Runs in your current directory after action confirmation (--yes skips it). Select a pipeline and compose a multiline task with Ctrl+S. Stage tools are enabled automatically; no file list or typed RUN confirmation. Cancellation does not undo edits. --title sets an explicit run title; without it the title is derived from the prompt. --model PROVIDER/MODEL overrides the model for every stage of that run; --stage-model STAGE=PROVIDER/MODEL overrides one stage and is repeatable, with a repeated key last-wins. Precedence is per-stage > whole-run > the model pinned in the pipeline version; overrides apply to that run only and never change the pinned definition. An unknown stage key or a malformed provider/model is refused before the run starts, naming the valid stage keys. In JSON mode --wait polls until terminal; --wait-timeout bounds only the client wait after admission and is independent of per-request --timeout. A reached wait limit leaves the run active: inspect read-only with `inspect <run-id> --json`, or reattach with --wait reusing the original --request-key (never an automatic replay or cancel).", + "retry": "Resume a failed stage by default, restoring the completed prefix handoff; use --mode=restart to start at entry. --stage selects a recorded stage (use --visit for repeated stages). --input-file accepts a bounded JSON object, merged with replacement at the selected stage; '-' reads stdin. Original history and pinned identity are retained; --version selects an explicit published version of the same pipeline only. --wait-timeout leaves an active retry for later read-only inspection or reattachment with the original --request-key, rather than starting a duplicate retry.", + "title": "Rename a run's operator-facing title after the fact without touching its input, steps or execution. The title is collapsed to a single bounded line and recorded as explicitly set. `pipelines runs` and `pipelines inspect` then show it.", + "wait": "Block until each named run reaches a terminal status - succeeded, failed, canceled or interrupted, and also through a run waiting on a person - then print one line per run with its id, status and outcome, its stage and how long it took, and exit 0 even for a run that finished unsuccessfully. Several ids wait together in one command. The exit is non-zero only for a client-side failure: an unknown run id, a connection that could not be established, or --timeout expiring while a run is still active; a timeout says so plainly, never cancels, retries or otherwise touches a run, and is never reported as its outcome. Read-only: it needs no workspace, is not refused by the workspace guard and writes nothing. --json prints the final run envelope instead of the line (one envelope for one id, an array for several).", + "decide": "Choose an option for a run waiting on a human stage. The run must be `waiting`; the option must be one of that stage's non-error edges. The stage is inferred from the run's current stage unless named. The choice and optional --feedback are recorded as the step's evidence (visible in inspect and the TUI) and the run resumes along the chosen edge. An unknown option is refused with the valid list; a repeated decision for an already-decided visit does not double-apply. Works across a server restart because the wait is persisted.", + "debug": "Open a fresh configured harness conversation to investigate a run using inspect/show/runs CLI queries. --harness opencode|pi|nvim chooses another configured harness or read-only nvim packet. Does not resume implementation or retry. --json returns the prompt without launching anything.", + "rpc": "Accepts a JSON protocol envelope; '-' reads stdin. Every TUI server operation is available here too. Mutating commands are not dry runs. The omni-search command is `search` (fields query, optional kind and limit) and answers with ranked `search_results`.", + "chat-adopt": "Adopt a session that already exists on the configured OpenCode endpoint so it appears in chat_sessions and can be continued. The id must be one the endpoint reports; its directory and title are taken from the endpoint's own record (never guessed), and a missing id records nothing. Adopting is always explicit and does not change the server's endpoint choice.", + "retention": "Local, explicit run-history retention; never automatic and never a side effect of another command. `report` is strictly read-only and prints what a policy would remove; `apply` removes it after approval (--yes for scripted use). Policy: --older-than DURATION selects runs created before now minus that rolling UTC duration (Go durations plus d/w; not a calendar boundary), --keep N keeps the newest N runs by run id and selects older ranks (--keep 0 keeps none). Both together are the conservative intersection; at least one is required. Protected and never removed: queued/running runs, runs waiting on a person (waiting run or step), runs with an active cleanup agent, and any run referenced by a retained run's retry provenance (transitively). A run that becomes protected before deletion aborts the whole apply. Artifacts at <root>/<run> are removed with their run and kept with protected/retained runs; the database is VACUUMed and its WAL checkpointed, with measured reclaimed bytes. Definitions and versions are immutable and never touched. --db must name an existing database (default: the standard data directory); --artifacts defaults to artifacts/ beside --db. Works without a running server.", + "search": "One read-only query over pipelines, versions, runs, steps, field text and artifacts. QUERY is free text, a bare status word (queued, running, succeeded, failed, canceled, interrupted) selecting those runs, or a lone date expression (today, yesterday, this week [Monday 00:00 local], or YYYY-MM-DD) selecting the runs created in that window. --type is the only narrowing mechanism and takes one kind: pipeline, version, run, step, field or artifact; an unknown kind is an error. Ranking: an exact name match (3) beats a name substring (2) beats a body-text match (1); equal tiers break by recency, then a stable kind/target tie-break, and every hit reports score, matched_field and recency so a client can re-rank. Bounds: the result cap defaults to 50 and clamps to 200 (0 = default); every kind scans at most 2000 rows and field-text scanning is additionally capped at 2000 steps / 1 MiB; Truncated plus scanned_steps, scanned_bytes and scan_bound_bytes report when a bound applied. Targets are stable: pipeline:<id>, pipeline:<id>:v<version>, run:<id>, run:<id>:<sequence>, run:<id>:<sequence>:<field>, or an artifact URL.", + "resource-pool-configure": "Read a bounded full pool declaration from JSON and replace the declaration only after explicit --yes consent. Use '-' for stdin. This is local operator transport only; it never accepts or returns capabilities or credentials. resource-pool-inspect reads the declared members and status.", } var serviceHelp = map[string]string{ diff --git a/internal/pipeline/resources.go b/internal/pipeline/resources.go index 61a1f3a6..d9f6ce1a 100644 --- a/internal/pipeline/resources.go +++ b/internal/pipeline/resources.go @@ -250,6 +250,21 @@ func (s *Store) ConfigureResourcePool(ctx context.Context, pool protocol.Resourc return err } +// InspectResourcePool returns the declared pool, including only bounded, +// non-secret member metadata and status. +func (s *Store) InspectResourcePool(ctx context.Context, name string) (protocol.ResourcePool, error) { + if !ValidResourceKey(name) { + return protocol.ResourcePool{}, fmt.Errorf("invalid resource pool") + } + s.resourceMu.Lock() + defer s.resourceMu.Unlock() + members, err := s.resourcePool(ctx, s.db, name) + if err != nil { + return protocol.ResourcePool{}, err + } + return protocol.ResourcePool{Name: name, Members: members}, nil +} + func (s *Store) resourcePool(ctx context.Context, tx interface { QueryRowContext(context.Context, string, ...any) *sql.Row }, name string) ([]protocol.ResourcePoolMember, error) { diff --git a/internal/protocol/message.go b/internal/protocol/message.go index b5359fbb..eb7a9ac0 100644 --- a/internal/protocol/message.go +++ b/internal/protocol/message.go @@ -20,6 +20,10 @@ const ( ResourceInspectionResult = "resource_inspection" ReconcileResource = "reconcile_resource" ResourceReconciled = "resource_reconciled" + InspectResourcePool = "inspect_resource_pool" + ResourcePoolInspection = "resource_pool_inspection" + ConfigureResourcePool = "configure_resource_pool" + ResourcePoolConfigured = "resource_pool_configured" ) type Stage struct { @@ -212,4 +216,5 @@ type Message struct { Resource string `json:"resource,omitempty"` ResourceInspection *ResourceInspection `json:"resource_inspection,omitempty"` ResourceRecovery *ResourceRecoveryRequest `json:"resource_recovery,omitempty"` + ResourcePool *ResourcePool `json:"resource_pool,omitempty"` } diff --git a/internal/protocol/resources.go b/internal/protocol/resources.go index 8f31867e..704d015d 100644 --- a/internal/protocol/resources.go +++ b/internal/protocol/resources.go @@ -5,6 +5,7 @@ type ResourceInspection struct { Owner *ResourceOwner `json:"owner,omitempty"` Waiters []ResourceWaiter `json:"waiters,omitempty"` Audits []ResourceRecoveryAudit `json:"audits,omitempty"` + Pool *ResourcePool `json:"pool,omitempty"` } // ResourceTarget is bounded, non-secret metadata for the concrete member that diff --git a/internal/server/resource_cli_test.go b/internal/server/resource_cli_test.go index 7147a7f4..e8a593c7 100644 --- a/internal/server/resource_cli_test.go +++ b/internal/server/resource_cli_test.go @@ -365,6 +365,92 @@ func TestResourceOperatorTransportRejectsForgedForwardedPeer(t *testing.T) { } } +func TestResourcePoolOperatorCLIConfigureReadbackAndPreserveLease(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + dbPath := filepath.Join(t.TempDir(), "pool-operator.db") + store, err := pipeline.Open(dbPath) + if err != nil { + t.Fatal(err) + } + defer store.Close() + if err := store.ConfigureResourcePool(ctx, protocol.ResourcePool{Name: "targets", Members: []protocol.ResourcePoolMember{{Member: "phone", Enabled: true, Metadata: map[string]string{"serial": "old"}}}}); err != nil { + t.Fatal(err) + } + if _, err := store.Submit(ctx, "pool-owner", pipeline.Template([]string{"work"}, true)+"```resources\n{\"pool\":\"targets\"}\n```\n"); err != nil { + t.Fatal(err) + } + items, err := store.List(ctx) + if err != nil || len(items) != 1 { + t.Fatal("pool pipeline missing", err) + } + detail, err := store.Get(ctx, items[0].ID, 1) + if err != nil { + t.Fatal(err) + } + runID, _, err := store.CreateRun(ctx, detail, "pool-owner", t.TempDir(), false, nil, nil) + if err != nil { + t.Fatal(err) + } + lease, ready, _, err := store.RequestResourcePool(ctx, "targets", pipeline.ResourceVisit{Resource: "targets", RunID: runID, Stage: "work", Sequence: 1}) + if err != nil || ready != nil { + t.Fatalf("pool lease: ready=%v err=%v", ready, err) + } + + handler := Handler(ctx, store) + srv := httptest.NewServer(handler) + defer srv.Close() + endpoint := "ws" + strings.TrimPrefix(srv.URL, "http") + "/connect" + normal, err := client.Open(ctx, endpoint) + if err != nil { + t.Fatal(err) + } + if _, err := normal.Call(ctx, protocol.Message{Type: protocol.ConfigureResourcePool, ResourcePool: &protocol.ResourcePool{Name: "targets", Members: []protocol.ResourcePoolMember{{Member: "phone", Enabled: true}}}}); err == nil || !strings.Contains(err.Error(), "operator transport") { + t.Fatalf("remote configuration was accepted: %v", err) + } + normal.Close() + + declaration := filepath.Join(t.TempDir(), "pool.json") + if err := os.WriteFile(declaration, []byte(`{"name":"targets","members":[{"member":"phone","enabled":true,"metadata":{"serial":"new"}},{"member":"emulator","enabled":false}]}`), 0600); err != nil { + t.Fatal(err) + } + var out bytes.Buffer + app := cli.App{Out: &out, Err: &out, In: strings.NewReader("")} + if err := app.Run(ctx, []string{"--url", "ws" + strings.TrimPrefix(srv.URL, "http"), "resource-pool-configure", declaration, "--yes", "--json"}); err != nil { + t.Fatalf("configure CLI: %v\n%s", err, out.String()) + } + out.Reset() + app = cli.App{Out: &out, Err: &out, In: strings.NewReader("")} + if err := app.Run(ctx, []string{"--url", "ws" + strings.TrimPrefix(srv.URL, "http"), "resource-pool-inspect", "targets", "--json"}); err != nil { + t.Fatalf("inspect CLI: %v\n%s", err, out.String()) + } + var pool protocol.ResourcePool + if err := json.Unmarshal(out.Bytes(), &pool); err != nil || len(pool.Members) != 2 || pool.Members[0].Metadata["serial"] != "new" { + t.Fatalf("bad pool readback: %s (%v)", out.String(), err) + } + inspection, err := store.InspectResource(ctx, "phone") + if err != nil || inspection.Owner == nil || inspection.Owner.RunID != runID { + t.Fatalf("configuration released active lease: %#v (%v)", inspection, err) + } + if err := store.SetResourceCleanup(ctx, lease, true); err != nil { + t.Fatal(err) + } + if err := store.ReleaseResource(ctx, lease); err != nil { + t.Fatal(err) + } + srv.Close() + store.Close() + reopened, err := pipeline.Open(dbPath) + if err != nil { + t.Fatal(err) + } + defer reopened.Close() + persisted, err := reopened.InspectResourcePool(ctx, "targets") + if err != nil || persisted.Members[0].Metadata["serial"] != "new" { + t.Fatalf("pool did not survive reopen: %#v (%v)", persisted, err) + } +} + func TestResourceAndroidDraftPublishedV3Provenance(t *testing.T) { baseline, err := os.ReadFile("testdata/android-v3-published.md") if err != nil { diff --git a/internal/server/websocket.go b/internal/server/websocket.go index 9003a433..0235281b 100644 --- a/internal/server/websocket.go +++ b/internal/server/websocket.go @@ -270,7 +270,7 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin response.Error = err.Error() } } - } else if (request.Type == protocol.InspectResource || request.Type == protocol.ReconcileResource) && len(stores) > 0 { + } else if (request.Type == protocol.InspectResource || request.Type == protocol.ReconcileResource || request.Type == protocol.InspectResourcePool || request.Type == protocol.ConfigureResourcePool) && len(stores) > 0 { var err error response.Type = protocol.ResourceInspectionResult // Recovery has a separate transport path. A stage/model client may @@ -280,6 +280,18 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin if r.URL.Path != "/operator-connect" || !isLoopbackPeer(r.RemoteAddr) { response.Type = protocol.Error response.Error = "resource recovery requires the local operator transport" + } else if request.Type == protocol.InspectResourcePool { + response.Type = protocol.ResourcePoolInspection + var pool protocol.ResourcePool + pool, err = stores[0].InspectResourcePool(ctx, request.Resource) + response.ResourcePool = &pool + } else if request.Type == protocol.ConfigureResourcePool { + response.Type = protocol.ResourcePoolConfigured + if request.ResourcePool == nil { + err = fmt.Errorf("resource pool declaration is required") + } else { + err = stores[0].ConfigureResourcePool(ctx, *request.ResourcePool) + } } else if request.Type == protocol.InspectResource { var inspection protocol.ResourceInspection inspection, err = stores[0].InspectResource(ctx, request.Resource) From 80a1d1e0ea3d824995ffd15436d0a4308fb3b56f Mon Sep 17 00:00:00 2001 From: Alan Silva <alanfortlink@gmail.com> Date: Sun, 18 Oct 2026 23:43:58 +0100 Subject: [PATCH 10/14] Bound pool declaration reads and document operator commands --- docs/cli.md | 15 +++++++++++++++ internal/cli/app.go | 16 ++++++++++++++-- internal/server/resource_cli_test.go | 16 ++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/docs/cli.md b/docs/cli.md index 996962c5..b1e9c2ac 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -16,6 +16,21 @@ service manager directly. Legacy Python-managed installations require an explici For source development, use `go run ./cmd/pipelines help`; start the native server separately. Set `PIPELINES_URL` or pass `--url` to select another server. +## Local resource pools + +`pipelines resource-pool-inspect NAME --json` reads a declared pool's members, +enabled/quarantined status and non-secret metadata. It does not report lease tokens. +`pipelines resource-pool-configure pool.json --yes --json` replaces the full pool +declaration; use `-` instead of a file to read stdin. Input is limited to 256 KiB. +Without `--yes`, interactive confirmation is required. Both commands require the +loopback operator transport; ordinary remote/stage clients are refused. + +Declarations contain `name` and `members`, each with `member`, `enabled`, optional +`quarantined` and bounded string `metadata`. Existing validation limits the member +count to 32 and metadata to 16 fields per member. Configuration does not release +active owners, discard waiters or prove a device is ready; inspect ownership and +verify readiness before activating a target. Never put credentials in metadata. + ## Dashboard and status `pipelines --url ws://HOST:PORT/connect ping --json` returns `pong` with the diff --git a/internal/cli/app.go b/internal/cli/app.go index 92032422..120b0964 100644 --- a/internal/cli/app.go +++ b/internal/cli/app.go @@ -1002,13 +1002,25 @@ func (a App) Run(ctx context.Context, args []string) error { } var data []byte if file == "-" { - data, err = io.ReadAll(a.In) + input := a.In + if input == nil { + input = os.Stdin + } + data, err = io.ReadAll(io.LimitReader(input, 256*1024+1)) } else { - data, err = os.ReadFile(file) + var input *os.File + input, err = os.Open(file) + if err == nil { + data, err = io.ReadAll(io.LimitReader(input, 256*1024+1)) + _ = input.Close() + } } if err != nil { return err } + if len(data) > 256*1024 { + return fmt.Errorf("resource pool input exceeds 262144 bytes") + } var pool protocol.ResourcePool if err := json.Unmarshal(data, &pool); err != nil { return fmt.Errorf("invalid resource pool JSON: %w", err) diff --git a/internal/server/resource_cli_test.go b/internal/server/resource_cli_test.go index e8a593c7..209be052 100644 --- a/internal/server/resource_cli_test.go +++ b/internal/server/resource_cli_test.go @@ -415,6 +415,22 @@ func TestResourcePoolOperatorCLIConfigureReadbackAndPreserveLease(t *testing.T) t.Fatal(err) } var out bytes.Buffer + for _, fromStdin := range []bool{false, true} { + oversized := strings.Repeat(" ", 256*1024+1) + path := filepath.Join(t.TempDir(), "oversized.json") + if err := os.WriteFile(path, []byte(oversized), 0600); err != nil { + t.Fatal(err) + } + if fromStdin { + path = "-" + } + bounded := cli.App{Out: &out, Err: &out, In: strings.NewReader(oversized)} + err := bounded.Run(ctx, []string{"--url", endpoint, "resource-pool-configure", path, "--yes", "--json"}) + if err == nil || !strings.Contains(err.Error(), "exceeds 262144 bytes") { + t.Fatalf("oversized pool input (stdin=%v): %v", fromStdin, err) + } + out.Reset() + } app := cli.App{Out: &out, Err: &out, In: strings.NewReader("")} if err := app.Run(ctx, []string{"--url", "ws" + strings.TrimPrefix(srv.URL, "http"), "resource-pool-configure", declaration, "--yes", "--json"}); err != nil { t.Fatalf("configure CLI: %v\n%s", err, out.String()) From 80f8e2edc4a2a0cc95d54c892dffce6fae61dbae Mon Sep 17 00:00:00 2001 From: Alan Silva <alanfortlink@gmail.com> Date: Sun, 18 Oct 2026 23:43:58 +0100 Subject: [PATCH 11/14] Wait for released workspace cleanup in retry isolation test --- internal/server/concurrency_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/server/concurrency_test.go b/internal/server/concurrency_test.go index 81aa2236..a60bf7e7 100644 --- a/internal/server/concurrency_test.go +++ b/internal/server/concurrency_test.go @@ -370,6 +370,9 @@ func TestRetryConflictsOnlyWithSameWorkspaceActiveRun(t *testing.T) { t.Fatalf("retry did not conflict with same-workspace active run: %v", err) } close(releaseA) + // Execution returning does not instantly remove the workspace reservation. + // Finish A's cleanup before asserting that only the unrelated B is active. + r.wg.Wait() // A different active workspace must not block the retry of the original. if _, err := r.start(protocol.Message{PipelineID: pipelineID, Version: 1, RequestKey: "active-b", Workspace: b, Input: map[string]any{"prompt": "task"}}); err != nil { t.Fatal(err) From 461b13841331a50cbdb935f6d38dc62e3cf92078 Mon Sep 17 00:00:00 2001 From: Alan Silva <alanfortlink@gmail.com> Date: Sun, 18 Oct 2026 23:57:28 +0100 Subject: [PATCH 12/14] fix retry-of-retry memory prefix reconstruction --- docs/notes/retry-transitive-memory-prefix.md | 6 + internal/server/retry_test.go | 90 ++++++++--- internal/server/runner.go | 153 +++++++++++++++---- 3 files changed, 202 insertions(+), 47 deletions(-) create mode 100644 docs/notes/retry-transitive-memory-prefix.md diff --git a/docs/notes/retry-transitive-memory-prefix.md b/docs/notes/retry-transitive-memory-prefix.md new file mode 100644 index 00000000..ec03aa5f --- /dev/null +++ b/docs/notes/retry-transitive-memory-prefix.md @@ -0,0 +1,6 @@ +# Transitive retry memory prefixes + +On 2026-10-18, resume checkpoints retain prefix provenance across selected-stage +retries. A retry-of-retry restores the original completed memory prefix from +the immutable checkpoint, then applies the new selected-stage override once; +the source attempt's steps remain evidence and are never copied or rerun. diff --git a/internal/server/retry_test.go b/internal/server/retry_test.go index c92b7470..3d79613b 100644 --- a/internal/server/retry_test.go +++ b/internal/server/retry_test.go @@ -317,13 +317,19 @@ func TestRetryResumeReusesPrefixHandoffAndReplaysRequestAfterReopen(t *testing.T if bCalls == 2 { wantTask = "corrected" } + if bCalls == 3 { + wantTask = "corrected-again" + } if input["task"] != wantTask { t.Errorf("selected retry memory input = %#v, want task %q", input["task"], wantTask) } if bCalls == 2 && input["correction"] != "use-new-plan" { t.Errorf("selected retry lost input override: %#v", input) } - if bCalls == 1 { + if bCalls == 3 && input["correction"] != "use-new-plan-again" { + t.Errorf("transitive selected retry lost input override: %#v", input) + } + if bCalls <= 2 { return stage.Result{}, fmt.Errorf("deliberate failure") } return stage.Result{Outcome: "success", Output: stage.Values{"handoff": "from-b"}}, nil @@ -353,17 +359,29 @@ func TestRetryResumeReusesPrefixHandoffAndReplaysRequestAfterReopen(t *testing.T } r.wg.Wait() retried, err = store.GetRun(ctx, retried.ID) - if err != nil || retried.Status != "succeeded" || retried.RetryMode != "resume" || retried.RetrySource != original.ID { - t.Fatalf("resume did not complete with provenance: %+v %v", retried, err) + if err != nil || retried.Status != "failed" || retried.RetryMode != "resume" || retried.RetrySource != original.ID { + t.Fatalf("first resume did not preserve failed provenance: %+v %v", retried, err) } - if aCalls != 1 || bCalls != 2 || cCalls != 1 { + transitive, err := r.retry(protocol.Message{RunID: retried.ID, RequestKey: "transitive-resume-key", RetryStage: "b", RetryInput: map[string]any{"task": "corrected-again", "correction": "use-new-plan-again"}}) + if err != nil { + t.Fatal(err) + } + r.wg.Wait() + transitive, err = store.GetRun(ctx, transitive.ID) + if err != nil || transitive.Status != "succeeded" || transitive.RetryMode != "resume" || transitive.RetrySource != retried.ID { + t.Fatalf("transitive resume did not complete with provenance: %+v %v", transitive, err) + } + if aCalls != 1 || bCalls != 3 || cCalls != 1 { t.Fatalf("retry duplicated prefix or suffix: A=%d B=%d C=%d", aCalls, bCalls, cCalls) } - if len(retried.Steps) != 2 || retried.Steps[0].Stage != "b" || retried.Steps[1].Stage != "c" { - t.Fatalf("retry persisted the wrong steps: %+v", retried.Steps) + if len(transitive.Steps) != 2 || transitive.Steps[0].Stage != "b" || transitive.Steps[1].Stage != "c" { + t.Fatalf("retry persisted the wrong steps: %+v", transitive.Steps) } - if retried.Steps[0].Input["task"] != "corrected" || retried.Steps[0].Input["correction"] != "use-new-plan" { - t.Fatalf("retry persisted the wrong selected input: %#v", retried.Steps[0].Input) + if transitive.Steps[0].Input["task"] != "corrected-again" || transitive.Steps[0].Input["correction"] != "use-new-plan-again" { + t.Fatalf("retry persisted the wrong selected input: %#v", transitive.Steps[0].Input) + } + if transitive.Steps[0].Control == nil || transitive.Steps[0].Control.Memory["handoff"].Sequence != 1 { + t.Fatalf("transitive retry lost prefix memory provenance: %#v", transitive.Steps[0].Control) } if err := store.Close(); err != nil { t.Fatal(err) @@ -377,8 +395,8 @@ func TestRetryResumeReusesPrefixHandoffAndReplaysRequestAfterReopen(t *testing.T if err != nil { t.Fatal(err) } - replayed, err := r2.retry(protocol.Message{RunID: original.ID, RequestKey: "resume-key"}) - if err != nil || replayed.ID != retried.ID { + replayed, err := r2.retry(protocol.Message{RunID: retried.ID, RequestKey: "transitive-resume-key"}) + if err != nil || replayed.ID != transitive.ID { t.Fatalf("persisted request-key replay lost its run: %+v %v", replayed, err) } } @@ -399,8 +417,9 @@ case "$3" in */prompt) echo '{}' ;; */wait) echo '{}' ;; */message*) - case "$3" in + case "$3" in */ses_native_2/*) echo '{"data":[{"finish":"stop","content":[{"type":"text","text":"{\\"outcome\\":\\"invalid\\",\\"output\\":{\\"wrong\\":\\"format\\"}}"}]}]}' ;; + */ses_native_3/*) echo '{}' ;; */ses_native_3/*) echo '{"data":[{"finish":"stop","content":[{"type":"text","text":"{\\"outcome\\":\\"success\\",\\"output\\":{\\"handoff\\":\\"from-b\\"}}"}]}]}' ;; *) echo '{"data":[{"finish":"stop","content":[{"type":"text","text":"{\\"outcome\\":\\"success\\",\\"output\\":{\\"handoff\\":\\"from-a\\",\\"summary\\":\\"done\\"}}"}]}]}' ;; esac ;; @@ -417,6 +436,7 @@ esac } defer store.Close() source := "# stage a\n\n```output\n{ handoff, summary }\n```\n\n```prompt\nDo A.\n```\n\n```tools\nread\nglob\ngrep\nshell\n```\n\n```edges\nsuccess -> b\nerror -> #failure\n```\n\n```configs\ntype=model\nprovider=opencode-go\nmodel=deepseek-v4.1-flash\nretries=0\n```\n\n# stage b\n\n```output\n{ handoff }\n```\n\n```prompt\nDo B.\n```\n\n```tools\nread\nglob\ngrep\nshell\n```\n\n```edges\nsuccess -> c\nerror -> #failure\n```\n\n```configs\ntype=model\nprovider=opencode-go\nmodel=deepseek-v4.1-flash\nretries=0\n```\n\n# stage c\n\n```output\n{ handoff, summary }\n```\n\n```prompt\nDo C.\n```\n\n```tools\nread\nglob\ngrep\nshell\n```\n\n```edges\nsuccess -> #success\nerror -> #failure\n```\n\n```configs\ntype=model\nprovider=opencode-go\nmodel=deepseek-v4.1-flash\nretries=0\n```\n" + source = strings.Replace(source, "# stage b\n\n```output", "# stage b\n\n```memory\n{\"task\":\"input.prompt\",\"handoff\":\"a.handoff\"}\n```\n\n```output", 1) if _, err := store.Submit(ctx, "native-resume", source); err != nil { t.Fatal(err) } @@ -444,20 +464,48 @@ esac if err == nil || first.RunDetail == nil || first.RunDetail.Status != "failed" || len(first.RunDetail.Steps) != 2 { t.Fatalf("initial native run did not fail at B: %v %#v", err, first.RunDetail) } - retried, err := runCLI("retry", fmt.Sprint(first.RunDetail.ID), "--yes", "--json", "--wait", "--timeout", "10s", "--request-key", "native-resume-key", "--mode", "resume") - if err != nil || retried.RunDetail == nil || retried.RunDetail.Status != "succeeded" { - t.Fatalf("native resume failed: %v %#v", err, retried.RunDetail) + firstInput := filepath.Join(dir, "first-input.json") + if err := os.WriteFile(firstInput, []byte(`{"task":"corrected-first","correction":"first"}`), 0600); err != nil { + t.Fatal(err) } - if retried.RunDetail.RetryOf != first.RunDetail.ID || len(retried.RunDetail.Steps) != 2 || retried.RunDetail.Steps[0].Stage != "b" || retried.RunDetail.Steps[1].Stage != "c" { + retried, err := runCLI("retry", fmt.Sprint(first.RunDetail.ID), "--yes", "--json", "--wait", "--timeout", "10s", "--request-key", "native-resume-key", "--mode", "resume", "--stage", "b", "--input-file", firstInput) + if err == nil || retried.RunDetail == nil || retried.RunDetail.Status != "failed" { + t.Fatalf("first selected native resume did not fail at B: %v %#v", err, retried.RunDetail) + } + if retried.RunDetail.RetryOf != first.RunDetail.ID || len(retried.RunDetail.Steps) != 1 || retried.RunDetail.Steps[0].Stage != "b" { t.Fatalf("native resume lost provenance or replayed prefix: %#v", retried.RunDetail) } - sessions, err := os.ReadFile(filepath.Join(dir, "sessions")) - if err != nil || strings.TrimSpace(string(sessions)) != "4" { - t.Fatalf("native stage execution count = %q, want 4 (A once, B twice, C once): %v", sessions, err) + secondInput := filepath.Join(dir, "second-input.json") + if err := os.WriteFile(secondInput, []byte(`{"task":"corrected-second","correction":"second"}`), 0600); err != nil { + t.Fatal(err) } - again, err := runCLI("retry", fmt.Sprint(first.RunDetail.ID), "--yes", "--json", "--request-key", "native-resume-key") - if err != nil || again.RunDetail == nil || again.RunDetail.ID != retried.RunDetail.ID { - t.Fatalf("same-key replay was not idempotent: %v %#v", err, again.RunDetail) + transitive, err := runCLI("retry", fmt.Sprint(retried.RunDetail.ID), "--yes", "--json", "--wait", "--timeout", "10s", "--request-key", "native-transitive-key", "--mode", "resume", "--stage", "b", "--input-file", secondInput) + if err != nil || transitive.RunDetail == nil || transitive.RunDetail.Status != "succeeded" { + t.Fatalf("transitive selected native resume failed: %v %#v", err, transitive.RunDetail) + } + if transitive.RunDetail.RetryOf != retried.RunDetail.ID || len(transitive.RunDetail.Steps) != 2 || transitive.RunDetail.Steps[0].Stage != "b" || transitive.RunDetail.Steps[1].Stage != "c" { + t.Fatalf("transitive native resume lost provenance or replayed prefix: %#v", transitive.RunDetail) + } + if transitive.RunDetail.Steps[0].Input["task"] != "corrected-second" || transitive.RunDetail.Steps[0].Input["correction"] != "second" { + t.Fatalf("transitive native resume persisted wrong selected input: %#v", transitive.RunDetail.Steps[0].Input) + } + if transitive.RunDetail.Steps[0].Control == nil || transitive.RunDetail.Steps[0].Control.Memory["handoff"].Sequence != 1 { + t.Fatalf("transitive native resume lost prefix provenance: %#v", transitive.RunDetail.Steps[0].Control) + } + sessions, err := os.ReadFile(filepath.Join(dir, "sessions")) + if err != nil || strings.TrimSpace(string(sessions)) != "5" { + t.Fatalf("native stage execution count = %q, want 5 (A once, B three times, C once): %v", sessions, err) + } + if err := store.Close(); err != nil { + t.Fatal(err) + } + store, err = pipeline.Open(filepath.Join(dir, "runs.db")) + if err != nil { + t.Fatal(err) + } + reopened, err := store.GetRun(ctx, transitive.RunDetail.ID) + if err != nil || reopened.Status != "succeeded" || reopened.RetryOf != retried.RunDetail.ID { + t.Fatalf("transitive retry did not survive database reopen: %#v %v", reopened, err) } } diff --git a/internal/server/runner.go b/internal/server/runner.go index 90fb7985..d8c836c6 100644 --- a/internal/server/runner.go +++ b/internal/server/runner.go @@ -420,6 +420,11 @@ type retryCheckpoint struct { handoff map[string]any } +type retryPrefixRecord struct { + stage string + sequence int +} + const maxRetryInputBytes = 64 * 1024 func retryInputOverride(base map[string]any, override map[string]any) (map[string]any, error) { @@ -526,14 +531,16 @@ func (r *runner) retrySourceCheckpointSelected(sourceID int64, pinned *protocol. // stage's memory bindings must be resolved from the original prefix before // this override is applied at invocation time. handoff = map[string]any{"input": failed.Input, "override": override, "sequence": failed.Sequence, "stage": failed.Stage, "visit": failed.Sequence} - prefix := map[string]any{} - for _, step := range source.Steps { - if step.Sequence >= failed.Sequence || step.Status != "completed" { - continue - } - prefix[step.Stage] = step.Output + prefix, prefixRecords, prefixErr := r.retryPrefix(sourceID, source, failed.Sequence) + if prefixErr != nil { + return retryCheckpoint{}, prefixErr } handoff["prefix"] = prefix + serializedPrefix := make([]map[string]any, 0, len(prefixRecords)) + for _, record := range prefixRecords { + serializedPrefix = append(serializedPrefix, map[string]any{"stage": record.stage, "sequence": record.sequence}) + } + handoff["prefix_provenance"] = serializedPrefix // A newer explicitly selected version may continue only when every recorded // prefix stage still exists with the same kind. Its outputs are evidence from // the source attempt and cannot be safely handed to a changed stage contract. @@ -541,20 +548,21 @@ func (r *runner) retrySourceCheckpointSelected(sourceID int64, pinned *protocol. for _, declared := range source.Graph.Stages { sourceStages[declared.Key] = declared } - for _, step := range source.Steps { - if step.Sequence >= failed.Sequence { - break + for _, record := range prefixRecords { + _, found := retryPrefixStep(source, record) + if !found { + continue } var pinnedStage *protocol.Stage for i := range pinned.Graph.Stages { - if pinned.Graph.Stages[i].Key == step.Stage { + if pinned.Graph.Stages[i].Key == record.stage { pinnedStage = &pinned.Graph.Stages[i] break } } - previous, ok := sourceStages[step.Stage] + previous, ok := sourceStages[record.stage] if pinnedStage == nil || !ok || !retryStageContractEqual(previous, *pinnedStage) { - return retryCheckpoint{}, fmt.Errorf("retry source %d has incompatible changed prefix stage %q", sourceID, step.Stage) + return retryCheckpoint{}, fmt.Errorf("retry source %d has incompatible changed prefix stage %q", sourceID, record.stage) } } var sourceSelected protocol.Stage @@ -775,7 +783,7 @@ func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail, r.finishRunState(id, d.Name, workspace, "failed", "error", "retry checkpoint is missing handoff context", 0, 0, "", nil) return } - state := rebuildRetryState(source, checkpoint.failed) + state := rebuildRetryState(source, checkpoint.failed, storedHandoff) if override, ok := storedHandoff["override"].(map[string]any); ok { state.retryOverride = stage.Values(override) } @@ -897,31 +905,124 @@ func rebuildRunState(detail *protocol.RunDetail, decided protocol.RunStep) *runS // rebuildRetryState restores only the completed prefix. The failed visit is // deliberately not counted and its step/evidence is not copied into the new // attempt, so retries cannot duplicate execution or usage aggregates. -func rebuildRetryState(detail *protocol.RunDetail, failed protocol.RunStep) *runState { +func rebuildRetryState(detail *protocol.RunDetail, failed protocol.RunStep, handoff map[string]any) *runState { state := newRunState(failed.Stage, stage.Values(failed.Input)) state.nextSequence = failed.Sequence if original, err := copyValues(stage.Values(detail.Input)); err == nil { state.memory["input"] = memoryValue{output: original} } - for _, step := range detail.Steps { - if step.Sequence >= failed.Sequence { - break - } - state.visits[step.Stage]++ - if step.Status == "completed" { - if values, err := copyValues(stage.Values(step.Output)); err == nil { - state.memory[step.Stage] = memoryValue{output: values, sequence: step.Sequence} + _, hasCheckpointPrefix := handoff["prefix"] + if !hasCheckpointPrefix { + for _, step := range detail.Steps { + if step.Sequence >= failed.Sequence { + break + } + state.visits[step.Stage]++ + if step.Status == "completed" { + if values, err := copyValues(stage.Values(step.Output)); err == nil { + state.memory[step.Stage] = memoryValue{output: values, sequence: step.Sequence} + } + state.completed++ + } + state.recordChanges(step.FileChanges, step.Artifacts) + if step.Status == "completed" { + state.artifactDirs = append(state.artifactDirs, filepath.Join("", strconv.Itoa(step.Sequence))) } - state.completed++ } - state.recordChanges(step.FileChanges, step.Artifacts) - if step.Status == "completed" { - state.artifactDirs = append(state.artifactDirs, filepath.Join("", strconv.Itoa(step.Sequence))) + } + // A selected-stage retry has no persisted rows for its inherited prefix. + // Restore it from the immutable checkpoint instead of guessing from the + // retry source's immediate steps (which is what made retry-of-retry fail + // closed with "memory source ... has not completed"). + if prefix, ok := handoff["prefix"].(map[string]any); ok { + for _, record := range retryPrefixRecords(handoff["prefix_provenance"]) { + output, exists := prefix[record.stage].(map[string]any) + if !exists { + continue + } + values, err := copyValues(stage.Values(output)) + if err != nil { + continue + } + state.memory[record.stage] = memoryValue{output: values, sequence: record.sequence} + state.visits[record.stage]++ + state.completed++ + state.artifactDirs = append(state.artifactDirs, filepath.Join("", strconv.Itoa(record.sequence))) } } return state } +// retryPrefix returns the completed prefix from the source attempt plus the +// immutable prefix carried by that attempt's checkpoint. A retry attempt only +// persists the selected stage onward, so immediate source steps are not a +// complete representation of its memory. +func (r *runner) retryPrefix(sourceID int64, source *protocol.RunDetail, failedSequence int) (map[string]any, []retryPrefixRecord, error) { + prefix := map[string]any{} + records := []retryPrefixRecord{} + if source.RetryMode == "resume" && source.RetrySource > 0 { + checkpointSource, mode, _, inherited, err := r.store.RetryCheckpoint(r.ctx, sourceID) + if err != nil { + return nil, nil, fmt.Errorf("retry source %d has no immutable prefix checkpoint: %w", sourceID, err) + } + if checkpointSource != source.RetrySource || mode != "resume" { + return nil, nil, fmt.Errorf("retry source %d has an invalid prefix checkpoint", sourceID) + } + if values, ok := inherited["prefix"].(map[string]any); ok { + for key, value := range values { + prefix[key] = value + } + } + for _, record := range retryPrefixRecords(inherited["prefix_provenance"]) { + records = append(records, record) + } + } + for _, step := range source.Steps { + if step.Sequence >= failedSequence || step.Status != "completed" { + continue + } + prefix[step.Stage] = step.Output + records = append(records, retryPrefixRecord{stage: step.Stage, sequence: step.Sequence}) + } + return prefix, records, nil +} + +func retryPrefixRecords(raw any) []retryPrefixRecord { + items, ok := raw.([]any) + if !ok { + return nil + } + result := make([]retryPrefixRecord, 0, len(items)) + for _, item := range items { + entry, ok := item.(map[string]any) + if !ok { + continue + } + stageName, ok := entry["stage"].(string) + if !ok || stageName == "" { + continue + } + sequence, ok := entry["sequence"].(float64) + if !ok || sequence < 1 { + continue + } + result = append(result, retryPrefixRecord{stage: stageName, sequence: int(sequence)}) + } + return result +} + +func retryPrefixStep(detail *protocol.RunDetail, record retryPrefixRecord) (protocol.RunStep, bool) { + for _, step := range detail.Steps { + if step.Stage == record.stage && step.Sequence == record.sequence { + return step, true + } + } + // Inherited rows intentionally are not present in a selected retry. The + // contract check still uses the stage key; this marker keeps that check + // independent from model-claimed output. + return protocol.RunStep{Stage: record.stage, Sequence: record.sequence}, true +} + // artifactAccess derives exact visit directories; it never consumes a path from // a definition or model result. An unreadable or symlinked root fails closed. func (r *runner) artifactAccess(runID int64, sequence int, prior []string, current string) stage.ArtifactAccess { From 946055d6df7132021c53aef71d475032ef5d4268 Mon Sep 17 00:00:00 2001 From: Alan Silva <alanfortlink@gmail.com> Date: Sun, 18 Oct 2026 23:58:58 +0100 Subject: [PATCH 13/14] Provide ready attention UI fixture and explicit Android tool path --- android/AGENTS.md | 11 +++ .../server/ready_attention_fixture_test.go | 79 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 internal/server/ready_attention_fixture_test.go diff --git a/android/AGENTS.md b/android/AGENTS.md index 2e035722..c6ae43ed 100644 --- a/android/AGENTS.md +++ b/android/AGENTS.md @@ -20,6 +20,17 @@ time while iterating — the full suite is for the end. ## The dev device +The ADB executable is `/Users/alan/Library/Android/sdk/platform-tools/adb`. +Use that absolute path (or explicitly add its directory to PATH); `adb: command +not found` alone is not evidence that the leased phone is disconnected. + +For populated attention review, `TestExportReadyAttentionReviewFixture` exports a +create-only scratch database when `PIPELINES_ATTENTION_REVIEW_DIR` names a new +directory. Its fixture.json identifies one failed, one finished and one waiting +run and the workspace. Build a scratch server, read back those states, and keep +the owned server alive through all UI interactions. The export itself does not +leave a server running or configure a phone. Never use a dead scratch endpoint. + Only the **UI reviewer**, while its stage holds a declared runner-exclusive resource for the selected phone, may use the device. Implementers and code reviewers do code, builds, unit tests and source review only: **no adb, installs, captures, device-setting diff --git a/internal/server/ready_attention_fixture_test.go b/internal/server/ready_attention_fixture_test.go new file mode 100644 index 00000000..1a003158 --- /dev/null +++ b/internal/server/ready_attention_fixture_test.go @@ -0,0 +1,79 @@ +package server + +import ( + "context" + "encoding/json" + "os" + "path/filepath" + "testing" + + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// Operator-invoked scratch fixture only. No devices or live databases are used. +func TestExportReadyAttentionReviewFixture(t *testing.T) { + dir := os.Getenv("PIPELINES_ATTENTION_REVIEW_DIR") + if dir == "" { + t.Skip("explicit export directory required") + } + if err := os.Mkdir(dir, 0700); err != nil { + t.Fatal(err) + } + workspace := filepath.Join(dir, "workspace") + if err := os.Mkdir(workspace, 0700); err != nil { + t.Fatal(err) + } + ctx := context.Background() + db := filepath.Join(dir, "attention.db") + store, err := pipeline.Open(db) + if err != nil { + t.Fatal(err) + } + defer store.Close() + if _, err := store.Submit(ctx, "Attention fixture", humanPipelineSource); err != nil { + t.Fatal(err) + } + items, err := store.List(ctx) + if err != nil || len(items) != 1 { + t.Fatal("missing fixture definition", err) + } + definition, err := store.Get(ctx, items[0].ID, 1) + if err != nil { + t.Fatal(err) + } + for _, status := range []string{"failed", "succeeded"} { + id, _, err := store.CreateRun(ctx, definition, "fixture-"+status, workspace, false, nil, map[string]any{"prompt": "Controlled " + status + " attention entry"}) + if err != nil { + t.Fatal(err) + } + outcome := "success" + if status == "failed" { + outcome = "error" + } + if err := store.FinishRun(ctx, id, status, outcome, "Controlled fixture", nil); err != nil { + t.Fatal(err) + } + } + r, err := newRunner(ctx, store) + if err != nil { + t.Fatal(err) + } + r.execute = successExecute + waiting, err := r.start(protocol.Message{PipelineID: definition.ID, Version: 1, RequestKey: "fixture-waiting", Workspace: workspace, Input: map[string]any{"prompt": "Controlled waiting attention entry"}}) + if err != nil { + t.Fatal(err) + } + r.wg.Wait() + readback, err := store.GetRun(ctx, waiting.ID) + if err != nil || readback.Status != protocol.RunStatusWaiting || len(readback.Steps) != 1 { + t.Fatalf("waiting fixture invalid: %+v %v", readback, err) + } + data, err := json.MarshalIndent(map[string]any{"database": db, "workspace": workspace, "waiting_run": waiting.ID, "failed_run": 1, "finished_run": 2}, "", " ") + if err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "fixture.json"), data, 0600); err != nil { + t.Fatal(err) + } +} From 51881cb4b3271eb1d48c9024f299ffe5ab3d207d Mon Sep 17 00:00:00 2001 From: Alan Silva <alanfortlink@gmail.com> Date: Mon, 19 Oct 2026 00:00:12 +0100 Subject: [PATCH 14/14] Seed explicit empty partial and complete captures for UI review --- internal/server/ready_attention_fixture_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/internal/server/ready_attention_fixture_test.go b/internal/server/ready_attention_fixture_test.go index 1a003158..35874083 100644 --- a/internal/server/ready_attention_fixture_test.go +++ b/internal/server/ready_attention_fixture_test.go @@ -51,6 +51,16 @@ func TestExportReadyAttentionReviewFixture(t *testing.T) { if status == "failed" { outcome = "error" } + capture := &protocol.StageFileChanges{Status: "complete", Files: []protocol.FileChange{}} + if status == "failed" { + capture.Status, capture.Skipped, capture.Diagnostic = "partial", 2, "Controlled partial fixture: files skipped." + } + if err := store.BeginStep(ctx, id, 1, "gate", map[string]any{"prompt": "Controlled fixture step"}); err != nil { + t.Fatal(err) + } + if err := store.FinishStep(ctx, id, protocol.RunStep{Sequence: 1, Stage: "gate", Status: "completed", Outcome: outcome, FileChanges: capture}); err != nil { + t.Fatal(err) + } if err := store.FinishRun(ctx, id, status, outcome, "Controlled fixture", nil); err != nil { t.Fatal(err) }