@kabmo-labs/kabmo 0.15.0 → 0.17.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 (177) hide show
  1. package/README.md +3 -2
  2. package/dist/client/index.html +1 -1
  3. package/dist/client/index.js +412 -394
  4. package/dist/client/static/js/index-3ve31rzx.css +1 -0
  5. package/dist/client/static/js/{index-mx658m43.js → index-rerjq5th.js} +846 -844
  6. package/marketplace/plugins/stats/README.md +27 -8
  7. package/marketplace/plugins/stats/package.json +2 -2
  8. package/marketplace/plugins/stats/src/HeaderReadout.test.tsx +2 -1
  9. package/marketplace/plugins/stats/src/HeaderReadout.tsx +6 -2
  10. package/marketplace/plugins/stats/src/KillConfirm.test.tsx +159 -0
  11. package/marketplace/plugins/stats/src/KillConfirm.tsx +145 -0
  12. package/marketplace/plugins/stats/src/ProcessTable.tsx +54 -8
  13. package/marketplace/plugins/stats/src/TotalsBar.test.tsx +5 -2
  14. package/marketplace/plugins/stats/src/TotalsBar.tsx +13 -3
  15. package/marketplace/plugins/stats/src/app.test.tsx +100 -4
  16. package/marketplace/plugins/stats/src/app.tsx +51 -4
  17. package/marketplace/plugins/stats/src/groups.test.ts +1 -1
  18. package/marketplace/plugins/stats/src/kill.test.ts +131 -0
  19. package/marketplace/plugins/stats/src/kill.ts +74 -0
  20. package/marketplace/plugins/stats/src/labels.test.ts +1 -0
  21. package/marketplace/plugins/stats/src/machine.test.ts +77 -0
  22. package/marketplace/plugins/stats/src/machine.ts +115 -0
  23. package/marketplace/plugins/stats/src/proc/darwin.ts +38 -0
  24. package/marketplace/plugins/stats/src/proc/linux.ts +4 -0
  25. package/marketplace/plugins/stats/src/proc/ps.ts +1 -0
  26. package/marketplace/plugins/stats/src/proc/types.ts +11 -0
  27. package/marketplace/plugins/stats/src/sampler.test.ts +42 -4
  28. package/marketplace/plugins/stats/src/sampler.ts +32 -18
  29. package/marketplace/plugins/stats/src/server.ts +29 -2
  30. package/marketplace/plugins/stats/src/snapshot.ts +31 -3
  31. package/package.json +3 -2
  32. package/src/client/components/common/Modal.tsx +7 -0
  33. package/src/client/components/common/code-surface/bracket-guide.test.ts +2 -2
  34. package/src/client/components/common/code-surface/bracket-guide.ts +2 -2
  35. package/src/client/components/common/code-surface/conflict-markers.ts +5 -5
  36. package/src/client/components/common/code-surface/extensions.ts +7 -4
  37. package/src/client/components/common/code-surface/theme.ts +34 -9
  38. package/src/client/components/common/code-surface/whitespace.test.ts +223 -0
  39. package/src/client/components/common/code-surface/whitespace.ts +140 -0
  40. package/src/client/components/common/{server-switcher/index.tsx → server-control/Panel.tsx} +73 -35
  41. package/src/client/components/common/{server-switcher → server-control}/index.test.tsx +98 -37
  42. package/src/client/components/common/server-control/index.tsx +118 -0
  43. package/src/client/components/layout/LeftDock.tsx +1 -0
  44. package/src/client/components/layout/desktop-layout/TopNavbar.tsx +8 -50
  45. package/src/client/components/layout/desktop-layout/WorkspacePanels.tsx +1 -0
  46. package/src/client/components/mobile/RightSidebar.tsx +1 -0
  47. package/src/client/components/mobile/mobile-main-view/Header.tsx +6 -12
  48. package/src/client/components/mobile/mobile-session-sidebar/Header.tsx +41 -13
  49. package/src/client/components/mobile/mobile-session-sidebar/use-dock.ts +1 -0
  50. package/src/client/components/settings/Modal.tsx +9 -3
  51. package/src/client/components/settings/ProjectSelectorDropdown.tsx +19 -9
  52. package/src/client/components/settings/categories/AgentSettings.tsx +27 -6
  53. package/src/client/components/settings/categories/ProjectSettings.tsx +122 -156
  54. package/src/client/components/settings/categories/SkillCatalogSettings.tsx +220 -260
  55. package/src/client/components/settings/categories/SkillSettings.tsx +6 -3
  56. package/src/client/components/settings/categories/agent-settings/SidebarList.tsx +5 -0
  57. package/src/client/components/settings/categories/project-settings/AccentPalette.test.tsx +189 -0
  58. package/src/client/components/settings/categories/project-settings/AccentPalette.tsx +156 -24
  59. package/src/client/components/settings/categories/project-settings/AddProjectModal.tsx +175 -0
  60. package/src/client/components/settings/categories/project-settings/DeleteProjectModal.tsx +107 -0
  61. package/src/client/components/settings/categories/project-settings/DetailsForm.tsx +40 -24
  62. package/src/client/components/settings/categories/project-settings/DetailsHeader.tsx +17 -7
  63. package/src/client/components/settings/categories/project-settings/IconGrid.tsx +164 -69
  64. package/src/client/components/settings/categories/project-settings/ModelDropdown.test.tsx +122 -0
  65. package/src/client/components/settings/categories/project-settings/ModelDropdown.tsx +124 -53
  66. package/src/client/components/settings/categories/project-settings/NameInput.tsx +57 -11
  67. package/src/client/components/settings/categories/project-settings/PathField.tsx +122 -0
  68. package/src/client/components/settings/categories/project-settings/SidebarList.tsx +100 -59
  69. package/src/client/components/settings/categories/skill-catalog/GitImportPanel.tsx +253 -0
  70. package/src/client/components/settings/categories/skill-catalog/SkillCard.tsx +89 -0
  71. package/src/client/components/settings/categories/skill-catalog/SourceRail.tsx +122 -0
  72. package/src/client/components/settings/categories/skill-catalog/catalog-ui.test.tsx +179 -0
  73. package/src/client/components/settings/categories/skill-settings/DetailHeader.tsx +208 -0
  74. package/src/client/components/settings/categories/skill-settings/DetailPane.tsx +137 -232
  75. package/src/client/components/settings/categories/skill-settings/InstructionsEditor.tsx +84 -0
  76. package/src/client/components/settings/categories/skill-settings/SidebarList.tsx +123 -87
  77. package/src/client/components/settings/categories/skill-settings/pane.test.tsx +179 -0
  78. package/src/client/components/settings/categories/skill-settings/row-meta.ts +58 -0
  79. package/src/client/components/workspace/chat-timeline/index.tsx +6 -1
  80. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Proc.test.ts +46 -0
  81. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Proc.tsx +15 -6
  82. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Wait.test.ts +29 -0
  83. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Wait.tsx +11 -3
  84. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/card-header.test.ts +24 -1
  85. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/card-header.ts +6 -2
  86. package/src/client/components/workspace/editor/viewers/SvgPreview.tsx +18 -2
  87. package/src/client/components/workspace/editor/viewers/viewers.test.ts +39 -0
  88. package/src/client/components/workspace/file-explorer/ContextMenu.tsx +18 -5
  89. package/src/client/components/workspace/file-explorer/Modals.tsx +15 -1
  90. package/src/client/components/workspace/file-explorer/RowLabel.tsx +68 -0
  91. package/src/client/components/workspace/file-explorer/TreeItem.tsx +47 -57
  92. package/src/client/components/workspace/file-explorer/collapsed-chain.test.ts +167 -0
  93. package/src/client/components/workspace/file-explorer/drag-props.ts +42 -0
  94. package/src/client/components/workspace/file-explorer/drag.test.ts +43 -0
  95. package/src/client/components/workspace/file-explorer/index.tsx +21 -20
  96. package/src/client/components/workspace/model-dropdown/Panel.tsx +7 -2
  97. package/src/client/components/workspace/model-dropdown/Section.test.tsx +13 -0
  98. package/src/client/components/workspace/model-dropdown/Section.tsx +5 -1
  99. package/src/client/components/workspace/panels/builtin.tsx +7 -0
  100. package/src/client/components/workspace/wiki-panel/NavModal.test.tsx +136 -0
  101. package/src/client/components/workspace/wiki-panel/NavModal.tsx +94 -0
  102. package/src/client/components/workspace/wiki-panel/PageView.tsx +16 -10
  103. package/src/client/components/workspace/wiki-panel/index.tsx +44 -9
  104. package/src/client/data/settings/omp-schema.json +91 -4
  105. package/src/client/data/settings/project.ts +9 -28
  106. package/src/client/data/settings/skill.ts +6 -32
  107. package/src/client/hooks/chat/timeline/pending-model.ts +28 -0
  108. package/src/client/hooks/settings/projects/index.ts +294 -0
  109. package/src/client/hooks/settings/projects.test.ts +237 -0
  110. package/src/client/hooks/settings/skill-catalog.ts +101 -9
  111. package/src/client/hooks/settings/skill-git-import.ts +153 -0
  112. package/src/client/hooks/shell/servers.test.ts +166 -0
  113. package/src/client/hooks/{desktop → shell}/servers.ts +17 -14
  114. package/src/client/hooks/ui/updates/shell-download.ts +1 -1
  115. package/src/client/hooks/ui/updates/shell-update.ts +2 -6
  116. package/src/client/hooks/workspace/file-row-drag.ts +41 -0
  117. package/src/client/hooks/workspace/file-tree-actions.ts +25 -1
  118. package/src/client/hooks/workspace/file-tree-edits.ts +23 -6
  119. package/src/client/hooks/workspace/root-drop-target.ts +77 -0
  120. package/src/client/hooks/workspace/unfolded-chains.ts +165 -0
  121. package/src/client/lib/desktop/dropped-paths.ts +1 -1
  122. package/src/client/lib/desktop/titlebar.test.ts +12 -3
  123. package/src/client/lib/desktop/titlebar.ts +1 -1
  124. package/src/client/lib/shell/desktop.ts +117 -0
  125. package/src/client/lib/shell/servers.ts +75 -0
  126. package/src/client/lib/shell/transport.ts +216 -0
  127. package/src/client/main.tsx +9 -0
  128. package/src/server/lib/db/schema.ts +11 -0
  129. package/src/server/lib/fs/git-merge-conflict.test.ts +14 -1
  130. package/src/server/lib/lifecycle/memory.test.ts +41 -1
  131. package/src/server/lib/lifecycle/memory.ts +131 -14
  132. package/src/server/lib/omp/config/skill-sources.ts +49 -0
  133. package/src/server/lib/omp/config/skills-git-install.ts +155 -0
  134. package/src/server/lib/omp/config/skills-git-repo.ts +288 -0
  135. package/src/server/lib/omp/config/skills-git.test.ts +220 -0
  136. package/src/server/lib/omp/session/subagent-presence-scan.test.ts +137 -0
  137. package/src/server/lib/omp/session/subagent-presence-scan.ts +120 -0
  138. package/src/server/lib/omp/session/subagent-presence-store.test.ts +120 -0
  139. package/src/server/lib/omp/session/subagent-presence-store.ts +65 -0
  140. package/src/server/lib/omp/session/subagent-presence.ts +52 -15
  141. package/src/server/lib/workspace/project-model.server.test.ts +102 -0
  142. package/src/server/lib/workspace/project-model.server.ts +47 -0
  143. package/src/server/lib/workspace/project-patch.server.ts +156 -0
  144. package/src/server/routes/agent/new.ts +8 -1
  145. package/src/server/routes/settings/agents.ts +19 -7
  146. package/src/server/routes/settings/projects.ts +53 -19
  147. package/src/server/routes/settings/settings-projects-routes.test.ts +214 -0
  148. package/src/server/routes/settings/settings-store-routes.test.ts +5 -2
  149. package/src/server/routes/settings/skills-catalog-actions.ts +195 -0
  150. package/src/server/routes/settings/skills.test.ts +225 -0
  151. package/src/server/routes/settings/skills.ts +18 -15
  152. package/src/shared/lib/fs/folder-chain.test.ts +81 -1
  153. package/src/shared/lib/fs/folder-chain.ts +84 -0
  154. package/src/shared/lib/omp/config/schema.test.ts +35 -0
  155. package/src/shared/lib/omp/session/job-progress.test.ts +37 -1
  156. package/src/shared/lib/omp/session/job-progress.ts +31 -0
  157. package/src/shared/lib/omp/session/proc.ts +5 -0
  158. package/src/shared/lib/omp/session/wait.ts +5 -0
  159. package/src/shared/lib/ui/mobile-overlay.ts +61 -0
  160. package/src/shared/lib/ui/svg-scope.test.ts +97 -0
  161. package/src/shared/lib/ui/svg-scope.ts +134 -0
  162. package/src/shared/lib/workspace/project-accent.test.ts +69 -0
  163. package/src/shared/lib/workspace/project-accent.ts +63 -0
  164. package/src/shared/lib/workspace/project-icon.ts +40 -0
  165. package/src/shared/lib/workspace/project-model.test.ts +79 -0
  166. package/src/shared/lib/workspace/project-model.ts +64 -0
  167. package/src/shared/types/settings/skill.ts +53 -3
  168. package/src/test-support/file-explorer-disk.test-util.ts +25 -1
  169. package/src/test-support/file-explorer-drag.test-util.ts +88 -0
  170. package/src/test-support/file-explorer.test-util.ts +47 -47
  171. package/dist/client/static/js/index-28pwtt69.css +0 -1
  172. package/src/client/components/common/code-surface/indent-guide.ts +0 -104
  173. package/src/client/components/settings/categories/skill-catalog/AddSourceModal.tsx +0 -133
  174. package/src/client/hooks/desktop/servers.test.ts +0 -73
  175. package/src/client/lib/desktop/bridge.ts +0 -203
  176. package/src/shared/lib/code/editor/indent-guide-rule.test.ts +0 -65
  177. package/src/shared/lib/code/editor/indent-guide-rule.ts +0 -47
