shennian 0.4.75 → 0.4.77

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.
@@ -46,8 +46,8 @@
46
46
  },
47
47
  {
48
48
  "file": "src/agents/acp/discovery.js",
49
- "beforeBytes": 9943,
50
- "afterBytes": 4539
49
+ "beforeBytes": 10037,
50
+ "afterBytes": 4586
51
51
  },
52
52
  {
53
53
  "file": "src/agents/acp/events.js",
@@ -176,8 +176,8 @@
176
176
  },
177
177
  {
178
178
  "file": "src/agents/detect.js",
179
- "beforeBytes": 3767,
180
- "afterBytes": 1707
179
+ "beforeBytes": 4019,
180
+ "afterBytes": 1827
181
181
  },
182
182
  {
183
183
  "file": "src/agents/event-terminal-gate.js",
@@ -311,8 +311,8 @@
311
311
  },
312
312
  {
313
313
  "file": "src/commands/agent-readiness.js",
314
- "beforeBytes": 3718,
315
- "afterBytes": 3018
314
+ "beforeBytes": 3848,
315
+ "afterBytes": 3118
316
316
  },
317
317
  {
318
318
  "file": "src/commands/agent.js",
@@ -416,8 +416,8 @@
416
416
  },
417
417
  {
418
418
  "file": "src/commands/worker.js",
419
- "beforeBytes": 6654,
420
- "afterBytes": 4212
419
+ "beforeBytes": 6706,
420
+ "afterBytes": 4264
421
421
  },
422
422
  {
423
423
  "file": "src/config/index.js",
@@ -426,8 +426,8 @@
426
426
  },
427
427
  {
428
428
  "file": "src/daemon-ipc/agent-readiness.js",
429
- "beforeBytes": 4154,
430
- "afterBytes": 2206
429
+ "beforeBytes": 4397,
430
+ "afterBytes": 2308
431
431
  },
432
432
  {
433
433
  "file": "src/daemon-ipc/client.js",
@@ -496,8 +496,8 @@
496
496
  },
497
497
  {
498
498
  "file": "src/index.js",
499
- "beforeBytes": 41017,
500
- "afterBytes": 20498
499
+ "beforeBytes": 41850,
500
+ "afterBytes": 20757
501
501
  },
502
502
  {
503
503
  "file": "src/log-redaction.js",
@@ -526,13 +526,13 @@
526
526
  },
527
527
  {
528
528
  "file": "src/manager/runtime.js",
529
- "beforeBytes": 35179,
530
- "afterBytes": 17108
529
+ "beforeBytes": 35299,
530
+ "afterBytes": 17196
531
531
  },
532
532
  {
533
533
  "file": "src/manager/worker-ipc.js",
534
- "beforeBytes": 20466,
535
- "afterBytes": 11563
534
+ "beforeBytes": 20321,
535
+ "afterBytes": 11379
536
536
  },
537
537
  {
538
538
  "file": "src/manager/worker-markdown.js",
@@ -851,8 +851,8 @@
851
851
  },
852
852
  {
853
853
  "file": "src/session/handlers/agents.js",
854
- "beforeBytes": 2481,
855
- "afterBytes": 1211
854
+ "beforeBytes": 2536,
855
+ "afterBytes": 1240
856
856
  },
857
857
  {
858
858
  "file": "src/session/handlers/chat-adapter-config.js",
@@ -956,8 +956,8 @@
956
956
  },
957
957
  {
958
958
  "file": "src/session/manager.js",
959
- "beforeBytes": 42836,
960
- "afterBytes": 22584
959
+ "beforeBytes": 42645,
960
+ "afterBytes": 22256
961
961
  },
