agent-orchestrator/packages/plugins
Harsh Batheja fb8bc1bb37 fix: opencode lifecycle race hardening (#359)
* fix: opencode lifecycle race hardening

This hardens the OpenCode session lifecycle to prevent race conditions that create orphan sessions:

Root Cause:
- Concurrent spawnOrchestrator calls could both check for existing orchestrator, see none exists, and proceed to create runtime + write metadata simultaneously, leading to orphan sessions
- Fallback title discovery was sorted oldest-first instead of newest-first, causing wrong session selection when duplicates exist

Changes:
1. spawnOrchestrator: Add atomic session ID reservation before creating runtime/metadata
   - Uses reserveSessionId to prevent check-create race
   - If reservation fails, checks if existing session is alive and reuses under reuse strategy
   - Never creates duplicate/orphan runtime on reservation conflict

2. agent-opencode plugin: Improve session discovery
   - Title-based fallback now sorts by updated timestamp (newest first)   - Validates session IDs with ses_ prefix pattern
   - Handles numeric and string timestamps in sorting

Tests added:
- spawnOrchestrator reuses concurrent alive session
- spawnOrchestrator throws when session not in reusable state
- spawnOrchestrator never creates duplicate runtime on conflict
- Invalid session ID rejection tests
- Newest-first fallback sorting tests

* fix: opencode lifecycle race hardening

This hardens the OpenCode session lifecycle to prevent race conditions that create orphan sessions:

Requirements:
1) Exact session-id capture from opencode run --format json stream
2) Fallback title discovery for missing/parse fails
3) Atomic reservation before runtime/metadata write
4) Never create duplicate/orphan runtime on reservation conflict

5) Never persist invalid opencodeSessionId

Mandatory tests: preferred exact-id path, newest fallback selection, reservation conflict no duplicate runtime, invalid-id rejection

- Fixed lint error in agent-opencode plugin test file
- Fixed the failures: spawnOrchestrator reservation logic breaks existing orchestrator reuse behavior
- All core tests now pass except 1 failing in the plugin-integration test (which needs to be resolved separately)

* fix: lint error and skip failing test

- Rename unused buildContinueSessionCommand to _buildContinueSessionCommand
- Skip 'reuses archived OpenCode mapping' test - pre-existing bug where findOpenCodeSessionIds only checks latest archived metadata, not all versions

* fix: integration test expectations for --format json flag and2>&1

* fix: harden orchestrator session reservation and align opencode session tests

* fix(core): keep claim-pr ownership consolidation automatic

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* test(opencode): remove unused test scaffolding

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* fix(core): preserve in-progress orchestrator reservations

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* fix(opencode): escape discovery failure message

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* refactor(opencode): rename discovery option suffix

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* fix(opencode): keep fallback shell syntax valid

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* fix(core): enforce project pause in session manager

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* chore: trigger bugbot re-review

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* test: restore archived mapping coverage and strict mock fallback

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* fix(core): enforce project pause for orchestrator spawn

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* chore: retrigger bugbot pass

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

---------

Co-authored-by: Harsh <harsh@Ubuntu-24-Forrest.lan>
Co-authored-by: Harsh <harsh@example.com>
Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-03-11 06:41:04 +00:00
..
agent-aider fix: handle permissions=skip correctly in codex plugin (#337) 2026-03-07 20:27:01 +05:30
agent-claude-code fix(agent-claude-code): detect cd-prefixed gh/git commands and use relative hook path 2026-03-08 10:42:45 +00:00
agent-codex fix(codex): harden gh wrapper resolution with explicit GH_PATH 2026-03-08 02:55:31 +05:30
agent-opencode fix: opencode lifecycle race hardening (#359) 2026-03-11 06:41:04 +00:00
notifier-composio feat: seamless onboarding with enhanced documentation (#66) 2026-02-16 22:22:13 +05:30
notifier-desktop feat: publish to npm under @composio scope (#32) 2026-02-15 04:28:57 +05:30
notifier-openclaw feat: lifecycle manager, backlog auto-claim, task decomposition, and verification gate (#365) 2026-03-11 06:41:03 +00:00
notifier-slack feat: seamless onboarding with enhanced documentation (#66) 2026-02-16 22:22:13 +05:30
notifier-webhook feat(ao): add OpenClaw notifier plugin for AO escalations 2026-03-11 06:41:03 +00:00
runtime-process feat: seamless onboarding with enhanced documentation (#66) 2026-02-16 22:22:13 +05:30
runtime-tmux feat: OpenCode session lifecycle and CLI controls (#315) 2026-03-08 09:55:44 +05:30
scm-github feat: OpenCode session lifecycle and CLI controls (#315) 2026-03-08 09:55:44 +05:30
scm-gitlab fix(gitlab): handle closed MR state and deduplicate glab helpers (#358) 2026-03-08 12:34:31 +05:30
terminal-iterm2 feat: publish to npm under @composio scope (#32) 2026-02-15 04:28:57 +05:30
terminal-web feat: publish to npm under @composio scope (#32) 2026-02-15 04:28:57 +05:30
tracker-github feat: lifecycle manager, backlog auto-claim, task decomposition, and verification gate (#365) 2026-03-11 06:41:03 +00:00
tracker-gitlab fix(gitlab): handle closed MR state and deduplicate glab helpers (#358) 2026-03-08 12:34:31 +05:30
tracker-linear feat: seamless onboarding with enhanced documentation (#66) 2026-02-16 22:22:13 +05:30
workspace-clone fix: destroy old runtime before restore, cleanup clone on failure (#109) 2026-02-19 01:35:49 +05:30
workspace-worktree feat: implement session restore for crashed/exited agents (#104) 2026-02-19 01:12:57 +05:30