@@ -107,6 +107,8 @@ function snapshot(overrides: Partial<StatsSnapshot> = {}): StatsSnapshot {
107
107
  detail: 'the server itself · 1m',
108
108
  groupKey: 'kabmo',
109
109
  depth: 0,
110
+ memory: 2 * 1024 * 1024,
111
+ memorySource: 'rss',
110
112
  rss: 2 * 1024 * 1024,
111
113
  cpuPercent: 1.5,
112
114
  threads: 4,
@@ -120,11 +122,12 @@ function snapshot(overrides: Partial<StatsSnapshot> = {}): StatsSnapshot {
120
122
  title: 'Kabmo server',
121
123
  detail: 'the server itself · 1m',
122
124
  processes: 1,
123
- rss: 2 * 1024 * 1024,
125
+ memory: 2 * 1024 * 1024,
124
126
  cpuPercent: 1.5,
125
127
  },
126
128
  ],
127
- total: { processes: 1, rss: 2 * 1024 * 1024, cpuPercent: 1.5 },
129
+ total: { processes: 1, memory: 2 * 1024 * 1024, cpuPercent: 1.5 },
130
+ memorySource: 'rss',
128
131
  machine: { totalMemBytes: 8 * 1024 ** 3, freeMemBytes: 1024 ** 3, cpuCount: 8, loadPerCore: 0.1 },
