@playcraft/cli 0.0.57 → 0.0.59
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 +34 -16
- package/dist/cli-root-help.js +1 -0
- package/dist/commands/build-all.js +10 -9
- package/dist/commands/build.js +13 -12
- package/dist/commands/create.js +26 -33
- package/dist/commands/platform-skills.generated.js +19 -0
- package/dist/commands/skills.js +2 -0
- package/dist/commands/tools-generation.js +1 -1
- package/dist/commands/workspace-runtime.js +54 -0
- package/dist/index.js +3 -1
- package/dist/project-skills/commands.js +72 -0
- package/dist/project-skills/lifecycle.js +28 -0
- package/dist/project-skills/local-run.js +112 -0
- package/dist/project-skills/local-store.js +149 -0
- package/dist/project-skills/messages.js +72 -0
- package/dist/project-skills/reconcile.js +62 -0
- package/dist/project-skills/remote-cache.js +51 -0
- package/dist/project-skills/validation.js +19 -0
- package/dist/remix/clone.js +2 -0
- package/dist/remix/init-template.js +2 -0
- package/dist/remix/pull.js +2 -0
- package/dist/remix/push.js +21 -1
- package/dist/utils/agent-api-client.js +54 -17
- package/dist/utils/tool-operation-journal.js +54 -0
- package/dist/workspace-runtime/codex/app-server.js +395 -0
- package/dist/workspace-runtime/codex/config-toml.js +25 -0
- package/dist/workspace-runtime/codex/jsonrpc-stdio.js +106 -0
- package/dist/workspace-runtime/codex/loopback.js +59 -0
- package/dist/workspace-runtime/codex/native-adapter.js +3 -0
- package/dist/workspace-runtime/main.js +10 -0
- package/dist/workspace-runtime/persistence/journal.js +350 -0
- package/dist/workspace-runtime/processes/managed-writes.js +69 -0
- package/dist/workspace-runtime/serve.js +111 -0
- package/dist/workspace-runtime/server/auth.js +21 -0
- package/dist/workspace-runtime/server/dispatch.js +861 -0
- package/dist/workspace-runtime/server/execution-group.js +88 -0
- package/dist/workspace-runtime/server/http.js +561 -0
- package/dist/workspace-runtime/server/lock.js +53 -0
- package/dist/workspace-runtime/server/types.js +1 -0
- package/dist/workspace-runtime/workspaces/context-error.js +2 -0
- package/dist/workspace-runtime/workspaces/file-snapshot.js +213 -0
- package/dist/workspace-runtime/workspaces/files.js +143 -0
- package/dist/workspace-runtime/workspaces/git.js +242 -0
- package/dist/workspace-runtime/workspaces/json5-edit.js +170 -0
- package/dist/workspace-runtime/workspaces/parameters.js +272 -0
- package/dist/workspace-runtime/workspaces/prepare.js +364 -0
- package/dist/workspace-runtime/workspaces/registry.js +107 -0
- package/dist/workspace-runtime/workspaces/revisions.js +31 -0
- package/package.json +6 -3
- package/project-template-v2/.claude/agents/artist.md +82 -0
- package/project-template-v2/.claude/agents/developer.md +153 -0
- package/project-template-v2/.claude/agents/game-designer.md +264 -0
- package/project-template-v2/.claude/agents/refs/artist-art-style-catalog.md +533 -0
- package/project-template-v2/.claude/agents/refs/artist-color-audio-recipes.md +153 -0
- package/project-template-v2/.claude/agents/refs/artist-dimension-axis.md +27 -0
- package/project-template-v2/.claude/agents/refs/artist-master-composite-recipes.md +208 -0
- package/project-template-v2/.claude/agents/refs/atom-skill-library.md +81 -0
- package/project-template-v2/.claude/agents/refs/developer-impl-cookbook.md +432 -0
- package/project-template-v2/.claude/agents/refs/framework-5-component-filter.md +252 -0
- package/project-template-v2/.claude/agents/refs/framework-game-feel-juice.md +266 -0
- package/project-template-v2/.claude/agents/refs/framework-mda.md +147 -0
- package/project-template-v2/.claude/agents/refs/game-designer-gameplay-sufficiency.md +123 -0
- package/project-template-v2/.claude/agents/refs/ta-3d-flip-recipe.md +88 -0
- package/project-template-v2/.claude/agents/refs/ta-atlas-deliverable-standard.md +67 -0
- package/project-template-v2/.claude/agents/refs/ta-batch-pipeline-recipes.md +120 -0
- package/project-template-v2/.claude/agents/refs/ta-image-generation-detail.md +300 -0
- package/project-template-v2/.claude/agents/refs/ta-image-ops-reference.md +495 -0
- package/project-template-v2/.claude/agents/refs/ta-pipeline-cookbook.md +1141 -0
- package/project-template-v2/.claude/agents/refs/ta-tools-reference.md +111 -0
- package/project-template-v2/.claude/agents/refs/ta-vfx-preset-catalog.md +365 -0
- package/project-template-v2/.claude/agents/refs/threejs-cannon-pitfalls.md +412 -0
- package/project-template-v2/.claude/agents/reviewer.md +75 -0
- package/project-template-v2/.claude/agents/technical-artist.md +86 -0
- package/project-template-v2/.claude/hooks/snapshot-milestone.mjs +243 -0
- package/project-template-v2/.claude/hooks/user-prompt.mjs +133 -0
- package/project-template-v2/.claude/settings.json +33 -0
- package/project-template-v2/.claude/skills/brainstorming/SKILL.md +161 -0
- package/project-template-v2/.claude/skills/brainstorming/scripts/frame-template.html +270 -0
- package/project-template-v2/.claude/skills/brainstorming/scripts/helper.js +177 -0
- package/project-template-v2/.claude/skills/brainstorming/scripts/server.cjs +354 -0
- package/project-template-v2/.claude/skills/brainstorming/scripts/start-server.sh +148 -0
- package/project-template-v2/.claude/skills/brainstorming/scripts/stop-server.sh +56 -0
- package/project-template-v2/.claude/skills/brainstorming/scripts/wait-for-selection.sh +62 -0
- package/project-template-v2/.claude/skills/brainstorming/spec-document-reviewer-prompt.md +49 -0
- package/project-template-v2/.claude/skills/brainstorming/visual-companion.md +309 -0
- package/project-template-v2/.claude/skills/playcraft-ad-psychology/SKILL.md +182 -0
- package/project-template-v2/.claude/skills/playcraft-art-style-guide/SKILL.md +123 -0
- package/project-template-v2/.claude/skills/playcraft-asset-state-sheet/SKILL.md +205 -0
- package/project-template-v2/.claude/skills/playcraft-audio-generation/SKILL.md +280 -0
- package/project-template-v2/.claude/skills/playcraft-batch-pipeline/SKILL.md +184 -0
- package/project-template-v2/.claude/skills/playcraft-build-optimizer/SKILL.md +306 -0
- package/project-template-v2/.claude/skills/playcraft-image-generation/SKILL.md +298 -0
- package/project-template-v2/.claude/skills/playcraft-image-generation/reference/build-sprite-sheet.template.mjs +123 -0
- package/project-template-v2/.claude/skills/playcraft-image-generation/reference/compare-style.template.mjs +254 -0
- package/project-template-v2/.claude/skills/playcraft-image-generation/reference/gen-batch-sprite.template.mjs +324 -0
- package/project-template-v2/.claude/skills/playcraft-image-generation/reference/gen-batch.template.mjs +97 -0
- package/project-template-v2/.claude/skills/playcraft-image-generation/reference/gen-edit-variants.template.mjs +118 -0
- package/project-template-v2/.claude/skills/playcraft-image-generation/reference/process-batch.template.mjs +137 -0
- package/project-template-v2/.claude/skills/playcraft-image-generation/reference/prompt-cookbook.md +397 -0
- package/project-template-v2/.claude/skills/playcraft-image-generation/reference/validate-sprite-sheet.template.mjs +296 -0
- package/project-template-v2/.claude/skills/playcraft-image-ops/SKILL.md +122 -0
- package/project-template-v2/.claude/skills/playcraft-image-processing/SKILL.md +219 -0
- package/project-template-v2/.claude/skills/playcraft-masking/SKILL.md +373 -0
- package/project-template-v2/.claude/skills/playcraft-playable-optimization/SKILL.md +161 -0
- package/project-template-v2/.claude/skills/playcraft-research/SKILL.md +215 -0
- package/project-template-v2/.claude/skills/playcraft-skill-recommender/SKILL.md +382 -0
- package/project-template-v2/.claude/skills/playcraft-sprite-generation/SKILL.md +423 -0
- package/project-template-v2/.claude/skills/playcraft-sprite-remix/SKILL.md +158 -0
- package/project-template-v2/.claude/skills/playcraft-sprite-sheet/SKILL.md +100 -0
- package/project-template-v2/.claude/skills/playcraft-storyboard/SKILL.md +167 -0
- package/project-template-v2/.claude/skills/playcraft-style-qa/SKILL.md +270 -0
- package/project-template-v2/.claude/skills/playcraft-text-rendering/SKILL.md +236 -0
- package/project-template-v2/.claude/skills/playcraft-vfx-animation/SKILL.md +130 -0
- package/project-template-v2/.claude/skills/playwright-cli/SKILL.md +390 -0
- package/project-template-v2/.claude/skills/playwright-cli/references/element-attributes.md +23 -0
- package/project-template-v2/.claude/skills/playwright-cli/references/playwright-tests.md +39 -0
- package/project-template-v2/.claude/skills/playwright-cli/references/request-mocking.md +87 -0
- package/project-template-v2/.claude/skills/playwright-cli/references/running-code.md +240 -0
- package/project-template-v2/.claude/skills/playwright-cli/references/session-management.md +226 -0
- package/project-template-v2/.claude/skills/playwright-cli/references/spec-driven-testing.md +308 -0
- package/project-template-v2/.claude/skills/playwright-cli/references/storage-state.md +275 -0
- package/project-template-v2/.claude/skills/playwright-cli/references/test-generation.md +134 -0
- package/project-template-v2/.claude/skills/playwright-cli/references/tracing.md +142 -0
- package/project-template-v2/.claude/skills/playwright-cli/references/video-recording.md +153 -0
- package/project-template-v2/.claude/skills/session-analyzer/SKILL.md +386 -0
- package/project-template-v2/.claude/skills/session-analyzer/scripts/execution-breakdown.mjs +182 -0
- package/project-template-v2/.claude/skills/session-analyzer/scripts/find-turns.mjs +72 -0
- package/project-template-v2/.claude/skills/session-analyzer/scripts/heavy-output.mjs +121 -0
- package/project-template-v2/.claude/skills/session-analyzer/scripts/resolve-session.mjs +102 -0
- package/project-template-v2/.claude/skills/session-analyzer/scripts/subagent-stats.mjs +127 -0
- package/project-template-v2/.claude/skills/session-analyzer/scripts/subagent-tool-timeline.mjs +106 -0
- package/project-template-v2/.claude/skills/session-analyzer/scripts/time-gaps.mjs +128 -0
- package/project-template-v2/.claude/skills/session-analyzer/scripts/turn-timeline.mjs +67 -0
- package/project-template-v2/.claude/snapshot.mjs +263 -0
- package/project-template-v2/.playcraft/skills.lock.json +152 -0
- package/project-template-v2/CLAUDE.md +146 -0
- package/project-template-v2/assets/audio/bgm/.gitkeep +0 -0
- package/project-template-v2/assets/audio/sfx/.gitkeep +0 -0
- package/project-template-v2/assets/bundles/.gitkeep +0 -0
- package/project-template-v2/assets/images/bg/.gitkeep +0 -0
- package/project-template-v2/assets/images/reference/.gitkeep +0 -0
- package/project-template-v2/assets/images/storyboard/.gitkeep +0 -0
- package/project-template-v2/assets/images/tiles/.gitkeep +0 -0
- package/project-template-v2/assets/images/ui/.gitkeep +0 -0
- package/project-template-v2/assets/images/vfx/.gitkeep +0 -0
- package/project-template-v2/assets/models/.gitkeep +0 -0
- package/project-template-v2/docs/harness/iteration-1/context-flow.md +254 -0
- package/project-template-v2/docs/harness/iteration-1/generate-flow.md +91 -0
- package/project-template-v2/docs/harness/iteration-1/ideate-flow.md +214 -0
- package/project-template-v2/docs/harness/iteration-1/optimize-flow.md +75 -0
- package/project-template-v2/docs/harness/iteration-1/wrapup-flow.md +63 -0
- package/project-template-v2/docs/harness/iteration-2/context-flow.md +223 -0
- package/project-template-v2/docs/harness/iteration-2/generate-flow.md +129 -0
- package/project-template-v2/docs/harness/iteration-2/ideate-flow.md +267 -0
- package/project-template-v2/docs/harness/iteration-2/optimize-flow.md +164 -0
- package/project-template-v2/docs/harness/iteration-2/wrapup-flow.md +115 -0
- package/project-template-v2/docs/harness/orchestrator-flow.md +364 -0
- package/project-template-v2/docs/project-state.json +60 -0
- package/project-template-v2/docs/project-state.md +72 -0
- package/project-template-v2/docs/standards/README.md +225 -0
- package/project-template-v2/docs/standards/agent-behavior-standards.md +174 -0
- package/project-template-v2/docs/standards/artifacts/design-brief.md +19 -0
- package/project-template-v2/docs/standards/artifacts/design.md +22 -0
- package/project-template-v2/docs/standards/artifacts/game-code.md +41 -0
- package/project-template-v2/docs/standards/artifacts/todo-list.md +41 -0
- package/project-template-v2/docs/standards/iter1-agent-behavior-standards.md +343 -0
- package/project-template-v2/game/index.ts +18 -0
- package/project-template-v2/globals.d.ts +51 -0
- package/project-template-v2/index.css +34 -0
- package/project-template-v2/index.html +18 -0
- package/project-template-v2/main.ts +9 -0
- package/project-template-v2/package.json +46 -0
- package/project-template-v2/skills/_shared/scripts/dispatch-clear.mjs +31 -0
- package/project-template-v2/skills/_shared/scripts/dispatch-set.mjs +86 -0
- package/project-template-v2/skills/_shared/scripts/dod-check.mjs +153 -0
- package/project-template-v2/skills/_shared/scripts/handoff-append.mjs +70 -0
- package/project-template-v2/skills/_shared/scripts/lib/validator-artifacts.mjs +91 -0
- package/project-template-v2/skills/_shared/scripts/pipeline/dod-config.mjs +131 -0
- package/project-template-v2/skills/_shared/scripts/pipeline/index.mjs +80 -0
- package/project-template-v2/skills/_shared/scripts/pipeline/iteration-1-core.mjs +90 -0
- package/project-template-v2/skills/_shared/scripts/pipeline/iteration-2-wrap.mjs +93 -0
- package/project-template-v2/skills/_shared/scripts/pipeline/iteration-3-visual.mjs +23 -0
- package/project-template-v2/skills/_shared/scripts/render-project-state.mjs +230 -0
- package/project-template-v2/skills/_shared/scripts/state-advance-stage.mjs +58 -0
- package/project-template-v2/skills/_shared/scripts/state-get.mjs +339 -0
- package/project-template-v2/skills/_shared/scripts/state-handoff.mjs +39 -0
- package/project-template-v2/skills/_shared/scripts/state-set.mjs +94 -0
- package/project-template-v2/skills/_shared/scripts/state-store.mjs +783 -0
- package/project-template-v2/skills/_shared/scripts/todo-add.mjs +88 -0
- package/project-template-v2/skills/_shared/scripts/todo-get.mjs +130 -0
- package/project-template-v2/skills/_shared/scripts/todo-remove.mjs +33 -0
- package/project-template-v2/skills/_shared/scripts/todo-set.mjs +47 -0
- package/project-template-v2/skills/_shared/scripts/verify-asset-code-sync.mjs +268 -0
- package/project-template-v2/skills/_shared/scripts/verify-env.mjs +285 -0
- package/project-template-v2/skills/_shared/scripts/verify-placeholders.mjs +161 -0
- package/project-template-v2/skills/playable-autoplay/SKILL.md +176 -0
- package/project-template-v2/skills/playable-autoplay/agents/openai.yaml +4 -0
- package/project-template-v2/skills/playable-debug/SKILL.md +116 -0
- package/project-template-v2/skills/playable-debug/agents/openai.yaml +4 -0
- package/project-template-v2/skills/playable-debug/references/debug-config.md +40 -0
- package/project-template-v2/skills/playable-record/SKILL.md +140 -0
- package/project-template-v2/skills/playable-record/scripts/lib/dev-server.mjs +104 -0
- package/project-template-v2/skills/playable-record/scripts/lib/record-audio-bridge.js +141 -0
- package/project-template-v2/skills/playable-record/scripts/record-playable.mjs +425 -0
- package/project-template-v2/skills/playable-record/scripts/verify-contract.mjs +261 -0
- package/project-template-v2/skills/playable-record/scripts/verify-firstwin.mjs +398 -0
- package/project-template-v2/skills/playable-record/scripts/verify-fusion.mjs +322 -0
- package/project-template-v2/skills/playable-record/scripts/verify-lifecycle.mjs +105 -0
- package/project-template-v2/skills/playable-record/scripts/verify-vlm-video.mjs +230 -0
- package/project-template-v2/skills/playable-validate/SKILL.md +61 -0
- package/project-template-v2/skills/playable-validate/validation-rules.md +14 -0
- package/project-template-v2/skills/playable-verify-ui/SKILL.md +63 -0
- package/project-template-v2/skills/playable-verify-ui/scripts/lib/__init__.py +1 -0
- package/project-template-v2/skills/playable-verify-ui/scripts/lib/config_expr.js +218 -0
- package/project-template-v2/skills/playable-verify-ui/scripts/lib/node_utils.js +51 -0
- package/project-template-v2/skills/playable-verify-ui/scripts/lib/profile_loader.py +59 -0
- package/project-template-v2/skills/playable-verify-ui/scripts/lib/report.py +106 -0
- package/project-template-v2/skills/playable-verify-ui/scripts/lib/runtime_contract.js +116 -0
- package/project-template-v2/skills/playable-verify-ui/scripts/verify_fetch_antipatterns.py +59 -0
- package/project-template-v2/skills/playable-verify-ui/scripts/verify_hardcoded_layout.js +64 -0
- package/project-template-v2/skills/playable-verify-ui/scripts/verify_runtime_contract.js +58 -0
- package/project-template-v2/skills/playable-verify-ui/scripts/verify_text_overlap.py +333 -0
- package/project-template-v2/ta-workspace/scripts/.gitkeep +0 -0
- package/project-template-v2/tsconfig.json +20 -0
- package/project-template-v2/vite.config.ts +27 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
function enqueue(state, action) {
|
|
2
|
+
const run = state.executionQueue.then(action, action);
|
|
3
|
+
state.executionQueue = run.then(() => undefined, () => undefined);
|
|
4
|
+
return run;
|
|
5
|
+
}
|
|
6
|
+
function processAlive(pid) {
|
|
7
|
+
try {
|
|
8
|
+
process.kill(pid, 0);
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function requireRestartable(state) {
|
|
16
|
+
const native = state.native;
|
|
17
|
+
if (!native?.executionProcess || !native.replaceProcess) {
|
|
18
|
+
throw new Error('execution_group_unavailable');
|
|
19
|
+
}
|
|
20
|
+
return native;
|
|
21
|
+
}
|
|
22
|
+
async function demoteSessions(state) {
|
|
23
|
+
await state.journal.replaceSessions(state.journal.listSessions().map((session) => ({ ...session, recoverability: 'history_only' })));
|
|
24
|
+
}
|
|
25
|
+
async function advanceEpoch(state, input) {
|
|
26
|
+
const runtimeEpoch = state.runtimeEpoch + 1;
|
|
27
|
+
await state.journal.writeExecutionEpoch({
|
|
28
|
+
runtimeEpoch,
|
|
29
|
+
pid: input.pid,
|
|
30
|
+
startedAt: input.startedAt,
|
|
31
|
+
previousPid: input.previousPid,
|
|
32
|
+
stopConfirmed: input.stopConfirmed,
|
|
33
|
+
});
|
|
34
|
+
state.runtimeEpoch = runtimeEpoch;
|
|
35
|
+
await demoteSessions(state);
|
|
36
|
+
}
|
|
37
|
+
export function executionGroupRestarted(state, checkpointEpoch) {
|
|
38
|
+
return state.runtimeEpoch !== checkpointEpoch;
|
|
39
|
+
}
|
|
40
|
+
export async function replaceExecutionGroup(state, options) {
|
|
41
|
+
return enqueue(state, async () => {
|
|
42
|
+
const native = requireRestartable(state);
|
|
43
|
+
const previous = native.executionProcess();
|
|
44
|
+
if (!previous)
|
|
45
|
+
throw new Error('execution_group_missing');
|
|
46
|
+
const next = await native.replaceProcess();
|
|
47
|
+
await advanceEpoch(state, {
|
|
48
|
+
pid: next.pid,
|
|
49
|
+
startedAt: next.startedAt,
|
|
50
|
+
previousPid: previous.pid,
|
|
51
|
+
stopConfirmed: false,
|
|
52
|
+
});
|
|
53
|
+
if (options.confirmStop)
|
|
54
|
+
await confirmStopped(state, previous.pid);
|
|
55
|
+
return {
|
|
56
|
+
runtimeEpoch: state.runtimeEpoch,
|
|
57
|
+
stopConfirmed: state.journal.executionGroup().stopConfirmed,
|
|
58
|
+
pid: next.pid,
|
|
59
|
+
previousPid: previous.pid,
|
|
60
|
+
};
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
export async function confirmExecutionGroupStopped(state, previousPid) {
|
|
64
|
+
return enqueue(state, () => confirmStopped(state, previousPid));
|
|
65
|
+
}
|
|
66
|
+
async function confirmStopped(state, previousPid) {
|
|
67
|
+
if (processAlive(previousPid))
|
|
68
|
+
return false;
|
|
69
|
+
await state.journal.setExecutionStopConfirmed(true);
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
export async function handleExecutionGroupCrash(state, exitedPid) {
|
|
73
|
+
return enqueue(state, async () => {
|
|
74
|
+
const group = state.journal.executionGroup();
|
|
75
|
+
if (group.pid !== exitedPid)
|
|
76
|
+
return;
|
|
77
|
+
const native = state.native;
|
|
78
|
+
if (!native?.replaceProcess)
|
|
79
|
+
return;
|
|
80
|
+
const next = await native.replaceProcess();
|
|
81
|
+
await advanceEpoch(state, {
|
|
82
|
+
pid: next.pid,
|
|
83
|
+
startedAt: next.startedAt,
|
|
84
|
+
previousPid: exitedPid,
|
|
85
|
+
stopConfirmed: true,
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
}
|
|
@@ -0,0 +1,561 @@
|
|
|
1
|
+
import { readObservedFile, withObservedFile } from '../workspaces/revisions.js';
|
|
2
|
+
import { registerWorkspace, revisionScope, workspaceRoot, WorkspaceContextError } from '../workspaces/registry.js';
|
|
3
|
+
import { randomUUID } from 'node:crypto';
|
|
4
|
+
import { workspaceRegistrationRequestSchema, HTTP_STATUS_BY_ERROR, assertLeaseFence, buildHandshakeDocument, capability, findUntrustedRuntimeFields, hashOperationPayload, instanceRebuildRequestSchema, operationAcceptRequestSchema, operationKindSchema, operationScopeKey, resolveIdempotentAccept, runtimeError, unavailableRuntimeCapabilities, } from '@playcraft/common/cloud-workspace/runtime';
|
|
5
|
+
import { controlTokenMatches, extractControlToken } from './auth.js';
|
|
6
|
+
import { canExecuteKind, executeOperation } from './dispatch.js';
|
|
7
|
+
import { InvalidWorkspacePathError, listWorkspaceFiles, } from '../workspaces/files.js';
|
|
8
|
+
import { materializeRemoteSnapshot } from '../workspaces/file-snapshot.js';
|
|
9
|
+
import { GitWorkspaceError, readWorkspaceGitDiff, readWorkspaceGitGraph, readWorkspaceGitStatus, } from '../workspaces/git.js';
|
|
10
|
+
import { readWorkspaceParameters } from '../workspaces/parameters.js';
|
|
11
|
+
export async function handleRuntimeRequest(state, request, response) {
|
|
12
|
+
const url = new URL(request.url ?? '/', 'http://127.0.0.1');
|
|
13
|
+
if (!controlTokenMatches(state.config.controlToken, extractControlToken(request.headers))) {
|
|
14
|
+
writeError(response, runtimeError('unauthenticated', 'control_token_required'));
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
try {
|
|
18
|
+
if (request.method === 'GET' && url.pathname === '/runtime/health') {
|
|
19
|
+
writeJson(response, 200, { status: 'ok', handshake: handshake(state) });
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (request.method === 'GET' && url.pathname === '/runtime/identity') {
|
|
23
|
+
const { runtimeId, userId, environment, generation } = state.config;
|
|
24
|
+
writeJson(response, 200, {
|
|
25
|
+
runtimeId,
|
|
26
|
+
userId,
|
|
27
|
+
environment,
|
|
28
|
+
generation,
|
|
29
|
+
runtimeEpoch: state.runtimeEpoch,
|
|
30
|
+
instanceToken: state.instanceToken,
|
|
31
|
+
workspaceMode: state.config.workspaceMode,
|
|
32
|
+
executionGroup: {
|
|
33
|
+
replaced: state.journal.executionGroup().replaced,
|
|
34
|
+
stopConfirmed: state.journal.executionGroup().stopConfirmed,
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (request.method === 'GET' && url.pathname === '/runtime/approvals') {
|
|
40
|
+
writeJson(response, 200, { approvals: state.journal.listApprovals() });
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (request.method === 'POST' && url.pathname === '/runtime/workspaces/register') {
|
|
44
|
+
if (writeDestroyed(state, response))
|
|
45
|
+
return;
|
|
46
|
+
const parsed = workspaceRegistrationRequestSchema.safeParse(await readJson(request));
|
|
47
|
+
if (!parsed.success || parsed.data.generation !== state.config.generation || parsed.data.runtimeEpoch !== state.runtimeEpoch) {
|
|
48
|
+
writeJson(response, 409, { error: 'invalid_workspace_registration' });
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const binding = await registerWorkspace(state, parsed.data.binding, parsed.data.leaseFence, parsed.data.preparation);
|
|
52
|
+
writeJson(response, 200, { workspaceId: binding.workspaceId, status: 'registered' });
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (request.method === 'GET' && url.pathname === '/runtime/handshake') {
|
|
56
|
+
writeJson(response, 200, handshake(state));
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (isUnimplementedWorkbenchPath(url.pathname)) {
|
|
60
|
+
if (writeDestroyed(state, response))
|
|
61
|
+
return;
|
|
62
|
+
writeError(response, runtimeError('capability_unavailable', 'capability_not_implemented'));
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (request.method === 'POST' && url.pathname === '/runtime/instance/rebuild') {
|
|
66
|
+
await rebuildInstanceFiles(state, request, response);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (request.method === 'GET' && url.pathname === '/runtime/checkpoint') {
|
|
70
|
+
const checkpointId = url.searchParams.get('checkpointId') ?? '';
|
|
71
|
+
const checkpoint = state.journal.getCheckpoint(checkpointId);
|
|
72
|
+
if (!checkpoint) {
|
|
73
|
+
writeJson(response, 404, { error: 'unknown_checkpoint' });
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
writeJson(response, 200, {
|
|
77
|
+
checkpoint,
|
|
78
|
+
fileCheckpointId: state.journal.fileCheckpointId() ?? null,
|
|
79
|
+
consistentCheckpointId: state.journal.consistentCheckpointId() ?? null,
|
|
80
|
+
});
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (request.method === 'GET' && url.pathname === '/runtime/events') {
|
|
84
|
+
const after = Number(url.searchParams.get('after') ?? '0');
|
|
85
|
+
writeJson(response, 200, {
|
|
86
|
+
events: state.journal.listEvents(Number.isInteger(after) ? after : 0),
|
|
87
|
+
});
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (request.method === 'GET' && url.pathname === '/runtime/files') {
|
|
91
|
+
if (writeDestroyed(state, response))
|
|
92
|
+
return;
|
|
93
|
+
writeJson(response, 200, await listWorkspaceFiles(await workspaceRoot(state, url.searchParams.get('workspaceId') ?? undefined), state.journal.fileRevisions(revisionScope(state, url.searchParams.get('workspaceId') ?? undefined))));
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (request.method === 'GET' && url.pathname === '/runtime/file') {
|
|
97
|
+
if (writeDestroyed(state, response))
|
|
98
|
+
return;
|
|
99
|
+
const relativePath = url.searchParams.get('path') ?? '';
|
|
100
|
+
try {
|
|
101
|
+
writeJson(response, 200, await readObservedFile(state, url.searchParams.get('workspaceId') ?? undefined, relativePath));
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
if (error instanceof InvalidWorkspacePathError) {
|
|
105
|
+
writeJson(response, 400, { error: 'invalid_workspace_path' });
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if (error.code === 'ENOENT') {
|
|
109
|
+
writeJson(response, 404, { error: 'file_not_found' });
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
throw error;
|
|
113
|
+
}
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (request.method === 'GET' && url.pathname === '/runtime/git/status') {
|
|
117
|
+
await writeGitRead(state, response, async () => readWorkspaceGitStatus(await workspaceRoot(state, url.searchParams.get('workspaceId') ?? undefined)));
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (request.method === 'GET' && url.pathname === '/runtime/git/diff') {
|
|
121
|
+
await writeGitRead(state, response, async () => readWorkspaceGitDiff(await workspaceRoot(state, url.searchParams.get('workspaceId') ?? undefined)));
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (request.method === 'GET' && url.pathname === '/runtime/git/graph') {
|
|
125
|
+
await writeGitRead(state, response, async () => readWorkspaceGitGraph(await workspaceRoot(state, url.searchParams.get('workspaceId') ?? undefined)));
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (request.method === 'GET' && url.pathname === '/runtime/parameters') {
|
|
129
|
+
if (writeDestroyed(state, response))
|
|
130
|
+
return;
|
|
131
|
+
writeJson(response, 200, await readObservedParameters(state, url.searchParams.get('workspaceId') ?? undefined));
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
if (request.method === 'GET' && url.pathname === '/runtime/terminals') {
|
|
135
|
+
if (writeDestroyed(state, response))
|
|
136
|
+
return;
|
|
137
|
+
writeJson(response, 200, {
|
|
138
|
+
terminals: state.journal.listTerminals(url.searchParams.get('workspaceId') ?? undefined),
|
|
139
|
+
});
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (url.pathname.startsWith('/runtime/terminals/')) {
|
|
143
|
+
await handleTerminalIdentity(state, request, response, url.pathname.slice('/runtime/terminals/'.length));
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
if (request.method === 'GET' && url.pathname === '/runtime/operations') {
|
|
147
|
+
const kind = operationKindSchema.safeParse(url.searchParams.get('kind'));
|
|
148
|
+
const clientRequestId = url.searchParams.get('clientRequestId');
|
|
149
|
+
if (!kind.success || !clientRequestId) {
|
|
150
|
+
writeJson(response, 400, { error: 'invalid_operation_lookup' });
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const operation = state.journal.getOperation(operationScopeKey({
|
|
154
|
+
environment: state.config.environment,
|
|
155
|
+
userId: state.config.userId,
|
|
156
|
+
kind: kind.data,
|
|
157
|
+
clientRequestId,
|
|
158
|
+
workspaceId: url.searchParams.get('workspaceId') ?? undefined,
|
|
159
|
+
sessionId: url.searchParams.get('sessionId') ?? undefined,
|
|
160
|
+
}));
|
|
161
|
+
writeJson(response, operation ? 200 : 404, operation ?? { error: 'operation_not_found' });
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
if (request.method === 'GET' && url.pathname.startsWith('/runtime/operations/')) {
|
|
165
|
+
const operationId = url.pathname.slice('/runtime/operations/'.length);
|
|
166
|
+
const operation = state.journal.getOperationById(operationId);
|
|
167
|
+
if (!operation) {
|
|
168
|
+
writeJson(response, 404, { error: 'operation_not_found' });
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
writeJson(response, 200, operation);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
if (request.method === 'POST' && url.pathname === '/runtime/operations') {
|
|
175
|
+
await acceptOperation(state, request, response);
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
writeJson(response, 404, { error: 'not_found' });
|
|
179
|
+
}
|
|
180
|
+
catch (error) {
|
|
181
|
+
if (error instanceof WorkspaceContextError) {
|
|
182
|
+
writeJson(response, 409, { error: error.message });
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
writeJson(response, 500, {
|
|
186
|
+
error: 'internal_error',
|
|
187
|
+
reason: error instanceof Error ? error.message : 'unknown',
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function handshake(state) {
|
|
192
|
+
const destroyed = state.journal.destroyed();
|
|
193
|
+
const conversationReady = Boolean(state.native?.ready()) && !destroyed;
|
|
194
|
+
const filesReady = !destroyed;
|
|
195
|
+
const gitReady = state.gitAvailable && !destroyed;
|
|
196
|
+
const capabilities = unavailableRuntimeCapabilities('adapter_not_started').map((item) => {
|
|
197
|
+
if (item.name === 'conversation') {
|
|
198
|
+
if (conversationReady)
|
|
199
|
+
return capability('conversation', true);
|
|
200
|
+
return destroyed ? capability('conversation', false, 'runtime_destroyed') : item;
|
|
201
|
+
}
|
|
202
|
+
if (item.name === 'files') {
|
|
203
|
+
return filesReady
|
|
204
|
+
? capability('files', true)
|
|
205
|
+
: capability('files', false, 'runtime_destroyed');
|
|
206
|
+
}
|
|
207
|
+
if (item.name === 'git') {
|
|
208
|
+
if (gitReady)
|
|
209
|
+
return capability('git', true);
|
|
210
|
+
if (destroyed)
|
|
211
|
+
return capability('git', false, 'runtime_destroyed');
|
|
212
|
+
if (!state.gitAvailable)
|
|
213
|
+
return capability('git', false, 'git_binary_missing');
|
|
214
|
+
return item;
|
|
215
|
+
}
|
|
216
|
+
if (item.name === 'terminal') {
|
|
217
|
+
return destroyed
|
|
218
|
+
? capability('terminal', false, 'runtime_destroyed')
|
|
219
|
+
: capability('terminal', true);
|
|
220
|
+
}
|
|
221
|
+
if (item.name === 'desktop') {
|
|
222
|
+
return destroyed
|
|
223
|
+
? capability('desktop', false, 'runtime_destroyed')
|
|
224
|
+
: capability('desktop', false, 'display_not_started');
|
|
225
|
+
}
|
|
226
|
+
if (item.name === 'parameters') {
|
|
227
|
+
return destroyed
|
|
228
|
+
? capability('parameters', false, 'runtime_destroyed')
|
|
229
|
+
: capability('parameters', true);
|
|
230
|
+
}
|
|
231
|
+
if (item.name === 'artifacts.singleFile') {
|
|
232
|
+
return destroyed
|
|
233
|
+
? capability('artifacts.singleFile', false, 'runtime_destroyed')
|
|
234
|
+
: capability('artifacts.singleFile', false, 'capability_not_implemented');
|
|
235
|
+
}
|
|
236
|
+
return item;
|
|
237
|
+
});
|
|
238
|
+
return buildHandshakeDocument({
|
|
239
|
+
protocolMajor: state.config.protocolMajor,
|
|
240
|
+
protocolMinor: state.config.protocolMinor,
|
|
241
|
+
cliVersion: state.config.cliVersion,
|
|
242
|
+
codexVersion: state.config.codexVersion,
|
|
243
|
+
imageDigest: state.config.imageDigest,
|
|
244
|
+
phase: destroyed ? 'unavailable' : conversationReady ? 'ready' : 'awaiting_native',
|
|
245
|
+
generations: {
|
|
246
|
+
generation: state.config.generation,
|
|
247
|
+
runtimeEpoch: state.runtimeEpoch,
|
|
248
|
+
leaseFence: state.acceptedLeaseFence,
|
|
249
|
+
},
|
|
250
|
+
capabilities,
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
async function acceptOperation(state, request, response) {
|
|
254
|
+
const body = await readJson(request);
|
|
255
|
+
if (!body || typeof body !== 'object' || Array.isArray(body)) {
|
|
256
|
+
writeJson(response, 400, { error: 'invalid_operation' });
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
// leaseFence 由 Runtime Server 派发,本控制面单独校验;其余代次/身份/根目录字段仍只来自引导配置。
|
|
260
|
+
const untrusted = findUntrustedRuntimeFields(body).filter((field) => field !== 'leaseFence');
|
|
261
|
+
if (untrusted.length > 0) {
|
|
262
|
+
writeError(response, runtimeError('untrusted_runtime_field', untrusted.join(',')));
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
const incomingFence = Number(body.leaseFence ?? state.acceptedLeaseFence);
|
|
266
|
+
if (!Number.isInteger(incomingFence) || incomingFence < 0) {
|
|
267
|
+
writeJson(response, 400, { error: 'invalid_lease_fence' });
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
if (assertLeaseFence(incomingFence, state.acceptedLeaseFence) !== 'ok') {
|
|
271
|
+
writeError(response, runtimeError('stale_lease', 'leaseFence is behind the accepted dispatcher'));
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
const { leaseFence: _leaseFence, ...requestBody } = body;
|
|
275
|
+
const parsed = operationAcceptRequestSchema.safeParse(requestBody);
|
|
276
|
+
if (!parsed.success) {
|
|
277
|
+
writeJson(response, 400, { error: 'invalid_operation', issues: parsed.error.issues });
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
if (parsed.data.kind.startsWith('session.') && !parsed.data.sessionId) {
|
|
281
|
+
writeJson(response, 400, { error: 'invalid_operation', reason: 'sessionId_required' });
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
if (state.config.workspaceMode === 'multi')
|
|
285
|
+
await workspaceRoot(state, parsed.data.workspaceId);
|
|
286
|
+
const payloadHash = hashOperationPayload(parsed.data.payload);
|
|
287
|
+
const scopeKey = operationScopeKey({
|
|
288
|
+
environment: state.config.environment,
|
|
289
|
+
userId: state.config.userId,
|
|
290
|
+
kind: parsed.data.kind,
|
|
291
|
+
clientRequestId: parsed.data.clientRequestId,
|
|
292
|
+
workspaceId: parsed.data.workspaceId,
|
|
293
|
+
sessionId: parsed.data.sessionId,
|
|
294
|
+
});
|
|
295
|
+
const accept = () => state.journal.exclusive(scopeKey, async () => {
|
|
296
|
+
// Recheck after a concurrent dispatcher may have advanced the fence while this request waited.
|
|
297
|
+
if (assertLeaseFence(incomingFence, state.acceptedLeaseFence) !== 'ok') {
|
|
298
|
+
writeError(response, runtimeError('stale_lease', 'leaseFence is behind the accepted dispatcher'));
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
const existing = state.journal.getOperation(scopeKey);
|
|
302
|
+
const decision = resolveIdempotentAccept(existing, payloadHash);
|
|
303
|
+
if (decision.action === 'conflict') {
|
|
304
|
+
writeError(response, runtimeError('idempotency_conflict', 'clientRequestId reused with a different payload', {
|
|
305
|
+
operationId: decision.operation.operationId,
|
|
306
|
+
}));
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
if (decision.action === 'reuse') {
|
|
310
|
+
if (decision.operation.status === 'uncertain') {
|
|
311
|
+
writeError(response, runtimeError('uncertain_operation', decision.operation.error ?? 'native_receipt_lost', {
|
|
312
|
+
operationId: decision.operation.operationId,
|
|
313
|
+
}));
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
writeJson(response, 200, decision.operation);
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
if (parsed.data.kind === 'session.create') {
|
|
320
|
+
const creation = state.journal.findSessionCreation(parsed.data.sessionId);
|
|
321
|
+
const session = state.journal.getSession(parsed.data.sessionId);
|
|
322
|
+
if (creation || session) {
|
|
323
|
+
writeJson(response, 409, { error: 'session_already_bound' });
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
else if (parsed.data.kind === 'terminal.create') {
|
|
328
|
+
const payload = asRecord(parsed.data.payload);
|
|
329
|
+
const terminalId = typeof payload.terminalId === 'string' ? payload.terminalId.trim() : '';
|
|
330
|
+
if (!terminalId) {
|
|
331
|
+
writeJson(response, 400, { error: 'invalid_operation', reason: 'terminalId_required' });
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
const creation = state.journal.findTerminalCreation(terminalId);
|
|
335
|
+
const terminal = state.journal.getTerminal(terminalId);
|
|
336
|
+
if (creation && operationScopeKey(creation) !== scopeKey) {
|
|
337
|
+
writeJson(response, 409, { error: 'terminal_already_bound' });
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
if (terminal && !creation) {
|
|
341
|
+
writeJson(response, 409, { error: 'terminal_already_bound' });
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
else if (parsed.data.kind.startsWith('session.') && state.config.workspaceMode === 'multi') {
|
|
346
|
+
const session = state.journal.getSession(parsed.data.sessionId);
|
|
347
|
+
if (!session || session.workspaceId !== parsed.data.workspaceId) {
|
|
348
|
+
writeJson(response, 409, { error: 'session_workspace_mismatch' });
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
const now = new Date().toISOString();
|
|
353
|
+
const executable = canExecuteKind(state, parsed.data.kind);
|
|
354
|
+
const payloadRecord = asRecord(parsed.data.payload);
|
|
355
|
+
const operation = {
|
|
356
|
+
operationId: randomUUID(),
|
|
357
|
+
environment: state.config.environment,
|
|
358
|
+
userId: state.config.userId,
|
|
359
|
+
clientRequestId: parsed.data.clientRequestId,
|
|
360
|
+
kind: parsed.data.kind,
|
|
361
|
+
status: executable ? 'queued' : 'failed',
|
|
362
|
+
payloadHash,
|
|
363
|
+
workspaceId: parsed.data.workspaceId,
|
|
364
|
+
sessionId: parsed.data.sessionId,
|
|
365
|
+
expectedRevision: parsed.data.expectedRevision,
|
|
366
|
+
generation: state.config.generation,
|
|
367
|
+
runtimeEpoch: state.runtimeEpoch,
|
|
368
|
+
leaseFence: incomingFence,
|
|
369
|
+
terminalId: parsed.data.kind === 'terminal.create' && typeof payloadRecord.terminalId === 'string'
|
|
370
|
+
? payloadRecord.terminalId.trim()
|
|
371
|
+
: undefined,
|
|
372
|
+
cwd: parsed.data.kind === 'terminal.create' && typeof payloadRecord.cwd === 'string'
|
|
373
|
+
? payloadRecord.cwd
|
|
374
|
+
: undefined,
|
|
375
|
+
error: executable ? undefined : 'capability_unavailable',
|
|
376
|
+
createdAt: now,
|
|
377
|
+
updatedAt: now,
|
|
378
|
+
};
|
|
379
|
+
if (!executable) {
|
|
380
|
+
const saved = await state.journal.putOperation(scopeKey, operation);
|
|
381
|
+
writeError(response, runtimeError('capability_unavailable', unavailableReason(state, parsed.data.kind), {
|
|
382
|
+
operationId: saved.operationId,
|
|
383
|
+
}));
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
state.acceptedLeaseFence = Math.max(state.acceptedLeaseFence, incomingFence);
|
|
387
|
+
await state.journal.setAcceptedLeaseFence(state.acceptedLeaseFence);
|
|
388
|
+
const queued = await state.journal.putOperation(scopeKey, operation);
|
|
389
|
+
const executed = await executeOperation(state, scopeKey, queued, parsed.data.payload);
|
|
390
|
+
if (executed.status === 'uncertain') {
|
|
391
|
+
writeError(response, runtimeError('uncertain_operation', executed.error ?? 'native_receipt_lost', {
|
|
392
|
+
operationId: executed.operationId,
|
|
393
|
+
}));
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
writeJson(response, executed.status === 'failed' ? 409 : 202, executed);
|
|
397
|
+
});
|
|
398
|
+
if (parsed.data.kind === 'session.create')
|
|
399
|
+
await state.journal.exclusive(`session:${parsed.data.sessionId}`, accept);
|
|
400
|
+
else if (parsed.data.kind === 'terminal.create') {
|
|
401
|
+
const terminalId = typeof asRecord(parsed.data.payload).terminalId === 'string'
|
|
402
|
+
? String(asRecord(parsed.data.payload).terminalId).trim()
|
|
403
|
+
: '';
|
|
404
|
+
if (terminalId)
|
|
405
|
+
await state.journal.exclusive(`terminal:${terminalId}`, accept);
|
|
406
|
+
else
|
|
407
|
+
await accept();
|
|
408
|
+
}
|
|
409
|
+
else
|
|
410
|
+
await accept();
|
|
411
|
+
}
|
|
412
|
+
async function writeGitRead(state, response, read) {
|
|
413
|
+
if (writeDestroyed(state, response))
|
|
414
|
+
return;
|
|
415
|
+
if (!state.gitAvailable) {
|
|
416
|
+
writeError(response, runtimeError('capability_unavailable', 'git_binary_missing'));
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
try {
|
|
420
|
+
writeJson(response, 200, await read());
|
|
421
|
+
}
|
|
422
|
+
catch (error) {
|
|
423
|
+
if (error instanceof GitWorkspaceError && error.code === 'invalid_workspace_path') {
|
|
424
|
+
writeJson(response, 400, { error: 'invalid_workspace_path' });
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
const reason = error instanceof GitWorkspaceError ? error.code : 'git_command_failed';
|
|
428
|
+
writeError(response, runtimeError('capability_unavailable', reason));
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
async function rebuildInstanceFiles(state, request, response) {
|
|
432
|
+
if (writeDestroyed(state, response))
|
|
433
|
+
return;
|
|
434
|
+
if (state.journal.fileCheckpointId() || state.journal.listSessions().length > 0) {
|
|
435
|
+
writeJson(response, 409, { status: 'failed', error: 'instance_still_alive', nativeContinuable: false });
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
const parsed = instanceRebuildRequestSchema.safeParse(await readJson(request));
|
|
439
|
+
if (!parsed.success) {
|
|
440
|
+
writeJson(response, 400, { status: 'failed', error: 'snapshot_mismatch', nativeContinuable: false });
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
try {
|
|
444
|
+
const root = await workspaceRoot(state, parsed.data.workspaceId);
|
|
445
|
+
const materialized = await materializeRemoteSnapshot(root, parsed.data.entries);
|
|
446
|
+
state.rebuildGate = {
|
|
447
|
+
directoryReady: materialized.directoryReady,
|
|
448
|
+
repositoryReady: materialized.repositoryReady,
|
|
449
|
+
environmentReady: materialized.environmentReady,
|
|
450
|
+
};
|
|
451
|
+
writeJson(response, 200, {
|
|
452
|
+
status: 'completed',
|
|
453
|
+
snapshotId: parsed.data.snapshotId,
|
|
454
|
+
nativeContinuable: false,
|
|
455
|
+
filesRestored: true,
|
|
456
|
+
skipped: materialized.skipped,
|
|
457
|
+
directoryReady: materialized.directoryReady,
|
|
458
|
+
repositoryReady: materialized.repositoryReady,
|
|
459
|
+
environmentReady: materialized.environmentReady,
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
catch (error) {
|
|
463
|
+
const reason = error instanceof Error && error.message === 'snapshot_mismatch' ? 'snapshot_mismatch' : 'snapshot_mismatch';
|
|
464
|
+
writeJson(response, 409, { status: 'failed', error: reason, nativeContinuable: false });
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
async function readJson(request) {
|
|
468
|
+
const chunks = [];
|
|
469
|
+
for await (const chunk of request) {
|
|
470
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
471
|
+
}
|
|
472
|
+
if (chunks.length === 0)
|
|
473
|
+
return {};
|
|
474
|
+
try {
|
|
475
|
+
return JSON.parse(Buffer.concat(chunks).toString('utf8'));
|
|
476
|
+
}
|
|
477
|
+
catch {
|
|
478
|
+
return null;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
function writeDestroyed(state, response) {
|
|
482
|
+
if (!state.journal.destroyed())
|
|
483
|
+
return false;
|
|
484
|
+
writeError(response, runtimeError('capability_unavailable', 'runtime_destroyed'));
|
|
485
|
+
return true;
|
|
486
|
+
}
|
|
487
|
+
async function handleTerminalIdentity(state, request, response, remainder) {
|
|
488
|
+
if (writeDestroyed(state, response))
|
|
489
|
+
return;
|
|
490
|
+
const [terminalId, action] = remainder.split('/');
|
|
491
|
+
if (!terminalId || action && action !== 'connect') {
|
|
492
|
+
writeJson(response, 404, { error: 'not_found' });
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
const terminal = state.journal.getTerminal(terminalId);
|
|
496
|
+
if (!terminal) {
|
|
497
|
+
writeJson(response, 404, { error: 'terminal_not_found' });
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
if (request.method === 'GET' && action === 'connect') {
|
|
501
|
+
if (terminal.status === 'destroyed') {
|
|
502
|
+
writeError(response, runtimeError('capability_unavailable', 'terminal_destroyed'));
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
writeError(response, runtimeError('capability_unavailable', 'pty_not_started'));
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
if (request.method === 'GET' && !action) {
|
|
509
|
+
writeJson(response, 200, terminal);
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
if (request.method === 'DELETE' && !action) {
|
|
513
|
+
const destroyed = terminal.status === 'destroyed'
|
|
514
|
+
? terminal
|
|
515
|
+
: await state.journal.putTerminal({
|
|
516
|
+
...terminal,
|
|
517
|
+
status: 'destroyed',
|
|
518
|
+
destroyedAt: new Date().toISOString(),
|
|
519
|
+
});
|
|
520
|
+
writeJson(response, 200, { terminalId: destroyed.terminalId, status: destroyed.status });
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
writeJson(response, 404, { error: 'not_found' });
|
|
524
|
+
}
|
|
525
|
+
function unavailableReason(state, kind) {
|
|
526
|
+
if (state.journal.destroyed())
|
|
527
|
+
return 'runtime_destroyed';
|
|
528
|
+
if (kind === 'preview.publish')
|
|
529
|
+
return 'capability_not_implemented';
|
|
530
|
+
return 'adapter_not_started';
|
|
531
|
+
}
|
|
532
|
+
function isUnimplementedWorkbenchPath(pathname) {
|
|
533
|
+
const parts = pathname.split('/').filter(Boolean);
|
|
534
|
+
return parts[0] === 'runtime' && (parts.includes('previews') || parts.includes('history'));
|
|
535
|
+
}
|
|
536
|
+
async function readObservedParameters(state, workspaceId) {
|
|
537
|
+
const root = await workspaceRoot(state, workspaceId);
|
|
538
|
+
const scope = revisionScope(state, workspaceId);
|
|
539
|
+
const first = await readWorkspaceParameters(root, state.journal.fileRevisions(scope));
|
|
540
|
+
for (const path of [first.schemaPath, first.dataPath]) {
|
|
541
|
+
if (path)
|
|
542
|
+
await withObservedFile(state, workspaceId, path, async () => undefined);
|
|
543
|
+
}
|
|
544
|
+
return readWorkspaceParameters(root, state.journal.fileRevisions(scope));
|
|
545
|
+
}
|
|
546
|
+
function writeError(response, error) {
|
|
547
|
+
writeJson(response, HTTP_STATUS_BY_ERROR[error.code], error);
|
|
548
|
+
}
|
|
549
|
+
function writeJson(response, status, body) {
|
|
550
|
+
const payload = JSON.stringify(body);
|
|
551
|
+
response.writeHead(status, {
|
|
552
|
+
'content-type': 'application/json; charset=utf-8',
|
|
553
|
+
'content-length': Buffer.byteLength(payload),
|
|
554
|
+
});
|
|
555
|
+
response.end(payload);
|
|
556
|
+
}
|
|
557
|
+
function asRecord(value) {
|
|
558
|
+
return value && typeof value === 'object' && !Array.isArray(value)
|
|
559
|
+
? value
|
|
560
|
+
: {};
|
|
561
|
+
}
|