@fugood/bricks-ctor 2.25.0-beta.5 → 2.25.0-beta.50

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 (185) hide show
  1. package/compile/__tests__/config-diff.test.js +100 -0
  2. package/compile/__tests__/index.test.js +365 -0
  3. package/compile/__tests__/util.test.js +317 -0
  4. package/compile/action-name-map.ts +64 -0
  5. package/compile/config-diff.ts +155 -0
  6. package/compile/index.ts +273 -32
  7. package/compile/util.ts +26 -7
  8. package/package.json +7 -3
  9. package/skills/bricks-ctor/SKILL.md +23 -17
  10. package/skills/bricks-ctor/{rules → references}/animation.md +3 -2
  11. package/skills/bricks-ctor/{rules → references}/architecture-patterns.md +18 -0
  12. package/skills/bricks-ctor/{rules → references}/automations.md +11 -0
  13. package/skills/bricks-ctor/references/buttress.md +245 -0
  14. package/skills/bricks-ctor/references/data-calculation.md +239 -0
  15. package/skills/bricks-ctor/references/simulator.md +132 -0
  16. package/skills/bricks-ctor/references/source-editing-tools.md +81 -0
  17. package/skills/bricks-ctor/references/verification-toolchain.md +200 -0
  18. package/skills/bricks-design/SKILL.md +150 -45
  19. package/skills/bricks-design/references/architecture-truths.md +132 -0
  20. package/skills/bricks-design/references/avoiding-complexity.md +91 -0
  21. package/skills/bricks-design/references/design-critique.md +195 -0
  22. package/skills/bricks-design/references/design-languages.md +265 -0
  23. package/skills/bricks-design/references/performance.md +116 -0
  24. package/skills/bricks-design/references/presentation-and-slideshow.md +137 -0
  25. package/skills/bricks-design/references/translating-inputs.md +152 -0
  26. package/skills/bricks-design/references/variations-and-tweaks.md +124 -0
  27. package/skills/bricks-design/references/when-the-brief-is-branded.md +284 -0
  28. package/skills/bricks-design/references/when-the-brief-is-vague.md +85 -0
  29. package/skills/bricks-design/references/workflow.md +134 -0
  30. package/skills/bricks-ux/SKILL.md +114 -0
  31. package/skills/bricks-ux/references/accessibility.md +162 -0
  32. package/skills/bricks-ux/references/flow-states.md +175 -0
  33. package/skills/bricks-ux/references/interaction-archetypes.md +189 -0
  34. package/skills/bricks-ux/references/monitoring-screens.md +153 -0
  35. package/skills/bricks-ux/references/pressable-composition.md +126 -0
  36. package/skills/bricks-ux/references/user-journey.md +168 -0
  37. package/skills/bricks-ux/references/ux-critique.md +256 -0
  38. package/tools/__tests__/_cli-error.test.ts +35 -0
  39. package/tools/_cli-error.ts +17 -0
  40. package/tools/_edits-log.ts +41 -0
  41. package/tools/_git-author.ts +10 -2
  42. package/tools/_last-pushed-commit.ts +28 -0
  43. package/tools/_shell.ts +8 -1
  44. package/tools/deploy.ts +17 -6
  45. package/tools/mcp-env.ts +13 -0
  46. package/tools/mcp-server.ts +8 -0
  47. package/tools/mcp-tools/__tests__/data-calc-editing.test.js +516 -0
  48. package/tools/mcp-tools/__tests__/entry-editing.test.js +866 -0
  49. package/tools/mcp-tools/__tests__/huggingface.test.ts +49 -0
  50. package/tools/mcp-tools/__tests__/icons.test.ts +21 -0
  51. package/tools/mcp-tools/__tests__/mcp-env.test.js +19 -0
  52. package/tools/mcp-tools/_editing-helpers.ts +58 -0
  53. package/tools/mcp-tools/_verify.ts +50 -0
  54. package/tools/mcp-tools/compile.ts +21 -9
  55. package/tools/mcp-tools/data-calc-editing.ts +1349 -0
  56. package/tools/mcp-tools/entry-editing.ts +2336 -0
  57. package/tools/mcp-tools/huggingface.ts +23 -13
  58. package/tools/mcp-tools/icons.ts +23 -7
  59. package/tools/mcp-tools/media.ts +4 -1
  60. package/tools/postinstall.ts +80 -3
  61. package/tools/pull.ts +93 -22
  62. package/tools/push-config.ts +114 -0
  63. package/tools/{preview-main.mjs → simulator-main.mjs} +207 -12
  64. package/tools/simulator-preload.cjs +16 -0
  65. package/tools/{preview.ts → simulator.ts} +4 -4
  66. package/types/{animation.ts → animation.d.ts} +24 -8
  67. package/types/{automation.ts → automation.d.ts} +16 -20
  68. package/types/{brick-base.ts → brick-base.d.ts} +1 -1
  69. package/types/bricks/{Camera.ts → Camera.d.ts} +8 -8
  70. package/types/bricks/{Chart.ts → Chart.d.ts} +4 -4
  71. package/types/bricks/{GenerativeMedia.ts → GenerativeMedia.d.ts} +15 -15
  72. package/types/bricks/{Icon.ts → Icon.d.ts} +7 -7
  73. package/types/bricks/{Image.ts → Image.d.ts} +21 -9
  74. package/types/bricks/{Items.ts → Items.d.ts} +7 -7
  75. package/types/bricks/{Lottie.ts → Lottie.d.ts} +10 -10
  76. package/types/bricks/{Maps.ts → Maps.d.ts} +11 -11
  77. package/types/bricks/{QrCode.ts → QrCode.d.ts} +7 -7
  78. package/types/bricks/{Rect.ts → Rect.d.ts} +7 -7
  79. package/types/bricks/{RichText.ts → RichText.d.ts} +12 -9
  80. package/types/bricks/{Rive.ts → Rive.d.ts} +9 -9
  81. package/types/bricks/Scene3D.d.ts +676 -0
  82. package/types/bricks/{Sketch.ts → Sketch.d.ts} +6 -6
  83. package/types/bricks/{Slideshow.ts → Slideshow.d.ts} +7 -7
  84. package/types/bricks/{Svg.ts → Svg.d.ts} +7 -7
  85. package/types/bricks/{Text.ts → Text.d.ts} +9 -9
  86. package/types/bricks/{TextInput.ts → TextInput.d.ts} +10 -10
  87. package/types/bricks/{Video.ts → Video.d.ts} +12 -12
  88. package/types/bricks/{VideoStreaming.ts → VideoStreaming.d.ts} +10 -10
  89. package/types/bricks/{WebRtcStream.ts → WebRtcStream.d.ts} +1 -1
  90. package/types/bricks/{WebView.ts → WebView.d.ts} +4 -4
  91. package/types/bricks/{index.ts → index.d.ts} +1 -0
  92. package/types/{common.ts → common.d.ts} +3 -6
  93. package/types/data-calc-command/base.d.ts +57 -0
  94. package/types/data-calc-command/collection.d.ts +418 -0
  95. package/types/data-calc-command/color.d.ts +432 -0
  96. package/types/data-calc-command/constant.d.ts +50 -0
  97. package/types/data-calc-command/datetime.d.ts +147 -0
  98. package/types/data-calc-command/file.d.ts +129 -0
  99. package/types/data-calc-command/index.d.ts +13 -0
  100. package/types/data-calc-command/iteratee.d.ts +23 -0
  101. package/types/data-calc-command/logictype.d.ts +190 -0
  102. package/types/data-calc-command/math.d.ts +275 -0
  103. package/types/data-calc-command/object.d.ts +119 -0
  104. package/types/data-calc-command/sandbox.d.ts +66 -0
  105. package/types/data-calc-command/string.d.ts +407 -0
  106. package/types/{data-calc.ts → data-calc.d.ts} +1 -0
  107. package/types/{data.ts → data.d.ts} +4 -2
  108. package/types/generators/{Assistant.ts → Assistant.d.ts} +19 -0
  109. package/types/generators/{LlmGgml.ts → LlmGgml.d.ts} +43 -1
  110. package/types/generators/{LlmMlx.ts → LlmMlx.d.ts} +1 -0
  111. package/types/generators/{RerankerGgml.ts → RerankerGgml.d.ts} +5 -1
  112. package/types/generators/{SoundRecorder.ts → SoundRecorder.d.ts} +10 -1
  113. package/types/generators/{SpeechToTextGgml.ts → SpeechToTextGgml.d.ts} +6 -1
  114. package/types/generators/{SttAppleBuiltin.ts → SttAppleBuiltin.d.ts} +27 -4
  115. package/types/generators/{ThermalPrinter.ts → ThermalPrinter.d.ts} +9 -7
  116. package/types/generators/{VadGgml.ts → VadGgml.d.ts} +12 -2
  117. package/types/{subspace.ts → subspace.d.ts} +1 -1
  118. package/utils/__tests__/calc.test.js +25 -0
  119. package/utils/__tests__/id.test.js +154 -0
  120. package/utils/calc.ts +5 -1
  121. package/utils/data.ts +5 -7
  122. package/utils/event-props.ts +17 -0
  123. package/utils/id.ts +109 -56
  124. package/skills/bricks-ctor/rules/buttress.md +0 -156
  125. package/skills/bricks-ctor/rules/data-calculation.md +0 -209
  126. package/skills/bricks-design/LICENSE.txt +0 -180
  127. package/types/data-calc-command.ts +0 -7005
  128. /package/skills/bricks-ctor/{rules → references}/local-sync.md +0 -0
  129. /package/skills/bricks-ctor/{rules → references}/media-flow.md +0 -0
  130. /package/skills/bricks-ctor/{rules → references}/remote-data-bank.md +0 -0
  131. /package/skills/bricks-ctor/{rules → references}/standby-transition.md +0 -0
  132. /package/types/{canvas.ts → canvas.d.ts} +0 -0
  133. /package/types/{data-calc-script.ts → data-calc-script.d.ts} +0 -0
  134. /package/types/generators/{AlarmClock.ts → AlarmClock.d.ts} +0 -0
  135. /package/types/generators/{BleCentral.ts → BleCentral.d.ts} +0 -0
  136. /package/types/generators/{BlePeripheral.ts → BlePeripheral.d.ts} +0 -0
  137. /package/types/generators/{CanvasMap.ts → CanvasMap.d.ts} +0 -0
  138. /package/types/generators/{CastlesPay.ts → CastlesPay.d.ts} +0 -0
  139. /package/types/generators/{DataBank.ts → DataBank.d.ts} +0 -0
  140. /package/types/generators/{File.ts → File.d.ts} +0 -0
  141. /package/types/generators/{GraphQl.ts → GraphQl.d.ts} +0 -0
  142. /package/types/generators/{Http.ts → Http.d.ts} +0 -0
  143. /package/types/generators/{HttpServer.ts → HttpServer.d.ts} +0 -0
  144. /package/types/generators/{Information.ts → Information.d.ts} +0 -0
  145. /package/types/generators/{Intent.ts → Intent.d.ts} +0 -0
  146. /package/types/generators/{Iterator.ts → Iterator.d.ts} +0 -0
  147. /package/types/generators/{Keyboard.ts → Keyboard.d.ts} +0 -0
  148. /package/types/generators/{LlmAnthropicCompat.ts → LlmAnthropicCompat.d.ts} +0 -0
  149. /package/types/generators/{LlmAppleBuiltin.ts → LlmAppleBuiltin.d.ts} +0 -0
  150. /package/types/generators/{LlmMediaTekNeuroPilot.ts → LlmMediaTekNeuroPilot.d.ts} +0 -0
  151. /package/types/generators/{LlmOnnx.ts → LlmOnnx.d.ts} +0 -0
  152. /package/types/generators/{LlmOpenAiCompat.ts → LlmOpenAiCompat.d.ts} +0 -0
  153. /package/types/generators/{LlmQualcommAiEngine.ts → LlmQualcommAiEngine.d.ts} +0 -0
  154. /package/types/generators/{Mcp.ts → Mcp.d.ts} +0 -0
  155. /package/types/generators/{McpServer.ts → McpServer.d.ts} +0 -0
  156. /package/types/generators/{MediaFlow.ts → MediaFlow.d.ts} +0 -0
  157. /package/types/generators/{MqttBroker.ts → MqttBroker.d.ts} +0 -0
  158. /package/types/generators/{MqttClient.ts → MqttClient.d.ts} +0 -0
  159. /package/types/generators/{Question.ts → Question.d.ts} +0 -0
  160. /package/types/generators/{RealtimeTranscription.ts → RealtimeTranscription.d.ts} +0 -0
  161. /package/types/generators/{SerialPort.ts → SerialPort.d.ts} +0 -0
  162. /package/types/generators/{SoundPlayer.ts → SoundPlayer.d.ts} +0 -0
  163. /package/types/generators/{SpeechToTextOnnx.ts → SpeechToTextOnnx.d.ts} +0 -0
  164. /package/types/generators/{SpeechToTextPlatform.ts → SpeechToTextPlatform.d.ts} +0 -0
  165. /package/types/generators/{SqLite.ts → SqLite.d.ts} +0 -0
  166. /package/types/generators/{Step.ts → Step.d.ts} +0 -0
  167. /package/types/generators/{Tcp.ts → Tcp.d.ts} +0 -0
  168. /package/types/generators/{TcpServer.ts → TcpServer.d.ts} +0 -0
  169. /package/types/generators/{TextToSpeechAppleBuiltin.ts → TextToSpeechAppleBuiltin.d.ts} +0 -0
  170. /package/types/generators/{TextToSpeechGgml.ts → TextToSpeechGgml.d.ts} +0 -0
  171. /package/types/generators/{TextToSpeechOnnx.ts → TextToSpeechOnnx.d.ts} +0 -0
  172. /package/types/generators/{TextToSpeechOpenAiLike.ts → TextToSpeechOpenAiLike.d.ts} +0 -0
  173. /package/types/generators/{Tick.ts → Tick.d.ts} +0 -0
  174. /package/types/generators/{Udp.ts → Udp.d.ts} +0 -0
  175. /package/types/generators/{VadOnnx.ts → VadOnnx.d.ts} +0 -0
  176. /package/types/generators/{VadTraditional.ts → VadTraditional.d.ts} +0 -0
  177. /package/types/generators/{VectorStore.ts → VectorStore.d.ts} +0 -0
  178. /package/types/generators/{Watchdog.ts → Watchdog.d.ts} +0 -0
  179. /package/types/generators/{WebCrawler.ts → WebCrawler.d.ts} +0 -0
  180. /package/types/generators/{WebRtc.ts → WebRtc.d.ts} +0 -0
  181. /package/types/generators/{WebSocket.ts → WebSocket.d.ts} +0 -0
  182. /package/types/generators/{index.ts → index.d.ts} +0 -0
  183. /package/types/{index.ts → index.d.ts} +0 -0
  184. /package/types/{switch.ts → switch.d.ts} +0 -0
  185. /package/types/{system.ts → system.d.ts} +0 -0
