Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
MUL-7875 fix(daemon): preserve Hermes sessions across worktrees (ZIC-301) (#9065)
ZIcefbd3470eacc1
diff --git a/server/internal/daemon/daemon.go b/server/internal/daemon/daemon.goindex 85745dab5c..be0e353755 100644--- a/server/internal/daemon/daemon.go+++ b/server/internal/daemon/daemon.go@@ -6543,18 +6543,12 @@ func providerNeedsInlineSystemPrompt(provider string) bool {// changes, so binding it to workdir reuse discards healthy conversation history// and forces the model to reconstruct it through `multica chat history`.//-// A matching workdir is not sufficient on its own. Hermes keys its sessions to-// HERMES_HOME — the per-task overlay under envRoot — not to the cwd, and the-// two keys come apart precisely in the local_directory flow: reuse is disabled-// there (shouldReusePriorWorkdir), so every task builds a fresh overlay with an-// empty state.db, while envWorkDir stays the user's own directory and therefore-// still equals PriorWorkDir. The gate read "reused" and forwarded a session id-// that could not possibly resolve, and Hermes answers an unresolvable resume by-// silently starting over (GH #6806). sessionHomeReachable is the provider's own-// answer to "can a prior session still be found here?" — for Hermes, whether-// the conversation's session store got mounted (execenv.Environment-// HermesSessionStore) — and false drops the resume with the same disclosure as-// a workdir mismatch.+// Hermes is also independent of cwd: its transcript lives in HERMES_HOME's+// state.db. sessionHomeReachable checks whether the conversation-scoped store+// mounted by execenv holds history, or whether the task-local home was reused.+// Requiring the prior cwd as well would discard reachable history whenever a+// local_directory task gets a new worktree (#9062). An empty or unavailable+// store must still drop the resume, even when the cwd matches (#6806).// sameExistingDir reports whether two paths name the same existing directory.// False when either cannot be stat'd, which is the safe answer for cwd-keyed// providers: an absent prior workdir means there is nothing to resume from.@@ -6577,6 +6571,8 @@ func gateResumeToReachableSession(task *Task, taskCtx *execenv.TaskContextForEnvvar reachable boolif providerUsesPiSessionFile(provider) {reachable = piSessionResumable(task.PriorSessionID, refusesMissingSessionCwd)+ } else if provider == "hermes" {+ reachable = sessionHomeReachable} else {// Compare the directories, not the spelling. Reuse runs in the canonical// path it validated and locked, which need not be character-identical todiff --git a/server/internal/daemon/daemon_test.go b/server/internal/daemon/daemon_test.goindex 3bda712758..d2c550f7fa 100644--- a/server/internal/daemon/daemon_test.go+++ b/server/internal/daemon/daemon_test.go@@ -2021,6 +2021,106 @@ func TestGateResumeToReachableSession(t *testing.T) {}}+// Hermes resumes from HERMES_HOME, independently of the task's cwd (#9062).+func TestGateHermesResumeToSessionHome(t *testing.T) {+ t.Parallel()+ for _, tt := range []struct {+ name string+ sameWorkdir bool+ history bool+ envReused bool+ noStore bool+ want bool+ }{+ {name: "changed worktree with history", history: true, want: true},+ {name: "in place with history", sameWorkdir: true, history: true, want: true},+ {name: "changed worktree with unavailable history"},+ {name: "in place with unavailable history", sameWorkdir: true},+ {name: "empty store despite reused environment", sameWorkdir: true, envReused: true},+ {name: "unmounted store in fresh environment", noStore: true},+ {name: "task local history in reused environment", sameWorkdir: true, noStore: true, envReused: true, want: true},+ } {+ t.Run(tt.name, func(t *testing.T) {+ priorDir, workDir := t.TempDir(), t.TempDir()+ if tt.sameWorkdir {+ workDir = priorDir+ }+ env := &execenv.Environment{HermesSessionStore: "conversation-store", HermesSessionHistoryPresent: tt.history}+ if tt.noStore {+ env.HermesSessionStore = ""+ }+ task := Task{PriorSessionID: "session-1", PriorWorkDir: priorDir}+ taskCtx := execenv.TaskContextForEnv{PriorSessionResumed: true}+ got := gateResumeToReachableSession(&task, &taskCtx, "hermes", workDir,+ sessionHomeReachable("hermes", env, tt.envReused), false, slog.Default())+ if got != tt.want || (task.PriorSessionID == "session-1") != tt.want || taskCtx.PriorSessionResumed != tt.want {+ t.Fatalf("resume = %v, session = %q, resumed = %v; want %v", got, task.PriorSessionID, taskCtx.PriorSessionResumed, tt.want)+ }+ if task.PriorSessionResumeUnavailable != !tt.want || taskCtx.PriorSessionResumeUnavailable != !tt.want {+ t.Fatal("session continuity notice does not match reachability")+ }+ })+ }+}++func TestHermesPreparedSessionReachability(t *testing.T) {+ // Keep profile/store resolution inside synthetic homes, never the user's.+ t.Setenv("MULTICA_TASK_CONFIG_ROOT", "")+ home := t.TempDir()+ t.Setenv("HOME", home)+ t.Setenv("USERPROFILE", home)+ sourceHome, root := t.TempDir(), t.TempDir()+ taskNumber := 0+ prepare := func(t *testing.T, agentID, issueID string) *execenv.Environment {+ t.Helper()+ taskCtx := execenv.TaskContextForEnv{AgentID: agentID, IssueID: issueID,+ AgentSkills: []execenv.SkillContextForEnv{{Name: "fixture", Content: "synthetic skill"}},+ }+ taskNumber+++ env, err := execenv.Prepare(execenv.PrepareParams{+ WorkspacesRoot: root, WorkspaceID: "workspace-1", TaskID: fmt.Sprintf("task-%012d", taskNumber),+ Provider: "hermes", HermesSourceHome: sourceHome, Task: taskCtx,+ HermesSessionStore: execenv.HermesSessionStorePath("", agentID, sourceHome, taskCtx),+ }, slog.Default())+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = env.Cleanup(true) })+ return env+ }+ first := prepare(t, "agent-1", "issue-1")+ if first.HermesSessionStore == "" {+ if runtime.GOOS == "windows" {+ t.Skip("host cannot mount Hermes session stores")+ }+ t.Fatal("Hermes session store was not mounted")+ }+ if err := os.WriteFile(filepath.Join(first.HermesHome, "state.db"), []byte("synthetic transcript"), 0o600); err != nil {+ t.Fatal(err)+ }+ for _, tt := range []struct {+ name, agentID, issueID string+ want bool+ }{+ {"same conversation in fresh workdir", "agent-1", "issue-1", true},+ {"different issue", "agent-1", "issue-2", false},+ {"different agent", "agent-2", "issue-1", false},+ } {+ t.Run(tt.name, func(t *testing.T) {+ env := prepare(t, tt.agentID, tt.issueID)+ if sameExistingDir(first.WorkDir, env.WorkDir) {+ t.Fatal("fixture must prepare a different task workdir")+ }+ task := Task{PriorSessionID: "session-1", PriorWorkDir: first.WorkDir}+ taskCtx := execenv.TaskContextForEnv{PriorSessionResumed: true}+ if got := gateResumeToReachableSession(&task, &taskCtx, "hermes", env.WorkDir,+ sessionHomeReachable("hermes", env, false), false, slog.Default()); got != tt.want {+ t.Fatalf("reachable = %v, want %v", got, tt.want)+ }+ })+ }+}+func TestGatePiResumeToSessionFile(t *testing.T) {t.Parallel()diff --git a/server/internal/daemon/execenv/hermes_sessions.go b/server/internal/daemon/execenv/hermes_sessions.goindex 140ccac15f..98f55be6bb 100644--- a/server/internal/daemon/execenv/hermes_sessions.go+++ b/server/internal/daemon/execenv/hermes_sessions.go@@ -210,10 +210,27 @@ func mountHermesSessionDB(hermesHome, storeDir string, logger *slog.Logger) (herconst hermesSessionLinkStagingEntry = ".multica-session-link"// hermesStoreHasSessionDB reports whether storeDir holds a session database-// with content. A zero-length file is what SQLite leaves after an `open` that-// never wrote a page, and resuming against it is the same amnesia as an absent-// one — so it counts as no history, not as history.+// with readable content. A zero-length file is what SQLite leaves after an+// `open` that never wrote a page; resuming against it is the same as an absent+// database, so it counts as no history.func hermesStoreHasSessionDB(storeDir string) bool {+ path := filepath.Join(storeDir, hermesSessionDBEntry)+ fi, err := os.Stat(path)+ if err != nil || !fi.Mode().IsRegular() || fi.Size() == 0 {+ return false+ }+ db, err := os.Open(path)+ if err != nil {+ return false+ }+ defer db.Close()+ return true+}++// hermesStoreContainsSessionDB protects existing history during migration,+// even when it cannot currently be opened for resume. Read permissions do not+// prevent unlinking a database from a writable directory.+func hermesStoreContainsSessionDB(storeDir string) bool {fi, err := os.Stat(filepath.Join(storeDir, hermesSessionDBEntry))return err == nil && fi.Mode().IsRegular() && fi.Size() > 0}@@ -234,7 +251,7 @@ func hermesStoreHasSessionDB(storeDir string) bool {// Only an empty store is migrated into — a store that already holds a database// is this conversation's real history and must never be overwritten.func migrateHermesTaskSessionDB(hermesHome, storeDir string, logger *slog.Logger) error {- if hermesStoreHasSessionDB(storeDir) {+ if hermesStoreContainsSessionDB(storeDir) {return nil // store already holds this conversation — never overwrite it}@@ -306,7 +323,7 @@ func migrateHermesTaskSessionDB(hermesHome, storeDir string, logger *slog.Loggerfunc publishHermesSessionStaging(staging, storeDir string) (bool, error) {hermesSessionPublishMu.Lock()defer hermesSessionPublishMu.Unlock()- if hermesStoreHasSessionDB(storeDir) {+ if hermesStoreContainsSessionDB(storeDir) {return false, nil // a competitor published a real transcript first}if hermesSessionPublishBarrier != nil {@@ -315,7 +332,7 @@ func publishHermesSessionStaging(staging, storeDir string) (bool, error) {if err := removeHermesSessionDBFamily(storeDir); err != nil {return false, err}- return promoteHermesStoreStaging(staging, storeDir, hermesStoreHasSessionDB)+ return promoteHermesStoreStaging(staging, storeDir, hermesStoreContainsSessionDB)}// hermesSessionPublishMu serializes every session-store publish in thisdiff --git a/server/internal/daemon/execenv/hermes_sessions_test.go b/server/internal/daemon/execenv/hermes_sessions_test.goindex 2996ededbd..e2404a2ea0 100644--- a/server/internal/daemon/execenv/hermes_sessions_test.go+++ b/server/internal/daemon/execenv/hermes_sessions_test.go@@ -229,6 +229,53 @@ func TestPrepareHermesHomeMigrationNeverOverwritesStore(t *testing.T) {}}+func TestPrepareHermesHomeMigrationPreservesUnreadableStore(t *testing.T) {+ if runtime.GOOS == "windows" {+ t.Skip("Unix permissions required")+ }+ sharedHome := t.TempDir()+ store := t.TempDir()+ hermesHome := filepath.Join(t.TempDir(), "hermes-home")+ skills := []SkillContextForEnv{{Name: "deploy", Content: "# Deploy"}}+ db := filepath.Join(store, "state.db")+ mustWrite(t, db, "the real transcript")+ mustWrite(t, filepath.Join(store, "state.db-wal"), "the real uncheckpointed history")+ if err := os.Chmod(db, 0); err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = os.Chmod(db, 0o600) })+ if f, err := os.Open(db); err == nil {+ f.Close()+ t.Skip("current user can read files despite permissions")+ }+ staging := t.TempDir()+ mustWrite(t, filepath.Join(staging, "state.db"), "competing transcript")+ mustWrite(t, filepath.Join(staging, "state.db-wal"), "competing WAL")+ if published, err := publishHermesSessionStaging(staging, store); err != nil || published {+ t.Fatalf("publish over unreadable history = %v, %v; want false, nil", published, err)+ }+ if _, err := prepareHermesHome(hermesHome, sharedHome, false, skills, nil, "", "", testLogger()); err != nil {+ t.Fatal(err)+ }+ mustWrite(t, filepath.Join(hermesHome, "state.db"), "stray task-local database")+ mustWrite(t, filepath.Join(hermesHome, "state.db-wal"), "stray task-local WAL")+ if _, err := prepareHermesHome(hermesHome, sharedHome, false, skills, nil, "", store, testLogger()); err != nil {+ t.Fatal(err)+ }+ if hermesStoreHasSessionDB(store) {+ t.Error("unreadable history must not be resumable")+ }+ if err := os.Chmod(db, 0o600); err != nil {+ t.Fatal(err)+ }+ for name, want := range map[string]string{"state.db": "the real transcript", "state.db-wal": "the real uncheckpointed history"} {+ got, err := os.ReadFile(filepath.Join(store, name))+ if err != nil || string(got) != want {+ t.Errorf("persistent %s = %q, %v; want %q", name, got, err, want)+ }+ }+}+// TestPrepareHermesHomeSessionMountIsIdempotent covers Reuse: rebuilding the// overlay for a follow-up turn in the same task directory must leave the link// (and therefore the live database) alone.@@ -843,3 +890,23 @@ func requireSymlinks(t *testing.T) {t.Skipf("symlinks unavailable on this host: %v", err)}}++func TestHermesStoreHasSessionDBUnreadable(t *testing.T) {+ if runtime.GOOS == "windows" {+ t.Skip("Unix permissions required")+ }+ store := t.TempDir()+ db := filepath.Join(store, "state.db")+ mustWrite(t, db, "transcript")+ if err := os.Chmod(db, 0); err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = os.Chmod(db, 0o600) })+ if f, err := os.Open(db); err == nil {+ f.Close()+ t.Skip("current user can read files despite permissions")+ }+ if hermesStoreHasSessionDB(store) {+ t.Fatal("unreadable session database reported as reachable")+ }+}