129
132
  daemons: [],
130
133
  serverPid: 1,
@@ -192,7 +195,8 @@ describe('StatsPanel server-half state', () => {
192
195
  probe: { name: 'win32', supported: false, reason: 'no subprocess-free route' },
193
196
  rows: [],
194
197
  groups: [],
195
- total: { processes: 0, rss: 0, cpuPercent: 0 },
198
+ total: { processes: 0, memory: 0, cpuPercent: 0 },
199
+ memorySource: 'rss',
196
200
  error: 'no subprocess-free route',
197
201
  }),
198
202
  });
@@ -206,7 +210,7 @@ describe('StatsPanel server-half state', () => {
206
210
  const text = await textFor({
207
211
  live: true,
208
212
  publish: snapshot({
209
- history: { rss: [1_400_000_000, 1_500_000_000], cpuPercent: [2.5, 12.0] },
213
+ history: { memory: [1_400_000_000, 1_500_000_000], cpuPercent: [2.5, 12.0] },
210
214
  }),
211
215
  });
212
216
  expect(text).not.toMatch(/\d{9,}\.\d/);
@@ -226,3 +230,95 @@ describe('StatsPanel server-half state', () => {
226
230
  expect(html).toContain('@max-[300px]:hidden');
227
231
  });
228
232
  });
