@craft_eco/craft-mcp 0.1.29 → 0.1.30

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.
Files changed (152) hide show
  1. package/README.md +10 -55
  2. package/bin/craft-mcp.mjs +4 -1
  3. package/dist-server/game-workflow/check.mjs +3 -2
  4. package/dist-server/game-workflow/context-state.mjs +67 -67
  5. package/dist-server/game-workflow/draft-archive.mjs +147 -2
  6. package/dist-server/game-workflow/draft-upload-identity.mjs +11 -0
  7. package/dist-server/game-workflow/draft-uploads.mjs +105 -60
  8. package/dist-server/game-workflow/local-context.mjs +62 -62
  9. package/dist-server/game-workflow/native-context.mjs +289 -289
  10. package/dist-server/game-workflow/project-bundles.mjs +246 -246
  11. package/dist-server/game-workflow/project-context.mjs +8 -8
  12. package/dist-server/game-workflow/projects.mjs +6 -4
  13. package/dist-server/game-workflow/tasks.mjs +1 -1
  14. package/dist-server/game-workflow/work-scope.mjs +7 -7
  15. package/dist-server/integrations/config.mjs +145 -145
  16. package/dist-server/integrations/connect.mjs +36 -0
  17. package/dist-server/integrations/connection-handoff.mjs +36 -42
  18. package/dist-server/integrations/dsh-config.mjs +182 -182
  19. package/dist-server/integrations/files.mjs +54 -54
  20. package/dist-server/integrations/fixed-launcher.mjs +31 -0
  21. package/dist-server/integrations/host-reload.mjs +19 -0
  22. package/dist-server/integrations/hosts.mjs +55 -55
  23. package/dist-server/integrations/install.mjs +223 -205
  24. package/dist-server/integrations/installation-state.mjs +84 -84
  25. package/dist-server/integrations/launcher-start.mjs +52 -0
  26. package/dist-server/integrations/npm-diagnostics.mjs +32 -0
  27. package/dist-server/integrations/runtime-gc.mjs +191 -188
  28. package/dist-server/integrations/runtime-identity.mjs +22 -0
  29. package/dist-server/integrations/runtime.mjs +111 -126
  30. package/dist-server/integrations/updates.mjs +258 -225
  31. package/dist-server/integrations/version-policy.mjs +32 -32
  32. package/dist-server/launcher.mjs +1845 -0
  33. package/dist-server/mcp/control-panel.mjs +8 -23
  34. package/dist-server/mcp/server.mjs +31 -80
  35. package/dist-server/mcp/update-session.mjs +107 -92
  36. package/dist-server/upstream/craft-aigc.mjs +43 -16
  37. package/dist-server/upstream/draft-source.mjs +29 -0
  38. package/package.json +3 -2
  39. package/resources/context-filter-report.json +1 -1
  40. package/resources/craft-create/SKILL.md +19 -19
  41. package/resources/craft-update/SKILL.md +9 -11
  42. package/resources/targets/claude/js/bootstrap/SKILL.md +3 -1
  43. package/resources/targets/claude/js/context-filter-report.json +1 -1
  44. package/resources/targets/claude/js/craft-game-maker/SKILL.md +11 -15
  45. package/resources/targets/claude/js/js/bundle.json +7 -7
  46. package/resources/targets/claude/js/recovery.md +5 -1
  47. package/resources/targets/claude/js/target.json +1 -1
  48. package/resources/targets/claude/js3d/bootstrap/SKILL.md +3 -1
  49. package/resources/targets/claude/js3d/context-filter-report.json +1 -1
  50. package/resources/targets/claude/js3d/craft-game-maker/SKILL.md +11 -15
  51. package/resources/targets/claude/js3d/js3d/bundle.json +7 -7
  52. package/resources/targets/claude/js3d/recovery.md +5 -1
  53. package/resources/targets/claude/js3d/target.json +1 -1
  54. package/resources/targets/codebuddy/js/bootstrap/SKILL.md +3 -1
  55. package/resources/targets/codebuddy/js/context-filter-report.json +1 -1
  56. package/resources/targets/codebuddy/js/craft-game-maker/SKILL.md +11 -15
  57. package/resources/targets/codebuddy/js/js/bundle.json +7 -7
  58. package/resources/targets/codebuddy/js/recovery.md +5 -1
  59. package/resources/targets/codebuddy/js/target.json +1 -1
  60. package/resources/targets/codebuddy/js3d/bootstrap/SKILL.md +3 -1
  61. package/resources/targets/codebuddy/js3d/context-filter-report.json +1 -1
  62. package/resources/targets/codebuddy/js3d/craft-game-maker/SKILL.md +11 -15
  63. package/resources/targets/codebuddy/js3d/js3d/bundle.json +7 -7
  64. package/resources/targets/codebuddy/js3d/recovery.md +5 -1
  65. package/resources/targets/codebuddy/js3d/target.json +1 -1
  66. package/resources/targets/codex/js/bootstrap/SKILL.md +2 -4
  67. package/resources/targets/codex/js/context-filter-report.json +1 -1
  68. package/resources/targets/codex/js/craft-game-maker/SKILL.md +11 -15
  69. package/resources/targets/codex/js/js/bundle.json +7 -7
  70. package/resources/targets/codex/js/recovery.md +3 -3
  71. package/resources/targets/codex/js/target.json +1 -1
  72. package/resources/targets/codex/js3d/bootstrap/SKILL.md +2 -4
  73. package/resources/targets/codex/js3d/context-filter-report.json +1 -1
  74. package/resources/targets/codex/js3d/craft-game-maker/SKILL.md +11 -15
  75. package/resources/targets/codex/js3d/js3d/bundle.json +7 -7
  76. package/resources/targets/codex/js3d/recovery.md +3 -3
  77. package/resources/targets/codex/js3d/target.json +1 -1
  78. package/resources/targets/cursor/js/bootstrap/SKILL.md +3 -1
  79. package/resources/targets/cursor/js/context-filter-report.json +1 -1
  80. package/resources/targets/cursor/js/craft-game-maker/SKILL.md +11 -15
  81. package/resources/targets/cursor/js/js/bundle.json +7 -7
  82. package/resources/targets/cursor/js/recovery.md +5 -1
  83. package/resources/targets/cursor/js/target.json +1 -1
  84. package/resources/targets/cursor/js3d/bootstrap/SKILL.md +3 -1
  85. package/resources/targets/cursor/js3d/context-filter-report.json +1 -1
  86. package/resources/targets/cursor/js3d/craft-game-maker/SKILL.md +11 -15
  87. package/resources/targets/cursor/js3d/js3d/bundle.json +7 -7
  88. package/resources/targets/cursor/js3d/recovery.md +5 -1
  89. package/resources/targets/cursor/js3d/target.json +1 -1
  90. package/resources/targets/dsh/js/bootstrap/SKILL.md +3 -1
  91. package/resources/targets/dsh/js/context-filter-report.json +1 -1
  92. package/resources/targets/dsh/js/craft-game-maker/SKILL.md +11 -15
  93. package/resources/targets/dsh/js/js/bundle.json +7 -7
  94. package/resources/targets/dsh/js/recovery.md +5 -1
  95. package/resources/targets/dsh/js/target.json +1 -1
  96. package/resources/targets/dsh/js3d/bootstrap/SKILL.md +3 -1
  97. package/resources/targets/dsh/js3d/context-filter-report.json +1 -1
  98. package/resources/targets/dsh/js3d/craft-game-maker/SKILL.md +11 -15
  99. package/resources/targets/dsh/js3d/js3d/bundle.json +7 -7
  100. package/resources/targets/dsh/js3d/recovery.md +5 -1
  101. package/resources/targets/dsh/js3d/target.json +1 -1
  102. package/resources/targets/index.json +32 -32
  103. package/resources/targets/kimi/js/bootstrap/SKILL.md +3 -1
  104. package/resources/targets/kimi/js/context-filter-report.json +1 -1
  105. package/resources/targets/kimi/js/craft-game-maker/SKILL.md +11 -15
  106. package/resources/targets/kimi/js/js/bundle.json +7 -7
  107. package/resources/targets/kimi/js/recovery.md +5 -1
  108. package/resources/targets/kimi/js/target.json +1 -1
  109. package/resources/targets/kimi/js3d/bootstrap/SKILL.md +3 -1
  110. package/resources/targets/kimi/js3d/context-filter-report.json +1 -1
  111. package/resources/targets/kimi/js3d/craft-game-maker/SKILL.md +11 -15
  112. package/resources/targets/kimi/js3d/js3d/bundle.json +7 -7
  113. package/resources/targets/kimi/js3d/recovery.md +5 -1
  114. package/resources/targets/kimi/js3d/target.json +1 -1
  115. package/resources/targets/workbuddy/js/bootstrap/SKILL.md +22 -24
  116. package/resources/targets/workbuddy/js/context-filter-report.json +1 -1
  117. package/resources/targets/workbuddy/js/craft-game-maker/SKILL.md +11 -15
  118. package/resources/targets/workbuddy/js/js/bundle.json +8 -8
  119. package/resources/targets/workbuddy/js/js/context/AGENTS.md +29 -29
  120. package/resources/targets/workbuddy/js/recovery.md +19 -19
  121. package/resources/targets/workbuddy/js/target.json +1 -1
  122. package/resources/targets/workbuddy/js3d/bootstrap/SKILL.md +22 -24
  123. package/resources/targets/workbuddy/js3d/context-filter-report.json +1 -1
  124. package/resources/targets/workbuddy/js3d/craft-game-maker/SKILL.md +11 -15
  125. package/resources/targets/workbuddy/js3d/js3d/bundle.json +8 -8
  126. package/resources/targets/workbuddy/js3d/js3d/context/AGENTS.md +30 -30
  127. package/resources/targets/workbuddy/js3d/recovery.md +19 -19
  128. package/resources/targets/workbuddy/js3d/target.json +1 -1
  129. package/resources/targets/zcode/js/bootstrap/SKILL.md +3 -1
  130. package/resources/targets/zcode/js/context-filter-report.json +1 -1
  131. package/resources/targets/zcode/js/craft-game-maker/SKILL.md +11 -15
  132. package/resources/targets/zcode/js/js/bundle.json +7 -7
  133. package/resources/targets/zcode/js/recovery.md +5 -1
  134. package/resources/targets/zcode/js/target.json +1 -1
  135. package/resources/targets/zcode/js3d/bootstrap/SKILL.md +3 -1
  136. package/resources/targets/zcode/js3d/context-filter-report.json +1 -1
  137. package/resources/targets/zcode/js3d/craft-game-maker/SKILL.md +11 -15
  138. package/resources/targets/zcode/js3d/js3d/bundle.json +7 -7
  139. package/resources/targets/zcode/js3d/recovery.md +5 -1
  140. package/resources/targets/zcode/js3d/target.json +1 -1
  141. package/vendor/craft-runtime/SOURCE.json +4 -4
  142. package/vendor/craft-runtime/package.json +1 -1
  143. package/dist-server/game-workflow/publication-materials.mjs +0 -92
  144. package/dist-server/game-workflow/publications.mjs +0 -168
  145. package/dist-server/game-workflow/publish-capture-worker.mjs +0 -57
  146. package/dist-server/game-workflow/publish-capture.mjs +0 -75
  147. package/dist-server/mcp/publication-page.html +0 -86
  148. package/dist-server/mcp/publication-page.mjs +0 -375
  149. package/dist-server/mcp/publication-preview.mjs +0 -63
  150. package/dist-server/mcp/publication-tools.mjs +0 -22
  151. package/dist-server/mcp/publish-panel.mjs +0 -210
  152. package/dist-server/upstream/publication-transport.mjs +0 -101
