@kruntime/komputer 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +368 -0
- package/dist/agent/approvals.d.ts +28 -0
- package/dist/agent/approvals.js +109 -0
- package/dist/agent/approvals.js.map +1 -0
- package/dist/agent/messages.d.ts +67 -0
- package/dist/agent/messages.js +213 -0
- package/dist/agent/messages.js.map +1 -0
- package/dist/agent/recovery.d.ts +13 -0
- package/dist/agent/recovery.js +112 -0
- package/dist/agent/recovery.js.map +1 -0
- package/dist/agent/session-utils.d.ts +17 -0
- package/dist/agent/session-utils.js +77 -0
- package/dist/agent/session-utils.js.map +1 -0
- package/dist/agent-session.d.ts +303 -0
- package/dist/agent-session.js +3031 -0
- package/dist/agent-session.js.map +1 -0
- package/dist/authority.d.ts +34 -0
- package/dist/authority.js +105 -0
- package/dist/authority.js.map +1 -0
- package/dist/content.d.ts +17 -0
- package/dist/content.js +69 -0
- package/dist/content.js.map +1 -0
- package/dist/cron.d.ts +20 -0
- package/dist/cron.js +25 -0
- package/dist/cron.js.map +1 -0
- package/dist/drive.d.ts +198 -0
- package/dist/drive.js +656 -0
- package/dist/drive.js.map +1 -0
- package/dist/events.d.ts +20 -0
- package/dist/events.js +149 -0
- package/dist/events.js.map +1 -0
- package/dist/fs-mode.d.ts +9 -0
- package/dist/fs-mode.js +98 -0
- package/dist/fs-mode.js.map +1 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/komputer.d.ts +123 -0
- package/dist/komputer.js +733 -0
- package/dist/komputer.js.map +1 -0
- package/dist/kstate.d.ts +107 -0
- package/dist/kstate.js +529 -0
- package/dist/kstate.js.map +1 -0
- package/dist/model.d.ts +81 -0
- package/dist/model.js +824 -0
- package/dist/model.js.map +1 -0
- package/dist/node.d.ts +2 -0
- package/dist/node.js +3 -0
- package/dist/node.js.map +1 -0
- package/dist/path.d.ts +4 -0
- package/dist/path.js +38 -0
- package/dist/path.js.map +1 -0
- package/dist/pool.d.ts +56 -0
- package/dist/pool.js +172 -0
- package/dist/pool.js.map +1 -0
- package/dist/receipts.d.ts +44 -0
- package/dist/receipts.js +35 -0
- package/dist/receipts.js.map +1 -0
- package/dist/remote-node.d.ts +2 -0
- package/dist/remote-node.js +122 -0
- package/dist/remote-node.js.map +1 -0
- package/dist/remote.d.ts +84 -0
- package/dist/remote.js +11 -0
- package/dist/remote.js.map +1 -0
- package/dist/sandbox.d.ts +4 -0
- package/dist/sandbox.js +70 -0
- package/dist/sandbox.js.map +1 -0
- package/dist/shell/builtins/catalog.d.ts +6 -0
- package/dist/shell/builtins/catalog.js +12 -0
- package/dist/shell/builtins/catalog.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/basename.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/basename.js +23 -0
- package/dist/shell/builtins/commands/filesystem/basename.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/cat.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/cat.js +85 -0
- package/dist/shell/builtins/commands/filesystem/cat.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/cd.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/cd.js +14 -0
- package/dist/shell/builtins/commands/filesystem/cd.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/chmod.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/chmod.js +46 -0
- package/dist/shell/builtins/commands/filesystem/chmod.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/copy-directory.d.ts +4 -0
- package/dist/shell/builtins/commands/filesystem/copy-directory.js +48 -0
- package/dist/shell/builtins/commands/filesystem/copy-directory.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/cp.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/cp.js +92 -0
- package/dist/shell/builtins/commands/filesystem/cp.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/dirname.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/dirname.js +23 -0
- package/dist/shell/builtins/commands/filesystem/dirname.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/du.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/du.js +93 -0
- package/dist/shell/builtins/commands/filesystem/du.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/find.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/find.js +111 -0
- package/dist/shell/builtins/commands/filesystem/find.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/install.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/install.js +89 -0
- package/dist/shell/builtins/commands/filesystem/install.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/ln.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/ln.js +57 -0
- package/dist/shell/builtins/commands/filesystem/ln.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/ls.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/ls.js +153 -0
- package/dist/shell/builtins/commands/filesystem/ls.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/mkdir.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/mkdir.js +41 -0
- package/dist/shell/builtins/commands/filesystem/mkdir.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/mktemp.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/mktemp.js +94 -0
- package/dist/shell/builtins/commands/filesystem/mktemp.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/mv.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/mv.js +41 -0
- package/dist/shell/builtins/commands/filesystem/mv.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/pwd.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/pwd.js +2 -0
- package/dist/shell/builtins/commands/filesystem/pwd.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/realpath.d.ts +3 -0
- package/dist/shell/builtins/commands/filesystem/realpath.js +61 -0
- package/dist/shell/builtins/commands/filesystem/realpath.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/rm.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/rm.js +38 -0
- package/dist/shell/builtins/commands/filesystem/rm.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/stat.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/stat.js +141 -0
- package/dist/shell/builtins/commands/filesystem/stat.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/touch.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/touch.js +143 -0
- package/dist/shell/builtins/commands/filesystem/touch.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem.js +44 -0
- package/dist/shell/builtins/commands/filesystem.js.map +1 -0
- package/dist/shell/builtins/commands/network/curl.d.ts +2 -0
- package/dist/shell/builtins/commands/network/curl.js +25 -0
- package/dist/shell/builtins/commands/network/curl.js.map +1 -0
- package/dist/shell/builtins/commands/network/http.d.ts +27 -0
- package/dist/shell/builtins/commands/network/http.js +226 -0
- package/dist/shell/builtins/commands/network/http.js.map +1 -0
- package/dist/shell/builtins/commands/network/wget.d.ts +2 -0
- package/dist/shell/builtins/commands/network/wget.js +32 -0
- package/dist/shell/builtins/commands/network/wget.js.map +1 -0
- package/dist/shell/builtins/commands/network.d.ts +2 -0
- package/dist/shell/builtins/commands/network.js +9 -0
- package/dist/shell/builtins/commands/network.js.map +1 -0
- package/dist/shell/builtins/commands/process/bg.d.ts +2 -0
- package/dist/shell/builtins/commands/process/bg.js +10 -0
- package/dist/shell/builtins/commands/process/bg.js.map +1 -0
- package/dist/shell/builtins/commands/process/fg.d.ts +2 -0
- package/dist/shell/builtins/commands/process/fg.js +14 -0
- package/dist/shell/builtins/commands/process/fg.js.map +1 -0
- package/dist/shell/builtins/commands/process/jobs.d.ts +2 -0
- package/dist/shell/builtins/commands/process/jobs.js +8 -0
- package/dist/shell/builtins/commands/process/jobs.js.map +1 -0
- package/dist/shell/builtins/commands/process/jobspec.d.ts +4 -0
- package/dist/shell/builtins/commands/process/jobspec.js +20 -0
- package/dist/shell/builtins/commands/process/jobspec.js.map +1 -0
- package/dist/shell/builtins/commands/process/kill.d.ts +2 -0
- package/dist/shell/builtins/commands/process/kill.js +35 -0
- package/dist/shell/builtins/commands/process/kill.js.map +1 -0
- package/dist/shell/builtins/commands/process/ps.d.ts +2 -0
- package/dist/shell/builtins/commands/process/ps.js +6 -0
- package/dist/shell/builtins/commands/process/ps.js.map +1 -0
- package/dist/shell/builtins/commands/process/sleep.d.ts +2 -0
- package/dist/shell/builtins/commands/process/sleep.js +6 -0
- package/dist/shell/builtins/commands/process/sleep.js.map +1 -0
- package/dist/shell/builtins/commands/process/wait.d.ts +2 -0
- package/dist/shell/builtins/commands/process/wait.js +21 -0
- package/dist/shell/builtins/commands/process/wait.js.map +1 -0
- package/dist/shell/builtins/commands/process.d.ts +2 -0
- package/dist/shell/builtins/commands/process.js +19 -0
- package/dist/shell/builtins/commands/process.js.map +1 -0
- package/dist/shell/builtins/commands/system/command.d.ts +2 -0
- package/dist/shell/builtins/commands/system/command.js +42 -0
- package/dist/shell/builtins/commands/system/command.js.map +1 -0
- package/dist/shell/builtins/commands/system/date.d.ts +2 -0
- package/dist/shell/builtins/commands/system/date.js +121 -0
- package/dist/shell/builtins/commands/system/date.js.map +1 -0
- package/dist/shell/builtins/commands/system/env.d.ts +2 -0
- package/dist/shell/builtins/commands/system/env.js +84 -0
- package/dist/shell/builtins/commands/system/env.js.map +1 -0
- package/dist/shell/builtins/commands/system/export.d.ts +2 -0
- package/dist/shell/builtins/commands/system/export.js +18 -0
- package/dist/shell/builtins/commands/system/export.js.map +1 -0
- package/dist/shell/builtins/commands/system/false.d.ts +2 -0
- package/dist/shell/builtins/commands/system/false.js +4 -0
- package/dist/shell/builtins/commands/system/false.js.map +1 -0
- package/dist/shell/builtins/commands/system/help.d.ts +2 -0
- package/dist/shell/builtins/commands/system/help.js +38 -0
- package/dist/shell/builtins/commands/system/help.js.map +1 -0
- package/dist/shell/builtins/commands/system/hostname.d.ts +2 -0
- package/dist/shell/builtins/commands/system/hostname.js +6 -0
- package/dist/shell/builtins/commands/system/hostname.js.map +1 -0
- package/dist/shell/builtins/commands/system/id.d.ts +2 -0
- package/dist/shell/builtins/commands/system/id.js +55 -0
- package/dist/shell/builtins/commands/system/id.js.map +1 -0
- package/dist/shell/builtins/commands/system/printenv.d.ts +2 -0
- package/dist/shell/builtins/commands/system/printenv.js +22 -0
- package/dist/shell/builtins/commands/system/printenv.js.map +1 -0
- package/dist/shell/builtins/commands/system/read.d.ts +2 -0
- package/dist/shell/builtins/commands/system/read.js +75 -0
- package/dist/shell/builtins/commands/system/read.js.map +1 -0
- package/dist/shell/builtins/commands/system/resolve-command.d.ts +3 -0
- package/dist/shell/builtins/commands/system/resolve-command.js +34 -0
- package/dist/shell/builtins/commands/system/resolve-command.js.map +1 -0
- package/dist/shell/builtins/commands/system/set.d.ts +2 -0
- package/dist/shell/builtins/commands/system/set.js +49 -0
- package/dist/shell/builtins/commands/system/set.js.map +1 -0
- package/dist/shell/builtins/commands/system/sh.d.ts +2 -0
- package/dist/shell/builtins/commands/system/sh.js +55 -0
- package/dist/shell/builtins/commands/system/sh.js.map +1 -0
- package/dist/shell/builtins/commands/system/source.d.ts +2 -0
- package/dist/shell/builtins/commands/system/source.js +42 -0
- package/dist/shell/builtins/commands/system/source.js.map +1 -0
- package/dist/shell/builtins/commands/system/test-expression.d.ts +2 -0
- package/dist/shell/builtins/commands/system/test-expression.js +93 -0
- package/dist/shell/builtins/commands/system/test-expression.js.map +1 -0
- package/dist/shell/builtins/commands/system/test.d.ts +4 -0
- package/dist/shell/builtins/commands/system/test.js +19 -0
- package/dist/shell/builtins/commands/system/test.js.map +1 -0
- package/dist/shell/builtins/commands/system/true.d.ts +2 -0
- package/dist/shell/builtins/commands/system/true.js +2 -0
- package/dist/shell/builtins/commands/system/true.js.map +1 -0
- package/dist/shell/builtins/commands/system/type.d.ts +2 -0
- package/dist/shell/builtins/commands/system/type.js +77 -0
- package/dist/shell/builtins/commands/system/type.js.map +1 -0
- package/dist/shell/builtins/commands/system/uname.d.ts +2 -0
- package/dist/shell/builtins/commands/system/uname.js +39 -0
- package/dist/shell/builtins/commands/system/uname.js.map +1 -0
- package/dist/shell/builtins/commands/system/unset.d.ts +2 -0
- package/dist/shell/builtins/commands/system/unset.js +9 -0
- package/dist/shell/builtins/commands/system/unset.js.map +1 -0
- package/dist/shell/builtins/commands/system/which.d.ts +2 -0
- package/dist/shell/builtins/commands/system/which.js +38 -0
- package/dist/shell/builtins/commands/system/which.js.map +1 -0
- package/dist/shell/builtins/commands/system/whoami.d.ts +2 -0
- package/dist/shell/builtins/commands/system/whoami.js +6 -0
- package/dist/shell/builtins/commands/system/whoami.js.map +1 -0
- package/dist/shell/builtins/commands/system.d.ts +2 -0
- package/dist/shell/builtins/commands/system.js +49 -0
- package/dist/shell/builtins/commands/system.js.map +1 -0
- package/dist/shell/builtins/commands/text/awk.d.ts +2 -0
- package/dist/shell/builtins/commands/text/awk.js +8 -0
- package/dist/shell/builtins/commands/text/awk.js.map +1 -0
- package/dist/shell/builtins/commands/text/base64.d.ts +2 -0
- package/dist/shell/builtins/commands/text/base64.js +69 -0
- package/dist/shell/builtins/commands/text/base64.js.map +1 -0
- package/dist/shell/builtins/commands/text/cut.d.ts +2 -0
- package/dist/shell/builtins/commands/text/cut.js +35 -0
- package/dist/shell/builtins/commands/text/cut.js.map +1 -0
- package/dist/shell/builtins/commands/text/diff.d.ts +2 -0
- package/dist/shell/builtins/commands/text/diff.js +83 -0
- package/dist/shell/builtins/commands/text/diff.js.map +1 -0
- package/dist/shell/builtins/commands/text/echo.d.ts +2 -0
- package/dist/shell/builtins/commands/text/echo.js +6 -0
- package/dist/shell/builtins/commands/text/echo.js.map +1 -0
- package/dist/shell/builtins/commands/text/grep.d.ts +2 -0
- package/dist/shell/builtins/commands/text/grep.js +123 -0
- package/dist/shell/builtins/commands/text/grep.js.map +1 -0
- package/dist/shell/builtins/commands/text/head.d.ts +2 -0
- package/dist/shell/builtins/commands/text/head.js +8 -0
- package/dist/shell/builtins/commands/text/head.js.map +1 -0
- package/dist/shell/builtins/commands/text/jq.d.ts +2 -0
- package/dist/shell/builtins/commands/text/jq.js +239 -0
- package/dist/shell/builtins/commands/text/jq.js.map +1 -0
- package/dist/shell/builtins/commands/text/printf.d.ts +2 -0
- package/dist/shell/builtins/commands/text/printf.js +3 -0
- package/dist/shell/builtins/commands/text/printf.js.map +1 -0
- package/dist/shell/builtins/commands/text/sed.d.ts +2 -0
- package/dist/shell/builtins/commands/text/sed.js +21 -0
- package/dist/shell/builtins/commands/text/sed.js.map +1 -0
- package/dist/shell/builtins/commands/text/sha256sum.d.ts +2 -0
- package/dist/shell/builtins/commands/text/sha256sum.js +63 -0
- package/dist/shell/builtins/commands/text/sha256sum.js.map +1 -0
- package/dist/shell/builtins/commands/text/sort.d.ts +2 -0
- package/dist/shell/builtins/commands/text/sort.js +134 -0
- package/dist/shell/builtins/commands/text/sort.js.map +1 -0
- package/dist/shell/builtins/commands/text/tail.d.ts +2 -0
- package/dist/shell/builtins/commands/text/tail.js +10 -0
- package/dist/shell/builtins/commands/text/tail.js.map +1 -0
- package/dist/shell/builtins/commands/text/tee.d.ts +2 -0
- package/dist/shell/builtins/commands/text/tee.js +29 -0
- package/dist/shell/builtins/commands/text/tee.js.map +1 -0
- package/dist/shell/builtins/commands/text/tr.d.ts +2 -0
- package/dist/shell/builtins/commands/text/tr.js +3 -0
- package/dist/shell/builtins/commands/text/tr.js.map +1 -0
- package/dist/shell/builtins/commands/text/uniq.d.ts +2 -0
- package/dist/shell/builtins/commands/text/uniq.js +8 -0
- package/dist/shell/builtins/commands/text/uniq.js.map +1 -0
- package/dist/shell/builtins/commands/text/wc.d.ts +2 -0
- package/dist/shell/builtins/commands/text/wc.js +30 -0
- package/dist/shell/builtins/commands/text/wc.js.map +1 -0
- package/dist/shell/builtins/commands/text/xargs.d.ts +2 -0
- package/dist/shell/builtins/commands/text/xargs.js +138 -0
- package/dist/shell/builtins/commands/text/xargs.js.map +1 -0
- package/dist/shell/builtins/commands/text.d.ts +2 -0
- package/dist/shell/builtins/commands/text.js +41 -0
- package/dist/shell/builtins/commands/text.js.map +1 -0
- package/dist/shell/builtins/help-text.d.ts +69 -0
- package/dist/shell/builtins/help-text.js +381 -0
- package/dist/shell/builtins/help-text.js.map +1 -0
- package/dist/shell/builtins/path.d.ts +28 -0
- package/dist/shell/builtins/path.js +184 -0
- package/dist/shell/builtins/path.js.map +1 -0
- package/dist/shell/builtins/registry.d.ts +3 -0
- package/dist/shell/builtins/registry.js +75 -0
- package/dist/shell/builtins/registry.js.map +1 -0
- package/dist/shell/builtins/run.d.ts +4 -0
- package/dist/shell/builtins/run.js +29 -0
- package/dist/shell/builtins/run.js.map +1 -0
- package/dist/shell/builtins/runtime.d.ts +83 -0
- package/dist/shell/builtins/runtime.js +46 -0
- package/dist/shell/builtins/runtime.js.map +1 -0
- package/dist/shell/builtins/text.d.ts +90 -0
- package/dist/shell/builtins/text.js +970 -0
- package/dist/shell/builtins/text.js.map +1 -0
- package/dist/shell/control.d.ts +31 -0
- package/dist/shell/control.js +232 -0
- package/dist/shell/control.js.map +1 -0
- package/dist/shell/expansion.d.ts +30 -0
- package/dist/shell/expansion.js +514 -0
- package/dist/shell/expansion.js.map +1 -0
- package/dist/shell/functions.d.ts +10 -0
- package/dist/shell/functions.js +108 -0
- package/dist/shell/functions.js.map +1 -0
- package/dist/shell/runtime.d.ts +44 -0
- package/dist/shell/runtime.js +276 -0
- package/dist/shell/runtime.js.map +1 -0
- package/dist/shell/time.d.ts +1 -0
- package/dist/shell/time.js +68 -0
- package/dist/shell/time.js.map +1 -0
- package/dist/state-backend.d.ts +61 -0
- package/dist/state-backend.js +275 -0
- package/dist/state-backend.js.map +1 -0
- package/dist/state-node.d.ts +2 -0
- package/dist/state-node.js +266 -0
- package/dist/state-node.js.map +1 -0
- package/dist/types.d.ts +462 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +5 -0
- package/dist/utils.js +117 -0
- package/dist/utils.js.map +1 -0
- package/dist/vbash.d.ts +40 -0
- package/dist/vbash.js +389 -0
- package/dist/vbash.js.map +1 -0
- package/dist/vfs.d.ts +64 -0
- package/dist/vfs.js +248 -0
- package/dist/vfs.js.map +1 -0
- package/dist/vprocess.d.ts +139 -0
- package/dist/vprocess.js +756 -0
- package/dist/vprocess.js.map +1 -0
- package/package.json +28 -0
package/dist/vprocess.js
ADDED
|
@@ -0,0 +1,756 @@
|
|
|
1
|
+
import { id, now, sleep } from "./utils.js";
|
|
2
|
+
const MAX_CAPTURED_PROCESS_OUTPUT_CHARS = 64 * 1024;
|
|
3
|
+
const MAX_PROCESS_OUTPUT_CHUNK_CHARS = 16 * 1024;
|
|
4
|
+
export class VProcessTable {
|
|
5
|
+
kstate;
|
|
6
|
+
sessionId;
|
|
7
|
+
eventLog;
|
|
8
|
+
emitEvent;
|
|
9
|
+
defaultRemote;
|
|
10
|
+
remoteByName;
|
|
11
|
+
active;
|
|
12
|
+
constructor(kstate, sessionId, eventLog, defaultRemote, remoteByName = () => undefined, emitEvent = (event) => eventLog.emit(event)) {
|
|
13
|
+
this.kstate = kstate;
|
|
14
|
+
this.sessionId = sessionId;
|
|
15
|
+
this.eventLog = eventLog;
|
|
16
|
+
this.emitEvent = emitEvent;
|
|
17
|
+
this.defaultRemote = defaultRemote;
|
|
18
|
+
this.remoteByName = remoteByName;
|
|
19
|
+
this.active = new Map();
|
|
20
|
+
}
|
|
21
|
+
processesName() {
|
|
22
|
+
return 'processes/processes.jsonl';
|
|
23
|
+
}
|
|
24
|
+
jobsName() {
|
|
25
|
+
return 'processes/jobs.jsonl';
|
|
26
|
+
}
|
|
27
|
+
stdoutName(vpid) {
|
|
28
|
+
return `processes/io/${vpid}.stdout.log`;
|
|
29
|
+
}
|
|
30
|
+
stderrName(vpid) {
|
|
31
|
+
return `processes/io/${vpid}.stderr.log`;
|
|
32
|
+
}
|
|
33
|
+
async spawnShell(script, { cwd, env, stdin, foreground = true, remote = this.defaultRemote, limits, sandbox } = {}) {
|
|
34
|
+
if (!remote.shell)
|
|
35
|
+
throw new Error(`remote ${remote.name} does not provide shell execution`);
|
|
36
|
+
if (!remote.shell.spawn)
|
|
37
|
+
return this.execShellWithoutProcess(script, { cwd, env, stdin, foreground, remote, limits, sandbox });
|
|
38
|
+
const vpid = id('vpid');
|
|
39
|
+
const jobId = id('job');
|
|
40
|
+
const pgrp = jobId;
|
|
41
|
+
const startedAt = now();
|
|
42
|
+
const proc = await remote.shell.spawn({
|
|
43
|
+
script,
|
|
44
|
+
cwd,
|
|
45
|
+
env,
|
|
46
|
+
stdin,
|
|
47
|
+
foreground,
|
|
48
|
+
limits,
|
|
49
|
+
sandbox,
|
|
50
|
+
});
|
|
51
|
+
const record = {
|
|
52
|
+
vpid,
|
|
53
|
+
jobId,
|
|
54
|
+
pgrp,
|
|
55
|
+
kind: 'remote',
|
|
56
|
+
status: 'running',
|
|
57
|
+
foreground,
|
|
58
|
+
cwd,
|
|
59
|
+
argv: ['bash', '-lc', script],
|
|
60
|
+
remote: { name: remote.name, ref: proc.ref || {}, fencingToken: id('fence') },
|
|
61
|
+
sandbox: sandbox || proc.sandbox ? { request: sandbox, attestation: proc.sandbox } : undefined,
|
|
62
|
+
startedAt,
|
|
63
|
+
restartPolicy: 'never',
|
|
64
|
+
};
|
|
65
|
+
const startupFailure = sandboxStartupFailure(sandbox, proc.sandbox);
|
|
66
|
+
if (startupFailure) {
|
|
67
|
+
await this.stopUntrustedProcess(remote, proc);
|
|
68
|
+
const endedAt = now();
|
|
69
|
+
const failed = {
|
|
70
|
+
...record,
|
|
71
|
+
status: 'failed',
|
|
72
|
+
endedAt,
|
|
73
|
+
exitCode: startupFailure.code,
|
|
74
|
+
signal: startupFailure.signal,
|
|
75
|
+
error: startupFailure.stderr,
|
|
76
|
+
};
|
|
77
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.processesName(), failed);
|
|
78
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.jobsName(), {
|
|
79
|
+
jobId,
|
|
80
|
+
pgrp,
|
|
81
|
+
command: script,
|
|
82
|
+
processIds: [vpid],
|
|
83
|
+
foreground,
|
|
84
|
+
status: 'failed',
|
|
85
|
+
startedAt,
|
|
86
|
+
endedAt,
|
|
87
|
+
});
|
|
88
|
+
return { vpid, jobId, background: false, ...startupFailure };
|
|
89
|
+
}
|
|
90
|
+
const startWrites = Promise.all([
|
|
91
|
+
this.kstate.appendSessionJsonl(this.sessionId, this.processesName(), record),
|
|
92
|
+
this.kstate.appendSessionJsonl(this.sessionId, this.jobsName(), {
|
|
93
|
+
jobId,
|
|
94
|
+
pgrp,
|
|
95
|
+
command: script,
|
|
96
|
+
processIds: [vpid],
|
|
97
|
+
foreground,
|
|
98
|
+
status: 'running',
|
|
99
|
+
startedAt,
|
|
100
|
+
}),
|
|
101
|
+
]);
|
|
102
|
+
let stdout = '';
|
|
103
|
+
let stderr = '';
|
|
104
|
+
let detached = false;
|
|
105
|
+
const stdoutLimit = outputLimit(limits?.stdoutChars ?? limits?.outputChars);
|
|
106
|
+
const stderrLimit = outputLimit(limits?.stderrChars ?? limits?.outputChars);
|
|
107
|
+
const stdoutTask = this.consumeProcessStream(proc.stdout, vpid, 'stdout', startWrites, (text) => { stdout = appendBoundedOutput(stdout, text, stdoutLimit); }, () => detached);
|
|
108
|
+
const stderrTask = this.consumeProcessStream(proc.stderr, vpid, 'stderr', startWrites, (text) => { stderr = appendBoundedOutput(stderr, text, stderrLimit); }, () => detached);
|
|
109
|
+
await startWrites;
|
|
110
|
+
const done = (async () => {
|
|
111
|
+
const result = enforceSandboxAttestation(sandbox, await proc.done);
|
|
112
|
+
await Promise.all([stdoutTask, stderrTask]);
|
|
113
|
+
if (detached)
|
|
114
|
+
return { code: 0, signal: 'SIGDETACH', stdout: '', stderr: '', sandbox: result.sandbox };
|
|
115
|
+
const status = result.code === 0 ? 'exited' : 'failed';
|
|
116
|
+
const endedAt = now();
|
|
117
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.processesName(), {
|
|
118
|
+
...record,
|
|
119
|
+
sandbox: { ...record.sandbox, attestation: result.sandbox },
|
|
120
|
+
status,
|
|
121
|
+
endedAt,
|
|
122
|
+
exitCode: result.code ?? null,
|
|
123
|
+
signal: result.signal ?? null,
|
|
124
|
+
});
|
|
125
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.jobsName(), {
|
|
126
|
+
jobId,
|
|
127
|
+
pgrp,
|
|
128
|
+
command: script,
|
|
129
|
+
processIds: [vpid],
|
|
130
|
+
foreground,
|
|
131
|
+
status,
|
|
132
|
+
startedAt,
|
|
133
|
+
endedAt,
|
|
134
|
+
});
|
|
135
|
+
return {
|
|
136
|
+
code: result.code ?? 1,
|
|
137
|
+
signal: result.signal ?? null,
|
|
138
|
+
stdout: appendBoundedOutput(stdout, result.stdout || '', stdoutLimit),
|
|
139
|
+
stderr: appendBoundedOutput(stderr, result.stderr || '', stderrLimit),
|
|
140
|
+
timedOut: result.timedOut,
|
|
141
|
+
sandbox: result.sandbox,
|
|
142
|
+
};
|
|
143
|
+
})();
|
|
144
|
+
if (!foreground) {
|
|
145
|
+
this.trackActive(vpid, jobId, {
|
|
146
|
+
done,
|
|
147
|
+
detach: () => {
|
|
148
|
+
detached = true;
|
|
149
|
+
proc.detach?.();
|
|
150
|
+
proc.unref?.();
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
proc.unref?.();
|
|
154
|
+
return { vpid, jobId, background: true, code: undefined, done, sandbox: proc.sandbox };
|
|
155
|
+
}
|
|
156
|
+
const result = await done;
|
|
157
|
+
return { vpid, jobId, background: false, ...result };
|
|
158
|
+
}
|
|
159
|
+
async spawnVirtual(script, run, { cwd, foreground = true } = {}) {
|
|
160
|
+
const vpid = id('vpid');
|
|
161
|
+
const jobId = id('job');
|
|
162
|
+
const pgrp = jobId;
|
|
163
|
+
const startedAt = now();
|
|
164
|
+
const record = {
|
|
165
|
+
vpid,
|
|
166
|
+
jobId,
|
|
167
|
+
pgrp,
|
|
168
|
+
kind: 'k',
|
|
169
|
+
status: 'running',
|
|
170
|
+
foreground,
|
|
171
|
+
cwd,
|
|
172
|
+
argv: ['k', script],
|
|
173
|
+
startedAt,
|
|
174
|
+
restartPolicy: 'never',
|
|
175
|
+
};
|
|
176
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.processesName(), record);
|
|
177
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.jobsName(), {
|
|
178
|
+
jobId,
|
|
179
|
+
pgrp,
|
|
180
|
+
command: script,
|
|
181
|
+
processIds: [vpid],
|
|
182
|
+
foreground,
|
|
183
|
+
status: 'running',
|
|
184
|
+
startedAt,
|
|
185
|
+
});
|
|
186
|
+
const controller = new AbortController();
|
|
187
|
+
let abortedSignal = null;
|
|
188
|
+
let detached = false;
|
|
189
|
+
const done = (async () => {
|
|
190
|
+
let result;
|
|
191
|
+
try {
|
|
192
|
+
result = await run(controller.signal);
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
if (detached)
|
|
196
|
+
return { code: 0, signal: 'SIGDETACH', stdout: '', stderr: '' };
|
|
197
|
+
result = controller.signal.aborted
|
|
198
|
+
? { code: 130, signal: abortedSignal || 'SIGTERM', stdout: '', stderr: '' }
|
|
199
|
+
: { code: 1, signal: null, stdout: '', stderr: error instanceof Error ? error.message : String(error) };
|
|
200
|
+
}
|
|
201
|
+
if (detached)
|
|
202
|
+
return { code: 0, signal: 'SIGDETACH', stdout: '', stderr: '' };
|
|
203
|
+
if (result.stdout)
|
|
204
|
+
await this.recordOutput(vpid, 'stdout', result.stdout);
|
|
205
|
+
if (result.stderr)
|
|
206
|
+
await this.recordOutput(vpid, 'stderr', result.stderr);
|
|
207
|
+
const stdout = appendBoundedOutput('', result.stdout);
|
|
208
|
+
const stderr = appendBoundedOutput('', result.stderr);
|
|
209
|
+
const code = result.code ?? 0;
|
|
210
|
+
const status = code === 0 ? 'exited' : 'failed';
|
|
211
|
+
const endedAt = now();
|
|
212
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.processesName(), { ...record, status, endedAt, exitCode: code, signal: result.signal ?? null });
|
|
213
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.jobsName(), {
|
|
214
|
+
jobId,
|
|
215
|
+
pgrp,
|
|
216
|
+
command: script,
|
|
217
|
+
processIds: [vpid],
|
|
218
|
+
foreground,
|
|
219
|
+
status,
|
|
220
|
+
startedAt,
|
|
221
|
+
endedAt,
|
|
222
|
+
});
|
|
223
|
+
return { code, signal: result.signal ?? null, stdout, stderr };
|
|
224
|
+
})();
|
|
225
|
+
const abort = (signal) => {
|
|
226
|
+
abortedSignal = signal;
|
|
227
|
+
controller.abort(new Error(signal));
|
|
228
|
+
};
|
|
229
|
+
if (!foreground) {
|
|
230
|
+
this.trackActive(vpid, jobId, {
|
|
231
|
+
done,
|
|
232
|
+
abort,
|
|
233
|
+
detach: () => {
|
|
234
|
+
detached = true;
|
|
235
|
+
controller.abort(new Error('detached'));
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
return { vpid, jobId, background: true, done };
|
|
239
|
+
}
|
|
240
|
+
const result = await done;
|
|
241
|
+
return { vpid, jobId, background: false, ...result };
|
|
242
|
+
}
|
|
243
|
+
async execShellWithoutProcess(script, { cwd, env, stdin, foreground = true, remote = this.defaultRemote, limits, sandbox }) {
|
|
244
|
+
if (!foreground)
|
|
245
|
+
throw new Error(`remote ${remote.name} does not support background shell processes`);
|
|
246
|
+
const vpid = id('vpid');
|
|
247
|
+
const jobId = id('job');
|
|
248
|
+
const pgrp = jobId;
|
|
249
|
+
const startedAt = now();
|
|
250
|
+
const record = {
|
|
251
|
+
vpid,
|
|
252
|
+
jobId,
|
|
253
|
+
pgrp,
|
|
254
|
+
kind: 'remote',
|
|
255
|
+
status: 'running',
|
|
256
|
+
foreground,
|
|
257
|
+
cwd,
|
|
258
|
+
argv: ['shell', script],
|
|
259
|
+
remote: { name: remote.name, ref: {}, fencingToken: id('fence') },
|
|
260
|
+
sandbox: sandbox ? { request: sandbox } : undefined,
|
|
261
|
+
startedAt,
|
|
262
|
+
restartPolicy: 'never',
|
|
263
|
+
};
|
|
264
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.processesName(), record);
|
|
265
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.jobsName(), {
|
|
266
|
+
jobId,
|
|
267
|
+
pgrp,
|
|
268
|
+
command: script,
|
|
269
|
+
processIds: [vpid],
|
|
270
|
+
foreground,
|
|
271
|
+
status: 'running',
|
|
272
|
+
startedAt,
|
|
273
|
+
});
|
|
274
|
+
const result = enforceSandboxAttestation(sandbox, await remote.shell.exec({ script, cwd, env, stdin, foreground, limits, sandbox }));
|
|
275
|
+
if (result.stdout)
|
|
276
|
+
await this.recordOutput(vpid, 'stdout', result.stdout);
|
|
277
|
+
if (result.stderr)
|
|
278
|
+
await this.recordOutput(vpid, 'stderr', result.stderr);
|
|
279
|
+
const status = result.code === 0 ? 'exited' : 'failed';
|
|
280
|
+
const endedAt = now();
|
|
281
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.processesName(), {
|
|
282
|
+
...record,
|
|
283
|
+
sandbox: { ...record.sandbox, attestation: result.sandbox },
|
|
284
|
+
status,
|
|
285
|
+
endedAt,
|
|
286
|
+
exitCode: result.code ?? null,
|
|
287
|
+
signal: result.signal ?? null,
|
|
288
|
+
});
|
|
289
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.jobsName(), {
|
|
290
|
+
jobId,
|
|
291
|
+
pgrp,
|
|
292
|
+
command: script,
|
|
293
|
+
processIds: [vpid],
|
|
294
|
+
foreground,
|
|
295
|
+
status,
|
|
296
|
+
startedAt,
|
|
297
|
+
endedAt,
|
|
298
|
+
});
|
|
299
|
+
return {
|
|
300
|
+
vpid,
|
|
301
|
+
jobId,
|
|
302
|
+
background: false,
|
|
303
|
+
code: result.code,
|
|
304
|
+
signal: result.signal,
|
|
305
|
+
stdout: appendBoundedOutput('', result.stdout || '', outputLimit(limits?.stdoutChars ?? limits?.outputChars)),
|
|
306
|
+
stderr: appendBoundedOutput('', result.stderr || '', outputLimit(limits?.stderrChars ?? limits?.outputChars)),
|
|
307
|
+
timedOut: result.timedOut,
|
|
308
|
+
sandbox: result.sandbox,
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
async consumeProcessStream(stream, vpid, name, ready, onText, detached = () => false) {
|
|
312
|
+
try {
|
|
313
|
+
for await (const text of stream) {
|
|
314
|
+
if (detached())
|
|
315
|
+
break;
|
|
316
|
+
onText(text);
|
|
317
|
+
await ready;
|
|
318
|
+
if (detached())
|
|
319
|
+
break;
|
|
320
|
+
await this.recordOutput(vpid, name, text);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
catch (error) {
|
|
324
|
+
if (!detached())
|
|
325
|
+
throw error;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
async recordOutput(vpid, name, text) {
|
|
329
|
+
for (const chunk of splitOutputChunks(text)) {
|
|
330
|
+
await this.kstate.appendSessionJsonl(this.sessionId, name === 'stdout' ? this.stdoutName(vpid) : this.stderrName(vpid), { at: now(), text: chunk });
|
|
331
|
+
await this.emitEvent({ type: 'process', status: 'output', text: chunk, stream: name, opId: vpid, vpid });
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
async stopUntrustedProcess(remote, proc) {
|
|
335
|
+
try {
|
|
336
|
+
if (proc.ref)
|
|
337
|
+
await remote.shell?.kill?.(proc.ref, 'SIGKILL');
|
|
338
|
+
}
|
|
339
|
+
catch {
|
|
340
|
+
// Startup attestation failure is already fail-closed; kill failures remain audit-visible through the failed vProcess row.
|
|
341
|
+
}
|
|
342
|
+
proc.unref?.();
|
|
343
|
+
proc.done.catch(() => undefined);
|
|
344
|
+
}
|
|
345
|
+
async recover() {
|
|
346
|
+
const rows = await this.kstate.readSessionJsonl(this.sessionId, this.processesName());
|
|
347
|
+
const latest = new Map();
|
|
348
|
+
for (const row of rows)
|
|
349
|
+
latest.set(row.vpid, row);
|
|
350
|
+
const recovered = [];
|
|
351
|
+
for (const record of latest.values()) {
|
|
352
|
+
if (record.status !== 'running')
|
|
353
|
+
continue;
|
|
354
|
+
if (record.kind === 'k') {
|
|
355
|
+
const resumed = await this.recoverDeterministicVirtualProcess(record);
|
|
356
|
+
if (resumed) {
|
|
357
|
+
recovered.push(resumed);
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
const uncertain = { ...record, status: 'uncertain', endedAt: now(), reconcile: { needed: true } };
|
|
361
|
+
recovered.push(uncertain);
|
|
362
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.processesName(), uncertain);
|
|
363
|
+
await this.appendJobForProcess(uncertain);
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
const pid = record.remote?.ref?.pid;
|
|
367
|
+
if (!pid) {
|
|
368
|
+
const uncertain = {
|
|
369
|
+
...record,
|
|
370
|
+
status: 'uncertain',
|
|
371
|
+
endedAt: now(),
|
|
372
|
+
error: 'remote process outcome is unknown after worker recovery; this worker cannot reattach its streams or completion handle',
|
|
373
|
+
reconcile: { needed: true },
|
|
374
|
+
};
|
|
375
|
+
recovered.push(uncertain);
|
|
376
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.processesName(), uncertain);
|
|
377
|
+
await this.appendJobForProcess(uncertain);
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
try {
|
|
381
|
+
const remote = record.remote?.name ? this.remoteByName(record.remote.name) : this.defaultRemote;
|
|
382
|
+
const alive = remote?.shell?.isRunning?.({ pid }) ?? false;
|
|
383
|
+
if (!alive)
|
|
384
|
+
throw new Error('process is not alive');
|
|
385
|
+
const uncertain = {
|
|
386
|
+
...record,
|
|
387
|
+
status: 'uncertain',
|
|
388
|
+
endedAt: now(),
|
|
389
|
+
error: 'remote process is still alive, but this worker cannot reattach its streams or completion handle',
|
|
390
|
+
reconcile: { needed: true },
|
|
391
|
+
};
|
|
392
|
+
recovered.push(uncertain);
|
|
393
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.processesName(), uncertain);
|
|
394
|
+
await this.appendJobForProcess(uncertain);
|
|
395
|
+
}
|
|
396
|
+
catch {
|
|
397
|
+
const uncertain = {
|
|
398
|
+
...record,
|
|
399
|
+
status: 'uncertain',
|
|
400
|
+
endedAt: now(),
|
|
401
|
+
error: 'remote process outcome is unknown after worker recovery; this worker cannot reattach its streams or completion handle',
|
|
402
|
+
reconcile: { needed: true },
|
|
403
|
+
};
|
|
404
|
+
recovered.push(uncertain);
|
|
405
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.processesName(), uncertain);
|
|
406
|
+
await this.appendJobForProcess(uncertain);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
return recovered;
|
|
410
|
+
}
|
|
411
|
+
async recoverDeterministicVirtualProcess(record) {
|
|
412
|
+
if (this.active.has(record.vpid))
|
|
413
|
+
return record;
|
|
414
|
+
const sleepMs = recoverableSleepMs(record);
|
|
415
|
+
if (sleepMs === undefined)
|
|
416
|
+
return undefined;
|
|
417
|
+
const startedAtMs = Date.parse(record.startedAt);
|
|
418
|
+
if (!Number.isFinite(startedAtMs))
|
|
419
|
+
return undefined;
|
|
420
|
+
const remainingMs = Math.max(0, sleepMs - (Date.now() - startedAtMs));
|
|
421
|
+
if (remainingMs <= 0) {
|
|
422
|
+
const exited = { ...record, status: 'exited', endedAt: now(), exitCode: 0, signal: null };
|
|
423
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.processesName(), exited);
|
|
424
|
+
await this.appendJobForProcess(exited);
|
|
425
|
+
return exited;
|
|
426
|
+
}
|
|
427
|
+
const recovered = { ...record, recovered: true };
|
|
428
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.processesName(), recovered);
|
|
429
|
+
const controller = new AbortController();
|
|
430
|
+
let abortedSignal = null;
|
|
431
|
+
let detached = false;
|
|
432
|
+
const done = (async () => {
|
|
433
|
+
let code = 0;
|
|
434
|
+
let signal = null;
|
|
435
|
+
try {
|
|
436
|
+
await sleep(remainingMs, controller.signal);
|
|
437
|
+
}
|
|
438
|
+
catch {
|
|
439
|
+
if (detached)
|
|
440
|
+
return { code: 0, signal: 'SIGDETACH', stdout: '', stderr: '' };
|
|
441
|
+
code = 130;
|
|
442
|
+
signal = abortedSignal || 'SIGTERM';
|
|
443
|
+
}
|
|
444
|
+
if (detached)
|
|
445
|
+
return { code: 0, signal: 'SIGDETACH', stdout: '', stderr: '' };
|
|
446
|
+
const status = code === 0 ? 'exited' : 'failed';
|
|
447
|
+
const endedAt = now();
|
|
448
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.processesName(), { ...recovered, status, endedAt, exitCode: code, signal });
|
|
449
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.jobsName(), {
|
|
450
|
+
jobId: record.jobId,
|
|
451
|
+
pgrp: record.pgrp,
|
|
452
|
+
command: record.argv.slice(1).join(' '),
|
|
453
|
+
processIds: [record.vpid],
|
|
454
|
+
foreground: record.foreground,
|
|
455
|
+
status,
|
|
456
|
+
startedAt: record.startedAt,
|
|
457
|
+
endedAt,
|
|
458
|
+
});
|
|
459
|
+
return { code, signal, stdout: '', stderr: '' };
|
|
460
|
+
})();
|
|
461
|
+
this.trackActive(record.vpid, record.jobId, {
|
|
462
|
+
done,
|
|
463
|
+
abort: (signal) => {
|
|
464
|
+
abortedSignal = signal;
|
|
465
|
+
controller.abort(new Error(signal));
|
|
466
|
+
},
|
|
467
|
+
detach: () => {
|
|
468
|
+
detached = true;
|
|
469
|
+
controller.abort(new Error('detached'));
|
|
470
|
+
},
|
|
471
|
+
recovered: true,
|
|
472
|
+
});
|
|
473
|
+
await this.emitEvent({
|
|
474
|
+
type: 'signal',
|
|
475
|
+
name: 'process.recovered',
|
|
476
|
+
data: { vpid: record.vpid, jobId: record.jobId, kind: 'k.sleep', remainingMs },
|
|
477
|
+
level: 'info',
|
|
478
|
+
});
|
|
479
|
+
return recovered;
|
|
480
|
+
}
|
|
481
|
+
async processes() {
|
|
482
|
+
return latestById(await this.kstate.readSessionJsonl(this.sessionId, this.processesName()), 'vpid');
|
|
483
|
+
}
|
|
484
|
+
localActiveProcessCount() {
|
|
485
|
+
return new Set(this.active.values()).size;
|
|
486
|
+
}
|
|
487
|
+
detachActive() {
|
|
488
|
+
const active = new Set(this.active.values());
|
|
489
|
+
this.active.clear();
|
|
490
|
+
for (const handle of active) {
|
|
491
|
+
handle.detach?.();
|
|
492
|
+
handle.done.catch(() => undefined);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
async jobs() {
|
|
496
|
+
const jobs = latestById(await this.kstate.readSessionJsonl(this.sessionId, this.jobsName()), 'jobId');
|
|
497
|
+
const processes = await this.processes();
|
|
498
|
+
return jobs.map((job) => projectJobFromProcesses(job, processes));
|
|
499
|
+
}
|
|
500
|
+
async kill(idOrJob, signal = 'SIGTERM') {
|
|
501
|
+
const targets = (await this.processes())
|
|
502
|
+
.filter((record) => record.status === 'running' || record.status === 'uncertain')
|
|
503
|
+
.filter((record) => record.vpid === idOrJob || record.jobId === idOrJob);
|
|
504
|
+
const updated = [];
|
|
505
|
+
for (const record of targets) {
|
|
506
|
+
const active = this.active.get(record.vpid) || this.active.get(record.jobId);
|
|
507
|
+
let killed = false;
|
|
508
|
+
if (record.remote) {
|
|
509
|
+
const remote = this.remoteByName(record.remote.name) || this.defaultRemote;
|
|
510
|
+
killed = Boolean(await remote.shell?.kill?.(record.remote.ref, signal));
|
|
511
|
+
}
|
|
512
|
+
else if (record.kind === 'k' && active?.abort) {
|
|
513
|
+
active.abort(signal);
|
|
514
|
+
killed = true;
|
|
515
|
+
}
|
|
516
|
+
const next = killed
|
|
517
|
+
? { ...record, status: 'uncertain', endedAt: now(), signal, reconcile: { needed: true } }
|
|
518
|
+
: { ...record, status: 'uncertain', endedAt: now(), signal, error: `unable to signal process with ${signal}`, reconcile: { needed: true } };
|
|
519
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.processesName(), next);
|
|
520
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.jobsName(), {
|
|
521
|
+
jobId: record.jobId,
|
|
522
|
+
pgrp: record.pgrp,
|
|
523
|
+
command: record.argv.join(' '),
|
|
524
|
+
processIds: [record.vpid],
|
|
525
|
+
foreground: record.foreground,
|
|
526
|
+
status: next.status,
|
|
527
|
+
startedAt: record.startedAt,
|
|
528
|
+
endedAt: next.endedAt,
|
|
529
|
+
});
|
|
530
|
+
await this.emitEvent({ type: 'signal', name: 'process.signaled', data: { vpid: record.vpid, jobId: record.jobId, signal, killed: Boolean(killed) }, level: killed ? 'info' : 'warn' });
|
|
531
|
+
updated.push(next);
|
|
532
|
+
}
|
|
533
|
+
return updated;
|
|
534
|
+
}
|
|
535
|
+
async wait(idOrJob) {
|
|
536
|
+
const record = await this.findProcess(idOrJob);
|
|
537
|
+
if (!record)
|
|
538
|
+
throw new Error(`no process or job matched: ${idOrJob}`);
|
|
539
|
+
const active = this.active.get(record.vpid) || this.active.get(record.jobId);
|
|
540
|
+
if (active) {
|
|
541
|
+
const result = await active.done;
|
|
542
|
+
const output = await this.processOutput(record.vpid);
|
|
543
|
+
return {
|
|
544
|
+
...result,
|
|
545
|
+
stdout: result.stdout || output.stdout,
|
|
546
|
+
stderr: result.stderr || output.stderr,
|
|
547
|
+
status: result.signal === 'SIGDETACH' ? 'uncertain' : result.code === 0 ? 'exited' : 'failed',
|
|
548
|
+
vpid: record.vpid,
|
|
549
|
+
jobId: record.jobId,
|
|
550
|
+
receiptId: record.receiptId,
|
|
551
|
+
recovered: active.recovered || record.recovered,
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
const output = await this.processOutput(record.vpid);
|
|
555
|
+
if (record.status === 'running') {
|
|
556
|
+
return {
|
|
557
|
+
code: 125,
|
|
558
|
+
signal: null,
|
|
559
|
+
stdout: output.stdout,
|
|
560
|
+
stderr: output.stderr || 'process is still running, but this worker cannot wait on its live handle\n',
|
|
561
|
+
status: 'uncertain',
|
|
562
|
+
vpid: record.vpid,
|
|
563
|
+
jobId: record.jobId,
|
|
564
|
+
receiptId: record.receiptId,
|
|
565
|
+
recovered: Boolean(record.recovered),
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
return {
|
|
569
|
+
code: record.exitCode ?? (record.status === 'failed' ? 1 : record.status === 'uncertain' ? 125 : 0),
|
|
570
|
+
signal: record.signal ?? null,
|
|
571
|
+
stdout: output.stdout,
|
|
572
|
+
stderr: output.stderr || (record.error ? `${record.error}\n` : record.status === 'uncertain' ? 'process outcome is uncertain; recovery must reconcile it\n' : ''),
|
|
573
|
+
status: record.status,
|
|
574
|
+
vpid: record.vpid,
|
|
575
|
+
jobId: record.jobId,
|
|
576
|
+
receiptId: record.receiptId,
|
|
577
|
+
recovered: Boolean(record.recovered),
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
async foreground(idOrJob) {
|
|
581
|
+
const record = await this.findProcess(idOrJob);
|
|
582
|
+
if (!record)
|
|
583
|
+
throw new Error(`no process or job matched: ${idOrJob}`);
|
|
584
|
+
const active = this.active.get(record.vpid) || this.active.get(record.jobId);
|
|
585
|
+
if (!active && (record.status === 'running' || record.status === 'uncertain'))
|
|
586
|
+
return this.wait(record.vpid);
|
|
587
|
+
await this.markForeground(record, true);
|
|
588
|
+
await this.emitEvent({ type: 'signal', name: 'process.foreground', data: { vpid: record.vpid, jobId: record.jobId }, level: 'info' });
|
|
589
|
+
return this.wait(record.vpid);
|
|
590
|
+
}
|
|
591
|
+
async background(idOrJob) {
|
|
592
|
+
const record = await this.findProcess(idOrJob);
|
|
593
|
+
if (!record)
|
|
594
|
+
throw new Error(`no process or job matched: ${idOrJob}`);
|
|
595
|
+
const next = await this.markForeground(record, false);
|
|
596
|
+
await this.emitEvent({ type: 'signal', name: 'process.background', data: { vpid: record.vpid, jobId: record.jobId }, level: 'info' });
|
|
597
|
+
return next;
|
|
598
|
+
}
|
|
599
|
+
async attachReceipt(vpid, receiptId) {
|
|
600
|
+
const record = await this.findProcess(vpid);
|
|
601
|
+
if (!record || record.receiptId === receiptId)
|
|
602
|
+
return;
|
|
603
|
+
const next = { ...record, receiptId };
|
|
604
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.processesName(), next);
|
|
605
|
+
}
|
|
606
|
+
trackActive(vpid, jobId, active) {
|
|
607
|
+
this.active.set(vpid, active);
|
|
608
|
+
this.active.set(jobId, active);
|
|
609
|
+
active.done.finally(() => {
|
|
610
|
+
this.active.delete(vpid);
|
|
611
|
+
this.active.delete(jobId);
|
|
612
|
+
}).catch(() => undefined);
|
|
613
|
+
}
|
|
614
|
+
async findProcess(idOrJob) {
|
|
615
|
+
return (await this.processes()).find((record) => record.vpid === idOrJob || record.jobId === idOrJob);
|
|
616
|
+
}
|
|
617
|
+
async processOutput(vpid) {
|
|
618
|
+
const [stdout, stderr] = await Promise.all([
|
|
619
|
+
this.readOutputLog(this.stdoutName(vpid)),
|
|
620
|
+
this.readOutputLog(this.stderrName(vpid)),
|
|
621
|
+
]);
|
|
622
|
+
return { stdout, stderr };
|
|
623
|
+
}
|
|
624
|
+
async readOutputLog(name) {
|
|
625
|
+
const rows = await this.kstate.readSessionJsonl(this.sessionId, name);
|
|
626
|
+
let output = '';
|
|
627
|
+
for (const row of rows) {
|
|
628
|
+
if (typeof row.text === 'string')
|
|
629
|
+
output = appendBoundedOutput(output, row.text);
|
|
630
|
+
}
|
|
631
|
+
return output;
|
|
632
|
+
}
|
|
633
|
+
async markForeground(record, foreground) {
|
|
634
|
+
const next = { ...record, foreground };
|
|
635
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.processesName(), next);
|
|
636
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.jobsName(), {
|
|
637
|
+
jobId: record.jobId,
|
|
638
|
+
pgrp: record.pgrp,
|
|
639
|
+
command: record.argv.join(' '),
|
|
640
|
+
processIds: [record.vpid],
|
|
641
|
+
foreground,
|
|
642
|
+
status: record.status,
|
|
643
|
+
startedAt: record.startedAt,
|
|
644
|
+
endedAt: record.endedAt,
|
|
645
|
+
});
|
|
646
|
+
return next;
|
|
647
|
+
}
|
|
648
|
+
async appendJobForProcess(record) {
|
|
649
|
+
await this.kstate.appendSessionJsonl(this.sessionId, this.jobsName(), jobFromProcess(record));
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
function latestById(rows, key) {
|
|
653
|
+
const latest = new Map();
|
|
654
|
+
for (const row of rows)
|
|
655
|
+
latest.set(row[key], row);
|
|
656
|
+
return [...latest.values()];
|
|
657
|
+
}
|
|
658
|
+
function jobFromProcess(record) {
|
|
659
|
+
return {
|
|
660
|
+
jobId: record.jobId,
|
|
661
|
+
pgrp: record.pgrp,
|
|
662
|
+
command: record.argv.join(' '),
|
|
663
|
+
processIds: [record.vpid],
|
|
664
|
+
foreground: record.foreground,
|
|
665
|
+
status: record.status,
|
|
666
|
+
startedAt: record.startedAt,
|
|
667
|
+
endedAt: record.endedAt,
|
|
668
|
+
};
|
|
669
|
+
}
|
|
670
|
+
function projectJobFromProcesses(job, processes) {
|
|
671
|
+
const members = processes.filter((process) => job.processIds.includes(process.vpid));
|
|
672
|
+
if (!members.length)
|
|
673
|
+
return job;
|
|
674
|
+
const status = aggregateProcessStatus(members);
|
|
675
|
+
const foreground = members.some((process) => process.foreground);
|
|
676
|
+
const endedAt = members
|
|
677
|
+
.map((process) => process.endedAt)
|
|
678
|
+
.filter((value) => Boolean(value))
|
|
679
|
+
.sort()
|
|
680
|
+
.at(-1);
|
|
681
|
+
return {
|
|
682
|
+
...job,
|
|
683
|
+
foreground,
|
|
684
|
+
status,
|
|
685
|
+
endedAt: status === 'running' ? undefined : endedAt || job.endedAt,
|
|
686
|
+
};
|
|
687
|
+
}
|
|
688
|
+
function aggregateProcessStatus(processes) {
|
|
689
|
+
if (processes.some((process) => process.status === 'running'))
|
|
690
|
+
return 'running';
|
|
691
|
+
if (processes.some((process) => process.status === 'uncertain'))
|
|
692
|
+
return 'uncertain';
|
|
693
|
+
if (processes.some((process) => process.status === 'failed'))
|
|
694
|
+
return 'failed';
|
|
695
|
+
return 'exited';
|
|
696
|
+
}
|
|
697
|
+
function enforceSandboxAttestation(request, result) {
|
|
698
|
+
if (request?.enforcement !== 'hard')
|
|
699
|
+
return result;
|
|
700
|
+
if (result.sandbox?.enforcement === 'hard')
|
|
701
|
+
return result;
|
|
702
|
+
return {
|
|
703
|
+
...result,
|
|
704
|
+
code: result.code && result.code !== 0 ? result.code : 126,
|
|
705
|
+
stderr: appendBoundedOutput(result.stderr || '', 'remote shell did not provide hard sandbox attestation\n'),
|
|
706
|
+
};
|
|
707
|
+
}
|
|
708
|
+
function sandboxStartupFailure(request, attestation) {
|
|
709
|
+
if (request?.enforcement !== 'hard')
|
|
710
|
+
return undefined;
|
|
711
|
+
if (attestation?.enforcement === 'hard')
|
|
712
|
+
return undefined;
|
|
713
|
+
return {
|
|
714
|
+
code: 126,
|
|
715
|
+
signal: null,
|
|
716
|
+
stdout: '',
|
|
717
|
+
stderr: 'remote shell did not provide hard sandbox attestation\n',
|
|
718
|
+
sandbox: attestation,
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
function appendBoundedOutput(current, next, limit = MAX_CAPTURED_PROCESS_OUTPUT_CHARS) {
|
|
722
|
+
if (!next)
|
|
723
|
+
return current;
|
|
724
|
+
const combined = `${current}${next}`;
|
|
725
|
+
if (combined.length <= limit)
|
|
726
|
+
return combined;
|
|
727
|
+
return combined.slice(combined.length - limit);
|
|
728
|
+
}
|
|
729
|
+
function outputLimit(value) {
|
|
730
|
+
if (typeof value !== 'number' || !Number.isFinite(value))
|
|
731
|
+
return MAX_CAPTURED_PROCESS_OUTPUT_CHARS;
|
|
732
|
+
const integer = Math.trunc(value);
|
|
733
|
+
return integer > 0 ? Math.min(integer, MAX_CAPTURED_PROCESS_OUTPUT_CHARS) : MAX_CAPTURED_PROCESS_OUTPUT_CHARS;
|
|
734
|
+
}
|
|
735
|
+
function splitOutputChunks(text) {
|
|
736
|
+
if (!text)
|
|
737
|
+
return [];
|
|
738
|
+
const chunks = [];
|
|
739
|
+
for (let offset = 0; offset < text.length; offset += MAX_PROCESS_OUTPUT_CHUNK_CHARS) {
|
|
740
|
+
chunks.push(text.slice(offset, offset + MAX_PROCESS_OUTPUT_CHUNK_CHARS));
|
|
741
|
+
}
|
|
742
|
+
return chunks;
|
|
743
|
+
}
|
|
744
|
+
function recoverableSleepMs(record) {
|
|
745
|
+
if (record.argv[0] !== 'k')
|
|
746
|
+
return undefined;
|
|
747
|
+
const script = record.argv.slice(1).join(' ').trim();
|
|
748
|
+
const match = script.match(/^sleep\s+((?:\d+(?:\.\d+)?)|(?:\.\d+))$/);
|
|
749
|
+
if (!match)
|
|
750
|
+
return undefined;
|
|
751
|
+
const seconds = Number(match[1]);
|
|
752
|
+
if (!Number.isFinite(seconds) || seconds < 0)
|
|
753
|
+
return undefined;
|
|
754
|
+
return Math.max(0, seconds) * 1000;
|
|
755
|
+
}
|
|
756
|
+
//# sourceMappingURL=vprocess.js.map
|