962
962
  {
963
963
  "file": "src/session/native-cleanup-outbox.js",
@@ -6,4 +6,5 @@ export declare function discoverAcpAgents(options?: {
6
6
  serverUrl?: string;
7
7
  authToken?: string;
8
8
  fetchImpl?: typeof fetch;
9
+ agentType?: string;
9
10
  }): Promise<DetectedAgent[]>;
@@ -1 +1 @@
1
- import{RequestError as R}from"@agentclientprotocol/sdk";import{buildCommandStringLaunchSpec as T,commandExists as E,resolveCommandFileIdentity as F,resolveCommandFilePath as P}from"../command-spec.js";import{registerAcpAgent as x}from"./adapter.js";import{getRegisteredAgents as L,unregisterAgent as N}from"../adapter.js";import{readAcpManifests as D}from"./manifests.js";import{AcpProcessClient as _}from"./process-client.js";import{acpAgentType as h}from"./types.js";import{loadAcpCatalog as q}from"./catalog.js";import{buildAcpCapabilityCacheKey as K,readAcpCapabilityCache as O,writeAcpCapabilityCache as W}from"./capability-cache.js";import{getAcpRuntimeAuthState as I,setAcpRuntimeAuthState as $}from"./auth-state.js";import{recordAcpScan as H}from"./metrics.js";import{fingerprintNativeExecutable as U}from"../../security/managed-agent-executable.js";const V=[{id:"qwen-code",name:"Qwen Code",source:"registry",launch:{command:"qwen",args:["--acp"]}},{id:"kimi",name:"Kimi CLI",source:"registry",launch:{command:"kimi",args:["acp"]}}],j=new Set(["claude","codex","codebuddy","cursor","opencode","pi"]);async function le(t={}){const i=Date.now();let o=0,n=0;const a={},f=new Set(L().filter(e=>e.startsWith("acp:"))),c=t.candidates?{candidates:t.candidates,revision:void 0}:await q(t),A=G([...c.candidates.length?c.candidates:V,...D()]),B=A.filter(e=>{const l=e.launch.command;return j.has(l)?!1:E(l)}).filter((e,l,g)=>g.findIndex(v=>v.id===e.id)===l),C=(await J(B,3,async e=>{const l=P(e.launch.command),g=l?U(l):void 0,u={...T(e.launch.command,e.launch.args??[],t.cwd??process.cwd()),...e.launch.env?{env:e.launch.env}:{}},y=K({manifest:e,platform:process.platform,catalogRevision:c.revision,executableIdentity:F(e.launch.command)}),d=y?O(y):null;if(d?.type===h(e.id)&&d.integration==="acp"&&d.acpCapabilities?.protocolVersion===1)return o+=1,x({...e,launch:u,authLaunch:e.launch}),{...d,authState:I(h(e.id))??d.authState,command:u.command,args:u.args??[],executableFingerprint:g,display:{...d.display,name:e.name},...c.revision?{catalogRevision:c.revision}:{}};const w=new _(u,{onSessionUpdate:()=>{},onPermission:async()=>({outcome:{outcome:"cancelled"}})},process.env,{terminalAuth:!0});try{const r=await w.start(t.cwd??process.cwd()),s=r.agentCapabilities??{},M={protocolVersion:1,listSessions:!!s.sessionCapabilities?.list,loadSession:!!s.loadSession,resumeSession:!!s.sessionCapabilities?.resume,closeSession:!!s.sessionCapabilities?.close,deleteSession:!!s.sessionCapabilities?.delete,terminal:!!e.clientCapabilities?.terminal,fileRead:!!e.clientCapabilities?.readTextFile,fileWrite:!!e.clientCapabilities?.writeTextFile,permission:!0,modelConfig:!1};let p=r.authMethods?.length?"unknown":"ready";if(s.sessionCapabilities?.list)try{await w.listSessions(t.cwd??process.cwd()),p=r.authMethods?.length?"unknown":"ready"}catch(m){p=m instanceof R&&m.code===-32e3?"required":"unknown"}const S=I(h(e.id));S?p=S:p==="required"&&$(h(e.id),p),x({...e,launch:u,authLaunch:e.launch});const b={type:h(e.id),command:u.command,args:u.args??[],version:r.agentInfo?.version??void 0,runtimePlatform:process.platform,executableFingerprint:g,integration:"acp",display:{name:e.name,version:r.agentInfo?.version??void 0},acpCapabilities:M,authState:p,acpAuthMethods:(r.authMethods??[]).slice(0,8).map(m=>({id:m.id.slice(0,128),name:m.name.slice(0,80),type:"type"in m&&m.type==="terminal"?"terminal":"agent"})),...c.revision?{catalogRevision:c.revision}:{}};return y&&W(y,b),b}catch(r){n+=1;const s=z(r);return a[s]=(a[s]??0)+1,null}finally{await w.close()}})).filter(e=>!!e),k=new Set(C.map(e=>e.type));for(const e of f)k.has(e)||N(e);return H({durationMs:Date.now()-i,candidateCount:A.length,installedCount:C.length,cacheHits:o,handshakeFailures:n,handshakeFailuresByKind:a}),C}function z(t){const i=t instanceof Error?t.message:"";return/timed out/i.test(i)?"timeout":/protocol version/i.test(i)?"version":/safety limit|exceeded/i.test(i)?"output_limit":/json|parse/i.test(i)?"invalid_protocol":/enoent|not found/i.test(i)?"launch":"other"}function G(t){const i=new Set(t.filter(n=>n.source==="local").map(n=>n.id)),o=new Map;for(const n of t){if(n.source!=="local"&&i.has(n.id))continue;const a=`${n.id}\0${n.launch.command}\0${JSON.stringify(n.launch.args??[])}`;o.has(a)||o.set(a,n)}return[...o.values()]}async function J(t,i,o){const n=new Array(t.length);let a=0;return await Promise.all(Array.from({length:Math.min(i,t.length)},async()=>{for(;;){const f=a++;if(f>=t.length)return;n[f]=await o(t[f])}})),n}export{le as discoverAcpAgents};
1
+ import{RequestError as M}from"@agentclientprotocol/sdk";import{buildCommandStringLaunchSpec as R,commandExists as E,resolveCommandFileIdentity as F,resolveCommandFilePath as P}from"../command-spec.js";import{registerAcpAgent as x}from"./adapter.js";import{getRegisteredAgents as L,unregisterAgent as N}from"../adapter.js";import{readAcpManifests as D}from"./manifests.js";import{AcpProcessClient as _}from"./process-client.js";import{acpAgentType as f}from"./types.js";import{loadAcpCatalog as q}from"./catalog.js";import{buildAcpCapabilityCacheKey as K,readAcpCapabilityCache as O,writeAcpCapabilityCache as W}from"./capability-cache.js";import{getAcpRuntimeAuthState as I,setAcpRuntimeAuthState as $}from"./auth-state.js";import{recordAcpScan as H}from"./metrics.js";import{fingerprintNativeExecutable as U}from"../../security/managed-agent-executable.js";const V=[{id:"qwen-code",name:"Qwen Code",source:"registry",launch:{command:"qwen",args:["--acp"]}},{id:"kimi",name:"Kimi CLI",source:"registry",launch:{command:"kimi",args:["acp"]}}],j=new Set(["claude","codex","codebuddy","cursor","opencode","pi"]);async function le(t={}){const i=Date.now();let s=0,n=0;const o={},h=new Set(L().filter(e=>e.startsWith("acp:"))),c=t.candidates?{candidates:t.candidates,revision:void 0}:await q(t),A=G([...c.candidates.length?c.candidates:V,...D()]).filter(e=>!t.agentType||f(e.id)===t.agentType),B=A.filter(e=>{const l=e.launch.command;return j.has(l)?!1:E(l)}).filter((e,l,g)=>g.findIndex(v=>v.id===e.id)===l),C=(await J(B,3,async e=>{const l=P(e.launch.command),g=l?U(l):void 0,u={...R(e.launch.command,e.launch.args??[],t.cwd??process.cwd()),...e.launch.env?{env:e.launch.env}:{}},y=K({manifest:e,platform:process.platform,catalogRevision:c.revision,executableIdentity:F(e.launch.command)}),d=y?O(y):null;if(d?.type===f(e.id)&&d.integration==="acp"&&d.acpCapabilities?.protocolVersion===1)return s+=1,x({...e,launch:u,authLaunch:e.launch}),{...d,authState:I(f(e.id))??d.authState,command:u.command,args:u.args??[],executableFingerprint:g,display:{...d.display,name:e.name},...c.revision?{catalogRevision:c.revision}:{}};const w=new _(u,{onSessionUpdate:()=>{},onPermission:async()=>({outcome:{outcome:"cancelled"}})},process.env,{terminalAuth:!0});try{const r=await w.start(t.cwd??process.cwd()),a=r.agentCapabilities??{},k={protocolVersion:1,listSessions:!!a.sessionCapabilities?.list,loadSession:!!a.loadSession,resumeSession:!!a.sessionCapabilities?.resume,closeSession:!!a.sessionCapabilities?.close,deleteSession:!!a.sessionCapabilities?.delete,terminal:!!e.clientCapabilities?.terminal,fileRead:!!e.clientCapabilities?.readTextFile,fileWrite:!!e.clientCapabilities?.writeTextFile,permission:!0,modelConfig:!1};let p=r.authMethods?.length?"unknown":"ready";if(a.sessionCapabilities?.list)try{await w.listSessions(t.cwd??process.cwd()),p=r.authMethods?.length?"unknown":"ready"}catch(m){p=m instanceof M&&m.code===-32e3?"required":"unknown"}const S=I(f(e.id));S?p=S:p==="required"&&$(f(e.id),p),x({...e,launch:u,authLaunch:e.launch});const b={type:f(e.id),command:u.command,args:u.args??[],version:r.agentInfo?.version??void 0,runtimePlatform:process.platform,executableFingerprint:g,integration:"acp",display:{name:e.name,version:r.agentInfo?.version??void 0},acpCapabilities:k,authState:p,acpAuthMethods:(r.authMethods??[]).slice(0,8).map(m=>({id:m.id.slice(0,128),name:m.name.slice(0,80),type:"type"in m&&m.type==="terminal"?"terminal":"agent"})),...c.revision?{catalogRevision:c.revision}:{}};return y&&W(y,b),b}catch(r){n+=1;const a=z(r);return o[a]=(o[a]??0)+1,null}finally{await w.close()}})).filter(e=>!!e),T=new Set(C.map(e=>e.type));for(const e of h)T.has(e)||N(e);return H({durationMs:Date.now()-i,candidateCount:A.length,installedCount:C.length,cacheHits:s,handshakeFailures:n,handshakeFailuresByKind:o}),C}function z(t){const i=t instanceof Error?t.message:"";return/timed out/i.test(i)?"timeout":/protocol version/i.test(i)?"version":/safety limit|exceeded/i.test(i)?"output_limit":/json|parse/i.test(i)?"invalid_protocol":/enoent|not found/i.test(i)?"launch":"other"}function G(t){const i=new Set(t.filter(n=>n.source==="local").map(n=>n.id)),s=new Map;for(const n of t){if(n.source!=="local"&&i.has(n.id))continue;const o=`${n.id}\0${n.launch.command}\0${JSON.stringify(n.launch.args??[])}`;s.has(o)||s.set(o,n)}return[...s.values()]}async function J(t,i,s){const n=new Array(t.length);let o=0;return await Promise.all(Array.from({length:Math.min(i,t.length)},async()=>{for(;;){const h=o++;if(h>=t.length)return;n[h]=await s(t[h])}})),n}export{le as discoverAcpAgents};
@@ -19,4 +19,5 @@ export declare function isPiCodingAgentRuntimeCompatible(piVersion: string | und
19
19
  export declare function detectAgents(options?: {
20
20
  serverUrl?: string;
21
21
  authToken?: string;
22
+ agentType?: AgentType;
22
23
  }): Promise<DetectedAgent[]>;
@@ -1 +1 @@
1
- import{probeBuiltinAuthStatus as c}from"./auth-status.js";import{AVAILABLE_BUILTIN_AGENT_TYPES as m}from"@shennian/wire";import{loadConfig as u}from"../config/index.js";import{getBuiltinAgentVersion as a,resolveBuiltinCommand as l}from"./command-spec.js";import{registerCustomRuntimeSecurity as f,resolveCustomRuntimeExecutableFingerprint as p}from"./custom-managed-runtime.js";import{resolveManagedAgentExecutableIdentity as d}from"../security/managed-agent-executable.js";import{discoverAcpAgents as g}from"./acp/discovery.js";const A=m.filter(n=>n!=="pi");function v(n,o){const i=n?.match(/(?:^|v)(\d+)\.(\d+)\.(\d+)/);if(!i)return!1;const t=i.slice(1,4).map(Number);for(let e=0;e<o.length;e+=1)if(t[e]!==o[e])return t[e]>o[e];return!0}function x(n){return v(n,[0,85,0])}function b(n){const o=n.models?.length?n.models:n.model?[n.model]:[],i=n.defaultModel??n.model;return o.map(t=>({id:t,name:t,isDefault:t===i}))}async function B(n={}){const o=[];for(const t of A){const e=l(t);if(!e)continue;const r=a(t,e);if((t==="codex"||t==="claude")&&!r||t==="pi-coding-agent"&&!x(r))continue;const s=d(e);o.push({type:t,command:e.command,args:e.args,version:r,runtimePlatform:s.platform,runtimeIsWsl:s.isWsl,executableFingerprint:s.executableFingerprint,authState:await c(t,e,r)})}const i=u();for(const[t,e]of Object.entries(i.customAgents??{})){const r=`custom:${t}`;f(r,e),o.push({type:r,command:e.command,args:[],version:e.caps.version,runtimePlatform:process.platform,runtimeIsWsl:process.platform==="linux"&&!!(process.env.WSL_DISTRO_NAME||process.env.WSL_INTEROP),executableFingerprint:p(e),models:b(e.caps)})}return o.push(...await g(n)),o}export{B as detectAgents,x as isPiCodingAgentRuntimeCompatible};
1
+ import{probeBuiltinAuthStatus as a}from"./auth-status.js";import{AVAILABLE_BUILTIN_AGENT_TYPES as c}from"@shennian/wire";import{loadConfig as m}from"../config/index.js";import{getBuiltinAgentVersion as u,resolveBuiltinCommand as l}from"./command-spec.js";import{registerCustomRuntimeSecurity as f,resolveCustomRuntimeExecutableFingerprint as g}from"./custom-managed-runtime.js";import{resolveManagedAgentExecutableIdentity as p}from"../security/managed-agent-executable.js";import{discoverAcpAgents as d}from"./acp/discovery.js";const A=c.filter(e=>e!=="pi");function y(e,o){const i=e?.match(/(?:^|v)(\d+)\.(\d+)\.(\d+)/);if(!i)return!1;const n=i.slice(1,4).map(Number);for(let t=0;t<o.length;t+=1)if(n[t]!==o[t])return n[t]>o[t];return!0}function T(e){return y(e,[0,85,0])}function v(e){const o=e.models?.length?e.models:e.model?[e.model]:[],i=e.defaultModel??e.model;return o.map(n=>({id:n,name:n,isDefault:n===i}))}async function B(e={}){const o=[];for(const n of A){if(e.agentType&&e.agentType!==n)continue;const t=l(n);if(!t)continue;const r=u(n,t);if((n==="codex"||n==="claude")&&!r||n==="pi-coding-agent"&&!T(r))continue;const s=p(t);o.push({type:n,command:t.command,args:t.args,version:r,runtimePlatform:s.platform,runtimeIsWsl:s.isWsl,executableFingerprint:s.executableFingerprint,authState:await a(n,t,r)})}const i=m();for(const[n,t]of Object.entries(i.customAgents??{})){const r=`custom:${n}`;e.agentType&&e.agentType!==r||(f(r,t),o.push({type:r,command:t.command,args:[],version:t.caps.version,runtimePlatform:process.platform,runtimeIsWsl:process.platform==="linux"&&!!(process.env.WSL_DISTRO_NAME||process.env.WSL_INTEROP),executableFingerprint:g(t),models:v(t.caps)}))}return(!e.agentType||e.agentType.startsWith("acp:"))&&o.push(...await d(e)),o}export{B as detectAgents,T as isPiCodingAgentRuntimeCompatible};
@@ -1,2 +1,2 @@
1
- import{callDaemonIpc as a}from"../daemon-ipc/client.js";import{DaemonIpcError as r}from"../daemon-ipc/protocol.js";const s={method_not_found:"Update the existing Shennian installation: Desktop through Desktop updates, standalone CLI through its own update flow. Do not install a second Client.",daemon_unavailable:"Check shennian status --json on the target computer. Do not resume an intentional pause without the user requesting it.",daemon_paused:"Shennian is paused. Ask the user to resume it before checking.",ipc_timeout:"The result is unknown. Check the target machine and wait for the current check to finish; do not repeatedly send tests.",agent_check_in_progress:"Wait for the running check to finish before trying again.",agent_validation_runtime_unavailable:"Refresh discovery. If the tool is still missing, guide the user through its official installation on this computer.",agent_validation_runtime_auth_required:"Guide the user through the provider\u2019s own sign-in on this computer, then retry the selected AI and model.",agent_validation_model_unavailable:"Choose a model returned by discovery and check the provider account\u2019s model access.",agent_validation_quota_exceeded:"Check the provider account\u2019s credits, subscription or rate limit before another test.",agent_validation_network_failed:"Check the target computer\u2019s connection to the provider, then retry when it is available."};async function c(n,i,t=a){try{const e=await t({method:n,params:i,timeoutMs:9e4});if(n==="agent.check"&&(!e||typeof e!="object"||Array.isArray(e)||e.ready!==!0))throw new r("agent_check_invalid_response","The daemon did not confirm a successful hello reply.");return{ok:!0,command:n,result:e}}catch(e){const o=e instanceof r?e.code:"agent_check_failed";return{ok:!1,command:n,error:o,nextAction:s[o]??"Return to the AI preparation guide on this machine. Check the selected directory, account, model and network; do not reinstall by default."}}}function l(n){const i=async(t,e)=>{const o=await c(t,e);process.stdout.write(`${JSON.stringify(o)}
2
- `),o.ok||(process.exitCode=1)};n.command("discover").description("Refresh AI discovery and sign-in summaries on the running local daemon; no model requests").option("--agent <type>","Return only one discovered Agent type").option("--compact","Return capability summary without the full model list").option("--json","Print JSON (default)").action(t=>i("agent.discover",{...t.agent?{agentType:t.agent}:{},compact:t.compact===!0})),n.command("check <agentType>").description("Send one hello test using the selected AI and model; uses provider quota, creates no managed Agent or room").requiredOption("--workdir <path>","Absolute project directory on this machine").option("--model <id>","Model returned by discovery; otherwise use the provider default").option("--json","Print JSON (default)").action((t,e)=>i("agent.check",{agentType:t,workDir:e.workdir,modelId:e.model??null}))}export{c as executeAgentReadinessCommand,l as registerAgentReadinessCommands};
1
+ import{callDaemonIpc as a}from"../daemon-ipc/client.js";import{DaemonIpcError as i}from"../daemon-ipc/protocol.js";const s={method_not_found:"Update the existing Shennian installation: Desktop through Desktop updates, standalone CLI through its own update flow. Do not install a second Client.",daemon_unavailable:"Check shennian status --json on the target computer. Do not resume an intentional pause without the user requesting it.",daemon_paused:"Shennian is paused. Ask the user to resume it before checking.",ipc_timeout:"The result is unknown. Check the target machine and wait for the current check to finish; do not repeatedly send tests.",agent_check_in_progress:"Wait for the running check to finish before trying again.",agent_validation_runtime_unavailable:"Refresh discovery. If the tool is still missing, guide the user through its official installation on this computer.",agent_validation_runtime_auth_required:"Guide the user through the provider\u2019s own sign-in on this computer, then retry the selected AI and model.",agent_validation_model_unavailable:"Choose a model returned by discovery and check the provider account\u2019s model access.",agent_validation_quota_exceeded:"Check the provider account\u2019s credits, subscription or rate limit before another test.",agent_validation_network_failed:"Check the target computer\u2019s connection to the provider, then retry when it is available."};async function c(o,r,t=a){try{const e=await t({method:o,params:r,timeoutMs:9e4});if(o==="agent.check"&&(!e||typeof e!="object"||Array.isArray(e)||e.ready!==!0))throw new i("agent_check_invalid_response","The daemon did not confirm a successful hello reply.");return{ok:!0,command:o,result:e}}catch(e){const n=e instanceof i?e.code:"agent_check_failed";return{ok:!1,command:o,error:n,nextAction:s[n]??"Return to the AI preparation guide on this machine. Check the selected directory, account, model and network; do not reinstall by default."}}}function h(o){const r=async(t,e)=>{const n=await c(t,e);process.stdout.write(`${JSON.stringify(n)}
2
+ `),n.ok||(process.exitCode=1)};o.command("discover").description("Read the daemon AI snapshot; use --refresh only to rerun local provider and model discovery").option("--agent <type>","Return only one discovered Agent type").option("--compact","Return capability summary without the full model list").option("--refresh","Rerun discovery; with --agent, probe only that Agent").option("--json","Print JSON (default)").action(t=>r("agent.discover",{...t.agent?{agentType:t.agent}:{},compact:t.compact===!0,refresh:t.refresh===!0})),o.command("check <agentType>").description("Send one hello test using the selected AI and model; uses provider quota, creates no managed Agent or room").requiredOption("--workdir <path>","Absolute project directory on this machine").option("--model <id>","Model returned by discovery; otherwise use the provider default").option("--json","Print JSON (default)").action((t,e)=>r("agent.check",{agentType:t,workDir:e.workdir,modelId:e.model??null}))}export{c as executeAgentReadinessCommand,h as registerAgentReadinessCommands};
@@ -1,2 +1,2 @@
1
- import c from"node:fs";import{resolveShennianPath as d}from"../config/index.js";function u(){const r=d("runtime","manager-ipc.json");let t;try{t=c.statSync(r)}catch{throw new Error("daemon_unavailable")}if(process.platform!=="win32"){const o=typeof process.getuid=="function"?process.getuid():t.uid;if(t.uid!==o||(t.mode&63)!==0)throw new Error("worker_runtime_permissions_invalid")}const e=JSON.parse(c.readFileSync(r,"utf-8")),n=typeof e.url=="string"?e.url:"",a=typeof e.token=="string"?e.token:"";if(!/^http:\/\/127\.0\.0\.1:\d+$/.test(n)||!a)throw new Error("daemon_unavailable");return{url:n,token:a}}function f(r){return r==="daemon_unavailable"?"Check shennian status --json and start the existing paired daemon if it was not intentionally paused.":r==="worker_runtime_permissions_invalid"?"Repair or update the existing Shennian installation; do not bypass the local runtime file permissions.":"Check the local Shennian daemon and retry the same command once."}async function i(r,t){try{const e=u(),n=r==="wait"?Math.max(2e3,Math.min(Number(t.timeoutMs||6e5)+5e3,605e3)):r==="start"||r==="send"?18e4:15e3,a=await fetch(`${e.url}/workers/${r}`,{method:"POST",headers:{authorization:`Bearer ${e.token}`,"content-type":"application/json"},body:JSON.stringify(t),signal:AbortSignal.timeout(n)}),o=await a.json().catch(()=>({ok:!1,error:"worker_invalid_response",message:a.statusText}));return a.ok&&o.ok?o:{ok:!1,error:o.error||"worker_command_failed",...o.message?{message:o.message}:{},nextAction:o.nextAction||"Check the selected Agent, working directory, account, and Worker reference."}}catch(e){const n=e instanceof Error&&e.message==="daemon_unavailable"||e instanceof Error&&e.message==="worker_runtime_permissions_invalid"?e.message:e instanceof Error&&e.name==="TimeoutError"?"ipc_timeout":"worker_command_failed";return{ok:!1,error:n,message:e instanceof Error?e.message:String(e),nextAction:f(n)}}}function m(r){return r.messageFile?c.readFileSync(r.messageFile,"utf8"):r.message??""}function s(r){process.stdout.write(`${JSON.stringify(r)}
2
- `),r.ok||(process.exitCode=1)}function k(r){const t=r.command("worker").description("Start and control an external temporary Worker by opaque reference");t.command("start").description("Start a new Worker; returns the only reference that can control it").requiredOption("--agent <agent>","Agent type returned by shennian agent discover").option("--workdir <path>","Working directory; defaults to the current directory").option("--title <title>","Short Worker title").option("--model <model>","Model id returned by discovery").option("--message <text>","Worker task").option("--message-file <path>","Read the Worker task from a file").option("--json","Print JSON (default)").action(async e=>s(await i("start",{agentType:e.agent,workDir:e.workdir??process.cwd(),title:e.title,modelId:e.model,message:m(e)}))),t.command("read").description("Read a Worker transcript using the reference returned by worker start").requiredOption("--ref <worker-ref>","Opaque Worker reference").option("--limit <n>","Message limit","200").option("--json","Print JSON (default)").action(async e=>s(await i("read",{workerRef:e.ref,limit:Number(e.limit)}))),t.command("send").description("Queue another message for a Worker").requiredOption("--ref <worker-ref>","Opaque Worker reference").option("--message <text>","Message text").option("--message-file <path>","Read the message from a file").option("--json","Print JSON (default)").action(async e=>s(await i("send",{workerRef:e.ref,message:m(e)}))),t.command("wait").description("Wait once for Worker progress or a terminal event").requiredOption("--ref <worker-ref>","Opaque Worker reference").option("--timeout <seconds>","Maximum wait in seconds (1-600)","600").option("--json","Print JSON (default)").action(async e=>{const n=Math.max(1,Math.min(Number(e.timeout)||600,600));s(await i("wait",{workerRef:e.ref,timeoutMs:n*1e3}))}),t.command("stop").description("Stop a Worker using its opaque reference").requiredOption("--ref <worker-ref>","Opaque Worker reference").option("--json","Print JSON (default)").action(async e=>s(await i("stop",{workerRef:e.ref})))}export{i as executeWorkerCommand,k as registerWorkerCommand};
1
+ import c from"node:fs";import{resolveShennianPath as d}from"../config/index.js";function u(){const r=d("runtime","manager-ipc.json");let t;try{t=c.statSync(r)}catch{throw new Error("daemon_unavailable")}if(process.platform!=="win32"){const n=typeof process.getuid=="function"?process.getuid():t.uid;if(t.uid!==n||(t.mode&63)!==0)throw new Error("worker_runtime_permissions_invalid")}const e=JSON.parse(c.readFileSync(r,"utf-8")),o=typeof e.url=="string"?e.url:"",a=typeof e.token=="string"?e.token:"";if(!/^http:\/\/127\.0\.0\.1:\d+$/.test(o)||!a)throw new Error("daemon_unavailable");return{url:o,token:a}}function f(r){return r==="daemon_unavailable"?"Check shennian status --json and start the existing paired daemon if it was not intentionally paused.":r==="worker_runtime_permissions_invalid"?"Repair or update the existing Shennian installation; do not bypass the local runtime file permissions.":"Check the local Shennian daemon and retry the same command once."}async function i(r,t){try{const e=u(),o=r==="wait"?Math.max(2e3,Math.min(Number(t.timeoutMs||6e5)+5e3,605e3)):r==="start"||r==="send"?18e4:15e3,a=await fetch(`${e.url}/workers/${r}`,{method:"POST",headers:{authorization:`Bearer ${e.token}`,"content-type":"application/json"},body:JSON.stringify(t),signal:AbortSignal.timeout(o)}),n=await a.json().catch(()=>({ok:!1,error:"worker_invalid_response",message:a.statusText}));return a.ok&&n.ok?n:{ok:!1,error:n.error||"worker_command_failed",...n.message?{message:n.message}:{},nextAction:n.nextAction||"Check the selected Agent, working directory, account, and Worker reference."}}catch(e){const o=e instanceof Error&&e.message==="daemon_unavailable"||e instanceof Error&&e.message==="worker_runtime_permissions_invalid"?e.message:e instanceof Error&&e.name==="TimeoutError"?"ipc_timeout":"worker_command_failed";return{ok:!1,error:o,message:e instanceof Error?e.message:String(e),nextAction:f(o)}}}function m(r){return r.messageFile?c.readFileSync(r.messageFile,"utf8"):r.message??""}function s(r){process.stdout.write(`${JSON.stringify(r)}
2
+ `),r.ok||(process.exitCode=1)}function k(r){const t=r.command("worker").description("Start and control an external temporary Worker by opaque reference");t.command("start").description("Start a new Worker; returns the only reference that can control it").requiredOption("--agent <agent>","Agent type in the daemon snapshot, for example claude").option("--workdir <path>","Working directory; defaults to the current directory").option("--title <title>","Short Worker title").option("--model <model>","Optional model id from the daemon snapshot; otherwise use the Agent default").option("--message <text>","Worker task").option("--message-file <path>","Read the Worker task from a file").option("--json","Print JSON (default)").action(async e=>s(await i("start",{agentType:e.agent,workDir:e.workdir??process.cwd(),title:e.title,modelId:e.model,message:m(e)}))),t.command("read").description("Read a Worker transcript using the reference returned by worker start").requiredOption("--ref <worker-ref>","Opaque Worker reference").option("--limit <n>","Message limit","200").option("--json","Print JSON (default)").action(async e=>s(await i("read",{workerRef:e.ref,limit:Number(e.limit)}))),t.command("send").description("Queue another message for a Worker").requiredOption("--ref <worker-ref>","Opaque Worker reference").option("--message <text>","Message text").option("--message-file <path>","Read the message from a file").option("--json","Print JSON (default)").action(async e=>s(await i("send",{workerRef:e.ref,message:m(e)}))),t.command("wait").description("Wait once for Worker progress or a terminal event").requiredOption("--ref <worker-ref>","Opaque Worker reference").option("--timeout <seconds>","Maximum wait in seconds (1-600)","600").option("--json","Print JSON (default)").action(async e=>{const o=Math.max(1,Math.min(Number(e.timeout)||600,600));s(await i("wait",{workerRef:e.ref,timeoutMs:o*1e3}))}),t.command("stop").description("Stop a Worker using its opaque reference").requiredOption("--ref <worker-ref>","Opaque Worker reference").option("--json","Print JSON (default)").action(async e=>s(await i("stop",{workerRef:e.ref})))}export{i as executeWorkerCommand,k as registerWorkerCommand};
@@ -8,7 +8,8 @@ type DiscoveredAi = AgentInfo & {
8
8
  };
9
9
  export declare function createAgentReadinessHandlers(input: {
10
10
  getStatus: () => DaemonStatus;
11
- discover: () => Promise<DiscoveredAi[]>;
11
+ getSnapshot: () => DiscoveredAi[];
12
+ discover: (agentType?: string) => Promise<DiscoveredAi[]>;
12
13
  validate: (params: AgentWorkspaceValidationInput) => Promise<AgentWorkspaceValidationResult>;
13
14
  }): Record<string, DaemonIpcHandler>;
14
15
  export {};
@@ -1 +1 @@
1
- import{isAvailableAgentType as a}from"@shennian/wire";import{DaemonIpcError as n}from"./protocol.js";function c(i){let s=!1;const l=()=>{const e=i.getStatus();if(e.remoteAccessDisabled)throw new n("daemon_paused","Shennian is paused; resume it only when requested.");if(!e.running)throw new n("daemon_unavailable","Shennian is not running.")};return{"agent.discover":async e=>{l();const r=e&&typeof e=="object"&&!Array.isArray(e)&&typeof e.agentType=="string"?e.agentType:null,u=!!(e&&typeof e=="object"&&!Array.isArray(e)&&e.compact===!0),d=(await i.discover()).filter(t=>!r||t.type===r);return{modelTested:!1,compact:u,agents:d.map(t=>({type:t.type,name:t.display?.name??t.type,authState:t.authState??"unknown",...t.runtime?.cliVersion?{version:t.runtime.cliVersion}:{},...t.runtime?.compatibility?{compatibility:t.runtime.compatibility}:{},...t.runtimePlatform?{runtimePlatform:t.runtimePlatform}:{},...t.runtimeIsWsl!==void 0?{runtimeIsWsl:t.runtimeIsWsl}:{},supportsStandardSession:t.supportsStandardSession===!0,supportsManagedAgent:t.supportsManagedAgent===!0,supportsManagedNativeFull:t.supportsManagedNativeFull===!0,supportsRoomTools:t.supportsRoomTools===!0,supportsWorker:t.supportsWorker===!0,supportsManagerMode:t.supportsManagerMode===!0,supportsExternalWorkerTarget:t.supportsExternalWorkerTarget===!0,...u?{modelCount:t.models.length,defaultModelId:t.models.find(o=>o.isDefault)?.id??null}:{models:t.models.map(o=>({id:o.id,name:o.name,isDefault:o.isDefault===!0}))}}))}},"agent.check":async e=>{if(l(),!e||typeof e!="object"||Array.isArray(e)||typeof e.agentType!="string"||!a(e.agentType)||typeof e.workDir!="string"||!e.workDir.trim()||e.modelId!=null&&typeof e.modelId!="string")throw new n("agent_validation_invalid_configuration","Select an AI and its absolute working directory.");if(s)throw new n("agent_check_in_progress","An AI reply check is already running.");s=!0;try{const r=await i.validate({runtimeAdapter:e.agentType,workDir:e.workDir,modelId:e.modelId??null,sessionMode:"standard"});if(!r.ok)throw new n(r.errorCode,r.errorCode);return{ready:!0,agentType:e.agentType,modelId:e.modelId??null,scope:"hello_reply"}}finally{s=!1}}}}export{c as createAgentReadinessHandlers};
1
+ import{isAvailableAgentType as p}from"@shennian/wire";import{DaemonIpcError as n}from"./protocol.js";function m(i){let s=!1;const l=()=>{const e=i.getStatus();if(e.remoteAccessDisabled)throw new n("daemon_paused","Shennian is paused; resume it only when requested.");if(!e.running)throw new n("daemon_unavailable","Shennian is not running.")};return{"agent.discover":async e=>{l();const o=e&&typeof e=="object"&&!Array.isArray(e)&&typeof e.agentType=="string"?e.agentType:null,u=!!(e&&typeof e=="object"&&!Array.isArray(e)&&e.compact===!0),d=!!(e&&typeof e=="object"&&!Array.isArray(e)&&e.refresh===!0),a=(d?await i.discover(o??void 0):i.getSnapshot()).filter(t=>!o||t.type===o);return{modelTested:!1,compact:u,refreshed:d,agents:a.map(t=>({type:t.type,name:t.display?.name??t.type,authState:t.authState??"unknown",...t.runtime?.cliVersion?{version:t.runtime.cliVersion}:{},...t.runtime?.compatibility?{compatibility:t.runtime.compatibility}:{},...t.runtimePlatform?{runtimePlatform:t.runtimePlatform}:{},...t.runtimeIsWsl!==void 0?{runtimeIsWsl:t.runtimeIsWsl}:{},supportsStandardSession:t.supportsStandardSession===!0,supportsManagedAgent:t.supportsManagedAgent===!0,supportsManagedNativeFull:t.supportsManagedNativeFull===!0,supportsRoomTools:t.supportsRoomTools===!0,supportsWorker:t.supportsWorker===!0,supportsManagerMode:t.supportsManagerMode===!0,supportsExternalWorkerTarget:t.supportsExternalWorkerTarget===!0,...u?{modelCount:t.models.length,defaultModelId:t.models.find(r=>r.isDefault)?.id??null}:{models:t.models.map(r=>({id:r.id,name:r.name,isDefault:r.isDefault===!0}))}}))}},"agent.check":async e=>{if(l(),!e||typeof e!="object"||Array.isArray(e)||typeof e.agentType!="string"||!p(e.agentType)||typeof e.workDir!="string"||!e.workDir.trim()||e.modelId!=null&&typeof e.modelId!="string")throw new n("agent_validation_invalid_configuration","Select an AI and its absolute working directory.");if(s)throw new n("agent_check_in_progress","An AI reply check is already running.");s=!0;try{const o=await i.validate({runtimeAdapter:e.agentType,workDir:e.workDir,modelId:e.modelId??null,sessionMode:"standard"});if(!o.ok)throw new n(o.errorCode,o.errorCode);return{ready:!0,agentType:e.agentType,modelId:e.modelId??null,scope:"hello_reply"}}finally{s=!1}}}}export{m as createAgentReadinessHandlers};
package/dist/src/index.js CHANGED
@@ -1,3 +1,3 @@
1
- import{registerScheduleCommand as ve}from"./commands/schedule.js";import{resolvePersonalScheduleContext as Ie}from"./schedules/personal-context.js";import{executeServerSchedule as Z,deliverServerSchedule as Se}from"./schedules/server-client.js";import{Command as ye}from"commander";import P from"chalk";import we from"node:os";import j from"node:fs";import{loadConfig as m,saveConfig as Ce,getConfigPath as be,getShennianDir as ke,resolveShennianPath as H,normalizeAutoUpgradePolicy as Re}from"./config/index.js";import{CliRelayClient as Pe}from"./relay/client.js";import{collectKnownLogSecrets as X,installCliLogRedaction as Ae,sanitizeCliLogValue as Ee}from"./log-redaction.js";import{registerPairCommand as Te,runSmartStart as De}from"./commands/pair.js";import{registerLogoutCommand as Ue}from"./commands/logout.js";import{clearDaemonPidIfOwner as ee,createDaemonInstanceId as Me,clearDaemonLauncher as oe,findRunningDaemonProcessIds as _e,isRemoteAccessDisabled as ne,prepareRemoteAccessPause as xe,stopPreparedRemoteAccessService as $e,isIsolatedDaemonE2eInstance as Ne,getDaemonStatus as te,registerDaemonCommand as Oe,writeDaemonPid as re,writeDaemonLauncher as ae}from"./commands/daemon.js";import{registerRuntimeCommand as Ve}from"./commands/runtime.js";import{registerAgentCommand as Le}from"./commands/agent.js";import{registerManagerCommand as je}from"./commands/manager.js";import{registerWorkerCommand as He}from"./commands/worker.js";import{handleUpgradeStart as Be,registerUpgradeCommand as Fe}from"./commands/upgrade.js";import{registerToolsCommand as Ge}from"./commands/tools.js";import{registerIntegrationCommand as qe}from"./commands/integration.js";import{registerRoomCommand as ze}from"./commands/room.js";import{SessionManager as We}from"./session/manager.js";import{createTrustedProjectFsRequestResolver as Je}from"./session/handlers/project-fs.js";import{SERVERS as B,regionToUrl as Ke,urlToRegion as Ye}from"./region.js";import{clearUpgradeAttempt as Qe,getCurrentVersion as ie,handleStartupCrashCheck as Ze,readUpgradeAttempt as Xe}from"./upgrade/engine.js";import{AutoUpgradeScheduler as eo}from"./upgrade/scheduler.js";import{ensureUpdaterInstalled as oo}from"./upgrade/updater-bootstrap.js";import{isDesktopDistribution as no}from"./runtime-distribution.js";import{launchUpdater as to,markUpdaterHealthy as ro}from"./upgrade/updater-launcher.js";import{detectAgents as ao}from"./agents/detect.js";import{augmentProcessPath as se}from"./env-path.js";import{acquireDaemonInstanceLock as io}from"./commands/daemon-instance-lock.js";se();const F=ie();import{getCachedAgentInfos as so,resolveAgentInfos as co}from"./agents/model-registry.js";import{initCliLogReporter as lo,reportLog as A,reportUpgradeAggregateMetric as mo}from"./log-reporter.js";import{isNativeFusionEnabled as po}from"./native-fusion/config.js";import{NativeSessionFusionService as go}from"./native-fusion/service.js";import{startDaemonLogRetention as uo}from"./daemon-log.js";import{startDaemonIpcServer as ce}from"./daemon-ipc/server.js";import{resolveDaemonIpcPaths as fo,resolveManagedRoomIpcPaths as ho}from"./daemon-ipc/paths.js";import{createDaemonControlHandlers as vo}from"./daemon-ipc/control.js";import{createAgentReadinessHandlers as Io}from"./daemon-ipc/agent-readiness.js";import{scanMachineAgents as So}from"./session/handlers/agents.js";import{validateAgentWorkspaceConfiguration as yo}from"./session/handlers/agent-workspace-validation.js";import{DAEMON_IPC_VERSION as wo}from"./daemon-ipc/protocol.js";import{RoomSessionRegistry as Co}from"./room/session-registry.js";import{HttpRoomBindingClient as bo}from"./room/room-binding-client.js";import{TrustedManagedRoomBinder as ko}from"./room/managed-room-binding.js";import{RoomHostSessionObservationStore as Ro}from"./room/host-session-observations.js";import{RoomMirrorSyncService as Po}from"./room/mirror-sync-service.js";import{NativeSourceDeletionStore as Ao}from"./native-fusion/native-source-deletions.js";import{matchesRoomOwner as Eo,isCurrentRegistration as To}from"./room/registration-authority.js";import{RoomActivationService as Do,resolveManagedAgentPolicy as Uo}from"./room/activation-service.js";import{RoomActivationAudit as Mo}from"./room/activation-audit.js";import{RoomAttachmentCache as _o}from"./room/attachment-cache.js";import{RoomAttachmentUploader as xo}from"./room/attachment-upload.js";import{ManagedRoomContextAuthority as $o}from"./room/managed-context.js";import{createManagedRoomExecutor as No}from"./room/managed-room-ipc.js";import{SESSION_SYNC_PROTOCOL_VERSION as de,SESSION_SYNC_ADVERTISED_CAPABILITIES as le}from"@shennian/wire";import{getDaemonInstallationId as Oo}from"./session/store.js";import{synchronizeMachinePersonalAuthority as Vo}from"./personal-sync/machine-authority.js";import{CLI_SESSION_SYNC_DATA_EPOCH as me,ensurePersonalDataEpoch as Lo}from"./personal-sync/epoch.js";import{DeletionSyncRetryWaitError as jo,DeletionSyncTerminalError as Ho}from"./session/deletion-sync-state.js";const pe=ke(),Bo=H("daemon.pid"),Fo=H("daemon.instance.lock");function Go(){try{const a=j.readFileSync(Bo,"utf-8").trim();if(a.startsWith("{")){const p=JSON.parse(a),c=Number(p.pid);return Number.isInteger(c)&&c>0?c:null}const s=parseInt(a,10);return Number.isInteger(s)&&s>0?s:null}catch{return null}}function qo(a){return a.replace(/^https:\/\//,"wss://").replace(/^http:\/\//,"ws://")}function G(){process.stdout.write(`${JSON.stringify({ok:!1,error:"integration_command_retired",message:"Room MCP and Hooks were retired. Use the Shennian Skill and CLI.",nextAction:{type:"open_install_guide",url:"https://shennian.net/install.md"}})}
2
- `),process.exitCode=1}function zo(a){const s=[`trigger=${a.trigger??"unknown"}`,`phase=${a.phase}`,`code=${a.code??"unknown"}`,`attempt=${a.reconnectAttempt}`];return a.reason&&s.push(`reason=${a.reason}`),a.error&&s.push(`error=${a.error}`),s.join(" ")}const i=new ye;i.name("shennian").description("Shennian \u2014 AI Agent Control Plane").version(F),i.enablePositionalOptions(),qe(i),ze(i),ve(i),i.option("--api <url>","Server URL override").option("--name <name>","Machine name",we.hostname()).action(async a=>{const s=m(),p=a.api??process.env.SHENNIAN_DESKTOP_SERVER_URL??s.serverUrl??void 0;await De(p,a.name)}),i.command("run-service",{hidden:!0}).description("(internal) Connect to relay server, called by the background service").option("--api <url>","Server URL override").action(async a=>{const s=H("env.json");try{const e=JSON.parse(j.readFileSync(s,"utf-8"));for(const[n,o]of Object.entries(e))process.env[n]||(process.env[n]=o);se()}catch{}Ae(X(process.env,m()));const p=uo();ne()&&(console.log(`[${new Date().toISOString()}] remote access disabled, service start skipped`),clearInterval(p),process.exit(0));const c=Me(),y=io({lockPath:Fo,instanceId:c});y.acquired||(console.log(`[${new Date().toISOString()}] daemon already running`+(y.ownerPid?` (PID ${y.ownerPid})`:"")+", skipping duplicate start"),clearInterval(p),process.exit(0));try{const e=Go();if(e&&e!==process.pid)try{process.kill(e,0),console.log(`[${new Date().toISOString()}] daemon already running (PID ${e}), skipping duplicate start`),y.release(),process.exit(0)}catch{}}catch{}const q=Ne(process.env,pe)?[]:_e(process.pid);q.length>0&&(console.log(`[${new Date().toISOString()}] daemon already running (PID ${q[0]}), skipping duplicate start`),y.release(),process.exit(0)),re(process.pid,c,{version:F}),ae(process.pid,void 0,c),process.on("exit",()=>{ee(process.pid,c),oe(c)}),process.on("exit",y.release);let $=null;try{no()||($=oo())}catch(e){console.error(`[upgrade] updater bootstrap unavailable: ${e instanceof Error?e.message:String(e)}`)}const E=await Ze();if(E){$||(console.error("[upgrade] Crash-loop rollback blocked: updater bootstrap unavailable"),process.exit(1));try{to({fromVersion:F,targetVersion:E.from,oldDaemonPid:process.pid,operation:"rollback",rollbackJobId:E.jobId},{readiness:$}),console.error(`[upgrade] New version crashed ${E.attemptCount} times; external rollback scheduled.`),process.stdin.resume();return}catch(e){console.error(`[upgrade] Crash-loop rollback could not start: ${e instanceof Error?e.message:String(e)}`),process.exit(1)}}try{Lo()}catch(e){console.error(`[personal-sync] data epoch initialization failed: ${e instanceof Error?e.message:String(e)}`),process.exitCode=1;return}const l=m(),g=a.api??process.env.SHENNIAN_DESKTOP_SERVER_URL??l.serverUrl??B.cn.url,f=ie(),v=await ao({serverUrl:g,authToken:l.machineToken??l.accessToken}),w=v.map(e=>e.type),N=so(v),u=new Co,T=new Ro,C=new $o(u),I=new bo(g),ue=new _o({actionClient:I}),fe=new xo({actionClient:I});let S=null,r;const z=No({authority:C,actionClient:I,attachmentCache:ue,attachmentUploader:fe,observations:T,getMachineCredentials:()=>({machineToken:m().machineToken}),managerExecutor:e=>r.executeManagedManagerAction(e),scheduleExecutor:(e,n,o)=>Z({serverUrl:g,machineToken:m().machineToken,sessionId:e,action:n,args:o}),onPaused:({registryKey:e,sessionId:n})=>{const t=u.getByRegistryKey(e)?.binding;t&&u.setBinding({registryKey:e,bindingId:t.bindingId,roomId:t.roomId,agentIdentityId:t.agentIdentityId,agentMembershipId:t.agentMembershipId,status:"paused",activationMode:"paused"}),C.revokeSession(e),r?.discardPendingManagedRoomMessages(n)}});let O=null,V=null,L=null;const b=await ce({paths:fo(),handlers:{...vo({daemonVersion:f,protocolVersion:wo,getStatus:()=>te(),getAgents:()=>v,completeAgentAuthentication:e=>r.resumePendingAcpAuthentication(e),startUpgrade:async e=>{if(!O)throw new Error("upgrade_unavailable");return O(e)},prepareExternalUpgrade:e=>{if(!V)throw new Error("external_upgrade_unavailable");return V(e)},cancelExternalUpgrade:()=>{if(!L)throw new Error("external_upgrade_unavailable");return L()}}),...Io({getStatus:()=>te(),discover:async()=>{if(!r)throw new Error("daemon_starting");const e=await So(!0);return v.splice(0,v.length,...e.detectedAgents),d.sendEvent({type:"event",event:"machine.agents",payload:{agentList:e.agentList,agents:e.agents}}),e.agents.map(n=>{const o=e.detectedAgents.find(t=>t.type===n.type);return{...n,runtimePlatform:o?.runtimePlatform,runtimeIsWsl:o?.runtimeIsWsl}})},validate:async e=>{if(!r)throw new Error("daemon_starting");return yo(e,{ignoreNativeSession:(n,o)=>R?.ignoreNativeSource(n,o)})}})}});let k;try{k=await ce({paths:ho(),handlers:{"managed-room.execute":e=>z(e),"schedule.personal":async e=>{const n=e,o=Ie(n?.context);if(u.getByNianSessionId(o)?.binding)throw new Error("schedule_personal_context_required");return await Z({serverUrl:g,machineToken:m().machineToken,sessionId:o,action:n.operation,args:n})}}})}catch(e){throw await b.close(),e}let W=!1;const D=()=>{W||(W=!0,Promise.allSettled([b.close(),k.close()]).finally(()=>process.exit(0)))};if(process.once("SIGINT",D),process.once("SIGTERM",D),!l.machineToken){console.error('Shennian CLI is not paired. Run "shennian pair --browser" or "shennian pair --token" first.'),await Promise.allSettled([b.close(),k.close()]),process.exitCode=1;return}process.removeListener("SIGINT",D),process.removeListener("SIGTERM",D);const J=`${qo(g)}/relay/machine`,U=l.machineToken;if(!U)return;try{await Vo({serverUrl:g,machineToken:U,machineId:l.machineId})}catch(e){console.error(`[personal-sync] machine authority verification failed: ${e instanceof Error?e.message:String(e)}`),await Promise.allSettled([b.close(),k.close()]),process.exit(1)}l.machineId&&lo(g,l.machineId),console.log(`[${new Date().toISOString()}] Connecting to ${J}... (v${f}) agents: ${w.join(",")}`),A({level:"info",wsEvent:"daemon.start",metadata:{version:f,agents:w}});let R=null,M=null,_=0,h=Xe();const K=Re(l.autoUpgrade),d=new Pe({serverUrl:J,machineToken:U,cliVersion:f,agentList:w,autoUpgradePolicy:K,sessionSyncProtocolVersion:de,sessionSyncCapabilities:le,daemonInstallationId:Oo(),dataEpoch:me,updaterProtocolVersion:1,onConnected:()=>{console.log(`[${new Date().toISOString()}] \u2713 Connected`),A({level:"info",wsEvent:"daemon.connected"}),N.some(o=>o.models.length>0)&&d.sendEvent({type:"event",event:"machine.agents",payload:{agentList:w,agents:N}}),co(v,{serverUrl:l.serverUrl??g,authToken:l.machineToken??l.accessToken}).then(o=>{JSON.stringify(o)!==JSON.stringify(N)&&d.sendEvent({type:"event",event:"machine.agents",payload:{agentList:w,agents:o}})}).catch(()=>{}),h&&d.sendEvent({type:"event",event:"upgrade",payload:{state:"recovering",machineId:"self",from:h.from,to:h.to,...h.jobId?{jobId:h.jobId}:{},activeRunCount:r.getPendingRecoveryCount()}}),x.start(),M?.start();const e=++_,n=async()=>{try{if(await r.synchronizeSessionDeletions(),await r.recoverInterruptedRuns(),h){const o=h;h=null,o.jobId&&ro(o.jobId,o.to),Qe(),console.log(`[${new Date().toISOString()}] [upgrade] ${o.operation==="rollback"?"Rolled back":"Healthy"}: ${o.from} \u2192 ${o.to}`),mo({kind:"upgrade",result:o.operation==="rollback"?"rolled_back":"healthy",stage:"terminal"}),d.sendEvent({type:"event",event:"upgrade",payload:{state:o.operation==="rollback"?"rolled_back":"healthy",machineId:"self",from:o.from,to:o.to,...o.jobId?{jobId:o.jobId}:{}}}),o.operation!=="rollback"&&d.sendEvent({type:"event",event:"upgrade",payload:{state:"success",machineId:"self",from:o.from,to:o.to}})}e===_&&d.getState()==="connected"&&R?.handleConnected()}catch(o){if(console.error("[session.deletions] startup catch-up failed",o),e===_&&d.getState()==="connected"){if(o instanceof Ho)return;const t=o instanceof jo?Math.max(1e3,o.retryAfterMs):5e3;setTimeout(()=>{n()},t).unref?.()}}};n()},onDisconnected:e=>{_+=1,r?.revokeProjectFsHandles(),M?.stop(),console.log(`[${new Date().toISOString()}] \u26A0 Disconnected, reconnecting... ${zo(e)}`),A({level:"warn",wsEvent:"daemon.disconnected",metadata:{code:e.code,reason:e.reason,error:e.error,phase:e.phase,trigger:e.trigger,reconnectAttempt:e.reconnectAttempt}})},onProjectFsCapabilityChanged:e=>{e||r?.revokeProjectFsHandles()},onReq:e=>{console.log(`[${new Date().toISOString()}] [req] ${e.method}`),A({level:"info",type:"ws",wsEvent:e.method,wsDirection:"in",traceId:e.traceId}),r.handleReq(e)}});R=po()?new go(d):null,M=new Po({registry:u,observations:T,actionClient:I,getMachineCredentials:()=>({machineToken:m().machineToken}),isSessionDeleted:e=>new Ao().hasSession(e),onMessages:async(e,n,o,t)=>{await S?.processMessages(e,n,o,t)},onBindingPaused:e=>{C.revokeSession(e.registryKey),e.lifecycleState==="closed"?r?.retireManagedRoomSession(e.nianSessionId).catch(()=>{console.warn("[room.sync] retired session cleanup failed")}):r?.discardPendingManagedRoomMessages(e.nianSessionId)}});const x=new eo({currentVersion:f,machineId:l.machineId??"unknown-machine",serverUrl:l.serverUrl??g,getIdleState:()=>r.getUpgradeIdleState(),sessionSyncProtocolVersion:de,dataEpoch:me,advertisedCapabilities:le,updaterProtocolVersion:1,startUpgrade:async(e,n)=>{const{handleUpgradeStart:o}=await import("./commands/upgrade.js");await o(d,"auto-upgrade",e,{currentVersion:f,prepareForUpgrade:t=>r.prepareForUpgrade(t),cancelUpgradePreparation:()=>r.cancelUpgradePreparation(),shutdownAfterHandoff:()=>{setTimeout(()=>process.kill(process.pid,"SIGTERM"),100).unref?.()},suppressResponse:!0,waitForCompletion:!0,canHandoff:n?void 0:()=>r.getUpgradeIdleState().idle})}});x.setPolicy(K),r=new We(d,R,f,x,new ko({registry:u,observations:T,bindingClient:I,getMachineCredentials:()=>{const e=m();return{machineId:e.machineId,machineToken:e.machineToken}},hostProcessIdentity:()=>`daemon:${c}`}),{prepare:()=>xe(),isDisabled:()=>ne(),shutdownAfterResponse:()=>{setTimeout(()=>{$e(),process.kill(process.pid,"SIGTERM")},100).unref?.()}},e=>C.revokeToken(e),z,null,Je(U)),O=async e=>{const n=e&&typeof e=="object"&&!Array.isArray(e)?e:{},o=typeof n.targetVersion=="string"?n.targetVersion:void 0,t=n.confirmedMajor===!0;return await Be(d,`local-upgrade-${Date.now()}`,o,{currentVersion:f,confirmedMajor:t,prepareForUpgrade:he=>r.prepareForUpgrade(he),cancelUpgradePreparation:()=>r.cancelUpgradePreparation(),shutdownAfterHandoff:()=>{setTimeout(()=>process.kill(process.pid,"SIGTERM"),100).unref?.()},suppressResponse:!0})},V=e=>{const n=e&&typeof e=="object"&&!Array.isArray(e)?e:{},t=`desktop-${(typeof n.targetVersion=="string"?n.targetVersion:"unknown").replace(/[^A-Za-z0-9._-]/g,"_")}-${Date.now()}`;return{jobId:t,...r.prepareForUpgrade(t)}},L=()=>(r.cancelUpgradePreparation(),{cancelled:!0}),S=new Do({registry:u,observations:T,enqueueConversationBatch:e=>r.enqueueManagedRoomBatch(e),loadContext:async e=>{const n=m().machineToken?.trim(),o=e.binding;if(!n||!o)throw new Error("managed_room_context_unavailable");const t=await I.status({machineToken:n,roomId:o.roomId,bindingId:o.bindingId});if(!To(u,e)||!Eo(e,t))throw new Error("managed_room_binding_unavailable");return{roomName:t.room.name,memberCount:t.room.memberCount,members:t.room.members,agentsByIdentityId:t.agent?{[t.agentIdentityId]:t.agent}:{},agentSystemPromptsByIdentityId:t.agent?{[t.agentIdentityId]:t.agent.systemPrompt??""}:{}}},issueManagedRoomContext:(e,n,o)=>{const t=m().machineToken;if(!t)throw new Error("machine_not_authorized");return C.issue({registryKey:e,machineCredential:t,activationCauseMessageId:n,allowedActions:o})},audit:new Mo,resolveManagedAgentPolicy:e=>Uo({...e,runtime:v.find(n=>n.type===e.registration.runtimeAdapter)})}),r.scheduleHandler=e=>Se({serverUrl:g,machineToken:m().machineToken,req:e,client:d,queue:r.getScheduleQueue(),registry:u}),r.getScheduleQueue().validateScheduledTarget=async e=>{if(!e.bindingId||!e.roomId)return"ready";if(u.getByNianSessionId(e.sessionId)?.binding?.bindingId!==e.bindingId)return"ended";const o=await I.status({machineToken:m().machineToken,roomId:e.roomId,bindingId:e.bindingId});return o.status==="active"&&o.room.status==="active"?"ready":"ended"},r.setManagedRoomTurnPreparer(e=>S.prepareConversationTurn(e)),r.setManagedAgentDirectTurnPreparer((e,n)=>S.prepareDirectConversationTurn(e,n)),r.setManagedRecoveryPreparer(async e=>e.source==="managed_direct"?S.prepareDirectConversationTurn(e.sessionId,{allowedRoomActions:[]}):e.source==="manager_event"?S.prepareDirectConversationTurn(e.sessionId,{allowedRoomActions:[],managerEvent:!0}):e.source!=="managed_room"||!e.roomContextRegistryKey||!e.activationCauseMessageId||!e.rootCauseMessageId||!e.activationSequence||e.activationHopCount===null?null:S.prepareConversationTurn([{registryKey:e.roomContextRegistryKey,causeMessageId:e.activationCauseMessageId,rootCauseMessageId:e.rootCauseMessageId,sequence:e.activationSequence,hopCount:e.activationHopCount}])),j.mkdirSync(pe,{recursive:!0}),re(process.pid,c,{version:f}),ae(process.pid,void 0,c),d.connect(),process.stdin.resume();let Y=!1;const Q=async()=>{if(!Y){Y=!0,console.log(P.gray(`
3
- Disconnecting...`)),A({level:"info",wsEvent:"daemon.stop"}),clearInterval(p);try{await r.cleanup(),x.stop(),R?.stop(),M?.stop(),d.disconnect(),await Promise.allSettled([b.close(),k.close()]),ee(process.pid,c),oe(c)}finally{process.exit(0)}}};process.on("SIGINT",()=>{Q()}),process.on("SIGTERM",()=>{Q()})}),i.command("workbuddy-session-hook",{hidden:!0}).description("(retired) WorkBuddy Room Hook").allowUnknownOption(!0).action(()=>G()),i.command("room-session-hook",{hidden:!0}).description("(internal) Authorize the official Room tool for one trusted Agent Session").argument("<runtime>","Trusted runtime adapter").allowUnknownOption(!0).action(()=>G()),i.command("room-tool-server",{hidden:!0}).description("(internal) Run the official Shennian Room MCP tool").allowUnknownOption(!0).action(()=>G());const ge=i.command("config").description("View or modify configuration");ge.command("show",{isDefault:!0}).description("Show current configuration").action(()=>{console.log(`Config file: ${be()}`);const a=m(),s=Ee(a,X(process.env,a));if(a.serverUrl){const p=Ye(a.serverUrl);console.log(`Region: ${p} (${B[p].label})`)}console.log(JSON.stringify(s,null,2))}),ge.command("set").description("Update a config value").argument("<key>",'Config key (e.g. "server")').argument("<value>",'Config value (e.g. "cn" or "global")').action((a,s)=>{const p=m();if(a==="server"){s!=="cn"&&s!=="global"&&(console.error(P.red('\u2717 Value must be "cn" or "global"')),process.exit(1));const c=s;p.serverUrl=Ke(c),Ce(p),console.log(P.green(`\u2713 Server set to ${B[c].label}`)),console.log(P.yellow(" Restart the background service for this to take effect: shennian stop && shennian start"));return}console.error(P.red(`\u2717 Unknown config key: ${a}. Supported: server`)),process.exit(1)}),Te(i),Ue(i),Oe(i),Ve(i),Le(i),je(i),He(i),Fe(i),Ge(i),i.parse();
1
+ import{registerScheduleCommand as Se}from"./commands/schedule.js";import{resolvePersonalScheduleContext as ye}from"./schedules/personal-context.js";import{executeServerSchedule as oe,deliverServerSchedule as we}from"./schedules/server-client.js";import{Command as Ce}from"commander";import A from"chalk";import be from"node:os";import F from"node:fs";import{loadConfig as p,saveConfig as ke,getConfigPath as Re,getShennianDir as Pe,resolveShennianPath as G,normalizeAutoUpgradePolicy as Ae}from"./config/index.js";import{CliRelayClient as Ee}from"./relay/client.js";import{collectKnownLogSecrets as ne,installCliLogRedaction as Te,sanitizeCliLogValue as De}from"./log-redaction.js";import{registerPairCommand as Ue,runSmartStart as Me}from"./commands/pair.js";import{registerLogoutCommand as _e}from"./commands/logout.js";import{clearDaemonPidIfOwner as te,createDaemonInstanceId as xe,clearDaemonLauncher as re,findRunningDaemonProcessIds as $e,isRemoteAccessDisabled as ae,prepareRemoteAccessPause as Ne,stopPreparedRemoteAccessService as Oe,isIsolatedDaemonE2eInstance as Ve,getDaemonStatus as ie,registerDaemonCommand as Le,writeDaemonPid as se,writeDaemonLauncher as ce}from"./commands/daemon.js";import{registerRuntimeCommand as je}from"./commands/runtime.js";import{registerAgentCommand as He}from"./commands/agent.js";import{registerManagerCommand as Be}from"./commands/manager.js";import{registerWorkerCommand as Fe}from"./commands/worker.js";import{handleUpgradeStart as Ge,registerUpgradeCommand as We}from"./commands/upgrade.js";import{registerToolsCommand as qe}from"./commands/tools.js";import{registerIntegrationCommand as ze}from"./commands/integration.js";import{registerRoomCommand as Je}from"./commands/room.js";import{SessionManager as Ke}from"./session/manager.js";import{createTrustedProjectFsRequestResolver as Ye}from"./session/handlers/project-fs.js";import{SERVERS as W,regionToUrl as Qe,urlToRegion as Ze}from"./region.js";import{clearUpgradeAttempt as Xe,getCurrentVersion as de,handleStartupCrashCheck as eo,readUpgradeAttempt as oo}from"./upgrade/engine.js";import{AutoUpgradeScheduler as no}from"./upgrade/scheduler.js";import{ensureUpdaterInstalled as to}from"./upgrade/updater-bootstrap.js";import{isDesktopDistribution as ro}from"./runtime-distribution.js";import{launchUpdater as ao,markUpdaterHealthy as io}from"./upgrade/updater-launcher.js";import{detectAgents as so}from"./agents/detect.js";import{augmentProcessPath as le}from"./env-path.js";import{acquireDaemonInstanceLock as co}from"./commands/daemon-instance-lock.js";le();const q=de();import{getCachedAgentInfos as N,resolveAgentInfos as lo}from"./agents/model-registry.js";import{initCliLogReporter as mo,reportLog as E,reportUpgradeAggregateMetric as po}from"./log-reporter.js";import{isNativeFusionEnabled as go}from"./native-fusion/config.js";import{NativeSessionFusionService as uo}from"./native-fusion/service.js";import{startDaemonLogRetention as fo}from"./daemon-log.js";import{startDaemonIpcServer as me}from"./daemon-ipc/server.js";import{resolveDaemonIpcPaths as ho,resolveManagedRoomIpcPaths as vo}from"./daemon-ipc/paths.js";import{createDaemonControlHandlers as Io}from"./daemon-ipc/control.js";import{createAgentReadinessHandlers as So}from"./daemon-ipc/agent-readiness.js";import{scanMachineAgents as yo}from"./session/handlers/agents.js";import{validateAgentWorkspaceConfiguration as wo}from"./session/handlers/agent-workspace-validation.js";import{DAEMON_IPC_VERSION as Co}from"./daemon-ipc/protocol.js";import{RoomSessionRegistry as bo}from"./room/session-registry.js";import{HttpRoomBindingClient as ko}from"./room/room-binding-client.js";import{TrustedManagedRoomBinder as Ro}from"./room/managed-room-binding.js";import{RoomHostSessionObservationStore as Po}from"./room/host-session-observations.js";import{RoomMirrorSyncService as Ao}from"./room/mirror-sync-service.js";import{NativeSourceDeletionStore as Eo}from"./native-fusion/native-source-deletions.js";import{matchesRoomOwner as To,isCurrentRegistration as Do}from"./room/registration-authority.js";import{RoomActivationService as Uo,resolveManagedAgentPolicy as Mo}from"./room/activation-service.js";import{RoomActivationAudit as _o}from"./room/activation-audit.js";import{RoomAttachmentCache as xo}from"./room/attachment-cache.js";import{RoomAttachmentUploader as $o}from"./room/attachment-upload.js";import{ManagedRoomContextAuthority as No}from"./room/managed-context.js";import{createManagedRoomExecutor as Oo}from"./room/managed-room-ipc.js";import{SESSION_SYNC_PROTOCOL_VERSION as pe,SESSION_SYNC_ADVERTISED_CAPABILITIES as ge}from"@shennian/wire";import{getDaemonInstallationId as Vo}from"./session/store.js";import{synchronizeMachinePersonalAuthority as Lo}from"./personal-sync/machine-authority.js";import{CLI_SESSION_SYNC_DATA_EPOCH as ue,ensurePersonalDataEpoch as jo}from"./personal-sync/epoch.js";import{DeletionSyncRetryWaitError as Ho,DeletionSyncTerminalError as Bo}from"./session/deletion-sync-state.js";const fe=Pe(),Fo=G("daemon.pid"),Go=G("daemon.instance.lock");function Wo(){try{const a=F.readFileSync(Fo,"utf-8").trim();if(a.startsWith("{")){const g=JSON.parse(a),c=Number(g.pid);return Number.isInteger(c)&&c>0?c:null}const s=parseInt(a,10);return Number.isInteger(s)&&s>0?s:null}catch{return null}}function qo(a){return a.replace(/^https:\/\//,"wss://").replace(/^http:\/\//,"ws://")}function z(){process.stdout.write(`${JSON.stringify({ok:!1,error:"integration_command_retired",message:"Room MCP and Hooks were retired. Use the Shennian Skill and CLI.",nextAction:{type:"open_install_guide",url:"https://shennian.net/install.md"}})}
2
+ `),process.exitCode=1}function zo(a){const s=[`trigger=${a.trigger??"unknown"}`,`phase=${a.phase}`,`code=${a.code??"unknown"}`,`attempt=${a.reconnectAttempt}`];return a.reason&&s.push(`reason=${a.reason}`),a.error&&s.push(`error=${a.error}`),s.join(" ")}const i=new Ce;i.name("shennian").description("Shennian \u2014 AI Agent Control Plane").version(q),i.enablePositionalOptions(),ze(i),Je(i),Se(i),i.option("--api <url>","Server URL override").option("--name <name>","Machine name",be.hostname()).action(async a=>{const s=p(),g=a.api??process.env.SHENNIAN_DESKTOP_SERVER_URL??s.serverUrl??void 0;await Me(g,a.name)}),i.command("run-service",{hidden:!0}).description("(internal) Connect to relay server, called by the background service").option("--api <url>","Server URL override").action(async a=>{const s=G("env.json");try{const e=JSON.parse(F.readFileSync(s,"utf-8"));for(const[n,o]of Object.entries(e))process.env[n]||(process.env[n]=o);le()}catch{}Te(ne(process.env,p()));const g=fo();ae()&&(console.log(`[${new Date().toISOString()}] remote access disabled, service start skipped`),clearInterval(g),process.exit(0));const c=xe(),y=co({lockPath:Go,instanceId:c});y.acquired||(console.log(`[${new Date().toISOString()}] daemon already running`+(y.ownerPid?` (PID ${y.ownerPid})`:"")+", skipping duplicate start"),clearInterval(g),process.exit(0));try{const e=Wo();if(e&&e!==process.pid)try{process.kill(e,0),console.log(`[${new Date().toISOString()}] daemon already running (PID ${e}), skipping duplicate start`),y.release(),process.exit(0)}catch{}}catch{}const J=Ve(process.env,fe)?[]:$e(process.pid);J.length>0&&(console.log(`[${new Date().toISOString()}] daemon already running (PID ${J[0]}), skipping duplicate start`),y.release(),process.exit(0)),se(process.pid,c,{version:q}),ce(process.pid,void 0,c),process.on("exit",()=>{te(process.pid,c),re(c)}),process.on("exit",y.release);let O=null;try{ro()||(O=to())}catch(e){console.error(`[upgrade] updater bootstrap unavailable: ${e instanceof Error?e.message:String(e)}`)}const T=await eo();if(T){O||(console.error("[upgrade] Crash-loop rollback blocked: updater bootstrap unavailable"),process.exit(1));try{ao({fromVersion:q,targetVersion:T.from,oldDaemonPid:process.pid,operation:"rollback",rollbackJobId:T.jobId},{readiness:O}),console.error(`[upgrade] New version crashed ${T.attemptCount} times; external rollback scheduled.`),process.stdin.resume();return}catch(e){console.error(`[upgrade] Crash-loop rollback could not start: ${e instanceof Error?e.message:String(e)}`),process.exit(1)}}try{jo()}catch(e){console.error(`[personal-sync] data epoch initialization failed: ${e instanceof Error?e.message:String(e)}`),process.exitCode=1;return}const m=p(),u=a.api??process.env.SHENNIAN_DESKTOP_SERVER_URL??m.serverUrl??W.cn.url,h=de(),d=await so({serverUrl:u,authToken:m.machineToken??m.accessToken}),w=d.map(e=>e.type),V=N(d),f=new bo,D=new Po,C=new No(f),I=new ko(u),ve=new xo({actionClient:I}),Ie=new $o({actionClient:I});let S=null,r;const K=Oo({authority:C,actionClient:I,attachmentCache:ve,attachmentUploader:Ie,observations:D,getMachineCredentials:()=>({machineToken:p().machineToken}),managerExecutor:e=>r.executeManagedManagerAction(e),scheduleExecutor:(e,n,o)=>oe({serverUrl:u,machineToken:p().machineToken,sessionId:e,action:n,args:o}),onPaused:({registryKey:e,sessionId:n})=>{const t=f.getByRegistryKey(e)?.binding;t&&f.setBinding({registryKey:e,bindingId:t.bindingId,roomId:t.roomId,agentIdentityId:t.agentIdentityId,agentMembershipId:t.agentMembershipId,status:"paused",activationMode:"paused"}),C.revokeSession(e),r?.discardPendingManagedRoomMessages(n)}});let L=null,j=null,H=null;const b=await me({paths:ho(),handlers:{...Io({daemonVersion:h,protocolVersion:Co,getStatus:()=>ie(),getAgents:()=>d,completeAgentAuthentication:e=>r.resumePendingAcpAuthentication(e),startUpgrade:async e=>{if(!L)throw new Error("upgrade_unavailable");return L(e)},prepareExternalUpgrade:e=>{if(!j)throw new Error("external_upgrade_unavailable");return j(e)},cancelExternalUpgrade:()=>{if(!H)throw new Error("external_upgrade_unavailable");return H()}}),...So({getStatus:()=>ie(),getSnapshot:()=>N(d).map(e=>{const n=d.find(o=>o.type===e.type);return{...e,runtimePlatform:n?.runtimePlatform,runtimeIsWsl:n?.runtimeIsWsl}}),discover:async e=>{if(!r)throw new Error("daemon_starting");const n=await yo(!0,e);if(e){const t=d.filter(P=>P.type!==e);d.splice(0,d.length,...t,...n.detectedAgents)}else d.splice(0,d.length,...n.detectedAgents);const o=N(d);return l.sendEvent({type:"event",event:"machine.agents",payload:{agentList:d.map(t=>t.type),agents:o}}),n.agents.map(t=>{const P=n.detectedAgents.find(B=>B.type===t.type);return{...t,runtimePlatform:P?.runtimePlatform,runtimeIsWsl:P?.runtimeIsWsl}})},validate:async e=>{if(!r)throw new Error("daemon_starting");return wo(e,{ignoreNativeSession:(n,o)=>R?.ignoreNativeSource(n,o)})}})}});let k;try{k=await me({paths:vo(),handlers:{"managed-room.execute":e=>K(e),"schedule.personal":async e=>{const n=e,o=ye(n?.context);if(f.getByNianSessionId(o)?.binding)throw new Error("schedule_personal_context_required");return await oe({serverUrl:u,machineToken:p().machineToken,sessionId:o,action:n.operation,args:n})}}})}catch(e){throw await b.close(),e}let Y=!1;const U=()=>{Y||(Y=!0,Promise.allSettled([b.close(),k.close()]).finally(()=>process.exit(0)))};if(process.once("SIGINT",U),process.once("SIGTERM",U),!m.machineToken){console.error('Shennian CLI is not paired. Run "shennian pair --browser" or "shennian pair --token" first.'),await Promise.allSettled([b.close(),k.close()]),process.exitCode=1;return}process.removeListener("SIGINT",U),process.removeListener("SIGTERM",U);const Q=`${qo(u)}/relay/machine`,M=m.machineToken;if(!M)return;try{await Lo({serverUrl:u,machineToken:M,machineId:m.machineId})}catch(e){console.error(`[personal-sync] machine authority verification failed: ${e instanceof Error?e.message:String(e)}`),await Promise.allSettled([b.close(),k.close()]),process.exit(1)}m.machineId&&mo(u,m.machineId),console.log(`[${new Date().toISOString()}] Connecting to ${Q}... (v${h}) agents: ${w.join(",")}`),E({level:"info",wsEvent:"daemon.start",metadata:{version:h,agents:w}});let R=null,_=null,x=0,v=oo();const Z=Ae(m.autoUpgrade),l=new Ee({serverUrl:Q,machineToken:M,cliVersion:h,agentList:w,autoUpgradePolicy:Z,sessionSyncProtocolVersion:pe,sessionSyncCapabilities:ge,daemonInstallationId:Vo(),dataEpoch:ue,updaterProtocolVersion:1,onConnected:()=>{console.log(`[${new Date().toISOString()}] \u2713 Connected`),E({level:"info",wsEvent:"daemon.connected"}),V.some(o=>o.models.length>0)&&l.sendEvent({type:"event",event:"machine.agents",payload:{agentList:w,agents:V}}),lo(d,{serverUrl:m.serverUrl??u,authToken:m.machineToken??m.accessToken}).then(o=>{JSON.stringify(o)!==JSON.stringify(V)&&l.sendEvent({type:"event",event:"machine.agents",payload:{agentList:w,agents:o}})}).catch(()=>{}),v&&l.sendEvent({type:"event",event:"upgrade",payload:{state:"recovering",machineId:"self",from:v.from,to:v.to,...v.jobId?{jobId:v.jobId}:{},activeRunCount:r.getPendingRecoveryCount()}}),$.start(),_?.start();const e=++x,n=async()=>{try{if(await r.synchronizeSessionDeletions(),await r.recoverInterruptedRuns(),v){const o=v;v=null,o.jobId&&io(o.jobId,o.to),Xe(),console.log(`[${new Date().toISOString()}] [upgrade] ${o.operation==="rollback"?"Rolled back":"Healthy"}: ${o.from} \u2192 ${o.to}`),po({kind:"upgrade",result:o.operation==="rollback"?"rolled_back":"healthy",stage:"terminal"}),l.sendEvent({type:"event",event:"upgrade",payload:{state:o.operation==="rollback"?"rolled_back":"healthy",machineId:"self",from:o.from,to:o.to,...o.jobId?{jobId:o.jobId}:{}}}),o.operation!=="rollback"&&l.sendEvent({type:"event",event:"upgrade",payload:{state:"success",machineId:"self",from:o.from,to:o.to}})}e===x&&l.getState()==="connected"&&R?.handleConnected()}catch(o){if(console.error("[session.deletions] startup catch-up failed",o),e===x&&l.getState()==="connected"){if(o instanceof Bo)return;const t=o instanceof Ho?Math.max(1e3,o.retryAfterMs):5e3;setTimeout(()=>{n()},t).unref?.()}}};n()},onDisconnected:e=>{x+=1,r?.revokeProjectFsHandles(),_?.stop(),console.log(`[${new Date().toISOString()}] \u26A0 Disconnected, reconnecting... ${zo(e)}`),E({level:"warn",wsEvent:"daemon.disconnected",metadata:{code:e.code,reason:e.reason,error:e.error,phase:e.phase,trigger:e.trigger,reconnectAttempt:e.reconnectAttempt}})},onProjectFsCapabilityChanged:e=>{e||r?.revokeProjectFsHandles()},onReq:e=>{console.log(`[${new Date().toISOString()}] [req] ${e.method}`),E({level:"info",type:"ws",wsEvent:e.method,wsDirection:"in",traceId:e.traceId}),r.handleReq(e)}});R=go()?new uo(l):null,_=new Ao({registry:f,observations:D,actionClient:I,getMachineCredentials:()=>({machineToken:p().machineToken}),isSessionDeleted:e=>new Eo().hasSession(e),onMessages:async(e,n,o,t)=>{await S?.processMessages(e,n,o,t)},onBindingPaused:e=>{C.revokeSession(e.registryKey),e.lifecycleState==="closed"?r?.retireManagedRoomSession(e.nianSessionId).catch(()=>{console.warn("[room.sync] retired session cleanup failed")}):r?.discardPendingManagedRoomMessages(e.nianSessionId)}});const $=new no({currentVersion:h,machineId:m.machineId??"unknown-machine",serverUrl:m.serverUrl??u,getIdleState:()=>r.getUpgradeIdleState(),sessionSyncProtocolVersion:pe,dataEpoch:ue,advertisedCapabilities:ge,updaterProtocolVersion:1,startUpgrade:async(e,n)=>{const{handleUpgradeStart:o}=await import("./commands/upgrade.js");await o(l,"auto-upgrade",e,{currentVersion:h,prepareForUpgrade:t=>r.prepareForUpgrade(t),cancelUpgradePreparation:()=>r.cancelUpgradePreparation(),shutdownAfterHandoff:()=>{setTimeout(()=>process.kill(process.pid,"SIGTERM"),100).unref?.()},suppressResponse:!0,waitForCompletion:!0,canHandoff:n?void 0:()=>r.getUpgradeIdleState().idle})}});$.setPolicy(Z),r=new Ke(l,R,h,$,new Ro({registry:f,observations:D,bindingClient:I,getMachineCredentials:()=>{const e=p();return{machineId:e.machineId,machineToken:e.machineToken}},hostProcessIdentity:()=>`daemon:${c}`}),{prepare:()=>Ne(),isDisabled:()=>ae(),shutdownAfterResponse:()=>{setTimeout(()=>{Oe(),process.kill(process.pid,"SIGTERM")},100).unref?.()}},e=>C.revokeToken(e),K,null,Ye(M),()=>N(d)),L=async e=>{const n=e&&typeof e=="object"&&!Array.isArray(e)?e:{},o=typeof n.targetVersion=="string"?n.targetVersion:void 0,t=n.confirmedMajor===!0;return await Ge(l,`local-upgrade-${Date.now()}`,o,{currentVersion:h,confirmedMajor:t,prepareForUpgrade:B=>r.prepareForUpgrade(B),cancelUpgradePreparation:()=>r.cancelUpgradePreparation(),shutdownAfterHandoff:()=>{setTimeout(()=>process.kill(process.pid,"SIGTERM"),100).unref?.()},suppressResponse:!0})},j=e=>{const n=e&&typeof e=="object"&&!Array.isArray(e)?e:{},t=`desktop-${(typeof n.targetVersion=="string"?n.targetVersion:"unknown").replace(/[^A-Za-z0-9._-]/g,"_")}-${Date.now()}`;return{jobId:t,...r.prepareForUpgrade(t)}},H=()=>(r.cancelUpgradePreparation(),{cancelled:!0}),S=new Uo({registry:f,observations:D,enqueueConversationBatch:e=>r.enqueueManagedRoomBatch(e),loadContext:async e=>{const n=p().machineToken?.trim(),o=e.binding;if(!n||!o)throw new Error("managed_room_context_unavailable");const t=await I.status({machineToken:n,roomId:o.roomId,bindingId:o.bindingId});if(!Do(f,e)||!To(e,t))throw new Error("managed_room_binding_unavailable");return{roomName:t.room.name,memberCount:t.room.memberCount,members:t.room.members,agentsByIdentityId:t.agent?{[t.agentIdentityId]:t.agent}:{},agentSystemPromptsByIdentityId:t.agent?{[t.agentIdentityId]:t.agent.systemPrompt??""}:{}}},issueManagedRoomContext:(e,n,o)=>{const t=p().machineToken;if(!t)throw new Error("machine_not_authorized");return C.issue({registryKey:e,machineCredential:t,activationCauseMessageId:n,allowedActions:o})},audit:new _o,resolveManagedAgentPolicy:e=>Mo({...e,runtime:d.find(n=>n.type===e.registration.runtimeAdapter)})}),r.scheduleHandler=e=>we({serverUrl:u,machineToken:p().machineToken,req:e,client:l,queue:r.getScheduleQueue(),registry:f}),r.getScheduleQueue().validateScheduledTarget=async e=>{if(!e.bindingId||!e.roomId)return"ready";if(f.getByNianSessionId(e.sessionId)?.binding?.bindingId!==e.bindingId)return"ended";const o=await I.status({machineToken:p().machineToken,roomId:e.roomId,bindingId:e.bindingId});return o.status==="active"&&o.room.status==="active"?"ready":"ended"},r.setManagedRoomTurnPreparer(e=>S.prepareConversationTurn(e)),r.setManagedAgentDirectTurnPreparer((e,n)=>S.prepareDirectConversationTurn(e,n)),r.setManagedRecoveryPreparer(async e=>e.source==="managed_direct"?S.prepareDirectConversationTurn(e.sessionId,{allowedRoomActions:[]}):e.source==="manager_event"?S.prepareDirectConversationTurn(e.sessionId,{allowedRoomActions:[],managerEvent:!0}):e.source!=="managed_room"||!e.roomContextRegistryKey||!e.activationCauseMessageId||!e.rootCauseMessageId||!e.activationSequence||e.activationHopCount===null?null:S.prepareConversationTurn([{registryKey:e.roomContextRegistryKey,causeMessageId:e.activationCauseMessageId,rootCauseMessageId:e.rootCauseMessageId,sequence:e.activationSequence,hopCount:e.activationHopCount}])),F.mkdirSync(fe,{recursive:!0}),se(process.pid,c,{version:h}),ce(process.pid,void 0,c),l.connect(),process.stdin.resume();let X=!1;const ee=async()=>{if(!X){X=!0,console.log(A.gray(`
3
+ Disconnecting...`)),E({level:"info",wsEvent:"daemon.stop"}),clearInterval(g);try{await r.cleanup(),$.stop(),R?.stop(),_?.stop(),l.disconnect(),await Promise.allSettled([b.close(),k.close()]),te(process.pid,c),re(c)}finally{process.exit(0)}}};process.on("SIGINT",()=>{ee()}),process.on("SIGTERM",()=>{ee()})}),i.command("workbuddy-session-hook",{hidden:!0}).description("(retired) WorkBuddy Room Hook").allowUnknownOption(!0).action(()=>z()),i.command("room-session-hook",{hidden:!0}).description("(internal) Authorize the official Room tool for one trusted Agent Session").argument("<runtime>","Trusted runtime adapter").allowUnknownOption(!0).action(()=>z()),i.command("room-tool-server",{hidden:!0}).description("(internal) Run the official Shennian Room MCP tool").allowUnknownOption(!0).action(()=>z());const he=i.command("config").description("View or modify configuration");he.command("show",{isDefault:!0}).description("Show current configuration").action(()=>{console.log(`Config file: ${Re()}`);const a=p(),s=De(a,ne(process.env,a));if(a.serverUrl){const g=Ze(a.serverUrl);console.log(`Region: ${g} (${W[g].label})`)}console.log(JSON.stringify(s,null,2))}),he.command("set").description("Update a config value").argument("<key>",'Config key (e.g. "server")').argument("<value>",'Config value (e.g. "cn" or "global")').action((a,s)=>{const g=p();if(a==="server"){s!=="cn"&&s!=="global"&&(console.error(A.red('\u2717 Value must be "cn" or "global"')),process.exit(1));const c=s;g.serverUrl=Qe(c),ke(g),console.log(A.green(`\u2713 Server set to ${W[c].label}`)),console.log(A.yellow(" Restart the background service for this to take effect: shennian stop && shennian start"));return}console.error(A.red(`\u2717 Unknown config key: ${a}. Supported: server`)),process.exit(1)}),Ue(i),_e(i),Le(i),je(i),He(i),Be(i),Fe(i),We(i),qe(i),i.parse();
@@ -1,5 +1,5 @@
1
1
  import type { AgentEvent, AgentAdapter } from '../agents/adapter.js';
2
- import { type AgentType, type ExternalChannelSessionStatus, type ReqFrame } from '@shennian/wire';
2
+ import { type AgentType, type AgentInfo, type ExternalChannelSessionStatus, type ReqFrame } from '@shennian/wire';
3
3
  import { ManagerRegistry, type ManagedWorkerRoomOrigin } from './registry.js';
4
4
  import type { SessionManagerRuntime } from '../session/types.js';
5
5
  import { RetiredChannelRuntime as ChannelRuntime, type ChannelRuntimeOptions, type WeChatAutomationLane } from './retired-external-channel-compat.js';
@@ -12,6 +12,7 @@ export type LocalReqDispatchResult = {
12
12
  export type LocalReqDispatcher = (req: ReqFrame) => Promise<void | LocalReqDispatchResult>;
13
13
  type ManagerRuntimeServiceOptions = {
14
14
  getRuntime: () => SessionManagerRuntime;
15
+ getAgentSnapshot?: () => AgentInfo[];
15
16
  dispatchReq: LocalReqDispatcher;
16
17
  channelRuntime?: ChannelRuntime;
17
18
  createWeChatRpaProductRunner?: ChannelRuntimeOptions['createWeChatRpaProductRunner'];
@@ -1,13 +1,13 @@
1
- import _ from"node:http";import{createHash as N,randomBytes as W,randomUUID as k}from"node:crypto";import p from"node:fs";import A from"node:os";import M from"node:path";import{ManagerRegistry as T}from"./registry.js";import{appendMessage as O}from"../session/store.js";import{requestSessionIndexPublish as q}from"../session/index-publisher.js";import{RetiredChannelRuntime as D}from"./retired-external-channel-compat.js";import{resolveShennianPath as P}from"../config/index.js";import{buildExternalChannelInstructions as C}from"../agents/external-channel-instructions.js";import{getPersonalSyncIdentity as w}from"../personal-sync/epoch.js";import{EphemeralWorkerError as B,handleEphemeralWorkerIpcRoute as H,handleWorkerIpcRoute as v}from"./worker-ipc.js";import{deriveWorkerTitle as $,formatWorkerMarkdown as U}from"./worker-markdown.js";import{markTrustedManagerEvent as S}from"../session/manager-event-authority.js";const j=Number(process.env.SHENNIAN_MANAGER_IPC_BODY_MAX_BYTES||2*1024*1024),y=12*6e4;let b=null;function ge(d){b=d}function he(){return b}function f(d){return M.resolve(d||A.homedir())}function m(d,e,r){d.writeHead(e,{"content-type":"application/json; charset=utf-8"}),d.end(JSON.stringify(r))}function R(d){return d.replace(/\r\n/g,`
2
- `).trim()}function G(d){return d==="worker.final"?"completed":d==="worker.error"?"failed":d==="worker.aborted"?"stopped":"progress"}function F(d,e){return`manager-inbox:${N("sha256").update(d).update("\0").update(e.map(t=>t.id).join("\0")).digest("hex").slice(0,32)}`}async function J(d){const e=[];let r=0;for await(const t of d){const n=Buffer.isBuffer(t)?t:Buffer.from(t);if(r+=n.length,r>j)throw new Error("Request body too large");e.push(n)}return e.length===0?{}:JSON.parse(Buffer.concat(e).toString("utf8"))}function K(d,e,r,t){d.client.sendRes({type:"res",id:e,ok:r,...r?{payload:t}:{error:String(t.error||"unknown error")}})}function x(){return V()?M.join(A.tmpdir(),"shennian-vitest-runtime",String(process.pid),"manager-ipc.json"):P("runtime","manager-ipc.json")}function V(){return(process.env.VITEST==="true"||!!process.env.VITEST_WORKER_ID)&&!process.env.SHENNIAN_HOME?.trim()}class le{opts;managedPolicies=new Map;registry=new T;channelRuntime;server=null;ipcUrl=null;ipcToken=W(24).toString("hex");healthTimer=null;startPromise=null;workerTextAcc=new Map;pendingWorkerOrigins=new Map;prepareWorkerTurnOrigin(e,r){r?this.pendingWorkerOrigins.set(e,r):this.pendingWorkerOrigins.delete(e)}constructor(e){this.opts=e,this.channelRuntime=e.channelRuntime??new D}async start(){return this.startPromise?this.startPromise:(this.startPromise=this.doStart(),this.startPromise)}async doStart(){if(this.server)return;this.registry.clearWorkerRunOrigins(),this.server=_.createServer((r,t)=>{this.handleIpc(r,t)}),this.server.requestTimeout=y,this.server.timeout=y,this.server.keepAliveTimeout=y,this.server.headersTimeout=y+5e3,await new Promise((r,t)=>{this.server.once("error",t),this.server.listen(0,"127.0.0.1",()=>r())});const e=this.server.address();typeof e=="object"&&e&&(this.ipcUrl=`http://127.0.0.1:${e.port}`,this.writeIpcRuntimeFile()),this.server.unref(),await this.channelRuntime.start(),this.healthTimer=setInterval(()=>this.scanWorkerHealth(),6e4),this.healthTimer.unref(),this.recoverPersistedInboxDeliveries()}async ready(){await this.start()}async stop(){this.healthTimer&&clearInterval(this.healthTimer),this.healthTimer=null,await this.channelRuntime.stop(),await new Promise(e=>{if(!this.server)return e();this.server.close(()=>e())}),this.server=null,this.ipcUrl=null,this.removeIpcRuntimeFile()}writeIpcRuntimeFile(){if(this.ipcUrl)try{const e=x();p.mkdirSync(M.dirname(e),{recursive:!0}),p.writeFileSync(e,JSON.stringify({url:this.ipcUrl,token:this.ipcToken,pid:process.pid,updatedAt:new Date().toISOString()},null,2),{mode:384}),p.chmodSync(e,384)}catch{}}removeIpcRuntimeFile(){try{const e=x(),r=JSON.parse(p.readFileSync(e,"utf-8"));(r.url===this.ipcUrl||r.token===this.ipcToken)&&p.unlinkSync(e)}catch{}}getInjectedEnv(e,r,t,n){return this.ipcUrl?{SHENNIAN_MANAGER_SESSION_ID:e,SHENNIAN_MANAGER_AGENT_SESSION_ID:r??"",SHENNIAN_MANAGER_WORKDIR:f(t),SHENNIAN_MANAGER_MODEL:n,SHENNIAN_MANAGER_IPC_URL:this.ipcUrl,SHENNIAN_MANAGER_IPC_TOKEN:this.ipcToken}:{}}registerManager(e){this.registry.upsertManager({...e,workDir:f(e.workDir),machineId:process.env.SHENNIAN_MACHINE_ID??null,managedRoomBound:e.managedRoomBound??(this.managedPolicies.has(e.sessionId)||void 0)})}setManagedAgentPolicy(e,r){if(r){this.managedPolicies.set(e,r);const t=this.registry.getManager(e);t&&!t.managedRoomBound&&this.registry.upsertManager({...t,managedRoomBound:!0})}else this.managedPolicies.delete(e)}getManagedAgentPolicyForWorker(e,r){const t=this.registry.getManager(e),n=this.managedPolicies.get(e);return!t||!n||f(r)!==t.workDir?null:n}isExternalWorkerController(e,r){const t=this.registry.getManager(e);return t?.controllerKind==="ephemeral"&&f(r)===t.workDir}isManagedWorkerSession(e){return!!this.findWorker(e)}isManagedRoomBoundManager(e){return this.registry.getManager(e)?.managedRoomBound===!0}async retireManagerAndWorkers(e){const r=this.registry.retireManager(e);this.managedPolicies.delete(e);const t=this.opts.getRuntime(),n=[],a=async(s,u)=>{const i=[];try{t.runJournal?.markSessionAborted(s)}catch(h){i.push(h)}try{t.recoveryBarrier?.end(s)}catch(h){i.push(h)}try{t.chatQueue?.purgeSession(s)}catch(h){i.push(h)}const o=t.sessions.get(s);let c=!o;if(o){if(o.managedRoomContextToken)try{t.revokeManagedRoomContextToken?.(o.managedRoomContextToken),o.managedRoomContextToken=null}catch(h){i.push(h)}try{await o.adapter.stop(),c=!0}catch(h){i.push(h)}}if(o&&c&&i.length===0&&(t.sessions.delete(s),o.heartbeatTimer&&clearInterval(o.heartbeatTimer),o.adapter.removeAllListeners()),c&&i.length===0)try{u?.()}catch(h){i.push(h),n.push(h)}else n.push(...i)};await a(e);for(const s of r)await a(s.sessionId,()=>this.registry.completeRetiredWorker(e,s.sessionId));if(n.length)throw new AggregateError(n,"Failed to retire every Manager or Worker session");return r.map(s=>s.sessionId)}setManagerWorkerDefaults(e,r,t){const n=this.registry.getManager(e);n&&this.registry.upsertManager({...n,defaultWorkerAgentType:r??null,defaultWorkerModelId:t??null})}getManagerWorkerDefaults(e){const r=this.registry.getManager(e);return{agentType:r?.defaultWorkerAgentType??null,modelId:r?.defaultWorkerModelId??null}}noteManagerAgentSession(e,r,t,n){const a=this.registry.getManager(e);this.registry.upsertManager({sessionId:e,agentSessionId:r,workDir:f(t),machineId:process.env.SHENNIAN_MACHINE_ID??null,modelId:n,agentType:a?.agentType,providerModelId:a?.providerModelId})}noteAgentEvent(e,r){const t=this.findWorker(e);if(!t)return;const n={lastActivityAt:new Date().toISOString(),runId:r.runId};if(r.agentSessionId&&(n.agentSessionId=r.agentSessionId),r.state==="start"){const i=this.pendingWorkerOrigins.get(e);this.pendingWorkerOrigins.delete(e),i&&(n.runRoomOrigins={...t.runRoomOrigins??{},[r.runId]:i})}const a=`${e}:${r.runId}`;let s=r.message||t.summary||"(\u65E0\u53EF\u89C1\u5185\u5BB9)";if(r.state==="delta"&&r.text&&!r.thinking){const i=(this.workerTextAcc.get(a)??"")+r.text;this.workerTextAcc.set(a,i);const o=R(i);o&&(n.summary=o.length>160?`${o.slice(0,160)}...`:o)}if(r.state==="final"||r.state==="error"||r.state==="aborted"){n.status=r.state;const i=R(this.workerTextAcc.get(a)??"");if(i&&(n.summary=i.length>240?`${i.slice(0,240)}...`:i,s=r.state==="final"?i:r.message||i),this.workerTextAcc.delete(a),t.runRoomOrigins?.[r.runId]){const o={...t.runRoomOrigins};delete o[r.runId],n.runRoomOrigins=o}}else r.state==="start"&&(n.status="running");const u=this.registry.updateWorker(e,n);u&&r.errorCode==="auth_retrying"&&this.registry.enqueueInboxEvent({managerSessionId:u.managedBy,workerSessionId:u.sessionId,kind:"worker.health",priority:"normal",runId:r.runId,roomOrigin:t.runRoomOrigins?.[r.runId]??null,summary:r.message||"Worker authentication is retrying.",code:"auth_retrying"}),u&&(r.state==="final"||r.state==="error"||r.state==="aborted")&&this.enqueueWorkerEvent(u.managedBy,u,r.state,r.runId,s,t.runRoomOrigins?.[r.runId]??null,r.errorCode==="auth_required"||r.errorCode==="acp_auth_required"?"auth_required":void 0)}findWorker(e){return this.registry.load().workers[e]}async handleAppReq(e){K(this.opts.getRuntime(),e.id,!1,{error:"feature_retired"})}async handleIpc(e,r){if(e.headers.authorization!==`Bearer ${this.ipcToken}`){m(r,401,{ok:!1,error:"Unauthorized"});return}try{const t=new URL(e.url??"/","http://127.0.0.1"),n=await J(e);if(t.pathname.startsWith("/channel/")||t.pathname.startsWith("/external/")||t.pathname.startsWith("/wechat-rpa/")){m(r,410,{ok:!1,error:"feature_retired"});return}const a={registry:this.registry,getRuntime:this.opts.getRuntime,dispatchReq:this.opts.dispatchReq,dispatchChatEnqueue:this.dispatchChatEnqueue.bind(this),appendManagerVisibleMarkdown:this.appendManagerVisibleMarkdown.bind(this)},s=await H(a,t.pathname,n)??await v(a,t.pathname,n,String(e.headers["x-shennian-manager-session-id"]||""));if(s){m(r,s.status,s.body);return}m(r,404,{ok:!1,error:`Unknown manager IPC path: ${t.pathname}`})}catch(t){if(t instanceof B){m(r,t.status,{ok:!1,error:t.code,message:t.message,nextAction:t.nextAction});return}m(r,400,{ok:!1,error:t instanceof Error?t.message:String(t)})}}async executeScopedWorkerAction(e,r,t,n){const a={"manager.sessions.list":"/sessions/list","manager.sessions.start":"/sessions/start","manager.sessions.send":"/sessions/send","manager.sessions.queue":"/sessions/queue","manager.sessions.queue.edit":"/sessions/queue/edit","manager.sessions.queue.delete":"/sessions/queue/delete","manager.sessions.stop":"/sessions/stop","manager.sessions.read":"/sessions/read","manager.memory.path":"/memory/path"},s=await v({registry:this.registry,getRuntime:this.opts.getRuntime,dispatchReq:this.opts.dispatchReq,dispatchChatEnqueue:this.dispatchChatEnqueue.bind(this),appendManagerVisibleMarkdown:this.appendManagerVisibleMarkdown.bind(this)},a[r],t,e,{roomOrigin:n});if(!s||s.status<200||s.status>=300)throw new Error(String(s?.body.error||"Managed Manager action failed"));return JSON.parse(JSON.stringify(s.body))}async dispatchChatEnqueue(e,r,t,n,a,s,u,i){if((await this.opts.dispatchReq(S({type:"req",id:`manager-enqueue-${k()}`,method:"chat.enqueue",params:{...w(),sessionId:e,text:n,agentType:r,workDir:t,agentSessionId:a,modelId:s,managedWorkerParentSessionId:u,managerWorkerRoomOrigin:i}})))?.durableQueueHandoff===!1)throw new Error("manager_worker_queue_handoff_unconfirmed")}appendManagerVisibleMarkdown(e,r,t){this.registry.getManager(e)?.controllerKind==="managed_session"&&(O(e,{id:t,sessionId:e,role:"agent",ts:Date.now(),payload:r},"committed"),q(this.opts.getRuntime().client))}enqueueWorkerEvent(e,r,t,n,a,s,u){const i=this.registry.enqueueInboxEvent({managerSessionId:e,workerSessionId:r.sessionId,kind:t==="final"?"worker.final":t==="error"?"worker.error":"worker.aborted",priority:"normal",runId:n??null,roomOrigin:s??null,summary:a||r.summary||"(\u65E0\u53EF\u89C1\u6458\u8981)",...u?{code:u}:{}}),o=this.registry.getManager(e);i&&o?.controllerKind==="managed_session"&&this.drainManagerInbox(e)}scanWorkerHealth(){const e=Date.now(),r=this.registry.load(),t=this.opts.getRuntime();for(const n of Object.values(r.workers)){if(n.status!=="running")continue;const a=e-Date.parse(n.createdAt);if(a<10*6e4)continue;const s=e-Date.parse(n.lastActivityAt),u=t.sessions.get(n.sessionId)?.currentRunId,i=!!t.chatQueue?.getSnapshot(n.sessionId).pending.length;if(!u){if(!i&&s>=10*6e4){const E="\u672A\u627E\u5230\u6D3B\u52A8\u4E2D\u7684 Provider Run \u6216\u5F85\u5904\u7406\u6D88\u606F\uFF1B\u5DF2\u5C06\u9057\u7559\u7684\u8FD0\u884C\u72B6\u6001\u6536\u655B\u4E3A\u5931\u8D25\u3002\u539F Worker \u4F1A\u8BDD\u4ECD\u4FDD\u7559\uFF0C\u53EF\u7EE7\u7EED\u53D1\u9001\u65B0\u6D88\u606F\u3002",I=this.registry.updateWorker(n.sessionId,{status:"error",lastActivityAt:new Date(e).toISOString(),summary:E});I&&this.enqueueWorkerEvent(I.managedBy,I,"error",n.runId??void 0,E)}continue}const o=n.healthNotifiedAt?Date.parse(n.healthNotifiedAt):0;if(o&&e-o<10*6e4)continue;const c=r.managers[n.managedBy];if(!c)continue;const h=Math.max(0,Math.floor(s/6e4)),g=R(n.runId?this.workerTextAcc.get(`${n.sessionId}:${n.runId}`)??"":""),l=`\u5DF2\u8FD0\u884C ${Math.floor(a/6e4)} \u5206\u949F\uFF0C\u5C1A\u672A\u7ED3\u675F\u3002
1
+ import _ from"node:http";import{createHash as N,randomBytes as W,randomUUID as k}from"node:crypto";import p from"node:fs";import A from"node:os";import M from"node:path";import{ManagerRegistry as T}from"./registry.js";import{appendMessage as O}from"../session/store.js";import{requestSessionIndexPublish as q}from"../session/index-publisher.js";import{RetiredChannelRuntime as D}from"./retired-external-channel-compat.js";import{resolveShennianPath as P}from"../config/index.js";import{buildExternalChannelInstructions as C}from"../agents/external-channel-instructions.js";import{getPersonalSyncIdentity as w}from"../personal-sync/epoch.js";import{EphemeralWorkerError as B,handleEphemeralWorkerIpcRoute as H,handleWorkerIpcRoute as S}from"./worker-ipc.js";import{deriveWorkerTitle as $,formatWorkerMarkdown as U}from"./worker-markdown.js";import{markTrustedManagerEvent as v}from"../session/manager-event-authority.js";const j=Number(process.env.SHENNIAN_MANAGER_IPC_BODY_MAX_BYTES||2*1024*1024),y=12*6e4;let b=null;function de(g){b=g}function he(){return b}function f(g){return M.resolve(g||A.homedir())}function m(g,e,t){g.writeHead(e,{"content-type":"application/json; charset=utf-8"}),g.end(JSON.stringify(t))}function R(g){return g.replace(/\r\n/g,`
2
+ `).trim()}function G(g){return g==="worker.final"?"completed":g==="worker.error"?"failed":g==="worker.aborted"?"stopped":"progress"}function F(g,e){return`manager-inbox:${N("sha256").update(g).update("\0").update(e.map(r=>r.id).join("\0")).digest("hex").slice(0,32)}`}async function J(g){const e=[];let t=0;for await(const r of g){const n=Buffer.isBuffer(r)?r:Buffer.from(r);if(t+=n.length,t>j)throw new Error("Request body too large");e.push(n)}return e.length===0?{}:JSON.parse(Buffer.concat(e).toString("utf8"))}function K(g,e,t,r){g.client.sendRes({type:"res",id:e,ok:t,...t?{payload:r}:{error:String(r.error||"unknown error")}})}function x(){return V()?M.join(A.tmpdir(),"shennian-vitest-runtime",String(process.pid),"manager-ipc.json"):P("runtime","manager-ipc.json")}function V(){return(process.env.VITEST==="true"||!!process.env.VITEST_WORKER_ID)&&!process.env.SHENNIAN_HOME?.trim()}class le{opts;managedPolicies=new Map;registry=new T;channelRuntime;server=null;ipcUrl=null;ipcToken=W(24).toString("hex");healthTimer=null;startPromise=null;workerTextAcc=new Map;pendingWorkerOrigins=new Map;prepareWorkerTurnOrigin(e,t){t?this.pendingWorkerOrigins.set(e,t):this.pendingWorkerOrigins.delete(e)}constructor(e){this.opts=e,this.channelRuntime=e.channelRuntime??new D}async start(){return this.startPromise?this.startPromise:(this.startPromise=this.doStart(),this.startPromise)}async doStart(){if(this.server)return;this.registry.clearWorkerRunOrigins(),this.server=_.createServer((t,r)=>{this.handleIpc(t,r)}),this.server.requestTimeout=y,this.server.timeout=y,this.server.keepAliveTimeout=y,this.server.headersTimeout=y+5e3,await new Promise((t,r)=>{this.server.once("error",r),this.server.listen(0,"127.0.0.1",()=>t())});const e=this.server.address();typeof e=="object"&&e&&(this.ipcUrl=`http://127.0.0.1:${e.port}`,this.writeIpcRuntimeFile()),this.server.unref(),await this.channelRuntime.start(),this.healthTimer=setInterval(()=>this.scanWorkerHealth(),6e4),this.healthTimer.unref(),this.recoverPersistedInboxDeliveries()}async ready(){await this.start()}async stop(){this.healthTimer&&clearInterval(this.healthTimer),this.healthTimer=null,await this.channelRuntime.stop(),await new Promise(e=>{if(!this.server)return e();this.server.close(()=>e())}),this.server=null,this.ipcUrl=null,this.removeIpcRuntimeFile()}writeIpcRuntimeFile(){if(this.ipcUrl)try{const e=x();p.mkdirSync(M.dirname(e),{recursive:!0}),p.writeFileSync(e,JSON.stringify({url:this.ipcUrl,token:this.ipcToken,pid:process.pid,updatedAt:new Date().toISOString()},null,2),{mode:384}),p.chmodSync(e,384)}catch{}}removeIpcRuntimeFile(){try{const e=x(),t=JSON.parse(p.readFileSync(e,"utf-8"));(t.url===this.ipcUrl||t.token===this.ipcToken)&&p.unlinkSync(e)}catch{}}getInjectedEnv(e,t,r,n){return this.ipcUrl?{SHENNIAN_MANAGER_SESSION_ID:e,SHENNIAN_MANAGER_AGENT_SESSION_ID:t??"",SHENNIAN_MANAGER_WORKDIR:f(r),SHENNIAN_MANAGER_MODEL:n,SHENNIAN_MANAGER_IPC_URL:this.ipcUrl,SHENNIAN_MANAGER_IPC_TOKEN:this.ipcToken}:{}}registerManager(e){this.registry.upsertManager({...e,workDir:f(e.workDir),machineId:process.env.SHENNIAN_MACHINE_ID??null,managedRoomBound:e.managedRoomBound??(this.managedPolicies.has(e.sessionId)||void 0)})}setManagedAgentPolicy(e,t){if(t){this.managedPolicies.set(e,t);const r=this.registry.getManager(e);r&&!r.managedRoomBound&&this.registry.upsertManager({...r,managedRoomBound:!0})}else this.managedPolicies.delete(e)}getManagedAgentPolicyForWorker(e,t){const r=this.registry.getManager(e),n=this.managedPolicies.get(e);return!r||!n||f(t)!==r.workDir?null:n}isExternalWorkerController(e,t){const r=this.registry.getManager(e);return r?.controllerKind==="ephemeral"&&f(t)===r.workDir}isManagedWorkerSession(e){return!!this.findWorker(e)}isManagedRoomBoundManager(e){return this.registry.getManager(e)?.managedRoomBound===!0}async retireManagerAndWorkers(e){const t=this.registry.retireManager(e);this.managedPolicies.delete(e);const r=this.opts.getRuntime(),n=[],a=async(s,u)=>{const i=[];try{r.runJournal?.markSessionAborted(s)}catch(h){i.push(h)}try{r.recoveryBarrier?.end(s)}catch(h){i.push(h)}try{r.chatQueue?.purgeSession(s)}catch(h){i.push(h)}const o=r.sessions.get(s);let c=!o;if(o){if(o.managedRoomContextToken)try{r.revokeManagedRoomContextToken?.(o.managedRoomContextToken),o.managedRoomContextToken=null}catch(h){i.push(h)}try{await o.adapter.stop(),c=!0}catch(h){i.push(h)}}if(o&&c&&i.length===0&&(r.sessions.delete(s),o.heartbeatTimer&&clearInterval(o.heartbeatTimer),o.adapter.removeAllListeners()),c&&i.length===0)try{u?.()}catch(h){i.push(h),n.push(h)}else n.push(...i)};await a(e);for(const s of t)await a(s.sessionId,()=>this.registry.completeRetiredWorker(e,s.sessionId));if(n.length)throw new AggregateError(n,"Failed to retire every Manager or Worker session");return t.map(s=>s.sessionId)}setManagerWorkerDefaults(e,t,r){const n=this.registry.getManager(e);n&&this.registry.upsertManager({...n,defaultWorkerAgentType:t??null,defaultWorkerModelId:r??null})}getManagerWorkerDefaults(e){const t=this.registry.getManager(e);return{agentType:t?.defaultWorkerAgentType??null,modelId:t?.defaultWorkerModelId??null}}noteManagerAgentSession(e,t,r,n){const a=this.registry.getManager(e);this.registry.upsertManager({sessionId:e,agentSessionId:t,workDir:f(r),machineId:process.env.SHENNIAN_MACHINE_ID??null,modelId:n,agentType:a?.agentType,providerModelId:a?.providerModelId})}noteAgentEvent(e,t){const r=this.findWorker(e);if(!r)return;const n={lastActivityAt:new Date().toISOString(),runId:t.runId};if(t.agentSessionId&&(n.agentSessionId=t.agentSessionId),t.state==="start"){const i=this.pendingWorkerOrigins.get(e);this.pendingWorkerOrigins.delete(e),i&&(n.runRoomOrigins={...r.runRoomOrigins??{},[t.runId]:i})}const a=`${e}:${t.runId}`;let s=t.message||r.summary||"(\u65E0\u53EF\u89C1\u5185\u5BB9)";if(t.state==="delta"&&t.text&&!t.thinking){const i=(this.workerTextAcc.get(a)??"")+t.text;this.workerTextAcc.set(a,i);const o=R(i);o&&(n.summary=o.length>160?`${o.slice(0,160)}...`:o)}if(t.state==="final"||t.state==="error"||t.state==="aborted"){n.status=t.state;const i=R(this.workerTextAcc.get(a)??"");if(i&&(n.summary=i.length>240?`${i.slice(0,240)}...`:i,s=t.state==="final"?i:t.message||i),this.workerTextAcc.delete(a),r.runRoomOrigins?.[t.runId]){const o={...r.runRoomOrigins};delete o[t.runId],n.runRoomOrigins=o}}else t.state==="start"&&(n.status="running");const u=this.registry.updateWorker(e,n);u&&t.errorCode==="auth_retrying"&&this.registry.enqueueInboxEvent({managerSessionId:u.managedBy,workerSessionId:u.sessionId,kind:"worker.health",priority:"normal",runId:t.runId,roomOrigin:r.runRoomOrigins?.[t.runId]??null,summary:t.message||"Worker authentication is retrying.",code:"auth_retrying"}),u&&(t.state==="final"||t.state==="error"||t.state==="aborted")&&this.enqueueWorkerEvent(u.managedBy,u,t.state,t.runId,s,r.runRoomOrigins?.[t.runId]??null,t.errorCode==="auth_required"||t.errorCode==="acp_auth_required"?"auth_required":void 0)}findWorker(e){return this.registry.load().workers[e]}async handleAppReq(e){K(this.opts.getRuntime(),e.id,!1,{error:"feature_retired"})}async handleIpc(e,t){if(e.headers.authorization!==`Bearer ${this.ipcToken}`){m(t,401,{ok:!1,error:"Unauthorized"});return}try{const r=new URL(e.url??"/","http://127.0.0.1"),n=await J(e);if(r.pathname.startsWith("/channel/")||r.pathname.startsWith("/external/")||r.pathname.startsWith("/wechat-rpa/")){m(t,410,{ok:!1,error:"feature_retired"});return}const a={registry:this.registry,getRuntime:this.opts.getRuntime,getAgentSnapshot:this.opts.getAgentSnapshot,dispatchReq:this.opts.dispatchReq,dispatchChatEnqueue:this.dispatchChatEnqueue.bind(this),appendManagerVisibleMarkdown:this.appendManagerVisibleMarkdown.bind(this)},s=await H(a,r.pathname,n)??await S(a,r.pathname,n,String(e.headers["x-shennian-manager-session-id"]||""));if(s){m(t,s.status,s.body);return}m(t,404,{ok:!1,error:`Unknown manager IPC path: ${r.pathname}`})}catch(r){if(r instanceof B){m(t,r.status,{ok:!1,error:r.code,message:r.message,nextAction:r.nextAction});return}m(t,400,{ok:!1,error:r instanceof Error?r.message:String(r)})}}async executeScopedWorkerAction(e,t,r,n){const a={"manager.sessions.list":"/sessions/list","manager.sessions.start":"/sessions/start","manager.sessions.send":"/sessions/send","manager.sessions.queue":"/sessions/queue","manager.sessions.queue.edit":"/sessions/queue/edit","manager.sessions.queue.delete":"/sessions/queue/delete","manager.sessions.stop":"/sessions/stop","manager.sessions.read":"/sessions/read","manager.memory.path":"/memory/path"},s=await S({registry:this.registry,getRuntime:this.opts.getRuntime,getAgentSnapshot:this.opts.getAgentSnapshot,dispatchReq:this.opts.dispatchReq,dispatchChatEnqueue:this.dispatchChatEnqueue.bind(this),appendManagerVisibleMarkdown:this.appendManagerVisibleMarkdown.bind(this)},a[t],r,e,{roomOrigin:n});if(!s||s.status<200||s.status>=300)throw new Error(String(s?.body.error||"Managed Manager action failed"));return JSON.parse(JSON.stringify(s.body))}async dispatchChatEnqueue(e,t,r,n,a,s,u,i){if((await this.opts.dispatchReq(v({type:"req",id:`manager-enqueue-${k()}`,method:"chat.enqueue",params:{...w(),sessionId:e,text:n,agentType:t,workDir:r,agentSessionId:a,modelId:s,managedWorkerParentSessionId:u,managerWorkerRoomOrigin:i}})))?.durableQueueHandoff===!1)throw new Error("manager_worker_queue_handoff_unconfirmed")}appendManagerVisibleMarkdown(e,t,r){this.registry.getManager(e)?.controllerKind==="managed_session"&&(O(e,{id:r,sessionId:e,role:"agent",ts:Date.now(),payload:t},"committed"),q(this.opts.getRuntime().client))}enqueueWorkerEvent(e,t,r,n,a,s,u){const i=this.registry.enqueueInboxEvent({managerSessionId:e,workerSessionId:t.sessionId,kind:r==="final"?"worker.final":r==="error"?"worker.error":"worker.aborted",priority:"normal",runId:n??null,roomOrigin:s??null,summary:a||t.summary||"(\u65E0\u53EF\u89C1\u6458\u8981)",...u?{code:u}:{}}),o=this.registry.getManager(e);i&&o?.controllerKind==="managed_session"&&this.drainManagerInbox(e)}scanWorkerHealth(){const e=Date.now(),t=this.registry.load(),r=this.opts.getRuntime();for(const n of Object.values(t.workers)){if(n.status!=="running")continue;const a=e-Date.parse(n.createdAt);if(a<10*6e4)continue;const s=e-Date.parse(n.lastActivityAt),u=r.sessions.get(n.sessionId)?.currentRunId,i=!!r.chatQueue?.getSnapshot(n.sessionId).pending.length;if(!u){if(!i&&s>=10*6e4){const E="\u672A\u627E\u5230\u6D3B\u52A8\u4E2D\u7684 Provider Run \u6216\u5F85\u5904\u7406\u6D88\u606F\uFF1B\u5DF2\u5C06\u9057\u7559\u7684\u8FD0\u884C\u72B6\u6001\u6536\u655B\u4E3A\u5931\u8D25\u3002\u539F Worker \u4F1A\u8BDD\u4ECD\u4FDD\u7559\uFF0C\u53EF\u7EE7\u7EED\u53D1\u9001\u65B0\u6D88\u606F\u3002",I=this.registry.updateWorker(n.sessionId,{status:"error",lastActivityAt:new Date(e).toISOString(),summary:E});I&&this.enqueueWorkerEvent(I.managedBy,I,"error",n.runId??void 0,E)}continue}const o=n.healthNotifiedAt?Date.parse(n.healthNotifiedAt):0;if(o&&e-o<10*6e4)continue;const c=t.managers[n.managedBy];if(!c)continue;const h=Math.max(0,Math.floor(s/6e4)),d=R(n.runId?this.workerTextAcc.get(`${n.sessionId}:${n.runId}`)??"":""),l=`\u5DF2\u8FD0\u884C ${Math.floor(a/6e4)} \u5206\u949F\uFF0C\u5C1A\u672A\u7ED3\u675F\u3002
3
3
 
4
4
  \u5F53\u524D\u53EF\u89C1\u8FDB\u5C55\uFF1A
5
- - \u6700\u8FD1\u6587\u672C\uFF1A${g||n.summary||"(\u65E0\u53EF\u89C1\u5185\u5BB9)"}
5
+ - \u6700\u8FD1\u6587\u672C\uFF1A${d||n.summary||"(\u65E0\u53EF\u89C1\u5185\u5BB9)"}
6
6
  - \u6700\u8FD1\u6D3B\u52A8\u65F6\u95F4\uFF1A${n.lastActivityAt}
7
- - \u6700\u8FD1 ${h} \u5206\u949F\u6CA1\u6709\u65B0\u6D3B\u52A8\u3002`;this.registry.updateWorker(n.sessionId,{healthNotifiedAt:new Date(e).toISOString(),lastHealthSummary:l}),this.registry.enqueueInboxEvent({managerSessionId:c.sessionId,workerSessionId:n.sessionId,kind:"worker.health",priority:"normal",runId:n.runId??null,roomOrigin:n.runRoomOrigins?.[n.runId??""]??null,summary:l})&&c.controllerKind==="managed_session"&&this.drainManagerInbox(c.sessionId)}}async interruptAndResumeManager(e,r,t,n){this.opts.getRuntime().sessions.get(e.sessionId)?.currentRunId&&await this.opts.dispatchReq({type:"req",id:`manager-interrupt-${k()}`,method:"chat.abort",params:{...w(),sessionId:e.sessionId}}),await this.dispatchManagerContinuation(e,r,!1,t,n)}bindManagerAdapterEvents(e,r){r.on("agentEvent",t=>{if(t.state==="start"&&t.agentSessionId){const n=this.registry.getManager(e);n&&this.noteManagerAgentSession(e,t.agentSessionId,n.workDir,n.modelId)}t.state==="start"&&(this.acknowledgeManagerInboxDelivery(e),this.updateManagerStatus(e,"running")),(t.state==="final"||t.state==="error"||t.state==="aborted")&&(this.updateManagerStatus(e,"idle"),queueMicrotask(()=>{this.drainManagerInbox(e)}))})}updateManagerStatus(e,r){const t=this.registry.getManager(e);t&&this.registry.upsertManager({...t,status:r})}async drainManagerInbox(e){const r=this.registry.getManager(e);if(!r||r.deliveringInboxEventIds?.length)return;const t=this.registry.listInboxEvents(e);if(!t.length)return;const n=t.find(g=>g.priority==="high"),a=this.opts.getRuntime().sessions.get(e);if(r.status!=="idle"&&!n||a?.currentRunId&&!n)return;const u=(n??t[0]).roomOrigin??null,i=JSON.stringify(u),o=n?[n]:t.filter(g=>JSON.stringify(g.roomOrigin??null)===i).slice(0,20),c=o.map(g=>{const l=this.registry.getWorkerForManager(e,g.workerSessionId);return U({kind:G(g.kind),sessionId:g.workerSessionId,title:l?.title||$(l?.userGoal||l?.summary||g.summary),body:g.summary})}).join(`
7
+ - \u6700\u8FD1 ${h} \u5206\u949F\u6CA1\u6709\u65B0\u6D3B\u52A8\u3002`;this.registry.updateWorker(n.sessionId,{healthNotifiedAt:new Date(e).toISOString(),lastHealthSummary:l}),this.registry.enqueueInboxEvent({managerSessionId:c.sessionId,workerSessionId:n.sessionId,kind:"worker.health",priority:"normal",runId:n.runId??null,roomOrigin:n.runRoomOrigins?.[n.runId??""]??null,summary:l})&&c.controllerKind==="managed_session"&&this.drainManagerInbox(c.sessionId)}}async interruptAndResumeManager(e,t,r,n){this.opts.getRuntime().sessions.get(e.sessionId)?.currentRunId&&await this.opts.dispatchReq({type:"req",id:`manager-interrupt-${k()}`,method:"chat.abort",params:{...w(),sessionId:e.sessionId}}),await this.dispatchManagerContinuation(e,t,!1,r,n)}bindManagerAdapterEvents(e,t){t.on("agentEvent",r=>{if(r.state==="start"&&r.agentSessionId){const n=this.registry.getManager(e);n&&this.noteManagerAgentSession(e,r.agentSessionId,n.workDir,n.modelId)}r.state==="start"&&(this.acknowledgeManagerInboxDelivery(e),this.updateManagerStatus(e,"running")),(r.state==="final"||r.state==="error"||r.state==="aborted")&&(this.updateManagerStatus(e,"idle"),queueMicrotask(()=>{this.drainManagerInbox(e)}))})}updateManagerStatus(e,t){const r=this.registry.getManager(e);r&&this.registry.upsertManager({...r,status:t})}async drainManagerInbox(e){const t=this.registry.getManager(e);if(!t||t.deliveringInboxEventIds?.length)return;const r=this.registry.listInboxEvents(e);if(!r.length)return;const n=r.find(d=>d.priority==="high"),a=this.opts.getRuntime().sessions.get(e);if(t.status!=="idle"&&!n||a?.currentRunId&&!n)return;const u=(n??r[0]).roomOrigin??null,i=JSON.stringify(u),o=n?[n]:r.filter(d=>JSON.stringify(d.roomOrigin??null)===i).slice(0,20),c=o.map(d=>{const l=this.registry.getWorkerForManager(e,d.workerSessionId);return U({kind:G(d.kind),sessionId:d.workerSessionId,title:l?.title||$(l?.userGoal||l?.summary||d.summary),body:d.summary})}).join(`
8
8
 
9
9
  ---
10
10
 
11
- `),h=F(e,o);try{n&&a?.currentRunId?(this.registry.upsertManager({...r,status:"interrupting",deliveringInboxEventIds:o.map(l=>l.id)}),await this.interruptAndResumeManager(r,c,h,u)):(this.registry.upsertManager({...r,status:"running",deliveringInboxEventIds:o.map(l=>l.id)}),await this.dispatchManagerContinuation(r,c,!0,h,u)),this.registry.removeInboxEvents(e,o.map(l=>l.id));const g=this.registry.getManager(e);g&&this.registry.upsertManager({...g,deliveringInboxEventIds:[]})}catch(g){const l=this.registry.getManager(e);l&&this.registry.upsertManager({...l,status:"idle",deliveringInboxEventIds:[]}),console.error(`[manager-inbox] delivery failed sessionId=${e}: ${g instanceof Error?g.message:String(g)}`)}}acknowledgeManagerInboxDelivery(e){const r=this.registry.getManager(e),t=r?.deliveringInboxEventIds??[];!r||t.length===0||(this.registry.removeInboxEvents(e,t),this.registry.upsertManager({...r,deliveringInboxEventIds:[]}))}recoverPersistedInboxDeliveries(){const e=this.registry.load(),r=this.opts.getRuntime();for(const t of Object.values(e.managers)){if(r.sessions.get(t.sessionId)?.currentRunId)continue;(t.status!=="idle"||t.deliveringInboxEventIds?.length)&&this.registry.upsertManager({...t,status:"idle",deliveringInboxEventIds:[]});const a=this.registry.listInboxEvents(t.sessionId).filter(s=>s.kind==="worker.health"&&!r.sessions.get(s.workerSessionId)?.currentRunId).map(s=>s.id);this.registry.removeInboxEvents(t.sessionId,a),this.registry.listInboxEvents(t.sessionId).length>0&&queueMicrotask(()=>{this.drainManagerInbox(t.sessionId)})}}async dispatchManagerContinuation(e,r,t=!1,n,a){const s=e.agentType??"manager",u=s==="manager"?e.modelId:e.providerModelId??void 0,i={...w(),sessionId:e.sessionId,text:r,agentType:s,sessionMode:"manager",workDir:e.workDir,agentSessionId:e.agentSessionId,modelId:u,managerDefaultWorkerAgentType:e.defaultWorkerAgentType??null,managerDefaultWorkerModelId:e.defaultWorkerModelId??null,...e.managedRoomBound?{managedAgentDirect:!0}:{},clientMessageId:n,managerEventId:n,...a?{managerEventRoomOrigin:a}:{}};if((await this.opts.dispatchReq(S({type:"req",id:`manager-${t?"enqueue":"send"}-${k()}`,method:"chat.enqueue",params:i})))?.durableQueueHandoff===!1)throw new Error("manager_event_queue_handoff_unconfirmed")}getExternalChannelStatus(e){return this.channelRuntime.getManagerChannelStatus(e)}getManagerExternalChannelSystemPrompt(e){return this.channelRuntime.listManagerExternalChannels(e).map(r=>C(r,void 0,e,"manager")).join(`
11
+ `),h=F(e,o);try{n&&a?.currentRunId?(this.registry.upsertManager({...t,status:"interrupting",deliveringInboxEventIds:o.map(l=>l.id)}),await this.interruptAndResumeManager(t,c,h,u)):(this.registry.upsertManager({...t,status:"running",deliveringInboxEventIds:o.map(l=>l.id)}),await this.dispatchManagerContinuation(t,c,!0,h,u)),this.registry.removeInboxEvents(e,o.map(l=>l.id));const d=this.registry.getManager(e);d&&this.registry.upsertManager({...d,deliveringInboxEventIds:[]})}catch(d){const l=this.registry.getManager(e);l&&this.registry.upsertManager({...l,status:"idle",deliveringInboxEventIds:[]}),console.error(`[manager-inbox] delivery failed sessionId=${e}: ${d instanceof Error?d.message:String(d)}`)}}acknowledgeManagerInboxDelivery(e){const t=this.registry.getManager(e),r=t?.deliveringInboxEventIds??[];!t||r.length===0||(this.registry.removeInboxEvents(e,r),this.registry.upsertManager({...t,deliveringInboxEventIds:[]}))}recoverPersistedInboxDeliveries(){const e=this.registry.load(),t=this.opts.getRuntime();for(const r of Object.values(e.managers)){if(t.sessions.get(r.sessionId)?.currentRunId)continue;(r.status!=="idle"||r.deliveringInboxEventIds?.length)&&this.registry.upsertManager({...r,status:"idle",deliveringInboxEventIds:[]});const a=this.registry.listInboxEvents(r.sessionId).filter(s=>s.kind==="worker.health"&&!t.sessions.get(s.workerSessionId)?.currentRunId).map(s=>s.id);this.registry.removeInboxEvents(r.sessionId,a),this.registry.listInboxEvents(r.sessionId).length>0&&queueMicrotask(()=>{this.drainManagerInbox(r.sessionId)})}}async dispatchManagerContinuation(e,t,r=!1,n,a){const s=e.agentType??"manager",u=s==="manager"?e.modelId:e.providerModelId??void 0,i={...w(),sessionId:e.sessionId,text:t,agentType:s,sessionMode:"manager",workDir:e.workDir,agentSessionId:e.agentSessionId,modelId:u,managerDefaultWorkerAgentType:e.defaultWorkerAgentType??null,managerDefaultWorkerModelId:e.defaultWorkerModelId??null,...e.managedRoomBound?{managedAgentDirect:!0}:{},clientMessageId:n,managerEventId:n,...a?{managerEventRoomOrigin:a}:{}};if((await this.opts.dispatchReq(v({type:"req",id:`manager-${r?"enqueue":"send"}-${k()}`,method:"chat.enqueue",params:i})))?.durableQueueHandoff===!1)throw new Error("manager_event_queue_handoff_unconfirmed")}getExternalChannelStatus(e){return this.channelRuntime.getManagerChannelStatus(e)}getManagerExternalChannelSystemPrompt(e){return this.channelRuntime.listManagerExternalChannels(e).map(t=>C(t,void 0,e,"manager")).join(`
12
12
 
13
- `).trim()}}export{le as ManagerRuntimeService,he as getManagerRuntimeService,ge as setManagerRuntimeService};
13
+ `).trim()}}export{le as ManagerRuntimeService,he as getManagerRuntimeService,de as setManagerRuntimeService};
@@ -1,4 +1,4 @@
1
- import { type AgentType } from '@shennian/wire';
1
+ import { type AgentInfo, type AgentType } from '@shennian/wire';
2
2
  import type { SessionManagerRuntime } from '../session/types.js';
3
3
  import { ManagerRegistry, type ManagedWorkerRoomOrigin } from './registry.js';
4
4
  import type { LocalReqDispatcher } from './runtime.js';
@@ -9,6 +9,7 @@ export type WorkerIpcResponse = {
9
9
  type WorkerIpcHost = {
10
10
  registry: ManagerRegistry;
11
11
  getRuntime: () => SessionManagerRuntime;
12
+ getAgentSnapshot?: () => AgentInfo[];
12
13
  dispatchReq: LocalReqDispatcher;
13
14
  dispatchChatEnqueue: (sessionId: string, agentType: AgentType, workDir: string, text: string, agentSessionId: string | null, modelId?: string, managedWorkerParentSessionId?: string, roomOrigin?: ManagedWorkerRoomOrigin) => Promise<void>;
14
15
  appendManagerVisibleMarkdown: (managerSessionId: string, markdown: string, id: string) => void;
@@ -1 +1 @@
1
- import S from"node:fs";import q from"node:path";import{randomUUID as W}from"node:crypto";import{extractPayloadText as $,isAvailableAgentType as P,isAgentHiddenPayload as C,isToolPayload as U}from"@shennian/wire";import{loadConfig as O}from"../config/index.js";import{detectAgents as j}from"../agents/detect.js";import{getCachedAgentInfos as N}from"../agents/model-registry/service.js";import{getPersonalSyncIdentity as T}from"../personal-sync/epoch.js";import{readMessages as G,recordSession as K}from"../session/store.js";import{deriveWorkerTitle as p,formatWorkerMarkdown as h,normalizeWorkerTitle as L}from"./worker-markdown.js";class g extends Error{code;nextAction;status;constructor(a,r,i,k=400){super(r),this.code=a,this.nextAction=i,this.status=k}}function b(e){return q.resolve(e||process.cwd())}function B(e){const a=b(e);let r;try{r=S.statSync(a)}catch{throw new g("worker_workdir_invalid","The Worker working directory does not exist.","Choose an existing local project directory and retry worker start.")}if(!r.isDirectory())throw new g("worker_workdir_invalid","The Worker working directory is not a directory.","Choose an existing local project directory and retry worker start.");const i=S.realpathSync.native(a);try{S.accessSync(i,S.constants.R_OK|S.constants.X_OK)}catch{throw new g("worker_workdir_inaccessible","The Worker working directory is not readable and searchable by this user.","Choose a project directory this Shennian daemon user can access and retry worker start.")}return i}function x(e){return P(e)&&e!=="manager"&&e!=="pi"&&e!=="openclaw"}function z(e){return e==="worker.final"?"completed":e==="worker.error"?"failed":e==="worker.aborted"?"stopped":"progress"}function F(e,a){const r=[];for(const i of e)if(!C(i.payload))if(U(i.payload)){let k="tool",c="completed";try{const d=JSON.parse(i.payload);k=d.name||k,c=d.status||c}catch{}r.push({...i,payload:`[\u5DE5\u5177] ${k} \xB7 ${c}`})}else r.push({...i,payload:$(i.payload)});return r.slice(-Math.max(1,Math.min(a,500)))}async function M(e,a,r,i,k){if(!a.startsWith("/sessions/")&&a!=="/memory/path")return null;const c=String(r.managerSessionId||i||"");if(!c)throw new Error("managerSessionId is required");const d=e.registry.getManager(c);if(!d)throw new Error("Manager runtime is not registered");if(a==="/sessions/list"){const s=new Set([...e.getRuntime().sessions.entries()].filter(([,u])=>!!u.currentRunId).map(([u])=>u));return{status:200,body:{ok:!0,sessions:e.registry.listWorkers(c,{runningSessionIds:s})}}}if(a==="/memory/path")return{status:200,body:{ok:!0,path:q.join(d.workDir,".shennian")}};if(a==="/sessions/start"){const s=d.controllerKind==="ephemeral",o=s?String(r.agentType||r.agent||""):d.defaultWorkerAgentType||d.agentType||d.modelId;if(!x(o))throw new Error(`Unsupported manager worker agent: ${o}`);const u=s?b(String(r.workDir||d.workDir)):d.workDir;if(u!==d.workDir)throw new Error("Manager can only start workers in the same workDir");const m=String(r.message||"");if(!m)throw new Error("message is required");const n=r.title?L(String(r.title)):p(m),w=e.registry.addWorker({managerSessionId:c,agentType:o,workDir:u,title:n,summary:m.slice(0,120)}),f=s?String(r.modelId||""):d.defaultWorkerModelId??d.providerModelId??"";K({sessionId:w.sessionId,agentType:o,workDir:u,agentSessionId:null,modelId:f||null,title:n,titleSource:"shennian_generated"}),await e.dispatchChatEnqueue(w.sessionId,o,u,m,null,f||void 0,c,k?.roomOrigin);const y=h({kind:"created",sessionId:w.sessionId,title:n,body:m});return e.appendManagerVisibleMarkdown(c,y,`worker-created:${w.sessionId}`),{status:200,body:{ok:!0,session:w,markdown:y}}}const l=String(r.sessionId||""),t=e.registry.getWorkerForManager(c,l);if(!t||t.role!=="managed_worker"||t.managedBy!==c)throw new Error("Worker not found in this manager scope");if(a==="/sessions/send"){const s=String(r.message||"");if(!s.trim())throw new Error("message is required");const o=r.enqueue===void 0?!0:!!r.enqueue,m=e.getRuntime().sessions.get(l)?.currentRunId?"worker_busy":t.status==="final"||t.status==="error"||t.status==="aborted"?"worker_terminal":void 0,n=String(r.modelId||(t.agentType===d.defaultWorkerAgentType?d.defaultWorkerModelId??"":""));await e.dispatchChatEnqueue(t.sessionId,t.agentType,t.workDir,s,t.agentSessionId??null,n||void 0,c,k?.roomOrigin);const w=h({kind:"message",sessionId:l,title:t.title||p(t.userGoal||t.summary||s),body:s});return e.appendManagerVisibleMarkdown(c,w,`worker-message:${W()}`),{status:200,body:{ok:!0,markdown:w,queued:!0,directRequested:!o,...m?{code:m}:{}}}}if(a==="/sessions/queue")return{status:200,body:{ok:!0,queue:e.getRuntime().chatQueue?.getSnapshot(l)}};if(a==="/sessions/queue/edit"||a==="/sessions/queue/delete"){const s=a.endsWith("/edit");return await e.dispatchReq({type:"req",id:`manager-queue-${s?"edit":"delete"}-${W()}`,method:s?"chat.queue.edit":"chat.queue.delete",params:{...T(),sessionId:l,queueMessageId:String(r.queueMessageId||r.messageId||""),...s?{text:String(r.message||r.text||"")}:{}}}),{status:200,body:{ok:!0,queue:e.getRuntime().chatQueue?.getSnapshot(l)}}}if(a==="/sessions/stop"||a==="/sessions/terminate"){if(t.status==="final"||t.status==="error"||t.status==="aborted"){const s=t.status==="final"?"completed":t.status==="error"?"failed":"stopped";return{status:200,body:{ok:!0,alreadyTerminal:!0,code:"worker_terminal",status:t.status,markdown:h({kind:s,sessionId:l,title:t.title||p(t.userGoal||t.summary||""),body:t.summary||"\u672C\u6B21\u6267\u884C\u5DF2\u7ECF\u7ED3\u675F\u3002"})}}}return await e.dispatchReq({type:"req",id:`manager-abort-${W()}`,method:"chat.abort",params:{...T(),sessionId:l}}),e.registry.updateWorker(l,{status:"aborted"}),{status:200,body:{ok:!0,markdown:h({kind:"stopped",sessionId:l,title:t.title||p(t.userGoal||t.summary||""),body:"\u5DF2\u505C\u6B62\u672C\u6B21\u6267\u884C\u3002"})}}}if(a==="/sessions/read"){const s=Number(r.limit||200),o=G(l,{limit:Math.max(s*20,s)});return{status:200,body:{ok:!0,messages:F(o,s),rawMessageCount:o.length}}}if(a==="/sessions/wait"){const s=Math.max(1e3,Math.min(Number(r.timeoutMs||6e5),6e5)),o=Date.now()+s;let u;for(;Date.now()<o&&(u=e.registry.listInboxEvents(c).find(n=>n.workerSessionId===l),!u);)await new Promise(n=>setTimeout(n,1e3));if(!u){const n=e.registry.getWorkerForManager(c,l);if(n&&(n.status==="final"||n.status==="error"||n.status==="aborted")){const w=n.status==="final"?"completed":n.status==="error"?"failed":"stopped";return{status:200,body:{ok:!0,timedOut:!1,alreadyTerminal:!0,status:n.status,markdown:h({kind:w,sessionId:l,title:n.title||p(n.userGoal||n.summary||""),body:n.summary||"\u672C\u6B21\u6267\u884C\u5DF2\u7ECF\u7ED3\u675F\u3002"})}}}return{status:200,body:{ok:!0,timedOut:!0,code:"wait_timed_out",status:n?.status??"running",nextAction:"Call worker read or worker wait again later with the same workerRef."}}}const m=h({kind:z(u.kind),sessionId:l,title:t.title||p(t.userGoal||t.summary||u.summary),body:u.summary});return e.registry.removeInboxEvents(c,[u.id]),{status:200,body:{ok:!0,timedOut:!1,markdown:m,event:u}}}return null}const H={"/workers/read":{action:"read",managerPath:"/sessions/read"},"/workers/send":{action:"send",managerPath:"/sessions/send"},"/workers/wait":{action:"wait",managerPath:"/sessions/wait"},"/workers/stop":{action:"stop",managerPath:"/sessions/stop"}};async function oe(e,a,r){if(!a.startsWith("/workers/"))return null;const i=O();if(!i.machineId||!i.machineToken&&!i.accessToken)throw new g("auth_required","Shennian CLI must be paired and logged in.","Pair this installation with shennian pair, then retry the same worker command.",401);if(a==="/workers/start"){const s=String(r.message||"").trim();if(!s)throw new g("worker_message_required","A Worker task message is required.","Provide --message or --message-file and retry worker start.");const o=String(r.agentType||r.agent||"");if(!o||!x(o))throw new g("runtime_unavailable",`Unsupported Worker Agent: ${o||"(empty)"}`,"Run shennian agent discover --compact --json and select a Worker-capable Agent type.");const u=await j({serverUrl:i.serverUrl,authToken:i.machineToken??i.accessToken}),m=N(u).find(I=>I.type===o);if(!m||m.supportsExternalWorkerTarget!==!0)throw new g("runtime_unavailable",`Worker Agent is not available on this machine: ${o}`,"Run shennian agent discover --compact --json and select a discovered Agent with supportsExternalWorkerTarget=true.");if(m.authState==="required"||m.authState==="error")throw new g("auth_required",`Worker Agent requires provider sign-in: ${o}`,"Sign in with the provider\u2019s own CLI on this computer, refresh discovery, and retry worker start.");const n=typeof r.modelId=="string"?r.modelId.trim():"";if(n&&m.models.length>0&&!m.models.some(I=>I.id===n))throw new g("model_unavailable",`The selected model was not returned by discovery for ${o}.`,`Run shennian agent discover --agent ${o} --json and choose a returned model id.`);const w=B(String(r.workDir||process.cwd())),f=`ephemeral_controller_${W()}`;e.registry.upsertManager({sessionId:f,agentSessionId:null,workDir:w,machineId:i.machineId,modelId:"manager",agentType:"manager",controllerKind:"ephemeral",status:"idle"});let y;try{y=await M(e,"/sessions/start",{managerSessionId:f,agentType:o,workDir:w,title:r.title,modelId:n||void 0,message:s},"")}catch(I){const A=e.registry.retireManager(f);for(const D of A)e.registry.completeRetiredWorker(f,D.sessionId);throw I}const _=y.body.session,v=typeof _?.sessionId=="string"?_.sessionId:"";if(!v)throw new Error("worker_start_invalid_response");const{workerRef:R,grant:E}=e.registry.issueWorkerRef({managerSessionId:f,workerSessionId:v,workDir:w,machineId:i.machineId,accountNamespaceId:i.accountNamespaceId??null});return{status:200,body:{ok:!0,workerId:v,workerRef:R,resolvedWorkDir:w,agentType:o,status:typeof _?.status=="string"?_.status:"running",expiresAt:E.expiresAt}}}const k=H[a];if(!k)throw new g("worker_command_unavailable","Unknown external Worker command.","Use worker start, read, send, wait, or stop. External Worker history cannot be listed.",404);const c=String(r.workerRef||r.ref||""),d=e.registry.resolveWorkerRefDetailed({workerRef:c,action:k.action,machineId:i.machineId,accountNamespaceId:i.accountNamespaceId??null});if(!d.ok){const s=d.reason==="expired"?"worker_ref_expired":d.reason==="revoked"?"worker_ref_revoked":"worker_ref_invalid";throw new g(s,`The Worker reference is ${d.reason} or belongs to another local scope.`,d.reason==="invalid"?"Use the exact workerRef returned by worker start. Lost references cannot be rediscovered.":"Start a new Worker if the task still needs to continue.",403)}const l=d.grant;let t;try{t=await M(e,k.managerPath,{...r,managerSessionId:l.managerSessionId,sessionId:l.workerSessionId},"")}catch(s){const o=s instanceof Error?s.message:String(s);throw o==="message is required"?new g("worker_message_required","A non-empty Worker message is required.","Provide --message or --message-file and retry worker send."):o.includes("not found")||o.includes("not registered")?new g("worker_ref_revoked","The Worker is no longer available in this controller scope.","Start a new Worker if the task still needs to continue.",410):new g("worker_command_failed",o,"Read the Worker state, correct the request, and retry once.")}if(!t)throw new g("worker_command_unavailable","The requested Worker operation is unavailable.","Use worker start, read, send, wait, or stop.",404);return{status:t.status,body:{...t.body,workerId:l.workerSessionId}}}export{g as EphemeralWorkerError,oe as handleEphemeralWorkerIpcRoute,M as handleWorkerIpcRoute};
1
+ import S from"node:fs";import v from"node:path";import{randomUUID as _}from"node:crypto";import{extractPayloadText as D,isAvailableAgentType as $,isAgentHiddenPayload as P,isToolPayload as C}from"@shennian/wire";import{loadConfig as U}from"../config/index.js";import{getPersonalSyncIdentity as q}from"../personal-sync/epoch.js";import{readMessages as O,recordSession as j}from"../session/store.js";import{deriveWorkerTitle as p,formatWorkerMarkdown as h,normalizeWorkerTitle as N}from"./worker-markdown.js";class w extends Error{code;nextAction;status;constructor(a,r,u,k=400){super(r),this.code=a,this.nextAction=u,this.status=k}}function b(e){return v.resolve(e||process.cwd())}function G(e){const a=b(e);let r;try{r=S.statSync(a)}catch{throw new w("worker_workdir_invalid","The Worker working directory does not exist.","Choose an existing local project directory and retry worker start.")}if(!r.isDirectory())throw new w("worker_workdir_invalid","The Worker working directory is not a directory.","Choose an existing local project directory and retry worker start.");const u=S.realpathSync.native(a);try{S.accessSync(u,S.constants.R_OK|S.constants.X_OK)}catch{throw new w("worker_workdir_inaccessible","The Worker working directory is not readable and searchable by this user.","Choose a project directory this Shennian daemon user can access and retry worker start.")}return u}function x(e){return $(e)&&e!=="manager"&&e!=="pi"&&e!=="openclaw"}function K(e){return e==="worker.final"?"completed":e==="worker.error"?"failed":e==="worker.aborted"?"stopped":"progress"}function L(e,a){const r=[];for(const u of e)if(!P(u.payload))if(C(u.payload)){let k="tool",c="completed";try{const d=JSON.parse(u.payload);k=d.name||k,c=d.status||c}catch{}r.push({...u,payload:`[\u5DE5\u5177] ${k} \xB7 ${c}`})}else r.push({...u,payload:D(u.payload)});return r.slice(-Math.max(1,Math.min(a,500)))}async function M(e,a,r,u,k){if(!a.startsWith("/sessions/")&&a!=="/memory/path")return null;const c=String(r.managerSessionId||u||"");if(!c)throw new Error("managerSessionId is required");const d=e.registry.getManager(c);if(!d)throw new Error("Manager runtime is not registered");if(a==="/sessions/list"){const s=new Set([...e.getRuntime().sessions.entries()].filter(([,i])=>!!i.currentRunId).map(([i])=>i));return{status:200,body:{ok:!0,sessions:e.registry.listWorkers(c,{runningSessionIds:s})}}}if(a==="/memory/path")return{status:200,body:{ok:!0,path:v.join(d.workDir,".shennian")}};if(a==="/sessions/start"){const s=d.controllerKind==="ephemeral",o=s?String(r.agentType||r.agent||""):d.defaultWorkerAgentType||d.agentType||d.modelId;if(!x(o))throw new Error(`Unsupported manager worker agent: ${o}`);const i=s?b(String(r.workDir||d.workDir)):d.workDir;if(i!==d.workDir)throw new Error("Manager can only start workers in the same workDir");const g=String(r.message||"");if(!g)throw new Error("message is required");const n=r.title?N(String(r.title)):p(g),m=e.registry.addWorker({managerSessionId:c,agentType:o,workDir:i,title:n,summary:g.slice(0,120)}),y=s?String(r.modelId||""):d.defaultWorkerModelId??d.providerModelId??"";j({sessionId:m.sessionId,agentType:o,workDir:i,agentSessionId:null,modelId:y||null,title:n,titleSource:"shennian_generated"}),await e.dispatchChatEnqueue(m.sessionId,o,i,g,null,y||void 0,c,k?.roomOrigin);const f=h({kind:"created",sessionId:m.sessionId,title:n,body:g});return e.appendManagerVisibleMarkdown(c,f,`worker-created:${m.sessionId}`),{status:200,body:{ok:!0,session:m,markdown:f}}}const l=String(r.sessionId||""),t=e.registry.getWorkerForManager(c,l);if(!t||t.role!=="managed_worker"||t.managedBy!==c)throw new Error("Worker not found in this manager scope");if(a==="/sessions/send"){const s=String(r.message||"");if(!s.trim())throw new Error("message is required");const o=r.enqueue===void 0?!0:!!r.enqueue,g=e.getRuntime().sessions.get(l)?.currentRunId?"worker_busy":t.status==="final"||t.status==="error"||t.status==="aborted"?"worker_terminal":void 0,n=String(r.modelId||(t.agentType===d.defaultWorkerAgentType?d.defaultWorkerModelId??"":""));await e.dispatchChatEnqueue(t.sessionId,t.agentType,t.workDir,s,t.agentSessionId??null,n||void 0,c,k?.roomOrigin);const m=h({kind:"message",sessionId:l,title:t.title||p(t.userGoal||t.summary||s),body:s});return e.appendManagerVisibleMarkdown(c,m,`worker-message:${_()}`),{status:200,body:{ok:!0,markdown:m,queued:!0,directRequested:!o,...g?{code:g}:{}}}}if(a==="/sessions/queue")return{status:200,body:{ok:!0,queue:e.getRuntime().chatQueue?.getSnapshot(l)}};if(a==="/sessions/queue/edit"||a==="/sessions/queue/delete"){const s=a.endsWith("/edit");return await e.dispatchReq({type:"req",id:`manager-queue-${s?"edit":"delete"}-${_()}`,method:s?"chat.queue.edit":"chat.queue.delete",params:{...q(),sessionId:l,queueMessageId:String(r.queueMessageId||r.messageId||""),...s?{text:String(r.message||r.text||"")}:{}}}),{status:200,body:{ok:!0,queue:e.getRuntime().chatQueue?.getSnapshot(l)}}}if(a==="/sessions/stop"||a==="/sessions/terminate"){if(t.status==="final"||t.status==="error"||t.status==="aborted"){const s=t.status==="final"?"completed":t.status==="error"?"failed":"stopped";return{status:200,body:{ok:!0,alreadyTerminal:!0,code:"worker_terminal",status:t.status,markdown:h({kind:s,sessionId:l,title:t.title||p(t.userGoal||t.summary||""),body:t.summary||"\u672C\u6B21\u6267\u884C\u5DF2\u7ECF\u7ED3\u675F\u3002"})}}}return await e.dispatchReq({type:"req",id:`manager-abort-${_()}`,method:"chat.abort",params:{...q(),sessionId:l}}),e.registry.updateWorker(l,{status:"aborted"}),{status:200,body:{ok:!0,markdown:h({kind:"stopped",sessionId:l,title:t.title||p(t.userGoal||t.summary||""),body:"\u5DF2\u505C\u6B62\u672C\u6B21\u6267\u884C\u3002"})}}}if(a==="/sessions/read"){const s=Number(r.limit||200),o=O(l,{limit:Math.max(s*20,s)});return{status:200,body:{ok:!0,messages:L(o,s),rawMessageCount:o.length}}}if(a==="/sessions/wait"){const s=Math.max(1e3,Math.min(Number(r.timeoutMs||6e5),6e5)),o=Date.now()+s;let i;for(;Date.now()<o&&(i=e.registry.listInboxEvents(c).find(n=>n.workerSessionId===l),!i);)await new Promise(n=>setTimeout(n,1e3));if(!i){const n=e.registry.getWorkerForManager(c,l);if(n&&(n.status==="final"||n.status==="error"||n.status==="aborted")){const m=n.status==="final"?"completed":n.status==="error"?"failed":"stopped";return{status:200,body:{ok:!0,timedOut:!1,alreadyTerminal:!0,status:n.status,markdown:h({kind:m,sessionId:l,title:n.title||p(n.userGoal||n.summary||""),body:n.summary||"\u672C\u6B21\u6267\u884C\u5DF2\u7ECF\u7ED3\u675F\u3002"})}}}return{status:200,body:{ok:!0,timedOut:!0,code:"wait_timed_out",status:n?.status??"running",nextAction:"Call worker read or worker wait again later with the same workerRef."}}}const g=h({kind:K(i.kind),sessionId:l,title:t.title||p(t.userGoal||t.summary||i.summary),body:i.summary});return e.registry.removeInboxEvents(c,[i.id]),{status:200,body:{ok:!0,timedOut:!1,markdown:g,event:i}}}return null}const B={"/workers/read":{action:"read",managerPath:"/sessions/read"},"/workers/send":{action:"send",managerPath:"/sessions/send"},"/workers/wait":{action:"wait",managerPath:"/sessions/wait"},"/workers/stop":{action:"stop",managerPath:"/sessions/stop"}};async function Z(e,a,r){if(!a.startsWith("/workers/"))return null;const u=U();if(!u.machineId||!u.machineToken&&!u.accessToken)throw new w("auth_required","Shennian CLI must be paired and logged in.","Pair this installation with shennian pair, then retry the same worker command.",401);if(a==="/workers/start"){const s=String(r.message||"").trim();if(!s)throw new w("worker_message_required","A Worker task message is required.","Provide --message or --message-file and retry worker start.");const o=String(r.agentType||r.agent||"");if(!o||!x(o))throw new w("runtime_unavailable",`Unsupported Worker Agent: ${o||"(empty)"}`,"Run shennian agent discover --compact --json and select a Worker-capable Agent type.");const i=e.getAgentSnapshot?.().find(I=>I.type===o);if(!i||i.supportsExternalWorkerTarget!==!0)throw new w("runtime_unavailable",`Worker Agent is not available on this machine: ${o}`,"Run shennian agent discover --compact --json and select a discovered Agent with supportsExternalWorkerTarget=true.");if(i.authState==="required"||i.authState==="error")throw new w("auth_required",`Worker Agent requires provider sign-in: ${o}`,"Sign in with the provider\u2019s own CLI on this computer, refresh discovery, and retry worker start.");const g=typeof r.modelId=="string"?r.modelId.trim():"";if(g&&i.models.length>0&&!i.models.some(I=>I.id===g))throw new w("model_unavailable",`The selected model was not returned by discovery for ${o}.`,`Run shennian agent discover --agent ${o} --json and choose a returned model id.`);const n=G(String(r.workDir||process.cwd())),m=`ephemeral_controller_${_()}`;e.registry.upsertManager({sessionId:m,agentSessionId:null,workDir:n,machineId:u.machineId,modelId:"manager",agentType:"manager",controllerKind:"ephemeral",status:"idle"});let y;try{y=await M(e,"/sessions/start",{managerSessionId:m,agentType:o,workDir:n,title:r.title,modelId:g||void 0,message:s},"")}catch(I){const E=e.registry.retireManager(m);for(const A of E)e.registry.completeRetiredWorker(m,A.sessionId);throw I}const f=y.body.session,W=typeof f?.sessionId=="string"?f.sessionId:"";if(!W)throw new Error("worker_start_invalid_response");const{workerRef:T,grant:R}=e.registry.issueWorkerRef({managerSessionId:m,workerSessionId:W,workDir:n,machineId:u.machineId,accountNamespaceId:u.accountNamespaceId??null});return{status:200,body:{ok:!0,workerId:W,workerRef:T,resolvedWorkDir:n,agentType:o,status:typeof f?.status=="string"?f.status:"running",expiresAt:R.expiresAt}}}const k=B[a];if(!k)throw new w("worker_command_unavailable","Unknown external Worker command.","Use worker start, read, send, wait, or stop. External Worker history cannot be listed.",404);const c=String(r.workerRef||r.ref||""),d=e.registry.resolveWorkerRefDetailed({workerRef:c,action:k.action,machineId:u.machineId,accountNamespaceId:u.accountNamespaceId??null});if(!d.ok){const s=d.reason==="expired"?"worker_ref_expired":d.reason==="revoked"?"worker_ref_revoked":"worker_ref_invalid";throw new w(s,`The Worker reference is ${d.reason} or belongs to another local scope.`,d.reason==="invalid"?"Use the exact workerRef returned by worker start. Lost references cannot be rediscovered.":"Start a new Worker if the task still needs to continue.",403)}const l=d.grant;let t;try{t=await M(e,k.managerPath,{...r,managerSessionId:l.managerSessionId,sessionId:l.workerSessionId},"")}catch(s){const o=s instanceof Error?s.message:String(s);throw o==="message is required"?new w("worker_message_required","A non-empty Worker message is required.","Provide --message or --message-file and retry worker send."):o.includes("not found")||o.includes("not registered")?new w("worker_ref_revoked","The Worker is no longer available in this controller scope.","Start a new Worker if the task still needs to continue.",410):new w("worker_command_failed",o,"Read the Worker state, correct the request, and retry once.")}if(!t)throw new w("worker_command_unavailable","The requested Worker operation is unavailable.","Use worker start, read, send, wait, or stop.",404);return{status:t.status,body:{...t.body,workerId:l.workerSessionId}}}export{w as EphemeralWorkerError,Z as handleEphemeralWorkerIpcRoute,M as handleWorkerIpcRoute};
@@ -1,10 +1,10 @@
1
- import type { AgentInfo, ReqFrame } from '@shennian/wire';
1
+ import type { AgentInfo, AgentType, ReqFrame } from '@shennian/wire';
2
2
  import type { SessionManagerRuntime } from '../types.js';
3
3
  /** Shared discovery path for the App refresh and local CLI. Never sends a model prompt. */
4
- export declare function scanMachineAgents(refresh?: boolean): Promise<{
4
+ export declare function scanMachineAgents(refresh?: boolean, agentType?: AgentType): Promise<{
5
5
  detectedAgents: import("../../agents/detect.js").DetectedAgent[];
6
6
  agents: AgentInfo[];
7
- agentList: import("@shennian/wire").AgentType[];
7
+ agentList: AgentType[];
8
8
  }>;
9
9
  export declare function refreshMachineAgents(runtime: SessionManagerRuntime, req?: ReqFrame): Promise<void>;
10
10
  export declare function handleModelsRefresh(runtime: SessionManagerRuntime, req: ReqFrame): Promise<void>;
@@ -1 +1 @@
1
- import{SERVERS as p}from"../../region.js";import{loadConfig as l}from"../../config/index.js";import{detectAgents as g}from"../../agents/detect.js";import{refreshAgentInfos as d}from"../../agents/model-registry.js";import{resetCommandSpecCache as r}from"../../agents/command-spec.js";import{clearAcpCapabilityCache as c}from"../../agents/acp/capability-cache.js";const i=new WeakMap;async function h(e=!1){e&&(r(),c());const t=l(),n={serverUrl:t.serverUrl??p.cn.url,authToken:t.machineToken??t.accessToken},s=await g(n),a=s.length?await d(s,n):[];return{detectedAgents:s,agents:a,agentList:s.map(o=>o.type)}}async function f(e,t){try{const{agentList:n,agents:s}=await h(),a={agentList:n,agents:s};t&&e.client.sendRes({type:"res",id:t.id,ok:!0,payload:a});const o=JSON.stringify(a);i.get(e.client)!==o&&(i.set(e.client,o),e.client.sendEvent({type:"event",event:"machine.agents",payload:a}))}catch(n){if(!t)return;e.client.sendRes({type:"res",id:t.id,ok:!1,error:n instanceof Error?n.message:String(n)})}}async function C(e,t){await f(e,t)}async function R(e,t){r(),c(),e.reloadCustomAgents(),await f(e,t)}export{R as handleAgentsRefresh,C as handleModelsRefresh,f as refreshMachineAgents,h as scanMachineAgents};
1
+ import{SERVERS as l}from"../../region.js";import{loadConfig as g}from"../../config/index.js";import{detectAgents as d}from"../../agents/detect.js";import{refreshAgentInfos as h}from"../../agents/model-registry.js";import{resetCommandSpecCache as r}from"../../agents/command-spec.js";import{clearAcpCapabilityCache as c}from"../../agents/acp/capability-cache.js";const i=new WeakMap;async function m(e=!1,t){e&&(r(),c());const n=g(),o={serverUrl:n.serverUrl??l.cn.url,authToken:n.machineToken??n.accessToken},s=await d({...o,...t?{agentType:t}:{}}),a=s.length?await h(s,o):[];return{detectedAgents:s,agents:a,agentList:s.map(p=>p.type)}}async function f(e,t){try{const{agentList:n,agents:o}=await m(),s={agentList:n,agents:o};t&&e.client.sendRes({type:"res",id:t.id,ok:!0,payload:s});const a=JSON.stringify(s);i.get(e.client)!==a&&(i.set(e.client,a),e.client.sendEvent({type:"event",event:"machine.agents",payload:s}))}catch(n){if(!t)return;e.client.sendRes({type:"res",id:t.id,ok:!1,error:n instanceof Error?n.message:String(n)})}}async function R(e,t){await f(e,t)}async function S(e,t){r(),c(),e.reloadCustomAgents(),await f(e,t)}export{S as handleAgentsRefresh,R as handleModelsRefresh,f as refreshMachineAgents,m as scanMachineAgents};
@@ -18,6 +18,7 @@ import type { ManagedManagerExecutor, ManagedRoomExecutor } from '../room/manage
18
18
  import { ActiveRunJournal } from './run-recovery/journal.js';
19
19
  import { type TrustedProjectFsRequestResolver } from './handlers/project-fs.js';
20
20
  export { resolveSessionWorkDir } from '../fs/boundary.js';
21
+ export declare function upgradeRecoveryVisibleMessage(): string;
21
22
  export declare class SessionManager {
22
23
  private client;
23
24
  private nativeFusion;
@@ -28,6 +29,7 @@ export declare class SessionManager {
28
29
  private managedRoomContextRevoker;
29
30
  private managedRoomExecutor;
30
31
  private trustedProjectFsRequestResolver;
32
+ private getAgentSnapshot;
31
33
  scheduleHandler?: (req: ReqFrame) => Promise<void>;
32
34
  getScheduleQueue(): ChatQueueManager;
33
35
  private sessions;
@@ -51,7 +53,7 @@ export declare class SessionManager {
51
53
  private recoveryPass;
52
54
  private prepareManagedRecovery;
53
55
  private projectFsHandleStore;
54
- constructor(client: CliRelayClient, nativeFusion?: NativeSessionFusionService | null, cliVersion?: string | undefined, upgradePolicyController?: UpgradePolicyController | null, managedRoomBinder?: ManagedRoomBinder | null, remoteAccessPauseController?: RemoteAccessPauseController | null, managedRoomContextRevoker?: ((token: string) => void) | null, managedRoomExecutor?: ManagedRoomExecutor | null, runJournalOverride?: ActiveRunJournal | null, trustedProjectFsRequestResolver?: TrustedProjectFsRequestResolver | null);
56
+ constructor(client: CliRelayClient, nativeFusion?: NativeSessionFusionService | null, cliVersion?: string | undefined, upgradePolicyController?: UpgradePolicyController | null, managedRoomBinder?: ManagedRoomBinder | null, remoteAccessPauseController?: RemoteAccessPauseController | null, managedRoomContextRevoker?: ((token: string) => void) | null, managedRoomExecutor?: ManagedRoomExecutor | null, runJournalOverride?: ActiveRunJournal | null, trustedProjectFsRequestResolver?: TrustedProjectFsRequestResolver | null, getAgentSnapshot?: (() => import('@shennian/wire').AgentInfo[]) | null);
55
57
  executeManagedManagerAction(input: Parameters<ManagedManagerExecutor>[0]): Promise<import("../daemon-ipc/json.js").JsonValue>;
56
58
  private getRuntime;
57
59
  resumePendingAcpAuthentication(agentType: string): Promise<boolean>;
@@ -1,3 +1 @@
1
- import{SESSION_SYNC_PROTOCOL_VERSION as g,SESSION_SYNC_DATA_EPOCH as f}from"@shennian/wire";import h from"node:crypto";import{getRegisteredAgents as k,unregisterAgent as I}from"../agents/adapter.js";import{resetCommandSpecCache as A}from"../agents/command-spec.js";import{loadConfig as T}from"../config/index.js";import{handleUpgradeStart as P,handleUpgradeStatus as C}from"../commands/upgrade.js";import{handleAgentsRefresh as M,handleModelsRefresh as D,refreshMachineAgents as x}from"./handlers/agents.js";import{AcpDiscoveryWatcher as E}from"../agents/acp/watcher.js";import{handleAgentCapabilitiesList as F}from"./handlers/agent-capabilities.js";import{handleAgentConfigClear as O,handleAgentConfigGet as _,handleAgentConfigTest as Q,handleAgentConfigUpsert as j}from"./handlers/agent-config.js";import{handleChatAbort as B,handleChatSend as U}from"./handlers/chat.js";import{recoverJournaledRun as z}from"./handlers/chat-recovery.js";import{handleSessionRefresh as W}from"./handlers/session-refresh.js";import{handleSessionToolDetail as $}from"./handlers/tool-detail.js";import{handleSessionTitleSet as H}from"./handlers/title.js";import{handleSessionMessageStatus as J}from"./handlers/message-status.js";import{handleAgentWorkspaceValidation as N}from"./handlers/agent-workspace-validation.js";import{handleVoiceSessionBodyWindow as V}from"./handlers/voice-window.js";import{handleRemoteAccessPause as L}from"./handlers/remote-access.js";import{cleanupPendingTransfers as G,handleFsLs as Y,handleFsRead as K,handleFsRename as X,handleFsWrite as Z,handleFsTransferAbort as q,handleFsTransferChunk as ee,handleFsTransferFinish as se,handleFsTransferStart as te,handleFsExportMarkdownPdf as ae,handleFsExportMarkdownPdfSetup as ne,handleFsArchiveZip as re}from"./handlers/fs.js";import{handleRegionProbe as ie,handleRegionSwitch as oe,handleUpgradeSetPolicy as ce}from"./handlers/control.js";import{ManagerRuntimeService as le,setManagerRuntimeService as v}from"../manager/runtime.js";import{ChatQueueManager as de}from"./queue.js";import{createAuthorizedFsRoot as he,createAuthorizedFsRootAsync as ue,resolveAuthorizedPath as me,resolveAuthorizedPathAsync as pe,resolveSessionWorkDir as ge}from"../fs/boundary.js";import"../agents/claude.js";import"../agents/codex.js";import"../agents/workbuddy.js";import"../agents/gemini.js";import"../agents/cursor.js";import"../agents/opencode.js";import"../agents/pi-coding-agent.js";import"../agents/manager.js";import{registerCustomAgent as fe}from"../agents/custom.js";import{handleManagedRoomBind as ve,handleManagedRoomRestart as ye,discardPreBindingSession as Re}from"./handlers/room-managed.js";import{getDaemonInstallationId as u,listSessionRecords as m,purgeCanonicalSession as be,purgeSessionRecord as Se,recordSession as we,upsertMessage as ke}from"./store.js";import{requestSessionIndexPublish as Ie}from"./index-publisher.js";import{reportUpgradeAggregateMetric as Ae}from"../log-reporter.js";import{PersonalHistorySubscriptions as Te}from"./history-subscriptions.js";import{sweepAttachmentTransfers as Pe}from"./attachment-transfer-store.js";import{NativeSourceDeletionStore as Ce}from"../native-fusion/native-source-deletions.js";import{purgeToolDetails as Me}from"./tool-detail-store.js";import{readDeletionSyncRevision as De,writeDeletionSyncRevision as y}from"./deletion-sync-state.js";import{assertDeletionSyncRunnable as xe,clearDeletionSyncFailure as Ee,DeletionSyncRetryWaitError as Fe,DeletionSyncTerminalError as Oe,recordDeletionSyncFailure as p}from"./deletion-sync-state.js";import{NativeSessionCleanupOutbox as _e}from"./native-cleanup-outbox.js";import{assertPersonalSyncIdentity as c}from"../personal-sync/epoch.js";import{ActiveRunJournal as Qe}from"./run-recovery/journal.js";import{RunRecoveryBarrier as je}from"./run-recovery/barrier.js";import{RunRecoveryCoordinator as Be}from"./run-recovery/coordinator.js";import{ProjectFsHandleStore as Ue}from"./project-fs-store.js";import{handleProjectFsRequest as ze}from"./handlers/project-fs.js";import{handleAgentPermissionResponse as We,handleAcpAgentRequest as $e}from"./handlers/agent-control.js";import{validateDeletionSyncResponse as He}from"./deletion-sync-validation.js";import{dispatchManagedChat as Je,dispatchQueuedChat as Ne,interruptQueuedChat as Ve}from"./queued-chat-dispatch.js";const Le=50;import{resolveSessionWorkDir as Xs}from"../fs/boundary.js";class Gs{client;nativeFusion;cliVersion;upgradePolicyController;managedRoomBinder;remoteAccessPauseController;managedRoomContextRevoker;managedRoomExecutor;trustedProjectFsRequestResolver;scheduleHandler;getScheduleQueue(){return this.chatQueue}sessions=new Map;processedReqIds=new Set;runTextAcc=new Map;pendingTransfers=new Map;managerRuntime;chatQueue;historySubscriptions;nativeCleanupOutbox;activityProbeTimer=null;attachmentCleanupTimer=null;activeRequests=0;acpDiscoveryWatcher;runJournal;recoveryBarrier=new je;recoveryCoordinator;recoveryPass=null;prepareManagedRecovery;projectFsHandleStore=new Ue;constructor(e,t=null,s,r=null,n=null,d=null,o=null,R=null,b=null,S=null){this.client=e,this.nativeFusion=t,this.cliVersion=s,this.upgradePolicyController=r,this.managedRoomBinder=n,this.remoteAccessPauseController=d,this.managedRoomContextRevoker=o,this.managedRoomExecutor=R,this.trustedProjectFsRequestResolver=S,this.runJournal=b??new Qe(u());for(const a of this.runJournal.loadPendingReconciliation().records)this.recoveryBarrier.begin(a.sessionId);this.recoveryCoordinator=new Be({journal:this.runJournal,barrier:this.recoveryBarrier,recover:(a,i)=>z(this.getRuntime(),a,i),commitTerminal:(a,i)=>this.commitRecoveryTerminal(a,i),isCanonicalTerminal:a=>{const i=m().find(w=>w.sessionId===a.sessionId),l=`terminal:${h.createHash("sha256").update(`${a.runId}\0`).digest("hex")}`;return i?.lastAttentionId===l},onOutcome:a=>Ae({kind:"recovery",adapterType:a.adapterType,result:a.result,stage:a.result==="continued"?"continue":a.result==="reattached"?"reattach":a.result==="resumed"?"resume":a.result==="terminal_reconciled"?"inspect":"terminal",durationMs:a.durationMs,retries:Math.max(0,a.attempts-1),...a.reason?{errorCode:a.reason}:{}})}),this.managerRuntime=new le({getRuntime:()=>this.getRuntime(),dispatchReq:a=>this.handleReq(a,{propagateErrors:!0})}),this.chatQueue=new de({getRuntime:()=>this.getRuntime(),dispatchReq:a=>this.handleReq(a),dispatchManagedReq:(a,i)=>Je(this.getRuntime(),a,i),dispatchQueuedReq:(a,i)=>Ne(this.getRuntime(),a,i),interruptQueuedSession:a=>Ve(this.getRuntime(),a)}),this.historySubscriptions=new Te(this.client),this.nativeCleanupOutbox=new _e({logger:console.info}),this.acpDiscoveryWatcher=new E(async()=>{A(),await x(this.getRuntime())}),this.nativeFusion?.setExternalTerminalHandler?.(a=>{this.handleExternalTerminal(a)}),this.nativeFusion?.setExternalClaimHandler?.((a,i)=>{this.chatQueue.noteExternalClaim(a,i)}),this.nativeCleanupOutbox.start(),v(this.managerRuntime),this.managerRuntime.start(),this.reloadCustomAgents();try{this.acpDiscoveryWatcher.start()}catch(a){console.warn(`[acp.discovery] watcher unavailable: ${a instanceof Error?a.message:String(a)}`)}this.activityProbeTimer=setInterval(()=>{this.publishManagedActivitySnapshots().catch(a=>{console.error("[session.activity] managed probe failed",a)})},15e3),this.activityProbeTimer.unref?.(),this.attachmentCleanupTimer=setInterval(()=>{const a=new Set([...m().map(i=>i.workDir),...Array.from(this.pendingTransfers.values()).map(i=>i.rootPath)]);for(const i of a)try{Pe(this.resolvePath(i))}catch(l){console.error("[attachment.cleanup] failed",l instanceof Error?l.message:String(l))}},6e4),this.attachmentCleanupTimer.unref?.()}executeManagedManagerAction(e){return this.managerRuntime.executeScopedWorkerAction(e.managerSessionId,e.action,e.params,e.roomOrigin)}getRuntime(){return{client:this.client,pendingTransfers:this.pendingTransfers,processedReqIds:this.processedReqIds,reloadCustomAgents:()=>this.reloadCustomAgents(),resolvePath:e=>this.resolvePath(e),resolveAuthorizedPath:(e,t)=>this.resolveAuthorizedPath(e,t),resolveAuthorizedPathAsync:(e,t)=>this.resolveAuthorizedPathAsync(e,t),runTextAcc:this.runTextAcc,sessions:this.sessions,evictIdleSessions:()=>this.evictIdleSessions(),nativeFusion:this.nativeFusion,managerRuntime:this.managerRuntime,chatQueue:this.chatQueue,activityPublisher:{publish:(e,t)=>this.publishSessionActivity(e,t)},upgradePolicyController:this.upgradePolicyController,revokeManagedRoomContextToken:this.managedRoomContextRevoker,managedRoomExecutor:this.managedRoomExecutor,runJournal:this.runJournal,recoveryBarrier:this.recoveryBarrier,prepareManagedRecovery:this.prepareManagedRecovery}}async resumePendingAcpAuthentication(e){const t=[...this.sessions.values()].find(s=>s.agentType===e&&s.adapter.hasPendingAuthenticationRetry?.());return t?.adapter.resumeAfterExternalAuthentication?(await t.adapter.resumeAfterExternalAuthentication(),!0):!1}getUpgradeIdleState(){const e=[];return[...this.sessions.values()].some(t=>t.currentRunId!==null)&&e.push("active-agent-turn"),this.pendingTransfers.size>0&&e.push("file-transfer"),this.activeRequests>0&&e.push("active-daemon-request"),{idle:e.length===0,reasons:e}}prepareForUpgrade(e){this.recoveryBarrier.freezeDispatch();const t=this.runJournal.markPlannedUpgrade(e);return this.runJournal.sync(),{activeRunCount:t}}cancelUpgradePreparation(){this.recoveryBarrier.unfreezeDispatch()}async recoverInterruptedRuns(){this.recoveryPass??=this.recoveryCoordinator.recoverAll(),await this.recoveryPass}getPendingRecoveryCount(){return this.runJournal.loadPendingReconciliation().records.length}revokeProjectFsHandles(){this.projectFsHandleStore.revokeAll()}async commitRecoveryTerminal(e,t){const s=this.sessions.get(e.sessionId);s&&(s.currentRunId=null,s.currentRunPhase=null,s.journalRunId=null,s.heartbeatTimer&&(clearInterval(s.heartbeatTimer),s.heartbeatTimer=null)),this.runTextAcc.delete(`${e.sessionId}:${e.providerRunId??e.runId}`),this.publishSessionActivity(e.sessionId,null);const r=t.result==="interrupted_by_upgrade"?`\u4EFB\u52A1\u56E0\u795E\u5FF5\u5347\u7EA7\u800C\u4E2D\u65AD\uFF08${t.reason}\uFF09\u3002\u539F\u4F1A\u8BDD\u5DF2\u4FDD\u7559\uFF0C\u53EF\u7EE7\u7EED\u53D1\u9001\u65B0\u6D88\u606F\u3002
2
- Task interrupted by the Shennian upgrade (${t.reason}). The session is preserved and ready for a new message.`:`\u795E\u5FF5\u5347\u7EA7\u540E\u672A\u80FD\u6062\u590D\u4EFB\u52A1\uFF08${t.reason}\uFF09\u3002\u539F\u4F1A\u8BDD\u5DF2\u4FDD\u7559\uFF0C\u53EF\u91CD\u8BD5\u6216\u7EE7\u7EED\u53D1\u9001\u65B0\u6D88\u606F\u3002
3
- The task could not be recovered after the Shennian upgrade (${t.reason}). The session is preserved; retry or send a new message.`;ke(e.sessionId,{id:`upgrade-recovery-terminal-${e.runId}`,sessionId:e.sessionId,role:"agent",ts:Date.now(),payload:JSON.stringify({v:1,type:"error",content:r,error:!0,recoveryState:t.result,recoveryReason:t.reason})}),we({sessionId:e.sessionId,agentType:e.agentType,sessionMode:e.sessionMode,workDir:e.workDir,agentSessionId:e.agentSessionId,modelId:e.modelId,status:"failed",attentionId:`upgrade-recovery:${e.runId}`,lastActivityAt:new Date().toISOString()}),this.managerRuntime.noteAgentEvent(e.sessionId,{state:"error",runId:e.providerRunId??e.runId,seq:e.lastObservedEventSeq+1,message:r}),Ie(this.client);const n=e.lastObservedEventSeq+1;this.client.sendAgentEvent({type:"event",event:"agent",payload:t.result==="interrupted_by_upgrade"?{state:"interrupted_by_upgrade",sessionId:e.sessionId,runId:e.providerRunId??e.runId,seq:n,reason:t.reason}:{state:"recovery_failed",sessionId:e.sessionId,runId:e.providerRunId??e.runId,seq:n,reason:t.reason,...t.message?{message:t.message}:{}},seq:n,id:`agent-recovery-terminal-${e.runId}`}),this.chatQueue.noteTerminal(e.sessionId)}setManagedRoomTurnPreparer(e){this.chatQueue.setManagedRoomTurnPreparer(e)}setManagedAgentDirectTurnPreparer(e){this.chatQueue.setManagedAgentDirectTurnPreparer(e)}setManagedRecoveryPreparer(e){this.prepareManagedRecovery=e}async enqueueManagedRoomBatch(e){const t=e.map(s=>{const r=m().find(n=>n.sessionId===s.params.sessionId);return{context:s.context,params:{...s.params,modelId:r?.modelId??s.params.modelId,sessionMode:r?.sessionMode??s.params.sessionMode,managerConfig:r?.managerConfig??s.params.managerConfig,managerDefaultWorkerAgentType:r?.managerDefaultWorkerAgentType??s.params.managerDefaultWorkerAgentType,managerDefaultWorkerModelId:r?.managerDefaultWorkerModelId??s.params.managerDefaultWorkerModelId}}});await this.chatQueue.enqueueManagedRoomBatch(t)}async retireManagedRoomSession(e){this.chatQueue.purgeSession(e),await Re(this.getRuntime(),e)}discardPendingManagedRoomMessages(e){return this.chatQueue.discardPendingManagedRoomMessages(e)}async synchronizeSessionDeletions(){xe();try{await this.synchronizeSessionDeletionsOnce(),Ee()}catch(e){if(e instanceof Fe||e instanceof Oe)throw e;const t=e instanceof Error?e.message:String(e),s=/offline|not connected|disconnect|timed out|timeout|temporar|unavailable|ECONN|ENOTFOUND|EIO|EBUSY/i.test(t)?"transport_unavailable":/invalid (?:deletion sync|session deletion)|deletion revision gap/i.test(t)?"invalid_deletion_sync_response":"deletion_sync_failed";throw console.warn("[session.deletions] retrying failed synchronization",{code:s}),p({code:s,retryable:!0}),e}}async synchronizeSessionDeletionsOnce(){const e=u(),t=De(),s=await this.client.sendReq({type:"req",id:`session-deletions-sync-${h.randomUUID()}`,method:"session.deletions.sync",params:{protocolVersion:g,dataEpoch:f,daemonInstallationId:e,afterRevision:t}});if(!s.ok){const o=s.rejection;throw o?(p({code:o.code,retryable:o.retryable&&o.requiredAction==="retry"}),new Error(o.code)):(p({code:"unstructured_rejection",retryable:!0}),new Error(s.error??"unstructured_rejection"))}const r=s.payload;c(r),He(r,t);let n=r.baselineRevision;n>t&&y(n);for(const o of r.deletions)o.deletionRevision<=n||(await this.applySessionDeletion(o),n=o.deletionRevision,y(n));if(n!==r.latestRevision)throw new Error(`deletion revision gap: applied ${n}, latest ${r.latestRevision}`);if(n===0)return;const d=await this.client.sendReq({type:"req",id:`session-deletions-ack-${h.randomUUID()}`,method:"session.deletions.ack",params:{protocolVersion:g,dataEpoch:f,daemonInstallationId:e,deletionRevision:n}});if(!d.ok)throw new Error(d.error??"session deletion acknowledgement failed")}async applySessionDeletion(e){const t=new Ce({daemonInstallationId:u()});t.recordSession(e);for(const r of e.sources){const n=t.find(r.agentType,r.sourceSessionKey);n&&n.sessionId!==e.sessionId||t.record({...e,...r})}this.runJournal.markSessionAborted(e.sessionId),this.recoveryBarrier.end(e.sessionId);const s=this.sessions.get(e.sessionId);if(s){this.revokeManagedRoomToken(s);try{await s.adapter.stop()}catch{}this.sessions.delete(e.sessionId)}this.historySubscriptions.closeSession(e.sessionId),this.nativeFusion?.handleSessionDeleted(e.sessionId),this.chatQueue.purgeSession(e.sessionId),Me(e.sessionId),be(e.sessionId),Se(e.sessionId),this.runTextAcc.delete(e.sessionId),this.publishSessionActivity(e.sessionId,null),this.nativeCleanupOutbox.enqueue(e),this.nativeCleanupOutbox.kick()}publishSessionActivity(e,t){this.client.sendEvent({type:"event",event:"session.activity",payload:{sessionId:e,activity:t}})}async handleExternalTerminal(e){const t=this.sessions.get(e);t&&(t.journalRunId&&(this.recoveryCoordinator.completeFromProvider(e,t.journalRunId),t.journalRunId=null),this.sessions.delete(e),this.revokeManagedRoomToken(t),t.heartbeatTimer&&clearInterval(t.heartbeatTimer),t.adapter.removeAllListeners(),await t.adapter.stop().catch(()=>{})),this.chatQueue.noteTerminal(e)}async publishManagedActivitySnapshots(){for(const[e,t]of this.sessions.entries()){if(t.heartbeatTimer||!t.currentRunId||!t.adapter.getStatus)continue;const s=await t.adapter.getStatus().catch(()=>null);if(!s?.active||!s.runPhase)continue;const r=new Date().toISOString();this.publishSessionActivity(e,{sessionId:e,runId:s.runId||t.currentRunId,runPhase:s.runPhase,startedAt:new Date(t.lastActiveAt).toISOString(),updatedAt:r,canStop:s.canStop??!0});const n=t.heartbeatSeq++;this.client.sendAgentEvent({type:"event",event:"agent",payload:{state:"heartbeat",sessionId:e,runId:s.runId||t.currentRunId,seq:n,runPhase:s.runPhase,canStop:s.canStop??!0},seq:n,id:`agent-status-${s.runId||t.currentRunId}-${Date.now()}`})}}reloadCustomAgents(){for(const t of k())t.startsWith("custom:")&&I(t);const e=T();for(const[t,s]of Object.entries(e.customAgents??{}))fe(t,s)}async handleReq(e,t={}){const s=this.getRuntime();let r;this.activeRequests++;try{switch(e.method){case"schedule.deliver":if(!this.scheduleHandler)throw new Error("schedules_unavailable");await this.scheduleHandler(e);break;case"chat.send":if(e.params.scheduledDelivery)throw new Error("schedule_source_untrusted");c(e.params),this.recoveryBarrier.isDispatchFrozen()||this.recoveryBarrier.isRecovering(String(e.params.sessionId??""))?r={durableQueueHandoff:await this.chatQueue.handleEnqueue({...e,method:"chat.enqueue"})}:await U(s,e);break;case"chat.enqueue":c(e.params),r={durableQueueHandoff:await this.chatQueue.handleEnqueue(e)};break;case"chat.queue.get":c(e.params),await this.chatQueue.handleGet(e);break;case"chat.queue.edit":c(e.params),await this.chatQueue.handleEdit(e);break;case"chat.queue.delete":c(e.params),await this.chatQueue.handleDelete(e);break;case"chat.queue.promote":c(e.params),await this.chatQueue.handlePromote(e);break;case"chat.abort":c(e.params),await B(s,e);break;case"session.refresh":c(e.params),await W(s,e);break;case"session.history.open":this.historySubscriptions.handleOpen(e);break;case"session.history.sync":this.historySubscriptions.handleSync(e);break;case"session.history.page":this.historySubscriptions.handlePage(e);break;case"session.history.close":this.historySubscriptions.handleClose(e);break;case"session.body.replica.subscribe":this.historySubscriptions.handleReplicaSubscribe(e);break;case"session.body.replica.unsubscribe":this.historySubscriptions.handleReplicaUnsubscribe(e);break;case"session.history":this.client.sendRes({type:"res",id:e.id,ok:!1,error:"upgrade_required"});break;case"session.message.status":J(s,e);break;case"session.tool.detail":await $(s,e);break;case"voice.session.body.window":V(s,e);break;case"session.title.set":await H(s,e);break;case"room.agent.bind-managed":await ve(s,this.managedRoomBinder,e);break;case"room.agent.restart-managed":await ye(s,this.managedRoomBinder,e);break;case"fs.ls":await Y(s,e);break;case"project.fs.open":case"project.fs.list":case"project.fs.read":case"project.fs.close":await ze(this.client,this.projectFsHandleStore,e,this.trustedProjectFsRequestResolver);break;case"fs.read":await K(s,e);break;case"fs.write":await Z(s,e);break;case"fs.export.markdown-pdf":await ae(s,e);break;case"fs.export.markdown-pdf.setup":await ne(s,e);break;case"fs.archive.zip":await re(s,e);break;case"fs.rename":await X(s,e);break;case"fs.transfer.start":await te(s,e);break;case"fs.transfer.chunk":await ee(s,e);break;case"fs.transfer.finish":await se(s,e);break;case"fs.transfer.abort":await q(s,e);break;case"region.probe":await ie(s,e);break;case"region.switch":await oe(s,e);break;case"upgrade.start":await P(this.client,e.id,e.params.version,{currentVersion:this.cliVersion,confirmedMajor:e.params.confirmedMajor===!0,prepareForUpgrade:n=>this.prepareForUpgrade(n),cancelUpgradePreparation:()=>this.cancelUpgradePreparation(),shutdownAfterHandoff:()=>{setTimeout(()=>process.kill(process.pid,"SIGTERM"),100).unref?.()}});break;case"upgrade.status":await C(this.client,e.id,{currentVersion:this.cliVersion});break;case"upgrade.set-policy":await ce(s,e);break;case"machine.remote-access.pause":await L(this.client,this.remoteAccessPauseController,e);break;case"agents.refresh":await M(s,e);break;case"agent.permission.respond":await We(s,e);break;case"agent.auth.start":case"agent.auth.logout":case"agent.session-options.get":case"agent.external-sessions.list":case"agent.external-sessions.import":case"agent.external-sessions.close":case"agent.external-sessions.delete":await $e(s,e);break;case"models.refresh":await D(s,e);break;case"agent.config.get":await _(s,e);break;case"agent.config.upsert":await j(s,e);break;case"agent.config.clear":await O(s,e);break;case"agent.config.test":await Q(s,e);break;case"agent.workspace.validate":await N(s,e);break;case"agent.capabilities.list":await F(s,e);break;case"skill.list":case"skill.install":case"skill.doctor":case"skill.setup":case"skill.use":this.client.sendRes({type:"res",id:e.id,ok:!1,error:"feature_retired"});break;case"session.wechat-rpa.channel.get":case"session.wechat-rpa.channel.upsert":case"session.wechat-rpa.channel.sync":case"manager.wechat-rpa.channel.get":case"manager.wechat-rpa.channel.upsert":case"manager.wechat-rpa.channel.sync":this.client.sendRes({type:"res",id:e.id,ok:!1,error:"feature_retired"});break;default:this.client.sendRes({type:"res",id:e.id,ok:!1,error:`Unknown method: ${e.method}`})}}catch(n){if(t.propagateErrors)throw n;this.client.sendRes({type:"res",id:e.id,ok:!1,error:n instanceof Error?n.message:String(n)})}finally{this.activeRequests--}return r}evictIdleSessions(){if(this.sessions.size<Le)return;let e=null;for(const[t,s]of this.sessions)(!e||s.lastActiveAt<e.session.lastActiveAt)&&(e={key:t,session:s});e&&(this.revokeManagedRoomToken(e.session),e.session.heartbeatTimer&&(clearInterval(e.session.heartbeatTimer),e.session.heartbeatTimer=null),e.session.adapter.removeAllListeners(),e.session.adapter.stop().catch(()=>{}),this.sessions.delete(e.key))}resolvePath(e){return ge(e)}resolveAuthorizedPath(e,t){return me(e,he(t))}async resolveAuthorizedPathAsync(e,t){return pe(e,await ue(t))}async cleanup(){this.chatQueue.stop(),this.projectFsHandleStore.revokeAll(),this.acpDiscoveryWatcher.stop(),this.nativeFusion?.setExternalTerminalHandler?.(null),this.nativeCleanupOutbox.stop(),this.historySubscriptions.closeAll(),this.activityProbeTimer&&(clearInterval(this.activityProbeTimer),this.activityProbeTimer=null),this.attachmentCleanupTimer&&(clearInterval(this.attachmentCleanupTimer),this.attachmentCleanupTimer=null);for(const[,e]of this.sessions)this.revokeManagedRoomToken(e),e.heartbeatTimer&&(clearInterval(e.heartbeatTimer),e.heartbeatTimer=null),e.adapter.removeAllListeners(),await e.adapter.stop().catch(()=>{});this.sessions.clear(),this.runTextAcc.clear(),G(this.getRuntime()),await this.managerRuntime.stop(),v(null)}revokeManagedRoomToken(e){const t=e.managedRoomContextToken;e.managedRoomContextToken=null,t&&this.managedRoomContextRevoker?.(t)}}export{Gs as SessionManager,Xs as resolveSessionWorkDir};
1
+ import{SESSION_SYNC_PROTOCOL_VERSION as g,SESSION_SYNC_DATA_EPOCH as f}from"@shennian/wire";import h from"node:crypto";import{getRegisteredAgents as I,unregisterAgent as A}from"../agents/adapter.js";import{resetCommandSpecCache as T}from"../agents/command-spec.js";import{loadConfig as P}from"../config/index.js";import{handleUpgradeStart as C,handleUpgradeStatus as M}from"../commands/upgrade.js";import{handleAgentsRefresh as D,handleModelsRefresh as x,refreshMachineAgents as E}from"./handlers/agents.js";import{AcpDiscoveryWatcher as F}from"../agents/acp/watcher.js";import{handleAgentCapabilitiesList as O}from"./handlers/agent-capabilities.js";import{handleAgentConfigClear as Q,handleAgentConfigGet as _,handleAgentConfigTest as j,handleAgentConfigUpsert as B}from"./handlers/agent-config.js";import{handleChatAbort as U,handleChatSend as z}from"./handlers/chat.js";import{recoverJournaledRun as W}from"./handlers/chat-recovery.js";import{handleSessionRefresh as H}from"./handlers/session-refresh.js";import{handleSessionToolDetail as $}from"./handlers/tool-detail.js";import{handleSessionTitleSet as J}from"./handlers/title.js";import{handleSessionMessageStatus as N}from"./handlers/message-status.js";import{handleAgentWorkspaceValidation as V}from"./handlers/agent-workspace-validation.js";import{handleVoiceSessionBodyWindow as L}from"./handlers/voice-window.js";import{handleRemoteAccessPause as G}from"./handlers/remote-access.js";import{cleanupPendingTransfers as Y,handleFsLs as K,handleFsRead as X,handleFsRename as Z,handleFsWrite as q,handleFsTransferAbort as ee,handleFsTransferChunk as te,handleFsTransferFinish as se,handleFsTransferStart as ae,handleFsExportMarkdownPdf as ne,handleFsExportMarkdownPdfSetup as re,handleFsArchiveZip as ie}from"./handlers/fs.js";import{handleRegionProbe as oe,handleRegionSwitch as ce,handleUpgradeSetPolicy as le}from"./handlers/control.js";import{ManagerRuntimeService as de,setManagerRuntimeService as v}from"../manager/runtime.js";import{ChatQueueManager as he}from"./queue.js";import{createAuthorizedFsRoot as ue,createAuthorizedFsRootAsync as me,resolveAuthorizedPath as pe,resolveAuthorizedPathAsync as ge,resolveSessionWorkDir as fe}from"../fs/boundary.js";import"../agents/claude.js";import"../agents/codex.js";import"../agents/workbuddy.js";import"../agents/gemini.js";import"../agents/cursor.js";import"../agents/opencode.js";import"../agents/pi-coding-agent.js";import"../agents/manager.js";import{registerCustomAgent as ve}from"../agents/custom.js";import{handleManagedRoomBind as ye,handleManagedRoomRestart as Re,discardPreBindingSession as be}from"./handlers/room-managed.js";import{getDaemonInstallationId as u,listSessionRecords as m,purgeCanonicalSession as Se,purgeSessionRecord as we,recordSession as ke,upsertMessage as Ie}from"./store.js";import{requestSessionIndexPublish as Ae}from"./index-publisher.js";import{reportUpgradeAggregateMetric as Te}from"../log-reporter.js";import{PersonalHistorySubscriptions as Pe}from"./history-subscriptions.js";import{sweepAttachmentTransfers as Ce}from"./attachment-transfer-store.js";import{NativeSourceDeletionStore as Me}from"../native-fusion/native-source-deletions.js";import{purgeToolDetails as De}from"./tool-detail-store.js";import{readDeletionSyncRevision as xe,writeDeletionSyncRevision as y}from"./deletion-sync-state.js";import{assertDeletionSyncRunnable as Ee,clearDeletionSyncFailure as Fe,DeletionSyncRetryWaitError as Oe,DeletionSyncTerminalError as Qe,recordDeletionSyncFailure as p}from"./deletion-sync-state.js";import{NativeSessionCleanupOutbox as _e}from"./native-cleanup-outbox.js";import{assertPersonalSyncIdentity as c}from"../personal-sync/epoch.js";import{ActiveRunJournal as je}from"./run-recovery/journal.js";import{RunRecoveryBarrier as Be}from"./run-recovery/barrier.js";import{RunRecoveryCoordinator as Ue}from"./run-recovery/coordinator.js";import{ProjectFsHandleStore as ze}from"./project-fs-store.js";import{handleProjectFsRequest as We}from"./handlers/project-fs.js";import{handleAgentPermissionResponse as He,handleAcpAgentRequest as $e}from"./handlers/agent-control.js";import{validateDeletionSyncResponse as Je}from"./deletion-sync-validation.js";import{dispatchManagedChat as Ne,dispatchQueuedChat as Ve,interruptQueuedChat as Le}from"./queued-chat-dispatch.js";const Ge=50;import{resolveSessionWorkDir as qt}from"../fs/boundary.js";function Ye(){return"\u795E\u5FF5\u5347\u7EA7\u4E2D\u65AD\u4E86\u672C\u6B21\u56DE\u590D\u3002\u539F\u4F1A\u8BDD\u5DF2\u4FDD\u7559\uFF0C\u53EF\u91CD\u8BD5\u6216\u7EE7\u7EED\u53D1\u9001\u65B0\u6D88\u606F\u3002"}class Kt{client;nativeFusion;cliVersion;upgradePolicyController;managedRoomBinder;remoteAccessPauseController;managedRoomContextRevoker;managedRoomExecutor;trustedProjectFsRequestResolver;getAgentSnapshot;scheduleHandler;getScheduleQueue(){return this.chatQueue}sessions=new Map;processedReqIds=new Set;runTextAcc=new Map;pendingTransfers=new Map;managerRuntime;chatQueue;historySubscriptions;nativeCleanupOutbox;activityProbeTimer=null;attachmentCleanupTimer=null;activeRequests=0;acpDiscoveryWatcher;runJournal;recoveryBarrier=new Be;recoveryCoordinator;recoveryPass=null;prepareManagedRecovery;projectFsHandleStore=new ze;constructor(e,s=null,t,r=null,n=null,d=null,o=null,R=null,b=null,S=null,w=null){this.client=e,this.nativeFusion=s,this.cliVersion=t,this.upgradePolicyController=r,this.managedRoomBinder=n,this.remoteAccessPauseController=d,this.managedRoomContextRevoker=o,this.managedRoomExecutor=R,this.trustedProjectFsRequestResolver=S,this.getAgentSnapshot=w,this.runJournal=b??new je(u());for(const a of this.runJournal.loadPendingReconciliation().records)this.recoveryBarrier.begin(a.sessionId);this.recoveryCoordinator=new Ue({journal:this.runJournal,barrier:this.recoveryBarrier,recover:(a,i)=>W(this.getRuntime(),a,i),commitTerminal:(a,i)=>this.commitRecoveryTerminal(a,i),isCanonicalTerminal:a=>{const i=m().find(k=>k.sessionId===a.sessionId),l=`terminal:${h.createHash("sha256").update(`${a.runId}\0`).digest("hex")}`;return i?.lastAttentionId===l},onOutcome:a=>Te({kind:"recovery",adapterType:a.adapterType,result:a.result,stage:a.result==="continued"?"continue":a.result==="reattached"?"reattach":a.result==="resumed"?"resume":a.result==="terminal_reconciled"?"inspect":"terminal",durationMs:a.durationMs,retries:Math.max(0,a.attempts-1),...a.reason?{errorCode:a.reason}:{}})}),this.managerRuntime=new de({getRuntime:()=>this.getRuntime(),getAgentSnapshot:this.getAgentSnapshot??void 0,dispatchReq:a=>this.handleReq(a,{propagateErrors:!0})}),this.chatQueue=new he({getRuntime:()=>this.getRuntime(),dispatchReq:a=>this.handleReq(a),dispatchManagedReq:(a,i)=>Ne(this.getRuntime(),a,i),dispatchQueuedReq:(a,i)=>Ve(this.getRuntime(),a,i),interruptQueuedSession:a=>Le(this.getRuntime(),a)}),this.historySubscriptions=new Pe(this.client),this.nativeCleanupOutbox=new _e({logger:console.info}),this.acpDiscoveryWatcher=new F(async()=>{T(),await E(this.getRuntime())}),this.nativeFusion?.setExternalTerminalHandler?.(a=>{this.handleExternalTerminal(a)}),this.nativeFusion?.setExternalClaimHandler?.((a,i)=>{this.chatQueue.noteExternalClaim(a,i)}),this.nativeCleanupOutbox.start(),v(this.managerRuntime),this.managerRuntime.start(),this.reloadCustomAgents();try{this.acpDiscoveryWatcher.start()}catch(a){console.warn(`[acp.discovery] watcher unavailable: ${a instanceof Error?a.message:String(a)}`)}this.activityProbeTimer=setInterval(()=>{this.publishManagedActivitySnapshots().catch(a=>{console.error("[session.activity] managed probe failed",a)})},15e3),this.activityProbeTimer.unref?.(),this.attachmentCleanupTimer=setInterval(()=>{const a=new Set([...m().map(i=>i.workDir),...Array.from(this.pendingTransfers.values()).map(i=>i.rootPath)]);for(const i of a)try{Ce(this.resolvePath(i))}catch(l){console.error("[attachment.cleanup] failed",l instanceof Error?l.message:String(l))}},6e4),this.attachmentCleanupTimer.unref?.()}executeManagedManagerAction(e){return this.managerRuntime.executeScopedWorkerAction(e.managerSessionId,e.action,e.params,e.roomOrigin)}getRuntime(){return{client:this.client,pendingTransfers:this.pendingTransfers,processedReqIds:this.processedReqIds,reloadCustomAgents:()=>this.reloadCustomAgents(),resolvePath:e=>this.resolvePath(e),resolveAuthorizedPath:(e,s)=>this.resolveAuthorizedPath(e,s),resolveAuthorizedPathAsync:(e,s)=>this.resolveAuthorizedPathAsync(e,s),runTextAcc:this.runTextAcc,sessions:this.sessions,evictIdleSessions:()=>this.evictIdleSessions(),nativeFusion:this.nativeFusion,managerRuntime:this.managerRuntime,chatQueue:this.chatQueue,activityPublisher:{publish:(e,s)=>this.publishSessionActivity(e,s)},upgradePolicyController:this.upgradePolicyController,revokeManagedRoomContextToken:this.managedRoomContextRevoker,managedRoomExecutor:this.managedRoomExecutor,runJournal:this.runJournal,recoveryBarrier:this.recoveryBarrier,prepareManagedRecovery:this.prepareManagedRecovery}}async resumePendingAcpAuthentication(e){const s=[...this.sessions.values()].find(t=>t.agentType===e&&t.adapter.hasPendingAuthenticationRetry?.());return s?.adapter.resumeAfterExternalAuthentication?(await s.adapter.resumeAfterExternalAuthentication(),!0):!1}getUpgradeIdleState(){const e=[];return[...this.sessions.values()].some(s=>s.currentRunId!==null)&&e.push("active-agent-turn"),this.pendingTransfers.size>0&&e.push("file-transfer"),this.activeRequests>0&&e.push("active-daemon-request"),{idle:e.length===0,reasons:e}}prepareForUpgrade(e){this.recoveryBarrier.freezeDispatch();const s=this.runJournal.markPlannedUpgrade(e);return this.runJournal.sync(),{activeRunCount:s}}cancelUpgradePreparation(){this.recoveryBarrier.unfreezeDispatch()}async recoverInterruptedRuns(){this.recoveryPass??=this.recoveryCoordinator.recoverAll(),await this.recoveryPass}getPendingRecoveryCount(){return this.runJournal.loadPendingReconciliation().records.length}revokeProjectFsHandles(){this.projectFsHandleStore.revokeAll()}async commitRecoveryTerminal(e,s){const t=this.sessions.get(e.sessionId);t&&(t.currentRunId=null,t.currentRunPhase=null,t.journalRunId=null,t.heartbeatTimer&&(clearInterval(t.heartbeatTimer),t.heartbeatTimer=null)),this.runTextAcc.delete(`${e.sessionId}:${e.providerRunId??e.runId}`),this.publishSessionActivity(e.sessionId,null);const r=Ye();Ie(e.sessionId,{id:`upgrade-recovery-terminal-${e.runId}`,sessionId:e.sessionId,role:"agent",ts:Date.now(),payload:JSON.stringify({v:1,type:"error",content:r,error:!0,recoveryState:s.result,recoveryReason:s.reason})}),ke({sessionId:e.sessionId,agentType:e.agentType,sessionMode:e.sessionMode,workDir:e.workDir,agentSessionId:e.agentSessionId,modelId:e.modelId,status:"failed",attentionId:`upgrade-recovery:${e.runId}`,lastActivityAt:new Date().toISOString()}),this.managerRuntime.noteAgentEvent(e.sessionId,{state:"error",runId:e.providerRunId??e.runId,seq:e.lastObservedEventSeq+1,message:r}),Ae(this.client);const n=e.lastObservedEventSeq+1;this.client.sendAgentEvent({type:"event",event:"agent",payload:s.result==="interrupted_by_upgrade"?{state:"interrupted_by_upgrade",sessionId:e.sessionId,runId:e.providerRunId??e.runId,seq:n,reason:s.reason}:{state:"recovery_failed",sessionId:e.sessionId,runId:e.providerRunId??e.runId,seq:n,reason:s.reason,...s.message?{message:s.message}:{}},seq:n,id:`agent-recovery-terminal-${e.runId}`}),this.chatQueue.noteTerminal(e.sessionId)}setManagedRoomTurnPreparer(e){this.chatQueue.setManagedRoomTurnPreparer(e)}setManagedAgentDirectTurnPreparer(e){this.chatQueue.setManagedAgentDirectTurnPreparer(e)}setManagedRecoveryPreparer(e){this.prepareManagedRecovery=e}async enqueueManagedRoomBatch(e){const s=e.map(t=>{const r=m().find(n=>n.sessionId===t.params.sessionId);return{context:t.context,params:{...t.params,modelId:r?.modelId??t.params.modelId,sessionMode:r?.sessionMode??t.params.sessionMode,managerConfig:r?.managerConfig??t.params.managerConfig,managerDefaultWorkerAgentType:r?.managerDefaultWorkerAgentType??t.params.managerDefaultWorkerAgentType,managerDefaultWorkerModelId:r?.managerDefaultWorkerModelId??t.params.managerDefaultWorkerModelId}}});await this.chatQueue.enqueueManagedRoomBatch(s)}async retireManagedRoomSession(e){this.chatQueue.purgeSession(e),await be(this.getRuntime(),e)}discardPendingManagedRoomMessages(e){return this.chatQueue.discardPendingManagedRoomMessages(e)}async synchronizeSessionDeletions(){Ee();try{await this.synchronizeSessionDeletionsOnce(),Fe()}catch(e){if(e instanceof Oe||e instanceof Qe)throw e;const s=e instanceof Error?e.message:String(e),t=/offline|not connected|disconnect|timed out|timeout|temporar|unavailable|ECONN|ENOTFOUND|EIO|EBUSY/i.test(s)?"transport_unavailable":/invalid (?:deletion sync|session deletion)|deletion revision gap/i.test(s)?"invalid_deletion_sync_response":"deletion_sync_failed";throw console.warn("[session.deletions] retrying failed synchronization",{code:t}),p({code:t,retryable:!0}),e}}async synchronizeSessionDeletionsOnce(){const e=u(),s=xe(),t=await this.client.sendReq({type:"req",id:`session-deletions-sync-${h.randomUUID()}`,method:"session.deletions.sync",params:{protocolVersion:g,dataEpoch:f,daemonInstallationId:e,afterRevision:s}});if(!t.ok){const o=t.rejection;throw o?(p({code:o.code,retryable:o.retryable&&o.requiredAction==="retry"}),new Error(o.code)):(p({code:"unstructured_rejection",retryable:!0}),new Error(t.error??"unstructured_rejection"))}const r=t.payload;c(r),Je(r,s);let n=r.baselineRevision;n>s&&y(n);for(const o of r.deletions)o.deletionRevision<=n||(await this.applySessionDeletion(o),n=o.deletionRevision,y(n));if(n!==r.latestRevision)throw new Error(`deletion revision gap: applied ${n}, latest ${r.latestRevision}`);if(n===0)return;const d=await this.client.sendReq({type:"req",id:`session-deletions-ack-${h.randomUUID()}`,method:"session.deletions.ack",params:{protocolVersion:g,dataEpoch:f,daemonInstallationId:e,deletionRevision:n}});if(!d.ok)throw new Error(d.error??"session deletion acknowledgement failed")}async applySessionDeletion(e){const s=new Me({daemonInstallationId:u()});s.recordSession(e);for(const r of e.sources){const n=s.find(r.agentType,r.sourceSessionKey);n&&n.sessionId!==e.sessionId||s.record({...e,...r})}this.runJournal.markSessionAborted(e.sessionId),this.recoveryBarrier.end(e.sessionId);const t=this.sessions.get(e.sessionId);if(t){this.revokeManagedRoomToken(t);try{await t.adapter.stop()}catch{}this.sessions.delete(e.sessionId)}this.historySubscriptions.closeSession(e.sessionId),this.nativeFusion?.handleSessionDeleted(e.sessionId),this.chatQueue.purgeSession(e.sessionId),De(e.sessionId),Se(e.sessionId),we(e.sessionId),this.runTextAcc.delete(e.sessionId),this.publishSessionActivity(e.sessionId,null),this.nativeCleanupOutbox.enqueue(e),this.nativeCleanupOutbox.kick()}publishSessionActivity(e,s){this.client.sendEvent({type:"event",event:"session.activity",payload:{sessionId:e,activity:s}})}async handleExternalTerminal(e){const s=this.sessions.get(e);s&&(s.journalRunId&&(this.recoveryCoordinator.completeFromProvider(e,s.journalRunId),s.journalRunId=null),this.sessions.delete(e),this.revokeManagedRoomToken(s),s.heartbeatTimer&&clearInterval(s.heartbeatTimer),s.adapter.removeAllListeners(),await s.adapter.stop().catch(()=>{})),this.chatQueue.noteTerminal(e)}async publishManagedActivitySnapshots(){for(const[e,s]of this.sessions.entries()){if(s.heartbeatTimer||!s.currentRunId||!s.adapter.getStatus)continue;const t=await s.adapter.getStatus().catch(()=>null);if(!t?.active||!t.runPhase)continue;const r=new Date().toISOString();this.publishSessionActivity(e,{sessionId:e,runId:t.runId||s.currentRunId,runPhase:t.runPhase,startedAt:new Date(s.lastActiveAt).toISOString(),updatedAt:r,canStop:t.canStop??!0});const n=s.heartbeatSeq++;this.client.sendAgentEvent({type:"event",event:"agent",payload:{state:"heartbeat",sessionId:e,runId:t.runId||s.currentRunId,seq:n,runPhase:t.runPhase,canStop:t.canStop??!0},seq:n,id:`agent-status-${t.runId||s.currentRunId}-${Date.now()}`})}}reloadCustomAgents(){for(const s of I())s.startsWith("custom:")&&A(s);const e=P();for(const[s,t]of Object.entries(e.customAgents??{}))ve(s,t)}async handleReq(e,s={}){const t=this.getRuntime();let r;this.activeRequests++;try{switch(e.method){case"schedule.deliver":if(!this.scheduleHandler)throw new Error("schedules_unavailable");await this.scheduleHandler(e);break;case"chat.send":if(e.params.scheduledDelivery)throw new Error("schedule_source_untrusted");c(e.params),this.recoveryBarrier.isDispatchFrozen()||this.recoveryBarrier.isRecovering(String(e.params.sessionId??""))?r={durableQueueHandoff:await this.chatQueue.handleEnqueue({...e,method:"chat.enqueue"})}:await z(t,e);break;case"chat.enqueue":c(e.params),r={durableQueueHandoff:await this.chatQueue.handleEnqueue(e)};break;case"chat.queue.get":c(e.params),await this.chatQueue.handleGet(e);break;case"chat.queue.edit":c(e.params),await this.chatQueue.handleEdit(e);break;case"chat.queue.delete":c(e.params),await this.chatQueue.handleDelete(e);break;case"chat.queue.promote":c(e.params),await this.chatQueue.handlePromote(e);break;case"chat.abort":c(e.params),await U(t,e);break;case"session.refresh":c(e.params),await H(t,e);break;case"session.history.open":this.historySubscriptions.handleOpen(e);break;case"session.history.sync":this.historySubscriptions.handleSync(e);break;case"session.history.page":this.historySubscriptions.handlePage(e);break;case"session.history.close":this.historySubscriptions.handleClose(e);break;case"session.body.replica.subscribe":this.historySubscriptions.handleReplicaSubscribe(e);break;case"session.body.replica.unsubscribe":this.historySubscriptions.handleReplicaUnsubscribe(e);break;case"session.history":this.client.sendRes({type:"res",id:e.id,ok:!1,error:"upgrade_required"});break;case"session.message.status":N(t,e);break;case"session.tool.detail":await $(t,e);break;case"voice.session.body.window":L(t,e);break;case"session.title.set":await J(t,e);break;case"room.agent.bind-managed":await ye(t,this.managedRoomBinder,e);break;case"room.agent.restart-managed":await Re(t,this.managedRoomBinder,e);break;case"fs.ls":await K(t,e);break;case"project.fs.open":case"project.fs.list":case"project.fs.read":case"project.fs.close":await We(this.client,this.projectFsHandleStore,e,this.trustedProjectFsRequestResolver);break;case"fs.read":await X(t,e);break;case"fs.write":await q(t,e);break;case"fs.export.markdown-pdf":await ne(t,e);break;case"fs.export.markdown-pdf.setup":await re(t,e);break;case"fs.archive.zip":await ie(t,e);break;case"fs.rename":await Z(t,e);break;case"fs.transfer.start":await ae(t,e);break;case"fs.transfer.chunk":await te(t,e);break;case"fs.transfer.finish":await se(t,e);break;case"fs.transfer.abort":await ee(t,e);break;case"region.probe":await oe(t,e);break;case"region.switch":await ce(t,e);break;case"upgrade.start":await C(this.client,e.id,e.params.version,{currentVersion:this.cliVersion,confirmedMajor:e.params.confirmedMajor===!0,prepareForUpgrade:n=>this.prepareForUpgrade(n),cancelUpgradePreparation:()=>this.cancelUpgradePreparation(),shutdownAfterHandoff:()=>{setTimeout(()=>process.kill(process.pid,"SIGTERM"),100).unref?.()}});break;case"upgrade.status":await M(this.client,e.id,{currentVersion:this.cliVersion});break;case"upgrade.set-policy":await le(t,e);break;case"machine.remote-access.pause":await G(this.client,this.remoteAccessPauseController,e);break;case"agents.refresh":await D(t,e);break;case"agent.permission.respond":await He(t,e);break;case"agent.auth.start":case"agent.auth.logout":case"agent.session-options.get":case"agent.external-sessions.list":case"agent.external-sessions.import":case"agent.external-sessions.close":case"agent.external-sessions.delete":await $e(t,e);break;case"models.refresh":await x(t,e);break;case"agent.config.get":await _(t,e);break;case"agent.config.upsert":await B(t,e);break;case"agent.config.clear":await Q(t,e);break;case"agent.config.test":await j(t,e);break;case"agent.workspace.validate":await V(t,e);break;case"agent.capabilities.list":await O(t,e);break;case"skill.list":case"skill.install":case"skill.doctor":case"skill.setup":case"skill.use":this.client.sendRes({type:"res",id:e.id,ok:!1,error:"feature_retired"});break;case"session.wechat-rpa.channel.get":case"session.wechat-rpa.channel.upsert":case"session.wechat-rpa.channel.sync":case"manager.wechat-rpa.channel.get":case"manager.wechat-rpa.channel.upsert":case"manager.wechat-rpa.channel.sync":this.client.sendRes({type:"res",id:e.id,ok:!1,error:"feature_retired"});break;default:this.client.sendRes({type:"res",id:e.id,ok:!1,error:`Unknown method: ${e.method}`})}}catch(n){if(s.propagateErrors)throw n;this.client.sendRes({type:"res",id:e.id,ok:!1,error:n instanceof Error?n.message:String(n)})}finally{this.activeRequests--}return r}evictIdleSessions(){if(this.sessions.size<Ge)return;let e=null;for(const[s,t]of this.sessions)(!e||t.lastActiveAt<e.session.lastActiveAt)&&(e={key:s,session:t});e&&(this.revokeManagedRoomToken(e.session),e.session.heartbeatTimer&&(clearInterval(e.session.heartbeatTimer),e.session.heartbeatTimer=null),e.session.adapter.removeAllListeners(),e.session.adapter.stop().catch(()=>{}),this.sessions.delete(e.key))}resolvePath(e){return fe(e)}resolveAuthorizedPath(e,s){return pe(e,ue(s))}async resolveAuthorizedPathAsync(e,s){return ge(e,await me(s))}async cleanup(){this.chatQueue.stop(),this.projectFsHandleStore.revokeAll(),this.acpDiscoveryWatcher.stop(),this.nativeFusion?.setExternalTerminalHandler?.(null),this.nativeCleanupOutbox.stop(),this.historySubscriptions.closeAll(),this.activityProbeTimer&&(clearInterval(this.activityProbeTimer),this.activityProbeTimer=null),this.attachmentCleanupTimer&&(clearInterval(this.attachmentCleanupTimer),this.attachmentCleanupTimer=null);for(const[,e]of this.sessions)this.revokeManagedRoomToken(e),e.heartbeatTimer&&(clearInterval(e.heartbeatTimer),e.heartbeatTimer=null),e.adapter.removeAllListeners(),await e.adapter.stop().catch(()=>{});this.sessions.clear(),this.runTextAcc.clear(),Y(this.getRuntime()),await this.managerRuntime.stop(),v(null)}revokeManagedRoomToken(e){const s=e.managedRoomContextToken;e.managedRoomContextToken=null,s&&this.managedRoomContextRevoker?.(s)}}export{Kt as SessionManager,qt as resolveSessionWorkDir,Ye as upgradeRecoveryVisibleMessage};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shennian",
3
- "version": "0.4.75",
3
+ "version": "0.4.77",
4
4
  "description": "Shennian — AI Agent Control Plane CLI",
5
5
  "type": "module",
6
6
  "bin": {