thinkpool-pair 0.7.164 → 0.7.165
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bridge.mjs +89 -15
- package/package.json +2 -1
- package/switch-provider.mjs +57 -0
package/bridge.mjs
CHANGED
|
@@ -43,6 +43,7 @@ import { reapTerminalRow as _reapTerminalRow } from './reap-terminal.mjs'
|
|
|
43
43
|
// registered custom provider, or null for the built-in/unknown (leave the default env intact).
|
|
44
44
|
// Multi-provider BYOK slice 1: a lane spawned with a `provider` id runs on that endpoint.
|
|
45
45
|
import { resolveProviderEnv, providerNameMap, publicKeyB64, announceProviders, listProviders, addProvider, removeProvider, unseal } from './providers.mjs'
|
|
46
|
+
import { validateProviderSwitch, BUILTIN_PROVIDER } from './switch-provider.mjs'
|
|
46
47
|
import { createSdkMcpServer, tool } from '@anthropic-ai/claude-agent-sdk'
|
|
47
48
|
import { z } from 'zod'
|
|
48
49
|
import { startClaudeSession } from './claude-session.mjs'
|
|
@@ -597,6 +598,24 @@ async function refreshOwnerPlan() {
|
|
|
597
598
|
function reapTerminalRow(id) {
|
|
598
599
|
return _reapTerminalRow({ id, token: codeAuthToken, supabaseUrl: SUPABASE_URL, anonKey: SUPABASE_ANON, shuttingDown })
|
|
599
600
|
}
|
|
601
|
+
// Auth: is the sender a PARTICIPANT of THIS room (owner OR granted OR joined)? Room mode
|
|
602
|
+
// serves the room, so RLS returns participants; we confirm the jwt's user is one of them.
|
|
603
|
+
// Pattern (per #218/#219): getUser + uid → REST existence check on code_sessions
|
|
604
|
+
// (code=room, participants cs.{uid}) — NOT byte comparison of the jwt against codeAuthToken
|
|
605
|
+
// (that bug already happened once tonight: a freshly-refreshed jwt has a new signature/iat/jti
|
|
606
|
+
// per the grant cycle, so a byte compare rejects the legitimate sender). Hoisted to module
|
|
607
|
+
// scope so provider-switch + providers-list-req share the ONE implementation. Returns false
|
|
608
|
+
// for anon / missing / invalid jwt without throwing. Fails closed on any fetch error.
|
|
609
|
+
const isRoomParticipant = async (jwt) => {
|
|
610
|
+
if (!jwt || typeof jwt !== 'string') return false
|
|
611
|
+
try {
|
|
612
|
+
const { data } = await supabase.auth.getUser(jwt)
|
|
613
|
+
if (!data?.user) return false
|
|
614
|
+
const uid = data.user.id
|
|
615
|
+
const r = await fetch(`${SUPABASE_URL}/rest/v1/code_sessions?code=eq.${encodeURIComponent(room)}&participants=cs.${uid}`, { headers: { apikey: SUPABASE_ANON, Authorization: `Bearer ${codeAuthToken}` } })
|
|
616
|
+
return r.ok
|
|
617
|
+
} catch { return false }
|
|
618
|
+
}
|
|
600
619
|
// ── Room-serve owner consent (Contract C-CODE-3) ──────────────────────────────
|
|
601
620
|
// A room is served by its OWNER's bridge; a non-owner may serve only with the
|
|
602
621
|
// owner's explicit grant (code_sessions.serve_grant_uid). Enforced HERE — before the
|
|
@@ -2248,6 +2267,38 @@ function acceptEditsPending(s) {
|
|
|
2248
2267
|
}
|
|
2249
2268
|
}
|
|
2250
2269
|
|
|
2270
|
+
// Respawn a LIVE structured lane under a DIFFERENT LLM provider, keeping its
|
|
2271
|
+
// terminal id + visible transcript/history. Provider env is process-level, so a
|
|
2272
|
+
// switch = end the SDK session + reopen under the SAME id with the new provider's
|
|
2273
|
+
// env (resolveProviderEnv spreads into startClaudeSession's child env). The lane is
|
|
2274
|
+
// NOT closing — this mirrors how a BRIDGE RESTART preserves the row (#205/#208
|
|
2275
|
+
// durability): NO `term-exit` broadcast, NO reapTerminalRow, NO deleteSession. The
|
|
2276
|
+
// agent's conversational memory resets (cross-backend, no SDK resume across
|
|
2277
|
+
// providers) — accepted UX, the UI warns; the visible transcript persists via the
|
|
2278
|
+
// restored `log`. A no-op switch (same effective provider) is rejected by the
|
|
2279
|
+
// caller's validateProviderSwitch, so this fn always changes the env.
|
|
2280
|
+
//
|
|
2281
|
+
// Returns true if the lane was live + respawned; false if the id had no live
|
|
2282
|
+
// structured session (caller replies ok:false "no such live lane").
|
|
2283
|
+
function respawnStructured(id, provider) {
|
|
2284
|
+
const s = sessions.get(id)
|
|
2285
|
+
if (!s) return false
|
|
2286
|
+
// Capture the lane's identity for re-open (NOT resume — fresh SDK session).
|
|
2287
|
+
const { model, log, commands, mode, spawnedBy, flowSessionId, flowTaskKey, cwd, rolePrompt, reviewSliceRoots, openedAt } = s
|
|
2288
|
+
// Teardown WITHOUT the close side effects. drainPending denies any in-flight
|
|
2289
|
+
// permission cards (the switch aborts the current turn, same as /model mid-turn).
|
|
2290
|
+
drainPending(s)
|
|
2291
|
+
try { s.session?.end() } catch { /* noop */ }
|
|
2292
|
+
try { s.mockupWatcher?.close() } catch { /* noop */ }
|
|
2293
|
+
sessions.delete(id) // openStructured early-returns if the id is still mapped
|
|
2294
|
+
// Re-open under the SAME id with the new provider env. openStructured persists
|
|
2295
|
+
// sessionData() (provider included) synchronously on open, so a bridge restart
|
|
2296
|
+
// restores the lane on its CURRENT provider, not the original — and its next
|
|
2297
|
+
// announce carries the new provider badge (additive {id,name} projection).
|
|
2298
|
+
openStructured({ id, model, provider, log, commands, mode, spawnedBy, flowSessionId, flowTaskKey, cwd, rolePrompt, reviewSliceRoots, openedAt })
|
|
2299
|
+
return true
|
|
2300
|
+
}
|
|
2301
|
+
|
|
2251
2302
|
function endStructured(id) {
|
|
2252
2303
|
if (!id) return
|
|
2253
2304
|
const s = sessions.get(id)
|
|
@@ -2680,25 +2731,48 @@ channel
|
|
|
2680
2731
|
channel.send({ type: 'broadcast', event: 'provider-remove-res', payload: { nonce, ok: r.ok, error: r.ok ? undefined : r.error } })
|
|
2681
2732
|
})
|
|
2682
2733
|
// providers-list-req {nonce, jwt} → the masked list (keyHint = last 4 chars only).
|
|
2734
|
+
// Auth via the shared isRoomParticipant (hoisted; same pattern as provider-switch).
|
|
2683
2735
|
.on('broadcast', { event: 'providers-list-req' }, async ({ payload }) => {
|
|
2684
2736
|
const nonce = payload?.nonce
|
|
2685
|
-
|
|
2686
|
-
if (!jwt || typeof jwt !== 'string') return false
|
|
2687
|
-
try {
|
|
2688
|
-
const { data } = await supabase.auth.getUser(jwt)
|
|
2689
|
-
if (!data?.user) return false
|
|
2690
|
-
const uid = data.user.id
|
|
2691
|
-
// Verify the user is a participant of THIS room (owner OR granted OR joined).
|
|
2692
|
-
// Room mode serves the room, so RLS returns participants; we just need to confirm
|
|
2693
|
-
// the jwt's user is one of them. A simple existence check on code_sessions
|
|
2694
|
-
// with the room code + the user's id in the participants array is enough.
|
|
2695
|
-
const r = await fetch(`${SUPABASE_URL}/rest/v1/code_sessions?code=eq.${encodeURIComponent(room)}&participants=cs.${uid}`, { headers: { apikey: SUPABASE_ANON, Authorization: `Bearer ${codeAuthToken}` } })
|
|
2696
|
-
return r.ok
|
|
2697
|
-
} catch { return false }
|
|
2698
|
-
}
|
|
2699
|
-
if (!(await isParticipant(payload?.jwt))) return channel.send({ type: 'broadcast', event: 'providers-list-res', payload: { nonce, ok: false, error: 'unauthorized', providers: [] } })
|
|
2737
|
+
if (!(await isRoomParticipant(payload?.jwt))) return channel.send({ type: 'broadcast', event: 'providers-list-res', payload: { nonce, ok: false, error: 'unauthorized', providers: [] } })
|
|
2700
2738
|
channel.send({ type: 'broadcast', event: 'providers-list-res', payload: { nonce, ok: true, providers: listProviders() } })
|
|
2701
2739
|
})
|
|
2740
|
+
// ── Per-lane provider SWITCH (multi-BYOK slice 2). Switch a LIVE structured lane to a
|
|
2741
|
+
// different LLM provider by restarting that lane's agent process under the SAME terminal
|
|
2742
|
+
// id with the new provider's env. Visible transcript persists; the agent's conversational
|
|
2743
|
+
// memory resets (cross-backend, no SDK resume) — accepted UX the UI warns about. Same-
|
|
2744
|
+
// provider model changes keep using the EXISTING /model path (in-process setModel); the UI
|
|
2745
|
+
// only sends provider-switch when the provider ACTUALLY changes.
|
|
2746
|
+
// provider-switch {term, provider, nonce, jwt} → bridge validates + respawns
|
|
2747
|
+
// provider-switch-res {nonce, ok, error?} ← reply (NEVER key material)
|
|
2748
|
+
// Auth: participant check via supabase.auth.getUser (isRoomParticipant) — NOT byte
|
|
2749
|
+
// comparison of the jwt (a refreshed jwt has a new signature/iat/jti). Anon refused.
|
|
2750
|
+
// After the respawn the lane's next announce reflects its new provider (additive {id,name}
|
|
2751
|
+
// projection the client already renders from #219). Spec: docs/specs/2026-07-05-provider-switch.md.
|
|
2752
|
+
.on('broadcast', { event: 'provider-switch' }, async ({ payload }) => {
|
|
2753
|
+
const nonce = payload?.nonce
|
|
2754
|
+
const reply = (ok, error) => channel.send({ type: 'broadcast', event: 'provider-switch-res', payload: { nonce, ok, ...(error ? { error } : {}) } })
|
|
2755
|
+
// AUTH first — anon / non-participant / bad jwt → ok:false 'unauthorized'. No lane
|
|
2756
|
+
// state is touched for a rejected sender (fail closed before any lookup).
|
|
2757
|
+
if (!(await isRoomParticipant(payload?.jwt))) return reply(false, 'unauthorized')
|
|
2758
|
+
const term = payload?.term
|
|
2759
|
+
const s = term ? sessions.get(term) : null
|
|
2760
|
+
// The target must be a LIVE structured session this bridge serves.
|
|
2761
|
+
if (!s) return reply(false, 'no such live lane')
|
|
2762
|
+
// VALIDATE — pure: missing / unknown provider, or a no-op same-provider switch.
|
|
2763
|
+
const v = validateProviderSwitch({
|
|
2764
|
+
provider: payload?.provider,
|
|
2765
|
+
currentProvider: s.provider,
|
|
2766
|
+
registeredIds: listProviders().map((p) => p.id),
|
|
2767
|
+
})
|
|
2768
|
+
if (!v.ok) return reply(false, v.error)
|
|
2769
|
+
if (v.noop) return reply(true) // nothing to do; idempotent ok (UI guarantees a change)
|
|
2770
|
+
const target = payload?.provider === BUILTIN_PROVIDER ? null : payload?.provider
|
|
2771
|
+
const done = respawnStructured(term, target)
|
|
2772
|
+
if (!done) return reply(false, 'no such live lane') // raced closed between the lookup + respawn
|
|
2773
|
+
process.stderr.write(`\n ◆ lane ${String(term).slice(0, 8)} switched provider → ${target || 'anthropic'} (memory reset, transcript kept).\n`)
|
|
2774
|
+
reply(true)
|
|
2775
|
+
})
|
|
2702
2776
|
.subscribe(status => {
|
|
2703
2777
|
if (status === 'SUBSCRIBED') {
|
|
2704
2778
|
realtimeHealthy = true; brokenSince = 0
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "thinkpool-pair",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.165",
|
|
4
4
|
"description": "Share a local coding-agent CLI (Claude Code, Codex, Gemini, Aider, …) into a ThinkPool Code room, live.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
"serve-dir.mjs",
|
|
34
34
|
"serve-consent.mjs",
|
|
35
35
|
"reap-terminal.mjs",
|
|
36
|
+
"switch-provider.mjs",
|
|
36
37
|
"service.mjs",
|
|
37
38
|
"presence.mjs",
|
|
38
39
|
"account.mjs",
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/* ─────────────────────────────────────────────────────────────
|
|
2
|
+
switch-provider.mjs — validation for the per-lane provider-switch
|
|
3
|
+
wire request (`provider-switch` on the room channel).
|
|
4
|
+
|
|
5
|
+
The bridge handler (bridge.mjs) owns AUTH (supabase.getUser + room
|
|
6
|
+
participation) and the actual RESPAWN (teardown-without-reap +
|
|
7
|
+
reopen under the same terminal id with the new provider env). This
|
|
8
|
+
module owns the decidable part — pure, no bridge state, no supabase —
|
|
9
|
+
so the validation branches are unit-testable in isolation:
|
|
10
|
+
|
|
11
|
+
V1 missing provider id → ok:false error:'missing provider'
|
|
12
|
+
V2 unknown id (not registered) → ok:false error:'unknown provider'
|
|
13
|
+
V3 valid + actually changes → ok:true noop:false
|
|
14
|
+
V4 valid but same as current → ok:true noop:true (UI guarantees a
|
|
15
|
+
change, but we stay correct + idempotent
|
|
16
|
+
if it ever sends a no-op)
|
|
17
|
+
|
|
18
|
+
The built-in Anthropic path is internally `provider: null` (omitted on
|
|
19
|
+
the announce), but the wire sends the literal `'anthropic'`. Both mean
|
|
20
|
+
the default Claude env, so `null` ↔ `'anthropic'` is a SAME-provider
|
|
21
|
+
(no-op) switch. resolveProviderEnv (providers.mjs) treats them the same.
|
|
22
|
+
|
|
23
|
+
Spec: docs/specs/2026-07-05-provider-switch.md (wire contract LOCKED;
|
|
24
|
+
this module must NOT change the request/reply field names or shapes).
|
|
25
|
+
───────────────────────────────────────────────────────────── */
|
|
26
|
+
|
|
27
|
+
export const BUILTIN_PROVIDER = 'anthropic'
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Normalize a provider id so the built-in default (`null`, undefined, or the
|
|
31
|
+
* literal 'anthropic') collapses to a single canonical value. Used by the
|
|
32
|
+
* no-op check: switching a lane whose current provider is null (the default
|
|
33
|
+
* path) to 'anthropic' is NOT a real change.
|
|
34
|
+
* @param {string|null|undefined} p
|
|
35
|
+
* @returns {string}
|
|
36
|
+
*/
|
|
37
|
+
export function effectiveProvider(p) {
|
|
38
|
+
return (!p || p === BUILTIN_PROVIDER) ? BUILTIN_PROVIDER : String(p)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Validate a provider-switch request's provider field.
|
|
43
|
+
*
|
|
44
|
+
* @param {{provider: string, currentProvider?: string|null, registeredIds?: string[]}} input
|
|
45
|
+
* @returns {{ok: true, noop: boolean} | {ok: false, error: string}}
|
|
46
|
+
*/
|
|
47
|
+
export function validateProviderSwitch({ provider, currentProvider, registeredIds }) {
|
|
48
|
+
const id = String(provider ?? '').trim()
|
|
49
|
+
if (!id) return { ok: false, error: 'missing provider' }
|
|
50
|
+
|
|
51
|
+
const known = id === BUILTIN_PROVIDER ||
|
|
52
|
+
(Array.isArray(registeredIds) && registeredIds.includes(id))
|
|
53
|
+
if (!known) return { ok: false, error: 'unknown provider' }
|
|
54
|
+
|
|
55
|
+
const noop = effectiveProvider(id) === effectiveProvider(currentProvider)
|
|
56
|
+
return { ok: true, noop }
|
|
57
|
+
}
|