wezard 2.8.0 → 2.8.2
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/CLAUDE.md +3 -3
- package/dist/daemon/chat-log.js +25 -6
- package/dist/daemon/chat-log.js.map +1 -1
- package/dist/daemon/chat-name.js +52 -5
- package/dist/daemon/chat-name.js.map +1 -1
- package/dist/daemon/handoff.js +30 -1
- package/dist/daemon/handoff.js.map +1 -1
- package/dist/daemon/index.js +125 -21
- package/dist/daemon/index.js.map +1 -1
- package/dist/daemon/message-events.js.map +1 -1
- package/dist/daemon/mirror-bridge.js +224 -25
- package/dist/daemon/mirror-bridge.js.map +1 -1
- package/dist/daemon/peers.js +67 -52
- package/dist/daemon/peers.js.map +1 -1
- package/dist/daemon/picker-policy.js +8 -0
- package/dist/daemon/picker-policy.js.map +1 -1
- package/dist/daemon/router.js +5 -1
- package/dist/daemon/router.js.map +1 -1
- package/dist/daemon/session-scan.js +4 -1
- package/dist/daemon/session-scan.js.map +1 -1
- package/dist/daemon/spawn-tmux.js +598 -42
- package/dist/daemon/spawn-tmux.js.map +1 -1
- package/dist/daemon/steward-guard.js +8 -15
- package/dist/daemon/steward-guard.js.map +1 -1
- package/dist/daemon/tips.js +1 -1
- package/dist/daemon/tips.js.map +1 -1
- package/dist/daemon/wizard.js +4 -4
- package/dist/daemon/wizard.js.map +1 -1
- package/dist/mcp/server.js +88 -35
- package/dist/mcp/server.js.map +1 -1
- package/dist/shared/chat-view.js +3 -1
- package/dist/shared/chat-view.js.map +1 -1
- package/dist/shared/config.js +14 -1
- package/dist/shared/config.js.map +1 -1
- package/dist/shared/reminder.js +13 -5
- package/dist/shared/reminder.js.map +1 -1
- package/dist/shared/role-view.js +5 -2
- package/dist/shared/role-view.js.map +1 -1
- package/dist/shared/startup-notice.js +21 -0
- package/dist/shared/startup-notice.js.map +1 -0
- package/dist/shared/task-template.js +118 -0
- package/dist/shared/task-template.js.map +1 -0
- package/dist/shared/workspace-trust.js +54 -2
- package/dist/shared/workspace-trust.js.map +1 -1
- package/dist/shared/world.js +3 -1
- package/dist/shared/world.js.map +1 -1
- package/package.json +1 -1
|
@@ -35,10 +35,10 @@ import { hasMirrorAskq, runMirrorAskqFlow, hasMirrorPlan, mootMirrorPlan, runMir
|
|
|
35
35
|
import { isAutoWindowActive } from "./session-cache.js";
|
|
36
36
|
import { noticeSuffixFor } from "./notices.js";
|
|
37
37
|
import { dispatchPickerPolicy, pickerPolicy } from "./picker-policy.js";
|
|
38
|
-
import { renameWindow, runTmux, spawnTmuxClaude } from "./spawn-tmux.js";
|
|
38
|
+
import { renameWindow, runTmux, sparePoolFencedSids, spawnTmuxClaude } from "./spawn-tmux.js";
|
|
39
39
|
import { selectEffort, selectModel } from "./model-select.js";
|
|
40
40
|
import { parseEffort } from "../shared/effort.js";
|
|
41
|
-
import { hasRegistry, liveSessionPids, markTranscript, probeOf, registrySnapshot, sessionOfPid, sessionOnPane, submittedSince, submittedEventSince, watchRegistry } from "./wezard-session.js";
|
|
41
|
+
import { hasRegistry, liveSessionPids, markTranscript, probeOf, readRegistry, registrySnapshot, sessionOfPid, sessionOnPane, submittedSince, submittedEventSince, watchRegistry } from "./wezard-session.js";
|
|
42
42
|
import { waitForIdle } from "./graph.js";
|
|
43
43
|
import { wizardStore } from "./wizard.js";
|
|
44
44
|
import { startSubagentWatch } from "./subagent-tail.js";
|
|
@@ -4478,6 +4478,10 @@ export const startMirror = (deps) => {
|
|
|
4478
4478
|
const spawningPanes = new Set();
|
|
4479
4479
|
const sidsClaimedByOthers = (principal) => {
|
|
4480
4480
|
const s = new Set(inFlightSids);
|
|
4481
|
+
// 空白备用池里的会话 (以及刚被领走、attach 还没落定的那几秒) 同样不能认领:
|
|
4482
|
+
// 它们住在同一个 cwd 的 project dir 里, 却还没有任何绑定指着。
|
|
4483
|
+
for (const sid of sparePoolFencedSids())
|
|
4484
|
+
s.add(sid);
|
|
4481
4485
|
for (const [k, v] of Object.entries(deps.store.all()))
|
|
4482
4486
|
if (k !== principal && v.sessionId)
|
|
4483
4487
|
s.add(v.sessionId);
|
|
@@ -5233,6 +5237,26 @@ export const startMirror = (deps) => {
|
|
|
5233
5237
|
return undefined;
|
|
5234
5238
|
}
|
|
5235
5239
|
};
|
|
5240
|
+
/** 宪章 → 首条注入顶在最前的 reminder 块。认/剥/折叠全靠它是合法 reminder 块
|
|
5241
|
+
* (kind "charter", shared/reminder.ts): cleanUserText 剥它 → 回显去重与印章
|
|
5242
|
+
* 认到的仍是纯正文; peers.stripMeta 剥它 → talkTurns / read_chat 的问话干净;
|
|
5243
|
+
* rolepage 把它折叠成 `<charter>` 标签。正文里出现闭合标签字面量会截断这些
|
|
5244
|
+
* 剥离 (懒惰匹配到第一个闭合就停), 顺手转义掉。 */
|
|
5245
|
+
const charterPreamble = (charter) => `<system-reminder wezard="charter">\n${charter.trim().replace(/<\/(system-reminder|pasted_content)/g, "<\\/$1")}\n</system-reminder>\n`;
|
|
5246
|
+
/** 这一条注入该带的前置宪章: 挂了 charterPending 且不是斜杠命令才有 —— preamble
|
|
5247
|
+
* 会把命令顶得不再是命令, 宪章留到下一条非命令消息。 */
|
|
5248
|
+
const charterPrefixOf = (a, text) => a.charterPending?.trim() && !text.trimStart().startsWith("/") ? charterPreamble(a.charterPending) : "";
|
|
5249
|
+
/** 把宪章挂到 target 的首条注入上 (见 AttachState.charterPending)。给了 `charter`
|
|
5250
|
+
* 就用它, 没给现渲染。codex 免: developer_instructions 已带, 不必重复。 */
|
|
5251
|
+
const armCharter = (target, charter) => {
|
|
5252
|
+
const a = byTarget.get(target);
|
|
5253
|
+
if (!a)
|
|
5254
|
+
return false;
|
|
5255
|
+
const cli = backendForPath(a.jsonlPath).name;
|
|
5256
|
+
const text = (charter ?? charterFor(target, { cwd: a.runningCwd, cli }) ?? "").trim();
|
|
5257
|
+
a.charterPending = cli === "codex" || !text ? undefined : text;
|
|
5258
|
+
return true;
|
|
5259
|
+
};
|
|
5236
5260
|
/** 死 pane 重生: 管家重新取 router 档位, 普通 wizard 沿用绑定模型。 */
|
|
5237
5261
|
const respawnSpec = (a) => {
|
|
5238
5262
|
return resumedStewardSpec(cfg, a.target, backendForPath(a.jsonlPath).name, a);
|
|
@@ -5270,25 +5294,30 @@ export const startMirror = (deps) => {
|
|
|
5270
5294
|
// 已在跑的管家不动 (换模型会让缓存整份重读); 死 pane 重生见 respawnSpec。
|
|
5271
5295
|
const backendChanged = Boolean(boundPath && effCli && backendForPath(expandHome(boundPath)).name !== effCli);
|
|
5272
5296
|
const { model: effModel, effort: effEffort } = newStewardSpec(cfg, target, effCli, opts, backendChanged ? undefined : prev?.effort ?? deps.store.get(target)?.effort);
|
|
5273
|
-
if (prev?.tmuxPane) {
|
|
5297
|
+
if (prev?.tmuxPane && !opts?.preservePrevious) {
|
|
5274
5298
|
// Best-effort kill; ignore errors (pane may already be dead).
|
|
5275
5299
|
void runTmux(["kill-pane", "-t", prev.tmuxPane]);
|
|
5276
5300
|
}
|
|
5277
|
-
if (prev)
|
|
5301
|
+
if (prev && !opts?.preservePrevious)
|
|
5278
5302
|
detach(prev, "/new respawn");
|
|
5279
5303
|
// sid 由这里先定, 出生到 attach 之间挂在 inFlightSids 上 (见那里)。
|
|
5280
|
-
const freshSid = randomUUID();
|
|
5304
|
+
const freshSid = opts?.sessionId ?? randomUUID();
|
|
5281
5305
|
inFlightSids.add(freshSid);
|
|
5306
|
+
// 宪章只用来落盘 (spawn 侧 charters/<sid>.md) 与 codex 的 developer_instructions;
|
|
5307
|
+
// claude/codebuddy 的身份改由首条注入带 (下面挂到新附着上)。
|
|
5308
|
+
const charter = opts?.systemPrompt ?? charterFor(target, { cwd: eff, cli: effCli });
|
|
5282
5309
|
const r = await spawnTmuxClaude({
|
|
5283
5310
|
cfg,
|
|
5284
5311
|
log: log.child({ sub: "new-session", target }),
|
|
5285
5312
|
sessionId: freshSid,
|
|
5313
|
+
handoffId: opts?.handoffId,
|
|
5286
5314
|
windowName: windowLabel(target, windowName),
|
|
5287
5315
|
cwdOverride: eff,
|
|
5316
|
+
chatKey: target,
|
|
5288
5317
|
cli: effCli,
|
|
5289
5318
|
model: effModel,
|
|
5290
5319
|
effort: effEffort,
|
|
5291
|
-
systemPrompt:
|
|
5320
|
+
systemPrompt: charter,
|
|
5292
5321
|
}).catch((e) => { inFlightSids.delete(freshSid); throw e; });
|
|
5293
5322
|
if (!r.ok) {
|
|
5294
5323
|
inFlightSids.delete(freshSid);
|
|
@@ -5325,6 +5354,8 @@ export const startMirror = (deps) => {
|
|
|
5325
5354
|
if (spawned) {
|
|
5326
5355
|
spawned.justSpawned = true;
|
|
5327
5356
|
spawned.muteUntilInject = true;
|
|
5357
|
+
// 全新的会话 = 一份它还没读过的宪章; codex 的已在 developer_instructions。
|
|
5358
|
+
spawned.charterPending = effCli === "codex" || !charter?.trim() ? undefined : charter;
|
|
5328
5359
|
// 只有换过 pane 才是断点 —— 首次建会话时 prev 为空, 那不是"不连续", 是开局。
|
|
5329
5360
|
if (prev)
|
|
5330
5361
|
markCut(target, spawned.sessionId, "new");
|
|
@@ -5438,6 +5469,7 @@ export const startMirror = (deps) => {
|
|
|
5438
5469
|
forkSession: true,
|
|
5439
5470
|
windowName: windowLabel(args.target, args.windowName),
|
|
5440
5471
|
cwdOverride: parent.runningCwd,
|
|
5472
|
+
chatKey: args.target,
|
|
5441
5473
|
cli: parentCli,
|
|
5442
5474
|
model,
|
|
5443
5475
|
effort,
|
|
@@ -5456,9 +5488,13 @@ export const startMirror = (deps) => {
|
|
|
5456
5488
|
// 顺便就是它的开场白 (「你是谁、继承了什么、准备接什么」)。
|
|
5457
5489
|
const hello = args.bootstrap?.trim()
|
|
5458
5490
|
|| "(wezard 分身初始化)用一句话说明: 你是谁、从谁那里继承了哪些上下文、准备接什么活。不要复述这条指令。";
|
|
5491
|
+
// 子宪章随开场白一起进 pane (此刻还没有附着, 不走 charterPending): 分叉继承了
|
|
5492
|
+
// 父亲转录里父亲的 preamble, 子宪章必须声明取代那份继承来的身份。codex 例外 ——
|
|
5493
|
+
// 它的宪章仍在 developer_instructions 里。
|
|
5494
|
+
const pre = parentCli !== "codex" && args.systemPrompt?.trim() ? charterPreamble(args.systemPrompt) : "";
|
|
5459
5495
|
rememberInject(hello);
|
|
5460
5496
|
const boot = await inject({
|
|
5461
|
-
text: hello, images: [], cfg, log: lg,
|
|
5497
|
+
text: pre + hello, images: [], cfg, log: lg,
|
|
5462
5498
|
sessionId: parentCli === "codex" ? r.sessionId : parent.sessionId, jsonlPath: parentCli === "codex" ? r.jsonlPath : parent.jsonlPath,
|
|
5463
5499
|
tmuxTarget: r.tmuxPane, freshSpawn: true, guard: args.guard,
|
|
5464
5500
|
});
|
|
@@ -5555,7 +5591,7 @@ export const startMirror = (deps) => {
|
|
|
5555
5591
|
return { runningCwd: chat.running || expandedDefaultCwd, pendingCwd: pending, defaultCwd: expandedDefaultCwd };
|
|
5556
5592
|
};
|
|
5557
5593
|
// Write `pendingCwd` to the BASE principal so the switch applies chat-wide —
|
|
5558
|
-
// the next /new in any tagged/untagged session picks it up. `set_workspace`
|
|
5594
|
+
// the next /new or self-handoff in any tagged/untagged session picks it up. `set_workspace`
|
|
5559
5595
|
// from a tagged session still writes to the shared slot, not the tagged
|
|
5560
5596
|
// session's own record, matching "sessions share the chat's cwd".
|
|
5561
5597
|
const setPendingCwd = (target, cwd) => {
|
|
@@ -6391,7 +6427,9 @@ export const startMirror = (deps) => {
|
|
|
6391
6427
|
a.pickerSentinelFp = v.screen;
|
|
6392
6428
|
const pick = pickAutoAllowAnswer(parseModalOptions(v.screen));
|
|
6393
6429
|
if (!pick) {
|
|
6394
|
-
|
|
6430
|
+
// tail 与上面「未判成 modal」分支同款: 远端 (devcloud) 选项文案变体排查靠它,
|
|
6431
|
+
// 不必再守着屏抓 capture。
|
|
6432
|
+
log.info({ target: a.target, title: v.title, tail: v.screen.split("\n").slice(-8).join("\\n").slice(-600) }, "picker sentinel: modal on pane but not a trustworthy permission picker — leaving it");
|
|
6395
6433
|
return;
|
|
6396
6434
|
}
|
|
6397
6435
|
const ctx = parseConfirmContext(v.screen);
|
|
@@ -6578,14 +6616,17 @@ export const startMirror = (deps) => {
|
|
|
6578
6616
|
if (!a || !clear.nextSid || !clear.panePid || a.tmuxPane !== clear.pane || a.sessionId !== clear.nextSid || await panePidOf(clear.pane) !== clear.panePid)
|
|
6579
6617
|
return false;
|
|
6580
6618
|
const backend = backendForPath(a.jsonlPath);
|
|
6581
|
-
if (backend.name === "claude") {
|
|
6619
|
+
if (backend.name === "claude" || backend.name === "claude-internal") {
|
|
6582
6620
|
const actual = sessionOnPane(backend.homeDir, clear.pane);
|
|
6583
6621
|
return actual?.pid === clear.pid && actual.sessionId === clear.nextSid;
|
|
6584
6622
|
}
|
|
6585
|
-
if (backend.name
|
|
6623
|
+
if (backend.name === "codex" && codexSessionForPane(clear.pane)?.sessionId !== clear.nextSid)
|
|
6586
6624
|
return false;
|
|
6587
6625
|
const ps = await psSnap();
|
|
6588
|
-
|
|
6626
|
+
if (backend.name === "codebuddy")
|
|
6627
|
+
return registeredPidUnder(clear.panePid, liveSessionPids(backend.homeDir), ps.kids) === clear.pid;
|
|
6628
|
+
const executable = backend.name === "codex" ? "codex" : basename(backend.bin);
|
|
6629
|
+
const pids = new Set([...ps.commands].filter(([, command]) => basename(command) === executable).map(([pid]) => pid));
|
|
6589
6630
|
return registeredPidUnder(clear.panePid, pids, ps.kids) === clear.pid;
|
|
6590
6631
|
};
|
|
6591
6632
|
const verifyClearInput = async (target, clear, phase, allowed) => {
|
|
@@ -6607,28 +6648,162 @@ export const startMirror = (deps) => {
|
|
|
6607
6648
|
if (!a?.tmuxPane)
|
|
6608
6649
|
return { ok: false, reason: "handoff requires the original live pane; no respawn" };
|
|
6609
6650
|
const backend = backendForPath(a.jsonlPath);
|
|
6610
|
-
// 只开放本机真实探针验证过的后端; 不能因家族相近就假定 slash/注册表语义相同。
|
|
6611
|
-
if (!["claude", "codex"].includes(backend.name))
|
|
6612
|
-
return { ok: false, reason: `${backend.name} native /clear completion is not verified; pane unchanged` };
|
|
6613
|
-
if (a.pendingCwd && a.pendingCwd !== a.runningCwd)
|
|
6614
|
-
return { ok: false, reason: "pending workspace switch requires an explicit /new; /clear keeps the original process cwd" };
|
|
6615
6651
|
const pane = a.tmuxPane;
|
|
6616
6652
|
const valid = () => guard() && byTarget.get(target) === a && a.tmuxPane === pane;
|
|
6617
6653
|
try {
|
|
6618
6654
|
return await withPaneInput(pane, async (live) => {
|
|
6619
6655
|
const codex = backend.name === "codex";
|
|
6620
6656
|
const nativePid = async () => {
|
|
6621
|
-
if (!codex)
|
|
6622
|
-
return sessionOnPane(backend.homeDir, pane)?.pid;
|
|
6623
6657
|
const root = await panePidOf(pane);
|
|
6658
|
+
if (!root)
|
|
6659
|
+
return undefined;
|
|
6660
|
+
if (backend.name === "claude" || backend.name === "claude-internal")
|
|
6661
|
+
return sessionOnPane(backend.homeDir, pane)?.pid;
|
|
6624
6662
|
const ps = await psSnap();
|
|
6625
|
-
|
|
6663
|
+
if (backend.name === "codebuddy")
|
|
6664
|
+
return registeredPidUnder(root, liveSessionPids(backend.homeDir), ps.kids);
|
|
6665
|
+
const executable = codex ? "codex" : basename(backend.bin);
|
|
6666
|
+
return registeredPidUnder(root, new Set([...ps.commands].filter(([, command]) => basename(command) === executable).map(([pid]) => pid)), ps.kids);
|
|
6626
6667
|
};
|
|
6627
6668
|
const pid = await nativePid();
|
|
6628
6669
|
const panePid = await panePidOf(pane);
|
|
6629
6670
|
if (!pid || !panePid || !(await tmuxPaneAlive(pane)))
|
|
6630
6671
|
return { ok: false, reason: "original CLI process identity is unavailable" };
|
|
6631
6672
|
const original = sessionOnPane(backend.homeDir, pane);
|
|
6673
|
+
const queuedCwd = chatCwdFallback(target).pending || a.pendingCwd;
|
|
6674
|
+
const pendingCwd = queuedCwd || attempt?.workspaceCwd || "";
|
|
6675
|
+
const workspaceStillNeedsHandoff = pendingCwd && (expandHome(pendingCwd) !== expandHome(a.runningCwd) || (!!attempt && a.sessionId !== attempt.sid));
|
|
6676
|
+
if (workspaceStillNeedsHandoff) {
|
|
6677
|
+
const prior = attempt ?? { pane, pid, panePid, sid: a.sessionId, at: Date.now(), workspaceCwd: pendingCwd };
|
|
6678
|
+
const actualCwd = (binding) => expandHome(binding.runningCwd.trim());
|
|
6679
|
+
const processEvidence = async (binding) => {
|
|
6680
|
+
if (!binding.tmuxPane || !binding.sessionId || actualCwd(binding) !== expandHome(pendingCwd))
|
|
6681
|
+
return undefined;
|
|
6682
|
+
const nextPanePid = await panePidOf(binding.tmuxPane);
|
|
6683
|
+
if (!nextPanePid)
|
|
6684
|
+
return undefined;
|
|
6685
|
+
const nextBackend = backendForPath(binding.jsonlPath);
|
|
6686
|
+
let nextPid;
|
|
6687
|
+
if (nextBackend.name === "claude" || nextBackend.name === "claude-internal") {
|
|
6688
|
+
const session = sessionOnPane(nextBackend.homeDir, binding.tmuxPane);
|
|
6689
|
+
if (session?.sessionId === binding.sessionId)
|
|
6690
|
+
nextPid = session.pid;
|
|
6691
|
+
}
|
|
6692
|
+
if (nextBackend.name !== "claude" && nextBackend.name !== "claude-internal") {
|
|
6693
|
+
const ps = await psSnap();
|
|
6694
|
+
const executable = nextBackend.name === "codex" ? "codex" : basename(nextBackend.bin);
|
|
6695
|
+
const processIds = nextBackend.name === "codebuddy"
|
|
6696
|
+
? liveSessionPids(nextBackend.homeDir)
|
|
6697
|
+
: new Set([...ps.commands].filter(([, command]) => basename(command) === executable).map(([childPid]) => childPid));
|
|
6698
|
+
nextPid = registeredPidUnder(nextPanePid, processIds, ps.kids);
|
|
6699
|
+
if (nextBackend.name === "codex" && codexSessionForPane(binding.tmuxPane)?.sessionId !== binding.sessionId)
|
|
6700
|
+
nextPid = undefined;
|
|
6701
|
+
}
|
|
6702
|
+
return nextPid ? { ...prior, pane: binding.tmuxPane, pid: nextPid, panePid: nextPanePid, nextSid: binding.sessionId } : undefined;
|
|
6703
|
+
};
|
|
6704
|
+
// A queued workspace switch must replace the CLI process, not clear its
|
|
6705
|
+
// context in place. Persist the old process identity before spawning so
|
|
6706
|
+
// a daemon reload can distinguish an unapplied switch from a completed one.
|
|
6707
|
+
if (attempt?.nextSid) {
|
|
6708
|
+
return await verifyClearContext(target, attempt)
|
|
6709
|
+
? { ok: true }
|
|
6710
|
+
: { ok: false, reason: "new workspace handoff process/session changed; refusing brief replay" };
|
|
6711
|
+
}
|
|
6712
|
+
const current = byTarget.get(target);
|
|
6713
|
+
if (!current)
|
|
6714
|
+
return { ok: false, reason: "workspace handoff attachment disappeared" };
|
|
6715
|
+
if (current.sessionId !== prior.sid) {
|
|
6716
|
+
const recovered = await processEvidence(current);
|
|
6717
|
+
if (!recovered)
|
|
6718
|
+
return { ok: false, reason: "workspace handoff changed session without a verifiable pending-cwd process" };
|
|
6719
|
+
submitted(recovered);
|
|
6720
|
+
if (prior.pane !== recovered.pane)
|
|
6721
|
+
await runTmux(["kill-pane", "-t", prior.pane]);
|
|
6722
|
+
return { ok: true };
|
|
6723
|
+
}
|
|
6724
|
+
if (current.tmuxPane !== prior.pane || pid !== prior.pid || panePid !== prior.panePid) {
|
|
6725
|
+
return { ok: false, reason: "original pane/CLI process changed before workspace handoff" };
|
|
6726
|
+
}
|
|
6727
|
+
let durable = attempt ?? prior;
|
|
6728
|
+
if (!attempt || !attempt.workspaceCwd || !attempt.spawnSid) {
|
|
6729
|
+
const actualSid = codex ? codexSessionForPane(pane)?.sessionId : original?.sessionId;
|
|
6730
|
+
if ((backend.name === "claude" || backend.name === "claude-internal" || codex) && actualSid !== prior.sid)
|
|
6731
|
+
return { ok: false, reason: "original session changed before workspace handoff" };
|
|
6732
|
+
durable = { ...prior, workspaceCwd: pendingCwd, spawnSid: attempt?.spawnSid ?? prior.spawnSid ?? randomUUID() };
|
|
6733
|
+
submitted(durable);
|
|
6734
|
+
}
|
|
6735
|
+
const spawnSid = durable.spawnSid ?? randomUUID();
|
|
6736
|
+
if (!durable.spawnSid) {
|
|
6737
|
+
durable = { ...durable, spawnSid };
|
|
6738
|
+
submitted(durable);
|
|
6739
|
+
}
|
|
6740
|
+
if (!live() || !valid() || !guard())
|
|
6741
|
+
return { ok: false, reason: "workspace handoff permission withdrawn before respawn" };
|
|
6742
|
+
// A daemon may have died after tmux launched the stable session but
|
|
6743
|
+
// before attach() persisted it. Recover that pane by its durable SID.
|
|
6744
|
+
const backendForSpawn = resolveCliBackend(cfg.wrc);
|
|
6745
|
+
const orphan = backendForSpawn.name === "claude" || backendForSpawn.name === "claude-internal"
|
|
6746
|
+
? readRegistry(backendForSpawn.homeDir).find((s) => s.sessionId === spawnSid && s.pane && s.pid > 0)
|
|
6747
|
+
: undefined;
|
|
6748
|
+
const codexOrphan = backendForSpawn.name === "codex" ? await (async () => {
|
|
6749
|
+
const panes = await runTmux(["list-panes", "-a", "-F", "#{pane_id}"]);
|
|
6750
|
+
if (!panes.ok)
|
|
6751
|
+
return undefined;
|
|
6752
|
+
for (const candidate of panes.stdout.split("\n").map((s) => s.trim()).filter(Boolean)) {
|
|
6753
|
+
const [tag, session] = await Promise.all([
|
|
6754
|
+
runTmux(["show-options", "-p", "-t", candidate, "-v", "@wezard_handoff_id"]),
|
|
6755
|
+
Promise.resolve(codexSessionForPane(candidate)),
|
|
6756
|
+
]);
|
|
6757
|
+
if (tag.ok && tag.stdout.trim() === spawnSid && session?.sessionId)
|
|
6758
|
+
return session;
|
|
6759
|
+
}
|
|
6760
|
+
return undefined;
|
|
6761
|
+
})() : undefined;
|
|
6762
|
+
const recovery = orphan ? { pane: orphan.pane, sessionId: orphan.sessionId,
|
|
6763
|
+
jsonlPath: join(projectDirFor(a.jsonlPath, expandHome(pendingCwd)), `${orphan.sessionId}.jsonl`) } : codexOrphan;
|
|
6764
|
+
let launchedCwd = "";
|
|
6765
|
+
if (recovery && await tmuxPaneAlive(recovery.pane)) {
|
|
6766
|
+
const session = await runTmux(["display-message", "-p", "-t", recovery.pane, "#{session_name}"]);
|
|
6767
|
+
const attached = attach({ sessionId: recovery.sessionId, jsonlPath: recovery.jsonlPath, target, tmuxPane: recovery.pane,
|
|
6768
|
+
tmuxSession: session.ok ? session.stdout.trim() : "", cwd: pendingCwd, model: "", effort: "", pendingCwd: "" });
|
|
6769
|
+
if (!attached.ok)
|
|
6770
|
+
return { ok: false, reason: `recovered workspace pane could not bind: ${attached.reason}` };
|
|
6771
|
+
launchedCwd = pendingCwd;
|
|
6772
|
+
}
|
|
6773
|
+
else {
|
|
6774
|
+
let spawned;
|
|
6775
|
+
for (let retry = 0; retry < 3; retry++) {
|
|
6776
|
+
spawned = await newSession(target, displayName(target) || target, undefined, {
|
|
6777
|
+
cwd: pendingCwd, guard, preservePrevious: true, sessionId: spawnSid, handoffId: spawnSid,
|
|
6778
|
+
});
|
|
6779
|
+
if (spawned.ok || !guard())
|
|
6780
|
+
break;
|
|
6781
|
+
if (retry < 2)
|
|
6782
|
+
await sleep(500 * (retry + 1));
|
|
6783
|
+
}
|
|
6784
|
+
if (!spawned?.ok)
|
|
6785
|
+
return { ok: false, reason: `new workspace CLI failed to start: ${spawned?.reason ?? "unknown"}` };
|
|
6786
|
+
launchedCwd = spawned.cwd ?? "";
|
|
6787
|
+
}
|
|
6788
|
+
const next = byTarget.get(target);
|
|
6789
|
+
const evidence = next && launchedCwd && expandHome(launchedCwd) === expandHome(pendingCwd)
|
|
6790
|
+
? await processEvidence(next)
|
|
6791
|
+
: undefined;
|
|
6792
|
+
if (!evidence)
|
|
6793
|
+
return { ok: false, reason: "new CLI did not confirm the requested workspace and session" };
|
|
6794
|
+
submitted(evidence);
|
|
6795
|
+
if (prior.pane !== evidence.pane) {
|
|
6796
|
+
// attach() replaced the target binding; kill the former pane only
|
|
6797
|
+
// after the replacement process and binding have both been verified.
|
|
6798
|
+
await runTmux(["kill-pane", "-t", prior.pane]);
|
|
6799
|
+
}
|
|
6800
|
+
log.info({ target, oldSessionId: prior.sid, sessionId: evidence.nextSid, cwd: launchedCwd }, "handoff: pending workspace applied in fresh CLI process");
|
|
6801
|
+
return { ok: true };
|
|
6802
|
+
}
|
|
6803
|
+
// Native /clear is separately restricted to backends with verified
|
|
6804
|
+
// completion; pending workspace switches use fresh-process evidence above.
|
|
6805
|
+
if (!["claude", "codex"].includes(backend.name))
|
|
6806
|
+
return { ok: false, reason: `${backend.name} native /clear completion is not verified; pane unchanged` };
|
|
6632
6807
|
let transaction = attempt ?? { pane, pid, panePid, sid: a.sessionId, at: Date.now() };
|
|
6633
6808
|
if (transaction.pane !== pane || transaction.pid !== pid || (transaction.panePid && transaction.panePid !== panePid))
|
|
6634
6809
|
return { ok: false, reason: "original pane/CLI process changed; refusing /clear" };
|
|
@@ -6971,7 +7146,9 @@ export const startMirror = (deps) => {
|
|
|
6971
7146
|
// Same resume-fork hazard as dispatch: snapshot before spawn, re-bind
|
|
6972
7147
|
// onto the forked jsonl once it appears (EOF offset — fork is seeded).
|
|
6973
7148
|
const resumeBaseline = listJsonls(dirname(a.jsonlPath));
|
|
6974
|
-
const
|
|
7149
|
+
const respawnCli = backendForPath(a.jsonlPath).name;
|
|
7150
|
+
const charter = charterFor(target, { cwd: a.runningCwd, cli: respawnCli });
|
|
7151
|
+
const r = await spawnTmuxClaude({ cfg, log: log.child({ sub: "respawn-init", sessionId: sid }), resumeSessionId: sid, windowName: displayName(target) || target, cwdOverride: a.runningCwd, chatKey: target, cli: respawnCli, ...respawnSpec(a), systemPrompt: charter });
|
|
6975
7152
|
if (!r.ok || !r.tmuxPane)
|
|
6976
7153
|
return { ok: false, retryable: r.retryable !== false, reason: `respawn failed: ${r.reason ?? "unknown"}` };
|
|
6977
7154
|
if (!permitted() || a.tmuxPane !== probedPane) {
|
|
@@ -6993,6 +7170,9 @@ export const startMirror = (deps) => {
|
|
|
6993
7170
|
a.model = r.model;
|
|
6994
7171
|
deps.store.set(target, { sessionId: sid, jsonlPath: a.jsonlPath, tmuxSession: a.tmuxSession, tmuxPane: a.tmuxPane, cwd: a.runningCwd || undefined, model: a.model || undefined, effort: a.effort || undefined, pendingCwd: a.pendingCwd || undefined });
|
|
6995
7172
|
startMigrationWatcher(a, resumeBaseline, forkOf(a.jsonlPath));
|
|
7173
|
+
// 重生出的 pane 里是同一份会话, 但宪章要重挂: --resume 读的是 CLI 自己的
|
|
7174
|
+
// 系统提示, 我们的宪章只跟着消息走。下面的注入 (本函数尾部) 会带上它。
|
|
7175
|
+
a.charterPending = respawnCli === "codex" || !charter?.trim() ? undefined : charter;
|
|
6996
7176
|
fresh = true;
|
|
6997
7177
|
}
|
|
6998
7178
|
if (!permitted())
|
|
@@ -7001,7 +7181,10 @@ export const startMirror = (deps) => {
|
|
|
7001
7181
|
// Render only after the pane lane grants delivery, then stamp exactly at
|
|
7002
7182
|
// the first permitted input.
|
|
7003
7183
|
const envelope = text.trimStart().startsWith("/") ? "" : opts?.envelope ?? "";
|
|
7004
|
-
|
|
7184
|
+
// 首条注入带上宪章 preamble (newSession/重生/清空后挂上的那份)。印章与回显
|
|
7185
|
+
// 去重都记纯正文 —— cleanUserText 两侧都会剥掉 preamble, 认出的仍是同一句。
|
|
7186
|
+
const pre = charterPrefixOf(a, text);
|
|
7187
|
+
let full = pre + text + renderTail(envelope);
|
|
7005
7188
|
const beforeInput = () => {
|
|
7006
7189
|
a.pendingFromCli = false;
|
|
7007
7190
|
a.turnFromChat = true;
|
|
@@ -7022,11 +7205,14 @@ export const startMirror = (deps) => {
|
|
|
7022
7205
|
physicalGuard: opts?.handoff ? async (phase) => { const ok = permission() && await verifyClearInput(target, opts.handoff, phase, permission) && permission(); if (!ok)
|
|
7023
7206
|
pinRejected = true; return ok; } : undefined,
|
|
7024
7207
|
requireIdle: opts?.requireIdle ? () => idleNow(target) : undefined,
|
|
7025
|
-
prepareText: () => (full = text + renderTail(envelope, noticeSuffixFor(target, text))),
|
|
7208
|
+
prepareText: () => (full = pre + text + renderTail(envelope, noticeSuffixFor(target, text))),
|
|
7026
7209
|
beforeInput,
|
|
7027
7210
|
});
|
|
7028
7211
|
if (pinRejected)
|
|
7029
7212
|
return { ...r, ok: false, retryable: false, reason: "handoff process/session or continuation authority changed at physical input; carry withheld" };
|
|
7213
|
+
// 带进去的宪章随这一条落定; 失败可重试 (r.ok=false) 的不清 —— 重投的下一条接着带。
|
|
7214
|
+
if (r.ok)
|
|
7215
|
+
a.charterPending = undefined;
|
|
7030
7216
|
if (r.ok && a.tmuxPane && permitted()) {
|
|
7031
7217
|
if (preClearBaseline) {
|
|
7032
7218
|
a.clearRebind = { baseline: preClearBaseline };
|
|
@@ -7357,6 +7543,7 @@ export const startMirror = (deps) => {
|
|
|
7357
7543
|
sessionInfo,
|
|
7358
7544
|
inputSessionInfo,
|
|
7359
7545
|
setCharterProvider,
|
|
7546
|
+
armCharter,
|
|
7360
7547
|
onSpawn,
|
|
7361
7548
|
onContextCut,
|
|
7362
7549
|
shutdown: () => {
|
|
@@ -7588,7 +7775,9 @@ export const startMirror = (deps) => {
|
|
|
7588
7775
|
const resumeBaseline = !armMigration ? listJsonls(dirname(a.jsonlPath)) : undefined;
|
|
7589
7776
|
// Respawn in the binding's runningCwd (pendingCwd doesn't apply to a
|
|
7590
7777
|
// mid-turn reincarnation — only /new and /clear-with-pending swap cwd).
|
|
7591
|
-
const
|
|
7778
|
+
const respawnCli = backendForPath(a.jsonlPath).name;
|
|
7779
|
+
const charter = charterFor(a.target, { cwd: a.runningCwd, cli: respawnCli });
|
|
7780
|
+
const r = await spawnTmuxClaude({ cfg, log: log.child({ sub: "respawn", sessionId: sid }), resumeSessionId: sid, windowName: displayName(a.target) || a.target, cwdOverride: a.runningCwd, chatKey: a.target, cli: respawnCli, ...respawnSpec(a), systemPrompt: charter });
|
|
7592
7781
|
if (byTarget.get(a.target) !== a || a.sessionId !== sid || (a.injectGen ?? 0) !== myGen || (guard && !guard())) {
|
|
7593
7782
|
// A stop or replacement during startup revokes this birth before binding.
|
|
7594
7783
|
if (r.tmuxPane)
|
|
@@ -7615,6 +7804,9 @@ export const startMirror = (deps) => {
|
|
|
7615
7804
|
pendingCwd: a.pendingCwd || undefined,
|
|
7616
7805
|
});
|
|
7617
7806
|
log.info({ target: a.target, sessionId: sid, newPane: a.tmuxPane, newSession: a.tmuxSession }, "mirror: tmux respawned");
|
|
7807
|
+
// 重挂宪章 (同 injectText 的 respawn-init): --resume 只带回对话内容,
|
|
7808
|
+
// 我们的宪章跟着消息走。本条注入 (下面) 会带上它。
|
|
7809
|
+
a.charterPending = respawnCli === "codex" || !charter?.trim() ? undefined : charter;
|
|
7618
7810
|
// Re-bind onto the resume fork once it appears (EOF offset: the fork
|
|
7619
7811
|
// already holds the full prior transcript — replaying from 0 would
|
|
7620
7812
|
// re-dump it). If --resume happens to keep the same jsonl, no new
|
|
@@ -7659,6 +7851,8 @@ export const startMirror = (deps) => {
|
|
|
7659
7851
|
if (!brief)
|
|
7660
7852
|
pushSeal(a, { eventId: a.activeEventId, keys: sealKeys(effText), at: Date.now(), query: effText, channel: channel ?? baseOfKey(principal), speaker, fresh: true });
|
|
7661
7853
|
rememberInject(effText);
|
|
7854
|
+
// 首条注入带上宪章 preamble (印章/去重/轮次问话都记纯正文 —— cleanUserText
|
|
7855
|
+
// 在两侧剥掉 preamble, 认出的仍是同一句, 回执链的认领不受影响)。
|
|
7662
7856
|
// 解除静默必须在 inject 之前: inject 内部的 freshSpawn 验证循环可达 10+s
|
|
7663
7857
|
// (paste-verify + settle + waitForCleared + retry), 而 LLM 的回复可能在 Enter
|
|
7664
7858
|
// 发出后 2-3s 就落盘 —— tail 在 mute 期间读到的 item 会被 onItem 永久丢弃,
|
|
@@ -7672,7 +7866,7 @@ export const startMirror = (deps) => {
|
|
|
7672
7866
|
// 贴进运行中的一轮会被记成 queued 轮, 等它在 transcript 露面再接上这条 event。
|
|
7673
7867
|
// slash 命令要等闲, 那一道在上面 (dispatch 开头) 已经拦过了。
|
|
7674
7868
|
let r = await inject({
|
|
7675
|
-
text: effText, images: effImages, cfg, log: log.child({ principal, sessionId: sid }),
|
|
7869
|
+
text: charterPrefixOf(a, effText) + effText, images: effImages, cfg, log: log.child({ principal, sessionId: sid }),
|
|
7676
7870
|
sessionId: sid, eventId, jsonlPath: a.jsonlPath, tmuxTarget: a.tmuxPane, freshSpawn,
|
|
7677
7871
|
requireIdle: eventId ? () => true : undefined,
|
|
7678
7872
|
guard: (phase, identity) => byTarget.get(a.target) === a && a.tmuxPane === injectPane && (a.injectGen ?? 0) === myGen && (!guard || guard(phase, identity)),
|
|
@@ -7688,6 +7882,11 @@ export const startMirror = (deps) => {
|
|
|
7688
7882
|
log.info({ target: a.target, eventId, reason: r.reason }, "mirror inject: late failure superseded by event submission evidence");
|
|
7689
7883
|
r = { ok: true };
|
|
7690
7884
|
}
|
|
7885
|
+
// 宪章已随这一条进了 pane; 失败可重试的 (消息事件层会重投) 不清 —— 重投接着带。
|
|
7886
|
+
// /clear 例外: 它自己不带 preamble, 而 tail 认出会话轮换 → contextCut → armCharter
|
|
7887
|
+
// 可能先于这里的返回发生, 清了就会把刚挂上的下一份宪章抹掉。
|
|
7888
|
+
if (r.ok && !armMigration)
|
|
7889
|
+
a.charterPending = undefined;
|
|
7691
7890
|
onSubmitted?.(r);
|
|
7692
7891
|
if (!r.ok) {
|
|
7693
7892
|
// retryable = 一个字都没进输入框, 消息事件层会稍后重投 —— 不是失败, 别报 ✗
|