Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

MUL-8177: fix(daemon): label worktree finalize failures as local error (#9465)

Multica Evead382b86ca9c
diff --git a/server/internal/daemon/daemon.go b/server/internal/daemon/daemon.go
index 802c11c4a0..e5310efb8e 100644
--- a/server/internal/daemon/daemon.go
+++ b/server/internal/daemon/daemon.go
@@ -5988,9 +5988,51 @@ func taskRunFailureReason(err error) string {
if errors.As(err, &envSetupErr) {
return taskfailure.ReasonEnvironmentPrepareFailed.String()
}
+ // Local worktree finalization ran after the agent process was already
+ // gone: its error is the daemon's own git bookkeeping, never agent or
+ // provider output. Left to Classify, its text — worktree paths, short
+ // SHAs, branch names — lands in agent_error.*: provider_server_error when
+ // a path segment reads like a 5xx, the agent_error.unknown catchall
+ // otherwise, and either one tells the user to look at the model (#9447).
+ //
+ // Only when it is the whole failure, though. Finalize runs on every exit
+ // path and joins its error onto an earlier one; that earlier one is the
+ // primary cause and keeps its own classification, judged without the
+ // worktree text so a path cannot decide it either.
+ var preserved *worktreePreservedError
+ if errors.As(err, &preserved) {
+ primary := withoutWorktreePreserved(err)
+ if primary == nil {
+ return "local_directory_error"
+ }
+ return taskRunFailureReason(primary)
+ }
return taskfailure.Classify(err.Error()).String()
}
+// withoutWorktreePreserved strips the worktree finalization error out of a
+// run error, returning what is left: nil when finalization was the only
+// failure, the remaining errors when runTask joined it onto an earlier one.
+// A single-chain error that wraps it is finalization's alone and yields nil;
+// errors that do not contain it are returned unchanged.
+func withoutWorktreePreserved(err error) error {
+ joined, ok := err.(interface{ Unwrap() []error })
+ if !ok {
+ var preserved *worktreePreservedError
+ if errors.As(err, &preserved) {
+ return nil
+ }
+ return err
+ }
+ var rest []error
+ for _, e := range joined.Unwrap() {
+ if e = withoutWorktreePreserved(e); e != nil {
+ rest = append(rest, e)
+ }
+ }
+ return errors.Join(rest...)
+}
+
// acquireLocalDirectoryLockIfNeeded inspects the task's project resources for
// a local_directory pinned to this daemon, validates the path, and takes the
// path mutex. Returns a release callback (nil when no local_directory
diff --git a/server/internal/daemon/worktree_finalize_failure_reason_test.go b/server/internal/daemon/worktree_finalize_failure_reason_test.go
new file mode 100644
index 0000000000..6cf46c7b33
--- /dev/null
+++ b/server/internal/daemon/worktree_finalize_failure_reason_test.go
@@ -0,0 +1,71 @@
+package daemon
+
+import (
+ "errors"
+ "fmt"
+ "testing"
+
+ "github.com/multica-ai/multica/server/pkg/taskfailure"
+)
+
+// finalizeRefusal builds the error runTask's Finalize defer returns when the
+// delivery point cannot be recorded (#9447), with a worktree path chosen by the
+// caller — the path is exactly the part of the text the classifier used to
+// read as provider output.
+func finalizeRefusal(path string) error {
+ return &worktreePreservedError{err: fmt.Errorf("local_directory worktree: %w", fmt.Errorf(
+ "refusing to record branch agent/codex/1a2b3c4d: the delivered commit 9c1d2e3f no longer contains a503f1e2, "+
+ "the commit this turn started from; the task worktree is preserved at %s (listed by `git worktree list` in /Users/u/repo)",
+ path))}
+}
+
+// TestTaskRunFailureReasonLabelsWorktreeFinalization: a run whose agent
+// succeeded but whose worktree could not be finalized is a local git
+// bookkeeping failure. Before #9447 its text went to taskfailure.Classify and
+// came out as agent_error.provider_server_error when a path segment looked
+// like a 5xx, agent_error.unknown otherwise — both blame the agent/provider.
+func TestTaskRunFailureReasonLabelsWorktreeFinalization(t *testing.T) {
+ cases := map[string]error{
+ "5xx-looking path segment": finalizeRefusal("/Users/u/multica_workspaces/ws-500/task/worktree"),
+ "plain path": finalizeRefusal("/Users/u/multica_workspaces/ws/c503f/worktree"),
+ "joined with nothing else": errors.Join(finalizeRefusal("/Users/u/w/503/worktree")),
+ }
+ for name, err := range cases {
+ t.Run(name, func(t *testing.T) {
+ got := taskRunFailureReason(err)
+ if got != "local_directory_error" {
+ t.Fatalf("taskRunFailureReason = %q, want local_directory_error", got)
+ }
+ if taskfailure.Reason(got).IsAgentError() {
+ t.Fatalf("finalization failure labelled as an agent error: %q", got)
+ }
+ })
+ }
+}
+
+// TestTaskRunFailureReasonKeepsPrimaryCauseOverFinalization: Finalize runs on
+// every exit path and joins its error onto the run's own. The run's error is
+// the primary cause and keeps its label — classified without the worktree text,
+// so the path can neither override nor produce it.
+func TestTaskRunFailureReasonKeepsPrimaryCauseOverFinalization(t *testing.T) {
+ primary := errors.New("stream disconnected before completion")
+ err := errors.Join(primary, finalizeRefusal("/Users/u/w/ws-500/worktree"))
+ want := taskfailure.Classify(primary.Error()).String()
+ if got := taskRunFailureReason(err); got != want {
+ t.Fatalf("taskRunFailureReason = %q, want %q (primary cause)", got, want)
+ }
+
+ // A primary cause the classifier cannot name must not borrow a label
+ // from the worktree path joined after it.
+ vague := errors.New("agent exited")
+ err = errors.Join(vague, finalizeRefusal("/Users/u/w/ws-500/worktree"))
+ if got, want := taskRunFailureReason(err), taskfailure.Classify(vague.Error()).String(); got != want {
+ t.Fatalf("taskRunFailureReason = %q, want %q (path must not decide)", got, want)
+ }
+
+ // Structural sentinels on the primary side still win.
+ err = errors.Join(errTaskPrepareTimeout, finalizeRefusal("/Users/u/w/worktree"))
+ if got := taskRunFailureReason(err); got != taskfailure.ReasonTimeout.String() {
+ t.Fatalf("taskRunFailureReason = %q, want %q", got, taskfailure.ReasonTimeout)
+ }
+}
diff --git a/server/internal/integrations/wecom/quoted_media_bind_db_test.go b/server/internal/integrations/wecom/quoted_media_bind_db_test.go
index 56a56df400..73841bc20b 100644
--- a/server/internal/integrations/wecom/quoted_media_bind_db_test.go
+++ b/server/internal/integrations/wecom/quoted_media_bind_db_test.go
@@ -249,17 +249,18 @@ func runQuotedBindScenario(t *testing.T, sc quotedBindScenario, streaming bool,
var attachments int
deadline := time.Now().Add(20 * time.Second)
for {
+ // Body and attachment count in ONE statement, so both come from the
+ // same snapshot. Read separately, the bind transaction could commit
+ // between them: the count then says binding is done while the body
+ // read just before it still carries the unbound markers.
err := pool.QueryRow(ctx, `
- SELECT cm.id, cm.content
+ SELECT cm.id, cm.content,
+ (SELECT count(*) FROM attachment a WHERE a.chat_message_id = cm.id)
FROM chat_message cm
JOIN chat_session cs ON cs.id = cm.chat_session_id
WHERE cs.workspace_id = $1 AND cm.role = 'user'
- ORDER BY cm.created_at DESC LIMIT 1`, fixture.workspaceID).Scan(&chatMessageID, &body)
+ ORDER BY cm.created_at DESC LIMIT 1`, fixture.workspaceID).Scan(&chatMessageID, &body, &attachments)
if err == nil {
- if err := pool.QueryRow(ctx,
- `SELECT count(*) FROM attachment WHERE chat_message_id = $1`, chatMessageID).Scan(&attachments); err != nil {
- t.Fatalf("count attachments: %v", err)
- }
if (attachments >= len(sc.wantAttachments) && tasks.promotions() > 0) || time.Now().After(deadline) {
break
}