File size: 6,027 Bytes
010bc6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c67d8f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
010bc6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# NaturalCAD Backend v0

## Goal

Build a low-cost backend for NaturalCAD that is safe enough for an MVP:
- public UI on Hugging Face Spaces
- hosted inference on Hugging Face
- no important execution on Noah's laptop
- no public arbitrary code execution
- logs, metadata, and artifacts stored off-machine

## Guiding priorities

1. Keep costs low
2. Prevent spam and abuse
3. Keep secrets off the frontend
4. Avoid exposing raw Python execution to the public
5. Keep the system simple enough to actually ship

## Recommended stack

### Frontend
- Hugging Face Space
- current Gradio app is the fastest path

### Backend API
- FastAPI deployed on Fly.io
- reason: build123d and worker logic are already Python-adjacent, so this reduces stack complexity while giving us a sturdier app host for API + worker processes

### Inference
- Hugging Face Inference Endpoint or hosted HF model endpoint
- prefer free or low-cost model path for MVP

### Database
- Supabase Postgres
- reason: structured job records, artifact metadata, and status transitions fit naturally in Postgres, and Supabase gives a good hosted dashboard with low MVP friction

### Object storage
- hosted object storage, not local disk
- S3-compatible storage is preferred

### Worker
- isolated Python worker for geometry generation
- build123d execution should happen here, not in the public frontend tier

## Trust boundaries

### Hugging Face Space
Allowed:
- collect prompts
- submit jobs to backend
- display status and results

Not allowed:
- store backend secrets
- directly execute build123d jobs
- write directly to database with privileged credentials
- be the source of truth for rate limiting or audit policy

### Backend API
Responsible for:
- request validation
- rate limiting
- job creation
- inference calls
- schema validation
- queue handoff
- database writes
- artifact metadata
- audit logs

### Worker
Responsible for:
- consuming approved jobs
- generating structured CAD outputs
- optionally translating internal spec to build123d code
- exporting STL/STEP
- uploading artifacts to hosted storage
- updating job status

### Database
Store:
- job records
- prompt text
- derived structured spec
- status transitions
- artifact metadata
- session or user metadata
- audit events
- rate-limit counters if needed

### Object storage
Store:
- STL files
- STEP files
- previews
- log blobs if needed

## Public input model

### Public API rule
Public users submit prompts, not arbitrary code.

That means:
- user sends prompt text
- backend calls model
- model returns structured data or a constrained internal representation
- worker generates geometry from approved internal data

### Internal flexibility
Internally, NaturalCAD may still generate build123d code if that helps implementation.
But code generation should stay behind the backend/worker boundary, not exposed as a public execution surface.

## Job lifecycle

Use these statuses:
- `submitted`
- `validated`
- `queued`
- `running`
- `completed`
- `failed`

Optional later:
- `blocked`
- `expired`
- `canceled`

## Minimal API shape

### `POST /jobs`
Create a job.

Input:
- prompt
- optional session id
- optional client metadata

Server actions:
- validate payload
- apply rate limit
- create job record
- call inference or enqueue pre-inference flow

Returns:
- job id
- status

### `GET /jobs/{job_id}`
Fetch job status.

Returns:
- current status
- error info if failed
- artifact metadata if completed

### `GET /jobs/{job_id}/artifacts`
Return artifact metadata and signed URLs if applicable.

### `GET /health`
Basic health check.

## Suggested Postgres tables

### `jobs`
Columns:
- `id`
- `created_at`
- `updated_at`
- `status`
- `prompt`
- `normalized_prompt`
- `spec_json`
- `error_text`
- `client_session_id`
- `ip_hash`
- `model_info_json`

### `artifacts`
Columns:
- `id`
- `job_id`
- `kind` (`stl`, `step`, `preview`, `log`)
- `storage_key`
- `size_bytes`
- `created_at`
- `expires_at`

### `audit_events`
Columns:
- `id`
- `job_id`
- `event_type`
- `created_at`
- `details_json`

### `rate_limits`
Optional if not handled elsewhere.

## Queue strategy

For MVP, keep it simple.

Options:
1. DB-backed queue with polling
2. lightweight Redis queue later if needed

Recommendation:
- start with a DB-backed queue and one worker
- upgrade only when traffic justifies it

## Low-cost implementation order

### Phase 0
- keep Gradio frontend
- do not deploy public raw code execution

### Phase 1
- create FastAPI service on Fly.io
- add `POST /jobs` and `GET /jobs/{job_id}`
- connect Supabase Postgres
- add simple rate limiting

### Phase 2
- connect HF inference endpoint
- store prompt, status, and response metadata
- validate returned structured output

### Phase 3
- add Python worker
- generate artifacts
- upload artifacts to hosted storage
- return signed artifact links

### Phase 4
- tighten retention, add auth tiers, add cancellation, add preview generation

## Spec direction for the next phase

NaturalCAD should move next toward a **loose compositional / semantic JSON spec** rather than a rigid family-first schema.

Reason:
- rigid family routing too early will bias the model toward repetitive safe defaults
- concept-grade generation needs room for novelty, unexpected topology, and broader prompt coverage
- reuse and dedupe should exist, but as later optimization layers rather than the main creative frame

Recommended next spec target:
- `intent`
- `semantic_part`
- `family_hint` (optional, not dominant)
- `geometry`
- `dimensions`
- `constraints`
- `style`
- `dedupe`

Reference:
- `docs/compositional-spec-v1.1.md`

## What not to do in v0

- no public arbitrary Python execution
- no local laptop as production backend
- no secrets in frontend code
- no unlimited artifact retention
- no complicated microservice split

## Default recommendation

NaturalCAD v0 should ship as:
- Hugging Face Space frontend
- FastAPI backend on Fly.io
- Supabase Postgres
- hosted object storage
- one Python worker
- strict prompt-only public interface