@playcraft/cli 0.1.49-beta → 0.1.50-beta

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.
@@ -0,0 +1,56 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { lstat } from 'node:fs/promises';
3
+ import { join } from 'node:path';
4
+ import { isMutableWorkspaceDirectoryPath } from '@playcraft/common/cloud-workspace/runtime';
5
+ import { listWorkspaceFiles } from './files.js';
6
+ /** Reuse the listing's streamed hashes; only directory and preservation-marker metadata is sampled again. */
7
+ export async function observeWorkspaceDirectories(root, journal, scope, listed) {
8
+ const previous = journal.fileRevisions(scope);
9
+ const result = listed ?? await listWorkspaceFiles(root, previous);
10
+ const directoryRevisions = {};
11
+ const fingerprints = new Map();
12
+ for (const path of result.directories ?? []) {
13
+ if (!isMutableWorkspaceDirectoryPath(path)
14
+ || result.unreadablePaths?.some(item => item === path || item.startsWith(`${path}/`)))
15
+ continue;
16
+ const stat = await lstat(join(root, ...path.split('/')));
17
+ if (!stat.isDirectory() || stat.isSymbolicLink())
18
+ continue;
19
+ const marker = await lstat(join(root, ...path.split('/'), '.gitkeep')).catch(error => {
20
+ if (error.code !== 'ENOENT')
21
+ throw error;
22
+ return undefined;
23
+ });
24
+ fingerprints.set(path, JSON.stringify([path, stat.dev, stat.ino, stat.mtimeMs, stat.ctimeMs,
25
+ marker && [marker.dev, marker.ino, marker.size, marker.mtimeMs, marker.ctimeMs]]));
26
+ }
27
+ for (const [path] of fingerprints) {
28
+ const hash = createHash('sha256').update('directory\0');
29
+ for (const [child, metadata] of fingerprints)
30
+ if (child === path || child.startsWith(`${path}/`))
31
+ hash.update(metadata);
32
+ for (const file of result.files)
33
+ if (file.path.startsWith(`${path}/`))
34
+ hash.update(JSON.stringify([file.path, file.contentHash, file.size]));
35
+ const contentHash = hash.digest('hex');
36
+ const old = previous[path];
37
+ const revision = old ? old.revision + Number(old.contentHash !== contentHash) : 0;
38
+ if (!old || old.contentHash !== contentHash)
39
+ await journal.putFileRevision(path, { revision, contentHash }, scope);
40
+ directoryRevisions[path] = revision;
41
+ }
42
+ for (const file of result.files) {
43
+ const old = previous[file.path];
44
+ const revision = old ? old.revision + Number(old.contentHash !== file.contentHash) : 0;
45
+ if (!old || old.contentHash !== file.contentHash)
46
+ await journal.putFileRevision(file.path, { revision, contentHash: file.contentHash }, scope);
47
+ file.revision = revision;
48
+ }
49
+ // Retain tombstones so recreating the same path cannot satisfy an old revision.
50
+ const present = new Set([...result.files.map(file => file.path), ...(result.directories ?? [])]);
51
+ for (const [path, old] of Object.entries(previous)) {
52
+ if (old.contentHash && !present.has(path) && !result.unreadablePaths?.some(item => path === item || path.startsWith(`${item}/`)))
53
+ await journal.putFileRevision(path, { revision: old.revision + 1, contentHash: '' }, scope);
54
+ }
55
+ return { ...result, directoryRevisions };
56
+ }
@@ -1,12 +1,12 @@
1
1
  import { createHash } from 'node:crypto';
2
2
  import { constants } from 'node:fs';
3
3
  import { open } from 'node:fs/promises';
4
- import { WORKSPACE_VIDEO_ATTACHMENT_MAX_BYTES } from '@playcraft/common/cloud-workspace/runtime';
4
+ import { ASSET_TRANSFER_FILE_MAX_BYTES } from '@playcraft/common/cloud-workspace/runtime';
5
5
  export async function sampleDiskFile(path) {
6
6
  const handle = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW);
