pi-lens 3.8.73 → 4.0.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.
- package/CHANGELOG.md +2614 -12
- package/README.md +50 -21
- package/dist/clients/actionable-warnings.js +73 -18
- package/dist/clients/agent-behavior-client.js +21 -4
- package/dist/clients/ast-grep-client.js +68 -16
- package/dist/clients/ast-grep-types.js +0 -1
- package/dist/clients/ast-grep-yaml-synth.js +37 -10
- package/dist/clients/atomic-write-staging.js +63 -0
- package/dist/clients/atomic-write.js +116 -16
- package/dist/clients/bash-file-access.js +193 -23
- package/dist/clients/biome-client.js +29 -25
- package/dist/clients/bootstrap.js +11 -2
- package/dist/clients/bounded-cache.js +34 -0
- package/dist/clients/bounded-pid-file-lock.js +192 -0
- package/dist/clients/bus-events-logger.js +5 -1
- package/dist/clients/bus-publish.js +10 -3
- package/dist/clients/cache/rule-cache.js +89 -6
- package/dist/clients/cache-manager.js +97 -9
- package/dist/clients/cache-observability.js +498 -0
- package/dist/clients/call-graph.js +460 -108
- package/dist/clients/cascade-logger.js +5 -1
- package/dist/clients/child-unref.js +85 -0
- package/dist/clients/codebase-model.js +52 -23
- package/dist/clients/collateral-test-role.js +40 -0
- package/dist/clients/complexity-client.js +6 -8
- package/dist/clients/console-guard-install.js +14 -0
- package/dist/clients/cooperative-budget.js +42 -0
- package/dist/clients/dead-code-client.js +2 -1
- package/dist/clients/debug-handles.js +206 -0
- package/dist/clients/debug-heap.js +167 -0
- package/dist/clients/degradation-ledger.js +153 -0
- package/dist/clients/dependency-checker.js +466 -63
- package/dist/clients/diagnostic-dispositions.js +83 -13
- package/dist/clients/diagnostics-publish.js +10 -3
- package/dist/clients/dispatch/auxiliary-lsp.js +34 -0
- package/dist/clients/dispatch/dispatcher.js +71 -13
- package/dist/clients/dispatch/fact-store.js +11 -0
- package/dist/clients/dispatch/facts/function-facts.js +37 -7
- package/dist/clients/dispatch/facts/import-facts.js +108 -119
- package/dist/clients/dispatch/facts/tree-sitter-facts.js +12 -12
- package/dist/clients/dispatch/facts/try-catch-facts.js +13 -0
- package/dist/clients/dispatch/indent-detect.js +35 -0
- package/dist/clients/dispatch/inline-suppressions.js +17 -8
- package/dist/clients/dispatch/integration.js +1094 -611
- package/dist/clients/dispatch/lazy.js +14 -0
- package/dist/clients/dispatch/plan.js +11 -1
- package/dist/clients/dispatch/rule-id-normalize.js +50 -0
- package/dist/clients/dispatch/rule-policy.js +135 -0
- package/dist/clients/dispatch/rules/async-noise.js +1 -0
- package/dist/clients/dispatch/rules/unsafe-boundary.js +7 -0
- package/dist/clients/dispatch/runners/ast-grep-napi.js +47 -6
- package/dist/clients/dispatch/runners/biome-check.js +5 -9
- package/dist/clients/dispatch/runners/detekt.js +3 -2
- package/dist/clients/dispatch/runners/hadolint.js +2 -1
- package/dist/clients/dispatch/runners/helm-lint.js +154 -0
- package/dist/clients/dispatch/runners/htmlhint.js +3 -2
- package/dist/clients/dispatch/runners/index.js +5 -1
- package/dist/clients/dispatch/runners/lsp.js +14 -6
- package/dist/clients/dispatch/runners/markdownlint.js +4 -9
- package/dist/clients/dispatch/runners/psscriptanalyzer.js +10 -1
- package/dist/clients/dispatch/runners/pyright.js +9 -5
- package/dist/clients/dispatch/runners/ruff.js +4 -8
- package/dist/clients/dispatch/runners/shellcheck.js +2 -3
- package/dist/clients/dispatch/runners/shfmt.js +2 -3
- package/dist/clients/dispatch/runners/spotbugs.js +2 -1
- package/dist/clients/dispatch/runners/taplo.js +2 -1
- package/dist/clients/dispatch/runners/terragrunt.js +163 -0
- package/dist/clients/dispatch/runners/tflint.js +37 -5
- package/dist/clients/dispatch/runners/tree-sitter.js +155 -166
- package/dist/clients/dispatch/runners/trivy-config.js +13 -7
- package/dist/clients/dispatch/runners/utils/lazy-installer.js +3 -0
- package/dist/clients/dispatch/runners/utils/runner-helpers.js +284 -34
- package/dist/clients/dispatch/runners/utils/spawn-outcome.js +22 -0
- package/dist/clients/dispatch/runners/yaml-rule-parser.js +5 -4
- package/dist/clients/disposition-publish.js +10 -3
- package/dist/clients/durable-store.js +97 -0
- package/dist/clients/event-loop-monitor.js +94 -3
- package/dist/clients/extension-log.js +139 -0
- package/dist/clients/extension-mode.js +73 -0
- package/dist/clients/feature-hints.js +53 -7
- package/dist/clients/file-kinds.js +143 -31
- package/dist/clients/file-role.js +18 -3
- package/dist/clients/file-utils.js +56 -34
- package/dist/clients/fix-worklog.js +14 -2
- package/dist/clients/format-events-publish.js +16 -3
- package/dist/clients/format-service.js +24 -8
- package/dist/clients/formatters-lazy.js +8 -0
- package/dist/clients/formatters.js +358 -61
- package/dist/clients/generated-artifacts.js +169 -15
- package/dist/clients/git-guard.js +681 -11
- package/dist/clients/git-tracked-ignore.js +4 -0
- package/dist/clients/go-client.js +2 -1
- package/dist/clients/govulncheck-client.js +8 -0
- package/dist/clients/grammar-source.js +12 -4
- package/dist/clients/gzip-stage-write.js +102 -0
- package/dist/clients/host-ports.js +31 -0
- package/dist/clients/installer/index.js +733 -213
- package/dist/clients/instance-reaper.js +142 -172
- package/dist/clients/jscpd-client.js +31 -54
- package/dist/clients/knip-client.js +78 -36
- package/dist/clients/language-policy.js +14 -2
- package/dist/clients/language-profile.js +72 -77
- package/dist/clients/latency-logger.js +28 -16
- package/dist/clients/lens-config.js +140 -166
- package/dist/clients/lens-engine.js +124 -21
- package/dist/clients/lens-events.js +27 -4
- package/dist/clients/lens-flag-registry.js +293 -0
- package/dist/clients/lens-map.js +96 -26
- package/dist/clients/live-bus-emitter.js +30 -0
- package/dist/clients/log-cleanup.js +8 -18
- package/dist/clients/lsp/cascade-tier.js +23 -3
- package/dist/clients/lsp/client.js +566 -57
- package/dist/clients/lsp/config.js +74 -10
- package/dist/clients/lsp/diagnostic-binding.js +86 -0
- package/dist/clients/lsp/edits.js +1012 -121
- package/dist/clients/lsp/index.js +1840 -803
- package/dist/clients/lsp/language.js +4 -1
- package/dist/clients/lsp/launch.js +40 -32
- package/dist/clients/lsp/path-utils.js +1 -1
- package/dist/clients/lsp/ruby-drive-dirs.js +83 -0
- package/dist/clients/lsp/server.js +279 -60
- package/dist/clients/lsp/workspace-diagnostics-cache.js +28 -4
- package/dist/clients/lsp-document-symbols.js +112 -0
- package/dist/clients/lsp-lazy.js +8 -0
- package/dist/clients/lsp-mutation.js +248 -0
- package/dist/clients/mcp/analyze.js +128 -36
- package/dist/clients/mcp/ipc.js +157 -24
- package/dist/clients/mcp/review.js +36 -3
- package/dist/clients/mcp/session.js +216 -8
- package/dist/clients/memory-sampler.js +115 -0
- package/dist/clients/metrics-client.js +2 -1
- package/dist/clients/metrics-history.js +2 -1
- package/dist/clients/module-report-lsp.js +5 -0
- package/dist/clients/module-report.js +267 -82
- package/dist/clients/ndjson-logger.js +334 -103
- package/dist/clients/opengrep-client.js +1 -1
- package/dist/clients/partial-edit-apply.js +120 -5
- package/dist/clients/path-keyed-map.js +76 -0
- package/dist/clients/path-utils.js +141 -5
- package/dist/clients/performance-report.js +327 -0
- package/dist/clients/pipeline.js +54 -13
- package/dist/clients/print-mode.js +21 -0
- package/dist/clients/project-changes.js +117 -4
- package/dist/clients/project-diagnostics/cache.js +3 -2
- package/dist/clients/project-diagnostics/extractors.js +26 -115
- package/dist/clients/project-diagnostics/fresh-fetch.js +149 -19
- package/dist/clients/project-diagnostics/runner-adapters/call-graph-impact.js +117 -0
- package/dist/clients/project-diagnostics/runner-adapters/runner-findings.js +37 -5
- package/dist/clients/project-diagnostics/scanner.js +268 -128
- package/dist/clients/project-lens-config.js +138 -34
- package/dist/clients/project-report.js +64 -25
- package/dist/clients/project-scale.js +8 -10
- package/dist/clients/project-snapshot-persist-worker.js +6 -0
- package/dist/clients/project-snapshot.js +767 -8
- package/dist/clients/project-trust.js +181 -0
- package/dist/clients/quiet-window.js +27 -5
- package/dist/clients/read-bridge.js +147 -0
- package/dist/clients/read-expansion.js +22 -12
- package/dist/clients/read-guard-logger.js +159 -1
- package/dist/clients/read-guard-tool-lines.js +150 -29
- package/dist/clients/read-guard.js +262 -3
- package/dist/clients/redact/secrets.js +449 -0
- package/dist/clients/resource-sampler.js +144 -39
- package/dist/clients/reverse-deps.js +48 -0
- package/dist/clients/review-graph/builder.js +2570 -326
- package/dist/clients/review-graph/git-identity.js +2 -1
- package/dist/clients/review-graph/import-resolvers.js +21 -10
- package/dist/clients/review-graph/persist-worker.js +10 -0
- package/dist/clients/review-graph/query.js +5 -0
- package/dist/clients/review-graph/shared-extraction-ir.js +90 -0
- package/dist/clients/review-graph/symbol-id.js +46 -0
- package/dist/clients/review-graph/tsconfig-paths.js +57 -6
- package/dist/clients/review-graph/workspace-modules.js +51 -3
- package/dist/clients/review-graph-logger.js +53 -0
- package/dist/clients/ruff-client.js +27 -44
- package/dist/clients/runtime-agent-end.js +111 -75
- package/dist/clients/runtime-context.js +36 -12
- package/dist/clients/runtime-coordinator.js +76 -8
- package/dist/clients/runtime-session.js +799 -137
- package/dist/clients/runtime-tool-call.js +117 -62
- package/dist/clients/runtime-tool-result.js +218 -23
- package/dist/clients/runtime-turn.js +284 -40
- package/dist/clients/rust-client.js +2 -1
- package/dist/clients/safe-spawn.js +873 -71
- package/dist/clients/security-scan-client.js +2 -1
- package/dist/clients/session-start-observability.js +15 -0
- package/dist/clients/session-state-store.js +5 -3
- package/dist/clients/sessionstart-logger.js +18 -0
- package/dist/clients/sg-runner.js +284 -159
- package/dist/clients/slow-fs.js +2 -1
- package/dist/clients/smells-rollup.js +224 -0
- package/dist/clients/source-filter.js +231 -43
- package/dist/clients/source-walker.js +66 -10
- package/dist/clients/startup-scan.js +15 -16
- package/dist/clients/startup-timing.js +9 -0
- package/dist/clients/test-runner-client.js +38 -8
- package/dist/clients/todo-scanner.js +30 -3
- package/dist/clients/tool-event.js +35 -0
- package/dist/clients/tool-policy.js +388 -39
- package/dist/clients/tool-render.js +155 -0
- package/dist/clients/tree-sitter-cache.js +153 -140
- package/dist/clients/tree-sitter-client.js +1722 -80
- package/dist/clients/tree-sitter-logger.js +64 -1
- package/dist/clients/tree-sitter-query-loader.js +117 -12
- package/dist/clients/tree-sitter-shared.js +48 -19
- package/dist/clients/tree-sitter-symbol-extractor.js +115 -11
- package/dist/clients/user-notify.js +51 -0
- package/dist/clients/vanished-instance-marker.js +57 -0
- package/dist/clients/warm-attach.js +33 -13
- package/dist/clients/widget-state.js +342 -49
- package/dist/clients/word-index-logger.js +44 -0
- package/dist/clients/word-index.js +612 -68
- package/dist/clients/workspace-topology.js +100 -7
- package/dist/index.js +65923 -47161
- package/dist/mcp/analyze-cli.js +185 -29
- package/dist/mcp/cli.js +110 -0
- package/dist/mcp/server.js +272 -50
- package/dist/tools/ast-grep-search.js +93 -27
- package/dist/tools/lens-diagnostics.js +247 -41
- package/dist/tools/lsp-diagnostics.js +153 -40
- package/dist/tools/lsp-navigation.js +51 -31
- package/dist/tools/module-report.js +9 -0
- package/dist/tools/symbol-search.js +21 -2
- package/docs/agent-guide.md +247 -0
- package/docs/agent-tools.md +21 -10
- package/docs/analysisall.md +192 -0
- package/docs/api-ports-inventory.md +165 -0
- package/docs/ast-grep_rules_catalog.md +5 -2
- package/docs/astplayground.md +3 -4
- package/docs/audit1.md +5 -5
- package/docs/durable-store-audit-1202.md +32 -0
- package/docs/environment-variables.md +197 -11
- package/docs/fable.md +27 -0
- package/docs/features.md +35 -5
- package/docs/globalconfig.md +101 -5
- package/docs/language-coverage.md +6 -5
- package/docs/lsp-capability-matrix.md +2 -2
- package/docs/mcp.md +54 -20
- package/docs/servercapabilities.md +7 -4
- package/docs/settings.md +192 -0
- package/docs/tools.md +18 -1
- package/docs/tree-sitter_rules_catalog.md +11 -31
- package/docs/usage.md +31 -0
- package/package.json +139 -138
- package/rules/ast-grep-rules/rule-tests/hardcoded-url-js-test.yml +1 -0
- package/rules/ast-grep-rules/rule-tests/hardcoded-url-test.yml +2 -0
- package/rules/ast-grep-rules/rule-tests/no-bare-except-test.yml +49 -0
- package/rules/ast-grep-rules/rule-tests/no-bare-host-path-in-win32-branch-test.yml +87 -0
- package/rules/ast-grep-rules/rule-tests/no-case-declarations-js-test.yml +10 -0
- package/rules/ast-grep-rules/rule-tests/no-case-declarations-test.yml +10 -0
- package/rules/ast-grep-rules/rule-tests/no-javascript-url-js-test.yml +10 -0
- package/rules/ast-grep-rules/rule-tests/no-javascript-url-test.yml +10 -0
- package/rules/ast-grep-rules/rule-tests/no-nested-links-test.yml +11 -1
- package/rules/ast-grep-rules/rule-tests/no-raw-json-store-write-test.yml +17 -0
- package/rules/ast-grep-rules/rule-tests/no-win32-isabsolute-for-qualification-test.yml +16 -0
- package/rules/ast-grep-rules/rule-tests/strict-equality-js-test.yml +3 -0
- package/rules/ast-grep-rules/rule-tests/strict-equality-test.yml +3 -0
- package/rules/ast-grep-rules/rule-tests/strict-inequality-js-test.yml +3 -0
- package/rules/ast-grep-rules/rule-tests/strict-inequality-test.yml +4 -0
- package/rules/ast-grep-rules/rules/hardcoded-url-js.yml +22 -4
- package/rules/ast-grep-rules/rules/hardcoded-url.yml +22 -4
- package/rules/ast-grep-rules/rules/no-bare-except.yml +3 -15
- package/rules/ast-grep-rules/rules/no-bare-host-path-in-win32-branch.yml +25 -0
- package/rules/ast-grep-rules/rules/no-case-declarations-js.yml +4 -3
- package/rules/ast-grep-rules/rules/no-case-declarations.yml +4 -3
- package/rules/ast-grep-rules/rules/no-console-except-error-js.yml +10 -0
- package/rules/ast-grep-rules/rules/no-console-except-error.yml +10 -0
- package/rules/ast-grep-rules/rules/no-javascript-url-js.yml +42 -6
- package/rules/ast-grep-rules/rules/no-javascript-url.yml +42 -6
- package/rules/ast-grep-rules/rules/no-nested-links.yml +7 -0
- package/rules/ast-grep-rules/rules/no-raw-json-store-write.yml +30 -0
- package/rules/ast-grep-rules/rules/no-win32-isabsolute-for-qualification.yml +23 -0
- package/rules/ast-grep-rules/rules/strict-equality-js.yml +14 -1
- package/rules/ast-grep-rules/rules/strict-equality.yml +14 -1
- package/rules/ast-grep-rules/rules/strict-inequality-js.yml +14 -1
- package/rules/ast-grep-rules/rules/strict-inequality.yml +14 -1
- package/rules/tree-sitter-queries/cpp/no-memset-sensitive-data.yml +9 -3
- package/rules/tree-sitter-queries/cpp/no-scoped-lock-without-args.yml +6 -6
- package/rules/tree-sitter-queries/css/calc-spacing.yml +8 -6
- package/rules/tree-sitter-queries/go/go-mutex-copy.yml +21 -18
- package/rules/tree-sitter-queries/go/go-shared-map-write-goroutine.yml +3 -3
- package/rules/tree-sitter-queries/java/infinite-loop-java.yml +3 -3
- package/rules/tree-sitter-queries/java/junit-call-super.yml +6 -4
- package/rules/tree-sitter-queries/java/no-double-checked-locking.yml +10 -8
- package/rules/tree-sitter-queries/java/no-field-shadowing.yml +1 -1
- package/rules/tree-sitter-queries/java/prepared-statement-valid-indices.yml +2 -3
- package/rules/tree-sitter-queries/java/resources-closed.yml +2 -2
- package/rules/tree-sitter-queries/java/springboot-default-package.yml +6 -3
- package/rules/tree-sitter-queries/java/switch-fall-through.yml +6 -4
- package/rules/tree-sitter-queries/java/switch-non-case-labels.yml +4 -3
- package/rules/tree-sitter-queries/java/tests-include-assertions.yml +6 -5
- package/rules/tree-sitter-queries/javascript/switch-case-termination-js.yml +5 -8
- package/rules/tree-sitter-queries/javascript/switch-non-case-labels.yml +3 -2
- package/rules/tree-sitter-queries/kotlin/prepared-statement-indices.yml +9 -5
- package/rules/tree-sitter-queries/php/this-in-static-context.yml +5 -5
- package/rules/tree-sitter-queries/python/exit-signature-check.yml +2 -9
- package/rules/tree-sitter-queries/python/in-operator-unsupported.yml +3 -4
- package/rules/tree-sitter-queries/python/no-super-torchscript.yml +4 -8
- package/rules/tree-sitter-queries/python/notimplemented-boolean-context.yml +1 -5
- package/rules/tree-sitter-queries/python/python-empty-except.yml +1 -1
- package/rules/tree-sitter-queries/python/return-in-init.yml +0 -2
- package/rules/tree-sitter-queries/python/yield-return-outside-function.yml +0 -3
- package/rules/tree-sitter-queries/ruby/ruby-command-injection.yml +1 -12
- package/rules/tree-sitter-queries/ruby/ruby-eval.yml +1 -1
- package/rules/tree-sitter-queries/ruby/ruby-insecure-deserialization.yml +4 -4
- package/rules/tree-sitter-queries/ruby/ruby-insecure-random.yml +4 -11
- package/rules/tree-sitter-queries/ruby/ruby-open-struct.yml +1 -1
- package/rules/tree-sitter-queries/ruby/ruby-string-eval.yml +2 -3
- package/rules/tree-sitter-queries/ruby/ruby-weak-hash.yml +4 -4
- package/rules/tree-sitter-queries/rust/rust-lock-held-across-await.yml +4 -3
- package/rules/tree-sitter-queries/tsx/no-nested-links.yml +4 -6
- package/rules/tree-sitter-queries/typescript/console-statement.yml +11 -0
- package/rules/tree-sitter-queries/typescript/duplicate-function-arg.yml +4 -9
- package/rules/tree-sitter-queries/typescript/empty-switch-case.yml +13 -9
- package/rules/tree-sitter-queries/typescript/infinite-loop.yml +2 -2
- package/rules/tree-sitter-queries/typescript/mixed-async-styles.yml +10 -1
- package/rules/tree-sitter-queries/typescript/switch-case-termination.yml +5 -8
- package/rules/tree-sitter-queries/typescript/ts-insecure-random.yml +7 -10
- package/rules/tree-sitter-queries/typescript/unsafe-regex.yml +4 -2
- package/rules/typos/_typos.toml +17 -0
- package/scripts/analyze-pi-lens-logs.mjs +55 -2
- package/skills/pi-lens-write-tree-sitter-rule/SKILL.md +5 -0
- package/dist/clients/ast-grep-parser.js +0 -86
- package/dist/clients/config-enabled-shape.js +0 -39
- package/dist/clients/lsp/interactive-install.js +0 -367
- package/dist/clients/lsp/lsp-index.js +0 -10
- package/dist/clients/lsp/server-strategies.js +0 -5
- package/dist/clients/project-metadata.js +0 -690
- package/dist/clients/source-groups.js +0 -96
- package/dist/clients/types.js +0 -11
- package/docs/env_variables.md +0 -10
- package/rules/tree-sitter-queries/abap-disabled/delete-where.yml +0 -46
- package/rules/tree-sitter-queries/cpp/no-confused-move-forward.yml +0 -59
- package/rules/tree-sitter-queries/cpp/noexcept-functions.yml +0 -58
- package/rules/tree-sitter-queries/java/mockito-initialized.yml +0 -66
- package/rules/tree-sitter-queries/java/no-wait-notify-on-thread.yml +0 -58
- package/rules/tree-sitter-queries/java/short-circuit-logic.yml +0 -57
- package/rules/tree-sitter-queries/java/spring-session-attributes-setcomplete.yml +0 -75
- package/rules/tree-sitter-queries/plsql-disabled/delete-update-where.yml +0 -56
- package/rules/tree-sitter-queries/plsql-disabled/end-loop-semicolon.yml +0 -51
- package/rules/tree-sitter-queries/plsql-disabled/fetch-bulk-collect-limit.yml +0 -47
- package/rules/tree-sitter-queries/plsql-disabled/forallsave-exceptions.yml +0 -51
- package/rules/tree-sitter-queries/plsql-disabled/nchar-nvarchar2-bytes.yml +0 -54
- package/rules/tree-sitter-queries/plsql-disabled/not-null-initialization.yml +0 -59
- package/rules/tree-sitter-queries/plsql-disabled/raise-application-error-codes.yml +0 -50
- package/rules/tree-sitter-queries/typescript-disabled/ts-path-traversal.yml +0 -71
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
const PEM_BEGIN = "-----BEGIN ";
|
|
2
|
+
const PEM_DELIMITER = "-----";
|
|
3
|
+
function codePointOf(char) {
|
|
4
|
+
return char?.codePointAt(0) ?? -1;
|
|
5
|
+
}
|
|
6
|
+
function isAsciiAlphaNumeric(char) {
|
|
7
|
+
const code = codePointOf(char);
|
|
8
|
+
return ((code >= 48 && code <= 57) ||
|
|
9
|
+
(code >= 65 && code <= 90) ||
|
|
10
|
+
(code >= 97 && code <= 122));
|
|
11
|
+
}
|
|
12
|
+
function isUpperAlphaNumeric(char) {
|
|
13
|
+
const code = codePointOf(char);
|
|
14
|
+
return (code >= 48 && code <= 57) || (code >= 65 && code <= 90);
|
|
15
|
+
}
|
|
16
|
+
function isIdentifierChar(char) {
|
|
17
|
+
return char === "_" || isAsciiAlphaNumeric(char);
|
|
18
|
+
}
|
|
19
|
+
function isTokenChar(char) {
|
|
20
|
+
return char === "-" || isIdentifierChar(char);
|
|
21
|
+
}
|
|
22
|
+
const PREFIXED_SECRETS = [
|
|
23
|
+
{
|
|
24
|
+
prefix: "ghp_",
|
|
25
|
+
minSuffixLength: 20,
|
|
26
|
+
predicate: isAsciiAlphaNumeric,
|
|
27
|
+
replacement: "[REDACTED:github-token]",
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
prefix: "gho_",
|
|
31
|
+
minSuffixLength: 20,
|
|
32
|
+
predicate: isAsciiAlphaNumeric,
|
|
33
|
+
replacement: "[REDACTED:github-token]",
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
prefix: "ghu_",
|
|
37
|
+
minSuffixLength: 20,
|
|
38
|
+
predicate: isAsciiAlphaNumeric,
|
|
39
|
+
replacement: "[REDACTED:github-token]",
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
prefix: "ghs_",
|
|
43
|
+
minSuffixLength: 20,
|
|
44
|
+
predicate: isAsciiAlphaNumeric,
|
|
45
|
+
replacement: "[REDACTED:github-token]",
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
prefix: "ghr_",
|
|
49
|
+
minSuffixLength: 20,
|
|
50
|
+
predicate: isAsciiAlphaNumeric,
|
|
51
|
+
replacement: "[REDACTED:github-token]",
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
prefix: "github_pat_",
|
|
55
|
+
minSuffixLength: 20,
|
|
56
|
+
predicate: isTokenChar,
|
|
57
|
+
replacement: "[REDACTED:github-token]",
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
prefix: "AKIA",
|
|
61
|
+
minSuffixLength: 16,
|
|
62
|
+
predicate: isUpperAlphaNumeric,
|
|
63
|
+
replacement: "[REDACTED:aws-access-key]",
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
prefix: "ASIA",
|
|
67
|
+
minSuffixLength: 16,
|
|
68
|
+
predicate: isUpperAlphaNumeric,
|
|
69
|
+
replacement: "[REDACTED:aws-access-key]",
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
prefix: "sk_live_",
|
|
73
|
+
minSuffixLength: 16,
|
|
74
|
+
predicate: isAsciiAlphaNumeric,
|
|
75
|
+
replacement: "[REDACTED:stripe-key]",
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
prefix: "sk_test_",
|
|
79
|
+
minSuffixLength: 16,
|
|
80
|
+
predicate: isAsciiAlphaNumeric,
|
|
81
|
+
replacement: "[REDACTED:stripe-key]",
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
prefix: "rk_live_",
|
|
85
|
+
minSuffixLength: 16,
|
|
86
|
+
predicate: isAsciiAlphaNumeric,
|
|
87
|
+
replacement: "[REDACTED:stripe-key]",
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
prefix: "rk_test_",
|
|
91
|
+
minSuffixLength: 16,
|
|
92
|
+
predicate: isAsciiAlphaNumeric,
|
|
93
|
+
replacement: "[REDACTED:stripe-key]",
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
prefix: "glpat-",
|
|
97
|
+
minSuffixLength: 20,
|
|
98
|
+
predicate: isTokenChar,
|
|
99
|
+
replacement: "[REDACTED:gitlab-token]",
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
prefix: "AIza",
|
|
103
|
+
minSuffixLength: 35,
|
|
104
|
+
predicate: isTokenChar,
|
|
105
|
+
replacement: "[REDACTED:google-api-key]",
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
prefix: "GOCSPX-",
|
|
109
|
+
minSuffixLength: 20,
|
|
110
|
+
predicate: isTokenChar,
|
|
111
|
+
replacement: "[REDACTED:google-oauth-secret]",
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
prefix: "sk-proj-",
|
|
115
|
+
minSuffixLength: 20,
|
|
116
|
+
predicate: isTokenChar,
|
|
117
|
+
replacement: "[REDACTED:openai-key]",
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
prefix: "sk-svcacct-",
|
|
121
|
+
minSuffixLength: 20,
|
|
122
|
+
predicate: isTokenChar,
|
|
123
|
+
replacement: "[REDACTED:openai-key]",
|
|
124
|
+
},
|
|
125
|
+
];
|
|
126
|
+
/**
|
|
127
|
+
* Minimum suffix length for a legacy `sk-<base62>` OpenAI key. Legacy keys are
|
|
128
|
+
* 48 base62 chars; require a high bar so ordinary kebab-case identifiers
|
|
129
|
+
* (`sk-skeleton-loading-placeholder`, `sk-button-large`) never match.
|
|
130
|
+
*/
|
|
131
|
+
const OPENAI_LEGACY_MIN_SUFFIX = 40;
|
|
132
|
+
function consumeWhile(options) {
|
|
133
|
+
let end = options.start;
|
|
134
|
+
while (end < options.text.length && options.predicate(options.text[end])) {
|
|
135
|
+
end++;
|
|
136
|
+
}
|
|
137
|
+
return end;
|
|
138
|
+
}
|
|
139
|
+
function scanPrefixedSecret(position) {
|
|
140
|
+
for (const secret of PREFIXED_SECRETS) {
|
|
141
|
+
if (position.text[position.start] !== secret.prefix[0] ||
|
|
142
|
+
!position.text.startsWith(secret.prefix, position.start)) {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
const suffixStart = position.start + secret.prefix.length;
|
|
146
|
+
const end = consumeWhile({
|
|
147
|
+
text: position.text,
|
|
148
|
+
start: suffixStart,
|
|
149
|
+
predicate: secret.predicate,
|
|
150
|
+
});
|
|
151
|
+
return end - suffixStart >= secret.minSuffixLength
|
|
152
|
+
? { end, replacement: secret.replacement }
|
|
153
|
+
: { end: Math.max(end, suffixStart) };
|
|
154
|
+
}
|
|
155
|
+
return undefined;
|
|
156
|
+
}
|
|
157
|
+
function scanSlackToken(position) {
|
|
158
|
+
const variant = position.text[position.start + 3];
|
|
159
|
+
if (!position.text.startsWith("xox", position.start) ||
|
|
160
|
+
variant === undefined ||
|
|
161
|
+
variant < "a" ||
|
|
162
|
+
variant > "z" ||
|
|
163
|
+
position.text[position.start + 4] !== "-") {
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
const suffixStart = position.start + 5;
|
|
167
|
+
const end = consumeWhile({
|
|
168
|
+
text: position.text,
|
|
169
|
+
start: suffixStart,
|
|
170
|
+
predicate: isTokenChar,
|
|
171
|
+
});
|
|
172
|
+
return end - suffixStart >= 10
|
|
173
|
+
? { end, replacement: "[REDACTED:slack-token]" }
|
|
174
|
+
: { end: Math.max(end, suffixStart) };
|
|
175
|
+
}
|
|
176
|
+
function scanSendGridKey(position) {
|
|
177
|
+
if (!position.text.startsWith("SG.", position.start))
|
|
178
|
+
return undefined;
|
|
179
|
+
const retryAt = position.start + 1;
|
|
180
|
+
const firstStart = position.start + 3;
|
|
181
|
+
const firstEnd = firstStart + 22;
|
|
182
|
+
for (let index = firstStart; index < firstEnd; index++) {
|
|
183
|
+
if (!isTokenChar(position.text[index]))
|
|
184
|
+
return { end: retryAt };
|
|
185
|
+
}
|
|
186
|
+
if (position.text[firstEnd] !== ".")
|
|
187
|
+
return { end: retryAt };
|
|
188
|
+
const secondStart = firstEnd + 1;
|
|
189
|
+
const end = secondStart + 43;
|
|
190
|
+
for (let index = secondStart; index < end; index++) {
|
|
191
|
+
if (!isTokenChar(position.text[index]))
|
|
192
|
+
return { end: retryAt };
|
|
193
|
+
}
|
|
194
|
+
return isTokenChar(position.text[end])
|
|
195
|
+
? { end: retryAt }
|
|
196
|
+
: { end, replacement: "[REDACTED:sendgrid-key]" };
|
|
197
|
+
}
|
|
198
|
+
function createJwtSegment(options) {
|
|
199
|
+
const possibleCandidate = options.text.indexOf("eyJ", options.start);
|
|
200
|
+
const candidate = possibleCandidate !== -1 &&
|
|
201
|
+
possibleCandidate < options.end &&
|
|
202
|
+
options.end - (possibleCandidate + 3) >= 5
|
|
203
|
+
? possibleCandidate
|
|
204
|
+
: -1;
|
|
205
|
+
return {
|
|
206
|
+
candidate,
|
|
207
|
+
length: options.end - options.start,
|
|
208
|
+
end: options.end,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
function chooseSecretRange(options) {
|
|
212
|
+
if (!options.candidate)
|
|
213
|
+
return options.current;
|
|
214
|
+
if (!options.current)
|
|
215
|
+
return options.candidate;
|
|
216
|
+
return options.candidate.start < options.current.start ||
|
|
217
|
+
(options.candidate.start === options.current.start &&
|
|
218
|
+
options.candidate.end > options.current.end)
|
|
219
|
+
? options.candidate
|
|
220
|
+
: options.current;
|
|
221
|
+
}
|
|
222
|
+
function findNestedJwtRange(segments) {
|
|
223
|
+
let result;
|
|
224
|
+
const jweStart = segments.length - 5;
|
|
225
|
+
if (jweStart >= 0 &&
|
|
226
|
+
segments[jweStart].candidate !== -1 &&
|
|
227
|
+
segments[jweStart + 2].length > 0 &&
|
|
228
|
+
segments[jweStart + 3].length > 0 &&
|
|
229
|
+
segments[jweStart + 4].length > 0) {
|
|
230
|
+
result = {
|
|
231
|
+
start: segments[jweStart].candidate,
|
|
232
|
+
end: segments[jweStart + 4].end,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
const jwtStart = segments.length - 3;
|
|
236
|
+
const isJwt = jwtStart >= 0 &&
|
|
237
|
+
segments[jwtStart].candidate !== -1 &&
|
|
238
|
+
segments[jwtStart + 1].length > 0 &&
|
|
239
|
+
segments[jwtStart + 2].length > 0;
|
|
240
|
+
return isJwt
|
|
241
|
+
? chooseSecretRange({
|
|
242
|
+
current: result,
|
|
243
|
+
candidate: {
|
|
244
|
+
start: segments[jwtStart].candidate,
|
|
245
|
+
end: segments[jwtStart + 2].end,
|
|
246
|
+
},
|
|
247
|
+
})
|
|
248
|
+
: result;
|
|
249
|
+
}
|
|
250
|
+
function topLevelJwtEnd(options) {
|
|
251
|
+
if (options.segmentCount >= 5 &&
|
|
252
|
+
options.segmentLengths[2] > 0 &&
|
|
253
|
+
options.segmentLengths[3] > 0 &&
|
|
254
|
+
options.segmentLengths[4] > 0) {
|
|
255
|
+
return options.segmentEnds[4];
|
|
256
|
+
}
|
|
257
|
+
return options.segmentCount >= 3 &&
|
|
258
|
+
options.segmentLengths[1] > 0 &&
|
|
259
|
+
options.segmentLengths[2] > 0
|
|
260
|
+
? options.segmentEnds[2]
|
|
261
|
+
: undefined;
|
|
262
|
+
}
|
|
263
|
+
function scanJwt(position) {
|
|
264
|
+
if (!position.text.startsWith("eyJ", position.start))
|
|
265
|
+
return undefined;
|
|
266
|
+
let segmentStart = position.start + 3;
|
|
267
|
+
let cursor = consumeWhile({
|
|
268
|
+
text: position.text,
|
|
269
|
+
start: segmentStart,
|
|
270
|
+
predicate: isTokenChar,
|
|
271
|
+
});
|
|
272
|
+
if (cursor - segmentStart < 5)
|
|
273
|
+
return { end: cursor };
|
|
274
|
+
const segmentLengths = [cursor - segmentStart];
|
|
275
|
+
const segmentEnds = [cursor];
|
|
276
|
+
const recentSegments = [
|
|
277
|
+
createJwtSegment({ text: position.text, start: segmentStart, end: cursor }),
|
|
278
|
+
];
|
|
279
|
+
let segmentCount = 1;
|
|
280
|
+
let nestedRange;
|
|
281
|
+
while (position.text[cursor] === ".") {
|
|
282
|
+
segmentStart = cursor + 1;
|
|
283
|
+
cursor = consumeWhile({
|
|
284
|
+
text: position.text,
|
|
285
|
+
start: segmentStart,
|
|
286
|
+
predicate: isTokenChar,
|
|
287
|
+
});
|
|
288
|
+
segmentCount++;
|
|
289
|
+
recentSegments.push(createJwtSegment({
|
|
290
|
+
text: position.text,
|
|
291
|
+
start: segmentStart,
|
|
292
|
+
end: cursor,
|
|
293
|
+
}));
|
|
294
|
+
if (recentSegments.length > 5)
|
|
295
|
+
recentSegments.shift();
|
|
296
|
+
nestedRange = chooseSecretRange({
|
|
297
|
+
current: nestedRange,
|
|
298
|
+
candidate: findNestedJwtRange(recentSegments),
|
|
299
|
+
});
|
|
300
|
+
if (segmentLengths.length < 5) {
|
|
301
|
+
segmentLengths.push(cursor - segmentStart);
|
|
302
|
+
segmentEnds.push(cursor);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
const topLevelEnd = topLevelJwtEnd({
|
|
306
|
+
segmentCount,
|
|
307
|
+
segmentLengths,
|
|
308
|
+
segmentEnds,
|
|
309
|
+
});
|
|
310
|
+
if (topLevelEnd !== undefined) {
|
|
311
|
+
return { end: topLevelEnd, replacement: "[REDACTED:jwt]" };
|
|
312
|
+
}
|
|
313
|
+
return nestedRange
|
|
314
|
+
? { ...nestedRange, replacement: "[REDACTED:jwt]" }
|
|
315
|
+
: { end: cursor };
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Legacy OpenAI keys are `sk-` followed by base62 (ASCII alphanumeric only —
|
|
319
|
+
* no `-`/`_`). Redact only on a strict shape: at least
|
|
320
|
+
* OPENAI_LEGACY_MIN_SUFFIX alphanumeric chars AND at least one digit. Stopping
|
|
321
|
+
* the suffix at the first non-alphanumeric char means a hyphenated identifier
|
|
322
|
+
* such as `sk-skeleton-loading-placeholder` yields only `skeleton` (8 chars) —
|
|
323
|
+
* far below the bar — so ordinary kebab-case slugs are never touched.
|
|
324
|
+
*/
|
|
325
|
+
function scanOpenAiLegacyKey(position) {
|
|
326
|
+
if (!position.text.startsWith("sk-", position.start))
|
|
327
|
+
return undefined;
|
|
328
|
+
const suffixStart = position.start + 3;
|
|
329
|
+
let end = suffixStart;
|
|
330
|
+
let hasDigit = false;
|
|
331
|
+
while (end < position.text.length &&
|
|
332
|
+
isAsciiAlphaNumeric(position.text[end])) {
|
|
333
|
+
const code = codePointOf(position.text[end]);
|
|
334
|
+
if (code >= 48 && code <= 57)
|
|
335
|
+
hasDigit = true;
|
|
336
|
+
end++;
|
|
337
|
+
}
|
|
338
|
+
return end - suffixStart >= OPENAI_LEGACY_MIN_SUFFIX && hasDigit
|
|
339
|
+
? { end, replacement: "[REDACTED:openai-key]" }
|
|
340
|
+
: { end: Math.max(end, suffixStart) };
|
|
341
|
+
}
|
|
342
|
+
function scanSecretAt(position) {
|
|
343
|
+
const initial = position.text[position.start];
|
|
344
|
+
if (initial === "S")
|
|
345
|
+
return scanSendGridKey(position);
|
|
346
|
+
if (initial === "x")
|
|
347
|
+
return scanSlackToken(position);
|
|
348
|
+
// `s`: modern `sk-proj-`/`sk-svcacct-`/`sk_live_`/`sk_test_` via the prefix
|
|
349
|
+
// registry, else fall back to the strict legacy `sk-<base62>` scanner.
|
|
350
|
+
if (initial === "s") {
|
|
351
|
+
return scanPrefixedSecret(position) ?? scanOpenAiLegacyKey(position);
|
|
352
|
+
}
|
|
353
|
+
if (initial === "A" ||
|
|
354
|
+
initial === "G" ||
|
|
355
|
+
initial === "g" ||
|
|
356
|
+
initial === "r") {
|
|
357
|
+
return scanPrefixedSecret(position);
|
|
358
|
+
}
|
|
359
|
+
return undefined;
|
|
360
|
+
}
|
|
361
|
+
function redactScannedSecrets(options) {
|
|
362
|
+
let cursor = 0;
|
|
363
|
+
let copyFrom = 0;
|
|
364
|
+
let result = "";
|
|
365
|
+
const position = { text: options.text, start: 0 };
|
|
366
|
+
while (cursor < options.text.length) {
|
|
367
|
+
position.start = cursor;
|
|
368
|
+
const scan = options.scanner(position);
|
|
369
|
+
if (!scan) {
|
|
370
|
+
cursor++;
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
if (scan.replacement) {
|
|
374
|
+
const replacementStart = scan.start ?? cursor;
|
|
375
|
+
result +=
|
|
376
|
+
options.text.slice(copyFrom, replacementStart) + scan.replacement;
|
|
377
|
+
copyFrom = scan.end;
|
|
378
|
+
}
|
|
379
|
+
cursor = Math.max(cursor + 1, scan.end);
|
|
380
|
+
}
|
|
381
|
+
return copyFrom === 0 ? options.text : result + options.text.slice(copyFrom);
|
|
382
|
+
}
|
|
383
|
+
function redactTokenSecrets(text) {
|
|
384
|
+
return redactScannedSecrets({ text, scanner: scanSecretAt });
|
|
385
|
+
}
|
|
386
|
+
function redactJwtSecrets(text) {
|
|
387
|
+
return redactScannedSecrets({ text, scanner: scanJwt });
|
|
388
|
+
}
|
|
389
|
+
function isPrivateKeyLabel(label) {
|
|
390
|
+
if (label.length === 0 ||
|
|
391
|
+
label.length > 64 ||
|
|
392
|
+
!label.includes("PRIVATE KEY")) {
|
|
393
|
+
return false;
|
|
394
|
+
}
|
|
395
|
+
for (const char of label) {
|
|
396
|
+
const code = codePointOf(char);
|
|
397
|
+
if (char !== " " && (code < 48 || code > 57) && (code < 65 || code > 90)) {
|
|
398
|
+
return false;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
return true;
|
|
402
|
+
}
|
|
403
|
+
function findUnescapedQuote(position) {
|
|
404
|
+
let escaped = false;
|
|
405
|
+
for (let index = position.start; index < position.text.length; index++) {
|
|
406
|
+
const char = position.text[index];
|
|
407
|
+
if (char === "\\") {
|
|
408
|
+
escaped = !escaped;
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
if (char === '"' && !escaped)
|
|
412
|
+
return index;
|
|
413
|
+
escaped = false;
|
|
414
|
+
}
|
|
415
|
+
return position.text.length;
|
|
416
|
+
}
|
|
417
|
+
function redactPrivateKeyBlocks(text) {
|
|
418
|
+
let cursor = 0;
|
|
419
|
+
let result = "";
|
|
420
|
+
while (cursor < text.length) {
|
|
421
|
+
const begin = text.indexOf(PEM_BEGIN, cursor);
|
|
422
|
+
if (begin === -1)
|
|
423
|
+
return result + text.slice(cursor);
|
|
424
|
+
const labelStart = begin + PEM_BEGIN.length;
|
|
425
|
+
const labelEnd = text.indexOf(PEM_DELIMITER, labelStart);
|
|
426
|
+
if (labelEnd === -1)
|
|
427
|
+
return result + text.slice(cursor);
|
|
428
|
+
const label = text.slice(labelStart, labelEnd);
|
|
429
|
+
if (!isPrivateKeyLabel(label)) {
|
|
430
|
+
const next = labelEnd + PEM_DELIMITER.length;
|
|
431
|
+
result += text.slice(cursor, next);
|
|
432
|
+
cursor = next;
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
const contentStart = labelEnd + PEM_DELIMITER.length;
|
|
436
|
+
const endMarker = `-----END ${label}-----`;
|
|
437
|
+
const end = text.indexOf(endMarker, contentStart);
|
|
438
|
+
result += text.slice(cursor, begin) + "[REDACTED:private-key]";
|
|
439
|
+
if (end === -1) {
|
|
440
|
+
cursor = findUnescapedQuote({ text, start: contentStart });
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
cursor = end + endMarker.length;
|
|
444
|
+
}
|
|
445
|
+
return result;
|
|
446
|
+
}
|
|
447
|
+
export function redactSecrets(text) {
|
|
448
|
+
return redactJwtSecrets(redactTokenSecrets(redactPrivateKeyBlocks(text)));
|
|
449
|
+
}
|
|
@@ -12,13 +12,24 @@
|
|
|
12
12
|
* `spawn()`, stopped at `child.on("close", ...)`), tracking peak/average
|
|
13
13
|
* CPU% and RSS for that one invocation.
|
|
14
14
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
15
|
+
* On **Linux/macOS** it uses `pidusage` (procfs on Linux, `ps` on macOS) — a
|
|
16
|
+
* small pure-JS package (one transitive dep, `safe-buffer`) that bundles like
|
|
17
|
+
* the repo's other pure-JS runtime deps (minimatch, js-yaml) rather than
|
|
18
|
+
* needing an EXTERNAL entry in scripts/bundle-dist.mjs.
|
|
19
|
+
*
|
|
20
|
+
* On **Windows** it does NOT use `pidusage`: pidusage's Windows path shells out
|
|
21
|
+
* to `gwmi` via an internal `spawn(..., { shell: "powershell.exe" })` that has
|
|
22
|
+
* NO try/catch, and it runs that spawn from inside a ChildProcess `close`
|
|
23
|
+
* callback (a detached async context). Under real Windows handle/commit
|
|
24
|
+
* pressure that `spawn()` can throw `spawn UNKNOWN` (errno -4094)
|
|
25
|
+
* **synchronously in that detached callback**, which no `try { await pidusage }
|
|
26
|
+
* catch {}` at the call site can catch → uncaughtException → the pi host
|
|
27
|
+
* crashes (#620, #533). pidusage 4.0.1 exposes no option to avoid the gwmi
|
|
28
|
+
* path. So on Windows this module runs its OWN fully guarded
|
|
29
|
+
* `Get-CimInstance Win32_Process` query (see `sampleProcessesWindows`),
|
|
30
|
+
* mirroring `findDescendantPidsWindows`'s guard pattern, and computes CPU%
|
|
31
|
+
* from the same KernelModeTime/UserModeTime delta-over-elapsed formula gwmi
|
|
32
|
+
* uses — so a spawn failure can only ever lose a data point, never throw.
|
|
22
33
|
*
|
|
23
34
|
* Every export here is best-effort: a sampling failure (pid already exited,
|
|
24
35
|
* `pidusage` throwing, permission denied, etc.) must never throw into the
|
|
@@ -32,10 +43,14 @@
|
|
|
32
43
|
* process/pidusage involvement — mirrors the pure/impure split in
|
|
33
44
|
* clients/instance-reaper.ts (`decideOrphanReaping` vs `sweepOrphans`).
|
|
34
45
|
*/
|
|
35
|
-
import { spawn as nodeSpawn } from "node:child_process";
|
|
36
46
|
import * as path from "node:path";
|
|
37
47
|
import pidusage from "pidusage";
|
|
38
|
-
|
|
48
|
+
import { spawnCollectStdout } from "./child-unref.js";
|
|
49
|
+
// Read the platform live (not a module-load const) so both the Windows and the
|
|
50
|
+
// POSIX sampling paths are exercisable in unit tests regardless of the host OS.
|
|
51
|
+
function runningOnWindows() {
|
|
52
|
+
return process.platform === "win32";
|
|
53
|
+
}
|
|
39
54
|
/**
|
|
40
55
|
* PURE BFS over a (pid, parentPid) snapshot: every live descendant of
|
|
41
56
|
* `rootPid`, however deep. Split out from `findDescendantPidsWindows` so the
|
|
@@ -84,52 +99,142 @@ export function walkDescendantPids(rootPid, pairs) {
|
|
|
84
99
|
* Mirrors the identity-verification CIM queries in clients/instance-reaper.ts.
|
|
85
100
|
*/
|
|
86
101
|
async function findDescendantPidsWindows(rootPid) {
|
|
87
|
-
if (!
|
|
102
|
+
if (!runningOnWindows() || !Number.isFinite(rootPid) || rootPid <= 0)
|
|
88
103
|
return [];
|
|
89
104
|
// One WQL query pulls every process's (pid, parentPid) pair; walk the BFS
|
|
90
105
|
// in JS rather than issuing N queries for N tree levels.
|
|
91
106
|
const psScript = "Get-CimInstance Win32_Process " +
|
|
92
107
|
'| Select-Object -Property ProcessId,ParentProcessId ' +
|
|
93
108
|
'| ForEach-Object { "$($_.ProcessId),$($_.ParentProcessId)" }';
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
109
|
+
const powershell = path.join(process.env.SystemRoot ?? String.raw `C:\Windows`, "WindowsPowerShell", "v1.0", "powershell.exe");
|
|
110
|
+
// Fire-and-forget, per-poll-tick spawn (#1155): `spawnCollectStdout` unrefs
|
|
111
|
+
// the child AND its piped stdout so this one-shot CIM query can never keep
|
|
112
|
+
// a settled `pi --print` process alive past its own close — mirrors the
|
|
113
|
+
// reaper's identical spawn→collect plumbing (#1153/#1160). Sampling still
|
|
114
|
+
// works normally in an interactive/long-lived session: unref only means
|
|
115
|
+
// "don't hold the loop open FOR this alone," the collected stdout is still
|
|
116
|
+
// delivered whenever `close` fires. Resolves to `""` on any spawn/`error`
|
|
117
|
+
// failure, which the parse below turns into an empty pairs list (same
|
|
118
|
+
// result the old inline `resolve([])` error path produced).
|
|
119
|
+
const out = await spawnCollectStdout(powershell, ["-NoProfile", "-NonInteractive", "-Command", psScript], { shell: false, windowsHide: true, stdio: ["ignore", "pipe", "ignore"] });
|
|
120
|
+
const pairs = [];
|
|
121
|
+
for (const line of out.split(/\r?\n/)) {
|
|
122
|
+
const [pidStr, ppidStr] = line.split(",");
|
|
123
|
+
const pid = Number(pidStr);
|
|
124
|
+
const ppid = Number(ppidStr);
|
|
125
|
+
if (Number.isFinite(pid) && Number.isFinite(ppid)) {
|
|
126
|
+
pairs.push([pid, ppid]);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return walkDescendantPids(rootPid, pairs);
|
|
130
|
+
}
|
|
131
|
+
const windowsCpuHistory = new Map();
|
|
132
|
+
const CPU_HISTORY_MAX_AGE_MS = 60_000;
|
|
133
|
+
/**
|
|
134
|
+
* TEST-ONLY: clear the Windows CPU%-history so a test's two-sample CPU%
|
|
135
|
+
* assertion starts from a known-empty state (module-level state otherwise
|
|
136
|
+
* persists across tests in the same worker).
|
|
137
|
+
*/
|
|
138
|
+
export function __resetWindowsCpuHistoryForTests() {
|
|
139
|
+
windowsCpuHistory.clear();
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Windows-only CPU%/RSS sampling via a FULLY GUARDED `Get-CimInstance
|
|
143
|
+
* Win32_Process` query (mirrors `findDescendantPidsWindows`): a synchronous
|
|
144
|
+
* throw from `spawn` (the `spawn UNKNOWN` crash vector, #620), a `child`
|
|
145
|
+
* `error` event, or a non-zero/garbage exit all resolve to a partial/empty
|
|
146
|
+
* map — this function can NEVER throw or reject. Deliberately does NOT call
|
|
147
|
+
* `pidusage`, whose unguarded internal `gwmi` spawn is the crash we're fixing.
|
|
148
|
+
*
|
|
149
|
+
* RSS comes from `WorkingSetSize`; CPU% from `KernelModeTime`+`UserModeTime`
|
|
150
|
+
* (both in 100 ns units → ms via `/1e4`) differenced against this pid's prior
|
|
151
|
+
* sample over the elapsed wall time — the same computation pidusage's gwmi
|
|
152
|
+
* path uses. The first time a pid is seen it has no prior sample, so CPU% is
|
|
153
|
+
* reported as 0 for that tick and a real rate lands on the next one.
|
|
154
|
+
*/
|
|
155
|
+
async function sampleProcessesWindows(valid) {
|
|
156
|
+
const result = new Map();
|
|
157
|
+
if (valid.length === 0)
|
|
158
|
+
return result;
|
|
159
|
+
// pids are pre-validated finite positive integers, so this WQL filter is
|
|
160
|
+
// injection-safe. One line per pid: "pid,workingSet,kernel100ns,user100ns".
|
|
161
|
+
const filter = valid.map((p) => `ProcessId=${p}`).join(" or ");
|
|
162
|
+
const psScript = `Get-CimInstance Win32_Process -Filter "${filter}" ` +
|
|
163
|
+
"| Select-Object -Property ProcessId,WorkingSetSize,KernelModeTime,UserModeTime " +
|
|
164
|
+
'| ForEach-Object { "$($_.ProcessId),$($_.WorkingSetSize),$($_.KernelModeTime),$($_.UserModeTime)" }';
|
|
165
|
+
const powershell = path.join(process.env.SystemRoot ?? String.raw `C:\Windows`, "WindowsPowerShell", "v1.0", "powershell.exe");
|
|
166
|
+
// Fire-and-forget, per-poll-tick spawn (#1155): `spawnCollectStdout` unrefs
|
|
167
|
+
// the child AND its piped stdout so this one-shot CIM query can never keep
|
|
168
|
+
// a settled `pi --print` process alive past its own close — mirrors the
|
|
169
|
+
// reaper's identical spawn→collect plumbing (#1153/#1160). It also absorbs
|
|
170
|
+
// both failure modes this function used to guard inline — a synchronous
|
|
171
|
+
// `spawn` throw (the `spawn UNKNOWN` crash vector, #620) and an async
|
|
172
|
+
// `error` event — resolving to `""` either way, which the parse below
|
|
173
|
+
// turns into the same empty/partial `result` map the old inline handlers
|
|
174
|
+
// produced. Sampling still works normally in an interactive/long-lived
|
|
175
|
+
// session: unref only means "don't hold the loop open FOR this alone."
|
|
176
|
+
const out = await spawnCollectStdout(powershell, ["-NoProfile", "-NonInteractive", "-Command", psScript], { shell: false, windowsHide: true, stdio: ["ignore", "pipe", "ignore"] });
|
|
177
|
+
try {
|
|
178
|
+
const now = Date.now();
|
|
179
|
+
const seen = new Set();
|
|
180
|
+
for (const line of out.split(/\r?\n/)) {
|
|
181
|
+
const parts = line.split(",");
|
|
182
|
+
if (parts.length < 4)
|
|
183
|
+
continue;
|
|
184
|
+
const pid = Number(parts[0]);
|
|
185
|
+
const workingSet = Number(parts[1]);
|
|
186
|
+
const kernel100ns = Number(parts[2]);
|
|
187
|
+
const user100ns = Number(parts[3]);
|
|
188
|
+
if (!Number.isFinite(pid) || pid <= 0)
|
|
189
|
+
continue;
|
|
190
|
+
if (!Number.isFinite(workingSet))
|
|
191
|
+
continue;
|
|
192
|
+
const cpuMs = Math.round(kernel100ns / 1e4) + Math.round(user100ns / 1e4);
|
|
193
|
+
const prev = windowsCpuHistory.get(pid);
|
|
194
|
+
let cpuPercent = 0;
|
|
195
|
+
if (prev) {
|
|
196
|
+
const wallMs = now - prev.ts;
|
|
197
|
+
if (wallMs > 0) {
|
|
198
|
+
cpuPercent = ((cpuMs - prev.cpuMs) / wallMs) * 100;
|
|
199
|
+
if (!Number.isFinite(cpuPercent) || cpuPercent < 0)
|
|
200
|
+
cpuPercent = 0;
|
|
112
201
|
}
|
|
113
|
-
|
|
114
|
-
});
|
|
202
|
+
}
|
|
203
|
+
windowsCpuHistory.set(pid, { cpuMs, ts: now });
|
|
204
|
+
seen.add(pid);
|
|
205
|
+
result.set(pid, { rssBytes: workingSet, cpuPercent });
|
|
115
206
|
}
|
|
116
|
-
|
|
117
|
-
|
|
207
|
+
// Prune stale history so pids that have gone away don't accumulate.
|
|
208
|
+
for (const [pid, entry] of windowsCpuHistory) {
|
|
209
|
+
if (!seen.has(pid) && now - entry.ts > CPU_HISTORY_MAX_AGE_MS) {
|
|
210
|
+
windowsCpuHistory.delete(pid);
|
|
211
|
+
}
|
|
118
212
|
}
|
|
119
|
-
}
|
|
120
|
-
|
|
213
|
+
}
|
|
214
|
+
catch {
|
|
215
|
+
// Parsing must never throw into the caller; best-effort.
|
|
216
|
+
}
|
|
217
|
+
return result;
|
|
121
218
|
}
|
|
122
219
|
/**
|
|
123
|
-
* Sample CPU%/RSS for a set of pids
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
220
|
+
* Sample CPU%/RSS for a set of pids. Best-effort: a pid that can't be resolved
|
|
221
|
+
* (already exited, permission denied, spawn failed, etc.) is simply absent
|
|
222
|
+
* from the returned map — callers MUST treat "absent" as "unsampled this
|
|
223
|
+
* tick", never as zero usage.
|
|
224
|
+
*
|
|
225
|
+
* On Windows this uses a guarded CIM query (`sampleProcessesWindows`) and
|
|
226
|
+
* never touches `pidusage`, whose unguarded internal spawn could crash the
|
|
227
|
+
* host (#620, #533). On Linux/macOS it uses `pidusage`.
|
|
127
228
|
*/
|
|
128
229
|
export async function sampleProcesses(pids) {
|
|
129
230
|
const result = new Map();
|
|
130
231
|
const valid = [...new Set(pids.filter((p) => Number.isFinite(p) && p > 0))];
|
|
131
232
|
if (valid.length === 0)
|
|
132
233
|
return result;
|
|
234
|
+
if (runningOnWindows()) {
|
|
235
|
+
// Fully guarded; cannot throw/reject.
|
|
236
|
+
return await sampleProcessesWindows(valid);
|
|
237
|
+
}
|
|
133
238
|
try {
|
|
134
239
|
const stats = await pidusage(valid);
|
|
135
240
|
for (const pid of valid) {
|
|
@@ -216,7 +321,7 @@ export function startSpawnUsageSampler(pid, intervalMs = 750) {
|
|
|
216
321
|
if (stopped)
|
|
217
322
|
return;
|
|
218
323
|
try {
|
|
219
|
-
const pids =
|
|
324
|
+
const pids = runningOnWindows()
|
|
220
325
|
? [targetPid, ...(await findDescendantPidsWindows(targetPid))]
|
|
221
326
|
: [targetPid];
|
|
222
327
|
const usageByPid = await sampleProcesses(pids);
|