@sixb/core 0.1.2 → 0.1.4
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.
- package/dist/actions/index.js +19 -17
- package/dist/actions/index.js.map +1 -1
- package/dist/actions/worker.js +4 -4
- package/dist/actions/worker.js.map +1 -1
- package/dist/agents/adapters.d.ts +11 -1
- package/dist/agents/adapters.d.ts.map +1 -1
- package/dist/agents/context-compaction.d.ts +40 -0
- package/dist/agents/context-compaction.d.ts.map +1 -0
- package/dist/agents/context.js +1 -1
- package/dist/agents/errors.d.ts +1 -1
- package/dist/agents/errors.d.ts.map +1 -1
- package/dist/agents/execution.d.ts +3 -0
- package/dist/agents/execution.d.ts.map +1 -1
- package/dist/agents/ids.d.ts +2 -0
- package/dist/agents/ids.d.ts.map +1 -1
- package/dist/agents/index.d.ts +10 -6
- package/dist/agents/index.d.ts.map +1 -1
- package/dist/agents/index.js +51 -35
- package/dist/agents/index.js.map +1 -1
- package/dist/agents/request.d.ts.map +1 -1
- package/dist/agents/streams.d.ts +17 -1
- package/dist/agents/streams.d.ts.map +1 -1
- package/dist/agents/streams.js +5 -3
- package/dist/agents/streams.js.map +1 -1
- package/dist/agents/thread-context-projection.d.ts +19 -0
- package/dist/agents/thread-context-projection.d.ts.map +1 -0
- package/dist/agents/tool-definition.d.ts.map +1 -1
- package/dist/agents/tool-result.d.ts +6 -0
- package/dist/agents/tool-result.d.ts.map +1 -0
- package/dist/agents/types.d.ts +46 -1
- package/dist/agents/types.d.ts.map +1 -1
- package/dist/agents/validation.d.ts +2 -2
- package/dist/agents/validation.d.ts.map +1 -1
- package/dist/auth/index.js +11 -5
- package/dist/auth/index.js.map +1 -1
- package/dist/authorization/index.js +6 -2
- package/dist/authorization/index.js.map +1 -1
- package/dist/bootstrap/index.js +33 -31
- package/dist/bootstrap/index.js.map +1 -1
- package/dist/{chunk-ys1nse2v.js → chunk-1wk20wn3.js} +438 -246
- package/dist/{chunk-ys1nse2v.js.map → chunk-1wk20wn3.js.map} +11 -11
- package/dist/{chunk-sa4340y1.js → chunk-1yz3x65y.js} +7 -7
- package/dist/{chunk-gqdd7508.js → chunk-4rh2m6zg.js} +1 -1
- package/dist/{chunk-fmx1sz2h.js → chunk-72q2y8hn.js} +3 -3
- package/dist/{chunk-fmx1sz2h.js.map → chunk-72q2y8hn.js.map} +4 -4
- package/dist/{chunk-nnyp53zt.js → chunk-7gw7mepb.js} +259 -9
- package/dist/chunk-7gw7mepb.js.map +14 -0
- package/dist/{chunk-nten49ta.js → chunk-819wnjz8.js} +11 -5
- package/dist/chunk-819wnjz8.js.map +12 -0
- package/dist/chunk-96swp2wd.js +47 -0
- package/dist/chunk-96swp2wd.js.map +10 -0
- package/dist/{chunk-4x43bkbp.js → chunk-a09jx26t.js} +1 -1
- package/dist/{chunk-st4xgxey.js → chunk-afjpa2hs.js} +205 -48
- package/dist/chunk-afjpa2hs.js.map +45 -0
- package/dist/{chunk-0mbx247x.js → chunk-c43qr6s0.js} +427 -99
- package/dist/chunk-c43qr6s0.js.map +46 -0
- package/dist/{chunk-zg9yryhz.js → chunk-cbkm2vam.js} +3 -3
- package/dist/{chunk-57zpb99z.js → chunk-dm72y8wt.js} +26 -3
- package/dist/chunk-dm72y8wt.js.map +10 -0
- package/dist/chunk-drjqm1aa.js +913 -0
- package/dist/chunk-drjqm1aa.js.map +15 -0
- package/dist/chunk-dysw7nsm.js.map +1 -1
- package/dist/{chunk-8ryen3p3.js → chunk-f6erf2vv.js} +1 -1
- package/dist/{chunk-34bqh420.js → chunk-f83ka50g.js} +188 -44
- package/dist/chunk-f83ka50g.js.map +23 -0
- package/dist/{chunk-bct7hmfc.js → chunk-g13869yf.js} +3 -3
- package/dist/{chunk-0k4avqr2.js → chunk-hbeh8j9w.js} +2 -2
- package/dist/{chunk-qvybnnpe.js → chunk-nzw7sm3b.js} +2 -2
- package/dist/{chunk-k9pts48t.js → chunk-pn8n8sba.js} +16 -15
- package/dist/{chunk-k9pts48t.js.map → chunk-pn8n8sba.js.map} +3 -3
- package/dist/{chunk-63te770v.js → chunk-qewtks41.js} +8 -8
- package/dist/{chunk-63te770v.js.map → chunk-qewtks41.js.map} +2 -2
- package/dist/{chunk-85j5ybfj.js → chunk-rdeagz6b.js} +2 -2
- package/dist/{chunk-85j5ybfj.js.map → chunk-rdeagz6b.js.map} +3 -3
- package/dist/chunk-rhrbz7d4.js +36 -0
- package/dist/chunk-rhrbz7d4.js.map +10 -0
- package/dist/{chunk-bh6k3nc6.js → chunk-rs1wbvy6.js} +1 -1
- package/dist/{chunk-88ppkr54.js → chunk-rte5sm3z.js} +26 -2
- package/dist/chunk-rte5sm3z.js.map +12 -0
- package/dist/{chunk-n8herkry.js → chunk-s31d0qmq.js} +57 -20
- package/dist/{chunk-n8herkry.js.map → chunk-s31d0qmq.js.map} +8 -7
- package/dist/{chunk-7pc8b818.js → chunk-sxwk41qa.js} +1 -1
- package/dist/{chunk-663csy96.js → chunk-t0jjkep3.js} +2 -1
- package/dist/chunk-t0jjkep3.js.map +10 -0
- package/dist/{chunk-c6qd89g5.js → chunk-tek1qwgm.js} +6 -6
- package/dist/{chunk-c6qd89g5.js.map → chunk-tek1qwgm.js.map} +1 -1
- package/dist/{chunk-jaj13078.js → chunk-tkdscva6.js} +3 -33
- package/dist/{chunk-jaj13078.js.map → chunk-tkdscva6.js.map} +3 -4
- package/dist/{chunk-5egfka8j.js → chunk-tycbzedm.js} +2 -2
- package/dist/{chunk-g9t3kdtx.js → chunk-x2yw73hy.js} +1 -1
- package/dist/{chunk-57r1cpxx.js → chunk-xc7hq65b.js} +3 -2
- package/dist/{chunk-57r1cpxx.js.map → chunk-xc7hq65b.js.map} +3 -3
- package/dist/chunk-xdf9yswn.js +133 -0
- package/dist/chunk-xdf9yswn.js.map +11 -0
- package/dist/execution/agent.js +29 -27
- package/dist/execution/agent.js.map +1 -1
- package/dist/execution/primitive.js +29 -27
- package/dist/execution/primitive.js.map +2 -2
- package/dist/execution/request.js +30 -28
- package/dist/execution/request.js.map +2 -2
- package/dist/http/index.js +1 -1
- package/dist/index.d.ts +6 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +60 -58
- package/dist/index.js.map +1 -1
- package/dist/lake-storage/types.d.ts +2 -1
- package/dist/lake-storage/types.d.ts.map +1 -1
- package/dist/materialization/model.d.ts +4 -0
- package/dist/materialization/model.d.ts.map +1 -1
- package/dist/materializer/edits/apply.d.ts +6 -1
- package/dist/materializer/edits/apply.d.ts.map +1 -1
- package/dist/materializer/edits/working-state.d.ts +2 -0
- package/dist/materializer/edits/working-state.d.ts.map +1 -1
- package/dist/materializer/effective/resolve.d.ts +3 -1
- package/dist/materializer/effective/resolve.d.ts.map +1 -1
- package/dist/materializer/execution/work-records.d.ts.map +1 -1
- package/dist/materializer/index.js +19 -17
- package/dist/materializer/index.js.map +1 -1
- package/dist/materializer/projections/replacement-plan.d.ts.map +1 -1
- package/dist/materializer/shared/normalize.d.ts.map +1 -1
- package/dist/models/index.d.ts +40 -0
- package/dist/models/index.d.ts.map +1 -0
- package/dist/objects/execution.d.ts +6 -1
- package/dist/objects/execution.d.ts.map +1 -1
- package/dist/objects/index.js +21 -19
- package/dist/objects/index.js.map +1 -1
- package/dist/objects/link/endpoints.d.ts.map +1 -1
- package/dist/objects/query/executor.d.ts +1 -0
- package/dist/objects/query/executor.d.ts.map +1 -1
- package/dist/objects/query/explain.d.ts +1 -0
- package/dist/objects/query/explain.d.ts.map +1 -1
- package/dist/objects/query/index.d.ts +3 -1
- package/dist/objects/query/index.d.ts.map +1 -1
- package/dist/objects/query/index.js +30 -5
- package/dist/objects/query/index.js.map +1 -1
- package/dist/objects/query/ir.d.ts +14 -1
- package/dist/objects/query/ir.d.ts.map +1 -1
- package/dist/objects/query/links.d.ts +25 -0
- package/dist/objects/query/links.d.ts.map +1 -0
- package/dist/objects/query/normalize.d.ts.map +1 -1
- package/dist/objects/query/planner.d.ts.map +1 -1
- package/dist/objects/query/validate.d.ts +1 -0
- package/dist/objects/query/validate.d.ts.map +1 -1
- package/dist/pipelines/index.js +16 -15
- package/dist/pipelines/index.js.map +1 -1
- package/dist/projections/builders.d.ts +52 -10
- package/dist/projections/builders.d.ts.map +1 -1
- package/dist/projections/index.d.ts +2 -1
- package/dist/projections/index.d.ts.map +1 -1
- package/dist/projections/internal.d.ts +4 -1
- package/dist/projections/internal.d.ts.map +1 -1
- package/dist/projections/internal.js +22 -17
- package/dist/projections/internal.js.map +1 -1
- package/dist/projections/run-dispatch.d.ts +1 -0
- package/dist/projections/run-dispatch.d.ts.map +1 -1
- package/dist/projections/timestamp.d.ts +3 -0
- package/dist/projections/timestamp.d.ts.map +1 -0
- package/dist/projections/types.d.ts +21 -9
- package/dist/projections/types.d.ts.map +1 -1
- package/dist/projections/validation.d.ts +6 -5
- package/dist/projections/validation.d.ts.map +1 -1
- package/dist/query.d.ts +2 -1
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +2 -2
- package/dist/queues/index.d.ts +1 -1
- package/dist/queues/index.d.ts.map +1 -1
- package/dist/queues/types.d.ts +7 -0
- package/dist/queues/types.d.ts.map +1 -1
- package/dist/runtime/ai-cost.d.ts +8 -0
- package/dist/runtime/ai-cost.d.ts.map +1 -0
- package/dist/runtime/create.d.ts +3 -0
- package/dist/runtime/create.d.ts.map +1 -1
- package/dist/runtime/definitions.d.ts +3 -0
- package/dist/runtime/definitions.d.ts.map +1 -1
- package/dist/runtime/host.d.ts +2 -0
- package/dist/runtime/host.d.ts.map +1 -1
- package/dist/runtime/resolve-definitions.d.ts +2 -0
- package/dist/runtime/resolve-definitions.d.ts.map +1 -1
- package/dist/storage/agents/in-memory.d.ts +22 -4
- package/dist/storage/agents/in-memory.d.ts.map +1 -1
- package/dist/storage/agents/index.d.ts +1 -1
- package/dist/storage/agents/index.d.ts.map +1 -1
- package/dist/storage/agents/provider.d.ts +31 -0
- package/dist/storage/agents/provider.d.ts.map +1 -1
- package/dist/storage/agents/provider.js +22 -7
- package/dist/storage/agents/provider.js.map +1 -1
- package/dist/storage/agents/types.d.ts +75 -4
- package/dist/storage/agents/types.d.ts.map +1 -1
- package/dist/storage/ai-cost/analytics.d.ts +61 -0
- package/dist/storage/ai-cost/analytics.d.ts.map +1 -0
- package/dist/storage/ai-cost/codec.d.ts +14 -0
- package/dist/storage/ai-cost/codec.d.ts.map +1 -0
- package/dist/storage/ai-cost/errors.d.ts +7 -0
- package/dist/storage/ai-cost/errors.d.ts.map +1 -0
- package/dist/storage/ai-cost/in-memory.d.ts +32 -0
- package/dist/storage/ai-cost/in-memory.d.ts.map +1 -0
- package/dist/storage/ai-cost/index.d.ts +6 -0
- package/dist/storage/ai-cost/index.d.ts.map +1 -0
- package/dist/storage/ai-cost/provider.d.ts +7 -0
- package/dist/storage/ai-cost/provider.d.ts.map +1 -0
- package/dist/storage/ai-cost/provider.js +132 -0
- package/dist/storage/ai-cost/provider.js.map +10 -0
- package/dist/storage/ai-cost/types.d.ts +154 -0
- package/dist/storage/ai-cost/types.d.ts.map +1 -0
- package/dist/storage/ai-cost/validation.d.ts +7 -0
- package/dist/storage/ai-cost/validation.d.ts.map +1 -0
- package/dist/storage/ai-usage/in-memory.d.ts +2 -1
- package/dist/storage/ai-usage/in-memory.d.ts.map +1 -1
- package/dist/storage/ai-usage/index.d.ts +1 -1
- package/dist/storage/ai-usage/index.d.ts.map +1 -1
- package/dist/storage/ai-usage/types.d.ts +6 -0
- package/dist/storage/ai-usage/types.d.ts.map +1 -1
- package/dist/storage/in-memory/index.d.ts +5 -0
- package/dist/storage/in-memory/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +6 -3
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/index.js +33 -21
- package/dist/storage/index.js.map +1 -1
- package/dist/storage/objects/in-memory.d.ts +6 -3
- package/dist/storage/objects/in-memory.d.ts.map +1 -1
- package/dist/storage/objects/index.d.ts +3 -1
- package/dist/storage/objects/index.d.ts.map +1 -1
- package/dist/storage/objects/keys.d.ts +16 -0
- package/dist/storage/objects/keys.d.ts.map +1 -0
- package/dist/storage/objects/types.d.ts +33 -6
- package/dist/storage/objects/types.d.ts.map +1 -1
- package/dist/storage/ontology/in-memory/materializations.d.ts.map +1 -1
- package/dist/storage/ontology/materializations.d.ts +3 -0
- package/dist/storage/ontology/materializations.d.ts.map +1 -1
- package/dist/storage/ontology/provider.js +1 -1
- package/dist/storage/operation-scope.d.ts.map +1 -1
- package/dist/storage/operation-scope.js +1 -1
- package/dist/storage/types.d.ts +2 -0
- package/dist/storage/types.d.ts.map +1 -1
- package/dist/storage/workflow-runs/provider.js +10 -5
- package/dist/storage/workflow-runs/provider.js.map +1 -1
- package/dist/syncs/index.js +16 -15
- package/dist/syncs/index.js.map +1 -1
- package/dist/testing/agent-storage-contract.d.ts +3 -2
- package/dist/testing/agent-storage-contract.d.ts.map +1 -1
- package/dist/testing/ai-cost-storage-contract.d.ts +12 -0
- package/dist/testing/ai-cost-storage-contract.d.ts.map +1 -0
- package/dist/testing/ai-usage-storage-contract.d.ts.map +1 -1
- package/dist/testing/effective-storage-contract.d.ts +1 -1
- package/dist/testing/effective-storage-contract.d.ts.map +1 -1
- package/dist/testing/index.d.ts +1 -0
- package/dist/testing/index.d.ts.map +1 -1
- package/dist/testing/index.js +2525 -1480
- package/dist/testing/index.js.map +10 -9
- package/dist/testing/lake-storage-contract.d.ts.map +1 -1
- package/dist/testing/materializer-storage-contract.d.ts.map +1 -1
- package/dist/testing/object-query-contract.d.ts +1 -1
- package/dist/testing/object-query-contract.d.ts.map +1 -1
- package/dist/webhooks/internal.js +14 -13
- package/dist/webhooks/internal.js.map +2 -2
- package/dist/workers/index.js +12 -2
- package/dist/workers/index.js.map +3 -3
- package/dist/workers/queue-worker.d.ts +2 -0
- package/dist/workers/queue-worker.d.ts.map +1 -1
- package/dist/workflows/execution.d.ts +3 -0
- package/dist/workflows/execution.d.ts.map +1 -1
- package/dist/workflows/index.js +30 -28
- package/dist/workflows/index.js.map +1 -1
- package/package.json +7 -1
- package/src/agents/adapters.ts +64 -23
- package/src/agents/builders.ts +3 -3
- package/src/agents/context-compaction.ts +261 -0
- package/src/agents/errors.ts +2 -2
- package/src/agents/execution.ts +14 -2
- package/src/agents/ids.ts +5 -0
- package/src/agents/index.ts +32 -6
- package/src/agents/request.ts +5 -0
- package/src/agents/streams.ts +56 -0
- package/src/agents/thread-context-projection.ts +93 -0
- package/src/agents/tool-definition.ts +17 -2
- package/src/agents/tool-result.ts +61 -0
- package/src/agents/types.ts +57 -1
- package/src/agents/validation.ts +36 -6
- package/src/http/api-routes.ts +7 -7
- package/src/index.ts +27 -0
- package/src/lake-storage/types.ts +2 -1
- package/src/materialization/model.ts +4 -0
- package/src/materializer/edits/apply.ts +55 -2
- package/src/materializer/edits/operations.ts +8 -1
- package/src/materializer/edits/working-state.ts +7 -0
- package/src/materializer/effective/resolve.ts +78 -9
- package/src/materializer/execution/work-records.ts +3 -1
- package/src/materializer/projections/entry-validator.ts +23 -1
- package/src/materializer/projections/replacement-plan.ts +2 -0
- package/src/materializer/shared/normalize.ts +16 -0
- package/src/materializer/telemetry/append.ts +33 -16
- package/src/models/index.ts +71 -0
- package/src/objects/execution.ts +20 -0
- package/src/objects/link/endpoints.ts +2 -1
- package/src/objects/query/executor.ts +42 -11
- package/src/objects/query/explain.ts +11 -0
- package/src/objects/query/index.ts +6 -0
- package/src/objects/query/ir.ts +16 -0
- package/src/objects/query/links.ts +308 -0
- package/src/objects/query/normalize.ts +40 -1
- package/src/objects/query/planner.ts +5 -0
- package/src/objects/query/validate.ts +58 -2
- package/src/projections/builders.ts +320 -50
- package/src/projections/index.ts +7 -0
- package/src/projections/internal.ts +4 -1
- package/src/projections/ownership.ts +5 -5
- package/src/projections/registry.ts +16 -3
- package/src/projections/revision.ts +35 -5
- package/src/projections/run-dispatch.ts +10 -1
- package/src/projections/timestamp.ts +66 -0
- package/src/projections/types.ts +20 -9
- package/src/projections/validation.ts +85 -19
- package/src/query.ts +2 -0
- package/src/queues/index.ts +1 -0
- package/src/queues/types.ts +8 -0
- package/src/runtime/ai-cost.ts +15 -0
- package/src/runtime/create.ts +4 -0
- package/src/runtime/definitions.ts +3 -0
- package/src/runtime/host.ts +2 -0
- package/src/runtime/resolve-definitions.ts +28 -0
- package/src/storage/agents/in-memory.ts +186 -4
- package/src/storage/agents/index.ts +5 -0
- package/src/storage/agents/provider.ts +225 -0
- package/src/storage/agents/types.ts +82 -3
- package/src/storage/ai-cost/analytics.ts +602 -0
- package/src/storage/ai-cost/codec.ts +136 -0
- package/src/storage/ai-cost/errors.ts +12 -0
- package/src/storage/ai-cost/in-memory.ts +164 -0
- package/src/storage/ai-cost/index.ts +35 -0
- package/src/storage/ai-cost/provider.ts +20 -0
- package/src/storage/ai-cost/types.ts +193 -0
- package/src/storage/ai-cost/validation.ts +337 -0
- package/src/storage/ai-usage/in-memory.ts +23 -0
- package/src/storage/ai-usage/index.ts +1 -0
- package/src/storage/ai-usage/types.ts +10 -0
- package/src/storage/in-memory/index.ts +50 -0
- package/src/storage/index.ts +48 -0
- package/src/storage/objects/in-memory.ts +103 -16
- package/src/storage/objects/index.ts +9 -0
- package/src/storage/objects/keys.ts +36 -0
- package/src/storage/objects/types.ts +34 -6
- package/src/storage/ontology/in-memory/materializations.ts +1 -0
- package/src/storage/ontology/materializations.ts +3 -0
- package/src/storage/ontology/provider-header-validation.ts +2 -2
- package/src/storage/operation-scope.ts +1 -0
- package/src/storage/types.ts +2 -0
- package/src/testing/agent-storage-contract.ts +353 -2
- package/src/testing/ai-cost-storage-contract.ts +320 -0
- package/src/testing/ai-usage-storage-contract.ts +35 -0
- package/src/testing/effective-storage-contract.ts +194 -4
- package/src/testing/index.ts +5 -0
- package/src/testing/lake-storage-contract.ts +56 -0
- package/src/testing/materializer-storage-contract.ts +120 -2
- package/src/testing/object-query-contract.ts +208 -1
- package/src/workers/queue-worker.ts +14 -1
- package/src/workflows/execution.ts +31 -10
- package/dist/agents/prompt.d.ts +0 -15
- package/dist/agents/prompt.d.ts.map +0 -1
- package/dist/chunk-0mbx247x.js.map +0 -43
- package/dist/chunk-34bqh420.js.map +0 -23
- package/dist/chunk-57zpb99z.js.map +0 -10
- package/dist/chunk-663csy96.js.map +0 -10
- package/dist/chunk-6wetht33.js +0 -89
- package/dist/chunk-6wetht33.js.map +0 -10
- package/dist/chunk-88ppkr54.js.map +0 -12
- package/dist/chunk-nnyp53zt.js.map +0 -13
- package/dist/chunk-nten49ta.js.map +0 -12
- package/dist/chunk-st4xgxey.js.map +0 -45
- package/dist/chunk-vp1vc6ee.js +0 -35
- package/dist/chunk-vp1vc6ee.js.map +0 -11
- package/src/agents/prompt.ts +0 -57
- /package/dist/{chunk-sa4340y1.js.map → chunk-1yz3x65y.js.map} +0 -0
- /package/dist/{chunk-gqdd7508.js.map → chunk-4rh2m6zg.js.map} +0 -0
- /package/dist/{chunk-4x43bkbp.js.map → chunk-a09jx26t.js.map} +0 -0
- /package/dist/{chunk-zg9yryhz.js.map → chunk-cbkm2vam.js.map} +0 -0
- /package/dist/{chunk-8ryen3p3.js.map → chunk-f6erf2vv.js.map} +0 -0
- /package/dist/{chunk-bct7hmfc.js.map → chunk-g13869yf.js.map} +0 -0
- /package/dist/{chunk-0k4avqr2.js.map → chunk-hbeh8j9w.js.map} +0 -0
- /package/dist/{chunk-qvybnnpe.js.map → chunk-nzw7sm3b.js.map} +0 -0
- /package/dist/{chunk-bh6k3nc6.js.map → chunk-rs1wbvy6.js.map} +0 -0
- /package/dist/{chunk-7pc8b818.js.map → chunk-sxwk41qa.js.map} +0 -0
- /package/dist/{chunk-5egfka8j.js.map → chunk-tycbzedm.js.map} +0 -0
- /package/dist/{chunk-g9t3kdtx.js.map → chunk-x2yw73hy.js.map} +0 -0
|
@@ -5,8 +5,17 @@ import { parseSixbFailure } from "../../errors/internal"
|
|
|
5
5
|
import type { SixbFailure } from "../../errors/types"
|
|
6
6
|
import type { ExecutionStorage } from "../executions"
|
|
7
7
|
import { AgentStorageError } from "./errors"
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
agentContextCheckpointMatchesCreateInput,
|
|
10
|
+
assertAgentContextCheckpointAnchors,
|
|
11
|
+
assertAgentContextCheckpointAuthority,
|
|
12
|
+
assertAgentContextCheckpointReplayState,
|
|
13
|
+
assertAgentRunExecution,
|
|
14
|
+
assertCreateAgentContextCheckpointInput,
|
|
15
|
+
} from "./provider"
|
|
9
16
|
import type {
|
|
17
|
+
AgentContextCheckpointRecord,
|
|
18
|
+
AgentContextCheckpointStore,
|
|
10
19
|
AgentMessageRecord,
|
|
11
20
|
AgentMessageStore,
|
|
12
21
|
AgentRunFailureCode,
|
|
@@ -17,8 +26,10 @@ import type {
|
|
|
17
26
|
AgentThreadStore,
|
|
18
27
|
AppendAgentMessageInput,
|
|
19
28
|
ConfirmAgentRunExecutionOwnershipInput,
|
|
29
|
+
CreateAgentContextCheckpointInput,
|
|
20
30
|
CreateAgentRunInput,
|
|
21
31
|
CreateAgentThreadInput,
|
|
32
|
+
DeleteAgentMessagesByRunInput,
|
|
22
33
|
FinishAgentRunInput,
|
|
23
34
|
FinishQueuedAgentRunInput,
|
|
24
35
|
ListAgentMessagesInput,
|
|
@@ -77,21 +88,23 @@ function applyWindow<T>(rows: T[], offset = 0, limit?: number): Window<T> {
|
|
|
77
88
|
}
|
|
78
89
|
|
|
79
90
|
/**
|
|
80
|
-
* Shared in-memory state behind the
|
|
91
|
+
* Shared in-memory state behind the four sub-stores. They are tiered for ergonomics
|
|
81
92
|
* (`storage.runs.create(...)`), but several operations span tables — creating/finishing a run
|
|
82
|
-
* updates the thread anchor, appending a message bumps thread stats
|
|
83
|
-
* same maps through this object.
|
|
93
|
+
* updates the thread anchor, appending a message bumps thread stats, and checkpoint creation reads
|
|
94
|
+
* run/thread/message anchors — so they all read and write the same maps through this object.
|
|
84
95
|
*/
|
|
85
96
|
interface AgentStoreState {
|
|
86
97
|
readonly threads: Map<string, AgentThreadRecord>
|
|
87
98
|
readonly runs: Map<string, AgentRunRecord>
|
|
88
99
|
readonly messages: Map<string, AgentMessageRecord>
|
|
100
|
+
readonly checkpoints: Map<string, AgentContextCheckpointRecord>
|
|
89
101
|
}
|
|
90
102
|
|
|
91
103
|
export interface InMemoryAgentStorageSnapshot {
|
|
92
104
|
readonly threads: Map<string, AgentThreadRecord>
|
|
93
105
|
readonly runs: Map<string, AgentRunRecord>
|
|
94
106
|
readonly messages: Map<string, AgentMessageRecord>
|
|
107
|
+
readonly checkpoints: Map<string, AgentContextCheckpointRecord>
|
|
95
108
|
}
|
|
96
109
|
|
|
97
110
|
// ── threads ───────────────────────────────────────────────────────────────────────────────────
|
|
@@ -466,6 +479,50 @@ class InMemoryAgentMessageStore implements AgentMessageStore {
|
|
|
466
479
|
return clone(message)
|
|
467
480
|
}
|
|
468
481
|
|
|
482
|
+
async deleteByRunId(input: DeleteAgentMessagesByRunInput): Promise<number> {
|
|
483
|
+
const threadKey = key(input.projectId, input.threadId)
|
|
484
|
+
const thread = this.state.threads.get(threadKey)
|
|
485
|
+
if (!thread) {
|
|
486
|
+
throw new AgentStorageError(
|
|
487
|
+
"thread_not_found",
|
|
488
|
+
`[Sixb] Agent thread '${input.threadId}' not found for project '${input.projectId}'.`
|
|
489
|
+
)
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
const messages = [...this.state.messages.entries()].filter(
|
|
493
|
+
([, message]) =>
|
|
494
|
+
message.projectId === input.projectId &&
|
|
495
|
+
message.threadId === input.threadId &&
|
|
496
|
+
message.runId === input.runId
|
|
497
|
+
)
|
|
498
|
+
if (messages.length === 0) {
|
|
499
|
+
return 0
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
for (const [messageKey] of messages) {
|
|
503
|
+
this.state.messages.delete(messageKey)
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
const remainingMessages = [...this.state.messages.values()]
|
|
507
|
+
.filter(
|
|
508
|
+
(message) => message.projectId === input.projectId && message.threadId === input.threadId
|
|
509
|
+
)
|
|
510
|
+
.sort((left, right) => right.seq - left.seq)
|
|
511
|
+
const latestMessage = remainingMessages[0]
|
|
512
|
+
const updatedAt = new Date()
|
|
513
|
+
this.state.threads.set(
|
|
514
|
+
threadKey,
|
|
515
|
+
clone({
|
|
516
|
+
...thread,
|
|
517
|
+
messageCount: remainingMessages.length,
|
|
518
|
+
lastMessageAt: latestMessage?.createdAt,
|
|
519
|
+
updatedAt,
|
|
520
|
+
})
|
|
521
|
+
)
|
|
522
|
+
|
|
523
|
+
return messages.length
|
|
524
|
+
}
|
|
525
|
+
|
|
469
526
|
async getById(params: { projectId: string; id: string }): Promise<AgentMessageRecord | null> {
|
|
470
527
|
const record = this.state.messages.get(key(params.projectId, params.id))
|
|
471
528
|
return record ? clone(record) : null
|
|
@@ -478,6 +535,7 @@ class InMemoryAgentMessageStore implements AgentMessageStore {
|
|
|
478
535
|
const filtered = [...this.state.messages.values()]
|
|
479
536
|
.filter((message) => message.projectId === input.projectId)
|
|
480
537
|
.filter((message) => message.threadId === input.threadId)
|
|
538
|
+
.filter((message) => (input.afterSeq === undefined ? true : message.seq > input.afterSeq))
|
|
481
539
|
.filter((message) => (roles ? roles.has(message.role) : true))
|
|
482
540
|
.sort((a, b) => (order === "asc" ? a.seq - b.seq : b.seq - a.seq))
|
|
483
541
|
|
|
@@ -496,21 +554,143 @@ class InMemoryAgentMessageStore implements AgentMessageStore {
|
|
|
496
554
|
}
|
|
497
555
|
}
|
|
498
556
|
|
|
557
|
+
// ── context checkpoints ──────────────────────────────────────────────────────────────────────
|
|
558
|
+
|
|
559
|
+
class InMemoryAgentContextCheckpointStore implements AgentContextCheckpointStore {
|
|
560
|
+
constructor(private readonly state: AgentStoreState) {}
|
|
561
|
+
|
|
562
|
+
async create(input: CreateAgentContextCheckpointInput): Promise<AgentContextCheckpointRecord> {
|
|
563
|
+
assertCreateAgentContextCheckpointInput(input)
|
|
564
|
+
|
|
565
|
+
// No `await` between authority checks and append: this is one in-memory critical section.
|
|
566
|
+
const run = this.state.runs.get(key(input.projectId, input.createdByRunId)) ?? null
|
|
567
|
+
const thread = this.state.threads.get(key(input.projectId, input.threadId)) ?? null
|
|
568
|
+
assertAgentContextCheckpointAuthority({ create: input, run, thread })
|
|
569
|
+
|
|
570
|
+
const messages = [...this.state.messages.values()].filter(
|
|
571
|
+
(message) => message.projectId === input.projectId && message.threadId === input.threadId
|
|
572
|
+
)
|
|
573
|
+
const headSeq = messages.reduce((head, message) => Math.max(head, message.seq), 0)
|
|
574
|
+
const latest = this.findLatest(input.projectId, input.threadId)
|
|
575
|
+
|
|
576
|
+
const existingForRun = [...this.state.checkpoints.values()].find(
|
|
577
|
+
(checkpoint) =>
|
|
578
|
+
checkpoint.projectId === input.projectId &&
|
|
579
|
+
checkpoint.createdByRunId === input.createdByRunId
|
|
580
|
+
)
|
|
581
|
+
if (existingForRun) {
|
|
582
|
+
if (agentContextCheckpointMatchesCreateInput(existingForRun, input)) {
|
|
583
|
+
assertAgentContextCheckpointReplayState({
|
|
584
|
+
create: input,
|
|
585
|
+
existing: existingForRun,
|
|
586
|
+
latest,
|
|
587
|
+
headSeq,
|
|
588
|
+
})
|
|
589
|
+
return clone(existingForRun)
|
|
590
|
+
}
|
|
591
|
+
throw new AgentStorageError(
|
|
592
|
+
"invalid_state",
|
|
593
|
+
`[Sixb] Agent run '${input.createdByRunId}' already created a different context checkpoint.`
|
|
594
|
+
)
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
if (this.state.checkpoints.has(key(input.projectId, input.id))) {
|
|
598
|
+
throw new AgentStorageError(
|
|
599
|
+
"duplicate_id",
|
|
600
|
+
`[Sixb] Agent context checkpoint '${input.id}' already exists for project '${input.projectId}'.`
|
|
601
|
+
)
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
const firstRetained =
|
|
605
|
+
messages
|
|
606
|
+
.filter((message) => message.seq > input.summarizedThroughSeq)
|
|
607
|
+
.sort((left, right) => left.seq - right.seq)[0] ?? null
|
|
608
|
+
assertAgentContextCheckpointAnchors({
|
|
609
|
+
create: input,
|
|
610
|
+
latest,
|
|
611
|
+
headSeq,
|
|
612
|
+
firstRetained,
|
|
613
|
+
})
|
|
614
|
+
|
|
615
|
+
const record: AgentContextCheckpointRecord = {
|
|
616
|
+
id: input.id,
|
|
617
|
+
projectId: input.projectId,
|
|
618
|
+
threadId: input.threadId,
|
|
619
|
+
createdByRunId: input.createdByRunId,
|
|
620
|
+
...(input.expectedPreviousCheckpointId === null
|
|
621
|
+
? {}
|
|
622
|
+
: { previousCheckpointId: input.expectedPreviousCheckpointId }),
|
|
623
|
+
reason: input.reason,
|
|
624
|
+
summary: input.summary,
|
|
625
|
+
summaryFormatVersion: input.summaryFormatVersion,
|
|
626
|
+
summarizedThroughSeq: input.summarizedThroughSeq,
|
|
627
|
+
observedHeadSeq: input.observedHeadSeq,
|
|
628
|
+
estimatedInputTokensBefore: input.estimatedInputTokensBefore,
|
|
629
|
+
estimatedInputTokensAfter: input.estimatedInputTokensAfter,
|
|
630
|
+
summaryModelId: input.summaryModelId,
|
|
631
|
+
createdAt: new Date(input.createdAt ?? new Date()),
|
|
632
|
+
}
|
|
633
|
+
this.state.checkpoints.set(key(input.projectId, input.id), clone(record))
|
|
634
|
+
return clone(record)
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
async getLatest(input: {
|
|
638
|
+
readonly projectId: string
|
|
639
|
+
readonly threadId: string
|
|
640
|
+
}): Promise<AgentContextCheckpointRecord | null> {
|
|
641
|
+
const record = this.findLatest(input.projectId, input.threadId)
|
|
642
|
+
return record ? clone(record) : null
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
async getByRunIds(input: {
|
|
646
|
+
readonly projectId: string
|
|
647
|
+
readonly runIds: readonly string[]
|
|
648
|
+
}): Promise<readonly AgentContextCheckpointRecord[]> {
|
|
649
|
+
const byRunId = new Map(
|
|
650
|
+
[...this.state.checkpoints.values()]
|
|
651
|
+
.filter((checkpoint) => checkpoint.projectId === input.projectId)
|
|
652
|
+
.map((checkpoint) => [checkpoint.createdByRunId, checkpoint] as const)
|
|
653
|
+
)
|
|
654
|
+
return [...new Set(input.runIds)].flatMap((runId) => {
|
|
655
|
+
const checkpoint = byRunId.get(runId)
|
|
656
|
+
return checkpoint ? [clone(checkpoint)] : []
|
|
657
|
+
})
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
private findLatest(projectId: string, threadId: string): AgentContextCheckpointRecord | null {
|
|
661
|
+
return (
|
|
662
|
+
[...this.state.checkpoints.values()]
|
|
663
|
+
.filter(
|
|
664
|
+
(checkpoint) => checkpoint.projectId === projectId && checkpoint.threadId === threadId
|
|
665
|
+
)
|
|
666
|
+
.sort(
|
|
667
|
+
(left, right) =>
|
|
668
|
+
right.summarizedThroughSeq - left.summarizedThroughSeq ||
|
|
669
|
+
right.createdAt.getTime() - left.createdAt.getTime() ||
|
|
670
|
+
right.id.localeCompare(left.id)
|
|
671
|
+
)[0] ?? null
|
|
672
|
+
)
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
|
|
499
676
|
export class InMemoryAgentStorage implements AgentStorage {
|
|
500
677
|
private readonly state: AgentStoreState = {
|
|
501
678
|
threads: new Map(),
|
|
502
679
|
runs: new Map(),
|
|
503
680
|
messages: new Map(),
|
|
681
|
+
checkpoints: new Map(),
|
|
504
682
|
}
|
|
505
683
|
|
|
506
684
|
readonly threads: InMemoryAgentThreadStore
|
|
507
685
|
readonly runs: InMemoryAgentRunStore
|
|
508
686
|
readonly messages: InMemoryAgentMessageStore
|
|
687
|
+
readonly checkpoints: InMemoryAgentContextCheckpointStore
|
|
509
688
|
|
|
510
689
|
constructor(executions: ExecutionStorage) {
|
|
511
690
|
this.threads = new InMemoryAgentThreadStore(this.state)
|
|
512
691
|
this.runs = new InMemoryAgentRunStore(this.state, executions)
|
|
513
692
|
this.messages = new InMemoryAgentMessageStore(this.state)
|
|
693
|
+
this.checkpoints = new InMemoryAgentContextCheckpointStore(this.state)
|
|
514
694
|
}
|
|
515
695
|
|
|
516
696
|
snapshot(): InMemoryAgentStorageSnapshot {
|
|
@@ -518,6 +698,7 @@ export class InMemoryAgentStorage implements AgentStorage {
|
|
|
518
698
|
threads: structuredClone(this.state.threads),
|
|
519
699
|
runs: structuredClone(this.state.runs),
|
|
520
700
|
messages: structuredClone(this.state.messages),
|
|
701
|
+
checkpoints: structuredClone(this.state.checkpoints),
|
|
521
702
|
}
|
|
522
703
|
}
|
|
523
704
|
|
|
@@ -525,6 +706,7 @@ export class InMemoryAgentStorage implements AgentStorage {
|
|
|
525
706
|
replace(this.state.threads, snapshot.threads)
|
|
526
707
|
replace(this.state.runs, snapshot.runs)
|
|
527
708
|
replace(this.state.messages, snapshot.messages)
|
|
709
|
+
replace(this.state.checkpoints, snapshot.checkpoints)
|
|
528
710
|
}
|
|
529
711
|
}
|
|
530
712
|
|
|
@@ -3,6 +3,9 @@ export { AgentStorageError } from "./errors"
|
|
|
3
3
|
export type { InMemoryAgentStorageSnapshot } from "./in-memory"
|
|
4
4
|
export { InMemoryAgentStorage } from "./in-memory"
|
|
5
5
|
export type {
|
|
6
|
+
AgentContextCheckpointReason,
|
|
7
|
+
AgentContextCheckpointRecord,
|
|
8
|
+
AgentContextCheckpointStore,
|
|
6
9
|
AgentExecutionStatus,
|
|
7
10
|
AgentMessageRecord,
|
|
8
11
|
AgentMessageRole,
|
|
@@ -22,8 +25,10 @@ export type {
|
|
|
22
25
|
AgentThreadStore,
|
|
23
26
|
AppendAgentMessageInput,
|
|
24
27
|
ConfirmAgentRunExecutionOwnershipInput,
|
|
28
|
+
CreateAgentContextCheckpointInput,
|
|
25
29
|
CreateAgentRunInput,
|
|
26
30
|
CreateAgentThreadInput,
|
|
31
|
+
DeleteAgentMessagesByRunInput,
|
|
27
32
|
FinishAgentRunInput,
|
|
28
33
|
FinishQueuedAgentRunInput,
|
|
29
34
|
ListAgentMessagesInput,
|
|
@@ -2,6 +2,13 @@ import { agentServiceAccountId } from "../../agents/authority"
|
|
|
2
2
|
import type { ExecutionStorage } from "../executions"
|
|
3
3
|
import { findAgentRunExecution } from "../executions/run-link"
|
|
4
4
|
import { AgentStorageError } from "./errors"
|
|
5
|
+
import type {
|
|
6
|
+
AgentContextCheckpointRecord,
|
|
7
|
+
AgentMessageRecord,
|
|
8
|
+
AgentRunRecord,
|
|
9
|
+
AgentThreadRecord,
|
|
10
|
+
CreateAgentContextCheckpointInput,
|
|
11
|
+
} from "./types"
|
|
5
12
|
|
|
6
13
|
/** Validate the semantic link between a conversational Agent run and its immutable execution. */
|
|
7
14
|
export async function assertAgentRunExecution(input: {
|
|
@@ -25,3 +32,221 @@ export async function assertAgentRunExecution(input: {
|
|
|
25
32
|
)
|
|
26
33
|
}
|
|
27
34
|
}
|
|
35
|
+
|
|
36
|
+
/** Validate provider-independent checkpoint fields before entering a storage critical section. */
|
|
37
|
+
export function assertCreateAgentContextCheckpointInput(
|
|
38
|
+
input: CreateAgentContextCheckpointInput,
|
|
39
|
+
prefix = "Sixb"
|
|
40
|
+
): void {
|
|
41
|
+
for (const [name, value] of [
|
|
42
|
+
["id", input.id],
|
|
43
|
+
["projectId", input.projectId],
|
|
44
|
+
["threadId", input.threadId],
|
|
45
|
+
["createdByRunId", input.createdByRunId],
|
|
46
|
+
["executionToken", input.executionToken],
|
|
47
|
+
["summary", input.summary],
|
|
48
|
+
["summaryModelId", input.summaryModelId],
|
|
49
|
+
] as const) {
|
|
50
|
+
if (value.trim().length === 0) {
|
|
51
|
+
throw new AgentStorageError(
|
|
52
|
+
"invalid_input",
|
|
53
|
+
`[${prefix}] Agent context checkpoint '${name}' must not be empty.`
|
|
54
|
+
)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (input.summaryFormatVersion !== 1) {
|
|
59
|
+
throw new AgentStorageError(
|
|
60
|
+
"invalid_input",
|
|
61
|
+
`[${prefix}] Agent context checkpoint summary format version must be 1.`
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
if (input.reason !== "threshold" && input.reason !== "overflow") {
|
|
65
|
+
throw new AgentStorageError(
|
|
66
|
+
"invalid_input",
|
|
67
|
+
`[${prefix}] Agent context checkpoint reason must be 'threshold' or 'overflow'.`
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
if (
|
|
71
|
+
input.expectedPreviousCheckpointId !== null &&
|
|
72
|
+
input.expectedPreviousCheckpointId.trim().length === 0
|
|
73
|
+
) {
|
|
74
|
+
throw new AgentStorageError(
|
|
75
|
+
"invalid_input",
|
|
76
|
+
`[${prefix}] Agent context checkpoint expected previous id must not be empty.`
|
|
77
|
+
)
|
|
78
|
+
}
|
|
79
|
+
if (input.createdAt && !Number.isFinite(input.createdAt.getTime())) {
|
|
80
|
+
throw new AgentStorageError(
|
|
81
|
+
"invalid_input",
|
|
82
|
+
`[${prefix}] Agent context checkpoint createdAt must be a valid date.`
|
|
83
|
+
)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
for (const [name, value, minimum] of [
|
|
87
|
+
["expectedHeadSeq", input.expectedHeadSeq, 1],
|
|
88
|
+
["summarizedThroughSeq", input.summarizedThroughSeq, 1],
|
|
89
|
+
["observedHeadSeq", input.observedHeadSeq, 1],
|
|
90
|
+
["estimatedInputTokensBefore", input.estimatedInputTokensBefore, 0],
|
|
91
|
+
["estimatedInputTokensAfter", input.estimatedInputTokensAfter, 0],
|
|
92
|
+
] as const) {
|
|
93
|
+
if (!Number.isSafeInteger(value) || value < minimum) {
|
|
94
|
+
throw new AgentStorageError(
|
|
95
|
+
"invalid_input",
|
|
96
|
+
`[${prefix}] Agent context checkpoint '${name}' must be a safe integer greater than or equal to ${minimum}.`
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (input.observedHeadSeq !== input.expectedHeadSeq) {
|
|
102
|
+
throw new AgentStorageError(
|
|
103
|
+
"invalid_input",
|
|
104
|
+
`[${prefix}] Agent context checkpoint observed head must equal its expected message head.`
|
|
105
|
+
)
|
|
106
|
+
}
|
|
107
|
+
if (input.summarizedThroughSeq >= input.observedHeadSeq) {
|
|
108
|
+
throw new AgentStorageError(
|
|
109
|
+
"invalid_input",
|
|
110
|
+
`[${prefix}] Agent context checkpoint must retain the triggering head message.`
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Assert that the current delivery still owns the active conversational run. */
|
|
116
|
+
export function assertAgentContextCheckpointAuthority(input: {
|
|
117
|
+
readonly create: CreateAgentContextCheckpointInput
|
|
118
|
+
readonly run: AgentRunRecord | null
|
|
119
|
+
readonly thread: AgentThreadRecord | null
|
|
120
|
+
readonly prefix?: string
|
|
121
|
+
}): asserts input is typeof input & {
|
|
122
|
+
readonly run: AgentRunRecord
|
|
123
|
+
readonly thread: AgentThreadRecord
|
|
124
|
+
} {
|
|
125
|
+
const { create, run, thread } = input
|
|
126
|
+
const prefix = input.prefix ?? "Sixb"
|
|
127
|
+
if (!run) {
|
|
128
|
+
throw new AgentStorageError(
|
|
129
|
+
"run_not_found",
|
|
130
|
+
`[${prefix}] Agent run '${create.createdByRunId}' not found for project '${create.projectId}'.`
|
|
131
|
+
)
|
|
132
|
+
}
|
|
133
|
+
if (!thread) {
|
|
134
|
+
throw new AgentStorageError(
|
|
135
|
+
"thread_not_found",
|
|
136
|
+
`[${prefix}] Agent thread '${create.threadId}' not found for project '${create.projectId}'.`
|
|
137
|
+
)
|
|
138
|
+
}
|
|
139
|
+
if (run.threadId !== create.threadId) {
|
|
140
|
+
throw new AgentStorageError(
|
|
141
|
+
"invalid_input",
|
|
142
|
+
`[${prefix}] Agent run '${run.id}' does not belong to thread '${create.threadId}'.`
|
|
143
|
+
)
|
|
144
|
+
}
|
|
145
|
+
if (run.status !== "running") {
|
|
146
|
+
throw new AgentStorageError(
|
|
147
|
+
"invalid_state",
|
|
148
|
+
`[${prefix}] Agent run '${run.id}' is not running (status '${run.status}').`
|
|
149
|
+
)
|
|
150
|
+
}
|
|
151
|
+
if (!run.execution || run.execution.token !== create.executionToken) {
|
|
152
|
+
throw new AgentStorageError(
|
|
153
|
+
"execution_lost",
|
|
154
|
+
`[${prefix}] Execution token is no longer current on agent run '${run.id}'.`
|
|
155
|
+
)
|
|
156
|
+
}
|
|
157
|
+
if (thread.activeRunId !== run.id) {
|
|
158
|
+
throw new AgentStorageError(
|
|
159
|
+
"invalid_state",
|
|
160
|
+
`[${prefix}] Agent run '${run.id}' no longer owns thread '${thread.id}'.`
|
|
161
|
+
)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Assert both compare-and-swap anchors and the retained-turn boundary. */
|
|
166
|
+
export function assertAgentContextCheckpointAnchors(input: {
|
|
167
|
+
readonly create: CreateAgentContextCheckpointInput
|
|
168
|
+
readonly latest: AgentContextCheckpointRecord | null
|
|
169
|
+
readonly headSeq: number
|
|
170
|
+
readonly firstRetained: AgentMessageRecord | null
|
|
171
|
+
readonly prefix?: string
|
|
172
|
+
}): void {
|
|
173
|
+
const { create, latest, headSeq, firstRetained } = input
|
|
174
|
+
const prefix = input.prefix ?? "Sixb"
|
|
175
|
+
if (headSeq !== create.expectedHeadSeq) {
|
|
176
|
+
throw new AgentStorageError(
|
|
177
|
+
"invalid_state",
|
|
178
|
+
`[${prefix}] Agent thread '${create.threadId}' message head changed from ${create.expectedHeadSeq} to ${headSeq}.`
|
|
179
|
+
)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const latestId = latest?.id ?? null
|
|
183
|
+
if (latestId !== create.expectedPreviousCheckpointId) {
|
|
184
|
+
throw new AgentStorageError(
|
|
185
|
+
"invalid_state",
|
|
186
|
+
`[${prefix}] Agent thread '${create.threadId}' checkpoint head changed.`
|
|
187
|
+
)
|
|
188
|
+
}
|
|
189
|
+
if (latest && create.summarizedThroughSeq <= latest.summarizedThroughSeq) {
|
|
190
|
+
throw new AgentStorageError(
|
|
191
|
+
"invalid_input",
|
|
192
|
+
`[${prefix}] Agent context checkpoint boundary must advance beyond sequence ${latest.summarizedThroughSeq}.`
|
|
193
|
+
)
|
|
194
|
+
}
|
|
195
|
+
if (!firstRetained || firstRetained.seq !== create.summarizedThroughSeq + 1) {
|
|
196
|
+
throw new AgentStorageError(
|
|
197
|
+
"invalid_state",
|
|
198
|
+
`[${prefix}] Agent context checkpoint boundary does not align with stored thread messages.`
|
|
199
|
+
)
|
|
200
|
+
}
|
|
201
|
+
if (firstRetained.role !== "user") {
|
|
202
|
+
throw new AgentStorageError(
|
|
203
|
+
"invalid_input",
|
|
204
|
+
`[${prefix}] Agent context checkpoint must retain a complete turn beginning with a user message.`
|
|
205
|
+
)
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** Validate the mutable anchors again before returning an idempotently created checkpoint. */
|
|
210
|
+
export function assertAgentContextCheckpointReplayState(input: {
|
|
211
|
+
readonly create: CreateAgentContextCheckpointInput
|
|
212
|
+
readonly existing: AgentContextCheckpointRecord
|
|
213
|
+
readonly latest: AgentContextCheckpointRecord | null
|
|
214
|
+
readonly headSeq: number
|
|
215
|
+
readonly prefix?: string
|
|
216
|
+
}): void {
|
|
217
|
+
const prefix = input.prefix ?? "Sixb"
|
|
218
|
+
if (input.headSeq !== input.create.expectedHeadSeq) {
|
|
219
|
+
throw new AgentStorageError(
|
|
220
|
+
"invalid_state",
|
|
221
|
+
`[${prefix}] Agent thread '${input.create.threadId}' message head changed from ${input.create.expectedHeadSeq} to ${input.headSeq}.`
|
|
222
|
+
)
|
|
223
|
+
}
|
|
224
|
+
if (input.latest?.id !== input.existing.id) {
|
|
225
|
+
throw new AgentStorageError(
|
|
226
|
+
"invalid_state",
|
|
227
|
+
`[${prefix}] Agent thread '${input.create.threadId}' checkpoint head changed after '${input.existing.id}'.`
|
|
228
|
+
)
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Compare the durable semantic payload; delivery token and generated timestamp are not content. */
|
|
233
|
+
export function agentContextCheckpointMatchesCreateInput(
|
|
234
|
+
record: AgentContextCheckpointRecord,
|
|
235
|
+
input: CreateAgentContextCheckpointInput
|
|
236
|
+
): boolean {
|
|
237
|
+
return (
|
|
238
|
+
record.id === input.id &&
|
|
239
|
+
record.projectId === input.projectId &&
|
|
240
|
+
record.threadId === input.threadId &&
|
|
241
|
+
record.createdByRunId === input.createdByRunId &&
|
|
242
|
+
(record.previousCheckpointId ?? null) === input.expectedPreviousCheckpointId &&
|
|
243
|
+
record.reason === input.reason &&
|
|
244
|
+
record.summary === input.summary &&
|
|
245
|
+
record.summaryFormatVersion === input.summaryFormatVersion &&
|
|
246
|
+
record.summarizedThroughSeq === input.summarizedThroughSeq &&
|
|
247
|
+
record.observedHeadSeq === input.observedHeadSeq &&
|
|
248
|
+
record.estimatedInputTokensBefore === input.estimatedInputTokensBefore &&
|
|
249
|
+
record.estimatedInputTokensAfter === input.estimatedInputTokensAfter &&
|
|
250
|
+
record.summaryModelId === input.summaryModelId
|
|
251
|
+
)
|
|
252
|
+
}
|
|
@@ -69,15 +69,17 @@ export const AGENT_RUN_FAILURE_CODES = [
|
|
|
69
69
|
export type AgentRunFailureCode = (typeof AGENT_RUN_FAILURE_CODES)[number]
|
|
70
70
|
|
|
71
71
|
/**
|
|
72
|
-
* Why a run ended — our own SDK-independent vocabulary
|
|
73
|
-
* reasons
|
|
74
|
-
* catch-alls:
|
|
72
|
+
* Why a run ended — our own SDK-independent vocabulary. It includes the AI SDK's unified finish
|
|
73
|
+
* reasons plus platform-level endings such as `timeout`, so reads are typed and exhaustive without
|
|
74
|
+
* core depending on `ai`. `other`/`unknown` are catch-alls: an unrecognised provider value still
|
|
75
|
+
* records *that* the run ended.
|
|
75
76
|
*/
|
|
76
77
|
export const AGENT_RUN_FINISH_REASONS = [
|
|
77
78
|
"stop",
|
|
78
79
|
"length",
|
|
79
80
|
"content-filter",
|
|
80
81
|
"tool-calls",
|
|
82
|
+
"timeout",
|
|
81
83
|
"error",
|
|
82
84
|
"other",
|
|
83
85
|
"unknown",
|
|
@@ -287,9 +289,17 @@ export interface AppendAgentMessageInput extends AgentMessage {
|
|
|
287
289
|
readonly completedAt?: Date
|
|
288
290
|
}
|
|
289
291
|
|
|
292
|
+
export interface DeleteAgentMessagesByRunInput {
|
|
293
|
+
readonly projectId: string
|
|
294
|
+
readonly threadId: string
|
|
295
|
+
readonly runId: string
|
|
296
|
+
}
|
|
297
|
+
|
|
290
298
|
export interface ListAgentMessagesInput {
|
|
291
299
|
readonly projectId: string
|
|
292
300
|
readonly threadId: string
|
|
301
|
+
/** Return only messages whose per-thread sequence is greater than this retained boundary. */
|
|
302
|
+
readonly afterSeq?: number
|
|
293
303
|
readonly roles?: readonly AgentMessageRole[]
|
|
294
304
|
readonly limit?: number
|
|
295
305
|
readonly offset?: number
|
|
@@ -302,6 +312,55 @@ export interface ListAgentMessagesResult {
|
|
|
302
312
|
readonly total: number
|
|
303
313
|
}
|
|
304
314
|
|
|
315
|
+
// ── agent_context_checkpoints — append-only model-context boundaries ──────────────────────────
|
|
316
|
+
|
|
317
|
+
export type AgentContextCheckpointReason = "threshold" | "overflow"
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* A durable summary boundary used only when projecting a thread into model context.
|
|
321
|
+
*
|
|
322
|
+
* Checkpoints never replace or mutate transcript messages. Normal thread reads remain authoritative
|
|
323
|
+
* and complete; the worker reads the latest checkpoint and only the retained message tail.
|
|
324
|
+
*/
|
|
325
|
+
export interface AgentContextCheckpointRecord {
|
|
326
|
+
readonly id: string
|
|
327
|
+
readonly projectId: string
|
|
328
|
+
readonly threadId: string
|
|
329
|
+
readonly createdByRunId: string
|
|
330
|
+
readonly previousCheckpointId?: string
|
|
331
|
+
readonly reason: AgentContextCheckpointReason
|
|
332
|
+
readonly summary: string
|
|
333
|
+
readonly summaryFormatVersion: 1
|
|
334
|
+
readonly summarizedThroughSeq: number
|
|
335
|
+
readonly observedHeadSeq: number
|
|
336
|
+
readonly estimatedInputTokensBefore: number
|
|
337
|
+
readonly estimatedInputTokensAfter: number
|
|
338
|
+
readonly summaryModelId: string
|
|
339
|
+
readonly createdAt: Date
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
export interface CreateAgentContextCheckpointInput {
|
|
343
|
+
readonly id: string
|
|
344
|
+
readonly projectId: string
|
|
345
|
+
readonly threadId: string
|
|
346
|
+
readonly createdByRunId: string
|
|
347
|
+
/** Compare-and-swap anchor. `null` means the thread must not have a prior checkpoint. */
|
|
348
|
+
readonly expectedPreviousCheckpointId: string | null
|
|
349
|
+
/** Compare-and-swap anchor for the complete transcript observed by the summarizer. */
|
|
350
|
+
readonly expectedHeadSeq: number
|
|
351
|
+
/** Current delivery token for `createdByRunId`; stale redeliveries must not write. */
|
|
352
|
+
readonly executionToken: string
|
|
353
|
+
readonly reason: AgentContextCheckpointReason
|
|
354
|
+
readonly summary: string
|
|
355
|
+
readonly summaryFormatVersion: 1
|
|
356
|
+
readonly summarizedThroughSeq: number
|
|
357
|
+
readonly observedHeadSeq: number
|
|
358
|
+
readonly estimatedInputTokensBefore: number
|
|
359
|
+
readonly estimatedInputTokensAfter: number
|
|
360
|
+
readonly summaryModelId: string
|
|
361
|
+
readonly createdAt?: Date
|
|
362
|
+
}
|
|
363
|
+
|
|
305
364
|
// ── Store ───────────────────────────────────────────────────────────────────────────────────────
|
|
306
365
|
|
|
307
366
|
export interface AgentThreadStore {
|
|
@@ -337,12 +396,32 @@ export interface AgentRunStore {
|
|
|
337
396
|
export interface AgentMessageStore {
|
|
338
397
|
/** Insert a message (its `parts` are the canonical content) and bump thread stats. */
|
|
339
398
|
append(input: AppendAgentMessageInput): Promise<AgentMessageRecord>
|
|
399
|
+
/** Delete every message produced by one run and repair the thread's message stats. */
|
|
400
|
+
deleteByRunId(input: DeleteAgentMessagesByRunInput): Promise<number>
|
|
340
401
|
getById(params: { projectId: string; id: string }): Promise<AgentMessageRecord | null>
|
|
341
402
|
list(input: ListAgentMessagesInput): Promise<ListAgentMessagesResult>
|
|
342
403
|
}
|
|
343
404
|
|
|
405
|
+
export interface AgentContextCheckpointStore {
|
|
406
|
+
/**
|
|
407
|
+
* Append one checkpoint after atomically validating run ownership and both transcript anchors.
|
|
408
|
+
* Repeating the same payload for the same creating run is idempotent.
|
|
409
|
+
*/
|
|
410
|
+
create(input: CreateAgentContextCheckpointInput): Promise<AgentContextCheckpointRecord>
|
|
411
|
+
/** Return checkpoints created by the requested runs, in first-occurrence input order. */
|
|
412
|
+
getByRunIds(input: {
|
|
413
|
+
readonly projectId: string
|
|
414
|
+
readonly runIds: readonly string[]
|
|
415
|
+
}): Promise<readonly AgentContextCheckpointRecord[]>
|
|
416
|
+
getLatest(input: {
|
|
417
|
+
readonly projectId: string
|
|
418
|
+
readonly threadId: string
|
|
419
|
+
}): Promise<AgentContextCheckpointRecord | null>
|
|
420
|
+
}
|
|
421
|
+
|
|
344
422
|
export interface AgentStorage {
|
|
345
423
|
readonly threads: AgentThreadStore
|
|
346
424
|
readonly runs: AgentRunStore
|
|
347
425
|
readonly messages: AgentMessageStore
|
|
426
|
+
readonly checkpoints: AgentContextCheckpointStore
|
|
348
427
|
}
|