@@ -1,28 +1,77 @@
1
+ import { checkedDraftArchive, checkedDraftSource } from './draft-archive.mjs';
1
2
  import { guardWork } from './work-scope.mjs';
2
- import { checkedDraftArchive } from './draft-archive.mjs';
3
3
  import path from 'node:path';
4
4
  import { DraftBindings } from './draft-bindings.mjs';
5
+ import { draftUploadIdentity } from './draft-upload-identity.mjs';
5
6
  import { digest } from './request-store.mjs';
7
+ import { assertSourceUploadLimit, validSourceMetadata } from '../upstream/draft-source.mjs';
6
8
 
7
9
  function prepareInput(record) {
8
10
  const input = record.prepare_input ?? { client_request_id:record.request_id, engine:record.engine,
9
11
  title:record.name.slice(0,40), bytes:record.bytes, sha256:record.sha256 };
10
12
  if (input.client_request_id !== record.request_id || input.engine !== record.engine ||
11
13
  input.bytes !== record.bytes || input.sha256 !== record.sha256 ||
14
+ (input.source !== undefined && !validSourceMetadata(input.source)) ||
12
15
  Boolean(input.conversation_id) !== Boolean(input.expected_base_version_id)) throw new Error('Invalid saved draft prepare input');
13
16
  return input;
14
17
  }
