patchcord 0.5.65 → 0.5.66

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/bin/patchcord.mjs CHANGED
@@ -79,7 +79,7 @@ Usage:
79
79
  patchcord agents List every agent (with whoami)
80
80
  patchcord agents <name> Show one agent's whoami
81
81
  patchcord upload <file> [--mime <type>] [--as <name>] Upload a file as a patchcord attachment (prints the storage path)
82
- patchcord subscribe Start the realtime listener
82
+ patchcord subscribe [interval] Start the realtime listener (Kimi: background poll, default 30s)
83
83
  patchcord update Update to the latest version
84
84
  patchcord --version Show installed version
85
85
  patchcord --help Show this help
@@ -463,12 +463,30 @@ if (cmd === "upload") {
463
463
  // env vars so subscribe.mjs works regardless of which MCP client is
464
464
  // running — OpenCode, Codex, Cursor, etc. — not just Claude Code.
465
465
  if (cmd === "subscribe") {
466
+ const bearerInfo = await _resolveBearer();
467
+
468
+ // Kimi CLI uses polling instead of WebSocket realtime
469
+ if (bearerInfo?.tool === "kimi") {
470
+ const kimiSubScript = join(HOME, ".kimi", "patchcord-subscribe.sh");
471
+ if (!existsSync(kimiSubScript)) {
472
+ console.error(`Kimi subscribe script not found at ${kimiSubScript}`);
473
+ console.error(`Run npx patchcord@latest to install it.`);
474
+ process.exit(1);
475
+ }
476
+ const { spawnSync } = await import("child_process");
477
+ const intervalArg = process.argv[3] || "30";
478
+ const result = spawnSync("bash", [kimiSubScript, intervalArg], {
479
+ stdio: "inherit",
480
+ env: { ...process.env, PATH: process.env.PATH || "/usr/local/bin:/usr/bin:/bin" },
481
+ });
482
+ process.exit(result.status ?? (result.signal ? 1 : 0));
483
+ }
484
+
466
485
  const subscribeScript = join(pluginRoot, "scripts", "subscribe.mjs");
467
486
  if (!existsSync(subscribeScript)) {
468
487
  console.error(`subscribe.mjs not found at ${subscribeScript}`);
469
488
  process.exit(1);
470
489
  }
471
- const bearerInfo = await _resolveBearer();
472
490
  const spawnEnv = { ...process.env };
473
491
  if (bearerInfo) {
474
492
  spawnEnv.PATCHCORD_BASE_URL = bearerInfo.baseUrl;
@@ -955,15 +973,52 @@ if (!cmd || cmd === "install" || cmd === "agent" || cmd?.startsWith("--")) {
955
973
  let kimiChanged = false;
956
974
  if (!existsSync(kimiSkillDir)) {
957
975
  mkdirSync(kimiSkillDir, { recursive: true });
958
- cpSync(join(pluginRoot, "skills", "inbox", "SKILL.md"), join(kimiSkillDir, "SKILL.md"));
976
+ cpSync(join(pluginRoot, "per-project-skills", "kimi", "SKILL.md"), join(kimiSkillDir, "SKILL.md"));
959
977
  kimiChanged = true;
960
978
  }
961
979
  if (!existsSync(kimiWaitDir)) {
962
980
  mkdirSync(kimiWaitDir, { recursive: true });
963
- cpSync(join(pluginRoot, "skills", "wait", "SKILL.md"), join(kimiWaitDir, "SKILL.md"));
981
+ cpSync(join(pluginRoot, "per-project-skills", "kimi", "wait", "SKILL.md"), join(kimiWaitDir, "SKILL.md"));
964
982
  kimiChanged = true;
965
983
  }
966
984
  if (kimiChanged) globalChanges.push("Kimi CLI skills installed");
985
+
986
+ // Install/update stop hook — fires after each Kimi turn to check inbox
987
+ const kimiHookSrc = join(pluginRoot, "scripts", "kimi-stop-hook.sh");
988
+ const kimiHookDest = join(HOME, ".kimi", "patchcord-stop-hook.sh");
989
+ if (existsSync(kimiHookSrc)) {
990
+ const hookAlreadyExisted = existsSync(kimiHookDest);
991
+ copyFileSync(kimiHookSrc, kimiHookDest);
992
+ chmodSync(kimiHookDest, 0o755);
993
+
994
+ const kimiConfigPath = join(HOME, ".kimi", "config.toml");
995
+ let kimiConfig = existsSync(kimiConfigPath) ? readFileSync(kimiConfigPath, "utf-8") : "";
996
+
997
+ // Remove old inline hooks = [] (conflicts with [[hooks]] array-of-tables)
998
+ kimiConfig = kimiConfig.replace(/^hooks\s*=\s*\[\]\n?/gm, "");
999
+
1000
+ // Remove existing patchcord stop hook blocks
1001
+ const segments = kimiConfig.split("[[hooks]]");
1002
+ const kept = segments.filter((seg, idx) => idx === 0 || !seg.includes("patchcord-stop-hook"));
1003
+ kimiConfig = kept.join("[[hooks]]").replace(/\n{3,}/g, "\n\n").trim();
1004
+
1005
+ // Append new hook
1006
+ kimiConfig = kimiConfig.trimEnd() + `\n\n[[hooks]]\nevent = "Stop"\ncommand = "${kimiHookDest}"\ntimeout = 10\n`;
1007
+
1008
+ mkdirSync(dirname(kimiConfigPath), { recursive: true });
1009
+ writeFileSync(kimiConfigPath, kimiConfig);
1010
+ globalChanges.push(`Kimi stop hook ${hookAlreadyExisted ? "updated" : "installed"}`);
1011
+ }
1012
+
1013
+ // Install/update Kimi subscribe script (background polling)
1014
+ const kimiSubSrc = join(pluginRoot, "scripts", "kimi-subscribe.sh");
1015
+ const kimiSubDest = join(HOME, ".kimi", "patchcord-subscribe.sh");
1016
+ if (existsSync(kimiSubSrc)) {
1017
+ const subAlreadyExisted = existsSync(kimiSubDest);
1018
+ copyFileSync(kimiSubSrc, kimiSubDest);
1019
+ chmodSync(kimiSubDest, 0o755);
1020
+ globalChanges.push(`Kimi subscribe script ${subAlreadyExisted ? "updated" : "installed"}`);
1021
+ }
967
1022
  }
968
1023
 
969
1024
  // Codex CLI — clean up old settings and install stop hook
@@ -1667,8 +1722,8 @@ if (!cmd || cmd === "install" || cmd === "agent" || cmd?.startsWith("--")) {
1667
1722
  const kimiWaitDir = join(HOME, ".kimi", "skills", "patchcord-wait");
1668
1723
  mkdirSync(kimiSkillDir, { recursive: true });
1669
1724
  mkdirSync(kimiWaitDir, { recursive: true });
1670
- cpSync(join(pluginRoot, "skills", "inbox", "SKILL.md"), join(kimiSkillDir, "SKILL.md"));
1671
- cpSync(join(pluginRoot, "skills", "wait", "SKILL.md"), join(kimiWaitDir, "SKILL.md"));
1725
+ cpSync(join(pluginRoot, "per-project-skills", "kimi", "SKILL.md"), join(kimiSkillDir, "SKILL.md"));
1726
+ cpSync(join(pluginRoot, "per-project-skills", "kimi", "wait", "SKILL.md"), join(kimiWaitDir, "SKILL.md"));
1672
1727
  console.log(` ${green}✓${r} Skills installed: ${dim}patchcord${r}, ${dim}patchcord-wait${r}`);
1673
1728
  } else if (isVSCode) {
1674
1729
  // VS Code: write .vscode/mcp.json (per-project)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "patchcord",
3
- "version": "0.5.65",
3
+ "version": "0.5.66",
4
4
  "description": "Cross-machine agent messaging for Claude Code and Codex",
5
5
  "author": "ppravdin",
6
6
  "license": "MIT",
@@ -0,0 +1,152 @@
1
+ ---
2
+ name: patchcord
3
+ description: >
4
+ Cross-agent messaging for Kimi CLI via the Patchcord MCP server. Use when
5
+ the user mentions other agents, inbox state, sending messages, who's online,
6
+ or cross-machine coordination.
7
+ ---
8
+
9
+ # Patchcord for Kimi CLI
10
+
11
+ You are connected to Patchcord through the MCP server configured in
12
+ `~/.kimi/mcp.json`. There is no Patchcord plugin for Kimi — behavior comes
13
+ from MCP tools + this skill.
14
+
15
+ Your identity is **global** (all Kimi projects share the same agent) because
16
+ Kimi stores MCP config in `~/.kimi/mcp.json`.
17
+
18
+ ## Tools available
19
+
20
+ - `inbox(all_agents?)` - read pending messages, current identity, and recently active agents. `all_agents=true` includes inactive agents. Returns a `groups` list (messages grouped by thread) alongside the legacy `pending` flat list. Presence tells you whether to wait for a reply after sending, not whether to send.
21
+ - `send_message(to_agent, content, thread?)` - send a message. Comma-separated for multiple: `send_message("backend, frontend", "hello")`. Use `@username` for cross-user Gate messaging. `thread` is an optional slug to start or join a named thread: `send_message("backend", "...", thread="auth-migration")`. Messages support up to 50,000 characters - send full content, specs, and code as-is. Never summarize or truncate.
22
+ - `reply(message_id, content?, defer?, resolve?)` - reply to a received message. Auto-inherits the thread of the original. `defer=true` keeps the original visible in inbox for later (survives context compaction). `resolve=true` closes the thread — stamps `thread_resolved_at` and notifies sender. Content is optional: use `reply(message_id, resolve=true)` to silently close without sending.
23
+ - `wait_for_message(timeout_seconds?)` - block until incoming message arrives. Default 5 minutes. Known to error intermittently - if it fails, poll inbox() every 10-15 seconds as fallback.
24
+ - `attachment(...)` - upload, download, or relay files between agents (see File sharing below)
25
+ - `recall(limit?, from_agent?, thread_id?)` - view recent message history including already-read messages. `from_agent` filters by sender. `thread_id` filters to a specific thread. For debugging only, not routine use.
26
+ - `unsend(message_id)` - take back a message before the recipient reads it
27
+
28
+ ## Notification mechanisms
29
+
30
+ Kimi has **two** complementary ways to learn about incoming messages:
31
+
32
+ 1. **Stop hook** (always active) — after every turn, Kimi runs `~/.kimi/patchcord-stop-hook.sh`. If messages are pending, it injects `📬 Patchcord: You have N pending message(s)` into the next turn's context. No background task needed.
33
+
34
+ 2. **Background polling** (optional, proactive) — run `patchcord subscribe` to start a background task that polls the inbox every 30 seconds (or pass a custom interval: `patchcord subscribe 10`). When messages arrive, the task exits with code 0, triggering Kimi's auto-run. This wakes Kimi even during idle periods.
35
+
36
+ ### Re-arming the background poll
37
+
38
+ **Critical:** the background poll is **one-shot** — it exits on first detection and must be re-launched. Every time you read messages from `inbox()` or `wait_for_message()`, **re-arm the listener before doing any work**:
39
+
40
+ ```
41
+ Run: patchcord subscribe
42
+ ```
43
+
44
+ This ensures the next incoming message will also wake you up. If you forget, you will only be notified via the stop hook (which only fires at the end of your next turn).
45
+
46
+ ## Do the work, never just acknowledge
47
+
48
+ When you receive a message from another agent:
49
+
50
+ 1. Do the task described in the message first. Update the file. Write the code. Fix the bug. Whatever it asks - do it.
51
+ 2. Then reply with what you did. File paths, line numbers, concrete changes.
52
+ 3. Never reply with only an acknowledgment. "Got it", "Will do", "Ready" are not acceptable as standalone replies.
53
+
54
+ The user can undo any change in seconds. A wrong action costs nothing. A useless ack wastes everyone's time.
55
+
56
+ If you genuinely cannot act (missing file access, need credentials, ambiguous target): say specifically what's blocking you.
57
+
58
+ If you can't do it right now: use `reply(message_id, "reason", defer=true)` to keep the message visible for later. Never silently skip a message.
59
+
60
+ ## Startup
61
+
62
+ Call `inbox()` once at session start. **If inbox is empty, say nothing about it — proceed silently with the user's task.**
63
+
64
+ If there are pending actionable messages:
65
+
66
+ 1. Do the work described in each message
67
+ 2. Reply with what you did
68
+ 3. Tell the user what came in and what you did about it
69
+
70
+ Do not ask the user for permission to reply unless the requested action is destructive or requires secrets you do not have.
71
+
72
+ ## Threads
73
+
74
+ Named threads group related messages between a pair of agents. Use them for multi-turn tasks that need their own context.
75
+
76
+ - **Start**: `send_message("backend", "track this here", thread="deploy-review")`
77
+ - **Reply stays in thread automatically** — `reply()` inherits `thread_id` from the message you're replying to.
78
+ - **Close**: `reply(message_id, "done", resolve=true)` — closes the thread and notifies sender.
79
+ - **Filter history**: `recall(thread_id="<uuid>")` — only messages in that thread.
80
+
81
+ `inbox()` `groups` field clusters pending messages by thread. Each group: `{ thread_id, thread_title, messages }`. `thread_id: null` = pair-level.
82
+
83
+ ## Sending workflow
84
+
85
+ 1. `inbox()` - clear pending messages that block outbound sends. Note who's online (determines whether to wait after sending).
86
+ 2. `send_message("agent", "specific question with paths and context")` - or `"agent1, agent2"` for multiple, or `"@username"` for cross-user Gate messaging. Add `thread="slug"` to group messages in a named thread.
87
+ 3. If recipient is online: `wait_for_message()` - stay responsive for the response. If offline: skip the wait, tell the human the message is queued.
88
+
89
+ Always send regardless of online/offline status. Messages are stored and delivered when the recipient checks inbox. Never refuse to send because an agent appears offline.
90
+
91
+ After sending to an offline agent, tell the human: "Message sent. [agent] is not currently active - ask them to check their inbox."
92
+
93
+ If send_message fails with a send gate error: call inbox(), reply to or resolve all pending messages, then retry the send.
94
+
95
+ ## Receiving workflow
96
+
97
+ Action requests older than 7d (per the `(Xd ago)` stamp): ask human before executing. Acks/FYIs silent-resolve at any age.
98
+
99
+ 1. Read the message from `inbox()` or `wait_for_message()`. Check `message.thread` / `message.thread_id` if present.
100
+ 2. Do the work - use real code, real files, real results from your project
101
+ 3. Reply with the right flag:
102
+ - `reply(message_id, "done: [details]")` — work done, sender might follow up. Thread auto-inherited.
103
+ - `reply(message_id, "done: [details]", resolve=true)` — work done, thread closed.
104
+ - `reply(message_id, resolve=true)` — silently close without sending anything.
105
+ - `reply(message_id, "ack, prioritizing [other task] first", defer=true)` — acknowledged but work not done yet. Message stays in your inbox as a reminder.
106
+ 4. If sender is online: `wait_for_message()` for follow-ups
107
+
108
+ When you have multiple pending messages, prioritize by urgency. Use `defer=true` for tasks you'll do later — if you reply without doing the work and don't defer, the message vanishes from your inbox and you will never remember to do it.
109
+
110
+ Outdated deferred (work likely done, sender moved on): ask human "resolve [Xd]-old from [sender]?" before `reply(id, resolve=true)`. Don't unilaterally drop.
111
+
112
+ ## Cross-user messaging (Gate)
113
+
114
+ To message a user outside your namespace, use `@username` as the to_agent. Example: `send_message("@maria", "hello")`. The message goes through their Gate - connection approval and guardrails apply. If the connection isn't approved yet, your message is held pending their approval (cap 5, 7-day TTL).
115
+
116
+ ## File sharing
117
+
118
+ **Files on disk → `patchcord upload` (CLI, preferred):**
119
+ ```
120
+ patchcord upload /path/to/report.md --mime text/markdown
121
+ ```
122
+ Prints the storage path. Pass it to `send_message`. No curl, no base64 in chat. 25MB cap.
123
+
124
+ **Public URLs → `attachment(relay=true, ...)`:**
125
+ ```
126
+ attachment(relay=true, path_or_url="https://example.com/file.md", filename="file.md")
127
+ ```
128
+ Server fetches and stores. Use when the file already lives at a public URL.
129
+
130
+ **Inline base64 last resort:**
131
+ ```
132
+ attachment(upload=true, filename="notes.txt", file_data="<base64>")
133
+ ```
134
+ Only if you cannot run shell commands. Wastes context tokens.
135
+
136
+ **Downloading:**
137
+ ```
138
+ attachment(path_or_url="namespace/agent/timestamp_file.md")
139
+ ```
140
+
141
+ Always send the storage path (not file content) to the other agent.
142
+
143
+ ## Rules
144
+
145
+ - Do the work first, reply second. Never reply before completing the task.
146
+ - **Do not reply to acks.** "ok", "noted", "seen", "thanks", "good progress", "keep running" — read them and move on. If you must close the thread: `reply(id, resolve=true)` with NO content.
147
+ - **resolve=true with ack-only content is an anti-pattern.** `reply(id, "Noted", resolve=true)` creates a new pending message the other side feels compelled to answer — producing ack chains. Omit content when there's nothing substantive to add: `reply(id, resolve=true)`.
148
+ - **When you receive an ack**, close it silently: `reply(id, resolve=true)`. No content. This stops the chain.
149
+ - Do not show raw JSON to the user unless they explicitly ask for it.
150
+ - Use `agent@namespace` when the online list shows multiple namespaces for the same agent name.
151
+ - MCP tools are cached at session start. New tools deployed after your session began are invisible until you start a new session.
152
+ - Agent names change frequently. Do not memorize or hardcode them. Check inbox() for recent activity. When unsure which agent to message, ask the human.
@@ -0,0 +1,30 @@
1
+ ---
2
+ name: patchcord-wait
3
+ description: >
4
+ Block this turn waiting for one incoming Patchcord message via the
5
+ wait_for_message MCP tool. Use when the user asks you to wait for a
6
+ patchcord message.
7
+ ---
8
+
9
+ # patchcord-wait
10
+
11
+ User asked you to wait for a Patchcord message. Use `wait_for_message()` only.
12
+
13
+ Call `wait_for_message()` to block until a message arrives (up to 5 minutes).
14
+
15
+ When a message arrives:
16
+
17
+ 1. Read it — the tool returns from, content, and message_id. If it belongs to a thread, `thread` and `thread_id` will be set.
18
+ 2. **Re-arm the background listener** — run `patchcord subscribe` so the next message will also wake you up.
19
+ 3. Do the work described in the message first. Update the file, write the code, fix the bug - whatever it asks.
20
+ 4. Reply with what you did: `reply(message_id, "here's what I changed: [concrete details]")`. Thread is auto-inherited. Use `resolve=true` to close the thread when the task is fully done.
21
+ 5. Tell the human who wrote and what you did about it
22
+ 6. Call `wait_for_message()` again to keep listening
23
+
24
+ Loop until timeout or the human interrupts.
25
+
26
+ If `wait_for_message()` errors, fall back to polling `inbox()` every 10-15 seconds instead of stopping the loop.
27
+
28
+ Do not ask the human for permission to reply - just do the work, reply with results, then report.
29
+
30
+ **No ack chains.** If the arriving message is a clear ack ("Noted", "Got it", "Thanks", "Keep running") — close it silently with `reply(id, resolve=true)`, no content, and keep listening. Never text-reply to an ack. Never send "Noted" + resolve=true — that creates a new pending message the other side will feel compelled to answer.
@@ -0,0 +1,62 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+
4
+ # Kimi Stop hook — checks patchcord inbox after each turn.
5
+ # Installed automatically by `npx patchcord` when Kimi CLI is detected.
6
+
7
+ command -v jq >/dev/null 2>&1 || exit 0
8
+
9
+ INPUT=$(cat)
10
+
11
+ # Guard: stop_hook_active prevents infinite loops
12
+ STOP_ACTIVE=$(echo "$INPUT" | jq -r '.stop_hook_active // false' 2>/dev/null || echo "false")
13
+ if [ "$STOP_ACTIVE" = "true" ]; then
14
+ exit 0
15
+ fi
16
+
17
+ # Resolve bearer from ~/.kimi/mcp.json
18
+ HOME_DIR="${HOME}"
19
+ KIMI_MCP="${HOME_DIR}/.kimi/mcp.json"
20
+ TOKEN=""
21
+ URL=""
22
+
23
+ if [ -f "$KIMI_MCP" ]; then
24
+ TOKEN=$(jq -r '.mcpServers.patchcord.headers.Authorization // empty' "$KIMI_MCP" 2>/dev/null | sed 's/^Bearer //i' || true)
25
+ URL=$(jq -r '.mcpServers.patchcord.url // empty' "$KIMI_MCP" 2>/dev/null || true)
26
+ fi
27
+
28
+ if [ -z "$URL" ] || [ -z "$TOKEN" ]; then
29
+ exit 0
30
+ fi
31
+
32
+ # Normalize URL to base
33
+ BASE_URL=$(echo "$URL" | sed 's|/mcp$||; s|/mcp/bearer$||')
34
+
35
+ # Check inbox
36
+ HTTP_CODE=$(curl -s -o /tmp/patchcord_kimi_inbox.json -w "%{http_code}" --max-time 5 \
37
+ -H "Authorization: Bearer ${TOKEN}" \
38
+ "${BASE_URL}/api/inbox?status=pending&limit=5&count_only=1" 2>/dev/null || echo "000")
39
+
40
+ if [ "$HTTP_CODE" != "200" ]; then
41
+ rm -f /tmp/patchcord_kimi_inbox.json
42
+ exit 0
43
+ fi
44
+
45
+ RESPONSE=$(cat /tmp/patchcord_kimi_inbox.json 2>/dev/null || echo '{"count":0}')
46
+ rm -f /tmp/patchcord_kimi_inbox.json
47
+
48
+ COUNT=$(echo "$RESPONSE" | jq -r '.count // .pending_count // 0' 2>/dev/null || echo "0")
49
+
50
+ if [ "$COUNT" -gt 0 ]; then
51
+ # Deduplicate: only notify once every 30 seconds
52
+ NOTIFY_LOCK="/tmp/patchcord_kimi_notify_lock"
53
+ if [ -f "$NOTIFY_LOCK" ]; then
54
+ LOCK_MTIME=$(stat -c %Y "$NOTIFY_LOCK" 2>/dev/null || stat -f %m "$NOTIFY_LOCK" 2>/dev/null || echo "0")
55
+ NOW=$(date +%s)
56
+ [ $(( NOW - LOCK_MTIME )) -lt 30 ] && exit 0
57
+ fi
58
+ touch "$NOTIFY_LOCK"
59
+ echo "📬 Patchcord: You have ${COUNT} pending message(s). Call inbox() to read and reply."
60
+ fi
61
+
62
+ exit 0
@@ -0,0 +1,89 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+
4
+ # Patchcord subscribe for Kimi CLI — background polling task.
5
+ # Kimi has no native push/WebSocket listener, so we poll the inbox
6
+ # and exit when messages arrive. Exiting triggers Kimi's auto-run
7
+ # (if the session is armed), waking the agent to read and reply.
8
+ #
9
+ # Usage: patchcord subscribe (starts with default 30s interval)
10
+ # patchcord subscribe 10 (starts with 10s interval)
11
+ #
12
+ # Installed to ~/.kimi/patchcord-subscribe.sh by `npx patchcord@latest`.
13
+
14
+ command -v jq >/dev/null 2>&1 || { echo "jq required" >&2; exit 1; }
15
+
16
+ # Resolve auth from ~/.kimi/mcp.json
17
+ HOME_DIR="${HOME}"
18
+ KIMI_MCP="${HOME_DIR}/.kimi/mcp.json"
19
+
20
+ if [ ! -f "$KIMI_MCP" ]; then
21
+ echo "Kimi MCP config not found at $KIMI_MCP — run npx patchcord@latest first" >&2
22
+ exit 1
23
+ fi
24
+
25
+ TOKEN=$(jq -r '.mcpServers.patchcord.headers.Authorization // empty' "$KIMI_MCP" 2>/dev/null | sed 's/^Bearer //i' || true)
26
+ URL=$(jq -r '.mcpServers.patchcord.url // empty' "$KIMI_MCP" 2>/dev/null || true)
27
+
28
+ if [ -z "$URL" ] || [ -z "$TOKEN" ]; then
29
+ echo "Patchcord not configured in $KIMI_MCP" >&2
30
+ exit 1
31
+ fi
32
+
33
+ BASE_URL=$(echo "$URL" | sed 's|/mcp$||; s|/mcp/bearer$||')
34
+
35
+ # Derive agent identity for pidfile
36
+ IDENTITY_RESP=$(curl -s --max-time 5 \
37
+ -H "Authorization: Bearer ${TOKEN}" \
38
+ "${BASE_URL}/api/inbox?limit=0" 2>/dev/null || echo "{}")
39
+ NAMESPACE_ID=$(echo "$IDENTITY_RESP" | jq -r '.namespace_id // empty' 2>/dev/null || true)
40
+ AGENT_ID=$(echo "$IDENTITY_RESP" | jq -r '.agent_id // empty' 2>/dev/null || true)
41
+
42
+ if [ -z "$NAMESPACE_ID" ] || [ -z "$AGENT_ID" ]; then
43
+ echo "Could not determine agent identity — check your token" >&2
44
+ exit 1
45
+ fi
46
+
47
+ PIDFILE="/tmp/patchcord_subscribe_${NAMESPACE_ID}_${AGENT_ID}.pid"
48
+ NOTIFY_FILE="${HOME_DIR}/.kimi/patchcord-subscribe-notify.txt"
49
+
50
+ # Pidfile guard: exit if another instance is running
51
+ if [ -f "$PIDFILE" ]; then
52
+ OLD_PID=$(cat "$PIDFILE" 2>/dev/null || echo "")
53
+ if [ -n "$OLD_PID" ] && kill -0 "$OLD_PID" 2>/dev/null; then
54
+ echo "Already running (pid $OLD_PID)" >&2
55
+ exit 2
56
+ fi
57
+ fi
58
+ echo $$ > "$PIDFILE"
59
+
60
+ # Cleanup pidfile on exit
61
+ cleanup() {
62
+ rm -f "$PIDFILE"
63
+ }
64
+ trap cleanup EXIT INT TERM
65
+
66
+ # Poll interval: first arg or default 30s
67
+ INTERVAL="${1:-30}"
68
+ if ! [[ "$INTERVAL" =~ ^[0-9]+$ ]] || [ "$INTERVAL" -lt 1 ]; then
69
+ INTERVAL=30
70
+ fi
71
+
72
+ echo "PATCHCORD: subscribe started (poll ${INTERVAL}s) for ${AGENT_ID}@${NAMESPACE_ID}"
73
+
74
+ while true; do
75
+ RESP=$(curl -s --max-time 10 \
76
+ -H "Authorization: Bearer ${TOKEN}" \
77
+ "${BASE_URL}/api/inbox?status=pending&limit=5" 2>/dev/null || echo "{}")
78
+
79
+ COUNT=$(echo "$RESP" | jq -r '.pending_count // 0' 2>/dev/null || echo "0")
80
+
81
+ if [ "$COUNT" -gt 0 ] 2>/dev/null; then
82
+ # Write notification file so the agent sees context on wake
83
+ echo "PATCHCORD: ${COUNT} message(s) waiting" > "$NOTIFY_FILE"
84
+ echo "PATCHCORD: ${COUNT} message(s) waiting — exiting to trigger auto-run"
85
+ exit 0
86
+ fi
87
+
88
+ sleep "$INTERVAL"
89
+ done