Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(hosted-team): an approval never overwrites a team entry someone else wrote (M3)

review822ad70c5059
diff --git a/docs/hosted-team-relay.md b/docs/hosted-team-relay.md
index fe06a8d81..7fdb20b91 100644
--- a/docs/hosted-team-relay.md
+++ b/docs/hosted-team-relay.md
@@ -312,7 +312,8 @@ unapproved after 2.5 s, the tab says "Waiting for an owner of X to approve this
window refuses the device. An owner whose screen still showed a request that closed is told
"Another owner answered this request."
- An approval is pinned only after **both** humans confirmed. A deny or expiry that lands while that
- pin is being written wins: the write is skipped, or taken back.
+ pin is being written wins: the write is skipped, or taken back. A key that gained a team entry
+ meanwhile (a `team add-owner` of the same key) keeps that entry: the approval writes nothing.
- The CLI has **no** approve verb. `team status` lists pending requests by SAS and says to approve
from an owner's desktop. The only way the CLI admits a device is `team add-owner`, which makes it
an owner.
diff --git a/src/core/relay/hosted-service.test.ts b/src/core/relay/hosted-service.test.ts
index d5e279510..045346764 100644
--- a/src/core/relay/hosted-service.test.ts
+++ b/src/core/relay/hosted-service.test.ts
@@ -495,6 +495,31 @@ describe('hosted service — approve and deny', () => {
})
})
+describe('hosted service — an approval never overwrites an entry someone else wrote (M3)', () => {
+ it('a `team add-owner` that lands while the approval waits for its pin write keeps its OWNER role', async () => {
+ const w = world()
+ const owner = await ownerOnline(w)
+ const keys = genKeyPair()
+ const { g, pendingId } = await pendingGuest(w, keys)
+ // The CLI promotes that very key; its write is held, so the approval's pin queues behind it.
+ disk.holdTeamWrite = true
+ const promoted = w.svc.addOwner(pub(keys), 'Racer')
+ await vi.waitFor(() => expect(disk.held).toHaveLength(1))
+ owner.req(5, IPC.relayHostedApprove, [pendingId, 'viewer'])
+ await vi.waitFor(() => expect(g.isApproved()).toBe(true))
+ const writes = disk.done
+ releaseHeldWrites()
+ await promoted
+ await hostOpened(w, keys)
+ // Only the add-owner write landed: the approval found the key already there and wrote nothing.
+ await vi.waitFor(() => expect(disk.done).toBe(writes + 1))
+ const entry = w.teamOnDisk().find((p) => p.pubkeyB64 === pub(keys))
+ expect(entry).toMatchObject({ role: 'owner', label: 'Racer', addedBy: 'cli' })
+ g.req(1, IPC.relayHostedSelf)
+ await vi.waitFor(() => expect(g.res(1)).toMatchObject({ ok: true, result: { role: 'owner' } }))
+ })
+})
+
describe('hosted service — a refusal while the approval’s pin is being written (R26)', () => {
it('a deny that lands while the pin write waits its turn pins nothing', async () => {
const w = world()
diff --git a/src/core/relay/hosted-service.ts b/src/core/relay/hosted-service.ts
index 56857cdf3..e58c4e351 100644
--- a/src/core/relay/hosted-service.ts
+++ b/src/core/relay/hosted-service.ts
@@ -20,7 +20,8 @@
// request per device key, and at most PENDING_MAX at once.
// - An approval is pinned only once both humans confirmed. A deny or an expiry that lands while
// that pin is still being written wins: the write is skipped, or taken back. A peer that merely
-// drops meanwhile keeps its pin — both humans did approve.
+// drops meanwhile keeps its pin — both humans did approve. A key that already has an entry when
+// the pin is written (a racing `team add-owner`) keeps it: the approval writes nothing.
// - The scheduler hears about EVERY session end. The core fires `onClose` only for ends the shell
// did not ask for; every end this service causes (deny, expiry, removal, a listener the
// scheduler closes) runs the same `ended` bookkeeping, at most once per session.
@@ -322,6 +323,10 @@ export function createHostedService(deps: HostedServiceDeps): HostedService {
await team.update((d) => {
// Denied or expired while this write waited its turn: pin nothing ('last-owner' = write nothing).
if (c.refused) return 'last-owner'
+ // The key already has an entry: someone wrote it while this pin waited its turn (a racing
+ // `team add-owner`, say). That is a deliberate act and it stands; replacing it would demote
+ // an owner to the role this dialog picked. The session is served that entry's role.
+ if (peerFor(d, peerKeyB64)) return 'last-owner'
wrote = true
return upsertPeer(d, entry)
})