kovence-cli 1.0.3 → 1.3.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.
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  const { spawn } = require("node:child_process");
4
- const { scrubChildEnv, killProcessGroup } = require("./exec");
4
+ const { scrubChildEnv, killProcessGroup, stripAnsiEscapes } = require("./exec");
5
5
 
6
6
  const MAX_LOG_CHARS = 64000;
7
7
 
@@ -12,31 +12,9 @@ class BackgroundTaskManager {
12
12
  this.onChange = onChange;
13
13
  }
14
14
 
15
- startTask(command, { cwd = process.cwd(), env = null } = {}) {
16
- this.seq += 1;
17
- const id = String(this.seq);
18
- const child = spawn("sh", ["-c", command], {
19
- cwd,
20
- env: env || scrubChildEnv(process.env),
21
- detached: process.platform !== "win32",
22
- stdio: ["ignore", "pipe", "pipe"],
23
- });
24
-
25
- const task = {
26
- id,
27
- pid: child.pid,
28
- command: String(command),
29
- cwd,
30
- startedAt: new Date().toISOString(),
31
- endedAt: null,
32
- status: "running",
33
- exitCode: null,
34
- output: "",
35
- _child: child,
36
- };
37
-
15
+ _attachChild(task, child) {
38
16
  const appendOut = (chunk) => {
39
- task.output += chunk.toString("utf8");
17
+ task.output += stripAnsiEscapes(chunk.toString("utf8"));
40
18
  if (task.output.length > MAX_LOG_CHARS) {
41
19
  task.output = task.output.slice(-MAX_LOG_CHARS);
42
20
  }
@@ -61,7 +39,64 @@ class BackgroundTaskManager {
61
39
  task._child = null;
62
40
  this.onChange?.();
63
41
  });
42
+ }
43
+
44
+ startTask(command, { cwd = process.cwd(), env = null } = {}) {
45
+ this.seq += 1;
46
+ const id = String(this.seq);
47
+ const isWin = process.platform === "win32";
48
+ const shellBin = isWin ? process.env.COMSPEC || "cmd.exe" : "sh";
49
+ const shellArgs = isWin ? ["/d", "/s", "/c", command] : ["-c", command];
50
+ const child = spawn(shellBin, shellArgs, {
51
+ cwd,
52
+ env: env || scrubChildEnv(process.env),
53
+ detached: !isWin,
54
+ stdio: ["ignore", "pipe", "pipe"],
55
+ });
64
56
 
57
+ const task = {
58
+ id,
59
+ pid: child.pid,
60
+ command: String(command),
61
+ cwd,
62
+ startedAt: new Date().toISOString(),
63
+ endedAt: null,
64
+ status: "running",
65
+ exitCode: null,
66
+ output: "",
67
+ _child: child,
68
+ };
69
+
70
+ this._attachChild(task, child);
71
+ this.tasks.set(id, task);
72
+ this.onChange?.();
73
+ return this.serializeTask(task);
74
+ }
75
+
76
+ /**
77
+ * Adopt an already-running child process (e.g. promoted on command timeout).
78
+ */
79
+ adoptProcess(child, { command, cwd = process.cwd(), initialOutput = "" } = {}) {
80
+ this.seq += 1;
81
+ const id = String(this.seq);
82
+ const cleanInitial = stripAnsiEscapes(initialOutput);
83
+ const task = {
84
+ id,
85
+ pid: child.pid,
86
+ command: String(command),
87
+ cwd,
88
+ startedAt: new Date().toISOString(),
89
+ endedAt: null,
90
+ status: child.exitCode !== null ? "exited" : "running",
91
+ exitCode: child.exitCode,
92
+ output: cleanInitial.length > MAX_LOG_CHARS ? cleanInitial.slice(-MAX_LOG_CHARS) : cleanInitial,
93
+ _child: child.exitCode !== null ? null : child,
94
+ };
95
+ if (task._child) {
96
+ this._attachChild(task, child);
97
+ } else {
98
+ task.endedAt = new Date().toISOString();
99
+ }
65
100
  this.tasks.set(id, task);
66
101
  this.onChange?.();
67
102
  return this.serializeTask(task);
@@ -102,9 +137,10 @@ class BackgroundTaskManager {
102
137
  if (!t) return false;
103
138
  if (t.status === "running" && t._child) {
104
139
  t.status = "killed";
105
- killProcessGroup(t._child, "SIGTERM");
140
+ const child = t._child;
141
+ killProcessGroup(child, "SIGTERM");
106
142
  setTimeout(() => {
107
- if (t._child) killProcessGroup(t._child, "SIGKILL");
143
+ if (t._child) killProcessGroup(child, "SIGKILL");
108
144
  }, 300).unref?.();
109
145
  this.onChange?.();
110
146
  return true;
@@ -116,7 +152,11 @@ class BackgroundTaskManager {
116
152
  for (const t of this.tasks.values()) {
117
153
  if (t.status === "running" && t._child) {
118
154
  t.status = "killed";
119
- killProcessGroup(t._child, "SIGTERM");
155
+ const child = t._child;
156
+ killProcessGroup(child, "SIGTERM");
157
+ setTimeout(() => {
158
+ killProcessGroup(child, "SIGKILL");
159
+ }, 200).unref?.();
120
160
  }
121
161
  }
122
162
  }
package/store.js CHANGED
@@ -88,6 +88,8 @@ module.exports = {
88
88
  loadInputHistory: (paths = getDefaultPaths()) => sessionsMod.loadInputHistory(paths),
89
89
  saveInputHistory: (items, paths = getDefaultPaths()) => sessionsMod.saveInputHistory(items, paths),
90
90
  deleteSession: (id, paths = getDefaultPaths()) => sessionsMod.deleteSession(id, paths),
91
+ cleanupOldSessions: (retentionDays, paths = getDefaultPaths(), activeSessionId = "") =>
92
+ sessionsMod.cleanupOldSessions(retentionDays, paths, activeSessionId),
91
93
 
92
94
  // Permissions
93
95
  loadPermissionRules: permStoreMod.loadPermissionRules,