@tonyclaw/agent-inspector 4.0.1 → 4.0.2
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/.output/backend/nitro.json +1 -1
- package/.output/cli.js +192 -109
- package/.output/server/_ssr/index.mjs +1 -1
- package/.output/server/_ssr/{router-CTHqmroT.mjs → router-BBY_zWRK.mjs} +91 -3
- package/.output/server/index.mjs +1 -1
- package/.output/ui/assets/{CompareDrawer-L3n4J-Or.js → CompareDrawer-rtNElN-B.js} +1 -1
- package/.output/ui/assets/{InspectorPet-D5o_3OZS.js → InspectorPet-QaAiuF3d.js} +1 -1
- package/.output/ui/assets/{ProxyViewerContainer-Ocagx75S.js → ProxyViewerContainer-6twNZHwl.js} +4 -4
- package/.output/ui/assets/{ReplayDialog-Bk7x1h-y.js → ReplayDialog-B7KlziO2.js} +1 -1
- package/.output/ui/assets/{RequestAnatomy-U4lzL7Sc.js → RequestAnatomy-2XVFBrW4.js} +1 -1
- package/.output/ui/assets/{ResponseView-LOWfvBLK.js → ResponseView-BULgzLVB.js} +1 -1
- package/.output/ui/assets/{StreamingChunkSequence-DPfsrrXQ.js → StreamingChunkSequence-DSkvkbns.js} +1 -1
- package/.output/ui/assets/{_sessionId-OdX2Jvt1.js → _sessionId-D2dmJi3G.js} +1 -1
- package/.output/ui/assets/{_sessionId-0MlWS-CO.js → _sessionId-DcS0s1Mr.js} +1 -1
- package/.output/ui/assets/{index-Du4qNOOt.js → index-B0TN1KKO.js} +2 -2
- package/.output/ui/assets/{index-gvFj_9K6.js → index-Bva1zHEs.js} +1 -1
- package/.output/ui/assets/{index-Ea-c8hPg.js → index-BzBExOxu.js} +1 -1
- package/.output/ui/assets/{index-zVVS53Hd.js → index-k6sRQz55.js} +1 -1
- package/.output/ui/assets/{json-viewer-C1WAMzg3.js → json-viewer-CWM4tY-j.js} +1 -1
- package/.output/ui/assets/{jszip.min-B8AKu2zm.js → jszip.min-BkWvRFlo.js} +1 -1
- package/.output/ui/index.html +1 -1
- package/package.json +5 -10
- package/src/assets/IDE/claudecode-color.svg +0 -1
- package/src/assets/IDE/codex-color.svg +0 -1
- package/src/assets/IDE/opencode.svg +0 -1
- package/src/assets/IDE/xiaomimimo.svg +0 -1
- package/src/assets/favicon.svg +0 -21
- package/src/assets/logos/alibaba.svg +0 -1
- package/src/assets/logos/anthropic.svg +0 -1
- package/src/assets/logos/claude-code.svg +0 -4
- package/src/assets/logos/deepseek.svg +0 -1
- package/src/assets/logos/minimax.jpeg +0 -0
- package/src/assets/logos/openai.svg +0 -1
- package/src/assets/logos/opencode.svg +0 -4
- package/src/assets/logos/qwen.webp +0 -0
- package/src/assets/logos/zhipuai.svg +0 -219
- package/src/backend/router.tsx +0 -36
- package/src/backend/routes/$.ts +0 -26
- package/src/backend/routes/__root.ts +0 -4
- package/src/backend/routes/api/-instances.ts +0 -7
- package/src/backend/routes/api/alerts.summary.ts +0 -28
- package/src/backend/routes/api/alerts.ts +0 -70
- package/src/backend/routes/api/config.paths.ts +0 -14
- package/src/backend/routes/api/config.ts +0 -73
- package/src/backend/routes/api/ecosystem.packages.$packageId.help.ts +0 -11
- package/src/backend/routes/api/ecosystem.packages.$packageId.install.ts +0 -11
- package/src/backend/routes/api/ecosystem.packages.$packageId.runner-presets.ts +0 -11
- package/src/backend/routes/api/ecosystem.packages.$packageId.upgrade.ts +0 -11
- package/src/backend/routes/api/ecosystem.packages.ts +0 -21
- package/src/backend/routes/api/ecosystem.recipes.$recipeId.run.ts +0 -11
- package/src/backend/routes/api/ecosystem.tasks.$taskId.ts +0 -16
- package/src/backend/routes/api/ecosystem.tasks.ts +0 -12
- package/src/backend/routes/api/groups.$groupId.evidence.ts +0 -55
- package/src/backend/routes/api/groups.$groupId.sessions.ts +0 -50
- package/src/backend/routes/api/groups.$groupId.ts +0 -93
- package/src/backend/routes/api/groups.ts +0 -79
- package/src/backend/routes/api/health.ts +0 -22
- package/src/backend/routes/api/instances.$name.connection.ts +0 -12
- package/src/backend/routes/api/instances.$name.restart.ts +0 -23
- package/src/backend/routes/api/instances.$name.start.ts +0 -18
- package/src/backend/routes/api/instances.$name.stop.ts +0 -23
- package/src/backend/routes/api/instances.$name.ts +0 -12
- package/src/backend/routes/api/instances.ts +0 -16
- package/src/backend/routes/api/knowledge.candidates.$candidateId.promote.ts +0 -32
- package/src/backend/routes/api/knowledge.candidates.$candidateId.ts +0 -50
- package/src/backend/routes/api/knowledge.candidates.ts +0 -10
- package/src/backend/routes/api/knowledge.project-context.ts +0 -29
- package/src/backend/routes/api/knowledge.search.ts +0 -62
- package/src/backend/routes/api/knowledge.sessions.$sessionId.candidates.ts +0 -26
- package/src/backend/routes/api/logs.$id.body.ts +0 -58
- package/src/backend/routes/api/logs.$id.chunks.ts +0 -37
- package/src/backend/routes/api/logs.$id.replay.ts +0 -297
- package/src/backend/routes/api/logs.$id.ts +0 -23
- package/src/backend/routes/api/logs.import.ts +0 -39
- package/src/backend/routes/api/logs.stream.ts +0 -174
- package/src/backend/routes/api/logs.ts +0 -214
- package/src/backend/routes/api/mcp.ts +0 -25
- package/src/backend/routes/api/models.ts +0 -10
- package/src/backend/routes/api/pi-agent.ts +0 -29
- package/src/backend/routes/api/providers.$providerId.model-metadata.ts +0 -173
- package/src/backend/routes/api/providers.$providerId.test.log.ts +0 -342
- package/src/backend/routes/api/providers.$providerId.ts +0 -111
- package/src/backend/routes/api/providers.export.ts +0 -44
- package/src/backend/routes/api/providers.import.ts +0 -81
- package/src/backend/routes/api/providers.scan.ts +0 -28
- package/src/backend/routes/api/providers.ts +0 -102
- package/src/backend/routes/api/runs.$runId.evidence.ts +0 -55
- package/src/backend/routes/api/runs.$runId.ts +0 -50
- package/src/backend/routes/api/runs.ts +0 -62
- package/src/backend/routes/api/runtime.ts +0 -46
- package/src/backend/routes/api/sessions.ts +0 -51
- package/src/backend/routes/api/storage.ts +0 -56
- package/src/backend/routes/livez.ts +0 -13
- package/src/backend/routes/metrics.ts +0 -14
- package/src/backend/routes/proxy/$.ts +0 -15
- package/src/backend/routes/readyz.ts +0 -18
- package/src/backend/start.ts +0 -10
- package/src/cli/alias.ts +0 -431
- package/src/cli/detect-tools.ts +0 -123
- package/src/cli/doctor.ts +0 -836
- package/src/cli/instance.ts +0 -1071
- package/src/cli/instanceApi.ts +0 -213
- package/src/cli/instanceArgs.ts +0 -301
- package/src/cli/instanceControl.ts +0 -191
- package/src/cli/instanceModel.ts +0 -276
- package/src/cli/instanceRegistry.ts +0 -731
- package/src/cli/networkHints.ts +0 -170
- package/src/cli/onboard.ts +0 -1668
- package/src/cli/onboardTarget.ts +0 -228
- package/src/cli/rustBackendPackage.ts +0 -458
- package/src/cli/startupOutput.ts +0 -16
- package/src/cli/templates/codex-skill-onboard.ts +0 -320
- package/src/cli/templates/command-onboard.ts +0 -25
- package/src/cli/templates/skill-onboard.ts +0 -625
- package/src/cli.ts +0 -1433
- package/src/components/OnboardingBanner.tsx +0 -115
- package/src/components/ProxyViewer.tsx +0 -2259
- package/src/components/ProxyViewerContainer.tsx +0 -1076
- package/src/components/alerts/AlertsDialog.tsx +0 -615
- package/src/components/clients/ClientLogo.tsx +0 -175
- package/src/components/ecosystem/AgentLabDialog.tsx +0 -947
- package/src/components/errors/SafeErrorBoundary.tsx +0 -201
- package/src/components/groups/GroupsDialog.tsx +0 -1304
- package/src/components/inspector-pet/InspectorPet.tsx +0 -398
- package/src/components/inspector-pet/InspectorPetCrab3D.tsx +0 -455
- package/src/components/inspector-pet/inspectorPetLogic.ts +0 -272
- package/src/components/pi-agent/PiAgentPanel.tsx +0 -446
- package/src/components/pi-agent/piAgentChatLogic.ts +0 -273
- package/src/components/providers/ImportWizardDialog.tsx +0 -281
- package/src/components/providers/ProviderCard.tsx +0 -347
- package/src/components/providers/ProviderForm.tsx +0 -1183
- package/src/components/providers/ProviderLogo.tsx +0 -122
- package/src/components/providers/ProviderTestResultsView.tsx +0 -734
- package/src/components/providers/ProvidersPanel.tsx +0 -714
- package/src/components/providers/SettingsDialog.tsx +0 -1042
- package/src/components/providers/providerFormCapabilities.ts +0 -36
- package/src/components/proxy-viewer/AgentTraceSummary.tsx +0 -902
- package/src/components/proxy-viewer/AnswerMarkdown.tsx +0 -51
- package/src/components/proxy-viewer/ApplicationBar.tsx +0 -123
- package/src/components/proxy-viewer/CompareDrawer.tsx +0 -984
- package/src/components/proxy-viewer/ConversationGroup.tsx +0 -208
- package/src/components/proxy-viewer/ConversationGroupList.tsx +0 -652
- package/src/components/proxy-viewer/ConversationHeader.tsx +0 -429
- package/src/components/proxy-viewer/LiveConnectionStatus.tsx +0 -186
- package/src/components/proxy-viewer/LogEntry.tsx +0 -1419
- package/src/components/proxy-viewer/LogEntryHeader.tsx +0 -596
- package/src/components/proxy-viewer/ProviderLogoStack.tsx +0 -57
- package/src/components/proxy-viewer/RawExportWarning.tsx +0 -77
- package/src/components/proxy-viewer/ReplayDialog.tsx +0 -636
- package/src/components/proxy-viewer/RequestToolsPanel.tsx +0 -299
- package/src/components/proxy-viewer/ResponseView.tsx +0 -202
- package/src/components/proxy-viewer/SessionLoadProgressBar.tsx +0 -61
- package/src/components/proxy-viewer/StreamingChunkSequence.tsx +0 -265
- package/src/components/proxy-viewer/ThreadConnector.tsx +0 -165
- package/src/components/proxy-viewer/ToolTraceEvents.tsx +0 -64
- package/src/components/proxy-viewer/TurnGroup.tsx +0 -383
- package/src/components/proxy-viewer/TurnGroupList.tsx +0 -156
- package/src/components/proxy-viewer/accessibility.ts +0 -8
- package/src/components/proxy-viewer/anatomy/RequestAnatomy.tsx +0 -887
- package/src/components/proxy-viewer/anatomy/SegmentBar.tsx +0 -222
- package/src/components/proxy-viewer/anatomy/contextIntelligence.ts +0 -737
- package/src/components/proxy-viewer/anatomy/sessionContextSummary.ts +0 -268
- package/src/components/proxy-viewer/anatomy/tokenEstimate.ts +0 -53
- package/src/components/proxy-viewer/anatomy/types.ts +0 -41
- package/src/components/proxy-viewer/anatomy/useAnatomyJump.ts +0 -114
- package/src/components/proxy-viewer/bodyHydration.ts +0 -123
- package/src/components/proxy-viewer/cacheTrend.ts +0 -50
- package/src/components/proxy-viewer/diff/DiffView.tsx +0 -321
- package/src/components/proxy-viewer/diff/computeDiff.ts +0 -178
- package/src/components/proxy-viewer/diff/index.ts +0 -3
- package/src/components/proxy-viewer/formats/anthropic/ContentBlocks.tsx +0 -194
- package/src/components/proxy-viewer/formats/anthropic/ResponseView.tsx +0 -81
- package/src/components/proxy-viewer/formats/anthropic/thinkingExtract.ts +0 -21
- package/src/components/proxy-viewer/formats/index.tsx +0 -47
- package/src/components/proxy-viewer/formats/openai/ResponseView.tsx +0 -442
- package/src/components/proxy-viewer/index.ts +0 -9
- package/src/components/proxy-viewer/lazy.ts +0 -41
- package/src/components/proxy-viewer/liveConnectionState.ts +0 -158
- package/src/components/proxy-viewer/log-formats/anthropic.ts +0 -191
- package/src/components/proxy-viewer/log-formats/index.ts +0 -23
- package/src/components/proxy-viewer/log-formats/openai.ts +0 -256
- package/src/components/proxy-viewer/log-formats/types.ts +0 -40
- package/src/components/proxy-viewer/log-formats/unknown.ts +0 -18
- package/src/components/proxy-viewer/logEntryVisibility.ts +0 -73
- package/src/components/proxy-viewer/logFocus.ts +0 -54
- package/src/components/proxy-viewer/proxyViewerContainerLogic.ts +0 -124
- package/src/components/proxy-viewer/proxyViewerLogic.ts +0 -488
- package/src/components/proxy-viewer/replayComparison.ts +0 -131
- package/src/components/proxy-viewer/requestDiff.ts +0 -277
- package/src/components/proxy-viewer/requestTools.ts +0 -220
- package/src/components/proxy-viewer/useCopyFeedback.ts +0 -38
- package/src/components/proxy-viewer/useKeyboardNavigation.ts +0 -232
- package/src/components/proxy-viewer/viewerState.ts +0 -176
- package/src/components/ui/badge.tsx +0 -47
- package/src/components/ui/button.tsx +0 -47
- package/src/components/ui/collapsible.tsx +0 -21
- package/src/components/ui/confirm-dialog.tsx +0 -51
- package/src/components/ui/crab-logo.tsx +0 -45
- package/src/components/ui/crab-variants.tsx +0 -467
- package/src/components/ui/dialog.tsx +0 -130
- package/src/components/ui/icon-trigger.ts +0 -2
- package/src/components/ui/json-viewer-bulk.ts +0 -132
- package/src/components/ui/json-viewer.tsx +0 -508
- package/src/components/ui/scroll-area.tsx +0 -54
- package/src/components/ui/select.tsx +0 -178
- package/src/components/ui/separator.tsx +0 -28
- package/src/components/ui/tabs.tsx +0 -88
- package/src/components/ui/tooltip.tsx +0 -51
- package/src/components/ui/transient-toast.tsx +0 -105
- package/src/contracts/anthropic.ts +0 -230
- package/src/contracts/index.ts +0 -59
- package/src/contracts/json.ts +0 -37
- package/src/contracts/log.ts +0 -139
- package/src/contracts/openai.ts +0 -267
- package/src/index.css +0 -11
- package/src/knowledge/candidateStore.ts +0 -94
- package/src/knowledge/distiller.ts +0 -98
- package/src/knowledge/openclawClient.ts +0 -161
- package/src/knowledge/openclawGatewayClient.ts +0 -249
- package/src/knowledge/openclawMarkdown.ts +0 -146
- package/src/knowledge/redactor.ts +0 -80
- package/src/knowledge/types.ts +0 -83
- package/src/lib/alertContract.ts +0 -76
- package/src/lib/apiClient.ts +0 -166
- package/src/lib/apiFormat.ts +0 -29
- package/src/lib/backendImplementation.ts +0 -16
- package/src/lib/basePath.ts +0 -107
- package/src/lib/browserRuntimeContract.ts +0 -124
- package/src/lib/browserRuntimeResolver.ts +0 -333
- package/src/lib/chunkLoadRecovery.ts +0 -83
- package/src/lib/claudeCodeStrip.ts +0 -98
- package/src/lib/clipboard.ts +0 -67
- package/src/lib/codingAgentEndpoints.ts +0 -54
- package/src/lib/ecosystemContract.ts +0 -130
- package/src/lib/export-logs.ts +0 -287
- package/src/lib/groupContract.ts +0 -166
- package/src/lib/instanceContract.ts +0 -239
- package/src/lib/jsonFile.ts +0 -46
- package/src/lib/logImportContract.ts +0 -12
- package/src/lib/managedInstance.ts +0 -112
- package/src/lib/mask.ts +0 -4
- package/src/lib/objectUtils.ts +0 -22
- package/src/lib/piAgentContract.ts +0 -102
- package/src/lib/providerContract.ts +0 -125
- package/src/lib/providerImportContract.ts +0 -27
- package/src/lib/providerModelMetadata.ts +0 -458
- package/src/lib/providerTestContract.ts +0 -170
- package/src/lib/providerTestPrompt.ts +0 -100
- package/src/lib/publicBasePathContext.ts +0 -137
- package/src/lib/resourceLimits.ts +0 -345
- package/src/lib/runContract.ts +0 -164
- package/src/lib/runtimeConfig.ts +0 -43
- package/src/lib/safeDiagnostic.ts +0 -38
- package/src/lib/safeFetch.ts +0 -508
- package/src/lib/serverPort.ts +0 -47
- package/src/lib/sessionInfoContract.ts +0 -78
- package/src/lib/sessionUrl.ts +0 -46
- package/src/lib/ssrfGuard.ts +0 -133
- package/src/lib/stopReason.ts +0 -130
- package/src/lib/themeMode.ts +0 -41
- package/src/lib/timeDisplay.ts +0 -22
- package/src/lib/toolTrace.ts +0 -290
- package/src/lib/uiScale.ts +0 -117
- package/src/lib/unknown.ts +0 -27
- package/src/lib/upstreamUrl.ts +0 -150
- package/src/lib/useAlerts.ts +0 -82
- package/src/lib/useGroupEvidence.ts +0 -37
- package/src/lib/useGroups.ts +0 -32
- package/src/lib/useOnboarding.ts +0 -93
- package/src/lib/useProviders.ts +0 -30
- package/src/lib/useStripConfig.ts +0 -154
- package/src/lib/useThemeMode.ts +0 -40
- package/src/lib/useUiScale.ts +0 -52
- package/src/lib/utils.ts +0 -78
- package/src/mcp/currentContext.ts +0 -65
- package/src/mcp/instanceHandlers.ts +0 -151
- package/src/mcp/loopback.ts +0 -88
- package/src/mcp/mode.ts +0 -75
- package/src/mcp/previewExtractor.ts +0 -233
- package/src/mcp/server.ts +0 -2274
- package/src/mcp/toolHandlers.ts +0 -1533
- package/src/proxy/alerts.ts +0 -664
- package/src/proxy/chunkStorage.ts +0 -123
- package/src/proxy/config.ts +0 -218
- package/src/proxy/constants.ts +0 -66
- package/src/proxy/dataDir.ts +0 -147
- package/src/proxy/ecosystemExecutionConfirmation.ts +0 -114
- package/src/proxy/ecosystemExecutionRoute.ts +0 -116
- package/src/proxy/ecosystemRegistry.ts +0 -296
- package/src/proxy/ecosystemTasks.ts +0 -415
- package/src/proxy/evidenceAnalysis.ts +0 -526
- package/src/proxy/evidenceExporter.ts +0 -235
- package/src/proxy/formats/anthropic/anthropicProvider.ts +0 -75
- package/src/proxy/formats/anthropic/handler.ts +0 -75
- package/src/proxy/formats/anthropic/index.ts +0 -13
- package/src/proxy/formats/anthropic/register.ts +0 -4
- package/src/proxy/formats/anthropic/schemas.ts +0 -6
- package/src/proxy/formats/anthropic/stream.ts +0 -205
- package/src/proxy/formats/handler.ts +0 -46
- package/src/proxy/formats/index.ts +0 -13
- package/src/proxy/formats/jsonSchema.ts +0 -2
- package/src/proxy/formats/openai/alibabaProvider.ts +0 -34
- package/src/proxy/formats/openai/codexMetadata.ts +0 -61
- package/src/proxy/formats/openai/handler.ts +0 -133
- package/src/proxy/formats/openai/index.ts +0 -33
- package/src/proxy/formats/openai/provider.ts +0 -50
- package/src/proxy/formats/openai/register.ts +0 -4
- package/src/proxy/formats/openai/schemas.ts +0 -25
- package/src/proxy/formats/openai/stream.ts +0 -600
- package/src/proxy/formats/openai/zhipuProvider.ts +0 -31
- package/src/proxy/formats/providerAdapter.ts +0 -50
- package/src/proxy/formats/providerRegistry.ts +0 -51
- package/src/proxy/formats/providers/index.ts +0 -3
- package/src/proxy/formats/registry.ts +0 -65
- package/src/proxy/groupEvidenceExporter.ts +0 -371
- package/src/proxy/groupStore.ts +0 -336
- package/src/proxy/handler.ts +0 -729
- package/src/proxy/identityProxy.ts +0 -1842
- package/src/proxy/jsonlRecovery.ts +0 -554
- package/src/proxy/logBodyChunks.ts +0 -91
- package/src/proxy/logFinalizer.ts +0 -777
- package/src/proxy/logFinalizer.worker.ts +0 -24
- package/src/proxy/logImportUpload.ts +0 -128
- package/src/proxy/logImporter.ts +0 -813
- package/src/proxy/logIndex.ts +0 -663
- package/src/proxy/logSearch.ts +0 -122
- package/src/proxy/logger.ts +0 -537
- package/src/proxy/openaiOrphanToolStrip.ts +0 -142
- package/src/proxy/platformCommands.ts +0 -258
- package/src/proxy/privateDataPath.ts +0 -419
- package/src/proxy/providerImporters.ts +0 -578
- package/src/proxy/providerScanStore.ts +0 -87
- package/src/proxy/providerSecretStore.ts +0 -216
- package/src/proxy/providers.ts +0 -998
- package/src/proxy/rawStreamCapture.ts +0 -342
- package/src/proxy/runFailures.ts +0 -94
- package/src/proxy/runStore.ts +0 -214
- package/src/proxy/runtimeAdmission.ts +0 -55
- package/src/proxy/runtimeHealth.ts +0 -221
- package/src/proxy/runtimeMetrics.ts +0 -149
- package/src/proxy/runtimeShutdown.ts +0 -157
- package/src/proxy/schemas.ts +0 -112
- package/src/proxy/sessionArchive.ts +0 -479
- package/src/proxy/sessionInfo.ts +0 -266
- package/src/proxy/sessionProcess.ts +0 -194
- package/src/proxy/sessionRuntime.ts +0 -121
- package/src/proxy/sessionSupervisor.ts +0 -305
- package/src/proxy/sessionWorkerEntry.ts +0 -26
- package/src/proxy/shutdownCoordinator.ts +0 -90
- package/src/proxy/socketTracker.ts +0 -639
- package/src/proxy/sqliteLogIndex.ts +0 -733
- package/src/proxy/storageLifecycle.ts +0 -432
- package/src/proxy/store.ts +0 -1773
- package/src/proxy/streamTiming.ts +0 -31
- package/src/proxy/toolSchemaWarnings.ts +0 -529
- package/src/proxy/uiAssetServer.ts +0 -106
- package/src/proxy/upstream.ts +0 -127
- package/src/services/alerts.ts +0 -12
- package/src/services/groups.ts +0 -119
- package/src/services/piAgent.ts +0 -421
- package/src/services/piAgentRetrieval.ts +0 -1180
- package/src/services/result.ts +0 -17
- package/src/services/runs.ts +0 -76
- package/src/ui/bootstrap.ts +0 -167
- package/src/ui/index.html +0 -38
- package/src/ui/logsStreamClient.ts +0 -20
- package/src/ui/main.tsx +0 -13
- package/src/ui/public/runtime-config.js +0 -2
- package/src/ui/router.tsx +0 -13
- package/src/ui/routes/__root.tsx +0 -69
- package/src/ui/routes/index.tsx +0 -35
- package/src/ui/routes/session/$sessionId.tsx +0 -50
- package/src/ui/runtimeDiscovery.ts +0 -50
package/src/proxy/logSearch.ts
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import type { CapturedLog } from "./schemas";
|
|
2
|
-
import { listLogsPage } from "./store";
|
|
3
|
-
|
|
4
|
-
export const LOG_SEARCH_DEFAULT_LIMIT = 10;
|
|
5
|
-
export const LOG_SEARCH_MAX_LIMIT = 50;
|
|
6
|
-
export const LOG_SEARCH_DEFAULT_SCAN_LIMIT = 200;
|
|
7
|
-
export const LOG_SEARCH_MAX_SCAN_LIMIT = 1_000;
|
|
8
|
-
|
|
9
|
-
export type LogSearchOptions = {
|
|
10
|
-
query: string;
|
|
11
|
-
sessionId?: string;
|
|
12
|
-
model?: string;
|
|
13
|
-
clientPid?: number;
|
|
14
|
-
offset: number;
|
|
15
|
-
limit: number;
|
|
16
|
-
scanLimit: number;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export type LogSearchResult = {
|
|
20
|
-
query: string;
|
|
21
|
-
logs: CapturedLog[];
|
|
22
|
-
total: number;
|
|
23
|
-
offset: number;
|
|
24
|
-
limit: number;
|
|
25
|
-
scanned: number;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
function clampPositiveInt(value: number, fallback: number, max: number): number {
|
|
29
|
-
if (!Number.isInteger(value) || value < 1) return fallback;
|
|
30
|
-
return Math.min(value, max);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function clampLogSearchLimit(value: number): number {
|
|
34
|
-
return clampPositiveInt(value, LOG_SEARCH_DEFAULT_LIMIT, LOG_SEARCH_MAX_LIMIT);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function clampLogSearchScanLimit(value: number): number {
|
|
38
|
-
return clampPositiveInt(value, LOG_SEARCH_DEFAULT_SCAN_LIMIT, LOG_SEARCH_MAX_SCAN_LIMIT);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function queryTerms(query: string): string[] {
|
|
42
|
-
return query
|
|
43
|
-
.trim()
|
|
44
|
-
.toLowerCase()
|
|
45
|
-
.split(/\s+/)
|
|
46
|
-
.filter((term) => term.length > 0);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function optionalText(value: string | number | boolean | null | undefined): string | null {
|
|
50
|
-
if (value === null || value === undefined) return null;
|
|
51
|
-
return String(value);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function searchableFields(log: CapturedLog): string[] {
|
|
55
|
-
return [
|
|
56
|
-
String(log.id),
|
|
57
|
-
log.timestamp,
|
|
58
|
-
log.method,
|
|
59
|
-
log.path,
|
|
60
|
-
optionalText(log.model),
|
|
61
|
-
optionalText(log.sessionId),
|
|
62
|
-
optionalText(log.responseStatus),
|
|
63
|
-
optionalText(log.elapsedMs),
|
|
64
|
-
optionalText(log.streaming),
|
|
65
|
-
log.streaming ? "streaming" : "non-streaming",
|
|
66
|
-
log.apiFormat,
|
|
67
|
-
optionalText(log.providerName),
|
|
68
|
-
optionalText(log.userAgent),
|
|
69
|
-
optionalText(log.origin),
|
|
70
|
-
optionalText(log.clientPid),
|
|
71
|
-
optionalText(log.clientCwd),
|
|
72
|
-
optionalText(log.clientProjectFolder),
|
|
73
|
-
optionalText(log.error),
|
|
74
|
-
...(log.warnings ?? []),
|
|
75
|
-
].filter((value): value is string => value !== null && value.length > 0);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function matchesTerms(log: CapturedLog, terms: readonly string[]): boolean {
|
|
79
|
-
if (terms.length === 0) return true;
|
|
80
|
-
const haystack = searchableFields(log).join("\n").toLowerCase();
|
|
81
|
-
return terms.every((term) => haystack.includes(term));
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function sortNewestFirst(logs: CapturedLog[]): CapturedLog[] {
|
|
85
|
-
return logs.sort((left, right) => {
|
|
86
|
-
const timestampCompare = right.timestamp.localeCompare(left.timestamp);
|
|
87
|
-
return timestampCompare !== 0 ? timestampCompare : right.id - left.id;
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export async function searchLogsPage(options: LogSearchOptions): Promise<LogSearchResult> {
|
|
92
|
-
const limit = clampLogSearchLimit(options.limit);
|
|
93
|
-
const scanLimit = clampLogSearchScanLimit(Math.max(options.scanLimit, options.offset + limit));
|
|
94
|
-
const firstPage = await listLogsPage({
|
|
95
|
-
sessionId: options.sessionId,
|
|
96
|
-
model: options.model,
|
|
97
|
-
clientPid: options.clientPid,
|
|
98
|
-
offset: 0,
|
|
99
|
-
limit: 1,
|
|
100
|
-
includeBodies: false,
|
|
101
|
-
});
|
|
102
|
-
const scanOffset = Math.max(firstPage.total - scanLimit, 0);
|
|
103
|
-
const scanPage = await listLogsPage({
|
|
104
|
-
sessionId: options.sessionId,
|
|
105
|
-
model: options.model,
|
|
106
|
-
clientPid: options.clientPid,
|
|
107
|
-
offset: scanOffset,
|
|
108
|
-
limit: scanLimit,
|
|
109
|
-
includeBodies: false,
|
|
110
|
-
});
|
|
111
|
-
const terms = queryTerms(options.query);
|
|
112
|
-
const matches = sortNewestFirst(scanPage.logs.filter((log) => matchesTerms(log, terms)));
|
|
113
|
-
|
|
114
|
-
return {
|
|
115
|
-
query: options.query,
|
|
116
|
-
logs: matches.slice(options.offset, options.offset + limit),
|
|
117
|
-
total: matches.length,
|
|
118
|
-
offset: options.offset,
|
|
119
|
-
limit,
|
|
120
|
-
scanned: scanPage.logs.length,
|
|
121
|
-
};
|
|
122
|
-
}
|
package/src/proxy/logger.ts
DELETED
|
@@ -1,537 +0,0 @@
|
|
|
1
|
-
import { Buffer } from "node:buffer";
|
|
2
|
-
import { closeSync, constants, fsyncSync, openSync, writeSync } from "node:fs";
|
|
3
|
-
import { appendFile, readdir, rename, stat, unlink } from "node:fs/promises";
|
|
4
|
-
import path from "node:path";
|
|
5
|
-
import { getDataDir } from "./dataDir";
|
|
6
|
-
import {
|
|
7
|
-
ensurePrivateDirectorySync,
|
|
8
|
-
PRIVATE_FILE_MODE,
|
|
9
|
-
securePrivateFileSync,
|
|
10
|
-
securePrivateTreeSync,
|
|
11
|
-
} from "./privateDataPath";
|
|
12
|
-
import {
|
|
13
|
-
canAppendCapturedLogFileOnExit,
|
|
14
|
-
markCapturedLogFileAppended,
|
|
15
|
-
recoverCapturedLogFileBeforeAppend,
|
|
16
|
-
recoverCapturedLogStorage,
|
|
17
|
-
} from "./jsonlRecovery";
|
|
18
|
-
|
|
19
|
-
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
20
|
-
const DEFAULT_RETENTION_DAYS = 7;
|
|
21
|
-
const DEFAULT_APP_LOG_MAX_BYTES = 10 * 1024 * 1024;
|
|
22
|
-
const DEFAULT_APP_LOG_MAX_FILES = 5;
|
|
23
|
-
|
|
24
|
-
type ApplicationLogPolicy = {
|
|
25
|
-
maxBytes: number;
|
|
26
|
-
maxFiles: number;
|
|
27
|
-
retentionMs: number;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
type ApplicationLogFile = {
|
|
31
|
-
generation: number;
|
|
32
|
-
modifiedAt: number;
|
|
33
|
-
path: string;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
function writeStderrFallback(message: string, error: unknown): void {
|
|
37
|
-
process.stderr.write(`${message} ${String(error)}\n`);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function resolveLogDir(): string {
|
|
41
|
-
const logDirEnv = process.env["LOG_DIR"];
|
|
42
|
-
if (logDirEnv !== undefined && logDirEnv !== "") {
|
|
43
|
-
return path.isAbsolute(logDirEnv) ? logDirEnv : path.join(getDataDir(), logDirEnv);
|
|
44
|
-
}
|
|
45
|
-
return path.join(getDataDir(), "logs");
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export function getLogFilePath(): string {
|
|
49
|
-
const date = new Date();
|
|
50
|
-
const yyyy = date.getUTCFullYear();
|
|
51
|
-
const mm = String(date.getUTCMonth() + 1).padStart(2, "0");
|
|
52
|
-
const dd = String(date.getUTCDate()).padStart(2, "0");
|
|
53
|
-
return path.join(resolveLogDir(), `${yyyy}-${mm}-${dd}.jsonl`);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function getInspectorLogPath(): string {
|
|
57
|
-
const logFileEnv = process.env["AGENT_INSPECTOR_LOG_FILE"];
|
|
58
|
-
if (logFileEnv !== undefined && logFileEnv !== "") {
|
|
59
|
-
return logFileEnv;
|
|
60
|
-
}
|
|
61
|
-
return path.join(getDataDir(), "logs", "inspector.log");
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function resolveApplicationLogPath(): string {
|
|
65
|
-
return getInspectorLogPath();
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function readBoundedInteger(
|
|
69
|
-
name: string,
|
|
70
|
-
fallback: number,
|
|
71
|
-
minimum: number,
|
|
72
|
-
maximum: number,
|
|
73
|
-
): number {
|
|
74
|
-
const raw = process.env[name];
|
|
75
|
-
if (raw === undefined || raw === "") return fallback;
|
|
76
|
-
const parsed = Number(raw);
|
|
77
|
-
if (!Number.isInteger(parsed) || parsed < minimum || parsed > maximum) return fallback;
|
|
78
|
-
return parsed;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function getApplicationLogPolicy(): ApplicationLogPolicy {
|
|
82
|
-
const retentionDays = readBoundedInteger("LOG_RETENTION_DAYS", DEFAULT_RETENTION_DAYS, 1, 3650);
|
|
83
|
-
return {
|
|
84
|
-
maxBytes: readBoundedInteger(
|
|
85
|
-
"AGENT_INSPECTOR_APP_LOG_MAX_BYTES",
|
|
86
|
-
DEFAULT_APP_LOG_MAX_BYTES,
|
|
87
|
-
1,
|
|
88
|
-
1024 * 1024 * 1024,
|
|
89
|
-
),
|
|
90
|
-
maxFiles: readBoundedInteger(
|
|
91
|
-
"AGENT_INSPECTOR_APP_LOG_MAX_FILES",
|
|
92
|
-
DEFAULT_APP_LOG_MAX_FILES,
|
|
93
|
-
1,
|
|
94
|
-
100,
|
|
95
|
-
),
|
|
96
|
-
retentionMs: retentionDays * DAY_MS,
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function getRotatedGeneration(entry: string, baseName: string): number | null {
|
|
101
|
-
const prefix = `${baseName}.`;
|
|
102
|
-
if (!entry.startsWith(prefix)) return null;
|
|
103
|
-
const suffix = entry.slice(prefix.length);
|
|
104
|
-
if (!/^\d+$/.test(suffix)) return null;
|
|
105
|
-
const generation = Number(suffix);
|
|
106
|
-
if (!Number.isSafeInteger(generation) || generation < 1) return null;
|
|
107
|
-
return generation;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
async function listApplicationLogFiles(logPath: string): Promise<ApplicationLogFile[]> {
|
|
111
|
-
const directory = path.dirname(logPath);
|
|
112
|
-
const baseName = path.basename(logPath);
|
|
113
|
-
const entries = await readdir(directory);
|
|
114
|
-
const files: ApplicationLogFile[] = [];
|
|
115
|
-
|
|
116
|
-
for (const entry of entries) {
|
|
117
|
-
const generation = entry === baseName ? 0 : getRotatedGeneration(entry, baseName);
|
|
118
|
-
if (generation === null) continue;
|
|
119
|
-
const filePath = path.join(directory, entry);
|
|
120
|
-
try {
|
|
121
|
-
const fileStat = await stat(filePath);
|
|
122
|
-
if (!fileStat.isFile()) continue;
|
|
123
|
-
files.push({ generation, modifiedAt: fileStat.mtimeMs, path: filePath });
|
|
124
|
-
} catch {
|
|
125
|
-
// The file may have been removed by another maintenance pass.
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
return files;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
async function removeApplicationLog(filePath: string): Promise<boolean> {
|
|
133
|
-
try {
|
|
134
|
-
await stat(filePath);
|
|
135
|
-
} catch {
|
|
136
|
-
return true;
|
|
137
|
-
}
|
|
138
|
-
try {
|
|
139
|
-
await unlink(filePath);
|
|
140
|
-
return true;
|
|
141
|
-
} catch {
|
|
142
|
-
// Retention is best-effort; a later maintenance pass retries failed removals.
|
|
143
|
-
return false;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
async function maintainApplicationLogs(
|
|
148
|
-
logPath: string,
|
|
149
|
-
policy: ApplicationLogPolicy,
|
|
150
|
-
): Promise<boolean> {
|
|
151
|
-
const directory = path.dirname(logPath);
|
|
152
|
-
if (!ensurePrivateDirectorySync(directory)) return false;
|
|
153
|
-
|
|
154
|
-
let files: ApplicationLogFile[];
|
|
155
|
-
try {
|
|
156
|
-
files = await listApplicationLogFiles(logPath);
|
|
157
|
-
} catch {
|
|
158
|
-
return false;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
for (const file of files) {
|
|
162
|
-
if (!securePrivateFileSync(file.path)) return false;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
const cutoff = Date.now() - policy.retentionMs;
|
|
166
|
-
const retained = files.filter((file) => file.modifiedAt >= cutoff);
|
|
167
|
-
let maintained = true;
|
|
168
|
-
for (const file of files) {
|
|
169
|
-
if (file.modifiedAt < cutoff && !(await removeApplicationLog(file.path))) maintained = false;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
const rotated = retained
|
|
173
|
-
.filter((file) => file.generation > 0)
|
|
174
|
-
.sort((left, right) => right.modifiedAt - left.modifiedAt);
|
|
175
|
-
const retainedRotatedCount = Math.max(0, policy.maxFiles - 1);
|
|
176
|
-
for (const file of rotated.slice(retainedRotatedCount)) {
|
|
177
|
-
if (!(await removeApplicationLog(file.path))) maintained = false;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
return maintained;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
async function moveApplicationLog(source: string, destination: string): Promise<boolean> {
|
|
184
|
-
try {
|
|
185
|
-
await stat(source);
|
|
186
|
-
} catch {
|
|
187
|
-
return true;
|
|
188
|
-
}
|
|
189
|
-
try {
|
|
190
|
-
await rename(source, destination);
|
|
191
|
-
if (!securePrivateFileSync(destination)) {
|
|
192
|
-
writeStderrFallback("[logger] Failed to secure rotated application log:", destination);
|
|
193
|
-
return false;
|
|
194
|
-
}
|
|
195
|
-
return true;
|
|
196
|
-
} catch (error) {
|
|
197
|
-
writeStderrFallback("[logger] Failed to rotate application log:", error);
|
|
198
|
-
return false;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
async function rotateApplicationLog(logPath: string, maxFiles: number): Promise<boolean> {
|
|
203
|
-
if (maxFiles === 1) {
|
|
204
|
-
return await removeApplicationLog(logPath);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
if (!(await removeApplicationLog(`${logPath}.${String(maxFiles - 1)}`))) return false;
|
|
208
|
-
for (let generation = maxFiles - 2; generation >= 1; generation -= 1) {
|
|
209
|
-
if (
|
|
210
|
-
!(await moveApplicationLog(
|
|
211
|
-
`${logPath}.${String(generation)}`,
|
|
212
|
-
`${logPath}.${String(generation + 1)}`,
|
|
213
|
-
))
|
|
214
|
-
) {
|
|
215
|
-
return false;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
return await moveApplicationLog(logPath, `${logPath}.1`);
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
async function rotateApplicationLogIfNeeded(
|
|
222
|
-
logPath: string,
|
|
223
|
-
incomingBytes: number,
|
|
224
|
-
policy: ApplicationLogPolicy,
|
|
225
|
-
): Promise<boolean> {
|
|
226
|
-
try {
|
|
227
|
-
const fileStat = await stat(logPath);
|
|
228
|
-
if (fileStat.size > 0 && fileStat.size + incomingBytes > policy.maxBytes) {
|
|
229
|
-
return await rotateApplicationLog(logPath, policy.maxFiles);
|
|
230
|
-
}
|
|
231
|
-
return true;
|
|
232
|
-
} catch {
|
|
233
|
-
// The active file does not exist yet.
|
|
234
|
-
return true;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
export async function initLogger(): Promise<void> {
|
|
239
|
-
const dir = resolveLogDir();
|
|
240
|
-
const policy = getApplicationLogPolicy();
|
|
241
|
-
const cutoff = Date.now() - policy.retentionMs;
|
|
242
|
-
|
|
243
|
-
try {
|
|
244
|
-
if (!ensurePrivateDirectorySync(dir)) {
|
|
245
|
-
writeStderrFallback("[logger] Failed to secure log directory:", dir);
|
|
246
|
-
} else if (!securePrivateTreeSync(dir)) {
|
|
247
|
-
writeStderrFallback("[logger] Failed to secure existing log files:", dir);
|
|
248
|
-
} else {
|
|
249
|
-
const recovery = await recoverCapturedLogStorage(dir);
|
|
250
|
-
if (recovery.level === "unavailable") {
|
|
251
|
-
writeStderrFallback(
|
|
252
|
-
"[logger] Captured-log recovery blocked readiness:",
|
|
253
|
-
recovery.diagnostic ?? "unknown recovery failure",
|
|
254
|
-
);
|
|
255
|
-
} else {
|
|
256
|
-
const entries = await readdir(dir);
|
|
257
|
-
for (const entry of entries) {
|
|
258
|
-
if (!entry.endsWith(".jsonl")) continue;
|
|
259
|
-
const fullPath = path.join(dir, entry);
|
|
260
|
-
try {
|
|
261
|
-
const s = await stat(fullPath);
|
|
262
|
-
if (s.mtimeMs < cutoff) {
|
|
263
|
-
await unlink(fullPath);
|
|
264
|
-
}
|
|
265
|
-
} catch {
|
|
266
|
-
// Skip files that can't be stat - not critical
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
} catch (err) {
|
|
272
|
-
// Log directory initialization errors but don't fail startup
|
|
273
|
-
writeStderrFallback("[logger] Failed to initialize log directory:", err);
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
const applicationLogPath = getInspectorLogPath();
|
|
277
|
-
if (!(await maintainApplicationLogs(applicationLogPath, policy))) {
|
|
278
|
-
writeStderrFallback(
|
|
279
|
-
"[logger] Failed to initialize application log lifecycle:",
|
|
280
|
-
applicationLogPath,
|
|
281
|
-
);
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
// File-based logger for application logs (not log entries)
|
|
286
|
-
let loggerInitialization: Promise<void> | null = null;
|
|
287
|
-
let applicationLogWriteQueue: Promise<void> = Promise.resolve();
|
|
288
|
-
let lastApplicationLogWriteFailureKey: string | null = null;
|
|
289
|
-
let lastApplicationLogWriteFailureAt = 0;
|
|
290
|
-
const APPLICATION_LOG_WRITE_FAILURE_SUPPRESS_MS = 5_000;
|
|
291
|
-
|
|
292
|
-
type ApplicationLogWriteResult = { ok: true } | { ok: false; reason: unknown };
|
|
293
|
-
|
|
294
|
-
function ensureLoggerInitialized(): Promise<void> {
|
|
295
|
-
if (loggerInitialization === null) loggerInitialization = initLogger();
|
|
296
|
-
return loggerInitialization;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
async function appendApplicationLogLine(
|
|
300
|
-
logPath: string,
|
|
301
|
-
line: string,
|
|
302
|
-
policy: ApplicationLogPolicy,
|
|
303
|
-
): Promise<ApplicationLogWriteResult> {
|
|
304
|
-
const logDirPath = path.dirname(logPath);
|
|
305
|
-
if (!ensurePrivateDirectorySync(logDirPath)) {
|
|
306
|
-
return { ok: false, reason: "application log directory is not private" };
|
|
307
|
-
}
|
|
308
|
-
try {
|
|
309
|
-
if (!(await rotateApplicationLogIfNeeded(logPath, Buffer.byteLength(line, "utf-8"), policy))) {
|
|
310
|
-
return { ok: false, reason: "application log rotation failed" };
|
|
311
|
-
}
|
|
312
|
-
await appendFile(logPath, line, { encoding: "utf-8", mode: PRIVATE_FILE_MODE });
|
|
313
|
-
if (!securePrivateFileSync(logPath)) {
|
|
314
|
-
return { ok: false, reason: "application log permissions could not be secured" };
|
|
315
|
-
}
|
|
316
|
-
return { ok: true };
|
|
317
|
-
} catch (error) {
|
|
318
|
-
return { ok: false, reason: error };
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
/** Isolated lifecycle hook used by the rotation tests without mutating process-wide environment. */
|
|
323
|
-
export async function appendApplicationLogLineForTests(
|
|
324
|
-
logPath: string,
|
|
325
|
-
line: string,
|
|
326
|
-
maxBytes: number,
|
|
327
|
-
maxFiles: number,
|
|
328
|
-
): Promise<boolean> {
|
|
329
|
-
const result = await appendApplicationLogLine(logPath, line, {
|
|
330
|
-
maxBytes,
|
|
331
|
-
maxFiles,
|
|
332
|
-
retentionMs: DEFAULT_RETENTION_DAYS * DAY_MS,
|
|
333
|
-
});
|
|
334
|
-
return result.ok;
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
async function writeAppLog(message: string): Promise<void> {
|
|
338
|
-
const logPath = getInspectorLogPath();
|
|
339
|
-
const line = `[${new Date().toISOString()}] ${message}\n`;
|
|
340
|
-
const initialization = ensureLoggerInitialized();
|
|
341
|
-
const write = async (): Promise<void> => {
|
|
342
|
-
await initialization;
|
|
343
|
-
const result = await appendApplicationLogLine(logPath, line, getApplicationLogPolicy());
|
|
344
|
-
if (!result.ok) {
|
|
345
|
-
const key = `${logPath}:${String(result.reason)}`;
|
|
346
|
-
const now = Date.now();
|
|
347
|
-
if (
|
|
348
|
-
key !== lastApplicationLogWriteFailureKey ||
|
|
349
|
-
now - lastApplicationLogWriteFailureAt >= APPLICATION_LOG_WRITE_FAILURE_SUPPRESS_MS
|
|
350
|
-
) {
|
|
351
|
-
lastApplicationLogWriteFailureKey = key;
|
|
352
|
-
lastApplicationLogWriteFailureAt = now;
|
|
353
|
-
writeStderrFallback(`[logger] Failed to write to ${logPath}:`, result.reason);
|
|
354
|
-
}
|
|
355
|
-
return;
|
|
356
|
-
}
|
|
357
|
-
if (lastApplicationLogWriteFailureKey !== null) {
|
|
358
|
-
writeStderrFallback(`[logger] Application log writes recovered for ${logPath}:`, "ok");
|
|
359
|
-
lastApplicationLogWriteFailureKey = null;
|
|
360
|
-
lastApplicationLogWriteFailureAt = 0;
|
|
361
|
-
}
|
|
362
|
-
};
|
|
363
|
-
|
|
364
|
-
applicationLogWriteQueue = applicationLogWriteQueue.then(write, write);
|
|
365
|
-
await applicationLogWriteQueue;
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
/** Wait for all queued application-log writes and rotations. */
|
|
369
|
-
export async function flushApplicationLog(): Promise<void> {
|
|
370
|
-
await applicationLogWriteQueue;
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
// Application logger - writes to inspector.log
|
|
374
|
-
export const logger = {
|
|
375
|
-
debug(message: string, ...args: unknown[]): void {
|
|
376
|
-
const msg = args.length > 0 ? `${message} ${args.map(String).join(" ")}` : message;
|
|
377
|
-
void writeAppLog(`[DEBUG] ${msg}`);
|
|
378
|
-
},
|
|
379
|
-
info(message: string, ...args: unknown[]): void {
|
|
380
|
-
const msg = args.length > 0 ? `${message} ${args.map(String).join(" ")}` : message;
|
|
381
|
-
void writeAppLog(`[INFO] ${msg}`);
|
|
382
|
-
},
|
|
383
|
-
warn(message: string, ...args: unknown[]): void {
|
|
384
|
-
const msg = args.length > 0 ? `${message} ${args.map(String).join(" ")}` : message;
|
|
385
|
-
void writeAppLog(`[WARN] ${msg}`);
|
|
386
|
-
},
|
|
387
|
-
error(message: string, ...args: unknown[]): void {
|
|
388
|
-
const msg = args.length > 0 ? `${message} ${args.map(String).join(" ")}` : message;
|
|
389
|
-
void writeAppLog(`[ERROR] ${msg}`);
|
|
390
|
-
},
|
|
391
|
-
};
|
|
392
|
-
|
|
393
|
-
// Write buffer for batching async writes
|
|
394
|
-
const MAX_BUFFER_SIZE = 1000;
|
|
395
|
-
const BUFFER_FLUSH_DELAY_MS = 10;
|
|
396
|
-
let writeBuffer: string[] = [];
|
|
397
|
-
let writeQueue: Promise<void> = Promise.resolve();
|
|
398
|
-
let logWriteLock: Promise<void> = Promise.resolve();
|
|
399
|
-
let scheduledBufferFlush: ReturnType<typeof setTimeout> | null = null;
|
|
400
|
-
|
|
401
|
-
function queueBufferedFlush(): void {
|
|
402
|
-
writeQueue = writeQueue.then(() => runWithLogWriteLock(flushWriteBufferUnlocked));
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
function cancelScheduledBufferFlush(): void {
|
|
406
|
-
if (scheduledBufferFlush === null) return;
|
|
407
|
-
clearTimeout(scheduledBufferFlush);
|
|
408
|
-
scheduledBufferFlush = null;
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
function scheduleBufferedFlush(): void {
|
|
412
|
-
if (scheduledBufferFlush !== null) return;
|
|
413
|
-
scheduledBufferFlush = setTimeout(() => {
|
|
414
|
-
scheduledBufferFlush = null;
|
|
415
|
-
queueBufferedFlush();
|
|
416
|
-
}, BUFFER_FLUSH_DELAY_MS);
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
export async function runWithLogWriteLock<T>(fn: () => Promise<T>): Promise<T> {
|
|
420
|
-
const previousLock = logWriteLock;
|
|
421
|
-
let releaseLock = (): void => {};
|
|
422
|
-
logWriteLock = new Promise<void>((resolve) => {
|
|
423
|
-
releaseLock = resolve;
|
|
424
|
-
});
|
|
425
|
-
|
|
426
|
-
await previousLock;
|
|
427
|
-
try {
|
|
428
|
-
return await fn();
|
|
429
|
-
} finally {
|
|
430
|
-
releaseLock();
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
function drainBuffer(): string {
|
|
435
|
-
const toWrite = writeBuffer.join("");
|
|
436
|
-
writeBuffer = [];
|
|
437
|
-
return toWrite;
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
async function flushWriteBufferUnlocked(): Promise<void> {
|
|
441
|
-
let toWrite: string;
|
|
442
|
-
{
|
|
443
|
-
toWrite = drainBuffer();
|
|
444
|
-
}
|
|
445
|
-
if (toWrite.length === 0) return;
|
|
446
|
-
|
|
447
|
-
try {
|
|
448
|
-
const filePath = getLogFilePath();
|
|
449
|
-
if (!ensurePrivateDirectorySync(path.dirname(filePath))) {
|
|
450
|
-
writeBuffer.unshift(toWrite);
|
|
451
|
-
writeStderrFallback("[logger] Failed to secure captured-log directory:", filePath);
|
|
452
|
-
return;
|
|
453
|
-
}
|
|
454
|
-
const recovery = await recoverCapturedLogFileBeforeAppend(resolveLogDir(), filePath);
|
|
455
|
-
if (recovery.level === "unavailable") {
|
|
456
|
-
writeBuffer.unshift(toWrite);
|
|
457
|
-
writeStderrFallback(
|
|
458
|
-
"[logger] Refused captured-log append until recovery succeeds:",
|
|
459
|
-
recovery.diagnostic ?? "unknown recovery failure",
|
|
460
|
-
);
|
|
461
|
-
return;
|
|
462
|
-
}
|
|
463
|
-
await appendFile(filePath, toWrite, { encoding: "utf-8", mode: PRIVATE_FILE_MODE });
|
|
464
|
-
if (!securePrivateFileSync(filePath)) {
|
|
465
|
-
writeStderrFallback("[logger] Failed to secure captured-log file:", filePath);
|
|
466
|
-
}
|
|
467
|
-
await markCapturedLogFileAppended(resolveLogDir(), filePath);
|
|
468
|
-
} catch (err) {
|
|
469
|
-
// On failure, re-queue the data so it is not lost
|
|
470
|
-
writeBuffer.unshift(toWrite);
|
|
471
|
-
writeStderrFallback("[logger] Failed to flush write buffer, re-queued:", err);
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
export function appendLogEntry(entry: Record<string, unknown>): void {
|
|
476
|
-
const line = JSON.stringify(entry) + "\n";
|
|
477
|
-
writeBuffer.push(line);
|
|
478
|
-
if (writeBuffer.length >= MAX_BUFFER_SIZE) {
|
|
479
|
-
cancelScheduledBufferFlush();
|
|
480
|
-
queueBufferedFlush();
|
|
481
|
-
} else if (writeBuffer.length === 1) {
|
|
482
|
-
scheduleBufferedFlush();
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
/**
|
|
487
|
-
* Flush all buffered log entries to disk. Call before process exit
|
|
488
|
-
* to avoid losing unflushed data.
|
|
489
|
-
*/
|
|
490
|
-
export async function flushLogBuffer(): Promise<void> {
|
|
491
|
-
cancelScheduledBufferFlush();
|
|
492
|
-
await writeQueue;
|
|
493
|
-
if (writeBuffer.length > 0) {
|
|
494
|
-
await runWithLogWriteLock(flushWriteBufferUnlocked);
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
export async function runWithFlushedLogWriteLock<T>(fn: () => Promise<T>): Promise<T> {
|
|
499
|
-
await flushLogBuffer();
|
|
500
|
-
return await runWithLogWriteLock(async () => {
|
|
501
|
-
await flushWriteBufferUnlocked();
|
|
502
|
-
return await fn();
|
|
503
|
-
});
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
// Ensure buffered logs are written before the process exits
|
|
507
|
-
process.on("exit", () => {
|
|
508
|
-
if (writeBuffer.length > 0) {
|
|
509
|
-
const toWrite = drainBuffer();
|
|
510
|
-
try {
|
|
511
|
-
// Synchronous write required in 'exit' handler (async I/O not allowed)
|
|
512
|
-
const filePath = getLogFilePath();
|
|
513
|
-
if (!ensurePrivateDirectorySync(path.dirname(filePath))) return;
|
|
514
|
-
if (!canAppendCapturedLogFileOnExit(resolveLogDir(), filePath)) return;
|
|
515
|
-
const descriptor = openSync(
|
|
516
|
-
filePath,
|
|
517
|
-
constants.O_WRONLY | constants.O_APPEND | constants.O_NOFOLLOW,
|
|
518
|
-
PRIVATE_FILE_MODE,
|
|
519
|
-
);
|
|
520
|
-
try {
|
|
521
|
-
const payload = Buffer.from(toWrite, "utf-8");
|
|
522
|
-
let written = 0;
|
|
523
|
-
while (written < payload.length) {
|
|
524
|
-
const writeCount = writeSync(descriptor, payload, written, payload.length - written);
|
|
525
|
-
if (writeCount === 0) return;
|
|
526
|
-
written += writeCount;
|
|
527
|
-
}
|
|
528
|
-
fsyncSync(descriptor);
|
|
529
|
-
} finally {
|
|
530
|
-
closeSync(descriptor);
|
|
531
|
-
}
|
|
532
|
-
void securePrivateFileSync(filePath);
|
|
533
|
-
} catch {
|
|
534
|
-
// Best-effort: nothing we can do in an exit handler
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
});
|