15
18
 
16
- const terminal = record => ['draft_ready','expired'].includes(record.state);
19
+ const terminal = record => ['draft_ready','expired','superseded'].includes(record.state);
20
+
21
+ function bindingBase(record) {
22
+ const input = prepareInput(record);
23
+ return input.upload_mode === 'new_project' ? record.previous_binding ?? {} :
24
+ { conversation_id:input.conversation_id,version_id:input.expected_base_version_id };
25
+ }
26
+
27
+ function assertFrozenInputs(record, archive, source) {
28
+ if (!record) return;
29
+ const input = prepareInput(record);
30
+ if (input.bytes !== archive.bytes || input.sha256 !== archive.sha256) throw new Error('Frozen build differs from the original draft upload');
31
+ if (!input.source || input.source.format !== source.format || input.source.bytes !== source.bytes || input.source.sha256 !== source.sha256) {
32
+ throw new Error('Frozen project source differs from the original draft upload. Run craft_game_check again');
33
+ }
34
+ }
35
+
36
+ function mergeReceipt(record, result) {
37
+ if (record.upload_id !== undefined && record.upload_id !== result.upload_id) throw new Error('Craft changed the upload ID for this request');
38
+ if (record.prepare_input?.source && result.state !== 'expired') {
39
+ if (typeof result.dist_ready !== 'boolean' || typeof result.source_ready !== 'boolean' ||
40
+ (['uploaded','draft_ready'].includes(result.state) !== (result.dist_ready && result.source_ready)) ||
41
+ (record.dist_ready && !result.dist_ready) || (record.source_ready && !result.source_ready)) {
42
+ throw new Error('Craft did not confirm compatible build and source upload status');
43
+ }
44
+ }
45
+ if (result.state === 'draft_ready') {
46
+ const project = record.conversation_id ?? record.prepare_input?.conversation_id;
47
+ if (!result.conversation_id || !result.version_id || (project && project !== result.conversation_id) ||
48
+ (record.version_id && record.version_id !== result.version_id)) throw new Error('Craft returned a different draft project or version');
49
+ if (record.version_number && result.version_number !== record.version_number) throw new Error('Craft changed the committed version number');
50
+ }
51
+ if (record.prepare_input?.upload_mode && result.state !== 'expired') {
52
+ if (result.upload_mode !== record.prepare_input.upload_mode) throw new Error('Craft 后端尚未支持本次上传模式,请先部署新版后端');
53
+ if (result.state === 'draft_ready' && (!Number.isSafeInteger(result.version_number) || result.version_number < 1)) throw new Error('Craft 未返回有效的上传版本号,请查询上传状态后重试');
54
+ }
55
+ return { ...record, ...result };
56
+ }
17
57
 
