@mdsohail99/opencode-agent-teams 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +64 -120
- package/CHANGELOG.md +158 -137
- package/README.md +165 -348
- package/agents/orchestrators/agent-teams.md +198 -198
- package/bin/cli.js +351 -351
- package/package.json +9 -4
- package/src/index.ts +115 -115
- package/src/state/tracker.ts +102 -102
- package/src/tools/broadcast.ts +58 -58
- package/src/tools/delegate.ts +154 -154
- package/src/tools/status.ts +25 -25
- package/src/tools/terminate.ts +70 -70
- package/src/tui.ts +9 -9
- package/src/worktree/manager.ts +142 -142
- package/agents/orchestrators-fork/orchestrator-agent-teams.md +0 -203
- package/bin/install.mjs +0 -418
- package/bin/lib/config-root.mjs +0 -51
- package/bin/lib/merge-commands.mjs +0 -138
- package/bin/lib/merge-env-flags.mjs +0 -92
- package/bin/lib/merge-schema.mjs +0 -77
- package/bin/lib/merge-subagent-depth.mjs +0 -133
- package/bin/lib/plugin-park.mjs +0 -48
- package/bin/lib/relay-process.mjs +0 -116
- package/bin/lib/runtime-state.mjs +0 -118
- package/bin/menu.mjs +0 -582
- package/bin/uninstall.mjs +0 -360
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
// Shared: configure OPENCODE_EXPERIMENTAL_BACKGROUND_SUBAGENTS=true by default
|
|
2
|
-
// so stock OpenCode users automatically get native background subagents (Ctrl+B)
|
|
3
|
-
// enabled out of the box without manual configuration.
|
|
4
|
-
|
|
5
|
-
import { spawnSync } from "node:child_process"
|
|
6
|
-
import { existsSync } from "node:fs"
|
|
7
|
-
import { readFile, writeFile } from "node:fs/promises"
|
|
8
|
-
import { join } from "node:path"
|
|
9
|
-
import { homedir } from "node:os"
|
|
10
|
-
|
|
11
|
-
const FLAG_NAME = "OPENCODE_EXPERIMENTAL_BACKGROUND_SUBAGENTS"
|
|
12
|
-
const EXPORT_LINE = `export ${FLAG_NAME}=true`
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Configure or revert OPENCODE_EXPERIMENTAL_BACKGROUND_SUBAGENTS in user environment.
|
|
16
|
-
* @param {(msg: string) => void} log
|
|
17
|
-
* @param {"full" | "agents" | "uninstall"} mode
|
|
18
|
-
*/
|
|
19
|
-
export async function mergeEnvFlags(log = console.log, mode = "full") {
|
|
20
|
-
const isWindows = process.platform === "win32"
|
|
21
|
-
|
|
22
|
-
if (mode === "full" || mode === "agents") {
|
|
23
|
-
process.env[FLAG_NAME] = "true"
|
|
24
|
-
|
|
25
|
-
if (isWindows) {
|
|
26
|
-
try {
|
|
27
|
-
spawnSync(
|
|
28
|
-
"powershell",
|
|
29
|
-
[
|
|
30
|
-
"-NoProfile",
|
|
31
|
-
"-Command",
|
|
32
|
-
`[Environment]::SetEnvironmentVariable("${FLAG_NAME}", "true", "User")`,
|
|
33
|
-
],
|
|
34
|
-
{ stdio: "ignore" },
|
|
35
|
-
)
|
|
36
|
-
log(` configured ${FLAG_NAME}=true in Windows user environment`)
|
|
37
|
-
} catch (err) {
|
|
38
|
-
log(` warning: could not set ${FLAG_NAME} in user environment: ${err.message}`)
|
|
39
|
-
}
|
|
40
|
-
} else {
|
|
41
|
-
// Unix: append to ~/.bashrc or ~/.zshrc if present
|
|
42
|
-
const home = homedir()
|
|
43
|
-
const targets = [join(home, ".bashrc"), join(home, ".zshrc")].filter(existsSync)
|
|
44
|
-
for (const target of targets) {
|
|
45
|
-
try {
|
|
46
|
-
const content = await readFile(target, "utf8")
|
|
47
|
-
if (!content.includes(FLAG_NAME)) {
|
|
48
|
-
await writeFile(target, `${content}\n# OpenCode Agent-Teams\n${EXPORT_LINE}\n`, "utf8")
|
|
49
|
-
log(` configured ${FLAG_NAME}=true in ${target}`)
|
|
50
|
-
}
|
|
51
|
-
} catch (err) {
|
|
52
|
-
log(` warning: could not update ${target}: ${err.message}`)
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
} else if (mode === "uninstall") {
|
|
57
|
-
if (isWindows) {
|
|
58
|
-
try {
|
|
59
|
-
spawnSync(
|
|
60
|
-
"powershell",
|
|
61
|
-
[
|
|
62
|
-
"-NoProfile",
|
|
63
|
-
"-Command",
|
|
64
|
-
`[Environment]::SetEnvironmentVariable("${FLAG_NAME}", $null, "User")`,
|
|
65
|
-
],
|
|
66
|
-
{ stdio: "ignore" },
|
|
67
|
-
)
|
|
68
|
-
log(` reverted ${FLAG_NAME} in Windows user environment`)
|
|
69
|
-
} catch (err) {
|
|
70
|
-
log(` warning: could not revert ${FLAG_NAME}: ${err.message}`)
|
|
71
|
-
}
|
|
72
|
-
} else {
|
|
73
|
-
const home = homedir()
|
|
74
|
-
const targets = [join(home, ".bashrc"), join(home, ".zshrc")].filter(existsSync)
|
|
75
|
-
for (const target of targets) {
|
|
76
|
-
try {
|
|
77
|
-
const content = await readFile(target, "utf8")
|
|
78
|
-
if (content.includes(FLAG_NAME)) {
|
|
79
|
-
const cleaned = content
|
|
80
|
-
.split("\n")
|
|
81
|
-
.filter((line) => !line.includes(FLAG_NAME) && !line.includes("# OpenCode Agent-Teams"))
|
|
82
|
-
.join("\n")
|
|
83
|
-
await writeFile(target, cleaned, "utf8")
|
|
84
|
-
log(` reverted ${FLAG_NAME} from ${target}`)
|
|
85
|
-
}
|
|
86
|
-
} catch (err) {
|
|
87
|
-
log(` warning: could not update ${target}: ${err.message}`)
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
package/bin/lib/merge-schema.mjs
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
// bin/lib/merge-schema.mjs
|
|
2
|
-
// Manages local schema.json and $schema reference in opencode.json
|
|
3
|
-
//
|
|
4
|
-
// In both full and agents mode:
|
|
5
|
-
// - Copies schema.json to <configRoot>/schema.json
|
|
6
|
-
// - Updates opencode.json to point "$schema": "./schema.json"
|
|
7
|
-
// This eliminates IDE warnings (e.g. "Property max_concurrent_agents is not allowed")
|
|
8
|
-
// while providing full autocomplete and tooltips in VS Code.
|
|
9
|
-
//
|
|
10
|
-
// On uninstall:
|
|
11
|
-
// - Removes <configRoot>/schema.json
|
|
12
|
-
// - Reverts "$schema": "https://opencode.ai/config.json" if opencode.json remains.
|
|
13
|
-
|
|
14
|
-
import { copyFileSync, existsSync, readFileSync, rmSync, writeFileSync } from "node:fs"
|
|
15
|
-
import { dirname, join } from "node:path"
|
|
16
|
-
import { fileURLToPath } from "node:url"
|
|
17
|
-
|
|
18
|
-
const packageRoot = join(dirname(fileURLToPath(import.meta.url)), "../..")
|
|
19
|
-
const SCHEMA_FILE = join(packageRoot, "schema.json")
|
|
20
|
-
const REMOTE_SCHEMA = "https://opencode.ai/config.json"
|
|
21
|
-
const LOCAL_SCHEMA = "./schema.json"
|
|
22
|
-
|
|
23
|
-
export function mergeSchema(configDir, log = console.log, mode = "full") {
|
|
24
|
-
const targetSchema = join(configDir, "schema.json")
|
|
25
|
-
const opencodeJsonPath = join(configDir, "opencode.json")
|
|
26
|
-
|
|
27
|
-
if (mode === "uninstall") {
|
|
28
|
-
if (existsSync(targetSchema)) {
|
|
29
|
-
try {
|
|
30
|
-
rmSync(targetSchema, { force: true })
|
|
31
|
-
log("removed schema.json from " + configDir)
|
|
32
|
-
} catch (e) {
|
|
33
|
-
log("warning: failed to remove schema.json: " + (e?.message || e))
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (existsSync(opencodeJsonPath)) {
|
|
38
|
-
try {
|
|
39
|
-
const raw = readFileSync(opencodeJsonPath, "utf-8").replace(/^\uFEFF/, "")
|
|
40
|
-
const parsed = JSON.parse(raw)
|
|
41
|
-
if (parsed.$schema === LOCAL_SCHEMA) {
|
|
42
|
-
parsed.$schema = REMOTE_SCHEMA
|
|
43
|
-
writeFileSync(opencodeJsonPath, JSON.stringify(parsed, null, 2) + "\n", "utf-8")
|
|
44
|
-
log("reverted $schema in opencode.json to " + REMOTE_SCHEMA)
|
|
45
|
-
}
|
|
46
|
-
} catch (e) {
|
|
47
|
-
log("warning: failed to revert $schema in opencode.json: " + (e?.message || e))
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
return
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// Install mode (full or agents)
|
|
54
|
-
if (existsSync(SCHEMA_FILE)) {
|
|
55
|
-
try {
|
|
56
|
-
copyFileSync(SCHEMA_FILE, targetSchema)
|
|
57
|
-
log("installed schema.json into " + configDir)
|
|
58
|
-
} catch (e) {
|
|
59
|
-
log("warning: failed to copy schema.json: " + (e?.message || e))
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (existsSync(opencodeJsonPath)) {
|
|
64
|
-
try {
|
|
65
|
-
const raw = readFileSync(opencodeJsonPath, "utf-8").replace(/^\uFEFF/, "")
|
|
66
|
-
const parsed = JSON.parse(raw)
|
|
67
|
-
if (!parsed.$schema || parsed.$schema === REMOTE_SCHEMA) {
|
|
68
|
-
parsed.$schema = LOCAL_SCHEMA
|
|
69
|
-
const ordered = { $schema: LOCAL_SCHEMA, ...parsed }
|
|
70
|
-
writeFileSync(opencodeJsonPath, JSON.stringify(ordered, null, 2) + "\n", "utf-8")
|
|
71
|
-
log("updated $schema in opencode.json to " + LOCAL_SCHEMA)
|
|
72
|
-
}
|
|
73
|
-
} catch (e) {
|
|
74
|
-
log("warning: failed to update $schema in opencode.json: " + (e?.message || e))
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
// Shared: merge the Agent-Teams `subagent_depth` and `max_concurrent_agents` defaults
|
|
2
|
-
// into the USER's global opencode config WITHOUT overwriting their existing keys
|
|
3
|
-
// (MCP servers, model, provider, custom values, etc.). Idempotent and additive-only:
|
|
4
|
-
// we never clobber a value the user already set, and we never remove keys we don't own.
|
|
5
|
-
//
|
|
6
|
-
// - `subagent_depth` and `max_concurrent_agents` are supported at root in BOTH
|
|
7
|
-
// `full` and `agents` modes (validated by local schema.json).
|
|
8
|
-
// In both modes, they default additively (subagent_depth=2, max_concurrent_agents=20)
|
|
9
|
-
// if absent, and migrate from `experimental.*` to root if misplaced.
|
|
10
|
-
// Existing user values are preserved; on `uninstall`, both keys are cleaned up.
|
|
11
|
-
import { readFile, writeFile } from "node:fs/promises"
|
|
12
|
-
import { existsSync } from "node:fs"
|
|
13
|
-
import { join } from "node:path"
|
|
14
|
-
|
|
15
|
-
export const SUBAGENT_DEPTH = 2
|
|
16
|
-
export const MAX_CONCURRENT_AGENTS = 20
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Apply subagent_depth and max_concurrent_agents (both modes)
|
|
20
|
-
* in `configRoot/opencode.json`, preserving every other key.
|
|
21
|
-
*/
|
|
22
|
-
export async function mergeSubagentDepth(configRoot, log = console.log, mode = "full") {
|
|
23
|
-
const configPath = configRoot.endsWith("opencode.json") ? configRoot : join(configRoot, "opencode.json")
|
|
24
|
-
let config = {}
|
|
25
|
-
let exists = false
|
|
26
|
-
|
|
27
|
-
if (existsSync(configPath)) {
|
|
28
|
-
exists = true
|
|
29
|
-
try {
|
|
30
|
-
config = JSON.parse((await readFile(configPath, "utf8")).replace(/^\uFEFF/, ""))
|
|
31
|
-
} catch (err) {
|
|
32
|
-
throw new Error(`Cannot parse ${configPath}; fix it before installing Agent-Teams: ${err.message}`)
|
|
33
|
-
}
|
|
34
|
-
} else {
|
|
35
|
-
if (mode === "uninstall") {
|
|
36
|
-
return config
|
|
37
|
-
}
|
|
38
|
-
config = { $schema: "https://opencode.ai/config.json" }
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (typeof config !== "object" || config === null || Array.isArray(config)) {
|
|
42
|
-
throw new Error(`${configPath} does not contain a JSON object; refusing to overwrite it.`)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
let changed = false
|
|
46
|
-
|
|
47
|
-
// ── 1. max_concurrent_agents (both full and agents mode) ──
|
|
48
|
-
if (mode === "full" || mode === "agents") {
|
|
49
|
-
// Migrate misplaced experimental.max_concurrent_agents to root if present
|
|
50
|
-
if (config.experimental && typeof config.experimental === "object" && !Array.isArray(config.experimental)) {
|
|
51
|
-
if (config.experimental.max_concurrent_agents !== undefined) {
|
|
52
|
-
if (config.max_concurrent_agents === undefined) {
|
|
53
|
-
config.max_concurrent_agents = config.experimental.max_concurrent_agents
|
|
54
|
-
}
|
|
55
|
-
delete config.experimental.max_concurrent_agents
|
|
56
|
-
if (Object.keys(config.experimental).length === 0) {
|
|
57
|
-
delete config.experimental
|
|
58
|
-
}
|
|
59
|
-
changed = true
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// Set root max_concurrent_agents = 20 additively if absent
|
|
64
|
-
if (config.max_concurrent_agents === undefined) {
|
|
65
|
-
config.max_concurrent_agents = MAX_CONCURRENT_AGENTS
|
|
66
|
-
if (config.$schema === undefined) config.$schema = "https://opencode.ai/config.json"
|
|
67
|
-
changed = true
|
|
68
|
-
log(` ${exists ? "updated" : "created"} ${configPath} with max_concurrent_agents=${MAX_CONCURRENT_AGENTS}`)
|
|
69
|
-
} else {
|
|
70
|
-
log(` max_concurrent_agents already set to ${config.max_concurrent_agents} in ${configPath} (kept)`)
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// ── 2. subagent_depth (both full and agents mode) ──
|
|
75
|
-
if (mode === "full" || mode === "agents") {
|
|
76
|
-
// Migrate misplaced experimental.subagent_depth back to root if present
|
|
77
|
-
if (config.experimental && typeof config.experimental === "object" && !Array.isArray(config.experimental)) {
|
|
78
|
-
if (config.experimental.subagent_depth !== undefined) {
|
|
79
|
-
if (config.subagent_depth === undefined) {
|
|
80
|
-
config.subagent_depth = config.experimental.subagent_depth
|
|
81
|
-
}
|
|
82
|
-
delete config.experimental.subagent_depth
|
|
83
|
-
if (Object.keys(config.experimental).length === 0) {
|
|
84
|
-
delete config.experimental
|
|
85
|
-
}
|
|
86
|
-
changed = true
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Ensure root subagent_depth exists additively
|
|
91
|
-
if (config.subagent_depth === undefined) {
|
|
92
|
-
config.subagent_depth = SUBAGENT_DEPTH
|
|
93
|
-
if (config.$schema === undefined) config.$schema = "https://opencode.ai/config.json"
|
|
94
|
-
changed = true
|
|
95
|
-
log(` ${exists ? "updated" : "created"} ${configPath} with subagent_depth=${SUBAGENT_DEPTH} (existing keys preserved)`)
|
|
96
|
-
} else {
|
|
97
|
-
log(` subagent_depth already set to ${config.subagent_depth} in ${configPath} (kept)`)
|
|
98
|
-
}
|
|
99
|
-
} else if (mode === "uninstall") {
|
|
100
|
-
// Clean up both root and experimental subagent_depth & max_concurrent_agents on uninstall
|
|
101
|
-
if (config.subagent_depth !== undefined) {
|
|
102
|
-
delete config.subagent_depth
|
|
103
|
-
changed = true
|
|
104
|
-
}
|
|
105
|
-
if (config.max_concurrent_agents !== undefined) {
|
|
106
|
-
delete config.max_concurrent_agents
|
|
107
|
-
changed = true
|
|
108
|
-
}
|
|
109
|
-
if (config.experimental && typeof config.experimental === "object" && !Array.isArray(config.experimental)) {
|
|
110
|
-
if (config.experimental.subagent_depth !== undefined) {
|
|
111
|
-
delete config.experimental.subagent_depth
|
|
112
|
-
changed = true
|
|
113
|
-
}
|
|
114
|
-
if (config.experimental.max_concurrent_agents !== undefined) {
|
|
115
|
-
delete config.experimental.max_concurrent_agents
|
|
116
|
-
changed = true
|
|
117
|
-
}
|
|
118
|
-
if (Object.keys(config.experimental).length === 0) {
|
|
119
|
-
delete config.experimental
|
|
120
|
-
changed = true
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
if (changed) {
|
|
124
|
-
log(` removed subagent_depth and max_concurrent_agents from ${configPath}`)
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (changed) {
|
|
129
|
-
await writeFile(configPath, `${JSON.stringify(config, null, 2)}\n`, "utf8")
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return config
|
|
133
|
-
}
|
package/bin/lib/plugin-park.mjs
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
// Shared "disabled plugin" helpers for Agent-Teams install/uninstall/menu.
|
|
2
|
-
//
|
|
3
|
-
// Native-fork (agents-only) mode installs into the SAME config root as full
|
|
4
|
-
// mode (~/.config/opencode) but DISABLES the plugin by parking it OUT of the
|
|
5
|
-
// autodiscovery directory. The core only scans `{plugin,plugins}/*.{ts,js}`
|
|
6
|
-
// under the config root, so a file parked in `plugins-disabled/` is never
|
|
7
|
-
// loaded — the native fork's compiled-in Task/next_agent/agents_status then
|
|
8
|
-
// run collision-free (Issue 1), while `full` mode re-enables it by copying it
|
|
9
|
-
// back into `plugins/`.
|
|
10
|
-
import { join } from "node:path"
|
|
11
|
-
import { existsSync, mkdirSync, renameSync, copyFileSync, rmSync } from "node:fs"
|
|
12
|
-
import { rmRetry } from "./relay-process.mjs"
|
|
13
|
-
|
|
14
|
-
export const DISABLED_PLUGIN_DIR = "plugins-disabled"
|
|
15
|
-
export const PARKED_PLUGIN_FILE = join(DISABLED_PLUGIN_DIR, "agent-teams.ts")
|
|
16
|
-
|
|
17
|
-
/** Park our plugin out of autodiscovery (agents-only mode). No-op when absent. */
|
|
18
|
-
export function parkPlugin(configRoot) {
|
|
19
|
-
const active = join(configRoot, "plugins", "agent-teams.ts")
|
|
20
|
-
const parked = join(configRoot, PARKED_PLUGIN_FILE)
|
|
21
|
-
if (!existsSync(active)) return false
|
|
22
|
-
mkdirSync(join(configRoot, DISABLED_PLUGIN_DIR), { recursive: true })
|
|
23
|
-
try {
|
|
24
|
-
renameSync(active, parked)
|
|
25
|
-
} catch {
|
|
26
|
-
// Locked/cross-volume rename fallback: copy then remove.
|
|
27
|
-
copyFileSync(active, parked)
|
|
28
|
-
try {
|
|
29
|
-
rmSync(active, { force: true })
|
|
30
|
-
} catch {
|
|
31
|
-
// Leave the active copy; the parked one still guarantees disablement.
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return true
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/** Remove the parked copy (full-mode installs / uninstall). Also drops the
|
|
38
|
-
* reserved dir when it becomes empty. */
|
|
39
|
-
export async function removeParkedPlugin(configRoot) {
|
|
40
|
-
const parkedDir = join(configRoot, DISABLED_PLUGIN_DIR)
|
|
41
|
-
const parked = join(parkedDir, "agent-teams.ts")
|
|
42
|
-
if (existsSync(parked)) await rmRetry(parked)
|
|
43
|
-
if (existsSync(parkedDir)) {
|
|
44
|
-
const { readdir } = await import("node:fs/promises")
|
|
45
|
-
const entries = await readdir(parkedDir).catch(() => [])
|
|
46
|
-
if (entries.length === 0) await rmRetry(parkedDir)
|
|
47
|
-
}
|
|
48
|
-
}
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import { execFileSync } from "node:child_process"
|
|
2
|
-
import { writeFileSync, rmSync } from "node:fs"
|
|
3
|
-
import { tmpdir } from "node:os"
|
|
4
|
-
import { join } from "node:path"
|
|
5
|
-
import { rm } from "node:fs/promises"
|
|
6
|
-
|
|
7
|
-
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
|
|
8
|
-
|
|
9
|
-
// Kill any node process running the agent-teams relay. The relay is spawned
|
|
10
|
-
// detached with cwd = the config relay directory, so on Windows it holds that
|
|
11
|
-
// directory open and blocks rmdir/rm during install/uninstall (EBUSY).
|
|
12
|
-
export async function killRelayProcesses() {
|
|
13
|
-
const pattern = "agent-teams-relay.mjs"
|
|
14
|
-
if (process.platform === "win32") {
|
|
15
|
-
const ps =
|
|
16
|
-
`Get-CimInstance Win32_Process -Filter "name='node.exe'" | ` +
|
|
17
|
-
`Where-Object { $_.CommandLine -match '${pattern}' } | ` +
|
|
18
|
-
`ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }`
|
|
19
|
-
try {
|
|
20
|
-
execFileSync("powershell.exe", ["-NoProfile", "-NonInteractive", "-Command", ps], {
|
|
21
|
-
stdio: "ignore",
|
|
22
|
-
timeout: 15000,
|
|
23
|
-
})
|
|
24
|
-
} catch {
|
|
25
|
-
// No relay processes found — nothing to kill.
|
|
26
|
-
}
|
|
27
|
-
} else {
|
|
28
|
-
try {
|
|
29
|
-
execFileSync("pkill", ["-f", pattern], { stdio: "ignore", timeout: 15000 })
|
|
30
|
-
} catch {
|
|
31
|
-
// No relay processes found — nothing to kill.
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
// Wait for port 25800 to fully release (TCP TIME_WAIT drain) before returning.
|
|
35
|
-
// A flat 500ms sleep was not enough on Windows — the supervisor fired concurrent
|
|
36
|
-
// respawn attempts into a port still in TIME_WAIT, each timing out and falling
|
|
37
|
-
// through to the aux-server fallback, producing the split-brain storm seen in
|
|
38
|
-
// server.log. Poll until the port is actually free, up to 5s.
|
|
39
|
-
const relayPort = Number(process.env.RELAY_PORT ?? process.env.AGENT_TEAMS_RELAY_PORT ?? 25800)
|
|
40
|
-
const deadline = Date.now() + 5000
|
|
41
|
-
while (Date.now() < deadline) {
|
|
42
|
-
let portFree = true
|
|
43
|
-
try {
|
|
44
|
-
if (process.platform === "win32") {
|
|
45
|
-
const out = execFileSync("netstat.exe", ["-ano"], { encoding: "utf8", timeout: 3000 })
|
|
46
|
-
portFree = !out.split(/\r?\n/).some((l) =>
|
|
47
|
-
l.match(new RegExp(`127\\.0\\.0\\.1:${relayPort}\\s+\\S+\\s+LISTENING`))
|
|
48
|
-
)
|
|
49
|
-
} else {
|
|
50
|
-
execFileSync("lsof", [`-tiTCP:${relayPort}`, "-sTCP:LISTEN"], { stdio: "ignore", timeout: 3000 })
|
|
51
|
-
portFree = false // lsof exit 0 = something listening
|
|
52
|
-
}
|
|
53
|
-
} catch {
|
|
54
|
-
portFree = true // lsof exit non-zero = nothing listening
|
|
55
|
-
}
|
|
56
|
-
if (portFree) break
|
|
57
|
-
await sleep(200)
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// Kill auxiliary `opencode serve` processes whose PARENT is gone (Windows).
|
|
62
|
-
// Aux servers are spawned detached and survive their terminal's death; their
|
|
63
|
-
// in-memory plugin then keeps respawning relays + state dirs after every
|
|
64
|
-
// install/uninstall (the "Removed N stale dirs" churn). Windows preserves the
|
|
65
|
-
// dead parent's PID, so the orphan test is exact: parent PID not found = kill.
|
|
66
|
-
//
|
|
67
|
-
// SAFETY — only these processes may be killed, nothing else:
|
|
68
|
-
// 1. process name must be exactly `opencode.exe` (the npm/IDE binary), and
|
|
69
|
-
// 2. the command line must have `serve` as an argument (a real server, not
|
|
70
|
-
// the TUI and not any other program), and
|
|
71
|
-
// 3. the parent PID must no longer exist (orphaned) — a live instance's
|
|
72
|
-
// server has a live parent and is NEVER touched.
|
|
73
|
-
// POSIX reparents orphans to init (ppid=1), which can't be distinguished from
|
|
74
|
-
// a deliberately daemonized server — there the plugin's own parentIsAlive()
|
|
75
|
-
// watchdog handles self-exit instead, so no sweep is needed.
|
|
76
|
-
export async function killOrphanedServeProcesses() {
|
|
77
|
-
if (process.platform !== "win32") return
|
|
78
|
-
// A temp .ps1 file + -File avoids Windows command-line quoting hell with
|
|
79
|
-
// embedded double quotes / $() (which broke -Command style).
|
|
80
|
-
const script = [
|
|
81
|
-
`Get-CimInstance Win32_Process -Filter "name='opencode.exe'" | ForEach-Object {`,
|
|
82
|
-
` if ($_.ParentProcessId -eq 0) { return }`,
|
|
83
|
-
` if ($_.CommandLine -notmatch ' serve(\\s|$)') { return }`,
|
|
84
|
-
` $par = Get-CimInstance Win32_Process -Filter "ProcessId=$($_.ParentProcessId)" -ErrorAction SilentlyContinue`,
|
|
85
|
-
` if (-not $par) { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }`,
|
|
86
|
-
`}`,
|
|
87
|
-
].join("\n")
|
|
88
|
-
const scriptPath = join(tmpdir(), `agent-teams-orphan-sweep-${process.pid}.ps1`)
|
|
89
|
-
writeFileSync(scriptPath, script)
|
|
90
|
-
try {
|
|
91
|
-
execFileSync(
|
|
92
|
-
"powershell.exe",
|
|
93
|
-
["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-File", scriptPath],
|
|
94
|
-
{ stdio: "ignore", timeout: 30000 },
|
|
95
|
-
)
|
|
96
|
-
} catch {
|
|
97
|
-
// No orphaned servers found — nothing to kill.
|
|
98
|
-
} finally {
|
|
99
|
-
rmSync(scriptPath, { force: true })
|
|
100
|
-
}
|
|
101
|
-
await sleep(500)
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// Remove a path, retrying on transient EBUSY/EPERM (Windows releases handles
|
|
105
|
-
// asynchronously after the owning process exits).
|
|
106
|
-
export async function rmRetry(target, retries = 5) {
|
|
107
|
-
for (let i = 0; i < retries; i++) {
|
|
108
|
-
try {
|
|
109
|
-
await rm(target, { recursive: true, force: true })
|
|
110
|
-
return
|
|
111
|
-
} catch (error) {
|
|
112
|
-
if (i === retries - 1) throw error
|
|
113
|
-
await sleep(300)
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
// Shared: locate and SAFELY remove Agent-Teams runtime state/token dirs.
|
|
2
|
-
//
|
|
3
|
-
// The plugin keeps one relay state dir per PORT under a single root
|
|
4
|
-
// (see plugins/agent-teams.ts stateRoot()/stateDir() and
|
|
5
|
-
// relay/agent-teams-relay.mjs persistToken()):
|
|
6
|
-
// root = AGENT_TEAMS_STATE_DIR
|
|
7
|
-
// | %LOCALAPPDATA%\opencode\agent-teams (Windows)
|
|
8
|
-
// | $HOME/.local/state/opencode/agent-teams (POSIX)
|
|
9
|
-
// root/<port>/token — shared-secret the relay writes and the plugin reads
|
|
10
|
-
// root/<port>/token.tmp — temp file during atomic persistToken()
|
|
11
|
-
// root/<port>/relay.log — relay stdout/stderr
|
|
12
|
-
// root/<port>/server.log — optional auxiliary opencode serve stdout/stderr
|
|
13
|
-
// root/<port>/agent-teams-relay.wal — append-only write-ahead log (durable hot path)
|
|
14
|
-
// root/<port>/agent-teams-relay-state.json — periodic atomic checkpoint
|
|
15
|
-
// root/<port>/agent-teams-relay.wal.tmp — transient temp during atomic WAL write (crash-scrub)
|
|
16
|
-
// root/<port>/agent-teams-relay-state.json.tmp — transient temp during atomic checkpoint write (crash-scrub)
|
|
17
|
-
//
|
|
18
|
-
// SAFETY (critical): we ONLY delete
|
|
19
|
-
// 1. numeric per-port directories whose contents are EXACTLY plugin-owned
|
|
20
|
-
// files from PLUGIN_STATE_FILES (or that are empty), and
|
|
21
|
-
// 2. stray plugin-named files sitting directly in the state root.
|
|
22
|
-
// Anything else — non-plugin files, foreign dirs, other users' data — is left
|
|
23
|
-
// untouched and skipped, so an uninstall can never delete user data that merely
|
|
24
|
-
// happens to live under the plugin's state root.
|
|
25
|
-
//
|
|
26
|
-
// Returns { root, removed, skipped } and never throws.
|
|
27
|
-
|
|
28
|
-
import { readdir, rm } from "node:fs/promises"
|
|
29
|
-
import { existsSync } from "node:fs"
|
|
30
|
-
import { join } from "node:path"
|
|
31
|
-
import { rmRetry } from "./relay-process.mjs"
|
|
32
|
-
|
|
33
|
-
// Filenames the relay/plugin may own, per port dir (see above). Includes the
|
|
34
|
-
// WAL + checkpoint durable store and the transient temp files that may briefly
|
|
35
|
-
// exist during atomic writes — an uninstall would otherwise skip every per-port
|
|
36
|
-
// dir containing a live relay store.
|
|
37
|
-
export const PLUGIN_STATE_FILES = new Set([
|
|
38
|
-
"token",
|
|
39
|
-
"token.tmp",
|
|
40
|
-
"relay.log",
|
|
41
|
-
"server.log",
|
|
42
|
-
"agent-teams-relay.wal",
|
|
43
|
-
"agent-teams-relay-state.json",
|
|
44
|
-
"agent-teams-relay.wal.tmp",
|
|
45
|
-
"agent-teams-relay-state.json.tmp",
|
|
46
|
-
])
|
|
47
|
-
|
|
48
|
-
// Mirrors plugins/agent-teams.ts stateRoot() exactly.
|
|
49
|
-
export function runtimeStateRoot() {
|
|
50
|
-
if (process.env.AGENT_TEAMS_STATE_DIR) return process.env.AGENT_TEAMS_STATE_DIR
|
|
51
|
-
if (process.env.LOCALAPPDATA) return join(process.env.LOCALAPPDATA, "opencode", "agent-teams")
|
|
52
|
-
if (process.env.HOME) return join(process.env.HOME, ".local", "state", "opencode", "agent-teams")
|
|
53
|
-
return null
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export const TRANSIENT_STATE_FILES = new Set([
|
|
57
|
-
"token",
|
|
58
|
-
"token.tmp",
|
|
59
|
-
"relay.log",
|
|
60
|
-
"server.log",
|
|
61
|
-
])
|
|
62
|
-
|
|
63
|
-
export async function removeRuntimeStateDirs(options = {}) {
|
|
64
|
-
const { removeDatabase = true } = options
|
|
65
|
-
const root = runtimeStateRoot()
|
|
66
|
-
if (!root || !existsSync(root)) return { root, removed: 0, skipped: 0 }
|
|
67
|
-
|
|
68
|
-
let removed = 0
|
|
69
|
-
let skipped = 0
|
|
70
|
-
let entries
|
|
71
|
-
try {
|
|
72
|
-
entries = await readdir(root, { withFileTypes: true })
|
|
73
|
-
} catch {
|
|
74
|
-
// Unreadable root — fail safe, report nothing removed.
|
|
75
|
-
return { root, removed, skipped }
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
for (const entry of entries) {
|
|
79
|
-
try {
|
|
80
|
-
if (entry.isDirectory()) {
|
|
81
|
-
if (!/^\d+$/.test(entry.name)) continue
|
|
82
|
-
const dir = join(root, entry.name)
|
|
83
|
-
const files = await readdir(dir)
|
|
84
|
-
if (removeDatabase) {
|
|
85
|
-
if (!files.every((f) => PLUGIN_STATE_FILES.has(f))) {
|
|
86
|
-
skipped++
|
|
87
|
-
continue
|
|
88
|
-
}
|
|
89
|
-
await rmRetry(dir)
|
|
90
|
-
removed++
|
|
91
|
-
} else {
|
|
92
|
-
for (const file of files) {
|
|
93
|
-
if (TRANSIENT_STATE_FILES.has(file)) {
|
|
94
|
-
await rmRetry(join(dir, file))
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
} else if (PLUGIN_STATE_FILES.has(entry.name)) {
|
|
99
|
-
await rmRetry(join(root, entry.name))
|
|
100
|
-
removed++
|
|
101
|
-
}
|
|
102
|
-
} catch {
|
|
103
|
-
skipped++
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// If the root is now empty it was purely plugin-created shells — remove it
|
|
108
|
-
// so `LOCALAPPDATA\opencode\agent-teams` does not linger with a stale token
|
|
109
|
-
// mtime. When anything non-plugin remains, the root stays.
|
|
110
|
-
try {
|
|
111
|
-
const after = await readdir(root)
|
|
112
|
-
if (after.length === 0) await rm(root, { recursive: true, force: true })
|
|
113
|
-
} catch {
|
|
114
|
-
/* non-fatal */
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return { root, removed, skipped }
|
|
118
|
-
}
|