magpie icon indicating copy to clipboard operation
magpie copied to clipboard

provider: `this-device` on a signed-in subscription prints success but stores nothing, so sync still replaces it

Open Dunqing opened this issue 2 weeks ago • 1 comments

Title: provider: this-device on a signed-in subscription prints success but stores nothing, so sync still replaces it

Version: magpie v0.1.4533 (Linux, Docker gateway). main at be400389 shows the same code.

Surface: CLI (GUI path affected too).

Setup: two computers syncing through GitHub; one is a server where the Claude subscription is signed in and must stay on, the other has the same provider id switched off.

Steps to reproduce

  1. Sign a subscription in on one computer (here: Claude Code, id claude).
  2. magpie provider this-device claude yes → prints ✓ Claude Code stays on this device: sync doesn't send it out or change it.
  3. Check the record: providers.json has no thisDevice on the claude entry, and the file's size and mtime are unchanged.
  4. The next sync replaces the provider with the other computer's switched-off copy, which turns Claude off on the server.

Expected: the record keeps "thisDevice": true, and provider.Mirror (internal/provider/backup.go:125,139) leaves it alone as it does for a plain provider.

Actual: nothing is written; the CLI reports success.

Root cause: provider.Save routes any id in Accounts() (a signed-in subscription) through accountSettings (internal/provider/provider.go:1125), which builds the stored record and copies ~45 fields (Models, Off, Routing, KeepLogin, …) but never ThisDevice. SetThisDevice (internal/provider/backup.go:272) only sets the field and calls Save, so the flag is dropped on the way to disk. The GUI's toggle (internal/gui/providers.go:3018) calls the same SetThisDevice.

Existing coverage misses it: internal/davsync/this_device_test.go uses a plain provider (ollama), never an id in Accounts().

Patch (one line):

func accountSettings(a, pk Provider) Provider {
	a.PickedFrom, a.Delisted, a.DelistedAt = pk.PickedFrom, pk.Delisted, pk.DelistedAt
	...
	a.PassHeaders = pk.PassHeaders
	a.ThisDevice = pk.ThisDevice // sync neither sends it out nor replaces it, a subscription too
	return a
}

Test sketch: in internal/provider, write a login (writeLogins([]savedLogin{{Agent: "codex", User: "[email protected]", Plan: "pro"}}), as logins_test.go does), Save a picks-only record for codex, SetThisDevice("codex", true), then assert Stored() has ThisDevice set. On the current code it stays false.

Why it matters: this is the mechanism that exists today for exactly the case in #2966 — a subscription signed in on one computer among several that sync. It silently no-ops there, so the only workarounds are turning the providers part off on that computer or patching the file by hand (which the next save of that provider undoes).

Dunqing avatar Nov 19 '26 15:11 Dunqing

provider: keep this-device on a signed-in account through Save

Fixes #7229

Root cause

Save routes any id in Accounts() (a signed-in subscription) through accountSettings (internal/provider/provider.go:1125), which copies the ~45 user-set fields onto the stored record — but never ThisDevice. SetThisDevice (internal/provider/backup.go:272) only sets the field and calls Save, so the flag was dropped on the way to providers.json while the CLI printed success. On the next sync the provider was replaced with the other computer's copy (Mirror, backup.go:125), switching a signed-in subscription off — exactly #2966's setup, today.

Change

One line plus its why-comment: accountSettings now copies a.ThisDevice = pk.ThisDevice like the rest of the picks. The flag lands on disk for an account as it already does for a plain provider, and Mirror's existing ThisDevice handling keeps it home from then on.

Test

New internal/provider/account_this_device_test.go (TestAccountThisDeviceKept), following the existing account-settings test pattern: with a written Codex/Copilot sign-in (signIn), SetThisDevice(id, true) must leave the flag in Stored() — Red before (stayed false for both accounts, reproducing the no-op), Green after — and SetThisDevice(id, false) clears it again, so nothing sticks behind the test's back.

$ go test -tags nogui ./internal/provider/ -run 'TestAccountThisDeviceKept' -count=1
ok  github.com/yetone/magpie/internal/provider  0.520s

$ go test -tags nogui ./internal/provider/ -run 'TestAccount|TestDav|TestThis|TestSaved|TestSync' -count=1
ok  github.com/yetone/magpie/internal/provider  11.344s

$ go test -tags nogui ./internal/davsync/ -count=1
ok  github.com/yetone/magpie/internal/davsync  110.560s

Static: GOOS=linux and GOOS=windows go vet -tags nogui ./internal/provider/ clean; gofmt -l empty; git diff --check clean.

The patch follows the patch sketch in the issue; commit carries Co-authored-by: Dunqing per docs/contributors.md.

Gaoshou101 avatar Nov 20 '26 05:11 Gaoshou101