@vellumai/assistant 0.12.4 → 0.12.5-dev.202609251017.e467a6c
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/docs/architecture/memory.md +9 -1
- package/docs/architecture/scheduling.md +13 -5
- package/docs/desktop-browser-cli.md +3 -2
- package/docs/guardian-request-flow.md +1 -1
- package/node_modules/@vellumai/avatar-manifest/src/__tests__/notification-avatar.test.ts +48 -4
- package/node_modules/@vellumai/avatar-manifest/src/notification-avatar.ts +12 -8
- package/node_modules/@vellumai/ces-client/node_modules/@vellumai/service-contracts/src/__tests__/guardian-requests.test.ts +26 -0
- package/node_modules/@vellumai/ces-client/node_modules/@vellumai/service-contracts/src/guardian-requests.ts +77 -3
- package/node_modules/@vellumai/gateway-client/node_modules/@vellumai/service-contracts/src/__tests__/guardian-requests.test.ts +26 -0
- package/node_modules/@vellumai/gateway-client/node_modules/@vellumai/service-contracts/src/guardian-requests.ts +77 -3
- package/node_modules/@vellumai/gateway-client/src/outbound-contract.ts +2 -4
- package/node_modules/@vellumai/service-contracts/src/__tests__/guardian-requests.test.ts +26 -0
- package/node_modules/@vellumai/service-contracts/src/guardian-requests.ts +77 -3
- package/openapi.yaml +152 -3
- package/package.json +1 -1
- package/src/__tests__/agent-loop.test.ts +76 -0
- package/src/__tests__/anthropic-error-formatting.test.ts +9 -0
- package/src/__tests__/background-shell-bash.test.ts +34 -0
- package/src/__tests__/background-shell-host-bash.test.ts +98 -1
- package/src/__tests__/background-tool-registry.test.ts +39 -0
- package/src/__tests__/channel-readiness-discord.test.ts +8 -0
- package/src/__tests__/channel-readiness-slack-remote.test.ts +8 -0
- package/src/__tests__/client-os-metadata-persistence.test.ts +4 -20
- package/src/__tests__/conversation-agent-loop.test.ts +529 -1
- package/src/__tests__/conversation-error.test.ts +18 -0
- package/src/__tests__/conversation-pairing.test.ts +69 -0
- package/src/__tests__/conversation-queue.test.ts +257 -4
- package/src/__tests__/conversation-runtime-assembly.test.ts +27 -0
- package/src/__tests__/emit-signal-routing-intent.test.ts +62 -0
- package/src/__tests__/evict-conversations-for-reload.test.ts +21 -0
- package/src/__tests__/gemini-provider.test.ts +9 -0
- package/src/__tests__/host-cu-proxy.test.ts +79 -0
- package/src/__tests__/host-cu-routes-targeted.test.ts +14 -5
- package/src/__tests__/introduction-card-resolver.test.ts +53 -8
- package/src/__tests__/job-handler-registration-guard.test.ts +1 -0
- package/src/__tests__/non-member-access-request.test.ts +48 -0
- package/src/__tests__/notification-events-source-context-probe.test.ts +48 -1
- package/src/__tests__/notification-platform-adapter.test.ts +97 -0
- package/src/__tests__/notification-vellum-adapter.test.ts +103 -3
- package/src/__tests__/platform-callback-registration.test.ts +46 -1
- package/src/__tests__/plugin-import-boundary-guard.test.ts +3 -0
- package/src/__tests__/scaffold-managed-skill-tool.test.ts +116 -66
- package/src/__tests__/scheduler-disk-pressure.test.ts +1 -0
- package/src/__tests__/scheduler-result-notification.test.ts +521 -7
- package/src/__tests__/scheduler-wake.test.ts +1 -0
- package/src/__tests__/skill-load-inline-command.test.ts +35 -0
- package/src/__tests__/subagent-cron-run-attribution.test.ts +41 -2
- package/src/__tests__/subagent-manager-notify.test.ts +48 -0
- package/src/__tests__/trusted-contact-inline-approval-integration.test.ts +6 -6
- package/src/__tests__/usage-attribution.test.ts +31 -0
- package/src/__tests__/wake-schedule-escalation.test.ts +1 -0
- package/src/agent/loop.ts +23 -0
- package/src/api/ask-question-tool.ts +50 -0
- package/src/api/events/generation-handoff.ts +4 -0
- package/src/api/events/message-complete.ts +4 -0
- package/src/api/events/tool-result.ts +11 -0
- package/src/api/index.ts +11 -0
- package/src/api/requests/guardian-actions.test.ts +45 -0
- package/src/api/requests/guardian-actions.ts +20 -0
- package/src/api/responses/conversation-message.ts +4 -0
- package/src/api/responses/home.ts +27 -0
- package/src/approvals/guardian-decision-primitive.ts +6 -4
- package/src/approvals/guardian-request-resolvers.ts +35 -30
- package/src/avatar/notification-avatar.test.ts +42 -34
- package/src/avatar/notification-avatar.ts +1 -0
- package/src/calls/__tests__/call-controller.test.ts +18 -0
- package/src/calls/__tests__/voice-control-protocol.test.ts +6 -1
- package/src/calls/__tests__/voice-leg-coordinator.test.ts +70 -0
- package/src/calls/__tests__/voice-session-bridge.test.ts +116 -4
- package/src/calls/__tests__/voice-triage-escalate.test.ts +77 -11
- package/src/calls/call-controller.ts +3 -1
- package/src/calls/voice-control-protocol.ts +12 -4
- package/src/calls/voice-escalation-judge.ts +2 -2
- package/src/calls/voice-leg-coordinator.ts +23 -5
- package/src/calls/voice-screen-annotation.ts +44 -0
- package/src/calls/voice-session-bridge.ts +63 -20
- package/src/calls/voice-triage-escalate.ts +82 -27
- package/src/cli/commands/__tests__/conversations-clear.test.ts +156 -0
- package/src/cli/commands/conversations.help.ts +7 -2
- package/src/cli/commands/conversations.ts +36 -30
- package/src/cli/commands/oauth/index.help.ts +8 -2
- package/src/cli/commands/oauth/request.test.ts +32 -0
- package/src/cli/commands/platform/index.help.ts +2 -1
- package/src/cli/commands/platform/index.ts +8 -2
- package/src/cli/lib/__tests__/plugin-catalog-cache.test.ts +60 -0
- package/src/cli/lib/__tests__/plugin-catalog-platform.test.ts +66 -2
- package/src/cli/lib/bundled-marketplace.json +35 -0
- package/src/cli/lib/bundled-plugin-packages.json +21 -0
- package/src/cli/lib/plugin-catalog-cache.ts +17 -2
- package/src/cli/lib/plugin-catalog-platform.ts +48 -9
- package/src/config/__tests__/default-profile-catalog.test.ts +28 -0
- package/src/config/__tests__/platform-identity.test.ts +59 -0
- package/src/config/__tests__/profile-order.test.ts +19 -0
- package/src/config/__tests__/sync-gated-profiles.test.ts +71 -0
- package/src/config/bundled-skills/computer-use/SKILL.md +20 -0
- package/src/config/bundled-skills/computer-use/TOOLS.json +11 -11
- package/src/config/bundled-skills/screen-annotation/SKILL.md +41 -29
- package/src/config/bundled-skills/screen-annotation/TOOLS.json +5 -5
- package/src/config/call-site-defaults.ts +6 -0
- package/src/config/default-profile-catalog.ts +30 -1
- package/src/config/default-profile-names.ts +36 -2
- package/src/config/feature-flag-registry.json +9 -1
- package/src/config/platform-identity.ts +22 -5
- package/src/config/profile-order.ts +7 -1
- package/src/config/schemas/call-site-catalog.ts +7 -0
- package/src/config/schemas/llm.ts +21 -5
- package/src/config/sync-gated-profiles.ts +96 -44
- package/src/daemon/__tests__/abort-null-controller.test.ts +135 -1
- package/src/daemon/__tests__/auto-profile-router.test.ts +380 -0
- package/src/daemon/__tests__/turn-tail-assistant-reply-notify.test.ts +16 -0
- package/src/daemon/activity-trail.test.ts +206 -0
- package/src/daemon/activity-trail.ts +190 -0
- package/src/daemon/auto-profile-router.ts +431 -0
- package/src/daemon/computer-use-observation.test.ts +81 -0
- package/src/daemon/computer-use-observation.ts +23 -0
- package/src/daemon/conversation-agent-loop-handlers.ts +11 -0
- package/src/daemon/conversation-agent-loop.ts +157 -32
- package/src/daemon/conversation-error.ts +19 -0
- package/src/daemon/conversation-lifecycle.ts +70 -7
- package/src/daemon/conversation-media-retry.ts +1 -2
- package/src/daemon/conversation-messaging.ts +31 -2
- package/src/daemon/conversation-process.ts +107 -16
- package/src/daemon/conversation-queue-manager.ts +7 -0
- package/src/daemon/conversation-runtime-assembly.ts +1 -0
- package/src/daemon/conversation-turn-finalize.ts +14 -0
- package/src/daemon/conversation.ts +25 -6
- package/src/daemon/event-loop-watchdog.test.ts +201 -2
- package/src/daemon/event-loop-watchdog.ts +182 -6
- package/src/daemon/host-cu-proxy.ts +34 -3
- package/src/daemon/pkb-reminder-builder.test.ts +10 -3
- package/src/daemon/pkb-reminder-builder.ts +1 -1
- package/src/daemon/wake-conversation-ops.ts +4 -2
- package/src/desktop/desktop-computer-use.test.ts +14 -4
- package/src/home/feed-types.ts +2 -0
- package/src/inbound/platform-callback-registration.ts +19 -6
- package/src/live-voice/__tests__/live-voice-agent-turn.test.ts +27 -0
- package/src/live-voice/__tests__/live-voice-flux-turn-end.test.ts +83 -0
- package/src/live-voice/__tests__/live-voice-triage-escalate.test.ts +48 -1
- package/src/live-voice/__tests__/live-voice-vad.test.ts +34 -2
- package/src/live-voice/__tests__/protocol.test.ts +22 -0
- package/src/live-voice/continuation-judge.ts +1 -1
- package/src/live-voice/live-voice-session.ts +25 -19
- package/src/live-voice/protocol.ts +16 -5
- package/src/messaging/providers/discord/withdraw.ts +4 -5
- package/src/messaging/providers/gmail/client.ts +4 -0
- package/src/messaging/providers/slack/withdraw.ts +5 -5
- package/src/messaging/providers/telegram-bot/withdraw.ts +4 -5
- package/src/monitoring/__tests__/proc-fixtures.ts +28 -0
- package/src/monitoring/__tests__/proc-wait-state.test.ts +232 -0
- package/src/monitoring/__tests__/process-usage.test.ts +243 -0
- package/src/monitoring/proc-wait-state.ts +366 -0
- package/src/monitoring/process-memory.ts +4 -3
- package/src/monitoring/process-usage.ts +215 -0
- package/src/monitoring/resource-sample-types.ts +8 -0
- package/src/monitoring/resource-sampler.ts +17 -2
- package/src/monitoring/stall-capture.ts +12 -0
- package/src/notifications/README.md +88 -9
- package/src/notifications/__tests__/assistant-reply-producer.test.ts +390 -119
- package/src/notifications/__tests__/background-result-producer.test.ts +1272 -0
- package/src/notifications/__tests__/broadcaster.test.ts +292 -1
- package/src/notifications/__tests__/completion-policy.test.ts +243 -0
- package/src/notifications/__tests__/decision-engine.test.ts +51 -4
- package/src/notifications/__tests__/has-pending-background-work.test.ts +281 -0
- package/src/notifications/__tests__/home-feed-side-effect.test.ts +161 -3
- package/src/notifications/__tests__/resolve-visible-in-source.test.ts +110 -1
- package/src/notifications/__tests__/schedule-result-producer.test.ts +11 -4
- package/src/notifications/adapters/macos.ts +24 -12
- package/src/notifications/adapters/platform.ts +16 -5
- package/src/notifications/assistant-reply-producer.ts +96 -71
- package/src/notifications/background-result-producer.ts +369 -0
- package/src/notifications/broadcaster.ts +28 -8
- package/src/notifications/completion-policy.ts +115 -0
- package/src/notifications/completion-work.ts +46 -0
- package/src/notifications/conversation-pairing.ts +32 -13
- package/src/notifications/decision-engine.ts +18 -24
- package/src/notifications/emit-signal.ts +16 -1
- package/src/notifications/events-store.ts +4 -0
- package/src/notifications/has-pending-background-work.ts +91 -0
- package/src/notifications/home-feed-mirror.ts +44 -0
- package/src/notifications/home-feed-side-effect.ts +54 -28
- package/src/notifications/resolve-visible-in-source.ts +56 -2
- package/src/notifications/result-output.ts +154 -0
- package/src/notifications/schedule-result-producer.ts +6 -187
- package/src/notifications/types.ts +2 -0
- package/src/oauth/AGENTS.md +9 -1
- package/src/oauth/byo-connection.test.ts +28 -0
- package/src/persistence/__tests__/recent-conversation-messages.test.ts +41 -0
- package/src/persistence/conversation-crud.ts +26 -0
- package/src/persistence/conversation-types.ts +15 -37
- package/src/persistence/embeddings/__tests__/embedding-local-lifecycle.test.ts +85 -14
- package/src/persistence/jobs-store.ts +229 -0
- package/src/persistence/migrations/383-normalize-opencode-host-connections.ts +44 -0
- package/src/persistence/migrations/__tests__/383-normalize-opencode-host-connections.test.ts +102 -0
- package/src/persistence/steps.ts +12 -0
- package/src/platform/sync-avatar.test.ts +29 -0
- package/src/plugins/defaults/memory/AGENTS.md +5 -3
- package/src/plugins/defaults/memory/__tests__/skill-update-receipt-boundary-guard.test.ts +45 -0
- package/src/plugins/defaults/memory/__tests__/skill-update-receipt-job.test.ts +580 -0
- package/src/plugins/defaults/memory/__tests__/turn-retrieval.test.ts +21 -0
- package/src/plugins/defaults/memory/graph/tools.ts +9 -5
- package/src/plugins/defaults/memory/hooks/conversation-deleted.ts +4 -0
- package/src/plugins/defaults/memory/hooks/user-prompt-submit.ts +7 -3
- package/src/plugins/defaults/memory/job-handlers.ts +8 -2
- package/src/plugins/defaults/memory/skill-update-receipt-job.ts +334 -0
- package/src/plugins/defaults/memory/tools.test.ts +17 -0
- package/src/plugins/defaults/memory/turn-retrieval.ts +8 -0
- package/src/plugins/defaults/memory/v3/__tests__/injection.test.ts +24 -0
- package/src/plugins/defaults/memory/v3/injector.ts +4 -4
- package/src/plugins/types.ts +2 -0
- package/src/providers/__tests__/search-provider-catalog.test.ts +8 -0
- package/src/providers/anthropic/client.ts +4 -0
- package/src/providers/gemini/client.ts +7 -1
- package/src/providers/inference/auth.ts +25 -0
- package/src/providers/inference/profile-probe.ts +1 -0
- package/src/{calls/__tests__/typesafe-noul.test.ts → providers/jev/ask.test.ts} +4 -4
- package/src/{calls/typesafe-noul.ts → providers/jev/ask.ts} +67 -34
- package/src/providers/openai/__tests__/api-error-normalization.test.ts +12 -0
- package/src/providers/openai/api-error-normalization.ts +4 -0
- package/src/providers/retry.ts +2 -0
- package/src/providers/search-provider-catalog.ts +10 -0
- package/src/providers/types.ts +7 -0
- package/src/providers/usage-tracking.ts +5 -3
- package/src/runtime/__tests__/agent-wake.test.ts +308 -0
- package/src/runtime/access-request-helper.ts +16 -9
- package/src/runtime/agent-wake-queue.ts +52 -0
- package/src/runtime/agent-wake.ts +111 -18
- package/src/runtime/channel-approval-types.ts +6 -38
- package/src/runtime/channel-approvals.ts +4 -5
- package/src/runtime/http-server.ts +4 -1
- package/src/runtime/introduction-policy.test.ts +17 -1
- package/src/runtime/introduction-policy.ts +25 -7
- package/src/runtime/migrations/vbundle-builder.ts +9 -0
- package/src/runtime/routes/__tests__/inference-profiles-routes.test.ts +21 -1
- package/src/runtime/routes/__tests__/inference-provider-connection-routes.test.ts +60 -0
- package/src/runtime/routes/__tests__/oauth-proxy-routes.test.ts +61 -3
- package/src/runtime/routes/auto-profile-routes.ts +83 -0
- package/src/runtime/routes/conversation-management-routes.ts +1 -3
- package/src/runtime/routes/conversation-routes.ts +9 -0
- package/src/runtime/routes/guardian-action-routes.ts +8 -23
- package/src/runtime/routes/host-cu-routes.ts +3 -1
- package/src/runtime/routes/index.ts +2 -0
- package/src/runtime/routes/inference-profiles-routes.ts +5 -2
- package/src/runtime/routes/inference-provider-connection-routes.ts +25 -8
- package/src/runtime/routes/migration-routes.ts +8 -2
- package/src/runtime/routes/oauth-proxy-passthrough.test.ts +32 -0
- package/src/runtime/routes/platform-routes.ts +6 -1
- package/src/runtime/routes/user-route-dispatcher.ts +1 -2
- package/src/runtime/web-presence.ts +3 -4
- package/src/schedule/schedule-store.ts +10 -0
- package/src/schedule/scheduler.ts +92 -24
- package/src/subagent/manager.ts +32 -12
- package/src/telegram/__tests__/webhook-health.test.ts +8 -0
- package/src/telemetry/telemetry-wire-source.json +1 -1
- package/src/telemetry/telemetry-wire.generated.ts +1 -0
- package/src/tools/background-tool-registry.ts +30 -3
- package/src/tools/browser/cdp-client/local-cdp-client.ts +1 -2
- package/src/tools/host-terminal/host-shell.ts +7 -0
- package/src/tools/network/__tests__/web-fetch-firecrawl.test.ts +28 -1
- package/src/tools/network/__tests__/web-fetch-metadata.test.ts +34 -0
- package/src/tools/network/__tests__/web-fetch-tinyfish.test.ts +35 -1
- package/src/tools/network/__tests__/web-search.test.ts +96 -0
- package/src/tools/network/web-fetch.ts +30 -9
- package/src/tools/network/web-search.ts +235 -4
- package/src/tools/skills/load.ts +13 -17
- package/src/tools/skills/scaffold-managed.ts +86 -71
- package/src/tools/terminal/shell-launch.test.ts +27 -1
- package/src/tools/terminal/shell.ts +4 -0
- package/src/tools/workspace-tools/loader.ts +1 -2
- package/src/usage/attribution.ts +18 -1
- package/src/util/abort-reasons.ts +2 -0
- package/src/util/errors.ts +1 -0
- package/src/util/oauth-request-body.test.ts +5 -1
- package/src/util/provider-error-patterns.ts +11 -0
- package/src/util/single-flight.test.ts +6 -0
- package/src/util/single-flight.ts +2 -0
- package/src/util/worker-pipe.test.ts +56 -10
- package/src/util/worker-pipe.ts +9 -4
- package/src/workspace/git-service.ts +6 -1
- package/src/workspace/migrations/158-repoint-opencode-host-provider-fragments.ts +163 -0
- package/src/workspace/migrations/__tests__/158-repoint-opencode-host-provider-fragments.test.ts +188 -0
- package/src/workspace/migrations/registry.ts +2 -0
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the per-process CPU and memory tracker, against a fixture proc
|
|
3
|
+
* tree. CPU is a rate between two scans, so each test writes counters, scans,
|
|
4
|
+
* advances the counters, and scans again.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
8
|
+
import { tmpdir } from "node:os";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
11
|
+
|
|
12
|
+
import { readKernelPageSizeBytes } from "../proc-wait-state.js";
|
|
13
|
+
import { createProcessUsageTracker } from "../process-usage.js";
|
|
14
|
+
import { stat } from "./proc-fixtures.js";
|
|
15
|
+
|
|
16
|
+
const DAEMON = 1;
|
|
17
|
+
const MB_PAGES = 256; // 1 MiB in 4 KiB pages
|
|
18
|
+
|
|
19
|
+
let procRoot: string;
|
|
20
|
+
|
|
21
|
+
function writeProcess(
|
|
22
|
+
pid: number,
|
|
23
|
+
comm: string,
|
|
24
|
+
cpuTicks: number,
|
|
25
|
+
opts: { startTicks?: number; rssMb?: number; cmdline?: string[] } = {},
|
|
26
|
+
): void {
|
|
27
|
+
const dir = join(procRoot, String(pid));
|
|
28
|
+
mkdirSync(dir, { recursive: true });
|
|
29
|
+
writeFileSync(
|
|
30
|
+
join(dir, "stat"),
|
|
31
|
+
stat(pid, comm, "S", 0, opts.startTicks ?? 100, {
|
|
32
|
+
utime: cpuTicks,
|
|
33
|
+
rssPages: (opts.rssMb ?? 10) * MB_PAGES,
|
|
34
|
+
}),
|
|
35
|
+
);
|
|
36
|
+
writeFileSync(join(dir, "cmdline"), (opts.cmdline ?? [comm]).join("\0"));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
procRoot = mkdtempSync(join(tmpdir(), "process-usage-"));
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
afterEach(() => {
|
|
44
|
+
rmSync(procRoot, { recursive: true, force: true });
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe("createProcessUsageTracker", () => {
|
|
48
|
+
test("reports CPU as a rate between two scans, busiest first", () => {
|
|
49
|
+
// GIVEN a daemon and a memory worker
|
|
50
|
+
const tracker = createProcessUsageTracker({ procRoot });
|
|
51
|
+
writeProcess(DAEMON, "bun", 1_000, { rssMb: 900 });
|
|
52
|
+
writeProcess(20, "bun", 5_000, {
|
|
53
|
+
rssMb: 1_200,
|
|
54
|
+
cmdline: [
|
|
55
|
+
"bun",
|
|
56
|
+
"--smol",
|
|
57
|
+
"run",
|
|
58
|
+
"/app/src/plugins/defaults/memory/worker.ts",
|
|
59
|
+
],
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// WHEN the first scan runs
|
|
63
|
+
// THEN there is no rate yet
|
|
64
|
+
expect(tracker.sample(0, DAEMON)).toBeNull();
|
|
65
|
+
|
|
66
|
+
// WHEN 2s later the worker used 3 CPU-seconds and the daemon 0.1s
|
|
67
|
+
writeProcess(DAEMON, "bun", 1_010, { rssMb: 900 });
|
|
68
|
+
writeProcess(20, "bun", 5_300, {
|
|
69
|
+
rssMb: 1_200,
|
|
70
|
+
cmdline: [
|
|
71
|
+
"bun",
|
|
72
|
+
"--smol",
|
|
73
|
+
"run",
|
|
74
|
+
"/app/src/plugins/defaults/memory/worker.ts",
|
|
75
|
+
],
|
|
76
|
+
});
|
|
77
|
+
const usage = tracker.sample(2_000, DAEMON);
|
|
78
|
+
|
|
79
|
+
// THEN the worker shows 150% of a core and leads the list
|
|
80
|
+
expect(usage).toEqual([
|
|
81
|
+
{ pid: 20, name: "memory-worker", cpuPct: 150, rssMb: 1_200 },
|
|
82
|
+
{ pid: DAEMON, name: "daemon", cpuPct: 5, rssMb: 900 },
|
|
83
|
+
]);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test("never reports command-line arguments", () => {
|
|
87
|
+
// GIVEN a process started with a secret on its command line
|
|
88
|
+
const tracker = createProcessUsageTracker({ procRoot });
|
|
89
|
+
const cmdline = ["bun", "run", "deploy", "--token=sk-secret"];
|
|
90
|
+
writeProcess(30, "bun", 0, { cmdline });
|
|
91
|
+
tracker.sample(0, DAEMON);
|
|
92
|
+
writeProcess(30, "bun", 100, { cmdline });
|
|
93
|
+
|
|
94
|
+
// WHEN it is reported
|
|
95
|
+
const usage = tracker.sample(1_000, DAEMON)!;
|
|
96
|
+
|
|
97
|
+
// THEN only the kernel comm is used
|
|
98
|
+
expect(usage[0]!.name).toBe("bun");
|
|
99
|
+
expect(JSON.stringify(usage)).not.toContain("secret");
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test("a reused pid is not given its predecessor's counters", () => {
|
|
103
|
+
// GIVEN pid 40 exits and a new process takes the pid
|
|
104
|
+
const tracker = createProcessUsageTracker({ procRoot });
|
|
105
|
+
writeProcess(40, "old", 9_000, { startTicks: 100 });
|
|
106
|
+
tracker.sample(0, DAEMON);
|
|
107
|
+
writeProcess(40, "chromium", 50, { startTicks: 500 });
|
|
108
|
+
|
|
109
|
+
// WHEN the next scan runs
|
|
110
|
+
const usage = tracker.sample(1_000, DAEMON)!;
|
|
111
|
+
|
|
112
|
+
// THEN the new process is skipped until it has two readings
|
|
113
|
+
expect(usage).toEqual([]);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test("the daemon is kept even when it is not among the busiest", () => {
|
|
117
|
+
// GIVEN seven busy processes and an idle, small daemon
|
|
118
|
+
const tracker = createProcessUsageTracker({ procRoot });
|
|
119
|
+
writeProcess(DAEMON, "bun", 0, { rssMb: 1 });
|
|
120
|
+
for (let pid = 50; pid < 57; pid++) {
|
|
121
|
+
writeProcess(pid, `chrome-${pid}`, 0);
|
|
122
|
+
}
|
|
123
|
+
tracker.sample(0, DAEMON);
|
|
124
|
+
writeProcess(DAEMON, "bun", 0, { rssMb: 1 });
|
|
125
|
+
for (let pid = 50; pid < 57; pid++) {
|
|
126
|
+
writeProcess(pid, `chrome-${pid}`, 100);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// WHEN the next scan runs
|
|
130
|
+
const usage = tracker.sample(1_000, DAEMON)!;
|
|
131
|
+
|
|
132
|
+
// THEN four by CPU, two by memory, plus the daemon are reported
|
|
133
|
+
expect(usage).toHaveLength(7);
|
|
134
|
+
expect(usage.at(-1)).toMatchObject({ name: "daemon", cpuPct: 0 });
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test("scans at most once per interval and reuses the last result", () => {
|
|
138
|
+
// GIVEN two completed scans
|
|
139
|
+
const tracker = createProcessUsageTracker({ procRoot });
|
|
140
|
+
writeProcess(DAEMON, "bun", 0);
|
|
141
|
+
tracker.sample(0, DAEMON);
|
|
142
|
+
writeProcess(DAEMON, "bun", 50);
|
|
143
|
+
const first = tracker.sample(1_000, DAEMON);
|
|
144
|
+
|
|
145
|
+
// WHEN a tick arrives 250ms later with new counters
|
|
146
|
+
writeProcess(DAEMON, "bun", 999);
|
|
147
|
+
const again = tracker.sample(1_250, DAEMON);
|
|
148
|
+
|
|
149
|
+
// THEN the previous result is reused, not rescanned
|
|
150
|
+
expect(again).toBe(first);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("an unreadable /proc yields null", () => {
|
|
154
|
+
const tracker = createProcessUsageTracker({
|
|
155
|
+
procRoot: join(procRoot, "missing"),
|
|
156
|
+
});
|
|
157
|
+
expect(tracker.sample(0, DAEMON)).toBeNull();
|
|
158
|
+
expect(tracker.sample(2_000, DAEMON)).toBeNull();
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test("only allowlisted worker scripts get a worker label", () => {
|
|
162
|
+
// GIVEN the real schedule worker and a user script shaped like a worker
|
|
163
|
+
const tracker = createProcessUsageTracker({ procRoot });
|
|
164
|
+
const schedule = ["bun", "run", "/app/assistant/src/schedule/worker.ts"];
|
|
165
|
+
const lookalike = ["bun", "run", "/home/me/acme-payroll-worker.ts"];
|
|
166
|
+
writeProcess(60, "bun", 0, { cmdline: schedule });
|
|
167
|
+
writeProcess(61, "bun", 0, { cmdline: lookalike });
|
|
168
|
+
tracker.sample(0, DAEMON);
|
|
169
|
+
writeProcess(60, "bun", 20, { cmdline: schedule });
|
|
170
|
+
writeProcess(61, "bun", 10, { cmdline: lookalike });
|
|
171
|
+
|
|
172
|
+
// WHEN they are reported
|
|
173
|
+
const names = tracker.sample(1_000, DAEMON)!.map((p) => p.name);
|
|
174
|
+
|
|
175
|
+
// THEN the look-alike falls back to the kernel comm
|
|
176
|
+
expect(names).toEqual(["schedule-worker", "bun"]);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test("an idle process holding the most memory is still reported", () => {
|
|
180
|
+
// GIVEN six small busy processes and an idle, memory-heavy one
|
|
181
|
+
const tracker = createProcessUsageTracker({ procRoot });
|
|
182
|
+
for (let pid = 70; pid < 76; pid++) {
|
|
183
|
+
writeProcess(pid, `busy-${pid}`, 0, { rssMb: 5 });
|
|
184
|
+
}
|
|
185
|
+
writeProcess(80, "qdrant", 0, { rssMb: 2_000 });
|
|
186
|
+
tracker.sample(0, DAEMON);
|
|
187
|
+
for (let pid = 70; pid < 76; pid++) {
|
|
188
|
+
writeProcess(pid, `busy-${pid}`, 10, { rssMb: 5 });
|
|
189
|
+
}
|
|
190
|
+
writeProcess(80, "qdrant", 0, { rssMb: 2_000 });
|
|
191
|
+
|
|
192
|
+
// WHEN the next scan runs
|
|
193
|
+
const usage = tracker.sample(1_000, DAEMON)!;
|
|
194
|
+
|
|
195
|
+
// THEN the memory-heavy process takes a memory slot
|
|
196
|
+
expect(usage).toContainEqual({
|
|
197
|
+
pid: 80,
|
|
198
|
+
name: "qdrant",
|
|
199
|
+
cpuPct: 0,
|
|
200
|
+
rssMb: 2_000,
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test("converts resident pages with the kernel's page size", () => {
|
|
205
|
+
// GIVEN a 64 KiB-page kernel and a process with 16384 resident pages
|
|
206
|
+
const tracker = createProcessUsageTracker({
|
|
207
|
+
procRoot,
|
|
208
|
+
pageSizeBytes: 65_536,
|
|
209
|
+
});
|
|
210
|
+
writeProcess(90, "bun", 0, { rssMb: 256 }); // 256 * 256 = 65536 pages
|
|
211
|
+
tracker.sample(0, DAEMON);
|
|
212
|
+
writeProcess(90, "bun", 0, { rssMb: 256 });
|
|
213
|
+
|
|
214
|
+
// WHEN it is reported
|
|
215
|
+
const usage = tracker.sample(1_000, DAEMON)!;
|
|
216
|
+
|
|
217
|
+
// THEN RSS is 16x what a 4 KiB assumption would give
|
|
218
|
+
expect(usage[0]!.rssMb).toBe(4_096);
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
describe("readKernelPageSizeBytes", () => {
|
|
223
|
+
function writeSelf(rssKb: number, rssPages: number): void {
|
|
224
|
+
const dir = join(procRoot, "self");
|
|
225
|
+
mkdirSync(dir, { recursive: true });
|
|
226
|
+
writeFileSync(join(dir, "status"), `Name:\tbun\nVmRSS:\t ${rssKb} kB\n`);
|
|
227
|
+
writeFileSync(join(dir, "statm"), `9000 ${rssPages} 100 1 0 500 0\n`);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
test("derives 64 KiB from resident kB and pages", () => {
|
|
231
|
+
writeSelf(65_536, 1_024);
|
|
232
|
+
expect(readKernelPageSizeBytes(procRoot)).toBe(65_536);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
test("snaps a slightly skewed reading to the nearest page size", () => {
|
|
236
|
+
writeSelf(4_100, 1_000);
|
|
237
|
+
expect(readKernelPageSizeBytes(procRoot)).toBe(4_096);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
test("falls back to 4 KiB when /proc/self is unreadable", () => {
|
|
241
|
+
expect(readKernelPageSizeBytes(procRoot)).toBe(4_096);
|
|
242
|
+
});
|
|
243
|
+
});
|
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a stalled daemon is waiting on, read from `/proc` by the resource
|
|
3
|
+
* monitor mid-stall.
|
|
4
|
+
*
|
|
5
|
+
* The kernel stack alone cannot separate two stalls that look identical in
|
|
6
|
+
* it: the main thread asleep in `epoll_wait` on the daemon's own event loop
|
|
7
|
+
* (a runtime that stopped servicing its timers), and the main thread asleep in
|
|
8
|
+
* a *nested* epoll set that a synchronous call created and waits on. This
|
|
9
|
+
* module reads what tells them apart:
|
|
10
|
+
*
|
|
11
|
+
* - the main thread's blocking syscall (raw number, host-kernel numbering)
|
|
12
|
+
* and, for an epoll wait, what the waited epoll set watches. The daemon's
|
|
13
|
+
* loop set contains its HTTP listening socket; a nested set does not.
|
|
14
|
+
* - the daemon's direct children, since a synchronous child wait keeps its
|
|
15
|
+
* child alive for the length of the stall;
|
|
16
|
+
* - every daemon thread's kernel wait channel.
|
|
17
|
+
*
|
|
18
|
+
* Metadata only: children and threads are named by their kernel `comm` (15
|
|
19
|
+
* bytes, no argv), so no command-line content leaves the machine. Every read
|
|
20
|
+
* is best-effort and yields null or an empty list when `/proc` is missing or
|
|
21
|
+
* unreadable (non-Linux, or a permission boundary).
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { readdirSync, readFileSync, readlinkSync } from "node:fs";
|
|
25
|
+
import { join } from "node:path";
|
|
26
|
+
|
|
27
|
+
/** Caps that keep the capture inside the watchdog telemetry byte budget. */
|
|
28
|
+
const MAX_CHILDREN = 5;
|
|
29
|
+
const MAX_THREAD_GROUPS = 8;
|
|
30
|
+
|
|
31
|
+
/** Linux USER_HZ; `/proc/<pid>/stat` times are in these ticks. */
|
|
32
|
+
export const CLOCK_TICKS_PER_SECOND = 100;
|
|
33
|
+
|
|
34
|
+
/** Page sizes Linux kernels use; `/proc` page counts are in one of these. */
|
|
35
|
+
const KERNEL_PAGE_SIZES = [4096, 16384, 65536] as const;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The kernel page size, which `/proc/<pid>/stat` rss and `statm` counts are
|
|
39
|
+
* in. It varies by kernel (arm64 kernels can use 16 or 64 KiB), so it is
|
|
40
|
+
* derived from this process's own resident size, reported both in kB
|
|
41
|
+
* (`status` VmRSS) and in pages (`statm`). Falls back to 4 KiB.
|
|
42
|
+
*/
|
|
43
|
+
export function readKernelPageSizeBytes(procRoot = "/proc"): number {
|
|
44
|
+
const status = readText(join(procRoot, "self", "status"));
|
|
45
|
+
const statm = readText(join(procRoot, "self", "statm"));
|
|
46
|
+
const rssKb = status
|
|
47
|
+
? Number(/^VmRSS:\s+(\d+)\s+kB/m.exec(status)?.[1])
|
|
48
|
+
: NaN;
|
|
49
|
+
const rssPages = statm ? Number(statm.trim().split(/\s+/)[1]) : NaN;
|
|
50
|
+
if (!(rssKb > 0) || !(rssPages > 0)) {
|
|
51
|
+
return KERNEL_PAGE_SIZES[0];
|
|
52
|
+
}
|
|
53
|
+
const measured = (rssKb * 1024) / rssPages;
|
|
54
|
+
// The two files are read at slightly different moments; snap to the
|
|
55
|
+
// nearest real page size.
|
|
56
|
+
return KERNEL_PAGE_SIZES.reduce((best, size) =>
|
|
57
|
+
Math.abs(Math.log2(size / measured)) < Math.abs(Math.log2(best / measured))
|
|
58
|
+
? size
|
|
59
|
+
: best,
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** What `/proc/<pid>/fd/<n>` links to for an epoll instance. */
|
|
64
|
+
const EPOLL_FD_LINK = "anon_inode:[eventpoll]";
|
|
65
|
+
|
|
66
|
+
/** TCP state code for LISTEN in `/proc/net/tcp{,6}`. */
|
|
67
|
+
const TCP_LISTEN_STATE = "0A";
|
|
68
|
+
|
|
69
|
+
export interface EpollWaitTarget {
|
|
70
|
+
/** The epoll fd the main thread is waiting on. */
|
|
71
|
+
fd: number;
|
|
72
|
+
/** Count of fds registered in that epoll set. */
|
|
73
|
+
watched: number;
|
|
74
|
+
/**
|
|
75
|
+
* Registered sockets that are listening TCP sockets. Non-zero means the
|
|
76
|
+
* waited set is the daemon's own event loop (it holds the HTTP listener);
|
|
77
|
+
* zero means a nested, private wait.
|
|
78
|
+
*/
|
|
79
|
+
listeningSockets: number;
|
|
80
|
+
sockets: number;
|
|
81
|
+
pipes: number;
|
|
82
|
+
eventfds: number;
|
|
83
|
+
timerfds: number;
|
|
84
|
+
other: number;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface StallChildProcess {
|
|
88
|
+
pid: number;
|
|
89
|
+
/** Kernel command name (no arguments). */
|
|
90
|
+
comm: string;
|
|
91
|
+
state: string | null;
|
|
92
|
+
/** How long the child had been running at capture time. */
|
|
93
|
+
ageMs: number | null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export interface StallThreadGroup {
|
|
97
|
+
comm: string;
|
|
98
|
+
state: string | null;
|
|
99
|
+
/** Kernel function the thread sleeps in (`/proc/<pid>/task/<tid>/wchan`). */
|
|
100
|
+
wchan: string | null;
|
|
101
|
+
count: number;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export interface StallWaitState {
|
|
105
|
+
/** Main thread's syscall number, "running" when on-CPU, null if unreadable. */
|
|
106
|
+
syscall: number | "running" | null;
|
|
107
|
+
/** Set when the main thread is in an epoll wait and the set is readable. */
|
|
108
|
+
epoll: EpollWaitTarget | null;
|
|
109
|
+
children: StallChildProcess[];
|
|
110
|
+
threads: StallThreadGroup[];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function readText(path: string): string | null {
|
|
114
|
+
try {
|
|
115
|
+
return readFileSync(path, "utf-8");
|
|
116
|
+
} catch {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function listNumericDirs(path: string): number[] {
|
|
122
|
+
try {
|
|
123
|
+
return readdirSync(path)
|
|
124
|
+
.filter((name) => /^\d+$/.test(name))
|
|
125
|
+
.map(Number);
|
|
126
|
+
} catch {
|
|
127
|
+
return [];
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export interface ProcStat {
|
|
132
|
+
comm: string;
|
|
133
|
+
state: string;
|
|
134
|
+
ppid: number;
|
|
135
|
+
/** User plus system CPU time consumed, in clock ticks. */
|
|
136
|
+
cpuTicks: number;
|
|
137
|
+
/** Start time after boot, in clock ticks. */
|
|
138
|
+
startTicks: number;
|
|
139
|
+
/** Resident set size, in pages. */
|
|
140
|
+
rssPages: number;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Parse `/proc/<pid>/stat` around the parenthesised comm, which may contain spaces. */
|
|
144
|
+
export function parseProcStat(raw: string): ProcStat | null {
|
|
145
|
+
const open = raw.indexOf("(");
|
|
146
|
+
const close = raw.lastIndexOf(")");
|
|
147
|
+
if (open < 0 || close < open) {
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
// Fields after the comm start at field 3 (state), so field N is at index
|
|
151
|
+
// N - 3: utime 14, stime 15, starttime 22, rss 24.
|
|
152
|
+
const fields = raw
|
|
153
|
+
.slice(close + 1)
|
|
154
|
+
.trim()
|
|
155
|
+
.split(/\s+/);
|
|
156
|
+
const ppid = Number(fields[1]);
|
|
157
|
+
const cpuTicks = Number(fields[11]) + Number(fields[12]);
|
|
158
|
+
const startTicks = Number(fields[19]);
|
|
159
|
+
const rssPages = Number(fields[21]);
|
|
160
|
+
if (
|
|
161
|
+
!fields[0] ||
|
|
162
|
+
![ppid, cpuTicks, startTicks, rssPages].every(Number.isFinite)
|
|
163
|
+
) {
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
comm: raw.slice(open + 1, close),
|
|
168
|
+
state: fields[0],
|
|
169
|
+
ppid,
|
|
170
|
+
cpuTicks,
|
|
171
|
+
startTicks,
|
|
172
|
+
rssPages,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Parse `/proc/<pid>/task/<tid>/syscall`: "running", or the syscall number
|
|
178
|
+
* followed by its arguments in hex.
|
|
179
|
+
*/
|
|
180
|
+
export function parseProcSyscall(
|
|
181
|
+
raw: string,
|
|
182
|
+
): { nr: number | "running"; args: number[] } | null {
|
|
183
|
+
const parts = raw.trim().split(/\s+/);
|
|
184
|
+
if (parts[0] === "running") {
|
|
185
|
+
return { nr: "running", args: [] };
|
|
186
|
+
}
|
|
187
|
+
if (!/^\d+$/.test(parts[0] ?? "")) {
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
return {
|
|
191
|
+
nr: Number(parts[0]),
|
|
192
|
+
args: parts.slice(1).map((arg) => Number.parseInt(arg, 16)),
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Target fds of an epoll set, from its `/proc/<pid>/fdinfo/<fd>` "tfd:" lines. */
|
|
197
|
+
export function parseEpollTargetFds(fdinfo: string): number[] {
|
|
198
|
+
const fds: number[] = [];
|
|
199
|
+
for (const match of fdinfo.matchAll(/^tfd:\s*(\d+)/gm)) {
|
|
200
|
+
fds.push(Number(match[1]));
|
|
201
|
+
}
|
|
202
|
+
return fds;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** Socket inodes in LISTEN state from `/proc/<pid>/net/tcp{,6}` content. */
|
|
206
|
+
export function parseListeningSocketInodes(netTcp: string): Set<string> {
|
|
207
|
+
const inodes = new Set<string>();
|
|
208
|
+
for (const line of netTcp.split("\n").slice(1)) {
|
|
209
|
+
// sl local rem st tx:rx tr:when retrnsmt uid timeout inode ...
|
|
210
|
+
const cols = line.trim().split(/\s+/);
|
|
211
|
+
if (cols[3] === TCP_LISTEN_STATE && cols[9]) {
|
|
212
|
+
inodes.add(cols[9]);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return inodes;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function readFdLink(procRoot: string, pid: number, fd: number): string | null {
|
|
219
|
+
try {
|
|
220
|
+
return readlinkSync(join(procRoot, String(pid), "fd", String(fd)));
|
|
221
|
+
} catch {
|
|
222
|
+
return null;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function readEpollTarget(
|
|
227
|
+
procRoot: string,
|
|
228
|
+
pid: number,
|
|
229
|
+
epfd: number,
|
|
230
|
+
): EpollWaitTarget | null {
|
|
231
|
+
const fdinfo = readText(join(procRoot, String(pid), "fdinfo", String(epfd)));
|
|
232
|
+
if (fdinfo == null) {
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
const listening = new Set<string>();
|
|
236
|
+
for (const file of ["tcp", "tcp6"]) {
|
|
237
|
+
const table = readText(join(procRoot, String(pid), "net", file));
|
|
238
|
+
if (table != null) {
|
|
239
|
+
for (const inode of parseListeningSocketInodes(table)) {
|
|
240
|
+
listening.add(inode);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const target: EpollWaitTarget = {
|
|
246
|
+
fd: epfd,
|
|
247
|
+
watched: 0,
|
|
248
|
+
listeningSockets: 0,
|
|
249
|
+
sockets: 0,
|
|
250
|
+
pipes: 0,
|
|
251
|
+
eventfds: 0,
|
|
252
|
+
timerfds: 0,
|
|
253
|
+
other: 0,
|
|
254
|
+
};
|
|
255
|
+
for (const fd of parseEpollTargetFds(fdinfo)) {
|
|
256
|
+
target.watched++;
|
|
257
|
+
const link = readFdLink(procRoot, pid, fd);
|
|
258
|
+
if (link == null) {
|
|
259
|
+
target.other++;
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
const socket = /^socket:\[(\d+)\]$/.exec(link);
|
|
263
|
+
if (socket) {
|
|
264
|
+
target.sockets++;
|
|
265
|
+
if (listening.has(socket[1]!)) {
|
|
266
|
+
target.listeningSockets++;
|
|
267
|
+
}
|
|
268
|
+
} else if (link.startsWith("pipe:")) {
|
|
269
|
+
target.pipes++;
|
|
270
|
+
} else if (link === "anon_inode:[eventfd]") {
|
|
271
|
+
target.eventfds++;
|
|
272
|
+
} else if (link === "anon_inode:[timerfd]") {
|
|
273
|
+
target.timerfds++;
|
|
274
|
+
} else {
|
|
275
|
+
target.other++;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return target;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function readChildren(procRoot: string, pid: number): StallChildProcess[] {
|
|
282
|
+
const uptimeRaw = readText(join(procRoot, "uptime"));
|
|
283
|
+
const uptimeMs =
|
|
284
|
+
uptimeRaw != null ? Number(uptimeRaw.split(/\s+/)[0]) * 1000 : NaN;
|
|
285
|
+
const children: StallChildProcess[] = [];
|
|
286
|
+
for (const candidate of listNumericDirs(procRoot)) {
|
|
287
|
+
const stat = readText(join(procRoot, String(candidate), "stat"));
|
|
288
|
+
const parsed = stat != null ? parseProcStat(stat) : null;
|
|
289
|
+
if (parsed == null || parsed.ppid !== pid) {
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
const startedMs = (parsed.startTicks / CLOCK_TICKS_PER_SECOND) * 1000;
|
|
293
|
+
children.push({
|
|
294
|
+
pid: candidate,
|
|
295
|
+
comm: parsed.comm,
|
|
296
|
+
state: parsed.state,
|
|
297
|
+
ageMs: Number.isFinite(uptimeMs)
|
|
298
|
+
? Math.max(0, Math.round(uptimeMs - startedMs))
|
|
299
|
+
: null,
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
// Youngest first: a child spawned for the stalled call is the newest one.
|
|
303
|
+
children.sort((a, b) => (a.ageMs ?? Infinity) - (b.ageMs ?? Infinity));
|
|
304
|
+
return children.slice(0, MAX_CHILDREN);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function readThreadGroups(procRoot: string, pid: number): StallThreadGroup[] {
|
|
308
|
+
const groups = new Map<string, StallThreadGroup>();
|
|
309
|
+
for (const tid of listNumericDirs(join(procRoot, String(pid), "task"))) {
|
|
310
|
+
const taskDir = join(procRoot, String(pid), "task", String(tid));
|
|
311
|
+
const stat = readText(join(taskDir, "stat"));
|
|
312
|
+
const parsed = stat != null ? parseProcStat(stat) : null;
|
|
313
|
+
const wchanRaw = readText(join(taskDir, "wchan"))?.trim();
|
|
314
|
+
const group = {
|
|
315
|
+
comm: parsed?.comm ?? "?",
|
|
316
|
+
state: parsed?.state ?? null,
|
|
317
|
+
// "0" means the thread is not sleeping in the kernel.
|
|
318
|
+
wchan: wchanRaw && wchanRaw !== "0" ? wchanRaw : null,
|
|
319
|
+
};
|
|
320
|
+
const key = `${group.comm}\0${group.state}\0${group.wchan}`;
|
|
321
|
+
const existing = groups.get(key);
|
|
322
|
+
if (existing) {
|
|
323
|
+
existing.count++;
|
|
324
|
+
} else {
|
|
325
|
+
groups.set(key, { ...group, count: 1 });
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return [...groups.values()]
|
|
329
|
+
.sort((a, b) => b.count - a.count)
|
|
330
|
+
.slice(0, MAX_THREAD_GROUPS);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Read what the process `pid` is waiting on. `procRoot` is injectable so
|
|
335
|
+
* tests can run against a fixture tree on any platform.
|
|
336
|
+
*/
|
|
337
|
+
export function readStallWaitState(
|
|
338
|
+
pid: number,
|
|
339
|
+
procRoot = "/proc",
|
|
340
|
+
): StallWaitState {
|
|
341
|
+
// The main thread's tid equals the pid.
|
|
342
|
+
const syscallRaw = readText(
|
|
343
|
+
join(procRoot, String(pid), "task", String(pid), "syscall"),
|
|
344
|
+
);
|
|
345
|
+
const syscall = syscallRaw != null ? parseProcSyscall(syscallRaw) : null;
|
|
346
|
+
|
|
347
|
+
// An epoll wait is recognised by its first argument being an epoll fd, not
|
|
348
|
+
// by syscall number: the numbers differ per architecture, and an emulated
|
|
349
|
+
// process reports the host kernel's numbering.
|
|
350
|
+
let epoll: EpollWaitTarget | null = null;
|
|
351
|
+
const firstArg = syscall?.args[0];
|
|
352
|
+
if (
|
|
353
|
+
firstArg !== undefined &&
|
|
354
|
+
Number.isInteger(firstArg) &&
|
|
355
|
+
readFdLink(procRoot, pid, firstArg) === EPOLL_FD_LINK
|
|
356
|
+
) {
|
|
357
|
+
epoll = readEpollTarget(procRoot, pid, firstArg);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return {
|
|
361
|
+
syscall: syscall?.nr ?? null,
|
|
362
|
+
epoll,
|
|
363
|
+
children: readChildren(procRoot, pid),
|
|
364
|
+
threads: readThreadGroups(procRoot, pid),
|
|
365
|
+
};
|
|
366
|
+
}
|
|
@@ -17,9 +17,10 @@ import {
|
|
|
17
17
|
type ProcessTableRow,
|
|
18
18
|
} from "../util/process-table.js";
|
|
19
19
|
import { deriveName, readProcessCommand } from "../util/process-tree.js";
|
|
20
|
+
import { readKernelPageSizeBytes } from "./proc-wait-state.js";
|
|
20
21
|
|
|
21
|
-
/**
|
|
22
|
-
|
|
22
|
+
/** Kernel page size for `/proc/<pid>/statm` pages, read on first use. */
|
|
23
|
+
let pageSizeBytes: number | undefined;
|
|
23
24
|
|
|
24
25
|
export interface SmapsRollup {
|
|
25
26
|
rssBytes: number | null;
|
|
@@ -74,7 +75,7 @@ function readProcessMemory(pid: number): SmapsRollup | null {
|
|
|
74
75
|
return null;
|
|
75
76
|
}
|
|
76
77
|
return {
|
|
77
|
-
rssBytes: residentPages *
|
|
78
|
+
rssBytes: residentPages * (pageSizeBytes ??= readKernelPageSizeBytes()),
|
|
78
79
|
pssBytes: null,
|
|
79
80
|
pssAnonBytes: null,
|
|
80
81
|
pssFileBytes: null,
|