Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions packages/app/src/pages/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ import {
displayName,
effectiveWorkspaceOrder,
errorMessage,
newSessionRoute,
openProjectRoute,
openSessionRoute,
startupAutoselectDirectory,
sortedRootSessions,
workspaceKey,
Expand All @@ -84,6 +82,7 @@ import {
pawworkSessionDirectories,
sortPawworkSidebarSessions,
} from "./layout/pawwork-session-source"
import { createShellNavigation } from "./layout/shell-navigation"
import {
buildPawworkSessionWindow,
nextPawworkSessionWindowLimit,
Expand Down Expand Up @@ -1364,11 +1363,15 @@ export default function Layout(props: ParentProps) {
})
}

function openSettings() {
function openSettingsSurface() {
setSettingsTab("general")
setSettingsOpen(true)
}

function openSettings() {
shellNavigation.openSettings()
}

createEffect(() => {
command.setModalOpen(settingsOpen())
})
Expand Down Expand Up @@ -1415,6 +1418,14 @@ export default function Layout(props: ParentProps) {
return currentProject()?.worktree ?? projectRoot(directory)
}

function releaseTransientShellLocks() {
if (sizet !== undefined) {
clearTimeout(sizet)
sizet = undefined
}
setState("sizing", false)
}

function syncSessionRoute(directory: string, id: string, root = activeProjectRoot(directory)) {
notification.session.markViewed(id)
const expanded = untrack(() => store.workspaceExpanded[directory])
Expand All @@ -1433,19 +1444,22 @@ export default function Layout(props: ParentProps) {
}

function navigateToSession(session: Session | undefined) {
if (!session) return
navigate(openSessionRoute(session.directory, session.id))
shellNavigation.openSession(session)
}

function openPawworkHome(directory?: string) {
const root = directory ? projectRoot(directory) : currentProject()?.worktree ?? projectRoot(currentDir())
if (!root) {
chooseProject()
return
}
navigate(newSessionRoute(root))
shellNavigation.openNewSession(directory)
}

const shellNavigation = createShellNavigation({
navigate,
releaseTransientLocks: releaseTransientShellLocks,
resolveProjectRoot: projectRoot,
currentProjectRoot: () => currentProject()?.worktree ?? projectRoot(currentDir()),
chooseProject,
openSettingsSurface,
})

function openProject(directory: string, shouldNavigate = true) {
layout.projects.open(directory)
if (shouldNavigate) return navigateToProject(directory)
Expand Down
69 changes: 69 additions & 0 deletions packages/app/src/pages/layout/shell-navigation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { describe, expect, test } from "bun:test"
import { base64Encode } from "@opencode-ai/util/encode"
import { createShellNavigation } from "./shell-navigation"

describe("createShellNavigation", () => {
test("opens a new session through one shell action and releases transient locks first", () => {
const calls: string[] = []
const shell = createShellNavigation({
navigate: (route) => calls.push(`navigate:${route}`),
releaseTransientLocks: (reason) => calls.push(`release:${reason}`),
resolveProjectRoot: (directory) => `/root:${directory}`,
currentProjectRoot: () => "/current",
chooseProject: () => calls.push("chooseProject"),
openSettingsSurface: () => calls.push("settings"),
})

shell.openNewSession("/repo")

expect(calls).toEqual([`release:new-session`, `navigate:/${base64Encode("/root:/repo")}/session`])
})

test("opens an existing session through one shell action and releases transient locks first", () => {
const calls: string[] = []
const shell = createShellNavigation({
navigate: (route) => calls.push(`navigate:${route}`),
releaseTransientLocks: (reason) => calls.push(`release:${reason}`),
resolveProjectRoot: (directory) => directory,
currentProjectRoot: () => "/current",
chooseProject: () => calls.push("chooseProject"),
openSettingsSurface: () => calls.push("settings"),
})

shell.openSession({ directory: "/repo", id: "ses_123" })

expect(calls).toEqual([`release:session`, `navigate:/${base64Encode("/repo")}/session/ses_123`])
})

test("opens settings through the same shell action owner instead of a standalone signal", () => {
const calls: string[] = []
const shell = createShellNavigation({
navigate: (route) => calls.push(`navigate:${route}`),
releaseTransientLocks: (reason) => calls.push(`release:${reason}`),
resolveProjectRoot: (directory) => directory,
currentProjectRoot: () => "/current",
chooseProject: () => calls.push("chooseProject"),
openSettingsSurface: () => calls.push("settings"),
})

shell.openSettings()

expect(calls).toEqual(["release:settings", "settings"])
})

test("falls back to project chooser when no directory can be resolved for a new session", () => {
const calls: string[] = []
const shell = createShellNavigation({
navigate: (route) => calls.push(`navigate:${route}`),
releaseTransientLocks: (reason) => calls.push(`release:${reason}`),
resolveProjectRoot: () => "",
currentProjectRoot: () => undefined,
chooseProject: () => calls.push("chooseProject"),
openSettingsSurface: () => calls.push("settings"),
})

shell.openNewSession()

expect(calls).toEqual(["release:new-session", "chooseProject"])
})
})
49 changes: 49 additions & 0 deletions packages/app/src/pages/layout/shell-navigation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { newSessionRoute, openSessionRoute } from "./helpers"

export type ShellNavigationReleaseReason = "new-session" | "session" | "settings" | "project"

export type ShellNavigationSession = {
directory: string
id: string
}

export function createShellNavigation(input: {
navigate: (route: string) => void
releaseTransientLocks: (reason: ShellNavigationReleaseReason) => void
resolveProjectRoot: (directory: string) => string | undefined
currentProjectRoot: () => string | undefined
chooseProject: () => void
openSettingsSurface: () => void
}) {
const resolveNewSessionRoot = (directory?: string) => {
if (directory) return input.resolveProjectRoot(directory)
return input.currentProjectRoot()
}

const openNewSession = (directory?: string) => {
input.releaseTransientLocks("new-session")
const root = resolveNewSessionRoot(directory)
if (!root) {
input.chooseProject()
return
}
input.navigate(newSessionRoute(root))
}

const openSession = (session: ShellNavigationSession | undefined) => {
if (!session) return
input.releaseTransientLocks("session")
input.navigate(openSessionRoute(session.directory, session.id))
}

const openSettings = () => {
input.releaseTransientLocks("settings")
input.openSettingsSurface()
}

return {
openNewSession,
openSession,
openSettings,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,14 @@ describe("findRunningQuestionFallbackSession", () => {
).toBeUndefined()
})

test("ignores running question parts older than the lookback window", () => {
test("recovers running question parts even when they are older than the lookback window", () => {
expect(
findRunningQuestionFallbackSession({
sessionID: "s",
hasQuestionRequest: false,
lookback: 2,
messages: [message("old"), message("recent-1"), message("recent-2")],
partsByMessageID: { old: [toolPart("question")] },
}),
).toBeUndefined()
).toBe("s")
})
})
6 changes: 1 addition & 5 deletions packages/app/src/pages/session/blockers/question-fallback.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import type { Message, Part } from "@opencode-ai/sdk/v2"

export const QUESTION_FALLBACK_LOOKBACK_MESSAGES = 5

export function findRunningQuestionFallbackSession(input: {
sessionID?: string
hasQuestionRequest: boolean
messages?: Message[]
partsByMessageID: Record<string, Part[] | undefined>
lookback?: number
}): string | undefined {
if (!input.sessionID) return undefined
if (input.hasQuestionRequest) return undefined
const messages = input.messages
if (!messages?.length) return undefined

const lookback = input.lookback ?? QUESTION_FALLBACK_LOOKBACK_MESSAGES
for (let i = messages.length - 1; i >= Math.max(0, messages.length - lookback); i--) {
for (let i = messages.length - 1; i >= 0; i--) {
const parts = input.partsByMessageID[messages[i].id]
if (!parts) continue
for (const part of parts) {
Expand Down
23 changes: 9 additions & 14 deletions packages/app/src/pages/session/session-view-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("createSessionViewController", () => {
})
})

test("keeps route and visible state distinct while the route session is not ready", () => {
test("keeps route and visible identity aligned while the route session is not ready", () => {
createRoot((dispose) => {
const controller = createSessionViewController({
directory: () => "repo",
Expand All @@ -34,8 +34,8 @@ describe("createSessionViewController", () => {
expect(controller.route.id()).toBe("ses_target")
expect(controller.route.key()).toBe("repo/ses_target")
expect(controller.route.ready()).toBe(false)
expect(controller.visible.id()).toBeUndefined()
expect(controller.visible.key()).toBe("repo")
expect(controller.visible.id()).toBe("ses_target")
expect(controller.visible.key()).toBe("repo/ses_target")
expect(controller.visible.ready()).toBe(false)
expect(controller.transitioning()).toBe(true)

Expand All @@ -45,9 +45,8 @@ describe("createSessionViewController", () => {
})

describe("nextSessionViewState", () => {
test("keeps visible session on the previous ready session while route session loads", () => {
test("does not keep the previous visible session while the route session loads", () => {
const loading = nextSessionViewState({
currentVisibleSessionID: "ses_source",
directory: "repo",
routeSessionID: "ses_target",
routeMessagesReady: false,
Expand All @@ -56,14 +55,13 @@ describe("nextSessionViewState", () => {
expect(loading).toMatchObject({
routeSessionID: "ses_target",
routeReady: false,
visibleSessionID: "ses_source",
visibleSessionID: "ses_target",
transitioning: true,
routeSessionKey: "repo/ses_target",
visibleSessionKey: "repo/ses_source",
visibleSessionKey: "repo/ses_target",
})

const ready = nextSessionViewState({
currentVisibleSessionID: loading.visibleSessionID,
directory: "repo",
routeSessionID: "ses_target",
routeMessagesReady: true,
Expand All @@ -81,7 +79,6 @@ describe("nextSessionViewState", () => {

test("clears visible session when leaving a concrete session route", () => {
const next = nextSessionViewState({
currentVisibleSessionID: "ses_source",
directory: "repo",
routeSessionID: undefined,
routeMessagesReady: true,
Expand All @@ -95,10 +92,8 @@ describe("nextSessionViewState", () => {
expect(next.transitioning).toBe(false)
})

test("does not carry visible session state across directories", () => {
test("uses the target route identity when changing directories", () => {
const next = nextSessionViewState({
currentVisibleDirectory: "repo-a",
currentVisibleSessionID: "ses_source",
directory: "repo-b",
routeSessionID: "ses_target",
routeMessagesReady: false,
Expand All @@ -107,10 +102,10 @@ describe("nextSessionViewState", () => {
expect(next).toMatchObject({
routeSessionID: "ses_target",
routeReady: false,
visibleSessionID: undefined,
visibleSessionID: "ses_target",
transitioning: true,
routeSessionKey: "repo-b/ses_target",
visibleSessionKey: "repo-b",
visibleSessionKey: "repo-b/ses_target",
})
})
})
18 changes: 2 additions & 16 deletions packages/app/src/pages/session/session-view-controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { createMemo, type Accessor } from "solid-js"

export type SessionViewStateInput = {
currentVisibleDirectory?: string
currentVisibleSessionID: string | undefined
directory: string
routeSessionID: string | undefined
routeMessagesReady: boolean
Expand All @@ -19,32 +17,23 @@ export function sessionKey(input: { directory: string; sessionID: string | undef
}

export function nextVisibleSessionID(input: {
current: string | undefined
route: string | undefined
routeReady: boolean
}) {
if (!input.route) return undefined
if (input.routeReady) return input.route
return input.current
return input.route
}
Comment thread
Astro-Han marked this conversation as resolved.
Resolved

export function nextSessionViewState(input: SessionViewStateInput) {
const routeReady = !input.routeSessionID || input.routeMessagesReady
const currentVisibleSessionID =
input.currentVisibleDirectory && input.currentVisibleDirectory !== input.directory
? undefined
: input.currentVisibleSessionID
const visibleSessionID = nextVisibleSessionID({
current: currentVisibleSessionID,
route: input.routeSessionID,
routeReady,
})

return {
routeSessionID: input.routeSessionID,
routeReady,
visibleSessionID,
transitioning: visibleSessionID !== input.routeSessionID,
transitioning: !!input.routeSessionID && (!routeReady || visibleSessionID !== input.routeSessionID),
Comment thread
Astro-Han marked this conversation as resolved.
Resolved
Comment thread
Astro-Han marked this conversation as resolved.
Resolved
routeSessionKey: sessionKey({ directory: input.directory, sessionID: input.routeSessionID }),
visibleSessionKey: sessionKey({ directory: input.directory, sessionID: visibleSessionID }),
}
Expand All @@ -56,8 +45,6 @@ export function createSessionViewController(input: SessionViewControllerInput) {
const directory = input.directory()
return {
...nextSessionViewState({
currentVisibleDirectory: current?.directory,
currentVisibleSessionID: current?.visibleSessionID,
directory,
routeSessionID: input.routeSessionID(),
routeMessagesReady: input.routeMessagesReady(),
Expand All @@ -69,7 +56,6 @@ export function createSessionViewController(input: SessionViewControllerInput) {
const visibleReady = () => {
const next = state()
if (!next.visibleSessionID) return !next.routeSessionID || next.routeReady
if (next.visibleSessionID !== next.routeSessionID) return true
return next.routeReady
}
Comment thread
Astro-Han marked this conversation as resolved.
Resolved

Expand Down
Loading