feat: group-scoped workdir + init/destroy lifecycle scripts - #1
Merged
Conversation
…scripts
Engine.handleEvent now accepts an optional GroupConfig (forwarded by the router via x-ework-group-config header, parsed in server.ts). When a workdirTemplate is present, resolveWorkdir substitutes {owner}/{repo}/{issue}/{session} and uses it instead of the default baseWorkdir path. initScript runs in the workdir before each opencode spawn (execProcess pre-spawn). destroyScript runs on issue close (handleClosed). envInitScript is accepted but not yet executed (placeholder). Scripts run via bash with the workdir as cwd; failures are logged but never block the main flow.
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)
Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
…header parsing Extracts resolveTemplatedWorkdir and runHookScript as exported standalone functions (previously private Engine methods) and exports parseGroupConfigHeader from server.ts so they are unit-testable. Also tightens the owner/repo fallback to use || instead of ?? so empty-string scope parts correctly fall back to 'default'. 16 tests covering: all 4 template variables, scopeKey-part fallback, homedir expansion, unknown placeholders, base64 round-trip, malformed input rejection, script cwd, workdir auto-create, and never-throws-on-failure invariant. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
Addresses CRITICAL+HIGH+MEDIUM findings from dual-agent review:
H2+H3 (HIGH): runHookScript switched from Bun.spawnSync (blocks event loop) to async Bun.spawn with a 60s hard timeout (SIGKILL). A hung initScript no longer freezes the entire daemon (heartbeats, stdout draining, other webhooks).
H4 (HIGH): handleClosed no longer calls resolveWorkdir (which triggers git clone on empty dirs). New workdirPathFor computes the path without side effects; destroyScript only runs on workdirs that already exist (existsSync check).
M1 (MEDIUM): TOCTOU on groupConfigs Map — handleClosed now uses compare-and-delete (only deletes if the map value is still the same gc reference, so a concurrent handleEvent.set isn't clobbered).
M2 (MEDIUM): handleClosed early-returns if issue.state === 'closed' — duplicate close webhooks (Gitea redelivers) no longer re-run destroyScript.
M3 (MEDIUM): destroyScript dedupes workdirs via Set before looping — a template without {session} no longer runs destroy N times on the same dir.
M5 (MEDIUM): hookEnvFor injects EWORK_OWNER/EWORK_REPO/EWORK_ISSUE/EWORK_SESSION/EWORK_WORKDIR into the script environment. The web UI placeholder now shows real env var names instead of the non-existent ${CLONE_URL}.
L4 (LOW): resolveTemplatedWorkdir uses replacer functions (() => value) instead of raw strings — prevents $&/$1 interpretation in String.replace.
L5 (LOW): relative templates are now anchored to baseWorkdir (matching RecloneStrategy behavior). The baseWorkdir param is threaded from resolveWorkdir.
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)
Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of the daemon workdir refactor (cross-repo: router + daemon + web).
server.tsreads thex-ework-group-configheader (base64 JSON) and passes it toengine.handleEvent(event, groupConfig)opencode.tsEngine stores the config keyed by issue;resolveWorkdiruses the template ({owner}/{repo}/{issue}/{session}) when present, falling back to the defaultbaseWorkdirpathexecProcessrunsinitScriptin the workdir before each opencode spawnhandleClosedrunsdestroyScripton issue close, then clears the stored configenvInitScriptis accepted but not executed (placeholder per spec)bash -cwith the workdir as cwd; failures are logged (warn) but never block the main flow