7
7
  try {
8
8
  const before = await handle.stat();
9
- if (!before.isFile() || before.size > WORKSPACE_VIDEO_ATTACHMENT_MAX_BYTES)
9
+ if (!before.isFile() || before.size > ASSET_TRANSFER_FILE_MAX_BYTES)
10
10
  throw new Error('snapshot_body_missing');
11
11
  const digest = createHash('sha256');
12
12
  const chunk = Buffer.allocUnsafe(64 * 1024);
@@ -17,7 +17,7 @@ export async function sampleDiskFile(path) {
17
17
  break;
18
18
  digest.update(chunk.subarray(0, bytesRead));
19
19
  position += bytesRead;
20
- if (position > WORKSPACE_VIDEO_ATTACHMENT_MAX_BYTES)
20
+ if (position > ASSET_TRANSFER_FILE_MAX_BYTES)
21
21
  throw new Error('snapshot_body_missing');
22
22
  }
23
23
  const after = await handle.stat();
@@ -7,6 +7,127 @@ export class ParameterEditError extends Error {
7
7
  this.name = 'ParameterEditError';
8
8
  }
9
9
  }
10
+ /** Apply local JSON5 (JSON5 Data Interchange Format,JSON5 数据交换格式) edits without serializing unrelated fields. */
11
+ export function editJson5Paths(source, changes) {
12
+ const parsed = JSON5.parse(source);
13
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
14
+ throw new ParameterEditError('parameter_document_invalid');
15
+ }
16
+ const paths = [];
17
+ for (const change of changes) {
18
+ if (change.fieldPath.length === 0 || change.fieldPath.some((key) => !key || ['__proto__', 'prototype', 'constructor'].includes(key))) {
19
+ throw new ParameterEditError('parameter_value_invalid');
20
+ }
21
+ if (paths.some((other) => other.every((key, index) => key === change.fieldPath[index])
22
+ || change.fieldPath.every((key, index) => key === other[index]))) {
23
+ throw new ParameterEditError('parameter_changes_overlap');
24
+ }
25
+ if (change.op !== 'remove' && typeof change.value === 'number' && !Number.isFinite(change.value)) {
26
+ throw new ParameterEditError('parameter_value_invalid');
27
+ }
28
+ paths.push(change.fieldPath);
29
+ }
30
+ let next = source;
31
+ for (const change of changes)
32
+ next = editJson5Object(next, skipTrivia(next, 0, next.length), change, 0);
33
+ JSON5.parse(next);
34
+ return next;
35
+ }
36
+ function editJson5Object(source, braceIndex, change, depth) {
37
+ const object = readJson5Object(source, braceIndex);
38
+ const memberIndex = object.members.findIndex((member) => member.name === change.fieldPath[depth]);
39
+ const member = object.members[memberIndex];
40
+ if (member && depth < change.fieldPath.length - 1) {
41
+ if (source[member.value.start] !== '{')
42
+ throw new ParameterEditError('parameter_value_invalid');
43
+ return editJson5Object(source, member.value.start, change, depth + 1);
44
+ }
45
+ if (change.op === 'remove') {
46
+ if (!member)
47
+ return source;
48
+ if (source[member.value.start] === '{' || source[member.value.start] === '[') {
49
+ throw new ParameterEditError('parameter_value_invalid');
50
+ }
51
+ const comma = member.comma ?? object.members[memberIndex - 1]?.comma;
52
+ const spans = [{ start: member.keyStart, end: member.value.end },
53
+ ...(comma === undefined ? [] : [{ start: comma, end: comma + 1 }])].sort((left, right) => right.start - left.start);
54
+ let next = source;
55
+ for (const span of spans)
56
+ next = `${next.slice(0, span.start)}${next.slice(span.end)}`;
57
+ return next;
58
+ }
59
+ if (member) {
60
+ if (source[member.value.start] === '{' || source[member.value.start] === '[') {
61
+ throw new ParameterEditError('parameter_value_invalid');
62
+ }
63
+ return `${source.slice(0, member.value.start)}${formatJson5Value(change.value, source.slice(member.value.start, member.value.end))}${source.slice(member.value.end)}`;
64
+ }
65
+ const objectIndent = lineIndent(source, braceIndex);
66
+ const first = object.members[0];
67
+ const firstIndent = first ? lineIndent(source, first.keyStart) : undefined;
68
+ const indentUnit = firstIndent && firstIndent.length > objectIndent.length
69
+ ? firstIndent.slice(objectIndent.length) : ' ';
70
+ const multiline = /[\r\n]/.test(source.slice(braceIndex, object.close));
71
+ const eol = source.includes('\r\n') ? '\r\n' : '\n';
72
+ const render = (index, indent) => {
73
+ const key = formatJson5Key(change.fieldPath[index]);
74
+ if (index === change.fieldPath.length - 1)
75
+ return `${key}: ${formatJson5Value(change.value, '')}`;
76
+ return multiline
77
+ ? `${key}: {${eol}${indent}${indentUnit}${render(index + 1, indent + indentUnit)},${eol}${indent}}`
78
+ : `${key}: { ${render(index + 1, indent + indentUnit)} }`;
79
+ };
80
+ let tailStart = object.close;
81
+ while (tailStart > braceIndex + 1 && /\s/.test(source[tailStart - 1]))
82
+ tailStart -= 1;
83
+ const last = object.members.at(-1);
84
+ const insertion = multiline
85
+ ? `${eol}${objectIndent}${indentUnit}${render(depth, objectIndent + indentUnit)},${eol}${objectIndent}`
86
+ : `${object.members.length > 0 ? ' ' : ''}${render(depth, objectIndent + indentUnit)}${last?.comma === undefined ? '' : ','}${source.slice(tailStart, object.close)}`;
87
+ let next = `${source.slice(0, tailStart)}${insertion}${source.slice(object.close)}`;
88
+ if (last && last.comma === undefined)
89
+ next = `${next.slice(0, last.value.end)},${next.slice(last.value.end)}`;
90
+ return next;
91
+ }
92
+ function readJson5Object(source, braceIndex) {
93
+ if (source[braceIndex] !== '{')
94
+ throw new ParameterEditError('parameter_document_invalid');
95
+ const close = balancedEnd(source, braceIndex, source.length) - 1;
96
+ const members = [];
97
+ let index = skipTrivia(source, braceIndex + 1, close);
98
+ while (index < close) {
99
+ const keyStart = index;
100
+ let keyEnd;
101
+ if (source[index] === '"' || source[index] === "'")
102
+ keyEnd = stringEnd(source, index, close);
103
+ else {
104
+ keyEnd = index;
105
+ while (keyEnd < close && !/[\s:/]/.test(source[keyEnd]))
106
+ keyEnd += 1;
107
+ }
108
+ const key = JSON5.parse(`{${source.slice(index, keyEnd)}: null}`);
109
+ const name = Object.keys(key)[0];
110
+ if (members.some((member) => member.name === name))
111
+ throw new ParameterEditError('parameter_document_invalid');
112
+ index = skipTrivia(source, keyEnd, close);
113
+ if (source[index] !== ':')
114
+ throw new ParameterEditError('parameter_document_invalid');
115
+ const value = scanValue(source, index + 1, close);
116
+ index = skipTrivia(source, value.end, close);
117
+ const comma = source[index] === ',' ? index : undefined;
118
+ members.push({ name, keyStart, value, comma });
119
+ if (comma === undefined && index < close)
120
+ throw new ParameterEditError('parameter_document_invalid');
121
+ index = skipTrivia(source, comma === undefined ? index : index + 1, close);
122
+ }
123
+ return { close, members };
124
+ }
125
+ function lineIndent(source, index) {
126
+ return source.slice(source.lastIndexOf('\n', index - 1) + 1, index).match(/^[\t ]*/)?.[0] ?? '';
127
+ }
128
+ function formatJson5Key(key) {
129
+ return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key) ? key : JSON.stringify(key);
130
+ }
10
131
  export function replaceJson5Path(source, fieldPath, value) {
11
132
  const span = findJson5ValueSpan(source, fieldPath);
12
133
  if (!span)
@@ -17,6 +138,26 @@ export function replaceJson5Path(source, fieldPath, value) {
17
138
  }
18
139
  return `${source.slice(0, span.start)}${formatJson5Value(value, source.slice(span.start, span.end))}${source.slice(span.end)}`;
19
140
  }
