Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(daemon): trust the agent's session id from session/resume across ACP backends (#2070)
prellree10c508fbee
diff --git a/server/pkg/agent/hermes.go b/server/pkg/agent/hermes.goindex dccca64fd5..800ef19d94 100644--- a/server/pkg/agent/hermes.go+++ b/server/pkg/agent/hermes.go@@ -194,8 +194,15 @@ func (b *hermesBackend) Execute(ctx context.Context, prompt string, opts ExecOptresCh <- Result{Status: finalStatus, Error: finalError, DurationMs: time.Since(startTime).Milliseconds()}return}- sessionID = opts.ResumeSessionID- _ = result+ var changed bool+ sessionID, changed = resolveResumedSessionID(opts.ResumeSessionID, result)+ if changed {+ b.cfg.Logger.Warn("agent returned a different session id on resume — original was likely lost; continuing with the new id",+ "backend", "hermes",+ "requested", opts.ResumeSessionID,+ "actual", sessionID,+ )+ }} else {result, err := c.request(runCtx, "session/new", buildHermesSessionParams(cwd, opts.Model))if err != nil {@@ -1074,6 +1081,24 @@ func extractACPSessionID(result json.RawMessage) string {return r.SessionID}+// resolveResumedSessionID picks which session id we should treat as live+// after a `session/resume` round-trip. Hermes (and other ACP servers)+// return the canonical sessionId in the response — when the local+// state.db has been wiped, the server silently creates a brand-new+// session and returns its new id rather than failing. If we keep using+// our requested id in that case, every subsequent session/prompt is+// addressed to a session the server doesn't know about and fails with+// JSON-RPC -32603. Returns (chosenID, changed). When the response is+// malformed or omits sessionId we fall back to the requested id so the+// happy path keeps working against older / non-conforming servers.+func resolveResumedSessionID(requested string, response json.RawMessage) (string, bool) {+ got := extractACPSessionID(response)+ if got == "" {+ return requested, false+ }+ return got, got != requested+}+// buildHermesSessionParams constructs the params map for the ACP `session/new`// request. The `model` field is only included when non-empty so Hermes falls// back to its default only when no explicit model was configured.diff --git a/server/pkg/agent/hermes_test.go b/server/pkg/agent/hermes_test.goindex e45a9b6a0f..a6121528d3 100644--- a/server/pkg/agent/hermes_test.go+++ b/server/pkg/agent/hermes_test.go@@ -48,6 +48,63 @@ func TestExtractACPSessionIDInvalidJSON(t *testing.T) {}}+// ── resolveResumedSessionID ──++func TestResolveResumedSessionIDMatching(t *testing.T) {+ t.Parallel()+ // Server confirms our requested id — happy resume path. No change.+ got, changed := resolveResumedSessionID(+ "ses_alpha",+ json.RawMessage(`{"sessionId":"ses_alpha"}`),+ )+ if got != "ses_alpha" {+ t.Errorf("got %q, want ses_alpha", got)+ }+ if changed {+ t.Errorf("changed: got true, want false")+ }+}++func TestResolveResumedSessionIDDifferent(t *testing.T) {+ t.Parallel()+ // Server returned a different id — local state was lost and the+ // server silently spun up a new session. We trust the server.+ got, changed := resolveResumedSessionID(+ "ses_alpha",+ json.RawMessage(`{"sessionId":"ses_beta_new"}`),+ )+ if got != "ses_beta_new" {+ t.Errorf("got %q, want ses_beta_new", got)+ }+ if !changed {+ t.Errorf("changed: got false, want true")+ }+}++func TestResolveResumedSessionIDEmptyResponse(t *testing.T) {+ t.Parallel()+ // Older / non-conforming server returns no sessionId — defensive+ // fallback to the requested id. This preserves the legacy happy+ // path; a stale id will eventually fail downstream and be retried+ // via the daemon's session-resume fallback (daemon.go).+ for _, body := range []string{+ `{}`,+ `{"sessionId":""}`,+ `not json`,+ } {+ got, changed := resolveResumedSessionID(+ "ses_alpha",+ json.RawMessage(body),+ )+ if got != "ses_alpha" {+ t.Errorf("body=%q: got %q, want ses_alpha", body, got)+ }+ if changed {+ t.Errorf("body=%q: changed: got true, want false", body)+ }+ }+}+// ── buildHermesSessionParams ──func TestBuildHermesSessionParamsIncludesModel(t *testing.T) {diff --git a/server/pkg/agent/kimi.go b/server/pkg/agent/kimi.goindex 46d4c4744a..98a4b910ee 100644--- a/server/pkg/agent/kimi.go+++ b/server/pkg/agent/kimi.go@@ -190,8 +190,15 @@ func (b *kimiBackend) Execute(ctx context.Context, prompt string, opts ExecOptioresCh <- Result{Status: finalStatus, Error: finalError, DurationMs: time.Since(startTime).Milliseconds()}return}- sessionID = opts.ResumeSessionID- _ = result+ var changed bool+ sessionID, changed = resolveResumedSessionID(opts.ResumeSessionID, result)+ if changed {+ b.cfg.Logger.Warn("agent returned a different session id on resume — original was likely lost; continuing with the new id",+ "backend", "kimi",+ "requested", opts.ResumeSessionID,+ "actual", sessionID,+ )+ }} else {result, err := c.request(runCtx, "session/new", map[string]any{"cwd": cwd,diff --git a/server/pkg/agent/kiro.go b/server/pkg/agent/kiro.goindex e8e7197182..250030f976 100644--- a/server/pkg/agent/kiro.go+++ b/server/pkg/agent/kiro.go@@ -171,7 +171,7 @@ func (b *kiroBackend) Execute(ctx context.Context, prompt string, opts ExecOptio}if opts.ResumeSessionID != "" {- _, err := c.request(runCtx, "session/load", map[string]any{+ result, err := c.request(runCtx, "session/load", map[string]any{"cwd": cwd,"sessionId": opts.ResumeSessionID,"mcpServers": []any{},@@ -182,7 +182,23 @@ func (b *kiroBackend) Execute(ctx context.Context, prompt string, opts ExecOptioresCh <- Result{Status: finalStatus, Error: finalError, DurationMs: time.Since(startTime).Milliseconds()}return}- sessionID = opts.ResumeSessionID+ // Apply the same defensive resolution kimi/hermes use: if+ // kiro echoes a sessionId in the session/load response, prefer+ // it (the canonical id the backend is committed to). When the+ // response is empty or doesn't include sessionId — kiro's+ // current observed shape — the helper falls back to the+ // requested id, preserving today's behavior. Fixing this here+ // too means a future kiro that DOES return a different id on+ // silent state reset is handled the same way as hermes/kimi.+ var changed bool+ sessionID, changed = resolveResumedSessionID(opts.ResumeSessionID, result)+ if changed {+ b.cfg.Logger.Warn("agent returned a different session id on resume — original was likely lost; continuing with the new id",+ "backend", "kiro",+ "requested", opts.ResumeSessionID,+ "actual", sessionID,+ )+ }} else {result, err := c.request(runCtx, "session/new", map[string]any{"cwd": cwd,