@cydm/pie 4.6.1 → 4.7.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.
@@ -84,6 +84,45 @@ function parseProcessCommand(job) {
84
84
  function isProcessJob(job) {
85
85
  return job.sessionId === PROCESS_SESSION_ID && parseProcessCommand(job) !== null;
86
86
  }
87
+ function sweepStaleOwners(state, dir, opts = {}) {
88
+ const now = opts.now ?? Date.now();
89
+ const legacyOrCrashWindowMs = 60 * 60 * 1e3;
90
+ const reaped = [];
91
+ for (const key of Object.keys(state.owners)) {
92
+ if (key === opts.keepKey) continue;
93
+ const row = state.owners[key];
94
+ if (opts.sessionId !== void 0 && row.sessionId !== opts.sessionId) continue;
95
+ const liveness = path.join(dir, "owners-liveness", `${key}.json`);
96
+ let sidecar;
97
+ try {
98
+ sidecar = fs.readFileSync(liveness, "utf8");
99
+ } catch {
100
+ }
101
+ if (sidecar === void 0) {
102
+ if (row.registeredAt === void 0 || now - row.registeredAt > legacyOrCrashWindowMs) {
103
+ reaped.push({ key, sessionId: row.sessionId, reason: row.registeredAt === void 0 ? "legacy row (no sidecar, no registeredAt)" : "sidecar missing past crash window" });
104
+ delete state.owners[key];
105
+ }
106
+ continue;
107
+ }
108
+ try {
109
+ const parsed = JSON.parse(sidecar);
110
+ if (typeof parsed.pid !== "number" || parsed.pid <= 0) continue;
111
+ let dead = false;
112
+ try {
113
+ process.kill(parsed.pid, 0);
114
+ } catch (error) {
115
+ dead = error.code === "ESRCH";
116
+ }
117
+ if (dead) {
118
+ reaped.push({ key, sessionId: row.sessionId, reason: `pid ${parsed.pid} provably dead` });
119
+ delete state.owners[key];
120
+ }
121
+ } catch {
122
+ }
123
+ }
124
+ return reaped;
125
+ }
87
126
  var Store = class {
88
127
  constructor(dir) {
89
128
  this.dir = dir;
@@ -1000,25 +1039,8 @@ var Runtime = class {
1000
1039
  }
1001
1040
  persistOwner(sessionId) {
1002
1041
  this.store.transaction((state) => {
1003
- for (const key of Object.keys(state.owners)) {
1004
- if (key === this.id) continue;
1005
- const row = state.owners[key];
1006
- if (row.sessionId !== sessionId) continue;
1007
- const liveness = path3.join(this.store.dir, "owners-liveness", `${key}.json`);
1008
- try {
1009
- const parsed = JSON.parse(fs3.readFileSync(liveness, "utf8"));
1010
- if (typeof parsed.pid !== "number" || parsed.pid <= 0) continue;
1011
- try {
1012
- process.kill(parsed.pid, 0);
1013
- continue;
1014
- } catch (err) {
1015
- if (err.code !== "ESRCH") continue;
1016
- }
1017
- delete state.owners[key];
1018
- } catch {
1019
- }
1020
- }
1021
- state.owners[this.id] = { sessionId, cwd: this.cwd, busy: !this.ctx.isIdle() };
1042
+ sweepStaleOwners(state, this.store.dir, { sessionId, keepKey: this.id });
1043
+ state.owners[this.id] = { sessionId, cwd: this.cwd, busy: !this.ctx.isIdle(), registeredAt: Date.now() };
1022
1044
  });
1023
1045
  try {
1024
1046
  const livenessDir = path3.join(this.store.dir, "owners-liveness");
@@ -1592,7 +1614,12 @@ WARNING: change applied but the audit log could not be written (${String(e)})`;
1592
1614
  const note = auditNote({ kind: action, jobId, sessionId: job?.sessionId, cwd: job?.cwd, authorization });
1593
1615
  return `${action}: ${jobId}${note}`;
1594
1616
  }
1595
- throw new Error('Usage: /tools/cron list | add {"prompt":"...","schedule":{"every":3600}} | pause|resume|remove <id>');
1617
+ if (action === "reap-owners") {
1618
+ const reaped = store.transaction((state) => sweepStaleOwners(state, store.dir, {}));
1619
+ const note = auditNote({ kind: "remove", detail: `reap-owners: ${reaped.length} owner row(s) reaped`, authorization: authorizationNow() });
1620
+ return JSON.stringify({ reaped, total: reaped.length }, null, 2) + note;
1621
+ }
1622
+ throw new Error('Usage: /tools/cron list | reap-owners | add {"prompt":"...","schedule":{"every":3600}} | pause|resume|remove <id>');
1596
1623
  };
1597
1624
  ctx.registerCommand({ path: ["tools", "cron"], description: "Persistent same-session schedules (UTC): list/add/pause/resume/remove", handler: async (_ctx, args) => {
1598
1625
  try {
@@ -287,6 +287,48 @@ path no longer truncates bodies.
287
287
  text reply can overtake an earlier multi-image download. Wait for acceptance
288
288
  before sending a follow-up that depends on those images.
289
289
 
290
+ ## Panel actions (script-backed custom buttons)
291
+
292
+ 11-23, user-settled design. A custom panel button is the trigger for a
293
+ PRE-WRITTEN script (capability sedimentation): the LLM participates exactly
294
+ once — when a group member asks the manager for a repeatable capability and a
295
+ worker develops + tests the script — and every later click is deterministic
296
+ execution (zero LLM, zero token spend).
297
+
298
+ - Registry: `<configRoot>/panel-actions/registry.json` (atomic tmp+rename
299
+ writes; a corrupt file surfaces as an error and is never silently wiped).
300
+ Scripts, per-run full logs and per-action execution locks live beside it
301
+ (`logs/`, `locks/`).
302
+ - Scopes: `manager` (main /panel card), `worker` (EVERY worker card — the
303
+ button payload carries the clicked worker's name+dir, so one generic
304
+ definition runs with any worker's context), `worker:<name>` (pinned).
305
+ - Script contract: interpreter by extension (.mjs/.js→node, .sh→bash,
306
+ .py→python3, otherwise shebang+exec bit); context arrives as env
307
+ (`PIE_ACTION_ID`, `PIE_ACTION_TRIGGER_NAME/OPEN_ID`, `PIE_ACTION_CHAT_ID`,
308
+ `PIE_ACTION_PAYLOAD` (JSON), `PIE_ACTION_WORKER`, `PIE_ACTION_WORKER_DIR`)
309
+ over a deliberately minimal environment (manager bootstrap/model identity
310
+ never leaks into scripts); stdout IS the result (markdown posted to IM);
311
+ exit 0 = success, non-zero = failure with the stderr tail; per-action
312
+ timeout (default 5 min) kills the whole process tree.
313
+ - Execution: the callback acks fast (<500ms contract), runs in the
314
+ background, then patches the SAME ack card with the result (one click, one
315
+ card). The per-action execution lock is a cross-process mkdir + link-CAS
316
+ protocol (the pie-proc registry's primitive): write-once info.json,
317
+ token-merged renew.json (deadline re-arm + childPid fencing record),
318
+ generation-checked release/rearm, and rename-aside reaping with
319
+ last-moment revalidation. A reaper kills the recorded child tree BEFORE
320
+ freeing the slot (argv-marker verified; leader-absent/group-alive and any
321
+ unverifiable identity read as UNKNOWN and keep the slot with an operator
322
+ recovery note — fail closed, never overlap two runs).
323
+ - Governance is attribution, not permission (user decision): the registry
324
+ records the creator, every result card bylines the triggerer ("由 张三 触
325
+ 发"), and every run appends to `audit.jsonl`. There is deliberately NO
326
+ allowUsers gate on clicking; the hard limits are the execution lock and the
327
+ timeout (resource protection), plus honest degradation (`logDegraded`).
328
+ - The `panel_action` management tool (add/remove/list) registers on the
329
+ manager host only (`PIE_PROC_CURATED_NAME=manager`): the manager's panel
330
+ serves the registry, so only the manager may write it.
331
+
290
332
  ## Extension host API
291
333
 
292
334
  `ctx.beginUserInput(isCurrent?)` is interactive-only and unavailable until the