@draht/coding-agent 2026.7.13 → 2026.9.5-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +241 -0
- package/README.md +47 -2
- package/agents/debugger.md +12 -1
- package/agents/git-committer.md +1 -0
- package/agents/implementer.md +20 -0
- package/agents/investigator.md +75 -0
- package/agents/reviewer.md +7 -0
- package/agents/spec-reviewer.md +66 -0
- package/agents/verifier.md +4 -3
- package/bin/draht-tools.cjs +472 -41
- package/dist/agents/debugger.md +12 -1
- package/dist/agents/git-committer.md +1 -0
- package/dist/agents/implementer.md +20 -0
- package/dist/agents/investigator.md +75 -0
- package/dist/agents/reviewer.md +7 -0
- package/dist/agents/spec-reviewer.md +66 -0
- package/dist/agents/verifier.md +4 -3
- package/dist/bun/cli.js +2 -0
- package/dist/bun/cli.js.map +1 -1
- package/dist/cli/args.d.ts +10 -0
- package/dist/cli/args.d.ts.map +1 -1
- package/dist/cli/args.js +80 -3
- package/dist/cli/args.js.map +1 -1
- package/dist/cli/attach-mode.d.ts +61 -0
- package/dist/cli/attach-mode.d.ts.map +1 -1
- package/dist/cli/attach-mode.js +243 -5
- package/dist/cli/attach-mode.js.map +1 -1
- package/dist/cli/config-selector.js +1 -1
- package/dist/cli/config-selector.js.map +1 -1
- package/dist/cli/credential-print.d.ts +23 -0
- package/dist/cli/credential-print.d.ts.map +1 -0
- package/dist/cli/credential-print.js +117 -0
- package/dist/cli/credential-print.js.map +1 -0
- package/dist/cli/list-models.d.ts +2 -2
- package/dist/cli/list-models.d.ts.map +1 -1
- package/dist/cli/list-models.js +3 -3
- package/dist/cli/list-models.js.map +1 -1
- package/dist/cli/startup-ui.js +1 -1
- package/dist/cli/startup-ui.js.map +1 -1
- package/dist/config.d.ts +9 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +31 -4
- package/dist/config.js.map +1 -1
- package/dist/core/agent-session-runtime.d.ts +17 -1
- package/dist/core/agent-session-runtime.d.ts.map +1 -1
- package/dist/core/agent-session-runtime.js +90 -30
- package/dist/core/agent-session-runtime.js.map +1 -1
- package/dist/core/agent-session-services.d.ts +4 -7
- package/dist/core/agent-session-services.d.ts.map +1 -1
- package/dist/core/agent-session-services.js +24 -9
- package/dist/core/agent-session-services.js.map +1 -1
- package/dist/core/agent-session.d.ts +87 -27
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +337 -154
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/auth-storage.d.ts +20 -104
- package/dist/core/auth-storage.d.ts.map +1 -1
- package/dist/core/auth-storage.js +104 -260
- package/dist/core/auth-storage.js.map +1 -1
- package/dist/core/builtins/checkpoints.d.ts +20 -0
- package/dist/core/builtins/checkpoints.d.ts.map +1 -0
- package/dist/core/builtins/checkpoints.js +161 -0
- package/dist/core/builtins/checkpoints.js.map +1 -0
- package/dist/core/builtins/duet.d.ts +26 -0
- package/dist/core/builtins/duet.d.ts.map +1 -0
- package/dist/core/builtins/duet.js +660 -0
- package/dist/core/builtins/duet.js.map +1 -0
- package/dist/core/builtins/index.d.ts.map +1 -1
- package/dist/core/builtins/index.js +7 -1
- package/dist/core/builtins/index.js.map +1 -1
- package/dist/core/builtins/subagent.d.ts +24 -4
- package/dist/core/builtins/subagent.d.ts.map +1 -1
- package/dist/core/builtins/subagent.js +278 -35
- package/dist/core/builtins/subagent.js.map +1 -1
- package/dist/core/cache-stats.d.ts +2 -2
- package/dist/core/cache-stats.d.ts.map +1 -1
- package/dist/core/cache-stats.js +1 -1
- package/dist/core/cache-stats.js.map +1 -1
- package/dist/core/checkpoints/checkpoint-cli.d.ts +17 -0
- package/dist/core/checkpoints/checkpoint-cli.d.ts.map +1 -0
- package/dist/core/checkpoints/checkpoint-cli.js +78 -0
- package/dist/core/checkpoints/checkpoint-cli.js.map +1 -0
- package/dist/core/checkpoints/checkpoint-manager.d.ts +294 -0
- package/dist/core/checkpoints/checkpoint-manager.d.ts.map +1 -0
- package/dist/core/checkpoints/checkpoint-manager.js +865 -0
- package/dist/core/checkpoints/checkpoint-manager.js.map +1 -0
- package/dist/core/checkpoints/rewind.d.ts +141 -0
- package/dist/core/checkpoints/rewind.d.ts.map +1 -0
- package/dist/core/checkpoints/rewind.js +246 -0
- package/dist/core/checkpoints/rewind.js.map +1 -0
- package/dist/core/compaction/branch-summarization.d.ts +9 -3
- package/dist/core/compaction/branch-summarization.d.ts.map +1 -1
- package/dist/core/compaction/branch-summarization.js +16 -14
- package/dist/core/compaction/branch-summarization.js.map +1 -1
- package/dist/core/compaction/compaction.d.ts +24 -3
- package/dist/core/compaction/compaction.d.ts.map +1 -1
- package/dist/core/compaction/compaction.js +96 -30
- package/dist/core/compaction/compaction.js.map +1 -1
- package/dist/core/compaction/utils.d.ts +1 -1
- package/dist/core/compaction/utils.d.ts.map +1 -1
- package/dist/core/compaction/utils.js +6 -17
- package/dist/core/compaction/utils.js.map +1 -1
- package/dist/core/context-windows.d.ts +7 -0
- package/dist/core/context-windows.d.ts.map +1 -0
- package/dist/core/context-windows.js +35 -0
- package/dist/core/context-windows.js.map +1 -0
- package/dist/core/extensions/index.d.ts +1 -1
- package/dist/core/extensions/index.d.ts.map +1 -1
- package/dist/core/extensions/index.js.map +1 -1
- package/dist/core/extensions/loader.d.ts.map +1 -1
- package/dist/core/extensions/loader.js +101 -23
- package/dist/core/extensions/loader.js.map +1 -1
- package/dist/core/extensions/runner.d.ts +19 -3
- package/dist/core/extensions/runner.d.ts.map +1 -1
- package/dist/core/extensions/runner.js +117 -2
- package/dist/core/extensions/runner.js.map +1 -1
- package/dist/core/extensions/types.d.ts +236 -9
- package/dist/core/extensions/types.d.ts.map +1 -1
- package/dist/core/extensions/types.js.map +1 -1
- package/dist/core/extensions/wrapper.d.ts.map +1 -1
- package/dist/core/extensions/wrapper.js +22 -3
- package/dist/core/extensions/wrapper.js.map +1 -1
- package/dist/core/footer-data-provider.d.ts +10 -0
- package/dist/core/footer-data-provider.d.ts.map +1 -1
- package/dist/core/footer-data-provider.js +1 -1
- package/dist/core/footer-data-provider.js.map +1 -1
- package/dist/core/http-dispatcher.d.ts.map +1 -1
- package/dist/core/http-dispatcher.js +7 -0
- package/dist/core/http-dispatcher.js.map +1 -1
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js.map +1 -1
- package/dist/core/keybindings.d.ts +18 -3
- package/dist/core/keybindings.d.ts.map +1 -1
- package/dist/core/keybindings.js +12 -3
- package/dist/core/keybindings.js.map +1 -1
- package/dist/core/model-config.d.ts +542 -0
- package/dist/core/model-config.d.ts.map +1 -0
- package/dist/core/model-config.js +247 -0
- package/dist/core/model-config.js.map +1 -0
- package/dist/core/model-registry.d.ts +19 -124
- package/dist/core/model-registry.d.ts.map +1 -1
- package/dist/core/model-registry.js +56 -727
- package/dist/core/model-registry.js.map +1 -1
- package/dist/core/model-resolver.d.ts +7 -6
- package/dist/core/model-resolver.d.ts.map +1 -1
- package/dist/core/model-resolver.js +74 -27
- package/dist/core/model-resolver.js.map +1 -1
- package/dist/core/model-runtime.d.ts +89 -0
- package/dist/core/model-runtime.d.ts.map +1 -0
- package/dist/core/model-runtime.js +451 -0
- package/dist/core/model-runtime.js.map +1 -0
- package/dist/core/models-store.d.ts +17 -0
- package/dist/core/models-store.d.ts.map +1 -0
- package/dist/core/models-store.js +46 -0
- package/dist/core/models-store.js.map +1 -0
- package/dist/core/multi-agent/permission-gate.d.ts.map +1 -1
- package/dist/core/multi-agent/permission-gate.js +4 -3
- package/dist/core/multi-agent/permission-gate.js.map +1 -1
- package/dist/core/multi-agent/worktree.d.ts +2 -0
- package/dist/core/multi-agent/worktree.d.ts.map +1 -1
- package/dist/core/multi-agent/worktree.js +2 -2
- package/dist/core/multi-agent/worktree.js.map +1 -1
- package/dist/core/package-manager.d.ts +7 -1
- package/dist/core/package-manager.d.ts.map +1 -1
- package/dist/core/package-manager.js +41 -45
- package/dist/core/package-manager.js.map +1 -1
- package/dist/core/permission-relay/index.d.ts +19 -0
- package/dist/core/permission-relay/index.d.ts.map +1 -0
- package/dist/core/permission-relay/index.js +49 -0
- package/dist/core/permission-relay/index.js.map +1 -0
- package/dist/core/permission-relay/relay-ui-context.d.ts +39 -0
- package/dist/core/permission-relay/relay-ui-context.d.ts.map +1 -0
- package/dist/core/permission-relay/relay-ui-context.js +788 -0
- package/dist/core/permission-relay/relay-ui-context.js.map +1 -0
- package/dist/core/permission-relay/types.d.ts +222 -0
- package/dist/core/permission-relay/types.d.ts.map +1 -0
- package/dist/core/permission-relay/types.js +15 -0
- package/dist/core/permission-relay/types.js.map +1 -0
- package/dist/core/pi-manifest.d.ts +14 -0
- package/dist/core/pi-manifest.d.ts.map +1 -0
- package/dist/core/pi-manifest.js +35 -0
- package/dist/core/pi-manifest.js.map +1 -0
- package/dist/core/project-trust.js +2 -2
- package/dist/core/project-trust.js.map +1 -1
- package/dist/core/prompt-templates.d.ts +1 -0
- package/dist/core/prompt-templates.d.ts.map +1 -1
- package/dist/core/prompt-templates.js +4 -4
- package/dist/core/prompt-templates.js.map +1 -1
- package/dist/core/provider-composer.d.ts +55 -0
- package/dist/core/provider-composer.d.ts.map +1 -0
- package/dist/core/provider-composer.js +375 -0
- package/dist/core/provider-composer.js.map +1 -0
- package/dist/core/radius.d.ts +2 -0
- package/dist/core/radius.d.ts.map +1 -0
- package/dist/core/radius.js +2 -0
- package/dist/core/radius.js.map +1 -0
- package/dist/core/remote-catalog-provider.d.ts +5 -0
- package/dist/core/remote-catalog-provider.d.ts.map +1 -0
- package/dist/core/remote-catalog-provider.js +115 -0
- package/dist/core/remote-catalog-provider.js.map +1 -0
- package/dist/core/resource-loader.d.ts +36 -0
- package/dist/core/resource-loader.d.ts.map +1 -1
- package/dist/core/resource-loader.js +159 -27
- package/dist/core/resource-loader.js.map +1 -1
- package/dist/core/runtime-credentials.d.ts +15 -0
- package/dist/core/runtime-credentials.d.ts.map +1 -0
- package/dist/core/runtime-credentials.js +35 -0
- package/dist/core/runtime-credentials.js.map +1 -0
- package/dist/core/sandbox/bash-operations.d.ts +149 -0
- package/dist/core/sandbox/bash-operations.d.ts.map +1 -0
- package/dist/core/sandbox/bash-operations.js +163 -0
- package/dist/core/sandbox/bash-operations.js.map +1 -0
- package/dist/core/sandbox/env.d.ts +83 -0
- package/dist/core/sandbox/env.d.ts.map +1 -0
- package/dist/core/sandbox/env.js +123 -0
- package/dist/core/sandbox/env.js.map +1 -0
- package/dist/core/sandbox/executor.d.ts +104 -0
- package/dist/core/sandbox/executor.d.ts.map +1 -0
- package/dist/core/sandbox/executor.js +92 -0
- package/dist/core/sandbox/executor.js.map +1 -0
- package/dist/core/sandbox/index.d.ts +36 -0
- package/dist/core/sandbox/index.d.ts.map +1 -0
- package/dist/core/sandbox/index.js +36 -0
- package/dist/core/sandbox/index.js.map +1 -0
- package/dist/core/sandbox/linux.d.ts +121 -0
- package/dist/core/sandbox/linux.d.ts.map +1 -0
- package/dist/core/sandbox/linux.js +260 -0
- package/dist/core/sandbox/linux.js.map +1 -0
- package/dist/core/sandbox/policy.d.ts +142 -0
- package/dist/core/sandbox/policy.d.ts.map +1 -0
- package/dist/core/sandbox/policy.js +297 -0
- package/dist/core/sandbox/policy.js.map +1 -0
- package/dist/core/sandbox/real-path.d.ts +112 -0
- package/dist/core/sandbox/real-path.d.ts.map +1 -0
- package/dist/core/sandbox/real-path.js +158 -0
- package/dist/core/sandbox/real-path.js.map +1 -0
- package/dist/core/sandbox/seatbelt-profile.d.ts +95 -0
- package/dist/core/sandbox/seatbelt-profile.d.ts.map +1 -0
- package/dist/core/sandbox/seatbelt-profile.js +239 -0
- package/dist/core/sandbox/seatbelt-profile.js.map +1 -0
- package/dist/core/sandbox/seatbelt.d.ts +95 -0
- package/dist/core/sandbox/seatbelt.d.ts.map +1 -0
- package/dist/core/sandbox/seatbelt.js +154 -0
- package/dist/core/sandbox/seatbelt.js.map +1 -0
- package/dist/core/sandbox/self-test.d.ts +109 -0
- package/dist/core/sandbox/self-test.d.ts.map +1 -0
- package/dist/core/sandbox/self-test.js +369 -0
- package/dist/core/sandbox/self-test.js.map +1 -0
- package/dist/core/sandbox/spawn.d.ts +33 -0
- package/dist/core/sandbox/spawn.d.ts.map +1 -0
- package/dist/core/sandbox/spawn.js +87 -0
- package/dist/core/sandbox/spawn.js.map +1 -0
- package/dist/core/sdk.d.ts +3 -6
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/core/sdk.js +25 -22
- package/dist/core/sdk.js.map +1 -1
- package/dist/core/session-manager.d.ts +74 -7
- package/dist/core/session-manager.d.ts.map +1 -1
- package/dist/core/session-manager.js +169 -34
- package/dist/core/session-manager.js.map +1 -1
- package/dist/core/settings-manager.d.ts +22 -1
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +36 -16
- package/dist/core/settings-manager.js.map +1 -1
- package/dist/core/skills.d.ts +3 -1
- package/dist/core/skills.d.ts.map +1 -1
- package/dist/core/skills.js +19 -6
- package/dist/core/skills.js.map +1 -1
- package/dist/core/slash-commands.d.ts.map +1 -1
- package/dist/core/slash-commands.js +1 -0
- package/dist/core/slash-commands.js.map +1 -1
- package/dist/core/soak/soak-log.d.ts +298 -0
- package/dist/core/soak/soak-log.d.ts.map +1 -0
- package/dist/core/soak/soak-log.js +660 -0
- package/dist/core/soak/soak-log.js.map +1 -0
- package/dist/core/soak/soak-reader.d.ts +60 -0
- package/dist/core/soak/soak-reader.d.ts.map +1 -0
- package/dist/core/soak/soak-reader.js +106 -0
- package/dist/core/soak/soak-reader.js.map +1 -0
- package/dist/core/socket-server/discovery.d.ts +78 -1
- package/dist/core/socket-server/discovery.d.ts.map +1 -1
- package/dist/core/socket-server/discovery.js +356 -37
- package/dist/core/socket-server/discovery.js.map +1 -1
- package/dist/core/socket-server/index.d.ts +10 -4
- package/dist/core/socket-server/index.d.ts.map +1 -1
- package/dist/core/socket-server/index.js +6 -3
- package/dist/core/socket-server/index.js.map +1 -1
- package/dist/core/socket-server/permission-delivery.d.ts +125 -0
- package/dist/core/socket-server/permission-delivery.d.ts.map +1 -0
- package/dist/core/socket-server/permission-delivery.js +152 -0
- package/dist/core/socket-server/permission-delivery.js.map +1 -0
- package/dist/core/socket-server/permission-registry.d.ts +301 -0
- package/dist/core/socket-server/permission-registry.d.ts.map +1 -0
- package/dist/core/socket-server/permission-registry.js +410 -0
- package/dist/core/socket-server/permission-registry.js.map +1 -0
- package/dist/core/socket-server/permission-relay.d.ts +81 -0
- package/dist/core/socket-server/permission-relay.d.ts.map +1 -0
- package/dist/core/socket-server/permission-relay.js +528 -0
- package/dist/core/socket-server/permission-relay.js.map +1 -0
- package/dist/core/socket-server/safe-text.d.ts +83 -0
- package/dist/core/socket-server/safe-text.d.ts.map +1 -0
- package/dist/core/socket-server/safe-text.js +277 -0
- package/dist/core/socket-server/safe-text.js.map +1 -0
- package/dist/core/socket-server/session-integration.d.ts +65 -2
- package/dist/core/socket-server/session-integration.d.ts.map +1 -1
- package/dist/core/socket-server/session-integration.js +434 -32
- package/dist/core/socket-server/session-integration.js.map +1 -1
- package/dist/core/socket-server/socket-client.d.ts +21 -2
- package/dist/core/socket-server/socket-client.d.ts.map +1 -1
- package/dist/core/socket-server/socket-client.js +52 -1
- package/dist/core/socket-server/socket-client.js.map +1 -1
- package/dist/core/socket-server/socket-server.d.ts +152 -1
- package/dist/core/socket-server/socket-server.d.ts.map +1 -1
- package/dist/core/socket-server/socket-server.js +775 -26
- package/dist/core/socket-server/socket-server.js.map +1 -1
- package/dist/core/socket-server/types.d.ts +106 -2
- package/dist/core/socket-server/types.d.ts.map +1 -1
- package/dist/core/socket-server/types.js +13 -1
- package/dist/core/socket-server/types.js.map +1 -1
- package/dist/core/system-prompt.d.ts.map +1 -1
- package/dist/core/system-prompt.js +10 -21
- package/dist/core/system-prompt.js.map +1 -1
- package/dist/core/tools/bash.d.ts +3 -1
- package/dist/core/tools/bash.d.ts.map +1 -1
- package/dist/core/tools/bash.js +39 -7
- package/dist/core/tools/bash.js.map +1 -1
- package/dist/core/tools/edit.d.ts.map +1 -1
- package/dist/core/tools/edit.js +17 -2
- package/dist/core/tools/edit.js.map +1 -1
- package/dist/core/tools/find.d.ts +3 -0
- package/dist/core/tools/find.d.ts.map +1 -1
- package/dist/core/tools/find.js +11 -18
- package/dist/core/tools/find.js.map +1 -1
- package/dist/core/tools/read.js +1 -1
- package/dist/core/tools/read.js.map +1 -1
- package/dist/core/tools/tool-definition-wrapper.d.ts.map +1 -1
- package/dist/core/tools/tool-definition-wrapper.js +3 -1
- package/dist/core/tools/tool-definition-wrapper.js.map +1 -1
- package/dist/core/trust-manager.d.ts +6 -4
- package/dist/core/trust-manager.d.ts.map +1 -1
- package/dist/core/trust-manager.js +55 -21
- package/dist/core/trust-manager.js.map +1 -1
- package/dist/core/usage-totals.d.ts +19 -0
- package/dist/core/usage-totals.d.ts.map +1 -0
- package/dist/core/usage-totals.js +52 -0
- package/dist/core/usage-totals.js.map +1 -0
- package/dist/extensions/index.d.ts +3 -0
- package/dist/extensions/index.d.ts.map +1 -0
- package/dist/extensions/index.js +7 -0
- package/dist/extensions/index.js.map +1 -0
- package/dist/extensions/llama/client.d.ts +61 -0
- package/dist/extensions/llama/client.d.ts.map +1 -0
- package/dist/extensions/llama/client.js +300 -0
- package/dist/extensions/llama/client.js.map +1 -0
- package/dist/extensions/llama/huggingface.d.ts +23 -0
- package/dist/extensions/llama/huggingface.d.ts.map +1 -0
- package/dist/extensions/llama/huggingface.js +139 -0
- package/dist/extensions/llama/huggingface.js.map +1 -0
- package/dist/extensions/llama/index.d.ts +3 -0
- package/dist/extensions/llama/index.d.ts.map +1 -0
- package/dist/extensions/llama/index.js +208 -0
- package/dist/extensions/llama/index.js.map +1 -0
- package/dist/extensions/llama/provider.d.ts +10 -0
- package/dist/extensions/llama/provider.d.ts.map +1 -0
- package/dist/extensions/llama/provider.js +111 -0
- package/dist/extensions/llama/provider.js.map +1 -0
- package/dist/extensions/llama/ui.d.ts +42 -0
- package/dist/extensions/llama/ui.d.ts.map +1 -0
- package/dist/extensions/llama/ui.js +400 -0
- package/dist/extensions/llama/ui.js.map +1 -0
- package/dist/extensions/stub-provider/index.d.ts +9 -0
- package/dist/extensions/stub-provider/index.d.ts.map +1 -0
- package/dist/extensions/stub-provider/index.js +13 -0
- package/dist/extensions/stub-provider/index.js.map +1 -0
- package/dist/extensions/stub-provider/provider.d.ts +77 -0
- package/dist/extensions/stub-provider/provider.d.ts.map +1 -0
- package/dist/extensions/stub-provider/provider.js +193 -0
- package/dist/extensions/stub-provider/provider.js.map +1 -0
- package/dist/hooks/gsd/draht-quality-gate.js +3 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +305 -61
- package/dist/main.js.map +1 -1
- package/dist/modes/interactive/components/assistant-message.d.ts.map +1 -1
- package/dist/modes/interactive/components/assistant-message.js +22 -10
- package/dist/modes/interactive/components/assistant-message.js.map +1 -1
- package/dist/modes/interactive/components/config-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/config-selector.js +3 -2
- package/dist/modes/interactive/components/config-selector.js.map +1 -1
- package/dist/modes/interactive/components/custom-editor.js +1 -1
- package/dist/modes/interactive/components/custom-editor.js.map +1 -1
- package/dist/modes/interactive/components/custom-message.d.ts +3 -1
- package/dist/modes/interactive/components/custom-message.d.ts.map +1 -1
- package/dist/modes/interactive/components/custom-message.js +9 -2
- package/dist/modes/interactive/components/custom-message.js.map +1 -1
- package/dist/modes/interactive/components/extension-editor.d.ts +1 -2
- package/dist/modes/interactive/components/extension-editor.d.ts.map +1 -1
- package/dist/modes/interactive/components/extension-editor.js +16 -46
- package/dist/modes/interactive/components/extension-editor.js.map +1 -1
- package/dist/modes/interactive/components/extension-selector.d.ts +49 -0
- package/dist/modes/interactive/components/extension-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/extension-selector.js +202 -5
- package/dist/modes/interactive/components/extension-selector.js.map +1 -1
- package/dist/modes/interactive/components/footer.d.ts.map +1 -1
- package/dist/modes/interactive/components/footer.js +24 -23
- package/dist/modes/interactive/components/footer.js.map +1 -1
- package/dist/modes/interactive/components/login-dialog.d.ts +5 -5
- package/dist/modes/interactive/components/login-dialog.d.ts.map +1 -1
- package/dist/modes/interactive/components/login-dialog.js +17 -8
- package/dist/modes/interactive/components/login-dialog.js.map +1 -1
- package/dist/modes/interactive/components/model-selector.d.ts +11 -4
- package/dist/modes/interactive/components/model-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/model-selector.js +74 -41
- package/dist/modes/interactive/components/model-selector.js.map +1 -1
- package/dist/modes/interactive/components/oauth-selector.d.ts +4 -4
- package/dist/modes/interactive/components/oauth-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/oauth-selector.js +14 -25
- package/dist/modes/interactive/components/oauth-selector.js.map +1 -1
- package/dist/modes/interactive/components/scoped-models-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/scoped-models-selector.js +22 -13
- package/dist/modes/interactive/components/scoped-models-selector.js.map +1 -1
- package/dist/modes/interactive/components/session-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/session-selector.js +10 -2
- package/dist/modes/interactive/components/session-selector.js.map +1 -1
- package/dist/modes/interactive/components/settings-selector.d.ts +2 -0
- package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/settings-selector.js +10 -0
- package/dist/modes/interactive/components/settings-selector.js.map +1 -1
- package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
- package/dist/modes/interactive/components/tool-execution.js +10 -1
- package/dist/modes/interactive/components/tool-execution.js.map +1 -1
- package/dist/modes/interactive/components/tree-selector.d.ts +25 -2
- package/dist/modes/interactive/components/tree-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/tree-selector.js +68 -17
- package/dist/modes/interactive/components/tree-selector.js.map +1 -1
- package/dist/modes/interactive/external-editor.d.ts +12 -0
- package/dist/modes/interactive/external-editor.d.ts.map +1 -0
- package/dist/modes/interactive/external-editor.js +37 -0
- package/dist/modes/interactive/external-editor.js.map +1 -0
- package/dist/modes/interactive/interactive-mode.d.ts +107 -10
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +763 -285
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/rpc/rpc-client.d.ts +4 -0
- package/dist/modes/rpc/rpc-client.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-client.js +7 -0
- package/dist/modes/rpc/rpc-client.js.map +1 -1
- package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-mode.js +185 -9
- package/dist/modes/rpc/rpc-mode.js.map +1 -1
- package/dist/modes/rpc/rpc-types.d.ts +79 -1
- package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-types.js.map +1 -1
- package/dist/package-manager-cli.d.ts.map +1 -1
- package/dist/package-manager-cli.js +67 -5
- package/dist/package-manager-cli.js.map +1 -1
- package/dist/prompts/commands/atomic-commit.md +1 -12
- package/dist/prompts/commands/brainstorm.md +12 -15
- package/dist/prompts/commands/create-verification-skill.md +108 -0
- package/dist/prompts/commands/discuss-phase.md +3 -14
- package/dist/prompts/commands/execute-phase.md +3 -11
- package/dist/prompts/commands/fix.md +41 -18
- package/dist/prompts/commands/grill.md +66 -0
- package/dist/prompts/commands/init-project.md +5 -15
- package/dist/prompts/commands/map-codebase.md +1 -13
- package/dist/prompts/commands/new-project.md +5 -15
- package/dist/prompts/commands/next-milestone.md +1 -13
- package/dist/prompts/commands/orchestrate.md +1 -1
- package/dist/prompts/commands/pause-work.md +1 -13
- package/dist/prompts/commands/plan-phase.md +2 -11
- package/dist/prompts/commands/progress.md +0 -13
- package/dist/prompts/commands/quick.md +3 -11
- package/dist/prompts/commands/resolve-conflicts.md +71 -0
- package/dist/prompts/commands/resume-work.md +0 -13
- package/dist/prompts/commands/review.md +4 -13
- package/dist/prompts/commands/triage.md +159 -0
- package/dist/prompts/commands/verify-work.md +4 -14
- package/dist/prompts/commands/why.md +135 -0
- package/dist/skills/atomic-reasoning/SKILL.md +115 -0
- package/dist/skills/blast-radius/SKILL.md +55 -0
- package/dist/skills/epistemics/SKILL.md +99 -0
- package/dist/skills/hexagon-animation/SKILL.md +49 -0
- package/dist/skills/hexagon-animation/generate.mjs +81 -0
- package/dist/skills/hexagon-animation/template.html +361 -0
- package/dist/skills/typescript-discipline/SKILL.md +50 -0
- package/dist/skills/typescript-discipline/references/patterns.md +294 -0
- package/dist/skills/unslop/SKILL.md +101 -0
- package/dist/utils/canonical-path.d.ts +28 -0
- package/dist/utils/canonical-path.d.ts.map +1 -0
- package/dist/utils/canonical-path.js +90 -0
- package/dist/utils/canonical-path.js.map +1 -0
- package/dist/utils/child-process.d.ts +12 -1
- package/dist/utils/child-process.d.ts.map +1 -1
- package/dist/utils/child-process.js +25 -8
- package/dist/utils/child-process.js.map +1 -1
- package/dist/utils/clipboard-native.d.ts +1 -0
- package/dist/utils/clipboard-native.d.ts.map +1 -1
- package/dist/utils/clipboard-native.js.map +1 -1
- package/dist/utils/clipboard.d.ts +2 -0
- package/dist/utils/clipboard.d.ts.map +1 -1
- package/dist/utils/clipboard.js +53 -9
- package/dist/utils/clipboard.js.map +1 -1
- package/dist/utils/exif-orientation.d.ts.map +1 -1
- package/dist/utils/exif-orientation.js +2 -3
- package/dist/utils/exif-orientation.js.map +1 -1
- package/dist/utils/paths.d.ts +2 -7
- package/dist/utils/paths.d.ts.map +1 -1
- package/dist/utils/paths.js +13 -15
- package/dist/utils/paths.js.map +1 -1
- package/dist/utils/shell.d.ts.map +1 -1
- package/dist/utils/shell.js +6 -4
- package/dist/utils/shell.js.map +1 -1
- package/dist/utils/tools-manager.d.ts.map +1 -1
- package/dist/utils/tools-manager.js +3 -5
- package/dist/utils/tools-manager.js.map +1 -1
- package/dist/utils/version-check.d.ts.map +1 -1
- package/dist/utils/version-check.js +3 -1
- package/dist/utils/version-check.js.map +1 -1
- package/docs/attachable-sessions.md +79 -0
- package/docs/compaction.md +29 -24
- package/docs/containerization.md +23 -23
- package/docs/custom-provider.md +66 -32
- package/docs/development.md +9 -9
- package/docs/docs.json +6 -2
- package/docs/duet.md +42 -0
- package/docs/environment-variables.md +88 -0
- package/docs/extensions.md +278 -65
- package/docs/index.md +12 -10
- package/docs/json.md +12 -8
- package/docs/keybindings.md +8 -5
- package/docs/llama-cpp.md +99 -0
- package/docs/models.md +19 -8
- package/docs/packages.md +43 -42
- package/docs/prompt-templates.md +5 -4
- package/docs/providers.md +45 -11
- package/docs/quickstart.md +33 -33
- package/docs/rpc.md +190 -19
- package/docs/sdk.md +18 -15
- package/docs/security.md +18 -18
- package/docs/session-format.md +53 -15
- package/docs/sessions.md +17 -14
- package/docs/settings.md +26 -19
- package/docs/shell-aliases.md +2 -2
- package/docs/skills.md +12 -11
- package/docs/terminal-setup.md +12 -12
- package/docs/termux.md +6 -6
- package/docs/themes.md +11 -11
- package/docs/tmux.md +4 -4
- package/docs/tui.md +46 -31
- package/docs/usage.md +53 -63
- package/docs/windows.md +2 -2
- package/examples/extensions/custom-compaction.ts +4 -0
- package/examples/extensions/custom-provider-anthropic/index.ts +9 -3
- package/examples/extensions/custom-provider-anthropic/package.json +1 -1
- package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
- package/examples/extensions/custom-provider-qwen-cli/package.json +1 -1
- package/examples/extensions/gondolin/package.json +2 -2
- package/examples/extensions/handoff.ts +9 -1
- package/examples/extensions/kimi-deferred-tools.ts +61 -0
- package/examples/extensions/message-renderer.ts +2 -2
- package/examples/extensions/overlay-qa-tests.ts +3 -3
- package/examples/extensions/sandbox/package.json +1 -1
- package/examples/extensions/summarize.ts +3 -0
- package/examples/extensions/with-deps/package.json +1 -1
- package/examples/sdk/02-custom-model.ts +5 -8
- package/examples/sdk/09-api-keys-and-oauth.ts +15 -31
- package/examples/sdk/12-full-control.ts +9 -12
- package/hooks/gsd/draht-quality-gate.js +3 -3
- package/package.json +14 -11
- package/prompts/commands/atomic-commit.md +1 -12
- package/prompts/commands/brainstorm.md +12 -15
- package/prompts/commands/create-verification-skill.md +108 -0
- package/prompts/commands/discuss-phase.md +3 -14
- package/prompts/commands/execute-phase.md +3 -11
- package/prompts/commands/fix.md +41 -18
- package/prompts/commands/grill.md +66 -0
- package/prompts/commands/init-project.md +5 -15
- package/prompts/commands/map-codebase.md +1 -13
- package/prompts/commands/new-project.md +5 -15
- package/prompts/commands/next-milestone.md +1 -13
- package/prompts/commands/orchestrate.md +1 -1
- package/prompts/commands/pause-work.md +1 -13
- package/prompts/commands/plan-phase.md +2 -11
- package/prompts/commands/progress.md +0 -13
- package/prompts/commands/quick.md +3 -11
- package/prompts/commands/resolve-conflicts.md +71 -0
- package/prompts/commands/resume-work.md +0 -13
- package/prompts/commands/review.md +4 -13
- package/prompts/commands/triage.md +159 -0
- package/prompts/commands/verify-work.md +4 -14
- package/prompts/commands/why.md +135 -0
- package/skills/atomic-reasoning/SKILL.md +115 -0
- package/skills/blast-radius/SKILL.md +55 -0
- package/skills/epistemics/SKILL.md +99 -0
- package/skills/hexagon-animation/SKILL.md +49 -0
- package/skills/hexagon-animation/generate.mjs +81 -0
- package/skills/hexagon-animation/template.html +361 -0
- package/skills/typescript-discipline/SKILL.md +50 -0
- package/skills/typescript-discipline/references/patterns.md +294 -0
- package/skills/unslop/SKILL.md +101 -0
- package/dist/core/provider-display-names.d.ts +0 -2
- package/dist/core/provider-display-names.d.ts.map +0 -1
- package/dist/core/provider-display-names.js +0 -36
- package/dist/core/provider-display-names.js.map +0 -1
- package/dist/prompts/agents/build.md +0 -86
- package/dist/prompts/agents/plan.md +0 -79
- package/dist/prompts/agents/verify.md +0 -69
- package/prompts/agents/build.md +0 -86
- package/prompts/agents/plan.md +0 -79
- package/prompts/agents/verify.md +0 -69
|
@@ -2,17 +2,240 @@
|
|
|
2
2
|
* Socket Session Discovery
|
|
3
3
|
*
|
|
4
4
|
* Scans the sockets directory to find all running attachable sessions.
|
|
5
|
+
*
|
|
6
|
+
* ── OWNERSHIP AND HYGIENE RULES (R35-ALWAYS.3, R35-ALWAYS.4) ───────────────────
|
|
7
|
+
*
|
|
8
|
+
* These rules are implemented TWICE on purpose: here, and synchronously in
|
|
9
|
+
* `packages/geist-core/src/attach/socket-sessions.ts`, which may not import this
|
|
10
|
+
* package across the geist boundary gate. `scripts/check-geist-protocol.mjs`'s
|
|
11
|
+
* mirror clause only covers the relayed FRAMES — it does not cover any of this —
|
|
12
|
+
* so the two files have to be changed together by hand. They had already drifted
|
|
13
|
+
* once (this file used to shield a lock from the orphan sweep before it had
|
|
14
|
+
* established that `<id>.sock` was a socket at all); that drift is fixed here.
|
|
15
|
+
*
|
|
16
|
+
* O1. OURS OR INVISIBLE. An entry whose file uid is not this process's uid, or
|
|
17
|
+
* whose lock names a live process we cannot signal (`kill` → EPERM, i.e. it
|
|
18
|
+
* belongs to another user), is never listed and never dialled. Before this,
|
|
19
|
+
* a world-writable sockets directory holding a 0666 socket and a lock naming
|
|
20
|
+
* root's pid 1 was listed as a live attachable session by both readers and
|
|
21
|
+
* would have been dialled by the bridge; the 0700 directory mode was the
|
|
22
|
+
* only thing in the way, and nothing asserted it.
|
|
23
|
+
* O2. FOREIGN IS NOT REAPED HERE. A read must not destroy what it cannot
|
|
24
|
+
* interpret, and a fleet scan runs on an HTTP request. Foreign entries are
|
|
25
|
+
* skipped. `SocketServer.start()` is the one place allowed to remove them,
|
|
26
|
+
* and only after it has asserted the directory really is ours and owner-only
|
|
27
|
+
* — which makes a foreign file inside it debris by construction.
|
|
28
|
+
* O3. EPERM STILL MEANS ALIVE. `kill(pid, 0)` → EPERM says the process exists.
|
|
29
|
+
* It is never read as dead, so another user's live session is never reaped.
|
|
30
|
+
* What changed is that "alive" no longer implies "ours".
|
|
31
|
+
* O4. DEBRIS. Two classes used to be immortal — a `.sock` with no `.lock`, and a
|
|
32
|
+
* regular file named `<id>.sock` — because both readers `continue`d past
|
|
33
|
+
* them without reaping. Two such orphans had been sitting in the real
|
|
34
|
+
* `~/.draht/agent/sockets` since 2026-08-21. Both are now reaped once they
|
|
35
|
+
* are older than {@link DEBRIS_GRACE_MS}, which no legitimate transient
|
|
36
|
+
* state survives: the lock is claimed BEFORE the socket is bound, and
|
|
37
|
+
* teardown removes the socket BEFORE the lock, so a socket without a lock is
|
|
38
|
+
* never a normal intermediate state in either direction.
|
|
39
|
+
* O5. A LOCK CANNOT PREDATE THE BOOT. See {@link lockIsPreBootDebris}.
|
|
5
40
|
*/
|
|
6
41
|
import { existsSync } from "node:fs";
|
|
7
|
-
import { readdir, readFile,
|
|
42
|
+
import { lstat, readdir, readFile, rm } from "node:fs/promises";
|
|
43
|
+
import { uptime } from "node:os";
|
|
8
44
|
import path from "node:path";
|
|
45
|
+
import { getSoakLog, SOAK_EVENTS } from "../soak/soak-log.js";
|
|
46
|
+
/**
|
|
47
|
+
* How long a file has to sit untouched before it counts as debris rather than as
|
|
48
|
+
* a transient another starter is in the middle of creating.
|
|
49
|
+
*
|
|
50
|
+
* Deliberately the same number as `UNWRITTEN_LOCK_STALE_MS` in `socket-server.ts`
|
|
51
|
+
* and `DEBRIS_GRACE_MS` in geist-core's `socket-sessions.ts`. It is duplicated
|
|
52
|
+
* rather than imported for the boundary reason at the top of this file.
|
|
53
|
+
*/
|
|
54
|
+
export const DEBRIS_GRACE_MS = 10_000;
|
|
55
|
+
/**
|
|
56
|
+
* Slack on the boot-time comparison in {@link lockIsPreBootDebris}.
|
|
57
|
+
*
|
|
58
|
+
* `Date.now()` is wall clock and `os.uptime()` is elapsed-since-boot, so their
|
|
59
|
+
* difference moves whenever the clock is stepped. Five minutes is far more than
|
|
60
|
+
* NTP ever steps in one go and far less than the gap between a lock written
|
|
61
|
+
* before a reboot and the reboot itself.
|
|
62
|
+
*/
|
|
63
|
+
const PRE_BOOT_SLACK_MS = 300_000;
|
|
64
|
+
function newReapTally() {
|
|
65
|
+
return {
|
|
66
|
+
reaped: {
|
|
67
|
+
deadPid: 0,
|
|
68
|
+
preBootLock: 0,
|
|
69
|
+
locklessSocket: 0,
|
|
70
|
+
notASocket: 0,
|
|
71
|
+
orphanLockDeadPid: 0,
|
|
72
|
+
orphanLockPreBoot: 0,
|
|
73
|
+
},
|
|
74
|
+
skipped: { foreignSocketUid: 0, foreignLockUid: 0, foreignPid: 0 },
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/** Total files unlinked in this pass. */
|
|
78
|
+
function reapedFileCount(tally) {
|
|
79
|
+
return Object.values(tally.reaped).reduce((total, count) => total + count, 0);
|
|
80
|
+
}
|
|
81
|
+
/** Total entries left alone for an ownership reason. */
|
|
82
|
+
function skippedCount(tally) {
|
|
83
|
+
return Object.values(tally.skipped).reduce((total, count) => total + count, 0);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Publish one pass's tally, and only when it has something to say.
|
|
87
|
+
*
|
|
88
|
+
* A quiet sweep writes nothing: `discoverSocketSessions` runs on every session
|
|
89
|
+
* start and on every fleet read, and a record per no-op scan would be the bulk of
|
|
90
|
+
* a seven-day log. Silence here therefore means "nothing was removed and nothing
|
|
91
|
+
* was skipped", which is the common case and the one a verdict does not need
|
|
92
|
+
* evidence for.
|
|
93
|
+
*
|
|
94
|
+
* Never stdout, never stderr, never a throw — a hygiene sweep may not be what
|
|
95
|
+
* breaks a session start.
|
|
96
|
+
*
|
|
97
|
+
* NO `sessionId` IS SUPPLIED HERE, on purpose. The writer stamps its own whenever
|
|
98
|
+
* it has one, which it does for every session process — the id is set at bind,
|
|
99
|
+
* before `SocketServer.start()` runs this sweep. The one caller that reaches this
|
|
100
|
+
* function without a session of its own is `draht --list-sessions`, and a sweep
|
|
101
|
+
* performed by a process that owns no session is not attributable to one; the
|
|
102
|
+
* record is written without the field rather than with a borrowed value.
|
|
103
|
+
*/
|
|
104
|
+
function recordReapTally(socketDir, tally, scanned, live) {
|
|
105
|
+
const count = reapedFileCount(tally);
|
|
106
|
+
const skipped = skippedCount(tally);
|
|
107
|
+
if (count === 0 && skipped === 0)
|
|
108
|
+
return;
|
|
109
|
+
try {
|
|
110
|
+
getSoakLog().record(SOAK_EVENTS.SOCKETS_REAPED, {
|
|
111
|
+
// FILES, not pairs: a dead session contributes its `.sock` and its `.lock`
|
|
112
|
+
// separately, so this number can be compared against a `readdir` difference.
|
|
113
|
+
count,
|
|
114
|
+
reaped: tally.reaped,
|
|
115
|
+
skipped: tally.skipped,
|
|
116
|
+
skippedCount: skipped,
|
|
117
|
+
socketDir,
|
|
118
|
+
scanned,
|
|
119
|
+
live,
|
|
120
|
+
}, count > 0 ? "warn" : "info");
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
// Deliberately empty, and deliberately not logged anywhere.
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Who owns the process a lock names.
|
|
128
|
+
*
|
|
129
|
+
* `process.kill(pid, 0)` sends no signal; it only asks whether the pid could be
|
|
130
|
+
* signalled. EPERM is the discriminator that costs nothing: a process this uid
|
|
131
|
+
* could have started is always signallable by this uid, so EPERM means alive AND
|
|
132
|
+
* not ours. That single distinction is what R35-ALWAYS.3 rests on — without it a
|
|
133
|
+
* lock naming root's pid 1 reads as a live session of ours forever.
|
|
134
|
+
*/
|
|
135
|
+
export function pidOwnership(pid) {
|
|
136
|
+
if (!Number.isInteger(pid) || pid <= 0)
|
|
137
|
+
return "dead";
|
|
138
|
+
try {
|
|
139
|
+
process.kill(pid, 0);
|
|
140
|
+
return "ours";
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
return error?.code === "EPERM" ? "foreign" : "dead";
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Check if a process with the given PID is running.
|
|
148
|
+
*
|
|
149
|
+
* Uses `process.kill(pid, 0)` which doesn't actually send a signal,
|
|
150
|
+
* but throws if the process doesn't exist.
|
|
151
|
+
*
|
|
152
|
+
* EPERM means the process exists but belongs to another user: alive, and still
|
|
153
|
+
* reported as alive (O3). Callers that also care WHOSE it is ask
|
|
154
|
+
* {@link pidOwnership} instead.
|
|
155
|
+
*/
|
|
156
|
+
export function isProcessRunning(pid) {
|
|
157
|
+
return pidOwnership(pid) !== "dead";
|
|
158
|
+
}
|
|
159
|
+
/** This process's uid, or null off POSIX where the concept does not exist. */
|
|
160
|
+
export function currentUid() {
|
|
161
|
+
return typeof process.getuid === "function" ? process.getuid() : null;
|
|
162
|
+
}
|
|
163
|
+
/** Whether a directory entry belongs to this uid. Always true off POSIX. */
|
|
164
|
+
function ownedByUs(info, uid) {
|
|
165
|
+
return uid === null || info.uid === uid;
|
|
166
|
+
}
|
|
167
|
+
/** Whether a file has sat untouched long enough to count as debris. */
|
|
168
|
+
function olderThanGrace(info, now) {
|
|
169
|
+
return now - info.mtimeMs > DEBRIS_GRACE_MS;
|
|
170
|
+
}
|
|
171
|
+
/** Epoch milliseconds at which this machine booted. */
|
|
172
|
+
function bootEpochMs() {
|
|
173
|
+
return Date.now() - uptime() * 1000;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* A lock recording a process that started before this machine last booted.
|
|
177
|
+
*
|
|
178
|
+
* This is the answer to "bound readable-pid locks" (R35-ALWAYS.4). The two
|
|
179
|
+
* candidates were recording the owner's start time and comparing it, or putting a
|
|
180
|
+
* blanket age bound on readable-pid locks. **A blanket age bound is rejected**: a
|
|
181
|
+
* draht session legitimately runs for days, and reaping a live session's lock is
|
|
182
|
+
* worse than the leak it cures — the id becomes claimable while the socket is
|
|
183
|
+
* still bound, and two processes end up appending to one session JSONL.
|
|
184
|
+
*
|
|
185
|
+
* So the lock carries the owning process's start time as a 4th line, and the only
|
|
186
|
+
* comparison made is the one that cannot produce a false positive on a live
|
|
187
|
+
* session: nothing running now started before the last boot. That covers the
|
|
188
|
+
* dominant real-world source of pid recycling — a reboot recycles every pid at
|
|
189
|
+
* once, which is how a lock naming pid 1 comes to poison a session id forever.
|
|
190
|
+
*
|
|
191
|
+
* Line 3 (the ISO creation time) is used when line 4 is absent, so the locks
|
|
192
|
+
* already on disk from older builds are covered too. Old 3-line locks still parse:
|
|
193
|
+
* the length floor stays at 3.
|
|
194
|
+
*
|
|
195
|
+
* NOT covered: a same-uid pid recycled without a reboot. That needs the owner's
|
|
196
|
+
* real start time from the OS, which costs a subprocess per lock on a path that
|
|
197
|
+
* runs on every fleet read.
|
|
198
|
+
*/
|
|
199
|
+
function lockIsPreBootDebris(lock, bootMs) {
|
|
200
|
+
return lock.startedAtMs !== null && lock.startedAtMs < bootMs - PRE_BOOT_SLACK_MS;
|
|
201
|
+
}
|
|
202
|
+
/** Parse a lock file's contents, or null when it says nothing trustworthy. */
|
|
203
|
+
function parseLock(contents) {
|
|
204
|
+
const lines = contents.trim().split("\n");
|
|
205
|
+
// Three lines is still the contract. A 4th line is additive; a build that does
|
|
206
|
+
// not write one is read exactly as before.
|
|
207
|
+
if (lines.length < 3)
|
|
208
|
+
return null;
|
|
209
|
+
const pid = Number.parseInt(lines[0], 10);
|
|
210
|
+
if (!Number.isInteger(pid) || pid <= 0)
|
|
211
|
+
return null;
|
|
212
|
+
const createdAt = new Date(lines[2]);
|
|
213
|
+
const fromLine4 = lines.length >= 4 ? Number.parseInt(lines[3], 10) : Number.NaN;
|
|
214
|
+
const startedAtMs = Number.isFinite(fromLine4)
|
|
215
|
+
? fromLine4
|
|
216
|
+
: Number.isFinite(createdAt.getTime())
|
|
217
|
+
? createdAt.getTime()
|
|
218
|
+
: null;
|
|
219
|
+
return { pid, cwd: lines[1], createdAt, startedAtMs };
|
|
220
|
+
}
|
|
221
|
+
/** Read and parse a lock file, or null when it is missing or half-written. */
|
|
222
|
+
async function readLock(lockPath) {
|
|
223
|
+
try {
|
|
224
|
+
return parseLock(await readFile(lockPath, "utf-8"));
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
9
230
|
/**
|
|
10
231
|
* Discover all running socket-based sessions.
|
|
11
232
|
*
|
|
12
233
|
* Scans the socket directory for .sock files and reads their corresponding
|
|
13
234
|
* .lock files to extract metadata (PID, cwd, createdAt).
|
|
14
235
|
*
|
|
15
|
-
*
|
|
236
|
+
* Only sessions belonging to this uid are returned, and reaping follows the rules
|
|
237
|
+
* at the top of this file: dead-owner pairs, pre-boot locks, lockless sockets and
|
|
238
|
+
* non-socket `<id>.sock` files go; anything foreign, fresh, or unreadable stays.
|
|
16
239
|
*
|
|
17
240
|
* @param socketDir - Directory containing socket files
|
|
18
241
|
* @returns Array of discovered session info
|
|
@@ -22,50 +245,104 @@ export async function discoverSocketSessions(socketDir) {
|
|
|
22
245
|
return [];
|
|
23
246
|
}
|
|
24
247
|
const sessions = [];
|
|
248
|
+
const uid = currentUid();
|
|
249
|
+
const now = Date.now();
|
|
250
|
+
const bootMs = bootEpochMs();
|
|
251
|
+
const tally = newReapTally();
|
|
25
252
|
try {
|
|
26
253
|
const entries = await readdir(socketDir);
|
|
254
|
+
const sessionIdsWithSocket = new Set();
|
|
27
255
|
for (const entry of entries) {
|
|
28
256
|
// Only process .sock files
|
|
29
257
|
if (!entry.endsWith(".sock"))
|
|
30
258
|
continue;
|
|
31
|
-
const sessionId = entry.
|
|
259
|
+
const sessionId = entry.slice(0, -".sock".length);
|
|
260
|
+
if (sessionId.length === 0)
|
|
261
|
+
continue;
|
|
32
262
|
const socketPath = path.join(socketDir, entry);
|
|
33
263
|
const lockPath = path.join(socketDir, `${sessionId}.lock`);
|
|
34
|
-
//
|
|
264
|
+
// lstat, not stat: a symlink named `<id>.sock` is not a socket we published,
|
|
265
|
+
// and following it would let something outside the directory decide what this
|
|
266
|
+
// scan sees.
|
|
267
|
+
let socketInfo;
|
|
35
268
|
try {
|
|
36
|
-
|
|
37
|
-
if (!stats.isSocket())
|
|
38
|
-
continue;
|
|
269
|
+
socketInfo = await lstat(socketPath);
|
|
39
270
|
}
|
|
40
271
|
catch {
|
|
41
272
|
continue;
|
|
42
273
|
}
|
|
43
|
-
|
|
44
|
-
|
|
274
|
+
if (!socketInfo.isSocket()) {
|
|
275
|
+
// Debris class (ii): a regular file (or symlink) wearing a .sock name.
|
|
276
|
+
// Neither reader could ever remove one before. Directories are left alone —
|
|
277
|
+
// removing a tree is not this function's business.
|
|
278
|
+
if (!socketInfo.isDirectory() && ownedByUs(socketInfo, uid) && olderThanGrace(socketInfo, now)) {
|
|
279
|
+
await reapStaleSession(socketPath);
|
|
280
|
+
tally.reaped.notASocket++;
|
|
281
|
+
}
|
|
282
|
+
// Deliberately NOT added to sessionIdsWithSocket: this id has no socket, so
|
|
283
|
+
// its lock belongs to the orphan sweep below. Shielding it here (the old
|
|
284
|
+
// behaviour, and the drift from geist-core) is what made class (ii) immortal.
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
// O1: another user's socket is not ours to list, dial, or delete.
|
|
288
|
+
if (!ownedByUs(socketInfo, uid)) {
|
|
289
|
+
tally.skipped.foreignSocketUid++;
|
|
45
290
|
continue;
|
|
291
|
+
}
|
|
292
|
+
sessionIdsWithSocket.add(sessionId);
|
|
293
|
+
let lockInfo;
|
|
46
294
|
try {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
//
|
|
55
|
-
if (
|
|
56
|
-
|
|
57
|
-
|
|
295
|
+
lockInfo = await lstat(lockPath);
|
|
296
|
+
}
|
|
297
|
+
catch {
|
|
298
|
+
lockInfo = null;
|
|
299
|
+
}
|
|
300
|
+
if (lockInfo === null) {
|
|
301
|
+
// Debris class (i): a socket with no lock. Never a normal transient — the
|
|
302
|
+
// lock is claimed before the bind, and teardown removes the socket first.
|
|
303
|
+
if (olderThanGrace(socketInfo, now)) {
|
|
304
|
+
await reapStaleSession(socketPath);
|
|
305
|
+
tally.reaped.locklessSocket++;
|
|
58
306
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
// A lock that is not a plain file, or is not ours, is not evidence about a
|
|
310
|
+
// session of ours. Left strictly alone.
|
|
311
|
+
if (!lockInfo.isFile() || !ownedByUs(lockInfo, uid)) {
|
|
312
|
+
if (!ownedByUs(lockInfo, uid))
|
|
313
|
+
tally.skipped.foreignLockUid++;
|
|
314
|
+
continue;
|
|
66
315
|
}
|
|
67
|
-
|
|
316
|
+
const lock = await readLock(lockPath);
|
|
317
|
+
// Unreadable or half-written: ownership unknown, never guess.
|
|
318
|
+
if (!lock)
|
|
319
|
+
continue;
|
|
320
|
+
const ownership = pidOwnership(lock.pid);
|
|
321
|
+
if (ownership === "dead" || lockIsPreBootDebris(lock, bootMs)) {
|
|
322
|
+
await reapStaleSession(socketPath, lockPath);
|
|
323
|
+
// Two files, and counted as two: this number is compared against a
|
|
324
|
+
// directory listing, which cannot see pairs.
|
|
325
|
+
if (ownership === "dead")
|
|
326
|
+
tally.reaped.deadPid += 2;
|
|
327
|
+
else
|
|
328
|
+
tally.reaped.preBootLock += 2;
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
// Alive, but another user's process: never listed (O1), never reaped (O2).
|
|
332
|
+
if (ownership === "foreign") {
|
|
333
|
+
tally.skipped.foreignPid++;
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
sessions.push({
|
|
337
|
+
sessionId,
|
|
338
|
+
socketPath,
|
|
339
|
+
pid: lock.pid,
|
|
340
|
+
cwd: lock.cwd,
|
|
341
|
+
createdAt: lock.createdAt,
|
|
342
|
+
});
|
|
68
343
|
}
|
|
344
|
+
await reapOrphanedLocks(socketDir, entries, sessionIdsWithSocket, uid, bootMs, tally);
|
|
345
|
+
recordReapTally(socketDir, tally, entries.length, sessions.length);
|
|
69
346
|
}
|
|
70
347
|
catch {
|
|
71
348
|
// Directory not readable - return empty
|
|
@@ -74,18 +351,60 @@ export async function discoverSocketSessions(socketDir) {
|
|
|
74
351
|
return sessions;
|
|
75
352
|
}
|
|
76
353
|
/**
|
|
77
|
-
*
|
|
354
|
+
* Remove the files of a session that can never accept a connection again.
|
|
78
355
|
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
356
|
+
* Best effort: a concurrent reaper or a permission problem must never turn a
|
|
357
|
+
* discovery call into a failure.
|
|
81
358
|
*/
|
|
82
|
-
function
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
359
|
+
async function reapStaleSession(...paths) {
|
|
360
|
+
for (const target of paths) {
|
|
361
|
+
try {
|
|
362
|
+
await rm(target, { force: true });
|
|
363
|
+
}
|
|
364
|
+
catch { }
|
|
86
365
|
}
|
|
87
|
-
|
|
88
|
-
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Remove `.lock` files whose `.sock` is already gone and whose owner is provably
|
|
369
|
+
* gone with it.
|
|
370
|
+
*
|
|
371
|
+
* Such locks are invisible to the `.sock` scan above, so without this they accumulate
|
|
372
|
+
* forever. A lock with no socket is also the normal state for a moment during startup -
|
|
373
|
+
* the lock is claimed before the socket is bound - so a lock whose PID is alive, or
|
|
374
|
+
* whose PID cannot be read at all, is always left alone. A lock belonging to another
|
|
375
|
+
* uid, or naming another user's live process, is left alone too (O1, O2).
|
|
376
|
+
*/
|
|
377
|
+
async function reapOrphanedLocks(socketDir, entries, sessionIdsWithSocket, uid, bootMs, tally) {
|
|
378
|
+
for (const entry of entries) {
|
|
379
|
+
if (!entry.endsWith(".lock"))
|
|
380
|
+
continue;
|
|
381
|
+
if (sessionIdsWithSocket.has(entry.slice(0, -".lock".length)))
|
|
382
|
+
continue;
|
|
383
|
+
const lockPath = path.join(socketDir, entry);
|
|
384
|
+
try {
|
|
385
|
+
const info = await lstat(lockPath);
|
|
386
|
+
if (!info.isFile() || !ownedByUs(info, uid)) {
|
|
387
|
+
if (!ownedByUs(info, uid))
|
|
388
|
+
tally.skipped.foreignLockUid++;
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
const lock = await readLock(lockPath);
|
|
392
|
+
// Unreadable ownership: never guess, leave it alone.
|
|
393
|
+
if (!lock)
|
|
394
|
+
continue;
|
|
395
|
+
const ownership = pidOwnership(lock.pid);
|
|
396
|
+
if (ownership === "dead" || lockIsPreBootDebris(lock, bootMs)) {
|
|
397
|
+
await rm(lockPath, { force: true });
|
|
398
|
+
if (ownership === "dead")
|
|
399
|
+
tally.reaped.orphanLockDeadPid++;
|
|
400
|
+
else
|
|
401
|
+
tally.reaped.orphanLockPreBoot++;
|
|
402
|
+
}
|
|
403
|
+
else if (ownership === "foreign") {
|
|
404
|
+
tally.skipped.foreignPid++;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
catch { }
|
|
89
408
|
}
|
|
90
409
|
}
|
|
91
410
|
//# sourceMappingURL=discovery.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"discovery.js","sourceRoot":"","sources":["../../../src/core/socket-server/discovery.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAC3D,OAAO,IAAI,MAAM,WAAW,CAAC;AAG7B;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAAC,SAAiB;IAC7D,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC;QAC5B,OAAO,EAAE,CAAC;IACX,CAAC;IAED,MAAM,QAAQ,GAAwB,EAAE,CAAC;IAEzC,IAAI,CAAC;QACJ,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC,SAAS,CAAC,CAAC;QAEzC,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;YAC7B,2BAA2B;YAC3B,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,OAAO,CAAC;gBAAE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AAC;YACJ,CAAC;YAAC,MAAM,CAAC,CAAA,CAAC;QACX,CAAC;IACF,CAAC;IAAC,MAAM,CAAC;QACR,wCAAwC;QACxC,OAAO,EAAE,CAAC;IACX,CAAC;IAED,OAAO,QAAQ,CAAC;AACjB,CAAC;AAED;;;;;GAKG;AACH,SAAS,gBAAgB,CAAC,GAAW;IACpC,IAAI,CAAC;QACJ,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;QACrB,OAAO,IAAI,CAAC;IACb,CAAC;IAAC,MAAM,CAAC;QACR,OAAO,KAAK,CAAC;IACd,CAAC;AACF,CAAC","sourcesContent":["/**\n * Socket Session Discovery\n *\n * Scans the sockets directory to find all running attachable sessions.\n */\n\nimport { existsSync } from \"node:fs\";\nimport { readdir, readFile, stat } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport type { SocketSessionInfo } from \"./types.js\";\n\n/**\n * Discover all running socket-based sessions.\n *\n * Scans the socket directory for .sock files and reads their corresponding\n * .lock files to extract metadata (PID, cwd, createdAt).\n *\n * Filters out stale sessions (PID no longer running).\n *\n * @param socketDir - Directory containing socket files\n * @returns Array of discovered session info\n */\nexport async function discoverSocketSessions(socketDir: string): Promise<SocketSessionInfo[]> {\n\tif (!existsSync(socketDir)) {\n\t\treturn [];\n\t}\n\n\tconst sessions: SocketSessionInfo[] = [];\n\n\ttry {\n\t\tconst entries = await readdir(socketDir);\n\n\t\tfor (const entry of entries) {\n\t\t\t// Only process .sock files\n\t\t\tif (!entry.endsWith(\".sock\")) continue;\n\n\t\t\tconst sessionId = entry.replace(/\\.sock$/, \"\");\n\t\t\tconst socketPath = path.join(socketDir, entry);\n\t\t\tconst lockPath = path.join(socketDir, `${sessionId}.lock`);\n\n\t\t\t// Check if socket file exists and is a socket\n\t\t\ttry {\n\t\t\t\tconst stats = await stat(socketPath);\n\t\t\t\tif (!stats.isSocket()) continue;\n\t\t\t} catch {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// Read lock file for metadata\n\t\t\tif (!existsSync(lockPath)) continue;\n\n\t\t\ttry {\n\t\t\t\tconst lockContent = await readFile(lockPath, \"utf-8\");\n\t\t\t\tconst lines = lockContent.trim().split(\"\\n\");\n\n\t\t\t\tif (lines.length < 3) continue;\n\n\t\t\t\tconst pid = Number.parseInt(lines[0], 10);\n\t\t\t\tconst cwd = lines[1];\n\t\t\t\tconst createdAt = new Date(lines[2]);\n\n\t\t\t\t// Verify PID is still running\n\t\t\t\tif (!isProcessRunning(pid)) {\n\t\t\t\t\t// Stale socket - skip it (cleanup could be done here)\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tsessions.push({\n\t\t\t\t\tsessionId,\n\t\t\t\t\tsocketPath,\n\t\t\t\t\tpid,\n\t\t\t\t\tcwd,\n\t\t\t\t\tcreatedAt,\n\t\t\t\t});\n\t\t\t} catch {}\n\t\t}\n\t} catch {\n\t\t// Directory not readable - return empty\n\t\treturn [];\n\t}\n\n\treturn sessions;\n}\n\n/**\n * Check if a process with the given PID is running.\n *\n * Uses `process.kill(pid, 0)` which doesn't actually send a signal,\n * but throws if the process doesn't exist.\n */\nfunction isProcessRunning(pid: number): boolean {\n\ttry {\n\t\tprocess.kill(pid, 0);\n\t\treturn true;\n\t} catch {\n\t\treturn false;\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"discovery.js","sourceRoot":"","sources":["../../../src/core/socket-server/discovery.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAGH,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAC;AAChE,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM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IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,GAAG,CAAC,EAAE,CAAC;gBAC7C,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,GAAG,CAAC;oBAAE,KAAK,CAAC,OAAO,CAAC,cAAc,EAAE,CAAC;gBAC1D,SAAS;YACV,CAAC;YACD,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,QAAQ,CAAC,CAAC;YACtC,qDAAqD;YACrD,IAAI,CAAC,IAAI;gBAAE,SAAS;YACpB,MAAM,SAAS,GAAG,YAAY,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YACzC,IAAI,SAAS,KAAK,MAAM,IAAI,mBAAmB,CAAC,IAAI,EAAE,MAAM,CAAC,EAAE,CAAC;gBAC/D,MAAM,EAAE,CAAC,QAAQ,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;gBACpC,IAAI,SAAS,KAAK,MAAM;oBAAE,KAAK,CAAC,MAAM,CAAC,iBAAiB,EAAE,CAAC;;oBACtD,KAAK,CAAC,MAAM,CAAC,iBAAiB,EAAE,CAAC;YACvC,CAAC;iBAAM,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;gBACpC,KAAK,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC;YAC5B,CAAC;QACF,CAAC;QAAC,MAAM,CAAC,CAAA,CAAC;IACX,CAAC;AACF,CAAC","sourcesContent":["/**\n * Socket Session Discovery\n *\n * Scans the sockets directory to find all running attachable sessions.\n *\n * ── OWNERSHIP AND HYGIENE RULES (R35-ALWAYS.3, R35-ALWAYS.4) ───────────────────\n *\n * These rules are implemented TWICE on purpose: here, and synchronously in\n * `packages/geist-core/src/attach/socket-sessions.ts`, which may not import this\n * package across the geist boundary gate. `scripts/check-geist-protocol.mjs`'s\n * mirror clause only covers the relayed FRAMES — it does not cover any of this —\n * so the two files have to be changed together by hand. They had already drifted\n * once (this file used to shield a lock from the orphan sweep before it had\n * established that `<id>.sock` was a socket at all); that drift is fixed here.\n *\n * O1. OURS OR INVISIBLE. An entry whose file uid is not this process's uid, or\n * whose lock names a live process we cannot signal (`kill` → EPERM, i.e. it\n * belongs to another user), is never listed and never dialled. Before this,\n * a world-writable sockets directory holding a 0666 socket and a lock naming\n * root's pid 1 was listed as a live attachable session by both readers and\n * would have been dialled by the bridge; the 0700 directory mode was the\n * only thing in the way, and nothing asserted it.\n * O2. FOREIGN IS NOT REAPED HERE. A read must not destroy what it cannot\n * interpret, and a fleet scan runs on an HTTP request. Foreign entries are\n * skipped. `SocketServer.start()` is the one place allowed to remove them,\n * and only after it has asserted the directory really is ours and owner-only\n * — which makes a foreign file inside it debris by construction.\n * O3. EPERM STILL MEANS ALIVE. `kill(pid, 0)` → EPERM says the process exists.\n * It is never read as dead, so another user's live session is never reaped.\n * What changed is that \"alive\" no longer implies \"ours\".\n * O4. DEBRIS. Two classes used to be immortal — a `.sock` with no `.lock`, and a\n * regular file named `<id>.sock` — because both readers `continue`d past\n * them without reaping. Two such orphans had been sitting in the real\n * `~/.draht/agent/sockets` since 2026-08-21. Both are now reaped once they\n * are older than {@link DEBRIS_GRACE_MS}, which no legitimate transient\n * state survives: the lock is claimed BEFORE the socket is bound, and\n * teardown removes the socket BEFORE the lock, so a socket without a lock is\n * never a normal intermediate state in either direction.\n * O5. A LOCK CANNOT PREDATE THE BOOT. See {@link lockIsPreBootDebris}.\n */\n\nimport type { Stats } from \"node:fs\";\nimport { existsSync } from \"node:fs\";\nimport { lstat, readdir, readFile, rm } from \"node:fs/promises\";\nimport { uptime } from \"node:os\";\nimport path from \"node:path\";\nimport { getSoakLog, SOAK_EVENTS } from \"../soak/soak-log.js\";\nimport type { SocketSessionInfo } from \"./types.js\";\n\n/**\n * How long a file has to sit untouched before it counts as debris rather than as\n * a transient another starter is in the middle of creating.\n *\n * Deliberately the same number as `UNWRITTEN_LOCK_STALE_MS` in `socket-server.ts`\n * and `DEBRIS_GRACE_MS` in geist-core's `socket-sessions.ts`. It is duplicated\n * rather than imported for the boundary reason at the top of this file.\n */\nexport const DEBRIS_GRACE_MS = 10_000;\n\n/**\n * Slack on the boot-time comparison in {@link lockIsPreBootDebris}.\n *\n * `Date.now()` is wall clock and `os.uptime()` is elapsed-since-boot, so their\n * difference moves whenever the clock is stepped. Five minutes is far more than\n * NTP ever steps in one go and far less than the gap between a lock written\n * before a reboot and the reboot itself.\n */\nconst PRE_BOOT_SLACK_MS = 300_000;\n\n/**\n * What one discovery pass removed, and what it deliberately did not.\n *\n * This is the shape of the `sockets_reaped` soak record (R35-ALWAYS.11), and the\n * reason it exists rather than a bare total: Phase 39 has to assert \"zero orphaned\n * .sock/.lock pairs after seven days\" FROM THE LOG, not from one directory listing\n * taken at verdict time — by then the evidence has been tidied away by the very\n * sweep being measured.\n *\n * THE DISTINCTION THAT EARNS ITS KEEP is `reaped.deadPid` against\n * `skipped.foreignPid`. {@link isProcessRunning} answers true on EPERM (O3), so a\n * pid recycled by ANOTHER uid pins its pair as live forever — nothing here can\n * clean that up, and nothing should: the uid checks (O1, O2) are what stop this\n * process from deleting another user's files, and the price of that safety is a\n * pair we can neither use nor remove. A soak that saw only a total would read that\n * immortal pair as a leak in the reaper. Split out, it reads as exactly what it\n * is, and a rising `skipped.foreignPid` on a machine with one user is a real\n * finding rather than noise.\n */\ninterface ReapTally {\n\t/** Files actually unlinked, by why. Counted in FILES, because that is what a directory listing counts. */\n\treaped: {\n\t\t/** A pair whose lock names a process that no longer exists. */\n\t\tdeadPid: number;\n\t\t/** A pair whose lock names a process that started before this machine last booted (O5). */\n\t\tpreBootLock: number;\n\t\t/** A socket with no lock at all, past the debris grace (O4, class i). */\n\t\tlocklessSocket: number;\n\t\t/** A regular file or symlink wearing a `.sock` name, past the debris grace (O4, class ii). */\n\t\tnotASocket: number;\n\t\t/** A lock whose socket is gone and whose owner is gone with it. */\n\t\torphanLockDeadPid: number;\n\t\t/** A lock whose socket is gone and which predates the last boot. */\n\t\torphanLockPreBoot: number;\n\t};\n\t/** Entries left strictly alone, by why. Never an error; each is a rule being obeyed. */\n\tskipped: {\n\t\t/** A `.sock` belonging to another uid (O1). */\n\t\tforeignSocketUid: number;\n\t\t/** A `.lock` belonging to another uid (O1). */\n\t\tforeignLockUid: number;\n\t\t/** Ours to read, but naming a LIVE process of another uid — EPERM (O2, O3). */\n\t\tforeignPid: number;\n\t};\n}\n\nfunction newReapTally(): ReapTally {\n\treturn {\n\t\treaped: {\n\t\t\tdeadPid: 0,\n\t\t\tpreBootLock: 0,\n\t\t\tlocklessSocket: 0,\n\t\t\tnotASocket: 0,\n\t\t\torphanLockDeadPid: 0,\n\t\t\torphanLockPreBoot: 0,\n\t\t},\n\t\tskipped: { foreignSocketUid: 0, foreignLockUid: 0, foreignPid: 0 },\n\t};\n}\n\n/** Total files unlinked in this pass. */\nfunction reapedFileCount(tally: ReapTally): number {\n\treturn Object.values(tally.reaped).reduce((total, count) => total + count, 0);\n}\n\n/** Total entries left alone for an ownership reason. */\nfunction skippedCount(tally: ReapTally): number {\n\treturn Object.values(tally.skipped).reduce((total, count) => total + count, 0);\n}\n\n/**\n * Publish one pass's tally, and only when it has something to say.\n *\n * A quiet sweep writes nothing: `discoverSocketSessions` runs on every session\n * start and on every fleet read, and a record per no-op scan would be the bulk of\n * a seven-day log. Silence here therefore means \"nothing was removed and nothing\n * was skipped\", which is the common case and the one a verdict does not need\n * evidence for.\n *\n * Never stdout, never stderr, never a throw — a hygiene sweep may not be what\n * breaks a session start.\n *\n * NO `sessionId` IS SUPPLIED HERE, on purpose. The writer stamps its own whenever\n * it has one, which it does for every session process — the id is set at bind,\n * before `SocketServer.start()` runs this sweep. The one caller that reaches this\n * function without a session of its own is `draht --list-sessions`, and a sweep\n * performed by a process that owns no session is not attributable to one; the\n * record is written without the field rather than with a borrowed value.\n */\nfunction recordReapTally(socketDir: string, tally: ReapTally, scanned: number, live: number): void {\n\tconst count = reapedFileCount(tally);\n\tconst skipped = skippedCount(tally);\n\tif (count === 0 && skipped === 0) return;\n\ttry {\n\t\tgetSoakLog().record(\n\t\t\tSOAK_EVENTS.SOCKETS_REAPED,\n\t\t\t{\n\t\t\t\t// FILES, not pairs: a dead session contributes its `.sock` and its `.lock`\n\t\t\t\t// separately, so this number can be compared against a `readdir` difference.\n\t\t\t\tcount,\n\t\t\t\treaped: tally.reaped,\n\t\t\t\tskipped: tally.skipped,\n\t\t\t\tskippedCount: skipped,\n\t\t\t\tsocketDir,\n\t\t\t\tscanned,\n\t\t\t\tlive,\n\t\t\t},\n\t\t\tcount > 0 ? \"warn\" : \"info\",\n\t\t);\n\t} catch {\n\t\t// Deliberately empty, and deliberately not logged anywhere.\n\t}\n}\n\n/** What this process may do about the process a lock names. */\nexport type PidOwnership =\n\t/** Alive and signallable by us: a session this user is running. */\n\t| \"ours\"\n\t/** Alive, but `kill` says EPERM — it belongs to another user. */\n\t| \"foreign\"\n\t/** No such process. */\n\t| \"dead\";\n\n/**\n * Who owns the process a lock names.\n *\n * `process.kill(pid, 0)` sends no signal; it only asks whether the pid could be\n * signalled. EPERM is the discriminator that costs nothing: a process this uid\n * could have started is always signallable by this uid, so EPERM means alive AND\n * not ours. That single distinction is what R35-ALWAYS.3 rests on — without it a\n * lock naming root's pid 1 reads as a live session of ours forever.\n */\nexport function pidOwnership(pid: number): PidOwnership {\n\tif (!Number.isInteger(pid) || pid <= 0) return \"dead\";\n\ttry {\n\t\tprocess.kill(pid, 0);\n\t\treturn \"ours\";\n\t} catch (error) {\n\t\treturn (error as NodeJS.ErrnoException)?.code === \"EPERM\" ? \"foreign\" : \"dead\";\n\t}\n}\n\n/**\n * Check if a process with the given PID is running.\n *\n * Uses `process.kill(pid, 0)` which doesn't actually send a signal,\n * but throws if the process doesn't exist.\n *\n * EPERM means the process exists but belongs to another user: alive, and still\n * reported as alive (O3). Callers that also care WHOSE it is ask\n * {@link pidOwnership} instead.\n */\nexport function isProcessRunning(pid: number): boolean {\n\treturn pidOwnership(pid) !== \"dead\";\n}\n\n/** This process's uid, or null off POSIX where the concept does not exist. */\nexport function currentUid(): number | null {\n\treturn typeof process.getuid === \"function\" ? process.getuid() : null;\n}\n\n/** Whether a directory entry belongs to this uid. Always true off POSIX. */\nfunction ownedByUs(info: Stats, uid: number | null): boolean {\n\treturn uid === null || info.uid === uid;\n}\n\n/** Whether a file has sat untouched long enough to count as debris. */\nfunction olderThanGrace(info: Stats, now: number): boolean {\n\treturn now - info.mtimeMs > DEBRIS_GRACE_MS;\n}\n\n/** Epoch milliseconds at which this machine booted. */\nfunction bootEpochMs(): number {\n\treturn Date.now() - uptime() * 1000;\n}\n\n/**\n * A lock recording a process that started before this machine last booted.\n *\n * This is the answer to \"bound readable-pid locks\" (R35-ALWAYS.4). The two\n * candidates were recording the owner's start time and comparing it, or putting a\n * blanket age bound on readable-pid locks. **A blanket age bound is rejected**: a\n * draht session legitimately runs for days, and reaping a live session's lock is\n * worse than the leak it cures — the id becomes claimable while the socket is\n * still bound, and two processes end up appending to one session JSONL.\n *\n * So the lock carries the owning process's start time as a 4th line, and the only\n * comparison made is the one that cannot produce a false positive on a live\n * session: nothing running now started before the last boot. That covers the\n * dominant real-world source of pid recycling — a reboot recycles every pid at\n * once, which is how a lock naming pid 1 comes to poison a session id forever.\n *\n * Line 3 (the ISO creation time) is used when line 4 is absent, so the locks\n * already on disk from older builds are covered too. Old 3-line locks still parse:\n * the length floor stays at 3.\n *\n * NOT covered: a same-uid pid recycled without a reboot. That needs the owner's\n * real start time from the OS, which costs a subprocess per lock on a path that\n * runs on every fleet read.\n */\nfunction lockIsPreBootDebris(lock: ParsedLock, bootMs: number): boolean {\n\treturn lock.startedAtMs !== null && lock.startedAtMs < bootMs - PRE_BOOT_SLACK_MS;\n}\n\ninterface ParsedLock {\n\tpid: number;\n\tcwd: string;\n\tcreatedAt: Date;\n\t/** Owner's process start time in epoch ms, from line 4, or line 3 as a floor. */\n\tstartedAtMs: number | null;\n}\n\n/** Parse a lock file's contents, or null when it says nothing trustworthy. */\nfunction parseLock(contents: string): ParsedLock | null {\n\tconst lines = contents.trim().split(\"\\n\");\n\t// Three lines is still the contract. A 4th line is additive; a build that does\n\t// not write one is read exactly as before.\n\tif (lines.length < 3) return null;\n\tconst pid = Number.parseInt(lines[0], 10);\n\tif (!Number.isInteger(pid) || pid <= 0) return null;\n\tconst createdAt = new Date(lines[2]);\n\tconst fromLine4 = lines.length >= 4 ? Number.parseInt(lines[3], 10) : Number.NaN;\n\tconst startedAtMs = Number.isFinite(fromLine4)\n\t\t? fromLine4\n\t\t: Number.isFinite(createdAt.getTime())\n\t\t\t? createdAt.getTime()\n\t\t\t: null;\n\treturn { pid, cwd: lines[1], createdAt, startedAtMs };\n}\n\n/** Read and parse a lock file, or null when it is missing or half-written. */\nasync function readLock(lockPath: string): Promise<ParsedLock | null> {\n\ttry {\n\t\treturn parseLock(await readFile(lockPath, \"utf-8\"));\n\t} catch {\n\t\treturn null;\n\t}\n}\n\n/**\n * Discover all running socket-based sessions.\n *\n * Scans the socket directory for .sock files and reads their corresponding\n * .lock files to extract metadata (PID, cwd, createdAt).\n *\n * Only sessions belonging to this uid are returned, and reaping follows the rules\n * at the top of this file: dead-owner pairs, pre-boot locks, lockless sockets and\n * non-socket `<id>.sock` files go; anything foreign, fresh, or unreadable stays.\n *\n * @param socketDir - Directory containing socket files\n * @returns Array of discovered session info\n */\nexport async function discoverSocketSessions(socketDir: string): Promise<SocketSessionInfo[]> {\n\tif (!existsSync(socketDir)) {\n\t\treturn [];\n\t}\n\n\tconst sessions: SocketSessionInfo[] = [];\n\tconst uid = currentUid();\n\tconst now = Date.now();\n\tconst bootMs = bootEpochMs();\n\tconst tally = newReapTally();\n\n\ttry {\n\t\tconst entries = await readdir(socketDir);\n\t\tconst sessionIdsWithSocket = new Set<string>();\n\n\t\tfor (const entry of entries) {\n\t\t\t// Only process .sock files\n\t\t\tif (!entry.endsWith(\".sock\")) continue;\n\n\t\t\tconst sessionId = entry.slice(0, -\".sock\".length);\n\t\t\tif (sessionId.length === 0) continue;\n\t\t\tconst socketPath = path.join(socketDir, entry);\n\t\t\tconst lockPath = path.join(socketDir, `${sessionId}.lock`);\n\n\t\t\t// lstat, not stat: a symlink named `<id>.sock` is not a socket we published,\n\t\t\t// and following it would let something outside the directory decide what this\n\t\t\t// scan sees.\n\t\t\tlet socketInfo: Stats;\n\t\t\ttry {\n\t\t\t\tsocketInfo = await lstat(socketPath);\n\t\t\t} catch {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (!socketInfo.isSocket()) {\n\t\t\t\t// Debris class (ii): a regular file (or symlink) wearing a .sock name.\n\t\t\t\t// Neither reader could ever remove one before. Directories are left alone —\n\t\t\t\t// removing a tree is not this function's business.\n\t\t\t\tif (!socketInfo.isDirectory() && ownedByUs(socketInfo, uid) && olderThanGrace(socketInfo, now)) {\n\t\t\t\t\tawait reapStaleSession(socketPath);\n\t\t\t\t\ttally.reaped.notASocket++;\n\t\t\t\t}\n\t\t\t\t// Deliberately NOT added to sessionIdsWithSocket: this id has no socket, so\n\t\t\t\t// its lock belongs to the orphan sweep below. Shielding it here (the old\n\t\t\t\t// behaviour, and the drift from geist-core) is what made class (ii) immortal.\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// O1: another user's socket is not ours to list, dial, or delete.\n\t\t\tif (!ownedByUs(socketInfo, uid)) {\n\t\t\t\ttally.skipped.foreignSocketUid++;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tsessionIdsWithSocket.add(sessionId);\n\n\t\t\tlet lockInfo: Stats | null;\n\t\t\ttry {\n\t\t\t\tlockInfo = await lstat(lockPath);\n\t\t\t} catch {\n\t\t\t\tlockInfo = null;\n\t\t\t}\n\n\t\t\tif (lockInfo === null) {\n\t\t\t\t// Debris class (i): a socket with no lock. Never a normal transient — the\n\t\t\t\t// lock is claimed before the bind, and teardown removes the socket first.\n\t\t\t\tif (olderThanGrace(socketInfo, now)) {\n\t\t\t\t\tawait reapStaleSession(socketPath);\n\t\t\t\t\ttally.reaped.locklessSocket++;\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t// A lock that is not a plain file, or is not ours, is not evidence about a\n\t\t\t// session of ours. Left strictly alone.\n\t\t\tif (!lockInfo.isFile() || !ownedByUs(lockInfo, uid)) {\n\t\t\t\tif (!ownedByUs(lockInfo, uid)) tally.skipped.foreignLockUid++;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst lock = await readLock(lockPath);\n\t\t\t// Unreadable or half-written: ownership unknown, never guess.\n\t\t\tif (!lock) continue;\n\n\t\t\tconst ownership = pidOwnership(lock.pid);\n\t\t\tif (ownership === \"dead\" || lockIsPreBootDebris(lock, bootMs)) {\n\t\t\t\tawait reapStaleSession(socketPath, lockPath);\n\t\t\t\t// Two files, and counted as two: this number is compared against a\n\t\t\t\t// directory listing, which cannot see pairs.\n\t\t\t\tif (ownership === \"dead\") tally.reaped.deadPid += 2;\n\t\t\t\telse tally.reaped.preBootLock += 2;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t// Alive, but another user's process: never listed (O1), never reaped (O2).\n\t\t\tif (ownership === \"foreign\") {\n\t\t\t\ttally.skipped.foreignPid++;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tsessions.push({\n\t\t\t\tsessionId,\n\t\t\t\tsocketPath,\n\t\t\t\tpid: lock.pid,\n\t\t\t\tcwd: lock.cwd,\n\t\t\t\tcreatedAt: lock.createdAt,\n\t\t\t});\n\t\t}\n\n\t\tawait reapOrphanedLocks(socketDir, entries, sessionIdsWithSocket, uid, bootMs, tally);\n\t\trecordReapTally(socketDir, tally, entries.length, sessions.length);\n\t} catch {\n\t\t// Directory not readable - return empty\n\t\treturn [];\n\t}\n\n\treturn sessions;\n}\n\n/**\n * Remove the files of a session that can never accept a connection again.\n *\n * Best effort: a concurrent reaper or a permission problem must never turn a\n * discovery call into a failure.\n */\nasync function reapStaleSession(...paths: string[]): Promise<void> {\n\tfor (const target of paths) {\n\t\ttry {\n\t\t\tawait rm(target, { force: true });\n\t\t} catch {}\n\t}\n}\n\n/**\n * Remove `.lock` files whose `.sock` is already gone and whose owner is provably\n * gone with it.\n *\n * Such locks are invisible to the `.sock` scan above, so without this they accumulate\n * forever. A lock with no socket is also the normal state for a moment during startup -\n * the lock is claimed before the socket is bound - so a lock whose PID is alive, or\n * whose PID cannot be read at all, is always left alone. A lock belonging to another\n * uid, or naming another user's live process, is left alone too (O1, O2).\n */\nasync function reapOrphanedLocks(\n\tsocketDir: string,\n\tentries: string[],\n\tsessionIdsWithSocket: Set<string>,\n\tuid: number | null,\n\tbootMs: number,\n\ttally: ReapTally,\n): Promise<void> {\n\tfor (const entry of entries) {\n\t\tif (!entry.endsWith(\".lock\")) continue;\n\t\tif (sessionIdsWithSocket.has(entry.slice(0, -\".lock\".length))) continue;\n\n\t\tconst lockPath = path.join(socketDir, entry);\n\t\ttry {\n\t\t\tconst info = await lstat(lockPath);\n\t\t\tif (!info.isFile() || !ownedByUs(info, uid)) {\n\t\t\t\tif (!ownedByUs(info, uid)) tally.skipped.foreignLockUid++;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst lock = await readLock(lockPath);\n\t\t\t// Unreadable ownership: never guess, leave it alone.\n\t\t\tif (!lock) continue;\n\t\t\tconst ownership = pidOwnership(lock.pid);\n\t\t\tif (ownership === \"dead\" || lockIsPreBootDebris(lock, bootMs)) {\n\t\t\t\tawait rm(lockPath, { force: true });\n\t\t\t\tif (ownership === \"dead\") tally.reaped.orphanLockDeadPid++;\n\t\t\t\telse tally.reaped.orphanLockPreBoot++;\n\t\t\t} else if (ownership === \"foreign\") {\n\t\t\t\ttally.skipped.foreignPid++;\n\t\t\t}\n\t\t} catch {}\n\t}\n}\n"]}
|
|
@@ -3,11 +3,17 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Enables tmux-style multi-client attachment to draht sessions.
|
|
5
5
|
*/
|
|
6
|
-
export { discoverSocketSessions } from "./discovery.js";
|
|
7
|
-
export type {
|
|
8
|
-
export {
|
|
6
|
+
export { discoverSocketSessions, isProcessRunning } from "./discovery.js";
|
|
7
|
+
export type { DeliverableAsk, PermissionDeliveryOptions } from "./permission-delivery.js";
|
|
8
|
+
export { PermissionDelivery } from "./permission-delivery.js";
|
|
9
|
+
export type { PermissionEntry, PermissionInsert, PermissionRegistryOptions, RegisteredOption, SettleResult, TerminalDecision, } from "./permission-registry.js";
|
|
10
|
+
export { DEFAULT_PERMISSION_EXPIRY_MS, MAX_PERMISSION_EXPIRY_MS, MIN_PERMISSION_EXPIRY_MS, PERMISSION_EXPIRY_ENV, PermissionRegistry, resolvePermissionExpiryMs, } from "./permission-registry.js";
|
|
11
|
+
export type { PermissionRecorder, PermissionSocketServer, SocketPermissionRelay, SocketPermissionRelayOptions, } from "./permission-relay.js";
|
|
12
|
+
export { createSocketPermissionRelay } from "./permission-relay.js";
|
|
13
|
+
export type { AttachableSession, AttachableSessionOptions } from "./session-integration.js";
|
|
14
|
+
export { makeSessionAttachable, registerAttachableSessionCleanup } from "./session-integration.js";
|
|
9
15
|
export { SocketClient } from "./socket-client.js";
|
|
10
16
|
export type { SocketServerOptions } from "./socket-server.js";
|
|
11
|
-
export { SocketServer } from "./socket-server.js";
|
|
17
|
+
export { DEFAULT_MAX_LIVE_SOCKETS, SocketCapReachedError, SocketDirectoryUnsafeError, SocketServer, SocketSessionBusyError, } from "./socket-server.js";
|
|
12
18
|
export * from "./types.js";
|
|
13
19
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core/socket-server/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core/socket-server/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,sBAAsB,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAC1E,YAAY,EAAE,cAAc,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAC;AAC1F,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAC9D,YAAY,EACX,eAAe,EACf,gBAAgB,EAChB,yBAAyB,EACzB,gBAAgB,EAChB,YAAY,EACZ,gBAAgB,GAChB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EACN,4BAA4B,EAC5B,wBAAwB,EACxB,wBAAwB,EACxB,qBAAqB,EACrB,kBAAkB,EAClB,yBAAyB,GACzB,MAAM,0BAA0B,CAAC;AAClC,YAAY,EACX,kBAAkB,EAClB,sBAAsB,EACtB,qBAAqB,EACrB,4BAA4B,GAC5B,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,2BAA2B,EAAE,MAAM,uBAAuB,CAAC;AACpE,YAAY,EAAE,iBAAiB,EAAE,wBAAwB,EAAE,MAAM,0BAA0B,CAAC;AAC5F,OAAO,EAAE,qBAAqB,EAAE,gCAAgC,EAAE,MAAM,0BAA0B,CAAC;AACnG,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,YAAY,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,EACN,wBAAwB,EACxB,qBAAqB,EACrB,0BAA0B,EAC1B,YAAY,EACZ,sBAAsB,GACtB,MAAM,oBAAoB,CAAC;AAC5B,cAAc,YAAY,CAAC"}
|
|
@@ -3,9 +3,12 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Enables tmux-style multi-client attachment to draht sessions.
|
|
5
5
|
*/
|
|
6
|
-
export { discoverSocketSessions } from "./discovery.js";
|
|
7
|
-
export {
|
|
6
|
+
export { discoverSocketSessions, isProcessRunning } from "./discovery.js";
|
|
7
|
+
export { PermissionDelivery } from "./permission-delivery.js";
|
|
8
|
+
export { DEFAULT_PERMISSION_EXPIRY_MS, MAX_PERMISSION_EXPIRY_MS, MIN_PERMISSION_EXPIRY_MS, PERMISSION_EXPIRY_ENV, PermissionRegistry, resolvePermissionExpiryMs, } from "./permission-registry.js";
|
|
9
|
+
export { createSocketPermissionRelay } from "./permission-relay.js";
|
|
10
|
+
export { makeSessionAttachable, registerAttachableSessionCleanup } from "./session-integration.js";
|
|
8
11
|
export { SocketClient } from "./socket-client.js";
|
|
9
|
-
export { SocketServer } from "./socket-server.js";
|
|
12
|
+
export { DEFAULT_MAX_LIVE_SOCKETS, SocketCapReachedError, SocketDirectoryUnsafeError, SocketServer, SocketSessionBusyError, } from "./socket-server.js";
|
|
10
13
|
export * from "./types.js";
|
|
11
14
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/core/socket-server/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/core/socket-server/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,sBAAsB,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAE1E,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAS9D,OAAO,EACN,4BAA4B,EAC5B,wBAAwB,EACxB,wBAAwB,EACxB,qBAAqB,EACrB,kBAAkB,EAClB,yBAAyB,GACzB,MAAM,0BAA0B,CAAC;AAOlC,OAAO,EAAE,2BAA2B,EAAE,MAAM,uBAAuB,CAAC;AAEpE,OAAO,EAAE,qBAAqB,EAAE,gCAAgC,EAAE,MAAM,0BAA0B,CAAC;AACnG,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD,OAAO,EACN,wBAAwB,EACxB,qBAAqB,EACrB,0BAA0B,EAC1B,YAAY,EACZ,sBAAsB,GACtB,MAAM,oBAAoB,CAAC;AAC5B,cAAc,YAAY,CAAC","sourcesContent":["/**\n * Socket Server - Attachable session infrastructure\n *\n * Enables tmux-style multi-client attachment to draht sessions.\n */\n\nexport { discoverSocketSessions, isProcessRunning } from \"./discovery.js\";\nexport type { DeliverableAsk, PermissionDeliveryOptions } from \"./permission-delivery.js\";\nexport { PermissionDelivery } from \"./permission-delivery.js\";\nexport type {\n\tPermissionEntry,\n\tPermissionInsert,\n\tPermissionRegistryOptions,\n\tRegisteredOption,\n\tSettleResult,\n\tTerminalDecision,\n} from \"./permission-registry.js\";\nexport {\n\tDEFAULT_PERMISSION_EXPIRY_MS,\n\tMAX_PERMISSION_EXPIRY_MS,\n\tMIN_PERMISSION_EXPIRY_MS,\n\tPERMISSION_EXPIRY_ENV,\n\tPermissionRegistry,\n\tresolvePermissionExpiryMs,\n} from \"./permission-registry.js\";\nexport type {\n\tPermissionRecorder,\n\tPermissionSocketServer,\n\tSocketPermissionRelay,\n\tSocketPermissionRelayOptions,\n} from \"./permission-relay.js\";\nexport { createSocketPermissionRelay } from \"./permission-relay.js\";\nexport type { AttachableSession, AttachableSessionOptions } from \"./session-integration.js\";\nexport { makeSessionAttachable, registerAttachableSessionCleanup } from \"./session-integration.js\";\nexport { SocketClient } from \"./socket-client.js\";\nexport type { SocketServerOptions } from \"./socket-server.js\";\nexport {\n\tDEFAULT_MAX_LIVE_SOCKETS,\n\tSocketCapReachedError,\n\tSocketDirectoryUnsafeError,\n\tSocketServer,\n\tSocketSessionBusyError,\n} from \"./socket-server.js\";\nexport * from \"./types.js\";\n"]}
|