@playcraft/build 0.2.17 → 0.2.19-test-a
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/dist/audio-source-files.d.ts +5 -0
- package/dist/audio-source-files.js +135 -0
- package/dist/base-builder.js +42 -2
- package/dist/delivery/agent-build-runner.d.ts +44 -0
- package/dist/delivery/agent-build-runner.js +365 -0
- package/dist/delivery/agent-build-source.d.ts +8 -0
- package/dist/delivery/agent-build-source.js +25 -0
- package/dist/delivery/artifact-descriptor.d.ts +31 -0
- package/dist/delivery/artifact-descriptor.js +237 -0
- package/dist/delivery/artifact-package.d.ts +43 -0
- package/dist/delivery/artifact-package.js +283 -0
- package/dist/delivery/build-plan.d.ts +4 -0
- package/dist/delivery/build-plan.js +189 -0
- package/dist/delivery/build-plan.types.d.ts +51 -0
- package/dist/delivery/build-plan.types.js +1 -0
- package/dist/delivery/command-isolation.d.ts +21 -0
- package/dist/delivery/command-isolation.js +327 -0
- package/dist/delivery/commands.d.ts +5 -0
- package/dist/delivery/commands.js +146 -0
- package/dist/delivery/files.d.ts +9 -0
- package/dist/delivery/files.js +144 -0
- package/dist/delivery/git-source-fingerprint.d.ts +2 -0
- package/dist/delivery/git-source-fingerprint.js +58 -0
- package/dist/delivery/isolated-source.d.ts +5 -0
- package/dist/delivery/isolated-source.js +148 -0
- package/dist/delivery/output-publication.d.ts +21 -0
- package/dist/delivery/output-publication.js +444 -0
- package/dist/delivery/project-delivery.d.ts +6 -0
- package/dist/delivery/project-delivery.js +287 -0
- package/dist/delivery/recipe-build-plan.d.ts +4 -0
- package/dist/delivery/recipe-build-plan.js +53 -0
- package/dist/delivery/recipe.d.ts +20 -0
- package/dist/delivery/recipe.js +211 -0
- package/dist/delivery/safe-path.d.ts +2 -0
- package/dist/delivery/safe-path.js +18 -0
- package/dist/delivery/types.d.ts +81 -0
- package/dist/delivery/types.js +1 -0
- package/dist/engines/engine-detector.js +4 -0
- package/dist/engines/generic-adapter.js +14 -19
- package/dist/engines/playable-scripts-adapter.js +2 -5
- package/dist/index.d.ts +21 -4
- package/dist/index.js +12 -2
- package/dist/platforms/adikteev.js +1 -0
- package/dist/platforms/applovin.js +1 -0
- package/dist/platforms/base.d.ts +4 -0
- package/dist/platforms/base.js +25 -6
- package/dist/platforms/bigo.js +1 -0
- package/dist/platforms/facebook.js +1 -0
- package/dist/platforms/google.js +1 -0
- package/dist/platforms/inmobi.js +1 -0
- package/dist/platforms/ironsource.js +1 -0
- package/dist/platforms/liftoff.js +1 -0
- package/dist/platforms/mintegral.js +1 -0
- package/dist/platforms/moloco.js +1 -0
- package/dist/platforms/playcraft.js +1 -0
- package/dist/platforms/remerge.js +1 -0
- package/dist/platforms/snapchat.js +1 -0
- package/dist/platforms/tiktok.js +1 -0
- package/dist/platforms/unity.js +1 -0
- package/dist/production-animation-presence.d.ts +2 -0
- package/dist/production-animation-presence.js +146 -0
- package/dist/production-inputs.d.ts +30 -0
- package/dist/production-inputs.js +262 -0
- package/dist/production-source-paths.d.ts +5 -0
- package/dist/production-source-paths.js +27 -0
- package/dist/redirect-policy.d.ts +23 -0
- package/dist/redirect-policy.js +25 -0
- package/dist/types.d.ts +12 -2
- package/dist/validation/artifact-qa.d.ts +36 -0
- package/dist/validation/artifact-qa.js +263 -0
- package/dist/validation/artifact-rules.d.ts +39 -0
- package/dist/validation/artifact-rules.js +451 -0
- package/dist/validation/artifact.d.ts +14 -0
- package/dist/validation/artifact.js +188 -0
- package/dist/validation/html-validator.d.ts +5 -0
- package/dist/validation/html-validator.js +19 -17
- package/dist/validation/index.d.ts +5 -0
- package/dist/validation/index.js +5 -0
- package/dist/validation/package-validator.d.ts +2 -0
- package/dist/validation/package-validator.js +25 -64
- package/dist/validation/playwright-driver.d.ts +37 -0
- package/dist/validation/playwright-driver.js +302 -0
- package/dist/validation/profiles.d.ts +13 -1
- package/dist/validation/profiles.js +27 -0
- package/dist/validation/runtime-cover.d.ts +32 -0
- package/dist/validation/runtime-cover.js +120 -0
- package/dist/validation/runtime-load.d.ts +141 -0
- package/dist/validation/runtime-load.js +812 -0
- package/dist/validation/source-scanner.d.ts +11 -0
- package/dist/validation/source-scanner.js +32 -2
- package/dist/validation/types.d.ts +201 -2
- package/dist/validation/types.js +1 -1
- package/dist/validation/validation-runner.d.ts +22 -2
- package/dist/validation/validation-runner.js +64 -3
- package/dist/validation/zip-reader.d.ts +7 -0
- package/dist/validation/zip-reader.js +159 -26
- package/dist/vite/config-builder-generic.d.ts +3 -1
- package/dist/vite/config-builder-generic.js +9 -7
- package/dist/vite/config-builder.js +1 -1
- package/dist/vite/plugin-platform.js +16 -30
- package/dist/vite/plugin-source-builder.d.ts +2 -14
- package/dist/vite/plugin-source-builder.js +61 -997
- package/dist/vite/source-builder-asset-paths.d.ts +25 -0
- package/dist/vite/source-builder-asset-paths.js +154 -0
- package/dist/vite/source-builder-esm.d.ts +7 -0
- package/dist/vite/source-builder-esm.js +877 -0
- package/dist/vite/source-builder-types.d.ts +14 -0
- package/dist/vite/source-builder-types.js +1 -0
- package/dist/vite/workspace.d.ts +6 -0
- package/dist/vite/workspace.js +41 -0
- package/dist/vite-builder.d.ts +4 -5
- package/dist/vite-builder.js +33 -34
- package/dist/workspace-css-imports.d.ts +2 -0
- package/dist/workspace-css-imports.js +139 -0
- package/dist/workspace-model-source.d.ts +14 -0
- package/dist/workspace-model-source.js +184 -0
- package/dist/workspace-public-imports.d.ts +11 -0
- package/dist/workspace-public-imports.js +232 -0
- package/dist/workspace-public-source.d.ts +15 -0
- package/dist/workspace-public-source.js +564 -0
- package/package.json +30 -4
- package/templates/patches/playcraft-redirect-policy.js +115 -0
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ProductionSourceFile } from './production-inputs.js';
|
|
2
|
+
/** This only permits reads by the package inspector; a path alone never selects a file. */
|
|
3
|
+
export declare const productionImmutableAudioPath: (value: string) => boolean;
|
|
4
|
+
/** Keep the legacy sound/program closures, including unbound immutable packages. */
|
|
5
|
+
export declare function readProductionAudioFiles(root: string, read: (path: string, maximum: number) => Promise<ProductionSourceFile>): Promise<Map<string, ProductionSourceFile>>;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { lstat, readdir } from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { inspectAudioArtifactPackage } from '@playcraft/common/cloud-workspace/runtime';
|
|
4
|
+
import { inspectAudioProgramPackage, audioPackageRendererScript, audioProgramGameRuntime, audioProgramPrerenderRuntime, } from '@playcraft/common/audio-package-node';
|
|
5
|
+
const HASH = /^[a-f0-9]{64}$/;
|
|
6
|
+
// Legacy sound and program inputs each permit 64 MiB and 256 packages.
|
|
7
|
+
const MAXIMUM_BYTES = 128 * 1024 * 1024;
|
|
8
|
+
const MAXIMUM_PACKAGES = 512;
|
|
9
|
+
const MAXIMUM_ENTRIES = 8192;
|
|
10
|
+
const SOUND_MEMBER = '(?:manifest\\.json|sound\\.(?:pcaudio\\.json|pcscore\\.json|mjs)|runtime\\.mjs|audition\\.wav)';
|
|
11
|
+
const PROGRAM_MEMBER = '(?:manifest\\.json|program\\.pcaudioprogram\\.json|program\\.mjs|prerender\\.mjs)';
|
|
12
|
+
const H = '[a-f0-9]{64}';
|
|
13
|
+
const IMMUTABLE_AUDIO = new RegExp(`^dist/audio/(?:${H}/${H}/${H}/${SOUND_MEMBER}|programs/${H}/${H}/${H}/${PROGRAM_MEMBER}|shared/${H}/(?:runtime\\.mjs|state\\.wav))$`);
|
|
14
|
+
/** This only permits reads by the package inspector; a path alone never selects a file. */
|
|
15
|
+
export const productionImmutableAudioPath = (value) => IMMUTABLE_AUDIO.test(value) ||
|
|
16
|
+
// Existing programs also reference imported PCM samples below hashed audio
|
|
17
|
+
// paths. The inspector proves the full dependency closure and WAV bytes;
|
|
18
|
+
// the caller separately rejects private paths before allowing this read.
|
|
19
|
+
(/^dist\/audio\/[a-zA-Z0-9._/-]+$/.test(value) && value.split('/').some((part) => HASH.test(part)));
|
|
20
|
+
function invalid(code = 'production_audio_package_invalid') {
|
|
21
|
+
throw Object.assign(new Error(code), { code });
|
|
22
|
+
}
|
|
23
|
+
/** Keep the legacy sound/program closures, including unbound immutable packages. */
|
|
24
|
+
export async function readProductionAudioFiles(root, read) {
|
|
25
|
+
const result = new Map();
|
|
26
|
+
let entries = 0, packages = 0, total = 0;
|
|
27
|
+
const add = (name, file) => {
|
|
28
|
+
const previous = result.get(name);
|
|
29
|
+
if (previous && (previous.mode !== file.mode || !previous.body.equals(file.body)))
|
|
30
|
+
invalid('production_audio_input_changed');
|
|
31
|
+
if (!previous) {
|
|
32
|
+
total += file.body.length;
|
|
33
|
+
if (total > MAXIMUM_BYTES)
|
|
34
|
+
invalid('production_audio_input_limit');
|
|
35
|
+
result.set(name, file);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
const directory = async (relative) => {
|
|
39
|
+
let current = root;
|
|
40
|
+
for (const part of relative.split('/')) {
|
|
41
|
+
current = path.join(current, part);
|
|
42
|
+
const entry = await lstat(current).catch((error) => {
|
|
43
|
+
if (error.code === 'ENOENT')
|
|
44
|
+
return undefined;
|
|
45
|
+
throw error;
|
|
46
|
+
});
|
|
47
|
+
if (!entry)
|
|
48
|
+
return false;
|
|
49
|
+
if (!entry.isDirectory() || entry.isSymbolicLink())
|
|
50
|
+
invalid('production_audio_path_unsafe');
|
|
51
|
+
}
|
|
52
|
+
return true;
|
|
53
|
+
};
|
|
54
|
+
const candidates = [];
|
|
55
|
+
const visit = async (prefix, depth, program) => {
|
|
56
|
+
if (!(await directory(prefix)))
|
|
57
|
+
return;
|
|
58
|
+
const members = await readdir(path.join(root, prefix), { withFileTypes: true });
|
|
59
|
+
entries += members.length;
|
|
60
|
+
if (entries > MAXIMUM_ENTRIES)
|
|
61
|
+
invalid('production_audio_input_limit');
|
|
62
|
+
if (depth === 3) {
|
|
63
|
+
const names = program ? ['manifest.json'] : ['sound.pcaudio.json', 'sound.pcscore.json'];
|
|
64
|
+
for (const name of names) {
|
|
65
|
+
const entry = members.find((member) => member.name === name);
|
|
66
|
+
if (!entry)
|
|
67
|
+
continue;
|
|
68
|
+
if (++packages > MAXIMUM_PACKAGES)
|
|
69
|
+
invalid('production_audio_input_limit');
|
|
70
|
+
if (program && (!entry.isFile() || entry.isSymbolicLink()))
|
|
71
|
+
invalid();
|
|
72
|
+
candidates.push({ path: `${prefix}/${name}`, program });
|
|
73
|
+
}
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
for (const entry of members.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
77
|
+
if (!HASH.test(entry.name))
|
|
78
|
+
continue;
|
|
79
|
+
if (!entry.isDirectory() || entry.isSymbolicLink()) {
|
|
80
|
+
if (program)
|
|
81
|
+
invalid();
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
await visit(`${prefix}/${entry.name}`, depth + 1, program);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
await visit('dist/audio', 0, false);
|
|
88
|
+
await visit('dist/audio/programs', 0, true);
|
|
89
|
+
const engines = {
|
|
90
|
+
soundRuntime: audioPackageRendererScript,
|
|
91
|
+
gameRuntime: audioProgramGameRuntime,
|
|
92
|
+
prerenderRuntime: audioProgramPrerenderRuntime,
|
|
93
|
+
};
|
|
94
|
+
for (const candidate of candidates) {
|
|
95
|
+
const members = new Map();
|
|
96
|
+
const bytes = async (name, maximum) => {
|
|
97
|
+
const file = members.get(name) ?? await read(name, maximum);
|
|
98
|
+
if (file.body.length > maximum)
|
|
99
|
+
invalid();
|
|
100
|
+
members.set(name, file);
|
|
101
|
+
return file.body;
|
|
102
|
+
};
|
|
103
|
+
let sourcePath;
|
|
104
|
+
if (candidate.program) {
|
|
105
|
+
const bundle = await inspectAudioProgramPackage(candidate.path, bytes, engines);
|
|
106
|
+
sourcePath = bundle.sourcePath;
|
|
107
|
+
// Only the verified manifest/dependency closure is eligible. Do not promote
|
|
108
|
+
// unrelated files merely because they neighbor a valid immutable package.
|
|
109
|
+
for (const name of bundle.files.keys())
|
|
110
|
+
add(name, members.get(name));
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
let bundle;
|
|
114
|
+
try {
|
|
115
|
+
bundle = await inspectAudioArtifactPackage(candidate.path, bytes);
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
continue;
|
|
119
|
+
} // Existing unbound invalid sound candidates are ignored.
|
|
120
|
+
sourcePath = bundle.sourcePath;
|
|
121
|
+
for (const [name, file] of members)
|
|
122
|
+
add(name, file);
|
|
123
|
+
}
|
|
124
|
+
// Editable source may have a newer revision or be absent; immutable package
|
|
125
|
+
// bytes remain selected. Other read failures, including private paths, fail.
|
|
126
|
+
const source = await read(sourcePath, 65536).catch((error) => {
|
|
127
|
+
if (error.code === 'ENOENT')
|
|
128
|
+
return undefined;
|
|
129
|
+
throw error;
|
|
130
|
+
});
|
|
131
|
+
if (source)
|
|
132
|
+
add(sourcePath, source);
|
|
133
|
+
}
|
|
134
|
+
return result;
|
|
135
|
+
}
|
package/dist/base-builder.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import { EngineDetector, PlayCanvasAdapter, GenericAdapter, PlayableScriptsAdapter } from './engines/index.js';
|
|
1
|
+
import { EngineDetector, PlayCanvasAdapter, GenericAdapter, PlayableScriptsAdapter, } from './engines/index.js';
|
|
2
|
+
import { writeFile } from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { executeProjectDelivery } from './delivery/project-delivery.js';
|
|
2
5
|
import { formatValidationSummary, hasBlockingValidationErrors, runPlayableValidation, saveValidationReport, } from './validation/index.js';
|
|
3
6
|
/**
|
|
4
7
|
* 基础构建器 - 路由到不同构建工具适配器
|
|
@@ -32,6 +35,41 @@ export class BaseBuilder {
|
|
|
32
35
|
* 根据构建工具路由到对应适配器
|
|
33
36
|
*/
|
|
34
37
|
async build() {
|
|
38
|
+
const explicit = await executeProjectDelivery(this.projectDir, {
|
|
39
|
+
outputDir: this.options.outputDir,
|
|
40
|
+
channel: this.options.validation?.channel,
|
|
41
|
+
format: this.options.format,
|
|
42
|
+
expectedPreparation: this.options.deliveryPreparation,
|
|
43
|
+
trustedDependencyRoot: this.options.trustedDependencyRoot,
|
|
44
|
+
});
|
|
45
|
+
if (explicit) {
|
|
46
|
+
const metadata = {
|
|
47
|
+
mode: 'classic',
|
|
48
|
+
engine: 'generic',
|
|
49
|
+
buildTool: 'generic',
|
|
50
|
+
buildOutputDir: explicit.directory,
|
|
51
|
+
deliveryRecipeDigest: explicit.recipeDigest,
|
|
52
|
+
...(explicit.kind === 'channel-package'
|
|
53
|
+
? { skipChannelBuild: true, deliveryArtifact: explicit.entry }
|
|
54
|
+
: {}),
|
|
55
|
+
};
|
|
56
|
+
await writeFile(path.join(explicit.directory, '.build-metadata.json'), JSON.stringify(metadata, null, 2));
|
|
57
|
+
this._metadata = metadata;
|
|
58
|
+
return {
|
|
59
|
+
outputDir: explicit.directory,
|
|
60
|
+
metadata,
|
|
61
|
+
files: {
|
|
62
|
+
html: explicit.entry,
|
|
63
|
+
engine: null,
|
|
64
|
+
config: '',
|
|
65
|
+
settings: null,
|
|
66
|
+
modules: null,
|
|
67
|
+
start: '',
|
|
68
|
+
scenes: [],
|
|
69
|
+
assets: [],
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
}
|
|
35
73
|
// 1. 确定引擎类型和构建工具
|
|
36
74
|
const { engine, buildTool } = await this.resolveDetection();
|
|
37
75
|
console.log(`[BaseBuilder] 使用引擎: ${engine}, 构建工具: ${buildTool}`);
|
|
@@ -62,7 +100,9 @@ export class BaseBuilder {
|
|
|
62
100
|
}
|
|
63
101
|
async runPrebuildValidation(engine, buildTool) {
|
|
64
102
|
const validationOptions = this.options.validation;
|
|
65
|
-
if (validationOptions?.enabled === false ||
|
|
103
|
+
if (validationOptions?.enabled === false ||
|
|
104
|
+
validationOptions?.sourcePrechecked ||
|
|
105
|
+
!validationOptions?.channel) {
|
|
66
106
|
return;
|
|
67
107
|
}
|
|
68
108
|
if (!this.isPhaserValidationTarget(engine, buildTool)) {
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { BuildPlan } from './build-plan.types.js';
|
|
2
|
+
import type { ArtifactDescriptor } from './artifact-descriptor.js';
|
|
3
|
+
export declare const AGENT_BUILD_RUNNER_VERSION = "agent-build-runner-v1";
|
|
4
|
+
export type AgentBuildRunnerRequest = {
|
|
5
|
+
version: 1;
|
|
6
|
+
operationId: string;
|
|
7
|
+
requestId: string;
|
|
8
|
+
inputHash: string;
|
|
9
|
+
bindingHash: string;
|
|
10
|
+
buildId: string;
|
|
11
|
+
attempt: number;
|
|
12
|
+
phase: 'plan' | 'build';
|
|
13
|
+
projectRoot: string;
|
|
14
|
+
attemptRoot: string;
|
|
15
|
+
channel: 'applovin' | 'google';
|
|
16
|
+
format: 'html' | 'zip';
|
|
17
|
+
commitHash: string;
|
|
18
|
+
recipeDigest: string;
|
|
19
|
+
fingerprint: string;
|
|
20
|
+
plan?: BuildPlan;
|
|
21
|
+
environmentDigest?: string;
|
|
22
|
+
};
|
|
23
|
+
export type AgentBuildRunnerSuccess = {
|
|
24
|
+
outcome: 'settled';
|
|
25
|
+
status: 'success';
|
|
26
|
+
fingerprint: string;
|
|
27
|
+
phase: 'plan' | 'build';
|
|
28
|
+
plan?: BuildPlan;
|
|
29
|
+
environmentDigest?: string;
|
|
30
|
+
descriptor?: ArtifactDescriptor;
|
|
31
|
+
artifactName?: string;
|
|
32
|
+
};
|
|
33
|
+
export declare class AgentBuildRunnerError extends Error {
|
|
34
|
+
readonly code: string;
|
|
35
|
+
readonly stage: string;
|
|
36
|
+
readonly retryable: boolean;
|
|
37
|
+
constructor(code: string, stage: string, message: string, retryable?: boolean);
|
|
38
|
+
}
|
|
39
|
+
export declare function runAgentBuildRunner(request: AgentBuildRunnerRequest): Promise<AgentBuildRunnerSuccess>;
|
|
40
|
+
export { parseBuildPlan } from './build-plan.js';
|
|
41
|
+
export { parseArtifactDescriptor } from './artifact-descriptor.js';
|
|
42
|
+
export { verifyChannelArtifactDescriptor } from './artifact-package.js';
|
|
43
|
+
export type { BuildPlan } from './build-plan.types.js';
|
|
44
|
+
export type { ArtifactDescriptor } from './artifact-descriptor.js';
|
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { createRecipeBuildPlan } from './recipe-build-plan.js';
|
|
6
|
+
import { parseBuildPlan, buildPlanDigest } from './build-plan.js';
|
|
7
|
+
import { wrapDeliveryCommand } from './command-isolation.js';
|
|
8
|
+
import { runDeliveryCommand } from './commands.js';
|
|
9
|
+
import { safeExistingPath } from './safe-path.js';
|
|
10
|
+
import { readAgentBuildSourceIdentity } from './agent-build-source.js';
|
|
11
|
+
import { packageChannelArtifact, describeChannelArtifact, readArtifactDirectory } from './artifact-package.js';
|
|
12
|
+
import { deliveryContext, ProjectDeliveryError, readProjectDeliveryRecipe, recipeDigest } from './recipe.js';
|
|
13
|
+
export const AGENT_BUILD_RUNNER_VERSION = 'agent-build-runner-v1';
|
|
14
|
+
export class AgentBuildRunnerError extends Error {
|
|
15
|
+
constructor(code, stage, message, retryable = false) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.code = code;
|
|
18
|
+
this.stage = stage;
|
|
19
|
+
this.retryable = retryable;
|
|
20
|
+
this.name = 'AgentBuildRunnerError';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function digest(value) {
|
|
24
|
+
return createHash('sha256').update(JSON.stringify(value)).digest('hex');
|
|
25
|
+
}
|
|
26
|
+
function assertRequest(request) {
|
|
27
|
+
if (request.version !== 1 ||
|
|
28
|
+
!/^[a-f0-9]{64}$/.test(request.inputHash) ||
|
|
29
|
+
!/^[a-f0-9]{64}$/.test(request.bindingHash) ||
|
|
30
|
+
!/^[a-f0-9]{40,64}$/.test(request.commitHash) ||
|
|
31
|
+
!/^[a-f0-9]{64}$/.test(request.recipeDigest) ||
|
|
32
|
+
!/^[a-f0-9]{64}$/.test(request.fingerprint) ||
|
|
33
|
+
!path.isAbsolute(request.projectRoot) ||
|
|
34
|
+
!path.isAbsolute(request.attemptRoot)) {
|
|
35
|
+
throw new AgentBuildRunnerError('agent_build_runner_request_invalid', 'environment_preflight', 'Invalid Runner request');
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function workRoot(request) {
|
|
39
|
+
return path.join(request.attemptRoot, 'work');
|
|
40
|
+
}
|
|
41
|
+
function sourceRoot(request) {
|
|
42
|
+
return path.join(workRoot(request), 'source');
|
|
43
|
+
}
|
|
44
|
+
function isolation(request) {
|
|
45
|
+
return { projectRoot: request.projectRoot, isolatedRoot: workRoot(request) };
|
|
46
|
+
}
|
|
47
|
+
async function execute(argv, cwd, timeoutMs = 60000) {
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
const child = spawn(argv[0], argv.slice(1), {
|
|
50
|
+
cwd,
|
|
51
|
+
shell: false,
|
|
52
|
+
detached: process.platform !== 'win32',
|
|
53
|
+
env: {
|
|
54
|
+
PATH: process.env.PATH || '/usr/local/bin:/usr/bin:/bin',
|
|
55
|
+
HOME: cwd,
|
|
56
|
+
GIT_CONFIG_GLOBAL: '/dev/null',
|
|
57
|
+
GIT_CONFIG_NOSYSTEM: '1',
|
|
58
|
+
GIT_OPTIONAL_LOCKS: '0',
|
|
59
|
+
GIT_TERMINAL_PROMPT: '0',
|
|
60
|
+
CI: 'true',
|
|
61
|
+
},
|
|
62
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
63
|
+
});
|
|
64
|
+
let stdout = '';
|
|
65
|
+
let stderr = '';
|
|
66
|
+
const collect = (target) => (chunk) => {
|
|
67
|
+
if (target === 'stdout')
|
|
68
|
+
stdout = (stdout + chunk.toString()).slice(-16384);
|
|
69
|
+
else
|
|
70
|
+
stderr = (stderr + chunk.toString()).slice(-4000);
|
|
71
|
+
};
|
|
72
|
+
child.stdout.on('data', collect('stdout'));
|
|
73
|
+
child.stderr.on('data', collect('stderr'));
|
|
74
|
+
const stop = () => {
|
|
75
|
+
try {
|
|
76
|
+
if (process.platform !== 'win32' && child.pid)
|
|
77
|
+
process.kill(-child.pid, 'SIGTERM');
|
|
78
|
+
else
|
|
79
|
+
child.kill('SIGTERM');
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
// The process may already be gone.
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
const timer = setTimeout(stop, timeoutMs);
|
|
86
|
+
child.once('error', (error) => {
|
|
87
|
+
clearTimeout(timer);
|
|
88
|
+
reject(error);
|
|
89
|
+
});
|
|
90
|
+
child.once('close', (code, signal) => {
|
|
91
|
+
clearTimeout(timer);
|
|
92
|
+
if (code === 0)
|
|
93
|
+
resolve(stdout);
|
|
94
|
+
else
|
|
95
|
+
reject(new AgentBuildRunnerError('source_snapshot_failed', 'source_snapshot', `Controlled Git command failed (${code ?? signal}): ${stderr}`));
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
async function exportCommit(request) {
|
|
100
|
+
const git = [
|
|
101
|
+
'-c', 'core.hooksPath=/dev/null',
|
|
102
|
+
'-c', 'core.fsmonitor=false',
|
|
103
|
+
'-C', request.projectRoot,
|
|
104
|
+
];
|
|
105
|
+
const environment = {
|
|
106
|
+
PATH: process.env.PATH || '/usr/local/bin:/usr/bin:/bin',
|
|
107
|
+
HOME: workRoot(request),
|
|
108
|
+
GIT_CONFIG_GLOBAL: '/dev/null',
|
|
109
|
+
GIT_CONFIG_NOSYSTEM: '1',
|
|
110
|
+
GIT_OPTIONAL_LOCKS: '0',
|
|
111
|
+
GIT_TERMINAL_PROMPT: '0',
|
|
112
|
+
};
|
|
113
|
+
const head = async () => (await execute(['git', ...git, 'rev-parse', '--verify', 'HEAD^{commit}'], request.projectRoot)).trim();
|
|
114
|
+
if (await head() !== request.commitHash) {
|
|
115
|
+
throw new AgentBuildRunnerError('build_source_changed', 'source_snapshot', 'Sandbox HEAD differs from the reserved remote commit');
|
|
116
|
+
}
|
|
117
|
+
const source = sourceRoot(request);
|
|
118
|
+
await mkdir(source, { recursive: false, mode: 0o700 });
|
|
119
|
+
await new Promise((resolve, reject) => {
|
|
120
|
+
const archive = spawn('git', [...git, 'archive', '--format=tar', request.commitHash], {
|
|
121
|
+
cwd: request.projectRoot,
|
|
122
|
+
shell: false,
|
|
123
|
+
env: environment,
|
|
124
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
125
|
+
});
|
|
126
|
+
const extract = spawn('tar', ['-x', '-C', source, '--no-same-owner', '--no-same-permissions'], {
|
|
127
|
+
cwd: workRoot(request),
|
|
128
|
+
shell: false,
|
|
129
|
+
env: environment,
|
|
130
|
+
stdio: ['pipe', 'ignore', 'pipe'],
|
|
131
|
+
});
|
|
132
|
+
let errors = '';
|
|
133
|
+
let archiveCode = null;
|
|
134
|
+
let extractCode = null;
|
|
135
|
+
archive.stderr.on('data', (chunk) => { errors = `${errors}${chunk}`.slice(-4000); });
|
|
136
|
+
extract.stderr.on('data', (chunk) => { errors = `${errors}${chunk}`.slice(-4000); });
|
|
137
|
+
archive.stdout.pipe(extract.stdin);
|
|
138
|
+
const done = () => {
|
|
139
|
+
if (archiveCode === null || extractCode === null)
|
|
140
|
+
return;
|
|
141
|
+
if (archiveCode === 0 && extractCode === 0)
|
|
142
|
+
resolve();
|
|
143
|
+
else
|
|
144
|
+
reject(new AgentBuildRunnerError('source_snapshot_failed', 'source_snapshot', `Could not export the reserved commit: ${errors}`));
|
|
145
|
+
};
|
|
146
|
+
archive.once('error', reject);
|
|
147
|
+
extract.once('error', reject);
|
|
148
|
+
archive.once('close', (code) => { archiveCode = code; done(); });
|
|
149
|
+
extract.once('close', (code) => { extractCode = code; done(); });
|
|
150
|
+
});
|
|
151
|
+
if (await head() !== request.commitHash) {
|
|
152
|
+
throw new AgentBuildRunnerError('build_source_changed', 'source_snapshot', 'Sandbox HEAD changed while exporting the reserved commit');
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
async function packageManager(root) {
|
|
156
|
+
const candidates = [
|
|
157
|
+
['pnpm', ['pnpm-lock.yaml']],
|
|
158
|
+
['npm', ['package-lock.json', 'npm-shrinkwrap.json']],
|
|
159
|
+
['yarn', ['yarn.lock']],
|
|
160
|
+
['bun', ['bun.lock', 'bun.lockb']],
|
|
161
|
+
];
|
|
162
|
+
const found = [];
|
|
163
|
+
for (const [name, files] of candidates) {
|
|
164
|
+
for (const lockfile of files) {
|
|
165
|
+
try {
|
|
166
|
+
const bytes = await readFile(path.join(root, lockfile));
|
|
167
|
+
found.push({ name, lockfile, lockDigest: createHash('sha256').update(bytes).digest('hex') });
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
if (error.code !== 'ENOENT')
|
|
172
|
+
throw error;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (found.length !== 1) {
|
|
177
|
+
throw new AgentBuildRunnerError('environment_unsupported', 'environment_preflight', 'Exactly one supported package-manager lockfile is required');
|
|
178
|
+
}
|
|
179
|
+
return found[0];
|
|
180
|
+
}
|
|
181
|
+
async function isolatedVersion(executable, request) {
|
|
182
|
+
const argv = await wrapDeliveryCommand([executable, '--version'], isolation(request));
|
|
183
|
+
return new Promise((resolve, reject) => {
|
|
184
|
+
const child = spawn(argv[0], argv.slice(1), {
|
|
185
|
+
cwd: sourceRoot(request),
|
|
186
|
+
shell: false,
|
|
187
|
+
env: { PATH: process.env.PATH || '/usr/local/bin:/usr/bin:/bin', HOME: workRoot(request), CI: 'true' },
|
|
188
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
189
|
+
});
|
|
190
|
+
let stdout = '';
|
|
191
|
+
let stderr = '';
|
|
192
|
+
child.stdout.on('data', (chunk) => { stdout = `${stdout}${chunk}`.slice(-2000); });
|
|
193
|
+
child.stderr.on('data', (chunk) => { stderr = `${stderr}${chunk}`.slice(-2000); });
|
|
194
|
+
const timer = setTimeout(() => child.kill('SIGTERM'), 20000);
|
|
195
|
+
child.once('error', reject);
|
|
196
|
+
child.once('close', (code) => {
|
|
197
|
+
clearTimeout(timer);
|
|
198
|
+
if (code === 0 && stdout.trim())
|
|
199
|
+
resolve(stdout.trim());
|
|
200
|
+
else
|
|
201
|
+
reject(new AgentBuildRunnerError('delivery_isolation_unavailable', 'environment_preflight', `Managed isolation preflight failed: ${stderr || `exit ${code}`}`));
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
async function reportProgress(request, stage) {
|
|
206
|
+
const target = path.join(request.attemptRoot, 'progress.json');
|
|
207
|
+
const temporary = `${target}.${process.pid}.tmp`;
|
|
208
|
+
await writeFile(temporary, JSON.stringify({ fingerprint: request.fingerprint, stage }), { mode: 0o600, flag: 'wx' });
|
|
209
|
+
await rename(temporary, target);
|
|
210
|
+
}
|
|
211
|
+
async function sourceIdentityAt(root, recipe, stage, legacyOutputDirectory = recipe.output.directory) {
|
|
212
|
+
try {
|
|
213
|
+
return await readAgentBuildSourceIdentity(root, recipe, legacyOutputDirectory);
|
|
214
|
+
}
|
|
215
|
+
catch (error) {
|
|
216
|
+
if (error instanceof ProjectDeliveryError)
|
|
217
|
+
throw new AgentBuildRunnerError(error.code, stage, error.message);
|
|
218
|
+
throw new AgentBuildRunnerError('source_snapshot_failed', stage, 'The source input closure could not be verified');
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
async function plan(request) {
|
|
222
|
+
await mkdir(workRoot(request), { recursive: false, mode: 0o700 });
|
|
223
|
+
await exportCommit(request);
|
|
224
|
+
const source = sourceRoot(request);
|
|
225
|
+
const recipe = await readProjectDeliveryRecipe(source);
|
|
226
|
+
if (!recipe || recipeDigest(recipe) !== request.recipeDigest) {
|
|
227
|
+
throw new AgentBuildRunnerError('delivery_recipe_digest_mismatch', 'source_snapshot', 'The exported recipe differs from the Business reservation');
|
|
228
|
+
}
|
|
229
|
+
const sourceIdentity = await sourceIdentityAt(source, recipe, 'source_snapshot');
|
|
230
|
+
const manager = await packageManager(source);
|
|
231
|
+
const value = createRecipeBuildPlan(recipe, {
|
|
232
|
+
commitHash: request.commitHash,
|
|
233
|
+
...sourceIdentity,
|
|
234
|
+
channel: request.channel,
|
|
235
|
+
format: request.format,
|
|
236
|
+
packageManager: manager.name,
|
|
237
|
+
});
|
|
238
|
+
await reportProgress(request, 'environment_preflight');
|
|
239
|
+
const managerVersion = await isolatedVersion(manager.name, request);
|
|
240
|
+
await isolatedVersion(process.execPath, request);
|
|
241
|
+
const environmentDigest = digest({
|
|
242
|
+
version: 1,
|
|
243
|
+
runner: AGENT_BUILD_RUNNER_VERSION,
|
|
244
|
+
node: process.version,
|
|
245
|
+
packageManager: manager.name,
|
|
246
|
+
packageManagerVersion: managerVersion,
|
|
247
|
+
platform: process.platform,
|
|
248
|
+
arch: process.arch,
|
|
249
|
+
lockfile: manager.lockfile,
|
|
250
|
+
lockDigest: manager.lockDigest,
|
|
251
|
+
planDigest: buildPlanDigest(value),
|
|
252
|
+
});
|
|
253
|
+
return { outcome: 'settled', status: 'success', fingerprint: request.fingerprint, phase: 'plan', plan: value, environmentDigest };
|
|
254
|
+
}
|
|
255
|
+
function installCommand(manager) {
|
|
256
|
+
if (manager === 'pnpm')
|
|
257
|
+
return ['pnpm', 'install', '--frozen-lockfile'];
|
|
258
|
+
if (manager === 'npm')
|
|
259
|
+
return ['npm', 'ci'];
|
|
260
|
+
if (manager === 'yarn')
|
|
261
|
+
return ['yarn', 'install', '--frozen-lockfile'];
|
|
262
|
+
return ['bun', 'install', '--frozen-lockfile'];
|
|
263
|
+
}
|
|
264
|
+
async function build(request) {
|
|
265
|
+
const plan = parseBuildPlan(request.plan);
|
|
266
|
+
if (plan.source.commitHash !== request.commitHash ||
|
|
267
|
+
plan.source.recipeDigest !== request.recipeDigest ||
|
|
268
|
+
plan.target.channel !== request.channel ||
|
|
269
|
+
plan.target.format !== request.format ||
|
|
270
|
+
!request.environmentDigest) {
|
|
271
|
+
throw new AgentBuildRunnerError('delivery_build_plan_invalid', 'preparing', 'The bound BuildPlan does not match this attempt');
|
|
272
|
+
}
|
|
273
|
+
const source = sourceRoot(request);
|
|
274
|
+
const recipe = await readProjectDeliveryRecipe(source);
|
|
275
|
+
if (!recipe || recipeDigest(recipe) !== request.recipeDigest) {
|
|
276
|
+
throw new AgentBuildRunnerError('delivery_recipe_digest_mismatch', 'preparing', 'The source recipe changed after planning');
|
|
277
|
+
}
|
|
278
|
+
const context = deliveryContext(recipe, request);
|
|
279
|
+
const current = await sourceIdentityAt(source, recipe, 'preparing', context.outputDirectory);
|
|
280
|
+
if (current.sourceFingerprint !== plan.source.sourceFingerprint || current.sourceRulesVersion !== plan.source.sourceRulesVersion) {
|
|
281
|
+
throw new AgentBuildRunnerError('build_source_changed', 'preparing', 'The source snapshot changed after planning');
|
|
282
|
+
}
|
|
283
|
+
const controller = new AbortController();
|
|
284
|
+
const options = { channel: request.channel, format: request.format, signal: controller.signal };
|
|
285
|
+
const isolated = isolation(request);
|
|
286
|
+
await reportProgress(request, 'preparing');
|
|
287
|
+
await runDeliveryCommand(source, { argv: installCommand(plan.environment.packageManager), timeoutMs: plan.environment.maxDurationMs }, context.replace, options, isolated);
|
|
288
|
+
for (const executable of plan.environment.executables) {
|
|
289
|
+
await runDeliveryCommand(source, { argv: [context.replace(executable), '--version'], timeoutMs: 20000 }, context.replace, options, isolated);
|
|
290
|
+
}
|
|
291
|
+
for (const command of plan.setup.prepareCommands) {
|
|
292
|
+
await runDeliveryCommand(source, command, context.replace, options, isolated);
|
|
293
|
+
}
|
|
294
|
+
await reportProgress(request, 'compiling');
|
|
295
|
+
if (plan.compile.command) {
|
|
296
|
+
await runDeliveryCommand(source, plan.compile.command, context.replace, options, isolated);
|
|
297
|
+
}
|
|
298
|
+
const after = await sourceIdentityAt(source, recipe, 'compiling', context.outputDirectory);
|
|
299
|
+
if (after.sourceFingerprint !== plan.source.sourceFingerprint || after.sourceRulesVersion !== plan.source.sourceRulesVersion) {
|
|
300
|
+
throw new AgentBuildRunnerError('build_source_changed', 'compiling', 'Project commands modified undeclared source inputs');
|
|
301
|
+
}
|
|
302
|
+
await reportProgress(request, 'packaging');
|
|
303
|
+
const output = await safeExistingPath(source, context.outputDirectory);
|
|
304
|
+
const entry = await safeExistingPath(source, path.posix.join(context.outputDirectory, context.entry));
|
|
305
|
+
const identity = {
|
|
306
|
+
sourceFingerprint: plan.source.sourceFingerprint,
|
|
307
|
+
recipeDigest: plan.source.recipeDigest,
|
|
308
|
+
environmentDigest: request.environmentDigest,
|
|
309
|
+
channelRulesVersion: plan.delivery.channelRulesVersion,
|
|
310
|
+
};
|
|
311
|
+
let bytes;
|
|
312
|
+
let descriptor;
|
|
313
|
+
let artifactName;
|
|
314
|
+
if (plan.compile.output.kind === 'web-bundle') {
|
|
315
|
+
const files = await readArtifactDirectory(output, request.channel);
|
|
316
|
+
const packaged = await packageChannelArtifact({ channel: request.channel, format: request.format, files, ...identity });
|
|
317
|
+
bytes = packaged.bytes;
|
|
318
|
+
descriptor = packaged.descriptor;
|
|
319
|
+
artifactName = request.format === 'html' ? files[0].path : 'index.html';
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
bytes = await readFile(entry);
|
|
323
|
+
artifactName = request.format === 'html' ? path.basename(entry) : 'index.html';
|
|
324
|
+
descriptor = await describeChannelArtifact({
|
|
325
|
+
channel: request.channel,
|
|
326
|
+
format: request.format,
|
|
327
|
+
bytes,
|
|
328
|
+
artifactName,
|
|
329
|
+
...identity,
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
await reportProgress(request, 'validating');
|
|
333
|
+
const artifact = path.join(request.attemptRoot, `artifact.${request.format}`);
|
|
334
|
+
const descriptorPath = path.join(request.attemptRoot, 'descriptor.json');
|
|
335
|
+
const temporaryArtifact = `${artifact}.${process.pid}.tmp`;
|
|
336
|
+
const temporaryDescriptor = `${descriptorPath}.${process.pid}.tmp`;
|
|
337
|
+
await writeFile(temporaryArtifact, bytes, { flag: 'wx', mode: 0o600 });
|
|
338
|
+
await rename(temporaryArtifact, artifact);
|
|
339
|
+
await writeFile(temporaryDescriptor, JSON.stringify(descriptor), { flag: 'wx', mode: 0o600 });
|
|
340
|
+
await rename(temporaryDescriptor, descriptorPath);
|
|
341
|
+
return { outcome: 'settled', status: 'success', fingerprint: request.fingerprint, phase: 'build', descriptor, artifactName };
|
|
342
|
+
}
|
|
343
|
+
export async function runAgentBuildRunner(request) {
|
|
344
|
+
assertRequest(request);
|
|
345
|
+
try {
|
|
346
|
+
return request.phase === 'plan' ? await plan(request) : await build(request);
|
|
347
|
+
}
|
|
348
|
+
catch (error) {
|
|
349
|
+
if (error instanceof AgentBuildRunnerError)
|
|
350
|
+
throw error;
|
|
351
|
+
if (error instanceof ProjectDeliveryError) {
|
|
352
|
+
const stage = error.code === 'delivery_isolation_unavailable' ? 'environment_preflight'
|
|
353
|
+
: request.phase === 'plan' ? 'source_snapshot'
|
|
354
|
+
: error.code.startsWith('artifact_') ? 'validating' : 'compiling';
|
|
355
|
+
throw new AgentBuildRunnerError(error.code, stage, error.message, false);
|
|
356
|
+
}
|
|
357
|
+
throw new AgentBuildRunnerError('agent_build_runner_failed', request.phase === 'plan' ? 'source_snapshot' : 'compiling', error instanceof Error ? error.message.slice(0, 2000) : 'Runner failed');
|
|
358
|
+
}
|
|
359
|
+
finally {
|
|
360
|
+
await rm(path.join(request.attemptRoot, `progress.json.${process.pid}.tmp`), { force: true }).catch(() => undefined);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
export { parseBuildPlan } from './build-plan.js';
|
|
364
|
+
export { parseArtifactDescriptor } from './artifact-descriptor.js';
|
|
365
|
+
export { verifyChannelArtifactDescriptor } from './artifact-package.js';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AgentBuildSourceRulesVersion } from '@playcraft/common/cloud-workspace/runtime';
|
|
2
|
+
import type { ProjectDeliveryRecipe } from './types.js';
|
|
3
|
+
export type AgentBuildSourceIdentity = {
|
|
4
|
+
sourceFingerprint: string;
|
|
5
|
+
sourceRulesVersion: AgentBuildSourceRulesVersion;
|
|
6
|
+
};
|
|
7
|
+
/** Shares save/freeze/legacy delivery's exact production closure, never a fallback for invalid declarations. */
|
|
8
|
+
export declare function readAgentBuildSourceIdentity(root: string, recipe: ProjectDeliveryRecipe, legacyOutputDirectory?: string): Promise<AgentBuildSourceIdentity>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { deliveryProductionSourceSnapshot } from './files.js';
|
|
2
|
+
import { deliveryGitSourceFingerprint } from './git-source-fingerprint.js';
|
|
3
|
+
import { ProjectDeliveryError } from './recipe.js';
|
|
4
|
+
/** Shares save/freeze/legacy delivery's exact production closure, never a fallback for invalid declarations. */
|
|
5
|
+
export async function readAgentBuildSourceIdentity(root, recipe, legacyOutputDirectory = recipe.output.directory) {
|
|
6
|
+
const production = await deliveryProductionSourceSnapshot(root, recipe.output.directory, undefined, recipe.generatedDirectories);
|
|
7
|
+
if (production) {
|
|
8
|
+
let sourceRulesVersion;
|
|
9
|
+
switch (production.sourceRulesVersion) {
|
|
10
|
+
case 1:
|
|
11
|
+
sourceRulesVersion = 'production-v1';
|
|
12
|
+
break;
|
|
13
|
+
case 2:
|
|
14
|
+
sourceRulesVersion = 'production-v2';
|
|
15
|
+
break;
|
|
16
|
+
default:
|
|
17
|
+
throw new ProjectDeliveryError('delivery_source_rules_unsupported', 'Unsupported production source rules');
|
|
18
|
+
}
|
|
19
|
+
return { sourceFingerprint: production.fingerprint, sourceRulesVersion };
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
sourceFingerprint: await deliveryGitSourceFingerprint(root, legacyOutputDirectory, undefined, recipe.generatedDirectories),
|
|
23
|
+
sourceRulesVersion: 'legacy-v1',
|
|
24
|
+
};
|
|
25
|
+
}
|