@aliou/pi-processes 0.9.4 → 0.10.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.
Files changed (171) hide show
  1. package/CONTRIBUTING.md +40 -42
  2. package/README.md +54 -84
  3. package/extensions/process-tabs.ts +26 -0
  4. package/extensions/processes/client.ts +194 -0
  5. package/extensions/processes/commands/clear.ts +29 -0
  6. package/extensions/processes/commands/kill.ts +152 -0
  7. package/extensions/processes/commands/overview.ts +102 -0
  8. package/extensions/processes/components/overview-component.ts +729 -0
  9. package/extensions/processes/components/overview-panel.ts +177 -0
  10. package/extensions/processes/config/defaults.ts +28 -0
  11. package/extensions/processes/config/index.ts +13 -0
  12. package/extensions/processes/config/loader.ts +62 -0
  13. package/extensions/processes/config/migrations/000-import-legacy-process-config.ts +113 -0
  14. package/extensions/processes/config/migrations/001-v0-9-4-to-v0-10-0-config.ts +184 -0
  15. package/extensions/processes/config/migrations/002-stamp-v0-10-0-config-version.ts +21 -0
  16. package/extensions/processes/config/migrations/index.ts +25 -0
  17. package/extensions/processes/config/schema.ts +9 -0
  18. package/extensions/processes/config/types.ts +72 -0
  19. package/extensions/processes/constants.ts +1 -0
  20. package/extensions/processes/handlers/commands.ts +113 -0
  21. package/extensions/processes/handlers/kill-process.ts +31 -0
  22. package/extensions/processes/handlers/notifications.ts +147 -0
  23. package/extensions/processes/handlers/requests.ts +114 -0
  24. package/extensions/processes/handlers/subscriptions.ts +144 -0
  25. package/extensions/processes/hooks/background-blocker.ts +201 -0
  26. package/extensions/processes/hooks/cleanup.ts +43 -0
  27. package/extensions/processes/hooks/event-bridge.ts +32 -0
  28. package/extensions/processes/i18n/index.ts +3 -0
  29. package/extensions/processes/i18n/messages.ts +27 -0
  30. package/extensions/processes/i18n/translator.ts +49 -0
  31. package/extensions/processes/index.ts +109 -0
  32. package/extensions/processes/message-renderer.ts +117 -0
  33. package/extensions/processes/notification-sender.ts +43 -0
  34. package/extensions/processes/notifications/classify.ts +27 -0
  35. package/extensions/processes/notifications/log-matchers.ts +146 -0
  36. package/extensions/processes/notifications/registry.ts +225 -0
  37. package/extensions/processes/notifications/render-content.ts +122 -0
  38. package/extensions/processes/notifications/service.ts +336 -0
  39. package/extensions/processes/notifications/types.ts +11 -0
  40. package/extensions/processes/settings/apply-setting-change.ts +30 -0
  41. package/extensions/processes/settings/build-sections.ts +420 -0
  42. package/extensions/processes/settings/index.ts +42 -0
  43. package/extensions/processes/tools/clear/index.ts +21 -0
  44. package/extensions/processes/tools/clear/render.ts +40 -0
  45. package/extensions/processes/tools/components/index.ts +4 -0
  46. package/extensions/processes/tools/components/process-action-header.ts +31 -0
  47. package/extensions/processes/tools/components/process-action-title.ts +17 -0
  48. package/extensions/processes/tools/components/tool-layout.ts +70 -0
  49. package/extensions/processes/tools/components/watch.ts +53 -0
  50. package/extensions/processes/tools/index.ts +265 -0
  51. package/extensions/processes/tools/list/index.ts +157 -0
  52. package/extensions/processes/tools/list/render.ts +211 -0
  53. package/extensions/processes/tools/notify.ts +191 -0
  54. package/extensions/processes/tools/output/index.ts +307 -0
  55. package/extensions/processes/tools/output/render.ts +229 -0
  56. package/extensions/processes/tools/schema.ts +263 -0
  57. package/extensions/processes/tools/start/index.ts +64 -0
  58. package/extensions/processes/tools/start/render.ts +67 -0
  59. package/extensions/processes/tools/stop/index.ts +35 -0
  60. package/extensions/processes/tools/stop/render.ts +43 -0
  61. package/extensions/processes/tools/update/index.ts +320 -0
  62. package/extensions/processes/tools/update/render.ts +251 -0
  63. package/extensions/processes/tools/utils.ts +112 -0
  64. package/extensions/processes/tools/watch-format.ts +14 -0
  65. package/extensions/processes/tools/write/index.ts +75 -0
  66. package/extensions/processes/tools/write/render.ts +68 -0
  67. package/extensions/processes/utils/truncate.ts +285 -0
  68. package/extensions/processes-dock/client.ts +60 -0
  69. package/extensions/processes-dock/commands/dock.ts +70 -0
  70. package/extensions/processes-dock/commands/pin.ts +166 -0
  71. package/extensions/processes-dock/components/log-dock-component.ts +375 -0
  72. package/extensions/processes-dock/dock-state.ts +33 -0
  73. package/extensions/processes-dock/index.ts +28 -0
  74. package/extensions/processes-dock/logs-client.ts +5 -0
  75. package/extensions/processes-dock/widget/setup.ts +520 -0
  76. package/extensions/processes-dock/widget/status.ts +149 -0
  77. package/extensions/processes-dock/widget/types.ts +16 -0
  78. package/extensions/processes-logs/client.ts +51 -0
  79. package/extensions/processes-logs/commands/logs.ts +108 -0
  80. package/extensions/processes-logs/completions.ts +31 -0
  81. package/extensions/processes-logs/components/log-file-viewer.ts +274 -0
  82. package/extensions/processes-logs/components/log-overlay-component.ts +685 -0
  83. package/extensions/processes-logs/index.ts +27 -0
  84. package/extensions/processes-logs/logs-client.ts +101 -0
  85. package/extensions/shared/line-buffer.ts +35 -0
  86. package/extensions/shared/output-payload.ts +28 -0
  87. package/extensions/shared/ui.ts +146 -0
  88. package/package.json +47 -20
  89. package/schema.json +134 -0
  90. package/skills/pi-processes/SKILL.md +496 -25
  91. package/src/get-manager.ts +15 -0
  92. package/src/manager/index.ts +182 -0
  93. package/src/manager/internal-types.ts +130 -0
  94. package/src/manager/limits.ts +15 -0
  95. package/src/manager/process-log-store.ts +422 -0
  96. package/src/manager/process-output.ts +345 -0
  97. package/src/manager/process-registry.ts +78 -0
  98. package/src/manager/process-runtime-controller.ts +499 -0
  99. package/src/protocol/broadcasts.ts +14 -0
  100. package/src/protocol/channels.ts +30 -0
  101. package/src/protocol/commands.ts +24 -0
  102. package/src/protocol/index.ts +34 -0
  103. package/src/protocol/logs.ts +23 -0
  104. package/src/protocol/notifications.ts +48 -0
  105. package/src/protocol/requests.ts +76 -0
  106. package/src/types.ts +66 -0
  107. package/src/utils/ansi.ts +2 -4
  108. package/src/utils/format.ts +24 -32
  109. package/src/utils/index.ts +5 -1
  110. package/src/utils/is-record.ts +3 -0
  111. package/src/utils/match-line.ts +32 -0
  112. package/src/utils/platform.ts +13 -0
  113. package/src/utils/shorten-path.ts +11 -0
  114. package/src/utils/signals.ts +22 -0
  115. package/src/commands/clear/command.ts +0 -14
  116. package/src/commands/clear/index.ts +0 -1
  117. package/src/commands/completions.ts +0 -38
  118. package/src/commands/dock/command.ts +0 -29
  119. package/src/commands/dock/index.ts +0 -1
  120. package/src/commands/index.ts +0 -33
  121. package/src/commands/kill/command.ts +0 -69
  122. package/src/commands/kill/index.ts +0 -1
  123. package/src/commands/logs/command.ts +0 -47
  124. package/src/commands/logs/index.ts +0 -1
  125. package/src/commands/pick-process.ts +0 -34
  126. package/src/commands/pin/command.ts +0 -34
  127. package/src/commands/pin/index.ts +0 -1
  128. package/src/commands/processes/command.ts +0 -39
  129. package/src/commands/processes/index.ts +0 -1
  130. package/src/commands/settings/apply-setting-change.ts +0 -72
  131. package/src/commands/settings/build-sections.ts +0 -160
  132. package/src/commands/settings/command.ts +0 -20
  133. package/src/commands/settings/index.ts +0 -1
  134. package/src/components/log-dock-component.test.ts +0 -29
  135. package/src/components/log-dock-component.ts +0 -236
  136. package/src/components/log-file-viewer.ts +0 -317
  137. package/src/components/log-overlay-component.ts +0 -546
  138. package/src/components/panel-helpers.ts +0 -37
  139. package/src/components/process-picker-component.ts +0 -166
  140. package/src/components/processes-component.ts +0 -485
  141. package/src/components/status-format.ts +0 -38
  142. package/src/config.ts +0 -107
  143. package/src/constants/index.ts +0 -16
  144. package/src/constants/types.ts +0 -106
  145. package/src/hooks/background-blocker.ts +0 -66
  146. package/src/hooks/cleanup.ts +0 -10
  147. package/src/hooks/index.ts +0 -32
  148. package/src/hooks/message-renderer.ts +0 -115
  149. package/src/hooks/process-end.ts +0 -69
  150. package/src/hooks/process-watch.ts +0 -85
  151. package/src/hooks/utils.ts +0 -23
  152. package/src/hooks/widget/index.ts +0 -2
  153. package/src/hooks/widget/setup.ts +0 -167
  154. package/src/hooks/widget/status-widget.ts +0 -105
  155. package/src/hooks/widget/types.ts +0 -20
  156. package/src/index.ts +0 -35
  157. package/src/manager.test.ts +0 -331
  158. package/src/manager.ts +0 -758
  159. package/src/tools/actions/clear.ts +0 -20
  160. package/src/tools/actions/debug.ts +0 -148
  161. package/src/tools/actions/index.ts +0 -171
  162. package/src/tools/actions/kill.ts +0 -89
  163. package/src/tools/actions/list.ts +0 -188
  164. package/src/tools/actions/logs.ts +0 -117
  165. package/src/tools/actions/output.ts +0 -269
  166. package/src/tools/actions/start.ts +0 -245
  167. package/src/tools/actions/write.ts +0 -113
  168. package/src/tools/index.ts +0 -207
  169. package/src/utils/ansi.test.ts +0 -22
  170. package/src/utils/command-executor.test.ts +0 -48
  171. package/src/utils/keybindings.ts +0 -71
@@ -0,0 +1,182 @@
1
+ import { EventEmitter } from "node:events";
2
+
3
+ import type {
4
+ KillResult,
5
+ ManagerEvent,
6
+ ProcessInfo,
7
+ WriteResult,
8
+ } from "../types";
9
+ import { formatProcess } from "./internal-types";
10
+ import { ProcessLogStore } from "./process-log-store";
11
+ import { ProcessOutput } from "./process-output";
12
+ import { ProcessRegistry } from "./process-registry";
13
+ import { ProcessRuntimeController } from "./process-runtime-controller";
14
+
15
+ interface ProcessManagerOptions {
16
+ getConfiguredShellPath?: () => string | undefined;
17
+ }
18
+
19
+ export class ProcessManager {
20
+ private events = new EventEmitter();
21
+
22
+ private registry: ProcessRegistry;
23
+ private logStore: ProcessLogStore;
24
+ private output: ProcessOutput;
25
+ private runtime: ProcessRuntimeController;
26
+
27
+ constructor(options?: ProcessManagerOptions) {
28
+ const emit = (event: ManagerEvent): void => {
29
+ this.events.emit("event", event);
30
+ };
31
+
32
+ this.registry = new ProcessRegistry();
33
+ this.logStore = new ProcessLogStore();
34
+
35
+ this.output = new ProcessOutput({
36
+ emit,
37
+ logStore: this.logStore,
38
+ });
39
+
40
+ this.runtime = new ProcessRuntimeController({
41
+ registry: this.registry,
42
+ logs: this.logStore,
43
+ output: this.output,
44
+ emit,
45
+ getConfiguredShellPath:
46
+ options?.getConfiguredShellPath ?? (() => undefined),
47
+ });
48
+ }
49
+
50
+ onEvent(listener: (event: ManagerEvent) => void): () => void {
51
+ this.events.on("event", listener);
52
+ return () => this.events.off("event", listener);
53
+ }
54
+
55
+ start(name: string, command: string, cwd: string): ProcessInfo {
56
+ const managed = this.runtime.start(name, command, cwd);
57
+ return formatProcess(managed);
58
+ }
59
+
60
+ list(): ProcessInfo[] {
61
+ return this.registry.list();
62
+ }
63
+
64
+ get(id: string): ProcessInfo | null {
65
+ return this.registry.getPublicInfo(id);
66
+ }
67
+
68
+ rename(id: string, name: string): ProcessInfo | null {
69
+ const info = this.registry.rename(id, name);
70
+ if (info) {
71
+ this.events.emit("event", {
72
+ type: "processes_changed",
73
+ } satisfies ManagerEvent);
74
+ }
75
+ return info;
76
+ }
77
+
78
+ getOutput(
79
+ id: string,
80
+ tailLines = 100,
81
+ ): { stdout: string[]; stderr: string[]; status: string } | null {
82
+ const managed = this.registry.getRecord(id);
83
+ if (!managed) return null;
84
+
85
+ return {
86
+ stdout: this.logStore.readTailLines(managed.stdoutFile, tailLines),
87
+ stderr: this.logStore.readTailLines(managed.stderrFile, tailLines),
88
+ status: managed.status,
89
+ };
90
+ }
91
+
92
+ getCombinedOutput(
93
+ id: string,
94
+ tailLines = 100,
95
+ ): Array<{ type: "stdout" | "stderr"; text: string }> | null {
96
+ const managed = this.registry.getRecord(id);
97
+ if (!managed) return null;
98
+ return this.logStore.getCombinedOutput(managed.combinedFile, tailLines);
99
+ }
100
+
101
+ /**
102
+ * Return output capped at 16 MiB per stream. Use getLogFiles() and stream
103
+ * the returned paths when an exact, unbounded read is required.
104
+ */
105
+ getFullOutput(id: string): { stdout: string; stderr: string } | null {
106
+ const managed = this.registry.getRecord(id);
107
+ if (!managed) return null;
108
+ return {
109
+ stdout: this.logStore.readFullFile(managed.stdoutFile),
110
+ stderr: this.logStore.readFullFile(managed.stderrFile),
111
+ };
112
+ }
113
+
114
+ getLogFiles(
115
+ id: string,
116
+ ): { stdoutFile: string; stderrFile: string; combinedFile: string } | null {
117
+ const managed = this.registry.getRecord(id);
118
+ if (!managed) return null;
119
+ return {
120
+ stdoutFile: managed.stdoutFile,
121
+ stderrFile: managed.stderrFile,
122
+ combinedFile: managed.combinedFile,
123
+ };
124
+ }
125
+
126
+ getFileSize(id: string): { stdout: number; stderr: number } | null {
127
+ const managed = this.registry.getRecord(id);
128
+ if (!managed) return null;
129
+ return this.logStore.getFileSize({
130
+ stdoutFile: managed.stdoutFile,
131
+ stderrFile: managed.stderrFile,
132
+ combinedFile: managed.combinedFile,
133
+ });
134
+ }
135
+
136
+ async kill(
137
+ id: string,
138
+ opts?: { signal?: NodeJS.Signals; timeoutMs?: number },
139
+ ): Promise<KillResult> {
140
+ return this.runtime.kill(id, opts);
141
+ }
142
+
143
+ writeToStdin(
144
+ id: string,
145
+ data: string,
146
+ opts?: { end?: boolean },
147
+ ): WriteResult {
148
+ return this.runtime.writeToStdin(id, data, opts);
149
+ }
150
+
151
+ killAll(): void {
152
+ this.runtime.killAll();
153
+ }
154
+
155
+ clearFinished(): number {
156
+ return this.runtime.clearFinished();
157
+ }
158
+
159
+ stopWatcher(): void {
160
+ this.runtime.stopWatcher();
161
+ }
162
+
163
+ cleanup(): void {
164
+ this.runtime.beginShutdown();
165
+ this.runtime.stopWatcher();
166
+ this.output.clearAll();
167
+ this.runtime.killAllLive();
168
+ this.logStore.cleanup();
169
+ }
170
+
171
+ [Symbol.dispose](): void {
172
+ this.cleanup();
173
+ }
174
+ }
175
+
176
+ export type {
177
+ KillResult,
178
+ ManagerEvent,
179
+ ProcessInfo,
180
+ ProcessStatus,
181
+ WriteResult,
182
+ } from "../types";
@@ -0,0 +1,130 @@
1
+ import type { Writable } from "node:stream";
2
+
3
+ import type {
4
+ ProcessEndReason,
5
+ ProcessInfo,
6
+ ProcessSignalInfo,
7
+ ProcessStatus,
8
+ } from "../types";
9
+
10
+ export interface ProcessLogPaths {
11
+ stdoutFile: string;
12
+ stderrFile: string;
13
+ combinedFile: string;
14
+ }
15
+
16
+ /**
17
+ * Stable process metadata and lifecycle status.
18
+ *
19
+ * This is the part of a managed process that can be safely projected into the
20
+ * public `ProcessInfo` API. It intentionally contains no Node handles, mutable
21
+ * stream buffers, or manager bookkeeping fields.
22
+ */
23
+ interface ProcessPublicState {
24
+ id: string;
25
+ name: string;
26
+ pid: number;
27
+ command: string;
28
+ cwd: string;
29
+ startTime: number;
30
+ endTime: number | null;
31
+ status: ProcessStatus;
32
+ exitCode: number | null;
33
+ success: boolean | null;
34
+ stdoutFile: string;
35
+ stderrFile: string;
36
+ endReason: ProcessEndReason | null;
37
+ signal: ProcessSignalInfo | null;
38
+ errorMessage: string | null;
39
+ }
40
+
41
+ /**
42
+ * Process handles and signal bookkeeping used by runtime control.
43
+ *
44
+ * Only `ProcessRuntimeController` should need these fields. They are not safe
45
+ * to expose because they allow direct mutation/control outside manager methods.
46
+ */
47
+ interface ProcessRuntimeState {
48
+ stdin: Writable | null;
49
+ stdinClosed: boolean;
50
+ lastSignalSent: NodeJS.Signals | null;
51
+ completionSequence: number | null;
52
+ }
53
+
54
+ /**
55
+ * Internal log storage details.
56
+ *
57
+ * `stdoutFile` and `stderrFile` are public because agents/users can inspect
58
+ * them. The combined log file is an implementation detail used by manager/UI
59
+ * read APIs, so it stays internal.
60
+ */
61
+ interface ProcessLogState {
62
+ combinedFile: string;
63
+ }
64
+
65
+ /**
66
+ * Output parser state for incomplete line chunks.
67
+ *
68
+ * Node streams can split a single logical line across multiple chunks. These
69
+ * buffers let `ProcessOutput` emit only completed lines in events/logs.
70
+ */
71
+ interface ProcessLineBufferState {
72
+ stdoutPendingLine: Buffer;
73
+ stderrPendingLine: Buffer;
74
+ /** The line head was emitted; discard the tail through the next newline. */
75
+ stdoutLineOverflowed: boolean;
76
+ /** The line head was emitted; discard the tail through the next newline. */
77
+ stderrLineOverflowed: boolean;
78
+ }
79
+
80
+ /**
81
+ * Output lines accumulated since the last `process_output_changed` event.
82
+ *
83
+ * `ProcessOutput` drains this buffer when it emits an output event. This
84
+ * gives extension code live lines without re-reading log files or polling.
85
+ */
86
+ interface ProcessOutputEventBufferState {
87
+ appendedLines: Array<{ type: "stdout" | "stderr"; text: string }>;
88
+ droppedLineCount: number;
89
+ }
90
+
91
+ /**
92
+ * Internal mutable record owned by `ProcessManager`.
93
+ *
94
+ * This is intentionally broader than public `ProcessInfo`: it combines public
95
+ * lifecycle state with runtime handles, log implementation details, and output
96
+ * parser buffers. Callers must receive `ProcessInfo` snapshots instead, created
97
+ * by `formatProcess()` below.
98
+ */
99
+ export interface ManagedProcessRecord
100
+ extends ProcessPublicState,
101
+ ProcessRuntimeState,
102
+ ProcessLogState,
103
+ ProcessLineBufferState,
104
+ ProcessOutputEventBufferState {}
105
+
106
+ /**
107
+ * Convert an internal mutable record into the public process snapshot.
108
+ *
109
+ * Keep this function explicit instead of using object spread so new internal
110
+ * fields cannot accidentally leak into the public manager API.
111
+ */
112
+ export function formatProcess(record: ManagedProcessRecord): ProcessInfo {
113
+ return {
114
+ id: record.id,
115
+ name: record.name,
116
+ pid: record.pid,
117
+ command: record.command,
118
+ cwd: record.cwd,
119
+ startTime: record.startTime,
120
+ endTime: record.endTime,
121
+ status: record.status,
122
+ exitCode: record.exitCode,
123
+ success: record.success,
124
+ stdoutFile: record.stdoutFile,
125
+ stderrFile: record.stderrFile,
126
+ endReason: record.endReason,
127
+ signal: record.signal,
128
+ errorMessage: record.errorMessage,
129
+ };
130
+ }
@@ -0,0 +1,15 @@
1
+ /** Max bytes retained for an unterminated line before force-flush. */
2
+ export const MAX_PENDING_LINE_BYTES = 64 * 1024;
3
+ /** Max bytes retained for any single line in an event payload. */
4
+ export const MAX_LINE_BYTES = 32 * 1024;
5
+ /** Max lines carried in one process_output_changed payload. */
6
+ export const MAX_LINES_PER_EMIT = 2000;
7
+ /** Max bytes read when tailing a log file. */
8
+ export const MAX_TAIL_READ_BYTES = 2 * 1024 * 1024;
9
+ /** Max bytes retained per log file before truncate-and-restart. */
10
+ export const MAX_LOG_FILE_BYTES = 64 * 1024 * 1024;
11
+ export const TRUNCATION_SUFFIX = " … [line truncated]";
12
+ /** Max finished process records retained after the grace period. */
13
+ export const MAX_FINISHED_RECORDS = 50;
14
+ /** Keep recently finished records available to the UI before reaping. */
15
+ export const FINISHED_RECORD_GRACE_MS = 5 * 60 * 1000;