Skip to content

maxchen32/opencode-move-session-ps

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

opencode-move-session-ps

A /move-session slash command for opencode on Windows/PowerShell 5.

It moves the current session to another directory by updating opencode's session rows directly, so the session becomes discoverable from the new location.

中文README: README.zh-CN.md

Usage

/move-session <target>
/move-session --debug <target>

Examples:

/move-session E:\repo\subdir
/move-session ..\sibling
/move-session .\child-folder
/move-session --debug ..\other-folder

<target> can be:

  • An absolute Windows path like E:\repo\subdir
  • A ~/... path
  • A relative path like ..\sibling or .\child
  • The feature to specify the target directory via natural language was removed. It was taken out to simplify debugging. I don't have time to add it back.

What It Updates

The command updates the root session and all descendant subagent sessions.

Fields updated in session:

  • project_id
  • directory
  • path
  • time_updated

Project Rules

The current behavior:

  1. global -> global: allowed
  2. global -> git: allowed
  3. git -> same git project: allowed
  4. gitA -> gitB: allowed, but downgraded to global

That fourth case is important: the session is not rebound to another unrelated git project by default. Instead, it is moved as a plain directory-scoped session.

When a target directory is a git repository, the script resolves its project identity using opencode's precedence:

  1. origin remote, normalized and hashed as sha1("git-remote:<host/path>")
  2. .git/opencode cached id
  3. root commit hash
  4. fallback to global

If the target git repository has a non-global id but is missing from opencode's project table, the script registers it automatically.

Path Rules

Windows path handling was the main source of bugs during development.

The final rules are:

  • Stored directory stays a normalized Windows path like E:/test/testfolder2
  • Stored path must match how opencode actually filters sessions

For global sessions on Windows, that means:

  • directory = E:/test/testfolder2
  • path = test/testfolder2

Not:

  • path = E:/test/testfolder2

For git sessions, path is relative to the git worktree root:

  • repo root -> ""
  • repo subdir -> subdir/nested

Debugging

By default the command is quiet and prints only one of:

  • moved: ...
  • noop: already at ...
  • ERR: ...

To enable diagnostics:

/move-session --debug <target>

That prints DEBUG: lines with values such as:

  • ROOT_SESSION
  • TARGET
  • NEW_PID
  • PROJ_WORKTREE
  • REL_PATH
  • OLD_PROJECT
  • DOWNGRADED_TO_GLOBAL

How It Is Wired

File Role
plugins/session-env.js Injects OPENCODE_SESSION_ID, OPENCODE_PROJECT_ID, OPENCODE_WORKTREE, OPENCODE_DIRECTORY, and OPENCODE_CALL_ID into shell commands.
commands/move-session.md Slash command wrapper that passes the raw user argument through to the PowerShell script.
commands/move-session.ps1 Main move logic, path normalization, git detection, and direct SQLite updates.
plugins/move-session-auto-undo.js Deferred cleanup on next opencode startup.
install.ps1 Windows installer.

Why This Exists

opencode session discovery is not based only on directory.

The session list is filtered primarily by:

  • project_id
  • and then path or directory, depending on the caller

So simply editing directory is not enough. The session has to be rewritten in a way that matches opencode's real lookup rules.

PowerShell 5 Notes

This repo is written around Windows PowerShell 5 behavior.

Important implementation details:

  • Avoid nested quoting through powershell -Command by passing the target through an environment variable
  • Avoid native stderr causing terminating errors under $ErrorActionPreference = 'Stop' by using cmd /c "git ... 2>nul" where needed

Install

One-liner (Windows / PowerShell 5)

(Not tested.)

iex ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/maxchen32/opencode-move-session-ps/main/install.ps1'))

Manual

$dest = "$env:USERPROFILE\.config\opencode"
New-Item -ItemType Directory -Path "$dest\plugins", "$dest\commands" -Force | Out-Null
Copy-Item .\plugins\*.js   "$dest\plugins\"
Copy-Item .\commands\*.md  "$dest\commands\"
Copy-Item .\commands\*.ps1 "$dest\commands\"

Then restart opencode.

Caveats

  • Existing opencode instances may keep old in-memory context until you switch or reopen
  • This command writes directly to opencode.db
  • If opencode changes session/project schema or filtering rules, this plugin may need updating
  • Cross-git moves intentionally lose git project binding by degrading to global

License

MIT — see LICENSE.

About

A /move-session slash command for opencode that reassigns a chat session to a different project directory and cleans up after itself. This version is for Windows/PowerShell

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages