From e1c4add9fe6345de6f89a62c7d35504d897d582d Mon Sep 17 00:00:00 2001 From: hzhang Date: Mon, 1 Jun 2026 13:45:11 +0100 Subject: [PATCH] fix(runner): write thread id atomically (tmp+rename) Aligns with anthropic-contractor: a crash mid-write leaves a stale .plexum-codex-session.tmp instead of an empty real file that would make the next turn start fresh and lose conversation thread. --- 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 a59ddf4..9c376fd 100644 --- a/internal/runner/runner.go +++ b/internal/runner/runner.go @@ -293,9 +293,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", "codex: save session id failed", + map[string]any{"err": err.Error(), "path": tmp}) + return + } + if err := os.Rename(tmp, path); err != nil { + host.Log("warn", "codex: rename session id failed", map[string]any{"err": err.Error(), "path": path}) } }