mirror of
https://github.com/multica-ai/multica.git
synced 2026-08-02 18:13:27 +02:00
Wires the Phase 1 terminal.Manager into the live daemonws transport so a
browser tab on the Multica Desktop app can attach to a PTY running in
the daemon's task workdir. End-to-end frame path:
Desktop (xterm.js) → /ws/issues/{id}/terminal (server proxy, cookie
or first-frame JWT auth, workspace membership
enforced before upgrade)
→ daemonws hub (new SendToRuntime + TerminalRouter routing
terminal.* frames back to the proxy by
request_id, then re-keyed on session_id)
→ daemon (new terminal_bridge.go owns one Manager per WS
connection, drains PtySession.Output() into
terminal.data frames, surfaces ExitC as
terminal.exit; closeAll on WS disconnect)
→ terminal.Manager.OpenWithInfo (new method) — server resolves
task.work_dir/issue_id/prior_session_id from its DB and embeds
them on the protocol; daemon trusts the server payload, no
daemon-local task cache needed.
Auth + ACL:
- Browser proxy enforces workspace membership before the upgrade.
- TerminalOpenPayload carries the resolved task info; cross-workspace
is structurally impossible at the bridge layer because both
OpenParams.WorkspaceID and TaskInfo.WorkspaceID come from the same
server-resolved field.
- terminal_bridge maps terminal.{Manager.OpenWithInfo} errors to the
protocol error codes (workspace_mismatch / task_not_found /
unsupported_os / spawn_failed / internal).
Resume:
- Server passes prior_session_id; daemon injects CLAUDE_SESSION_ID +
MULTICA_{WORKSPACE,ISSUE,TASK,USER}_ID into the PTY env per the RFC.
`claude --resume \$CLAUDE_SESSION_ID` continues the agent's session.
Lifecycle:
- Daemon installs a fresh terminalBridge per daemonws connection and
tears every PtySession down on disconnect; session_ids minted on one
WS cannot be reused on a reconnect because the server-side routing
registration is also gone.
- daemonws client.send buffer raised from 16 to 256 and read limit
from 4KB to 64KB so PTY traffic fits without evicting connections
used for heartbeat / wakeup hints.
Desktop UI:
- packages/views/issues/components/terminal-panel.tsx renders an
xterm.js console with FitAddon, base64 wire encoding, ResizeObserver
→ terminal.resize, reconnect button, and a clear web-only placeholder
when window.desktopAPI is absent.
- TerminalPanelSection wrapper hangs collapsed in the issue-detail
sidebar next to the execution log so bootstrap doesn't run for
every issue view.
Tests:
- terminal/manager_test.go: OpenWithInfo happy path + cross-workspace
reject (16 tests total, all -race clean).
- daemonws/terminal_test.go: TerminalRouter request_id → session_id
re-keying and unknown-session drop.
- daemon/terminal_bridge_test.go: server-supplied workdir round-trips
through OpenWithInfo, missing-workdir surfaces task_not_found and
never spawns, data+exit round trip.
- GOOS=windows go test -c clean for both daemon and daemon/terminal.
Phase 1 / 2 / 3 / 4 mapping unchanged: Phase 3 (CLI) and Phase 4
(execenv GC hook + issue runs entry + audit log) are still untouched.
Co-authored-by: multica-agent <github@multica.ai>
96 lines
2.8 KiB
Go
96 lines
2.8 KiB
Go
package daemonws
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/multica-ai/multica/server/pkg/protocol"
|
|
)
|
|
|
|
type collectingSink struct {
|
|
frames [][]byte
|
|
}
|
|
|
|
func (s *collectingSink) Deliver(frame []byte) bool {
|
|
cp := make([]byte, len(frame))
|
|
copy(cp, frame)
|
|
s.frames = append(s.frames, cp)
|
|
return true
|
|
}
|
|
|
|
func TestTerminalRouter_RoutesByRequestThenSession(t *testing.T) {
|
|
router := NewTerminalRouter()
|
|
sink := &collectingSink{}
|
|
router.Register("req-1", sink)
|
|
|
|
// terminal.error before the daemon picked a session_id: routed on
|
|
// request_id. This is the failure path browsers see when the daemon
|
|
// can't spawn a PTY (e.g. ErrUnsupportedOS on windows).
|
|
errFrame := mustEncode(t, protocol.MessageTypeTerminalError, protocol.TerminalErrorPayload{
|
|
RequestID: "req-1",
|
|
Code: protocol.TerminalErrorCodeUnsupportedOS,
|
|
Message: "no PTY on windows",
|
|
})
|
|
router.Route(errFrame, protocol.MessageTypeTerminalError, mustPayload(t, errFrame))
|
|
|
|
if got, want := len(sink.frames), 1; got != want {
|
|
t.Fatalf("delivered = %d, want %d", got, want)
|
|
}
|
|
|
|
// Re-key to session_id after a hypothetical terminal.opened.
|
|
router.Register("sess-1", sink)
|
|
router.Unregister("req-1")
|
|
|
|
dataFrame := mustEncode(t, protocol.MessageTypeTerminalData, protocol.TerminalDataPayload{
|
|
SessionID: "sess-1",
|
|
DataB64: "Zm9vYmFy",
|
|
})
|
|
router.Route(dataFrame, protocol.MessageTypeTerminalData, mustPayload(t, dataFrame))
|
|
if got, want := len(sink.frames), 2; got != want {
|
|
t.Fatalf("after data delivered = %d, want %d", got, want)
|
|
}
|
|
|
|
// Frames for an unknown session must drop silently — never panic, and
|
|
// never leak into the wrong sink.
|
|
strayFrame := mustEncode(t, protocol.MessageTypeTerminalExit, protocol.TerminalExitPayload{
|
|
SessionID: "sess-2-unknown",
|
|
ExitCode: 0,
|
|
})
|
|
router.Route(strayFrame, protocol.MessageTypeTerminalExit, mustPayload(t, strayFrame))
|
|
if got, want := len(sink.frames), 2; got != want {
|
|
t.Fatalf("stray frame delivered to wrong sink: %d", got)
|
|
}
|
|
}
|
|
|
|
func TestTerminalRouter_UnknownSessionDropsSilently(t *testing.T) {
|
|
router := NewTerminalRouter()
|
|
frame := mustEncode(t, protocol.MessageTypeTerminalData, protocol.TerminalDataPayload{
|
|
SessionID: "ghost",
|
|
DataB64: "Zm9v",
|
|
})
|
|
// Should not panic / not deliver anywhere.
|
|
router.Route(frame, protocol.MessageTypeTerminalData, mustPayload(t, frame))
|
|
}
|
|
|
|
func mustEncode(t *testing.T, msgType string, payload any) []byte {
|
|
t.Helper()
|
|
raw, err := json.Marshal(payload)
|
|
if err != nil {
|
|
t.Fatalf("marshal payload: %v", err)
|
|
}
|
|
frame, err := json.Marshal(protocol.Message{Type: msgType, Payload: raw})
|
|
if err != nil {
|
|
t.Fatalf("marshal envelope: %v", err)
|
|
}
|
|
return frame
|
|
}
|
|
|
|
func mustPayload(t *testing.T, envelope []byte) json.RawMessage {
|
|
t.Helper()
|
|
var m protocol.Message
|
|
if err := json.Unmarshal(envelope, &m); err != nil {
|
|
t.Fatalf("unmarshal envelope: %v", err)
|
|
}
|
|
return m.Payload
|
|
}
|