Skip to content

Commit fb75719

Browse files
committed
feat: implement attachment upload functionality with chunked transfer and recovery
- Added support for attachment uploads through the Brain workspace, allowing files to be uploaded in chunks. - Introduced new RPC methods: `attachments.begin`, `attachments.chunk`, and `attachments.finish` for managing uploads. - Implemented validation for attachment size and chunk sequence to ensure integrity during uploads. - Enhanced error handling for attachment uploads, including digest verification and cleanup on failure. - Updated tests to cover new attachment upload features and ensure proper functionality. - Modified the UI to support attachment uploads and display relevant messages.
1 parent 1d6e665 commit fb75719

14 files changed

Lines changed: 457 additions & 68 deletions

‎README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
> Run, supervise, and review multiple AI coding agents across git worktrees without losing control of your repo.
88
99
[![License: Apache 2.0](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](./LICENSE)
10+
[![Website](https://img.shields.io/badge/Website-crewcode.logixhub.icu-0b7285.svg)](https://crewcode.logixhub.icu)
11+
[![X](https://img.shields.io/badge/X-@OnPointTools-000000.svg)](https://x.com/OnPointTools)
12+
[![YouTube](https://img.shields.io/badge/YouTube-@CjWisdom-FF0000.svg)](https://www.youtube.com/@CjWisdom)
1013

1114
</div>
1215

‎docs/security-model.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ per user and 1,000 events / 1 MiB of detached evidence buffered per resource;
110110
interrupted RPCs are never replayed.
111111
Execution custody is still process-resident rather than crash durable: Brain process,
112112
VPS, revocation, or persistent Brain-to-Hub relay loss can stop execution without a
113-
complete remote halt journal. Attachment tunneling is also not implemented.
113+
complete remote halt journal. Attachments are tunneled as ordered chunks inside the
114+
E2EE relay: Hub receives ciphertext only, while Brain enforces `workspace:write`, a
115+
25 MiB file limit, registered-root and symlink containment, strict sequence and
116+
size bounds, SHA-256 integrity, active-upload limits, and temporary-file cleanup.
114117

115118
## Hop 1 — untrusted content -> agent
116119

‎docs/web-remote-access.md‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,11 +401,16 @@ discovery, including the existing SSH routing. Network filesystem RPC also
401401
rejects roots absent from the server workspace store, preventing a browser from
402402
substituting `/` or another arbitrary host path. `workspaceStore.ts` and `fs.ts`
403403
are now Electron transport adapters for those operations. Native folder pickers,
404-
attachment handling, formatting, and destructive filesystem mutations remain in
405-
the Electron adapter until their browser API and validation contracts are added.
406-
407-
Hub-relayed attachment tunneling is not implemented. The browser can list the
408-
Brain-owned MCP registry and select entries by opaque id; `bridge.start` resolves
404+
formatting, and destructive filesystem mutations remain in the Electron adapter
405+
until their browser API and validation contracts are added.
406+
407+
Hub-relayed attachment tunneling uses ordered 256 KiB chunks inside the existing
408+
browser-to-Brain encrypted RPC tunnel. The Hub sees only bounded ciphertext frames.
409+
Brain requires `workspace:write`, restricts destinations to registered workspace
410+
roots, rejects symlink escapes and files over 25 MiB, verifies a final SHA-256
411+
digest, and removes canceled, failed, idle, or shutdown-temporary uploads. The
412+
browser can list the Brain-owned MCP registry and select entries by opaque id;
413+
`bridge.start` resolves
409414
those ids server-side and never accepts executable MCP command or environment
410415
definitions from the browser.
411416

‎src/main/hub-brain-relay.ts‎

Lines changed: 73 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export interface RunningBrainRelay {
4949
export function brainScopeForMethod(method: string): BrainAccessScope | null {
5050
if (READ_METHODS.has(method)) return 'workspace:read'
5151
if (method.startsWith('pty.')) return 'terminal'
52+
if (method.startsWith('attachments.')) return 'workspace:write'
5253
if (AGENT_METHOD_PREFIXES.some(prefix => method.startsWith(prefix))) return 'agent'
5354
if (WRITE_METHOD_PREFIXES.some(prefix => method.startsWith(prefix))) return 'workspace:write'
5455
return null
@@ -71,6 +72,13 @@ function latestAssistantMessageIndex(messages: Array<{ role: string; content: st
7172
return -1
7273
}
7374

75+
function precedingUserText(messages: Array<{ role: string; content: string }>, before: number): string | undefined {
76+
for (let index = before - 1; index >= 0; index -= 1) {
77+
if (messages[index]!.role === 'user') return messages[index]!.content
78+
}
79+
return undefined
80+
}
81+
7482
function websocketOrigin(origin: string): string {
7583
const url = new URL(origin)
7684
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:'
@@ -101,7 +109,11 @@ export async function startBrainRelay(options: BrainRelayOptions): Promise<Runni
101109
const eventSocket = new WebSocket(backend.url.replace(/^http/, 'ws') + '/api/v1/events', ['crewcode.v1', backendToken])
102110
await new Promise<void>((resolve, reject) => { eventSocket.once('open', resolve); eventSocket.once('error', reject) })
103111

104-
const relay = new WebSocket(websocketOrigin(options.credential.hubOrigin), ['crewcode.brain.v1', options.credential.token])
112+
// The Brain-local execution backend outlives every Hub/browser transport.
113+
// `relay` is replaced on transient Hub disconnects without recreating the
114+
// backend or stopping provider processes.
115+
let relay!: WebSocket
116+
let reconnectTimer: ReturnType<typeof setTimeout> | null = null
105117
const sessions = new Map<string, RelaySession>()
106118
type ResourceOwner = {
107119
userId: string
@@ -224,7 +236,7 @@ export async function startBrainRelay(options: BrainRelayOptions): Promise<Runni
224236
else appendDetachedEvent(resourceId, { channel: event.channel, event: event.event })
225237
})
226238

227-
relay.on('message', async raw => {
239+
const handleRelayMessage = async (raw: WebSocket.RawData): Promise<void> => {
228240
let frame: HubRelayControlFrame
229241
try { frame = JSON.parse(raw.toString()) as HubRelayControlFrame } catch { relay.close(4002, 'invalid Hub relay frame'); return }
230242
if (frame.type === 'brainReady') { relayReadyResolve(); return }
@@ -286,13 +298,17 @@ export async function startBrainRelay(options: BrainRelayOptions): Promise<Runni
286298
const existingOwner = ownerMap && resourceId ? ownerMap.get(resourceId) : undefined
287299
const ownedResourceCount = [...paneOwners.values(), ...bridgeOwners.values()].filter(owner => owner.userId === session.userId).length
288300
const exceedsResourceLimit = createsResource && !existingOwner && ownedResourceCount >= MAX_OWNED_RESOURCES_PER_USER
289-
const canAttach = existingOwner?.userId === session.userId
290-
&& (existingOwner.connectionId === null || existingOwner.connectionId === session.connectionId)
291-
const wrongOwner = !!existingOwner && !canAttach
292-
const missingOwner = !!ownerMap && !!resourceId && !createsResource && existingOwner?.connectionId !== session.connectionId
301+
// Execution authority belongs to the authenticated Hub user on this Brain,
302+
// not to one ephemeral browser websocket. connectionId is event-routing
303+
// custody only: a same-owner command from a replacement page atomically
304+
// attaches the resource to that page. Otherwise closing a tab turns a
305+
// transport lifecycle into an execution lifecycle and strands live agents.
306+
const ownsResource = existingOwner?.userId === session.userId
307+
const wrongOwner = !!existingOwner && !ownsResource
308+
const missingOwner = !!ownerMap && !!resourceId && !createsResource && !existingOwner
293309
const requestResourceId = responseRequestId ? requestOwners.get(responseRequestId) : undefined
294310
const requestOwner = requestResourceId ? bridgeOwners.get(requestResourceId) : undefined
295-
const wrongRequestOwner = !!responseRequestId && requestOwner?.connectionId !== session.connectionId
311+
const wrongRequestOwner = !!responseRequestId && requestOwner?.userId !== session.userId
296312
let response: CrewCodeRemoteResponse
297313
let replayResourceId = ''
298314
if (!scope || !session.grantedScopes.has(scope)) {
@@ -327,7 +343,12 @@ export async function startBrainRelay(options: BrainRelayOptions): Promise<Runni
327343
// conversation shard. Agent scope is still required above.
328344
const history = loadConversation(`web:${conversationScopeKey}`)
329345
const latestAssistantIndex = latestAssistantMessageIndex(history)
330-
response = { protocolVersion: CREWCODE_REMOTE_PROTOCOL_VERSION, id: request.id, ok: true, result: { replayed: latestAssistantIndex !== -1 } }
346+
const latestAssistant = latestAssistantIndex === -1 ? null : {
347+
index: latestAssistantIndex,
348+
text: history[latestAssistantIndex]!.content,
349+
userText: precedingUserText(history, latestAssistantIndex),
350+
}
351+
response = { protocolVersion: CREWCODE_REMOTE_PROTOCOL_VERSION, id: request.id, ok: true, result: { replayed: latestAssistant !== null, latestAssistant } }
331352
sendEncrypted(session, { type: 'rpcResult', response })
332353
if (latestAssistantIndex !== -1) {
333354
sendEncrypted(session, {
@@ -345,7 +366,12 @@ export async function startBrainRelay(options: BrainRelayOptions): Promise<Runni
345366
}
346367
const history = loadConversation(`web:${owner.conversationScopeKey}`)
347368
const latestAssistantIndex = latestAssistantMessageIndex(history)
348-
response = { protocolVersion: CREWCODE_REMOTE_PROTOCOL_VERSION, id: request.id, ok: true, result: { replayed: latestAssistantIndex !== -1 } }
369+
const latestAssistant = latestAssistantIndex === -1 ? null : {
370+
index: latestAssistantIndex,
371+
text: history[latestAssistantIndex]!.content,
372+
userText: precedingUserText(history, latestAssistantIndex),
373+
}
374+
response = { protocolVersion: CREWCODE_REMOTE_PROTOCOL_VERSION, id: request.id, ok: true, result: { replayed: latestAssistant !== null, latestAssistant } }
349375
sendEncrypted(session, { type: 'rpcResult', response })
350376
if (latestAssistantIndex !== -1) {
351377
sendEncrypted(session, {
@@ -394,7 +420,7 @@ export async function startBrainRelay(options: BrainRelayOptions): Promise<Runni
394420
return
395421
} else if (exceedsResourceLimit) {
396422
response = deniedResponse(request, `Brain resource limit of ${MAX_OWNED_RESOURCES_PER_USER} terminals and agents reached`)
397-
} else if (request.method === 'bridge.start' && params.freshSession !== true && existingOwner?.userId === session.userId) {
423+
} else if (request.method === 'bridge.start' && params.freshSession !== true && ownsResource) {
398424
// Stable browser bridge ids make start an idempotent same-owner attach as
399425
// well as a create operation. This is also the recovery fallback when a
400426
// restored page misses its eager bridge.claim (for example, because the
@@ -406,8 +432,13 @@ export async function startBrainRelay(options: BrainRelayOptions): Promise<Runni
406432
replayResourceId = resourceId
407433
response = { protocolVersion: CREWCODE_REMOTE_PROTOCOL_VERSION, id: request.id, ok: true, result: { ok: true, attached: true } }
408434
} else if (wrongOwner || missingOwner || wrongRequestOwner) {
409-
response = deniedResponse(request, 'Brain session does not own this terminal or agent resource')
435+
response = deniedResponse(request, 'Authenticated Hub user does not own this Brain resource')
410436
} else {
437+
// Any operation by the durable owner attaches event routing to the
438+
// replacement browser before backend work can emit. The prior connection
439+
// may still be open during a refresh; it no longer owns execution.
440+
if (existingOwner && ownsResource) existingOwner.connectionId = session.connectionId
441+
if (requestOwner?.userId === session.userId) requestOwner.connectionId = session.connectionId
411442
// Reserve caller-chosen resource ids before invoking the backend. PTY and
412443
// agent implementations may emit their first event before create/start
413444
// resolves; claiming afterward drops that event and leaves the UI stuck.
@@ -466,22 +497,45 @@ export async function startBrainRelay(options: BrainRelayOptions): Promise<Runni
466497
for (const event of detachedEvents.get(replayResourceId) ?? []) sendEncrypted(session, { type: 'event', channel: event.channel, event: event.event })
467498
detachedEvents.delete(replayResourceId)
468499
}
469-
})
500+
}
470501

471-
relay.on('close', () => {
472-
sessions.clear()
473-
eventSocket.close()
474-
void closeBackend().finally(closeResolve)
475-
})
476-
await new Promise<void>((resolve, reject) => { relay.once('open', resolve); relay.once('error', reject) })
502+
const connectRelay = (): Promise<void> => {
503+
const socket = new WebSocket(websocketOrigin(options.credential.hubOrigin), ['crewcode.brain.v1', options.credential.token])
504+
relay = socket
505+
socket.on('message', raw => { void handleRelayMessage(raw) })
506+
// The close handler owns retry. Keep an error listener installed so a failed
507+
// reconnect cannot become an uncaught EventEmitter error.
508+
socket.on('error', () => undefined)
509+
socket.on('close', () => {
510+
if (relay !== socket) return
511+
// Detach only browser routing. Provider execution and the loopback event
512+
// socket stay alive while the persistent Brain reconnects to the Hub.
513+
for (const connectionId of [...sessions.keys()]) releaseSession(connectionId)
514+
if (closing) {
515+
eventSocket.close()
516+
void closeBackend().finally(closeResolve)
517+
return
518+
}
519+
reconnectTimer = setTimeout(() => {
520+
reconnectTimer = null
521+
void connectRelay().catch(() => undefined)
522+
}, 5_000)
523+
})
524+
return new Promise<void>((resolve, reject) => {
525+
socket.once('open', resolve)
526+
socket.once('error', reject)
527+
})
528+
}
529+
await connectRelay()
477530
await relayReady
478531

479532
return {
480533
closed,
481534
async close() {
482535
if (closing) return closed
483536
closing = true
484-
relay.close(1000, 'brain stopping')
537+
if (reconnectTimer) { clearTimeout(reconnectTimer); reconnectTimer = null }
538+
if (relay.readyState === WebSocket.OPEN || relay.readyState === WebSocket.CONNECTING) relay.close(1000, 'brain stopping')
485539
eventSocket.close()
486540
await closeBackend()
487541
if (relay.readyState === WebSocket.CLOSED) closeResolve()

0 commit comments

Comments
 (0)