sessions: a gateway-sessions-hidden.json that doesn't parse is kept aside before the next delete or Restore writes over it (#1722, ggbdpq)

readHidden read a file that didn't parse as no deletions, and the next
DeleteGateway or Restore wrote that empty list (plus the new entry)
over it, or removed it, so every earlier deletion record was gone for
good. writeHidden now copies such a file to
gateway-sessions-hidden.json.bad-<time> first, as stash.json's and
logins.json's unread files are kept, and doesn't write when the copy
can't be made.

ggbdpq's patch (1ec0def6, marked wip and unverified on its branch).
Reviewer's changes: "doesn't parse" is decided by unmarshalling into
the list (json.Valid let an object through to be written over), and a
copy that fails stops the write instead of only logging, as d40fee80
doesn't move what it couldn't back up. TestUnreadHiddenObjectKeptAside
covers the object case.

Run (reviewer): TestUnreadHiddenKeptAside fails on the old
gateway_delete.go ("written over, not kept aside");
TestUnreadHiddenObjectKeptAside fails on the old code and on the
original patch; both pass -race -count=3. go test ./internal/sessions
ok (279s).
This commit is contained in:
ggbdpq
2026-10-14 04:59:09 +08:00
committed by yetone
parent 1a7aa76b4b
commit 94e3b3f529
2 changed files with 99 additions and 0 deletions
+27
View File
@@ -3,6 +3,8 @@ package sessions
import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"strings"
@@ -49,6 +51,9 @@ func readHidden() []hiddenGateway {
}
func writeHidden(list []hiddenGateway) error {
if err := keepUnreadHidden(); err != nil {
return err
}
if len(list) == 0 {
if err := os.Remove(gatewayHiddenPath()); err != nil && !errors.Is(err, os.ErrNotExist) {
return err
@@ -69,6 +74,28 @@ func writeHidden(list []hiddenGateway) error {
return os.Rename(tmp, gatewayHiddenPath())
}
// keepUnreadHidden copies a gateway-sessions-hidden.json that doesn't parse
// aside before it is written over (or taken away): it holds the deletions
// magpie made before, and a read that came back empty is unknown, not
// nothing. A copy that can't be made stops the write.
func keepUnreadHidden() error {
p := gatewayHiddenPath()
b, err := os.ReadFile(p)
if err != nil || len(b) == 0 {
return nil
}
var list []hiddenGateway
if json.Unmarshal(b, &list) == nil {
return nil
}
bad := p + ".bad-" + time.Now().Format("20060102-150405.000000000")
if err := os.WriteFile(bad, b, 0o600); err != nil {
return fmt.Errorf("gateway-sessions-hidden.json doesn't parse and couldn't be kept aside: %w", err)
}
log.Printf("gateway-sessions-hidden.json didn't parse; it is kept as %s", filepath.Base(bad))
return nil
}
// GatewayHidden says whether a gateway session was deleted: true when it
// was, and had no request after its last one then.
func GatewayHidden() func(agent, id string, last time.Time) bool {
@@ -0,0 +1,72 @@
package sessions
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// A gateway-sessions-hidden.json that doesn't parse holds deletions magpie
// made before, not nothing: the next DeleteGateway or Restore keeps it aside
// (as stash.json's and logins.json's unread files are kept) instead of
// writing the list over, so what it held can still be got back.
func TestUnreadHiddenKeptAside(t *testing.T) {
gatewayHome(t)
last := time.Now().Add(-2 * time.Hour)
if _, err := DeleteGateway("claude", "gone", "gone", last); err != nil {
t.Fatal(err)
}
p := gatewayHiddenPath()
was, err := os.ReadFile(p)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(was), `"gone"`) {
t.Fatalf("the hidden file lost the first deletion: %s", was)
}
// half a write, as an older magpie's cut-short one would leave
bad := was[:len(was)/2]
if err := os.WriteFile(p, bad, 0o600); err != nil {
t.Fatal(err)
}
if _, err := DeleteGateway("claude", "kept", "kept", last); err != nil {
t.Fatal(err)
}
matches, _ := filepath.Glob(p + ".bad-*")
if len(matches) == 0 {
t.Fatal("the unread hidden file was written over, not kept aside")
}
kept, err := os.ReadFile(matches[0])
if err != nil {
t.Fatal(err)
}
if string(kept) != string(bad) {
t.Fatalf("the file kept aside isn't the one that was there: %s", kept)
}
}
// JSON that isn't a list (an object, as a hand edit or another program might
// leave) is no more read than half a write, so it is kept aside too.
func TestUnreadHiddenObjectKeptAside(t *testing.T) {
gatewayHome(t)
p := gatewayHiddenPath()
if err := os.MkdirAll(filepath.Dir(p), 0o700); err != nil {
t.Fatal(err)
}
obj := []byte(`{"agent":"claude","id":"gone"}`)
if err := os.WriteFile(p, obj, 0o600); err != nil {
t.Fatal(err)
}
if _, err := DeleteGateway("claude", "kept", "kept", time.Now().Add(-2*time.Hour)); err != nil {
t.Fatal(err)
}
matches, _ := filepath.Glob(p + ".bad-*")
if len(matches) != 1 {
t.Fatalf("kept aside %d copies of the unread hidden file, want 1", len(matches))
}
if kept, _ := os.ReadFile(matches[0]); string(kept) != string(obj) {
t.Fatalf("the file kept aside isn't the one that was there: %s", kept)
}
}