fix: retain session failures across recovery surfaces (#3669)

This commit is contained in:
Carlos Florêncio
2026-09-15 09:36:47 +01:00
committed by GitHub
parent 54e2e21f32
commit eeeb6f5586
101 changed files with 4057 additions and 1704 deletions
@@ -1432,6 +1432,27 @@ func (a *messageCreatorAdapter) CreateSessionMessage(ctx context.Context, taskID
return err
}
// CreateSessionMessageIdempotent persists a lifecycle/status message with a
// deterministic ID so a replayed failure event cannot add another transcript
// entry for the same failure stamp.
func (a *messageCreatorAdapter) CreateSessionMessageIdempotent(
ctx context.Context,
messageID, taskID, content, agentSessionID, messageType, turnID string,
metadata map[string]interface{}, requestsInput bool,
) error {
_, err := a.svc.CreateMessageIdempotent(ctx, messageID, &taskservice.CreateMessageRequest{
TaskSessionID: agentSessionID,
TaskID: taskID,
TurnID: turnID,
Content: content,
AuthorType: "agent",
Type: messageType,
Metadata: metadata,
RequestsInput: requestsInput,
})
return err
}
// CreatePermissionRequestMessage creates a message for a permission request
func (a *messageCreatorAdapter) CreatePermissionRequestMessage(ctx context.Context, taskID, sessionID, requestID, pendingID, toolCallID, title, turnID string, options []map[string]interface{}, actionType string, actionDetails map[string]interface{}) (string, error) {
metadata := map[string]interface{}{
@@ -510,6 +510,7 @@ func loadTaskSessionObservations(
}
if lastError, ok := models.LoadLastAgentError(session.Metadata); ok && !lastError.IsDismissed() {
input.ActiveError = &statussummary.ActiveErrorSummary{
Scope: models.ErrorScopeSession,
SessionID: session.ID,
TaskRepositoryID: lastError.TaskRepositoryID,
ExecutionID: lastError.ExecutionID,
@@ -551,10 +552,13 @@ func loadTaskLaunchErrorObservation(
return statussummary.TaskLaunchErrorObservation{
Observed: true,
Error: &statussummary.ActiveErrorSummary{
Scope: models.ErrorScopeTask,
SessionID: errorValue.SessionID,
TaskRepositoryID: errorValue.TaskRepositoryID,
Stamp: errorValue.Stamp(),
OccurredAt: errorValue.OccurredAt,
Preview: errorValue.Message,
Details: errorValue.Details,
Category: errorValue.Code,
RecoveryActions: errorValue.RecoveryActions,
},
@@ -230,6 +230,20 @@ func (a *testMessageCreatorAdapter) CreateSessionMessage(ctx context.Context, ta
return err
}
func (a *testMessageCreatorAdapter) CreateSessionMessageIdempotent(ctx context.Context, messageID, taskID, content, agentSessionID, messageType, turnID string, metadata map[string]interface{}, requestsInput bool) error {
_, err := a.svc.CreateMessageIdempotent(ctx, messageID, &taskservice.CreateMessageRequest{
TaskSessionID: agentSessionID,
TaskID: taskID,
TurnID: turnID,
Content: content,
AuthorType: "agent",
Type: messageType,
Metadata: metadata,
RequestsInput: requestsInput,
})
return err
}
func (a *testMessageCreatorAdapter) CreatePermissionRequestMessage(ctx context.Context, taskID, sessionID, requestID, pendingID, toolCallID, title, turnID string, options []map[string]interface{}, actionType string, actionDetails map[string]interface{}) (string, error) {
metadata := map[string]interface{}{
"request_id": requestID,
@@ -21,6 +21,7 @@ import (
"github.com/kandev/kandev/internal/orchestrator/watcher"
"github.com/kandev/kandev/internal/task/models"
"github.com/kandev/kandev/internal/task/plancomments"
"github.com/kandev/kandev/internal/worktree"
v1 "github.com/kandev/kandev/pkg/api/v1"
)
@@ -2729,12 +2730,11 @@ func (s *Service) handleRecoverableFailureLockedState(ctx context.Context, data
s.completeTurnForSession(ctx, data.SessionID)
s.persistLastAgentError(ctx, data)
// Create a status message with recovery action metadata.
// Skipped for office sessions: the office task page renders its
// own structured RunErrorEntry sourced from the FAILED session,
// and including the legacy ActionMessage would double-show the
// red banner (top-level + inside the embedded chat panel).
if s.messageCreator != nil && !s.isOfficeSession(ctx, data.SessionID) {
// Create a status message with recovery action metadata. Session failures
// are chronological transcript entries for every task surface, including
// Office sessions. Only the current metadata record owns recovery controls;
// the persisted message remains as history after it is retired.
if s.messageCreator != nil {
s.createRecoveryStatusMessage(ctx, data)
}
@@ -2796,17 +2796,12 @@ func (s *Service) handleRecoverableFailureLockedState(ctx context.Context, data
}
func (s *Service) persistLastAgentError(ctx context.Context, data watcher.AgentEventData) error {
errMsg := data.ErrorMessage
if errMsg == "" {
errMsg = defaultAgentFailedMessage
}
errMsg := agentFailureMessage(data)
details := routingerr.Sanitize(data.FailureDetails)
// Keep this metadata until the user dismisses the UI notice locally or a
// later recoverable failure replaces it. A successful turn should not erase
// the investigation breadcrumb that explains why the task was marked REVIEW.
lastErr := models.LastAgentError{
Message: errMsg,
OccurredAt: time.Now().UTC(),
Scope: models.ErrorScopeSession,
AgentExecutionID: data.AgentExecutionID,
ExecutionID: data.AgentExecutionID,
Phase: data.Phase,
@@ -2815,7 +2810,7 @@ func (s *Service) persistLastAgentError(ctx context.Context, data watcher.AgentE
RemediationURL: providerRemediationURL(data),
Code: data.FailureCode,
Details: details,
StampValue: data.ErrorStamp,
StampValue: agentFailureStamp(data),
}
if err := s.repo.SetSessionMetadataKey(ctx, data.SessionID, models.SessionMetaKeyLastAgentError, lastErr); err != nil {
s.logger.Warn("failed to persist last agent error",
@@ -2829,6 +2824,7 @@ func (s *Service) persistLastAgentError(ctx context.Context, data watcher.AgentE
"task_id": data.TaskID,
"session_id": data.SessionID,
"active": true,
"scope": models.ErrorScopeSession,
"message": lastErr.Message,
"occurred_at": lastErr.OccurredAt.Format(time.RFC3339Nano),
"stamp": lastErr.Stamp(),
@@ -2868,39 +2864,160 @@ func (s *Service) persistLastAgentError(ctx context.Context, data watcher.AgentE
return nil
}
// clearRecoveredAgentError drops a session's stored agent failure once the agent
// completes a turn, and publishes the inactive error event so open clients drop
// the red error affordance.
//
// persistLastAgentError deliberately keeps the record across a successful turn
// as an investigation breadcrumb, but nothing ever retired it, so a failure the
// agent recovered from weeks ago still read as live: every path that re-derives
// task status from session metadata (a status-summary rebuild, a backend
// restart, a later session event) put it straight back. The failure also lands
// in the transcript as a recovery message, which is where an investigation
// actually looks, so the metadata copy is not the durable record.
//
// Writes JSON null rather than a delete: LoadLastAgentError already treats that
// as absent, so no new repository surface is needed.
func agentFailureMessage(data watcher.AgentEventData) string {
if strings.TrimSpace(data.ErrorMessage) != "" {
return data.ErrorMessage
}
return defaultAgentFailedMessage
}
func agentFailureStamp(data watcher.AgentEventData) string {
if stamp := strings.TrimSpace(data.ErrorStamp); stamp != "" {
return stamp
}
return models.StableLaunchErrorStamp(
data.TaskID,
data.SessionID,
data.AgentExecutionID,
data.AttemptID,
data.FailureCode,
agentFailureMessage(data),
)
}
// launchFailureIsTaskOwned reports whether the launch producer proved that a
// failure affects the task's shared workspace or workflow. Generic launch
// errors remain session-owned so a profile-specific startup failure cannot
// surface recovery controls on another session.
func launchFailureIsTaskOwned(lastError models.LastAgentError, launchErr error) bool {
if lastError.Scope == models.ErrorScopeTask {
return true
}
switch lastError.Code {
case models.LaunchErrorCategoryBaseBranchMissing,
models.LaunchErrorCategoryDefaultBranchUnresolved,
models.LaunchErrorCategoryWorkspaceCheckoutFailed,
models.LaunchErrorCategoryPRAlreadyClosed:
return true
case models.LaunchErrorCategoryGenericLaunchFailure:
var recoveryErr *worktree.WorktreeRecoveryError
return errors.As(launchErr, &recoveryErr)
default:
return false
}
}
// handleLaunchFailed projects shared workspace failures at task scope while
// retaining profile-specific startup failures on their originating session.
// The executor has already persisted the source session marker when this
// callback runs.
func (s *Service) handleLaunchFailed(
ctx context.Context,
taskID, sessionID, _ string,
launchErr error,
) {
session, err := s.repo.GetTaskSession(ctx, sessionID)
if err != nil || session == nil {
if err != nil {
s.logger.Warn("failed to load launch failure session",
zap.String("task_id", taskID),
zap.String("session_id", sessionID),
zap.Error(err))
}
return
}
lastError, found := models.LoadLastAgentError(session.Metadata)
if !found || lastError.IsDismissed() {
return
}
if !launchFailureIsTaskOwned(lastError, launchErr) {
if err := s.persistBootstrapFailureMessage(ctx, taskID, sessionID, session.AgentExecutionID, lastError); err != nil {
s.logger.Warn("failed to persist session-owned launch failure history",
zap.String("task_id", taskID),
zap.String("session_id", sessionID),
zap.Error(err))
}
return
}
taskError := models.TaskLaunchError{
Message: lastError.Message,
OccurredAt: lastError.OccurredAt,
Scope: models.ErrorScopeTask,
SessionID: sessionID,
Code: lastError.Code,
Details: lastError.Details,
RecoveryActions: lastError.RecoveryActions,
TaskRepositoryID: lastError.TaskRepositoryID,
StampValue: lastError.Stamp(),
}
if !s.persistTaskLaunchError(ctx, taskID, taskError) {
s.logger.Warn("failed to persist task-owned launch error",
zap.String("task_id", taskID),
zap.String("session_id", sessionID),
zap.Error(launchErr))
return
}
s.dismissRecoveredAgentError(ctx, taskID, session, time.Now().UTC())
}
// clearRecoveredAgentError retires the current session error while preserving
// its record for transcript hydration and investigation. The inactive event
// clears only the live projection; a later failure publishes a new active
// stamp and re-arms the controls.
func (s *Service) clearRecoveredAgentError(ctx context.Context, taskID string, session *models.TaskSession) {
s.dismissRecoveredAgentError(ctx, taskID, session, time.Now().UTC())
}
func (s *Service) dismissRecoveredAgentError(
ctx context.Context,
taskID string,
session *models.TaskSession,
dismissedAt time.Time,
) {
if session == nil || session.ID == "" {
return
}
if _, ok := models.LoadLastAgentError(session.Metadata); !ok {
lastErr, ok := models.LoadLastAgentError(session.Metadata)
if !ok || lastErr.IsDismissed() {
return
}
if err := s.repo.SetSessionMetadataKey(
ctx, session.ID, models.SessionMetaKeyLastAgentError, nil,
); err != nil {
s.logger.Warn("failed to clear recovered agent error",
lastErr.DismissedAt = &dismissedAt
recoveryRepo := s.taskLaunchRecoveryRepo
if recoveryRepo == nil {
var ok bool
recoveryRepo, ok = s.repo.(taskLaunchRecoveryRepository)
if !ok {
s.logger.Warn("cannot retire recovered agent error without stamp-CAS repository",
zap.String("task_id", taskID),
zap.String("session_id", session.ID))
return
}
}
stored, err := recoveryRepo.SetSessionMetadataKeyIfStamp(
ctx,
session.ID,
models.SessionMetaKeyLastAgentError,
lastErr.Stamp(),
lastErr,
)
if err != nil {
s.logger.Warn("failed to retire recovered agent error",
zap.String("task_id", taskID),
zap.String("session_id", session.ID),
zap.Error(err))
return
}
if !stored {
// A successor failure won the metadata race. Do not overwrite the
// caller's snapshot or publish an inactive event for the old stamp.
return
}
// Keep the in-memory copy in step: the session-state publish below reads
// its `session_metadata` straight off this object.
delete(session.Metadata, models.SessionMetaKeyLastAgentError)
if session.Metadata == nil {
session.Metadata = make(map[string]interface{})
}
session.Metadata[models.SessionMetaKeyLastAgentError] = lastErr
if s.eventBus == nil {
return
}
@@ -2910,6 +3027,8 @@ func (s *Service) clearRecoveredAgentError(ctx context.Context, taskID string, s
map[string]interface{}{
"task_id": taskID,
"session_id": session.ID,
"scope": models.ErrorScopeSession,
"stamp": lastErr.Stamp(),
"active": false,
},
)); err != nil {
@@ -2942,6 +3061,9 @@ func (s *Service) markRecoveryResolved(ctx context.Context, sessionID string, se
session.Metadata = make(map[string]interface{})
}
session.Metadata[models.SessionMetaKeyRecoveryResolvedAt] = resolvedAtValue
if session.TaskID != "" {
s.dismissRecoveredAgentError(ctx, session.TaskID, session, resolvedAt)
}
return &resolvedAt
}
@@ -2956,13 +3078,16 @@ func providerRemediationURL(data watcher.AgentEventData) string {
return data.ProviderError.RemediationURL
}
// createRecoveryStatusMessage builds and persists the ActionMessage shown
// in the kanban chat surface after a recoverable agent failure. Must only
// be called for non-office sessions (office sessions render their own error UI).
func (s *Service) createRecoveryStatusMessage(ctx context.Context, data watcher.AgentEventData) {
// createRecoveryStatusMessage builds and persists the ActionMessage shown in
// the session transcript after a recoverable agent failure. Its stable message
// identity keeps retries and bootstrap failures idempotent.
func (s *Service) createRecoveryStatusMessage(ctx context.Context, data watcher.AgentEventData) error {
if s.messageCreator == nil {
return fmt.Errorf("recovery status message creator is unavailable")
}
authErr := isAuthError(data.ErrorMessage)
resumeCorrupted := routingerr.IsResumeCorrupted(data.ErrorMessage)
displayMsg := data.ErrorMessage
displayMsg := agentFailureMessage(data)
if authErr {
if readable := extractReadableAuthError(data.ErrorMessage); readable != "" {
displayMsg = readable
@@ -2974,6 +3099,9 @@ func (s *Service) createRecoveryStatusMessage(ctx context.Context, data watcher.
// session instead of dumping the raw 400.
classified := classifyKanbanFailure(data)
statusMsg := fmt.Sprintf("Agent encountered an error: %s", displayMsg)
if data.Phase == models.LaunchErrorPhaseBootstrap {
statusMsg = fmt.Sprintf("Agent startup failed: %s", displayMsg)
}
if resumeCorrupted {
statusMsg = "This agent session can't be resumed — its saved reasoning state is corrupted. Start a fresh session to continue."
} else if routingerr.Decide(routingerr.ContextKanban, classified, time.Now().UTC()) == routingerr.DecisionShortRetry {
@@ -2985,6 +3113,8 @@ func (s *Service) createRecoveryStatusMessage(ctx context.Context, data watcher.
meta := map[string]interface{}{
"variant": "error",
"recovery_actions": true,
"scope": models.ErrorScopeSession,
"error_stamp": agentFailureStamp(data),
"session_id": data.SessionID,
"task_id": data.TaskID,
"has_resume_token": hasResumeToken,
@@ -2998,6 +3128,11 @@ func (s *Service) createRecoveryStatusMessage(ctx context.Context, data watcher.
meta["error_output"] = details
}
}
if data.Phase == models.LaunchErrorPhaseBootstrap {
if details := routingerr.Sanitize(data.FailureDetails); details != "" {
meta["error_output"] = details
}
}
// The validated remediation URL is carried independently of quota
// classification so the generic recoverable card can still show the link.
if remediationURL := providerRemediationURL(data); remediationURL != "" {
@@ -3028,8 +3163,22 @@ func (s *Service) createRecoveryStatusMessage(ctx context.Context, data watcher.
meta["actions"] = buildRecoveryActions(data.TaskID, data.SessionID, hasResumeToken, authErr, resumeCorrupted)
}
if err := s.messageCreator.CreateSessionMessage(
return s.persistRecoveryStatusMessage(ctx, data, statusMsg, meta)
}
func (s *Service) persistRecoveryStatusMessage(
ctx context.Context,
data watcher.AgentEventData,
statusMsg string,
meta map[string]interface{},
) error {
messageID := uuid.NewSHA1(
uuid.NameSpaceOID,
[]byte("session-recovery:"+data.SessionID+":"+agentFailureStamp(data)),
)
if err := s.messageCreator.CreateSessionMessageIdempotent(
ctx,
messageID.String(),
data.TaskID,
statusMsg,
data.SessionID,
@@ -3041,7 +3190,9 @@ func (s *Service) createRecoveryStatusMessage(ctx context.Context, data watcher.
s.logger.Warn("failed to create recovery status message",
zap.String("task_id", data.TaskID),
zap.Error(err))
return err
}
return nil
}
// applyProviderQuotaMetadata promotes only a validated OpenCode terminal
@@ -6,6 +6,7 @@ import (
"testing"
"time"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
@@ -408,6 +409,45 @@ func TestDispatchKanbanAgentErrorTrigger_OfficeAndLoadFailureGuards(t *testing.T
})
}
func TestHandleRecoverableFailurePersistsOfficeSessionHistory(t *testing.T) {
ctx := context.Background()
repo := setupTestRepo(t)
now := time.Now().UTC()
requireNoError(t, repo.CreateWorkspace(ctx, &models.Workspace{ID: "ws1", Name: "Test", CreatedAt: now, UpdatedAt: now}))
requireNoError(t, repo.CreateWorkflow(ctx, &models.Workflow{ID: "wf1", WorkspaceID: "ws1", Name: "WF", CreatedAt: now, UpdatedAt: now}))
requireNoError(t, repo.CreateTask(ctx, &models.Task{
ID: "office-history-task", WorkspaceID: "ws1", WorkflowID: "wf1", WorkflowStepID: "step1",
ProjectID: "proj1", Title: "Office task", State: v1.TaskStateInProgress, CreatedAt: now, UpdatedAt: now,
}))
requireNoError(t, repo.CreateTaskSession(ctx, &models.TaskSession{
ID: "office-history-session", TaskID: "office-history-task", State: models.TaskSessionStateRunning,
StartedAt: now, UpdatedAt: now,
}))
svc, _ := newAgentErrorTestService(t, repo, newMockStepGetter(), nil)
messages := &mockMessageCreator{}
svc.messageCreator = messages
svc.handleRecoverableFailureLocked(ctx, watcher.AgentEventData{
TaskID: "office-history-task",
SessionID: "office-history-session",
AgentExecutionID: "office-history-execution",
ErrorMessage: "The Office agent could not start.",
FailureCode: models.LaunchErrorCategoryGenericLaunchFailure,
Phase: models.LaunchErrorPhaseBootstrap,
AttemptID: "office-history-attempt",
ErrorStamp: "office-history-failure",
})
require.Len(t, messages.sessionMessages, 1, "Office failures must have one chronological session entry")
require.Equal(t, models.ErrorScopeSession, messages.sessionMessages[0].metadata["scope"])
require.Equal(t, "office-history-failure", messages.sessionMessages[0].metadata["error_stamp"])
require.Equal(t, true, messages.sessionMessages[0].metadata["recovery_actions"])
session, err := repo.GetTaskSession(ctx, "office-history-session")
requireNoError(t, err)
require.Equal(t, models.TaskSessionStateFailed, session.State)
}
type agentErrorTaskLoadErrorRepo struct {
sessionExecutorStore
err error
@@ -860,6 +860,14 @@ func TestHandleAgentBootReady_DoesNotTriggerOnTurnComplete(t *testing.T) {
}); err != nil {
t.Fatalf("create session: %v", err)
}
if err := repo.SetSessionMetadataKey(ctx, sessionID, models.SessionMetaKeyLastAgentError, models.LastAgentError{
Message: "saved session failed before recovery",
OccurredAt: now,
Scope: models.ErrorScopeSession,
StampValue: "saved-session-failure",
}); err != nil {
t.Fatalf("set saved session failure: %v", err)
}
taskRepo := newMockTaskRepo()
taskRepo.tasks["task-1"] = &v1.Task{
@@ -915,6 +923,10 @@ func TestHandleAgentBootReady_DoesNotTriggerOnTurnComplete(t *testing.T) {
if _, err := time.Parse(time.RFC3339Nano, resolvedAt); err != nil {
t.Fatalf("recovery resolution timestamp %q is invalid: %v", resolvedAt, err)
}
lastError, ok := models.LoadLastAgentError(finalSess.Metadata)
if !ok || !lastError.IsDismissed() {
t.Fatalf("last agent error = %#v, want dismissed historical error", lastError)
}
})
}
}
@@ -1373,7 +1373,9 @@ func (s *Service) transitionBootstrapFailure(
committer, ok := s.repo.(bootstrapFailureCommitter)
if !ok {
return s.transitionTaskSessionState(ctx, taskID, sessionID, models.TaskSessionStateFailed, errorValue.Message, nil)
return false, expectedState, fmt.Errorf(
"bootstrap failure requires an execution-fenced repository commit",
)
}
changed, updatedAt, err := committer.CommitBootstrapFailureIfCurrentExecution(
ctx,
@@ -1389,11 +1391,12 @@ func (s *Service) transitionBootstrapFailure(
}
refreshed, err := s.repo.GetTaskSession(ctx, sessionID)
if err != nil {
return false, models.TaskSessionStateFailed, fmt.Errorf("get session after bootstrap failure commit: %w", err)
return true, models.TaskSessionStateFailed, fmt.Errorf("get session after bootstrap failure commit: %w", err)
}
if refreshed == nil {
return false, models.TaskSessionStateFailed, fmt.Errorf("get session after bootstrap failure commit: session %q is nil", sessionID)
return true, models.TaskSessionStateFailed, fmt.Errorf("get session after bootstrap failure commit: session %q is nil", sessionID)
}
messageErr := s.persistBootstrapFailureMessage(ctx, taskID, sessionID, agentExecutionID, errorValue)
authoritativeUpdatedAt := updatedAt.UTC()
s.publishAcceptedTaskSessionState(
ctx,
@@ -1405,9 +1408,37 @@ func (s *Service) transitionBootstrapFailure(
&authoritativeUpdatedAt,
refreshed,
)
if messageErr != nil {
return true, models.TaskSessionStateFailed, fmt.Errorf("persist bootstrap failure history: %w", messageErr)
}
return true, models.TaskSessionStateFailed, nil
}
// persistBootstrapFailureMessage records the accepted bootstrap failure as a
// chronological session entry. Its stable message identity makes a retry
// after the state commit idempotent.
func (s *Service) persistBootstrapFailureMessage(
ctx context.Context,
taskID, sessionID, agentExecutionID string,
errorValue models.LastAgentError,
) error {
if s.messageCreator == nil {
return fmt.Errorf("bootstrap failure message creator is unavailable")
}
return s.createRecoveryStatusMessage(ctx, watcher.AgentEventData{
TaskID: taskID,
SessionID: sessionID,
AgentExecutionID: agentExecutionID,
ErrorMessage: errorValue.Message,
FailureCode: errorValue.Code,
FailureDetails: errorValue.Details,
Phase: errorValue.Phase,
AttemptID: errorValue.AttemptID,
ErrorStamp: errorValue.Stamp(),
Causes: errorValue.Causes,
})
}
func (s *Service) publishAcceptedTaskSessionState(
ctx context.Context,
taskID, sessionID string,
@@ -272,6 +272,25 @@ func (m *serviceBackedMessageCreator) CreateSessionMessage(
return err
}
func (m *serviceBackedMessageCreator) CreateSessionMessageIdempotent(
ctx context.Context,
messageID, taskID, content, sessionID, messageType, turnID string,
metadata map[string]interface{},
requestsInput bool,
) error {
_, err := m.svc.CreateMessageIdempotent(ctx, messageID, &taskservice.CreateMessageRequest{
TaskSessionID: sessionID,
TaskID: taskID,
TurnID: turnID,
Content: content,
AuthorType: "agent",
Type: messageType,
Metadata: metadata,
RequestsInput: requestsInput,
})
return err
}
func (m *serviceBackedMessageCreator) UpdateToolCallMessage(
ctx context.Context,
taskID, toolCallID, parentToolCallID, status, result, agentSessionID, title, turnID, msgType string,
@@ -2819,9 +2838,8 @@ func TestAutoStartFailureDoesNotMarkTaskWithActiveSession(t *testing.T) {
require.False(t, marked, "a late failure must not mark a task with active work")
}
// Nothing else ever retires a stored agent failure, so without this clear every
// path that re-derives task status from session metadata resurrects it and the
// error icon never goes away.
// A recovered failure remains available for transcript history, while this
// transition retires it from the live session-error projection.
func TestClearRecoveredAgentErrorOnTurnCompletion(t *testing.T) {
ctx := context.Background()
@@ -2846,11 +2864,12 @@ func TestClearRecoveredAgentErrorOnTurnCompletion(t *testing.T) {
newService := func(repo *sqliterepo.Repository) (*Service, *recordingEventBus) {
eb := &recordingEventBus{}
svc := createTestService(repo, newMockStepGetter(), newMockTaskRepo())
svc.taskLaunchRecoveryRepo = repo
svc.eventBus = eb
return svc, eb
}
t.Run("clears the record and publishes it inactive", func(t *testing.T) {
t.Run("retires the record and publishes it inactive", func(t *testing.T) {
repo := setupTestRepo(t)
seedSession(t, repo, "t1", "s1", "step1")
seedError(t, repo)
@@ -2862,13 +2881,15 @@ func TestClearRecoveredAgentErrorOnTurnCompletion(t *testing.T) {
stored, err := repo.GetTaskSession(ctx, "s1")
require.NoError(t, err)
_, ok := models.LoadLastAgentError(stored.Metadata)
require.False(t, ok, "a recovered failure must not keep driving the error icon")
storedError, ok := models.LoadLastAgentError(stored.Metadata)
require.True(t, ok, "a recovered failure remains available as history")
require.True(t, storedError.IsDismissed(), "a recovered failure must not remain active")
// The session-state publish that follows reads session_metadata straight
// off this object, so a stale copy would re-arm the icon immediately.
_, ok = models.LoadLastAgentError(session.Metadata)
require.False(t, ok, "the in-memory copy must be cleared too")
memoryError, ok := models.LoadLastAgentError(session.Metadata)
require.True(t, ok)
require.True(t, memoryError.IsDismissed(), "the in-memory copy must be retired too")
published := errorEvents(eb)
require.Len(t, published, 1, "clients need one event to drop the icon")
@@ -2876,6 +2897,7 @@ func TestClearRecoveredAgentErrorOnTurnCompletion(t *testing.T) {
require.True(t, ok)
require.Equal(t, false, data["active"])
require.Equal(t, "s1", data["session_id"])
require.Equal(t, models.ErrorScopeSession, data["scope"])
})
t.Run("stays silent when the session has no stored error", func(t *testing.T) {
@@ -2890,6 +2912,206 @@ func TestClearRecoveredAgentErrorOnTurnCompletion(t *testing.T) {
require.Empty(t, errorEvents(eb),
"every later completion must stay silent so turns do not churn the row")
})
t.Run("does not retire a successor failure from a stale snapshot", func(t *testing.T) {
repo := setupTestRepo(t)
seedSession(t, repo, "t1", "s1", "step1")
require.NoError(t, repo.SetSessionMetadataKey(
ctx, "s1", models.SessionMetaKeyLastAgentError,
models.LastAgentError{
Message: "old failure",
OccurredAt: time.Date(2026, 8, 20, 9, 0, 0, 0, time.UTC),
StampValue: "old-stamp",
},
))
svc, eb := newService(repo)
snapshot, err := repo.GetTaskSession(ctx, "s1")
require.NoError(t, err)
require.NoError(t, repo.SetSessionMetadataKey(
ctx, "s1", models.SessionMetaKeyLastAgentError,
models.LastAgentError{
Message: "successor failure",
OccurredAt: time.Date(2026, 8, 20, 10, 0, 0, 0, time.UTC),
StampValue: "successor-stamp",
},
))
require.NotNil(t, svc.markRecoveryResolved(ctx, "s1", snapshot))
stored, err := repo.GetTaskSession(ctx, "s1")
require.NoError(t, err)
storedError, ok := models.LoadLastAgentError(stored.Metadata)
require.True(t, ok)
require.Equal(t, "successor-stamp", storedError.Stamp())
require.False(t, storedError.IsDismissed())
snapshotError, ok := models.LoadLastAgentError(snapshot.Metadata)
require.True(t, ok)
require.Equal(t, "old-stamp", snapshotError.Stamp())
require.False(t, snapshotError.IsDismissed())
require.Empty(t, errorEvents(eb), "a stale retirement must not hide the successor")
})
}
func TestTransitionBootstrapFailurePersistsSessionHistory(t *testing.T) {
ctx := context.Background()
repo := setupTestRepo(t)
seedSession(t, repo, "bootstrap-history-task", "bootstrap-history-session", "step1")
session, err := repo.GetTaskSession(ctx, "bootstrap-history-session")
require.NoError(t, err)
session.State = models.TaskSessionStateStarting
require.NoError(t, repo.UpdateTaskSession(ctx, session))
seedExecutorRunning(t, repo, "bootstrap-history-session", "bootstrap-history-task", "exec-1")
svc := createTestService(repo, newMockStepGetter(), newMockTaskRepo())
svc.taskLaunchRecoveryRepo = repo
svc.messageCreator = newServiceBackedMessageCreator(repo)
startupFailure := &lifecycle.BootstrapFailure{
Operation: models.AgentErrorCauseOperationResume,
Code: models.AgentErrorCauseCodePermissionDenied,
Detail: "The saved session could not be loaded.",
Cause: errors.New("provider timeout with private token=secret"),
}
// The ordinary non-auth startup path returns false so the executor owns the
// terminal admission. The transition below is that accepted boundary.
require.False(t, svc.handleAgentStartFailed(
ctx,
"bootstrap-history-task",
"bootstrap-history-session",
"exec-1",
startupFailure,
false,
))
failure := models.LastAgentError{
Message: "The agent could not start.",
OccurredAt: time.Date(2026, 9, 14, 10, 0, 0, 0, time.UTC),
Scope: models.ErrorScopeSession,
AgentExecutionID: "exec-1",
ExecutionID: "exec-1",
Phase: models.LaunchErrorPhaseBootstrap,
AttemptID: "attempt-1",
Code: models.LaunchErrorCategoryGenericLaunchFailure,
Details: "operation=agent_bootstrap; cause=the provider did not respond",
StampValue: "bootstrap-failure-1",
}
changed, state, err := svc.transitionBootstrapFailure(
ctx,
"bootstrap-history-task",
"bootstrap-history-session",
"exec-1",
models.TaskSessionStateStarting,
"",
failure,
)
require.NoError(t, err)
require.True(t, changed)
require.Equal(t, models.TaskSessionStateFailed, state)
messages, err := repo.ListMessages(ctx, "bootstrap-history-session")
require.NoError(t, err)
require.Len(t, messages, 1, "accepted bootstrap failure must create one transcript entry")
require.Equal(t, models.MessageTypeStatus, messages[0].Type)
require.Equal(t, "bootstrap-failure-1", messages[0].Metadata["error_stamp"])
require.Equal(t, true, messages[0].Metadata["recovery_actions"])
require.Equal(t, failure.Details, messages[0].Metadata["error_output"])
require.Equal(t, "Agent startup failed: The agent could not start.", messages[0].Content)
reloaded, err := repo.GetTaskSession(ctx, "bootstrap-history-session")
require.NoError(t, err)
resolvedAt := svc.markRecoveryResolved(ctx, reloaded.ID, reloaded)
require.NotNil(t, resolvedAt)
afterRecovery, err := repo.GetTaskSession(ctx, "bootstrap-history-session")
require.NoError(t, err)
retainedError, ok := models.LoadLastAgentError(afterRecovery.Metadata)
require.True(t, ok)
require.True(t, retainedError.IsDismissed())
retainedMessages, err := repo.ListMessages(ctx, "bootstrap-history-session")
require.NoError(t, err)
require.Len(t, retainedMessages, 1, "recovery retirement must retain the chronological entry")
}
type failOnceBootstrapMessageCreator struct {
*serviceBackedMessageCreator
err error
}
func (m *failOnceBootstrapMessageCreator) CreateSessionMessageIdempotent(
ctx context.Context,
messageID, taskID, content, sessionID, messageType, turnID string,
metadata map[string]interface{}, requestsInput bool,
) error {
if m.err != nil {
err := m.err
m.err = nil
return err
}
return m.serviceBackedMessageCreator.CreateSessionMessageIdempotent(
ctx,
messageID,
taskID,
content,
sessionID,
messageType,
turnID,
metadata,
requestsInput,
)
}
func TestTransitionBootstrapFailureReturnsRepairableHistoryError(t *testing.T) {
ctx := context.Background()
repo := setupTestRepo(t)
seedSession(t, repo, "bootstrap-repair-task", "bootstrap-repair-session", "step1")
session, err := repo.GetTaskSession(ctx, "bootstrap-repair-session")
require.NoError(t, err)
session.State = models.TaskSessionStateStarting
require.NoError(t, repo.UpdateTaskSession(ctx, session))
seedExecutorRunning(t, repo, "bootstrap-repair-session", "bootstrap-repair-task", "exec-repair")
svc := createTestService(repo, newMockStepGetter(), newMockTaskRepo())
creator := &failOnceBootstrapMessageCreator{
serviceBackedMessageCreator: newServiceBackedMessageCreator(repo),
err: errors.New("transcript temporarily unavailable"),
}
svc.messageCreator = creator
failure := models.LastAgentError{
Message: "The agent could not start.",
OccurredAt: time.Date(2026, 9, 14, 11, 0, 0, 0, time.UTC),
Scope: models.ErrorScopeSession,
AgentExecutionID: "exec-repair",
ExecutionID: "exec-repair",
Phase: models.LaunchErrorPhaseBootstrap,
AttemptID: "exec-repair",
Code: models.LaunchErrorCategoryGenericLaunchFailure,
Details: "operation=agent_bootstrap; cause=provider unavailable",
StampValue: "bootstrap-repair-failure",
}
changed, state, err := svc.transitionBootstrapFailure(
ctx,
"bootstrap-repair-task",
"bootstrap-repair-session",
"exec-repair",
models.TaskSessionStateStarting,
"",
failure,
)
require.Error(t, err)
require.True(t, changed, "the state admission succeeded even though history needed repair")
require.Equal(t, models.TaskSessionStateFailed, state)
messages, err := repo.ListMessages(ctx, "bootstrap-repair-session")
require.NoError(t, err)
require.Empty(t, messages)
require.NoError(t, svc.persistBootstrapFailureMessage(
ctx, "bootstrap-repair-task", "bootstrap-repair-session", "exec-repair", failure,
))
messages, err = repo.ListMessages(ctx, "bootstrap-repair-session")
require.NoError(t, err)
require.Len(t, messages, 1, "the bounded repair must restore the accepted failure entry")
require.Equal(t, "bootstrap-repair-failure", messages[0].Metadata["error_stamp"])
}
func TestSetSessionStartingRejectsTerminalSession(t *testing.T) {
@@ -111,23 +111,6 @@ type sessionMetadataKeyStateSetter interface {
) (bool, error)
}
// sessionMetadataErrorCASWriter is the optional persistence seam for
// execution-scoped bootstrap errors. Production repositories implement both
// operations atomically; legacy test stores can fall back to the ordinary
// metadata setter after the executor's current-execution fence.
type sessionMetadataErrorCASWriter interface {
SetSessionMetadataKeyIfAbsent(
ctx context.Context,
sessionID, key string,
value interface{},
) (bool, error)
SetSessionMetadataKeyIfStamp(
ctx context.Context,
sessionID, key, expectedStamp string,
value interface{},
) (bool, error)
}
// bootstrapFailureCommitter is the atomic repository boundary for an
// asynchronous process-start failure. The expected state and error stamp are
// captured immediately before the commit; the repository must also require
@@ -831,6 +814,16 @@ type BootstrapFailureTransitionFunc func(
errorValue models.LastAgentError,
) (changed bool, finalState models.TaskSessionState, err error)
// BootstrapFailureMessageRepairFunc retries the idempotent transcript write
// after the state admission has already succeeded. The repair is deliberately
// separate from the state commit so a transient message-store failure cannot
// make an accepted bootstrap failure disappear from session history.
type BootstrapFailureMessageRepairFunc func(
ctx context.Context,
taskID, sessionID, agentExecutionID string,
errorValue models.LastAgentError,
) error
// SessionStartingFunc is called when the executor has prepared/resumed an
// execution and needs to mark the session STARTING while preserving other
// session-row updates such as metadata. expectedState is the state observed
@@ -983,6 +976,9 @@ type Executor struct {
// typed error, FAILED state, and corresponding publication as one ownership
// decision.
onBootstrapFailureTransition BootstrapFailureTransitionFunc
// Retry hook for the chronological transcript entry when the state commit
// succeeds but the first idempotent message write fails.
onBootstrapFailureMessageRepair BootstrapFailureMessageRepairFunc
// Callback for STARTING writes that carry full session-row changes. Set by
// the orchestrator so launch/resume/model-switch transitions serialize with
@@ -1304,6 +1300,12 @@ func (e *Executor) SetOnBootstrapFailureTransition(fn BootstrapFailureTransition
e.onBootstrapFailureTransition = fn
}
// SetOnBootstrapFailureMessageRepair wires the bounded repair hook for a
// bootstrap failure whose session admission already succeeded.
func (e *Executor) SetOnBootstrapFailureMessageRepair(fn BootstrapFailureMessageRepairFunc) {
e.onBootstrapFailureMessageRepair = fn
}
// SetOnSessionStarting sets a callback for full session-row STARTING updates.
func (e *Executor) SetOnSessionStarting(fn SessionStartingFunc) {
e.onSessionStarting = fn
@@ -285,6 +285,17 @@ func (e *Executor) handleAgentProcessStartFailure(
zap.String("session_id", sessionID),
zap.String("agent_execution_id", agentExecutionID),
zap.Error(transitionErr))
if changed && finalState == models.TaskSessionStateFailed && e.onBootstrapFailureMessageRepair != nil {
if repairErr := e.onBootstrapFailureMessageRepair(
ctx, taskID, sessionID, agentExecutionID, errorValue,
); repairErr != nil {
e.logger.Warn("failed to repair bootstrap failure history",
zap.String("task_id", taskID),
zap.String("session_id", sessionID),
zap.String("agent_execution_id", agentExecutionID),
zap.Error(repairErr))
}
}
} else if !changed {
// An ownership or compare-and-set miss means a newer execution won the
// race. Do not
@@ -170,6 +170,9 @@ func TestBuildLastAgentErrorSanitizesRepositoryPreparationDetails(t *testing.T)
if !strings.Contains(errorValue.Details, "repo-back") || !strings.Contains(errorValue.Details, "backend") {
t.Fatalf("launch details = %q, want repository identity", errorValue.Details)
}
if errorValue.Phase != models.LaunchErrorPhaseBootstrap {
t.Fatalf("launch phase = %q, want bootstrap", errorValue.Phase)
}
if strings.Contains(errorValue.Details, "ghp_abcdefghijklmnopqrstuvwxyz1234567890AB") ||
strings.Contains(errorValue.Details, "user:") {
t.Fatalf("launch details exposed credential-bearing URL: %q", errorValue.Details)
@@ -235,6 +238,27 @@ func TestBootstrapFailureProjection(t *testing.T) {
exec.SetOnSessionStateChange(func(ctx context.Context, _, sessionID string, state models.TaskSessionState, message string) error {
return repo.UpdateTaskSessionState(ctx, sessionID, state, message)
})
exec.SetOnBootstrapFailureTransition(func(
ctx context.Context,
taskID, sessionID, _ string,
_ models.TaskSessionState,
_ string,
errorValue models.LastAgentError,
) (bool, models.TaskSessionState, error) {
changed, _, err := repo.CommitBootstrapFailureIfCurrentExecution(
ctx,
taskID,
sessionID,
"exec-1",
models.TaskSessionStateStarting,
"",
errorValue,
)
if err != nil || !changed {
return changed, models.TaskSessionStateStarting, err
}
return true, models.TaskSessionStateFailed, nil
})
exec.runAgentProcessAsync(
context.Background(), "task-1", "session-1", "exec-1",
@@ -273,6 +297,54 @@ func TestBootstrapFailureProjection(t *testing.T) {
}
}
func TestBootstrapFailureHistoryRepairRunsAfterAcceptedCommitError(t *testing.T) {
repo := newMockRepository()
repo.sessions["session-repair"] = &models.TaskSession{
ID: "session-repair",
TaskID: "task-repair",
State: models.TaskSessionStateStarting,
AgentExecutionID: "exec-repair",
}
repo.tasks["task-repair"] = &models.Task{ID: "task-repair", State: v1.TaskStateReview}
exec := newTestExecutor(t, &mockAgentManager{}, repo)
var repairCalls int
exec.SetOnBootstrapFailureTransition(func(
context.Context,
string,
string,
string,
models.TaskSessionState,
string,
models.LastAgentError,
) (bool, models.TaskSessionState, error) {
return true, models.TaskSessionStateFailed, errors.New("transcript write failed after admission")
})
exec.SetOnBootstrapFailureMessageRepair(func(
context.Context,
string,
string,
string,
models.LastAgentError,
) error {
repairCalls++
return nil
})
exec.handleAgentProcessStartFailure(
context.Background(),
"task-repair",
"session-repair",
"exec-repair",
errors.New("agent process failed to start"),
false,
false,
)
if repairCalls != 1 {
t.Fatalf("bootstrap history repair calls = %d, want 1", repairCalls)
}
}
func TestBootstrapFailureSuccessorFence(t *testing.T) {
repo := newMockRepository()
successor := models.LastAgentError{
@@ -544,6 +544,45 @@ func (m *mockRepository) UpdateTaskSessionStateIfCurrent(
return true, now, nil
}
// CommitBootstrapFailureIfCurrentExecution mirrors the production admission
// boundary for executor tests. The fake has no separate executor table, so the
// session's execution identity is the equivalent ownership fence.
func (m *mockRepository) CommitBootstrapFailureIfCurrentExecution(
_ context.Context,
taskID, sessionID, agentExecutionID string,
expectedState models.TaskSessionState,
expectedStamp string,
errorValue models.LastAgentError,
) (bool, time.Time, error) {
m.mu.Lock()
defer m.mu.Unlock()
session, ok := m.sessions[sessionID]
if !ok || session == nil || (session.TaskID != "" && session.TaskID != taskID) || session.State != expectedState {
return false, time.Time{}, nil
}
if agentExecutionID != "" && session.AgentExecutionID != "" && session.AgentExecutionID != agentExecutionID {
return false, time.Time{}, nil
}
current, hasCurrent := models.LoadLastAgentError(session.Metadata)
if expectedStamp == "" {
if hasCurrent {
return false, time.Time{}, nil
}
} else if !hasCurrent || current.Stamp() != expectedStamp {
return false, time.Time{}, nil
}
if session.Metadata == nil {
session.Metadata = make(map[string]interface{})
}
session.Metadata[models.SessionMetaKeyLastAgentError] = errorValue
now := time.Now().UTC()
session.State = models.TaskSessionStateFailed
session.ErrorMessage = errorValue.Message
session.CompletedAt = &now
session.UpdatedAt = now
return true, now, nil
}
func (m *mockRepository) UpdateTaskSessionIfCurrentStateRemovingMetadataKeys(
ctx context.Context,
session *models.TaskSession,
@@ -2135,6 +2135,27 @@ func TestRunAgentProcessAsync_CleansUpOnStartFailure(t *testing.T) {
exec.SetOnSessionStateChange(func(ctx context.Context, taskID, sessionID string, state models.TaskSessionState, errorMessage string) error {
return repo.UpdateTaskSessionState(ctx, sessionID, state, errorMessage)
})
exec.SetOnBootstrapFailureTransition(func(
ctx context.Context,
taskID, sessionID, _ string,
_ models.TaskSessionState,
_ string,
errorValue models.LastAgentError,
) (bool, models.TaskSessionState, error) {
changed, _, err := repo.CommitBootstrapFailureIfCurrentExecution(
ctx,
taskID,
sessionID,
"exec-456",
models.TaskSessionStateStarting,
"",
errorValue,
)
if err != nil || !changed {
return changed, models.TaskSessionStateStarting, err
}
return true, models.TaskSessionStateFailed, nil
})
exec.SetOnTaskStateChange(func(ctx context.Context, taskID string, state v1.TaskState) error {
return repo.UpdateTaskState(ctx, taskID, state)
})
@@ -2310,6 +2331,15 @@ func TestHandleAgentProcessStartFailure_CancellationDuringCallbackStopsUnclaimed
transitionCalls.Add(1)
return false, models.TaskSessionStateCancelled, nil
})
exec.SetOnBootstrapFailureTransition(func(
ctx context.Context,
taskID, sessionID, _ string,
_ models.TaskSessionState,
_ string,
errorValue models.LastAgentError,
) (bool, models.TaskSessionState, error) {
return exec.transitionSessionState(ctx, taskID, sessionID, models.TaskSessionStateFailed, errorValue.Message)
})
exec.SetOnExecutionCleanupClaim(func(sessionID, executionID string) bool {
if sessionID != "session-123" || executionID != "exec-456" {
t.Fatalf("cleanup claim = (%q, %q), want (session-123, exec-456)", sessionID, executionID)
@@ -2654,6 +2684,30 @@ func newRunAgentProcessAsyncFailureFixture(t *testing.T) *runAgentProcessAsyncFa
}
return repo.UpdateTaskSessionState(ctx, sessionID, state, errorMessage)
})
f.exec.SetOnBootstrapFailureTransition(func(
ctx context.Context,
taskID, sessionID, _ string,
_ models.TaskSessionState,
_ string,
errorValue models.LastAgentError,
) (bool, models.TaskSessionState, error) {
changed, _, err := repo.CommitBootstrapFailureIfCurrentExecution(
ctx,
taskID,
sessionID,
"exec-456",
models.TaskSessionStateStarting,
"",
errorValue,
)
if changed {
f.sessionFailedSeen = true
}
if err != nil || !changed {
return changed, models.TaskSessionStateStarting, err
}
return true, models.TaskSessionStateFailed, nil
})
f.exec.SetOnAgentStartFailed(func(ctx context.Context, taskID, sessionID, agentExecutionID string, err error, fromResume bool) bool {
f.startFailedCalls++
f.lastFromResume = fromResume
@@ -102,6 +102,8 @@ func (e *Executor) buildLastAgentError(
return models.LastAgentError{
Message: classification.message,
OccurredAt: occurredAt,
Scope: models.ErrorScopeSession,
Phase: models.LaunchErrorPhaseBootstrap,
Code: classification.code,
Details: details,
RecoveryActions: func() []string {
@@ -167,6 +169,7 @@ func (e *Executor) buildBootstrapLastAgentError(
return models.LastAgentError{
Message: classification.message,
OccurredAt: occurredAt,
Scope: models.ErrorScopeSession,
AgentExecutionID: agentExecutionID,
ExecutionID: agentExecutionID,
Phase: models.LaunchErrorPhaseBootstrap,
@@ -306,8 +309,8 @@ func (e *Executor) bootstrapFailureExpectation(
// commitBootstrapFailure commits the correlated failure projection and the
// FAILED transition through the atomic orchestrator callback or repository
// capability. Legacy test stores retain the older CAS plus state-transition
// fallback, but the production SQL repository uses the guarded commit.
// capability. Stores without an execution-fenced commit fail closed so a
// bootstrap failure cannot overwrite a successor execution.
func (e *Executor) commitBootstrapFailure(
ctx context.Context,
taskID, sessionID, agentExecutionID string,
@@ -352,43 +355,7 @@ func (e *Executor) commitBootstrapFailure(
return true, models.TaskSessionStateFailed, nil
}
// Legacy stores cannot atomically fence the executor row and session write.
// Keep their existing behavior for focused test doubles and older adapters;
// the production repository always takes the guarded path above.
writer, supportsCAS := e.repo.(sessionMetadataErrorCASWriter)
var persisted bool
if supportsCAS {
if expectedStamp != "" {
persisted, err = writer.SetSessionMetadataKeyIfStamp(
ctx,
sessionID,
models.SessionMetaKeyLastAgentError,
expectedStamp,
errorValue,
)
} else {
persisted, err = writer.SetSessionMetadataKeyIfAbsent(
ctx,
sessionID,
models.SessionMetaKeyLastAgentError,
errorValue,
)
}
} else {
err = e.repo.SetSessionMetadataKey(
ctx, sessionID, models.SessionMetaKeyLastAgentError, errorValue,
)
persisted = err == nil
}
if err != nil {
return false, session.State, err
}
if !persisted {
return false, session.State, nil
}
changed, finalState, err := e.transitionSessionState(
ctx, taskID, sessionID, models.TaskSessionStateFailed, errorValue.Message,
return false, session.State, fmt.Errorf(
"bootstrap failure requires an execution-fenced repository commit",
)
return changed, finalState, err
}
@@ -8,6 +8,8 @@ import (
"github.com/kandev/kandev/internal/agentctl/types/streams"
"github.com/kandev/kandev/internal/orchestrator/watcher"
"github.com/kandev/kandev/internal/task/models"
"github.com/stretchr/testify/require"
)
// thinkingBlocks400 is the resume-corrupted signature surfaced by the
@@ -77,18 +79,28 @@ func TestCreateRecoveryStatusMessage_ManagedRuntimeNpmUsesOneRetryAction(t *test
mc := &mockMessageCreator{}
svc.messageCreator = mc
svc.createRecoveryStatusMessage(ctx, watcher.AgentEventData{
require.NoError(t, svc.createRecoveryStatusMessage(ctx, watcher.AgentEventData{
TaskID: "t-npm",
SessionID: "s-npm",
ErrorMessage: "managed npm runtime failed to prepare",
FailureCode: "managed_runtime_npm_resolution",
FailureDetails: "npm error code ETARGET\nnpm error notarget No matching version found",
})
}))
require.NoError(t, svc.createRecoveryStatusMessage(ctx, watcher.AgentEventData{
TaskID: "t-npm",
SessionID: "s-npm",
ErrorMessage: "managed npm runtime failed to prepare",
FailureCode: "managed_runtime_npm_resolution",
FailureDetails: "npm error code ETARGET\nnpm error notarget No matching version found",
}))
if len(mc.sessionMessages) != 1 {
t.Fatalf("expected one recovery message, got %d", len(mc.sessionMessages))
}
meta := mc.sessionMessages[0].metadata
if meta["scope"] != models.ErrorScopeSession || meta["error_stamp"] == "" {
t.Fatalf("recovery identity = %#v", meta)
}
if meta["failure_kind"] != "managed_runtime_npm_resolution" {
t.Fatalf("failure_kind = %#v", meta["failure_kind"])
}
@@ -120,11 +132,11 @@ func TestCreateRecoveryStatusMessage_ResumeCorrupted(t *testing.T) {
mc := &mockMessageCreator{}
svc.messageCreator = mc
svc.createRecoveryStatusMessage(ctx, watcher.AgentEventData{
require.NoError(t, svc.createRecoveryStatusMessage(ctx, watcher.AgentEventData{
TaskID: "t1",
SessionID: "s1",
ErrorMessage: thinkingBlocks400,
})
}))
if len(mc.sessionMessages) != 1 {
t.Fatalf("expected 1 session message, got %d", len(mc.sessionMessages))
@@ -147,12 +159,12 @@ func TestCreateRecoveryStatusMessage_TransientExhaustionUsesSafeReason(t *testin
mc := &mockMessageCreator{}
svc.messageCreator = mc
svc.createRecoveryStatusMessage(ctx, watcher.AgentEventData{
require.NoError(t, svc.createRecoveryStatusMessage(ctx, watcher.AgentEventData{
TaskID: "t-capacity",
SessionID: "s-capacity",
AgentID: "codex-acp",
ErrorMessage: "Selected model is at capacity. Please try a different model.",
})
}))
if len(mc.sessionMessages) != 1 {
t.Fatalf("expected 1 session message, got %d", len(mc.sessionMessages))
@@ -177,7 +189,7 @@ func TestCreateRecoveryStatusMessage_OpenCodeQuotaCarriesSafeMetadata(t *testing
resetAt := time.Date(2026, 8, 2, 19, 34, 44, 0, time.UTC)
const wantURL = "https://opencode.ai/workspace/wrk_01KQM7K5CYT715264YKKFB17ZY/go"
svc.createRecoveryStatusMessage(ctx, watcher.AgentEventData{
require.NoError(t, svc.createRecoveryStatusMessage(ctx, watcher.AgentEventData{
TaskID: "t-quota",
SessionID: "s-quota",
AgentID: "opencode-acp",
@@ -191,7 +203,7 @@ func TestCreateRecoveryStatusMessage_OpenCodeQuotaCarriesSafeMetadata(t *testing
OccurredAt: time.Date(2026, 8, 2, 15, 15, 44, 0, time.UTC),
ResetAt: &resetAt,
},
})
}))
if len(mc.sessionMessages) != 1 {
t.Fatalf("expected 1 session message, got %d", len(mc.sessionMessages))
@@ -226,7 +238,7 @@ func TestCreateRecoveryStatusMessage_GenericFailureCarriesRemediationURL(t *test
// A structured ACP-sourced diagnostic is not quota-classified, but the
// validated link still reaches the generic recoverable card.
svc.createRecoveryStatusMessage(ctx, watcher.AgentEventData{
require.NoError(t, svc.createRecoveryStatusMessage(ctx, watcher.AgentEventData{
TaskID: "t-generic",
SessionID: "s-generic",
AgentID: "opencode-acp",
@@ -237,7 +249,7 @@ func TestCreateRecoveryStatusMessage_GenericFailureCarriesRemediationURL(t *test
RemediationURL: wantURL,
OccurredAt: time.Date(2026, 8, 2, 15, 15, 44, 0, time.UTC),
},
})
}))
if len(mc.sessionMessages) != 1 {
t.Fatalf("expected 1 session message, got %d", len(mc.sessionMessages))
@@ -279,12 +291,12 @@ func TestProviderRemediationURLRejectsInvalidDiagnostics(t *testing.T) {
}},
} {
t.Run(tt.name, func(t *testing.T) {
svc.createRecoveryStatusMessage(ctx, watcher.AgentEventData{
require.NoError(t, svc.createRecoveryStatusMessage(ctx, watcher.AgentEventData{
TaskID: "t-none",
SessionID: "s-none",
ErrorMessage: "provider failed",
ProviderError: tt.providerErr,
})
}))
if len(mc.sessionMessages) == 0 {
t.Fatal("expected a session message")
}
@@ -292,6 +304,7 @@ func TestProviderRemediationURLRejectsInvalidDiagnostics(t *testing.T) {
t.Fatalf("remediation_url = %#v, want absent", url)
}
mc.sessionMessages = nil
mc.idempotentSessionMessages = nil
})
}
}
@@ -124,6 +124,7 @@ type MessageCreator interface {
// parentToolCallID is the parent Task tool call ID for subagent nesting (empty for top-level).
UpdateToolCallMessage(ctx context.Context, taskID, toolCallID, parentToolCallID, status, result, agentSessionID, title, turnID, msgType string, normalized *streams.NormalizedPayload) error
CreateSessionMessage(ctx context.Context, taskID, content, agentSessionID, messageType, turnID string, metadata map[string]interface{}, requestsInput bool) error
CreateSessionMessageIdempotent(ctx context.Context, messageID, taskID, content, agentSessionID, messageType, turnID string, metadata map[string]interface{}, requestsInput bool) error
CreatePermissionRequestMessage(ctx context.Context, taskID, sessionID, requestID, pendingID, toolCallID, title, turnID string, options []map[string]interface{}, actionType string, actionDetails map[string]interface{}) (string, error)
UpdatePermissionMessage(ctx context.Context, taskID, sessionID, requestID, pendingID string, status models.PermissionStatus) error
ClaimPermissionResolution(ctx context.Context, request models.PermissionResolutionClaimRequest) (*models.PermissionResolutionClaimResult, error)
@@ -1707,6 +1708,7 @@ func NewService(
})
exec.SetOnSessionStateTransition(s.transitionTaskSessionState)
exec.SetOnBootstrapFailureTransition(s.transitionBootstrapFailure)
exec.SetOnBootstrapFailureMessageRepair(s.persistBootstrapFailureMessage)
exec.SetOnSessionStarting(func(
ctx context.Context,
taskID string,
@@ -1728,6 +1730,7 @@ func NewService(
ctx, taskID, session, expectedState, promoteTask, allowCompletedResume,
)
})
exec.SetOnLaunchFailed(s.handleLaunchFailed)
exec.SetOnExecutionCleanupClaim(s.claimForcedExecutionCleanup)
exec.SetOnExecutionStopOwnerRegistration(s.RegisterExecutionStopOwner)
exec.SetOnTaskReviewStateReconcile(func(ctx context.Context, taskID, completedSessionID string) {
@@ -196,6 +196,7 @@ func (s *Service) shouldSkipTerminalPRAutoStart(
errorValue := models.TaskLaunchError{
Message: terminalPRLaunchMessage,
OccurredAt: time.Now().UTC(),
Scope: models.ErrorScopeTask,
Code: models.LaunchErrorCategoryPRAlreadyClosed,
RecoveryActions: terminalPRRecoveryActions(ctx, s.workflowStepGetter, task.WorkflowStepID),
TaskRepositoryID: taskPRLaunchErrorRepositoryID(matches),
@@ -12,7 +12,6 @@ import (
"github.com/kandev/kandev/internal/agent/runtime/routingerr"
"github.com/kandev/kandev/internal/task/models"
taskservice "github.com/kandev/kandev/internal/task/service"
"github.com/kandev/kandev/internal/task/statussummary"
wfmodels "github.com/kandev/kandev/internal/workflow/models"
"github.com/kandev/kandev/internal/worktree"
v1 "github.com/kandev/kandev/pkg/api/v1"
@@ -65,7 +64,14 @@ type taskLaunchRecoveryRepository interface {
UpdateRepositoryDefaultBranch(context.Context, string, string, string) error
SetSessionMetadataKeyIfStamp(context.Context, string, string, string, interface{}) (bool, error)
SetTaskMetadataKeyIfStamp(context.Context, string, string, string, interface{}) (bool, error)
RemoveSessionMetadataKeyIfStamp(context.Context, string, string, string) (bool, error)
}
type taskLaunchRecoveryEnvironmentReader interface {
GetTaskEnvironmentByTaskID(context.Context, string) (*models.TaskEnvironment, error)
}
type taskLaunchRecoveryEnvironmentResetter interface {
ResetTaskEnvironment(context.Context, string, taskservice.ResetOptions) error
}
type taskLaunchRecoverySource struct {
@@ -216,72 +222,35 @@ func (s *Service) verifyCurrentTaskLaunchRecoverySource(
ctx context.Context,
source *taskLaunchRecoverySource,
) error {
current, err := s.loadAuthoritativeTaskLaunchRecoveryError(ctx, source.task.ID)
if source.sessionOwned {
currentSession, err := s.repo.GetTaskSession(ctx, source.session.ID)
if err != nil {
return err
}
if currentSession == nil {
return ErrTaskLaunchRecoveryStale
}
current, found := models.LoadLastAgentError(currentSession.Metadata)
if !found || current.IsDismissed() || !current.MatchesStamp(source.errorStamp) {
return ErrTaskLaunchRecoveryStale
}
return nil
}
currentTask, err := s.repo.GetTask(ctx, source.task.ID)
if err != nil {
return err
}
if current == nil || current.SessionID != source.sessionID() || current.Stamp != source.errorStamp {
if currentTask == nil {
return ErrTaskLaunchRecoveryStale
}
current, found := models.LoadTaskLaunchError(currentTask.Metadata)
if !found || !current.MatchesStamp(source.errorStamp) {
return ErrTaskLaunchRecoveryStale
}
return nil
}
func (s *Service) loadAuthoritativeTaskLaunchRecoveryError(
ctx context.Context,
taskID string,
) (*statussummary.ActiveErrorSummary, error) {
task, err := s.repo.GetTask(ctx, taskID)
if err != nil {
return nil, err
}
if task == nil {
return nil, fmt.Errorf("task %q was not found", taskID)
}
sessions, err := s.repo.ListTaskSessions(ctx, taskID)
if err != nil {
return nil, err
}
input := statussummary.RebuildInput{Now: time.Now().UTC()}
for _, session := range sessions {
if session == nil {
continue
}
lastError, found := models.LoadLastAgentError(session.Metadata)
if !found || lastError.IsDismissed() {
continue
}
input.Sessions = append(input.Sessions, statussummary.RebuildSession{
ID: session.ID,
ActiveError: &statussummary.ActiveErrorSummary{
SessionID: session.ID,
TaskRepositoryID: lastError.TaskRepositoryID,
ExecutionID: lastError.ExecutionID,
AttemptID: lastError.AttemptID,
Phase: lastError.Phase,
Stamp: lastError.Stamp(),
OccurredAt: lastError.OccurredAt,
Preview: lastError.Message,
Details: lastError.Details,
Category: lastError.Code,
RecoveryActions: lastError.RecoveryActions,
Causes: lastError.Causes,
},
})
}
if launchError, found := models.LoadTaskLaunchError(task.Metadata); found {
input.TaskError = &statussummary.ActiveErrorSummary{
TaskRepositoryID: launchError.TaskRepositoryID,
Stamp: launchError.Stamp(),
OccurredAt: launchError.OccurredAt,
Preview: launchError.Message,
Details: launchError.Details,
Category: launchError.Code,
RecoveryActions: launchError.RecoveryActions,
}
}
return statussummary.BuildFromAuthoritative(input).ActiveError, nil
}
func (s *Service) loadSessionTaskLaunchRecoverySource(
ctx context.Context,
req *TaskLaunchRecoveryRequest,
@@ -298,7 +267,7 @@ func (s *Service) loadSessionTaskLaunchRecoverySource(
return nil, fmt.Errorf("session does not belong to task")
}
lastError, found := models.LoadLastAgentError(session.Metadata)
if !found {
if !found || lastError.IsDismissed() {
return nil, fmt.Errorf("session has no active launch error")
}
if !lastError.MatchesStamp(req.ErrorStamp) {
@@ -354,13 +323,6 @@ func (source *taskLaunchRecoverySource) taskRepositoryID() string {
return source.taskError.TaskRepositoryID
}
func (source *taskLaunchRecoverySource) sessionID() string {
if source.sessionOwned && source.session != nil {
return source.session.ID
}
return ""
}
func (source *taskLaunchRecoverySource) recoveryActions() []string {
if source.sessionOwned {
return source.sessionError.RecoveryActions
@@ -492,6 +454,7 @@ func (s *Service) recordSessionUnresolvedDefault(
value := models.LastAgentError{
Message: message,
OccurredAt: time.Now().UTC(),
Scope: models.ErrorScopeSession,
Code: models.LaunchErrorCategoryDefaultBranchUnresolved,
Details: details,
RecoveryActions: []string{models.RecoveryActionPickBaseBranch},
@@ -530,6 +493,7 @@ func (s *Service) recordTaskUnresolvedDefault(
value := models.TaskLaunchError{
Message: message,
OccurredAt: time.Now().UTC(),
Scope: models.ErrorScopeTask,
Code: models.LaunchErrorCategoryDefaultBranchUnresolved,
Details: details,
RecoveryActions: []string{models.RecoveryActionPickBaseBranch},
@@ -564,8 +528,13 @@ func (s *Service) relaunchRecoveredTask(ctx context.Context, req *TaskLaunchReco
}
return response.SessionID, nil
}
if err := s.resetFailedTaskEnvironmentForRecovery(ctx, req.TaskID); err != nil {
return "", err
}
agentProfileID, _ := s.resolveTaskAgentProfile(ctx, source.task)
response, err := s.LaunchSession(ctx, &LaunchSessionRequest{
TaskID: req.TaskID,
AgentProfileID: agentProfileID,
Intent: IntentStart,
WorkflowStepID: source.task.WorkflowStepID,
})
@@ -578,6 +547,29 @@ func (s *Service) relaunchRecoveredTask(ctx context.Context, req *TaskLaunchReco
return response.SessionID, nil
}
// resetFailedTaskEnvironmentForRecovery removes the unusable environment left
// by a failed first materialization. Ready environments remain attached so a
// task-scoped launch error from a later session does not destroy a usable
// workspace before retrying.
func (s *Service) resetFailedTaskEnvironmentForRecovery(ctx context.Context, taskID string) error {
reader, canRead := s.taskLaunchRecoveryTasks.(taskLaunchRecoveryEnvironmentReader)
resetter, canReset := s.taskLaunchRecoveryTasks.(taskLaunchRecoveryEnvironmentResetter)
if !canRead || !canReset {
return nil
}
environment, err := reader.GetTaskEnvironmentByTaskID(ctx, taskID)
if err != nil {
return fmt.Errorf("load failed task environment before recovery: %w", err)
}
if environment == nil || environment.Status != models.TaskEnvironmentStatusFailed {
return nil
}
if err := resetter.ResetTaskEnvironment(ctx, taskID, taskservice.ResetOptions{}); err != nil {
return fmt.Errorf("reset failed task environment before recovery: %w", err)
}
return nil
}
func (s *Service) markTaskLaunchReviewDone(ctx context.Context, req *TaskLaunchRecoveryRequest, source *taskLaunchRecoverySource) error {
if s.taskLaunchRecoveryTasks == nil || s.githubService == nil || s.workflowStepGetter == nil {
return fmt.Errorf("review completion recovery is unavailable")
@@ -634,8 +626,31 @@ func (s *Service) clearTaskLaunchRecoverySource(ctx context.Context, source *tas
if s.taskLaunchRecoveryRepo == nil {
return fmt.Errorf("session launch-error compare-and-clear is unavailable")
}
_, err := s.taskLaunchRecoveryRepo.RemoveSessionMetadataKeyIfStamp(ctx, source.session.ID, models.SessionMetaKeyLastAgentError, source.errorStamp)
return err
retired := source.sessionError
dismissedAt := time.Now().UTC()
retired.DismissedAt = &dismissedAt
stored, err := s.taskLaunchRecoveryRepo.SetSessionMetadataKeyIfStamp(
ctx,
source.session.ID,
models.SessionMetaKeyLastAgentError,
source.errorStamp,
retired,
)
if err != nil {
return err
}
if !stored {
return ErrTaskLaunchRecoveryStale
}
if s.eventBus != nil {
if err := s.publishTaskSessionErrorEvent(ctx, source.task.ID, source.session.ID, false, &retired); err != nil {
s.logger.Warn("failed to publish retired task session error",
zap.String("task_id", source.task.ID),
zap.String("session_id", source.session.ID),
zap.Error(err))
}
}
return nil
}
s.clearTaskLaunchErrorIfStamp(ctx, source.task.ID, source.errorStamp)
return nil
@@ -8,6 +8,8 @@ import (
"testing"
"time"
"github.com/kandev/kandev/internal/events"
"github.com/kandev/kandev/internal/events/bus"
"github.com/kandev/kandev/internal/github"
"github.com/kandev/kandev/internal/task/models"
sqliterepo "github.com/kandev/kandev/internal/task/repository/sqlite"
@@ -24,6 +26,21 @@ type taskLaunchRecoveryServiceFake struct {
moved []taskservice.MoveTaskOptions
moveErr error
moveTaskIDs []string
environment *models.TaskEnvironment
resetErr error
resetCalls int
}
type failingTaskSessionErrorBus struct {
bus.EventBus
err error
}
func (b failingTaskSessionErrorBus) Publish(ctx context.Context, subject string, event *bus.Event) error {
if subject == events.TaskSessionErrorChanged {
return b.err
}
return b.EventBus.Publish(ctx, subject, event)
}
func (f *taskLaunchRecoveryServiceFake) UpdateRepositoryBaseBranch(_ context.Context, req taskservice.UpdateRepositoryBaseBranchRequest) (*models.TaskRepository, error) {
@@ -44,6 +61,46 @@ func (f *taskLaunchRecoveryServiceFake) MoveTaskWithOptions(_ context.Context, t
return &taskservice.MoveTaskResult{Task: &models.Task{ID: taskID, WorkflowID: workflowID, WorkflowStepID: workflowStepID, Position: position}}, nil
}
func (f *taskLaunchRecoveryServiceFake) GetTaskEnvironmentByTaskID(context.Context, string) (*models.TaskEnvironment, error) {
return f.environment, nil
}
func (f *taskLaunchRecoveryServiceFake) ResetTaskEnvironment(context.Context, string, taskservice.ResetOptions) error {
f.resetCalls++
return f.resetErr
}
func TestResetFailedTaskEnvironmentForRecoveryOnlyResetsFailedEnvironment(t *testing.T) {
for _, tc := range []struct {
name string
status models.TaskEnvironmentStatus
wantCalls int
wantErr bool
resetError error
}{
{name: "no environment", wantCalls: 0},
{name: "ready environment", status: models.TaskEnvironmentStatusReady, wantCalls: 0},
{name: "failed environment", status: models.TaskEnvironmentStatusFailed, wantCalls: 1},
{name: "reset failure", status: models.TaskEnvironmentStatusFailed, wantCalls: 1, wantErr: true, resetError: errors.New("reset failed")},
} {
t.Run(tc.name, func(t *testing.T) {
fake := &taskLaunchRecoveryServiceFake{resetErr: tc.resetError}
if tc.status != "" {
fake.environment = &models.TaskEnvironment{ID: "env-1", Status: tc.status}
}
svc := &Service{taskLaunchRecoveryTasks: fake}
err := svc.resetFailedTaskEnvironmentForRecovery(context.Background(), "task-1")
if (err != nil) != tc.wantErr {
t.Fatalf("resetFailedTaskEnvironmentForRecovery error = %v, want error = %t", err, tc.wantErr)
}
if fake.resetCalls != tc.wantCalls {
t.Fatalf("reset calls = %d, want %d", fake.resetCalls, tc.wantCalls)
}
})
}
}
type taskLaunchRecoveryWorktreeFake struct {
branch string
err error
@@ -132,6 +189,166 @@ func recoveryFixtureService(t *testing.T, repo *sqliterepo.Repository, fake *tas
return svc
}
func TestHandleLaunchFailedProjectsTaskErrorAndRetiresSessionMarker(t *testing.T) {
repo := setupTestRepo(t)
seedSession(t, repo, "task-launch-failure", "session-launch-failure", "step1")
now := time.Now().UTC()
lastError := models.LastAgentError{
Message: "The selected base branch is not available.",
OccurredAt: now,
Scope: models.ErrorScopeSession,
Code: models.LaunchErrorCategoryBaseBranchMissing,
Details: "base branch is missing",
RecoveryActions: []string{models.RecoveryActionRetryDefault, models.RecoveryActionPickBaseBranch},
TaskRepositoryID: "task-repository-launch-failure",
StampValue: "launch-failure-stamp",
}
if err := repo.SetSessionMetadataKey(context.Background(), "session-launch-failure", models.SessionMetaKeyLastAgentError, lastError); err != nil {
t.Fatalf("SetSessionMetadataKey: %v", err)
}
svc := createTestService(repo, newMockStepGetter(), newMockTaskRepo())
svc.handleLaunchFailed(context.Background(), "task-launch-failure", "session-launch-failure", "repo-1", errors.New("workspace preparation failed"))
task, err := repo.GetTask(context.Background(), "task-launch-failure")
if err != nil {
t.Fatalf("GetTask: %v", err)
}
taskError, ok := models.LoadTaskLaunchError(task.Metadata)
if !ok {
t.Fatal("task launch error was not persisted")
}
if taskError.Scope != models.ErrorScopeTask || taskError.SessionID != "session-launch-failure" {
t.Fatalf("task launch error ownership = %#v, want task scope with source session", taskError)
}
if taskError.Stamp() != lastError.Stamp() {
t.Fatalf("task launch error stamp = %q, want %q", taskError.Stamp(), lastError.Stamp())
}
session, err := repo.GetTaskSession(context.Background(), "session-launch-failure")
if err != nil {
t.Fatalf("GetTaskSession: %v", err)
}
retired, ok := models.LoadLastAgentError(session.Metadata)
if !ok || !retired.IsDismissed() {
t.Fatalf("session launch error = %#v, want a dismissed correlation marker", retired)
}
}
func TestHandleLaunchFailedKeepsProfileSpecificFailureOnSession(t *testing.T) {
repo := setupTestRepo(t)
const (
taskID = "task-session-owned-launch-failure"
sessionID = "session-session-owned-launch-failure"
)
seedSession(t, repo, taskID, sessionID, "step1")
session, err := repo.GetTaskSession(context.Background(), sessionID)
if err != nil {
t.Fatalf("GetTaskSession: %v", err)
}
session.AgentProfileID = "profile-specific"
if err := repo.UpdateTaskSession(context.Background(), session); err != nil {
t.Fatalf("UpdateTaskSession: %v", err)
}
lastError := models.LastAgentError{
Message: "The agent could not start.",
OccurredAt: time.Date(2026, 9, 14, 10, 30, 0, 0, time.UTC),
Scope: models.ErrorScopeSession,
Code: models.LaunchErrorCategoryGenericLaunchFailure,
Details: "profile environment could not be resolved",
RecoveryActions: []string{models.RecoveryActionRetryLaunch},
StampValue: "profile-specific-launch-failure",
}
if err := repo.SetSessionMetadataKey(
context.Background(), sessionID, models.SessionMetaKeyLastAgentError, lastError,
); err != nil {
t.Fatalf("SetSessionMetadataKey: %v", err)
}
svc := createTestService(repo, newMockStepGetter(), newMockTaskRepo())
messages := &mockMessageCreator{}
svc.messageCreator = messages
svc.handleLaunchFailed(
context.Background(), taskID, sessionID, "repo-1", errors.New("profile env resolution failed"),
)
task, err := repo.GetTask(context.Background(), taskID)
if err != nil {
t.Fatalf("GetTask: %v", err)
}
if _, found := models.LoadTaskLaunchError(task.Metadata); found {
t.Fatal("profile-specific launch failure was incorrectly promoted to task scope")
}
updated, err := repo.GetTaskSession(context.Background(), sessionID)
if err != nil {
t.Fatalf("GetTaskSession after launch failure: %v", err)
}
retained, found := models.LoadLastAgentError(updated.Metadata)
if !found || retained.IsDismissed() || retained.Stamp() != lastError.Stamp() {
t.Fatalf("session launch error = %#v, want active profile-specific marker", retained)
}
if updated.AgentProfileID != "profile-specific" {
t.Fatalf("session profile = %q, want profile-specific", updated.AgentProfileID)
}
if len(messages.sessionMessages) != 1 {
t.Fatalf("session recovery messages = %d, want 1", len(messages.sessionMessages))
}
if messages.sessionMessages[0].metadata["scope"] != models.ErrorScopeSession {
t.Fatalf("recovery message scope = %#v, want session", messages.sessionMessages[0].metadata["scope"])
}
}
func TestClearTaskLaunchRecoverySourceSucceedsWhenRetirementPublishFails(t *testing.T) {
ctx := context.Background()
const (
taskID = "task-session-error-publish"
sessionID = "session-error-publish"
)
repo := setupTestRepo(t)
seedSession(t, repo, taskID, sessionID, "step1")
lastError := models.LastAgentError{
Message: "The agent could not start.",
OccurredAt: time.Date(2026, 9, 14, 11, 0, 0, 0, time.UTC),
Scope: models.ErrorScopeSession,
StampValue: "session-error-publish-stamp",
}
if err := repo.SetSessionMetadataKey(ctx, sessionID, models.SessionMetaKeyLastAgentError, lastError); err != nil {
t.Fatalf("SetSessionMetadataKey: %v", err)
}
task, err := repo.GetTask(ctx, taskID)
if err != nil {
t.Fatalf("GetTask: %v", err)
}
session, err := repo.GetTaskSession(ctx, sessionID)
if err != nil {
t.Fatalf("GetTaskSession: %v", err)
}
baseBus := bus.NewMemoryEventBus(testLogger())
t.Cleanup(func() { baseBus.Close() })
svc := createTestService(repo, newMockStepGetter(), newMockTaskRepo())
svc.taskLaunchRecoveryRepo = repo
svc.eventBus = failingTaskSessionErrorBus{EventBus: baseBus, err: errors.New("event bus unavailable")}
err = svc.clearTaskLaunchRecoverySource(ctx, &taskLaunchRecoverySource{
task: task,
session: session,
sessionError: lastError,
sessionOwned: true,
errorStamp: lastError.Stamp(),
})
if err != nil {
t.Fatalf("clearTaskLaunchRecoverySource: %v", err)
}
updated, err := repo.GetTaskSession(ctx, sessionID)
if err != nil {
t.Fatalf("reload session: %v", err)
}
retired, found := models.LoadLastAgentError(updated.Metadata)
if !found || !retired.IsDismissed() {
t.Fatalf("retired session error = %#v, want dismissed metadata", retired)
}
}
func TestRecoverTaskLaunchRejectsStaleStampBeforeMutation(t *testing.T) {
repo := setupTestRepo(t)
seedTaskLaunchRecoveryFixture(t, repo, "task-stale", "task-repo-stale", models.RecoveryActionPickBaseBranch)
@@ -150,7 +367,7 @@ func TestRecoverTaskLaunchRejectsStaleStampBeforeMutation(t *testing.T) {
}
}
func TestRecoverTaskLaunchRejectsOlderSessionErrorWhenTaskErrorIsNewer(t *testing.T) {
func TestRecoverTaskLaunchValidatesSessionErrorIndependentlyOfTaskError(t *testing.T) {
repo := setupTestRepo(t)
const taskID = "task-newer-task-error"
const taskRepositoryID = "task-repo-newer-task-error"
@@ -169,6 +386,7 @@ func TestRecoverTaskLaunchRejectsOlderSessionErrorWhenTaskErrorIsNewer(t *testin
if err := repo.SetSessionMetadataKey(context.Background(), "session-older-error", models.SessionMetaKeyLastAgentError, models.LastAgentError{
Message: "older session launch error",
OccurredAt: now.Add(-time.Hour),
Scope: models.ErrorScopeSession,
Code: models.LaunchErrorCategoryBaseBranchMissing,
RecoveryActions: []string{models.RecoveryActionPickBaseBranch},
TaskRepositoryID: taskRepositoryID,
@@ -179,15 +397,26 @@ func TestRecoverTaskLaunchRejectsOlderSessionErrorWhenTaskErrorIsNewer(t *testin
fake := &taskLaunchRecoveryServiceFake{branches: []taskservice.Branch{{Name: "main", Type: "remote"}}}
svc := recoveryFixtureService(t, repo, fake)
_, err := svc.RecoverTaskLaunch(context.Background(), &TaskLaunchRecoveryRequest{
req := &TaskLaunchRecoveryRequest{
TaskID: taskID, SessionID: "session-older-error", TaskRepositoryID: taskRepositoryID,
Action: models.RecoveryActionPickBaseBranch, BaseBranch: "main", ErrorStamp: "older-session-stamp",
})
if !errors.Is(err, ErrTaskLaunchRecoveryStale) {
t.Fatalf("RecoverTaskLaunch error = %v, want stale error", err)
}
source, err := svc.loadTaskLaunchRecoverySource(context.Background(), req)
if err != nil {
t.Fatalf("loadTaskLaunchRecoverySource: %v", err)
}
if err := svc.verifyCurrentTaskLaunchRecoverySource(context.Background(), source); err != nil {
t.Fatalf("verifyCurrentTaskLaunchRecoverySource: %v", err)
}
if len(fake.updated) != 0 {
t.Fatalf("older session recovery updated repositories: %#v", fake.updated)
t.Fatalf("validation updated repositories: %#v", fake.updated)
}
task, err := repo.GetTask(context.Background(), taskID)
if err != nil {
t.Fatalf("GetTask: %v", err)
}
if taskError, ok := models.LoadTaskLaunchError(task.Metadata); !ok || taskError.Stamp() != "recovery-stamp" {
t.Fatalf("task error = %#v, want independent task error", taskError)
}
}
@@ -4714,26 +4714,27 @@ func TestIssue1884_StepProfileSignalGateStaysInTaskMode(t *testing.T) {
// mockMessageCreator implements MessageCreator for testing.
// Only CreateUserMessage is tracked; all other methods are no-op stubs.
type mockMessageCreator struct {
mu sync.Mutex
userMessages []mockUserMessage
sessionMessages []mockSessionMessage
sessionMessageAttempts int
sessionMessageDone chan struct{}
sessionMessageOnce sync.Once
sessionMessageErr error
agentMessages []mockAgentMessage
agentMessageWrites int
agentStreamWrites int
agentStreamTexts []string
thinkingWrites int
toolCallWrites int
toolUpdateWrites int
userMessageErr error
idempotentUserMessages map[string]struct{}
permissionClaimFn func(context.Context, models.PermissionResolutionClaimRequest) (*models.PermissionResolutionClaimResult, error)
permissionFinishFn func(context.Context, models.PermissionResolutionFinalizeRequest) (*models.PermissionResolutionFinalizeResult, error)
permissionAuditFn func(context.Context, string, string, string, string) (*models.PermissionResolutionAudit, error)
permissionUpdateFn func(context.Context, string, string, string, string, models.PermissionStatus) error
mu sync.Mutex
userMessages []mockUserMessage
sessionMessages []mockSessionMessage
sessionMessageAttempts int
sessionMessageDone chan struct{}
sessionMessageOnce sync.Once
sessionMessageErr error
idempotentSessionMessages map[string]struct{}
agentMessages []mockAgentMessage
agentMessageWrites int
agentStreamWrites int
agentStreamTexts []string
thinkingWrites int
toolCallWrites int
toolUpdateWrites int
userMessageErr error
idempotentUserMessages map[string]struct{}
permissionClaimFn func(context.Context, models.PermissionResolutionClaimRequest) (*models.PermissionResolutionClaimResult, error)
permissionFinishFn func(context.Context, models.PermissionResolutionFinalizeRequest) (*models.PermissionResolutionFinalizeResult, error)
permissionAuditFn func(context.Context, string, string, string, string) (*models.PermissionResolutionAudit, error)
permissionUpdateFn func(context.Context, string, string, string, string, models.PermissionStatus) error
}
type mockUserMessage struct {
@@ -4818,6 +4819,31 @@ func (m *mockMessageCreator) CreateSessionMessage(_ context.Context, taskID, con
return nil
}
func (m *mockMessageCreator) CreateSessionMessageIdempotent(_ context.Context, messageID, taskID, content, sessionID, messageType, turnID string, metadata map[string]interface{}, requestsInput bool) error {
m.mu.Lock()
defer m.mu.Unlock()
if m.sessionMessageErr != nil {
return m.sessionMessageErr
}
if m.idempotentSessionMessages == nil {
m.idempotentSessionMessages = make(map[string]struct{})
}
if _, exists := m.idempotentSessionMessages[messageID]; exists {
return nil
}
m.idempotentSessionMessages[messageID] = struct{}{}
m.sessionMessages = append(m.sessionMessages, mockSessionMessage{
taskID: taskID,
content: content,
sessionID: sessionID,
messageType: messageType,
turnID: turnID,
metadata: metadata,
requestsInput: requestsInput,
})
return nil
}
func (m *mockMessageCreator) CreatePermissionRequestMessage(context.Context, string, string, string, string, string, string, string, []map[string]interface{}, string, map[string]interface{}) (string, error) {
return "", nil
}
@@ -506,7 +506,7 @@ func TestWSAddMessage_ConcurrentInitialBriefStartsOnlyAdmittedCandidate(t *testi
}
require.Eventually(t, func() bool { return len(orch.queueCalls()) == 1 }, time.Second, time.Millisecond)
require.Len(t, orch.started, 1)
require.Eventually(t, func() bool { return len(orch.started) == 1 }, time.Second, time.Millisecond)
require.Len(t, repo.messages, 2)
queuedCalls := orch.queueCalls()
require.True(t, queuedCalls[0].userMessageRecorded)
@@ -47,6 +47,12 @@ const (
const (
LaunchErrorPhaseBootstrap = "bootstrap"
// Error scopes identify the owner of a failure. Session failures belong in
// the session transcript; task failures belong to the task shell and remain
// visible while the task changes tabs or sessions.
ErrorScopeSession = "session"
ErrorScopeTask = "task"
AgentErrorCauseOperationResume = "resume"
AgentErrorCauseOperationRestoreWorkspace = "restore_workspace"
@@ -138,6 +144,8 @@ func isKnownAgentErrorCauseCode(code string) bool {
type TaskLaunchError struct {
Message string `json:"message"`
OccurredAt time.Time `json:"occurred_at"`
Scope string `json:"scope,omitempty"`
SessionID string `json:"session_id,omitempty"`
Code string `json:"code,omitempty"`
Details string `json:"details,omitempty"`
RecoveryActions []string `json:"recovery_actions,omitempty"`
@@ -210,6 +218,7 @@ func isKnownRecoveryAction(action string) bool {
}
func normalizeLastAgentError(value LastAgentError) LastAgentError {
value.Scope = normalizeErrorScope(value.Scope, ErrorScopeSession)
value.Message = truncateUTF8Bytes(value.Message, maxLaunchErrorMessageBytes)
value.Code = truncateUTF8Bytes(value.Code, maxLaunchErrorCategoryBytes)
value.RecoveryActions = NormalizeRecoveryActionsForCategory(value.Code, value.RecoveryActions)
@@ -233,8 +242,10 @@ func normalizeLastAgentError(value LastAgentError) LastAgentError {
}
func normalizeTaskLaunchError(value TaskLaunchError) TaskLaunchError {
value.Scope = normalizeErrorScope(value.Scope, ErrorScopeTask)
value.Message = truncateUTF8Bytes(value.Message, maxLaunchErrorMessageBytes)
value.Code = truncateUTF8Bytes(value.Code, maxLaunchErrorCategoryBytes)
value.SessionID = truncateUTF8Bytes(value.SessionID, maxLaunchErrorIDBytes)
value.RecoveryActions = NormalizeRecoveryActionsForCategory(value.Code, value.RecoveryActions)
value.TaskRepositoryID = truncateUTF8Bytes(value.TaskRepositoryID, maxTaskRepositoryIDBytes)
value.StampValue = boundedLaunchErrorStamp(value.StampValue)
@@ -242,6 +253,17 @@ func normalizeTaskLaunchError(value TaskLaunchError) TaskLaunchError {
return value
}
func normalizeErrorScope(value, fallback string) string {
switch strings.TrimSpace(value) {
case ErrorScopeSession:
return ErrorScopeSession
case ErrorScopeTask:
return ErrorScopeTask
default:
return fallback
}
}
// LoadTaskLaunchError reads and validates the task-owned launch error.
func LoadTaskLaunchError(metadata map[string]interface{}) (TaskLaunchError, bool) {
if metadata == nil {
@@ -735,6 +735,7 @@ const SessionMetaKeyLastAgentError = "last_agent_error"
type LastAgentError struct {
Message string `json:"message"`
OccurredAt time.Time `json:"occurred_at"`
Scope string `json:"scope,omitempty"`
AgentExecutionID string `json:"agent_execution_id,omitempty"`
ExecutionID string `json:"execution_id,omitempty"`
Phase string `json:"phase,omitempty"`
@@ -606,6 +606,7 @@ func (s *Service) rebuildInput(
var activeError *statussummary.ActiveErrorSummary
if lastError, ok := models.LoadLastAgentError(session.Metadata); ok && !lastError.IsDismissed() {
activeError = &statussummary.ActiveErrorSummary{
Scope: models.ErrorScopeSession,
SessionID: session.ID,
TaskRepositoryID: lastError.TaskRepositoryID,
ExecutionID: lastError.ExecutionID,
@@ -655,10 +656,13 @@ func taskLaunchErrorSummary(task *models.Task) *statussummary.ActiveErrorSummary
return nil
}
return &statussummary.ActiveErrorSummary{
Scope: models.ErrorScopeTask,
SessionID: errorValue.SessionID,
TaskRepositoryID: errorValue.TaskRepositoryID,
Stamp: errorValue.Stamp(),
OccurredAt: errorValue.OccurredAt,
Preview: errorValue.Message,
Details: errorValue.Details,
Category: errorValue.Code,
RecoveryActions: errorValue.RecoveryActions,
}
@@ -45,6 +45,7 @@ type TaskStatusSummary struct {
ActiveSubagentCount int `json:"active_subagent_count,omitempty"`
PendingAction string `json:"pending_action,omitempty"`
ActiveError *ActiveErrorSummary `json:"active_error,omitempty"`
TaskError *ActiveErrorSummary `json:"task_error,omitempty"`
Git *GitSummary `json:"git,omitempty"`
PullRequest *PullRequestSummary `json:"pull_request,omitempty"`
// QueuedPromptCount is the number of prompts currently en-queued for the
@@ -60,6 +61,7 @@ type PrimarySessionSummary struct {
}
type ActiveErrorSummary struct {
Scope string `json:"scope,omitempty"`
SessionID string `json:"session_id,omitempty"`
TaskRepositoryID string `json:"task_repository_id,omitempty"`
ExecutionID string `json:"execution_id,omitempty"`
@@ -136,6 +138,9 @@ func (s TaskStatusSummary) Validate() error {
if err := validateActiveError(s.ActiveError); err != nil {
return err
}
if err := validateActiveError(s.TaskError); err != nil {
return err
}
return validatePullRequest(s.PullRequest)
}
@@ -158,6 +163,7 @@ func validateActiveError(activeError *ActiveErrorSummary) error {
value string
limit int
}{
{"active error scope", activeError.Scope, maxActiveErrorCategoryBytes},
{"active error session id", activeError.SessionID, maxSessionIDBytes},
{"active error task repository id", activeError.TaskRepositoryID, maxTaskRepositoryIDBytes},
{"active error execution id", activeError.ExecutionID, maxSessionIDBytes},
@@ -168,6 +174,9 @@ func validateActiveError(activeError *ActiveErrorSummary) error {
{"active error details", activeError.Details, MaxActiveErrorDetailsBytes},
{"active error category", activeError.Category, maxActiveErrorCategoryBytes},
}
if activeError.Scope != "" && activeError.Scope != models.ErrorScopeSession && activeError.Scope != models.ErrorScopeTask {
return fmt.Errorf("active error has unknown scope")
}
for _, field := range fields {
if err := validateUTF8Bytes(field.name, field.value, field.limit); err != nil {
return err
@@ -239,6 +248,7 @@ func (s TaskStatusSummary) SemanticJSON() ([]byte, error) {
ActiveSubagentCount: s.ActiveSubagentCount,
PendingAction: s.PendingAction,
ActiveError: s.ActiveError,
TaskError: s.TaskError,
Git: s.Git,
PullRequest: s.PullRequest,
QueuedPromptCount: s.QueuedPromptCount,
@@ -252,6 +262,7 @@ type semanticPayload struct {
ActiveSubagentCount int `json:"active_subagent_count,omitempty"`
PendingAction string `json:"pending_action,omitempty"`
ActiveError *ActiveErrorSummary `json:"active_error,omitempty"`
TaskError *ActiveErrorSummary `json:"task_error,omitempty"`
Git *GitSummary `json:"git,omitempty"`
PullRequest *PullRequestSummary `json:"pull_request,omitempty"`
QueuedPromptCount int `json:"queued_prompt_count,omitempty"`
@@ -69,6 +69,11 @@ func TestTaskStatusSummarySemanticJSONIsBoundedAndOmitsTransportMetadata(t *test
Stamp: "error-1",
Preview: "safe preview",
},
TaskError: &ActiveErrorSummary{
Scope: models.ErrorScopeTask,
Stamp: "task-error-1",
Preview: "safe task preview",
},
Git: &GitSummary{ChangedFiles: 2, Additions: 3},
}
payload, err := summary.SemanticJSON()
@@ -85,6 +90,9 @@ func TestTaskStatusSummarySemanticJSONIsBoundedAndOmitsTransportMetadata(t *test
if !summary.SemanticEqual(decoded) {
t.Fatalf("semantic round trip changed value: %#v", decoded)
}
if decoded.TaskError == nil || decoded.TaskError.Scope != models.ErrorScopeTask {
t.Fatalf("task error missing from semantic payload: %#v", decoded.TaskError)
}
}
func TestTaskStatusSummaryQueuedPromptCountRoundTripsThroughSemanticJSON(t *testing.T) {
@@ -173,6 +181,9 @@ func TestTaskStatusSummaryProjectsTaskOwnedErrorFields(t *testing.T) {
if got.ActiveError.Category != "pr_already_closed" {
t.Fatalf("active error category = %q", got.ActiveError.Category)
}
if got.TaskError == nil || got.TaskError.Scope != models.ErrorScopeTask {
t.Fatalf("task error = %+v, want independent task projection", got.TaskError)
}
wantActions := []string{"mark_review_done"}
if !reflect.DeepEqual(got.ActiveError.RecoveryActions, wantActions) {
t.Fatalf("active error actions = %#v, want %#v", got.ActiveError.RecoveryActions, wantActions)
@@ -2,6 +2,8 @@ package statussummary
import (
"strings"
"github.com/kandev/kandev/internal/task/models"
)
func deriveSummary(state *projectionState) TaskStatusSummary {
@@ -12,6 +14,7 @@ func deriveSummary(state *projectionState) TaskStatusSummary {
ActiveSubagentCount: activeSubagentCount,
PendingAction: derivePendingAction(state),
ActiveError: deriveActiveError(state),
TaskError: cloneActiveError(state.taskError),
Git: deriveGitSummary(state),
PullRequest: derivePullRequestSummary(state),
QueuedPromptCount: state.queuedCount,
@@ -19,6 +22,16 @@ func deriveSummary(state *projectionState) TaskStatusSummary {
}
}
func cloneActiveError(value *ActiveErrorSummary) *ActiveErrorSummary {
if value == nil {
return nil
}
copy := *value
copy.RecoveryActions = append([]string(nil), value.RecoveryActions...)
copy.Causes = append([]models.AgentErrorCause(nil), value.Causes...)
return &copy
}
func deriveSessionFields(state *projectionState) (*PrimarySessionSummary, string, int) {
var primary *sessionObservation
activities := make([]string, 0, len(state.sessions))
@@ -90,7 +103,7 @@ func derivePendingAction(state *projectionState) string {
func deriveActiveError(state *projectionState) *ActiveErrorSummary {
var active *ActiveErrorSummary
if !state.errorsObserved && state.current != nil && state.current.ActiveError != nil &&
(!state.taskErrorObserved || state.current.ActiveError.SessionID != "") {
(!state.taskErrorObserved || activeErrorScope(state.current.ActiveError) == models.ErrorScopeSession) {
copy := *state.current.ActiveError
active = &copy
}
@@ -108,6 +121,18 @@ func deriveActiveError(state *projectionState) *ActiveErrorSummary {
return active
}
// activeErrorScope routes explicit ownership first. Older summaries omitted
// scope, so their session-id inference remains the compatibility fallback.
func activeErrorScope(value *ActiveErrorSummary) string {
if value != nil && (value.Scope == models.ErrorScopeSession || value.Scope == models.ErrorScopeTask) {
return value.Scope
}
if value != nil && value.SessionID != "" {
return models.ErrorScopeSession
}
return models.ErrorScopeTask
}
func newerActiveError(candidate, current *ActiveErrorSummary) bool {
if candidate == nil {
return false
@@ -330,12 +330,18 @@ func applySummaryBaseline(state *projectionState, summary *TaskStatusSummary) {
isPrimary: true,
}
}
if summary.TaskError != nil {
copy := *summary.TaskError
copy.RecoveryActions = normalizeRecoveryActionsForCategory(copy.Category, copy.RecoveryActions)
state.taskError = &copy
}
if summary.ActiveError != nil {
copy := *summary.ActiveError
copy.RecoveryActions = normalizeRecoveryActionsForCategory(copy.Category, copy.RecoveryActions)
if copy.SessionID != "" {
scope := activeErrorScope(&copy)
if scope == models.ErrorScopeSession && copy.SessionID != "" {
state.errors[copy.SessionID] = &copy
} else {
} else if scope == models.ErrorScopeTask && summary.TaskError == nil {
state.taskError = &copy
}
}
@@ -182,6 +182,7 @@ func errorFromMap(now time.Time, sessionID string, data map[string]interface{})
phase = ""
}
return &ActiveErrorSummary{
Scope: normalizeErrorScope(stringField(data, "scope"), sessionID != ""),
SessionID: truncateString(sessionID, maxSessionIDBytes),
TaskRepositoryID: truncateString(stringField(data, "task_repository_id"), maxTaskRepositoryIDBytes),
ExecutionID: truncateString(firstString(data, "execution_id", "agent_execution_id"), maxSessionIDBytes),
@@ -201,7 +202,7 @@ func errorEqual(a, b *ActiveErrorSummary) bool {
if a == nil || b == nil {
return a == b
}
return a.SessionID == b.SessionID && a.TaskRepositoryID == b.TaskRepositoryID &&
return a.Scope == b.Scope && a.SessionID == b.SessionID && a.TaskRepositoryID == b.TaskRepositoryID &&
a.ExecutionID == b.ExecutionID && a.AttemptID == b.AttemptID && a.Phase == b.Phase &&
a.Stamp == b.Stamp && a.OccurredAt.Equal(b.OccurredAt) &&
a.Preview == b.Preview && a.Details == b.Details && a.Category == b.Category &&
@@ -7,6 +7,7 @@ import (
"github.com/kandev/kandev/internal/events"
"github.com/kandev/kandev/internal/events/bus"
"github.com/kandev/kandev/internal/task/models"
)
func taskErrorProjector(t *testing.T, store *projectorTestStore, loader TaskLaunchErrorLoader) *Projector {
@@ -47,14 +48,17 @@ func TestProjectorProjectsTaskOwnedLaunchErrorWithoutSession(t *testing.T) {
t.Fatalf("project task error: %v", err)
}
got := store.summary("task-1")
if got == nil || got.ActiveError == nil {
t.Fatalf("summary = %+v, want task-owned active error", got)
if got == nil || got.TaskError == nil {
t.Fatalf("summary = %+v, want task-owned error projection", got)
}
if got.ActiveError.SessionID != "" || got.ActiveError.TaskRepositoryID != "task-repository-1" {
t.Fatalf("active error identity = %+v", got.ActiveError)
if got.TaskError.Scope != "task" || got.TaskError.SessionID != "" || got.TaskError.TaskRepositoryID != "task-repository-1" {
t.Fatalf("task error identity = %+v", got.TaskError)
}
if len(got.ActiveError.RecoveryActions) != 1 || got.ActiveError.RecoveryActions[0] != "mark_review_done" {
t.Fatalf("active error actions = %#v", got.ActiveError.RecoveryActions)
if len(got.TaskError.RecoveryActions) != 1 || got.TaskError.RecoveryActions[0] != "mark_review_done" {
t.Fatalf("task error actions = %#v", got.TaskError.RecoveryActions)
}
if got.ActiveError == nil || got.ActiveError.Scope != "task" {
t.Fatalf("active error compatibility projection = %+v", got.ActiveError)
}
}
@@ -123,3 +127,60 @@ func TestProjectorRefreshesTaskLaunchErrorRemoval(t *testing.T) {
t.Fatalf("summary after task error removal = %+v, want no active error", got)
}
}
func TestProjectorClearsTaskScopedErrorWithOriginatingSessionAfterTaskMetadataRemoval(t *testing.T) {
store := newProjectorTestStore()
originatingSession := "session-origin"
storedError := &ActiveErrorSummary{
Scope: models.ErrorScopeTask,
SessionID: originatingSession,
Stamp: "task-error-1",
OccurredAt: time.Date(2026, 8, 19, 19, 0, 0, 0, time.UTC),
Preview: "task error",
}
store.rows["task-1"] = &StoredTaskStatusSummary{
TaskID: "task-1",
WorkspaceID: "workspace-1",
Summary: TaskStatusSummary{
Revision: 1,
ActiveError: storedError,
TaskError: storedError,
},
}
active := true
projector := taskErrorProjector(t, store, func(context.Context, string) (TaskLaunchErrorObservation, error) {
if !active {
return TaskLaunchErrorObservation{Observed: true}, nil
}
return TaskLaunchErrorObservation{
Observed: true,
Error: storedError,
}, nil
})
event := func() *bus.Event {
return bus.NewEvent(events.TaskUpdated, "test", map[string]interface{}{
"task_id": "task-1",
"workspace_id": "workspace-1",
})
}
if err := projector.HandleEvent(context.Background(), event()); err != nil {
t.Fatalf("restore task error: %v", err)
}
got := store.summary("task-1")
if got == nil || got.TaskError == nil || got.ActiveError == nil {
t.Fatalf("summary after restore = %+v, want task error in both projections", got)
}
if got.TaskError.Scope != models.ErrorScopeTask || got.TaskError.SessionID != originatingSession {
t.Fatalf("restored task error = %+v, want explicit task scope", got.TaskError)
}
active = false
if err := projector.HandleEvent(context.Background(), event()); err != nil {
t.Fatalf("clear task error: %v", err)
}
got = store.summary("task-1")
if got == nil || got.TaskError != nil || got.ActiveError != nil {
t.Fatalf("summary after task metadata removal = %+v, want both errors cleared", got)
}
}
@@ -140,6 +140,7 @@ func normalizeRebuildError(input *ActiveErrorSummary, now time.Time) *ActiveErro
copy.OccurredAt = now.UTC()
}
copy.Preview = truncateString(copy.Preview, MaxActiveErrorPreviewBytes)
copy.Scope = normalizeErrorScope(copy.Scope, copy.SessionID != "")
copy.SessionID = truncateString(copy.SessionID, maxSessionIDBytes)
copy.TaskRepositoryID = truncateString(copy.TaskRepositoryID, maxTaskRepositoryIDBytes)
copy.ExecutionID = truncateString(copy.ExecutionID, maxSessionIDBytes)
@@ -157,3 +158,13 @@ func normalizeRebuildError(input *ActiveErrorSummary, now time.Time) *ActiveErro
copy.Stamp = truncateString(copy.Stamp, maxActiveErrorStampBytes)
return &copy
}
func normalizeErrorScope(scope string, sessionOwned bool) string {
if scope == models.ErrorScopeSession || scope == models.ErrorScopeTask {
return scope
}
if sessionOwned {
return models.ErrorScopeSession
}
return models.ErrorScopeTask
}
@@ -11,6 +11,8 @@ import { ExecutionIndicator } from "../../components/execution-indicator";
import { useOfficeTopbar } from "../../components/office-topbar-context";
import type { Task } from "./types";
import { useTranslation } from "react-i18next";
import { TaskLaunchErrorProvider } from "@/components/task/task-launch-error-context";
import { TaskSharedError } from "@/components/task/task-shared-error";
const OfficeDockviewLayout = dynamic(
() => import("./office-dockview-layout").then((m) => ({ default: m.OfficeDockviewLayout })),
@@ -57,14 +59,24 @@ export function TaskAdvancedMode({ task, onToggleSimple }: TaskAdvancedModeProps
});
return (
<div className="flex flex-col h-full">
{isSessionEnded && (
<div className="flex items-center gap-2 px-4 py-2 bg-muted border-b border-border shrink-0">
<IconInfoCircle className="h-4 w-4 text-muted-foreground" />
<span className="text-sm text-muted-foreground">{t("office:agentSessionEnded")}</span>
</div>
)}
<OfficeDockviewLayout taskId={task.id} sessionId={sessionId} task={task} />
</div>
<TaskLaunchErrorProvider
value={{
taskId: task.id,
workspaceId: task.workspaceId,
statusSummary: task.statusSummary,
repositories: task.repositories,
}}
>
<div className="flex flex-col h-full">
{isSessionEnded && (
<div className="flex items-center gap-2 px-4 py-2 bg-muted border-b border-border shrink-0">
<IconInfoCircle className="h-4 w-4 text-muted-foreground" />
<span className="text-sm text-muted-foreground">{t("office:agentSessionEnded")}</span>
</div>
)}
<TaskSharedError />
<OfficeDockviewLayout taskId={task.id} sessionId={sessionId} task={task} />
</div>
</TaskLaunchErrorProvider>
);
}
+5 -3
View File
@@ -95,9 +95,9 @@ export type TaskSession = {
};
/**
* RunError is a chat-timeline view of a single FAILED office session.
* Sourced from TaskSession (no separate API call) — derived in
* task-chat.tsx for entries.kind === "error".
* RunError is a chat-timeline view of one retained Office session failure.
* Sourced from TaskSession (no separate API call) and derived in task-chat.tsx
* for entries.kind === "error".
*/
export type RunError = {
id: string;
@@ -113,6 +113,8 @@ export type RunError = {
recoveryActions?: TaskLaunchRecoveryAction[];
taskRepositoryId?: string;
errorStamp?: string;
/** False when the session error is retained as history after recovery. */
isActive?: boolean;
};
export type TaskLabelLocal = {
@@ -1,6 +1,6 @@
"use client";
import { memo, useCallback, useEffect, useRef, useState, type ReactNode } from "react";
import { memo, useCallback, useEffect, useRef, useState } from "react";
import { useSettingsData } from "@/hooks/domains/settings/use-settings-data";
import { type ChatInputContainerHandle } from "@/components/task/chat/chat-input-container";
import { MessageList } from "@/components/task/chat/message-list";
@@ -21,8 +21,6 @@ type QuickChatContentProps = {
placeholderOverride?: string;
initialPrompt?: string;
onInitialPromptAttempted?: () => void;
recoveryContent?: ReactNode;
recoveryRevealKey?: string | null;
};
function useQuickChatState(sessionId: string) {
@@ -52,8 +50,6 @@ export const QuickChatContent = memo(function QuickChatContent({
placeholderOverride,
initialPrompt,
onInitialPromptAttempted,
recoveryContent,
recoveryRevealKey,
}: QuickChatContentProps) {
const [clarificationKey, setClarificationKey] = useState(0);
const shortcutScopeRef = useRef<HTMLDivElement>(null);
@@ -112,8 +108,6 @@ export const QuickChatContent = memo(function QuickChatContent({
sessionState={panelState.session?.state}
worktreePath={getSessionWorkspacePath(panelState.session)}
onOpenFile={undefined}
prependContent={recoveryContent}
recoveryRevealKey={recoveryRevealKey}
/>
</div>
<ClarificationPanelSection
@@ -1,4 +1,3 @@
import type { ReactNode } from "react";
import { cleanup, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
@@ -17,7 +16,6 @@ const sessionRows = vi.hoisted(
const quickChatSessions = vi.hoisted(
() => [] as Array<{ sessionId: string; taskId?: string; agentProfileId?: string }>,
);
const quickChatContentProps = vi.hoisted(() => [] as Array<{ recoveryRevealKey?: string | null }>);
const useEnsureTaskSession = vi.hoisted(() => vi.fn());
const useTask = vi.hoisted(() => vi.fn());
const useSessionResumption = vi.hoisted(() =>
@@ -35,6 +33,10 @@ const useSessionResumption = vi.hoisted(() =>
})),
);
const useTaskStatusSummary = vi.hoisted(() => vi.fn());
const TEST_IDS = vi.hoisted(() => ({
sessionRecoveryCard: "session-bootstrap-recovery-card",
quickChatContent: "quick-chat-content",
}));
vi.mock("@/components/state-provider", () => ({
useAppStore: (selector: (state: unknown) => unknown) =>
@@ -53,34 +55,7 @@ vi.mock("@/components/task/passthrough-terminal", () => ({
PassthroughTerminal: () => <div data-testid="passthrough-terminal" />,
}));
vi.mock("./quick-chat-content", () => ({
QuickChatContent: ({
recoveryContent,
recoveryRevealKey,
}: {
recoveryContent?: ReactNode;
recoveryRevealKey?: string | null;
}) => {
quickChatContentProps.push({ recoveryRevealKey });
return <div data-testid="quick-chat-content">{recoveryContent}</div>;
},
}));
vi.mock("@/components/task/chat/session-bootstrap-recovery-card", () => ({
SessionBootstrapRecoveryCard: ({
error,
automaticRecovery,
}: {
error: { stamp: string };
automaticRecovery?: {
notice: string | null;
recoveryFailure: { outcome: string } | null;
};
}) => (
<div data-testid="session-bootstrap-recovery-card">
{error.stamp}
{automaticRecovery?.notice ?? ""}
{automaticRecovery?.recoveryFailure?.outcome ?? ""}
</div>
),
QuickChatContent: () => <div data-testid={TEST_IDS.quickChatContent} />,
}));
vi.mock("react-i18next", () => ({
useTranslation: () => ({ t: (key: string) => key }),
@@ -95,7 +70,6 @@ const BOOTSTRAP_OCCURRED_AT = "2026-09-11T10:00:00Z";
const BOOTSTRAP_PREVIEW = "The agent could not start.";
const WORKSPACE_READ_ONLY_NOTICE = "workspace restored read-only";
const RESUME_FAILURE = "resume failed";
const RECOVERY_CARD_TEST_ID = "session-bootstrap-recovery-card";
const session = {
kind: "chat" as const,
@@ -107,7 +81,6 @@ afterEach(() => {
cleanup();
delete sessionRows[session.sessionId];
quickChatSessions.length = 0;
quickChatContentProps.length = 0;
vi.clearAllMocks();
useTask.mockReset();
useTask.mockReturnValue(null);
@@ -190,7 +163,7 @@ describe("QuickChatSessionView session resumption", () => {
);
});
it("gives a matching bootstrap failure to the inline recovery card", () => {
it("keeps a session bootstrap failure in the transcript-owned path", () => {
sessionRows[session.sessionId] = { task_id: HYDRATED_TASK_ID };
useTask.mockReturnValue({ isArchived: false, workspaceId: WORKSPACE_ID });
useTaskStatusSummary.mockReturnValue({
@@ -205,11 +178,11 @@ describe("QuickChatSessionView session resumption", () => {
render(<QuickChatSessionView session={session} />);
expect(screen.getByTestId(RECOVERY_CARD_TEST_ID).textContent).toBe("bootstrap-1");
expect(screen.getByTestId("quick-chat-content")).toBeTruthy();
expect(screen.queryByTestId(TEST_IDS.sessionRecoveryCard)).toBeNull();
expect(screen.getByTestId(TEST_IDS.quickChatContent)).toBeTruthy();
});
it("passes automatic read-only recovery into the inline card", () => {
it("does not move session recovery feedback into the transcript", () => {
sessionRows[session.sessionId] = { task_id: HYDRATED_TASK_ID };
useTask.mockReturnValue({ isArchived: false, workspaceId: WORKSPACE_ID });
useTaskStatusSummary.mockReturnValue({
@@ -235,13 +208,11 @@ describe("QuickChatSessionView session resumption", () => {
render(<QuickChatSessionView session={session} />);
expect(screen.getByTestId(RECOVERY_CARD_TEST_ID).textContent).toContain(
WORKSPACE_READ_ONLY_NOTICE,
);
expect(screen.getByTestId(RECOVERY_CARD_TEST_ID).textContent).not.toContain(RESUME_FAILURE);
expect(screen.queryByTestId(TEST_IDS.sessionRecoveryCard)).toBeNull();
expect(screen.getByTestId(TEST_IDS.quickChatContent)).toBeTruthy();
});
it("passes automatic recovery failure into the inline card", () => {
it("keeps session recovery failure out of the transcript", () => {
sessionRows[session.sessionId] = { task_id: HYDRATED_TASK_ID };
useTask.mockReturnValue({ isArchived: false, workspaceId: WORKSPACE_ID });
useTaskStatusSummary.mockReturnValue({
@@ -271,74 +242,19 @@ describe("QuickChatSessionView session resumption", () => {
render(<QuickChatSessionView session={session} />);
expect(screen.getByTestId(RECOVERY_CARD_TEST_ID).textContent).toContain("recovery_failed");
expect(screen.queryByTestId(TEST_IDS.sessionRecoveryCard)).toBeNull();
expect(screen.getByTestId(TEST_IDS.quickChatContent)).toBeTruthy();
});
it("keys unstamped recovery feedback by attempt and outcome while preserving duplicate state", () => {
const firstFailure = {
outcome: "recovery_failed" as const,
resumeError: "first resume failure",
restoreError: "first restore failure",
};
const firstAttempt = {
resumptionState: "error" as const,
sessionStatus: null,
error: "first translated failure",
notice: null,
recoveryFailure: firstFailure,
recoveryAttemptId: 4,
taskSessionState: "FAILED" as const,
worktreePath: null,
worktreeBranch: null,
resumeSession: vi.fn(),
};
useSessionResumption.mockReturnValue(firstAttempt as never);
it("does not pass a recovery reveal key to Quick Chat content", () => {
const view = render(<QuickChatSessionView session={session} />);
const firstKey = quickChatContentProps.at(-1)?.recoveryRevealKey;
expect(firstKey).toBe("session-1:recovery:4:recovery_failed");
expect(screen.getByTestId(TEST_IDS.quickChatContent)).toBeTruthy();
useSessionResumption.mockReturnValue({
...firstAttempt,
error: "same attempt, different translated failure",
} as never);
view.rerender(<QuickChatSessionView session={session} />);
expect(quickChatContentProps.at(-1)?.recoveryRevealKey).toBe(firstKey);
useSessionResumption.mockReturnValue({
...firstAttempt,
recoveryFailure: {
outcome: "status_unavailable" as const,
kind: "timeout" as const,
statusError: "status request failed",
},
} as never);
view.rerender(<QuickChatSessionView session={session} />);
expect(quickChatContentProps.at(-1)?.recoveryRevealKey).toBe(
"session-1:recovery:4:status_unavailable",
);
useSessionResumption.mockReturnValue({
...firstAttempt,
error: null,
recoveryFailure: null,
recoveryAttemptId: 4,
} as never);
view.rerender(<QuickChatSessionView session={session} />);
expect(quickChatContentProps.at(-1)?.recoveryRevealKey).toBeNull();
useSessionResumption.mockReturnValue({
...firstAttempt,
error: "second translated failure",
recoveryAttemptId: 5,
} as never);
view.rerender(<QuickChatSessionView session={session} />);
expect(quickChatContentProps.at(-1)?.recoveryRevealKey).toBe(
"session-1:recovery:5:recovery_failed",
);
expect(screen.getByTestId(TEST_IDS.quickChatContent)).toBeTruthy();
});
it("keeps the bootstrap recovery card and automatic outcome in passthrough Quick Chat", () => {
it("keeps passthrough recovery feedback outside the terminal", () => {
sessionRows[session.sessionId] = { task_id: HYDRATED_TASK_ID, is_passthrough: true };
useTask.mockReturnValue({ isArchived: false, workspaceId: WORKSPACE_ID });
useTaskStatusSummary.mockReturnValue({
@@ -364,10 +280,11 @@ describe("QuickChatSessionView session resumption", () => {
render(<QuickChatSessionView session={session} />);
expect(screen.getByTestId(RECOVERY_CARD_TEST_ID).textContent).toContain(
expect(screen.queryByTestId(TEST_IDS.sessionRecoveryCard)).toBeNull();
expect(screen.getByTestId("session-recovery-notice").textContent).toContain(
WORKSPACE_READ_ONLY_NOTICE,
);
expect(screen.getByTestId("passthrough-terminal")).toBeTruthy();
expect(screen.queryByTestId("quick-chat-content")).toBeNull();
expect(screen.queryByTestId(TEST_IDS.quickChatContent)).toBeNull();
});
});
@@ -7,15 +7,12 @@ import { useSessionResumption } from "@/hooks/domains/session/use-session-resump
import { useTaskStatusSummary } from "@/hooks/domains/task/use-task-status-summary";
import { PassthroughTerminal } from "@/components/task/passthrough-terminal";
import { SessionRecoveryFeedback } from "@/components/task/ensure-session-error";
import { SessionBootstrapRecoveryCard } from "@/components/task/chat/session-bootstrap-recovery-card";
import { selectSessionRecoveryError } from "@/lib/session-recovery-presentation";
import { TaskLaunchErrorProvider } from "@/components/task/task-launch-error-context";
import { TaskSharedError } from "@/components/task/task-shared-error";
import type { QuickChatSession } from "@/lib/state/slices/ui/types";
import { QuickChatContent } from "./quick-chat-content";
import { useTranslation } from "react-i18next";
// i18n-exempt: internal recovery identity token, not user-facing copy.
const GENERIC_RECOVERY_OUTCOME = "feedback";
function useIsQuickChatPassthrough(sessionId: string) {
return useAppStore((state) => {
const session = state.taskSessions.items[sessionId];
@@ -45,33 +42,6 @@ function resolveTaskArchiveState(
return quickChatTaskId === taskId ? false : null;
}
function resolveRecoveryRevealKey(
sessionId: string,
bootstrapRecoveryError: { stamp: string; occurred_at: string } | null,
hasRecoveryFeedback: boolean,
recoveryAttemptId: number | undefined,
recoveryOutcome: string | null,
): string | null {
if (bootstrapRecoveryError) {
return `${sessionId}:${bootstrapRecoveryError.stamp || bootstrapRecoveryError.occurred_at}`;
}
if (!hasRecoveryFeedback) return null;
return `${sessionId}:recovery:${recoveryAttemptId ?? 0}:${recoveryOutcome ?? GENERIC_RECOVERY_OUTCOME}`;
}
function hasRecoveryFeedback(state: {
error: string | null;
notice: string | null;
recoveryFailure: unknown;
}): boolean {
return Boolean(state.error) || Boolean(state.notice) || state.recoveryFailure !== null;
}
function recoveryOutcome(failure: { outcome: string } | null): string | null {
if (!failure) return null;
return failure.outcome;
}
export function QuickChatSessionView({
session,
onInitialPromptAttempted,
@@ -91,13 +61,6 @@ export function QuickChatSessionView({
const resumption = useSessionResumption(taskId, session.sessionId, taskArchiveState);
const isPassthrough = useIsQuickChatPassthrough(session.sessionId);
const statusSummary = useTaskStatusSummary(taskId, task?.statusSummary);
const bootstrapRecoveryError = taskId
? selectSessionRecoveryError(
statusSummary?.active_error,
session.sessionId,
taskSession?.metadata,
)
: null;
const recoveryFeedback = (
<SessionRecoveryFeedback
error={resumption.error}
@@ -109,48 +72,60 @@ export function QuickChatSessionView({
}
/>
);
const recoverySurface = bootstrapRecoveryError ? (
<SessionBootstrapRecoveryCard
taskId={taskId!}
sessionId={session.sessionId}
workspaceId={task?.workspaceId ?? null}
error={bootstrapRecoveryError}
automaticRecovery={resumption}
/>
) : (
recoveryFeedback
);
const recoveryRevealKey = resolveRecoveryRevealKey(
session.sessionId,
bootstrapRecoveryError,
hasRecoveryFeedback(resumption),
resumption.recoveryAttemptId,
recoveryOutcome(resumption.recoveryFailure),
);
if (isPassthrough) {
return (
<div className="flex min-h-0 flex-1 flex-col overflow-hidden">
{recoverySurface}
<div className="min-h-0 flex-1">
<PassthroughTerminal key={session.sessionId} sessionId={session.sessionId} mode="agent" />
</div>
const sessionContent = isPassthrough ? (
<div className="flex min-h-0 flex-1 flex-col overflow-hidden">
{recoveryFeedback}
<div className="min-h-0 flex-1">
<PassthroughTerminal key={session.sessionId} sessionId={session.sessionId} mode="agent" />
</div>
);
}
const isConfig = session.kind === "config";
return (
</div>
) : (
<div className="flex min-h-0 flex-1 flex-col">
<div className="flex min-h-0 flex-1 flex-col">
<QuickChatContent
sessionId={session.sessionId}
minimalToolbar={isConfig}
placeholderOverride={isConfig ? t("chat:configChatPlaceholder") : undefined}
minimalToolbar={session.kind === "config"}
placeholderOverride={
session.kind === "config" ? t("chat:configChatPlaceholder") : undefined
}
initialPrompt={session.initialPrompt}
onInitialPromptAttempted={onInitialPromptAttempted}
recoveryContent={recoverySurface}
recoveryRevealKey={recoveryRevealKey}
/>
</div>
</div>
);
if (!taskId) return sessionContent;
if (isPassthrough) {
return (
<TaskLaunchErrorProvider
value={{
taskId,
workspaceId: task?.workspaceId ?? "",
statusSummary,
automaticRecovery: resumption,
}}
>
<div className="flex min-h-0 flex-1 flex-col">
<TaskSharedError />
{sessionContent}
</div>
</TaskLaunchErrorProvider>
);
}
return (
<TaskLaunchErrorProvider
value={{
taskId,
workspaceId: task?.workspaceId ?? "",
statusSummary,
automaticRecovery: resumption,
}}
>
<div className="flex min-h-0 flex-1 flex-col">
<TaskSharedError />
{sessionContent}
</div>
</TaskLaunchErrorProvider>
);
}
@@ -4,24 +4,11 @@ import { resolveCompetingInitialScrollOwner } from "./message-list-native-scroll
const noProgrammaticLock = () => false;
describe("resolveCompetingInitialScrollOwner", () => {
it("gives a new recovery failure the initial reveal before unread placement", () => {
expect(
resolveCompetingInitialScrollOwner({
hasPendingLayoutRestore: false,
hasExplicitScrollTarget: false,
hasRecoveryReveal: true,
hasUnreadDivider: true,
isProgrammaticScrollLocked: noProgrammaticLock,
}),
).toBe("recovery");
});
it("preserves layout and explicit message placement precedence", () => {
expect(
resolveCompetingInitialScrollOwner({
hasPendingLayoutRestore: true,
hasExplicitScrollTarget: true,
hasRecoveryReveal: true,
hasUnreadDivider: true,
isProgrammaticScrollLocked: noProgrammaticLock,
}),
@@ -30,10 +17,28 @@ describe("resolveCompetingInitialScrollOwner", () => {
resolveCompetingInitialScrollOwner({
hasPendingLayoutRestore: false,
hasExplicitScrollTarget: true,
hasRecoveryReveal: true,
hasUnreadDivider: true,
isProgrammaticScrollLocked: noProgrammaticLock,
}),
).toBe("explicit-target");
});
it("uses the unread divider and then the programmatic owner for ordinary placement", () => {
expect(
resolveCompetingInitialScrollOwner({
hasPendingLayoutRestore: false,
hasExplicitScrollTarget: false,
hasUnreadDivider: true,
isProgrammaticScrollLocked: noProgrammaticLock,
}),
).toBe("unread-divider");
expect(
resolveCompetingInitialScrollOwner({
hasPendingLayoutRestore: false,
hasExplicitScrollTarget: false,
hasUnreadDivider: false,
isProgrammaticScrollLocked: () => true,
}),
).toBe("programmatic-scroll");
});
});
@@ -45,7 +45,6 @@ const SCROLL_KEYS = new Set([
"PageUp",
" ",
]);
const RECOVERY_SCROLL_KEYS = new Set(["ArrowDown", "End", "PageDown", " "]);
/** Writes a clamped maximum so the browser resolves the native bottom without
* forcing a synchronous scrollHeight layout read. */
@@ -234,112 +233,31 @@ type PrependScrollState = {
anchorTop: number | null;
};
type RecoveryRevealState = {
key: string | null;
keepAtTop: boolean;
};
/** Transfers recovery scroll ownership only after a real downward gesture. */
function useRecoveryPrependScrollOwnership({
/** Captures the reader's current position between older-history requests. */
function usePrependScrollStateCapture({
scrollRef,
recoveryRevealStateRef,
isLoadingMoreRef,
olderLoadPendingRef,
newestItemKeyRef,
scrollState,
}: {
scrollRef: React.RefObject<HTMLDivElement | null>;
recoveryRevealStateRef: React.MutableRefObject<RecoveryRevealState>;
isLoadingMoreRef: React.MutableRefObject<boolean>;
olderLoadPendingRef: React.MutableRefObject<boolean>;
newestItemKeyRef: React.MutableRefObject<string | null>;
scrollState: React.MutableRefObject<PrependScrollState>;
}) {
useEffect(() => {
const el = scrollRef.current;
if (!el) return;
let userScrollIntentUntil = 0;
let pointerActive = false;
let touchActive = false;
let touchStartY: number | null = null;
const markUserScrollIntent = () => {
userScrollIntentUntil = Date.now() + USER_SCROLL_INTENT_WINDOW_MS;
};
const onWheel = (event: WheelEvent) => {
if (event.deltaY > 0) markUserScrollIntent();
};
const onKeyDown = (event: KeyboardEvent) => {
if (RECOVERY_SCROLL_KEYS.has(event.key)) markUserScrollIntent();
};
const onPointerDown = () => {
pointerActive = true;
markUserScrollIntent();
};
const onPointerUp = () => {
pointerActive = false;
};
const onTouchStart = (event: TouchEvent) => {
touchActive = true;
touchStartY = event.touches[0]?.clientY ?? null;
};
const onTouchMove = (event: TouchEvent) => {
const nextY = event.touches[0]?.clientY;
if (touchStartY !== null && nextY !== undefined && nextY < touchStartY) {
markUserScrollIntent();
}
};
const onTouchEnd = () => {
touchActive = false;
touchStartY = null;
};
const onScroll = () => {
const recoveryScrolledAway =
recoveryRevealStateRef.current.keepAtTop &&
el.scrollTop > 0 &&
(userScrollIntentUntil >= Date.now() || pointerActive || touchActive);
if (recoveryScrolledAway) {
recoveryRevealStateRef.current.keepAtTop = false;
if (olderLoadPendingRef.current || isLoadingMoreRef.current) {
scrollState.current = capturePrependScrollState(el, newestItemKeyRef.current);
}
}
// Native overflow anchoring can adjust scrollTop while the older page is
// being inserted. Keep the pre-request baseline until our layout effect
// has restored the visual position explicitly.
const element = scrollRef.current;
if (!element) return;
const capture = () => {
if (olderLoadPendingRef.current || isLoadingMoreRef.current) return;
scrollState.current = capturePrependScrollState(el, newestItemKeyRef.current);
scrollState.current = capturePrependScrollState(element, newestItemKeyRef.current);
};
scrollState.current = capturePrependScrollState(el, newestItemKeyRef.current);
el.addEventListener("wheel", onWheel, { passive: true });
el.addEventListener("keydown", onKeyDown);
el.addEventListener("pointerdown", onPointerDown, { passive: true });
el.addEventListener("pointerup", onPointerUp, { passive: true });
el.addEventListener("pointercancel", onPointerUp, { passive: true });
el.addEventListener("touchstart", onTouchStart, { passive: true });
el.addEventListener("touchmove", onTouchMove, { passive: true });
el.addEventListener("touchend", onTouchEnd, { passive: true });
el.addEventListener("touchcancel", onTouchEnd, { passive: true });
el.addEventListener("scroll", onScroll, { passive: true });
return () => {
el.removeEventListener("wheel", onWheel);
el.removeEventListener("keydown", onKeyDown);
el.removeEventListener("pointerdown", onPointerDown);
el.removeEventListener("pointerup", onPointerUp);
el.removeEventListener("pointercancel", onPointerUp);
el.removeEventListener("touchstart", onTouchStart);
el.removeEventListener("touchmove", onTouchMove);
el.removeEventListener("touchend", onTouchEnd);
el.removeEventListener("touchcancel", onTouchEnd);
el.removeEventListener("scroll", onScroll);
};
}, [
isLoadingMoreRef,
newestItemKeyRef,
olderLoadPendingRef,
recoveryRevealStateRef,
scrollRef,
scrollState,
]);
capture();
element.addEventListener("scroll", capture, { passive: true });
return () => element.removeEventListener("scroll", capture);
}, [isLoadingMoreRef, newestItemKeyRef, olderLoadPendingRef, scrollRef, scrollState]);
}
/**
@@ -359,7 +277,6 @@ function useScrollPositionOnPrepend(
items: RenderItem[],
isLoadingMore: boolean,
isProgrammaticScrollLocked: () => boolean,
recoveryRevealKey: string | null,
): () => void {
const scrollState = useRef<PrependScrollState>({
scrollHeight: 0,
@@ -370,22 +287,11 @@ function useScrollPositionOnPrepend(
const isLoadingMoreRef = useRef(isLoadingMore);
const olderLoadPendingRef = useRef(false);
const newestItemKeyRef = useRef<string | null>(getNewestNonSyntheticItemKey(items));
const recoveryRevealStateRef = useRef({
key: recoveryRevealKey,
keepAtTop: recoveryRevealKey !== null,
});
if (recoveryRevealStateRef.current.key !== recoveryRevealKey) {
recoveryRevealStateRef.current = {
key: recoveryRevealKey,
keepAtTop: recoveryRevealKey !== null,
};
}
isLoadingMoreRef.current = isLoadingMore;
if (isLoadingMore) olderLoadPendingRef.current = true;
newestItemKeyRef.current = getNewestNonSyntheticItemKey(items);
useRecoveryPrependScrollOwnership({
usePrependScrollStateCapture({
scrollRef,
recoveryRevealStateRef,
isLoadingMoreRef,
olderLoadPendingRef,
newestItemKeyRef,
@@ -417,11 +323,6 @@ function useScrollPositionOnPrepend(
prevFirstKeyRef.current = nextFirstKey;
if (olderLoadSettled) olderLoadPendingRef.current = false;
if (!el || !prepend || isProgrammaticScrollLocked()) return;
if (recoveryRevealStateRef.current.key !== null && recoveryRevealStateRef.current.keepAtTop) {
if (el.scrollTop !== 0) el.scrollTop = 0;
scrollState.current = capturePrependScrollState(el, newestItemKeyRef.current);
return;
}
const prev = scrollState.current;
const anchor = findMessageRow(el, prev.anchorKey);
if (anchor && prev.anchorTop !== null) {
@@ -1311,8 +1212,6 @@ type InitialScrollApplyParams = {
isNearBottomRef: React.RefObject<boolean>;
envSwitchPlacementToken: number | null;
hasUnreadDivider: boolean;
recoveryRevealKey: string | null;
recoveryPlacementPendingRef: React.RefObject<boolean>;
isProgrammaticScrollLocked: () => boolean;
phase: "provisional" | "final";
};
@@ -1320,11 +1219,9 @@ type InitialScrollApplyParams = {
function markInitialScrollConsumed(
didInitialScroll: React.RefObject<boolean>,
activationPendingRef: React.RefObject<boolean>,
recoveryPlacementPendingRef: React.RefObject<boolean>,
): void {
didInitialScroll.current = true;
activationPendingRef.current = false;
recoveryPlacementPendingRef.current = false;
}
function completeEnvSwitchPlacement(envSwitchPlacementToken: number | null): void {
@@ -1348,30 +1245,23 @@ function isCurrentEnvSwitchPlacement(
export type CompetingInitialScrollOwner =
| "layout-restore"
| "explicit-target"
| "recovery"
| "unread-divider"
| "programmatic-scroll";
export function resolveCompetingInitialScrollOwner(params: {
hasPendingLayoutRestore: boolean;
hasExplicitScrollTarget: boolean;
hasRecoveryReveal: boolean;
hasUnreadDivider: boolean;
isProgrammaticScrollLocked: () => boolean;
}): CompetingInitialScrollOwner | null {
if (params.hasPendingLayoutRestore) return "layout-restore";
if (params.hasExplicitScrollTarget) return "explicit-target";
if (params.hasRecoveryReveal) return "recovery";
if (params.hasUnreadDivider) return "unread-divider";
if (params.isProgrammaticScrollLocked()) return "programmatic-scroll";
return null;
}
function resolveInitialPlacementOwner(
enabled: boolean,
recoveryRevealKey: string | null,
): "recovery" | "bottom" | "saved-position" {
if (recoveryRevealKey !== null) return "recovery";
function resolveInitialPlacementOwner(enabled: boolean): "bottom" | "saved-position" {
return enabled ? "bottom" : "saved-position";
}
@@ -1381,13 +1271,12 @@ function reportInitialPlacement(params: {
sessionId: string | null;
token: number | null;
enabled: boolean;
recoveryRevealKey: string | null;
}): void {
if (!isDebug()) return;
placementDebug(`${params.phase} placement`, {
sessionId: params.sessionId,
token: params.token,
owner: resolveInitialPlacementOwner(params.enabled, params.recoveryRevealKey),
owner: resolveInitialPlacementOwner(params.enabled),
scrollTop: params.element.scrollTop,
scrollHeight: params.element.scrollHeight,
clientHeight: params.element.clientHeight,
@@ -1434,8 +1323,6 @@ function applyInitialScrollPosition(params: InitialScrollApplyParams): void {
isNearBottomRef,
envSwitchPlacementToken,
hasUnreadDivider,
recoveryRevealKey,
recoveryPlacementPendingRef,
isProgrammaticScrollLocked,
phase,
} = params;
@@ -1455,18 +1342,17 @@ function applyInitialScrollPosition(params: InitialScrollApplyParams): void {
const competingOwner = resolveCompetingInitialScrollOwner({
hasPendingLayoutRestore,
hasExplicitScrollTarget,
hasRecoveryReveal: recoveryRevealKey !== null,
hasUnreadDivider,
isProgrammaticScrollLocked,
});
if (competingOwner && competingOwner !== "recovery") {
if (competingOwner) {
if (phase === "provisional") return;
placementDebug(`${phase} placement delegated`, {
sessionId,
owner: competingOwner,
token: envSwitchPlacementToken,
});
markInitialScrollConsumed(didInitialScroll, activationPendingRef, recoveryPlacementPendingRef);
markInitialScrollConsumed(didInitialScroll, activationPendingRef);
completeEnvSwitchPlacement(envSwitchPlacementToken);
return;
}
@@ -1475,17 +1361,14 @@ function applyInitialScrollPosition(params: InitialScrollApplyParams): void {
getStoredAutoScrollTop(sessionId) ??
undefined)
: undefined;
const scrollTop =
competingOwner === "recovery"
? 0
: resolveNativeInitialScrollTop({
enabled,
hasPendingLayoutRestore,
savedScrollTop,
scrollHeight: element.scrollHeight,
});
const scrollTop = resolveNativeInitialScrollTop({
enabled,
hasPendingLayoutRestore,
savedScrollTop,
scrollHeight: element.scrollHeight,
});
if (phase === "final") {
markInitialScrollConsumed(didInitialScroll, activationPendingRef, recoveryPlacementPendingRef);
markInitialScrollConsumed(didInitialScroll, activationPendingRef);
}
if (scrollTop === null) {
completeFinalEnvSwitchPlacement(phase, envSwitchPlacementToken);
@@ -1507,7 +1390,6 @@ function applyInitialScrollPosition(params: InitialScrollApplyParams): void {
sessionId,
token: envSwitchPlacementToken,
enabled,
recoveryRevealKey,
});
finishAppliedInitialPlacement({
phase,
@@ -1528,31 +1410,16 @@ function shouldSkipProvisionalPlacement(
return phase === "provisional" && (hasUnreadDivider || appliedToken === currentToken);
}
function useInitialPlacementLatches(
envSwitchPlacementToken: number | null,
recoveryRevealKey: string | null,
) {
function useInitialPlacementLatches(envSwitchPlacementToken: number | null) {
const didInitialScroll = useRef(false);
const lastTokenRef = useRef<number | null>(null);
const provisionalTokenRef = useRef<number | null>(null);
const lastRecoveryRevealKeyRef = useRef<string | null>(null);
const recoveryPlacementPendingRef = useRef(recoveryRevealKey !== null);
if (envSwitchPlacementToken !== null && envSwitchPlacementToken !== lastTokenRef.current) {
lastTokenRef.current = envSwitchPlacementToken;
provisionalTokenRef.current = null;
didInitialScroll.current = false;
}
if (recoveryRevealKey !== null && recoveryRevealKey !== lastRecoveryRevealKeyRef.current) {
lastRecoveryRevealKeyRef.current = recoveryRevealKey;
provisionalTokenRef.current = null;
didInitialScroll.current = false;
recoveryPlacementPendingRef.current = true;
}
if (recoveryRevealKey === null) {
lastRecoveryRevealKeyRef.current = null;
recoveryPlacementPendingRef.current = false;
}
return { didInitialScroll, provisionalTokenRef, recoveryPlacementPendingRef };
return { didInitialScroll, provisionalTokenRef };
}
type InitialPlacementLatches = ReturnType<typeof useInitialPlacementLatches>;
@@ -1569,7 +1436,6 @@ type InitialScrollPositionParams = {
envSwitchPlacementToken: number | null;
isRestoringLayout: boolean;
isProgrammaticScrollLocked: () => boolean;
recoveryRevealKey: string | null;
placementLatches: InitialPlacementLatches;
};
@@ -1579,7 +1445,6 @@ type InitialScrollPositionEffectParams = Omit<InitialScrollPositionParams, "plac
provisionalTokenRef: React.RefObject<number | null>;
isVisibleRef: React.RefObject<boolean>;
activationPendingRef: React.RefObject<boolean>;
recoveryPlacementPendingRef: React.RefObject<boolean>;
};
function runInitialScrollPositionEffect(
@@ -1597,19 +1462,17 @@ function runInitialScrollPositionEffect(
envSwitchPlacementToken,
isRestoringLayout,
isProgrammaticScrollLocked,
recoveryRevealKey,
storeApi,
didInitialScroll,
provisionalTokenRef,
isVisibleRef,
activationPendingRef,
recoveryPlacementPendingRef,
} = params;
if (!isVisible || (envSwitchPlacementToken !== null && isRestoringLayout)) return;
const hasEmptyTranscript = itemCount === 0 && recoveryRevealKey === null;
const hasEmptyTranscript = itemCount === 0;
if (envSwitchPlacementToken !== null && hasEmptyTranscript) {
if (historyRefreshPending) return;
markInitialScrollConsumed(didInitialScroll, activationPendingRef, recoveryPlacementPendingRef);
markInitialScrollConsumed(didInitialScroll, activationPendingRef);
completeEnvSwitchPlacement(envSwitchPlacementToken);
return;
}
@@ -1640,8 +1503,6 @@ function runInitialScrollPositionEffect(
isNearBottomRef,
envSwitchPlacementToken,
hasUnreadDivider,
recoveryRevealKey,
recoveryPlacementPendingRef,
isProgrammaticScrollLocked,
phase,
});
@@ -1664,11 +1525,10 @@ function useInitialScrollPosition({
envSwitchPlacementToken,
isRestoringLayout,
isProgrammaticScrollLocked,
recoveryRevealKey,
placementLatches,
}: InitialScrollPositionParams) {
const storeApi = useAppStoreApi();
const { didInitialScroll, provisionalTokenRef, recoveryPlacementPendingRef } = placementLatches;
const { didInitialScroll, provisionalTokenRef } = placementLatches;
const { isVisibleRef, activationPendingRef } = useActivationPending(
isVisible,
envSwitchPlacementToken,
@@ -1687,13 +1547,11 @@ function useInitialScrollPosition({
envSwitchPlacementToken,
isRestoringLayout,
isProgrammaticScrollLocked,
recoveryRevealKey,
storeApi,
didInitialScroll,
provisionalTokenRef,
isVisibleRef,
activationPendingRef,
recoveryPlacementPendingRef,
}),
[
itemCount,
@@ -1707,7 +1565,6 @@ function useInitialScrollPosition({
envSwitchPlacementToken,
isRestoringLayout,
isProgrammaticScrollLocked,
recoveryRevealKey,
scrollRef,
],
);
@@ -1728,7 +1585,6 @@ type NativeScrollManagementParams = {
isLoadingMore: boolean;
loadMore: () => Promise<number>;
isVisible: boolean;
recoveryRevealKey?: string | null;
};
/**
@@ -1755,7 +1611,6 @@ export function useNativeScrollManagement(params: NativeScrollManagementParams)
isLoadingMore,
loadMore,
isVisible,
recoveryRevealKey = null,
} = params;
const pendingChatInitialPlacement = useDockviewStore(
(state) => state.pendingChatInitialPlacement,
@@ -1763,9 +1618,8 @@ export function useNativeScrollManagement(params: NativeScrollManagementParams)
const isRestoringLayout = useDockviewStore((state) => state.isRestoringLayout);
const envSwitchPlacementToken =
pendingChatInitialPlacement?.sessionId === sessionId ? pendingChatInitialPlacement.token : null;
const placementLatches = useInitialPlacementLatches(envSwitchPlacementToken, recoveryRevealKey);
const initialPlacementPending =
envSwitchPlacementToken !== null || placementLatches.recoveryPlacementPendingRef.current;
const placementLatches = useInitialPlacementLatches(envSwitchPlacementToken);
const initialPlacementPending = envSwitchPlacementToken !== null;
const programmaticScrollLockRef = useRef(false);
const isProgrammaticScrollLocked = useCallback(() => programmaticScrollLockRef.current, []);
const { isNearBottomRef, resyncIsNearBottom, markNotNearBottom } = useAutoScroll({
@@ -1790,7 +1644,6 @@ export function useNativeScrollManagement(params: NativeScrollManagementParams)
items,
isLoadingMore,
isProgrammaticScrollLocked,
recoveryRevealKey,
);
const loadMoreWithPrependBaseline = useCallback(() => {
beginOlderLoad();
@@ -1825,7 +1678,6 @@ export function useNativeScrollManagement(params: NativeScrollManagementParams)
envSwitchPlacementToken,
isRestoringLayout,
isProgrammaticScrollLocked,
recoveryRevealKey,
placementLatches,
});
@@ -86,11 +86,6 @@ const HARNESS_RENDER_ERROR = "harness did not render";
const NATIVE_SCROLL_MANAGEMENT_TEST_ID = "native-scroll-management-container";
const AUTO_SCROLL_CONTAINER_TEST_ID = "auto-scroll-container";
const CACHED_MESSAGE_ID = "cached-message";
const RECOVERY_SESSION_ID = "session-recovery";
const RECOVERY_MESSAGE_ID = "recovery-message";
const OLDEST_ACTIVITY_ID = "activity-old";
const NEWEST_MESSAGE_ID = "newest";
const OLDER_MESSAGE_ID = "older";
const TEST_MESSAGES = [{} as Message];
/** Always returns false: the harness never locks programmatic scrolling. */
const NEVER_LOCKED = () => false;
@@ -114,7 +109,6 @@ function Harness({
enabled = true,
sessionId = null,
isProgrammaticScrollLocked = NEVER_LOCKED,
recoveryRevealKey = null,
}: {
itemCount: number;
anchoredBarOffsetPx: number;
@@ -127,7 +121,6 @@ function Harness({
enabled?: boolean;
sessionId?: string | null;
isProgrammaticScrollLocked?: () => boolean;
recoveryRevealKey?: string | null;
}) {
const scrollRef = useRef<HTMLDivElement>(null);
useLayoutEffect(() => {
@@ -155,7 +148,6 @@ function Harness({
sessionId,
isProgrammaticScrollLocked,
isVisible,
recoveryRevealKey,
} as Parameters<typeof useScrollToDividerOrBottom>[4];
useScrollToDividerOrBottom(scrollRef, itemCount, dividerKey, anchoredBarOffsetPx, scrollOptions);
return (
@@ -290,7 +282,6 @@ function NativeScrollManagementHarness({
historyRefreshPending = false,
hasUnreadDivider = false,
hasMore = true,
recoveryRevealKey = null,
}: {
items: RenderItem[];
messages?: Message[];
@@ -304,7 +295,6 @@ function NativeScrollManagementHarness({
historyRefreshPending?: boolean;
hasUnreadDivider?: boolean;
hasMore?: boolean;
recoveryRevealKey?: string | null;
}) {
const scrollRef = useRef<HTMLDivElement>(null);
useNativeScrollMetrics(scrollRef, metrics);
@@ -322,7 +312,6 @@ function NativeScrollManagementHarness({
loadMore,
isVisible,
historyRefreshPending,
recoveryRevealKey,
});
if (recoveryRef) recoveryRef.current = showRecovery;
return (
@@ -371,301 +360,6 @@ describe("isElementInPreloadRegion", () => {
// eslint-disable-next-line max-lines-per-function -- pagination invariants share one fixture and lifecycle.
describe("useNativeScrollManagement transcript pagination", () => {
it("reveals recovery at the top once per error stamp and preserves same-stamp scrolling", () => {
const metrics = { scrollHeight: 1000, scrollTop: 240, clientHeight: 400 };
const { rerender } = render(
<NativeScrollManagementHarness
items={[transcriptMessage(RECOVERY_MESSAGE_ID)]}
metrics={metrics}
sessionId={RECOVERY_SESSION_ID}
enabled
recoveryRevealKey="session-recovery:bootstrap-1"
/>,
);
expect(metrics.scrollTop).toBe(0);
metrics.scrollTop = 240;
rerender(
<NativeScrollManagementHarness
items={[transcriptMessage(RECOVERY_MESSAGE_ID)]}
metrics={metrics}
sessionId={RECOVERY_SESSION_ID}
enabled
recoveryRevealKey="session-recovery:bootstrap-1"
/>,
);
expect(metrics.scrollTop).toBe(240);
rerender(
<NativeScrollManagementHarness
items={[transcriptMessage(RECOVERY_MESSAGE_ID)]}
metrics={metrics}
sessionId={RECOVERY_SESSION_ID}
enabled
/>,
);
expect(metrics.scrollTop).toBe(240);
rerender(
<NativeScrollManagementHarness
items={[transcriptMessage(RECOVERY_MESSAGE_ID)]}
metrics={metrics}
sessionId={RECOVERY_SESSION_ID}
enabled
recoveryRevealKey="session-recovery:bootstrap-1"
/>,
);
expect(metrics.scrollTop).toBe(0);
metrics.scrollTop = 240;
rerender(
<NativeScrollManagementHarness
items={[transcriptMessage(RECOVERY_MESSAGE_ID)]}
metrics={metrics}
sessionId={RECOVERY_SESSION_ID}
enabled
recoveryRevealKey="session-recovery:bootstrap-2"
/>,
);
expect(metrics.scrollTop).toBe(0);
});
// eslint-disable-next-line max-lines-per-function -- recovery ownership and the subsequent prepend handoff share one controlled lifecycle.
it("keeps recovery visible while an older page settles, then resumes normal anchoring after user scroll", async () => {
const page = Promise.withResolvers<number>();
const metrics = { scrollHeight: 100, scrollTop: 0, clientHeight: 50 };
const activity = transcriptActivity(OLDEST_ACTIVITY_ID, "turn-1");
const newest = transcriptMessage(NEWEST_MESSAGE_ID);
const { rerender } = render(
<NativeScrollManagementHarness
items={[activity, newest]}
metrics={metrics}
sessionId={RECOVERY_SESSION_ID}
enabled
loadMore={() => page.promise}
recoveryRevealKey="session-recovery:failure-1"
/>,
);
const scroller = screen.getByTestId(NATIVE_SCROLL_MANAGEMENT_TEST_ID);
const wrappedLoadMore = sharedSentinelCalls.at(-1)?.[4] as () => Promise<number>;
const pendingLoad = wrappedLoadMore();
rerender(
<NativeScrollManagementHarness
items={[activity, newest]}
metrics={metrics}
sessionId={RECOVERY_SESSION_ID}
enabled
isLoadingMore
loadMore={() => page.promise}
recoveryRevealKey="session-recovery:failure-1"
/>,
);
metrics.scrollHeight = 200;
rerender(
<NativeScrollManagementHarness
items={[transcriptMessage(OLDER_MESSAGE_ID), activity, newest]}
metrics={metrics}
sessionId={RECOVERY_SESSION_ID}
enabled
isLoadingMore
loadMore={() => page.promise}
recoveryRevealKey="session-recovery:failure-1"
/>,
);
expect(metrics.scrollTop).toBe(0);
page.resolve(1);
await act(async () => {
await pendingLoad;
});
rerender(
<NativeScrollManagementHarness
items={[transcriptMessage(OLDER_MESSAGE_ID), activity, newest]}
metrics={metrics}
sessionId={RECOVERY_SESSION_ID}
enabled
loadMore={() => page.promise}
recoveryRevealKey="session-recovery:failure-1"
/>,
);
expect(metrics.scrollTop).toBe(0);
const nextPage = Promise.withResolvers<number>();
const nextLoadMore = sharedSentinelCalls.at(-1)?.[4] as () => Promise<number>;
const pendingNextLoad = nextLoadMore();
rerender(
<NativeScrollManagementHarness
items={[transcriptMessage(OLDER_MESSAGE_ID), activity, newest]}
metrics={metrics}
sessionId={RECOVERY_SESSION_ID}
enabled
isLoadingMore
loadMore={() => nextPage.promise}
recoveryRevealKey="session-recovery:failure-1"
/>,
);
act(() => {
scroller.dispatchEvent(new WheelEvent("wheel", { deltaY: 100 }));
metrics.scrollTop = 40;
scroller.dispatchEvent(new Event("scroll"));
});
expect(metrics.scrollTop).toBe(40);
metrics.scrollHeight = 300;
rerender(
<NativeScrollManagementHarness
items={[
transcriptMessage("older-2"),
transcriptMessage(OLDER_MESSAGE_ID),
activity,
newest,
]}
metrics={metrics}
sessionId={RECOVERY_SESSION_ID}
enabled
isLoadingMore
loadMore={() => nextPage.promise}
recoveryRevealKey="session-recovery:failure-1"
/>,
);
expect(metrics.scrollTop).toBe(140);
nextPage.resolve(1);
await act(async () => {
await pendingNextLoad;
});
rerender(
<NativeScrollManagementHarness
items={[
transcriptMessage("older-2"),
transcriptMessage(OLDER_MESSAGE_ID),
activity,
newest,
]}
metrics={metrics}
sessionId={RECOVERY_SESSION_ID}
enabled
loadMore={() => nextPage.promise}
recoveryRevealKey="session-recovery:failure-1"
/>,
);
expect(metrics.scrollTop).toBe(140);
});
it("keeps an already-loading older page below a newly revealed recovery card", async () => {
const page = Promise.withResolvers<number>();
const metrics = { scrollHeight: 100, scrollTop: 0, clientHeight: 50 };
const activity = transcriptActivity(OLDEST_ACTIVITY_ID, "turn-1");
const newest = transcriptMessage(NEWEST_MESSAGE_ID);
const { rerender } = render(
<NativeScrollManagementHarness
items={[activity, newest]}
metrics={metrics}
sessionId={RECOVERY_SESSION_ID}
enabled
loadMore={() => page.promise}
/>,
);
const wrappedLoadMore = sharedSentinelCalls.at(-1)?.[4] as () => Promise<number>;
const pendingLoad = wrappedLoadMore();
rerender(
<NativeScrollManagementHarness
items={[activity, newest]}
metrics={metrics}
sessionId={RECOVERY_SESSION_ID}
enabled
isLoadingMore
loadMore={() => page.promise}
/>,
);
rerender(
<NativeScrollManagementHarness
items={[activity, newest]}
metrics={metrics}
sessionId={RECOVERY_SESSION_ID}
enabled
isLoadingMore
loadMore={() => page.promise}
recoveryRevealKey="session-recovery:failure-2"
/>,
);
expect(metrics.scrollTop).toBe(0);
metrics.scrollHeight = 200;
rerender(
<NativeScrollManagementHarness
items={[transcriptMessage(OLDER_MESSAGE_ID), activity, newest]}
metrics={metrics}
sessionId={RECOVERY_SESSION_ID}
enabled
isLoadingMore
loadMore={() => page.promise}
recoveryRevealKey="session-recovery:failure-2"
/>,
);
expect(metrics.scrollTop).toBe(0);
page.resolve(1);
await act(async () => {
await pendingLoad;
});
rerender(
<NativeScrollManagementHarness
items={[transcriptMessage(OLDER_MESSAGE_ID), activity, newest]}
metrics={metrics}
sessionId={RECOVERY_SESSION_ID}
enabled
loadMore={() => page.promise}
recoveryRevealKey="session-recovery:failure-2"
/>,
);
expect(metrics.scrollTop).toBe(0);
});
it("resumes normal auto-scroll after the initial recovery placement is consumed", () => {
const metrics = { scrollHeight: 100, scrollTop: 0, clientHeight: 50 };
const firstMessage = { id: "first-message" } as Message;
const scrollerMessages = [firstMessage];
const activity = transcriptActivity(OLDEST_ACTIVITY_ID, "turn-1");
const newest = transcriptMessage(NEWEST_MESSAGE_ID);
const { rerender } = render(
<NativeScrollManagementHarness
items={[activity, newest]}
messages={scrollerMessages}
metrics={metrics}
sessionId={RECOVERY_SESSION_ID}
enabled
hasMore={false}
recoveryRevealKey="session-recovery:placement-1"
/>,
);
const scroller = screen.getByTestId(NATIVE_SCROLL_MANAGEMENT_TEST_ID);
metrics.scrollTop = 50;
act(() => {
scroller.dispatchEvent(new WheelEvent("wheel", { deltaY: 100 }));
scroller.dispatchEvent(new Event("scroll"));
});
metrics.scrollHeight = 150;
rerender(
<NativeScrollManagementHarness
items={[activity, newest]}
messages={[...scrollerMessages, { id: "second-message" } as Message]}
metrics={metrics}
sessionId={RECOVERY_SESSION_ID}
enabled
hasMore={false}
recoveryRevealKey="session-recovery:placement-1"
/>,
);
expect(metrics.scrollTop).toBe(2_147_483_647);
});
// @covers AC-UI-TRANSCRIPT-AUTO-SCROLL-001.11
// @covers AC-UI-TRANSCRIPT-AUTO-SCROLL-001.14
it("places cached enabled history before refresh and reconciles after it settles", () => {
@@ -1049,48 +743,6 @@ describe("useNativeScrollManagement transcript pagination", () => {
expect(sharedSentinelRecheck).not.toHaveBeenCalled();
});
it("does not recheck a restored transcript while explicit recovery is active", () => {
const requestAnimationFrame = vi
.spyOn(window, "requestAnimationFrame")
.mockImplementation((callback) => {
callback(0);
return 1;
});
const recoveryRef = { current: false };
const { rerender } = render(
<NativeScrollManagementHarness
items={[]}
sessionId="session-1"
recoveryRef={recoveryRef}
isVisible={false}
/>,
);
const options = sharedSentinelCalls.at(-1)?.[5] as {
onLoadSettled: (result: {
count: number;
rejected: boolean;
continuation: "no-progress";
}) => void;
};
act(() => {
options.onLoadSettled({ count: 0, rejected: false, continuation: "no-progress" });
});
sharedSentinelRecheck.mockClear();
rerender(
<NativeScrollManagementHarness
items={[]}
sessionId="session-1"
recoveryRef={recoveryRef}
isVisible
/>,
);
expect(recoveryRef.current).toBe(true);
expect(sharedSentinelRecheck).not.toHaveBeenCalled();
requestAnimationFrame.mockRestore();
});
it("retries a disarmed short page on the next upward scroll", () => {
const metrics = { scrollHeight: 1000, scrollTop: 100, clientHeight: 400 };
render(<NativeScrollManagementHarness items={[]} metrics={metrics} />);
@@ -1351,18 +1003,10 @@ describe("useNativeScrollManagement transcript pagination", () => {
// eslint-disable-next-line max-lines-per-function -- this suite keeps the related scroll invariants together.
describe("useScrollToDividerOrBottom — anchored-bar offset", () => {
it("leaves an active recovery transcript at the top for its first reveal", () => {
render(
<Harness
itemCount={2}
anchoredBarOffsetPx={0}
dividerKey={null}
scrollHeight={1000}
recoveryRevealKey="session-1:bootstrap-1"
/>,
);
it("places an enabled transcript at the bottom on its first render", () => {
render(<Harness itemCount={2} anchoredBarOffsetPx={0} dividerKey={null} scrollHeight={1000} />);
expect(screen.getByTestId(DIVIDER_SCROLL_CONTAINER_TEST_ID).scrollTop).toBe(0);
expect(screen.getByTestId(DIVIDER_SCROLL_CONTAINER_TEST_ID).scrollTop).toBe(1000);
});
it("waits for an inactive transcript to become visible before placing the initial view", () => {
@@ -2,15 +2,7 @@
/* eslint-disable max-lines -- native transcript composition owns scrolling. */
import {
useEffect,
useMemo,
useRef,
memo,
forwardRef,
useImperativeHandle,
type ReactNode,
} from "react";
import { useEffect, useMemo, useRef, memo, forwardRef, useImperativeHandle } from "react";
import { SessionPanelContent } from "@kandev/ui/pannel-session";
import type { Message, TaskSessionState } from "@/lib/types/http";
import type { RenderItem } from "@/hooks/use-processed-messages";
@@ -110,7 +102,6 @@ type NativeMessageListScrollParams = {
/** Changes when transcript status rows can add/remove space above messages. */
scrollLayoutKey: string;
isVisible: boolean;
recoveryRevealKey?: string | null;
};
type ScrollToDividerOptions = {
@@ -121,22 +112,8 @@ type ScrollToDividerOptions = {
isProgrammaticScrollLocked?: () => boolean;
isVisible?: boolean;
historyRefreshPending?: boolean;
recoveryRevealKey?: string | null;
};
function consumeRecoveryRevealForDivider(
recoveryRevealKey: string | null,
didInitialScroll: React.RefObject<boolean>,
activationPendingRef: React.RefObject<boolean>,
): boolean {
if (recoveryRevealKey === null) return false;
// The native initial-position hook owns the recovery reveal. Mark this
// hook consumed so its normal bottom/divider placement cannot override it.
didInitialScroll.current = true;
activationPendingRef.current = false;
return true;
}
function useNativeMessageListScroll(params: NativeMessageListScrollParams) {
const {
scrollRef,
@@ -159,7 +136,6 @@ function useNativeMessageListScroll(params: NativeMessageListScrollParams) {
onFirstMessageHiddenChange,
scrollLayoutKey,
isVisible,
recoveryRevealKey,
} = params;
const {
handleScrollToMessage,
@@ -182,7 +158,6 @@ function useNativeMessageListScroll(params: NativeMessageListScrollParams) {
isLoadingMore,
loadMore,
isVisible,
recoveryRevealKey,
});
const anchoredBarOffsetPx = anchoredBarScrollOffsetPx(anchoredBarHeight);
useEffect(() => {
@@ -196,7 +171,6 @@ function useNativeMessageListScroll(params: NativeMessageListScrollParams) {
isProgrammaticScrollLocked,
isVisible,
historyRefreshPending,
recoveryRevealKey,
});
useImperativeHandle(ref, () => ({ scrollToMessage: handleScrollToMessage }), [
handleScrollToMessage,
@@ -304,7 +278,6 @@ type NativeMessageListBodyProps = {
onScrollToMessage: (messageId: string, options?: { align?: "start" | "center" }) => void;
autoScrollEnabled: boolean;
dividerBeforeItemKey?: string | null;
prependContent?: ReactNode;
launchErrorOwned: boolean;
launchErrorStamp?: string;
launchErrorOccurredAt?: string;
@@ -346,7 +319,7 @@ type NativeMessageListBodyProps = {
* keep their existing visible-host behavior without participating in read
* tracking.
*/
// eslint-disable-next-line max-lines-per-function -- divider and recovery placement share one scroll lifecycle.
// eslint-disable-next-line max-lines-per-function -- divider and initial placement share one scroll lifecycle.
export function useScrollToDividerOrBottom(
scrollRef: React.RefObject<HTMLDivElement | null>,
itemCount: number,
@@ -362,7 +335,6 @@ export function useScrollToDividerOrBottom(
isProgrammaticScrollLocked = () => false,
isVisible = true,
historyRefreshPending = false,
recoveryRevealKey = null,
} = options;
const { isVisibleRef, activationPendingRef } = useActivationPending(isVisible);
const isUserScrollingRef = useDividerUserScrolling(scrollRef);
@@ -393,8 +365,6 @@ export function useScrollToDividerOrBottom(
}
const el = scrollRef.current;
if (!el || historyRefreshPending) return;
if (consumeRecoveryRevealForDivider(recoveryRevealKey, didInitialScroll, activationPendingRef))
return;
if (itemCount === 0) return;
const placeInitialPosition = () => {
@@ -464,7 +434,6 @@ export function useScrollToDividerOrBottom(
isProgrammaticScrollLocked,
isVisible,
historyRefreshPending,
recoveryRevealKey,
scrollRef,
]);
}
@@ -520,14 +489,12 @@ function NativeMessageListBody({
onScrollToMessage,
autoScrollEnabled,
dividerBeforeItemKey,
prependContent,
launchErrorOwned,
launchErrorStamp,
launchErrorOccurredAt,
}: NativeMessageListBodyProps) {
return (
<div className="p-4">
{prependContent ? <div className="mb-4">{prependContent}</div> : null}
{/* Sentinel for lazy loading older messages */}
{hasMore && <div ref={sentinelRef} className="h-px" />}
@@ -623,8 +590,6 @@ export const NativeMessageList = memo(
launchErrorOwned = false,
launchErrorStamp,
launchErrorOccurredAt,
prependContent,
recoveryRevealKey,
}: MessageListProps,
ref,
) {
@@ -699,7 +664,6 @@ export const NativeMessageList = memo(
isWorking,
].join(":"),
isVisible,
recoveryRevealKey,
});
return (
@@ -739,7 +703,6 @@ export const NativeMessageList = memo(
onScrollToMessage={handleScrollToMessage}
autoScrollEnabled={autoScrollEnabled}
dividerBeforeItemKey={dividerBeforeItemKey}
prependContent={prependContent}
launchErrorOwned={launchErrorOwned}
launchErrorStamp={launchErrorStamp}
launchErrorOccurredAt={launchErrorOccurredAt}
@@ -64,10 +64,6 @@ export type MessageListProps = {
* top — the desktop-only, opt-in anchored last-prompt bar. `null`/`undefined`
* when the setting is off or on mobile. */
stickyPromptBar?: ReactNode;
/** Rendered above transcript status and messages inside the native viewport. */
prependContent?: ReactNode;
/** Identity of the active recovery content for one-time initial reveal. */
recoveryRevealKey?: string | null;
/** Current rendered height (px) of the anchored last-prompt bar's pinned
* overlay, or 0/undefined when it isn't showing. Lets a target scrolled
* to the top of the transcript (e.g. the unread "New" divider) reserve
@@ -11,6 +11,9 @@ export type ActionMeta = {
action_visibility?: "running";
variant?: string;
recovery_actions?: boolean;
scope?: "session" | "task";
error_stamp?: string;
recovery_stamp?: string;
is_auth_error?: boolean;
auth_methods?: RecoveryAuthMethod[];
error_output?: string;
@@ -123,23 +123,25 @@ function renderWithTranscript(
});
}
describe("ActionMessage — recovery card retires once the agent is back", () => {
it("hides the card when a resume re-established the agent after the failure", () => {
describe("ActionMessage — recovery history remains after the agent is back", () => {
it("retains the entry without controls when a resume re-established the agent", () => {
// A resumed agent settles at WAITING_FOR_INPUT, not RUNNING, so the session
// state alone never retires the card.
renderWithTranscript("WAITING_FOR_INPUT", [bootMessage(BOOTED_AFTER_FAILURE)]);
expect(screen.queryByText(RECOVERY_MESSAGE)).toBeNull();
expect(screen.getByText(RECOVERY_MESSAGE)).toBeTruthy();
expect(screen.queryByTestId(RESUME_TEST_ID)).toBeNull();
expect(screen.queryByTestId(FRESH_TEST_ID)).toBeNull();
});
it("hides the card when a fresh start re-established the agent after the failure", () => {
it("retains the entry without controls when a fresh start re-established the agent", () => {
renderWithTranscript("WAITING_FOR_INPUT", [
bootMessage(BOOTED_AFTER_FAILURE, { is_resuming: false }),
]);
expect(screen.getByText(RECOVERY_MESSAGE)).toBeTruthy();
expect(screen.queryByTestId(RESUME_TEST_ID)).toBeNull();
expect(screen.queryByTestId(FRESH_TEST_ID)).toBeNull();
});
it("keeps the card visible when the resume attempt failed", () => {
@@ -314,7 +316,7 @@ describe("ActionMessage — a recovery that failed keeps its controls", () => {
fireEvent.click(screen.getByTestId(RESUME_TEST_ID));
await waitFor(() => expect(requestMock).toHaveBeenCalledTimes(1));
await waitFor(() => expect(screen.queryByText(RECOVERY_MESSAGE)).toBeNull());
await waitFor(() => expect(screen.queryByTestId(RESUME_TEST_ID)).toBeNull());
// The launch was accepted, so the card hid; the agent then failed to come up.
live.setSessionState("STARTING");
@@ -329,7 +331,7 @@ describe("ActionMessage — a recovery that failed keeps its controls", () => {
const live = renderWithLiveStore([]);
fireEvent.click(screen.getByTestId(RESUME_TEST_ID));
await waitFor(() => expect(screen.queryByText(RECOVERY_MESSAGE)).toBeNull());
await waitFor(() => expect(screen.queryByTestId(RESUME_TEST_ID)).toBeNull());
live.setSessionState("FAILED");
@@ -341,13 +343,13 @@ describe("ActionMessage — a recovery that failed keeps its controls", () => {
const live = renderWithLiveStore([]);
fireEvent.click(screen.getByTestId(RESUME_TEST_ID));
await waitFor(() => expect(screen.queryByText(RECOVERY_MESSAGE)).toBeNull());
await waitFor(() => expect(screen.queryByTestId(RESUME_TEST_ID)).toBeNull());
live.setSessionState("STARTING");
live.setMessages([bootMessage(BOOTED_AFTER_FAILURE)]);
live.setSessionState("WAITING_FOR_INPUT");
expect(screen.queryByText(RECOVERY_MESSAGE)).toBeNull();
expect(screen.getByText(RECOVERY_MESSAGE)).toBeTruthy();
expect(screen.queryByTestId(RESUME_TEST_ID)).toBeNull();
});
});
@@ -1,3 +1,4 @@
/* eslint-disable max-lines -- recovery action variants share one rendering harness. */
import { afterEach, describe, expect, it, vi } from "vitest";
import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
import { StateProvider, useAppStoreApi } from "@/components/state-provider";
@@ -142,12 +143,17 @@ function renderAction(
sessionState?: TaskSessionState,
sessionError?: string,
activeTurnId?: string,
sessionMetadata?: Record<string, unknown> | null,
) {
const initialState: Partial<AppState> = sessionState
? {
taskSessions: {
items: {
[TEST_SESSION_ID]: { state: sessionState, error_message: sessionError } as TaskSession,
[TEST_SESSION_ID]: {
state: sessionState,
error_message: sessionError,
metadata: sessionMetadata,
} as TaskSession,
},
},
turns: {
@@ -288,21 +294,68 @@ describe("ActionMessage — transient retry (warning variant)", () => {
renderAction(errorMsg, "WAITING_FOR_INPUT", "");
expect(screen.getByTestId(RESUME_TEST_ID)).toBeTruthy();
});
});
it("hides a recovery card after its Resume request succeeds", async () => {
describe("ActionMessage — session recovery history", () => {
it("keeps the recovery entry after its Resume request succeeds and removes controls", async () => {
const errorMsg = recoveryMessage(true);
renderAction(errorMsg, "WAITING_FOR_INPUT", "");
fireEvent.click(screen.getByTestId(RESUME_TEST_ID));
await waitFor(() => expect(screen.queryByText(RECOVERY_MESSAGE)).toBeNull());
await waitFor(() => expect(screen.getByText(RECOVERY_MESSAGE)).toBeTruthy());
expect(screen.queryByTestId(RESUME_TEST_ID)).toBeNull();
});
it("keeps the recovery card hidden after a successful resume settles back to waiting", async () => {
it("shows recovery controls only for the current stamped failure", () => {
const historical = recoveryMessage(true);
historical.metadata = {
...(historical.metadata as Record<string, unknown>),
error_stamp: "failure-old",
};
const current = recoveryMessage(true);
current.metadata = {
...(current.metadata as Record<string, unknown>),
error_stamp: "failure-current",
};
const { rerender } = renderAction(historical, "WAITING_FOR_INPUT", "", undefined, {
last_agent_error: {
message: "The newer session failure.",
stamp: "failure-current",
},
});
expect(screen.getByText(RECOVERY_MESSAGE)).toBeTruthy();
expect(screen.queryByTestId(RESUME_TEST_ID)).toBeNull();
rerender(<ActionMessage comment={current} />);
expect(screen.getByTestId(RESUME_TEST_ID)).toBeTruthy();
});
it("keeps controls for an unstamped legacy row matching the current failure", () => {
const legacy = recoveryMessage(true);
legacy.content = "Agent encountered an error: The agent could not start.";
legacy.metadata = {
...(legacy.metadata as Record<string, unknown>),
error_stamp: undefined,
};
renderAction(legacy, "WAITING_FOR_INPUT", "", undefined, {
last_agent_error: {
message: "The agent could not start.",
occurred_at: legacy.created_at,
},
});
expect(screen.getByTestId(RESUME_TEST_ID)).toBeTruthy();
});
it("keeps the historical recovery entry after a successful resume settles back to waiting", async () => {
const errorMsg = recoveryMessage(true);
const { setSessionState } = renderActionWithStore(errorMsg, "WAITING_FOR_INPUT", "");
fireEvent.click(screen.getByTestId(RESUME_TEST_ID));
await waitFor(() => expect(screen.queryByText(RECOVERY_MESSAGE)).toBeNull());
await waitFor(() => expect(screen.getByText(RECOVERY_MESSAGE)).toBeTruthy());
// A successful resume drives the session through an active state (which
// hides the card via isSessionActive) and then back to WAITING_FOR_INPUT
@@ -312,7 +365,7 @@ describe("ActionMessage — transient retry (warning variant)", () => {
setSessionState("STARTING");
setSessionState("WAITING_FOR_INPUT");
expect(screen.queryByText(RECOVERY_MESSAGE)).toBeNull();
expect(screen.getByText(RECOVERY_MESSAGE)).toBeTruthy();
expect(screen.queryByTestId(RESUME_TEST_ID)).toBeNull();
});
@@ -13,11 +13,62 @@ import { parseRetryAt, retryCountdownLabel } from "./transient-retry";
import { hasSessionRecoveryResolutionAfter } from "@/hooks/processed-message-filtering";
import { ActionButtons } from "./action-message-actions";
import { SessionRecoveryActionButtons, sessionRecoveryAction } from "./action-message-recovery";
import {
lastAgentErrorStamp,
readLastAgentError,
type LastAgentError,
} from "@/lib/session-last-agent-error";
import { legacyRecoveryMessageMatchesError } from "@/lib/session-recovery-presentation";
function isSessionActive(state?: TaskSessionState) {
return state === "RUNNING" || state === "STARTING" || state === "COMPLETED";
}
function currentSessionRecoveryError(sessionMetadata: Record<string, unknown> | null) {
if (!sessionMetadata) return null;
return readLastAgentError(sessionMetadata);
}
function isCurrentRecoveryMessage(
isRecoveryMessage: boolean,
messageRecoveryStamp: string | undefined,
currentRecoveryError: LastAgentError | null,
comment: Message,
) {
if (!isRecoveryMessage) return true;
if (!currentRecoveryError) return true;
const currentRecoveryStamp = lastAgentErrorStamp(currentRecoveryError);
if (messageRecoveryStamp) return currentRecoveryStamp === messageRecoveryStamp;
return legacyRecoveryMessageMatchesError(
comment.content,
comment.created_at,
currentRecoveryError,
);
}
function shouldShowRecoveryActions({
isRecoveryMessage,
isCurrentRecovery,
recoveryResolvedDurably,
agentRebooted,
recoveryRequested,
recoveryFailedAgain,
sessionState,
}: {
isRecoveryMessage: boolean;
isCurrentRecovery: boolean;
recoveryResolvedDurably: boolean;
agentRebooted: boolean;
recoveryRequested: boolean;
recoveryFailedAgain: boolean;
sessionState?: TaskSessionState;
}) {
if (!isRecoveryMessage) return true;
if (!isCurrentRecovery || recoveryResolvedDurably || agentRebooted) return false;
if (recoveryRequested && !recoveryFailedAgain) return false;
return !isSessionActive(sessionState);
}
export const ActionMessage = memo(function ActionMessage({ comment }: { comment: Message }) {
// Read session state from the store instead of receiving it as a prop, so a
// state transition doesn't re-render every message in the list (only the
@@ -51,6 +102,23 @@ export const ActionMessage = memo(function ActionMessage({ comment }: { comment:
// that came back failed — a failed boot row, or a session driven to FAILED —
// must surface its card again, buttons included, or the retry is unreachable.
const recoveryFailedAgain = agentBootFailed || sessionState === "FAILED";
const currentRecoveryError = currentSessionRecoveryError(sessionMetadata ?? null);
const messageRecoveryStamp = metadata?.recovery_stamp ?? metadata?.error_stamp;
const isCurrentRecovery = isCurrentRecoveryMessage(
isRecoveryMessage,
messageRecoveryStamp,
currentRecoveryError,
comment,
);
const recoveryActionsVisible = shouldShowRecoveryActions({
isRecoveryMessage,
isCurrentRecovery,
recoveryResolvedDurably,
agentRebooted,
recoveryRequested,
recoveryFailedAgain,
sessionState,
});
if (metadata?.action_visibility === "running") {
if (sessionState === "RUNNING" && comment.turn_id && activeTurnId === comment.turn_id) {
@@ -77,9 +145,7 @@ export const ActionMessage = memo(function ActionMessage({ comment }: { comment:
sessionState={sessionState}
taskId={comment.task_id}
sessionId={comment.session_id}
recoveryResolved={
recoveryResolvedDurably || agentRebooted || (recoveryRequested && !recoveryFailedAgain)
}
recoveryActionsVisible={recoveryActionsVisible}
onRecoveryRequested={() => setRecoveryRequested(true)}
/>
);
@@ -92,7 +158,7 @@ function SettledActionMessage({
sessionState,
taskId,
sessionId,
recoveryResolved,
recoveryActionsVisible,
onRecoveryRequested,
}: {
metadata: ActionMeta | undefined;
@@ -101,12 +167,12 @@ function SettledActionMessage({
sessionState?: TaskSessionState;
taskId?: string;
sessionId?: string;
recoveryResolved: boolean;
recoveryActionsVisible: boolean;
onRecoveryRequested: () => void;
}) {
// A retry card is persisted against the failed turn, so hide it while the
// replacement turn is starting or running to avoid showing stale progress.
if (isSessionActive(sessionState)) return null;
if (isSessionActive(sessionState) && metadata?.recovery_actions !== true) return null;
if (metadata?.retrying) {
return <TransientRetryNotice metadata={metadata} taskId={taskId} />;
@@ -117,10 +183,9 @@ function SettledActionMessage({
metadata={metadata}
message={message}
sessionError={sessionError}
sessionState={sessionState}
taskId={taskId}
sessionId={sessionId}
recoveryResolved={recoveryResolved}
recoveryActionsVisible={recoveryActionsVisible}
onRecoveryRequested={onRecoveryRequested}
/>
);
@@ -130,32 +195,23 @@ function SettledFailureMessage({
metadata,
message,
sessionError,
sessionState,
taskId,
sessionId,
recoveryResolved,
recoveryActionsVisible,
onRecoveryRequested,
}: {
metadata: ActionMeta | undefined;
message: string;
sessionError?: string;
sessionState?: TaskSessionState;
taskId?: string;
sessionId?: string;
recoveryResolved: boolean;
recoveryActionsVisible: boolean;
onRecoveryRequested: () => void;
}) {
// A waiting session can still need recovery, so only hide this persisted card
// once its own recovery resolved: either the Resume click was acknowledged or
// the transcript shows the agent booted again after this failure. A resumed
// agent settles at WAITING_FOR_INPUT, which isSessionActive deliberately
// excludes, so without that second signal the card outlives the failure it
// describes.
if (isSessionActive(sessionState) || (metadata?.recovery_actions && recoveryResolved))
return null;
const renderedMetadata = recoveryActionsVisible ? metadata : withoutRecoveryActions(metadata);
const specialRecovery = renderSpecialRecovery({
metadata,
metadata: renderedMetadata,
message,
sessionError,
taskId,
@@ -169,16 +225,16 @@ function SettledFailureMessage({
? "text-amber-600 dark:text-amber-400"
: "text-red-600 dark:text-red-400";
return (
<div className="w-full">
<div className="w-full" data-testid="session-recovery-action-message">
<div className="flex items-start gap-3 w-full rounded px-2 py-1 -mx-2">
<div className="flex-shrink-0 mt-0.5">
<IconAlertTriangle className={cn("h-4 w-4", iconClass)} />
</div>
<div className="flex-1 min-w-0 pt-0.5">
<div className={cn("text-xs break-words", textClass)}>{message}</div>
<ActionMessageDetails metadata={metadata} />
<ActionMessageDetails metadata={renderedMetadata} />
{renderSettledActionButtons({
actions: metadata?.actions,
actions: renderedMetadata?.actions,
taskId,
sessionId,
isRecoveryMessage: metadata?.recovery_actions === true,
@@ -190,6 +246,11 @@ function SettledFailureMessage({
);
}
function withoutRecoveryActions(metadata: ActionMeta | undefined): ActionMeta | undefined {
if (!metadata) return undefined;
return { ...metadata, actions: undefined };
}
function renderSettledActionButtons({
actions,
taskId,
@@ -74,6 +74,7 @@ vi.mock("../prompt-history-panel-content", () => ({
import {
MobilePanelArea,
mobilePanelTopNavHeight,
resolveMobilePluginPanel,
resolveMobileReviewSource,
terminalPaddingBottom,
@@ -327,6 +328,11 @@ describe("resolveMobilePluginPanel", () => {
});
describe("MobilePanelArea PR identity", () => {
it("removes nested top-bar padding when the outer task error reserves it", () => {
expect(mobilePanelTopNavHeight(false)).toBe("3.5rem");
expect(mobilePanelTopNavHeight(true)).toBe("0px");
});
it("remounts detail feedback when the user chooses another mixed-provider review", () => {
function MobileReviewHarness() {
const reviews: ReviewItemSummary[] = [
@@ -74,6 +74,10 @@ export function resolveMobileReviewSource(
const TOP_NAV_HEIGHT = "3.5rem";
const BOTTOM_NAV_HEIGHT = "3.25rem";
export function mobilePanelTopNavHeight(hasSharedTaskError: boolean): string {
return hasSharedTaskError ? "0px" : TOP_NAV_HEIGHT;
}
type SessionMobileLayoutProps = {
workspaceId: string | null;
workflowId: string | null;
@@ -94,6 +98,7 @@ type SessionMobileLayoutProps = {
onTaskUnarchived?: (taskId: string) => void;
taskCanvases?: Canvas[];
onOpenCanvas?: (canvasId: string) => void;
hasSharedTaskError?: boolean;
};
function MobileChatPanelContent({
@@ -213,7 +218,7 @@ export function MobilePanelArea({
style={{
paddingTop: `calc(${topNavHeight} + env(safe-area-inset-top, 0px))`,
paddingBottom: `calc(${bottomNavHeight} + env(safe-area-inset-bottom, 0px))`,
height: "100dvh",
height: "100%",
}}
>
{currentMobilePanel === "chat" && (
@@ -662,7 +667,7 @@ export const SessionMobileLayout = memo(function SessionMobileLayout(
}
}, [currentMobilePanel, effectiveMobilePanel]);
return (
<div className="h-dvh relative bg-background" data-testid="mobile-task-layout">
<div className="relative h-full min-h-0 bg-background" data-testid="mobile-task-layout">
<MobileTopBarSticky
{...props}
activeTaskId={activeTaskId}
@@ -686,7 +691,7 @@ export const SessionMobileLayout = memo(function SessionMobileLayout(
onNavigateToPrompt={handleNavigateToPrompt}
onScrollTargetConsumed={handleMobileScrollTargetConsumed}
mobileScrollTarget={mobileScrollTarget}
topNavHeight={TOP_NAV_HEIGHT}
topNavHeight={mobilePanelTopNavHeight(Boolean(props.hasSharedTaskError))}
bottomNavHeight={BOTTOM_NAV_HEIGHT}
reviews={reviews}
selectedReview={selectedReview}
@@ -78,6 +78,7 @@ vi.mock("./task-launch-error-context", () => ({
mocks.taskLaunchErrorProviderValue = value;
return <>{children}</>;
},
useTaskLaunchErrorContext: () => null,
}));
vi.mock("@/hooks/use-task-sessions", () => ({
useTaskSessions: mocks.useTaskSessions,
@@ -371,7 +372,7 @@ describe("PreviewSessionBody delivery", () => {
expect(mocks.taskLaunchErrorProviderValue?.automaticRecovery).toBe(recovery);
});
it("keeps the bootstrap recovery card and automatic outcome in passthrough preview", () => {
it("keeps passthrough recovery feedback outside the terminal preview", () => {
mocks.sessions = [
makeSession("session-1", {
is_passthrough: true,
@@ -410,9 +411,7 @@ describe("PreviewSessionBody delivery", () => {
render(<PreviewSessionTabs taskId={TASK_ID} sessionId="session-1" />);
expect(screen.getByTestId("preview-bootstrap-recovery-card").textContent).toContain(
"recovery_failed",
);
expect(screen.queryByTestId("preview-bootstrap-recovery-card")).toBeNull();
expect(screen.getByTestId("preview-passthrough-toolbar")).toBeTruthy();
});
});
@@ -25,11 +25,8 @@ import { TabRenameInput } from "./tab-rename-input";
import { PreviewPlanPanel, usePreviewPlanSummary } from "./preview-plan-panel";
import { TaskChatPanel } from "./task-chat-panel";
import { TaskLaunchErrorProvider } from "./task-launch-error-context";
import {
selectSessionRecoveryError,
type SessionRecoveryOwner,
} from "@/lib/session-recovery-presentation";
import { SessionBootstrapRecoveryCard } from "@/components/task/chat/session-bootstrap-recovery-card";
import { PreviewTaskErrorShell } from "./preview-task-error-shell";
import { type SessionRecoveryOwner } from "@/lib/session-recovery-presentation";
import type { HandoffPreset } from "./new-session-dialog";
import { MAX_SESSION_NAME_LENGTH, useSessionRenameCommitter } from "./use-session-rename";
import {
@@ -253,30 +250,12 @@ function PreviewSessionTabDialogHost({
}
function PreviewSessionRecoverySurface({
taskId,
activeSession,
workspaceId,
bootstrapRecoveryError,
resumption,
}: {
taskId: string;
activeSession: TaskSession | null;
workspaceId?: string | null;
bootstrapRecoveryError: ReturnType<typeof selectSessionRecoveryError>;
resumption: ReturnType<typeof useSessionResumption>;
}) {
if (activeSession?.is_passthrough && bootstrapRecoveryError) {
return (
<SessionBootstrapRecoveryCard
taskId={taskId}
sessionId={activeSession.id}
workspaceId={workspaceId}
error={bootstrapRecoveryError}
automaticRecovery={resumption}
/>
);
}
if (bootstrapRecoveryError) return null;
return (
<SessionRecoveryFeedback
error={resumption.error}
@@ -351,12 +330,6 @@ export function PreviewSessionTabs({
// active session is ready (resumes / restores workspace after a kandev
// restart where the session row is persisted but agentctl isn't alive).
const resumption = useSessionResumption(taskId, activeSessionId, isArchived ?? null);
const bootstrapRecoveryError = selectSessionRecoveryError(
taskStatusSummary?.active_error,
activeSessionId,
activeSession?.metadata,
);
const dialogs = usePreviewSessionTabDialogs(taskId, sortedSessions);
// `handleSessionRemoved` is captured once by `useSessionActions`'s `remove`
// closure at the moment delete is confirmed, and `session.delete` can take
@@ -397,16 +370,32 @@ export function PreviewSessionTabs({
const tabs = useMemo<SessionTab[]>(() => [...sessionTabs, planTab], [sessionTabs, planTab]);
if (!isLoaded && sortedSessions.length === 0) {
return <PreviewLoadingState label={t("task:loadingAgents")} />;
return (
<PreviewTaskErrorShell
taskId={taskId}
workspaceId={workspaceId}
statusSummary={taskStatusSummary}
resumption={resumption}
>
<PreviewLoadingState label={t("task:loadingAgents")} />
</PreviewTaskErrorShell>
);
}
if (hasNoSessions && !hasPlan) {
return (
<PreviewNoSessionsState
ensureSession={ensureSession}
resumption={resumption}
<PreviewTaskErrorShell
taskId={taskId}
workspaceId={workspaceId}
/>
statusSummary={taskStatusSummary}
resumption={resumption}
>
<PreviewNoSessionsState
ensureSession={ensureSession}
resumption={resumption}
workspaceId={workspaceId}
/>
</PreviewTaskErrorShell>
);
}
@@ -414,42 +403,43 @@ export function PreviewSessionTabs({
// still have a plan worth showing — fall back to the Plan view rather than
// an empty session body when there's nothing else to select.
return (
<div className="flex h-full flex-col min-h-0" data-testid="preview-session-tabs">
<PreviewSessionRecoverySurface
taskId={taskId}
activeSession={activeSession}
workspaceId={workspaceId}
bootstrapRecoveryError={bootstrapRecoveryError}
resumption={resumption}
/>
<div className="border-b px-2 py-1">
<SessionTabs
tabs={tabs}
activeTab={viewMode === "plan" ? PLAN_TAB_ID : (activeSessionId ?? "")}
onTabChange={planTabState.handleTabChange}
listClassName="bg-transparent p-0 !h-7 gap-1 overflow-x-auto overflow-y-hidden min-w-0 shrink [@media(pointer:coarse)]:!h-11 [&::-webkit-scrollbar]:hidden [-ms-overflow-style:none] [scrollbar-width:none]"
/>
</div>
<div className="flex-1 min-h-0">
<PreviewTabBody
viewMode={viewMode}
planState={planTabState.planState}
activeSession={activeSession}
<PreviewTaskErrorShell
taskId={taskId}
workspaceId={workspaceId}
statusSummary={taskStatusSummary}
resumption={resumption}
>
<div className="flex h-full flex-col min-h-0" data-testid="preview-session-tabs">
<PreviewSessionRecoverySurface workspaceId={workspaceId} resumption={resumption} />
<div className="border-b px-2 py-1">
<SessionTabs
tabs={tabs}
activeTab={viewMode === "plan" ? PLAN_TAB_ID : (activeSessionId ?? "")}
onTabChange={planTabState.handleTabChange}
listClassName="bg-transparent p-0 !h-7 gap-1 overflow-x-auto overflow-y-hidden min-w-0 shrink [@media(pointer:coarse)]:!h-11 [&::-webkit-scrollbar]:hidden [-ms-overflow-style:none] [scrollbar-width:none]"
/>
</div>
<div className="flex-1 min-h-0">
<PreviewTabBody
viewMode={viewMode}
planState={planTabState.planState}
activeSession={activeSession}
taskId={taskId}
workspaceId={workspaceId}
statusSummary={taskStatusSummary}
resumption={resumption}
/>
</div>
<PreviewSessionTabDialogHost
dialogs={dialogs}
sortedSessions={sortedSessions}
profilesById={profilesById}
agentLabelsById={agentLabelsById}
taskId={taskId}
workspaceId={workspaceId}
statusSummary={taskStatusSummary}
resumption={resumption}
isPrimarySession={isPrimarySession}
/>
</div>
<PreviewSessionTabDialogHost
dialogs={dialogs}
sortedSessions={sortedSessions}
profilesById={profilesById}
agentLabelsById={agentLabelsById}
taskId={taskId}
isPrimarySession={isPrimarySession}
/>
</div>
</PreviewTaskErrorShell>
);
}
@@ -0,0 +1,37 @@
"use client";
import type { ReactNode } from "react";
import { useSessionResumption } from "@/hooks/domains/session/use-session-resumption";
import { useTaskStatusSummary } from "@/hooks/domains/task/use-task-status-summary";
import { TaskLaunchErrorProvider } from "./task-launch-error-context";
import { TaskSharedError } from "./task-shared-error";
export function PreviewTaskErrorShell({
taskId,
workspaceId,
statusSummary,
resumption,
children,
}: {
taskId: string;
workspaceId?: string | null;
statusSummary?: ReturnType<typeof useTaskStatusSummary>;
resumption: ReturnType<typeof useSessionResumption>;
children: ReactNode;
}) {
return (
<TaskLaunchErrorProvider
value={{
taskId,
workspaceId: workspaceId ?? "",
statusSummary,
automaticRecovery: resumption,
}}
>
<div className="flex h-full min-h-0 flex-col">
<TaskSharedError />
{children}
</div>
</TaskLaunchErrorProvider>
);
}
@@ -24,6 +24,8 @@ import type {
TimelineEvent,
} from "@/app/office/tasks/[id]/types";
import { useTranslation } from "react-i18next";
import { TaskLaunchErrorProvider } from "@/components/task/task-launch-error-context";
import { TaskSharedError } from "@/components/task/task-shared-error";
// TAB_TRIGGER_BASE adds a 1px ring to the active tab on top of
// shadcn's default bg/text active styling, so the selected tab is
@@ -137,44 +139,54 @@ export function ChatActivityTabs({
);
return (
<Tabs defaultValue="chat" className="mt-6">
<TabsList>
<TabsTrigger value="chat" className={TAB_TRIGGER_BASE}>
{t("task:chat")}
</TabsTrigger>
<TabsTrigger value="activity" className={TAB_TRIGGER_BASE}>
{t("task:activity")}
</TabsTrigger>
<TaskLaunchErrorProvider
value={{
taskId: task.id,
workspaceId: task.workspaceId,
statusSummary: task.statusSummary,
repositories: task.repositories,
}}
>
<TaskSharedError />
<Tabs defaultValue="chat" className="mt-6">
<TabsList>
<TabsTrigger value="chat" className={TAB_TRIGGER_BASE}>
{t("task:chat")}
</TabsTrigger>
<TabsTrigger value="activity" className={TAB_TRIGGER_BASE}>
{t("task:activity")}
</TabsTrigger>
{officeGroups.map((g) => (
<AgentTabTrigger key={g.id} group={g} />
))}
</TabsList>
<TabsContent value="chat">
<ApprovalActionBar task={task} />
<TaskChat
taskId={task.id}
workspaceId={task.workspaceId}
statusSummary={task.statusSummary}
repositories={task.repositories}
comments={comments}
timeline={timeline}
sessions={sessions}
decisions={task.decisions}
reviewers={task.reviewers}
approvers={task.approvers}
scrollParent={scrollParent}
readOnly={readOnly}
onCommentsChanged={onCommentsChanged}
taskTitle={task.title}
taskDescription={task.description}
/>
</TabsContent>
<TabsContent value="activity">
<TaskActivity taskId={task.id} entries={activity} />
</TabsContent>
{officeGroups.map((g) => (
<AgentTabTrigger key={g.id} group={g} />
<AgentTabContent key={g.id} taskId={task.id} group={g} />
))}
</TabsList>
<TabsContent value="chat">
<ApprovalActionBar task={task} />
<TaskChat
taskId={task.id}
workspaceId={task.workspaceId}
statusSummary={task.statusSummary}
repositories={task.repositories}
comments={comments}
timeline={timeline}
sessions={sessions}
decisions={task.decisions}
reviewers={task.reviewers}
approvers={task.approvers}
scrollParent={scrollParent}
readOnly={readOnly}
onCommentsChanged={onCommentsChanged}
taskTitle={task.title}
taskDescription={task.description}
/>
</TabsContent>
<TabsContent value="activity">
<TaskActivity taskId={task.id} entries={activity} />
</TabsContent>
{officeGroups.map((g) => (
<AgentTabContent key={g.id} taskId={task.id} group={g} />
))}
</Tabs>
</Tabs>
</TaskLaunchErrorProvider>
);
}
@@ -1,7 +1,6 @@
import { describe, expect, it } from "vitest";
import {
buildRunErrorsFromSessions,
filterVisibleRunErrors,
hasMatchingSessionLaunchError,
liveSessionMetadataFromStore,
mergeLiveSessionMetadata,
@@ -9,6 +8,7 @@ import {
import type { TaskSession } from "@/app/office/tasks/[id]/types";
const URL = "https://opencode.ai/workspace/wrk_01KQM7K5CYT715264YKKFB17ZY/go";
const SESSION_STARTED_AT = "2026-08-02T15:00:00Z";
function session(overrides: Partial<TaskSession>): TaskSession {
return {
@@ -17,7 +17,7 @@ function session(overrides: Partial<TaskSession>): TaskSession {
agentRole: "agent",
state: "FAILED",
isPrimary: false,
startedAt: "2026-08-02T15:00:00Z",
startedAt: SESSION_STARTED_AT,
errorMessage: "usage limit reached",
...overrides,
};
@@ -64,12 +64,16 @@ describe("buildRunErrorsFromSessions", () => {
});
it("omits the URL when metadata or the field is absent", () => {
const legacyError = buildRunErrorsFromSessions([session({ errorMessage: "boom" })]);
expect(legacyError).toHaveLength(1);
expect(legacyError[0].remediationUrl).toBeUndefined();
expect(legacyError[0].isActive).toBe(true);
for (const s of [
session({ errorMessage: "boom" }),
session({ metadata: { last_agent_error: { message: "boom" } } }),
session({ metadata: { last_agent_error: { message: "boom", remediation_url: "" } } }),
]) {
const errors = buildRunErrorsFromSessions([s]);
expect(errors).toHaveLength(1);
expect(errors[0].remediationUrl).toBeUndefined();
}
});
@@ -88,11 +92,38 @@ describe("buildRunErrorsFromSessions", () => {
expect(errors[0].remediationUrl).toBeUndefined();
});
it("skips non-FAILED sessions", () => {
it("preserves an active error on a non-FAILED session", () => {
const errors = buildRunErrorsFromSessions([
session({ state: "RUNNING", metadata: { last_agent_error: { remediation_url: URL } } }),
session({
state: "RUNNING",
metadata: { last_agent_error: { message: "still starting", stamp: "failure-1" } },
}),
]);
expect(errors).toHaveLength(0);
expect(errors).toHaveLength(1);
expect(errors[0].isActive).toBe(true);
});
it("preserves a dismissed error as history after the session resumes", () => {
const errors = buildRunErrorsFromSessions([
session({
state: "IDLE",
metadata: {
last_agent_error: {
message: "The agent could not start.",
occurred_at: SESSION_STARTED_AT,
dismissed_at: "2026-08-02T15:05:00Z",
stamp: "failure-1",
recovery_actions: ["retry_default"],
},
},
}),
]);
expect(errors).toHaveLength(1);
expect(errors[0].failedAt).toBe(SESSION_STARTED_AT);
expect(errors[0].errorStamp).toBe("failure-1");
expect(errors[0].isActive).toBe(false);
expect(errors[0].recoveryActions).toBeUndefined();
});
});
@@ -118,7 +149,7 @@ describe("hasMatchingSessionLaunchError", () => {
it("does not match an unstamped run error to a stamped summary", () => {
expect(
hasMatchingSessionLaunchError("s1", "new-stamp", [
{ id: "e1", sessionId: "s1", rawPayload: "boom", failedAt: "2026-08-02T15:00:00Z" },
{ id: "e1", sessionId: "s1", rawPayload: "boom", failedAt: SESSION_STARTED_AT },
]),
).toBe(false);
});
@@ -130,7 +161,7 @@ describe("hasMatchingSessionLaunchError", () => {
id: "e1",
sessionId: "s1",
rawPayload: "boom",
failedAt: "2026-08-02T15:00:00Z",
failedAt: SESSION_STARTED_AT,
errorStamp: "new-stamp",
},
]),
@@ -138,32 +169,6 @@ describe("hasMatchingSessionLaunchError", () => {
});
});
describe("filterVisibleRunErrors", () => {
it("keeps ordinary provider errors when the active summary is not a launch error", () => {
const errors = buildRunErrorsFromSessions([
session({
metadata: {
last_agent_error: {
message: "The provider rejected the request.",
code: "provider_auth_required",
stamp: "provider-stamp-1",
},
},
}),
]);
expect(
filterVisibleRunErrors(errors, {
session_id: "s1",
stamp: "provider-stamp-1",
occurred_at: "2026-08-02T15:00:01Z",
preview: "The provider rejected the request.",
category: "provider_auth_required",
}),
).toEqual(errors);
});
});
describe("liveSessionMetadataFromStore", () => {
const metadata = { last_agent_error: { message: "boom", remediation_url: URL } };
+42 -64
View File
@@ -1,8 +1,11 @@
import type { CommentTurnContext } from "./turn-context";
import { groupSortKey, type SessionGroup } from "./session-groups";
import { normalizeRemediationUrl } from "@/lib/remediation-url";
import { lastAgentErrorStamp, readLastAgentError } from "@/lib/session-last-agent-error";
import { isTypedTaskLaunchError } from "./components/task-launch-error-entry";
import {
lastAgentErrorStamp,
readLastAgentError,
readLastAgentErrorIncludingDismissed,
} from "@/lib/session-last-agent-error";
import { isMatchingTaskLaunchError } from "@/components/task/chat/types";
import type {
RunError,
@@ -11,7 +14,6 @@ import type {
TaskSession,
TimelineEvent,
} from "@/app/office/tasks/[id]/types";
import type { TaskStatusSummary } from "@/lib/types/task-status-summary";
/**
* Discriminated union of every row that can appear in the task chat
@@ -50,38 +52,51 @@ export function buildLaterAgentReplyMap(comments: TaskComment[]): Map<string, bo
}
/**
* Pull RunError entries out of failed sessions. One entry per office
* session in FAILED state — the session row's error_message becomes
* the rawPayload for the chat entry's Show details. The remediation URL is
* copied from the persisted last_agent_error metadata and passed through the
* browser-edge allowlist, so RunError never carries an unvalidated string.
* Pull retained session failures into the Office chat timeline. The session
* metadata remains the source of the chronological breadcrumb after recovery;
* only an undismissed record carries recovery actions. The remediation URL is
* copied from the persisted metadata and passed through the browser-edge
* allowlist, so RunError never carries an unvalidated string.
*/
export function buildRunErrorsFromSessions(sessions: TaskSession[]): RunError[] {
const errors: RunError[] = [];
for (const s of sessions) {
if (s.state !== "FAILED") continue;
const failedAt = s.completedAt ?? s.updatedAt ?? s.startedAt ?? "";
const lastError = lastAgentErrorFromSessionMetadata(s.metadata);
const parsedLastError = readLastAgentError(s.metadata);
errors.push({
id: `re-${s.id}`,
sessionId: s.id,
agentProfileId: s.agentProfileId,
rawPayload: s.errorMessage ?? "",
failedAt,
failureCode: lastError?.code,
failureDetails: lastError?.details,
message: parsedLastError?.message,
recoveryActions: parsedLastError?.recoveryActions,
taskRepositoryId: parsedLastError?.taskRepositoryId,
errorStamp: parsedLastError ? lastAgentErrorStamp(parsedLastError) : undefined,
remediationUrl:
normalizeRemediationUrl(remediationUrlFromSessionMetadata(s.metadata)) ?? undefined,
});
const error = buildRunErrorFromSession(s);
if (error) errors.push(error);
}
return errors;
}
function buildRunErrorFromSession(s: TaskSession): RunError | null {
const parsedLastError = readLastAgentErrorIncludingDismissed(s.metadata);
const isLegacy = parsedLastError === null;
// Older Office sessions only stored error_message on the session row. Keep
// those FAILED rows in the chronological timeline while using metadata as
// the richer identity for resumed and dismissed failures.
if (isLegacy && s.state !== "FAILED") return null;
const activeLastError = readLastAgentError(s.metadata);
const error: RunError = {
id: `re-${s.id}`,
sessionId: s.id,
agentProfileId: s.agentProfileId,
rawPayload: s.errorMessage ?? "",
failedAt: parsedLastError?.occurredAt ?? s.completedAt ?? s.updatedAt ?? s.startedAt ?? "",
recoveryActions: activeLastError?.recoveryActions,
isActive: activeLastError !== null || isLegacy,
};
if (isLegacy) return error;
error.failureCode = parsedLastError.code;
error.failureDetails = parsedLastError.details;
error.message = parsedLastError.message;
error.taskRepositoryId = parsedLastError.taskRepositoryId;
error.errorStamp = lastAgentErrorStamp(parsedLastError);
const remediationUrl = normalizeRemediationUrl(parsedLastError.remediationUrl);
if (remediationUrl) error.remediationUrl = remediationUrl;
return error;
}
export function hasMatchingSessionLaunchError(
summarySessionId: string | undefined,
summaryStamp: string | undefined,
@@ -96,20 +111,6 @@ export function hasMatchingSessionLaunchError(
);
}
export function filterVisibleRunErrors(
runErrors: RunError[],
activeError: TaskStatusSummary["active_error"],
): RunError[] {
if (!isTypedTaskLaunchError(activeError)) return runErrors;
return runErrors.filter(
(error) =>
!isMatchingTaskLaunchError(activeError, {
sessionId: error.sessionId,
errorStamp: error.errorStamp,
}),
);
}
export function mergeLiveSessionMetadata(
base: Record<string, unknown> | null | undefined,
live: Record<string, unknown> | null | undefined,
@@ -138,29 +139,6 @@ export function liveSessionMetadataFromStore(
return row.metadata;
}
function remediationUrlFromSessionMetadata(metadata: Record<string, unknown> | null | undefined) {
const lastError = metadata?.last_agent_error;
if (!lastError || typeof lastError !== "object") return undefined;
const record = lastError as Record<string, unknown>;
const raw = record.remediation_url ?? record.remediationUrl;
return typeof raw === "string" && raw !== "" ? raw : undefined;
}
function lastAgentErrorFromSessionMetadata(
metadata: Record<string, unknown> | null | undefined,
): { code?: string; details?: string } | undefined {
const lastError = metadata?.last_agent_error;
if (!lastError || typeof lastError !== "object") return undefined;
const record = lastError as Record<string, unknown>;
const code = record.code ?? record.failure_code ?? record.failureCode;
const details =
record.details ?? record.failure_details ?? record.failureDetails ?? record.error_output;
return {
code: typeof code === "string" && code !== "" ? code : undefined,
details: typeof details === "string" && details !== "" ? details : undefined,
};
}
export type MergeChatEntriesArgs = {
comments: TaskComment[];
timeline: TimelineEvent[];
@@ -16,7 +16,7 @@ export function ManagedRuntimeNpmRunError({
}: {
error: RunError;
agentName: string;
onRetry: () => void;
onRetry?: () => void;
}) {
const { t } = useTranslation();
const [showDetails, setShowDetails] = useState(false);
@@ -56,18 +56,20 @@ export function ManagedRuntimeNpmRunError({
</CollapsibleContent>
</Collapsible>
)}
<div className="mt-2 flex flex-wrap items-center gap-2">
<Button
variant="outline"
size="sm"
className="h-auto min-h-11 cursor-pointer gap-1.5 text-xs sm:min-h-8"
onClick={onRetry}
data-testid="run-error-managed-runtime-retry-button"
>
<IconRefresh className="h-3 w-3" />
{t("chat:managedRuntimeRetry")}
</Button>
</div>
{onRetry && (
<div className="mt-2 flex flex-wrap items-center gap-2">
<Button
variant="outline"
size="sm"
className="h-auto min-h-11 cursor-pointer gap-1.5 text-xs sm:min-h-8"
onClick={onRetry}
data-testid="run-error-managed-runtime-retry-button"
>
<IconRefresh className="h-3 w-3" />
{t("chat:managedRuntimeRetry")}
</Button>
</div>
)}
</div>
</div>
);
@@ -72,4 +72,17 @@ describe("RunErrorEntry", () => {
expect(screen.getByTestId("run-error-continue-new-branch-button")).toBeTruthy();
expect(screen.getByTestId("run-error-restore-workspace-button")).toBeTruthy();
});
it("renders recovered session history without stale recovery controls", () => {
render(
<RunErrorEntry
taskId="task-1"
workspaceId="workspace-1"
error={{ ...runError("provider_auth_required"), isActive: false }}
/>,
);
expect(screen.queryByTestId("run-error-resume-button")).toBeNull();
expect(screen.queryByTestId("run-error-fresh-button")).toBeNull();
});
});
@@ -48,6 +48,7 @@ function TypedRunLaunchErrorEntry({
taskId={taskId}
workspaceId={workspaceId ?? ""}
repositories={repositories}
isActive={error.isActive !== false}
error={{
session_id: error.sessionId,
task_repository_id: error.taskRepositoryId,
@@ -125,9 +126,55 @@ function RunErrorRecoveryFeedback({
);
}
function LegacyRunErrorActions({
error,
isActive,
busyAction,
onRecover,
}: {
error: RunError;
isActive: boolean;
busyAction: SessionRecoveryAction | "restore" | null;
onRecover: (action: SessionRecoveryAction) => Promise<boolean>;
}) {
const { t } = useTranslation();
return (
<div className="mt-2 flex items-center gap-2 flex-wrap">
<RemediationLink url={error.remediationUrl} />
{isActive && (
<>
<Button
variant="outline"
size="sm"
className="h-auto min-h-11 cursor-pointer gap-1.5 text-xs sm:min-h-8"
onClick={() => onRecover("resume")}
disabled={busyAction !== null}
data-testid="run-error-resume-button"
>
<IconRefresh className="h-3 w-3" />
{t("task:resumeSession")}
</Button>
<Button
variant="outline"
size="sm"
className="h-auto min-h-11 cursor-pointer gap-1.5 text-xs sm:min-h-8"
onClick={() => onRecover("fresh_start")}
disabled={busyAction !== null}
data-testid="run-error-fresh-button"
>
<IconPlayerPlay className="h-3 w-3" />
{t("task:startFreshSession")}
</Button>
</>
)}
</div>
);
}
function LegacyRunErrorEntry({
agentName,
error,
isActive,
onRecover,
onRetry,
onRestore,
@@ -140,6 +187,7 @@ function LegacyRunErrorEntry({
}: {
agentName: string;
error: RunError;
isActive: boolean;
onRecover: (action: SessionRecoveryAction) => Promise<boolean>;
onRetry: () => void;
onRestore: () => void;
@@ -168,16 +216,18 @@ function LegacyRunErrorEntry({
</span>
</div>
<p className="mt-1 text-sm text-muted-foreground">{t("task:theAgentStoppedWithAnError")}</p>
<RunErrorRecoveryFeedback
workspaceId={workspaceId}
recoveryError={recoveryError}
recoveryNotice={recoveryNotice}
branchDetails={branchDetails}
busyAction={busyAction}
onRetry={onRetry}
onRestore={onRestore}
onNewBranch={onNewBranch}
/>
{isActive && (
<RunErrorRecoveryFeedback
workspaceId={workspaceId}
recoveryError={recoveryError}
recoveryNotice={recoveryNotice}
branchDetails={branchDetails}
busyAction={busyAction}
onRetry={onRetry}
onRestore={onRestore}
onNewBranch={onNewBranch}
/>
)}
{error.rawPayload && (
<Collapsible open={showDetails} onOpenChange={setShowDetails} className="mt-2">
<CollapsibleTrigger className="flex items-center gap-1 text-xs text-muted-foreground cursor-pointer hover:text-foreground transition-colors">
@@ -196,31 +246,12 @@ function LegacyRunErrorEntry({
</CollapsibleContent>
</Collapsible>
)}
<div className="mt-2 flex items-center gap-2 flex-wrap">
<RemediationLink url={error.remediationUrl} />
<Button
variant="outline"
size="sm"
className="h-auto min-h-11 cursor-pointer gap-1.5 text-xs sm:min-h-8"
onClick={() => onRecover("resume")}
disabled={busyAction !== null}
data-testid="run-error-resume-button"
>
<IconRefresh className="h-3 w-3" />
{t("task:resumeSession")}
</Button>
<Button
variant="outline"
size="sm"
className="h-auto min-h-11 cursor-pointer gap-1.5 text-xs sm:min-h-8"
onClick={() => onRecover("fresh_start")}
disabled={busyAction !== null}
data-testid="run-error-fresh-button"
>
<IconPlayerPlay className="h-3 w-3" />
{t("task:startFreshSession")}
</Button>
</div>
<LegacyRunErrorActions
error={error}
isActive={isActive}
busyAction={busyAction}
onRecover={onRecover}
/>
</div>
</div>
);
@@ -274,7 +305,7 @@ export function RunErrorEntry({
<ManagedRuntimeNpmRunError
error={error}
agentName={agentName}
onRetry={() => void handleRecover("runtime_retry")}
onRetry={error.isActive === false ? undefined : () => void handleRecover("runtime_retry")}
/>
);
}
@@ -283,6 +314,7 @@ export function RunErrorEntry({
<LegacyRunErrorEntry
agentName={agentName}
error={error}
isActive={error.isActive !== false}
onRecover={handleRecover}
onRetry={handleRetry}
onRestore={() => void handleRestore()}
@@ -62,11 +62,14 @@ vi.mock("@/components/task/task-launch-error-context", () => ({
const TASK_ID = "task-1";
const TASK_REPOSITORY_ID = "task-repo-1";
const SESSION_ID = "session-1";
const ERROR_STAMP = "launch-stamp-1";
const WORKSPACE_ID = "workspace-1";
const OCCURRED_AT = "2026-08-19T10:00:00Z";
const ERROR_ENTRY_TEST_ID = "task-launch-error-entry";
const RETRY_LAUNCH_BUTTON_TEST_ID = "task-launch-retry_launch-button";
const RETRY_DEFAULT_BUTTON_TEST_ID = "task-launch-retry_default-button";
const TASK_LAUNCH_RECOVER_METHOD = "task.launch.recover";
const error: TaskStatusSummaryActiveError = {
task_repository_id: TASK_REPOSITORY_ID,
@@ -118,7 +121,7 @@ describe("TaskLaunchErrorEntry", () => {
it("renders checkout details and retries the launch without branch settings", async () => {
const checkoutError: TaskStatusSummaryActiveError = {
session_id: "session-1",
session_id: SESSION_ID,
stamp: "checkout-stamp-1",
occurred_at: OCCURRED_AT,
preview: "The workspace could not be prepared for this launch.",
@@ -138,9 +141,9 @@ describe("TaskLaunchErrorEntry", () => {
fireEvent.click(screen.getByTestId(RETRY_LAUNCH_BUTTON_TEST_ID));
await waitFor(() => expect(requestMock).toHaveBeenCalledTimes(1));
expect(requestMock).toHaveBeenCalledWith("task.launch.recover", {
expect(requestMock).toHaveBeenCalledWith(TASK_LAUNCH_RECOVER_METHOD, {
task_id: TASK_ID,
session_id: "session-1",
session_id: SESSION_ID,
action: "retry_launch",
error_stamp: "checkout-stamp-1",
});
@@ -205,7 +208,21 @@ describe("TaskLaunchErrorEntry", () => {
expect(screen.getByTestId(ERROR_ENTRY_TEST_ID)).toBeTruthy();
expect(screen.getByText(error.preview)).toBeTruthy();
expect(screen.queryByTestId("task-launch-retry_default-button")).toBeNull();
expect(screen.queryByTestId(RETRY_DEFAULT_BUTTON_TEST_ID)).toBeNull();
});
it("hides recovery actions for a retained session history entry", () => {
render(
<TaskLaunchErrorEntry
taskId={TASK_ID}
workspaceId={WORKSPACE_ID}
isActive={false}
error={{ ...error, recovery_actions: ["retry_default"] }}
/>,
);
expect(screen.getByTestId(ERROR_ENTRY_TEST_ID)).toBeTruthy();
expect(screen.queryByTestId(RETRY_DEFAULT_BUTTON_TEST_ID)).toBeNull();
});
it("routes a session-owned bootstrap error to the recovery card", () => {
@@ -213,13 +230,13 @@ describe("TaskLaunchErrorEntry", () => {
<TaskChatLaunchError
taskId={TASK_ID}
workspaceId={WORKSPACE_ID}
sessionId="session-1"
sessionId={SESSION_ID}
statusSummary={{
revision: 2,
updated_at: OCCURRED_AT,
active_error: {
...error,
session_id: "session-1",
session_id: SESSION_ID,
phase: "bootstrap",
category: "generic_launch_failure",
recovery_actions: ["retry_launch"],
@@ -249,13 +266,13 @@ describe("TaskLaunchErrorEntry", () => {
<TaskChatLaunchError
taskId={TASK_ID}
workspaceId={WORKSPACE_ID}
sessionId="session-1"
sessionId={SESSION_ID}
statusSummary={{
revision: 3,
updated_at: OCCURRED_AT,
active_error: {
...error,
session_id: "session-1",
session_id: SESSION_ID,
phase: "bootstrap",
category: "generic_launch_failure",
recovery_actions: ["retry_launch"],
@@ -309,10 +326,10 @@ describe("TaskLaunchErrorEntry", () => {
expect(screen.getByTestId(ERROR_ENTRY_TEST_ID)).toBeTruthy();
expect(screen.getByText(error.preview)).toBeTruthy();
fireEvent.click(screen.getByTestId("task-launch-retry_default-button"));
fireEvent.click(screen.getByTestId(RETRY_DEFAULT_BUTTON_TEST_ID));
await waitFor(() => expect(requestMock).toHaveBeenCalledTimes(1));
expect(requestMock).toHaveBeenCalledWith("task.launch.recover", {
expect(requestMock).toHaveBeenCalledWith(TASK_LAUNCH_RECOVER_METHOD, {
task_id: TASK_ID,
task_repository_id: TASK_REPOSITORY_ID,
action: "retry_default",
@@ -332,7 +349,7 @@ describe("TaskLaunchErrorEntry", () => {
fireEvent.click(screen.getByTestId("mock-branch-option"));
await waitFor(() => expect(requestMock).toHaveBeenCalledTimes(1));
expect(requestMock).toHaveBeenCalledWith("task.launch.recover", {
expect(requestMock).toHaveBeenCalledWith(TASK_LAUNCH_RECOVER_METHOD, {
task_id: TASK_ID,
task_repository_id: TASK_REPOSITORY_ID,
action: "pick_base_branch",
@@ -341,6 +358,26 @@ describe("TaskLaunchErrorEntry", () => {
});
});
it("keeps task-scoped recovery on the task source when it has a session correlation", async () => {
render(
<TaskLaunchErrorEntry
taskId={TASK_ID}
workspaceId={WORKSPACE_ID}
repositories={[]}
error={{ ...error, scope: "task", session_id: "retired-session-1" }}
/>,
);
fireEvent.click(screen.getByTestId(RETRY_DEFAULT_BUTTON_TEST_ID));
await waitFor(() => expect(requestMock).toHaveBeenCalledTimes(1));
expect(requestMock).toHaveBeenCalledWith(TASK_LAUNCH_RECOVER_METHOD, {
task_id: TASK_ID,
task_repository_id: TASK_REPOSITORY_ID,
action: "retry_default",
error_stamp: ERROR_STAMP,
});
});
it("waits for a branch selection before sending the picker action", async () => {
render(
<TaskLaunchErrorEntry
@@ -380,7 +417,7 @@ describe("TaskLaunchErrorEntry", () => {
</>,
);
const buttons = screen.getAllByTestId("task-launch-retry_default-button");
const buttons = screen.getAllByTestId(RETRY_DEFAULT_BUTTON_TEST_ID);
fireEvent.click(buttons[0]);
fireEvent.click(buttons[1]);
@@ -22,6 +22,8 @@ type TaskLaunchErrorEntryProps = {
workspaceId: string;
error: TaskStatusSummaryActiveError;
repositories?: TaskRepository[];
/** Retained session history has no live recovery controls. */
isActive?: boolean;
};
const pendingRecoveryRequests = new Map<string, Promise<unknown>>();
@@ -58,7 +60,7 @@ function useTaskLaunchRecovery({
const request = Promise.resolve().then(() =>
client.request("task.launch.recover", {
task_id: taskId,
...(error.session_id ? { session_id: error.session_id } : {}),
...(error.scope !== "task" && error.session_id ? { session_id: error.session_id } : {}),
...(error.task_repository_id ? { task_repository_id: error.task_repository_id } : {}),
action,
...(baseBranch ? { base_branch: baseBranch } : {}),
@@ -196,6 +198,7 @@ export function TaskLaunchErrorEntry({
workspaceId,
error,
repositories,
isActive = true,
}: TaskLaunchErrorEntryProps) {
const { t } = useTranslation();
const { pendingAction, recoveryError, sendRecovery } = useTaskLaunchRecovery({ taskId, error });
@@ -243,16 +246,18 @@ export function TaskLaunchErrorEntry({
</CollapsibleContent>
</Collapsible>
)}
<TaskLaunchRecoveryActions
actions={error.recovery_actions ?? []}
workspaceId={workspaceId}
repositories={repositories}
taskRepositoryId={error.task_repository_id}
currentBase={currentBase}
pendingAction={pendingAction}
onRecover={sendRecovery}
/>
{recoveryError && (
{isActive && (
<TaskLaunchRecoveryActions
actions={error.recovery_actions ?? []}
workspaceId={workspaceId}
repositories={repositories}
taskRepositoryId={error.task_repository_id}
currentBase={currentBase}
pendingAction={pendingAction}
onRecover={sendRecovery}
/>
)}
{isActive && recoveryError && (
<p
className="mt-2 text-xs text-destructive"
data-testid="task-launch-recovery-error"
@@ -635,7 +635,7 @@ describe("TaskChat run error entries", () => {
expect(screen.queryByTestId("run-error-raw-payload")).toBeNull();
});
it("renders a task-owned launch error before the empty state", () => {
it("leaves task-owned launch errors to the task shell", () => {
const statusSummary: TaskStatusSummary = {
revision: 3,
updated_at: T_11,
@@ -658,8 +658,8 @@ describe("TaskChat run error entries", () => {
/>,
),
);
expect(screen.getByTestId("task-launch-error-entry")).toBeTruthy();
expect(screen.getByText(/choose a base branch/i)).toBeTruthy();
expect(screen.queryByTestId("task-launch-error-entry")).toBeNull();
expect(screen.queryByText(/choose a base branch/i)).toBeNull();
expect(screen.getByText(/no comments yet/i)).toBeTruthy();
});
+2 -15
View File
@@ -21,7 +21,6 @@ import { formatRelativeTime } from "@/lib/utils";
import { MarkdownComment } from "./markdown-comment";
import { AgentTurnPanel } from "./components/agent-turn-panel";
import { RunErrorEntry } from "./components/run-error-entry";
import { TaskChatLaunchError } from "./components/task-chat-launch-error";
import { UserCommentRunBadge } from "./components/user-comment-run-badge";
import { buildCommentTurnContext, type CommentTurnContext } from "./turn-context";
import { groupSessionsForTimeline, groupSortKey, type SessionGroup } from "./session-groups";
@@ -35,7 +34,6 @@ import type {
import {
buildLaterAgentReplyMap,
buildRunErrorsFromSessions,
filterVisibleRunErrors,
mergeChatEntries,
type ChatEntry,
} from "./chat-entries";
@@ -591,7 +589,6 @@ export function TaskChat({
onCommentsChanged,
taskTitle,
taskDescription,
statusSummary,
repositories,
}: TaskChatProps) {
const { t } = useTranslation();
@@ -616,10 +613,6 @@ export function TaskChat({
);
const turnCtx = useMemo(() => buildCommentTurnContext(comments, sessions), [comments, sessions]);
const runErrors = useMemo(() => buildRunErrorsFromSessions(sessions), [sessions]);
const visibleRunErrors = useMemo(
() => filterVisibleRunErrors(runErrors, statusSummary?.active_error),
[runErrors, statusSummary?.active_error],
);
const laterAgentReplyMap = useMemo(() => buildLaterAgentReplyMap(comments), [comments]);
const entries = useMemo(
() =>
@@ -629,10 +622,10 @@ export function TaskChat({
groups: renderedGroups,
decisions,
turnCtx,
runErrors: visibleRunErrors,
runErrors,
laterAgentReplyMap,
}),
[comments, timeline, renderedGroups, decisions, turnCtx, visibleRunErrors, laterAgentReplyMap],
[comments, timeline, renderedGroups, decisions, turnCtx, runErrors, laterAgentReplyMap],
);
useChatAutoScroll(scrollParent ?? null, sessions, taskId);
@@ -653,12 +646,6 @@ export function TaskChat({
{t("task:showOlderSessions", { count: olderGroups.length })}
</button>
)}
<TaskChatLaunchError
taskId={taskId}
workspaceId={workspaceId}
statusSummary={statusSummary}
repositories={repositories}
/>
{isEmpty ? (
<p className="text-sm text-muted-foreground py-4">{t("task:noCommentsYet")}</p>
) : (
@@ -2,13 +2,20 @@ import type { ReactNode } from "react";
import { cleanup, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import type { Message } from "@/lib/types/http";
import {
buildGroupedRenderItems,
insertLastAgentErrorItem,
type RenderItem,
} from "@/hooks/use-processed-messages";
const launchError = {
stamp: "task-wide-launch-error",
scope: "task" as const,
occurred_at: "2026-08-20T10:00:00Z",
preview: "The workspace could not be prepared.",
category: "workspace_checkout_failed",
};
const SESSION_ID = "prior-session";
const messageListRecoveryRevealKeys = vi.hoisted(() => [] as Array<string | null | undefined>);
@@ -28,15 +35,15 @@ const appStoreState = {
},
taskSessions: {
items: {
"prior-session": { name: null, agent_profile_id: null, last_read_message_id: null },
[SESSION_ID]: { name: null, agent_profile_id: null, last_read_message_id: null },
},
},
agentProfiles: { items: [] },
messages: { bySession: { "prior-session": [priorTranscriptMessage] } },
messages: { bySession: { [SESSION_ID]: [priorTranscriptMessage] } },
};
const panelState = {
resolvedSessionId: "prior-session",
resolvedSessionId: SESSION_ID,
session: {
state: "FAILED",
error_message: "The prior session stopped.",
@@ -47,7 +54,7 @@ const panelState = {
messagesLoading: false,
historyRefreshPending: false,
isInitialMessagesLoading: false,
groupedItems: [],
groupedItems: [] as RenderItem[],
allMessages: [priorTranscriptMessage],
footerActionMessages: [],
permissionsByToolCallId: new Map(),
@@ -118,22 +125,37 @@ vi.mock("./chat/chat-input-area", () => ({
vi.mock("@/components/task/chat/message-list", () => ({
MessageList: ({
items,
messages,
launchErrorOwned,
prependContent,
recoveryRevealKey,
}: {
items: RenderItem[];
messages: Message[];
launchErrorOwned?: boolean;
prependContent?: ReactNode;
recoveryRevealKey?: string | null;
}) => {
messageListRecoveryRevealKeys.push(recoveryRevealKey);
return (
<div data-testid="message-list">
{prependContent}
{!launchErrorOwned &&
messages.map((message) => <div key={message.id}>{message.content}</div>)}
{messages.map((message) => {
const metadata = message.metadata as Record<string, unknown> | undefined;
return (
<div
key={message.id}
data-testid={
metadata?.recovery_actions === true ? "persisted-recovery-message" : undefined
}
>
{message.content}
</div>
);
})}
{items.map((item) =>
item.type === "agent_error_notice" ? (
<div key={item.id} data-testid="agent-error-notice">
provisional recovery notice
</div>
) : null,
)}
</div>
);
},
@@ -218,18 +240,16 @@ afterEach(() => {
});
describe("TaskChatPanel launch-error ownership", () => {
it("keeps a task-wide card and prior failed-session surfaces together", () => {
render(<TaskChatPanel sessionId="prior-session" taskId="task-1" />);
it("leaves the task-wide card to the task shell and preserves session history", () => {
render(<TaskChatPanel sessionId={SESSION_ID} taskId="task-1" />);
expect(screen.getByTestId("task-launch-error-entry").textContent).toContain(
launchError.preview,
);
expect(screen.getByTestId("session-stopped-banner")).toBeTruthy();
expect(screen.queryByTestId("task-launch-error-entry")).toBeNull();
expect(screen.queryByTestId("session-stopped-banner")).toBeNull();
expect(screen.getByText(priorTranscriptMessage.content)).toBeTruthy();
expect(messageListRecoveryRevealKeys.at(-1)).toBe("prior-session:task-wide-launch-error");
expect(messageListRecoveryRevealKeys.at(-1)).toBeUndefined();
});
it("reveals a persisted bootstrap fallback for the selected session", () => {
it("does not reveal a persisted session failure through scroll coupling", () => {
const previousMetadata = panelState.session.metadata;
panelState.session.metadata = {
last_agent_error: {
@@ -241,11 +261,49 @@ describe("TaskChatPanel launch-error ownership", () => {
};
try {
render(<TaskChatPanel sessionId="prior-session" taskId="task-1" />);
render(<TaskChatPanel sessionId={SESSION_ID} taskId="task-1" />);
expect(messageListRecoveryRevealKeys.at(-1)).toBe("prior-session:persisted-bootstrap-error");
expect(messageListRecoveryRevealKeys.at(-1)).toBeUndefined();
} finally {
panelState.session.metadata = previousMetadata;
}
});
it("counts one recovery representation across persisted messages and provisional notices", () => {
const persistedRecoveryMessage = {
...priorTranscriptMessage,
id: "persisted-recovery",
session_id: SESSION_ID as Message["session_id"],
content: "Agent encountered an error: saved session failed.",
metadata: { recovery_actions: true, error_stamp: "session-failure-1" },
created_at: "2026-08-20T11:00:00Z",
} as Message;
const previousMessages = panelState.allMessages;
const previousItems = panelState.groupedItems;
panelState.allMessages = [priorTranscriptMessage, persistedRecoveryMessage];
panelState.groupedItems = insertLastAgentErrorItem(
buildGroupedRenderItems(panelState.allMessages, SESSION_ID, {
canAnchorPrepareProgress: false,
}),
SESSION_ID,
{
message: "saved session failed",
occurredAt: "2026-08-20T11:00:00Z",
stamp: "session-failure-1",
},
panelState.allMessages,
);
try {
render(<TaskChatPanel sessionId={SESSION_ID} taskId="task-1" />);
const persisted = screen.queryAllByTestId("persisted-recovery-message");
const provisional = screen.queryAllByTestId("agent-error-notice");
expect([...persisted, ...provisional]).toHaveLength(1);
expect(provisional).toHaveLength(0);
} finally {
panelState.allMessages = previousMessages;
panelState.groupedItems = previousItems;
}
});
});
+5 -41
View File
@@ -52,16 +52,10 @@ import { routePanelMouseDown } from "./chat/route-panel-mouse-down";
import { useTranslation } from "react-i18next";
import { loadMessageWindowAround } from "@/hooks/domains/session/load-message-window";
import { TaskChatLaunchError } from "./simple/components/task-chat-launch-error";
import { isTypedTaskLaunchError } from "./simple/components/task-launch-error-entry";
import { useTaskLaunchErrorContext } from "./task-launch-error-context";
import { useTaskStatusSummary } from "@/hooks/domains/task/use-task-status-summary";
import {
isTaskLaunchErrorOwnedBySession,
isTaskLaunchErrorVisibleForSession,
} from "@/components/task/chat/types";
import { TaskMarkdownFileLinkProvider } from "@/components/shared/task-markdown-file-link-provider";
import { selectSessionRecoveryError } from "@/lib/session-recovery-presentation";
import { statusSummaryTaskError } from "@/lib/task-status-summary";
/** Returns a `clarificationKey` that increments each time a pending
* clarification is resolved, letting the composer reset its input state for
@@ -1034,22 +1028,8 @@ export const TaskChatPanel = memo(function TaskChatPanel({
pendingClarification,
pendingClarificationGroup,
} = panelState;
const activeLaunchError = launchStatusSummary?.active_error;
const selectedSessionRecoveryError = selectSessionRecoveryError(
activeLaunchError,
resolvedSessionId,
session?.metadata,
);
const visibleRecoveryError =
selectedSessionRecoveryError ??
(isTypedTaskLaunchError(activeLaunchError) &&
isTaskLaunchErrorVisibleForSession(activeLaunchError, resolvedSessionId)
? activeLaunchError
: null);
const launchErrorOwned = Boolean(
isTypedTaskLaunchError(activeLaunchError) &&
isTaskLaunchErrorOwnedBySession(activeLaunchError, resolvedSessionId),
);
const taskLaunchError = statusSummaryTaskError(launchStatusSummary);
const launchErrorOwned = Boolean(taskLaunchError);
const showAgentStartHint = useComposerAgentStartHint(
resolvedSessionId,
session?.state,
@@ -1174,20 +1154,6 @@ export const TaskChatPanel = memo(function TaskChatPanel({
(e: React.MouseEvent<HTMLDivElement>) => routePanelMouseDown(e, panelRef),
[],
);
const launchErrorContent = launchErrorContext ? (
<TaskChatLaunchError
taskId={launchErrorContext.taskId}
workspaceId={launchErrorContext.workspaceId}
statusSummary={launchStatusSummary}
sessionId={resolvedSessionId}
sessionMetadata={session?.metadata}
repositories={launchErrorContext.repositories}
/>
) : null;
const recoveryRevealKey = visibleRecoveryError
? `${resolvedSessionId ?? ""}:${visibleRecoveryError.stamp || visibleRecoveryError.occurred_at}`
: null;
return (
<PanelRoot
ref={panelRef}
@@ -1231,10 +1197,8 @@ export const TaskChatPanel = memo(function TaskChatPanel({
anchoredBarHeight={showAnchoredBar && lastPromptMessage ? anchoredBarHeight : 0}
isVisible={transcriptIsVisible}
launchErrorOwned={launchErrorOwned}
launchErrorStamp={launchErrorOwned ? activeLaunchError?.stamp : undefined}
launchErrorOccurredAt={launchErrorOwned ? activeLaunchError?.occurred_at : undefined}
prependContent={launchErrorContent}
recoveryRevealKey={recoveryRevealKey}
launchErrorStamp={launchErrorOwned ? taskLaunchError?.stamp : undefined}
launchErrorOccurredAt={launchErrorOwned ? taskLaunchError?.occurred_at : undefined}
stickyPromptBar={
showAnchoredBar && lastPromptMessage ? (
<AnchoredLastPromptBar
@@ -6,11 +6,17 @@ import { TaskLaunchErrorProvider, useTaskLaunchErrorContext } from "./task-launc
const { toastMock } = vi.hoisted(() => ({
toastMock: vi.fn(),
}));
const { statusSummaryMock } = vi.hoisted(() => ({
statusSummaryMock: vi.fn(),
}));
vi.mock("@/components/toast-provider", () => ({
useToast: () => ({ toast: toastMock }),
}));
vi.mock("@/lib/i18n", () => ({ t: (key: string) => key }));
vi.mock("@/hooks/domains/task/use-task-status-summary", () => ({
useTaskStatusSummary: statusSummaryMock,
}));
const initialSummary: TaskStatusSummary = {
revision: 1,
@@ -46,6 +52,10 @@ afterEach(() => {
beforeEach(() => {
toastMock.mockReset();
statusSummaryMock.mockReset();
statusSummaryMock.mockImplementation(
(_taskId: string, detail: TaskStatusSummary | null | undefined) => detail,
);
});
describe("TaskLaunchErrorProvider", () => {
@@ -72,4 +82,20 @@ describe("TaskLaunchErrorProvider", () => {
expect(screen.getByTestId("summary-stamp").textContent).toBe("initial");
});
it("replaces the hydrated summary with a live task projection", () => {
const liveSummary: TaskStatusSummary = {
...initialSummary,
active_error: {
...initialSummary.active_error!,
stamp: "live",
},
};
statusSummaryMock.mockReturnValue(liveSummary);
renderProvider(initialSummary);
expect(screen.getByTestId("summary-stamp").textContent).toBe("live");
expect(statusSummaryMock).toHaveBeenCalledWith("task-1", initialSummary);
});
});
@@ -1,6 +1,6 @@
"use client";
import { createContext, useContext, useMemo, type ReactNode } from "react";
import { createContext, useCallback, useContext, useMemo, useRef, type ReactNode } from "react";
import type { TaskRepository } from "@/lib/types/http";
import type { TaskStatusSummary } from "@/lib/types/task-status-summary";
import type { SessionRecoveryOwner } from "@/lib/session-recovery-presentation";
@@ -12,6 +12,8 @@ export type TaskLaunchErrorContextValue = {
statusSummary?: TaskStatusSummary | null;
repositories?: TaskRepository[];
automaticRecovery?: SessionRecoveryOwner | null;
/** Claims one assertive announcement for a task error stamp. */
claimTaskErrorAnnouncement?: (stamp: string) => boolean;
};
const TaskLaunchErrorContext = createContext<TaskLaunchErrorContextValue | null>(null);
@@ -24,8 +26,21 @@ export function TaskLaunchErrorProvider({
children: ReactNode;
}) {
const statusSummary = useTaskStatusSummary(value.taskId, value.statusSummary);
const announcedTaskErrorRef = useRef<string | null>(null);
const claimTaskErrorAnnouncement = useCallback(
(stamp: string) => {
const key = `${value.taskId}:${stamp}`;
if (!stamp || announcedTaskErrorRef.current === key) return false;
announcedTaskErrorRef.current = key;
return true;
},
[value.taskId],
);
const contextValue = useMemo(() => ({ ...value, statusSummary }), [statusSummary, value]);
const contextValue = useMemo(
() => ({ ...value, statusSummary, claimTaskErrorAnnouncement }),
[claimTaskErrorAnnouncement, statusSummary, value],
);
return (
<TaskLaunchErrorContext.Provider value={contextValue}>
{children}
+5 -22
View File
@@ -9,10 +9,9 @@ import { SessionMobileLayout, SessionTabletLayout } from "./mobile";
import type { Repository, RepositoryScript } from "@/lib/types/http";
import type { Terminal } from "@/hooks/domains/session/use-terminals";
import type { Layout } from "react-resizable-panels";
import { isTypedTaskLaunchError } from "./simple/components/task-launch-error-entry";
import { TaskChatLaunchError } from "./simple/components/task-chat-launch-error";
import { useTaskLaunchErrorContext } from "./task-launch-error-context";
import { useTaskCanvasLifecycleActivation } from "./dockview-canvas-activation";
import { statusSummaryTaskError } from "@/lib/task-status-summary";
import { useTaskLaunchErrorContext } from "./task-launch-error-context";
// Re-export for backwards compatibility
export type { SelectedDiff } from "@/hooks/use-session-layout-state";
@@ -76,31 +75,14 @@ export const TaskLayout = memo(function TaskLayout({
taskCanvases = [],
}: TaskLayoutProps) {
const { isMobile, usesDesktopWorkbench, isFullDesktop } = useResponsiveBreakpoint();
const launchErrorContext = useTaskLaunchErrorContext();
const hasSharedTaskError = Boolean(statusSummaryTaskError(launchErrorContext?.statusSummary));
useTaskCanvasLifecycleActivation({ taskId, workspaceId, isMobile });
const router = useRouter();
const onOpenCanvas = useCallback(
(canvasId: string) => router.push(canvasHref(canvasId)),
[router],
);
const launchErrorContext = useTaskLaunchErrorContext();
const activeLaunchError = launchErrorContext?.statusSummary?.active_error;
if (launchErrorContext && !sessionId && isTypedTaskLaunchError(activeLaunchError)) {
return (
<div
className="flex h-full min-h-0 min-w-0 flex-col overflow-auto px-4"
data-testid="session-chat"
>
<TaskChatLaunchError
taskId={launchErrorContext.taskId}
workspaceId={launchErrorContext.workspaceId}
statusSummary={launchErrorContext.statusSummary}
repositories={launchErrorContext.repositories}
/>
</div>
);
}
// Mobile layout
if (isMobile) {
return (
@@ -123,6 +105,7 @@ export const TaskLayout = memo(function TaskLayout({
onTaskUnarchived={onTaskUnarchived}
taskCanvases={taskCanvases}
onOpenCanvas={onOpenCanvas}
hasSharedTaskError={hasSharedTaskError}
/>
);
}
+5 -1
View File
@@ -29,6 +29,7 @@ import {
} from "@/components/task/task-launch-error-context";
import { SessionBootstrapRecoveryCard } from "@/components/task/chat/session-bootstrap-recovery-card";
import { selectSessionRecoveryError } from "@/lib/session-recovery-presentation";
import { TaskSharedError } from "@/components/task/task-shared-error";
import {
buildDebugEntries,
buildArchivedValue,
@@ -430,7 +431,10 @@ export function TaskPageInner(props: TaskPageInnerProps) {
workspaceId={task?.workspace_id ?? null}
isPassthrough={sessionPanel.isSessionPassthrough}
/>
<TaskLayout {...layoutProps} />
<TaskSharedError reserveMobileTopBar={isMobile} />
<div className="flex min-h-0 flex-1 flex-col">
<TaskLayout {...layoutProps} />
</div>
</TaskLaunchErrorProvider>
</TaskArchivedProvider>
</div>
@@ -0,0 +1,113 @@
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { ReactNode } from "react";
import { TaskSharedError } from "./task-shared-error";
const { context, state, taskPreview } = vi.hoisted(() => {
const taskPreview = "The task could not be prepared.";
const state = { isMobile: false };
const context = {
taskId: "task-1",
workspaceId: "workspace-1",
statusSummary: {
revision: 4,
updated_at: "2026-09-14T10:00:00Z",
task_error: {
scope: "task" as const,
stamp: "task-error-1",
occurred_at: "2026-09-14T09:59:00Z",
preview: taskPreview,
recovery_actions: ["retry_launch" as const],
},
},
repositories: [],
claimTaskErrorAnnouncement: vi.fn((_stamp: string) => true),
};
return { context, state, taskPreview };
});
vi.mock("@/components/task/task-launch-error-context", () => ({
useTaskLaunchErrorContext: () => context,
}));
vi.mock("@/hooks/use-responsive-breakpoint", () => ({
useResponsiveBreakpoint: () => ({ isMobile: state.isMobile }),
}));
vi.mock("./simple/components/task-launch-error-entry", () => ({
TaskLaunchErrorEntry: () => <div data-testid="task-shared-error-details-content" />,
}));
vi.mock("@kandev/ui/dialog", () => ({
Dialog: ({ children, open }: { children: ReactNode; open?: boolean }) => (
<div>{open ? children : null}</div>
),
DialogContent: ({ children }: { children: ReactNode }) => <div>{children}</div>,
DialogDescription: ({ children }: { children: ReactNode }) => <div>{children}</div>,
DialogHeader: ({ children }: { children: ReactNode }) => <div>{children}</div>,
DialogTitle: ({ children }: { children: ReactNode }) => <div>{children}</div>,
}));
vi.mock("@kandev/ui/drawer", () => ({
Drawer: ({ children, open }: { children: ReactNode; open?: boolean }) => (
<div>{open ? children : null}</div>
),
DrawerContent: ({ children }: { children: ReactNode }) => <div>{children}</div>,
DrawerDescription: ({ children }: { children: ReactNode }) => <div>{children}</div>,
DrawerHeader: ({ children }: { children: ReactNode }) => <div>{children}</div>,
DrawerTitle: ({ children }: { children: ReactNode }) => <div>{children}</div>,
}));
describe("TaskSharedError", () => {
beforeEach(() => {
context.claimTaskErrorAnnouncement.mockReset();
context.claimTaskErrorAnnouncement.mockReturnValue(true);
});
afterEach(() => {
cleanup();
});
it("keeps a task-owned error above task content and opens desktop details", () => {
state.isMobile = false;
render(<TaskSharedError />);
expect(screen.getByTestId("task-shared-error")).toBeTruthy();
expect(screen.queryByRole("alert")).toBeNull();
expect(screen.getByTestId("task-shared-error-announcement").textContent).toContain(taskPreview);
expect(screen.getByText(taskPreview)).toBeTruthy();
expect(screen.queryByTestId("task-shared-error-details-content")).toBeNull();
fireEvent.click(screen.getByTestId("task-shared-error-details"));
expect(screen.getByTestId("task-shared-error-details-content")).toBeTruthy();
});
it("uses the mobile drawer for the same task error", () => {
state.isMobile = true;
render(<TaskSharedError reserveMobileTopBar />);
expect(screen.getByTestId("task-shared-error").className).toContain(
"mt-[calc(3.5rem+env(safe-area-inset-top,0px))]",
);
fireEvent.click(screen.getByTestId("task-shared-error-details"));
expect(screen.getByTestId("task-shared-error-details-content")).toBeTruthy();
});
it("announces a new task error stamp once across component remounts", () => {
let announcedStamp: string | null = null;
context.claimTaskErrorAnnouncement.mockImplementation((stamp: string) => {
if (announcedStamp === stamp) return false;
announcedStamp = stamp;
return true;
});
const first = render(<TaskSharedError />);
expect(screen.getByTestId("task-shared-error-announcement").textContent).toContain(taskPreview);
first.unmount();
render(<TaskSharedError />);
expect(screen.getByTestId("task-shared-error-announcement").textContent).toBe("");
expect(context.claimTaskErrorAnnouncement).toHaveBeenCalledTimes(2);
});
});
@@ -0,0 +1,130 @@
"use client";
import { useEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { IconAlertTriangle } from "@tabler/icons-react";
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
} from "@kandev/ui/dialog";
import {
Drawer,
DrawerContent,
DrawerDescription,
DrawerHeader,
DrawerTitle,
} from "@kandev/ui/drawer";
import { Button } from "@kandev/ui/button";
import { useResponsiveBreakpoint } from "@/hooks/use-responsive-breakpoint";
import { statusSummaryTaskError } from "@/lib/task-status-summary";
import { cn } from "@/lib/utils";
import { useTaskLaunchErrorContext } from "./task-launch-error-context";
import { TaskLaunchErrorEntry } from "./simple/components/task-launch-error-entry";
/** Persistent task-owned failure surface. It is mounted above the task tabs so
* session changes and Dockview panel changes cannot hide the recovery affordance. */
export function TaskSharedError({
reserveMobileTopBar = false,
}: {
reserveMobileTopBar?: boolean;
} = {}) {
const context = useTaskLaunchErrorContext();
const error = statusSummaryTaskError(context?.statusSummary);
const [detailsOpen, setDetailsOpen] = useState(false);
const localAnnouncementStampRef = useRef<string | null>(null);
const { isMobile } = useResponsiveBreakpoint();
const { t } = useTranslation();
const launchNeedsAttention = t("task:launchNeedsAttention");
const errorStamp = error?.stamp ?? "";
const [announcement, setAnnouncement] = useState("");
useEffect(() => {
if (!context || !error || !errorStamp) return;
const shouldAnnounce = context.claimTaskErrorAnnouncement
? context.claimTaskErrorAnnouncement(errorStamp)
: localAnnouncementStampRef.current !== errorStamp;
if (!shouldAnnounce) return;
localAnnouncementStampRef.current = errorStamp;
setAnnouncement(`${error.preview}. ${launchNeedsAttention}`);
}, [context, error, errorStamp, launchNeedsAttention]);
if (!context || !error) return null;
const details = (
<TaskLaunchErrorEntry
taskId={context.taskId}
workspaceId={context.workspaceId}
error={error}
repositories={context.repositories}
/>
);
return (
<>
<section
className={cn(
"flex min-w-0 shrink-0 items-start gap-3 border-b border-destructive/25 bg-destructive/5 px-4 py-2.5",
isMobile && reserveMobileTopBar && "mt-[calc(3.5rem+env(safe-area-inset-top,0px))]",
)}
data-testid="task-shared-error"
>
<IconAlertTriangle
className="mt-0.5 h-4 w-4 shrink-0 text-destructive"
aria-hidden="true"
/>
<div className="min-w-0 flex-1">
<p className="text-sm font-medium text-destructive">{error.preview}</p>
<p className="mt-0.5 text-xs text-muted-foreground">{launchNeedsAttention}</p>
</div>
<Button
type="button"
variant="outline"
size="sm"
className="min-h-11 shrink-0 cursor-pointer sm:min-h-8"
onClick={() => setDetailsOpen(true)}
data-testid="task-shared-error-details"
>
{t("task:showDetails")}
</Button>
</section>
<p
aria-atomic="true"
aria-live="assertive"
className="sr-only"
data-testid="task-shared-error-announcement"
>
{announcement}
</p>
{isMobile ? (
<Drawer open={detailsOpen} onOpenChange={setDetailsOpen} direction="bottom">
<DrawerContent
className="overflow-hidden pb-[max(1rem,env(safe-area-inset-bottom))] data-[vaul-drawer-direction=bottom]:mb-2 data-[vaul-drawer-direction=bottom]:max-h-[calc(100dvh-1rem)]"
data-testid="task-shared-error-drawer"
>
<DrawerHeader>
<DrawerTitle>{error.preview}</DrawerTitle>
<DrawerDescription>{launchNeedsAttention}</DrawerDescription>
</DrawerHeader>
<div className="min-h-0 overflow-y-auto overscroll-contain px-4 pb-4" data-vaul-no-drag>
{details}
</div>
</DrawerContent>
</Drawer>
) : (
<Dialog open={detailsOpen} onOpenChange={setDetailsOpen}>
<DialogContent className="max-h-[calc(100dvh-2rem)] overflow-y-auto">
<DialogHeader>
<DialogTitle>{error.preview}</DialogTitle>
<DialogDescription>{launchNeedsAttention}</DialogDescription>
</DialogHeader>
{details}
</DialogContent>
</Dialog>
)}
</>
);
}
@@ -63,11 +63,16 @@ async function assertUserVisibleFailure(page: Page, taskId: string, expected: Re
await page.goto(`/t/${taskId}`);
const session = new SessionPage(page);
await session.waitForLoad(30_000);
const launchError = session.activeChat().getByTestId("task-launch-error-entry");
await expect(launchError).toBeVisible({ timeout: 30_000 });
await launchError.getByRole("button", { name: "Show details" }).click();
const details = launchError.getByTestId("task-launch-error-details");
await expect(details).toBeVisible();
const chat = session.activeChat();
const recoveryRows = chat.locator(
"[data-testid='session-recovery-action-message']:has([data-testid='recovery-resume-button'], [data-testid='recovery-fresh-button'], [data-testid='recovery-restart-button'])",
);
await expect(recoveryRows).toHaveCount(1, { timeout: 30_000 });
const recovery = recoveryRows.first();
await expect(recovery).toBeVisible();
const details = recovery.locator("details");
await expect(details).toHaveCount(1);
await details.locator("summary").click();
await expect(details).toContainText(expected);
}
@@ -71,8 +71,10 @@ test.describe("mobile PR watcher missing branch", () => {
await testPage.goto(`/t/${task.id}`);
const session = new SessionPage(testPage);
await session.waitForLoad();
const chat = session.activeChat();
const recovery = chat.getByTestId("task-launch-error-entry");
const sharedError = testPage.getByTestId("task-shared-error");
await expect(sharedError).toBeVisible({ timeout: 30_000 });
await sharedError.getByTestId("task-shared-error-details").tap();
const recovery = testPage.getByTestId("task-launch-error-entry");
await expect(recovery).toHaveCount(1, { timeout: 30_000 });
await expect(recovery).toContainText("The workspace could not be prepared for this launch.");
@@ -92,9 +92,11 @@ test.describe("PR watcher missing branch", () => {
const session = new SessionPage(testPage);
await session.waitForLoad();
// --- Assert the bounded launch-error card appears once in chat ---
const chat = session.activeChat();
const recovery = chat.getByTestId("task-launch-error-entry");
// --- Assert the task-owned error strip and its focused recovery panel ---
const sharedError = testPage.getByTestId("task-shared-error");
await expect(sharedError).toBeVisible({ timeout: 30_000 });
await sharedError.getByTestId("task-shared-error-details").click();
const recovery = testPage.getByTestId("task-launch-error-entry");
await expect(recovery).toHaveCount(1, { timeout: 30_000 });
await expect(recovery).toContainText("The workspace could not be prepared for this launch.");
await expect(recovery).not.toContainText(prBranch);
@@ -106,7 +106,7 @@ test.describe("mobile: delayed resume cancellation", () => {
test.describe("mobile: failed resume recovery", () => {
test.describe.configure({ retries: 1 });
test("failed saved-session load returns a usable recovery card", async ({
test("failed saved-session load retains a usable recovery entry", async ({
testPage,
apiClient,
seedData,
@@ -133,14 +133,15 @@ test.describe("mobile: failed resume recovery", () => {
await fixture.session.recoveryResumeButton().tap();
await expect(fixture.session.recoveryResumeButton()).toBeVisible();
// The resume endpoint waits through provider startup before it returns;
// keep the card usable throughout that failed-load path and ensure the
// old permanently pending label never replaces the recovery action.
// keep the persisted recovery entry usable throughout that failed-load
// path and ensure the old permanently pending label never replaces it.
await expect(testPage.getByText(/Resume session requested/i)).toHaveCount(0, {
timeout: 15_000,
});
await expect(fixture.session.recoveryResumeButton()).toBeVisible({ timeout: 90_000 });
await expect(
fixture.session.activeChat().getByTestId("session-bootstrap-recovery-card"),
).toBeVisible({ timeout: 90_000 });
).toHaveCount(0);
await assertNoDocumentHorizontalOverflow(testPage, "mobile failed resume recovery");
} finally {
await apiClient.deleteAgentProfile(profile.id, true).catch(() => undefined);
@@ -644,7 +644,10 @@ test.describe("Task creation from GitHub URL", () => {
const session = new SessionPage(testPage);
await session.waitForLoad();
const launchError = session.activeChat().getByTestId("task-launch-error-entry");
const sharedError = testPage.getByTestId("task-shared-error");
await expect(sharedError).toBeVisible({ timeout: 30_000 });
await sharedError.getByTestId("task-shared-error-details").click();
const launchError = testPage.getByTestId("task-launch-error-entry");
await expect(launchError).toBeVisible({ timeout: 30_000 });
await expect(launchError).toContainText(/launch needs attention/i);
@@ -12,12 +12,14 @@ import { readTranscriptScrollState, setTranscriptScrollTop } from "../../helpers
import { SessionPage } from "../../pages/session-page";
type LaunchError = {
scope?: "session" | "task";
session_id?: string;
task_repository_id?: string;
stamp?: string;
category?: string;
recovery_actions?: string[];
preview?: string;
details?: string;
};
async function taskLaunchError(
@@ -27,7 +29,20 @@ async function taskLaunchError(
): Promise<LaunchError | null> {
const { tasks } = await apiClient.listTasks(workspaceId);
const task = tasks.find((candidate: { id: string }) => candidate.id === taskId);
return (task?.status_summary?.active_error as LaunchError | null | undefined) ?? null;
const summary = task?.status_summary as
| {
task_error?: LaunchError | null;
active_error?: LaunchError | null;
}
| null
| undefined;
const legacyTaskError = summary?.active_error;
return (
summary?.task_error ??
(legacyTaskError && !legacyTaskError.session_id && legacyTaskError.scope !== "session"
? legacyTaskError
: null)
);
}
async function waitForTaskLaunchError(
@@ -147,12 +162,17 @@ test.describe("task launch failure recovery", () => {
await testPage.goto(`/t/${task.id}`);
const session = new SessionPage(testPage);
await session.waitForLoad();
const sharedError = testPage.getByTestId("task-shared-error");
await expect(sharedError).toBeVisible({ timeout: 30_000 });
await expect(sharedError).toContainText("The linked pull request is already closed or merged.");
await testPage.getByTestId("task-shared-error-details").click();
const card = testPage.getByTestId("task-launch-error-entry");
await expect(card).toHaveCount(1, { timeout: 30_000 });
await expect(card).toContainText("The task is linked to a closed pull request.");
await expect(card).toHaveCount(1);
await testPage.reload();
await session.waitForLoad();
await expect(testPage.getByTestId("task-shared-error")).toBeVisible();
await testPage.getByTestId("task-shared-error-details").click();
await expect(testPage.getByTestId("task-launch-error-entry")).toHaveCount(1);
await testPage.getByTestId("task-launch-mark_review_done-button").click();
@@ -178,7 +198,6 @@ test.describe("task launch failure recovery", () => {
backend,
}, testInfo) => {
test.setTimeout(150_000);
const workflow = await apiClient.createWorkflow(
seedData.workspaceId,
`Missing base recovery ${Date.now()}`,
@@ -231,8 +250,11 @@ test.describe("task launch failure recovery", () => {
}),
).toHaveCount(0);
const sharedError = testPage.getByTestId("task-shared-error");
await expect(sharedError).toBeVisible({ timeout: 30_000 });
await testPage.getByTestId("task-shared-error-details").click();
const card = testPage.getByTestId("task-launch-error-entry");
await expect(card).toHaveCount(1, { timeout: 30_000 });
await expect(card).toHaveCount(1);
await expect(card).toContainText("The selected base branch is not available.");
await expect(card).not.toContainText("branch-that-no-longer-exists");
await expect(testPage.getByTestId("last-agent-error-notice")).toHaveCount(0);
@@ -243,6 +265,8 @@ test.describe("task launch failure recovery", () => {
restoreSeedRepositoryOrigin(seedData);
await testPage.reload();
await session.waitForLoad();
await expect(testPage.getByTestId("task-shared-error")).toBeVisible();
await testPage.getByTestId("task-shared-error-details").click();
await expect(testPage.getByTestId("task-launch-error-entry")).toHaveCount(1);
await testPage.getByTestId("task-launch-pick_base_branch-button").click();
@@ -353,7 +377,7 @@ test.describe("task launch failure recovery", () => {
}
});
test("renders one correlated bootstrap recovery card with safe details", async ({
test("retains session failure after automatic recovery and output", async ({
testPage,
apiClient,
seedData,
@@ -381,7 +405,8 @@ test.describe("task launch failure recovery", () => {
);
await apiClient.seedAgentMessages(task.session_id, 40, "bootstrap history");
const occurredAt = new Date(Date.now() + 60 * 60 * 1000).toISOString();
const failureStamp = "bootstrap-presentation-e2e";
const failureCreatedAt = new Date().toISOString();
const longDetails = Array.from(
{ length: 12 },
(_, index) => `agent_bootstrap; diagnostic_line=${index + 1}; cause=permission_denied`,
@@ -393,14 +418,15 @@ test.describe("task launch failure recovery", () => {
metadata: {
last_agent_error: {
message: "The agent could not start.",
occurred_at: occurredAt,
occurred_at: failureCreatedAt,
agent_execution_id: "bootstrap-execution-e2e",
execution_id: "bootstrap-execution-e2e",
phase: "bootstrap",
attempt_id: "bootstrap-execution-e2e",
code: "generic_launch_failure",
details: longDetails,
stamp: "bootstrap-presentation-e2e",
scope: "session",
stamp: failureStamp,
causes: [
{
operation: "resume",
@@ -411,31 +437,67 @@ test.describe("task launch failure recovery", () => {
},
},
});
await apiClient.seedSessionMessage(task.session_id, {
type: "status",
content: "Agent startup failed: The agent could not start.",
createdAt: failureCreatedAt,
metadata: {
recovery_actions: true,
scope: "session",
error_stamp: failureStamp,
error_output: longDetails,
actions: [
{
type: "ws_request",
label: "Resume session",
icon: "refresh",
test_id: "recovery-resume-button",
params: {
method: "session.recover",
payload: { task_id: task.id, session_id: task.session_id, action: "resume" },
},
},
{
type: "ws_request",
label: "Start fresh session",
icon: "player-play",
test_id: "recovery-fresh-button",
params: {
method: "session.recover",
payload: { task_id: task.id, session_id: task.session_id, action: "fresh_start" },
},
},
],
},
});
await testPage.goto(`/t/${task.id}`);
const session = new SessionPage(testPage);
await session.waitForLoad();
const card = testPage.getByTestId("session-bootstrap-recovery-card");
await expect(card).toHaveCount(1, { timeout: 30_000 });
await expect(testPage.getByTestId("task-launch-error-entry")).toHaveCount(0);
await expect(testPage.getByTestId("session-recovery-error")).toHaveCount(0);
await expect(card).toContainText("Session startup needs attention");
await expect(card).toContainText("Required contribution access was denied.");
await expect(card).not.toContainText("remote-secret");
const failureText = "Agent startup failed: The agent could not start.";
const failureRows = testPage.locator("[id^='msg-']").filter({ hasText: failureText });
await expect(failureRows).toHaveCount(1, { timeout: 30_000 });
const failureRow = failureRows.first();
await expect(failureRow).toContainText(failureText);
await expect(testPage.getByTestId("task-shared-error")).toHaveCount(0);
const transcript = session.activeChat().locator(".chat-message-list").first();
await expect
.poll(async () => (await readTranscriptScrollState(transcript)).scrollOwnerCount)
.toBe(1);
const initialScroll = await readTranscriptScrollState(transcript);
expect(initialScroll.scrollHeight).toBeGreaterThan(initialScroll.clientHeight);
expect(initialScroll.scrollTop).toBe(0);
expect(initialScroll.scrollTop).toBeGreaterThan(0);
await expect(session.activeChat().getByTestId("chat-input-area")).toBeVisible();
const details = card.getByTestId("session-bootstrap-recovery-details");
await expect(details).not.toHaveAttribute("open");
await details.getByText("Recovery details").click();
await expect(details).toHaveAttribute("open", "");
await expect(card).toContainText("The required contribution access was denied.");
const details = failureRow.getByText("Technical details", { exact: true });
await details.click();
const detailsPanel = failureRow.locator("details");
await expect(detailsPanel).toHaveAttribute("open", "");
await expect(detailsPanel).toContainText(
"agent_bootstrap; diagnostic_line=1; cause=permission_denied",
);
await expect(failureRow.getByTestId("recovery-resume-button")).toBeVisible();
await expect(failureRow.getByTestId("recovery-fresh-button")).toBeVisible();
const expandedScroll = await readTranscriptScrollState(transcript);
expect(expandedScroll.scrollOwnerCount).toBe(1);
expect(expandedScroll.scrollHeight).toBeGreaterThan(expandedScroll.clientHeight);
@@ -445,56 +507,103 @@ test.describe("task launch failure recovery", () => {
);
expect(middleScrollTop).toBeGreaterThan(0);
const sameStampMetadata = {
last_agent_error: {
message: "The agent could not start.",
occurred_at: occurredAt,
agent_execution_id: "bootstrap-execution-e2e",
execution_id: "bootstrap-execution-e2e",
phase: "bootstrap",
attempt_id: "bootstrap-execution-e2e",
code: "generic_launch_failure",
details: longDetails,
stamp: "bootstrap-presentation-e2e",
causes: [
{
operation: "resume",
code: "permission_denied",
detail: "The required contribution access was denied.",
},
],
},
};
await apiClient.seedTaskSession(task.id, {
state: "WAITING_FOR_INPUT",
sessionId: task.session_id,
agentProfileId: seedData.agentProfileId,
metadata: sameStampMetadata,
});
await expect
.poll(async () => (await readTranscriptScrollState(transcript)).scrollTop)
.toBeGreaterThanOrEqual(Math.max(0, middleScrollTop - 2));
await apiClient.seedTaskSession(task.id, {
state: "WAITING_FOR_INPUT",
sessionId: task.session_id,
agentProfileId: seedData.agentProfileId,
metadata: {
...sameStampMetadata,
last_agent_error: {
...sameStampMetadata.last_agent_error,
stamp: "bootstrap-presentation-e2e-new",
message: "The agent could not start.",
occurred_at: failureCreatedAt,
agent_execution_id: "bootstrap-execution-e2e",
execution_id: "bootstrap-execution-e2e",
phase: "bootstrap",
attempt_id: "bootstrap-execution-e2e",
code: "generic_launch_failure",
details: longDetails,
scope: "session",
stamp: failureStamp,
dismissed_at: new Date(Date.now() + 1_000).toISOString(),
},
},
});
await expect.poll(async () => (await readTranscriptScrollState(transcript)).scrollTop).toBe(0);
for (const testId of [
"recovery-resume-button",
"recovery-restore-workspace-button",
"recovery-fresh-button",
]) {
await expect(card.getByTestId(testId)).toBeVisible();
}
await apiClient.seedSessionMessage(task.session_id, {
type: "script_execution",
content: "Agent resumed",
createdAt: new Date(Date.now() + 2_000).toISOString(),
metadata: {
script_type: "agent_boot",
is_resuming: true,
status: "exited",
exit_code: 0,
},
});
await apiClient.seedSessionMessage(task.session_id, {
type: "message",
content: "Recovered agent output",
createdAt: new Date(Date.now() + 3_000).toISOString(),
});
await expect(failureRow).toContainText(failureText);
await expect(failureRow.getByTestId("recovery-resume-button")).toHaveCount(0);
await expect
.poll(async () => (await readTranscriptScrollState(transcript)).scrollTop)
.toBeGreaterThanOrEqual(Math.max(0, middleScrollTop - 2));
const nextFailureStamp = "bootstrap-presentation-e2e-new";
const nextFailureCreatedAt = new Date(Date.now() + 4_000).toISOString();
await apiClient.seedTaskSession(task.id, {
state: "WAITING_FOR_INPUT",
sessionId: task.session_id,
agentProfileId: seedData.agentProfileId,
metadata: {
last_agent_error: {
message: "The agent could not start.",
occurred_at: nextFailureCreatedAt,
agent_execution_id: "bootstrap-execution-e2e-new",
execution_id: "bootstrap-execution-e2e-new",
phase: "bootstrap",
attempt_id: "bootstrap-execution-e2e-new",
code: "generic_launch_failure",
details: longDetails,
scope: "session",
stamp: nextFailureStamp,
},
},
});
await apiClient.seedSessionMessage(task.session_id, {
type: "status",
content: failureText,
createdAt: nextFailureCreatedAt,
metadata: {
recovery_actions: true,
scope: "session",
error_stamp: nextFailureStamp,
error_output: longDetails,
actions: [
{
type: "ws_request",
label: "Resume session",
icon: "refresh",
test_id: "recovery-resume-button",
params: {
method: "session.recover",
payload: { task_id: task.id, session_id: task.session_id, action: "resume" },
},
},
],
},
});
await expect(failureRows).toHaveCount(2);
const currentFailureRow = failureRows.last();
await expect(currentFailureRow.getByTestId("recovery-resume-button")).toBeVisible();
await expect(failureRow.getByTestId("recovery-resume-button")).toHaveCount(0);
await testPage.reload();
await session.waitForLoad();
const reloadedFailureRows = testPage.locator("[id^='msg-']").filter({ hasText: failureText });
await expect(reloadedFailureRows).toHaveCount(2);
await expect(reloadedFailureRows.first().getByTestId("recovery-resume-button")).toHaveCount(0);
await expect(reloadedFailureRows.last().getByTestId("recovery-resume-button")).toBeVisible();
await assertNoDocumentHorizontalOverflow(testPage, "bootstrap recovery presentation");
await testPage.screenshot({
@@ -502,14 +611,8 @@ test.describe("task launch failure recovery", () => {
fullPage: true,
});
await prCapture.screenshot("bootstrap-recovery-card-desktop", {
caption: "Desktop bootstrap recovery card with labeled recovery details and actions.",
caption: "Desktop transcript keeps recovered session failure history and current actions.",
fullPage: true,
});
await testPage.reload();
await session.waitForLoad();
await expect(testPage.getByTestId("session-bootstrap-recovery-card")).toHaveCount(1, {
timeout: 30_000,
});
await expect(testPage.getByTestId("session-recovery-error")).toHaveCount(0);
});
});
@@ -13,13 +13,21 @@ import { waitForSessionDone } from "../../helpers/session";
import { readTranscriptScrollState, setTranscriptScrollTop } from "../../helpers/transcript-scroll";
import { SessionPage } from "../../pages/session-page";
async function taskLaunchError(apiClient: ApiClient, workspaceId: string, taskId: string) {
const { tasks } = await apiClient.listTasks(workspaceId);
const summary = tasks.find((candidate) => candidate.id === taskId)?.status_summary;
const legacy = summary?.active_error;
return (
summary?.task_error ??
(legacy && !legacy.session_id && legacy.scope !== "session" ? legacy : null)
);
}
async function waitForLaunchError(apiClient: ApiClient, workspaceId: string, taskId: string) {
await expect
.poll(
async () => {
const { tasks } = await apiClient.listTasks(workspaceId);
const error = tasks.find((candidate) => candidate.id === taskId)?.status_summary
?.active_error;
const error = await taskLaunchError(apiClient, workspaceId, taskId);
return Boolean(error?.stamp && error.category === "base_branch_missing");
},
{ timeout: 60_000, message: "waiting for the mobile launch-error projection" },
@@ -81,8 +89,11 @@ test.describe("mobile task launch failure recovery", () => {
await session.waitForLoad();
await waitForLaunchError(apiClient, seedData.workspaceId, task.id);
const sharedError = testPage.getByTestId("task-shared-error");
await expect(sharedError).toBeVisible({ timeout: 30_000 });
await testPage.getByTestId("task-shared-error-details").tap();
const card = testPage.getByTestId("task-launch-error-entry");
await expect(card).toHaveCount(1, { timeout: 30_000 });
await expect(card).toHaveCount(1);
await expect(testPage.getByTestId("last-agent-error-notice")).toHaveCount(0);
await expect(testPage.getByTestId("prepare-progress-panel")).toHaveCount(0);
await expect(testPage.getByTestId("missing-branch-recovery")).toHaveCount(0);
@@ -100,6 +111,8 @@ test.describe("mobile task launch failure recovery", () => {
restoreSeedRepositoryOrigin(seedData);
await testPage.reload();
await session.waitForLoad();
await expect(testPage.getByTestId("task-shared-error")).toBeVisible();
await testPage.getByTestId("task-shared-error-details").tap();
await testPage.getByTestId("task-launch-pick_base_branch-button").tap();
await expect(testPage.getByTestId("task-launch-branch-picker-mobile")).toBeVisible({
timeout: 30_000,
@@ -130,16 +143,10 @@ test.describe("mobile task launch failure recovery", () => {
})
.toBe("main");
await expect
.poll(
async () => {
const { tasks } = await apiClient.listTasks(seedData.workspaceId);
return (
tasks.find((candidate) => candidate.id === task.id)?.status_summary?.active_error ??
null
);
},
{ timeout: 60_000, message: "waiting for the mobile launch error to clear" },
)
.poll(() => taskLaunchError(apiClient, seedData.workspaceId, task.id), {
timeout: 60_000,
message: "waiting for the mobile launch error to clear",
})
.toBeNull();
expect(taskRepository.id).toBe((await apiClient.getTask(task.id)).repositories?.[0]?.id);
@@ -209,19 +216,10 @@ test.describe("mobile task launch failure recovery", () => {
.toBe(true);
await expect
.poll(
async () => {
const { tasks } = await apiClient.listTasks(seedData.workspaceId);
return (
tasks.find((candidate) => candidate.id === task.id)?.status_summary?.active_error ??
null
);
},
{
timeout: 30_000,
message: "waiting for the mobile local-base launch error to remain clear",
},
)
.poll(() => taskLaunchError(apiClient, seedData.workspaceId, task.id), {
timeout: 30_000,
message: "waiting for the mobile local-base launch error to remain clear",
})
.toBeNull();
await expect(testPage.getByTestId("task-launch-error-entry")).toHaveCount(0);
@@ -236,7 +234,7 @@ test.describe("mobile task launch failure recovery", () => {
}
});
test("renders the bootstrap recovery card with stacked touch controls", async ({
test("retains session failure history with stacked touch controls", async ({
testPage,
apiClient,
seedData,
@@ -264,7 +262,8 @@ test.describe("mobile task launch failure recovery", () => {
);
await apiClient.seedAgentMessages(task.session_id, 40, "mobile bootstrap history");
const occurredAt = new Date(Date.now() + 60 * 60 * 1000).toISOString();
const failureStamp = "mobile-bootstrap-presentation-e2e";
const failureCreatedAt = new Date().toISOString();
const longDetails = Array.from(
{ length: 12 },
(_, index) => `agent_bootstrap; diagnostic_line=${index + 1}; cause=permission_denied`,
@@ -276,14 +275,15 @@ test.describe("mobile task launch failure recovery", () => {
metadata: {
last_agent_error: {
message: "The agent could not start.",
occurred_at: occurredAt,
occurred_at: failureCreatedAt,
agent_execution_id: "mobile-bootstrap-execution-e2e",
execution_id: "mobile-bootstrap-execution-e2e",
phase: "bootstrap",
attempt_id: "mobile-bootstrap-execution-e2e",
code: "generic_launch_failure",
details: longDetails,
stamp: "mobile-bootstrap-presentation-e2e",
scope: "session",
stamp: failureStamp,
causes: [
{
operation: "resume",
@@ -294,29 +294,59 @@ test.describe("mobile task launch failure recovery", () => {
},
},
});
await apiClient.seedSessionMessage(task.session_id, {
type: "status",
content: "Agent startup failed: The agent could not start.",
createdAt: failureCreatedAt,
metadata: {
recovery_actions: true,
scope: "session",
error_stamp: failureStamp,
error_output: longDetails,
actions: [
{
type: "ws_request",
label: "Resume session",
icon: "refresh",
test_id: "recovery-resume-button",
params: {
method: "session.recover",
payload: { task_id: task.id, session_id: task.session_id, action: "resume" },
},
},
{
type: "ws_request",
label: "Start fresh session",
icon: "player-play",
test_id: "recovery-fresh-button",
params: {
method: "session.recover",
payload: { task_id: task.id, session_id: task.session_id, action: "fresh_start" },
},
},
],
},
});
await testPage.goto(`/t/${task.id}`);
const session = new SessionPage(testPage);
await session.waitForLoad();
const card = testPage.getByTestId("session-bootstrap-recovery-card");
await expect(card).toHaveCount(1, { timeout: 30_000 });
await expect(testPage.getByTestId("task-launch-error-entry")).toHaveCount(0);
await expect(testPage.getByTestId("session-recovery-error")).toHaveCount(0);
const failureText = "Agent startup failed: The agent could not start.";
const failureRows = testPage.locator("[id^='msg-']").filter({ hasText: failureText });
await expect(failureRows).toHaveCount(1, { timeout: 30_000 });
const failureRow = failureRows.first();
await expect(testPage.getByTestId("task-shared-error")).toHaveCount(0);
const transcript = session.activeChat().locator(".chat-message-list").first();
await expect
.poll(async () => (await readTranscriptScrollState(transcript)).scrollOwnerCount)
.toBe(1);
const initialScroll = await readTranscriptScrollState(transcript);
expect(initialScroll.scrollHeight).toBeGreaterThan(initialScroll.clientHeight);
expect(initialScroll.scrollTop).toBe(0);
expect(initialScroll.scrollTop).toBeGreaterThan(0);
await expect(session.activeChat().getByTestId("chat-input-area")).toBeVisible();
for (const testId of [
"recovery-resume-button",
"recovery-restore-workspace-button",
"recovery-fresh-button",
]) {
const button = card.getByTestId(testId);
for (const testId of ["recovery-resume-button", "recovery-fresh-button"]) {
const button = failureRow.getByTestId(testId);
await expect(button).toBeVisible();
await expect(button).toBeInViewport();
const box = await button.boundingBox();
@@ -324,11 +354,13 @@ test.describe("mobile task launch failure recovery", () => {
expect(box!.height).toBeGreaterThanOrEqual(44);
}
const details = card.getByTestId("session-bootstrap-recovery-details");
await expect(details).not.toHaveAttribute("open");
await details.getByText("Recovery details").tap();
await expect(details).toHaveAttribute("open", "");
await expect(card).toContainText("The required contribution access was denied.");
const details = failureRow.getByText("Technical details", { exact: true });
await details.tap();
const detailsPanel = failureRow.locator("details");
await expect(detailsPanel).toHaveAttribute("open", "");
await expect(detailsPanel).toContainText(
"agent_bootstrap; diagnostic_line=1; cause=permission_denied",
);
const expandedScroll = await readTranscriptScrollState(transcript);
expect(expandedScroll.scrollOwnerCount).toBe(1);
expect(expandedScroll.scrollHeight).toBeGreaterThan(expandedScroll.clientHeight);
@@ -338,49 +370,103 @@ test.describe("mobile task launch failure recovery", () => {
);
expect(middleScrollTop).toBeGreaterThan(0);
const sameStampMetadata = {
last_agent_error: {
message: "The agent could not start.",
occurred_at: occurredAt,
agent_execution_id: "mobile-bootstrap-execution-e2e",
execution_id: "mobile-bootstrap-execution-e2e",
phase: "bootstrap",
attempt_id: "mobile-bootstrap-execution-e2e",
code: "generic_launch_failure",
details: longDetails,
stamp: "mobile-bootstrap-presentation-e2e",
causes: [
{
operation: "resume",
code: "permission_denied",
detail: "The required contribution access was denied.",
},
],
},
};
await apiClient.seedTaskSession(task.id, {
state: "WAITING_FOR_INPUT",
sessionId: task.session_id,
agentProfileId: seedData.agentProfileId,
metadata: sameStampMetadata,
});
await expect
.poll(async () => (await readTranscriptScrollState(transcript)).scrollTop)
.toBeGreaterThanOrEqual(Math.max(0, middleScrollTop - 2));
await apiClient.seedTaskSession(task.id, {
state: "WAITING_FOR_INPUT",
sessionId: task.session_id,
agentProfileId: seedData.agentProfileId,
metadata: {
...sameStampMetadata,
last_agent_error: {
...sameStampMetadata.last_agent_error,
stamp: "mobile-bootstrap-presentation-e2e-new",
message: "The agent could not start.",
occurred_at: failureCreatedAt,
agent_execution_id: "mobile-bootstrap-execution-e2e",
execution_id: "mobile-bootstrap-execution-e2e",
phase: "bootstrap",
attempt_id: "mobile-bootstrap-execution-e2e",
code: "generic_launch_failure",
details: longDetails,
scope: "session",
stamp: failureStamp,
dismissed_at: new Date(Date.now() + 1_000).toISOString(),
},
},
});
await expect.poll(async () => (await readTranscriptScrollState(transcript)).scrollTop).toBe(0);
await apiClient.seedSessionMessage(task.session_id, {
type: "script_execution",
content: "Agent resumed",
createdAt: new Date(Date.now() + 2_000).toISOString(),
metadata: {
script_type: "agent_boot",
is_resuming: true,
status: "exited",
exit_code: 0,
},
});
await apiClient.seedSessionMessage(task.session_id, {
type: "message",
content: "Recovered agent output",
createdAt: new Date(Date.now() + 3_000).toISOString(),
});
await expect(failureRow).toContainText(failureText);
await expect(failureRow.getByTestId("recovery-resume-button")).toHaveCount(0);
await expect
.poll(async () => (await readTranscriptScrollState(transcript)).scrollTop)
.toBeGreaterThanOrEqual(Math.max(0, middleScrollTop - 2));
const nextFailureStamp = "mobile-bootstrap-presentation-e2e-new";
const nextFailureCreatedAt = new Date(Date.now() + 4_000).toISOString();
await apiClient.seedTaskSession(task.id, {
state: "WAITING_FOR_INPUT",
sessionId: task.session_id,
agentProfileId: seedData.agentProfileId,
metadata: {
last_agent_error: {
message: "The agent could not start.",
occurred_at: nextFailureCreatedAt,
agent_execution_id: "mobile-bootstrap-execution-e2e-new",
execution_id: "mobile-bootstrap-execution-e2e-new",
phase: "bootstrap",
attempt_id: "mobile-bootstrap-execution-e2e-new",
code: "generic_launch_failure",
details: longDetails,
scope: "session",
stamp: nextFailureStamp,
},
},
});
await apiClient.seedSessionMessage(task.session_id, {
type: "status",
content: failureText,
createdAt: nextFailureCreatedAt,
metadata: {
recovery_actions: true,
scope: "session",
error_stamp: nextFailureStamp,
error_output: longDetails,
actions: [
{
type: "ws_request",
label: "Resume session",
icon: "refresh",
test_id: "recovery-resume-button",
params: {
method: "session.recover",
payload: { task_id: task.id, session_id: task.session_id, action: "resume" },
},
},
],
},
});
await expect(failureRows).toHaveCount(2);
const currentFailureRow = failureRows.last();
await expect(currentFailureRow.getByTestId("recovery-resume-button")).toBeVisible();
await expect(failureRow.getByTestId("recovery-resume-button")).toHaveCount(0);
await testPage.reload();
await session.waitForLoad();
const reloadedFailureRows = testPage.locator("[id^='msg-']").filter({ hasText: failureText });
await expect(reloadedFailureRows).toHaveCount(2);
await expect(reloadedFailureRows.first().getByTestId("recovery-resume-button")).toHaveCount(0);
await expect(reloadedFailureRows.last().getByTestId("recovery-resume-button")).toBeVisible();
await assertNoDocumentHorizontalOverflow(testPage, "mobile bootstrap recovery presentation");
await testPage.screenshot({
@@ -388,7 +474,7 @@ test.describe("mobile task launch failure recovery", () => {
fullPage: true,
});
await prCapture.screenshot("bootstrap-recovery-card-mobile", {
caption: "Mobile bootstrap recovery card with stacked touch-sized actions.",
caption: "Mobile transcript keeps recovered session failure history and current actions.",
fullPage: true,
});
});
@@ -241,6 +241,53 @@ describe("filterVisibleMessages empty-turn notice supersession", () => {
});
});
describe("filterVisibleMessages recovery history", () => {
const RECOVERY_MESSAGE = "Could not resume the saved session.";
const FIRST_RECOVERY_ID = "recovery-1";
it("keeps a session failure after later agent output arrives", () => {
const failure = baseMessage({
id: FIRST_RECOVERY_ID,
type: "status",
content: RECOVERY_MESSAGE,
metadata: { recovery_actions: true, recovery_stamp: "failure-1" },
});
const output = baseMessage({
id: "agent-1",
type: "message",
content: "The resumed session is ready.",
created_at: AFTER,
});
expect(
filterVisibleMessages([failure, output], new Set(), new Set()).map((message) => message.id),
).toEqual([FIRST_RECOVERY_ID, "agent-1"]);
});
it("collapses duplicate deliveries of one failure while keeping later failures", () => {
const first = baseMessage({
id: FIRST_RECOVERY_ID,
type: "status",
content: RECOVERY_MESSAGE,
metadata: { recovery_actions: true, recovery_stamp: "failure-1" },
});
const duplicate = { ...first, id: `${FIRST_RECOVERY_ID}-duplicate` };
const second = baseMessage({
id: "recovery-2",
type: "status",
content: RECOVERY_MESSAGE,
metadata: { recovery_actions: true, recovery_stamp: "failure-2" },
created_at: AFTER,
});
expect(
filterVisibleMessages([first, duplicate, second], new Set(), new Set()).map(
(message) => message.id,
),
).toEqual([FIRST_RECOVERY_ID, "recovery-2"]);
});
});
describe("hasFailedAgentBootAfter", () => {
it("returns true when a boot after the failure reports status failed", () => {
expect(hasFailedAgentBootAfter([bootMessage(AFTER, { status: "failed" })], ERROR_AT)).toBe(
+15 -14
View File
@@ -91,22 +91,23 @@ function isRecoveryMessage(message: Message): boolean {
return metadata?.recovery_actions === true;
}
function deduplicateRecoveryMessages(messages: Message[]): Message[] {
let lastRecoveryIndex = -1;
for (let index = messages.length - 1; index >= 0; index--) {
if (isRecoveryMessage(messages[index])) {
lastRecoveryIndex = index;
break;
}
function recoveryMessageStamp(message: Message): string | null {
const metadata = message.metadata as Record<string, unknown> | undefined;
for (const key of ["recovery_stamp", "error_stamp", "failure_stamp"]) {
const value = metadata?.[key];
if (typeof value === "string" && value !== "") return value;
}
if (lastRecoveryIndex === -1) return messages;
const hasLaterActivity = messages
.slice(lastRecoveryIndex + 1)
.some((message) => message.type === "message" || message.type === "content");
return messages.filter((message, index) => {
return null;
}
function deduplicateRecoveryMessages(messages: Message[]): Message[] {
const seenStamps = new Set<string>();
return messages.filter((message) => {
if (!isRecoveryMessage(message)) return true;
if (hasLaterActivity) return false;
return index === lastRecoveryIndex;
const stamp = recoveryMessageStamp(message);
if (!stamp || seenStamps.has(stamp)) return !stamp;
seenStamps.add(stamp);
return true;
});
}
@@ -0,0 +1,35 @@
import { describe, expect, it } from "vitest";
import { sessionId as toSessionId, taskId as toTaskId, type Message } from "@/lib/types/http";
import { buildGroupedRenderItems, insertLastAgentErrorItem } from "./use-processed-messages";
const ERROR_MESSAGE = "peer disconnected before response";
const ERROR_OCCURRED_AT = "2026-06-14T10:05:00Z";
function legacyRecoveryMessage(): Message {
return {
id: "legacy-recovery",
session_id: toSessionId("s1"),
task_id: toTaskId("t1"),
author_type: "agent",
content: `Agent startup failed: ${ERROR_MESSAGE}`,
type: "status",
metadata: { recovery_actions: true },
created_at: ERROR_OCCURRED_AT,
};
}
describe("legacy recovery message deduplication", () => {
it("uses a matching unstamped row instead of adding a provisional notice", () => {
const legacy = legacyRecoveryMessage();
const items = buildGroupedRenderItems([legacy], "s1");
const result = insertLastAgentErrorItem(
items,
"s1",
{ message: ERROR_MESSAGE, occurredAt: ERROR_OCCURRED_AT },
[legacy],
);
expect(result).toBe(items);
});
});
@@ -0,0 +1,85 @@
import { describe, expect, it } from "vitest";
import {
sessionId as toSessionId,
taskId as toTaskId,
type Message,
type MessageType,
} from "@/lib/types/http";
import { buildGroupedRenderItems, insertLastAgentErrorItem } from "./use-processed-messages";
const SESSION_ID = "s1";
const ERROR_OCCURRED_AT = "2026-06-14T10:05:00Z";
function makeMessage(
id: string,
type: MessageType,
metadata?: Record<string, unknown>,
session = SESSION_ID,
): Message {
return {
id,
session_id: toSessionId(session),
task_id: toTaskId("t1"),
author_type: "agent",
content: "The agent encountered an error.",
type,
metadata,
created_at: ERROR_OCCURRED_AT,
};
}
describe("insertLastAgentErrorItem recovery identity", () => {
it("keeps one representation when the persisted recovery message is present", () => {
const persisted = makeMessage("recovery-1", "status", {
recovery_actions: true,
error_stamp: "failure-1",
});
const items = buildGroupedRenderItems([persisted], SESSION_ID, {
canAnchorPrepareProgress: false,
});
const result = insertLastAgentErrorItem(
items,
SESSION_ID,
{ message: persisted.content, occurredAt: ERROR_OCCURRED_AT, stamp: "failure-1" },
[persisted],
);
expect(result.map((item) => item.type)).toEqual(["message"]);
expect(result.filter((item) => item.type === "agent_error_notice")).toHaveLength(0);
});
it.each([
{
name: "a different failure stamp",
messageSession: SESSION_ID,
messageStamp: "failure-1",
errorStamp: "failure-2",
},
{
name: "a different session",
messageSession: "s2",
messageStamp: "failure-1",
errorStamp: "failure-1",
},
])("keeps the provisional notice for $name", ({ messageSession, messageStamp, errorStamp }) => {
const persisted = makeMessage(
"recovery-1",
"status",
{ recovery_actions: true, error_stamp: messageStamp },
messageSession,
);
const items = buildGroupedRenderItems([], SESSION_ID, {
canAnchorPrepareProgress: false,
});
const result = insertLastAgentErrorItem(
items,
SESSION_ID,
{ message: "new failure", occurredAt: ERROR_OCCURRED_AT, stamp: errorStamp },
[persisted],
);
expect(result.map((item) => item.type)).toEqual(["agent_error_notice"]);
});
});
@@ -23,6 +23,8 @@ import {
} from "./use-processed-messages";
const CURRENT_TURN_ID = "turn-current";
const PROVISIONAL_ERROR_MESSAGE = "peer disconnected before response";
const ERROR_OCCURRED_AT = "2026-06-14T10:05:00Z";
function makeMessage(
id: string,
@@ -416,8 +418,8 @@ describe("insertLastAgentErrorItem", () => {
});
const result = insertLastAgentErrorItem(items, "s1", {
message: "peer disconnected before response",
occurredAt: "2026-06-14T10:05:00Z",
message: PROVISIONAL_ERROR_MESSAGE,
occurredAt: ERROR_OCCURRED_AT,
});
expect(result.map((item) => item.type)).toEqual(["message", "agent_error_notice", "message"]);
@@ -425,8 +427,8 @@ describe("insertLastAgentErrorItem", () => {
it("uses the notice as the only item when there are no messages", () => {
const result = insertLastAgentErrorItem([], "s1", {
message: "peer disconnected before response",
occurredAt: "2026-06-14T10:05:00Z",
message: PROVISIONAL_ERROR_MESSAGE,
occurredAt: ERROR_OCCURRED_AT,
});
expect(result).toEqual([
@@ -444,8 +446,8 @@ describe("insertLastAgentErrorItem", () => {
});
const result = insertLastAgentErrorItem(items, "s1", {
message: "peer disconnected before response",
occurredAt: "2026-06-14T10:05:00Z",
message: PROVISIONAL_ERROR_MESSAGE,
occurredAt: ERROR_OCCURRED_AT,
});
expect(result.map((item) => item.type)).toEqual(["message", "agent_error_notice"]);
+26 -1
View File
@@ -15,7 +15,8 @@ import {
type PendingClarificationScope,
} from "@/lib/utils/pending-clarification";
import { createDebugLogger, isDebug } from "@/lib/debug/log";
import type { LastAgentError } from "@/lib/session-last-agent-error";
import { lastAgentErrorStamp, type LastAgentError } from "@/lib/session-last-agent-error";
import { legacyRecoveryMessageMatchesError } from "@/lib/session-recovery-presentation";
import {
buildChildrenByParentToolCallId,
buildPermissionsByToolCallId,
@@ -364,6 +365,7 @@ function buildGroupedItemsForHook(args: {
}),
args.resolvedSessionId,
args.lastAgentError,
args.allSessionMessages,
);
}
@@ -394,8 +396,16 @@ export function insertLastAgentErrorItem(
items: RenderItem[],
resolvedSessionId: string | null,
error?: LastAgentError | null,
persistedMessages: Message[] = [],
): RenderItem[] {
if (!resolvedSessionId || !error) return items;
if (
persistedMessages.some((message) =>
isPersistedRecoveryForSession(message, resolvedSessionId, error),
)
) {
return items;
}
const notice: AgentErrorNoticeItem = {
type: "agent_error_notice",
id: `last-agent-error-${resolvedSessionId}-${error.occurredAt ?? "unknown"}`,
@@ -406,6 +416,21 @@ export function insertLastAgentErrorItem(
return [...items.slice(0, insertAt), notice, ...items.slice(insertAt)];
}
function isPersistedRecoveryForSession(
message: Message,
sessionId: string,
error: LastAgentError,
): boolean {
if (message.session_id !== sessionId) return false;
const metadata = message.metadata as Record<string, unknown> | undefined;
if (metadata?.recovery_actions !== true) return false;
const messageStamp = metadata.error_stamp ?? metadata.recovery_stamp ?? metadata.failure_stamp;
if (typeof messageStamp === "string" && messageStamp !== "") {
return messageStamp === lastAgentErrorStamp(error);
}
return legacyRecoveryMessageMatchesError(message.content, message.created_at, error);
}
/** Builds the todo checklist from the latest persisted `todo`-type message,
* accepting string and `{ text, done }` entries. Total by construction:
* malformed metadata yields an empty list, never throws. */
+58 -11
View File
@@ -7,6 +7,7 @@ import type { AgentErrorCause } from "@/lib/types/task-status-summary";
export type LastAgentError = {
message: string;
scope?: "session" | "task";
occurredAt?: string;
agentExecutionId?: string;
executionId?: string;
@@ -57,6 +58,20 @@ export function setStoredAcknowledgedAgentErrors(map: Record<string, string>): v
}
export function readLastAgentError(metadata: Record<string, unknown> | null | undefined) {
return readLastAgentErrorValue(metadata, false);
}
/** Reads the durable error breadcrumb even after recovery retired its controls. */
export function readLastAgentErrorIncludingDismissed(
metadata: Record<string, unknown> | null | undefined,
) {
return readLastAgentErrorValue(metadata, true);
}
function readLastAgentErrorValue(
metadata: Record<string, unknown> | null | undefined,
includeDismissed: boolean,
): LastAgentError | null {
if (!metadata) return null;
const raw = metadata.last_agent_error;
if (!raw || typeof raw !== "object") return null;
@@ -64,15 +79,36 @@ export function readLastAgentError(metadata: Record<string, unknown> | null | un
const message = typeof record.message === "string" ? record.message : "";
if (!message) return null;
const dismissedAt = readFirstOptionalString(record, ["dismissed_at", "dismissedAt"]);
if (dismissedAt) return null;
return { message, ...readOptionalAgentErrorFields(record) };
if (dismissedAt && !includeDismissed) return null;
return {
message,
...readOptionalAgentErrorFields(record),
...(dismissedAt ? { dismissedAt } : {}),
};
}
function readOptionalAgentErrorFields(
record: Record<string, unknown>,
): Omit<LastAgentError, "message"> {
const result: Omit<LastAgentError, "message"> = {};
return {
...readOptionalAgentErrorIdentity(record),
...readOptionalAgentErrorRecovery(record),
...readStructuredFailureMetadata(record),
};
}
function readOptionalAgentErrorIdentity(
record: Record<string, unknown>,
): Pick<
LastAgentError,
"occurredAt" | "scope" | "agentExecutionId" | "executionId" | "phase" | "attemptId"
> {
const result: Pick<
LastAgentError,
"occurredAt" | "scope" | "agentExecutionId" | "executionId" | "phase" | "attemptId"
> = {};
const occurredAt = readFirstOptionalString(record, ["occurred_at", "occurredAt"]);
const scope = readFirstOptionalString(record, ["scope"]);
const agentExecutionId = readFirstOptionalString(record, [
"agent_execution_id",
"agentExecutionId",
@@ -80,6 +116,25 @@ function readOptionalAgentErrorFields(
const executionId = readFirstOptionalString(record, ["execution_id", "executionId"]);
const phase = readFirstOptionalString(record, ["phase"]);
const attemptId = readFirstOptionalString(record, ["attempt_id", "attemptId"]);
if (occurredAt) result.occurredAt = occurredAt;
if (scope === "session" || scope === "task") result.scope = scope;
if (agentExecutionId) result.agentExecutionId = agentExecutionId;
if (executionId) result.executionId = executionId;
if (phase) result.phase = phase;
if (attemptId) result.attemptId = attemptId;
return result;
}
function readOptionalAgentErrorRecovery(
record: Record<string, unknown>,
): Pick<
LastAgentError,
"causes" | "remediationUrl" | "recoveryActions" | "taskRepositoryId" | "stamp"
> {
const result: Pick<
LastAgentError,
"causes" | "remediationUrl" | "recoveryActions" | "taskRepositoryId" | "stamp"
> = {};
const causes = readAgentErrorCauses(record.causes);
const remediationUrl = readFirstOptionalString(record, ["remediation_url", "remediationUrl"]);
const recoveryActions = normalizeTaskLaunchRecoveryActions(
@@ -90,19 +145,11 @@ function readOptionalAgentErrorFields(
"taskRepositoryId",
]);
const stamp = readFirstOptionalString(record, ["stamp"]);
const structured = readStructuredFailureMetadata(record);
if (occurredAt) result.occurredAt = occurredAt;
if (agentExecutionId) result.agentExecutionId = agentExecutionId;
if (executionId) result.executionId = executionId;
if (phase) result.phase = phase;
if (attemptId) result.attemptId = attemptId;
if (causes.length > 0) result.causes = causes;
if (remediationUrl) result.remediationUrl = remediationUrl;
if (recoveryActions.length > 0) result.recoveryActions = recoveryActions;
if (taskRepositoryId) result.taskRepositoryId = taskRepositoryId;
if (stamp) result.stamp = stamp;
if (structured.code) result.code = structured.code;
if (structured.details) result.details = structured.details;
return result;
}
@@ -41,6 +41,7 @@ describe("session recovery presentation", () => {
selectedSessionMetadata,
),
).toEqual({
scope: "session",
session_id: "session-1",
stamp: "bootstrap-session-1",
occurred_at: "2026-09-11T09:00:00Z",
@@ -1,5 +1,6 @@
import type { TaskStatusSummaryActiveError } from "./types/task-status-summary";
import { lastAgentErrorStamp, readLastAgentError } from "./session-last-agent-error";
import type { LastAgentError } from "./session-last-agent-error";
import type {
ResumptionState,
SessionRecoveryFailure,
@@ -33,6 +34,7 @@ function sessionMetadataRecoveryError(
if (!lastError || lastError.phase !== "bootstrap") return null;
const error: TaskStatusSummaryActiveError = {
scope: "session",
session_id: sessionId,
stamp: lastAgentErrorStamp(lastError),
occurred_at: lastError.occurredAt ?? "",
@@ -61,6 +63,7 @@ export function selectSessionRecoveryError(
const persistedError = sessionMetadataRecoveryError(sessionId, sessionMetadata);
if (persistedError) return persistedError;
if (!isBootstrapSessionRecoveryError(activeError) || !activeError) return null;
if (activeError.scope === "task") return null;
return activeError.session_id === sessionId ? activeError : null;
}
@@ -71,3 +74,32 @@ export function ownsSessionRecoveryChat(
): boolean {
return selectSessionRecoveryError(activeError, sessionId, sessionMetadata) !== null;
}
/** Matches a pre-stamp recovery row to the active session error safely. */
export function legacyRecoveryMessageMatchesError(
contentValue: string,
createdAt: string | undefined,
currentError: LastAgentError,
): boolean {
const content = contentValue.trim();
const message = currentError.message.trim();
if (!content || !message) return false;
const contentMatches =
content === message ||
content === `Agent encountered an error: ${message}` ||
content === `Agent startup failed: ${message}` ||
((content.startsWith("Agent encountered an error:") ||
content.startsWith("Agent startup failed:")) &&
content.endsWith(message));
if (!contentMatches) return false;
// Metadata is written before the transcript row. Exclude an older legacy
// row with the same text when both sides carry usable timestamps.
if (!currentError.occurredAt) return true;
const occurredAt = Date.parse(currentError.occurredAt);
const messageCreatedAt = Date.parse(createdAt ?? "");
return (
Number.isNaN(occurredAt) || Number.isNaN(messageCreatedAt) || messageCreatedAt >= occurredAt
);
}
+19
View File
@@ -4,6 +4,7 @@ import {
pickFreshestStatusSummary,
selectTaskStatusSummary,
statusSummaryActiveErrorPreview,
statusSummaryTaskError,
} from "./task-status-summary";
import type { TaskStatusSummary } from "./types/task-status-summary";
@@ -90,4 +91,22 @@ describe("selectTaskStatusSummary", () => {
};
expect(statusSummaryActiveErrorPreview(taskOwned, {}, {})).toBe("Choose a base branch");
});
it("selects the independent task error even when a session error is newer", () => {
const taskOwned = summary(3);
taskOwned.task_error = {
scope: "task",
stamp: "task-error-3",
occurred_at: "2026-08-19T10:00:00Z",
preview: "Choose a base branch",
};
taskOwned.active_error = {
scope: "session",
session_id: "session-1",
stamp: "session-error-4",
occurred_at: "2026-08-19T11:00:00Z",
preview: "The session failed",
};
expect(statusSummaryTaskError(taskOwned)).toBe(taskOwned.task_error);
});
});
+11
View File
@@ -79,3 +79,14 @@ export function statusSummaryActiveErrorPreview(
}
return error.preview;
}
/** Returns the task-owned error while preserving compatibility with older
* summaries that placed it in `active_error` without a session identity. */
export function statusSummaryTaskError(
summary: TaskStatusSummary | null | undefined,
): TaskStatusSummary["task_error"] {
if (summary?.task_error) return summary.task_error;
const legacy = summary?.active_error;
if (legacy && !legacy.session_id && legacy.scope !== "session") return legacy;
return null;
}
@@ -8,6 +8,7 @@ export type AgentErrorCause = {
};
export type TaskStatusSummaryActiveError = {
scope?: "session" | "task";
session_id?: string;
task_repository_id?: string;
stamp: string;
@@ -37,6 +38,8 @@ export type TaskStatusSummary = {
/** Number of prompts currently en-queued for the task (all sessions). */
queued_prompt_count?: number;
active_error?: TaskStatusSummaryActiveError | null;
/** Current task-owned failure, independent of the selected session. */
task_error?: TaskStatusSummaryActiveError | null;
git?: {
additions?: number;
deletions?: number;