@@ -15,7 +15,15 @@ async function hasGitIdentity(cwd: string): Promise<boolean> {
15
15
  // If no identity is configured (typical for non-tech users), CTOR becomes the
16
16
  // main author via per-command `-c user.name/-c user.email` overrides so the
17
17
  // user's git config is left untouched.
18
- export async function buildCommitArgs(cwd: string, messages: string[]): Promise<string[]> {
18
+ //
19
+ // `extraFlags` are inserted between `commit` and the `-m` args (e.g.
20
+ // `['--no-verify']` to bypass pre-commit hooks for controlled cases like
21
+ // committing merge-conflict markers).
22
+ export async function buildCommitArgs(
23
+ cwd: string,
24
+ messages: string[],
25
+ extraFlags: string[] = [],
26
+ ): Promise<string[]> {
19
27
  const hasIdentity = await hasGitIdentity(cwd)
20
28
  const prefix = hasIdentity
21
29
  ? []
@@ -23,7 +31,7 @@ export async function buildCommitArgs(cwd: string, messages: string[]): Promise<
23
31
  const msgArgs: string[] = []
24
32
  for (const m of messages) msgArgs.push('-m', m)
25
33
  if (hasIdentity) msgArgs.push('-m', CTOR_COAUTHOR_TRAILER)
26
- return [...prefix, 'commit', ...msgArgs]
34
+ return [...prefix, 'commit', ...extraFlags, ...msgArgs]
27
35
  }
28
36
 
29
37
  export { CTOR_NAME, CTOR_EMAIL, CTOR_COAUTHOR_TRAILER }
@@ -0,0 +1,28 @@
1
+ import { mkdir, readFile, writeFile } from 'node:fs/promises'
2
+
3
+ // Local sync marker: the git commit hash that was on HEAD when we last
4
+ // successfully pushed to the server (deploy or push-config) or pulled
5
+ // from it. Stored at the `.bricks/` top level (gitignored explicitly) — it's
6
+ // per-clone state, not a build artifact, so it doesn't belong under
7
+ // `.bricks/build/`.
8
+ //
9
+ // Why this exists: server's `bricks_project_last_commit_id` can be an opaque
10
+ // nanoid for content-only edits (config-editor saves), so it isn't a
11
+ // reliable git anchor for `git checkout` during pull. The local marker is
12
+ // always a real reachable commit hash from THIS clone.
13
+ const REL_PATH = '.bricks/.last-pushed-commit'
14
+
15
+ export async function readLastPushedCommit(cwd: string): Promise<string | null> {
16
+ try {
17
+ const content = await readFile(`${cwd}/${REL_PATH}`, 'utf8')
18
+ return content.trim() || null
19
+ } catch {
20
+ return null
21
+ }
22
+ }
23
+
24
+ export async function writeLastPushedCommit(cwd: string, commitId: string): Promise<void> {
25
+ if (!commitId) return
26
+ await mkdir(`${cwd}/.bricks`, { recursive: true })
27
+ await writeFile(`${cwd}/${REL_PATH}`, commitId)
28
+ }
package/tools/_shell.ts CHANGED
@@ -29,6 +29,7 @@ class Sh implements PromiseLike<ShResult> {
29
29
  private _cwd: string | undefined
30
30
  private _mode: Mode = 'default'
31
31
  private _throw = true
32
+ private _env: NodeJS.ProcessEnv | undefined
32
33
  private _promise: Promise<ShResult> | null = null
33
34
 
34
35
  constructor(private readonly args: string[]) {}
@@ -48,6 +49,11 @@ class Sh implements PromiseLike<ShResult> {
48
49
  return this
49
50
  }
50
51
 
52
+ env(extra: NodeJS.ProcessEnv): this {
53
+ this._env = { ...this._env, ...extra }
54
+ return this
55
+ }
56
+
51
57
  async text(): Promise<string> {
52
58
  // If no explicit quiet was requested, capture stdout/stderr without
53
59
  // forwarding them to the parent's streams (matches Bun's `.text()`).
@@ -79,7 +85,8 @@ class Sh implements PromiseLike<ShResult> {
79
85
  ? ['ignore', 'pipe', 'pipe']
80
86
  : ['inherit', 'pipe', 'pipe']
81
87
 
82
- const proc = spawn(cmd, rest, { cwd: this._cwd, stdio })
88
+ const env = this._env ? { ...process.env, ...this._env } : undefined
89
+ const proc = spawn(cmd, rest, { cwd: this._cwd, stdio, env })
83
90
 
84
91
  const outChunks: Buffer[] = []
85
92
  const errChunks: Buffer[] = []
package/tools/deploy.ts CHANGED
@@ -1,7 +1,13 @@
1
1
  import { access, readFile, writeFile } from 'node:fs/promises'
2
2
  import { parseArgs } from 'util'
3
3
  import { sh } from './_shell'
4
+ import { extractCliErrorMessage } from './_cli-error'
4
5
  import { buildCommitArgs } from './_git-author'
6
+ import { writeLastPushedCommit } from './_last-pushed-commit'
7
+
8
+ if (!process.env.BRICKS_RELEASE_SIGN) {
9
+ process.env.BRICKS_RELEASE_SIGN = 'CTOR'
10
+ }
5
11
 
6
12
  const cwd = process.cwd()
7
13
 
@@ -141,6 +147,10 @@ await writeFile(configPath, JSON.stringify(releaseConfig))
141
147
 
142
148
  const args = ['bricks', command, 'release', app.id, '-c', configPath, '--json']
143
149
 
150
+ if (app.name) {
151
+ args.push('--name', app.name)
152
+ }
153
+
144
154
  if (version) {
145
155
  args.push('--version', version)
146
156
  }
@@ -153,13 +163,14 @@ const result = await sh`${args}`.quiet().nothrow()
153
163
 
154
164
  if (result.exitCode !== 0) {
155
165
  const output = result.stderr.toString() || result.stdout.toString()
156
- try {
157
- const json = JSON.parse(output)
158
- throw new Error(json.error || 'Release failed')
159
- } catch {
160
- throw new Error(output || 'Release failed')
161
- }
166
+ throw new Error(extractCliErrorMessage(output, 'Release failed'))
162
167
  }
163
168
 
164
169
  const output = JSON.parse(result.stdout.toString())
170
+
171
+ // Record the commit we just pushed from so a later pull can use it as the
172
+ // merge base regardless of what the server stores in
173
+ // bricks_project_last_commit_id.
174
+ if (commitId) await writeLastPushedCommit(cwd, commitId)
175
+
165
176
  console.log(`${isModule ? 'Module' : 'App'} deployed: ${output.name}`)
@@ -0,0 +1,13 @@
1
+ const truthyEnvValues = new Set(['1', 'true', 'yes', 'on'])
2
+
3
+ export function isTruthyEnv(value: string | undefined) {
4
+ return truthyEnvValues.has(
5
+ String(value || '')
6
+ .trim()
7
+ .toLowerCase(),
8
+ )
9
+ }
10
+
11
+ export function shouldRegisterEditingTools(env: Record<string, string | undefined> = process.env) {
12
+ return isTruthyEnv(env.BRICKS_CTOR_ENABLE_EDITING_TOOLS)
13
+ }
@@ -6,6 +6,9 @@ import { register as registerLottie } from './mcp-tools/lottie'
6
6
  import { register as registerIcons } from './mcp-tools/icons'
7
7
  import { register as registerHuggingface } from './mcp-tools/huggingface'
8
8
  import { register as registerMedia } from './mcp-tools/media'
9
+ import { register as registerEntryEditing } from './mcp-tools/entry-editing'
10
+ import { register as registerDataCalcEditing } from './mcp-tools/data-calc-editing'
11
+ import { shouldRegisterEditingTools } from './mcp-env'
9
12
 
10
13
  const server = new McpServer({
11
14
  name: 'bricks-ctor',
@@ -24,5 +27,10 @@ registerIcons(server)
24
27
  registerHuggingface(server)
25
28
  registerMedia(server, projectDir)
26
29
 
30
+ if (shouldRegisterEditingTools()) {
31
+ registerEntryEditing(server, projectDir)
32
+ registerDataCalcEditing(server, projectDir)
33
+ }
34
+
27
35
  const transport = new StdioServerTransport()
28
36
  await server.connect(transport)
@@ -0,0 +1,516 @@
1
+ import { mkdtemp, mkdir, readFile, rm, writeFile } from 'node:fs/promises'
2
+ import os from 'node:os'
3
+ import path from 'node:path'
4
+
5
+ jest.mock('oxfmt', () => ({
6
+ format: async (_file, source) => ({ code: source, errors: [] }),
7
+ }))
8
+
9
+ import { register, __test__ } from '../data-calc-editing'
10
+
11
+ const writeFixtureProject = async () => {
12
+ const projectDir = await mkdtemp(path.join(os.tmpdir(), 'bricks-data-calc-editing-'))
13
+ const subspaceDir = path.join(projectDir, 'subspaces/subspace-0')
14
+ await mkdir(subspaceDir, { recursive: true })
15
+ await writeFile(
16
+ path.join(subspaceDir, 'data.ts'),
17
+ `import type { Data } from 'bricks-ctor'
18
+ import { makeId } from 'bricks-ctor'
19
+
20
+ export const dPrice: Data = {
21
+ __typename: 'Data',
22
+ id: makeId('data'),
23
+ alias: 'price',
24
+ title: 'Price',
25
+ type: 'number',
26
+ value: 5,
27
+ }
28
+
29
+ export const dQuantity: Data = {
30
+ __typename: 'Data',
31
+ id: makeId('data'),
32
+ alias: 'quantity',
33
+ title: 'Quantity',
34
+ type: 'number',
35
+ value: 2,
36
+ }
37
+
38
+ export const dTotal: Data = {
39
+ __typename: 'Data',
40
+ id: makeId('data'),
41
+ alias: 'total',
42
+ title: 'Total',
43
+ type: 'number',
44
+ value: 0,
45
+ }
46
+
47
+ export const dError: Data = {
48
+ __typename: 'Data',
49
+ id: makeId('data'),
50
+ alias: 'error',
51
+ title: 'Error',
52
+ type: 'string',
53
+ value: '',
54
+ }
55
+ `,
56
+ )
57
+ await writeFile(
58
+ path.join(subspaceDir, 'index.ts'),
59
+ `import type { Subspace } from 'bricks-ctor'
60
+
61
+ export default {
62
+ __typename: 'Subspace',
63
+ id: 'SUBSPACE_TEST',
64
+ title: 'Main',
65
+ layout: { width: 96, height: 54 },
66
+ canvases: [],
67
+ rootCanvas: undefined,
68
+ animations: [],
69
+ bricks: [],
70
+ generators: [],
71
+ data: Object.values(data),
72
+ dataRouting: [],
73
+ dataCalculation: [],
74
+ } as Subspace
75
+ `,
76
+ )
77
+ return projectDir
78
+ }
79
+
80
+ const readProjectFile = (projectDir, relPath) => readFile(path.join(projectDir, relPath), 'utf8')
81
+
82
+ const readAudit = async (projectDir) =>
83
+ (await readFile(path.join(projectDir, '.bricks/edits.jsonl'), 'utf8'))
84
+ .trim()
85
+ .split('\n')
86
+ .map((line) => JSON.parse(line))
87
+
88
+ describe('ctor MCP data-calc editing tools', () => {
89
+ let projectDir
90
+
91
+ afterEach(async () => {
92
+ if (projectDir) await rm(projectDir, { recursive: true, force: true })
93
+ projectDir = null
94
+ })
95
+
96
+ test('registers the four data-calc MCP tools with descriptions', () => {
97
+ const calls = []
98
+ register(
99
+ {
100
+ tool(...args) {
101
+ calls.push(args)
102
+ },
103
+ },
104
+ '/tmp/project',
105
+ )
106
+
107
+ expect(calls.map(([name]) => name)).toEqual([
108
+ 'new_data_calc',
109
+ 'edit_data_calc',
110
+ 'edit_data_calc_io',
111
+ 'remove_data_calc',
112
+ ])
113
+ expect(
114
+ calls.every(([, description]) => typeof description === 'string' && description.length > 20),
115
+ ).toBe(true)
116
+ expect(calls[0][2].code.description).toContain('export function main')
117
+ expect(calls[2][2].data.description).toContain('{ ref')
118
+ })
119
+
120
+ test('new_data_calc creates script, sandbox, index, and minimal subspace wiring', async () => {
121
+ projectDir = await writeFixtureProject()
122
+
123
+ const result = await __test__.newDataCalc(projectDir, {
124
+ alias: 'totalCalc',
125
+ title: 'Total Calc',
126
+ code: 'return { total: inputs.price * inputs.quantity }',
127
+ inputs: [
128
+ { key: 'price', data: 'price' },
129
+ { key: 'quantity', data: 'quantity', trigger: false },
130
+ ],
131
+ outputs: [{ key: 'total', data: 'total' }],
132
+ output: 'total',
133
+ error: 'error',
134
+ verify: false,
135
+ })
136
+
137
+ expect(result.outcome).toBe('ok')
138
+ expect(result.calc).toBe('total-calc')
139
+ const calc = await readProjectFile(
140
+ projectDir,
141
+ 'subspaces/subspace-0/data-calc/data-calculation-total-calc.ts',
142
+ )
143
+ expect(calc).toMatch(/id:\s*makeId\(["']property_bank_calc["'], ["']totalCalc["']\)/)
144
+ expect(calc).toMatch(
145
+ /code:\s*await readFile\(new URL\(["']\.\/data-calculation-total-calc\.sandbox\.js["']/,
146
+ )
147
+ expect(calc).toContain('data: () => data.dPrice')
148
+ expect(calc).toContain('trigger: false')
149
+ expect(calc).toContain('output: () => data.dTotal')
150
+ expect(calc).toContain('error: () => data.dError')
151
+ const sandbox = await readProjectFile(
152
+ projectDir,
153
+ 'subspaces/subspace-0/data-calc/data-calculation-total-calc.sandbox.js',
154
+ )
155
+ expect(sandbox).toContain('export function main()')
156
+ expect(sandbox).toContain('return { total: inputs.price * inputs.quantity }')
157
+ const index = await readProjectFile(projectDir, 'subspaces/subspace-0/data-calc/index.ts')
158
+ expect(index).toContain(
159
+ "import { dataCalculation as dataCalculation0 } from './data-calculation-total-calc'",
160
+ )
161
+ const subspaceIndex = await readProjectFile(projectDir, 'subspaces/subspace-0/index.ts')
162
+ expect(subspaceIndex).toMatch(/import \{ dataCalculation \} from ["']\.\/data-calc["']/)
163
+ expect(subspaceIndex).toMatch(/dataCalculation\s*\n\s*} as Subspace/)
164
+ const audit = await readAudit(projectDir)
165
+ expect(audit.at(-1).summary).toContain('created data calc total-calc')
166
+ })
167
+
168
+ test('records audit provenance from env', async () => {
169
+ projectDir = await writeFixtureProject()
170
+ const previousSessionId = process.env.BRICKS_CTOR_SESSION_ID
171
+ const previousAgentId = process.env.BRICKS_CTOR_AGENT_ID
172
+ process.env.BRICKS_CTOR_SESSION_ID = 'session-1'
173
+ process.env.BRICKS_CTOR_AGENT_ID = 'agent-1'
174
+
175
+ try {
176
+ const result = await __test__.newDataCalc(projectDir, {
177
+ alias: 'auditCalc',
178
+ verify: false,
179
+ })
180
+
181
+ expect(result.outcome).toBe('ok')
182
+ const audit = await readAudit(projectDir)
183
+ expect(audit.at(-1).provenance).toEqual({
184
+ session: 'session-1',
185
+ agent: 'agent-1',
186
+ })
187
+ } finally {
188
+ if (previousSessionId === undefined) delete process.env.BRICKS_CTOR_SESSION_ID
189
+ else process.env.BRICKS_CTOR_SESSION_ID = previousSessionId
190
+ if (previousAgentId === undefined) delete process.env.BRICKS_CTOR_AGENT_ID
191
+ else process.env.BRICKS_CTOR_AGENT_ID = previousAgentId
192
+ }
193
+ })
194
+
195
+ test('edit_data_calc updates scalars, refs, and migrates inline code to sandbox form', async () => {
196
+ projectDir = await writeFixtureProject()
197
+ const dataCalcDir = path.join(projectDir, 'subspaces/subspace-0/data-calc')
198
+ await mkdir(dataCalcDir, { recursive: true })
199
+ await writeFile(
200
+ path.join(dataCalcDir, 'data-calculation-inline.ts'),
201
+ `import type { DataCalculationScript } from 'bricks-ctor'
202
+ import { makeId } from 'bricks-ctor'
203
+ import * as data from '../data'
204
+
205
+ export const dataCalculation: DataCalculationScript = {
206
+ __typename: 'DataCalculationScript',
207
+ id: makeId('property_bank_calc'),
208
+ alias: 'inline',
209
+ code: 'return inputs.price',
210
+ enableAsync: false,
211
+ inputs: [],
212
+ output: null,
213
+ outputs: [],
214
+ error: null,
215
+ }
216
+ `,
217
+ )
218
+
219
+ const result = await __test__.editDataCalc(projectDir, {
220
+ subspace: 0,
221
+ calc: 'inline',
222
+ set: {
223
+ title: 'Edited Inline',
224
+ enableAsync: true,
225
+ output: 'total',
226
+ error: 'error',
227
+ },
228
+ code: 'return { total: inputs.price }',
229
+ verify: false,
230
+ })
231
+
232
+ expect(result.outcome).toBe('ok')
233
+ const calc = await readProjectFile(
234
+ projectDir,
235
+ 'subspaces/subspace-0/data-calc/data-calculation-inline.ts',
236
+ )
237
+ expect(calc).toMatch(/import \{ readFile \} from ["']node:fs\/promises["']/)
238
+ expect(calc).toMatch(/title:\s*["']Edited Inline["']/)
239
+ expect(calc).toContain('enableAsync: true')
240
+ expect(calc).toMatch(
241
+ /code:\s*await readFile\(new URL\(["']\.\/data-calculation-inline\.sandbox\.js["']/,
242
+ )
243
+ expect(calc).toContain('output: () => data.dTotal')
244
+ expect(calc).toContain('error: () => data.dError')
245
+ const sandbox = await readProjectFile(
246
+ projectDir,
247
+ 'subspaces/subspace-0/data-calc/data-calculation-inline.sandbox.js',
248
+ )
249
+ expect(sandbox).toContain('return { total: inputs.price }')
250
+ })
251
+
252
+ test('edit_data_calc_io edits inputs and outputs with input-key uniqueness', async () => {
253
+ projectDir = await writeFixtureProject()
254
+ await __test__.newDataCalc(projectDir, {
255
+ alias: 'ioCalc',
256
+ inputs: [{ key: 'price', data: 'price' }],
257
+ outputs: [{ key: 'total', data: 'total' }],
258
+ verify: false,
259
+ })
260
+
261
+ const duplicate = await __test__.editDataCalcIo(projectDir, {
262
+ subspace: 0,
263
+ calc: 'io-calc',
264
+ field: 'inputs',
265
+ op: 'add',
266
+ key: 'price',
267
+ data: 'quantity',
268
+ verify: false,
269
+ })
270
+ expect(duplicate.outcome).toBe('error')
271
+ expect(duplicate.error.code).toBe('duplicate_key')
272
+
273
+ const addInput = await __test__.editDataCalcIo(projectDir, {
274
+ subspace: 0,
275
+ calc: 'io-calc',
276
+ field: 'inputs',
277
+ op: 'add',
278
+ key: 'quantity',
279
+ data: 'quantity',
280
+ trigger: false,
281
+ verify: false,
282
+ })
283
+ expect(addInput.outcome).toBe('ok')
284
+
285
+ const fanOut = await __test__.editDataCalcIo(projectDir, {
286
+ subspace: 0,
287
+ calc: 'io-calc',
288
+ field: 'outputs',
289
+ op: 'add',
290
+ key: 'total',
291
+ data: 'total',
292
+ verify: false,
293
+ })
294
+ expect(fanOut.outcome).toBe('ok')
295
+
296
+ const calc = await readProjectFile(
297
+ projectDir,
298
+ 'subspaces/subspace-0/data-calc/data-calculation-io-calc.ts',
299
+ )
300
+ expect(calc).toMatch(/key:\s*["']quantity["']/)
301
+ expect(calc).toContain('trigger: false')
302
+ expect((calc.match(/key:\s*["']total["']/g) || []).length).toBe(2)
303
+ })
304
+
305
+ test('new_data_calc wraps top-level await bodies in an async main', async () => {
306
+ projectDir = await writeFixtureProject()
307
+
308
+ const result = await __test__.newDataCalc(projectDir, {
309
+ alias: 'asyncCalc',
310
+ enableAsync: true,
311
+ code: 'const value = await Promise.resolve(inputs.price)\nreturn value',
312
+ inputs: [{ key: 'price', data: 'price' }],
313
+ verify: false,
314
+ })
315
+
316
+ expect(result.outcome).toBe('ok')
317
+ const sandbox = await readProjectFile(
318
+ projectDir,
319
+ 'subspaces/subspace-0/data-calc/data-calculation-async-calc.sandbox.js',
320
+ )
321
+ expect(sandbox).toContain('export async function main()')
322
+ })
323
+
324
+ test('new_data_calc rejects sandbox code that does not parse', async () => {
325
+ projectDir = await writeFixtureProject()
326
+
327
+ const result = await __test__.newDataCalc(projectDir, {
328
+ alias: 'broken',
329
+ code: 'return {',
330
+ verify: false,
331
+ })
332
+
333
+ expect(result.outcome).toBe('error')
334
+ expect(result.error.code).toBe('invalid_code')
335
+ })
336
+
337
+ test('edit_data_calc set routes inputs/outputs through the IO builder and guards required fields', async () => {
338
+ projectDir = await writeFixtureProject()
339
+ await __test__.newDataCalc(projectDir, { alias: 'setCalc', verify: false })
340
+
341
+ const ioViaSet = await __test__.editDataCalc(projectDir, {
342
+ subspace: 0,
343
+ calc: 'set-calc',
344
+ set: { inputs: [{ key: 'price', data: 'price' }] },
345
+ verify: false,
346
+ })
347
+ expect(ioViaSet.outcome).toBe('ok')
348
+ const calc = await readProjectFile(
349
+ projectDir,
350
+ 'subspaces/subspace-0/data-calc/data-calculation-set-calc.ts',
351
+ )
352
+ expect(calc).toContain('data: () => data.dPrice')
353
+
354
+ const codeViaSet = await __test__.editDataCalc(projectDir, {
355
+ subspace: 0,
356
+ calc: 'set-calc',
357
+ set: { code: 'return 1' },
358
+ verify: false,
359
+ })
360
+ expect(codeViaSet.outcome).toBe('error')
361
+ expect(codeViaSet.error.code).toBe('invalid_field')
362
+
363
+ const badUnset = await __test__.editDataCalc(projectDir, {
364
+ subspace: 0,
365
+ calc: 'set-calc',
366
+ unset: ['inputs'],
367
+ verify: false,
368
+ })
369
+ expect(badUnset.outcome).toBe('error')
370
+ expect(badUnset.error.code).toBe('invalid_field')
371
+ })
372
+
373
+ test('subspace+calc addressing tolerates non-Script calc files while scanning', async () => {
374
+ projectDir = await writeFixtureProject()
375
+ const dataCalcDir = path.join(projectDir, 'subspaces/subspace-0/data-calc')
376
+ await mkdir(dataCalcDir, { recursive: true })
377
+ await writeFile(
378
+ path.join(dataCalcDir, 'data-calculation-0.ts'),
379
+ `import type { DataCalculationMap } from 'bricks-ctor'
380
+
381
+ export const dataCalculation: DataCalculationMap = {
382
+ __typename: 'DataCalculationMap',
383
+ id: 'PROPERTY_BANK_COMMAND_MAP_MAP',
384
+ nodes: [],
385
+ editorInfo: [],
386
+ }
387
+ `,
388
+ )
389
+ await __test__.newDataCalc(projectDir, { alias: 'realCalc', verify: false })
390
+
391
+ const result = await __test__.editDataCalc(projectDir, {
392
+ subspace: 0,
393
+ calc: 'realCalc',
394
+ set: { title: 'Real' },
395
+ verify: false,
396
+ })
397
+
398
+ expect(result.outcome).toBe('ok')
399
+ expect(result.file).toBe('subspaces/subspace-0/data-calc/data-calculation-real-calc.ts')
400
+ })
401
+
402
+ test('new_data_calc warns instead of overwriting a non-empty inline dataCalculation', async () => {
403
+ projectDir = await writeFixtureProject()
404
+ await writeFile(
405
+ path.join(projectDir, 'subspaces/subspace-0/index.ts'),
406
+ `import type { Subspace } from 'bricks-ctor'
407
+
408
+ export default {
409
+ __typename: 'Subspace',
410
+ id: 'SUBSPACE_TEST',
411
+ title: 'Main',
412
+ dataCalculation: [{ __typename: 'DataCalculationScript' }],
413
+ } as Subspace
414
+ `,
415
+ )
416
+
417
+ const result = await __test__.newDataCalc(projectDir, { alias: 'warned', verify: false })
418
+
419
+ expect(result.outcome).toBe('ok')
420
+ expect(result.warnings?.[0]).toContain('non-empty dataCalculation')
421
+ const index = await readProjectFile(projectDir, 'subspaces/subspace-0/index.ts')
422
+ expect(index).not.toMatch(/import \{ dataCalculation \}/)
423
+ expect(index).toContain("dataCalculation: [{ __typename: 'DataCalculationScript' }]")
424
+ })
425
+
426
+ test('remove_data_calc ignores non-sibling sandbox filenames in code', async () => {
427
+ projectDir = await writeFixtureProject()
428
+ const dataCalcDir = path.join(projectDir, 'subspaces/subspace-0/data-calc')
429
+ await mkdir(dataCalcDir, { recursive: true })
430
+ await writeFile(path.join(projectDir, 'evil.sandbox.js'), 'export function main() {}\n')
431
+ await writeFile(
432
+ path.join(dataCalcDir, 'data-calculation-escape.ts'),
433
+ `import type { DataCalculationScript } from 'bricks-ctor'
434
+ import { makeId } from 'bricks-ctor'
435
+ import { readFile } from 'node:fs/promises'
436
+
437
+ export const dataCalculation: DataCalculationScript = {
438
+ __typename: 'DataCalculationScript',
439
+ id: makeId('property_bank_calc'),
440
+ alias: 'escape',
441
+ code: await readFile(new URL('../../../evil.sandbox.js', import.meta.url), 'utf8'),
442
+ enableAsync: false,
443
+ inputs: [],
444
+ output: null,
445
+ outputs: [],
446
+ error: null,
447
+ }
448
+ `,
449
+ )
450
+
451
+ const result = await __test__.removeDataCalc(projectDir, {
452
+ subspace: 0,
453
+ calc: 'escape',
454
+ verify: false,
455
+ })
456
+
457
+ expect(result.outcome).toBe('ok')
458
+ await expect(readProjectFile(projectDir, 'evil.sandbox.js')).resolves.toContain('main')
459
+ })
460
+
461
+ test('remove_data_calc deletes calc files and regenerates index', async () => {
462
+ projectDir = await writeFixtureProject()
463
+ await __test__.newDataCalc(projectDir, {
464
+ alias: 'removeMe',
465
+ code: 'return inputs.price',
466
+ verify: false,
467
+ })
468
+
469
+ const result = await __test__.removeDataCalc(projectDir, {
470
+ subspace: 0,
471
+ calc: 'remove-me',
472
+ verify: false,
473
+ })
474
+
475
+ expect(result.outcome).toBe('ok')
476
+ await expect(
477
+ readProjectFile(projectDir, 'subspaces/subspace-0/data-calc/data-calculation-remove-me.ts'),
478
+ ).rejects.toThrow()
479
+ await expect(
480
+ readProjectFile(
481
+ projectDir,
482
+ 'subspaces/subspace-0/data-calc/data-calculation-remove-me.sandbox.js',
483
+ ),
484
+ ).rejects.toThrow()
485
+ const index = await readProjectFile(projectDir, 'subspaces/subspace-0/data-calc/index.ts')
486
+ expect(index).not.toContain('remove-me')
487
+ })
488
+
489
+ test('DataCalculationMap returns fallback recommendation', async () => {
490
+ projectDir = await writeFixtureProject()
491
+ const dataCalcDir = path.join(projectDir, 'subspaces/subspace-0/data-calc')
492
+ await mkdir(dataCalcDir, { recursive: true })
493
+ await writeFile(
494
+ path.join(dataCalcDir, 'data-calculation-map.ts'),
495
+ `import type { DataCalculationMap } from 'bricks-ctor'
496
+
497
+ export const dataCalculation: DataCalculationMap = {
498
+ __typename: 'DataCalculationMap',
499
+ id: 'PROPERTY_BANK_COMMAND_MAP_MAP',
500
+ nodes: [],
501
+ editorInfo: [],
502
+ }
503
+ `,
504
+ )
505
+
506
+ const result = await __test__.editDataCalc(projectDir, {
507
+ file: 'subspaces/subspace-0/data-calc/data-calculation-map.ts',
508
+ set: { title: 'Map' },
509
+ verify: false,
510
+ })
511
+
512
+ expect(result.outcome).toBe('fallback_recommended')
513
+ expect(result.isError).toBe(false)
514
+ expect(result.error.message).toContain('DataCalculationMap is out of scope')
515
+ })
516
+ })