Skip to content
This repository was archived by the owner on Aug 17, 2026. It is now read-only.

Commit 17f2d4c

Browse files
committed
feat(grunt/tui): --peer-id flag on attach + TUI (anomalyco#222 patch 2a) (#3)
Adds a CLI flag that sets the OPENCODE_PEER_ID env var early in the process lifecycle. sanitizedProcessEnv() already propagates env to MCP children + workers, so any coordination MCP loaded into the session can read process.env.OPENCODE_PEER_ID to know its tab identity without asking the agent via a TAKEOFF prompt dance. Plumbing only. Patch 2d (auto-announce-at-session-start) is what actually USES this to call hivemind_announce automatically. ## Why Today's hivemind-mcp coordination flow has the agent call hivemind_announce({ id: <peer-id>, ... }) on its first turn. The peer-id has to be passed in via the prompt because the agent has no other way to know which terminal it's running in. With this flag, launch scripts can set tab identity at start, and patch 2d will auto-announce so the TAKEOFF dance disappears entirely. ## Surface - New constant OPENCODE_PEER_ID in @opencode-ai/core/util/opencode-process - New helper setPeerID() in same module — guards empty strings - --peer-id <string> flag on attach + TUI commands (not serve; headless serve has no terminal/tab identity) - Reads from $OPENCODE_PEER_ID env if the flag is not given (yargs picks this up automatically with the name 'peer-id' → 'peerId') Refs hivemind anomalyco#222 Phase 2 patch 2a.
1 parent 4d0f65f commit 17f2d4c

4 files changed

Lines changed: 28 additions & 2 deletions

File tree

‎bun.lock‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/core/src/util/opencode-process.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
export const OPENCODE_RUN_ID = "OPENCODE_RUN_ID"
22
export const OPENCODE_PROCESS_ROLE = "OPENCODE_PROCESS_ROLE"
33

4+
// Stable identifier for the human-facing terminal/tab this session lives in.
5+
// Set via the --peer-id CLI flag or the OPENCODE_PEER_ID env var; propagated to
6+
// MCP children via sanitizedProcessEnv so coordination MCPs (e.g. hivemind-mcp)
7+
// can use it as the peer-id without a TAKEOFF dance.
8+
export const OPENCODE_PEER_ID = "OPENCODE_PEER_ID"
9+
10+
export function setPeerID(peerID: string | undefined) {
11+
if (peerID && peerID.length > 0) process.env[OPENCODE_PEER_ID] = peerID
12+
}
13+
414
export function ensureRunID() {
515
return (process.env[OPENCODE_RUN_ID] ??= crypto.randomUUID())
616
}

‎packages/opencode/src/cli/cmd/tui/attach.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { ServerAuth } from "@/server/auth"
88
import { createOpencodeClient } from "@opencode-ai/sdk/v2"
99
import type { GlobalEvent } from "@opencode-ai/sdk/v2"
1010
import type { EventSource } from "./context/sdk"
11+
import { setPeerID } from "@opencode-ai/core/util/opencode-process"
1112

1213
// Subscribe to the server's /global/event SSE stream so externally-triggered
1314
// session activity (e.g. wakes via POST /session/<id>/prompt_async from another
@@ -83,12 +84,19 @@ export const AttachCommand = cmd({
8384
alias: ["u"],
8485
type: "string",
8586
describe: "basic auth username (defaults to OPENCODE_SERVER_USERNAME or 'opencode')",
87+
})
88+
.option("peer-id", {
89+
type: "string",
90+
describe:
91+
"stable identifier for this terminal/tab (also via OPENCODE_PEER_ID env). Propagated to MCP children for coordination MCPs to read.",
8692
}),
8793
handler: async (args) => {
8894
const unguard = win32InstallCtrlCGuard()
8995
try {
9096
win32DisableProcessedInput()
9197

98+
setPeerID(args["peer-id"])
99+
92100
if (args.fork && !args.continue && !args.session) {
93101
UI.error("--fork requires --continue or --session")
94102
process.exitCode = 1

‎packages/opencode/src/cli/cmd/tui/thread.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
OPENCODE_RUN_ID,
2020
ensureRunID,
2121
sanitizedProcessEnv,
22+
setPeerID,
2223
} from "@opencode-ai/core/util/opencode-process"
2324
import { validateSession } from "./validate-session"
2425

@@ -111,6 +112,11 @@ export const TuiThreadCommand = cmd({
111112
.option("agent", {
112113
type: "string",
113114
describe: "agent to use",
115+
})
116+
.option("peer-id", {
117+
type: "string",
118+
describe:
119+
"stable identifier for this terminal/tab (also via OPENCODE_PEER_ID env). Propagated to MCP children for coordination MCPs to read.",
114120
}),
115121
handler: async (args) => {
116122
// Keep ENABLE_PROCESSED_INPUT cleared even if other code flips it.
@@ -121,6 +127,8 @@ export const TuiThreadCommand = cmd({
121127
// spawn or async work so the OS cannot kill the process group.
122128
win32DisableProcessedInput()
123129

130+
setPeerID(args["peer-id"])
131+
124132
if (args.fork && !args.continue && !args.session) {
125133
UI.error("--fork requires --continue or --session")
126134
process.exitCode = 1

0 commit comments

Comments
 (0)