@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,59 @@
|
|
|
1
|
+
import { nativeEvent } from './native-adapter.js';
|
|
2
|
+
/** In-process Codex stand-in. Not the App Server; used to prove the operation/event loop. */
|
|
3
|
+
export function createLoopbackNativeAdapter() {
|
|
4
|
+
const threads = new Map();
|
|
5
|
+
const approvals = new Set();
|
|
6
|
+
return {
|
|
7
|
+
name: 'loopback',
|
|
8
|
+
ready: () => true,
|
|
9
|
+
async startThread(input) {
|
|
10
|
+
const threadId = `thread_${input.sessionId}`;
|
|
11
|
+
threads.set(threadId, input.sessionId);
|
|
12
|
+
return { threadId };
|
|
13
|
+
},
|
|
14
|
+
async startTurn(input) {
|
|
15
|
+
if (threads.get(input.threadId) !== input.sessionId) {
|
|
16
|
+
return {
|
|
17
|
+
outcome: 'failed',
|
|
18
|
+
events: [nativeEvent('turn.failed', { reason: 'unknown_thread' })],
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
if (input.simulateLostReceipt) {
|
|
22
|
+
return { outcome: 'uncertain', events: [] };
|
|
23
|
+
}
|
|
24
|
+
const turnId = `turn_${input.clientRequestId}`;
|
|
25
|
+
const events = [
|
|
26
|
+
nativeEvent('item/agentMessage/delta', { text: `loopback:${input.text}` }),
|
|
27
|
+
nativeEvent('turn.completed', { turnId, threadId: input.threadId }),
|
|
28
|
+
];
|
|
29
|
+
if (input.text.includes('[approval]')) {
|
|
30
|
+
const approvalId = `approval_${input.clientRequestId}`;
|
|
31
|
+
approvals.add(JSON.stringify([input.sessionId, approvalId]));
|
|
32
|
+
events.splice(1, 0, nativeEvent('item/tool/requestUserInput', { approvalId }));
|
|
33
|
+
return { outcome: 'completed', turnId, approvalId, events };
|
|
34
|
+
}
|
|
35
|
+
return { outcome: 'completed', turnId, events };
|
|
36
|
+
},
|
|
37
|
+
async interrupt(input) {
|
|
38
|
+
if (threads.get(input.threadId) !== input.sessionId) {
|
|
39
|
+
return { outcome: 'uncertain', events: [] };
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
outcome: 'interrupted',
|
|
43
|
+
events: [nativeEvent('turn.interrupted', { threadId: input.threadId })],
|
|
44
|
+
};
|
|
45
|
+
},
|
|
46
|
+
async respondApproval(input) {
|
|
47
|
+
if (!approvals.has(JSON.stringify([input.sessionId, input.approvalId]))) {
|
|
48
|
+
return { outcome: 'failed', events: [nativeEvent('approval.unknown', { approvalId: input.approvalId })] };
|
|
49
|
+
}
|
|
50
|
+
approvals.delete(JSON.stringify([input.sessionId, input.approvalId]));
|
|
51
|
+
const result = {
|
|
52
|
+
outcome: 'completed',
|
|
53
|
+
approvalId: input.approvalId,
|
|
54
|
+
events: [nativeEvent('approval.resolved', { approvalId: input.approvalId, accepted: input.accepted })],
|
|
55
|
+
};
|
|
56
|
+
return result;
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { runWorkspaceRuntimeServe } from '../commands/workspace-runtime.js';
|
|
2
|
+
const flag = process.argv.indexOf('--config');
|
|
3
|
+
const configPath = flag >= 0 ? process.argv[flag + 1] : undefined;
|
|
4
|
+
if (!configPath) {
|
|
5
|
+
console.error('workspace-runtime: missing --config');
|
|
6
|
+
process.exitCode = 2;
|
|
7
|
+
}
|
|
8
|
+
else {
|
|
9
|
+
await runWorkspaceRuntimeServe(configPath);
|
|
10
|
+
}
|
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { workspaceRegistrationSchema, checkpointRecordSchema, operationRecordSchema, pendingSourceEventSchema, sourceEventKey, sourceEventReceiptSchema, terminalRecordSchema, } from '@playcraft/common/cloud-workspace/runtime';
|
|
4
|
+
export class RuntimeJournal {
|
|
5
|
+
filePath;
|
|
6
|
+
writes = Promise.resolve();
|
|
7
|
+
locks = new Map();
|
|
8
|
+
async exclusive(key, action) {
|
|
9
|
+
const previous = this.locks.get(key) ?? Promise.resolve();
|
|
10
|
+
let release;
|
|
11
|
+
const current = new Promise((resolve) => {
|
|
12
|
+
release = resolve;
|
|
13
|
+
});
|
|
14
|
+
this.locks.set(key, current);
|
|
15
|
+
await previous;
|
|
16
|
+
try {
|
|
17
|
+
return await action();
|
|
18
|
+
}
|
|
19
|
+
finally {
|
|
20
|
+
release();
|
|
21
|
+
if (this.locks.get(key) === current)
|
|
22
|
+
this.locks.delete(key);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
snapshot = {
|
|
26
|
+
workspaces: {},
|
|
27
|
+
workspaceFileRevisions: {},
|
|
28
|
+
operations: {},
|
|
29
|
+
receipts: {},
|
|
30
|
+
events: {},
|
|
31
|
+
sessions: {},
|
|
32
|
+
terminals: {},
|
|
33
|
+
checkpoints: {},
|
|
34
|
+
fileRevisions: {},
|
|
35
|
+
approvals: {},
|
|
36
|
+
managedWrites: {},
|
|
37
|
+
executionGroup: { stopConfirmed: false, replaced: false },
|
|
38
|
+
lastSourceSequence: 0,
|
|
39
|
+
acceptedLeaseFence: 0,
|
|
40
|
+
destroyed: false,
|
|
41
|
+
};
|
|
42
|
+
constructor(stateDir) {
|
|
43
|
+
this.filePath = join(stateDir, 'journal.json');
|
|
44
|
+
}
|
|
45
|
+
async load() {
|
|
46
|
+
try {
|
|
47
|
+
const parsed = JSON.parse(await readFile(this.filePath, 'utf8'));
|
|
48
|
+
this.snapshot = {
|
|
49
|
+
workspaces: Object.fromEntries(Object.entries(parsed.workspaces ?? {}).map(([key, value]) => [key, workspaceRegistrationSchema.parse(value)])),
|
|
50
|
+
workspaceFileRevisions: parsed.workspaceFileRevisions ?? {},
|
|
51
|
+
operations: Object.fromEntries(Object.entries(parsed.operations ?? {}).map(([key, value]) => [
|
|
52
|
+
key,
|
|
53
|
+
operationRecordSchema.parse(value),
|
|
54
|
+
])),
|
|
55
|
+
receipts: Object.fromEntries(Object.entries(parsed.receipts ?? {}).map(([key, value]) => [
|
|
56
|
+
key,
|
|
57
|
+
sourceEventReceiptSchema.parse(value),
|
|
58
|
+
])),
|
|
59
|
+
events: Object.fromEntries(Object.entries(parsed.events ?? {}).map(([key, value]) => [
|
|
60
|
+
key,
|
|
61
|
+
pendingSourceEventSchema.parse(value),
|
|
62
|
+
])),
|
|
63
|
+
sessions: Object.fromEntries(Object.entries(parsed.sessions ?? {}).map(([key, value]) => [
|
|
64
|
+
key,
|
|
65
|
+
{
|
|
66
|
+
...value,
|
|
67
|
+
recoverability: value.recoverability === 'history_only' ? 'history_only' : 'native',
|
|
68
|
+
},
|
|
69
|
+
])),
|
|
70
|
+
terminals: Object.fromEntries(Object.entries(parsed.terminals ?? {}).map(([key, value]) => [
|
|
71
|
+
key,
|
|
72
|
+
terminalRecordSchema.parse(value),
|
|
73
|
+
])),
|
|
74
|
+
checkpoints: Object.fromEntries(Object.entries(parsed.checkpoints ?? {}).map(([key, value]) => [
|
|
75
|
+
key,
|
|
76
|
+
checkpointRecordSchema.parse(value),
|
|
77
|
+
])),
|
|
78
|
+
fileRevisions: parsed.fileRevisions ?? {},
|
|
79
|
+
managedWrites: sanitizeManagedWrites(parsed.managedWrites),
|
|
80
|
+
fileCheckpointId: typeof parsed.fileCheckpointId === 'string' ? parsed.fileCheckpointId : undefined,
|
|
81
|
+
consistentCheckpointId: typeof parsed.consistentCheckpointId === 'string' ? parsed.consistentCheckpointId : undefined,
|
|
82
|
+
approvals: Object.fromEntries(Object.entries(parsed.approvals ?? {})
|
|
83
|
+
.filter(([, value]) => isStoredApproval(value))
|
|
84
|
+
.map(([key, value]) => [key, value])),
|
|
85
|
+
executionGroup: {
|
|
86
|
+
pid: integerOrUndefined(parsed.executionGroup?.pid),
|
|
87
|
+
startedAt: typeof parsed.executionGroup?.startedAt === 'string'
|
|
88
|
+
? parsed.executionGroup.startedAt
|
|
89
|
+
: undefined,
|
|
90
|
+
previousPid: integerOrUndefined(parsed.executionGroup?.previousPid),
|
|
91
|
+
stopConfirmed: parsed.executionGroup?.stopConfirmed === true,
|
|
92
|
+
replaced: parsed.executionGroup?.replaced === true,
|
|
93
|
+
},
|
|
94
|
+
runtimeEpoch: integerOrUndefined(parsed.runtimeEpoch),
|
|
95
|
+
lastSourceSequence: parsed.lastSourceSequence ?? 0,
|
|
96
|
+
acceptedLeaseFence: parsed.acceptedLeaseFence ?? highestFence(parsed.operations ?? {}),
|
|
97
|
+
destroyed: parsed.destroyed === true,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
if (error.code === 'ENOENT')
|
|
102
|
+
return;
|
|
103
|
+
throw error;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
getWorkspace(workspaceId) {
|
|
107
|
+
return this.snapshot.workspaces[workspaceId];
|
|
108
|
+
}
|
|
109
|
+
async putWorkspace(binding) {
|
|
110
|
+
this.snapshot.workspaces[binding.workspaceId] = workspaceRegistrationSchema.parse(binding);
|
|
111
|
+
await this.flush();
|
|
112
|
+
}
|
|
113
|
+
findSessionCreation(sessionId) {
|
|
114
|
+
return Object.values(this.snapshot.operations).find((item) => item.kind === 'session.create' && item.sessionId === sessionId);
|
|
115
|
+
}
|
|
116
|
+
findTerminalCreation(terminalId) {
|
|
117
|
+
return Object.values(this.snapshot.operations).find((item) => item.kind === 'terminal.create' && item.terminalId === terminalId);
|
|
118
|
+
}
|
|
119
|
+
getOperation(scopeKey) {
|
|
120
|
+
return this.snapshot.operations[scopeKey];
|
|
121
|
+
}
|
|
122
|
+
getOperationById(operationId) {
|
|
123
|
+
return Object.values(this.snapshot.operations).find((operation) => operation.operationId === operationId);
|
|
124
|
+
}
|
|
125
|
+
getReceipt(key) {
|
|
126
|
+
return this.snapshot.receipts[key];
|
|
127
|
+
}
|
|
128
|
+
getSession(sessionId) {
|
|
129
|
+
return this.snapshot.sessions[sessionId];
|
|
130
|
+
}
|
|
131
|
+
listSessions() {
|
|
132
|
+
return Object.values(this.snapshot.sessions);
|
|
133
|
+
}
|
|
134
|
+
getTerminal(terminalId) {
|
|
135
|
+
return this.snapshot.terminals[terminalId];
|
|
136
|
+
}
|
|
137
|
+
listTerminals(workspaceId) {
|
|
138
|
+
return Object.values(this.snapshot.terminals).filter((item) => !workspaceId || item.workspaceId === workspaceId);
|
|
139
|
+
}
|
|
140
|
+
getCheckpoint(checkpointId) {
|
|
141
|
+
return this.snapshot.checkpoints[checkpointId];
|
|
142
|
+
}
|
|
143
|
+
fileCheckpointId() {
|
|
144
|
+
return this.snapshot.fileCheckpointId;
|
|
145
|
+
}
|
|
146
|
+
consistentCheckpointId() {
|
|
147
|
+
return this.snapshot.consistentCheckpointId;
|
|
148
|
+
}
|
|
149
|
+
async setFileCheckpointId(checkpointId) {
|
|
150
|
+
this.snapshot.fileCheckpointId = checkpointId;
|
|
151
|
+
await this.flush();
|
|
152
|
+
}
|
|
153
|
+
async setConsistentCheckpointId(checkpointId) {
|
|
154
|
+
this.snapshot.consistentCheckpointId = checkpointId;
|
|
155
|
+
await this.flush();
|
|
156
|
+
}
|
|
157
|
+
listManagedWrites(workspaceId) {
|
|
158
|
+
return Object.values(this.snapshot.managedWrites).filter((item) => !workspaceId || item.workspaceId === workspaceId);
|
|
159
|
+
}
|
|
160
|
+
getManagedWrite(writeId) {
|
|
161
|
+
return this.snapshot.managedWrites[writeId];
|
|
162
|
+
}
|
|
163
|
+
async putManagedWrite(record) {
|
|
164
|
+
this.snapshot.managedWrites[record.writeId] = record;
|
|
165
|
+
await this.flush();
|
|
166
|
+
}
|
|
167
|
+
fileRevisions(workspaceId) {
|
|
168
|
+
return { ...(workspaceId ? this.snapshot.workspaceFileRevisions[workspaceId] ?? {} : this.snapshot.fileRevisions) };
|
|
169
|
+
}
|
|
170
|
+
fileRevision(path, workspaceId) {
|
|
171
|
+
return this.fileRevisions(workspaceId)[path]?.revision ?? 0;
|
|
172
|
+
}
|
|
173
|
+
destroyed() {
|
|
174
|
+
return this.snapshot.destroyed;
|
|
175
|
+
}
|
|
176
|
+
lastSourceSequence() {
|
|
177
|
+
return this.snapshot.lastSourceSequence;
|
|
178
|
+
}
|
|
179
|
+
acceptedLeaseFence() {
|
|
180
|
+
return this.snapshot.acceptedLeaseFence;
|
|
181
|
+
}
|
|
182
|
+
storedRuntimeEpoch() {
|
|
183
|
+
return this.snapshot.runtimeEpoch;
|
|
184
|
+
}
|
|
185
|
+
executionGroup() {
|
|
186
|
+
return { ...this.snapshot.executionGroup };
|
|
187
|
+
}
|
|
188
|
+
getApproval(sessionId, approvalId) {
|
|
189
|
+
return this.snapshot.approvals[approvalKey(sessionId, approvalId)];
|
|
190
|
+
}
|
|
191
|
+
listApprovals() {
|
|
192
|
+
return Object.values(this.snapshot.approvals);
|
|
193
|
+
}
|
|
194
|
+
async putApproval(approval) {
|
|
195
|
+
this.snapshot.approvals[approvalKey(approval.sessionId, approval.approvalId)] = approval;
|
|
196
|
+
await this.flush();
|
|
197
|
+
}
|
|
198
|
+
async noteExecutionGroup(group) {
|
|
199
|
+
this.snapshot.executionGroup = {
|
|
200
|
+
...this.snapshot.executionGroup,
|
|
201
|
+
pid: group.pid,
|
|
202
|
+
startedAt: group.startedAt,
|
|
203
|
+
};
|
|
204
|
+
await this.flush();
|
|
205
|
+
}
|
|
206
|
+
async writeExecutionEpoch(input) {
|
|
207
|
+
this.snapshot.runtimeEpoch = input.runtimeEpoch;
|
|
208
|
+
this.snapshot.executionGroup = {
|
|
209
|
+
pid: input.pid,
|
|
210
|
+
startedAt: input.startedAt,
|
|
211
|
+
previousPid: input.previousPid,
|
|
212
|
+
stopConfirmed: input.stopConfirmed,
|
|
213
|
+
replaced: true,
|
|
214
|
+
};
|
|
215
|
+
for (const [key, approval] of Object.entries(this.snapshot.approvals)) {
|
|
216
|
+
if (approval.status === 'pending') {
|
|
217
|
+
this.snapshot.approvals[key] = { ...approval, status: 'expired' };
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
await this.flush();
|
|
221
|
+
}
|
|
222
|
+
async setExecutionStopConfirmed(stopConfirmed) {
|
|
223
|
+
this.snapshot.executionGroup = { ...this.snapshot.executionGroup, stopConfirmed };
|
|
224
|
+
await this.flush();
|
|
225
|
+
}
|
|
226
|
+
listEvents(afterSequence = 0) {
|
|
227
|
+
return Object.values(this.snapshot.events)
|
|
228
|
+
.filter((event) => event.source.sourceSequence > afterSequence)
|
|
229
|
+
.sort((left, right) => left.source.sourceSequence - right.source.sourceSequence);
|
|
230
|
+
}
|
|
231
|
+
async putOperation(scopeKey, operation) {
|
|
232
|
+
this.snapshot.operations[scopeKey] = operationRecordSchema.parse(operation);
|
|
233
|
+
await this.flush();
|
|
234
|
+
return this.snapshot.operations[scopeKey];
|
|
235
|
+
}
|
|
236
|
+
async putSession(binding) {
|
|
237
|
+
this.snapshot.sessions[binding.sessionId] = binding;
|
|
238
|
+
await this.flush();
|
|
239
|
+
}
|
|
240
|
+
async putTerminal(record) {
|
|
241
|
+
const parsed = terminalRecordSchema.parse(record);
|
|
242
|
+
this.snapshot.terminals[parsed.terminalId] = parsed;
|
|
243
|
+
await this.flush();
|
|
244
|
+
return parsed;
|
|
245
|
+
}
|
|
246
|
+
async putCheckpoint(checkpoint) {
|
|
247
|
+
const parsed = checkpointRecordSchema.parse(checkpoint);
|
|
248
|
+
this.snapshot.checkpoints[parsed.checkpointId] = parsed;
|
|
249
|
+
await this.flush();
|
|
250
|
+
return parsed;
|
|
251
|
+
}
|
|
252
|
+
async putFileRevision(path, record, workspaceId) {
|
|
253
|
+
if (workspaceId) {
|
|
254
|
+
(this.snapshot.workspaceFileRevisions[workspaceId] ??= {})[path] = record;
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
this.snapshot.fileRevisions[path] = record;
|
|
258
|
+
}
|
|
259
|
+
await this.flush();
|
|
260
|
+
}
|
|
261
|
+
async replaceFileRevisions(records, workspaceId) {
|
|
262
|
+
if (workspaceId)
|
|
263
|
+
this.snapshot.workspaceFileRevisions[workspaceId] = { ...records };
|
|
264
|
+
else
|
|
265
|
+
this.snapshot.fileRevisions = { ...records };
|
|
266
|
+
await this.flush();
|
|
267
|
+
}
|
|
268
|
+
async markDestroyed() {
|
|
269
|
+
this.snapshot.destroyed = true;
|
|
270
|
+
await this.flush();
|
|
271
|
+
}
|
|
272
|
+
async replaceSessions(sessions) {
|
|
273
|
+
this.snapshot.sessions = Object.fromEntries(sessions.map((item) => [item.sessionId, item]));
|
|
274
|
+
await this.flush();
|
|
275
|
+
}
|
|
276
|
+
async setAcceptedLeaseFence(value) {
|
|
277
|
+
this.snapshot.acceptedLeaseFence = Math.max(this.snapshot.acceptedLeaseFence, value);
|
|
278
|
+
await this.flush();
|
|
279
|
+
}
|
|
280
|
+
async putReceipt(receipt, sourceSequence) {
|
|
281
|
+
this.snapshot.receipts[sourceEventKey(receipt.source)] =
|
|
282
|
+
sourceEventReceiptSchema.parse(receipt);
|
|
283
|
+
this.snapshot.lastSourceSequence = Math.max(this.snapshot.lastSourceSequence, sourceSequence);
|
|
284
|
+
await this.flush();
|
|
285
|
+
}
|
|
286
|
+
async appendEvent(event) {
|
|
287
|
+
const parsed = pendingSourceEventSchema.parse(event);
|
|
288
|
+
this.snapshot.events[sourceEventKey(parsed.source)] = parsed;
|
|
289
|
+
this.snapshot.lastSourceSequence = Math.max(this.snapshot.lastSourceSequence, parsed.source.sourceSequence);
|
|
290
|
+
await this.flush();
|
|
291
|
+
return parsed;
|
|
292
|
+
}
|
|
293
|
+
async flush() {
|
|
294
|
+
const snapshot = `${JSON.stringify(this.snapshot)}\n`;
|
|
295
|
+
const write = this.writes.then(async () => {
|
|
296
|
+
await mkdir(dirname(this.filePath), { recursive: true, mode: 0o700 });
|
|
297
|
+
const tempPath = `${this.filePath}.${process.pid}.tmp`;
|
|
298
|
+
await writeFile(tempPath, snapshot, { mode: 0o600 });
|
|
299
|
+
await rename(tempPath, this.filePath);
|
|
300
|
+
});
|
|
301
|
+
this.writes = write.catch(() => undefined);
|
|
302
|
+
await write;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
function sanitizeManagedWrites(value) {
|
|
306
|
+
if (!value || typeof value !== 'object')
|
|
307
|
+
return {};
|
|
308
|
+
const records = {};
|
|
309
|
+
for (const [key, item] of Object.entries(value)) {
|
|
310
|
+
if (!item || typeof item !== 'object')
|
|
311
|
+
continue;
|
|
312
|
+
const record = item;
|
|
313
|
+
if (record.writeId !== key || typeof record.workspaceId !== 'string' || typeof record.startedAt !== 'string') {
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
if (record.source !== 'manual' && record.source !== 'agent' && record.source !== 'background')
|
|
317
|
+
continue;
|
|
318
|
+
records[key] = {
|
|
319
|
+
writeId: key,
|
|
320
|
+
workspaceId: record.workspaceId,
|
|
321
|
+
source: record.source,
|
|
322
|
+
startedAt: record.startedAt,
|
|
323
|
+
pid: integerOrUndefined(record.pid),
|
|
324
|
+
stoppedAt: typeof record.stoppedAt === 'string' ? record.stoppedAt : undefined,
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
return records;
|
|
328
|
+
}
|
|
329
|
+
function approvalKey(sessionId, approvalId) {
|
|
330
|
+
return `${sessionId}:${approvalId}`;
|
|
331
|
+
}
|
|
332
|
+
function integerOrUndefined(value) {
|
|
333
|
+
return typeof value === 'number' && Number.isInteger(value) && value >= 0 ? value : undefined;
|
|
334
|
+
}
|
|
335
|
+
function isStoredApproval(value) {
|
|
336
|
+
if (!value || typeof value !== 'object')
|
|
337
|
+
return false;
|
|
338
|
+
const record = value;
|
|
339
|
+
return (typeof record.approvalId === 'string' &&
|
|
340
|
+
typeof record.sessionId === 'string' &&
|
|
341
|
+
typeof record.runtimeEpoch === 'number' &&
|
|
342
|
+
typeof record.nativeRequestId === 'string' &&
|
|
343
|
+
(record.status === 'pending' ||
|
|
344
|
+
record.status === 'approved' ||
|
|
345
|
+
record.status === 'rejected' ||
|
|
346
|
+
record.status === 'expired'));
|
|
347
|
+
}
|
|
348
|
+
function highestFence(operations) {
|
|
349
|
+
return Object.values(operations).reduce((highest, operation) => Math.max(highest, operation.leaseFence ?? 0), 0);
|
|
350
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
3
|
+
export const MANAGED_WRITE_SOURCES = ['manual', 'agent', 'background'];
|
|
4
|
+
const BACKGROUND_WRITER = `
|
|
5
|
+
const fs = require('node:fs');
|
|
6
|
+
const file = process.argv[1];
|
|
7
|
+
if (!file) process.exit(1);
|
|
8
|
+
fs.appendFileSync(file, 'background\\n');
|
|
9
|
+
const timer = setInterval(() => {
|
|
10
|
+
try { fs.appendFileSync(file, 'x'); }
|
|
11
|
+
catch { clearInterval(timer); process.exit(0); }
|
|
12
|
+
}, 200);
|
|
13
|
+
`;
|
|
14
|
+
/** `true` alive, `false` confirmed exited, `undefined` when the probe cannot tell. */
|
|
15
|
+
export function probeProcess(pid) {
|
|
16
|
+
try {
|
|
17
|
+
process.kill(pid, 0);
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
if (error.code === 'ESRCH')
|
|
22
|
+
return false;
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export function spawnBackgroundWriter(absolutePath) {
|
|
27
|
+
const child = spawn(process.execPath, ['-e', BACKGROUND_WRITER, absolutePath], {
|
|
28
|
+
detached: true,
|
|
29
|
+
stdio: 'ignore',
|
|
30
|
+
});
|
|
31
|
+
child.unref();
|
|
32
|
+
if (!child.pid)
|
|
33
|
+
throw new Error('background_write_failed');
|
|
34
|
+
return child.pid;
|
|
35
|
+
}
|
|
36
|
+
export async function stopBackgroundWriter(pid) {
|
|
37
|
+
signal(pid, 'SIGTERM');
|
|
38
|
+
if (await waitUntilStopped(pid, 2_000))
|
|
39
|
+
return true;
|
|
40
|
+
signal(pid, 'SIGKILL');
|
|
41
|
+
return waitUntilStopped(pid, 2_000);
|
|
42
|
+
}
|
|
43
|
+
function signal(pid, name) {
|
|
44
|
+
try {
|
|
45
|
+
process.kill(-pid, name);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
if (error.code !== 'ESRCH')
|
|
49
|
+
return;
|
|
50
|
+
try {
|
|
51
|
+
process.kill(pid, name);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
/* already gone */
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
async function waitUntilStopped(pid, timeoutMs) {
|
|
59
|
+
const deadline = Date.now() + timeoutMs;
|
|
60
|
+
while (Date.now() < deadline) {
|
|
61
|
+
const alive = probeProcess(pid);
|
|
62
|
+
if (alive === false)
|
|
63
|
+
return true;
|
|
64
|
+
if (alive === undefined)
|
|
65
|
+
return false;
|
|
66
|
+
await delay(50);
|
|
67
|
+
}
|
|
68
|
+
return probeProcess(pid) === false;
|
|
69
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { mkdir } from 'node:fs/promises';
|
|
3
|
+
import { createServer } from 'node:http';
|
|
4
|
+
import { INCOMPATIBLE_EXIT_CODE, negotiateProtocol, } from '@playcraft/common/cloud-workspace/runtime';
|
|
5
|
+
import { RuntimeJournal } from './persistence/journal.js';
|
|
6
|
+
import { createCodexAppServerAdapter } from './codex/app-server.js';
|
|
7
|
+
import { createLoopbackNativeAdapter } from './codex/loopback.js';
|
|
8
|
+
import { handleRuntimeRequest } from './server/http.js';
|
|
9
|
+
import { handleExecutionGroupCrash } from './server/execution-group.js';
|
|
10
|
+
import { RuntimeProcessLock } from './server/lock.js';
|
|
11
|
+
import { probeGitBinary } from './workspaces/git.js';
|
|
12
|
+
export async function startWorkspaceRuntime(config, options = {}) {
|
|
13
|
+
process.env.PLAYCRAFT_SKIP_UPDATE_CHECK = '1';
|
|
14
|
+
const negotiated = negotiateProtocol(config);
|
|
15
|
+
if (!negotiated.ok) {
|
|
16
|
+
const error = Object.assign(new Error('protocol_incompatible'), {
|
|
17
|
+
code: 'protocol_incompatible',
|
|
18
|
+
exitCode: INCOMPATIBLE_EXIT_CODE,
|
|
19
|
+
});
|
|
20
|
+
throw error;
|
|
21
|
+
}
|
|
22
|
+
const lock = new RuntimeProcessLock(config.stateDir);
|
|
23
|
+
lock.acquire();
|
|
24
|
+
try {
|
|
25
|
+
const journal = new RuntimeJournal(config.stateDir);
|
|
26
|
+
await journal.load();
|
|
27
|
+
await mkdir(config.workspaceRoot, { recursive: true, mode: 0o700 });
|
|
28
|
+
const native = options.nativeAdapter ?? defaultNativeAdapter(config);
|
|
29
|
+
const state = {
|
|
30
|
+
config: { ...config, protocolMinor: negotiated.protocolMinor },
|
|
31
|
+
journal,
|
|
32
|
+
acceptedLeaseFence: journal.acceptedLeaseFence(),
|
|
33
|
+
gitAvailable: await probeGitBinary(),
|
|
34
|
+
native,
|
|
35
|
+
runtimeEpoch: Math.max(config.runtimeEpoch, journal.storedRuntimeEpoch() ?? 0),
|
|
36
|
+
executionQueue: Promise.resolve(),
|
|
37
|
+
instanceToken: randomUUID(),
|
|
38
|
+
};
|
|
39
|
+
native?.onUnexpectedExit?.((event) => {
|
|
40
|
+
void handleExecutionGroupCrash(state, event.pid);
|
|
41
|
+
});
|
|
42
|
+
try {
|
|
43
|
+
await native?.start?.();
|
|
44
|
+
const group = native?.executionProcess?.();
|
|
45
|
+
if (group)
|
|
46
|
+
await journal.noteExecutionGroup(group);
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
/* conversation stays adapter_not_started until Codex handshake succeeds */
|
|
50
|
+
}
|
|
51
|
+
const server = createServer((request, response) => {
|
|
52
|
+
void handleRuntimeRequest(state, request, response);
|
|
53
|
+
});
|
|
54
|
+
const port = await listenOn(server, config.listen.host, config.listen.port);
|
|
55
|
+
return {
|
|
56
|
+
config: state.config,
|
|
57
|
+
state,
|
|
58
|
+
url: `http://${formatHost(config.listen.host)}:${port}`,
|
|
59
|
+
async close() {
|
|
60
|
+
await closeServer(server);
|
|
61
|
+
try {
|
|
62
|
+
await native?.close?.();
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
/* adapter shutdown is best-effort */
|
|
66
|
+
}
|
|
67
|
+
lock.release();
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
lock.release();
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function listenOn(server, host, port) {
|
|
77
|
+
return new Promise((resolve, reject) => {
|
|
78
|
+
const onError = (error) => reject(error);
|
|
79
|
+
server.once('error', onError);
|
|
80
|
+
server.listen(port, host, () => {
|
|
81
|
+
server.removeListener('error', onError);
|
|
82
|
+
const address = server.address();
|
|
83
|
+
if (!address || typeof address === 'string') {
|
|
84
|
+
reject(new Error('listen_failed'));
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
resolve(address.port);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
function closeServer(server) {
|
|
92
|
+
return new Promise((resolve, reject) => {
|
|
93
|
+
server.close((error) => {
|
|
94
|
+
if (error)
|
|
95
|
+
reject(error);
|
|
96
|
+
else
|
|
97
|
+
resolve();
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
function formatHost(host) {
|
|
102
|
+
return host.includes(':') ? `[${host}]` : host;
|
|
103
|
+
}
|
|
104
|
+
function defaultNativeAdapter(config) {
|
|
105
|
+
if (process.env.PLAYCRAFT_NATIVE_ADAPTER === 'loopback')
|
|
106
|
+
return createLoopbackNativeAdapter();
|
|
107
|
+
if (process.env.PLAYCRAFT_NATIVE_ADAPTER === 'codex') {
|
|
108
|
+
return createCodexAppServerAdapter({ config });
|
|
109
|
+
}
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { timingSafeEqual } from 'node:crypto';
|
|
2
|
+
export function extractControlToken(headers) {
|
|
3
|
+
const authorization = headerValue(headers.authorization);
|
|
4
|
+
if (authorization?.startsWith('Bearer ')) {
|
|
5
|
+
return authorization.slice('Bearer '.length).trim() || null;
|
|
6
|
+
}
|
|
7
|
+
const dedicated = headerValue(headers['x-playcraft-control-token']);
|
|
8
|
+
return dedicated?.trim() || null;
|
|
9
|
+
}
|
|
10
|
+
export function controlTokenMatches(expected, actual) {
|
|
11
|
+
if (!actual)
|
|
12
|
+
return false;
|
|
13
|
+
const left = Buffer.from(expected);
|
|
14
|
+
const right = Buffer.from(actual);
|
|
15
|
+
if (left.length !== right.length)
|
|
16
|
+
return false;
|
|
17
|
+
return timingSafeEqual(left, right);
|
|
18
|
+
}
|
|
19
|
+
function headerValue(value) {
|
|
20
|
+
return Array.isArray(value) ? value[0] : value;
|
|
21
|
+
}
|