Repository navigation
fix: re-enter Instance context for session routes - #17320
majiayu000 wants to merge 1 commit into
Conversation
|
We encountered the exact same issue when using the OpenCode CLI with Our setup:
Verification logs: Session correctly stores the directory, but when AI executes Impact: The root cause analysis in this PR perfectly matches what we found - the middleware defaults to Looking forward to this fix being merged! 🙏 |
|
I hope this gets merged soon and the attention also of the maintainers. I am running into the same issues and dropped my current plugin development work for OpenCode. I dont understand, why this is not on their radar. Multi-Agents workflows and work-tree handling is key for us to work reliably and isolated. Everything else is a nice show off, but during production work we need control during concurrent work (as we also do with rules and workflow agreements with human colleagues). I tried different approaches to provide plugins that checkout new sessions based on command execution that triggers isolated git worktree creation (effectively a new directory for the new session). No chance, you hit the wall because of this. |
When a session is created via `POST /session?directory=/path`, the session stores that directory. However, subsequent prompt requests without the `?directory=` param default to `process.cwd()`, causing Instance context mismatch. This makes prompt processing run in the wrong working directory (ctx.directory / ctx.worktree read from InstanceState.context inside SessionPrompt all point at the server's cwd rather than the session's). Re-enter the correct Instance context using the session's stored directory before processing in all affected routes: summarize, message (prompt), prompt_async, command, and shell. Since Instance.provide caches by directory, there is zero overhead when the directory matches. Fixes #12271 Signed-off-by: majiayu000 <1835304752@qq.com>
fbf4846 to
bb95114
Compare
Issue for this PR
Closes #12271
Type of change
What does this PR do?
Session routes (
message,prompt_async,command,shell,summarize) lose the correct directory context on subsequent requests because the middleware defaults toprocess.cwd()when no?directory=query param is present.The root cause:
Session.create()stores the directory fromInstance.directoryat creation time. But later requests to/session/{id}/messageetc. don't pass?directory=, so the middleware creates an Instance context scoped toprocess.cwd()instead of the session's stored directory. This causesSessionPrompt.loop(),Instance.state(), and downstream operations to fail silently — Hono'sstream()handler swallows exceptions and returns empty HTTP 200 responses.The fix wraps each affected route handler in
Instance.provide({ directory: session.directory })to re-enter the correct context before processing. SinceInstance.provide()caches by directory, there is zero overhead when the directory matches the current context.How did you verify your code works?
npx tsc --noEmit)?directory=still operate in the correct contextChecklist