@cydm/pie 3.0.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/builtin/extensions/ask-user/index.js +8 -4
- package/dist/builtin/extensions/cron/index.js +130 -21
- package/dist/builtin/extensions/im-forward/README.md +20 -5
- package/dist/builtin/extensions/im-forward/index.js +415 -142
- package/dist/builtin/extensions/memory/index.js +71 -20
- package/dist/builtin/extensions/pie-ipc/README.md +2 -2
- package/dist/builtin/extensions/pie-ipc/index.js +269 -30
- package/dist/builtin/extensions/pie-proc/README.md +93 -0
- package/dist/builtin/extensions/pie-proc/index.js +630 -0
- package/dist/builtin/extensions/pie-proc/package.json +11 -0
- package/dist/builtin/extensions/plan-mode/index.js +4 -4
- package/dist/builtin/extensions/subagent/index.js +5 -5
- package/dist/builtin/extensions/todo/index.js +4 -4
- package/dist/builtin/skills/tui-use/tui-use-daemon.cjs +49 -0
- package/dist/chunks/{chunk-7FEPQHYX.js → chunk-FDLLNPIE.js} +143 -53
- package/dist/chunks/{chunk-3BC5KGDQ.js → chunk-JQRZLVPY.js} +12 -1
- package/dist/chunks/{chunk-GH4VW2OT.js → chunk-NCP264ZE.js} +8 -2
- package/dist/chunks/{chunk-YRR6C43W.js → chunk-QX6WS73C.js} +1 -1
- package/dist/chunks/{chunk-OEQYEYB3.js → chunk-TDKNKNPL.js} +5 -5
- package/dist/chunks/{chunk-BUDXV3TR.js → chunk-UOZHQ73X.js} +3 -3
- package/dist/chunks/{chunk-3FRKOFSP.js → chunk-WK4N5HT5.js} +32 -22
- package/dist/chunks/chunk-X6LBWHK3.js +37 -0
- package/dist/chunks/concierge-cli-PIOGXKDR.js +939 -0
- package/dist/chunks/{model-registry-LI5THXV4.js → model-registry-YA6U366L.js} +2 -2
- package/dist/chunks/{src-AE3OCAE6.js → src-AB7PLORG.js} +6 -4
- package/dist/chunks/{src-YG5M2H44.js → src-ODLT57PQ.js} +2 -2
- package/dist/chunks/{src-66XSMIAM.js → src-XSLD2IQ2.js} +1 -1
- package/dist/chunks/{src-RVTDUSPJ.js → src-ZZZO4HSR.js} +3 -3
- package/dist/chunks/{test-stream-JBWXKOQA.js → test-stream-GIUD5JWS.js} +1 -1
- package/dist/cli.js +962 -174
- package/package.json +105 -105
|
@@ -0,0 +1,939 @@
|
|
|
1
|
+
import { createRequire as __createRequire } from "node:module"; const require = __createRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
readProcessCommandLine
|
|
4
|
+
} from "./chunk-X6LBWHK3.js";
|
|
5
|
+
import "./chunk-LZQXQOPJ.js";
|
|
6
|
+
|
|
7
|
+
// src/commands/concierge-cli.ts
|
|
8
|
+
import { spawn, execFileSync } from "node:child_process";
|
|
9
|
+
import { createRequire } from "node:module";
|
|
10
|
+
import * as fs from "node:fs";
|
|
11
|
+
import * as os from "node:os";
|
|
12
|
+
import * as path from "node:path";
|
|
13
|
+
var require_ = createRequire(import.meta.url);
|
|
14
|
+
var SEED_AGENTS_MD = `# Pie Concierge\uFF08\u7BA1\u5BB6\uFF09
|
|
15
|
+
|
|
16
|
+
\u4F60\u662F\u7528\u6237\u7684\u5E38\u9A7B\u7BA1\u5BB6 pie\uFF1A\u7ED1\u5B9A IM\uFF08\u5E73\u53F0\u65E0\u5173\uFF09\uFF0C\u65E0\u4EBA\u503C\u5B88\uFF0C\u901A\u8FC7 IM \u4E0E\u7528\u6237\u5BF9\u8BDD\u3002
|
|
17
|
+
|
|
18
|
+
## \u89D2\u8272\u51C6\u5219
|
|
19
|
+
|
|
20
|
+
- \u4F60\u662F\u7528\u6237\u5728 IM \u4E0A\u7684\u552F\u4E00\u8054\u7EDC\u70B9\u3002\u9ED8\u8BA4\u6D3E\u6D3B\uFF1A\u7F16\u7801\u3001\u8C03\u67E5\u3001\u89C4\u5212\u3001\u590D\u73B0\u3001\u5BA1\u8BA1\u7B49\u4E00\u5207\u9879\u76EE\u5DE5\u4F5C\u90FD\u4EA4\u7ED9 worker \u53BB\u505A\uFF0C\u4F60\u8D1F\u8D23\u5206\u6D3E\u3001\u76D1\u7763\u3001\u6C47\u603B\u8F6C\u8FBE\u2014\u2014\u4E0D\u8981\u81EA\u5DF1\u52A8\u624B\u505A\u9879\u76EE\u5DE5\u4F5C\uFF0C\u4F60\u7684\u4E0A\u4E0B\u6587\u662F\u7A00\u7F3A\u8D44\u6E90\u3002
|
|
21
|
+
- \u53EA\u6709\u8F7B\u91CF\u7684\u64CD\u4F5C\u6027\u5C0F\u4E8B\u624D\u76F4\u63A5\u505A\uFF1A\u4E00\u6B21\u6027\u67E5\u8BE2\u3001\u8BFB\u4E2A\u6587\u4EF6\u3001\u8DD1\u4E00\u6761\u53EA\u8BFB\u547D\u4EE4\u8FD9\u7C7B\u201C\u4E0D\u6539\u52A8\u9879\u76EE\u72B6\u6001\u3001\u4E0D\u4EA7\u751F\u957F\u8F93\u51FA\u201D\u7684\u4E8B\uFF1B\u4E14\u4F18\u5148\u8D70\u6700\u7B80\u5355\u7684\u7AEF\u5230\u7AEF\u8DEF\u5F84\uFF0C\u4E0D\u4E3A\u5C0F\u4E8B\u642D\u6D41\u7A0B\u3002\u62FF\u4E0D\u51C6\u5C31\u6D3E\u6D3B\u3002
|
|
22
|
+
- \u6D3E\u6D3B\u4F18\u5148\u7EA7\uFF1A\u684C\u9762\u5DF2\u5F00\u7684\u7A7A\u95F2 pie\uFF08ipc_list_peers \u89C1 idle\uFF09\u2192 \u65B0\u5F00 worker\uFF08pie_proc spawn\uFF09\u3002
|
|
23
|
+
- \u6D3E\u6D3B\u7528 ipc_send_notify\uFF08\u9ED8\u8BA4\u8BA2\u9605\u5B8C\u6210\uFF09\uFF1B\u7ED3\u679C\u56DE\u6D41\u540E\u6458\u8981\u8F6C\u53D1\u7ED9\u7528\u6237\uFF0C\u5F15\u7528\u4EA7\u7269\u6587\u4EF6\u8DEF\u5F84\u8BA9\u9644\u4EF6\u81EA\u52A8\u968F\u884C\u3002
|
|
24
|
+
- worker \u7A7A\u95F2\u8FC7\u4E45\u6309 TTL \u56DE\u6536\uFF1B\u5173\u95ED\u524D\u786E\u8BA4\u4E0D\u5728 working\u3002
|
|
25
|
+
- \u4F60\u65E0\u4EBA\u503C\u5B88\uFF1A\u6CA1\u6709\u4EBA\u5728\u7EC8\u7AEF\u65C1\u3002\u9700\u8981\u7528\u6237\u51B3\u7B56\u65F6\u628A\u95EE\u9898\u539F\u6837\u53D1\u5230 IM\uFF0C\u4E0D\u8981\u5047\u8BBE\u7B54\u6848\u3002
|
|
26
|
+
- \u6210\u672C\u610F\u8BC6\uFF1A\u4E0D\u5E38\u5F00\u65E0\u7528\u7684 worker\uFF1B\u4E00\u6B21\u6027\u4EFB\u52A1\u7528 pie -p\uFF0C\u9000\u51FA\u5373\u5B8C\u6210\u3002
|
|
27
|
+
- \u7528\u6237\u5728 IM \u7684\u8FFD\u95EE\u843D\u5728\u54EA\u4E2A thread\uFF0C\u5C31\u67E5\u8DEF\u7531\u8D26\u672C\u7EE7\u7EED\u6D3E\u7ED9\u540C\u4E00\u4E2A worker\uFF08session \u4EB2\u548C\uFF09\u3002
|
|
28
|
+
`;
|
|
29
|
+
function workspaceDir() {
|
|
30
|
+
return path.join(os.homedir(), ".pie", "concierge");
|
|
31
|
+
}
|
|
32
|
+
function ensureSessionToken(dir) {
|
|
33
|
+
const tokenFile = path.join(dir, "session-token");
|
|
34
|
+
for (let attempt = 0; attempt < 5; attempt += 1) {
|
|
35
|
+
const mine = `concierge-${os.hostname().split(".")[0]}-${Math.random().toString(36).slice(2, 8)}`;
|
|
36
|
+
const tmp = `${tokenFile}.${process.pid}.${attempt}.tmp`;
|
|
37
|
+
let tmpKept = false;
|
|
38
|
+
try {
|
|
39
|
+
fs.writeFileSync(tmp, mine);
|
|
40
|
+
tmpKept = true;
|
|
41
|
+
fs.linkSync(tmp, tokenFile);
|
|
42
|
+
try {
|
|
43
|
+
fs.rmSync(tmp, { force: true });
|
|
44
|
+
tmpKept = false;
|
|
45
|
+
} catch {
|
|
46
|
+
}
|
|
47
|
+
} catch (error) {
|
|
48
|
+
if (error.code !== "EEXIST") {
|
|
49
|
+
if (tmpKept) {
|
|
50
|
+
try {
|
|
51
|
+
fs.rmSync(tmp, { force: true });
|
|
52
|
+
} catch {
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
const existing = (() => {
|
|
58
|
+
try {
|
|
59
|
+
return fs.readFileSync(tokenFile, "utf8").trim();
|
|
60
|
+
} catch {
|
|
61
|
+
return "";
|
|
62
|
+
}
|
|
63
|
+
})();
|
|
64
|
+
if (existing) {
|
|
65
|
+
if (tmpKept) {
|
|
66
|
+
try {
|
|
67
|
+
fs.rmSync(tmp, { force: true });
|
|
68
|
+
} catch {
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return existing;
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
fs.renameSync(tmp, tokenFile);
|
|
75
|
+
tmpKept = false;
|
|
76
|
+
} catch {
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const settled = (() => {
|
|
80
|
+
try {
|
|
81
|
+
return fs.readFileSync(tokenFile, "utf8").trim();
|
|
82
|
+
} catch {
|
|
83
|
+
return "";
|
|
84
|
+
}
|
|
85
|
+
})();
|
|
86
|
+
if (settled) return settled;
|
|
87
|
+
if (tmpKept) {
|
|
88
|
+
try {
|
|
89
|
+
fs.rmSync(tmp, { force: true });
|
|
90
|
+
} catch {
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
throw new Error("cannot establish the concierge workspace token; retry");
|
|
95
|
+
}
|
|
96
|
+
function statePath(dir = workspaceDir()) {
|
|
97
|
+
return path.join(dir, "concierge.json");
|
|
98
|
+
}
|
|
99
|
+
function logPath(dir = workspaceDir()) {
|
|
100
|
+
return path.join(dir, "log.jsonl");
|
|
101
|
+
}
|
|
102
|
+
function readState(dir = workspaceDir()) {
|
|
103
|
+
try {
|
|
104
|
+
const raw = JSON.parse(
|
|
105
|
+
fs.readFileSync(statePath(dir), "utf8")
|
|
106
|
+
);
|
|
107
|
+
return typeof raw.sessionId === "string" && raw.sessionId ? raw : void 0;
|
|
108
|
+
} catch {
|
|
109
|
+
return void 0;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function writeState(state, dir = workspaceDir()) {
|
|
113
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
114
|
+
const tmp = `${statePath(dir)}.${process.pid}.tmp`;
|
|
115
|
+
fs.writeFileSync(tmp, JSON.stringify(state, null, " "));
|
|
116
|
+
fs.renameSync(tmp, statePath(dir));
|
|
117
|
+
}
|
|
118
|
+
function conciergeLiveness(pid, state) {
|
|
119
|
+
if (!pid || pid <= 0) return "dead";
|
|
120
|
+
try {
|
|
121
|
+
process.kill(pid, 0);
|
|
122
|
+
} catch {
|
|
123
|
+
return "dead";
|
|
124
|
+
}
|
|
125
|
+
try {
|
|
126
|
+
const cmd = readProcessCommandLine(pid);
|
|
127
|
+
if (cmd === void 0) {
|
|
128
|
+
return "unknown";
|
|
129
|
+
}
|
|
130
|
+
const sessionMatch = cmd.includes(state?.sessionId ?? "concierge-");
|
|
131
|
+
return /--idle/.test(cmd) && sessionMatch && /(pie|cli\.(js|ts|mjs))/i.test(cmd) ? "alive" : "dead";
|
|
132
|
+
} catch {
|
|
133
|
+
return "unknown";
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
function isConciergeAlive(pid, state) {
|
|
137
|
+
return conciergeLiveness(pid, state) === "alive";
|
|
138
|
+
}
|
|
139
|
+
function seedWorkspace(dir) {
|
|
140
|
+
fs.mkdirSync(path.join(dir, ".pie"), { recursive: true });
|
|
141
|
+
const agents = path.join(dir, "AGENTS.md");
|
|
142
|
+
if (!fs.existsSync(agents)) {
|
|
143
|
+
fs.writeFileSync(agents, SEED_AGENTS_MD);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function cliEntry() {
|
|
147
|
+
return process.argv[1] ? path.resolve(process.argv[1]) : "";
|
|
148
|
+
}
|
|
149
|
+
function resolveTsxLoaderUrl() {
|
|
150
|
+
for (let i = 0; i < process.execArgv.length - 1; i += 1) {
|
|
151
|
+
if (process.execArgv[i] === "--import") {
|
|
152
|
+
const target = process.execArgv[i + 1];
|
|
153
|
+
if (target && !path.isAbsolute(target) && !target.startsWith("file:")) {
|
|
154
|
+
try {
|
|
155
|
+
return require_.resolve(target, { paths: [path.dirname(cliEntry())] }) ? pathToFileURLResolved(target) : target;
|
|
156
|
+
} catch {
|
|
157
|
+
return target;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return target;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return "";
|
|
164
|
+
}
|
|
165
|
+
function pathToFileURLResolved(spec) {
|
|
166
|
+
try {
|
|
167
|
+
const pkgRoot = path.dirname(
|
|
168
|
+
require_.resolve(`${spec}/package.json`, {
|
|
169
|
+
paths: [path.dirname(cliEntry())]
|
|
170
|
+
})
|
|
171
|
+
);
|
|
172
|
+
return path.join(pkgRoot, "dist", "loader.mjs");
|
|
173
|
+
} catch {
|
|
174
|
+
return spec;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
function spawnResident(state, dir) {
|
|
178
|
+
const entry = cliEntry();
|
|
179
|
+
if (!entry) throw new Error("cannot resolve the pie entry point");
|
|
180
|
+
const out = fs.openSync(logPath(dir), "a");
|
|
181
|
+
const loaderAbs = resolveTsxLoaderUrl();
|
|
182
|
+
const passthrough = loaderAbs ? ["--import", loaderAbs] : process.execArgv.filter(
|
|
183
|
+
(a) => a !== "--import" && !a.startsWith("--import=")
|
|
184
|
+
);
|
|
185
|
+
const args = [
|
|
186
|
+
...passthrough,
|
|
187
|
+
entry,
|
|
188
|
+
"--json-events",
|
|
189
|
+
"--idle",
|
|
190
|
+
"--session-id",
|
|
191
|
+
state.sessionId
|
|
192
|
+
];
|
|
193
|
+
const child = spawn(process.execPath, args, {
|
|
194
|
+
cwd: dir,
|
|
195
|
+
detached: true,
|
|
196
|
+
stdio: ["ignore", out, out],
|
|
197
|
+
windowsHide: true,
|
|
198
|
+
env: { ...process.env }
|
|
199
|
+
});
|
|
200
|
+
child.unref();
|
|
201
|
+
writeState({ ...state, pid: child.pid, startedAt: Date.now() });
|
|
202
|
+
return child.pid;
|
|
203
|
+
}
|
|
204
|
+
async function waitForAgentReady(dir, timeoutMs, sinceOffset, expectedSessionId) {
|
|
205
|
+
const deadline = Date.now() + timeoutMs;
|
|
206
|
+
while (Date.now() < deadline) {
|
|
207
|
+
try {
|
|
208
|
+
const size = fs.statSync(logPath(dir)).size;
|
|
209
|
+
if (size > sinceOffset) {
|
|
210
|
+
const fd = fs.openSync(logPath(dir), "r");
|
|
211
|
+
try {
|
|
212
|
+
const length = Math.min(size - sinceOffset, 65536);
|
|
213
|
+
const buffer = Buffer.alloc(length);
|
|
214
|
+
fs.readSync(
|
|
215
|
+
fd,
|
|
216
|
+
buffer,
|
|
217
|
+
0,
|
|
218
|
+
length,
|
|
219
|
+
Math.max(sinceOffset, size - 65536)
|
|
220
|
+
);
|
|
221
|
+
const slice = buffer.toString("utf8");
|
|
222
|
+
if (slice.includes('"type":"agent.ready"') && slice.includes(expectedSessionId))
|
|
223
|
+
return true;
|
|
224
|
+
} finally {
|
|
225
|
+
fs.closeSync(fd);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
} catch {
|
|
229
|
+
}
|
|
230
|
+
await new Promise((resolve2) => setTimeout(resolve2, 250));
|
|
231
|
+
}
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
function imForwardConfigured(dir) {
|
|
235
|
+
const layers = [
|
|
236
|
+
path.join(dir, ".pie", "im-forward.json"),
|
|
237
|
+
path.join(os.homedir(), ".pie", "im-forward.json")
|
|
238
|
+
];
|
|
239
|
+
for (const layer of layers) {
|
|
240
|
+
try {
|
|
241
|
+
const parsed = JSON.parse(fs.readFileSync(layer, "utf8"));
|
|
242
|
+
if (typeof parsed.provider === "string") return true;
|
|
243
|
+
} catch {
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return false;
|
|
247
|
+
}
|
|
248
|
+
async function runInlineImSetup() {
|
|
249
|
+
console.log(`
|
|
250
|
+
\u672A\u627E\u5230 IM \u8F6C\u53D1\u914D\u7F6E\u3002\u9996\u6B21\u7ED1\u5B9A\u9700\u8981\u4EA4\u4E92\u5F0F\u5411\u5BFC\uFF08\u9009\u62E9\u5E73\u53F0\u3001\u6388\u6743 app\u3001\u9009\u62E9\u4F1A\u8BDD\uFF09\uFF1A
|
|
251
|
+
|
|
252
|
+
1) \u5728\u4EFB\u610F\u9879\u76EE\u76EE\u5F55\u8FD0\u884C pie\uFF0C\u6309 Ctrl+R \u6216\u6267\u884C /tools/im-forward setup \u8D70\u5B8C\u5411\u5BFC\uFF1B
|
|
253
|
+
2) \u5411\u5BFC\u9ED8\u8BA4\u5199\u5165\u7528\u6237\u5C42\u914D\u7F6E\uFF08~/.pie/im-forward.json\uFF09\uFF0C\u6240\u6709 pie \u5171\u4EAB\uFF1B
|
|
254
|
+
3) \u91CD\u65B0\u8FD0\u884C pie concierge\u3002
|
|
255
|
+
|
|
256
|
+
\u82E5\u60F3\u7ED9\u7BA1\u5BB6\u5355\u72EC\u7ED1\u5B9A\u4E00\u4E2A app\uFF1A\u5728 ~/.pie/concierge/.pie/im-forward.json \u653E\u4E00\u4EFD\u9879\u76EE\u5C42\u914D\u7F6E\u3002
|
|
257
|
+
`);
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
function platformServiceName() {
|
|
261
|
+
return "dev.pie.concierge";
|
|
262
|
+
}
|
|
263
|
+
function xmlEscape(value) {
|
|
264
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
265
|
+
}
|
|
266
|
+
function systemdEscape(value) {
|
|
267
|
+
if (/[\n\r\t\u0000-\u001f]/.test(value)) throw new Error(`argument contains characters a systemd unit cannot express`);
|
|
268
|
+
const noSpecifiers = value.replace(/%/g, "%%");
|
|
269
|
+
return `"${noSpecifiers.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
|
|
270
|
+
}
|
|
271
|
+
function systemdScalarPath(value) {
|
|
272
|
+
if (/[\n\r\t\u0000-\u001f]/.test(value)) throw new Error(`service path contains characters a systemd unit cannot express: ${JSON.stringify(value)}`);
|
|
273
|
+
return value.replace(/%/g, "%%");
|
|
274
|
+
}
|
|
275
|
+
function systemdEnvironmentToken(value) {
|
|
276
|
+
if (/[\n\r\t\u0000-\u001f]/.test(value)) throw new Error(`environment value contains characters a systemd unit cannot express`);
|
|
277
|
+
const escaped = value.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/%/g, "%%");
|
|
278
|
+
return `"${escaped}"`;
|
|
279
|
+
}
|
|
280
|
+
function installService(dir, state) {
|
|
281
|
+
const entry = cliEntry();
|
|
282
|
+
const logFile = logPath(dir);
|
|
283
|
+
const servicePath = process.env.PATH ?? "/usr/bin:/bin:/usr/sbin:/sbin";
|
|
284
|
+
if (process.platform === "darwin") {
|
|
285
|
+
const plist = `<?xml version="1.0" encoding="UTF-8"?>
|
|
286
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
287
|
+
<plist version="1.0"><dict>
|
|
288
|
+
<key>Label</key><string>${platformServiceName()}</string>
|
|
289
|
+
<key>ProgramArguments</key><array>
|
|
290
|
+
<string>${xmlEscape(process.execPath)}</string>
|
|
291
|
+
<string>${xmlEscape(entry)}</string>
|
|
292
|
+
<string>--json-events</string><string>--idle</string>
|
|
293
|
+
<string>--session-id</string><string>${xmlEscape(state.sessionId)}</string>
|
|
294
|
+
</array>
|
|
295
|
+
<key>EnvironmentVariables</key><dict>
|
|
296
|
+
<key>PATH</key><string>${xmlEscape(servicePath)}</string>
|
|
297
|
+
</dict>
|
|
298
|
+
<key>WorkingDirectory</key><string>${xmlEscape(dir)}</string>
|
|
299
|
+
<key>RunAtLoad</key><true/>
|
|
300
|
+
<key>KeepAlive</key><true/>
|
|
301
|
+
<key>StandardOutPath</key><string>${xmlEscape(logFile)}</string>
|
|
302
|
+
<key>StandardErrorPath</key><string>${xmlEscape(logFile)}</string>
|
|
303
|
+
</dict></plist>`;
|
|
304
|
+
const target = path.join(
|
|
305
|
+
os.homedir(),
|
|
306
|
+
"Library",
|
|
307
|
+
"LaunchAgents",
|
|
308
|
+
`${platformServiceName()}.plist`
|
|
309
|
+
);
|
|
310
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
311
|
+
fs.writeFileSync(target, plist);
|
|
312
|
+
try {
|
|
313
|
+
execFileSync("launchctl", ["unload", target], { stdio: "ignore" });
|
|
314
|
+
} catch {
|
|
315
|
+
}
|
|
316
|
+
execFileSync("launchctl", ["load", target]);
|
|
317
|
+
console.log(`\u5DF2\u5B89\u88C5 launchd \u670D\u52A1: ${target}\uFF08RunAtLoad + KeepAlive\uFF09`);
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
if (process.platform === "linux") {
|
|
321
|
+
const unitDir = path.join(os.homedir(), ".config", "systemd", "user");
|
|
322
|
+
fs.mkdirSync(unitDir, { recursive: true });
|
|
323
|
+
const unit = `[Unit]
|
|
324
|
+
Description=Pie concierge (resident im-forward host)
|
|
325
|
+
After=network-online.target
|
|
326
|
+
|
|
327
|
+
[Service]
|
|
328
|
+
Environment=${systemdEnvironmentToken(`PATH=${servicePath}`)}
|
|
329
|
+
# r26 P2: the ':' prefix disables systemd's variable expansion for this
|
|
330
|
+
# command line \u2014 install paths may legitimately contain literal '$' runs,
|
|
331
|
+
# and quotes do NOT protect them (undefined vars expand to empty strings).
|
|
332
|
+
ExecStart=:${systemdEscape(process.execPath)} ${systemdEscape(entry)} --json-events --idle --session-id ${systemdEscape(state.sessionId)}
|
|
333
|
+
WorkingDirectory=${systemdScalarPath(dir)}
|
|
334
|
+
Restart=always
|
|
335
|
+
RestartSec=3
|
|
336
|
+
StandardOutput=append:${systemdScalarPath(logFile)}
|
|
337
|
+
StandardError=append:${systemdScalarPath(logFile)}
|
|
338
|
+
|
|
339
|
+
[Install]
|
|
340
|
+
WantedBy=default.target`;
|
|
341
|
+
const unitPath = path.join(unitDir, `${platformServiceName()}.service`);
|
|
342
|
+
fs.writeFileSync(unitPath, unit);
|
|
343
|
+
execFileSync("systemctl", ["--user", "daemon-reload"]);
|
|
344
|
+
execFileSync("systemctl", [
|
|
345
|
+
"--user",
|
|
346
|
+
"enable",
|
|
347
|
+
"--now",
|
|
348
|
+
platformServiceName()
|
|
349
|
+
]);
|
|
350
|
+
console.log(`\u5DF2\u5B89\u88C5 systemd \u7528\u6237\u670D\u52A1: ${unitPath}\uFF08Restart=always\uFF09`);
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
if (process.platform === "win32") {
|
|
354
|
+
const cmd = `cd /d "${dir}" && "${process.execPath}" "${entry}" --json-events --idle --session-id ${state.sessionId} >> "${logFile}" 2>&1`;
|
|
355
|
+
execFileSync("schtasks", [
|
|
356
|
+
"/Create",
|
|
357
|
+
"/TN",
|
|
358
|
+
platformServiceName(),
|
|
359
|
+
"/TR",
|
|
360
|
+
cmd,
|
|
361
|
+
"/SC",
|
|
362
|
+
"ONLOGON",
|
|
363
|
+
"/RL",
|
|
364
|
+
"LIMITED",
|
|
365
|
+
"/F"
|
|
366
|
+
]);
|
|
367
|
+
console.log(
|
|
368
|
+
`\u5DF2\u5B89\u88C5\u8BA1\u5212\u4EFB\u52A1: ${platformServiceName()}\uFF08\u767B\u5F55\u65F6\u542F\u52A8\uFF1B\u5D29\u6E83\u91CD\u542F\u8BF7\u7528\u4EFB\u52A1\u8BA1\u5212\u7A0B\u5E8F\u8BBE\u7F6E\uFF09`
|
|
369
|
+
);
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
throw new Error(
|
|
373
|
+
`unsupported platform for --install-service: ${process.platform}`
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
function uninstallService() {
|
|
377
|
+
if (process.platform === "darwin") {
|
|
378
|
+
const target = path.join(
|
|
379
|
+
os.homedir(),
|
|
380
|
+
"Library",
|
|
381
|
+
"LaunchAgents",
|
|
382
|
+
`${platformServiceName()}.plist`
|
|
383
|
+
);
|
|
384
|
+
let unloaded = false;
|
|
385
|
+
try {
|
|
386
|
+
execFileSync("launchctl", ["unload", target], { stdio: "ignore" });
|
|
387
|
+
unloaded = true;
|
|
388
|
+
} catch {
|
|
389
|
+
unloaded = !fs.existsSync(target);
|
|
390
|
+
}
|
|
391
|
+
if (unloaded) {
|
|
392
|
+
try {
|
|
393
|
+
fs.rmSync(target);
|
|
394
|
+
console.log(`\u5DF2\u5378\u8F7D launchd \u670D\u52A1: ${target}`);
|
|
395
|
+
} catch {
|
|
396
|
+
console.log("\u672A\u627E\u5230\u5DF2\u5B89\u88C5\u7684\u670D\u52A1\uFF08plist \u5DF2\u4E0D\u5728\uFF09\u3002");
|
|
397
|
+
}
|
|
398
|
+
return true;
|
|
399
|
+
}
|
|
400
|
+
console.error(
|
|
401
|
+
"launchctl unload \u5931\u8D25\uFF1B\u670D\u52A1\u53EF\u80FD\u4ECD\u52A0\u8F7D\uFF0C\u4FDD\u7559 plist \u4E0E\u6240\u6709\u6743\u3002"
|
|
402
|
+
);
|
|
403
|
+
return false;
|
|
404
|
+
}
|
|
405
|
+
if (process.platform === "linux") {
|
|
406
|
+
let ok = true;
|
|
407
|
+
try {
|
|
408
|
+
execFileSync("systemctl", [
|
|
409
|
+
"--user",
|
|
410
|
+
"disable",
|
|
411
|
+
"--now",
|
|
412
|
+
platformServiceName()
|
|
413
|
+
]);
|
|
414
|
+
} catch {
|
|
415
|
+
const unitPath2 = path.join(
|
|
416
|
+
os.homedir(),
|
|
417
|
+
".config",
|
|
418
|
+
"systemd",
|
|
419
|
+
"user",
|
|
420
|
+
`${platformServiceName()}.service`
|
|
421
|
+
);
|
|
422
|
+
ok = !fs.existsSync(unitPath2);
|
|
423
|
+
if (!ok) {
|
|
424
|
+
console.error(
|
|
425
|
+
"systemctl disable \u5931\u8D25\uFF1B\u670D\u52A1\u53EF\u80FD\u4ECD\u5728\u8FD0\u884C\uFF0C\u4FDD\u7559 unit \u6587\u4EF6\u4E0E\u6240\u6709\u6743\u3002"
|
|
426
|
+
);
|
|
427
|
+
return false;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
const unitPath = path.join(
|
|
431
|
+
os.homedir(),
|
|
432
|
+
".config",
|
|
433
|
+
"systemd",
|
|
434
|
+
"user",
|
|
435
|
+
`${platformServiceName()}.service`
|
|
436
|
+
);
|
|
437
|
+
try {
|
|
438
|
+
fs.rmSync(unitPath);
|
|
439
|
+
execFileSync("systemctl", ["--user", "daemon-reload"]);
|
|
440
|
+
console.log(`\u5DF2\u5378\u8F7D systemd \u670D\u52A1\u3002`);
|
|
441
|
+
} catch {
|
|
442
|
+
}
|
|
443
|
+
return ok;
|
|
444
|
+
}
|
|
445
|
+
if (process.platform === "win32") {
|
|
446
|
+
let endOutcome = "ended";
|
|
447
|
+
try {
|
|
448
|
+
execFileSync("schtasks", ["/End", "/TN", platformServiceName()], {
|
|
449
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
450
|
+
});
|
|
451
|
+
} catch (endError) {
|
|
452
|
+
const err = endError;
|
|
453
|
+
const text = `${String(err.stderr ?? "")} ${String(err.stdout ?? "")} ${String(err.message ?? "")}`;
|
|
454
|
+
endOutcome = /cannot find|does not exist|not found/i.test(text) ? "not-found" : "error";
|
|
455
|
+
}
|
|
456
|
+
if (endOutcome === "error") {
|
|
457
|
+
console.error(
|
|
458
|
+
"\u4EFB\u52A1\u7EC8\u6B62\u672A\u80FD\u786E\u8BA4\uFF08/End \u64CD\u4F5C\u6027\u5931\u8D25\uFF09\uFF1B\u6CE8\u518C\u4FDD\u7559\uFF0C\u6240\u6709\u6743\u4FDD\u7559\u3002"
|
|
459
|
+
);
|
|
460
|
+
return false;
|
|
461
|
+
}
|
|
462
|
+
let registrationGone = false;
|
|
463
|
+
try {
|
|
464
|
+
execFileSync("schtasks", ["/Delete", "/TN", platformServiceName(), "/F"]);
|
|
465
|
+
registrationGone = true;
|
|
466
|
+
console.log("\u5DF2\u5378\u8F7D\u8BA1\u5212\u4EFB\u52A1\u3002");
|
|
467
|
+
} catch {
|
|
468
|
+
try {
|
|
469
|
+
const probe = execFileSync(
|
|
470
|
+
"schtasks",
|
|
471
|
+
["/Query", "/TN", platformServiceName()],
|
|
472
|
+
{ encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }
|
|
473
|
+
).toString();
|
|
474
|
+
registrationGone = /cannot find|does not exist|not found/i.test(probe) || probe.trim().length === 0;
|
|
475
|
+
} catch (queryError) {
|
|
476
|
+
const text = String(queryError.stderr ?? "");
|
|
477
|
+
registrationGone = /cannot find the file|cannot find|does not exist/i.test(text);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
if (!registrationGone) {
|
|
481
|
+
console.error("schtasks \u5220\u9664/\u67E5\u8BE2\u672A\u80FD\u786E\u8BA4\u79FB\u9664\uFF1B\u4FDD\u7559\u6240\u6709\u6743\u3002");
|
|
482
|
+
return false;
|
|
483
|
+
}
|
|
484
|
+
return true;
|
|
485
|
+
}
|
|
486
|
+
throw new Error(`unsupported platform: ${process.platform}`);
|
|
487
|
+
}
|
|
488
|
+
async function stopConcierge(state) {
|
|
489
|
+
if (!state.pid) {
|
|
490
|
+
console.log("\u6CA1\u6709\u8BB0\u5F55\u7684 pid\uFF1B\u65E0\u64CD\u4F5C\u3002");
|
|
491
|
+
return 0;
|
|
492
|
+
}
|
|
493
|
+
const liveness0 = conciergeLiveness(state.pid, state);
|
|
494
|
+
if (liveness0 === "dead") {
|
|
495
|
+
writeState({ sessionId: state.sessionId });
|
|
496
|
+
console.log("\u8FDB\u7A0B\u5DF2\u4E0D\u5728\u8FD0\u884C\u3002");
|
|
497
|
+
return 0;
|
|
498
|
+
}
|
|
499
|
+
if (liveness0 === "unknown") {
|
|
500
|
+
console.error(
|
|
501
|
+
`\u65E0\u6CD5\u9A8C\u8BC1 pid ${state.pid} \u7684\u8EAB\u4EFD\uFF08ps/wmic \u5931\u8D25\uFF09\uFF1B\u62D2\u7EDD\u64CD\u4F5C\uFF0C\u4FDD\u7559\u72B6\u6001\u3002\u7A0D\u540E\u91CD\u8BD5\u6216\u624B\u52A8\u786E\u8BA4\u3002`
|
|
502
|
+
);
|
|
503
|
+
return 1;
|
|
504
|
+
}
|
|
505
|
+
try {
|
|
506
|
+
process.kill(
|
|
507
|
+
state.pid,
|
|
508
|
+
process.platform === "win32" ? void 0 : "SIGTERM"
|
|
509
|
+
);
|
|
510
|
+
} catch {
|
|
511
|
+
}
|
|
512
|
+
const deadline = Date.now() + 2e4;
|
|
513
|
+
while (Date.now() < deadline) {
|
|
514
|
+
if (!isConciergeAlive(state.pid, state)) break;
|
|
515
|
+
await new Promise((resolve2) => setTimeout(resolve2, 200));
|
|
516
|
+
}
|
|
517
|
+
const finalState = conciergeLiveness(state.pid, state);
|
|
518
|
+
if (finalState === "alive" || finalState === "unknown") {
|
|
519
|
+
console.error(
|
|
520
|
+
`\u5173\u505C\u672A\u786E\u8BA4\uFF08\u72B6\u6001: ${finalState}\uFF09\uFF1B\u672A\u6E05\u9664 pid\uFF1B\u7528 pie concierge stop \u91CD\u8BD5\u6216\u624B\u52A8\u5904\u7406\u3002`
|
|
521
|
+
);
|
|
522
|
+
return 1;
|
|
523
|
+
}
|
|
524
|
+
writeState({ sessionId: state.sessionId });
|
|
525
|
+
console.log("\u5DF2\u505C\u6B62\u3002");
|
|
526
|
+
return 0;
|
|
527
|
+
}
|
|
528
|
+
function startLockDir(dir) {
|
|
529
|
+
return path.join(dir, "start-locks");
|
|
530
|
+
}
|
|
531
|
+
function withStartLock(dir, fn) {
|
|
532
|
+
const locks = startLockDir(dir);
|
|
533
|
+
fs.mkdirSync(locks, { recursive: true });
|
|
534
|
+
const deadline = Date.now() + 9e4;
|
|
535
|
+
let mine;
|
|
536
|
+
for (; ; ) {
|
|
537
|
+
if (Date.now() > deadline)
|
|
538
|
+
throw new Error(
|
|
539
|
+
"another pie concierge lifecycle command is in progress; retry shortly"
|
|
540
|
+
);
|
|
541
|
+
let latest = 0;
|
|
542
|
+
try {
|
|
543
|
+
for (const entry of fs.readdirSync(locks)) {
|
|
544
|
+
const m = /^start\.r(\d+)\.json$/.exec(entry);
|
|
545
|
+
if (m) latest = Math.max(latest, Number(m[1]));
|
|
546
|
+
}
|
|
547
|
+
} catch {
|
|
548
|
+
}
|
|
549
|
+
if (latest > 0) {
|
|
550
|
+
let holderBlocking = false;
|
|
551
|
+
const latestPath = path.join(locks, `start.r${latest}.json`);
|
|
552
|
+
try {
|
|
553
|
+
if (fs.existsSync(`${latestPath}.released`)) holderBlocking = false;
|
|
554
|
+
else {
|
|
555
|
+
const content = JSON.parse(fs.readFileSync(latestPath, "utf8"));
|
|
556
|
+
if (typeof content.pid === "number" && content.pid > 0) {
|
|
557
|
+
try {
|
|
558
|
+
process.kill(content.pid, 0);
|
|
559
|
+
holderBlocking = true;
|
|
560
|
+
} catch {
|
|
561
|
+
holderBlocking = false;
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
} catch {
|
|
566
|
+
}
|
|
567
|
+
if (holderBlocking) {
|
|
568
|
+
Atomics.wait?.(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 100);
|
|
569
|
+
continue;
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
const next = latest + 1;
|
|
573
|
+
const target = path.join(locks, `start.r${next}.json`);
|
|
574
|
+
const tmp = `${target}.${process.pid}.${Date.now().toString(36)}.tmp`;
|
|
575
|
+
try {
|
|
576
|
+
fs.writeFileSync(
|
|
577
|
+
tmp,
|
|
578
|
+
JSON.stringify({ pid: process.pid, at: Date.now() })
|
|
579
|
+
);
|
|
580
|
+
fs.linkSync(tmp, target);
|
|
581
|
+
mine = target;
|
|
582
|
+
break;
|
|
583
|
+
} catch (error) {
|
|
584
|
+
try {
|
|
585
|
+
fs.rmSync(tmp, { force: true });
|
|
586
|
+
} catch {
|
|
587
|
+
}
|
|
588
|
+
if (error.code === "EEXIST") continue;
|
|
589
|
+
throw error;
|
|
590
|
+
} finally {
|
|
591
|
+
try {
|
|
592
|
+
if (mine) fs.rmSync(tmp, { force: true });
|
|
593
|
+
} catch {
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
return Promise.resolve(fn()).finally(() => {
|
|
598
|
+
if (!mine) return;
|
|
599
|
+
try {
|
|
600
|
+
const content = JSON.parse(fs.readFileSync(mine, "utf8"));
|
|
601
|
+
if (content.pid === process.pid) {
|
|
602
|
+
const tmp = `${mine}.released.${process.pid}.tmp`;
|
|
603
|
+
fs.writeFileSync(tmp, String(Date.now()));
|
|
604
|
+
try {
|
|
605
|
+
fs.linkSync(tmp, `${mine}.released`);
|
|
606
|
+
} catch {
|
|
607
|
+
}
|
|
608
|
+
try {
|
|
609
|
+
fs.rmSync(tmp, { force: true });
|
|
610
|
+
} catch {
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
} catch {
|
|
614
|
+
}
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
async function runConciergeCommand(args) {
|
|
618
|
+
const sub = args[0] ?? "";
|
|
619
|
+
const dir = workspaceDir();
|
|
620
|
+
if (sub === "status") {
|
|
621
|
+
const state = readState();
|
|
622
|
+
if (!state) {
|
|
623
|
+
console.log("\u672A\u521D\u59CB\u5316\uFF08\u5148\u8FD0\u884C pie concierge\uFF09\u3002");
|
|
624
|
+
return;
|
|
625
|
+
}
|
|
626
|
+
console.log(`session: ${state.sessionId}`);
|
|
627
|
+
if (state.service) {
|
|
628
|
+
let running = false;
|
|
629
|
+
try {
|
|
630
|
+
if (process.platform === "darwin") {
|
|
631
|
+
const listing = execFileSync("launchctl", ["list"], {
|
|
632
|
+
encoding: "utf8",
|
|
633
|
+
stdio: ["ignore", "pipe", "ignore"]
|
|
634
|
+
});
|
|
635
|
+
const row = listing.split("\n").find((line) => {
|
|
636
|
+
const cols = line.trim().split(/\s+/);
|
|
637
|
+
return cols.length >= 3 && cols.slice(2).join(" ") === platformServiceName();
|
|
638
|
+
});
|
|
639
|
+
if (row) {
|
|
640
|
+
const pid = row.trim().split(/\s+/)[0];
|
|
641
|
+
running = pid !== "-" && /^\d+$/.test(pid);
|
|
642
|
+
if (!running)
|
|
643
|
+
console.log(
|
|
644
|
+
"service: loaded, but no resident process (crashed or throttled)"
|
|
645
|
+
);
|
|
646
|
+
}
|
|
647
|
+
} else if (process.platform === "linux") {
|
|
648
|
+
execFileSync("systemctl", [
|
|
649
|
+
"--user",
|
|
650
|
+
"is-active",
|
|
651
|
+
"--quiet",
|
|
652
|
+
platformServiceName()
|
|
653
|
+
]);
|
|
654
|
+
running = true;
|
|
655
|
+
} else {
|
|
656
|
+
console.log(
|
|
657
|
+
`service: ${platformServiceName()} (unknown \u2014 Windows status query not implemented)`
|
|
658
|
+
);
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
} catch {
|
|
662
|
+
}
|
|
663
|
+
console.log(
|
|
664
|
+
`service: ${platformServiceName()} ${running ? "(running)" : "(stopped)"}`
|
|
665
|
+
);
|
|
666
|
+
} else {
|
|
667
|
+
const liveness = conciergeLiveness(state.pid, state);
|
|
668
|
+
const label = liveness === "alive" ? "(running)" : liveness === "unknown" ? "(alive, identity unverified)" : "(not running)";
|
|
669
|
+
console.log(`pid: ${state.pid ?? "-"} ${label}`);
|
|
670
|
+
}
|
|
671
|
+
console.log(`workspace: ${dir}`);
|
|
672
|
+
console.log(`log: ${logPath(dir)}`);
|
|
673
|
+
return;
|
|
674
|
+
}
|
|
675
|
+
if (sub === "stop") {
|
|
676
|
+
await withStartLock(dir, async () => {
|
|
677
|
+
const state = readState();
|
|
678
|
+
if (!state) {
|
|
679
|
+
console.log("\u672A\u521D\u59CB\u5316\u3002");
|
|
680
|
+
process.exitCode = 1;
|
|
681
|
+
return;
|
|
682
|
+
}
|
|
683
|
+
if (state.service) {
|
|
684
|
+
if (process.platform === "darwin") {
|
|
685
|
+
try {
|
|
686
|
+
execFileSync(
|
|
687
|
+
"launchctl",
|
|
688
|
+
[
|
|
689
|
+
"unload",
|
|
690
|
+
path.join(
|
|
691
|
+
os.homedir(),
|
|
692
|
+
"Library",
|
|
693
|
+
"LaunchAgents",
|
|
694
|
+
`${platformServiceName()}.plist`
|
|
695
|
+
)
|
|
696
|
+
],
|
|
697
|
+
// r28 P2: CAPTURE stderr — stdio:"ignore" discarded launchctl's
|
|
698
|
+
// diagnostics ("No such file or directory"), so the not-loaded
|
|
699
|
+
// classification below never saw them and every failure looked
|
|
700
|
+
// operational.
|
|
701
|
+
{ stdio: ["ignore", "pipe", "pipe"] }
|
|
702
|
+
);
|
|
703
|
+
console.log(
|
|
704
|
+
"\u5DF2\u505C\u6B62\uFF08launchd unload\uFF1B\u8981\u7981\u7528\u91CD\u542F\u8BF7 --uninstall-service\uFF09\u3002"
|
|
705
|
+
);
|
|
706
|
+
} catch (stopError) {
|
|
707
|
+
const err = stopError;
|
|
708
|
+
const text = `${String(err.stderr ?? "")} ${String(err.stdout ?? "")} ${String(err.message ?? "")}`;
|
|
709
|
+
if (/No such file|not loaded|not found/i.test(text)) {
|
|
710
|
+
console.log("\u670D\u52A1\u672A\u5728\u8FD0\u884C\uFF08\u672A\u52A0\u8F7D\uFF09\u3002");
|
|
711
|
+
} else {
|
|
712
|
+
console.error("launchctl unload \u5931\u8D25\uFF1B\u505C\u6B62\u672A\u786E\u8BA4\uFF0C\u670D\u52A1\u6240\u6709\u6743\u4FDD\u7559\u3002");
|
|
713
|
+
process.exitCode = 1;
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
} else if (process.platform === "linux") {
|
|
717
|
+
try {
|
|
718
|
+
execFileSync("systemctl", [
|
|
719
|
+
"--user",
|
|
720
|
+
"stop",
|
|
721
|
+
platformServiceName()
|
|
722
|
+
]);
|
|
723
|
+
console.log("\u5DF2\u505C\u6B62\uFF08systemctl stop\uFF09\u3002");
|
|
724
|
+
} catch (stopError) {
|
|
725
|
+
const text = `${String(stopError.stderr ?? "")} ${String(stopError.message ?? "")}`;
|
|
726
|
+
if (/not loaded|unit.*not found|unknown unit/i.test(text)) {
|
|
727
|
+
console.log("\u670D\u52A1\u672A\u5728\u8FD0\u884C\uFF08inactive\uFF09\u3002");
|
|
728
|
+
} else {
|
|
729
|
+
console.error("systemctl stop \u5931\u8D25\uFF1B\u505C\u6B62\u672A\u786E\u8BA4\uFF0C\u670D\u52A1\u6240\u6709\u6743\u4FDD\u7559\u3002");
|
|
730
|
+
process.exitCode = 1;
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
} else if (process.platform === "win32") {
|
|
734
|
+
try {
|
|
735
|
+
execFileSync("schtasks", ["/End", "/TN", platformServiceName()], {
|
|
736
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
737
|
+
});
|
|
738
|
+
console.log("\u5DF2\u505C\u6B62\uFF08schtasks /End\uFF09\u3002");
|
|
739
|
+
} catch (endError) {
|
|
740
|
+
const err = endError;
|
|
741
|
+
const text = `${String(err.stderr ?? "")} ${String(err.stdout ?? "")} ${String(err.message ?? "")}`;
|
|
742
|
+
if (/cannot find|does not exist|not found/i.test(text)) {
|
|
743
|
+
console.log("\u670D\u52A1\u672A\u5728\u8FD0\u884C\uFF08\u4EFB\u52A1\u672A\u6CE8\u518C\uFF09\u3002");
|
|
744
|
+
} else {
|
|
745
|
+
console.error(
|
|
746
|
+
"\u505C\u6B62\u670D\u52A1\u5931\u8D25\uFF08schtasks /End \u64CD\u4F5C\u6027\u5931\u8D25\uFF09\uFF1B\u505C\u6B62\u672A\u786E\u8BA4\uFF0C\u6240\u6709\u6743\u4FDD\u7559\u3002"
|
|
747
|
+
);
|
|
748
|
+
process.exitCode = 1;
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
return;
|
|
753
|
+
}
|
|
754
|
+
process.exitCode = await stopConcierge(state);
|
|
755
|
+
});
|
|
756
|
+
return;
|
|
757
|
+
}
|
|
758
|
+
if (args.includes("--uninstall-service")) {
|
|
759
|
+
await withStartLock(dir, async () => {
|
|
760
|
+
const toreDown = uninstallService();
|
|
761
|
+
if (toreDown) {
|
|
762
|
+
const state = readState();
|
|
763
|
+
if (state && state.service) {
|
|
764
|
+
writeState({ sessionId: state.sessionId, service: void 0 });
|
|
765
|
+
} else if (state) {
|
|
766
|
+
console.log("\u672A\u68C0\u6D4B\u5230\u670D\u52A1\u6258\u7BA1\u72B6\u6001\uFF1B\u624B\u52A8\u5B9E\u4F8B\uFF08\u5982\u6709\uFF09\u4E0D\u53D7\u5F71\u54CD\u3002");
|
|
767
|
+
}
|
|
768
|
+
} else {
|
|
769
|
+
console.error(
|
|
770
|
+
"\u670D\u52A1\u5378\u8F7D\u672A\u786E\u8BA4\uFF08manager \u5931\u8D25\uFF09\uFF1B\u4FDD\u7559\u670D\u52A1\u6240\u6709\u6743\uFF0C\u672A\u5141\u8BB8\u624B\u52A8\u63A5\u7BA1\u3002"
|
|
771
|
+
);
|
|
772
|
+
process.exitCode = 1;
|
|
773
|
+
}
|
|
774
|
+
});
|
|
775
|
+
return;
|
|
776
|
+
}
|
|
777
|
+
if (args.includes("--install-service")) {
|
|
778
|
+
await withStartLock(dir, async () => {
|
|
779
|
+
const state = readState();
|
|
780
|
+
if (!state) {
|
|
781
|
+
console.error(
|
|
782
|
+
"\u5148\u8FD0\u884C pie concierge \u5B8C\u6210\u521D\u59CB\u5316\uFF08\u9700\u8981\u7A33\u5B9A session id\uFF09\u3002"
|
|
783
|
+
);
|
|
784
|
+
process.exitCode = 2;
|
|
785
|
+
return;
|
|
786
|
+
}
|
|
787
|
+
if (!imForwardConfigured(dir)) {
|
|
788
|
+
console.error("\u672A\u68C0\u6D4B\u5230 IM \u914D\u7F6E\uFF1B\u5148\u5B8C\u6210\u9996\u6B21 pie concierge \u521D\u59CB\u5316\u3002");
|
|
789
|
+
process.exitCode = 2;
|
|
790
|
+
return;
|
|
791
|
+
}
|
|
792
|
+
const installLiveness = conciergeLiveness(state.pid, state);
|
|
793
|
+
if (installLiveness === "alive" || installLiveness === "unknown") {
|
|
794
|
+
console.log(
|
|
795
|
+
`\u68C0\u6D4B\u5230\u5E38\u9A7B\u8FDB\u7A0B\uFF08\u72B6\u6001: ${installLiveness}\uFF09\uFF1B\u5B89\u88C5\u670D\u52A1\u524D\u5148 pie concierge stop\u3002`
|
|
796
|
+
);
|
|
797
|
+
process.exitCode = 2;
|
|
798
|
+
return;
|
|
799
|
+
}
|
|
800
|
+
installService(dir, state);
|
|
801
|
+
writeState({ ...state, pid: void 0, service: true });
|
|
802
|
+
});
|
|
803
|
+
return;
|
|
804
|
+
}
|
|
805
|
+
if (sub && sub !== "start") {
|
|
806
|
+
console.error(`\u672A\u77E5\u5B50\u547D\u4EE4: ${sub}`);
|
|
807
|
+
console.error(
|
|
808
|
+
"\u7528\u6CD5: pie concierge [status|stop|--install-service|--uninstall-service]"
|
|
809
|
+
);
|
|
810
|
+
process.exitCode = 2;
|
|
811
|
+
return;
|
|
812
|
+
}
|
|
813
|
+
const existing = readState();
|
|
814
|
+
if (existing?.service) {
|
|
815
|
+
console.log(
|
|
816
|
+
"\u7BA1\u5BB6\u7531\u7CFB\u7EDF\u670D\u52A1\u6258\u7BA1\uFF08launchd/systemd/\u8BA1\u5212\u4EFB\u52A1\uFF09\u3002\u8981\u624B\u52A8\u63A5\u7BA1\uFF1A\u5148 --uninstall-service\u3002"
|
|
817
|
+
);
|
|
818
|
+
return;
|
|
819
|
+
}
|
|
820
|
+
const startLiveness = existing ? conciergeLiveness(existing.pid, existing) : "dead";
|
|
821
|
+
if (existing && (startLiveness === "alive" || startLiveness === "unknown")) {
|
|
822
|
+
console.log(
|
|
823
|
+
`\u7BA1\u5BB6\u53EF\u80FD\u5728\u8FD0\u884C\uFF08pid ${existing.pid ?? "-"}\uFF0C\u72B6\u6001: ${startLiveness}\uFF0Csession ${existing.sessionId}\uFF09\u3002`
|
|
824
|
+
);
|
|
825
|
+
return;
|
|
826
|
+
}
|
|
827
|
+
if (!imForwardConfigured(dir) && !process.stdin.isTTY) {
|
|
828
|
+
console.error(
|
|
829
|
+
"\u672A\u627E\u5230 IM \u914D\u7F6E\u4E14\u65E0\u7EC8\u7AEF\u53EF\u8D70\u5411\u5BFC\uFF1B\u5148\u5728\u6709 TTY \u7684\u73AF\u5883\u8FD0\u884C pie concierge\u3002"
|
|
830
|
+
);
|
|
831
|
+
process.exitCode = 2;
|
|
832
|
+
return;
|
|
833
|
+
}
|
|
834
|
+
if (!imForwardConfigured(dir)) {
|
|
835
|
+
await runInlineImSetup();
|
|
836
|
+
process.exitCode = 2;
|
|
837
|
+
return;
|
|
838
|
+
}
|
|
839
|
+
seedWorkspace(dir);
|
|
840
|
+
let readinessReport;
|
|
841
|
+
await withStartLock(dir, async () => {
|
|
842
|
+
const lockedState = readState();
|
|
843
|
+
if (lockedState?.service) {
|
|
844
|
+
console.log(
|
|
845
|
+
"\u7BA1\u5BB6\u7531\u7CFB\u7EDF\u670D\u52A1\u6258\u7BA1\uFF08launchd/systemd/\u8BA1\u5212\u4EFB\u52A1\uFF09\u3002\u8981\u624B\u52A8\u63A5\u7BA1\uFF1A\u5148 --uninstall-service\u3002"
|
|
846
|
+
);
|
|
847
|
+
return;
|
|
848
|
+
}
|
|
849
|
+
const lockedLiveness = lockedState ? conciergeLiveness(lockedState.pid, lockedState) : "dead";
|
|
850
|
+
if (lockedState && (lockedLiveness === "alive" || lockedLiveness === "unknown")) {
|
|
851
|
+
console.log(
|
|
852
|
+
`\u7BA1\u5BB6\u53EF\u80FD\u5728\u8FD0\u884C\uFF08pid ${lockedState.pid ?? "-"}\uFF0C\u72B6\u6001: ${lockedLiveness}\uFF0Csession ${lockedState.sessionId}\uFF09\u3002`
|
|
853
|
+
);
|
|
854
|
+
return;
|
|
855
|
+
}
|
|
856
|
+
const token = ensureSessionToken(dir);
|
|
857
|
+
const state = lockedState ?? { sessionId: token };
|
|
858
|
+
let sinceOffset = 0;
|
|
859
|
+
try {
|
|
860
|
+
sinceOffset = fs.statSync(logPath(dir)).size;
|
|
861
|
+
} catch {
|
|
862
|
+
}
|
|
863
|
+
const pid = spawnResident(state, dir);
|
|
864
|
+
try {
|
|
865
|
+
const listing = execFileSync("ps", ["-eo", "pid,command"], {
|
|
866
|
+
encoding: "utf8",
|
|
867
|
+
timeout: 3e3
|
|
868
|
+
});
|
|
869
|
+
const token2 = `--session-id ${state.sessionId}`;
|
|
870
|
+
const rivals = String(listing).split("\n").map((line) => line.trim()).filter(
|
|
871
|
+
(line) => line.includes(token2) && line.includes("--idle") && line.includes("json-events")
|
|
872
|
+
).map((line) => Number(line.split(/\s+/)[0])).filter((p) => Number.isFinite(p) && p > 0 && p !== pid);
|
|
873
|
+
const liveRivals = rivals.filter((rival) => {
|
|
874
|
+
try {
|
|
875
|
+
process.kill(rival, 0);
|
|
876
|
+
return true;
|
|
877
|
+
} catch {
|
|
878
|
+
return false;
|
|
879
|
+
}
|
|
880
|
+
});
|
|
881
|
+
if (liveRivals.length > 0 && liveRivals.some((rival) => rival < pid)) {
|
|
882
|
+
try {
|
|
883
|
+
process.kill(pid, "SIGKILL");
|
|
884
|
+
} catch {
|
|
885
|
+
}
|
|
886
|
+
const winnerPid = Math.min(...liveRivals);
|
|
887
|
+
writeState({ ...state, pid: winnerPid });
|
|
888
|
+
console.log(
|
|
889
|
+
`\u7BA1\u5BB6\u5DF2\u5728\u8FD0\u884C\uFF08\u5E76\u53D1\u542F\u52A8\u6536\u655B\uFF1Apid ${winnerPid}\uFF09\uFF1B\u672C\u6B21\u542F\u52A8\u7684\u591A\u4F59\u5B9E\u4F8B\u5DF2\u7EC8\u6B62\u3002`
|
|
890
|
+
);
|
|
891
|
+
return;
|
|
892
|
+
}
|
|
893
|
+
} catch {
|
|
894
|
+
const winner = readState();
|
|
895
|
+
if (winner && winner.pid && winner.pid !== pid && winner.sessionId === state.sessionId) {
|
|
896
|
+
const liveness = conciergeLiveness(winner.pid, winner);
|
|
897
|
+
if (liveness === "alive" || liveness === "unknown") {
|
|
898
|
+
try {
|
|
899
|
+
process.kill(pid, "SIGKILL");
|
|
900
|
+
} catch {
|
|
901
|
+
}
|
|
902
|
+
writeState({ ...winner });
|
|
903
|
+
console.log(
|
|
904
|
+
`\u65E0\u6CD5\u4EF2\u88C1\u5E76\u53D1\u542F\u52A8\uFF08\u8FDB\u7A0B\u8868\u4E0D\u53EF\u7528\uFF09\uFF0C\u5DF2\u68C0\u6D4B\u5230\u53EF\u80FD\u7684\u8FD0\u884C\u4E2D\u5B9E\u4F8B\uFF08pid ${winner.pid}\uFF0C\u72B6\u6001: ${liveness}\uFF09\uFF1B\u672C\u6B21\u542F\u52A8\u7684\u5B9E\u4F8B\u5DF2\u7EC8\u6B62\uFF0C\u672A\u4EA7\u751F\u53CC\u5F00\u3002`
|
|
905
|
+
);
|
|
906
|
+
process.exitCode = 1;
|
|
907
|
+
return;
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
console.log(`\u542F\u52A8\u4E2D\uFF08pid ${pid}\uFF0Csession ${state.sessionId}\uFF09\u2026`);
|
|
912
|
+
readinessReport = { pid, sinceOffset, sessionId: state.sessionId };
|
|
913
|
+
});
|
|
914
|
+
if (readinessReport) {
|
|
915
|
+
const { pid, sinceOffset, sessionId } = readinessReport;
|
|
916
|
+
const ready = await waitForAgentReady(dir, 3e4, sinceOffset, sessionId);
|
|
917
|
+
if (ready) {
|
|
918
|
+
const now = readState();
|
|
919
|
+
const stillOurs = now?.pid === pid && now.sessionId === sessionId && conciergeLiveness(pid, now) === "alive";
|
|
920
|
+
if (stillOurs) {
|
|
921
|
+
console.log(`\u2713 \u7BA1\u5BB6\u5DF2\u5C31\u7EEA \xB7 \u65E5\u5FD7 ${logPath(dir)}`);
|
|
922
|
+
console.log(
|
|
923
|
+
` \u4E4B\u540E\u76F4\u63A5\u5728\u98DE\u4E66\u5BF9\u8BDD\u5373\u53EF\uFF1B\u505C\u6B62: pie concierge stop\uFF1B\u72B6\u6001: pie concierge status`
|
|
924
|
+
);
|
|
925
|
+
} else {
|
|
926
|
+
console.log(
|
|
927
|
+
`\u26A0 agent.ready \u5DF2\u51FA\u73B0\uFF0C\u4F46\u5F53\u524D\u8BB0\u5F55\u4E0D\u518D\u662F\u672C\u6B21\u542F\u52A8\uFF08pid ${pid} \u53EF\u80FD\u5DF2\u88AB\u505C\u6B62\u6216\u66FF\u6362\uFF09\uFF1B\u7528 pie concierge status \u786E\u8BA4\u3002`
|
|
928
|
+
);
|
|
929
|
+
}
|
|
930
|
+
} else {
|
|
931
|
+
console.log(
|
|
932
|
+
`\u26A0 30 \u79D2\u5185\u672A\u89C1 agent.ready\uFF08pid ${pid}\uFF09\uFF1B\u67E5\u770B\u65E5\u5FD7: ${logPath(dir)}`
|
|
933
|
+
);
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
export {
|
|
938
|
+
runConciergeCommand
|
|
939
|
+
};
|