18
58
  export function draftUploadView(record) {
19
- return { state:record.state, check_task_id:record.request_id, request_id:record.request_id,
59
+ const ready = record.state === 'draft_ready';
60
+ const sourceReady = record.source_ready === true;
61
+ const distReady = record.dist_ready === true || ready;
62
+ const projectUrl = ready && sourceReady && record.platform && /^[a-zA-Z0-9_-]{1,128}$/.test(record.conversation_id ?? '')
63
+ ? new URL(`/chat/${record.conversation_id}`,record.platform).href : undefined;
64
+ return { state:record.state, check_task_id:record.check_task_id ?? record.request_id, request_id:record.request_id,
65
+ upload_mode:record.upload_mode ?? record.prepare_input?.upload_mode, request_key:record.request_key,
66
+ version_number:record.version_number,
20
67
  upload_id:record.upload_id, conversation_id:record.conversation_id, version_id:record.version_id,
21
- published:false,
68
+ dist_ready:distReady, source_ready:sourceReady, published:false,
69
+ ...(projectUrl ? { project_url:projectUrl } : {}),
22
70
  ...(record.external_manifest_sha256 ? { external_manifest_sha256:record.external_manifest_sha256 } : {}),
23
- next:record.state === 'draft_ready' ? 'Private draft upload is complete and is not published. Use craft_game_deliver for the checked build and the IDE terminal to start its local preview.' :
24
- record.state === 'expired' ? 'Run a new game check and upload that result to start a new draft.' :
25
- 'Run craft_game_upload with the same check_task_id to resume this upload.' };
71
+ next:projectUrl ? `Tell the user: uploaded ${record.version_number ? 'version v' + record.version_number : 'version ' + record.version_id}, project ${record.conversation_id}: ${projectUrl}. This is an upload, not a public release; preview and publish in Craft Web.` : ready
72
+ ? 'This previous upload contains the build only. Run a new game check and upload the source and build to open it in Craft Web.'
73
+ : terminal(record) ? 'Run a new game check and upload that result to start a new draft.'
74
+ : 'Run craft_game_upload with the same check_task_id, upload_mode and request_key to resume this upload.' };
26
75
  }
27
76
 
