From fab4e70c8481e7ec12cb5e6bf2a73220d4fba07b Mon Sep 17 00:00:00 2001 From: hzhang Date: Mon, 1 Jun 2026 13:45:07 +0100 Subject: [PATCH] fix(runner): write session id atomically (tmp+rename) Aligns with anthropic-contractor: a crash mid-write leaves a stale .plexum-gemini-session.tmp instead of an empty real file that would make the next turn start fresh and lose context. --- internal/runner/runner.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/internal/runner/runner.go b/internal/runner/runner.go index 4f97df9..ddff1c5 100644 --- a/internal/runner/runner.go +++ b/internal/runner/runner.go @@ -210,9 +210,18 @@ func loadSessionID(workspace string) string { } func saveSessionID(workspace, id string, host plugin.HostAPI) { + if id == "" { + return + } path := filepath.Join(workspace, SessionFile) - if err := os.WriteFile(path, []byte(id), 0o600); err != nil { + tmp := path + ".tmp" + if err := os.WriteFile(tmp, []byte(id), 0o600); err != nil { host.Log("warn", "gemini: save session id failed", + map[string]any{"err": err.Error(), "path": tmp}) + return + } + if err := os.Rename(tmp, path); err != nil { + host.Log("warn", "gemini: rename session id failed", map[string]any{"err": err.Error(), "path": path}) } }