@gonzih/cc-discord 0.2.41 → 0.2.43

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.
@@ -343,15 +343,15 @@ function spawnPersistentSession(ns, token, wire, onExit, onOutput) {
343
343
  const claudeBin = resolveClaude();
344
344
  const env = buildEnv(token);
345
345
  console.log(`[meta-agent-manager] spawning persistent session (ns=${ns})`);
346
- // DO NOT ADD -p OR --input-format stream-json HERE.
347
- // With -p + --input-format stream-json, Claude buffers all stdin until EOF before responding.
348
- // Since the stdin pipe is never closed (it stays open for subsequent messages), Claude
349
- // waits forever and never produces output. Interactive mode (no -p) reads one line at a
350
- // time and responds immediately. Verified by direct test: `echo msg | claude --continue
351
- // --output-format stream-json ...` responds correctly; the -p variant does not.
346
+ // --input-format stream-json: Claude reads newline-delimited JSON messages from stdin.
347
+ // Each message must be: {"type":"user","message":{"role":"user","content":"..."}}
348
+ // Claude responds immediately to each message and stays alive waiting for the next one.
349
+ // DO NOT omit --input-format stream-json: without it, Claude ignores stdin when the
350
+ // pipe stays open (no TTY), producing no output until stdin is closed (EOF).
352
351
  const proc = spawn(claudeBin, [
353
352
  "--continue",
354
353
  "--output-format", "stream-json",
354
+ "--input-format", "stream-json",
355
355
  "--verbose",
356
356
  "--dangerously-skip-permissions",
357
357
  ], { cwd: wsPath, env, stdio: ["pipe", "pipe", "pipe"] });
@@ -402,13 +402,14 @@ export function createMetaAgentManager() {
402
402
  * Resets the inactivity timer so the watchdog doesn't kill a session
403
403
  * that just received a message but hasn't responded yet.
404
404
  */
405
- function writeToStdin(ns, line) {
405
+ function writeToStdin(ns, content) {
406
406
  const session = sessions.get(ns);
407
407
  if (!session)
408
408
  return;
409
409
  try {
410
- // Interactive mode: plain text line, Claude reads it as user input
411
- session.proc.stdin.write(`${line}\n`);
410
+ // --input-format stream-json: each message must be a newline-delimited JSON object.
411
+ const jsonMsg = JSON.stringify({ type: "user", message: { role: "user", content } });
412
+ session.proc.stdin.write(`${jsonMsg}\n`);
412
413
  // Reset inactivity timer — the session now has SESSION_INACTIVITY_MS to respond
413
414
  session.lastOutputAt = Date.now();
414
415
  }
@@ -550,13 +551,19 @@ export function createMetaAgentManager() {
550
551
  return; // already running
551
552
  wireRef = wire;
552
553
  startWatchdog();
554
+ // Grace period: don't run stale-instance check until 20s after startup.
555
+ // The instance key write (in index.ts "ready" handler) is async — if the poll
556
+ // loop fires before that write completes, this instance sees the OLD key,
557
+ // incorrectly thinks it's stale, and self-destructs (SIGTERM to all sessions).
558
+ const pollStartTime = Date.now();
559
+ const STALE_CHECK_GRACE_MS = 20_000;
553
560
  pollInterval = setInterval(() => {
554
561
  const namespaces = getNamespaces();
555
562
  if (namespaces.length === 0)
556
563
  return;
557
564
  for (const { namespace: ns, repoUrl } of namespaces) {
558
- // Stale-instance check
559
- if (instanceId) {
565
+ // Stale-instance check — skip during grace period
566
+ if (instanceId && Date.now() - pollStartTime > STALE_CHECK_GRACE_MS) {
560
567
  wire._redis.get(DISCORD_INSTANCE_KEY).then((current) => {
561
568
  if (current && current !== instanceId) {
562
569
  console.log(`[meta-agent-manager] stale instance detected (current=${current}, ours=${instanceId}) — exiting`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gonzih/cc-discord",
3
- "version": "0.2.41",
3
+ "version": "0.2.43",
4
4
  "description": "Claude Code Discord bot — chat with Claude Code via Discord",
5
5
  "type": "module",
6
6
  "bin": {