Queue daemon PM agent context drift — agent operates in wrong directory #156

Closed
opened 2026-04-05 22:21:15 +02:00 by shoko · 0 comments
Owner

Summary

The kugetsu queue daemon uses opencode run --continue --session $pm_session which causes the PM agent to operate in the wrong working directory — the daemon process CWD (~/.kugetsu/) instead of the target repository worktree. This results in zero commits after 6+ hours of processing.
Replacing --continue with --fork alone does not fix this. The fix requires --fork combined with --dir <worktree_path>.

Root Cause

The daemon command at line 65 of kugetsu-queue-daemon.sh:

nohup bash -c "export GITEA_TOKEN=***; opencode run '$message' --continue --session '$pm_session'" >> '$log_file' 2>&1 &

Problems:

  1. No --dir flag — opencode uses the daemon process CWD, not the session directory
  2. No --fork flag — shared session has no per-task isolation
  3. No worktree path computationissue_ref_to_worktree_path is never called
  4. GITEA_TOKEN=*** — the token is literally ***, agent has no token

From SQLite evidence (~/.local/share/opencode/opencode.db):

PM agent session: directory = /home/shoko/.kugetsu-worktrees
When daemon calls `opencode run --continue --session $pm_session` (no --dir):
opencode uses daemon process CWD (~/.kugetsu/), NOT the session directory

Delegation logs confirm the agent operates in ~/.kugetsu/ and ~/.kugetsu/repositories/kugetsu/, processing kugetsu's own issues instead of target repos.

Why --fork alone does NOT fix it

From opencode run --help:

--fork   fork the session before continuing (requires --continue or --session)
--dir    directory to run in

When --fork --session $pm_session is used:

  1. opencode creates a new session with parent_id set
  2. The new session inherits the parent's directory from SQLite
  3. Without --dir, the forked session still has directory = /home/shoko/.kugetsu-worktrees — wrong

Proof from kugetsu start (which works correctly):

(cd "$worktree_path" && opencode run "$full_message" --fork --session "$base_session_id" --dir "$worktree_path") | tee "$fork_log" &

--fork + --dir + cd all needed together.

Concrete Fix

Before:
nohup bash -c "export GITEA_TOKEN=***; opencode run '$message' --continue --session '$pm_session'" >> '$log_file' 2>&1 &

After:
worktree_path=$(issue_ref_to_worktree_path "$issue_ref" "$HOME/.kugetsu-worktrees")
(cd "$worktree_path" && nohup bash -c "export GITEA_TOKEN=$GITEA_TOKEN; opencode run '$message' --fork --session '$pm_session' --dir '$worktree_path'" >> '$log_file' 2>&1) &

Additional Bugs to Fix

  1. Forked session completion detection — With --fork, parent pm_session never ends. Detection must check if forked session ended, not parent.
  2. GITEA_TOKEN — Must use actual token value, not ***. The load_agent_env function exists but is not called before task launch.
  3. Worktree existence — Queue items may not have pre-created worktrees. Daemon should create them.

Files Involved

  • ~/.local/bin/kugetsu-queue-daemon.sh (installed daemon, line 68)
  • ~/.hermes/skills/kugetsu/scripts/kugetsu-queue-daemon.sh (repository version, line 65)
  • ~/.local/bin/kugetsu-worktree.sh — provides issue_ref_to_worktree_path
  • opencode db at ~/.local/share/opencode/opencode.db
## Summary The kugetsu queue daemon uses `opencode run --continue --session $pm_session` which causes the PM agent to operate in the **wrong working directory** — the daemon process CWD (`~/.kugetsu/`) instead of the target repository worktree. This results in zero commits after 6+ hours of processing. Replacing `--continue` with `--fork` alone does **not** fix this. The fix requires **`--fork` combined with `--dir <worktree_path>`**. ## Root Cause The daemon command at line 65 of `kugetsu-queue-daemon.sh`: nohup bash -c "export GITEA_TOKEN=***; opencode run '$message' --continue --session '$pm_session'" >> '$log_file' 2>&1 & **Problems:** 1. **No `--dir` flag** — opencode uses the daemon process CWD, not the session directory 2. **No `--fork` flag** — shared session has no per-task isolation 3. **No worktree path computation** — `issue_ref_to_worktree_path` is never called 4. **`GITEA_TOKEN=***`** — the token is literally `***`, agent has no token From SQLite evidence (`~/.local/share/opencode/opencode.db`): PM agent session: directory = /home/shoko/.kugetsu-worktrees When daemon calls `opencode run --continue --session $pm_session` (no --dir): opencode uses daemon process CWD (~/.kugetsu/), NOT the session directory Delegation logs confirm the agent operates in `~/.kugetsu/` and `~/.kugetsu/repositories/kugetsu/`, processing kugetsu's own issues instead of target repos. ## Why `--fork` alone does NOT fix it From `opencode run --help`: --fork fork the session before continuing (requires --continue or --session) --dir directory to run in When `--fork --session $pm_session` is used: 1. opencode creates a new session with `parent_id` set 2. The new session inherits the **parent's `directory`** from SQLite 3. Without `--dir`, the forked session still has `directory = /home/shoko/.kugetsu-worktrees` — wrong **Proof** from `kugetsu start` (which works correctly): (cd "$worktree_path" && opencode run "$full_message" --fork --session "$base_session_id" --dir "$worktree_path") | tee "$fork_log" & `--fork` + `--dir` + `cd` all needed together. ## Concrete Fix **Before:** nohup bash -c "export GITEA_TOKEN=***; opencode run '$message' --continue --session '$pm_session'" >> '$log_file' 2>&1 & **After:** worktree_path=$(issue_ref_to_worktree_path "$issue_ref" "$HOME/.kugetsu-worktrees") (cd "$worktree_path" && nohup bash -c "export GITEA_TOKEN=$GITEA_TOKEN; opencode run '$message' --fork --session '$pm_session' --dir '$worktree_path'" >> '$log_file' 2>&1) & ## Additional Bugs to Fix 1. **Forked session completion detection** — With `--fork`, parent `pm_session` never ends. Detection must check if **forked session** ended, not parent. 2. **GITEA_TOKEN** — Must use actual token value, not `***`. The `load_agent_env` function exists but is not called before task launch. 3. **Worktree existence** — Queue items may not have pre-created worktrees. Daemon should create them. ## Files Involved - `~/.local/bin/kugetsu-queue-daemon.sh` (installed daemon, line 68) - `~/.hermes/skills/kugetsu/scripts/kugetsu-queue-daemon.sh` (repository version, line 65) - `~/.local/bin/kugetsu-worktree.sh` — provides `issue_ref_to_worktree_path` - `opencode db` at `~/.local/share/opencode/opencode.db`
shoko added the highbug labels 2026-04-05 22:22:43 +02:00
shoko closed this issue 2026-04-05 23:39:35 +02:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: shoko/kugetsu#156