Skip to content

fix(session): accept directory when creating sessions#21131

Open
EZotoff wants to merge 3 commits intoanomalyco:devfrom
EZotoff:fix/session-create-directory
Open

fix(session): accept directory when creating sessions#21131
EZotoff wants to merge 3 commits intoanomalyco:devfrom
EZotoff:fix/session-create-directory

Conversation

@EZotoff
Copy link
Copy Markdown

@EZotoff EZotoff commented Apr 5, 2026

Issue for this PR

Closes #12918

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

This makes session creation honor an explicit directory override.

Specifically:

  • Session.create now accepts an optional directory
  • POST /session now accepts ?directory=... and forwards it into session creation
  • supplied directories are canonicalized through Filesystem.resolve(...) before being persisted
  • a regression test covers the route-level override path

Why this works:

  • upstream already supports directory-scoped session listing and createNext already persists an explicit directory
  • the missing piece was exposing that directory through Session.create and the create route
  • the change stays backward-compatible because callers still fall back to InstanceState.directory when no override is provided

This is intentionally narrower than #9365, which tackles broader per-session working-directory behavior across the system.

How did you verify your code works?

  • ran bun test ./test/server/session-actions.test.ts from packages/opencode
  • pre-push hook typecheck passed when pushing the branch

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Copilot AI review requested due to automatic review settings April 5, 2026 21:35
@github-actions github-actions bot added the needs:compliance This means the issue will auto-close after 2 hours. label Apr 5, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 5, 2026

The following comment was made by an LLM, it may be inaccurate:

Based on my search, I found the following related PRs:

Potentially Related:

Other Potentially Relevant:

The current PR (21131) appears to be addressing issue #12918 and is a narrow, backward-compatible fix that enables the Session.create method to accept an optional directory parameter, which is a prerequisite for the broader per-session working directory support in #9365.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR makes session creation directory-aware by allowing callers to override the working directory at create time, aligning POST /session with existing directory-based session listing behavior and enabling correct session directory assignment when switching projects/worktrees.

Changes:

  • Add optional directory parameter to Session.create (schema + service implementation) and persist it to the session.
  • Extend POST /session to accept ?directory=... and forward it into Session.create.
  • Add a regression test covering the route-level directory override.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
packages/opencode/src/session/index.ts Adds optional directory support to the Session.create API and schema.
packages/opencode/src/server/routes/session.ts Accepts directory via query params for session creation and passes it through.
packages/opencode/test/server/session-actions.test.ts Adds a test verifying the create route honors ?directory=....
Comments suppressed due to low confidence (1)

packages/opencode/src/session/index.ts:507

  • Session.create now accepts a caller-supplied directory and persists it as-is. Unlike the default path (from InstanceState.directory), this can be relative, contain .. segments, or be an empty string, which risks creating sessions with a non-canonical/invalid working directory and later tool execution or path checks behaving inconsistently. Consider normalizing/validating input.directory (e.g., resolve to an absolute canonical path and reject empty strings) before passing it to createNext.
      const create = Effect.fn("Session.create")(function* (input?: {
        directory?: string
        parentID?: SessionID
        title?: string
        permission?: Permission.Ruleset
        workspaceID?: WorkspaceID
      }) {
        const directory = input?.directory ?? (yield* InstanceState.directory)
        return yield* createNext({
          parentID: input?.parentID,
          directory,
          title: input?.title,

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +209 to +213
validator("query", z.object({ directory: z.string().optional() }).optional()),
async (c) => {
const body = c.req.valid("json") ?? {}
const session = await Session.create(body)
const query = c.req.valid("query")
const session = await Session.create({ ...body, ...query })
Copy link

Copilot AI Apr 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new route-level directory override is persisted to the session, but the value is taken directly from the query string rather than the already-canonicalized Instance.directory that WorkspaceRouterMiddleware computes for routing. This can lead to sessions storing a different path representation than the active instance (e.g., relative paths, unnormalized casing on Windows, or paths with ..). Prefer resolving the incoming directory (or using Instance.directory) before passing it to Session.create.

Copilot uses AI. Check for mistakes.
Comment on lines +209 to +213
validator("query", z.object({ directory: z.string().optional() }).optional()),
async (c) => {
const body = c.req.valid("json") ?? {}
const session = await Session.create(body)
const query = c.req.valid("query")
const session = await Session.create({ ...body, ...query })
Copy link

Copilot AI Apr 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The query validator is defined as z.object({ directory: z.string().optional() }).optional(). Since the object already has only-optional fields, making the entire query object optional is redundant and makes c.req.valid("query") typed as possibly undefined, which complicates downstream handling. Prefer a non-optional object schema here (consistent with the other query validators in this file) and treat the validated query as { directory?: string }.

Copilot uses AI. Check for mistakes.
@github-actions github-actions bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Apr 5, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 5, 2026

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@EZotoff
Copy link
Copy Markdown
Author

EZotoff commented Apr 5, 2026

Addressed the review points in follow-up commit 1a667e93f:

  • normalized explicit directory overrides through Filesystem.resolve(...) before persisting them, so create-time overrides use the same canonical path style as the rest of the instance/bootstrap flow
  • simplified the route query validator to a non-optional object schema
  • updated the PR description to match the repository template

Validation:

  • bun test ./test/server/session-actions.test.ts
  • pre-push hook typecheck passed on push

@EZotoff
Copy link
Copy Markdown
Author

EZotoff commented Apr 5, 2026

I tracked the Windows-only failure down to the test assertion rather than the route implementation. On Windows, Session.create now stores the canonicalized path via Filesystem.resolve(...), so comparing session.directory to the raw dir string was too strict.

Pushed follow-up commit e1bf33041:

  • test(session): compare canonicalized directory in route test

Validation:

  • bun test ./test/server/session-actions.test.ts
  • pre-push typecheck hook ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: New session created with wrong directory when browsing a project

2 participants