@linxin666/dsh-remote-bridge 0.0.0-stage → 0.4.8
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/LICENSE +202 -0
- package/README.i18n.yaml +6 -0
- package/README.md +277 -2
- package/README.zh.md +276 -0
- package/cordis.patch.yml +17 -0
- package/icon.svg +3 -0
- package/lib/client.js +1830 -0
- package/lib/client.js.map +1 -0
- package/lib/index.js +5789 -0
- package/lib/types/client/PairEntry.d.ts +16 -0
- package/lib/types/client/PairPanel.d.ts +55 -0
- package/lib/types/client/icons.d.ts +18 -0
- package/lib/types/client/index.d.ts +35 -0
- package/lib/types/client/locales.d.ts +42 -0
- package/lib/types/client/pair-token.d.ts +73 -0
- package/lib/types/client/semantic.d.ts +28 -0
- package/lib/types/core/approvals.d.ts +99 -0
- package/lib/types/core/connection.d.ts +170 -0
- package/lib/types/core/crypto.d.ts +48 -0
- package/lib/types/core/delta.d.ts +101 -0
- package/lib/types/core/devices.d.ts +72 -0
- package/lib/types/core/dsh-frames.d.ts +332 -0
- package/lib/types/core/envelope.d.ts +132 -0
- package/lib/types/core/extension.d.ts +144 -0
- package/lib/types/core/gateway-errors.d.ts +49 -0
- package/lib/types/core/hub.d.ts +127 -0
- package/lib/types/core/jobs.d.ts +204 -0
- package/lib/types/core/loopback.d.ts +35 -0
- package/lib/types/core/owner-peer.d.ts +49 -0
- package/lib/types/core/pairing.d.ts +61 -0
- package/lib/types/core/protocol.d.ts +319 -0
- package/lib/types/core/questions.d.ts +294 -0
- package/lib/types/core/session-meta.d.ts +105 -0
- package/lib/types/core/session-permissions.d.ts +134 -0
- package/lib/types/core/wire.d.ts +115 -0
- package/lib/types/core/ws-frames.d.ts +72 -0
- package/lib/types/host/dsh-home.d.ts +16 -0
- package/lib/types/host/pair-origin.d.ts +44 -0
- package/lib/types/host/routes.d.ts +27 -0
- package/lib/types/host/websocket.d.ts +111 -0
- package/lib/types/index.d.ts +55 -0
- package/lib/types/mount-once.d.ts +55 -0
- package/locale/en.json +6 -0
- package/locale/ru.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +93 -3
- package/src/client/PairEntry.tsx +151 -0
- package/src/client/PairPanel.tsx +183 -0
- package/src/client/icons.tsx +72 -0
- package/src/client/index.ts +81 -0
- package/src/client/locales.ts +72 -0
- package/src/client/pair-token.ts +104 -0
- package/src/client/panel.module.css +344 -0
- package/src/client/semantic.ts +37 -0
- package/src/core/approvals.ts +173 -0
- package/src/core/connection.ts +1906 -0
- package/src/core/crypto.ts +113 -0
- package/src/core/delta.ts +204 -0
- package/src/core/devices.ts +209 -0
- package/src/core/dsh-frames.ts +843 -0
- package/src/core/envelope.ts +219 -0
- package/src/core/extension.ts +197 -0
- package/src/core/gateway-errors.ts +92 -0
- package/src/core/hub.ts +310 -0
- package/src/core/jobs.ts +372 -0
- package/src/core/loopback.ts +60 -0
- package/src/core/owner-peer.ts +70 -0
- package/src/core/pairing.ts +113 -0
- package/src/core/protocol.ts +381 -0
- package/src/core/questions.ts +630 -0
- package/src/core/session-meta.ts +176 -0
- package/src/core/session-permissions.ts +266 -0
- package/src/core/wire.ts +159 -0
- package/src/core/ws-frames.ts +139 -0
- package/src/css-modules.d.ts +6 -0
- package/src/host/dsh-home.ts +42 -0
- package/src/host/pair-origin.ts +77 -0
- package/src/host/routes.ts +85 -0
- package/src/host/websocket.ts +359 -0
- package/src/index.ts +257 -0
- package/src/mount-once.ts +142 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The gate in front of the bootstrap token endpoint.
|
|
3
|
+
*
|
|
4
|
+
* The socket address alone is NOT a loopback proof on this deployment: a
|
|
5
|
+
* request that arrives over a public tunnel is terminated by the tunnel client
|
|
6
|
+
* on the same machine, so cloudflared connects to the origin from loopback and
|
|
7
|
+
* `req.socket.remoteAddress` reads `127.0.0.1` for a request the whole internet
|
|
8
|
+
* can reach. The reliable discriminator is the `Host` header, which both the
|
|
9
|
+
* market relay and a plain tunnel rewrite to the public hostname before the
|
|
10
|
+
* origin sees the request (`market/worker/src/relay.js:263` sets it to the
|
|
11
|
+
* registered tunnel host). Both conditions must hold, so a forged
|
|
12
|
+
* `remoteAddress` in a test — or a proxy that forgets to rewrite `Host` — still
|
|
13
|
+
* cannot mint a pairing token.
|
|
14
|
+
* @module dsh-remote-bridge/core/loopback
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import type { IncomingMessage } from 'node:http'
|
|
18
|
+
|
|
19
|
+
/** Socket addresses that count as "this machine". */
|
|
20
|
+
export const LOOPBACK_ADDRESSES: ReadonlySet<string> = new Set(['127.0.0.1', '::1', '::ffff:127.0.0.1'])
|
|
21
|
+
|
|
22
|
+
/** Host header values that count as "this machine". */
|
|
23
|
+
export const LOOPBACK_HOSTS: ReadonlySet<string> = new Set(['127.0.0.1', 'localhost', '[::1]'])
|
|
24
|
+
|
|
25
|
+
/** @returns whether a socket's remote address is this machine. */
|
|
26
|
+
export function isLoopbackAddress(remoteAddress: string | undefined): boolean {
|
|
27
|
+
return remoteAddress !== undefined && LOOPBACK_ADDRESSES.has(remoteAddress)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @returns whether a `Host` header names this machine. The port is ignored;
|
|
32
|
+
* anything that is not a loopback literal — including a public tunnel
|
|
33
|
+
* hostname — is refused.
|
|
34
|
+
*/
|
|
35
|
+
export function isLoopbackHost(host: string | undefined): boolean {
|
|
36
|
+
if (host === undefined) return false
|
|
37
|
+
const name = host.trim().toLowerCase()
|
|
38
|
+
if (name === '') return false
|
|
39
|
+
const bare = name.startsWith('[')
|
|
40
|
+
// IPv6 literals carry their port after the closing bracket.
|
|
41
|
+
? name.slice(0, name.indexOf(']') + 1)
|
|
42
|
+
: name.split(':')[0] ?? ''
|
|
43
|
+
return LOOPBACK_HOSTS.has(bare)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Why this request may not reach the bootstrap token endpoint, or undefined
|
|
48
|
+
* when it may.
|
|
49
|
+
* @param req - the incoming request.
|
|
50
|
+
* @returns a one-line reason, or undefined.
|
|
51
|
+
*/
|
|
52
|
+
export function loopbackDenialOf(req: IncomingMessage): string | undefined {
|
|
53
|
+
if (!isLoopbackAddress(req.socket.remoteAddress)) {
|
|
54
|
+
return 'the request did not arrive from this machine'
|
|
55
|
+
}
|
|
56
|
+
if (!isLoopbackHost(req.headers.host)) {
|
|
57
|
+
return 'the request names a Host that is not this machine'
|
|
58
|
+
}
|
|
59
|
+
return undefined
|
|
60
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The in-process owner peer every gateway call of this bridge speaks for.
|
|
3
|
+
*
|
|
4
|
+
* The 0.2.1 gateway made `peer` mandatory on every request, and the
|
|
5
|
+
* authorization layer reads only `peer.principal`: `{ kind: 'owner' }` is how
|
|
6
|
+
* host-local code states that the call IS the local user driving their own
|
|
7
|
+
* Host, so the gateway answers it directly instead of running a remote-access
|
|
8
|
+
* decision (only non-owner principals are judged).
|
|
9
|
+
* @module dsh-remote-bridge/core/owner-peer
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
13
|
+
import type { GatewayPeer } from './wire.ts'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Mint the in-process owner peer this bridge's Host calls speak for.
|
|
17
|
+
*
|
|
18
|
+
* The bridge's security model states that pairing is a full-control
|
|
19
|
+
* credential: a paired device reaches the harness in-process — sessions,
|
|
20
|
+
* prompts, approvals. Protocol v1 is therefore deliberately paired with the
|
|
21
|
+
* one principal that preserves the pre-0.2.1 capability of the bridge's own
|
|
22
|
+
* in-process calls: the owner. That claim is safe only because of WHERE this
|
|
23
|
+
* peer is attached:
|
|
24
|
+
*
|
|
25
|
+
* - Every gateway call a connection can trigger sits behind the bridge's own
|
|
26
|
+
* pairing gate: dispatch answers `unpaired` before any session, approval or
|
|
27
|
+
* roster work, and the three unpaired methods (`hello`, `pair.begin`,
|
|
28
|
+
* `pair.finish`) never invoke the gateway at all. The owner peer crosses to
|
|
29
|
+
* the harness only after a connection proved a device token (or just
|
|
30
|
+
* enrolled one), never for an unpaired one.
|
|
31
|
+
* - No wire field can set a principal: the connection never reads a `peer`
|
|
32
|
+
* or `principal` (or any identity) out of a client frame. The peer is minted
|
|
33
|
+
* here, in-process, fixed at `{ kind: 'owner' }`, and a client that names
|
|
34
|
+
* one in its payload is ignored — the call still speaks for this peer.
|
|
35
|
+
*
|
|
36
|
+
* `interactions` stays `'none'`: the bridge only invokes unary methods and
|
|
37
|
+
* opens the `session/follow` business stream. The gateway's interactions
|
|
38
|
+
* check gates exactly the forwarded-event endpoints (opening an event stream,
|
|
39
|
+
* answering a waterfall), which this caller never touches; an `'answer'`
|
|
40
|
+
* peer would additionally receive forwarded Host events, a capability the
|
|
41
|
+
* bridge neither needs nor wants — approvals reach devices through this
|
|
42
|
+
* plugin's own `approval/request` listener and the protocol's own events, not
|
|
43
|
+
* through a Typert forwarded-event subscription.
|
|
44
|
+
*
|
|
45
|
+
* The peer's signal aborts once `ctx` disposes, so calls and streams opened
|
|
46
|
+
* for it end with the plugin fiber, and `dispose()` is idempotent with racing
|
|
47
|
+
* calls sharing one completion. The id carries no credential — the protocol
|
|
48
|
+
* fixes `principal` as the only identity persisted in a session log — so a
|
|
49
|
+
* fresh unique id per mount is sufficient.
|
|
50
|
+
*/
|
|
51
|
+
export function createOwnerPeer(ctx: Context): GatewayPeer {
|
|
52
|
+
const controller = new AbortController()
|
|
53
|
+
let completion: Promise<void> | undefined
|
|
54
|
+
const peer: GatewayPeer = {
|
|
55
|
+
id: `remote-bridge-owner:${crypto.randomUUID()}` as GatewayPeer['id'],
|
|
56
|
+
ctx,
|
|
57
|
+
principal: { kind: 'owner' },
|
|
58
|
+
signal: controller.signal,
|
|
59
|
+
interactions: 'none',
|
|
60
|
+
dispose() {
|
|
61
|
+
if (completion === undefined) {
|
|
62
|
+
controller.abort()
|
|
63
|
+
completion = Promise.resolve()
|
|
64
|
+
}
|
|
65
|
+
return completion
|
|
66
|
+
},
|
|
67
|
+
}
|
|
68
|
+
ctx.effect(() => () => { void peer.dispose() }, 'remote-bridge owner peer')
|
|
69
|
+
return peer
|
|
70
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-memory bootstrap tokens and pairing nonces.
|
|
3
|
+
*
|
|
4
|
+
* Both are one-shot and short-lived, and neither is ever persisted: a token an
|
|
5
|
+
* operator reads off the Host log is only useful while its 30 s window is open,
|
|
6
|
+
* and a restart invalidating every pending pairing is the desired behavior.
|
|
7
|
+
* @module dsh-remote-bridge/core/pairing
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { randomBase64Url } from './crypto.ts'
|
|
11
|
+
|
|
12
|
+
/** Lifetime of one bootstrap token handed to the operator's phone. */
|
|
13
|
+
export const BOOTSTRAP_TOKEN_TTL_MS = 30_000
|
|
14
|
+
/** Lifetime of one pairing nonce, from `pair.begin` to `pair.finish`. */
|
|
15
|
+
export const PAIR_NONCE_TTL_MS = 60_000
|
|
16
|
+
/** Entropy of one bootstrap token. */
|
|
17
|
+
export const BOOTSTRAP_TOKEN_BYTES = 32
|
|
18
|
+
/** Entropy of one pairing nonce. */
|
|
19
|
+
export const PAIR_NONCE_BYTES = 16
|
|
20
|
+
|
|
21
|
+
/** A minted one-shot secret and the instant it stops being accepted. */
|
|
22
|
+
export interface PairingTicket {
|
|
23
|
+
readonly value: string
|
|
24
|
+
readonly expiresAt: number
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface PairingServiceOptions {
|
|
28
|
+
/** Clock, injectable so expiry is tested without sleeping. */
|
|
29
|
+
readonly now?: () => number
|
|
30
|
+
readonly tokenTtlMs?: number
|
|
31
|
+
readonly nonceTtlMs?: number
|
|
32
|
+
/** Secret source, injectable so a test can pin the value it will present. */
|
|
33
|
+
readonly random?: (byteLength: number) => string
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The two one-shot secrets of the pairing handshake.
|
|
38
|
+
*
|
|
39
|
+
* `issueToken` is called by the loopback-only route an operator reaches from
|
|
40
|
+
* the Host itself; `beginPairing` consumes that token and mints the nonce the
|
|
41
|
+
* phone signs; `consumeNonce` closes the handshake. Consumption happens BEFORE
|
|
42
|
+
* any signature check in the caller, so a wrong signature still burns the
|
|
43
|
+
* nonce: a brute-force loop cannot reuse one challenge.
|
|
44
|
+
*/
|
|
45
|
+
export class PairingService {
|
|
46
|
+
readonly #now: () => number
|
|
47
|
+
readonly #tokenTtlMs: number
|
|
48
|
+
readonly #nonceTtlMs: number
|
|
49
|
+
readonly #random: (byteLength: number) => string
|
|
50
|
+
readonly #tokens = new Map<string, number>()
|
|
51
|
+
readonly #nonces = new Map<string, number>()
|
|
52
|
+
|
|
53
|
+
constructor(options: PairingServiceOptions = {}) {
|
|
54
|
+
this.#now = options.now ?? (() => Date.now())
|
|
55
|
+
this.#tokenTtlMs = options.tokenTtlMs ?? BOOTSTRAP_TOKEN_TTL_MS
|
|
56
|
+
this.#nonceTtlMs = options.nonceTtlMs ?? PAIR_NONCE_TTL_MS
|
|
57
|
+
this.#random = options.random ?? randomBase64Url
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Mint one bootstrap token, valid until its own expiry. */
|
|
61
|
+
issueToken(): PairingTicket {
|
|
62
|
+
this.#prune()
|
|
63
|
+
const value = this.#random(BOOTSTRAP_TOKEN_BYTES)
|
|
64
|
+
const expiresAt = this.#now() + this.#tokenTtlMs
|
|
65
|
+
this.#tokens.set(value, expiresAt)
|
|
66
|
+
return { value, expiresAt }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Consume one bootstrap token.
|
|
71
|
+
* @returns whether the token was live; a consumed or expired token is gone.
|
|
72
|
+
*/
|
|
73
|
+
consumeToken(token: string): boolean {
|
|
74
|
+
this.#prune()
|
|
75
|
+
const expiresAt = this.#tokens.get(token)
|
|
76
|
+
if (expiresAt === undefined) return false
|
|
77
|
+
this.#tokens.delete(token)
|
|
78
|
+
return true
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Mint one pairing nonce for a connection that just consumed a valid token. */
|
|
82
|
+
issueNonce(): PairingTicket {
|
|
83
|
+
this.#prune()
|
|
84
|
+
const value = this.#random(PAIR_NONCE_BYTES)
|
|
85
|
+
const expiresAt = this.#now() + this.#nonceTtlMs
|
|
86
|
+
this.#nonces.set(value, expiresAt)
|
|
87
|
+
return { value, expiresAt }
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Consume one pairing nonce.
|
|
92
|
+
* @returns whether the nonce was live; a consumed or expired nonce is gone.
|
|
93
|
+
*/
|
|
94
|
+
consumeNonce(nonce: string): boolean {
|
|
95
|
+
this.#prune()
|
|
96
|
+
const expiresAt = this.#nonces.get(nonce)
|
|
97
|
+
if (expiresAt === undefined) return false
|
|
98
|
+
this.#nonces.delete(nonce)
|
|
99
|
+
return true
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** @returns how many tokens and nonces are currently acceptable, for tests and diagnostics. */
|
|
103
|
+
get pending(): { tokens: number; nonces: number } {
|
|
104
|
+
this.#prune()
|
|
105
|
+
return { tokens: this.#tokens.size, nonces: this.#nonces.size }
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
#prune(): void {
|
|
109
|
+
const now = this.#now()
|
|
110
|
+
for (const [value, expiresAt] of this.#tokens) if (expiresAt <= now) this.#tokens.delete(value)
|
|
111
|
+
for (const [value, expiresAt] of this.#nonces) if (expiresAt <= now) this.#nonces.delete(value)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The frozen protocol surface: method names, topic names, refusal codes, and
|
|
3
|
+
* the one authorization rule the dispatcher enforces before any method runs.
|
|
4
|
+
* @module dsh-remote-bridge/core/protocol
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** Client-callable methods of protocol v1. */
|
|
8
|
+
export const METHOD_HELLO = 'hello'
|
|
9
|
+
export const METHOD_SESSION_LIST = 'session.list'
|
|
10
|
+
export const METHOD_SESSION_TITLES = 'session.titles'
|
|
11
|
+
export const METHOD_SESSION_SNAPSHOT = 'session.snapshot'
|
|
12
|
+
export const METHOD_SESSION_HISTORY = 'session.history'
|
|
13
|
+
export const METHOD_SESSION_HISTORY_DEEP = 'session.history.deep'
|
|
14
|
+
export const METHOD_SESSION_PROMPT = 'session.prompt'
|
|
15
|
+
export const METHOD_SESSION_INTERRUPT = 'session.interrupt'
|
|
16
|
+
export const METHOD_SESSION_ATTACHMENT = 'session.attachment'
|
|
17
|
+
export const METHOD_SESSION_PERMISSIONS = 'session.permissions'
|
|
18
|
+
export const METHOD_SESSION_SET_PERMISSION = 'session.setPermission'
|
|
19
|
+
export const METHOD_SESSION_MODELS = 'session.models'
|
|
20
|
+
export const METHOD_SESSION_SET_MODEL = 'session.setModel'
|
|
21
|
+
export const METHOD_SESSION_CREATE = 'session.create'
|
|
22
|
+
export const METHOD_SESSION_FORK = 'session.fork'
|
|
23
|
+
export const METHOD_PRESETS_LIST = 'presets.list'
|
|
24
|
+
export const METHOD_PAIR_BEGIN = 'pair.begin'
|
|
25
|
+
export const METHOD_PAIR_FINISH = 'pair.finish'
|
|
26
|
+
export const METHOD_APPROVAL_RESOLVE = 'approval.resolve'
|
|
27
|
+
export const METHOD_QUESTION_ANSWER = 'question.answer'
|
|
28
|
+
export const METHOD_JOBS_LIST = 'jobs.list'
|
|
29
|
+
|
|
30
|
+
/** Server-pushed topics of protocol v1. */
|
|
31
|
+
export const TOPIC_SESSION_DELTA = 'session.delta'
|
|
32
|
+
export const TOPIC_APPROVAL_REQUEST = 'approval.request'
|
|
33
|
+
export const TOPIC_QUESTION_REQUEST = 'question.request'
|
|
34
|
+
export const TOPIC_JOBS_CHANGED = 'jobs.changed'
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Capability a client declares in `hello`'s `cap` array to ask for a trimmed
|
|
38
|
+
* `session.snapshot` baseline. A client that does not declare it — including
|
|
39
|
+
* every client written before this capability existed — is served exactly the
|
|
40
|
+
* baseline protocol v1 always served.
|
|
41
|
+
*/
|
|
42
|
+
export const CAP_SESSION_SNAPSHOT_TRIM = 'session.snapshot.trim.v1'
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Capability a client declares in `hello`'s `cap` array to have
|
|
46
|
+
* `session.snapshot` answer with one bounded page instead of the whole opening
|
|
47
|
+
* window. Declaring it is what makes an opening screen cost one page rather
|
|
48
|
+
* than a whole session; a client that does not declare it — including every
|
|
49
|
+
* client written before this capability existed — receives byte for byte the
|
|
50
|
+
* snapshot protocol v1 always served.
|
|
51
|
+
*/
|
|
52
|
+
export const CAP_SESSION_SNAPSHOT_PAGE = 'session.snapshot.page.v1'
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Capability a client declares in `hello`'s `cap` array to have
|
|
56
|
+
* `session.snapshot` answer a reconnect with only what it missed.
|
|
57
|
+
*
|
|
58
|
+
* The declaration and the request's `sinceSeq` are what it takes together: a
|
|
59
|
+
* snapshot that carries no `sinceSeq` is answered, byte for byte, the body
|
|
60
|
+
* protocol v1 always served, whether or not the client declared this. Behind
|
|
61
|
+
* both, the client names the durable `lastSeq` of a snapshot it already holds
|
|
62
|
+
* and the answer is either that same reply in an incremental form —
|
|
63
|
+
* `{turns, lastSeq, mode:"since", reset:false}` carrying only the entries above
|
|
64
|
+
* that watermark — or a full baseline with `mode:"full"` and `reset:true`,
|
|
65
|
+
* which is the one answer that tells a client to throw its copy away. The
|
|
66
|
+
* estimate is only served when the bridge can PROVE nothing was lost: the
|
|
67
|
+
* current follow window's oldest entry must sit at or below `sinceSeq + 1`, so a
|
|
68
|
+
* window that has already scrolled past the watermark answers the baseline
|
|
69
|
+
* instead of a delta with a hole in it. It exists because a reconnect on the
|
|
70
|
+
* phone re-paid 0.86-1.06 s of baseline five times in one measured run.
|
|
71
|
+
*/
|
|
72
|
+
export const CAP_SESSION_SNAPSHOT_SINCE = 'session.snapshot.since.v1'
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Capability a client declares in `hello`'s `cap` array to be shown the
|
|
76
|
+
* host's pending user questions and to answer them with `question.answer`.
|
|
77
|
+
*
|
|
78
|
+
* Declaring it is what makes `question.request` reachable and what makes the
|
|
79
|
+
* method part of that client's protocol at all: a client that does not declare
|
|
80
|
+
* it — including every client written before this channel existed — receives no
|
|
81
|
+
* such event, and its `question.answer` is refused as an unknown method,
|
|
82
|
+
* exactly as that frame was refused before.
|
|
83
|
+
*/
|
|
84
|
+
export const CAP_QUESTIONS = 'questions.v1'
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Capability a client declares in `hello`'s `cap` array to be handed the
|
|
88
|
+
* approval questions this bridge is already holding for it.
|
|
89
|
+
*
|
|
90
|
+
* Unlike every channel capability beside it, this one gates a second delivery
|
|
91
|
+
* rather than the channel: `approval.request` still reaches every paired
|
|
92
|
+
* connection the moment a tool asks, exactly as it did before this name
|
|
93
|
+
* existed, because gating the first broadcast would take approvals away from
|
|
94
|
+
* every client written before the capability. What the declaration buys is the
|
|
95
|
+
* replay — after a reconnect, or on a session screen opened late, the client is
|
|
96
|
+
* handed the questions that are still waiting instead of a card it never saw.
|
|
97
|
+
* Only a live claim is replayed: a question that was answered, that the bridge
|
|
98
|
+
* timed out, or that was withdrawn with the last phone is gone from the claim
|
|
99
|
+
* registry before the replay reads it.
|
|
100
|
+
*
|
|
101
|
+
* It is deliberately absent from {@link SERVED_CAPABILITIES}: that list is what
|
|
102
|
+
* `hello` advertises as served, advertising a capability is a minor-version
|
|
103
|
+
* addition (see {@link MINOR}), and this channel changes nothing a client can
|
|
104
|
+
* observe in the handshake — no answer, no field and no method behind it. The
|
|
105
|
+
* declaration is the whole switch, and the name reaches a client through this
|
|
106
|
+
* bridge's own protocol documentation.
|
|
107
|
+
*/
|
|
108
|
+
export const CAP_APPROVALS = 'approvals.v1'
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Capability a client declares in `hello`'s `cap` array to be told where a
|
|
112
|
+
* message came from and that an agent spawned a child session.
|
|
113
|
+
*
|
|
114
|
+
* Declaring it is what makes the `subagent` delta part of that client's
|
|
115
|
+
* protocol: a client that does not declare it — including every client written
|
|
116
|
+
* before this channel existed — receives no such event, exactly as that frame
|
|
117
|
+
* was dropped before. The `actor` of a `user-message` is not gated: it is an
|
|
118
|
+
* additive field a client that does not read it ignores.
|
|
119
|
+
*/
|
|
120
|
+
export const CAP_SUBAGENT_THREAD = 'subagent.thread.v1'
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Capability a client declares in `hello`'s `cap` array to see the host's
|
|
124
|
+
* background jobs and to be told when the set changes.
|
|
125
|
+
*
|
|
126
|
+
* Declaring it is what makes `jobs.list` reachable, and `jobs.list` is the only
|
|
127
|
+
* thing that opens a roster: a connection that declares this capability but
|
|
128
|
+
* never asks for a scope receives no `jobs.changed` at all. A client that does
|
|
129
|
+
* not declare it — including every client written before this channel existed —
|
|
130
|
+
* receives no such event, and its `jobs.list` is refused as an unknown method,
|
|
131
|
+
* exactly as that frame was refused before.
|
|
132
|
+
*/
|
|
133
|
+
export const CAP_JOBS = 'jobs.v1'
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Capability a client declares in `hello`'s `cap` array to read the host's
|
|
137
|
+
* Agent presets and to instantiate one as a session.
|
|
138
|
+
*
|
|
139
|
+
* Declaring it is what makes `presets.list` and `session.create` part of that
|
|
140
|
+
* client's protocol at all: a client that does not declare it — including every
|
|
141
|
+
* client written before this channel existed — is refused both as unknown
|
|
142
|
+
* methods, exactly as those frames were refused before. Neither method is
|
|
143
|
+
* called on a connection's behalf either, so a client that never asks sends the
|
|
144
|
+
* host no request.
|
|
145
|
+
*/
|
|
146
|
+
export const CAP_AGENTS = 'agents.v1'
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Capability a client declares in `hello`'s `cap` array to be answered with
|
|
150
|
+
* byte-level increments instead of the whole roster or the whole board.
|
|
151
|
+
*
|
|
152
|
+
* Behind it, `session.list` and `board.snapshot` (the latter through the board
|
|
153
|
+
* plugin, §15) accept an opaque `since` token and answer either the whole set
|
|
154
|
+
* (`mode:"reset"`) or only what changed since the set that token names. A
|
|
155
|
+
* client that does not declare it — including every client written before this
|
|
156
|
+
* channel existed — is answered today's body plus the ignorable `mode` and
|
|
157
|
+
* `token` keys, with `mode` always `"reset"`.
|
|
158
|
+
*/
|
|
159
|
+
export const CAP_DELTA = 'delta.v1'
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Capability a client declares in `hello`'s `cap` array to be told that an
|
|
163
|
+
* archived session is not in the roster at all.
|
|
164
|
+
*
|
|
165
|
+
* Unlike every capability beside it this one negotiates nothing: the bridge
|
|
166
|
+
* never transfers an archived session, whether or not a client declares it, so
|
|
167
|
+
* an older client simply receives the shorter roster it would have hidden
|
|
168
|
+
* anyway. The name is an announcement of that fact, so a client can stop
|
|
169
|
+
* filtering for itself: behind it, no `session.list` row is a session the
|
|
170
|
+
* host's archive set names, a row that does survive still carries
|
|
171
|
+
* `archived: false` under the same schema, and a live child of an archived
|
|
172
|
+
* parent still crosses with its `parentSessionId` — the parent id, not the
|
|
173
|
+
* parent row.
|
|
174
|
+
*/
|
|
175
|
+
export const CAP_ROSTER_OMITS_ARCHIVED = 'session.list.omit-archived.v1'
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Capability a client declares in `hello`'s `cap` array to be answered a
|
|
179
|
+
* compact roster it can page.
|
|
180
|
+
*
|
|
181
|
+
* The declaration alone changes nothing: a listing that carries none of the
|
|
182
|
+
* compact parameters (`cwd`, `maxRows`, `after`) is answered exactly the body
|
|
183
|
+
* protocol v1 always served, so a client can opt into the name before it
|
|
184
|
+
* relies on any of it. Behind the parameters, a row drops its absolute `cwd`
|
|
185
|
+
* unless the request asks for it, drops its `updatedAt` when this connection
|
|
186
|
+
* already sent that id the same value, and a `maxRows`/`after` pair cuts the
|
|
187
|
+
* roster into pages ordered most recent first. The roster is the most repeated
|
|
188
|
+
* large reply on the link, so this is what lets a reconnect pay for the newest
|
|
189
|
+
* screenful plus a cursor instead of the whole list again.
|
|
190
|
+
*/
|
|
191
|
+
export const CAP_ROSTER_COMPACT = 'session.list.compact.v1'
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Capability a client declares in `hello`'s `cap` array to be told that this
|
|
195
|
+
* bridge can page a session's durable history down to its oldest entry.
|
|
196
|
+
*
|
|
197
|
+
* The capability is an announcement and nothing more. `session.history.deep` is
|
|
198
|
+
* open to every paired connection, exactly as `session.history` is, because it
|
|
199
|
+
* is a pure read of the session store that costs no other client anything;
|
|
200
|
+
* declaring the capability is how a client learns the method exists, and a
|
|
201
|
+
* client that does not declare it loses only that knowledge.
|
|
202
|
+
*/
|
|
203
|
+
export const CAP_SESSION_DEEP_HISTORY = 'session.deep-history.v1'
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Capability a client declares in `hello`'s `cap` array to fork a session.
|
|
207
|
+
*
|
|
208
|
+
* Declaring it is what makes `session.fork` part of that client's protocol at
|
|
209
|
+
* all: a client that does not declare it — including every client written
|
|
210
|
+
* before this channel existed — is refused `unknown-method`, and no fork is
|
|
211
|
+
* asked of the host on its behalf. Behind the name the bridge forwards one
|
|
212
|
+
* `SessionForkRequest` to the host's `session/fork` and hands back the host's
|
|
213
|
+
* own `SessionForkValue`, so what a fork point means stays the host's: an
|
|
214
|
+
* `atSeq` is the source event seq the fork includes, an omitted one is the
|
|
215
|
+
* newest finished turn, and a cut the host opens itself is closed by the host's
|
|
216
|
+
* own synthetic finishing event. Nothing about a fork is modelled or
|
|
217
|
+
* re-derived here.
|
|
218
|
+
*/
|
|
219
|
+
export const CAP_SESSION_FORK = 'session.fork.v1'
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Capability a client declares in `hello`'s `cap` array to be sent frames
|
|
223
|
+
* compressed instead of as plain text.
|
|
224
|
+
*
|
|
225
|
+
* Declaring it is what allows the host to compress at all: a frame whose JSON
|
|
226
|
+
* text is at least 1024 UTF-8 bytes crosses as one WebSocket binary frame
|
|
227
|
+
* carrying raw DEFLATE (RFC 1951) of exactly that text, and a frame that does
|
|
228
|
+
* not get smaller still crosses as the text frame it always was. A client that
|
|
229
|
+
* does not declare it — including every client written before this capability
|
|
230
|
+
* existed — receives byte for byte the text frames protocol v1 always wrote.
|
|
231
|
+
*
|
|
232
|
+
* The direction is one way: a client still sends text frames only, and the
|
|
233
|
+
* 10 MiB frame cap stays measured on the text, before inflation.
|
|
234
|
+
*/
|
|
235
|
+
export const CAP_FRAME_DEFLATE = 'frame.deflate.v1'
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Methods an unpaired connection may still call: the handshake itself and the
|
|
239
|
+
* two pairing steps. Everything else answers `unpaired` — a caller can pair
|
|
240
|
+
* without a device token, but cannot touch a session without one.
|
|
241
|
+
*/
|
|
242
|
+
export const UNPAIRED_METHODS: ReadonlySet<string> = new Set([
|
|
243
|
+
METHOD_HELLO,
|
|
244
|
+
METHOD_PAIR_BEGIN,
|
|
245
|
+
METHOD_PAIR_FINISH,
|
|
246
|
+
])
|
|
247
|
+
|
|
248
|
+
/** Every method this protocol version serves. */
|
|
249
|
+
export const KNOWN_METHODS: ReadonlySet<string> = new Set([
|
|
250
|
+
METHOD_HELLO,
|
|
251
|
+
METHOD_SESSION_LIST,
|
|
252
|
+
METHOD_SESSION_TITLES,
|
|
253
|
+
METHOD_SESSION_SNAPSHOT,
|
|
254
|
+
METHOD_SESSION_HISTORY,
|
|
255
|
+
METHOD_SESSION_HISTORY_DEEP,
|
|
256
|
+
METHOD_SESSION_PROMPT,
|
|
257
|
+
METHOD_SESSION_INTERRUPT,
|
|
258
|
+
METHOD_SESSION_ATTACHMENT,
|
|
259
|
+
METHOD_SESSION_PERMISSIONS,
|
|
260
|
+
METHOD_SESSION_SET_PERMISSION,
|
|
261
|
+
METHOD_SESSION_MODELS,
|
|
262
|
+
METHOD_SESSION_SET_MODEL,
|
|
263
|
+
METHOD_PAIR_BEGIN,
|
|
264
|
+
METHOD_PAIR_FINISH,
|
|
265
|
+
METHOD_APPROVAL_RESOLVE,
|
|
266
|
+
METHOD_QUESTION_ANSWER,
|
|
267
|
+
METHOD_JOBS_LIST,
|
|
268
|
+
METHOD_PRESETS_LIST,
|
|
269
|
+
METHOD_SESSION_CREATE,
|
|
270
|
+
METHOD_SESSION_FORK,
|
|
271
|
+
])
|
|
272
|
+
|
|
273
|
+
/** Refusal codes the bridge itself raises (DSH failures cross as their own code). */
|
|
274
|
+
export const CODE_UNPAIRED = 'unpaired'
|
|
275
|
+
export const CODE_UNKNOWN_METHOD = 'unknown-method'
|
|
276
|
+
export const CODE_INVALID_ARGS = 'invalid-args'
|
|
277
|
+
export const CODE_PROTO_UNSUPPORTED = 'proto-unsupported'
|
|
278
|
+
export const CODE_TOKEN_INVALID = 'token-invalid'
|
|
279
|
+
export const CODE_NONCE_INVALID = 'nonce-invalid'
|
|
280
|
+
export const CODE_SIGNATURE_INVALID = 'signature-invalid'
|
|
281
|
+
export const CODE_APPROVAL_UNKNOWN = 'approval-unknown'
|
|
282
|
+
/**
|
|
283
|
+
* The bridge does not hold the named question: it never claimed that call id, it
|
|
284
|
+
* already answered it, or the host no longer holds it as answerable. Host
|
|
285
|
+
* failures crossing this method keep their own codes instead.
|
|
286
|
+
*/
|
|
287
|
+
export const CODE_QUESTION_UNKNOWN = 'question-unknown'
|
|
288
|
+
/**
|
|
289
|
+
* The named session is a delegated child whose durable parent is not a session
|
|
290
|
+
* this connection may read. The bridge follows an addressable child and refuses
|
|
291
|
+
* an unaddressable one, rather than reading it blind.
|
|
292
|
+
*/
|
|
293
|
+
export const CODE_SESSION_NOT_FOLLOWABLE = 'session-not-followable'
|
|
294
|
+
export const CODE_SNAPSHOT_UNAVAILABLE = 'snapshot-unavailable'
|
|
295
|
+
/**
|
|
296
|
+
* This host composes no job registry, so it has no background jobs to publish.
|
|
297
|
+
* The capability stays advertised — it is a fact about this bridge, not about
|
|
298
|
+
* what a particular host mounts — and only a call that needs the registry fails.
|
|
299
|
+
*/
|
|
300
|
+
export const CODE_JOBS_UNAVAILABLE = 'jobs-unavailable'
|
|
301
|
+
export const CODE_SNAPSHOT_TOO_LARGE = 'snapshot-too-large'
|
|
302
|
+
export const CODE_INTERNAL = 'internal'
|
|
303
|
+
|
|
304
|
+
/** Protocol version this package speaks. */
|
|
305
|
+
export const PROTO = 1
|
|
306
|
+
/**
|
|
307
|
+
* Minor version this package speaks.
|
|
308
|
+
*
|
|
309
|
+
* 11 when the fork channel shipped: behind {@link CAP_SESSION_FORK} a client may
|
|
310
|
+
* call {@link METHOD_SESSION_FORK} and receive the host's own fork answer, so a
|
|
311
|
+
* phone branches a session that inherits the context of the one it forked. This
|
|
312
|
+
* is the second of two appends cut from 9 — the snapshot-`since` channel took
|
|
313
|
+
* 10 on the integration branch first — so the number steps over the one already
|
|
314
|
+
* on `dev`: the minor counts additions to this protocol, and a later landing
|
|
315
|
+
* steps over the earlier one instead of reusing its number.
|
|
316
|
+
* 10 when the snapshot became incremental on request: behind
|
|
317
|
+
* {@link CAP_SESSION_SNAPSHOT_SINCE} a `session.snapshot` that names `sinceSeq`
|
|
318
|
+
* is answered either the entries above that watermark or a full baseline
|
|
319
|
+
* carrying `mode` and `reset`, so a client reads a new reply shape off the
|
|
320
|
+
* wire and the number rises with it.
|
|
321
|
+
* 9 when the compact roster shipped: behind {@link CAP_ROSTER_COMPACT} a
|
|
322
|
+
* `session.list` that names one of the compact parameters is answered a
|
|
323
|
+
* shorter row and may be cut into pages, so a client reads a new row schema
|
|
324
|
+
* off the wire and the number rises with it.
|
|
325
|
+
* 8 when the outbound frame form became a capability: behind
|
|
326
|
+
* {@link CAP_FRAME_DEFLATE} a declaring client may be answered in binary
|
|
327
|
+
* frames carrying raw DEFLATE of the text, so the minor is raised for a change
|
|
328
|
+
* a client reads off the wire rather than for a name alone.
|
|
329
|
+
* 7 when the two channels met: the agent channel and the durable history
|
|
330
|
+
* channel each raised 5 to 6 on its own branch, so the protocol that carries
|
|
331
|
+
* both is 7. 6 when the agent channel shipped: behind {@link CAP_AGENTS}
|
|
332
|
+
* `presets.list` forwards the host's Agent-preset roster and `session.create`
|
|
333
|
+
* hands the host one creation request, both verbatim. 6 was also the durable
|
|
334
|
+
* history channel on its own: behind {@link CAP_SESSION_DEEP_HISTORY} a client
|
|
335
|
+
* may call {@link METHOD_SESSION_HISTORY_DEEP} and walk a session's store back
|
|
336
|
+
* to its oldest entry, below the follow window `session.history` slices. 5 when
|
|
337
|
+
* the roster stopped carrying archived sessions: behind
|
|
338
|
+
* {@link CAP_ROSTER_OMITS_ARCHIVED} `session.list` names no session the host's
|
|
339
|
+
* archive set holds, so a session archived between two listings arrives as a
|
|
340
|
+
* removal and one restored as a change. 4 when byte-level increments shipped:
|
|
341
|
+
* behind {@link CAP_DELTA} `session.list` and `board.snapshot` answer a reset
|
|
342
|
+
* or a delta against a `since` token. 3 was
|
|
343
|
+
* the background-job channel — behind {@link CAP_JOBS} a client may call
|
|
344
|
+
* `jobs.list` and receive `jobs.changed`. 2 was the subagent thread —
|
|
345
|
+
* a `user-message` delta may carry an `actor` and a `subagent` delta exists
|
|
346
|
+
* behind {@link CAP_SUBAGENT_THREAD}. 1 was the extension seam — a mounted
|
|
347
|
+
* plugin may answer methods outside {@link KNOWN_METHODS} once the client
|
|
348
|
+
* declares the capability that plugin requires. No addition changed the major
|
|
349
|
+
* version, a frozen field, or an existing method or topic name.
|
|
350
|
+
*/
|
|
351
|
+
export const MINOR = 11
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* The capabilities this bridge itself serves, as `hello` advertises them.
|
|
355
|
+
*
|
|
356
|
+
* A capability a mounted plugin adds is not listed here: the bridge cannot know
|
|
357
|
+
* what it is not the author of. A client learns such a capability from the
|
|
358
|
+
* plugin's own documentation, exactly as it did before this list existed.
|
|
359
|
+
*/
|
|
360
|
+
export const SERVED_CAPABILITIES: readonly string[] = [
|
|
361
|
+
CAP_SESSION_SNAPSHOT_TRIM,
|
|
362
|
+
CAP_SESSION_SNAPSHOT_PAGE,
|
|
363
|
+
CAP_QUESTIONS,
|
|
364
|
+
CAP_SUBAGENT_THREAD,
|
|
365
|
+
CAP_JOBS,
|
|
366
|
+
CAP_AGENTS,
|
|
367
|
+
CAP_DELTA,
|
|
368
|
+
CAP_ROSTER_OMITS_ARCHIVED,
|
|
369
|
+
CAP_SESSION_DEEP_HISTORY,
|
|
370
|
+
CAP_FRAME_DEFLATE,
|
|
371
|
+
CAP_ROSTER_COMPACT,
|
|
372
|
+
CAP_SESSION_SNAPSHOT_SINCE,
|
|
373
|
+
CAP_SESSION_FORK,
|
|
374
|
+
]
|
|
375
|
+
/** `server` field of the hello response. */
|
|
376
|
+
export const SERVER_ID = 'dsh-remote-bridge'
|
|
377
|
+
|
|
378
|
+
/** Exact route path of the bridge's WebSocket endpoint. */
|
|
379
|
+
export const WS_PATH = '/ios/v1'
|
|
380
|
+
/** Exact route path of the loopback-only bootstrap token endpoint. */
|
|
381
|
+
export const PAIR_TOKEN_PATH = '/remote-bridge/pair/token'
|