dsh-device-fleet 0.1.0-dev.2 → 0.1.0-dev.20

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.
@@ -0,0 +1,95 @@
1
+ // Local approval HTTP surface for the approval panel (dev.9). The page is
2
+ // served by the DSH web carrier, so browser fetch carries the session cookie.
3
+ // All decisions go through ApprovalInbox; nothing here bypasses Tool Policy.
4
+ const MAX_BODY_BYTES = 64 * 1024
5
+
6
+ function json(res, status, value) {
7
+ const text = JSON.stringify(value)
8
+ res.statusCode = status
9
+ res.setHeader('content-type', 'application/json; charset=utf-8')
10
+ res.setHeader('cache-control', 'no-store')
11
+ res.end(text)
12
+ }
13
+
14
+ async function readJsonBody(req) {
15
+ const chunks = []
16
+ let size = 0
17
+ for await (const chunk of req) {
18
+ size += chunk.length
19
+ if (size > MAX_BODY_BYTES) throw Object.assign(new Error('request body is too large'), { code: 'body-too-large' })
20
+ chunks.push(chunk)
21
+ }
22
+ if (chunks.length === 0) return {}
23
+ try {
24
+ return JSON.parse(Buffer.concat(chunks).toString('utf8'))
25
+ } catch {
26
+ throw Object.assign(new Error('request body must be JSON'), { code: 'bad-json' })
27
+ }
28
+ }
29
+
30
+ /** HTTP-free decision helper shared by the routes and unit tests. */
31
+ export function createApprovalApi({ inbox }) {
32
+ return {
33
+ list() {
34
+ return { approvals: inbox.list() }
35
+ },
36
+ decide(body = {}) {
37
+ const { id, action } = body
38
+ if (typeof id !== 'string' || id === '') {
39
+ throw Object.assign(new Error('id is required'), { code: 'bad-request' })
40
+ }
41
+ if (action === 'approve') {
42
+ const scope = body.scope === 'ttl' ? 'ttl' : 'once'
43
+ return { ok: true, result: inbox.approve(id, { scope }) }
44
+ }
45
+ if (action === 'deny') {
46
+ return { ok: true, result: inbox.deny(id, typeof body.reason === 'string' ? body.reason : 'denied by local user') }
47
+ }
48
+ throw Object.assign(new Error('action must be approve or deny'), { code: 'bad-request' })
49
+ },
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Register the approval routes on the DSH web carrier.
55
+ * @returns {() => void} disposer
56
+ */
57
+ export function registerApprovalRoutes({ webServer, inbox, logger = null }) {
58
+ if (!webServer?.register || !inbox) return () => {}
59
+ const api = createApprovalApi({ inbox })
60
+ const disposers = []
61
+ disposers.push(
62
+ webServer.register({
63
+ kind: 'exact',
64
+ path: '/device-fleet/approvals',
65
+ handler: async (req, res) => {
66
+ if (req.method !== 'GET') return json(res, 405, { error: 'method not allowed' })
67
+ try {
68
+ json(res, 200, api.list())
69
+ } catch (error) {
70
+ logger?.warn?.(`[device-fleet] approval list failed: ${String(error?.message ?? error)}`)
71
+ json(res, 500, { error: 'internal error' })
72
+ }
73
+ },
74
+ }),
75
+ )
76
+ disposers.push(
77
+ webServer.register({
78
+ kind: 'exact',
79
+ path: '/device-fleet/approvals/decide',
80
+ handler: async (req, res) => {
81
+ if (req.method !== 'POST') return json(res, 405, { error: 'method not allowed' })
82
+ try {
83
+ const body = await readJsonBody(req)
84
+ json(res, 200, api.decide(body))
85
+ } catch (error) {
86
+ const status = ['bad-request', 'bad-json', 'body-too-large', 'unknown-request'].includes(error?.code) ? 400 : 500
87
+ json(res, status, { error: String(error?.message ?? error), code: error?.code ?? 'error' })
88
+ }
89
+ },
90
+ }),
91
+ )
92
+ return () => {
93
+ for (const dispose of disposers) dispose()
94
+ }
95
+ }
@@ -0,0 +1,84 @@
1
+ // Runtime config HTTP surface (dev.15): read effective config, list UI options,
2
+ // and apply a patch live. The UI posts here; no plugin or DSH restart is involved.
3
+ const MAX_BODY_BYTES = 256 * 1024
4
+
5
+ function json(res, status, value) {
6
+ res.statusCode = status
7
+ res.setHeader('content-type', 'application/json; charset=utf-8')
8
+ res.setHeader('cache-control', 'no-store')
9
+ res.end(JSON.stringify(value))
10
+ }
11
+
12
+ async function readJsonBody(req) {
13
+ const chunks = []
14
+ let size = 0
15
+ for await (const chunk of req) {
16
+ size += chunk.length
17
+ if (size > MAX_BODY_BYTES) throw Object.assign(new Error('request body is too large'), { code: 'body-too-large' })
18
+ chunks.push(chunk)
19
+ }
20
+ if (chunks.length === 0) return {}
21
+ try {
22
+ return JSON.parse(Buffer.concat(chunks).toString('utf8'))
23
+ } catch {
24
+ throw Object.assign(new Error('request body must be JSON'), { code: 'bad-json' })
25
+ }
26
+ }
27
+
28
+ /**
29
+ * @param {object} options
30
+ * @param {object} options.webServer ctx.webServer
31
+ * @param {object} options.runtime RuntimeConfig
32
+ * @param {() => Promise<object>|object} [options.getOptions] UI option catalog (presets, shells, paths)
33
+ * @param {(patch: object) => Promise<object>|object} [options.preparePatch] side effects before applying (e.g. mkdir)
34
+ */
35
+ export function registerConfigRoutes({ webServer, runtime, getOptions = null, preparePatch = null, logger = null }) {
36
+ if (!webServer?.register || !runtime) return () => {}
37
+ const disposers = []
38
+ disposers.push(
39
+ webServer.register({
40
+ kind: 'exact',
41
+ path: '/device-fleet/options',
42
+ handler: async (req, res) => {
43
+ if (req.method !== 'GET') return json(res, 405, { error: 'method not allowed' })
44
+ try {
45
+ json(res, 200, (await getOptions?.()) ?? {})
46
+ } catch (error) {
47
+ logger?.warn?.(`[device-fleet] options failed: ${String(error?.message ?? error)}`)
48
+ json(res, 500, { error: 'internal error' })
49
+ }
50
+ },
51
+ }),
52
+ )
53
+ disposers.push(
54
+ webServer.register({
55
+ kind: 'exact',
56
+ path: '/device-fleet/config',
57
+ handler: async (req, res) => {
58
+ if (req.method === 'GET') {
59
+ try {
60
+ json(res, 200, runtime.snapshot())
61
+ } catch (error) {
62
+ json(res, 500, { error: String(error?.message ?? error) })
63
+ }
64
+ return
65
+ }
66
+ if (req.method !== 'POST') return json(res, 405, { error: 'method not allowed' })
67
+ try {
68
+ const body = await readJsonBody(req)
69
+ const patch = body?.patch ?? body
70
+ const prepared = preparePatch ? await preparePatch(patch) : patch
71
+ const config = await runtime.update(prepared ?? patch)
72
+ json(res, 200, { ok: true, config, appliedAt: Date.now() })
73
+ } catch (error) {
74
+ const status = ['bad-request', 'bad-json', 'body-too-large', 'readonly'].includes(error?.code) ? 400 : 500
75
+ if (status === 500) logger?.warn?.(`[device-fleet] config update failed: ${String(error?.message ?? error)}`)
76
+ json(res, status, { error: String(error?.message ?? error), code: error?.code ?? 'error' })
77
+ }
78
+ },
79
+ }),
80
+ )
81
+ return () => {
82
+ for (const dispose of disposers) dispose()
83
+ }
84
+ }
@@ -0,0 +1,127 @@
1
+ // Cross-device delegation (ADR-0015): the caller asks the target device's
2
+ // agent to do a one-shot task and gets the final assistant output back.
3
+ //
4
+ // On the target device this runs through the official SubagentProvider
5
+ // contract with `delivery: 'caller'`, so the child agent's own model turn and
6
+ // tools run on that device, and the `result` promise resolves with the final
7
+ // content blocks. No Room, no shared history: delegation is a one-shot task.
8
+ const DEFAULT_DELEGATE_TIMEOUT_MS = 180_000
9
+ const DEFAULT_PROVIDER = 'spawn'
10
+ const DEFAULT_LABEL = 'fleet delegation'
11
+
12
+ function textOf(result) {
13
+ const blocks = Array.isArray(result?.output) ? result.output : []
14
+ return blocks
15
+ .map((block) => (block?.type === 'text' ? block.text : `[${String(block?.type ?? 'block')}]`))
16
+ .join('\n')
17
+ .trim()
18
+ }
19
+
20
+ function withTimeout(promise, timeoutMs, onTimeout) {
21
+ let timer
22
+ return Promise.race([
23
+ promise.finally(() => clearTimeout(timer)),
24
+ new Promise((_, reject) => {
25
+ timer = setTimeout(() => reject(onTimeout()), timeoutMs)
26
+ timer.unref?.()
27
+ }),
28
+ ])
29
+ }
30
+
31
+ export class DelegationHost {
32
+ /**
33
+ * @param {object} options
34
+ * @param {object} options.subagents ctx.subagents service
35
+ * @param {object} options.slot ExecutionSlot
36
+ * @param {import('./tool-policy.js').ToolPolicy} options.policy
37
+ * @param {(request: object) => Promise<boolean>} [options.requestApproval]
38
+ * @param {string} [options.deviceId]
39
+ * @param {string} [options.defaultCwd]
40
+ * @param {string} [options.defaultAgentPreset]
41
+ * @param {string} [options.provider]
42
+ * @param {number} [options.timeoutMs]
43
+ */
44
+ constructor({
45
+ subagents,
46
+ slot,
47
+ policy,
48
+ requestApproval,
49
+ deviceId,
50
+ defaultCwd,
51
+ defaultAgentPreset,
52
+ provider = DEFAULT_PROVIDER,
53
+ timeoutMs = DEFAULT_DELEGATE_TIMEOUT_MS,
54
+ }) {
55
+ this.subagents = subagents
56
+ this.slot = slot
57
+ this.policy = policy
58
+ this.requestApproval = requestApproval
59
+ this.deviceId = deviceId
60
+ this.defaultCwd = defaultCwd
61
+ this.defaultAgentPreset = defaultAgentPreset
62
+ this.provider = provider
63
+ this.timeoutMs = timeoutMs
64
+ }
65
+
66
+ async delegate({ fromDeviceId, request = {} }) {
67
+ const task = typeof request.task === 'string' ? request.task.trim() : ''
68
+ if (task === '') throw Object.assign(new Error('delegate needs a non-empty task'), { code: 'bad-request' })
69
+
70
+ const decision = this.policy.decide({ capability: 'delegate', args: {} })
71
+ if (decision.decision === 'deny') {
72
+ throw Object.assign(new Error(decision.reason), { code: 'denied', data: { capability: 'delegate' } })
73
+ }
74
+ if (decision.decision === 'ask') {
75
+ const outcome =
76
+ typeof this.requestApproval === 'function'
77
+ ? await this.requestApproval({ fromDeviceId, capability: 'delegate', tool: 'delegate', args: { task }, reason: decision.reason })
78
+ : { approved: false, reason: 'no approval channel' }
79
+ const approved = outcome === true || outcome?.approved === true
80
+ if (!approved) {
81
+ throw Object.assign(new Error(outcome?.reason ?? 'local approval was not granted'), { code: 'approval_denied' })
82
+ }
83
+ this.policy.grant(decision.key, { scope: outcome?.scope === 'ttl' ? 'ttl' : 'once' })
84
+ }
85
+
86
+ const agent = await this.slot.ensureAgent()
87
+ const controller = new AbortController()
88
+ const startedAt = Date.now()
89
+ let activation
90
+ try {
91
+ activation = await this.subagents.startActivation({
92
+ provider: request.provider ?? this.provider,
93
+ label: typeof request.label === 'string' && request.label !== '' ? request.label : DEFAULT_LABEL,
94
+ request: {
95
+ prompt: [{ type: 'text', text: task }],
96
+ parent: agent,
97
+ ...(request.cwd ?? this.defaultCwd ? { cwd: request.cwd ?? this.defaultCwd } : {}),
98
+ ...(request.agentPreset ?? this.defaultAgentPreset
99
+ ? { agentPreset: request.agentPreset ?? this.defaultAgentPreset }
100
+ : {}),
101
+ },
102
+ signal: controller.signal,
103
+ delivery: 'caller',
104
+ })
105
+ const result = await withTimeout(
106
+ activation.result,
107
+ Number.isFinite(request.timeoutMs) ? request.timeoutMs : this.timeoutMs,
108
+ () => {
109
+ controller.abort()
110
+ return Object.assign(new Error('delegation timed out; the child agent was asked to stop'), { code: 'delegate-timeout' })
111
+ },
112
+ )
113
+ return {
114
+ text: textOf(result) || '(delegated agent returned no text)',
115
+ stopReason: result?.stopReason ?? null,
116
+ childId: activation.childId,
117
+ durationMs: Date.now() - startedAt,
118
+ }
119
+ } finally {
120
+ try {
121
+ await activation?.dispose?.()
122
+ } catch {
123
+ /* best effort */
124
+ }
125
+ }
126
+ }
127
+ }
@@ -27,6 +27,7 @@ export class DirectCallHost {
27
27
  * @param {string} [options.dshVersion]
28
28
  * @param {string[]} [options.capabilities]
29
29
  * @param {string} [options.shellTool]
30
+ * @param {object} [options.delegation] DelegationHost for `fleet.delegate`
30
31
  */
31
32
  constructor({
32
33
  slot,
@@ -40,6 +41,7 @@ export class DirectCallHost {
40
41
  capabilities = [],
41
42
  shellTool = DEFAULT_SHELL_TOOL,
42
43
  approvalTimeoutMs = DEFAULT_POLICY_TIMEOUT_MS,
44
+ delegation = null,
43
45
  }) {
44
46
  this.slot = slot
45
47
  this.policy = policy
@@ -52,6 +54,7 @@ export class DirectCallHost {
52
54
  this.capabilities = capabilities
53
55
  this.shellTool = shellTool
54
56
  this.approvalTimeoutMs = approvalTimeoutMs
57
+ this.delegation = delegation
55
58
  }
56
59
 
57
60
  info() {
@@ -75,6 +78,11 @@ export class DirectCallHost {
75
78
  return { deviceId: this.deviceId, tools: await this.slot.listTools(), policy: this.policy.summarize() }
76
79
  case 'fleet.call':
77
80
  return this.handleCall({ fromDeviceId: deviceId, request: params ?? {} })
81
+ case 'fleet.delegate':
82
+ if (!this.delegation) {
83
+ throw Object.assign(new Error('delegation is unavailable on this device'), { code: 'unsupported' })
84
+ }
85
+ return this.delegation.delegate({ fromDeviceId: deviceId, request: params ?? {} })
78
86
  default:
79
87
  throw Object.assign(new Error(`unknown fleet method: ${String(method)}`), { code: 'unknown-method' })
80
88
  }
@@ -89,9 +97,11 @@ export class DirectCallHost {
89
97
  throw Object.assign(new Error(decision.reason), { code: 'denied', data: { capability, tool } })
90
98
  }
91
99
  if (decision.decision === 'ask') {
92
- const approved = await this.askApproval({ fromDeviceId, capability, tool, args, reason: decision.reason })
93
- if (!approved) throw Object.assign(new Error('local approval was not granted'), { code: 'approval_denied' })
94
- this.policy.grant(decision.key, { scope: 'once' })
100
+ const outcome = await this.askApproval({ fromDeviceId, capability, tool, args, reason: decision.reason })
101
+ if (outcome.approved !== true) {
102
+ throw Object.assign(new Error(outcome.reason ?? 'local approval was not granted'), { code: 'approval_denied' })
103
+ }
104
+ this.policy.grant(decision.key, { scope: outcome.scope === 'ttl' ? 'ttl' : 'once' })
95
105
  }
96
106
  const startedAt = Date.now()
97
107
  const result = await this.execute({ capability, tool, args })
@@ -108,12 +118,16 @@ export class DirectCallHost {
108
118
  }
109
119
 
110
120
  async askApproval({ fromDeviceId, capability, tool, args, reason }) {
111
- if (typeof this.requestApproval !== 'function') return false
121
+ if (typeof this.requestApproval !== 'function') return { approved: false, reason: 'no approval channel' }
112
122
  try {
113
- const approved = await this.requestApproval({ fromDeviceId, capability, tool, args, reason })
114
- return approved === true
115
- } catch {
116
- return false
123
+ const outcome = await this.requestApproval({ fromDeviceId, capability, tool, args, reason })
124
+ if (outcome === true) return { approved: true, scope: 'once' }
125
+ if (outcome && typeof outcome === 'object') {
126
+ return { approved: outcome.approved === true, scope: outcome.scope ?? 'once', reason: outcome.reason }
127
+ }
128
+ return { approved: false, reason: 'approval not granted' }
129
+ } catch (error) {
130
+ return { approved: false, reason: String(error?.message ?? error) }
117
131
  }
118
132
  }
119
133
 
@@ -178,6 +192,11 @@ export class DirectCallClient {
178
192
  return this.node.request(deviceId, 'fleet.call', { capability, tool, args }, options)
179
193
  }
180
194
 
195
+ /** Ask the target device's agent to do a one-shot task and return its final text. */
196
+ async delegate(deviceId, request, options = {}) {
197
+ return this.node.request(deviceId, 'fleet.delegate', request, options)
198
+ }
199
+
181
200
  /** Format a remote result for a model-facing text tool. */
182
201
  render(result) {
183
202
  if (result?.isError) return `Error: ${result.text || 'remote tool failed'}`
@@ -18,6 +18,14 @@ export class ExecutionSlot {
18
18
  this.initializing = null
19
19
  }
20
20
 
21
+ /** Replace the slot's creation config; the next call lazily recreates the agent. */
22
+ setConfig(next = {}) {
23
+ this.config = { ...this.config, ...(next ?? {}) }
24
+ this.agent = null
25
+ this.sessionId = null
26
+ this.initializing = null
27
+ }
28
+
21
29
  async ensureAgent() {
22
30
  if (this.agent) return this.agent
23
31
  if (this.initializing) return this.initializing
@@ -55,11 +63,25 @@ export class ExecutionSlot {
55
63
  */
56
64
  async executeTool({ name, args = {}, signal }) {
57
65
  const agent = await this.ensureAgent()
58
- const batch = await this.tools.execute({
59
- calls: [{ id: randomUUID(), name, arguments: JSON.stringify(args ?? {}) }],
60
- agent,
61
- signal,
62
- })
66
+ // execToolBatch requires a real AbortSignal: without one the pipeline
67
+ // throws before dispatch, which surfaces as "Tool batch execution failed".
68
+ const effectiveSignal = signal ?? new AbortController().signal
69
+ let batch
70
+ try {
71
+ batch = await this.tools.execute({
72
+ calls: [{ id: randomUUID(), name, arguments: JSON.stringify(args ?? {}) }],
73
+ agent,
74
+ signal: effectiveSignal,
75
+ })
76
+ } catch (error) {
77
+ const cause = error?.cause
78
+ const detail = cause?.message ?? error?.message ?? String(error)
79
+ throw Object.assign(new Error(`tool ${name} failed: ${String(detail)}`), {
80
+ code: 'tool-failed',
81
+ cause,
82
+ data: { causeCode: cause?.code ?? null, causeMessage: cause?.message ?? null },
83
+ })
84
+ }
63
85
  const first = batch?.results?.[0]?.result
64
86
  if (!first) throw Object.assign(new Error(`tool ${name} returned no result`), { code: 'tool-no-result' })
65
87
  const text = (first.content ?? [])
package/src/host/index.js CHANGED
@@ -3,4 +3,11 @@ export * from './store.js'
3
3
  export * from './tool-policy.js'
4
4
  export * from './execution-slot.js'
5
5
  export * from './direct-call.js'
6
+ export * from './delegation.js'
7
+ export * from './approval-inbox.js'
8
+ export * from './approval-web.js'
9
+ export * from './ui-web.js'
10
+ export * from './config-web.js'
11
+ export * from './runtime-config.js'
12
+ export * from './room-service.js'
6
13
  export * from './tools.js'