28
77
  export class DraftUploads {
@@ -47,35 +96,10 @@ export class DraftUploads {
47
96
  return record;
48
97
  }
49
98
 
50
- async hasRequest(name, id, owner) { return Boolean(await this.#record(name,id,owner)); }
51
-
52
- async publicationTarget(check, owner) {
53
- const { name,task_id:id,result:{ engine } } = check;
54
- const record = await this.#record(name,id,owner);
55
- if (record) {
56
- if (record.engine !== engine) throw new Error('Draft upload engine changed');
57
- return record.conversation_id ?? record.prepare_input?.conversation_id;
58
- }
59
- const scope = this.#scope(name,engine,owner);
60
- const binding = await this.bindings.read(scope);
61
- if (binding?.conversation_id) return binding.conversation_id;
62
- const latest = binding ? binding.pending ?? binding.migration?.[0] : (await this.#history(scope))[0];
63
- return latest?.conversation_id ?? latest?.prepare_input?.conversation_id;
64
- }
65
-
66
- async publicationSource(name, id, owner, signal) {
67
- const record = await this.#record(name,id,owner);
68
- if (!record) throw new Error('Upload this checked build before preparing publication');
69
- return { ...await this.status(name,id,owner,signal),archive_sha256:record.sha256,engine:record.engine };
70
- }
99
+ async hasRequest(name, checkId, owner, options) { return Boolean(await this.#record(name,draftUploadIdentity(checkId,options).id,owner)); }
71
100
 
72
101
  async #accept(record, result) {
73
- if (record.upload_id !== undefined && record.upload_id !== result.upload_id) throw new Error('Craft changed the upload ID for this request');
74
- if (result.state === 'draft_ready' && (!result.conversation_id || !result.version_id ||
75
- (record.prepare_input?.conversation_id && record.prepare_input.conversation_id !== result.conversation_id))) {
76
- throw new Error('Craft returned a different draft project');
77
- }
78
- const updated = { ...record, ...result };
102
+ const updated = mergeReceipt(record,result);
79
103
  await this.store.save(updated);
80
104
  return updated;
81
105
  }
@@ -95,9 +119,9 @@ export class DraftUploads {
95
119
  async #finish(scope, binding, record) {
96
120
  if (binding.pending?.request_id !== record.request_id) throw new Error('Draft binding pending request changed');
97
121
  if (record.state === 'draft_ready') {
98
- const input = prepareInput(record);
99
- if (!binding.migration && (input.conversation_id !== binding.conversation_id ||
100
- input.expected_base_version_id !== binding.version_id)) throw new Error('Draft project base version changed');
122
+ const base = bindingBase(record);
123
+ if (!binding.migration && (base.conversation_id !== binding.conversation_id ||
124
+ base.version_id !== binding.version_id)) throw new Error('Draft project base version changed');
101
125
  binding.conversation_id = record.conversation_id;
102
126
  binding.version_id = record.version_id;
103
127
  binding.last_request_id = record.request_id;
@@ -109,11 +133,13 @@ export class DraftUploads {
109
133
 
110
134
  async #recover(record, request, signal) {
111
135
  if (terminal(record)) return record;
136
+ if (!record.prepare_input?.source && !record.upload_id) return record;
112
137
  signal?.throwIfAborted();
138
+ if (!record.upload_id) await guardWork();
113
139
  record = await this.#accept(record,await (record.upload_id
114
140
  ? request('status',{ id:record.upload_id },signal)
115
141
  : request('prepare',{ input:prepareInput(record) },signal)));
116
- if (record.state === 'uploaded') {
142
+ if (record.prepare_input?.source && record.state === 'uploaded') {
117
143
  signal?.throwIfAborted();
118
144
  await guardWork();
119
145
  record = await this.#accept(record,await request('commit',{ id:record.upload_id },signal));
@@ -123,7 +149,7 @@ export class DraftUploads {
123
149
 
124
150
  async #history(scope) {
125
151
  const history = (await this.store.list()).filter(record=>
126
- Object.entries(scope).every(([key,value])=>record[key] === value) && record.state !== 'expired');
152
+ Object.entries(scope).every(([key,value])=>record[key] === value) && !['expired','superseded'].includes(record.state));
127
153
  if (history.some(record=>!Number.isFinite(Date.parse(record.created_at)))) throw new Error('Invalid draft upload history timestamp');
128
154
  history.sort((a,b)=>Date.parse(b.created_at)-Date.parse(a.created_at));
129
155
  if (history.some((record,index)=>index && Date.parse(record.created_at) === Date.parse(history[index-1].created_at))) {
@@ -157,14 +183,19 @@ export class DraftUploads {
157
183
  }
158
184
  const record = await this.#pending(scope,binding);
159
185
  if (record.request_id === id) return binding;
160
- const recovered = await this.#recover(record,request,signal);
161
- if (!terminal(recovered)) throw new Error(`Previous draft upload ${record.request_id} is unfinished; resume craft_game_upload with that check_task_id before uploading a new check`);
186
+ let recovered = await this.#recover(record,request,signal);
187
+ if (!terminal(recovered) && !recovered.prepare_input?.source) {
188
+ recovered = { ...recovered,state:'superseded' };
189
+ await this.store.save(recovered);
190
+ }
191
+ if (!terminal(recovered)) throw new Error(`Previous draft upload is unfinished; resume craft_game_upload with check_task_id=${record.check_task_id ?? record.request_id}, upload_mode=${record.upload_mode ?? 'current_project'}${record.request_key ? ', request_key=' + record.request_key : ''} before starting another upload`);
162
192
  binding = await this.#finish(scope,binding,recovered);
163
193
  }
164
194
  }
165
195
 
166
- async upload(check, owner, signal) {
167
- const { name, task_id:id, result } = check;
196
+ async upload(check, owner, signal, options) {
197
+ const { name, task_id:checkId, result } = check;
198
+ const { id, upload_mode, request_key } = draftUploadIdentity(checkId,options);
168
199
  const request = this.platform.draftSession(owner);
169
200
  let record = await this.#record(name,id,owner);
170
201
  if (record && record.engine !== result.engine) throw new Error('Draft upload engine changed');
@@ -174,26 +205,34 @@ export class DraftUploads {
174
205
  if (binding?.pending?.request_id === id) await this.#finish(scope,binding,record);
175
206
  return draftUploadView(record);
176
207
  }
208
+ if (record && !record.prepare_input?.source) throw new Error('This upload predates project source uploads. Run a new craft_game_check');
209
+ const source = await checkedDraftSource(this.evidence,result.snapshot_id,result.source_snapshot,signal);
177
210
  const archive = await checkedDraftArchive(this.evidence,result.snapshot_id,result.artifacts,signal);
178
- if (record && (record.sha256 !== archive.sha256 || record.bytes !== archive.bytes)) {
179
- throw new Error('Frozen build differs from the original draft upload');
180
- }
211
+ assertFrozenInputs(record,archive,result.source_snapshot);
212
+ const limits = await this.platform.uploadLimits(owner,signal);
213
+ assertSourceUploadLimit(result.source_snapshot.bytes,limits.source_max_bytes);
181
214
  let binding = await this.#binding(scope,id,request,signal);
182
215
  record = binding.pending ? await this.#pending(scope,binding) : record;
183
- if (record && (record.sha256 !== archive.sha256 || record.bytes !== archive.bytes)) {
184
- throw new Error('Frozen build differs from the original draft upload');
185
- }
216
+ assertFrozenInputs(record,archive,result.source_snapshot);
186
217
  if (record && terminal(record)) {
187
218
  await this.#finish(scope,binding,record);
188
219
  return draftUploadView(record);
189
220
  }
190
221
  const input = record ? prepareInput(record) : { client_request_id:id, engine:result.engine, title:name.slice(0,40),
191
- bytes:archive.bytes, sha256:archive.sha256, ...(binding.conversation_id
222
+ upload_mode, bytes:archive.bytes, sha256:archive.sha256, source:result.source_snapshot, ...(upload_mode === 'current_project' && binding.conversation_id
192
223
  ? { conversation_id:binding.conversation_id,expected_base_version_id:binding.version_id } : {}) };
193
- if (!binding.pending && (input.conversation_id !== binding.conversation_id || input.expected_base_version_id !== binding.version_id)) {
224
+ const base = record ? bindingBase(record) : {conversation_id:binding.conversation_id,version_id:binding.version_id};
225
+ if (!binding.pending && (base.conversation_id !== binding.conversation_id || base.version_id !== binding.version_id)) {
226
+ // Another host may have recovered this request and advanced the binding.
227
+ // Read its receipt without making the old request current again.
228
+ if (record.upload_id) {
229
+ record = await this.#accept(record,await request('status',{id:record.upload_id},signal));
230
+ if (terminal(record)) return draftUploadView(record);
231
+ }
194
232
  throw new Error('Saved draft request targets an older project version; run a new game check');
195
233
  }
196
- record ??= await this.store.create({ request_id:id, ...scope, bytes:input.bytes, sha256:input.sha256,
234
+ record ??= await this.store.create({ request_id:id, check_task_id:checkId, upload_mode, request_key,
235
+ previous_binding:{ conversation_id:binding.conversation_id,version_id:binding.version_id }, ...scope, bytes:input.bytes, sha256:input.sha256,
197
236
  state:'preparing',prepare_input:input });
198
237
  record.prepare_input ??= input;
199
238
  // Save immutable target/base before any remote request, then make it recoverable across profiles.
@@ -205,24 +244,30 @@ export class DraftUploads {
205
244
  await guardWork();
206
245
  record = await this.#accept(record,await request(kind,options,signal));
207
246
  };
208
- // The same check ID and archive digest are replayed even when a response was lost.
247
+ // Reconcile each frozen artifact before sending it again after a lost response.
209
248
  await run('prepare',{ input });
210
- if (record.state === 'prepared') {
249
+ if (record.state === 'prepared' && !record.dist_ready) {
211
250
  await run('upload',{ id:record.upload_id, archive });
212
- if (!['uploaded','draft_ready'].includes(record.state)) throw new Error('Craft did not confirm the draft archive upload');
251
+ if (!record.dist_ready && !terminal(record)) throw new Error('Craft did not confirm the draft archive upload');
252
+ }
253
+ if (record.state === 'prepared' && !record.source_ready) {
254
+ await run('upload-source',{ id:record.upload_id, source, sourceFormat:input.source.format });
255
+ if (!record.source_ready && !terminal(record)) throw new Error('Craft did not confirm the project source upload');
213
256
  }
214
257
  if (record.state === 'uploaded') {
215
258
  await run('commit',{ id:record.upload_id });
216
259
  if (record.state !== 'draft_ready') throw new Error('Craft did not finish the draft version');
217
260
  }
218
- if (terminal(record)) binding = await this.#finish(scope,binding,record);
261
+ if (terminal(record)) await this.#finish(scope,binding,record);
219
262
  return draftUploadView(record);
220
263
  }
221
264
 
222
- async status(name, id, owner, signal) {
265
+ async status(name, checkId, owner, signal, options) {
266
+ const { id } = draftUploadIdentity(checkId,options);
223
267
  const record = await this.#record(name,id,owner);
224
- if (!record) return { state:'not_started', check_task_id:id, published:false };
225
- const remote = record.upload_id ? await this.platform.draftSession(owner)('status',{ id:record.upload_id },signal) : undefined;
226
- return draftUploadView({ ...record, ...remote });
268
+ if (!record) return { state:'not_started', check_task_id:checkId, dist_ready:false, source_ready:false, published:false };
269
+ const remote = record.upload_id && record.state !== 'superseded'
270
+ ? await this.platform.draftSession(owner)('status',{ id:record.upload_id },signal) : undefined;
271
+ return draftUploadView(remote ? mergeReceipt(record,remote) : record);
227
272
  }
228
273
  }
@@ -1,62 +1,62 @@
1
- import path from 'node:path';
2
- import { mkdir, mkdtemp, readFile, readdir, writeFile, lstat, rename, rm } from 'node:fs/promises';
3
- import { randomUUID } from 'node:crypto';
4
- import { safeProjectFile } from './asset-import.mjs';
5
- import { verifyBundle } from './bundle-files.mjs';
6
- import { loadEngineResource } from './engines.mjs';
7
-
8
- export const localContextPath = (host,engine,id)=>`.craft/contexts/${host}/${engine}/${id}`;
9
-
10
- // All parent components are checked using the same project boundary as game files.
11
- async function directory(project, relative) {
12
- const marker = await safeProjectFile(project,relative+'/.directory-check',true);
13
- return path.dirname(marker);
14
- }
15
- async function noLinks(root) {
16
- for (const entry of await readdir(root,{withFileTypes:true})) {
17
- if (entry.isSymbolicLink() || (!entry.isDirectory() && !entry.isFile())) throw new Error('Project context contains a link or special file');
18
- if (entry.isDirectory()) await noLinks(path.join(root,entry.name));
19
- }
20
- }
21
-
22
- // Immutable version directories keep an in-flight operation on the exact files it selected.
23
- // Only the sync record switches after the new snapshot is complete and verified.
24
- export async function materializeContext(project, current, previous) {
25
- const {host,engine,bundle,resources} = current;
26
- const relative = localContextPath(host,engine,bundle.id);
27
- const parent = await directory(project,`.craft/contexts/${host}/${engine}`);
28
- const target = path.join(parent,bundle.id);
29
- let present = false, backup;
30
- try {
31
- const stat = await lstat(target);
32
- if (!stat.isDirectory() || stat.isSymbolicLink()) throw new Error('Project context target must be a regular directory');
33
- await noLinks(target);
34
- present = true;
35
- } catch (error) { if (error.code !== 'ENOENT') throw error; }
36
- if (present) {
37
- try {
38
- const selected = await loadEngineResource(target,engine,bundle.id,host,{contextOnly:true});
39
- return {selected,relative,materialized:false};
40
- } catch (error) {
41
- // Do not claim an arbitrary pre-existing directory as an owned context.
42
- if (previous?.path !== relative) throw new Error('Unmanaged or incomplete project context at '+relative+'; preserve it before retrying', {cause:error});
43
- }
44
- }
45
- const staging = await mkdtemp(path.join(parent,'.staging-'));
46
- try {
47
- const files = [...Object.keys(bundle.files).filter(name=>!name.startsWith(engine+'/project/')),engine+'/bundle.json'];
48
- for (const name of files) {
49
- const output = await safeProjectFile(staging,name,true);
50
- await writeFile(output,await readFile(path.join(resources,name)),{flag:'wx'});
51
- }
52
- await verifyBundle(staging,engine,bundle.id,{contextOnly:true});
53
- if (present) {
54
- const backups = await directory(project,`.craft/context-backups/${host}/${engine}`);
55
- backup = path.join(backups,bundle.id+'-'+randomUUID());
56
- await rename(target,backup);
57
- }
58
- await rename(staging,target);
59
- const selected = await loadEngineResource(target,engine,bundle.id,host,{contextOnly:true});
60
- return {selected,relative,materialized:true,...(backup ? {backup} : {})};
61
- } finally { await rm(staging,{recursive:true,force:true}); }
62
- }
1
+ import path from 'node:path';
2
+ import { mkdir, mkdtemp, readFile, readdir, writeFile, lstat, rename, rm } from 'node:fs/promises';
3
+ import { randomUUID } from 'node:crypto';
4
+ import { safeProjectFile } from './asset-import.mjs';
5
+ import { verifyBundle } from './bundle-files.mjs';
6
+ import { loadEngineResource } from './engines.mjs';
7
+
8
+ export const localContextPath = (host,engine,id)=>`.craft/contexts/${host}/${engine}/${id}`;
9
+
10
+ // All parent components are checked using the same project boundary as game files.
11
+ async function directory(project, relative) {
12
+ const marker = await safeProjectFile(project,relative+'/.directory-check',true);
13
+ return path.dirname(marker);
14
+ }
15
+ async function noLinks(root) {
16
+ for (const entry of await readdir(root,{withFileTypes:true})) {
17
+ if (entry.isSymbolicLink() || (!entry.isDirectory() && !entry.isFile())) throw new Error('Project context contains a link or special file');
18
+ if (entry.isDirectory()) await noLinks(path.join(root,entry.name));
19
+ }
20
+ }
21
+
22
+ // Immutable version directories keep an in-flight operation on the exact files it selected.
23
+ // Only the sync record switches after the new snapshot is complete and verified.
24
+ export async function materializeContext(project, current, previous) {
25
+ const {host,engine,bundle,resources} = current;
26
+ const relative = localContextPath(host,engine,bundle.id);
27
+ const parent = await directory(project,`.craft/contexts/${host}/${engine}`);
28
+ const target = path.join(parent,bundle.id);
29
+ let present = false, backup;
30
+ try {
31
+ const stat = await lstat(target);
32
+ if (!stat.isDirectory() || stat.isSymbolicLink()) throw new Error('Project context target must be a regular directory');
33
+ await noLinks(target);
34
+ present = true;
35
+ } catch (error) { if (error.code !== 'ENOENT') throw error; }
36
+ if (present) {
37
+ try {
38
+ const selected = await loadEngineResource(target,engine,bundle.id,host,{contextOnly:true});
39
+ return {selected,relative,materialized:false};
40
+ } catch (error) {
41
+ // Do not claim an arbitrary pre-existing directory as an owned context.
42
+ if (previous?.path !== relative) throw new Error('Unmanaged or incomplete project context at '+relative+'; preserve it before retrying', {cause:error});
43
+ }
44
+ }
45
+ const staging = await mkdtemp(path.join(parent,'.staging-'));
46
+ try {
47
+ const files = [...Object.keys(bundle.files).filter(name=>!name.startsWith(engine+'/project/')),engine+'/bundle.json'];
48
+ for (const name of files) {
49
+ const output = await safeProjectFile(staging,name,true);
50
+ await writeFile(output,await readFile(path.join(resources,name)),{flag:'wx'});
51
+ }
52
+ await verifyBundle(staging,engine,bundle.id,{contextOnly:true});
53
+ if (present) {
54
+ const backups = await directory(project,`.craft/context-backups/${host}/${engine}`);
55
+ backup = path.join(backups,bundle.id+'-'+randomUUID());
56
+ await rename(target,backup);
57
+ }
58
+ await rename(staging,target);
59
+ const selected = await loadEngineResource(target,engine,bundle.id,host,{contextOnly:true});
60
+ return {selected,relative,materialized:true,...(backup ? {backup} : {})};
61
+ } finally { await rm(staging,{recursive:true,force:true}); }
62
+ }