233
+
234
+ /** A row the panel may stop, beside the one it may not. */
235
+ function killableSnapshot(): StatsSnapshot {
236
+ const base = snapshot();
237
+ const server = base.rows[0];
238
+ const host = {
239
+ ...server,
240
+ pid: 101,
241
+ ppid: server.pid,
242
+ title: 'omp · rpc-ui',
243
+ detail: 'project · 2m',
244
+ groupKey: 'session',
245
+ depth: 1,
246
+ };
247
+ return {
248
+ ...base,
249
+ rows: [server, host],
250
+ // The table renders GROUPS and then their rows, so a row without a group
251
+ // is a row the panel never draws.
252
+ groups: [base.groups[0], { ...base.groups[0], key: 'session', kind: 'session', ownerPid: 101, title: host.title, detail: host.detail }],
253
+ };
254
+ }
255
+
256
+ describe('StatsPanel kill control', () => {
257
+ test('the × exists for a measured process and never for the server itself', async () => {
258
+ const { harness } = loadPluginApp(pluginDefinition);
259
+ harness.services.rpc.stub('watch', { watching: 1 });
260
+ harness.services.publishTopic(killableSnapshot());
261
+ const rendered = await renderStatsPanel(harness);
262
+
263
+ expect(rendered.html).toContain('aria-label="Stop pid 101"');
264
+ expect(rendered.html).not.toContain('aria-label="Stop pid 1"');
265
+
266
+ rendered.unmount();
267
+ harness.dispose();
268
+ configureUiKit(uiKitServices() ?? harness.services);
269
+ });
270
+
271
+ test('clicking × opens the dialog, and Cancel closes it without a kill', async () => {
272
+ const { harness } = loadPluginApp(pluginDefinition);
273
+ harness.services.rpc.stub('watch', { watching: 1 });
274
+ harness.services.rpc.stub('kill', { killed: true, pid: 101, title: 'omp · rpc-ui', signal: 'SIGTERM' });
275
+ harness.services.publishTopic(killableSnapshot());
276
+ const rendered = await renderStatsPanel(harness);
277
+
278
+ const container = document.body.lastElementChild as HTMLElement;
279
+ const button = container.querySelector<HTMLButtonElement>('button[aria-label="Stop pid 101"]');
280
+ expect(button).not.toBeNull();
281
+ await act(async () => {
282
+ button?.click();
283
+ });
284
+ expect(container.textContent).toContain('Stop pid 101?');
285
+
286
+ const cancel = [...container.querySelectorAll('button')].find((candidate) => candidate.textContent === 'Cancel');
287
+ await act(async () => {
288
+ cancel?.click();
289
+ });
290
+ expect(container.textContent).not.toContain('Stop pid 101?');
291
+ expect(harness.services.rpc.calls.filter((call) => call.method === 'kill')).toHaveLength(0);
292
+
293
+ rendered.unmount();
294
+ harness.dispose();
295
+ configureUiKit(uiKitServices() ?? harness.services);
296
+ });
297
+
298
+ test('confirming sends the pid and forces a sample so the row disappears', async () => {
299
+ const { harness } = loadPluginApp(pluginDefinition);
300
+ harness.services.rpc.stub('watch', { watching: 1 });
301
+ harness.services.rpc.stub('kill', { killed: true, pid: 101, title: 'omp · rpc-ui', signal: 'SIGTERM' });
302
+ harness.services.rpc.stub('snapshot', killableSnapshot());
303
+ harness.services.publishTopic(killableSnapshot());
304
+ const rendered = await renderStatsPanel(harness);
305
+
306
+ const container = document.body.lastElementChild as HTMLElement;
307
+ await act(async () => {
308
+ container.querySelector<HTMLButtonElement>('button[aria-label="Stop pid 101"]')?.click();
309
+ });
310
+ const stop = [...container.querySelectorAll('button')].find((candidate) => candidate.textContent === 'Stop process');
311
+ await act(async () => {
312
+ stop?.click();
313
+ await new Promise((resolve) => setTimeout(resolve, 0));
314
+ });
315
+
316
+ const calls = harness.services.rpc.calls;
317
+ expect(calls.filter((call) => call.method === 'kill').map((call) => call.input)).toEqual([{ pid: 101 }]);
318
+ expect(calls.some((call) => call.method === 'snapshot' && (call.input as { force?: boolean } | undefined)?.force === true)).toBe(true);
319
+
320
+ rendered.unmount();
321
+ harness.dispose();
322
+ configureUiKit(uiKitServices() ?? harness.services);
323
+ });
324
+ });
@@ -30,12 +30,14 @@ import { definePluginApp } from '@kabmo-labs/kabmo-sdk/app';
30
30
  import { usePanelHost, useRealtime, useRpc } from '@kabmo-labs/kabmo-ui';
31
31
  import { Empty, Note, Panel } from '@kabmo-labs/kabmo-ui/components';
32
32
 
33
+ import { KillConfirm } from './KillConfirm';
33
34
  import { ProcessTable } from './ProcessTable';
34
35
  import { Sparkline } from './Sparkline';
35
36
  import { MachineFacts, TotalsBar } from './TotalsBar';
36
37
  import { StatsHeaderReadout } from './HeaderReadout';
37
38
  import { formatAge, formatBytes, formatPercent } from './format';
38
- import type { StatsSnapshot } from './sampler';
39
+ import type { KillOutcome } from './kill';
40
+ import type { ProcessRow, StatsSnapshot } from './sampler';
39
41
 
