@phins/engine 0.0.0-stage → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +29 -0
- package/PROVENANCE.md +28 -0
- package/README.md +44 -2
- package/THIRD_PARTY.md +40 -0
- package/notices/bun-LICENSE.md +74 -0
- package/notices/dependency-licenses.json +2314 -0
- package/notices/drizzle-LICENSE +201 -0
- package/notices/fff-LICENSE +21 -0
- package/notices/node-forge-LICENSE +331 -0
- package/notices/opentui-spinner-MIT.txt +27 -0
- package/notices/packages__http-recorder__LICENSE +21 -0
- package/notices/remeda-LICENSE +21 -0
- package/notices/runtime-sources.json +7081 -0
- package/notices/seq-queue-LICENSE +22 -0
- package/notices/supplemental-notices.json +61 -0
- package/notices/third-party-source-LICENSE +21 -0
- package/package.json +65 -4
- package/src/agents/commands.ts +67 -0
- package/src/agents/context/builtins.ts +47 -0
- package/src/agents/context/instructions.ts +92 -0
- package/src/agents/context/registry.ts +53 -0
- package/src/agents/context/system.ts +329 -0
- package/src/agents/questions.ts +219 -0
- package/src/agents/references/guidance.ts +71 -0
- package/src/agents/references/registry.ts +122 -0
- package/src/agents/registry.ts +108 -0
- package/src/agents/resources/client.ts +185 -0
- package/src/agents/resources/contract.ts +71 -0
- package/src/agents/resources/error.ts +7 -0
- package/src/agents/skills/discovery.ts +167 -0
- package/src/agents/skills/guidance.ts +76 -0
- package/src/agents/skills/registry.ts +131 -0
- package/src/contracts/agents/agent.ts +36 -0
- package/src/contracts/agents/command.ts +14 -0
- package/src/contracts/agents/question-compat.ts +66 -0
- package/src/contracts/agents/question.ts +79 -0
- package/src/contracts/agents/reference.ts +36 -0
- package/src/contracts/agents/skill.ts +54 -0
- package/src/contracts/events/durable-manifest.ts +10 -0
- package/src/contracts/events/event.ts +125 -0
- package/src/contracts/events/filesystem.ts +13 -0
- package/src/contracts/events/installation.ts +20 -0
- package/src/contracts/events/legacy.ts +18 -0
- package/src/contracts/events/manifest.ts +82 -0
- package/src/contracts/events/mcp.ts +21 -0
- package/src/contracts/events/server.ts +8 -0
- package/src/contracts/events/session-compaction.ts +13 -0
- package/src/contracts/events/session-status.ts +50 -0
- package/src/contracts/events/session.ts +734 -0
- package/src/contracts/events/tui.ts +58 -0
- package/src/contracts/events/vcs.ts +13 -0
- package/src/contracts/events/workspace.ts +32 -0
- package/src/contracts/events/worktree.ts +21 -0
- package/src/contracts/execution/terminal-ticket.ts +9 -0
- package/src/contracts/execution/terminal.ts +55 -0
- package/src/contracts/execution/verification-receipt.ts +281 -0
- package/src/contracts/extensions/connection.ts +22 -0
- package/src/contracts/extensions/integration-id.ts +7 -0
- package/src/contracts/extensions/integration.ts +128 -0
- package/src/contracts/extensions/plugin.ts +15 -0
- package/src/contracts/identifier.ts +30 -0
- package/src/contracts/inference/catalog.ts +6 -0
- package/src/contracts/inference/llm.ts +23 -0
- package/src/contracts/inference/model.ts +103 -0
- package/src/contracts/inference/models-dev.ts +9 -0
- package/src/contracts/inference/provider.ts +69 -0
- package/src/contracts/schema.ts +30 -0
- package/src/contracts/security/credential.ts +34 -0
- package/src/contracts/security/permission-compat.ts +71 -0
- package/src/contracts/security/permission-saved.ts +20 -0
- package/src/contracts/security/permission.ts +64 -0
- package/src/contracts/session/compat.ts +676 -0
- package/src/contracts/session/delivery.ts +6 -0
- package/src/contracts/session/id.ts +20 -0
- package/src/contracts/session/info.ts +52 -0
- package/src/contracts/session/input-origin.ts +43 -0
- package/src/contracts/session/input.ts +24 -0
- package/src/contracts/session/live.ts +43 -0
- package/src/contracts/session/message-id.ts +11 -0
- package/src/contracts/session/message.ts +206 -0
- package/src/contracts/session/prompt.ts +56 -0
- package/src/contracts/session/revert.ts +23 -0
- package/src/contracts/session/run-state.ts +109 -0
- package/src/contracts/session/todo.ts +24 -0
- package/src/contracts/workspace/file-diff.ts +12 -0
- package/src/contracts/workspace/filesystem.ts +39 -0
- package/src/contracts/workspace/location.ts +30 -0
- package/src/contracts/workspace/project-copy.ts +29 -0
- package/src/contracts/workspace/project-directories.ts +11 -0
- package/src/contracts/workspace/project-id.ts +8 -0
- package/src/contracts/workspace/project.ts +41 -0
- package/src/contracts/workspace/workspace-id.ts +19 -0
- package/src/contracts/workspace/workspace.ts +9 -0
- package/src/execution/commands/executor.ts +44 -0
- package/src/execution/commands/provider.ts +54 -0
- package/src/execution/commands/runner.ts +26 -0
- package/src/execution/control/command.ts +55 -0
- package/src/execution/control/grant.ts +123 -0
- package/src/execution/control/promotion.ts +113 -0
- package/src/execution/control/receiver.ts +153 -0
- package/src/execution/control/restoration.ts +264 -0
- package/src/execution/identity.ts +432 -0
- package/src/execution/process/runner.ts +243 -0
- package/src/execution/process/sessions.ts +239 -0
- package/src/execution/process/spawner.ts +508 -0
- package/src/execution/sandbox.ts +143 -0
- package/src/execution/shell.ts +226 -0
- package/src/execution/terminal/native.ts +26 -0
- package/src/execution/terminal/protocol.ts +37 -0
- package/src/execution/terminal/schema.ts +1 -0
- package/src/execution/terminal/service.ts +318 -0
- package/src/execution/terminal/ticket.ts +57 -0
- package/src/execution/terminal/types.ts +25 -0
- package/src/extensions/agents.ts +211 -0
- package/src/extensions/api/agent.ts +14 -0
- package/src/extensions/api/catalog.ts +29 -0
- package/src/extensions/api/command.ts +13 -0
- package/src/extensions/api/context.ts +27 -0
- package/src/extensions/api/integration.ts +63 -0
- package/src/extensions/api/plugin.ts +17 -0
- package/src/extensions/api/reference.ts +12 -0
- package/src/extensions/api/registration.ts +16 -0
- package/src/extensions/api/skill.ts +11 -0
- package/src/extensions/api/tool.ts +58 -0
- package/src/extensions/api.ts +6 -0
- package/src/extensions/definition.ts +13 -0
- package/src/extensions/host.ts +225 -0
- package/src/extensions/integrations/service.ts +517 -0
- package/src/extensions/internal.ts +117 -0
- package/src/extensions/mcp/local.ts +352 -0
- package/src/extensions/mcp/schema.ts +21 -0
- package/src/extensions/mcp/tool-names.ts +26 -0
- package/src/extensions/mcp/tool.ts +149 -0
- package/src/extensions/models-dev.ts +135 -0
- package/src/extensions/options.ts +1 -0
- package/src/extensions/service.ts +152 -0
- package/src/extensions/skills/customize-phins.md +106 -0
- package/src/extensions/skills.ts +31 -0
- package/src/extensions/tools.ts +88 -0
- package/src/inference/api.ts +34 -0
- package/src/inference/cache-policy.ts +111 -0
- package/src/inference/catalog/discovery.ts +163 -0
- package/src/inference/catalog/metadata.ts +249 -0
- package/src/inference/catalog/model.ts +41 -0
- package/src/inference/catalog/provider.ts +25 -0
- package/src/inference/catalog/registry.ts +437 -0
- package/src/inference/errors.ts +32 -0
- package/src/inference/protocols/openai-chat.ts +497 -0
- package/src/inference/protocols/openai-compatible-chat.ts +24 -0
- package/src/inference/protocols/shared.ts +483 -0
- package/src/inference/protocols/utils/lifecycle.ts +102 -0
- package/src/inference/protocols/utils/openai-options.ts +93 -0
- package/src/inference/protocols/utils/tool-stream.ts +214 -0
- package/src/inference/provider.ts +37 -0
- package/src/inference/request/api.ts +27 -0
- package/src/inference/request/auth-options.ts +57 -0
- package/src/inference/request/auth.ts +161 -0
- package/src/inference/request/client.ts +490 -0
- package/src/inference/request/endpoint.ts +55 -0
- package/src/inference/request/execution-boundary.ts +41 -0
- package/src/inference/request/executor.ts +478 -0
- package/src/inference/request/framing.ts +27 -0
- package/src/inference/request/protocol.ts +90 -0
- package/src/inference/request/spending.ts +159 -0
- package/src/inference/requests.ts +193 -0
- package/src/inference/schema/errors.ts +194 -0
- package/src/inference/schema/events.ts +372 -0
- package/src/inference/schema/ids.ts +43 -0
- package/src/inference/schema/messages.ts +312 -0
- package/src/inference/schema/options.ts +247 -0
- package/src/inference/schema.ts +5 -0
- package/src/inference/tools/definition.ts +253 -0
- package/src/inference/tools/dispatcher.ts +83 -0
- package/src/inference/tools/failure.ts +19 -0
- package/src/inference/transports/http.ts +115 -0
- package/src/inference/transports/transport.ts +34 -0
- package/src/inference/transports/websocket.ts +322 -0
- package/src/runtime/effect/keyed-mutex.ts +45 -0
- package/src/runtime/effect/platform.ts +12 -0
- package/src/runtime/effect/service-access.ts +43 -0
- package/src/runtime/effect/service-graph.ts +102 -0
- package/src/runtime/events/bus.ts +1222 -0
- package/src/runtime/events/session-deltas.ts +128 -0
- package/src/runtime/events/verification-publisher.ts +9 -0
- package/src/runtime/flags.ts +78 -0
- package/src/runtime/markdown.d.ts +4 -0
- package/src/runtime/paths.ts +87 -0
- package/src/runtime/services.ts +240 -0
- package/src/runtime/state.ts +128 -0
- package/src/runtime/vendor.d.ts +25 -0
- package/src/runtime/version.ts +8 -0
- package/src/security/credentials.ts +137 -0
- package/src/security/launcher-policy.ts +68 -0
- package/src/security/permissions/delegation.ts +32 -0
- package/src/security/permissions/origin.ts +70 -0
- package/src/security/permissions/saved.ts +78 -0
- package/src/security/permissions/service.ts +533 -0
- package/src/security/policy.ts +46 -0
- package/src/sessions/actions/administration.ts +109 -0
- package/src/sessions/actions/command-template.ts +23 -0
- package/src/sessions/actions/commands.ts +258 -0
- package/src/sessions/actions/errors.ts +16 -0
- package/src/sessions/actions/operations.ts +271 -0
- package/src/sessions/actions/revert.ts +121 -0
- package/src/sessions/context/checkpoint.ts +330 -0
- package/src/sessions/context/compaction.ts +268 -0
- package/src/sessions/context/epoch.ts +173 -0
- package/src/sessions/context/selection.ts +30 -0
- package/src/sessions/errors.ts +38 -0
- package/src/sessions/identifiers.ts +25 -0
- package/src/sessions/info.ts +90 -0
- package/src/sessions/input/inbox.ts +514 -0
- package/src/sessions/input/origin.ts +131 -0
- package/src/sessions/input/turns.ts +125 -0
- package/src/sessions/messages/history.ts +101 -0
- package/src/sessions/messages/service.ts +2 -0
- package/src/sessions/messages/updater.ts +420 -0
- package/src/sessions/projector.ts +699 -0
- package/src/sessions/run/budget.ts +62 -0
- package/src/sessions/run/chat-scope.ts +62 -0
- package/src/sessions/run/coding-workflow.ts +82 -0
- package/src/sessions/run/coordinator.ts +192 -0
- package/src/sessions/run/events.ts +572 -0
- package/src/sessions/run/execution.ts +57 -0
- package/src/sessions/run/inference.ts +1466 -0
- package/src/sessions/run/lifecycle.ts +316 -0
- package/src/sessions/run/local-executor.ts +307 -0
- package/src/sessions/run/loop.ts +33 -0
- package/src/sessions/run/messages.ts +161 -0
- package/src/sessions/run/model.ts +232 -0
- package/src/sessions/run/state.ts +288 -0
- package/src/sessions/run/subtasks.ts +190 -0
- package/src/sessions/runtime.ts +40 -0
- package/src/sessions/service.ts +686 -0
- package/src/sessions/store.ts +129 -0
- package/src/sessions/tasks.ts +78 -0
- package/src/sessions/title.ts +23 -0
- package/src/sessions/usage/contribution-value.ts +48 -0
- package/src/sessions/usage/contribution.ts +511 -0
- package/src/sessions/usage/cost.ts +21 -0
- package/src/sessions/verification/history.ts +79 -0
- package/src/sessions/verification/recovery.ts +971 -0
- package/src/sessions/verification/service.ts +671 -0
- package/src/settings/agents.ts +23 -0
- package/src/settings/attachments.ts +15 -0
- package/src/settings/commands.ts +12 -0
- package/src/settings/compaction.ts +15 -0
- package/src/settings/config.ts +234 -0
- package/src/settings/experimental.ts +18 -0
- package/src/settings/formatters.ts +12 -0
- package/src/settings/markdown.ts +36 -0
- package/src/settings/mcp.ts +39 -0
- package/src/settings/plugins/agent.ts +108 -0
- package/src/settings/plugins/command.ts +82 -0
- package/src/settings/plugins/provider.ts +113 -0
- package/src/settings/plugins/reference.ts +68 -0
- package/src/settings/plugins/skill.ts +50 -0
- package/src/settings/plugins.ts +13 -0
- package/src/settings/providers.ts +71 -0
- package/src/settings/references.ts +22 -0
- package/src/settings/tool-output.ts +9 -0
- package/src/settings/watchers.ts +7 -0
- package/src/shared/effect-flock.ts +93 -0
- package/src/shared/encode.ts +51 -0
- package/src/shared/flock.ts +584 -0
- package/src/shared/glob.ts +34 -0
- package/src/shared/hash.ts +11 -0
- package/src/shared/lazy.ts +26 -0
- package/src/shared/prefixed-id.ts +47 -0
- package/src/shared/record.ts +3 -0
- package/src/shared/schema.ts +87 -0
- package/src/shared/slug.ts +74 -0
- package/src/shared/token.ts +5 -0
- package/src/shared/which.ts +14 -0
- package/src/shared/wildcard.ts +48 -0
- package/src/storage/database.ts +63 -0
- package/src/storage/drizzle/effect-sqlite/driver.ts +77 -0
- package/src/storage/drizzle/effect-sqlite/migrator.ts +14 -0
- package/src/storage/drizzle/effect-sqlite/session.ts +218 -0
- package/src/storage/drizzle/index.ts +10 -0
- package/src/storage/drizzle/internal/drizzle-utils.ts +127 -0
- package/src/storage/drizzle/sqlite-core/effect/count.ts +58 -0
- package/src/storage/drizzle/sqlite-core/effect/db.ts +296 -0
- package/src/storage/drizzle/sqlite-core/effect/delete.ts +261 -0
- package/src/storage/drizzle/sqlite-core/effect/insert.ts +349 -0
- package/src/storage/drizzle/sqlite-core/effect/query.ts +198 -0
- package/src/storage/drizzle/sqlite-core/effect/raw.ts +49 -0
- package/src/storage/drizzle/sqlite-core/effect/select.ts +279 -0
- package/src/storage/drizzle/sqlite-core/effect/session.ts +490 -0
- package/src/storage/drizzle/sqlite-core/effect/update.ts +402 -0
- package/src/storage/drizzle/up-migrations/effect-sqlite.ts +102 -0
- package/src/storage/drizzle/up-migrations/sqlite.ts +253 -0
- package/src/storage/drizzle/up-migrations/utils.ts +45 -0
- package/src/storage/migration.gen.ts +62 -0
- package/src/storage/migration.ts +168 -0
- package/src/storage/migrations/20260127222353_familiar_lady_ursula.ts +107 -0
- package/src/storage/migrations/20260211171708_add_project_commands.ts +11 -0
- package/src/storage/migrations/20260213144116_wakeful_the_professor.ts +23 -0
- package/src/storage/migrations/20260225215848_workspace.ts +19 -0
- package/src/storage/migrations/20260227213759_add_session_workspace_id.ts +12 -0
- package/src/storage/migrations/20260228203230_blue_harpoon.ts +30 -0
- package/src/storage/migrations/20260303231226_add_workspace_fields.ts +15 -0
- package/src/storage/migrations/20260309230000_move_org_to_state.ts +15 -0
- package/src/storage/migrations/20260312043431_session_message_cursor.ts +16 -0
- package/src/storage/migrations/20260323234822_events.ts +26 -0
- package/src/storage/migrations/20260410174513_workspace-name.ts +29 -0
- package/src/storage/migrations/20260413175956_chief_energizer.ts +24 -0
- package/src/storage/migrations/20260423070820_add_icon_url_override.ts +14 -0
- package/src/storage/migrations/20260427172553_slow_nightmare.ts +30 -0
- package/src/storage/migrations/20260428004200_add_session_path.ts +11 -0
- package/src/storage/migrations/20260501142318_next_venus.ts +12 -0
- package/src/storage/migrations/20260504145000_add_sync_owner.ts +11 -0
- package/src/storage/migrations/20260507164347_add_workspace_time.ts +11 -0
- package/src/storage/migrations/20260510033149_session_usage.ts +56 -0
- package/src/storage/migrations/20260511000411_data_migration_state.ts +16 -0
- package/src/storage/migrations/20260511173437_session-metadata.ts +16 -0
- package/src/storage/migrations/20260601010001_normalize_storage_paths.ts +22 -0
- package/src/storage/migrations/20260601202201_amazing_prowler.ts +11 -0
- package/src/storage/migrations/20260602002951_lowly_union_jack.ts +24 -0
- package/src/storage/migrations/20260602182828_add_project_directories.ts +20 -0
- package/src/storage/migrations/20260603001617_session_message_projection_indexes.ts +19 -0
- package/src/storage/migrations/20260603040000_session_message_projection_order.ts +19 -0
- package/src/storage/migrations/20260603141458_session_input_inbox.ts +25 -0
- package/src/storage/migrations/20260603160727_jittery_ezekiel_stane.ts +20 -0
- package/src/storage/migrations/20260604172448_event_sourced_session_input.ts +47 -0
- package/src/storage/migrations/20260605003541_add_session_context_snapshot.ts +21 -0
- package/src/storage/migrations/20260605042240_add_context_epoch_agent.ts +11 -0
- package/src/storage/migrations/20260611035744_credential.ts +25 -0
- package/src/storage/migrations/20260611192811_lush_chimera.ts +25 -0
- package/src/storage/migrations/20260612174303_project_dir_strategy.ts +29 -0
- package/src/storage/migrations/20260622142730_simplify_session_context_epoch.ts +13 -0
- package/src/storage/migrations/20260622170816_reset_v2_session_state.ts +17 -0
- package/src/storage/migrations/20260622202450_simplify_session_input.ts +17 -0
- package/src/storage/migrations/20260722030810_rolling_work_context_epoch.ts +44 -0
- package/src/storage/migrations/20260729010558_verification_receipt.ts +45 -0
- package/src/storage/migrations/20260729040943_verification_request_outbox.ts +35 -0
- package/src/storage/migrations/20260731015112_verification_attempt_lease.ts +16 -0
- package/src/storage/migrations/20260731022945_verification_attempt_timeout.ts +11 -0
- package/src/storage/migrations/20260731023300_event_owner_generation.ts +11 -0
- package/src/storage/migrations/20260731031224_session_warp_outbox.ts +38 -0
- package/src/storage/migrations/20260731073410_workspace-mutation-lease.ts +28 -0
- package/src/storage/migrations/20260731074831_verification-execution-attestation.ts +11 -0
- package/src/storage/migrations/20260731081000_workspace-session-mutation-fences.ts +67 -0
- package/src/storage/migrations/20260801155612_session_run_state.ts +33 -0
- package/src/storage/migrations/20260801162000_session_usage_contribution.ts +46 -0
- package/src/storage/migrations/20260801163000_backfill_session_transcript.ts +551 -0
- package/src/storage/migrations/20260913045944_session_tree_budget.ts +20 -0
- package/src/storage/migrations/20261005065909_session_input_origin.ts +25 -0
- package/src/storage/migrations/20261005143808_session_run_input_owner.ts +13 -0
- package/src/storage/migrations/20261006020515_node_command_receipt.ts +19 -0
- package/src/storage/migrations/20261009151628_session_command_receipt.ts +21 -0
- package/src/storage/offline.ts +34 -0
- package/src/storage/path.ts +91 -0
- package/src/storage/schema.gen.ts +490 -0
- package/src/storage/schema.sql.ts +10 -0
- package/src/storage/sqlite.bun.ts +195 -0
- package/src/storage/sqlite.ts +8 -0
- package/src/storage/tables/commands.ts +19 -0
- package/src/storage/tables/credentials.ts +14 -0
- package/src/storage/tables/events.ts +26 -0
- package/src/storage/tables/permissions.ts +20 -0
- package/src/storage/tables/projects.ts +35 -0
- package/src/storage/tables/sessions.ts +358 -0
- package/src/storage/tables/workspaces.ts +87 -0
- package/src/tools/admission.ts +29 -0
- package/src/tools/application.ts +54 -0
- package/src/tools/builtins.ts +50 -0
- package/src/tools/definition.ts +198 -0
- package/src/tools/files/edit.ts +200 -0
- package/src/tools/files/format.ts +46 -0
- package/src/tools/files/glob.ts +102 -0
- package/src/tools/files/grep.ts +134 -0
- package/src/tools/files/patch.ts +179 -0
- package/src/tools/files/read.ts +146 -0
- package/src/tools/files/reader.ts +363 -0
- package/src/tools/files/write.ts +94 -0
- package/src/tools/output-store.ts +217 -0
- package/src/tools/planning.ts +56 -0
- package/src/tools/process/session.ts +44 -0
- package/src/tools/process/shell.ts +271 -0
- package/src/tools/question.ts +107 -0
- package/src/tools/registration.ts +13 -0
- package/src/tools/registry.ts +248 -0
- package/src/tools/skill.ts +103 -0
- package/src/tools/task.ts +100 -0
- package/src/tools/web/body.ts +30 -0
- package/src/tools/web/fetch.ts +211 -0
- package/src/tools/web/search.ts +253 -0
- package/src/transport/api/definition.ts +103 -0
- package/src/transport/api/errors.ts +112 -0
- package/src/transport/api/middleware/authorization.ts +7 -0
- package/src/transport/api/middleware/schema-error.ts +8 -0
- package/src/transport/api/middleware/service-admission.ts +8 -0
- package/src/transport/api/resources/agent.ts +21 -0
- package/src/transport/api/resources/command.ts +28 -0
- package/src/transport/api/resources/credential.ts +38 -0
- package/src/transport/api/resources/event.ts +61 -0
- package/src/transport/api/resources/fs.ts +69 -0
- package/src/transport/api/resources/health.ts +65 -0
- package/src/transport/api/resources/integration.ts +131 -0
- package/src/transport/api/resources/location.ts +43 -0
- package/src/transport/api/resources/message.ts +69 -0
- package/src/transport/api/resources/model.ts +33 -0
- package/src/transport/api/resources/permission.ts +96 -0
- package/src/transport/api/resources/project-copy.ts +57 -0
- package/src/transport/api/resources/project.ts +19 -0
- package/src/transport/api/resources/provider.ts +46 -0
- package/src/transport/api/resources/pty.ts +143 -0
- package/src/transport/api/resources/question.ts +85 -0
- package/src/transport/api/resources/reference.ts +28 -0
- package/src/transport/api/resources/service.ts +46 -0
- package/src/transport/api/resources/session.ts +477 -0
- package/src/transport/api/resources/skill.ts +28 -0
- package/src/transport/api/resources/tui.ts +100 -0
- package/src/transport/client/connection.ts +389 -0
- package/src/transport/client/create.ts +112 -0
- package/src/transport/client/errors.ts +51 -0
- package/src/transport/client/events.ts +218 -0
- package/src/transport/client/generated/client/client.gen.ts +311 -0
- package/src/transport/client/generated/client/index.ts +25 -0
- package/src/transport/client/generated/client/types.gen.ts +241 -0
- package/src/transport/client/generated/client/utils.gen.ts +337 -0
- package/src/transport/client/generated/client.gen.ts +25 -0
- package/src/transport/client/generated/core/auth.gen.ts +42 -0
- package/src/transport/client/generated/core/bodySerializer.gen.ts +100 -0
- package/src/transport/client/generated/core/params.gen.ts +176 -0
- package/src/transport/client/generated/core/pathSerializer.gen.ts +181 -0
- package/src/transport/client/generated/core/queryKeySerializer.gen.ts +136 -0
- package/src/transport/client/generated/core/serverSentEvents.gen.ts +266 -0
- package/src/transport/client/generated/core/types.gen.ts +118 -0
- package/src/transport/client/generated/core/utils.gen.ts +143 -0
- package/src/transport/client/generated/sdk.gen.ts +6695 -0
- package/src/transport/client/generated/types.gen.ts +16571 -0
- package/src/transport/client/session-state.ts +341 -0
- package/src/transport/host/api.ts +9 -0
- package/src/transport/host/auth.ts +64 -0
- package/src/transport/host/cors.ts +35 -0
- package/src/transport/host/handlers/agent.ts +14 -0
- package/src/transport/host/handlers/command.ts +9 -0
- package/src/transport/host/handlers/credential.ts +23 -0
- package/src/transport/host/handlers/event.ts +39 -0
- package/src/transport/host/handlers/fs.ts +40 -0
- package/src/transport/host/handlers/health.ts +11 -0
- package/src/transport/host/handlers/integration.ts +105 -0
- package/src/transport/host/handlers/location.ts +19 -0
- package/src/transport/host/handlers/message.ts +65 -0
- package/src/transport/host/handlers/model.ts +25 -0
- package/src/transport/host/handlers/permission.ts +68 -0
- package/src/transport/host/handlers/project-copy.ts +69 -0
- package/src/transport/host/handlers/project.ts +32 -0
- package/src/transport/host/handlers/provider.ts +33 -0
- package/src/transport/host/handlers/pty.ts +224 -0
- package/src/transport/host/handlers/question.ts +63 -0
- package/src/transport/host/handlers/reference.ts +9 -0
- package/src/transport/host/handlers/service.ts +24 -0
- package/src/transport/host/handlers/session.ts +581 -0
- package/src/transport/host/handlers/skill.ts +9 -0
- package/src/transport/host/handlers/tui.ts +136 -0
- package/src/transport/host/handlers.ts +68 -0
- package/src/transport/host/location.ts +66 -0
- package/src/transport/host/middleware/authorization.ts +59 -0
- package/src/transport/host/middleware/schema-error.ts +17 -0
- package/src/transport/host/middleware/service-admission.ts +22 -0
- package/src/transport/host/middleware/session-location.ts +72 -0
- package/src/transport/host/pty-environment.ts +17 -0
- package/src/transport/host/routes.ts +67 -0
- package/src/transport/host/runtime.ts +22 -0
- package/src/transport/host/service-control.ts +143 -0
- package/src/transport/host/sse.ts +415 -0
- package/src/transport/host/utf8.ts +26 -0
- package/src/verification/authority.ts +547 -0
- package/src/verification/canonical-signature.ts +145 -0
- package/src/verification/containment.ts +197 -0
- package/src/verification/enforcement.ts +255 -0
- package/src/verification/execution-attestation.ts +321 -0
- package/src/verification/key-lifecycle.ts +456 -0
- package/src/verification/local.ts +87 -0
- package/src/verification/oci/containment.ts +529 -0
- package/src/verification/oci/lease.ts +1549 -0
- package/src/verification/oci/runtime.ts +309 -0
- package/src/verification/oci/supervisor.ts +429 -0
- package/src/verification/registry.ts +379 -0
- package/src/verification/settlement-admission.ts +322 -0
- package/src/verification/trusted-file.ts +372 -0
- package/src/workspace/files/access.ts +257 -0
- package/src/workspace/files/diff.ts +6 -0
- package/src/workspace/files/formatter.ts +130 -0
- package/src/workspace/files/ignore.ts +67 -0
- package/src/workspace/files/mutation.ts +203 -0
- package/src/workspace/files/patch.ts +197 -0
- package/src/workspace/files/protected.ts +53 -0
- package/src/workspace/files/service.ts +115 -0
- package/src/workspace/files/watcher.ts +138 -0
- package/src/workspace/location.ts +35 -0
- package/src/workspace/media/image.ts +78 -0
- package/src/workspace/media/photon.ts +95 -0
- package/src/workspace/mutations.ts +158 -0
- package/src/workspace/projects/copy-strategies.ts +35 -0
- package/src/workspace/projects/copy.ts +282 -0
- package/src/workspace/projects/directories.ts +159 -0
- package/src/workspace/projects/schema.ts +16 -0
- package/src/workspace/projects/service.ts +136 -0
- package/src/workspace/repository/cache.ts +261 -0
- package/src/workspace/repository/git.ts +4272 -0
- package/src/workspace/repository/reference.ts +217 -0
- package/src/workspace/repository/retention-barrier.ts +18 -0
- package/src/workspace/repository/snapshot.ts +791 -0
- package/src/workspace/search/binary.ts +134 -0
- package/src/workspace/search/native.ts +140 -0
- package/src/workspace/search/ripgrep.ts +282 -0
- package/src/workspace/search/service.ts +239 -0
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
import { Schema } from "effect"
|
|
2
|
+
import z from "zod"
|
|
3
|
+
import type { Options as ResourceOptions } from "../agents/resources/client"
|
|
4
|
+
import { ResourceError } from "../agents/resources/error"
|
|
5
|
+
import * as ControlGrant from "./control/grant"
|
|
6
|
+
import * as ControlCommand from "./control/command"
|
|
7
|
+
|
|
8
|
+
export class IdentityError extends Schema.TaggedErrorClass<IdentityError>()(
|
|
9
|
+
"NodeIdentity.Error",
|
|
10
|
+
{
|
|
11
|
+
message: Schema.String,
|
|
12
|
+
status: Schema.optional(Schema.Number),
|
|
13
|
+
retryable: Schema.optional(Schema.Boolean),
|
|
14
|
+
},
|
|
15
|
+
) {}
|
|
16
|
+
|
|
17
|
+
const Expected = z.strictObject({
|
|
18
|
+
nodeId: z.uuid(),
|
|
19
|
+
userId: z.uuid(),
|
|
20
|
+
workspaceId: z.uuid(),
|
|
21
|
+
})
|
|
22
|
+
const Identity = Expected.extend({
|
|
23
|
+
version: z.literal(1),
|
|
24
|
+
sessionId: z.uuid(),
|
|
25
|
+
expiresAt: z.iso.datetime({ offset: true }),
|
|
26
|
+
})
|
|
27
|
+
export type Identity = Readonly<z.infer<typeof Identity>>
|
|
28
|
+
|
|
29
|
+
export interface Options {
|
|
30
|
+
readonly baseUrl: string
|
|
31
|
+
readonly expected: z.infer<typeof Expected>
|
|
32
|
+
readonly token: (signal: AbortSignal) => Promise<string>
|
|
33
|
+
readonly fetch?: (url: string, init: RequestInit) => Promise<Response>
|
|
34
|
+
}
|
|
35
|
+
type Execution = Pick<ResourceOptions, "kind" | "id" | "leaseId"> & {
|
|
36
|
+
readonly directory: string
|
|
37
|
+
}
|
|
38
|
+
const Work = z.strictObject({
|
|
39
|
+
kind: z.enum(["jobs", "tasks"]),
|
|
40
|
+
id: z.uuid(),
|
|
41
|
+
leaseId: z.uuid(),
|
|
42
|
+
directory: z
|
|
43
|
+
.string()
|
|
44
|
+
.max(2000)
|
|
45
|
+
.refine((value) => Boolean(value.trim()) && !value.includes("\0")),
|
|
46
|
+
})
|
|
47
|
+
const WorkIdentity = Identity.extend(Work.shape)
|
|
48
|
+
export type WorkIdentity = Readonly<z.infer<typeof WorkIdentity>>
|
|
49
|
+
|
|
50
|
+
/** Bounded JSON decoding shared by native adapters; never an authorization check. */
|
|
51
|
+
export async function responseJSON(
|
|
52
|
+
response: Response,
|
|
53
|
+
signal: AbortSignal,
|
|
54
|
+
scope = "identity",
|
|
55
|
+
maximumBytes = 16 * 1024,
|
|
56
|
+
) {
|
|
57
|
+
if (!response.body)
|
|
58
|
+
throw new IdentityError({ message: `Invalid Node ${scope} response` })
|
|
59
|
+
const reader = response.body.getReader()
|
|
60
|
+
const cancel = () => {
|
|
61
|
+
void reader.cancel().catch(() => {})
|
|
62
|
+
}
|
|
63
|
+
signal.addEventListener("abort", cancel, { once: true })
|
|
64
|
+
const chunks: Uint8Array[] = []
|
|
65
|
+
let size = 0
|
|
66
|
+
try {
|
|
67
|
+
signal.throwIfAborted()
|
|
68
|
+
while (true) {
|
|
69
|
+
const chunk = await reader.read()
|
|
70
|
+
signal.throwIfAborted()
|
|
71
|
+
if (chunk.done) break
|
|
72
|
+
size += chunk.value.byteLength
|
|
73
|
+
if (size > maximumBytes)
|
|
74
|
+
throw new IdentityError({
|
|
75
|
+
message: `Node ${scope} response exceeds the ${maximumBytes / 1024} KiB limit`,
|
|
76
|
+
})
|
|
77
|
+
chunks.push(chunk.value)
|
|
78
|
+
}
|
|
79
|
+
return JSON.parse(
|
|
80
|
+
new TextDecoder("utf-8", { fatal: true }).decode(Buffer.concat(chunks)),
|
|
81
|
+
) as unknown
|
|
82
|
+
} finally {
|
|
83
|
+
signal.removeEventListener("abort", cancel)
|
|
84
|
+
await reader.cancel().catch(() => {})
|
|
85
|
+
reader.releaseLock()
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Accept only an origin, without a credential, query or application path. */
|
|
90
|
+
export function trustedOrigin(baseUrl: string) {
|
|
91
|
+
try {
|
|
92
|
+
const url = new URL(baseUrl)
|
|
93
|
+
if (
|
|
94
|
+
url.username ||
|
|
95
|
+
url.password ||
|
|
96
|
+
url.search ||
|
|
97
|
+
url.hash ||
|
|
98
|
+
url.pathname !== "/" ||
|
|
99
|
+
(url.protocol !== "https:" &&
|
|
100
|
+
!(
|
|
101
|
+
url.protocol === "http:" &&
|
|
102
|
+
["localhost", "127.0.0.1", "[::1]"].includes(url.hostname)
|
|
103
|
+
))
|
|
104
|
+
)
|
|
105
|
+
throw new Error()
|
|
106
|
+
return url.origin
|
|
107
|
+
} catch {
|
|
108
|
+
throw new IdentityError({
|
|
109
|
+
message: "Node identity requires a trusted HTTPS origin or loopback HTTP",
|
|
110
|
+
})
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Host-only adapter. Identity proves a login, never execution or sharing rights. */
|
|
115
|
+
export async function authenticate(options: Options, signal: AbortSignal) {
|
|
116
|
+
const expected = Expected.safeParse(options.expected)
|
|
117
|
+
const origin = trustedOrigin(options.baseUrl)
|
|
118
|
+
if (!expected.success)
|
|
119
|
+
throw new IdentityError({ message: "Invalid expected Node identity" })
|
|
120
|
+
const owner = Object.freeze(expected.data)
|
|
121
|
+
const token = options.token
|
|
122
|
+
const fetchImpl = options.fetch ?? globalThis.fetch
|
|
123
|
+
const request = async (signal: AbortSignal) => {
|
|
124
|
+
const deadline = AbortSignal.any([signal, AbortSignal.timeout(10_000)])
|
|
125
|
+
try {
|
|
126
|
+
deadline.throwIfAborted()
|
|
127
|
+
const credential = await token(deadline)
|
|
128
|
+
deadline.throwIfAborted()
|
|
129
|
+
if (
|
|
130
|
+
typeof credential !== "string" ||
|
|
131
|
+
!credential ||
|
|
132
|
+
credential.length > 16384 ||
|
|
133
|
+
/[\s\x00-\x1f\x7f]/.test(credential)
|
|
134
|
+
)
|
|
135
|
+
throw new IdentityError({ message: "No valid Node credential" })
|
|
136
|
+
const response = await fetchImpl(`${origin}/v1/agent/node/identity`, {
|
|
137
|
+
method: "POST",
|
|
138
|
+
redirect: "error",
|
|
139
|
+
cache: "no-store",
|
|
140
|
+
headers: {
|
|
141
|
+
Accept: "application/json",
|
|
142
|
+
Authorization: `Bearer ${credential}`,
|
|
143
|
+
"Content-Type": "application/json",
|
|
144
|
+
},
|
|
145
|
+
body: JSON.stringify({
|
|
146
|
+
deviceId: owner.nodeId,
|
|
147
|
+
workspaceId: owner.workspaceId,
|
|
148
|
+
}),
|
|
149
|
+
signal: deadline,
|
|
150
|
+
})
|
|
151
|
+
if (!response.ok) {
|
|
152
|
+
await response.body?.cancel().catch(() => {})
|
|
153
|
+
throw new IdentityError({
|
|
154
|
+
message: "Node identity is unavailable or no longer authorized",
|
|
155
|
+
status: response.status,
|
|
156
|
+
retryable:
|
|
157
|
+
response.status === 408 ||
|
|
158
|
+
response.status === 429 ||
|
|
159
|
+
response.status >= 500,
|
|
160
|
+
})
|
|
161
|
+
}
|
|
162
|
+
const decoded = Identity.safeParse(await responseJSON(response, deadline))
|
|
163
|
+
if (!decoded.success)
|
|
164
|
+
throw new IdentityError({ message: "Invalid Node identity response" })
|
|
165
|
+
const identity = decoded.data
|
|
166
|
+
if (
|
|
167
|
+
identity.nodeId !== owner.nodeId ||
|
|
168
|
+
identity.userId !== owner.userId ||
|
|
169
|
+
identity.workspaceId !== owner.workspaceId ||
|
|
170
|
+
Date.parse(identity.expiresAt) <= Date.now()
|
|
171
|
+
)
|
|
172
|
+
throw new IdentityError({
|
|
173
|
+
message: "Node identity does not match this binding or has expired",
|
|
174
|
+
})
|
|
175
|
+
deadline.throwIfAborted()
|
|
176
|
+
return { identity: Object.freeze(identity), credential }
|
|
177
|
+
} catch (error) {
|
|
178
|
+
if (error instanceof IdentityError) throw error
|
|
179
|
+
throw new IdentityError({
|
|
180
|
+
message: deadline.aborted
|
|
181
|
+
? "Node identity request cancelled or timed out"
|
|
182
|
+
: "Could not verify Node identity",
|
|
183
|
+
retryable: !signal.aborted,
|
|
184
|
+
})
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const { identity } = await request(signal)
|
|
188
|
+
const current = async (signal: AbortSignal) => {
|
|
189
|
+
const result = await request(signal)
|
|
190
|
+
if (result.identity.sessionId !== identity.sessionId)
|
|
191
|
+
throw new IdentityError({
|
|
192
|
+
message: "Node login changed; a new binding is required",
|
|
193
|
+
})
|
|
194
|
+
return result
|
|
195
|
+
}
|
|
196
|
+
const controlRequest = async <Result extends ControlGrant.Proof>(
|
|
197
|
+
expected: ControlGrant.Expected,
|
|
198
|
+
schema: z.ZodType<Result>,
|
|
199
|
+
signal: AbortSignal,
|
|
200
|
+
attempt?: ControlGrant.Attempt,
|
|
201
|
+
command?: "record" | "verify",
|
|
202
|
+
): Promise<Result> => {
|
|
203
|
+
const deadline = AbortSignal.any([signal, AbortSignal.timeout(10_000)])
|
|
204
|
+
try {
|
|
205
|
+
const { credential, identity: live } = await current(deadline)
|
|
206
|
+
deadline.throwIfAborted()
|
|
207
|
+
const response = await fetchImpl(
|
|
208
|
+
`${origin}/v1/agent/node/control/grants/${expected.id}/${
|
|
209
|
+
command
|
|
210
|
+
? `commands/${attempt!.id}${command === "verify" ? "/verify" : ""}`
|
|
211
|
+
: `verify${attempt ? "-attempt" : ""}`
|
|
212
|
+
}`,
|
|
213
|
+
{
|
|
214
|
+
method: command === "record" ? "PUT" : "POST",
|
|
215
|
+
redirect: "error",
|
|
216
|
+
cache: "no-store",
|
|
217
|
+
headers: {
|
|
218
|
+
Accept: "application/json",
|
|
219
|
+
Authorization: `Bearer ${credential}`,
|
|
220
|
+
"Content-Type": "application/json",
|
|
221
|
+
},
|
|
222
|
+
body: JSON.stringify({
|
|
223
|
+
deviceId: owner.nodeId,
|
|
224
|
+
workspaceId: owner.workspaceId,
|
|
225
|
+
sessionId: identity.sessionId,
|
|
226
|
+
revision: expected.revision,
|
|
227
|
+
action: expected.action,
|
|
228
|
+
scope: expected.scope,
|
|
229
|
+
...(attempt ? { attempt } : {}),
|
|
230
|
+
}),
|
|
231
|
+
signal: deadline,
|
|
232
|
+
},
|
|
233
|
+
)
|
|
234
|
+
if (!response.ok) {
|
|
235
|
+
await response.body?.cancel().catch(() => {})
|
|
236
|
+
throw new IdentityError({
|
|
237
|
+
message: "Node grant is unavailable or no longer authorized",
|
|
238
|
+
status: response.status,
|
|
239
|
+
})
|
|
240
|
+
}
|
|
241
|
+
// Grant writes accept 64 KiB; command proofs add a 32 KiB payload.
|
|
242
|
+
const decoded = schema.safeParse(
|
|
243
|
+
await responseJSON(
|
|
244
|
+
response,
|
|
245
|
+
deadline,
|
|
246
|
+
"grant",
|
|
247
|
+
(command ? 128 : 80) * 1024,
|
|
248
|
+
),
|
|
249
|
+
)
|
|
250
|
+
if (!decoded.success)
|
|
251
|
+
throw new IdentityError({ message: "Invalid Node grant response" })
|
|
252
|
+
if (!ControlGrant.matches(decoded.data, expected, live))
|
|
253
|
+
throw new IdentityError({
|
|
254
|
+
message: "Node grant does not match this scope or has expired",
|
|
255
|
+
})
|
|
256
|
+
deadline.throwIfAborted()
|
|
257
|
+
return decoded.data
|
|
258
|
+
} catch (error) {
|
|
259
|
+
if (error instanceof IdentityError) throw error
|
|
260
|
+
throw new IdentityError({
|
|
261
|
+
message: deadline.aborted
|
|
262
|
+
? "Node grant request cancelled or timed out"
|
|
263
|
+
: "Could not verify Node grant",
|
|
264
|
+
})
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
const commandRequest = (
|
|
268
|
+
mode: "record" | "verify",
|
|
269
|
+
input: ControlCommand.Expected,
|
|
270
|
+
signal: AbortSignal,
|
|
271
|
+
) => {
|
|
272
|
+
const expected = ControlCommand.Expected.safeParse(input)
|
|
273
|
+
if (!expected.success)
|
|
274
|
+
throw new IdentityError({
|
|
275
|
+
message: "Invalid expected Node prompt command",
|
|
276
|
+
})
|
|
277
|
+
return controlRequest(
|
|
278
|
+
expected.data,
|
|
279
|
+
ControlCommand.Proof.refine((proof) =>
|
|
280
|
+
ControlCommand.matches(proof, expected.data),
|
|
281
|
+
),
|
|
282
|
+
signal,
|
|
283
|
+
expected.data.attempt,
|
|
284
|
+
mode,
|
|
285
|
+
)
|
|
286
|
+
}
|
|
287
|
+
return {
|
|
288
|
+
identity,
|
|
289
|
+
verify: async (signal: AbortSignal) => (await current(signal)).identity,
|
|
290
|
+
/** Explicit native producer: stores only a binding/hash, never dispatches. */
|
|
291
|
+
recordCommand: async (
|
|
292
|
+
input: ControlCommand.Expected,
|
|
293
|
+
signal: AbortSignal,
|
|
294
|
+
) => commandRequest("record", input, signal),
|
|
295
|
+
/** Reads an already bound command; never creates or renews one. */
|
|
296
|
+
verifyCommand: async (
|
|
297
|
+
input: ControlCommand.Expected,
|
|
298
|
+
signal: AbortSignal,
|
|
299
|
+
) => commandRequest("verify", input, signal),
|
|
300
|
+
/** A grant snapshot alone does not prove a queue actor, command or session. */
|
|
301
|
+
verifyGrant: async (input: ControlGrant.Expected, signal: AbortSignal) => {
|
|
302
|
+
const expected = ControlGrant.Expected.safeParse(input)
|
|
303
|
+
if (!expected.success)
|
|
304
|
+
throw new IdentityError({ message: "Invalid expected Node grant" })
|
|
305
|
+
return controlRequest(expected.data, ControlGrant.Proof, signal)
|
|
306
|
+
},
|
|
307
|
+
/** Joins grant and native queue actor/source/lease; no execution is dispatched. */
|
|
308
|
+
verifyControl: async (
|
|
309
|
+
input: ControlGrant.ExpectedControl,
|
|
310
|
+
signal: AbortSignal,
|
|
311
|
+
) => {
|
|
312
|
+
const expected = ControlGrant.ExpectedControl.safeParse(input)
|
|
313
|
+
if (!expected.success)
|
|
314
|
+
throw new IdentityError({
|
|
315
|
+
message: "Invalid expected Node control attempt",
|
|
316
|
+
})
|
|
317
|
+
const schema = ControlGrant.ControlProof.refine(
|
|
318
|
+
(proof) =>
|
|
319
|
+
proof.attempt.kind === expected.data.attempt.kind &&
|
|
320
|
+
proof.attempt.id === expected.data.attempt.id &&
|
|
321
|
+
proof.attempt.leaseId === expected.data.attempt.leaseId,
|
|
322
|
+
)
|
|
323
|
+
return controlRequest(
|
|
324
|
+
expected.data,
|
|
325
|
+
schema,
|
|
326
|
+
signal,
|
|
327
|
+
expected.data.attempt,
|
|
328
|
+
)
|
|
329
|
+
},
|
|
330
|
+
/** Rechecks a native queue attempt, never authorizes remote session/tools. */
|
|
331
|
+
verifyWork: async (
|
|
332
|
+
execution: Execution,
|
|
333
|
+
signal: AbortSignal,
|
|
334
|
+
): Promise<WorkIdentity> => {
|
|
335
|
+
const parsed = Work.safeParse(execution)
|
|
336
|
+
if (!parsed.success)
|
|
337
|
+
throw new IdentityError({ message: "Invalid expected Node work" })
|
|
338
|
+
const work = parsed.data
|
|
339
|
+
const deadline = AbortSignal.any([signal, AbortSignal.timeout(10_000)])
|
|
340
|
+
try {
|
|
341
|
+
const { credential, identity: live } = await current(deadline)
|
|
342
|
+
deadline.throwIfAborted()
|
|
343
|
+
const response = await fetchImpl(
|
|
344
|
+
`${origin}/v1/agent/node/${work.kind}/${work.id}/verify`,
|
|
345
|
+
{
|
|
346
|
+
method: "POST",
|
|
347
|
+
redirect: "error",
|
|
348
|
+
cache: "no-store",
|
|
349
|
+
headers: {
|
|
350
|
+
Accept: "application/json",
|
|
351
|
+
Authorization: `Bearer ${credential}`,
|
|
352
|
+
"Content-Type": "application/json",
|
|
353
|
+
},
|
|
354
|
+
body: JSON.stringify({
|
|
355
|
+
deviceId: owner.nodeId,
|
|
356
|
+
workspaceId: owner.workspaceId,
|
|
357
|
+
sessionId: identity.sessionId,
|
|
358
|
+
leaseId: work.leaseId,
|
|
359
|
+
directory: work.directory,
|
|
360
|
+
}),
|
|
361
|
+
signal: deadline,
|
|
362
|
+
},
|
|
363
|
+
)
|
|
364
|
+
if (!response.ok) {
|
|
365
|
+
await response.body?.cancel().catch(() => {})
|
|
366
|
+
throw new IdentityError({
|
|
367
|
+
message: "Node work is unavailable or no longer authorized",
|
|
368
|
+
status: response.status,
|
|
369
|
+
})
|
|
370
|
+
}
|
|
371
|
+
const decoded = WorkIdentity.safeParse(
|
|
372
|
+
await responseJSON(response, deadline, "work"),
|
|
373
|
+
)
|
|
374
|
+
if (!decoded.success)
|
|
375
|
+
throw new IdentityError({ message: "Invalid Node work response" })
|
|
376
|
+
const result = decoded.data
|
|
377
|
+
if (
|
|
378
|
+
result.nodeId !== owner.nodeId ||
|
|
379
|
+
result.userId !== owner.userId ||
|
|
380
|
+
result.workspaceId !== owner.workspaceId ||
|
|
381
|
+
result.sessionId !== identity.sessionId ||
|
|
382
|
+
result.kind !== work.kind ||
|
|
383
|
+
result.id !== work.id ||
|
|
384
|
+
result.leaseId !== work.leaseId ||
|
|
385
|
+
result.directory !== work.directory ||
|
|
386
|
+
Date.parse(result.expiresAt) <= Date.now() ||
|
|
387
|
+
Date.parse(result.expiresAt) > Date.parse(live.expiresAt)
|
|
388
|
+
)
|
|
389
|
+
throw new IdentityError({
|
|
390
|
+
message: "Node work does not match this attempt or has expired",
|
|
391
|
+
})
|
|
392
|
+
deadline.throwIfAborted()
|
|
393
|
+
return Object.freeze(result)
|
|
394
|
+
} catch (error) {
|
|
395
|
+
if (error instanceof IdentityError) throw error
|
|
396
|
+
throw new IdentityError({
|
|
397
|
+
message: deadline.aborted
|
|
398
|
+
? "Node work request cancelled or timed out"
|
|
399
|
+
: "Could not verify Node work",
|
|
400
|
+
})
|
|
401
|
+
}
|
|
402
|
+
},
|
|
403
|
+
/** Credentials are reverified after tool permission, for every read/receipt. */
|
|
404
|
+
resources: (execution: Execution): ResourceOptions => ({
|
|
405
|
+
baseUrl: origin,
|
|
406
|
+
kind: execution.kind,
|
|
407
|
+
id: execution.id,
|
|
408
|
+
leaseId: execution.leaseId,
|
|
409
|
+
nodeSessionId: identity.sessionId,
|
|
410
|
+
project: {
|
|
411
|
+
deviceId: owner.nodeId,
|
|
412
|
+
workspaceId: owner.workspaceId,
|
|
413
|
+
directory: execution.directory,
|
|
414
|
+
},
|
|
415
|
+
fetch: fetchImpl,
|
|
416
|
+
token: async (signal) => {
|
|
417
|
+
try {
|
|
418
|
+
return (await current(signal)).credential
|
|
419
|
+
} catch (error) {
|
|
420
|
+
if (error instanceof IdentityError)
|
|
421
|
+
throw new ResourceError({
|
|
422
|
+
message: error.message,
|
|
423
|
+
status: error.status,
|
|
424
|
+
})
|
|
425
|
+
throw new ResourceError({
|
|
426
|
+
message: "Could not verify the resource Node",
|
|
427
|
+
})
|
|
428
|
+
}
|
|
429
|
+
},
|
|
430
|
+
}),
|
|
431
|
+
}
|
|
432
|
+
}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { Context, Duration, Effect, Fiber, Layer, Schema, Stream } from "effect"
|
|
2
|
+
import type { PlatformError } from "effect/PlatformError"
|
|
3
|
+
import { ChildProcess } from "effect/unstable/process"
|
|
4
|
+
import { ChildProcessSpawner } from "effect/unstable/process/ChildProcessSpawner"
|
|
5
|
+
import { CrossSpawnSpawner } from "./spawner"
|
|
6
|
+
import { ServiceGraph } from "../../runtime/effect/service-graph"
|
|
7
|
+
|
|
8
|
+
export class ProcessExecutionError extends Schema.TaggedErrorClass<ProcessExecutionError>()("AppProcessError", {
|
|
9
|
+
command: Schema.String,
|
|
10
|
+
exitCode: Schema.optional(Schema.Number),
|
|
11
|
+
stderr: Schema.optional(Schema.String),
|
|
12
|
+
cause: Schema.optional(Schema.Defect()),
|
|
13
|
+
}) {
|
|
14
|
+
override get message() {
|
|
15
|
+
const detail =
|
|
16
|
+
this.stderr?.trim() || (this.cause instanceof Error ? this.cause.message : this.cause && String(this.cause))
|
|
17
|
+
const status = this.exitCode === undefined ? "" : ` (exit ${this.exitCode})`
|
|
18
|
+
return `Command failed${status}: ${this.command}${detail ? `: ${detail}` : ""}`
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface RunOptions {
|
|
23
|
+
readonly maxOutputBytes?: number
|
|
24
|
+
readonly maxErrorBytes?: number
|
|
25
|
+
readonly signal?: AbortSignal
|
|
26
|
+
readonly timeout?: Duration.Input
|
|
27
|
+
readonly stdin?: string | Uint8Array | Stream.Stream<Uint8Array, PlatformError>
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface RunStreamOptions {
|
|
31
|
+
readonly signal?: AbortSignal
|
|
32
|
+
readonly includeStderr?: boolean
|
|
33
|
+
readonly okExitCodes?: ReadonlyArray<number>
|
|
34
|
+
readonly maxErrorBytes?: number
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface RunResult {
|
|
38
|
+
readonly command: string
|
|
39
|
+
readonly exitCode: number
|
|
40
|
+
readonly stdout: Buffer
|
|
41
|
+
readonly stderr: Buffer
|
|
42
|
+
readonly stdoutTruncated: boolean
|
|
43
|
+
readonly stderrTruncated: boolean
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export type Interface = ChildProcessSpawner["Service"] & {
|
|
47
|
+
readonly run: (command: ChildProcess.Command, options?: RunOptions) => Effect.Effect<RunResult, ProcessExecutionError>
|
|
48
|
+
readonly runStream: (
|
|
49
|
+
command: ChildProcess.Command,
|
|
50
|
+
options?: RunStreamOptions,
|
|
51
|
+
) => Stream.Stream<string, ProcessExecutionError>
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export class Service extends Context.Service<Service, Interface>()("@phins/AppProcess") {}
|
|
55
|
+
|
|
56
|
+
export const requireSuccess = (result: RunResult): Effect.Effect<RunResult, ProcessExecutionError> =>
|
|
57
|
+
result.exitCode === 0
|
|
58
|
+
? Effect.succeed(result)
|
|
59
|
+
: Effect.fail(
|
|
60
|
+
new ProcessExecutionError({
|
|
61
|
+
command: result.command,
|
|
62
|
+
exitCode: result.exitCode,
|
|
63
|
+
stderr: result.stderr.toString("utf8"),
|
|
64
|
+
}),
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
export const requireExitIn =
|
|
68
|
+
(codes: ReadonlyArray<number>) =>
|
|
69
|
+
(result: RunResult): Effect.Effect<RunResult, ProcessExecutionError> =>
|
|
70
|
+
codes.includes(result.exitCode)
|
|
71
|
+
? Effect.succeed(result)
|
|
72
|
+
: Effect.fail(
|
|
73
|
+
new ProcessExecutionError({
|
|
74
|
+
command: result.command,
|
|
75
|
+
exitCode: result.exitCode,
|
|
76
|
+
stderr: result.stderr.toString("utf8"),
|
|
77
|
+
}),
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
const describeCommand = (command: ChildProcess.Command): string => {
|
|
81
|
+
if (command._tag === "StandardCommand") {
|
|
82
|
+
return command.args.length ? `${command.command} ${command.args.join(" ")}` : command.command
|
|
83
|
+
}
|
|
84
|
+
return `${describeCommand(command.left)} | ${describeCommand(command.right)}`
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const wrapError = (description: string, cause: unknown): ProcessExecutionError =>
|
|
88
|
+
cause instanceof ProcessExecutionError ? cause : new ProcessExecutionError({ command: description, cause })
|
|
89
|
+
|
|
90
|
+
export const abortError = (signal: AbortSignal): Error => {
|
|
91
|
+
const reason = signal.reason
|
|
92
|
+
if (reason instanceof Error) return reason
|
|
93
|
+
const err = new Error("Aborted")
|
|
94
|
+
err.name = "AbortError"
|
|
95
|
+
return err
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export const waitForAbort = (signal: AbortSignal) =>
|
|
99
|
+
Effect.callback<never, Error>((resume) => {
|
|
100
|
+
if (signal.aborted) {
|
|
101
|
+
resume(Effect.fail(abortError(signal)))
|
|
102
|
+
return
|
|
103
|
+
}
|
|
104
|
+
const onabort = () => resume(Effect.fail(abortError(signal)))
|
|
105
|
+
signal.addEventListener("abort", onabort, { once: true })
|
|
106
|
+
return Effect.sync(() => signal.removeEventListener("abort", onabort))
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
const normalizeStdin = (
|
|
110
|
+
input: string | Uint8Array | Stream.Stream<Uint8Array, PlatformError>,
|
|
111
|
+
): Stream.Stream<Uint8Array, PlatformError> =>
|
|
112
|
+
typeof input === "string"
|
|
113
|
+
? Stream.make(new TextEncoder().encode(input))
|
|
114
|
+
: input instanceof Uint8Array
|
|
115
|
+
? Stream.make(input)
|
|
116
|
+
: input
|
|
117
|
+
|
|
118
|
+
export const collectStream = (stream: Stream.Stream<Uint8Array, PlatformError>, maxOutputBytes: number | undefined) =>
|
|
119
|
+
Stream.runFold(
|
|
120
|
+
stream,
|
|
121
|
+
() => ({ chunks: [] as Uint8Array[], bytes: 0, truncated: false }),
|
|
122
|
+
(acc, chunk) => {
|
|
123
|
+
if (maxOutputBytes === undefined) {
|
|
124
|
+
acc.chunks.push(chunk)
|
|
125
|
+
acc.bytes += chunk.length
|
|
126
|
+
return acc
|
|
127
|
+
}
|
|
128
|
+
const remaining = maxOutputBytes - acc.bytes
|
|
129
|
+
if (remaining > 0) acc.chunks.push(remaining >= chunk.length ? chunk : chunk.slice(0, remaining))
|
|
130
|
+
acc.bytes += chunk.length
|
|
131
|
+
acc.truncated = acc.truncated || acc.bytes > maxOutputBytes
|
|
132
|
+
return acc
|
|
133
|
+
},
|
|
134
|
+
).pipe(Effect.map((x) => ({ buffer: Buffer.concat(x.chunks), truncated: x.truncated })))
|
|
135
|
+
|
|
136
|
+
export const layer = Layer.effect(
|
|
137
|
+
Service,
|
|
138
|
+
Effect.gen(function* () {
|
|
139
|
+
const spawner = yield* ChildProcessSpawner
|
|
140
|
+
|
|
141
|
+
const runCommand = (command: ChildProcess.Command, options?: RunOptions) => {
|
|
142
|
+
const description = describeCommand(command)
|
|
143
|
+
const collect = Effect.scoped(
|
|
144
|
+
Effect.gen(function* () {
|
|
145
|
+
const handle = yield* spawner.spawn(command)
|
|
146
|
+
const [stdout, stderr, exitCode] = yield* Effect.all(
|
|
147
|
+
[
|
|
148
|
+
collectStream(handle.stdout, options?.maxOutputBytes),
|
|
149
|
+
collectStream(handle.stderr, options?.maxErrorBytes),
|
|
150
|
+
handle.exitCode,
|
|
151
|
+
],
|
|
152
|
+
{ concurrency: "unbounded" },
|
|
153
|
+
)
|
|
154
|
+
return {
|
|
155
|
+
command: description,
|
|
156
|
+
exitCode,
|
|
157
|
+
stdout: stdout.buffer,
|
|
158
|
+
stderr: stderr.buffer,
|
|
159
|
+
stdoutTruncated: stdout.truncated,
|
|
160
|
+
stderrTruncated: stderr.truncated,
|
|
161
|
+
} satisfies RunResult
|
|
162
|
+
}),
|
|
163
|
+
)
|
|
164
|
+
const timed = options?.timeout
|
|
165
|
+
? Effect.timeoutOrElse(collect, {
|
|
166
|
+
duration: options.timeout,
|
|
167
|
+
orElse: () => Effect.fail(new ProcessExecutionError({ command: description, cause: new Error("Timed out") })),
|
|
168
|
+
})
|
|
169
|
+
: collect
|
|
170
|
+
const aborted = options?.signal
|
|
171
|
+
? timed.pipe(
|
|
172
|
+
Effect.raceFirst(
|
|
173
|
+
waitForAbort(options.signal).pipe(Effect.mapError((cause) => wrapError(description, cause))),
|
|
174
|
+
),
|
|
175
|
+
)
|
|
176
|
+
: timed
|
|
177
|
+
return aborted.pipe(Effect.catch((cause) => Effect.fail(wrapError(description, cause))))
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const run = Effect.fn("AppProcess.run")(function* (command: ChildProcess.Command, options?: RunOptions) {
|
|
181
|
+
if (options?.stdin === undefined) return yield* runCommand(command, options)
|
|
182
|
+
if (command._tag !== "StandardCommand") {
|
|
183
|
+
return yield* new ProcessExecutionError({
|
|
184
|
+
command: describeCommand(command),
|
|
185
|
+
cause: new Error("stdin option only supports StandardCommand; received PipedCommand"),
|
|
186
|
+
})
|
|
187
|
+
}
|
|
188
|
+
const next = ChildProcess.make(command.command, command.args, {
|
|
189
|
+
...command.options,
|
|
190
|
+
stdin: normalizeStdin(options.stdin),
|
|
191
|
+
})
|
|
192
|
+
return yield* runCommand(next, options)
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
const runStream = (
|
|
196
|
+
command: ChildProcess.Command,
|
|
197
|
+
options?: RunStreamOptions,
|
|
198
|
+
): Stream.Stream<string, ProcessExecutionError> => {
|
|
199
|
+
const description = describeCommand(command)
|
|
200
|
+
const okExitCodes = options?.okExitCodes
|
|
201
|
+
const built: Stream.Stream<string, ProcessExecutionError | PlatformError> = Stream.unwrap(
|
|
202
|
+
Effect.gen(function* () {
|
|
203
|
+
const handle = yield* spawner.spawn(command)
|
|
204
|
+
const stderrFiber = yield* Effect.forkScoped(
|
|
205
|
+
collectStream(handle.stderr, options?.maxErrorBytes).pipe(Effect.map((x) => x.buffer.toString("utf8"))),
|
|
206
|
+
)
|
|
207
|
+
const source = options?.includeStderr === true ? handle.all : handle.stdout
|
|
208
|
+
const lines = source.pipe(
|
|
209
|
+
Stream.decodeText,
|
|
210
|
+
Stream.splitLines,
|
|
211
|
+
Stream.filter((line) => line.length > 0),
|
|
212
|
+
)
|
|
213
|
+
const tail = Stream.unwrap(
|
|
214
|
+
Effect.gen(function* () {
|
|
215
|
+
const code = yield* handle.exitCode
|
|
216
|
+
if (okExitCodes && okExitCodes.length > 0 && !okExitCodes.includes(code)) {
|
|
217
|
+
const stderr = yield* Fiber.join(stderrFiber)
|
|
218
|
+
return Stream.fail(new ProcessExecutionError({ command: description, exitCode: code, stderr }))
|
|
219
|
+
}
|
|
220
|
+
return Stream.empty
|
|
221
|
+
}),
|
|
222
|
+
)
|
|
223
|
+
return Stream.concat(lines, tail) as Stream.Stream<string, ProcessExecutionError | PlatformError>
|
|
224
|
+
}),
|
|
225
|
+
)
|
|
226
|
+
const mapped = built.pipe(
|
|
227
|
+
Stream.catch((cause): Stream.Stream<string, ProcessExecutionError> => Stream.fail(wrapError(description, cause))),
|
|
228
|
+
)
|
|
229
|
+
if (!options?.signal) return mapped
|
|
230
|
+
const signal = options.signal
|
|
231
|
+
return mapped.pipe(
|
|
232
|
+
Stream.interruptWhen(waitForAbort(signal).pipe(Effect.mapError((cause) => wrapError(description, cause)))),
|
|
233
|
+
)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return Service.of({ ...spawner, run, runStream })
|
|
237
|
+
}),
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
export const defaultLayer = layer.pipe(Layer.provide(CrossSpawnSpawner.defaultLayer))
|
|
241
|
+
export const node = ServiceGraph.node(layer, [CrossSpawnSpawner.node])
|
|
242
|
+
|
|
243
|
+
export * as ProcessRunner from "./runner"
|