mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
feat(servers): harden pairing and run linked-server sessions in the UI
Replace the plaintext pairing exchange with a nonce and HMAC proof plus AES-256-GCM encrypted credentials, bumping links to v2. Track session ownership per linked server so activity, session picker, transcript, MCP and skills views work against remote servers. Share encrypted secret access, event routing and URL helpers across backend, shared and CLI.
This commit is contained in:
1 parent
c591c15743
commit
f566556fd6
77 files changed
+3170
-702
No files matched your search
@@ -9,6 +9,7 @@ import type {
|
||||
} from '@opencode-manager/shared/opencode'
|
||||
import { useSessionTranscript } from './useSessionTranscript'
|
||||
import { ContextUsageIndicator } from '@/components/session/ContextUsageIndicator'
|
||||
import { OpenCodeServerProvider } from '@/contexts/OpenCodeServerContext'
|
||||
import { applySessionEvent, emptySessionTranscript } from '@/lib/session-projection'
|
||||
import type { TranscriptCache } from '@/lib/session-projection'
|
||||
import { sessionTranscriptQueryKey } from '@/lib/queryInvalidation'
|
||||
@@ -31,6 +32,14 @@ const mocks = vi.hoisted(() => ({
|
||||
transport: null as unknown,
|
||||
}))
|
||||
|
||||
const serverStatusMocks = vi.hoisted(() => ({
|
||||
useOpenCodeServerStatuses: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useOpenCodeServers', () => ({
|
||||
useOpenCodeServerStatuses: serverStatusMocks.useOpenCodeServerStatuses,
|
||||
}))
|
||||
|
||||
vi.mock('@/api/opencode', () => ({
|
||||
listSessionMessages: mocks.listSessionMessages,
|
||||
readSessionSnapshot: mocks.readSessionSnapshot,
|
||||
@@ -58,7 +67,7 @@ interface TestTransport {
|
||||
connected(): void
|
||||
fail(): void
|
||||
message(data: unknown): void
|
||||
resync(): void
|
||||
resync(payload?: unknown): void
|
||||
}
|
||||
|
||||
function testTransport(): TestTransport {
|
||||
@@ -114,9 +123,20 @@ function createQueryClient() {
|
||||
return new QueryClient({ defaultOptions: { queries: { retry: false } } })
|
||||
}
|
||||
|
||||
function createRemoteWrapper(queryClient: QueryClient) {
|
||||
return function Wrapper({ children }: { children: ReactNode }) {
|
||||
return (
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<OpenCodeServerProvider serverId="srv_remote">{children}</OpenCodeServerProvider>
|
||||
</QueryClientProvider>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
describe('useSessionTranscript', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
serverStatusMocks.useOpenCodeServerStatuses.mockReturnValue({ statusById: {} })
|
||||
mocks.readSessionSnapshot.mockReset()
|
||||
mocks.readSessionSnapshot.mockResolvedValue({
|
||||
messages: seededMessages,
|
||||
@@ -216,6 +236,43 @@ describe('useSessionTranscript', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('ignores a resync scoped to another server', async () => {
|
||||
const queryClient = createQueryClient()
|
||||
const { result } = renderHook(() => useSessionTranscript(SESSION_ID, DIRECTORY), {
|
||||
wrapper: createWrapper(queryClient),
|
||||
})
|
||||
|
||||
await waitFor(() => expect(result.current.messages).toHaveLength(2))
|
||||
expect(mocks.readSessionSnapshot).toHaveBeenCalledTimes(1)
|
||||
|
||||
await act(async () => {
|
||||
testTransport().resync({ timestamp: Date.now(), serverId: 'srv_remote' })
|
||||
})
|
||||
|
||||
expect(mocks.readSessionSnapshot).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('refetches the newest page on a resync scoped to its own remote server', async () => {
|
||||
const queryClient = createQueryClient()
|
||||
const wrapper = function Wrapper({ children }: { children: ReactNode }) {
|
||||
return (
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<OpenCodeServerProvider serverId="srv_remote">{children}</OpenCodeServerProvider>
|
||||
</QueryClientProvider>
|
||||
)
|
||||
}
|
||||
const { result } = renderHook(() => useSessionTranscript(SESSION_ID, DIRECTORY), { wrapper })
|
||||
|
||||
await waitFor(() => expect(result.current.messages).toHaveLength(2))
|
||||
expect(mocks.readSessionSnapshot).toHaveBeenCalledTimes(1)
|
||||
|
||||
await act(async () => {
|
||||
testTransport().resync({ timestamp: Date.now(), serverId: 'srv_remote' })
|
||||
})
|
||||
|
||||
await waitFor(() => expect(mocks.readSessionSnapshot).toHaveBeenCalledTimes(2))
|
||||
})
|
||||
|
||||
it('reads the newest page once on a fresh mount with no cached transcript', async () => {
|
||||
const queryClient = createQueryClient()
|
||||
const { result } = renderHook(() => useSessionTranscript(SESSION_ID, DIRECTORY), {
|
||||
@@ -556,6 +613,111 @@ describe('useSessionTranscript', () => {
|
||||
}
|
||||
})
|
||||
|
||||
it('polls a remote transcript while the remote server is disconnected even when the browser stream is connected', async () => {
|
||||
vi.useFakeTimers()
|
||||
try {
|
||||
serverStatusMocks.useOpenCodeServerStatuses.mockReturnValue({
|
||||
statusById: {
|
||||
srv_remote: { serverId: 'srv_remote', connected: false, version: null, error: null },
|
||||
},
|
||||
})
|
||||
const queryClient = createQueryClient()
|
||||
const { result } = renderHook(() => useSessionTranscript(SESSION_ID, DIRECTORY), {
|
||||
wrapper: createRemoteWrapper(queryClient),
|
||||
})
|
||||
|
||||
await act(async () => {
|
||||
await vi.advanceTimersByTimeAsync(0)
|
||||
})
|
||||
expect(result.current.messages).toHaveLength(2)
|
||||
|
||||
act(() => {
|
||||
testTransport().connected()
|
||||
})
|
||||
await act(async () => {
|
||||
await vi.advanceTimersByTimeAsync(0)
|
||||
})
|
||||
const readsAfterConnect = mocks.readSessionSnapshot.mock.calls.length
|
||||
|
||||
await act(async () => {
|
||||
await vi.advanceTimersByTimeAsync(15000)
|
||||
})
|
||||
expect(mocks.readSessionSnapshot.mock.calls.length).toBeGreaterThan(readsAfterConnect)
|
||||
} finally {
|
||||
vi.useRealTimers()
|
||||
}
|
||||
})
|
||||
|
||||
it('stops polling a remote transcript once both the browser stream and the remote server are connected', async () => {
|
||||
vi.useFakeTimers()
|
||||
try {
|
||||
serverStatusMocks.useOpenCodeServerStatuses.mockReturnValue({
|
||||
statusById: {
|
||||
srv_remote: { serverId: 'srv_remote', connected: true, version: null, error: null },
|
||||
},
|
||||
})
|
||||
const queryClient = createQueryClient()
|
||||
const { result } = renderHook(() => useSessionTranscript(SESSION_ID, DIRECTORY), {
|
||||
wrapper: createRemoteWrapper(queryClient),
|
||||
})
|
||||
|
||||
await act(async () => {
|
||||
await vi.advanceTimersByTimeAsync(0)
|
||||
})
|
||||
expect(result.current.messages).toHaveLength(2)
|
||||
|
||||
act(() => {
|
||||
testTransport().connected()
|
||||
})
|
||||
await act(async () => {
|
||||
await vi.advanceTimersByTimeAsync(0)
|
||||
})
|
||||
const readsAfterConnect = mocks.readSessionSnapshot.mock.calls.length
|
||||
|
||||
await act(async () => {
|
||||
await vi.advanceTimersByTimeAsync(15000)
|
||||
})
|
||||
expect(mocks.readSessionSnapshot.mock.calls.length).toBe(readsAfterConnect)
|
||||
} finally {
|
||||
vi.useRealTimers()
|
||||
}
|
||||
})
|
||||
|
||||
it('keeps the local transcript polling behavior independent of server statuses', async () => {
|
||||
vi.useFakeTimers()
|
||||
try {
|
||||
serverStatusMocks.useOpenCodeServerStatuses.mockReturnValue({
|
||||
statusById: {
|
||||
local: { serverId: 'local', connected: false, version: null, error: null },
|
||||
},
|
||||
})
|
||||
const queryClient = createQueryClient()
|
||||
const { result } = renderHook(() => useSessionTranscript(SESSION_ID, DIRECTORY), {
|
||||
wrapper: createWrapper(queryClient),
|
||||
})
|
||||
|
||||
await act(async () => {
|
||||
await vi.advanceTimersByTimeAsync(0)
|
||||
})
|
||||
expect(result.current.messages).toHaveLength(2)
|
||||
|
||||
act(() => {
|
||||
testTransport().connected()
|
||||
})
|
||||
await act(async () => {
|
||||
await vi.advanceTimersByTimeAsync(0)
|
||||
})
|
||||
const readsAfterConnect = mocks.readSessionSnapshot.mock.calls.length
|
||||
|
||||
await act(async () => {
|
||||
await vi.advanceTimersByTimeAsync(15000)
|
||||
})
|
||||
expect(mocks.readSessionSnapshot.mock.calls.length).toBe(readsAfterConnect)
|
||||
} finally {
|
||||
vi.useRealTimers()
|
||||
}
|
||||
})
|
||||
|
||||
it('reads the newest page once when execution ends while a tool is still running', async () => {
|
||||
const running = promptSequence.slice(0, 15).reduce(applySessionEvent, emptySessionTranscript)
|
||||
mocks.readSessionSnapshot.mockResolvedValue({
|
||||
|
||||
Reference in new issue
Block a user