fix: retain session failures across recovery surfaces (#3669)
This commit is contained in:
@@ -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 ©
|
||||
}
|
||||
|
||||
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 = ©
|
||||
}
|
||||
@@ -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 = ©
|
||||
}
|
||||
if summary.ActiveError != nil {
|
||||
copy := *summary.ActiveError
|
||||
copy.RecoveryActions = normalizeRecoveryActionsForCategory(copy.Category, copy.RecoveryActions)
|
||||
if copy.SessionID != "" {
|
||||
scope := activeErrorScope(©)
|
||||
if scope == models.ErrorScopeSession && copy.SessionID != "" {
|
||||
state.errors[copy.SessionID] = ©
|
||||
} else {
|
||||
} else if scope == models.ErrorScopeTask && summary.TaskError == nil {
|
||||
state.taskError = ©
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 ©
|
||||
}
|
||||
|
||||
func normalizeErrorScope(scope string, sessionOwned bool) string {
|
||||
if scope == models.ErrorScopeSession || scope == models.ErrorScopeTask {
|
||||
return scope
|
||||
}
|
||||
if sessionOwned {
|
||||
return models.ErrorScopeSession
|
||||
}
|
||||
return models.ErrorScopeTask
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user