@myclaw163/clawclaw-cli 0.6.77 → 0.6.79

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@myclaw163/clawclaw-cli",
3
- "version": "0.6.77",
3
+ "version": "0.6.79",
4
4
  "type": "module",
5
5
  "description": "ClawClaw social deduction game CLI",
6
6
  "bin": {
@@ -1,7 +1,7 @@
1
1
  import { Command } from 'commander';
2
2
  import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from 'fs';
3
3
  import { join } from 'path';
4
- import { spawnSync, type ChildProcess } from 'child_process';
4
+ import { spawn, spawnSync, type ChildProcess } from 'child_process';
5
5
  import { AuthStore } from '../lib/auth.js';
6
6
  import { ApiError, GameClient } from '../lib/game-client.js';
7
7
  import { getProfileStateDir } from '../lib/init-command.js';
@@ -482,6 +482,41 @@ async function runGameStart(opts: { force?: boolean; channelUrl?: string }): Pro
482
482
  process.exit(2);
483
483
  }
484
484
 
485
+ // Channel 模式硬兜底:带 --channel-url 时,把真正的长流逻辑 fork 到 detached 后台进程,
486
+ // 父进程立即返回。这样即使 agent 误用 Powershell/前台直接执行,也不会阻塞——事件仍通过
487
+ // --channel-url 的 HTTP POST 推送给 channel,后台进程的 PID 写入 runtime 文件,
488
+ // `ccl game quit` 依旧能正确清理。CLAWCLAW_CHANNEL_DETACHED 防止子进程再次 fork。
489
+ if (opts.channelUrl && process.env.CLAWCLAW_CHANNEL_DETACHED !== '1') {
490
+ // 先做本地登录校验,保证前台调用能立即看到未登录错误(detach 后 stdout 被丢弃)。
491
+ const detachProfile = new AuthStore().getActive();
492
+ if (!detachProfile) throw new Error('Not logged in.');
493
+ // 已有运行中的 stream 时不重复 fork:detach 的瞬间返回容易诱发 agent 误重试,
494
+ // 在父进程同步拦截顺序重试,避免拉起第二个注定 already_running 的后台进程。
495
+ const existingPid = getRunningGameStartPid(getProfileStateDir(detachProfile));
496
+ if (existingPid && !opts.force) {
497
+ process.stdout.write(JSON.stringify({
498
+ exit_reason: ['already_running'],
499
+ next_step: `已有 ccl game start 后台进程在运行(pid ${existingPid}),事件正通过 channel 推送。不要重复启动;如需重启加 --force,或先 ccl game quit。`,
500
+ events: [{ type: 'already_running', pid: existingPid }],
501
+ summary: { phase: 'matching' },
502
+ }) + '\n');
503
+ return;
504
+ }
505
+ const child = spawn(process.execPath, process.argv.slice(1), {
506
+ detached: true,
507
+ stdio: 'ignore',
508
+ env: { ...process.env, CLAWCLAW_CHANNEL_DETACHED: '1' },
509
+ });
510
+ child.unref();
511
+ process.stdout.write(JSON.stringify({
512
+ exit_reason: ['channel_detached'],
513
+ next_step: 'ccl game start 已在后台启动,事件将通过 channel 实时推送。不要在前台等待此进程;结束本局用 ccl game quit。',
514
+ events: [{ type: 'channel_detached', pid: child.pid }],
515
+ summary: { phase: 'matching' },
516
+ }) + '\n');
517
+ return;
518
+ }
519
+
485
520
  const authStore = new AuthStore();
486
521
  const profile = authStore.getActive();
487
522
  if (!profile) throw new Error('Not logged in.');
@@ -546,7 +581,7 @@ async function runGameStart(opts: { force?: boolean; channelUrl?: string }): Pro
546
581
  const line = JSON.stringify(obj) + '\n';
547
582
  process.stdout.write(line);
548
583
  if (opts.channelUrl) {
549
- fetch(opts.channelUrl, { method: 'POST', body: line }).catch(() => {});
584
+ fetch(opts.channelUrl, { method: 'POST', headers: { 'content-type': 'application/json' }, body: line }).catch(() => {});
550
585
  }
551
586
  };
552
587
  const emitLifecycle = (
@@ -613,7 +648,7 @@ async function runGameStart(opts: { force?: boolean; channelUrl?: string }): Pro
613
648
  feedPath,
614
649
  sessionPath,
615
650
  getSessionPath: () => EventStore.latestSessionPath(),
616
- stdout: (s) => { process.stdout.write(s); if (opts.channelUrl) { fetch(opts.channelUrl, { method: 'POST', body: s }).catch(() => {}); } },
651
+ stdout: (s) => { process.stdout.write(s); if (opts.channelUrl) { fetch(opts.channelUrl, { method: 'POST', headers: { 'content-type': 'application/json' }, body: s }).catch(() => {}); } },
617
652
  signal: ctrl.signal,
618
653
  skipFeedWait: true,
619
654
  readSummary: currentSummary,