fix(pairing): harden re-pair per adversarial review

Adversarial review of the diff found four issues, now fixed:
- Validate the requested grant BEFORE the supersede revoke: a reducing
  re-pair with a bad scope/rate no longer destroys the live session and
  then fails to mint a replacement (assertValidTokenOptions runs up front).
- A re-pair with no live session releases tabs orphaned by an expired
  incarnation, closing the tab-inheritance gap /pair had (DELETE /token
  already released unconditionally).
- Test the DELETE /token revoked=0/tabs>0 path and the /pair orphaned-tab
  release at the handler level (HTTP e2e can't, headless owns no tabs).
- Test the CLI --client root fast-fail; fix its null-guard (parseFlag
  returns null when --client is absent).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Garry Tan
2026-08-21 15:15:13 -07:00
committed by Garry Tan
co-authored by Claude Fable 5
parent 6fe3e67736
commit d86edf1f8f
7 changed files with 85 additions and 5 deletions
+18
View File
@@ -373,6 +373,24 @@ describe('pair-agent flow end-to-end (HTTP only, no ngrok)', () => {
expect((await statusWith(s)).status).not.toBe(401); // still working
});
test('a reducing re-pair with an INVALID scope 400s and leaves the live session intact', async () => {
// Regression: the supersede revoke must run AFTER validation. A scope typo
// (--restrict red) on a narrowing re-pair must not destroy the session and
// then fail to mint a replacement — the agent would be knocked offline.
const { setup_key: k } = await pairAs({ clientId: 'validate-me' });
const { body: c } = await connectKey(k);
const s = c.token as string;
expect((await statusWith(s)).status).not.toBe(401);
const resp = await fetch(`${daemon.baseUrl}/pair`, {
method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${daemon.token}` },
body: JSON.stringify({ clientId: 'validate-me', scopes: ['red'] }),
});
expect(resp.status).toBe(400);
expect((await resp.json() as any).error).toContain('red');
// The working session survives the validation error (not revoked).
expect((await statusWith(s)).status).not.toBe(401);
});
// ─── D3: DELETE /token releases tabs unconditionally; 404 only when empty ─
test('DELETE /token returns tabs_released and 404 only when nothing to revoke or release', async () => {