141
+ export function replaceJson5Paths(source, changes) {
142
+ const replacements = changes.map(({ fieldPath, value }) => {
143
+ const span = findJson5ValueSpan(source, fieldPath);
144
+ if (!span)
145
+ throw new ParameterEditError('parameter_field_missing');
146
+ const current = skipTrivia(source, span.start, span.end);
147
+ if (source[current] === '{' || source[current] === '[')
148
+ throw new ParameterEditError('parameter_value_invalid');
149
+ return { ...span, value: formatJson5Value(value, source.slice(span.start, span.end)) };
150
+ }).sort((left, right) => right.start - left.start);
151
+ let next = source;
152
+ let previousStart = source.length;
153
+ for (const replacement of replacements) {
154
+ if (replacement.end > previousStart)
155
+ throw new ParameterEditError('parameter_changes_overlap');
156
+ next = `${next.slice(0, replacement.start)}${replacement.value}${next.slice(replacement.end)}`;
157
+ previousStart = replacement.start;
158
+ }
159
+ return next;
160
+ }
20
161
  export function findJson5ValueSpan(source, fieldPath) {
21
162
  const start = skipTrivia(source, 0, source.length);
22
163
  if (source[start] !== '{')
@@ -153,7 +294,9 @@ function formatJson5Value(value, previous) {
153
294
  if (typeof value === 'string') {
154
295
  const trimmed = previous.trim();
155
296
  if (trimmed.startsWith("'")) {
156
- return `'${value.replaceAll('\\', '\\\\').replaceAll("'", "\\'")}'`;
297
+ const escaped = value.replaceAll('\\', '\\\\').replaceAll("'", "\\'")
298
+ .replace(/[\u0000-\u001f\u2028\u2029\ud800-\udfff]/g, (character) => `\\u${character.charCodeAt(0).toString(16).padStart(4, '0')}`);
299
+ return `'${escaped}'`;
157
300
  }
158
301
  return JSON.stringify(value);
159
302
  }
@@ -0,0 +1,87 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { lstat, readdir, readFile, realpath } from 'node:fs/promises';
3
+ import { relative, resolve, sep } from 'node:path';
4
+ import { workspaceRelativePathSchema } from '@playcraft/common/cloud-workspace/runtime';
5
+ export const parameterDigest = (content) => createHash('sha256').update(content).digest('hex');
6
+ const skippedDirectories = new Set(['node_modules', '.git', 'dist', 'build', 'coverage', '.next', '.vite', '.cache', '.playcraft-editor-preview', '.playcraft-delivery', '.playcraft-temp', 'parameter-admission']);
7
+ const inputExtension = /\.(?:[cm]?[jt]sx?|json5?|ya?ml|html|css|scss|glsl|wgsl|vert|frag|toml)$/i;
8
+ export class ParameterAgentError extends Error {
9
+ code;
10
+ constructor(code) {
11
+ super(code);
12
+ this.code = code;
13
+ this.name = 'ParameterAgentError';
14
+ }
15
+ }
16
+ export function parameterInputPath(path) {
17
+ if (!workspaceRelativePathSchema.safeParse(path).success)
18
+ return false;
19
+ const segments = path.split('/');
20
+ if (segments.some(segment => skippedDirectories.has(segment)))
21
+ return false;
22
+ if (segments.some(segment => segment.startsWith('.') && segment !== '.playcraft'))
23
+ return false;
24
+ if (segments[0] === '.playcraft' && path !== '.playcraft/workspace.json')
25
+ return false;
26
+ if (segments.some(segment => /(?:secret|credential|private[-_.]?key|\.pem$|\.key$)/i.test(segment)))
27
+ return false;
28
+ return inputExtension.test(path);
29
+ }
30
+ export async function readParameterAgentFile(root, path, limit = 2 * 1024 * 1024) {
31
+ const parsed = workspaceRelativePathSchema.safeParse(path);
32
+ if (!parsed.success)
33
+ throw new ParameterAgentError('parameter_agent_path_invalid');
34
+ const base = await realpath(root);
35
+ let target = base;
36
+ const segments = parsed.data.split('/');
37
+ for (let index = 0; index < segments.length; index++) {
38
+ target = resolve(target, segments[index]);
39
+ const stat = await lstat(target);
40
+ if (stat.isSymbolicLink() || (index < segments.length - 1 ? !stat.isDirectory() : !stat.isFile()))
41
+ throw new ParameterAgentError('parameter_agent_path_invalid');
42
+ if (index === segments.length - 1 && stat.size > limit)
43
+ throw new ParameterAgentError('parameter_agent_input_too_large');
44
+ }
45
+ const rel = relative(base, await realpath(target));
46
+ if (rel === '..' || rel.startsWith(`..${sep}`))
47
+ throw new ParameterAgentError('parameter_agent_path_invalid');
48
+ const bytes = await readFile(target);
49
+ if (bytes.byteLength > limit)
50
+ throw new ParameterAgentError('parameter_agent_input_too_large');
51
+ return bytes;
52
+ }
53
+ export async function parameterInputSnapshot(root) {
54
+ const base = await realpath(root);
55
+ const sources = [];
56
+ let total = 0;
57
+ let directories = 0;
58
+ const walk = async (path, depth) => {
59
+ if (depth > 24 || ++directories > 4096)
60
+ throw new ParameterAgentError('parameter_agent_input_limit');
61
+ const entries = await readdir(resolve(base, path), { withFileTypes: true });
62
+ if (entries.length > 4096)
63
+ throw new ParameterAgentError('parameter_agent_input_limit');
64
+ for (const entry of entries) {
65
+ const child = path ? `${path}/${entry.name}` : entry.name;
66
+ if (entry.isSymbolicLink())
67
+ continue;
68
+ if (entry.isDirectory()) {
69
+ if (skippedDirectories.has(entry.name) || (entry.name.startsWith('.') && entry.name !== '.playcraft'))
70
+ continue;
71
+ if (child.startsWith('.playcraft/'))
72
+ continue;
73
+ await walk(child, depth + 1);
74
+ }
75
+ else if (entry.isFile() && parameterInputPath(child)) {
76
+ const bytes = await readParameterAgentFile(base, child);
77
+ total += bytes.byteLength;
78
+ if (total > 32 * 1024 * 1024 || sources.length >= 2048)
79
+ throw new ParameterAgentError('parameter_agent_input_limit');
80
+ sources.push({ path: child, sha256: parameterDigest(bytes) });
81
+ }
82
+ }
83
+ };
84
+ await walk('', 0);
85
+ sources.sort((left, right) => left.path.localeCompare(right.path));
86
+ return { sources, digest: parameterDigest(JSON.stringify(sources)) };
87
+ }
@@ -0,0 +1,29 @@
1
+ /** Versioned product protocol reference, compiled into the published CLI (Command Line Interface, 命令行接口). */
2
+ export const PARAMETER_AGENT_PROTOCOL = `PlayCraft parameter integration protocol v1
3
+ This is a product configuration reference, not a skill activation. Preserve the current project skill lock and permission/work modes.
4
+ Work only in the current authorized engineering directory. Never edit Remix Editor or a different workspace directory.
5
+
6
+ Configuration:
7
+ - Reuse existing schema and sparse theme data. .playcraft/workspace.json may declare parameters:{schemaPath,dataPath,themeId}; preserve all unrelated manifest keys. parameters accepts only those three keys.
8
+ - A schema already declares configurable fields. Do not add a parallel xplatform.parameter protocol. Reuse existing field paths and synonymous parameters first.
9
+ - For a new project, create a schema, a sparse data file ({} is valid), matching platform text, and explicit manifest paths. Use gamePlay.preloaded / gamePlay.sections / gamePlay.settlement for the three-screen tree. Preserve existing project paths instead of migrating gratuitously.
10
+ - Object nodes use type:object/properties; leaves declare a supported primitive type and a valid default. Preserve units, known bounds/enum and meaning; do not invent verified limits without code evidence. Basic leaves support string, finite number, integer, boolean and null. Do not add unsupported required/$ref/conditional composition merely for this workflow.
11
+ - Missing theme values inherit schema defaults; editing creates a raw override and restoring defaults removes that override. Never flatten resolved values into theme.data. Preserve comments, unknown data and unrelated overrides.
12
+ - Platform labels use i18n.platform.json5 beside the schema, same tree, with _self.title/description text for zh/en. Preserve xplatform.ext.readonly and xplatform.showIf/showIfAny. Resources (xplatform.value_type:path), multilingual references (i18n), arrays and complex structures retain their semantics; do not disguise them as primitive controls.
13
+
14
+ Consumption and checks:
15
+ - Modify real game consumers and the neutral configuration resolver, shared by production and preview. A schema-only field or a new getter with no real consumer is incomplete.
16
+ - The development preview runtime is separate from advertising code. Do not route debugging through advertising configuration caches. Production must not depend on the preview runtime.
17
+ - Instant capability requires real field registration and a version-matched preview handshake. Reports cannot declare live capability. A newly integrated field may require one preview build first.
18
+ - Add or extend package.json script test:parameters with meaningful tests of schema/default/sparse overrides and real consumer behavior; changing the requested value must affect its consumer. Preserve an existing test script. Ensure the production build script build exists and succeeds. Optional typecheck is also run by the platform.
19
+ - The platform independently runs only pnpm run test:parameters, pnpm run build, and optional pnpm run typecheck. Reports cannot choose executable commands. Do not replace checks with successful echo statements or self-reported flags.
20
+ - Do not claim a complete production module isolation proof without a trustworthy module report. Passing build/checks proves only their stated coverage.
21
+
22
+ Report JSON contract (no second copy of parameter values):
23
+ {version:1,taskId:"<exact platform task id>",sources:[{path:"relative/input.ts",sha256:"<current raw-file SHA-256>"}],candidates:[{id:"stable-id",title:"User-facing title",description:"Optional purpose",fieldPath:["gamePlay","sections","board","speed"],origins:[{path:"relative/input.ts",line:1,summary:"Actual origin evidence"}],consumers:[{path:"src/game.ts",line:1,summary:"Actual production consumer and behavior"}],status:"candidate",missing:["Concrete incomplete steps"]}]}
24
+ - Candidate status is exactly candidate, implemented, or blocked. Completed integration candidates use implemented with an empty missing array; discovery keeps candidates unimplemented.
25
+ - Include current file digests for every origin and consumer, and the actual schema/data/platform labels/manifest/check scripts/package and lock files when present. Paths must stay inside the engineering directory. Do not read credentials, dependency trees or build outputs as evidence.
26
+ - Summaries and source snippets are evidence claims, not proof of semantics. The platform checks current files, declared defaults, write support, and independently executed check exits.
27
+ - No passed/ready/live/checks fields are accepted in the report. All implementation and validation statements remain claims until platform verification.
28
+ - If blocked or partial, preserve reviewable changes and list concrete missing work. Do not overwrite later user edits or claim multi-file changes were atomic.
29
+ `;
@@ -0,0 +1,58 @@
1
+ import { mkdir, readFile, readdir, rename, lstat, writeFile } from 'node:fs/promises';
2
+ import { isAbsolute, join, relative, resolve, sep } from 'node:path';
3
+ import { randomUUID } from 'node:crypto';
4
+ import { parameterAgentTaskRecordSchema, parameterAgentTaskRequestSchema } from '@playcraft/common/cloud-workspace/runtime';
5
+ import { ParameterAgentError, parameterDigest } from './parameter-agent-inputs.js';
6
+ export function parameterTaskWorkspace(state, workspaceId) {
7
+ if (!workspaceId && state.config.workspaceMode === 'multi')
8
+ throw new ParameterAgentError('parameter_agent_scope_invalid');
9
+ return workspaceId ?? 'local';
10
+ }
11
+ async function taskDirectory(state, workspaceId, directoryName, root) {
12
+ const parent = resolve(state.config.stateDir, 'parameter-agent');
13
+ if (root) {
14
+ const rel = relative(resolve(root), parent);
15
+ if (rel === '' || (rel !== '..' && !rel.startsWith(`..${sep}`) && !isAbsolute(rel)))
16
+ throw new ParameterAgentError('parameter_agent_state_not_private');
17
+ }
18
+ const scope = [state.config.environment, state.config.userId, state.config.runtimeId, workspaceId, directoryName];
19
+ const directory = join(parent, parameterDigest(JSON.stringify(scope)));
20
+ await mkdir(directory, { recursive: true, mode: 0o700 });
21
+ if ((await lstat(parent)).isSymbolicLink() || (await lstat(directory)).isSymbolicLink())
22
+ throw new ParameterAgentError('parameter_agent_state_invalid');
23
+ return directory;
24
+ }
25
+ export async function readPrivateParameterTask(state, workspaceId, directoryName, taskId, root) {
26
+ if (!/^[a-f0-9-]{36}$/i.test(taskId))
27
+ throw new ParameterAgentError('parameter_agent_task_invalid');
28
+ const file = join(await taskDirectory(state, workspaceId, directoryName, root), `${taskId}.json`);
29
+ try {
30
+ const stat = await lstat(file);
31
+ if (!stat.isFile() || stat.isSymbolicLink() || stat.size > 1024 * 1024)
32
+ throw new ParameterAgentError('parameter_agent_state_invalid');
33
+ const value = JSON.parse(await readFile(file, 'utf8'));
34
+ value.record = parameterAgentTaskRecordSchema.parse(value.record);
35
+ value.request = parameterAgentTaskRequestSchema.parse(value.request);
36
+ if (value.record.taskId !== taskId || value.request.taskId !== taskId || value.record.workspaceId !== workspaceId || value.record.directoryName !== directoryName || !/^[a-f0-9]{64}$/.test(value.initialDigest))
37
+ throw new ParameterAgentError('parameter_agent_state_invalid');
38
+ return value;
39
+ }
40
+ catch (error) {
41
+ if (error.code === 'ENOENT')
42
+ return undefined;
43
+ throw error;
44
+ }
45
+ }
46
+ export async function writePrivateParameterTask(state, task, root) {
47
+ const directory = await taskDirectory(state, task.record.workspaceId, task.record.directoryName, root);
48
+ const record = parameterAgentTaskRecordSchema.parse(task.record);
49
+ const temporary = join(directory, `.${record.taskId}.${randomUUID()}.tmp`);
50
+ await writeFile(temporary, JSON.stringify({ ...task, record }), { mode: 0o600 });
51
+ await rename(temporary, join(directory, `${record.taskId}.json`));
52
+ }
53
+ export async function listPrivateParameterTaskIds(state, workspaceId, directoryName, root) {
54
+ const names = (await readdir(await taskDirectory(state, workspaceId, directoryName, root))).filter(name => /^[a-f0-9-]{36}\.json$/i.test(name));
55
+ if (names.length > 256)
56
+ throw new ParameterAgentError('parameter_agent_task_limit');
57
+ return names.map(name => name.slice(0, -5));
58
+ }