40
42
  function StatsPanel() {
41
43
  const host = usePanelHost();
@@ -48,6 +50,12 @@ function StatsPanel() {
48
50
  // subscriber, and the force call publishes to it, so reading the RPC's own
49
51
  // reply would put a second copy of the same snapshot on screen.
50
52
  const sample = useRpc<{ force?: boolean }, StatsSnapshot>('snapshot');
53
+ // The kill call and the row it is aimed at. A refusal comes back as a RESULT
54
+ // (the server never throws for one), so the sentence belongs in the dialog
55
+ // rather than in a toast that can vanish while the reader looks away.
56
+ const kill = useRpc<{ pid: number }, KillOutcome>('kill');
57
+ const [killTarget, setKillTarget] = useState<ProcessRow | null>(null);
58
+ const [killError, setKillError] = useState<string | null>(null);
51
59
  const [now, setNow] = useState(() => Date.now());
52
60
 
53
61
  // Arm the server's sampler while this panel is on screen, and disarm it on
@@ -76,6 +84,26 @@ function StatsPanel() {
76
84
  // in-flight flag — the RPC call's own `pending` is the honest spinner.
77
85
  const refreshing = sample.pending;
78
86
 
87
+ /** Run the confirmed kill, then take a sample so the row disappears. */
88
+ async function confirmKill(): Promise<void> {
89
+ if (!killTarget) return;
90
+ const outcome = await kill.call({ pid: killTarget.pid });
91
+ if (!outcome.ok) {
92
+ setKillError(outcome.error.message);
93
+ return;
94
+ }
95
+ const value = outcome.value as KillOutcome;
96
+ if (!value.killed) {
97
+ setKillError(value.reason);
98
+ return;
99
+ }
100
+ setKillTarget(null);
101
+ setKillError(null);
102
+ // The row is still in the snapshot the table is drawing; a forced sample
103
+ // publishes the table without it instead of leaving a dead pid on screen.
104
+ void sample.call({ force: true });
105
+ }
106
+
79
107
  // The arm attempt is also the LIVENESS probe: a plugin whose server half is
80
108
  // not loaded answers this route with a 404, which the kit reports as
81
109
  // `plugin_not_loaded`. That is the state a plugin installed into a running
@@ -120,12 +148,18 @@ function StatsPanel() {
120
148
 
121
149
  {history ? (
122
150
  <div class="flex flex-col gap-1.5 border-b border-ink/10 px-3 py-2">
123
- <Sparkline values={history.rss} label="rss" tone="text-info" format={formatBytes} />
151
+ <Sparkline values={history.memory} label={snapshot.memorySource === 'rss' ? 'rss' : 'mem'} tone="text-info" format={formatBytes} />
124
152
  <Sparkline values={history.cpuPercent} label="cpu" tone="text-warning" format={(value) => formatPercent(value)} />
125
153
  </div>
126
154
  ) : null}
127
155
 
128
- <ProcessTable snapshot={snapshot} />
156
+ <ProcessTable
157
+ snapshot={snapshot}
158
+ onRequestKill={(row) => {
159
+ setKillError(null);
160
+ setKillTarget(row);
161
+ }}
162
+ />
129
163
 
130
164
  <div class="flex flex-col gap-2 px-3 py-3 border-t border-ink/10">
131
165
  <div class="flex items-center gap-2">
@@ -162,9 +196,22 @@ function StatsPanel() {
162
196
  ) : null}
163
197
 
164
198
  <span class="text-[10px] font-mono text-ink/35">
165
- {formatBytes(snapshot.total.rss)} across {snapshot.total.processes} processes
199
+ {formatBytes(snapshot.total.memory)} across {snapshot.total.processes} processes
166
200
  </span>
167
201
  </div>
202
+
203
+ {killTarget ? (
204
+ <KillConfirm
205
+ row={killTarget}
206
+ pending={kill.pending}
207
+ error={killError}
208
+ onCancel={() => {
209
+ setKillTarget(null);
210
+ setKillError(null);
211
+ }}
212
+ onConfirm={() => void confirmKill()}
213
+ />
214
+ ) : null}
168
215
  </div>
169
216
  </Panel>
170
217
  );
@@ -21,7 +21,7 @@ import type { ProcessInfo } from './proc/types';
21
21
  const SERVER_PID = 100;
22
22
 
23
23
  function proc(pid: number, ppid: number, argv: string[] | null): ProcessInfo {
24
- return { pid, ppid, rss: 1024, cpuNs: 0, threads: 1, argv, cwd: null, startedAt: 1 };
24
+ return { pid, ppid, rss: 1024, footprint: null, cpuNs: 0, threads: 1, argv, cwd: null, startedAt: 1 };
25
25
  }
26
26
 
27
27
  function daemon(name: string, pid: number, brokerPid: number | null, state = 'ready'): DaemonEntry {
@@ -0,0 +1,131 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * The kill rules. Each refusal here is a way a panel button could destroy
8
+ * something the user did not point at, so the tests are about the refusals
9
+ * more than about the happy path:
10
+ *
11
+ * - `0` and `-1` are process-group / machine-wide signals to `kill(2)`, and a
12
+ * JSON body can carry either;
13
+ * - the server that HOSTS this panel is a row in the table, so its own pid has
14
+ * to be refused by name rather than by absence;
15
+ * - a pid read off a stale table may have been recycled, so membership is
16
+ * checked against a fresh sample, and a signal that is never called is the
17
+ * proof.
18
+ */
19
+
20
+ import { describe, expect, test } from 'bun:test';
21
+
22
+ import { killStackProcess } from './kill';
23
+ import type { ProcessRow, StatsSnapshot } from './snapshot';
24
+
25
+ const SERVER_PID = 100;
26
+
27
+ function row(pid: number, title = `process ${pid}`): ProcessRow {
28
+ return {
29
+ pid,
30
+ ppid: pid === SERVER_PID ? 1 : SERVER_PID,
31
+ title,
32
+ detail: 'detail',
33
+ groupKey: 'session',
34
+ depth: pid === SERVER_PID ? 0 : 1,
35
+ memory: 1024,
36
+ memorySource: 'footprint',
37
+ rss: 2048,
38
+ cpuPercent: null,
39
+ threads: 1,
40
+ };
41
+ }
42
+
43
+ function snapshot(pids: number[]): StatsSnapshot {
44
+ const rows = pids.map((pid) => row(pid));
45
+ return {
46
+ sampledAt: 0,
47
+ probe: { name: 'fake', supported: true },
48
+ rows,
49
+ groups: [],
50
+ total: { processes: rows.length, memory: 1024 * rows.length, cpuPercent: 0 },
51
+ memorySource: 'footprint',
52
+ machine: { totalMemBytes: 1, freeMemBytes: 1, cpuCount: 1, loadPerCore: 0 },
53
+ daemons: [],
54
+ serverPid: SERVER_PID,
55
+ };
56
+ }
57
+
58
+ /** Deps whose `signal` records instead of killing, so a test cannot hurt the runner. */
59
+ function deps(pids: number[], signal?: (pid: number, sig: 'SIGTERM') => void) {
60
+ const calls: Array<{ pid: number; signal: string }> = [];
61
+ return {
62
+ calls,
63
+ value: {
64
+ sample: () => snapshot(pids),
65
+ serverPid: SERVER_PID,
66
+ signal: signal ?? ((pid: number, sig: 'SIGTERM') => calls.push({ pid, signal: sig })),
67
+ },
68
+ };
69
+ }
70
+
71
+ describe('killStackProcess', () => {
72
+ test('signals a pid the fresh sample measured', () => {
73
+ const { calls, value } = deps([SERVER_PID, 101]);
74
+ expect(killStackProcess(101, value)).toEqual({ killed: true, pid: 101, title: 'process 101', signal: 'SIGTERM' });
75
+ expect(calls).toEqual([{ pid: 101, signal: 'SIGTERM' }]);
76
+ });
77
+
78
+ test('refuses the process group (0), the machine (-1) and init (1)', () => {
79
+ for (const pid of [0, -1, 1]) {
80
+ const { calls, value } = deps([SERVER_PID, 101]);
81
+ const outcome = killStackProcess(pid, value);
82
+ expect(outcome.killed).toBe(false);
83
+ expect(calls).toHaveLength(0);
84
+ }
85
+ });
86
+
87
+ test('refuses a non-integer or non-number pid', () => {
88
+ for (const pid of [1.5, '101', null, undefined, { pid: 101 }]) {
89
+ const { calls, value } = deps([SERVER_PID, 101]);
90
+ expect(killStackProcess(pid, value).killed).toBe(false);
91
+ expect(calls).toHaveLength(0);
92
+ }
93
+ });
94
+
95
+ test('refuses the Kabmo server even though it is a row in the table', () => {
96
+ const { calls, value } = deps([SERVER_PID, 101]);
97
+ const outcome = killStackProcess(SERVER_PID, value);
98
+ expect(outcome.killed).toBe(false);
99
+ expect(outcome.killed === false && outcome.reason).toContain('kabmo stop');
100
+ expect(calls).toHaveLength(0);
101
+ });
102
+
103
+ test('refuses a pid outside the measured union — a stale row may be recycled', () => {
104
+ const { calls, value } = deps([SERVER_PID, 101]);
105
+ const outcome = killStackProcess(999, value);
106
+ expect(outcome.killed).toBe(false);
107
+ expect(calls).toHaveLength(0);
108
+ });
109
+
110
+ test('an already-exited pid is a refusal, not a thrown error', () => {
111
+ const { value } = deps([SERVER_PID, 101], () => {
112
+ const error = new Error('kill ESRCH') as NodeJS.ErrnoException;
113
+ error.code = 'ESRCH';
114
+ throw error;
115
+ });
116
+ const outcome = killStackProcess(101, value);
117
+ expect(outcome.killed).toBe(false);
118
+ expect(outcome.killed === false && outcome.reason).toContain('already exited');
119
+ });
120
+
121
+ test('a process owned by another user says so', () => {
122
+ const { value } = deps([SERVER_PID, 101], () => {
123
+ const error = new Error('kill EPERM') as NodeJS.ErrnoException;
124
+ error.code = 'EPERM';
125
+ throw error;
126
+ });
127
+ const outcome = killStackProcess(101, value);
128
+ expect(outcome.killed).toBe(false);
129
+ expect(outcome.killed === false && outcome.reason).toContain('another user');
130
+ });
131
+ });
@@ -0,0 +1,74 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * Stopping one process from the panel — the rules, kept out of the RPC
8
+ * registration so they can be tested without a host around them.
9
+ *
10
+ * A kill button is the one control in this panel that can destroy work, so
11
+ * every refusal below is a thing that must not happen rather than an edge case:
12
+ *
13
+ * - **`pid > 1` and an integer.** `process.kill(0, …)` signals the CALLER's
14
+ * process group and `-1` signals every process the user may signal. A row
15
+ * can carry neither, and a JSON body can carry anything.
16
+ * - **The Kabmo's own pid is refused.** This panel's server half runs INSIDE
17
+ * the server it measures, so a kill row for it would take the UI, every live
18
+ * session and the sampler down together — and the CLI already owns stopping
19
+ * the server (`kabmo stop`).
20
+ * - **The pid must be in the union a FRESH sample measured.** The table on
21
+ * screen is up to a sampling interval old, and a pid recycled in between
22
+ * belongs to a process the user never looked at. The union is also the whole
23
+ * authority: a pid outside it is not something this panel offers to stop.
24
+ *
25
+ * `SIGTERM`, not `SIGKILL`: omp children flush their transcript on the polite
26
+ * signal, and a process that ignores it should be visible as still running
27
+ * rather than silently destroyed. Pressing again re-sends.
28
+ */
29
+
30
+ import type { StatsSnapshot } from './snapshot';
31
+
32
+ /** What the panel is told after asking for a kill. */
33
+ export type KillOutcome =
34
+ | { killed: true; pid: number; title: string; signal: 'SIGTERM' }
35
+ | { killed: false; reason: string };
36
+
37
+ export interface KillDeps {
38
+ /** A fresh sample; its rows are the only pids this may signal. */
39
+ sample: () => StatsSnapshot;
40
+ /** The Kabmo's own pid. */
41
+ serverPid: number;
42
+ /** The signalling call, injected so a test never kills anything. */
43
+ signal?: (pid: number, signal: 'SIGTERM') => void;
44
+ }
45
+
46
+ /**
47
+ * Signal one process in the measured stack, or explain why not.
48
+ *
49
+ * Never throws: every failure is an outcome the panel renders in its dialog.
50
+ */
51
+ export function killStackProcess(pid: unknown, deps: KillDeps): KillOutcome {
52
+ if (typeof pid !== 'number' || !Number.isInteger(pid) || pid <= 1) {
53
+ return { killed: false, reason: 'That request did not name a process id above 1.' };
54
+ }
55
+ if (pid === deps.serverPid) {
56
+ return { killed: false, reason: 'Refusing to stop the Kabmo server from its own panel — that is `kabmo stop`.' };
57
+ }
58
+ const row = deps.sample().rows.find((candidate) => candidate.pid === pid);
59
+ if (!row) {
60
+ return {
61
+ killed: false,
62
+ reason: `pid ${pid} is not part of the stack this panel measures — the table may be stale, so refresh and try again.`,
63
+ };
64
+ }
65
+ try {
66
+ (deps.signal ?? ((target, signal) => process.kill(target, signal)))(pid, 'SIGTERM');
67
+ } catch (error) {
68
+ const code = (error as NodeJS.ErrnoException | undefined)?.code;
69
+ if (code === 'ESRCH') return { killed: false, reason: `pid ${pid} already exited.` };
70
+ if (code === 'EPERM') return { killed: false, reason: `pid ${pid} belongs to another user, so it cannot be signalled.` };
71
+ return { killed: false, reason: error instanceof Error ? error.message : String(error) };
72
+ }
73
+ return { killed: true, pid, title: row.title, signal: 'SIGTERM' };
74
+ }
@@ -28,6 +28,7 @@ function proc(pid: number, argv: string[] | null, extra: Partial<ProcessInfo> =
28
28
  pid,
29
29
  ppid: 1,
30
30
  rss: 1024,
31
+ footprint: null,
31
32
  cpuNs: 0,
32
33
  threads: 1,
33
34
  argv,
@@ -0,0 +1,77 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * The machine reading — `os.freemem()` is not "memory available" on macOS.
8
+ *
9
+ * Measured on this host while 5.60 GB was actually available: `os.freemem()`
10
+ * and `Pages free` both read 1.05 GB, because the runtime counts only free
11
+ * pages and ignores the 4.53 GB of inactive ones the kernel hands out again
12
+ * without swapping. A panel that shows the stack's usage against 1 GB while
13
+ * 5.6 GB is available tells the reader the machine is full when it is not.
14
+ *
15
+ * The parsers are pinned here; the spawn (`vm_stat`, 8.8 ms) is not exercised,
16
+ * because a test that spawns a platform tool is a test that skips on the
17
+ * platform CI runs it on.
18
+ */
19
+
20
+ import { describe, expect, test } from 'bun:test';
21
+ import os from 'node:os';
22
+
23
+ import { freeMemBytes, freeMemBytesFromMeminfo, freeMemBytesFromVmStat } from './machine';
24
+
25
+ const VM_STAT = `Mach Virtual Memory Statistics: (page size of 16384 bytes)
26
+ Pages free: 68590.
27
+ Pages active: 286755.
28
+ Pages inactive: 296680.
29
+ Pages speculative: 2081.
30
+ Pages throttled: 0.
31
+ Pages wired down: 63387.
32
+ Pages purgeable: 7201.
33
+ `;
34
+
35
+ describe('freeMemBytesFromVmStat', () => {
36
+ test('counts free + inactive + speculative, at the reported page size', () => {
37
+ expect(freeMemBytesFromVmStat(VM_STAT)).toBe((68590 + 296680 + 2081) * 16384);
38
+ });
39
+
40
+ test('reads the page size from the header, not a constant', () => {
41
+ // Intel Macs report 4096; assuming Apple silicon's 16384 would be 4x off.
42
+ const intel = VM_STAT.replace('page size of 16384 bytes', 'page size of 4096 bytes');
43
+ expect(freeMemBytesFromVmStat(intel)).toBe((68590 + 296680 + 2081) * 4096);
44
+ });
45
+
46
+ test('a report with no page size is unusable rather than zero', () => {
47
+ expect(freeMemBytesFromVmStat('Mach Virtual Memory Statistics:')).toBeNull();
48
+ });
49
+
50
+ test('a report with counters but no usable page is unusable too', () => {
51
+ expect(freeMemBytesFromVmStat('page size of 16384 bytes\nPages active: 10.')).toBeNull();
52
+ });
53
+ });
54
+
55
+ describe('freeMemBytesFromMeminfo', () => {
56
+ test('reads MemAvailable, not MemFree', () => {
57
+ const meminfo = `MemTotal: 16000000 kB
58
+ MemFree: 1000000 kB
59
+ MemAvailable: 8123456 kB
60
+ `;
61
+ expect(freeMemBytesFromMeminfo(meminfo)).toBe(8_123_456 * 1024);
62
+ });
63
+
64
+ test('a kernel without MemAvailable answers null, never MemFree', () => {
65
+ // Pre-3.14 kernels have no MemAvailable. Falling back to MemFree would
66
+ // understate the same way `os.freemem()` does on macOS, under another name.
67
+ expect(freeMemBytesFromMeminfo('MemFree: 1000000 kB\n')).toBeNull();
68
+ });
69
+ });
70
+
71
+ describe('freeMemBytes', () => {
72
+ test('answers a plausible figure for this host', () => {
73
+ const bytes = freeMemBytes(Date.now() + 60_000);
74
+ expect(bytes).toBeGreaterThan(0);
75
+ expect(bytes).toBeLessThanOrEqual(Number(os.totalmem()));
76
+ });
77
+ });
@@ -0,0 +1,115 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * The machine's own numbers: total and AVAILABLE memory, cores, load.
8
+ *
9
+ * `os.freemem()` is the wrong answer on macOS, and it was the wrong answer for
10
+ * this panel: it reports `Pages free` alone. Measured on this host while the
11
+ * system had 5.60 GB available:
12
+ *
13
+ * ```
14
+ * os.freemem() 1.05 GB
15
+ * Pages free 1.04 GB
16
+ * Pages inactive 4.53 GB
17
+ * Pages speculative 0.03 GB
18
+ * free + inactive + speculative 5.60 GB
19
+ * ```
20
+ *
21
+ * Inactive and speculative pages are memory the kernel hands out again without
22
+ * touching swap, so a reader comparing the stack's usage against the 1 GB
23
+ * denominator concludes the machine is nearly full when it is not. "Available"
24
+ * here is `free + inactive + speculative`; `Pages purgeable` is deliberately
25
+ * NOT added, because it is already counted in the inactive total.
26
+ *
27
+ * Linux needs no new source: `/proc/meminfo`'s `MemAvailable` is the kernel's
28
+ * own estimate of what a new allocation can get without swapping, which is a
29
+ * better figure than `MemFree` for exactly the same reason.
30
+ *
31
+ * Windows keeps `os.freemem()`: it reports the same "Available" figure the
32
+ * task manager does.
33
+ *
34
+ * `vm_stat` costs 8.8 ms measured (a spawn), while a sample lands every
35
+ * second, so the reading is cached for `FREE_MEMORY_TTL_MS`. Free memory moves
36
+ * on the scale of seconds, not ticks; the panel's own totals are re-read every
37
+ * sample regardless.
38
+ */
39
+
40
+ import { readFileSync } from 'node:fs';
41
+ import os from 'node:os';
42
+
43
+ import type { MachineContext } from './snapshot';
44
+
45
+ /** How long a free-memory reading is reused. */
46
+ export const FREE_MEMORY_TTL_MS = 5_000;
47
+
48
+ /**
49
+ * `free + inactive + speculative`, from `vm_stat`.
50
+ *
51
+ * The page size is taken from the report's own header (`page size of 16384
52
+ * bytes` on Apple silicon, 4096 on Intel) rather than assumed: a page count
53
+ * read against the wrong size is silently off by 4x.
54
+ */
55
+ export function freeMemBytesFromVmStat(output: string): number | null {
56
+ const pageSize = Number(/page size of (\d+) bytes/.exec(output)?.[1] ?? 0);
57
+ if (!Number.isFinite(pageSize) || pageSize <= 0) return null;
58
+ const pages = (label: string): number => {
59
+ const match = new RegExp(`^${label}:\\s+(\\d+)\\.?$`, 'm').exec(output);
60
+ return match ? Number(match[1]) : 0;
61
+ };
62
+ const free = pages('Pages free');
63
+ const inactive = pages('Pages inactive');
64
+ const speculative = pages('Pages speculative');
65
+ if (free === 0 && inactive === 0 && speculative === 0) return null;
66
+ return (free + inactive + speculative) * pageSize;
67
+ }
68
+
69
+ /** `MemAvailable: 12345 kB` from `/proc/meminfo`, in bytes. */
70
+ export function freeMemBytesFromMeminfo(text: string): number | null {
71
+ const match = /^MemAvailable:\s+(\d+)\s+kB$/m.exec(text);
72
+ return match ? Number(match[1]) * 1024 : null;
73
+ }
74
+
75
+ let cached: { at: number; bytes: number } | null = null;
76
+
77
+ /** Available memory on this host, in bytes. Cached; never throws. */
78
+ export function freeMemBytes(now = Date.now()): number {
79
+ if (cached && now - cached.at < FREE_MEMORY_TTL_MS) return cached.bytes;
80
+ let bytes: number | null = null;
81
+ if (process.platform === 'darwin') {
82
+ try {
83
+ bytes = freeMemBytesFromVmStat(Bun.spawnSync(['vm_stat']).stdout.toString());
84
+ } catch {
85
+ // No `vm_stat` on this host: the runtime's own number is the fallback.
86
+ bytes = null;
87
+ }
88
+ } else if (process.platform === 'linux') {
89
+ try {
90
+ bytes = freeMemBytesFromMeminfo(readFileSync('/proc/meminfo', 'utf8'));
91
+ } catch {
92
+ bytes = null;
93
+ }
94
+ }
95
+ const answer = bytes ?? Number(os.freemem());
96
+ cached = { at: now, bytes: answer };
97
+ return answer;
98
+ }
99
+
100
+ /** Everything the panel reports about the machine itself. */
101
+ export function readMachine(): MachineContext {
102
+ const cpuCount = os.cpus().length;
103
+ const load = os.loadavg()[0];
104
+ return {
105
+ totalMemBytes: Number(os.totalmem()),
106
+ freeMemBytes: freeMemBytes(),
107
+ cpuCount,
108
+ loadPerCore: cpuCount > 0 ? load / cpuCount : load,
109
+ };
110
+ }
111
+
112
+ /** Drop the cached reading. For tests; the panel never needs it. */
113
+ export function clearMachineCache(): void {
114
+ cached = null;
115
+ }