Commit 8bc630a
feat(agents): generic externally-writable custom collections (comments as first consumer) (#4551)
## Summary
An alternative to #4529 that delivers the same session-comments feature,
but built on a **generic, extensible interface** instead of hardcoding
`comments` into the entity schema. Comments becomes one *custom
collection* among potentially many; the mechanism underneath is
reusable.
Three capabilities the agent runtime didn't have before:
1. **Opt-in externally-writable collections.** Entity state is
agent-owned. A collection becomes writable from the HTTP router only
when its definition sets `externallyWritable` — everything else stays
agent-only by default. (Every collection is always writable *by the
agent*; only an opt-in subset is writable *externally*, hence the name.)
2. **Authenticated, auditable writes.** Router writes require
authentication. The server stamps the authenticated principal into the
**change-event header** (provenance, outside the user payload), and the
client materializes it into a read-only **virtual column**
(`_principal`). A client can't spoof it via `value`.
3. **Schema-validated writes.** Each collection carries a schema; router
writes validate `value` server-side via the existing
`validateWriteEvent` before append.
## Design
**Runtime** (`@electric-ax/agents-runtime`)
- `CollectionDefinition.externallyWritable?: boolean`.
- At registration, writable collections are emitted as
`externally_writable_collections` (`{ type }` per collection).
- The entity stream DB materializes `headers.principal` into the fixed
`_principal` virtual column, mirroring `_timeline_order`, and strips it
(with `_seq`/`_timeline_order`) before client write-back.
- `createEntityTimelineQuery` accepts `customSources` —
consumer-provided sources unioned into the timeline query. The runtime
itself knows nothing about specific custom collections, and the agent's
LLM context never includes them.
**Server** (`@electric-ax/agents-server`)
- `externally_writable_collections` persisted as a jsonb column on
`entity_types` (migration 0016) and resolved via `getEffectiveSchemas`.
Legacy `principalColumn` in registration payloads is accepted and
ignored for version-skew tolerance.
- `EntityManager.writeCollection` gates on it (403 if not writable),
enforces entity-status/fork-lock, stamps `headers.principal = { url,
kind, id }`, validates `value`, and appends.
- `POST /:type/:instanceId/collections/:collection` exposes it (auth +
`write` permission, same middleware chain as `send`). Single POST,
`operation` in the body, 201 insert / 200 update·delete.
**Comments consumer** (`@electric-ax/agents` +
`@electric-ax/agents-server-ui`)
- `commentsCollection` (schema ported from #4529, minus
`from_principal`) declared as `state` on Horton and worker.
- The UI projects comments into the timeline by passing a comments
`customSources` entry to the timeline query (author resolved from
`_principal`); comments are a UI-level concern, hardcoded only in
`agents-server-ui`.
- The #4529 comments UI is cloned and re-sourced: writes via an
optimistic TanStack action backed by the authenticated
`/collections/comments` endpoint; reads `comment.from`.
## Test Plan
- [x] `@electric-ax/agents-runtime` — typecheck + 839 tests (incl.
principal virtual-column materialization, write-back stripping,
registration emit, timeline projection)
- [x] `@electric-ax/agents-server` — typecheck + suite (incl.
`writeCollection` 403/409 gating, principal-header stamping, schema
validation, `externally_writable_collections` jsonb round-trip)
- [x] `@electric-ax/agents` — typecheck + 60 tests (comments declared on
Horton/worker)
- [x] `@electric-ax/agents-server-ui` — typecheck + 88 tests (optimistic
comment write to `/collections/comments`, author from `_principal`)
- [ ] Manual: post a comment from the UI, confirm it syncs, right-aligns
for the author, and the principal is recorded in the change-event header
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent 8b1d39f commit 8bc630a
55 files changed
Lines changed: 3647 additions & 339 deletions
File tree
- .changeset
- packages
- agents-runtime
- src
- test
- agents-server-ui/src
- components
- views
- workspace
- hooks
- lib
- workspace
- agents-server
- drizzle
- meta
- src
- db
- routing
- test
- agents
- src/agents
- test
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
90 | 91 | | |
91 | 92 | | |
92 | 93 | | |
| 94 | + | |
93 | 95 | | |
94 | 96 | | |
95 | 97 | | |
| |||
104 | 106 | | |
105 | 107 | | |
106 | 108 | | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
107 | 114 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
207 | 208 | | |
208 | 209 | | |
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 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
210 | 325 | | |
211 | 326 | | |
212 | 327 | | |
| |||
413 | 528 | | |
414 | 529 | | |
415 | 530 | | |
416 | | - | |
417 | | - | |
418 | | - | |
419 | | - | |
420 | | - | |
421 | | - | |
422 | | - | |
423 | | - | |
424 | | - | |
425 | | - | |
426 | | - | |
427 | | - | |
428 | | - | |
429 | | - | |
430 | | - | |
431 | | - | |
432 | | - | |
433 | | - | |
434 | | - | |
435 | | - | |
436 | | - | |
437 | | - | |
438 | | - | |
439 | | - | |
440 | | - | |
441 | | - | |
442 | | - | |
443 | | - | |
444 | | - | |
445 | | - | |
446 | | - | |
447 | | - | |
448 | | - | |
449 | | - | |
450 | | - | |
451 | | - | |
452 | | - | |
453 | | - | |
454 | | - | |
455 | | - | |
456 | | - | |
457 | | - | |
458 | | - | |
459 | | - | |
460 | | - | |
461 | | - | |
462 | | - | |
463 | | - | |
464 | | - | |
465 | | - | |
466 | | - | |
467 | | - | |
468 | | - | |
469 | | - | |
470 | 531 | | |
471 | 532 | | |
472 | 533 | | |
473 | 534 | | |
474 | 535 | | |
475 | 536 | | |
476 | 537 | | |
477 | | - | |
478 | | - | |
479 | | - | |
480 | | - | |
481 | | - | |
482 | | - | |
483 | | - | |
484 | 538 | | |
485 | 539 | | |
486 | 540 | | |
487 | 541 | | |
488 | | - | |
489 | | - | |
490 | | - | |
491 | | - | |
492 | | - | |
493 | | - | |
494 | | - | |
495 | | - | |
496 | | - | |
497 | | - | |
498 | | - | |
499 | | - | |
500 | | - | |
501 | | - | |
502 | | - | |
503 | | - | |
504 | | - | |
505 | | - | |
506 | | - | |
507 | | - | |
508 | | - | |
509 | | - | |
510 | | - | |
511 | | - | |
512 | | - | |
513 | | - | |
514 | | - | |
515 | | - | |
516 | | - | |
517 | | - | |
518 | | - | |
519 | | - | |
| 542 | + | |
520 | 543 | | |
521 | 544 | | |
522 | 545 | | |
| |||
0 commit comments