@mastra/code-sdk 0.0.0-agent-tags-alpha-20261125185303
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/LICENSE.md +32 -0
- package/README.md +284 -0
- package/dist/acp/agent.d.ts +48 -0
- package/dist/acp/agent.js +414 -0
- package/dist/acp/errors.d.ts +3 -0
- package/dist/acp/errors.js +11 -0
- package/dist/acp/event-mapper.d.ts +28 -0
- package/dist/acp/event-mapper.js +274 -0
- package/dist/acp/index.d.ts +9 -0
- package/dist/acp/index.js +21 -0
- package/dist/acp/runtime.d.ts +5 -0
- package/dist/acp/runtime.js +91 -0
- package/dist/acp/server.d.ts +4 -0
- package/dist/acp/server.js +35 -0
- package/dist/acp/skills.d.ts +6 -0
- package/dist/acp/skills.js +30 -0
- package/dist/acp.d.ts +2 -0
- package/dist/acp.js +1 -0
- package/dist/agent-connections/messaging-processor.d.ts +17 -0
- package/dist/agent-connections/messaging-processor.js +131 -0
- package/dist/agent-connections/ownership.d.ts +24 -0
- package/dist/agent-connections/ownership.js +96 -0
- package/dist/agent-connections/registry.d.ts +20 -0
- package/dist/agent-connections/registry.js +167 -0
- package/dist/agent-connections/session-advertisement.d.ts +27 -0
- package/dist/agent-connections/session-advertisement.js +86 -0
- package/dist/agent-connections/signal-provider.d.ts +141 -0
- package/dist/agent-connections/signal-provider.js +35 -0
- package/dist/agent-connections/state-processor.d.ts +23 -0
- package/dist/agent-connections/state-processor.js +270 -0
- package/dist/agent-connections/thread-state.d.ts +41 -0
- package/dist/agent-connections/thread-state.js +157 -0
- package/dist/agent-connections/tools.d.ts +133 -0
- package/dist/agent-connections/tools.js +616 -0
- package/dist/agent-connections/types.d.ts +99 -0
- package/dist/agent-connections/types.js +4 -0
- package/dist/agent-connections/untrusted-text.d.ts +5 -0
- package/dist/agent-connections/untrusted-text.js +19 -0
- package/dist/agents/background-completion-events.d.ts +16 -0
- package/dist/agents/background-completion-events.js +16 -0
- package/dist/agents/background-completion.d.ts +22 -0
- package/dist/agents/background-completion.js +73 -0
- package/dist/agents/context-audit.d.ts +84 -0
- package/dist/agents/context-audit.js +115 -0
- package/dist/agents/credential-resolver.d.ts +74 -0
- package/dist/agents/credential-resolver.js +142 -0
- package/dist/agents/custom-provider-source.d.ts +35 -0
- package/dist/agents/custom-provider-source.js +22 -0
- package/dist/agents/fastembed-subprocess.d.ts +52 -0
- package/dist/agents/fastembed-subprocess.js +282 -0
- package/dist/agents/instructions.d.ts +42 -0
- package/dist/agents/instructions.js +69 -0
- package/dist/agents/mastracode-gateway.d.ts +85 -0
- package/dist/agents/mastracode-gateway.js +500 -0
- package/dist/agents/memory.d.ts +36 -0
- package/dist/agents/memory.js +248 -0
- package/dist/agents/model.d.ts +158 -0
- package/dist/agents/model.js +382 -0
- package/dist/agents/modes/build.d.ts +12 -0
- package/dist/agents/modes/build.js +33 -0
- package/dist/agents/modes/explore.d.ts +10 -0
- package/dist/agents/modes/explore.js +25 -0
- package/dist/agents/modes/plan.d.ts +8 -0
- package/dist/agents/modes/plan.js +43 -0
- package/dist/agents/prompts/agent-instructions.d.ts +122 -0
- package/dist/agents/prompts/agent-instructions.js +617 -0
- package/dist/agents/prompts/build.d.ts +12 -0
- package/dist/agents/prompts/build.js +73 -0
- package/dist/agents/prompts/fast.d.ts +5 -0
- package/dist/agents/prompts/fast.js +23 -0
- package/dist/agents/prompts/index.d.ts +61 -0
- package/dist/agents/prompts/index.js +141 -0
- package/dist/agents/prompts/model.d.ts +5 -0
- package/dist/agents/prompts/model.js +20 -0
- package/dist/agents/prompts/plan.d.ts +11 -0
- package/dist/agents/prompts/plan.js +132 -0
- package/dist/agents/prompts/tool-guidance.d.ts +18 -0
- package/dist/agents/prompts/tool-guidance.js +158 -0
- package/dist/agents/sandbox-filesystem.d.ts +232 -0
- package/dist/agents/sandbox-filesystem.js +851 -0
- package/dist/agents/subagents/execute.d.ts +3 -0
- package/dist/agents/subagents/execute.js +24 -0
- package/dist/agents/subagents/explore.d.ts +3 -0
- package/dist/agents/subagents/explore.js +15 -0
- package/dist/agents/subagents/index.d.ts +6 -0
- package/dist/agents/subagents/index.js +15 -0
- package/dist/agents/subagents/plan.d.ts +3 -0
- package/dist/agents/subagents/plan.js +15 -0
- package/dist/agents/thread-caveman-state.d.ts +17 -0
- package/dist/agents/thread-caveman-state.js +104 -0
- package/dist/agents/tool-availability.d.ts +17 -0
- package/dist/agents/tool-availability.js +134 -0
- package/dist/agents/tools.d.ts +37 -0
- package/dist/agents/tools.js +154 -0
- package/dist/agents/workflow-builder-agent.d.ts +2 -0
- package/dist/agents/workflow-builder-agent.js +79 -0
- package/dist/agents/workspace.d.ts +35 -0
- package/dist/agents/workspace.js +207 -0
- package/dist/analytics.d.ts +18 -0
- package/dist/analytics.js +159 -0
- package/dist/auth/account-rotation-processor.d.ts +210 -0
- package/dist/auth/account-rotation-processor.js +662 -0
- package/dist/auth/account-routing-context.d.ts +17 -0
- package/dist/auth/account-routing-context.js +38 -0
- package/dist/auth/authorization-input.d.ts +16 -0
- package/dist/auth/authorization-input.js +39 -0
- package/dist/auth/device-code.d.ts +93 -0
- package/dist/auth/device-code.js +126 -0
- package/dist/auth/index.d.ts +13 -0
- package/dist/auth/index.js +9 -0
- package/dist/auth/pkce.d.ts +13 -0
- package/dist/auth/pkce.js +29 -0
- package/dist/auth/provider-auth-error.d.ts +24 -0
- package/dist/auth/provider-auth-error.js +44 -0
- package/dist/auth/providers/anthropic.d.ts +40 -0
- package/dist/auth/providers/anthropic.js +117 -0
- package/dist/auth/providers/github-copilot.d.ts +154 -0
- package/dist/auth/providers/github-copilot.js +359 -0
- package/dist/auth/providers/kimi-coding.d.ts +38 -0
- package/dist/auth/providers/kimi-coding.js +225 -0
- package/dist/auth/providers/openai-codex.d.ts +141 -0
- package/dist/auth/providers/openai-codex.js +531 -0
- package/dist/auth/providers/xai.d.ts +64 -0
- package/dist/auth/providers/xai.js +223 -0
- package/dist/auth/storage.d.ts +222 -0
- package/dist/auth/storage.js +805 -0
- package/dist/auth/types.d.ts +164 -0
- package/dist/auth/types.js +1 -0
- package/dist/clipboard/index.d.ts +26 -0
- package/dist/clipboard/index.js +224 -0
- package/dist/constants.d.ts +29 -0
- package/dist/constants.js +34 -0
- package/dist/error-classification.d.ts +10 -0
- package/dist/error-classification.js +19 -0
- package/dist/evals/context-builder.d.ts +41 -0
- package/dist/evals/context-builder.js +156 -0
- package/dist/evals/index.d.ts +4 -0
- package/dist/evals/index.js +5 -0
- package/dist/evals/scorers/classify-command.d.ts +30 -0
- package/dist/evals/scorers/classify-command.js +61 -0
- package/dist/evals/scorers/efficiency.d.ts +44 -0
- package/dist/evals/scorers/efficiency.js +256 -0
- package/dist/evals/scorers/extract-tools.d.ts +25 -0
- package/dist/evals/scorers/extract-tools.js +57 -0
- package/dist/evals/scorers/index.d.ts +3 -0
- package/dist/evals/scorers/index.js +3 -0
- package/dist/evals/scorers/outcome.d.ts +49 -0
- package/dist/evals/scorers/outcome.js +360 -0
- package/dist/experimental-agent.d.ts +19 -0
- package/dist/experimental-agent.js +45 -0
- package/dist/factory/client.d.ts +44 -0
- package/dist/factory/client.js +110 -0
- package/dist/factory/remote-controller.d.ts +34 -0
- package/dist/factory/remote-controller.js +40 -0
- package/dist/factory/remote-session.d.ts +210 -0
- package/dist/factory/remote-session.js +415 -0
- package/dist/goal-manager.d.ts +114 -0
- package/dist/goal-manager.js +351 -0
- package/dist/goal-signal.d.ts +14 -0
- package/dist/goal-signal.js +14 -0
- package/dist/headless/cli.d.ts +55 -0
- package/dist/headless/cli.js +261 -0
- package/dist/headless/flags.d.ts +33 -0
- package/dist/headless/flags.js +207 -0
- package/dist/headless/format.d.ts +41 -0
- package/dist/headless/format.js +94 -0
- package/dist/headless/index.d.ts +25 -0
- package/dist/headless/index.js +6 -0
- package/dist/headless/policy.d.ts +17 -0
- package/dist/headless/policy.js +39 -0
- package/dist/headless/run-mc.d.ts +7 -0
- package/dist/headless/run-mc.js +438 -0
- package/dist/headless/types.d.ts +175 -0
- package/dist/headless/types.js +16 -0
- package/dist/hooks/config.d.ts +11 -0
- package/dist/hooks/config.js +77 -0
- package/dist/hooks/executor.d.ts +13 -0
- package/dist/hooks/executor.js +128 -0
- package/dist/hooks/index.d.ts +6 -0
- package/dist/hooks/index.js +5 -0
- package/dist/hooks/manager.d.ts +75 -0
- package/dist/hooks/manager.js +289 -0
- package/dist/hooks/types.d.ts +180 -0
- package/dist/hooks/types.js +5 -0
- package/dist/index.d.ts +537 -0
- package/dist/index.js +1399 -0
- package/dist/ipc/ipc-reporter.d.ts +14 -0
- package/dist/ipc/ipc-reporter.js +18 -0
- package/dist/knowledge-inspector.d.ts +139 -0
- package/dist/knowledge-inspector.js +533 -0
- package/dist/knowledge-scope-id.d.ts +7 -0
- package/dist/knowledge-scope-id.js +10 -0
- package/dist/knowledge-scope.d.ts +41 -0
- package/dist/knowledge-scope.js +85 -0
- package/dist/lsp/client.d.ts +56 -0
- package/dist/lsp/client.js +248 -0
- package/dist/lsp/index.d.ts +4 -0
- package/dist/lsp/index.js +4 -0
- package/dist/lsp/language.d.ts +9 -0
- package/dist/lsp/language.js +41 -0
- package/dist/lsp/manager.d.ts +34 -0
- package/dist/lsp/manager.js +79 -0
- package/dist/lsp/server.d.ts +27 -0
- package/dist/lsp/server.js +158 -0
- package/dist/lsp/workspace.d.ts +9 -0
- package/dist/lsp/workspace.js +52 -0
- package/dist/mcp/config.d.ts +77 -0
- package/dist/mcp/config.js +313 -0
- package/dist/mcp/index.d.ts +6 -0
- package/dist/mcp/index.js +3 -0
- package/dist/mcp/manager.d.ts +104 -0
- package/dist/mcp/manager.js +693 -0
- package/dist/mcp/state.d.ts +35 -0
- package/dist/mcp/state.js +94 -0
- package/dist/mcp/types.d.ts +139 -0
- package/dist/mcp/types.js +1 -0
- package/dist/onboarding/custom-packs.d.ts +7 -0
- package/dist/onboarding/custom-packs.js +17 -0
- package/dist/onboarding/custom-providers.d.ts +4 -0
- package/dist/onboarding/custom-providers.js +27 -0
- package/dist/onboarding/index.d.ts +5 -0
- package/dist/onboarding/index.js +3 -0
- package/dist/onboarding/om-settings.d.ts +47 -0
- package/dist/onboarding/om-settings.js +80 -0
- package/dist/onboarding/pack-activation.d.ts +43 -0
- package/dist/onboarding/pack-activation.js +105 -0
- package/dist/onboarding/packs.d.ts +106 -0
- package/dist/onboarding/packs.js +283 -0
- package/dist/onboarding/settings.d.ts +563 -0
- package/dist/onboarding/settings.js +1110 -0
- package/dist/permissions.d.ts +55 -0
- package/dist/permissions.js +139 -0
- package/dist/platform/index.d.ts +74 -0
- package/dist/platform/index.js +199 -0
- package/dist/plugin.d.ts +156 -0
- package/dist/plugin.js +27 -0
- package/dist/plugins/dependencies.d.ts +9 -0
- package/dist/plugins/dependencies.js +84 -0
- package/dist/plugins/install.d.ts +21 -0
- package/dist/plugins/install.js +187 -0
- package/dist/plugins/loader.d.ts +25 -0
- package/dist/plugins/loader.js +264 -0
- package/dist/plugins/manager.d.ts +115 -0
- package/dist/plugins/manager.js +507 -0
- package/dist/plugins/manifest.d.ts +14 -0
- package/dist/plugins/manifest.js +47 -0
- package/dist/plugins/package-link.d.ts +3 -0
- package/dist/plugins/package-link.js +58 -0
- package/dist/plugins/paths.d.ts +10 -0
- package/dist/plugins/paths.js +22 -0
- package/dist/plugins/registry.d.ts +8 -0
- package/dist/plugins/registry.js +109 -0
- package/dist/plugins/scaffold.d.ts +10 -0
- package/dist/plugins/scaffold.js +115 -0
- package/dist/plugins/signal-lane.d.ts +59 -0
- package/dist/plugins/signal-lane.js +163 -0
- package/dist/plugins/types.d.ts +95 -0
- package/dist/plugins/types.js +1 -0
- package/dist/process-memory-diagnostics.d.ts +136 -0
- package/dist/process-memory-diagnostics.js +497 -0
- package/dist/processors/plan-rejection-abort.d.ts +16 -0
- package/dist/processors/plan-rejection-abort.js +62 -0
- package/dist/providers/amazon-bedrock-gateway.d.ts +47 -0
- package/dist/providers/amazon-bedrock-gateway.js +142 -0
- package/dist/providers/amazon-bedrock.d.ts +28 -0
- package/dist/providers/amazon-bedrock.js +99 -0
- package/dist/providers/anthropic-prompt-cache.d.ts +19 -0
- package/dist/providers/anthropic-prompt-cache.js +8 -0
- package/dist/providers/anthropic-thinking.d.ts +5 -0
- package/dist/providers/anthropic-thinking.js +22 -0
- package/dist/providers/claude-max.d.ts +67 -0
- package/dist/providers/claude-max.js +232 -0
- package/dist/providers/github-copilot-gateway.d.ts +35 -0
- package/dist/providers/github-copilot-gateway.js +58 -0
- package/dist/providers/github-copilot.d.ts +60 -0
- package/dist/providers/github-copilot.js +320 -0
- package/dist/providers/google-thinking.d.ts +31 -0
- package/dist/providers/google-thinking.js +64 -0
- package/dist/providers/google-vertex-gateway.d.ts +23 -0
- package/dist/providers/google-vertex-gateway.js +49 -0
- package/dist/providers/google-vertex.d.ts +6 -0
- package/dist/providers/google-vertex.js +51 -0
- package/dist/providers/kimi-coding.d.ts +14 -0
- package/dist/providers/kimi-coding.js +80 -0
- package/dist/providers/model-ids.d.ts +53 -0
- package/dist/providers/model-ids.js +132 -0
- package/dist/providers/openai-codex.d.ts +114 -0
- package/dist/providers/openai-codex.js +427 -0
- package/dist/providers/provider-thinking.d.ts +23 -0
- package/dist/providers/provider-thinking.js +61 -0
- package/dist/providers/thinking-request.d.ts +14 -0
- package/dist/providers/thinking-request.js +87 -0
- package/dist/providers/xai.d.ts +33 -0
- package/dist/providers/xai.js +72 -0
- package/dist/schedules/index.d.ts +10 -0
- package/dist/schedules/index.js +6 -0
- package/dist/schedules/interval.d.ts +49 -0
- package/dist/schedules/interval.js +212 -0
- package/dist/schedules/prompt.d.ts +36 -0
- package/dist/schedules/prompt.js +36 -0
- package/dist/schedules/run-script.d.ts +8 -0
- package/dist/schedules/run-script.js +80 -0
- package/dist/schedules/scheduler.d.ts +101 -0
- package/dist/schedules/scheduler.js +167 -0
- package/dist/schedules/tools.d.ts +57 -0
- package/dist/schedules/tools.js +217 -0
- package/dist/schema.d.ts +499 -0
- package/dist/schema.js +131 -0
- package/dist/theme-palette.d.ts +17 -0
- package/dist/theme-palette.js +18 -0
- package/dist/thinking-catalog.d.ts +4 -0
- package/dist/thinking-catalog.js +13 -0
- package/dist/thinking.d.ts +37 -0
- package/dist/thinking.js +134 -0
- package/dist/tool-names.d.ts +77 -0
- package/dist/tool-names.js +53 -0
- package/dist/tools/index.d.ts +6 -0
- package/dist/tools/index.js +3 -0
- package/dist/tools/request-sandbox-access.d.ts +6 -0
- package/dist/tools/request-sandbox-access.js +80 -0
- package/dist/tools/utils.d.ts +20 -0
- package/dist/tools/utils.js +31 -0
- package/dist/tools/web-search.d.ts +116 -0
- package/dist/tools/web-search.js +158 -0
- package/dist/tools/workflows/create-workflow.d.ts +7 -0
- package/dist/tools/workflows/create-workflow.js +82 -0
- package/dist/tools/workflows/delete-workflow.d.ts +7 -0
- package/dist/tools/workflows/delete-workflow.js +18 -0
- package/dist/tools/workflows/ephemeral-memory.d.ts +28 -0
- package/dist/tools/workflows/ephemeral-memory.js +41 -0
- package/dist/tools/workflows/extract-json-schema.d.ts +12 -0
- package/dist/tools/workflows/extract-json-schema.js +19 -0
- package/dist/tools/workflows/get-workflow.d.ts +11 -0
- package/dist/tools/workflows/get-workflow.js +31 -0
- package/dist/tools/workflows/list-available-agents.d.ts +8 -0
- package/dist/tools/workflows/list-available-agents.js +29 -0
- package/dist/tools/workflows/list-available-classifiers.d.ts +19 -0
- package/dist/tools/workflows/list-available-classifiers.js +53 -0
- package/dist/tools/workflows/list-available-tools.d.ts +9 -0
- package/dist/tools/workflows/list-available-tools.js +37 -0
- package/dist/tools/workflows/list-available-workflows.d.ts +9 -0
- package/dist/tools/workflows/list-available-workflows.js +29 -0
- package/dist/tools/workflows/list-workflows.d.ts +9 -0
- package/dist/tools/workflows/list-workflows.js +30 -0
- package/dist/tools/workflows/run-workflow.d.ts +9 -0
- package/dist/tools/workflows/run-workflow.js +49 -0
- package/dist/tools/workflows/save-workflow.d.ts +1125 -0
- package/dist/tools/workflows/save-workflow.js +25 -0
- package/dist/tools/workflows/tool-ids.d.ts +16 -0
- package/dist/tools/workflows/tool-ids.js +17 -0
- package/dist/utils/binaries.d.ts +7 -0
- package/dist/utils/binaries.js +97 -0
- package/dist/utils/debug-log.d.ts +12 -0
- package/dist/utils/debug-log.js +56 -0
- package/dist/utils/discarding-scores-storage.d.ts +33 -0
- package/dist/utils/discarding-scores-storage.js +51 -0
- package/dist/utils/errors.d.ts +36 -0
- package/dist/utils/errors.js +215 -0
- package/dist/utils/gateway-sync.d.ts +26 -0
- package/dist/utils/gateway-sync.js +58 -0
- package/dist/utils/local-observability.d.ts +19 -0
- package/dist/utils/local-observability.js +54 -0
- package/dist/utils/maintenance-lock.d.ts +42 -0
- package/dist/utils/maintenance-lock.js +196 -0
- package/dist/utils/notification-dispatch.d.ts +74 -0
- package/dist/utils/notification-dispatch.js +128 -0
- package/dist/utils/open-url.d.ts +10 -0
- package/dist/utils/open-url.js +27 -0
- package/dist/utils/path-security.d.ts +2 -0
- package/dist/utils/path-security.js +7 -0
- package/dist/utils/plan-diff.d.ts +25 -0
- package/dist/utils/plan-diff.js +59 -0
- package/dist/utils/plans.d.ts +61 -0
- package/dist/utils/plans.js +120 -0
- package/dist/utils/project.d.ts +160 -0
- package/dist/utils/project.js +361 -0
- package/dist/utils/prune-cli.d.ts +24 -0
- package/dist/utils/prune-cli.js +124 -0
- package/dist/utils/signals-pubsub.d.ts +103 -0
- package/dist/utils/signals-pubsub.js +409 -0
- package/dist/utils/slash-command-loader.d.ts +52 -0
- package/dist/utils/slash-command-loader.js +139 -0
- package/dist/utils/slash-command-processor.d.ts +14 -0
- package/dist/utils/slash-command-processor.js +106 -0
- package/dist/utils/stdin-pipe.d.ts +33 -0
- package/dist/utils/stdin-pipe.js +93 -0
- package/dist/utils/storage-factory.d.ts +50 -0
- package/dist/utils/storage-factory.js +118 -0
- package/dist/utils/storage-maintenance.d.ts +121 -0
- package/dist/utils/storage-maintenance.js +352 -0
- package/dist/utils/thread-lock.d.ts +26 -0
- package/dist/utils/thread-lock.js +109 -0
- package/dist/utils/token-estimator.d.ts +3 -0
- package/dist/utils/token-estimator.js +19 -0
- package/dist/utils/update-check.d.ts +151 -0
- package/dist/utils/update-check.js +430 -0
- package/dist/utils/wake-session.d.ts +44 -0
- package/dist/utils/wake-session.js +83 -0
- package/dist/voice/stt-registry.d.ts +67 -0
- package/dist/voice/stt-registry.js +144 -0
- package/dist/voice/transcribe.d.ts +10 -0
- package/dist/voice/transcribe.js +75 -0
- package/dist/workflows/register-primitives.d.ts +52 -0
- package/dist/workflows/register-primitives.js +40 -0
- package/dist/workflows/service.d.ts +64 -0
- package/dist/workflows/service.js +50 -0
- package/package.json +117 -0
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { PerformanceObserver } from 'node:perf_hooks';
|
|
2
|
+
import type { PerformanceEntry } from 'node:perf_hooks';
|
|
3
|
+
import { getHeapSpaceStatistics, getHeapStatistics } from 'node:v8';
|
|
4
|
+
export declare const PROCESS_MEMORY_DIAGNOSTICS_DEFAULTS: {
|
|
5
|
+
readonly sampleIntervalMs: 10000;
|
|
6
|
+
readonly captureIntervalMs: 300000;
|
|
7
|
+
readonly allocationIntervalBytes: 524288;
|
|
8
|
+
};
|
|
9
|
+
export declare const PROCESS_MEMORY_DIAGNOSTICS_MINIMUMS: {
|
|
10
|
+
readonly sampleIntervalMs: 1000;
|
|
11
|
+
readonly captureIntervalMs: 10000;
|
|
12
|
+
readonly allocationIntervalBytes: 32768;
|
|
13
|
+
};
|
|
14
|
+
export interface ProcessMemoryDiagnosticsConfig {
|
|
15
|
+
parentDirectory: string;
|
|
16
|
+
sampleIntervalMs: number;
|
|
17
|
+
captureIntervalMs: number;
|
|
18
|
+
allocationIntervalBytes: number;
|
|
19
|
+
}
|
|
20
|
+
export interface ProcessMemoryDiagnosticsEnvironment {
|
|
21
|
+
MASTRACODE_PROFILE?: string;
|
|
22
|
+
MASTRACODE_PROFILE_DIR?: string;
|
|
23
|
+
MASTRACODE_PROFILE_SAMPLE_INTERVAL_MS?: string;
|
|
24
|
+
MASTRACODE_PROFILE_CAPTURE_INTERVAL_MS?: string;
|
|
25
|
+
MASTRACODE_PROFILE_ALLOCATION_INTERVAL_BYTES?: string;
|
|
26
|
+
}
|
|
27
|
+
export interface ProcessMemoryDiagnosticsMemorySample {
|
|
28
|
+
timestamp: string;
|
|
29
|
+
sequence: number;
|
|
30
|
+
elapsedMs: number;
|
|
31
|
+
memory: ReturnType<typeof process.memoryUsage>;
|
|
32
|
+
resourceUsage: ReturnType<typeof process.resourceUsage>;
|
|
33
|
+
heap: ReturnType<typeof getHeapStatistics>;
|
|
34
|
+
heapSpaces: ReturnType<typeof getHeapSpaceStatistics>;
|
|
35
|
+
}
|
|
36
|
+
export interface ProcessMemoryDiagnosticsStatus {
|
|
37
|
+
state: 'inactive' | 'starting' | 'active' | 'stopping' | 'error';
|
|
38
|
+
outputDirectory: string | null;
|
|
39
|
+
config: ProcessMemoryDiagnosticsConfig;
|
|
40
|
+
sampleCount: number;
|
|
41
|
+
captureCount: number;
|
|
42
|
+
gcEventCount: number;
|
|
43
|
+
latestSample: ProcessMemoryDiagnosticsMemorySample | null;
|
|
44
|
+
latestCapturePath: string | null;
|
|
45
|
+
error: string | null;
|
|
46
|
+
}
|
|
47
|
+
export interface ProcessMemoryDiagnosticsCapture {
|
|
48
|
+
path: string;
|
|
49
|
+
sequence: number;
|
|
50
|
+
timestamp: string;
|
|
51
|
+
reason: 'manual' | 'periodic' | 'stop';
|
|
52
|
+
}
|
|
53
|
+
interface InspectorSessionAdapter {
|
|
54
|
+
connect(): void;
|
|
55
|
+
disconnect(): void;
|
|
56
|
+
post(method: string, params?: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
57
|
+
}
|
|
58
|
+
interface PerformanceObserverAdapter {
|
|
59
|
+
observe(options: Parameters<PerformanceObserver['observe']>[0]): void;
|
|
60
|
+
disconnect(): void;
|
|
61
|
+
}
|
|
62
|
+
interface ProcessMemoryDiagnosticsDependencies {
|
|
63
|
+
createInspectorSession?: () => InspectorSessionAdapter;
|
|
64
|
+
createPerformanceObserver?: (callback: (entries: PerformanceEntry[]) => void) => PerformanceObserverAdapter;
|
|
65
|
+
now?: () => Date;
|
|
66
|
+
randomId?: () => string;
|
|
67
|
+
}
|
|
68
|
+
export interface ProcessMemoryDiagnosticsSetup {
|
|
69
|
+
diagnostics: ProcessMemoryDiagnostics;
|
|
70
|
+
enabled: boolean;
|
|
71
|
+
error: string | null;
|
|
72
|
+
}
|
|
73
|
+
export declare class ProcessMemoryDiagnosticsConfigError extends Error {
|
|
74
|
+
constructor(message: string);
|
|
75
|
+
}
|
|
76
|
+
export declare function parseProcessMemoryDiagnosticsEnvironment(env?: ProcessMemoryDiagnosticsEnvironment): {
|
|
77
|
+
enabled: boolean;
|
|
78
|
+
config: ProcessMemoryDiagnosticsConfig;
|
|
79
|
+
};
|
|
80
|
+
export declare function createProcessMemoryDiagnosticsFromEnvironment(env?: ProcessMemoryDiagnosticsEnvironment, dependencies?: ProcessMemoryDiagnosticsDependencies): ProcessMemoryDiagnosticsSetup;
|
|
81
|
+
export declare function startConfiguredProcessMemoryDiagnostics(setup: ProcessMemoryDiagnosticsSetup, warn: (message: string) => void): Promise<ProcessMemoryDiagnostics>;
|
|
82
|
+
export declare function stopProcessMemoryDiagnosticsWithTimeout(diagnostics: ProcessMemoryDiagnostics, warn: (message: string) => void, timeoutMs?: number): Promise<void>;
|
|
83
|
+
export declare class ProcessMemoryDiagnostics {
|
|
84
|
+
readonly config: ProcessMemoryDiagnosticsConfig;
|
|
85
|
+
private state;
|
|
86
|
+
private outputDirectory;
|
|
87
|
+
private inspector;
|
|
88
|
+
private observer;
|
|
89
|
+
private sampleTimer;
|
|
90
|
+
private captureTimer;
|
|
91
|
+
private startedAt;
|
|
92
|
+
private sampleCount;
|
|
93
|
+
private captureCount;
|
|
94
|
+
private gcEventCount;
|
|
95
|
+
private latestSample;
|
|
96
|
+
private latestCapturePath;
|
|
97
|
+
private latestError;
|
|
98
|
+
private samplingActive;
|
|
99
|
+
private stopRequested;
|
|
100
|
+
private startingPromise;
|
|
101
|
+
private stoppingPromise;
|
|
102
|
+
private restartAfterStopPromise;
|
|
103
|
+
private restartRequested;
|
|
104
|
+
private pendingGcEvents;
|
|
105
|
+
private gcEventBufferOverflowed;
|
|
106
|
+
private artifactWriteFailed;
|
|
107
|
+
private captureQueue;
|
|
108
|
+
private writeQueue;
|
|
109
|
+
private readonly configError;
|
|
110
|
+
private readonly createInspectorSession;
|
|
111
|
+
private readonly createPerformanceObserver;
|
|
112
|
+
private readonly now;
|
|
113
|
+
private readonly randomId;
|
|
114
|
+
constructor(config: ProcessMemoryDiagnosticsConfig, dependencies?: ProcessMemoryDiagnosticsDependencies, initialError?: string | null);
|
|
115
|
+
getStatus(): ProcessMemoryDiagnosticsStatus;
|
|
116
|
+
start(): Promise<ProcessMemoryDiagnosticsStatus>;
|
|
117
|
+
private startRun;
|
|
118
|
+
private abortStartIfRequested;
|
|
119
|
+
capture(reason?: 'manual' | 'periodic'): Promise<ProcessMemoryDiagnosticsCapture>;
|
|
120
|
+
stop(): Promise<ProcessMemoryDiagnosticsStatus>;
|
|
121
|
+
private createArtifacts;
|
|
122
|
+
private observeGc;
|
|
123
|
+
private startSampling;
|
|
124
|
+
private takeSample;
|
|
125
|
+
private enqueueWrite;
|
|
126
|
+
private enqueueWriteBatch;
|
|
127
|
+
private enqueueCapture;
|
|
128
|
+
private captureEpoch;
|
|
129
|
+
private restartSamplingAfterFailure;
|
|
130
|
+
private recordError;
|
|
131
|
+
private clearTimersAndObserver;
|
|
132
|
+
private disconnectInspector;
|
|
133
|
+
private cleanupAfterStartFailure;
|
|
134
|
+
}
|
|
135
|
+
export {};
|
|
136
|
+
//# sourceMappingURL=process-memory-diagnostics.d.ts.map
|
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
import { join } from "path";
|
|
2
|
+
import { homedir } from "os";
|
|
3
|
+
import { appendFile, chmod, mkdir, rename, rm, writeFile } from "fs/promises";
|
|
4
|
+
import { getHeapSpaceStatistics, getHeapStatistics } from "v8";
|
|
5
|
+
import { Session } from "inspector/promises";
|
|
6
|
+
import { PerformanceObserver } from "perf_hooks";
|
|
7
|
+
import { arch as arch$1, platform as platform$1 } from "process";
|
|
8
|
+
// src/process-memory-diagnostics.ts
|
|
9
|
+
const PROCESS_MEMORY_DIAGNOSTICS_DEFAULTS = {
|
|
10
|
+
sampleIntervalMs: 1e4,
|
|
11
|
+
captureIntervalMs: 3e5,
|
|
12
|
+
allocationIntervalBytes: 524288
|
|
13
|
+
};
|
|
14
|
+
const PROCESS_MEMORY_DIAGNOSTICS_MINIMUMS = {
|
|
15
|
+
sampleIntervalMs: 1e3,
|
|
16
|
+
captureIntervalMs: 1e4,
|
|
17
|
+
allocationIntervalBytes: 32768
|
|
18
|
+
};
|
|
19
|
+
const MAX_TIMER_DELAY_MS = 2147483647;
|
|
20
|
+
const MAX_PENDING_GC_EVENTS = 1e3;
|
|
21
|
+
var ProcessMemoryDiagnosticsConfigError = class extends Error {
|
|
22
|
+
constructor(message) {
|
|
23
|
+
super(message);
|
|
24
|
+
this.name = "ProcessMemoryDiagnosticsConfigError";
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
function isEnabled(value) {
|
|
28
|
+
return [
|
|
29
|
+
"1",
|
|
30
|
+
"true",
|
|
31
|
+
"yes",
|
|
32
|
+
"on"
|
|
33
|
+
].includes(value?.trim().toLowerCase() ?? "");
|
|
34
|
+
}
|
|
35
|
+
function parseBoundedInteger(env, name, fallback, minimum, maximum) {
|
|
36
|
+
const raw = env[name];
|
|
37
|
+
if (raw === void 0 || raw.trim() === "") return fallback;
|
|
38
|
+
const value = Number(raw);
|
|
39
|
+
if (!Number.isSafeInteger(value) || value < minimum || maximum !== void 0 && value > maximum) throw new ProcessMemoryDiagnosticsConfigError(`${name} must be an integer ${maximum === void 0 ? `greater than or equal to ${minimum}` : `between ${minimum} and ${maximum}`}; received ${JSON.stringify(raw)}.`);
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
function getDefaultProfileParentDirectory() {
|
|
43
|
+
if (process.env.MASTRA_APP_DATA_DIR) return join(process.env.MASTRA_APP_DATA_DIR, "profiles");
|
|
44
|
+
const baseDirectory = platform$1 === "darwin" ? join(homedir(), "Library", "Application Support") : platform$1 === "win32" ? process.env.APPDATA || join(homedir(), "AppData", "Roaming") : process.env.XDG_DATA_HOME || join(homedir(), ".local", "share");
|
|
45
|
+
return join(baseDirectory, "mastracode", "profiles");
|
|
46
|
+
}
|
|
47
|
+
function parseProcessMemoryDiagnosticsEnvironment(env = process.env) {
|
|
48
|
+
return {
|
|
49
|
+
enabled: isEnabled(env.MASTRACODE_PROFILE),
|
|
50
|
+
config: {
|
|
51
|
+
parentDirectory: env.MASTRACODE_PROFILE_DIR?.trim() || getDefaultProfileParentDirectory(),
|
|
52
|
+
sampleIntervalMs: parseBoundedInteger(env, "MASTRACODE_PROFILE_SAMPLE_INTERVAL_MS", PROCESS_MEMORY_DIAGNOSTICS_DEFAULTS.sampleIntervalMs, PROCESS_MEMORY_DIAGNOSTICS_MINIMUMS.sampleIntervalMs, MAX_TIMER_DELAY_MS),
|
|
53
|
+
captureIntervalMs: parseBoundedInteger(env, "MASTRACODE_PROFILE_CAPTURE_INTERVAL_MS", PROCESS_MEMORY_DIAGNOSTICS_DEFAULTS.captureIntervalMs, PROCESS_MEMORY_DIAGNOSTICS_MINIMUMS.captureIntervalMs, MAX_TIMER_DELAY_MS),
|
|
54
|
+
allocationIntervalBytes: parseBoundedInteger(env, "MASTRACODE_PROFILE_ALLOCATION_INTERVAL_BYTES", PROCESS_MEMORY_DIAGNOSTICS_DEFAULTS.allocationIntervalBytes, PROCESS_MEMORY_DIAGNOSTICS_MINIMUMS.allocationIntervalBytes)
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function createProcessMemoryDiagnosticsFromEnvironment(env = process.env, dependencies = {}) {
|
|
59
|
+
try {
|
|
60
|
+
const { enabled, config } = parseProcessMemoryDiagnosticsEnvironment(env);
|
|
61
|
+
return {
|
|
62
|
+
diagnostics: new ProcessMemoryDiagnostics(config, dependencies),
|
|
63
|
+
enabled,
|
|
64
|
+
error: null
|
|
65
|
+
};
|
|
66
|
+
} catch (error) {
|
|
67
|
+
const config = {
|
|
68
|
+
parentDirectory: env.MASTRACODE_PROFILE_DIR?.trim() || getDefaultProfileParentDirectory(),
|
|
69
|
+
...PROCESS_MEMORY_DIAGNOSTICS_DEFAULTS
|
|
70
|
+
};
|
|
71
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
72
|
+
return {
|
|
73
|
+
diagnostics: new ProcessMemoryDiagnostics(config, dependencies, message),
|
|
74
|
+
enabled: isEnabled(env.MASTRACODE_PROFILE),
|
|
75
|
+
error: message
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async function startConfiguredProcessMemoryDiagnostics(setup, warn) {
|
|
80
|
+
if (!setup.enabled) return setup.diagnostics;
|
|
81
|
+
if (setup.error) {
|
|
82
|
+
warn(`Process memory diagnostics were not started: ${setup.error}`);
|
|
83
|
+
return setup.diagnostics;
|
|
84
|
+
}
|
|
85
|
+
const status = await setup.diagnostics.start();
|
|
86
|
+
if (status.state !== "active") warn(`Process memory diagnostics were not started: ${status.error ?? "unknown inspector error"}`);
|
|
87
|
+
return setup.diagnostics;
|
|
88
|
+
}
|
|
89
|
+
async function stopProcessMemoryDiagnosticsWithTimeout(diagnostics, warn, timeoutMs = 5e3) {
|
|
90
|
+
let timeout;
|
|
91
|
+
const timedOut = await Promise.race([diagnostics.stop().then(() => false, (error) => {
|
|
92
|
+
warn(`Process memory diagnostics did not stop cleanly: ${errorMessage(error)}`);
|
|
93
|
+
return false;
|
|
94
|
+
}), new Promise((resolve) => {
|
|
95
|
+
timeout = setTimeout(() => resolve(true), timeoutMs);
|
|
96
|
+
timeout.unref();
|
|
97
|
+
})]);
|
|
98
|
+
if (timeout) clearTimeout(timeout);
|
|
99
|
+
if (timedOut) warn(`Process memory diagnostics did not stop within ${timeoutMs}ms; final artifacts may be incomplete.`);
|
|
100
|
+
}
|
|
101
|
+
function defaultInspectorSession() {
|
|
102
|
+
const session = new Session();
|
|
103
|
+
return {
|
|
104
|
+
connect: () => session.connect(),
|
|
105
|
+
disconnect: () => session.disconnect(),
|
|
106
|
+
post: async (method, params) => await session.post(method, params)
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function defaultPerformanceObserver(callback) {
|
|
110
|
+
return new PerformanceObserver((list) => callback(list.getEntries()));
|
|
111
|
+
}
|
|
112
|
+
function errorMessage(error) {
|
|
113
|
+
return error instanceof Error ? error.message : String(error);
|
|
114
|
+
}
|
|
115
|
+
function safeTimestamp(date) {
|
|
116
|
+
return date.toISOString().replaceAll(":", "-");
|
|
117
|
+
}
|
|
118
|
+
function unrefTimer(timer) {
|
|
119
|
+
timer.unref?.();
|
|
120
|
+
}
|
|
121
|
+
var ProcessMemoryDiagnostics = class {
|
|
122
|
+
config;
|
|
123
|
+
state = "inactive";
|
|
124
|
+
outputDirectory = null;
|
|
125
|
+
inspector = null;
|
|
126
|
+
observer = null;
|
|
127
|
+
sampleTimer = null;
|
|
128
|
+
captureTimer = null;
|
|
129
|
+
startedAt = null;
|
|
130
|
+
sampleCount = 0;
|
|
131
|
+
captureCount = 0;
|
|
132
|
+
gcEventCount = 0;
|
|
133
|
+
latestSample = null;
|
|
134
|
+
latestCapturePath = null;
|
|
135
|
+
latestError;
|
|
136
|
+
samplingActive = false;
|
|
137
|
+
stopRequested = false;
|
|
138
|
+
startingPromise = null;
|
|
139
|
+
stoppingPromise = null;
|
|
140
|
+
restartAfterStopPromise = null;
|
|
141
|
+
restartRequested = false;
|
|
142
|
+
pendingGcEvents = [];
|
|
143
|
+
gcEventBufferOverflowed = false;
|
|
144
|
+
artifactWriteFailed = false;
|
|
145
|
+
captureQueue = Promise.resolve();
|
|
146
|
+
writeQueue = Promise.resolve();
|
|
147
|
+
configError;
|
|
148
|
+
createInspectorSession;
|
|
149
|
+
createPerformanceObserver;
|
|
150
|
+
now;
|
|
151
|
+
randomId;
|
|
152
|
+
constructor(config, dependencies = {}, initialError = null) {
|
|
153
|
+
this.config = { ...config };
|
|
154
|
+
this.configError = initialError;
|
|
155
|
+
this.latestError = initialError;
|
|
156
|
+
this.createInspectorSession = dependencies.createInspectorSession ?? defaultInspectorSession;
|
|
157
|
+
this.createPerformanceObserver = dependencies.createPerformanceObserver ?? defaultPerformanceObserver;
|
|
158
|
+
this.now = dependencies.now ?? (() => /* @__PURE__ */ new Date());
|
|
159
|
+
this.randomId = dependencies.randomId ?? (() => globalThis.crypto.randomUUID());
|
|
160
|
+
}
|
|
161
|
+
getStatus() {
|
|
162
|
+
return {
|
|
163
|
+
state: this.state,
|
|
164
|
+
outputDirectory: this.outputDirectory,
|
|
165
|
+
config: { ...this.config },
|
|
166
|
+
sampleCount: this.sampleCount,
|
|
167
|
+
captureCount: this.captureCount,
|
|
168
|
+
gcEventCount: this.gcEventCount,
|
|
169
|
+
latestSample: this.latestSample,
|
|
170
|
+
latestCapturePath: this.latestCapturePath,
|
|
171
|
+
error: this.latestError
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
start() {
|
|
175
|
+
if (this.state === "active") return Promise.resolve(this.getStatus());
|
|
176
|
+
if (this.state === "starting" && this.startingPromise) return this.startingPromise;
|
|
177
|
+
if (this.state === "stopping" && this.stoppingPromise) {
|
|
178
|
+
this.restartRequested = true;
|
|
179
|
+
this.restartAfterStopPromise ??= this.stoppingPromise.then(() => {
|
|
180
|
+
this.restartAfterStopPromise = null;
|
|
181
|
+
if (!this.restartRequested) return this.getStatus();
|
|
182
|
+
this.restartRequested = false;
|
|
183
|
+
return this.start();
|
|
184
|
+
});
|
|
185
|
+
return this.restartAfterStopPromise;
|
|
186
|
+
}
|
|
187
|
+
if (this.configError) {
|
|
188
|
+
this.latestError = this.configError;
|
|
189
|
+
this.state = "error";
|
|
190
|
+
return Promise.resolve(this.getStatus());
|
|
191
|
+
}
|
|
192
|
+
this.state = "starting";
|
|
193
|
+
this.stoppingPromise = null;
|
|
194
|
+
this.restartRequested = false;
|
|
195
|
+
this.stopRequested = false;
|
|
196
|
+
this.outputDirectory = null;
|
|
197
|
+
this.sampleCount = 0;
|
|
198
|
+
this.captureCount = 0;
|
|
199
|
+
this.gcEventCount = 0;
|
|
200
|
+
this.pendingGcEvents = [];
|
|
201
|
+
this.gcEventBufferOverflowed = false;
|
|
202
|
+
this.artifactWriteFailed = false;
|
|
203
|
+
this.latestSample = null;
|
|
204
|
+
this.latestCapturePath = null;
|
|
205
|
+
this.latestError = null;
|
|
206
|
+
this.startedAt = this.now();
|
|
207
|
+
const startingPromise = this.startRun();
|
|
208
|
+
this.startingPromise = startingPromise;
|
|
209
|
+
startingPromise.finally(() => {
|
|
210
|
+
if (this.startingPromise === startingPromise) this.startingPromise = null;
|
|
211
|
+
});
|
|
212
|
+
return startingPromise;
|
|
213
|
+
}
|
|
214
|
+
async startRun() {
|
|
215
|
+
try {
|
|
216
|
+
await this.createArtifacts();
|
|
217
|
+
if (await this.abortStartIfRequested()) return this.getStatus();
|
|
218
|
+
this.observeGc();
|
|
219
|
+
this.inspector = this.createInspectorSession();
|
|
220
|
+
this.inspector.connect();
|
|
221
|
+
await this.startSampling();
|
|
222
|
+
if (await this.abortStartIfRequested()) return this.getStatus();
|
|
223
|
+
await this.takeSample();
|
|
224
|
+
if (await this.abortStartIfRequested()) return this.getStatus();
|
|
225
|
+
this.sampleTimer = setInterval(() => {
|
|
226
|
+
this.takeSample().catch((error) => this.recordError("Process sample failed", error));
|
|
227
|
+
}, this.config.sampleIntervalMs);
|
|
228
|
+
unrefTimer(this.sampleTimer);
|
|
229
|
+
this.captureTimer = setInterval(() => {
|
|
230
|
+
this.capture("periodic").catch((error) => this.recordError("Periodic allocation capture failed", error));
|
|
231
|
+
}, this.config.captureIntervalMs);
|
|
232
|
+
unrefTimer(this.captureTimer);
|
|
233
|
+
this.state = "active";
|
|
234
|
+
return this.getStatus();
|
|
235
|
+
} catch (error) {
|
|
236
|
+
this.latestError = `Unable to start process memory diagnostics: ${errorMessage(error)}`;
|
|
237
|
+
await this.cleanupAfterStartFailure();
|
|
238
|
+
this.state = "error";
|
|
239
|
+
return this.getStatus();
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
async abortStartIfRequested() {
|
|
243
|
+
if (!this.stopRequested) return false;
|
|
244
|
+
await this.cleanupAfterStartFailure();
|
|
245
|
+
this.state = "inactive";
|
|
246
|
+
return true;
|
|
247
|
+
}
|
|
248
|
+
capture(reason = "manual") {
|
|
249
|
+
if (this.state !== "active") return Promise.reject(new Error(this.latestError ?? "Process memory diagnostics are not active."));
|
|
250
|
+
return this.enqueueCapture(() => this.captureEpoch(reason, false));
|
|
251
|
+
}
|
|
252
|
+
async stop() {
|
|
253
|
+
if (this.stoppingPromise) {
|
|
254
|
+
this.restartRequested = false;
|
|
255
|
+
return this.stoppingPromise;
|
|
256
|
+
}
|
|
257
|
+
if (this.state === "inactive") return this.getStatus();
|
|
258
|
+
if (this.state === "error" && !this.inspector && !this.outputDirectory) return this.getStatus();
|
|
259
|
+
const startingPromise = this.state === "starting" ? this.startingPromise : null;
|
|
260
|
+
this.stopRequested = true;
|
|
261
|
+
this.state = "stopping";
|
|
262
|
+
this.clearTimersAndObserver();
|
|
263
|
+
if (startingPromise) {
|
|
264
|
+
this.stoppingPromise = startingPromise.then(() => this.getStatus());
|
|
265
|
+
return this.stoppingPromise;
|
|
266
|
+
}
|
|
267
|
+
this.stoppingPromise = (async () => {
|
|
268
|
+
let stopError = null;
|
|
269
|
+
try {
|
|
270
|
+
if (this.outputDirectory) await this.takeSample();
|
|
271
|
+
await this.enqueueCapture(async () => {
|
|
272
|
+
if (this.inspector && this.samplingActive) await this.captureEpoch("stop", true);
|
|
273
|
+
});
|
|
274
|
+
await this.writeQueue;
|
|
275
|
+
if (this.gcEventBufferOverflowed) throw new Error(`GC event buffer exceeded ${MAX_PENDING_GC_EVENTS} records before the next process sample.`);
|
|
276
|
+
if (this.artifactWriteFailed) throw new Error("One or more process memory diagnostic artifact writes failed.");
|
|
277
|
+
this.latestError = null;
|
|
278
|
+
} catch (error) {
|
|
279
|
+
stopError = `Unable to stop process memory diagnostics cleanly: ${errorMessage(error)}`;
|
|
280
|
+
this.latestError = stopError;
|
|
281
|
+
} finally {
|
|
282
|
+
this.disconnectInspector();
|
|
283
|
+
this.state = stopError ? "error" : "inactive";
|
|
284
|
+
}
|
|
285
|
+
return this.getStatus();
|
|
286
|
+
})();
|
|
287
|
+
return this.stoppingPromise;
|
|
288
|
+
}
|
|
289
|
+
async createArtifacts() {
|
|
290
|
+
await mkdir(this.config.parentDirectory, {
|
|
291
|
+
recursive: true,
|
|
292
|
+
mode: 448
|
|
293
|
+
});
|
|
294
|
+
await chmod(this.config.parentDirectory, 448);
|
|
295
|
+
const runName = `run-${safeTimestamp(this.startedAt)}-${process.pid}-${this.randomId().replaceAll(/[^a-zA-Z0-9-]/g, "").slice(0, 12)}`;
|
|
296
|
+
this.outputDirectory = join(this.config.parentDirectory, runName);
|
|
297
|
+
await mkdir(this.outputDirectory, { mode: 448 });
|
|
298
|
+
await chmod(this.outputDirectory, 448);
|
|
299
|
+
const metadata = {
|
|
300
|
+
schemaVersion: 1,
|
|
301
|
+
runId: runName,
|
|
302
|
+
startedAt: this.startedAt.toISOString(),
|
|
303
|
+
pid: process.pid,
|
|
304
|
+
nodeVersion: process.version,
|
|
305
|
+
platform: platform$1,
|
|
306
|
+
arch: arch$1,
|
|
307
|
+
config: this.config
|
|
308
|
+
};
|
|
309
|
+
await writeFile(join(this.outputDirectory, "metadata.json"), `${JSON.stringify(metadata, null, 2)}\n`, {
|
|
310
|
+
flag: "wx",
|
|
311
|
+
mode: 384
|
|
312
|
+
});
|
|
313
|
+
await writeFile(join(this.outputDirectory, "process-samples.jsonl"), "", {
|
|
314
|
+
flag: "wx",
|
|
315
|
+
mode: 384
|
|
316
|
+
});
|
|
317
|
+
await writeFile(join(this.outputDirectory, "gc-events.jsonl"), "", {
|
|
318
|
+
flag: "wx",
|
|
319
|
+
mode: 384
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
observeGc() {
|
|
323
|
+
const observer = this.createPerformanceObserver((entries) => {
|
|
324
|
+
const before = this.latestSample?.memory ?? null;
|
|
325
|
+
const after = process.memoryUsage();
|
|
326
|
+
const timestamp = this.now().toISOString();
|
|
327
|
+
for (const entry of entries) {
|
|
328
|
+
const gcEntry = entry;
|
|
329
|
+
this.gcEventCount += 1;
|
|
330
|
+
if (this.pendingGcEvents.length >= MAX_PENDING_GC_EVENTS) {
|
|
331
|
+
this.gcEventBufferOverflowed = true;
|
|
332
|
+
this.latestError = `GC event buffer exceeded ${MAX_PENDING_GC_EVENTS} records before the next process sample.`;
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
this.pendingGcEvents.push({
|
|
336
|
+
timestamp,
|
|
337
|
+
sequence: this.gcEventCount,
|
|
338
|
+
name: entry.name,
|
|
339
|
+
startTime: entry.startTime,
|
|
340
|
+
duration: entry.duration,
|
|
341
|
+
kind: gcEntry.detail?.kind ?? gcEntry.kind ?? null,
|
|
342
|
+
flags: gcEntry.detail?.flags ?? gcEntry.flags ?? null,
|
|
343
|
+
before,
|
|
344
|
+
after,
|
|
345
|
+
latestSampleSequence: this.latestSample?.sequence ?? null
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
observer.observe({ entryTypes: ["gc"] });
|
|
350
|
+
this.observer = observer;
|
|
351
|
+
}
|
|
352
|
+
async startSampling() {
|
|
353
|
+
if (!this.inspector) throw new Error("Inspector session is unavailable.");
|
|
354
|
+
await this.inspector.post("HeapProfiler.startSampling", {
|
|
355
|
+
samplingInterval: this.config.allocationIntervalBytes,
|
|
356
|
+
includeObjectsCollectedByMajorGC: true,
|
|
357
|
+
includeObjectsCollectedByMinorGC: true
|
|
358
|
+
});
|
|
359
|
+
this.samplingActive = true;
|
|
360
|
+
}
|
|
361
|
+
async takeSample() {
|
|
362
|
+
if (!this.outputDirectory || !this.startedAt) throw new Error("Diagnostics output directory is unavailable.");
|
|
363
|
+
const sample = {
|
|
364
|
+
timestamp: this.now().toISOString(),
|
|
365
|
+
sequence: this.sampleCount + 1,
|
|
366
|
+
elapsedMs: Math.max(0, this.now().getTime() - this.startedAt.getTime()),
|
|
367
|
+
memory: process.memoryUsage(),
|
|
368
|
+
resourceUsage: process.resourceUsage(),
|
|
369
|
+
heap: getHeapStatistics(),
|
|
370
|
+
heapSpaces: getHeapSpaceStatistics()
|
|
371
|
+
};
|
|
372
|
+
this.sampleCount = sample.sequence;
|
|
373
|
+
this.latestSample = sample;
|
|
374
|
+
await this.enqueueWrite("process-samples.jsonl", sample);
|
|
375
|
+
const gcEvents = this.pendingGcEvents.splice(0);
|
|
376
|
+
if (gcEvents.length > 0) await this.enqueueWriteBatch("gc-events.jsonl", gcEvents);
|
|
377
|
+
return sample;
|
|
378
|
+
}
|
|
379
|
+
enqueueWrite(fileName, value) {
|
|
380
|
+
return this.enqueueWriteBatch(fileName, [value]);
|
|
381
|
+
}
|
|
382
|
+
enqueueWriteBatch(fileName, values) {
|
|
383
|
+
const operation = this.writeQueue.then(async () => {
|
|
384
|
+
if (!this.outputDirectory || values.length === 0) return;
|
|
385
|
+
const lines = values.map((value) => JSON.stringify(value)).join("\n");
|
|
386
|
+
try {
|
|
387
|
+
await appendFile(join(this.outputDirectory, fileName), `${lines}\n`, { mode: 384 });
|
|
388
|
+
} catch (error) {
|
|
389
|
+
this.artifactWriteFailed = true;
|
|
390
|
+
throw error;
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
this.writeQueue = operation.catch(() => void 0);
|
|
394
|
+
return operation;
|
|
395
|
+
}
|
|
396
|
+
enqueueCapture(operation) {
|
|
397
|
+
const result = this.captureQueue.then(operation);
|
|
398
|
+
this.captureQueue = result.catch(() => void 0);
|
|
399
|
+
return result;
|
|
400
|
+
}
|
|
401
|
+
async captureEpoch(reason, final) {
|
|
402
|
+
if (!this.inspector || !this.samplingActive || !this.outputDirectory) throw new Error(this.latestError ?? "Allocation sampling is not active.");
|
|
403
|
+
let response;
|
|
404
|
+
try {
|
|
405
|
+
response = await this.inspector.post("HeapProfiler.stopSampling");
|
|
406
|
+
this.samplingActive = false;
|
|
407
|
+
} catch (error) {
|
|
408
|
+
this.latestError = `Allocation capture failed: ${errorMessage(error)}`;
|
|
409
|
+
if (!final && !this.stopRequested) await this.restartSamplingAfterFailure();
|
|
410
|
+
throw new Error(this.latestError, { cause: error });
|
|
411
|
+
}
|
|
412
|
+
const profile = response.profile;
|
|
413
|
+
if (!profile || typeof profile !== "object") {
|
|
414
|
+
this.latestError = "Allocation capture failed: inspector returned no profile.";
|
|
415
|
+
if (!final && !this.stopRequested) await this.restartSamplingAfterFailure();
|
|
416
|
+
throw new Error(this.latestError);
|
|
417
|
+
}
|
|
418
|
+
const sequence = this.captureCount + 1;
|
|
419
|
+
const timestamp = this.now().toISOString();
|
|
420
|
+
const filename = `allocation-${String(sequence).padStart(6, "0")}-${safeTimestamp(new Date(timestamp))}.heapprofile`;
|
|
421
|
+
const finalPath = join(this.outputDirectory, filename);
|
|
422
|
+
const temporaryPath = join(this.outputDirectory, `.${filename}.${this.randomId()}.tmp`);
|
|
423
|
+
try {
|
|
424
|
+
await writeFile(temporaryPath, `${JSON.stringify(profile)}\n`, {
|
|
425
|
+
flag: "wx",
|
|
426
|
+
mode: 384
|
|
427
|
+
});
|
|
428
|
+
await chmod(temporaryPath, 384);
|
|
429
|
+
await rename(temporaryPath, finalPath);
|
|
430
|
+
await chmod(finalPath, 384);
|
|
431
|
+
this.captureCount = sequence;
|
|
432
|
+
this.latestCapturePath = finalPath;
|
|
433
|
+
this.latestError = null;
|
|
434
|
+
} catch (error) {
|
|
435
|
+
this.artifactWriteFailed = true;
|
|
436
|
+
this.latestError = `Unable to persist allocation capture: ${errorMessage(error)}`;
|
|
437
|
+
throw new Error(this.latestError, { cause: error });
|
|
438
|
+
} finally {
|
|
439
|
+
if (!final && !this.stopRequested && this.state === "active") try {
|
|
440
|
+
await this.startSampling();
|
|
441
|
+
} catch (error) {
|
|
442
|
+
this.latestError = `Unable to restart allocation sampling: ${errorMessage(error)}`;
|
|
443
|
+
this.state = "error";
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
if (!final && !this.stopRequested && !this.samplingActive) throw new Error(this.latestError ?? "Allocation sampling did not restart.");
|
|
447
|
+
return {
|
|
448
|
+
path: finalPath,
|
|
449
|
+
sequence,
|
|
450
|
+
timestamp,
|
|
451
|
+
reason
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
async restartSamplingAfterFailure() {
|
|
455
|
+
try {
|
|
456
|
+
await this.startSampling();
|
|
457
|
+
} catch (error) {
|
|
458
|
+
this.latestError = `Unable to restart allocation sampling: ${errorMessage(error)}`;
|
|
459
|
+
this.state = "error";
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
recordError(prefix, error) {
|
|
463
|
+
this.latestError = `${prefix}: ${errorMessage(error)}`;
|
|
464
|
+
}
|
|
465
|
+
clearTimersAndObserver() {
|
|
466
|
+
if (this.sampleTimer) clearInterval(this.sampleTimer);
|
|
467
|
+
if (this.captureTimer) clearInterval(this.captureTimer);
|
|
468
|
+
this.sampleTimer = null;
|
|
469
|
+
this.captureTimer = null;
|
|
470
|
+
this.observer?.disconnect();
|
|
471
|
+
this.observer = null;
|
|
472
|
+
}
|
|
473
|
+
disconnectInspector() {
|
|
474
|
+
this.samplingActive = false;
|
|
475
|
+
if (!this.inspector) return;
|
|
476
|
+
try {
|
|
477
|
+
this.inspector.disconnect();
|
|
478
|
+
} catch {}
|
|
479
|
+
this.inspector = null;
|
|
480
|
+
}
|
|
481
|
+
async cleanupAfterStartFailure() {
|
|
482
|
+
this.clearTimersAndObserver();
|
|
483
|
+
this.disconnectInspector();
|
|
484
|
+
await this.writeQueue;
|
|
485
|
+
try {
|
|
486
|
+
if (this.outputDirectory) await rm(this.outputDirectory, {
|
|
487
|
+
recursive: true,
|
|
488
|
+
force: true
|
|
489
|
+
});
|
|
490
|
+
} catch (error) {
|
|
491
|
+
this.latestError = `${this.latestError} Failed to remove partial artifacts: ${errorMessage(error)}`;
|
|
492
|
+
} finally {
|
|
493
|
+
this.outputDirectory = null;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
};
|
|
497
|
+
export { PROCESS_MEMORY_DIAGNOSTICS_DEFAULTS, PROCESS_MEMORY_DIAGNOSTICS_MINIMUMS, ProcessMemoryDiagnostics, ProcessMemoryDiagnosticsConfigError, createProcessMemoryDiagnosticsFromEnvironment, parseProcessMemoryDiagnosticsEnvironment, startConfiguredProcessMemoryDiagnostics, stopProcessMemoryDiagnosticsWithTimeout };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Processor that aborts the agentic loop immediately after a `submit_plan` tool
|
|
3
|
+
* returns a rejection without inline feedback. This prevents the model from
|
|
4
|
+
* generating any text after the user clicks "Request Changes" — the tool result
|
|
5
|
+
* is persisted in thread history (for context on the next run) but no follow-up
|
|
6
|
+
* LLM call is made.
|
|
7
|
+
*
|
|
8
|
+
* A rejection that carries inline feedback is left alone: its result tells the
|
|
9
|
+
* model to revise the plan and submit again, so the run must continue.
|
|
10
|
+
*/
|
|
11
|
+
import type { Processor, ProcessInputStepArgs } from '@mastra/core/processors';
|
|
12
|
+
export declare class PlanRejectionAbortProcessor implements Processor<'plan-rejection-abort'> {
|
|
13
|
+
id: 'plan-rejection-abort';
|
|
14
|
+
processInputStep({ messages, stepNumber, abort }: ProcessInputStepArgs): Promise<undefined>;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=plan-rejection-abort.d.ts.map
|