@atomicmemory/core 1.0.0
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/CHANGELOG.md +27 -0
- package/LICENSE +201 -0
- package/README.md +314 -0
- package/dist/app/bind-ephemeral.d.ts +18 -0
- package/dist/app/bind-ephemeral.js +22 -0
- package/dist/app/cors-headers.d.ts +12 -0
- package/dist/app/cors-headers.js +18 -0
- package/dist/app/create-app.d.ts +25 -0
- package/dist/app/create-app.js +156 -0
- package/dist/app/runtime-config-route-snapshot.d.ts +27 -0
- package/dist/app/runtime-config-route-snapshot.js +27 -0
- package/dist/app/runtime-container.d.ts +281 -0
- package/dist/app/runtime-container.js +297 -0
- package/dist/app/startup-checks.d.ts +28 -0
- package/dist/app/startup-checks.js +45 -0
- package/dist/bin.d.ts +17 -0
- package/dist/bin.js +128 -0
- package/dist/config.d.ts +680 -0
- package/dist/config.js +808 -0
- package/dist/db/agent-trust-repository.d.ts +49 -0
- package/dist/db/agent-trust-repository.js +66 -0
- package/dist/db/belief-edges-repository.d.ts +68 -0
- package/dist/db/belief-edges-repository.js +124 -0
- package/dist/db/claim-repository.d.ts +6 -0
- package/dist/db/claim-repository.js +4 -0
- package/dist/db/contradictions-repository.d.ts +56 -0
- package/dist/db/contradictions-repository.js +88 -0
- package/dist/db/document-chunk-repository.d.ts +48 -0
- package/dist/db/document-chunk-repository.js +145 -0
- package/dist/db/document-chunk-types.d.ts +35 -0
- package/dist/db/document-chunk-types.js +9 -0
- package/dist/db/document-list-cursor.d.ts +45 -0
- package/dist/db/document-list-cursor.js +111 -0
- package/dist/db/document-list-repository.d.ts +103 -0
- package/dist/db/document-list-repository.js +204 -0
- package/dist/db/entity-cards-repository.d.ts +37 -0
- package/dist/db/entity-cards-repository.js +46 -0
- package/dist/db/entity-values-repository.d.ts +26 -0
- package/dist/db/entity-values-repository.js +57 -0
- package/dist/db/link-repository.d.ts +30 -0
- package/dist/db/link-repository.js +54 -0
- package/dist/db/memory-repository.d.ts +163 -0
- package/dist/db/memory-repository.js +232 -0
- package/dist/db/migrate.d.ts +6 -0
- package/dist/db/migrate.js +36 -0
- package/dist/db/mmr.d.ts +14 -0
- package/dist/db/mmr.js +57 -0
- package/dist/db/passport-feed-repository.d.ts +91 -0
- package/dist/db/passport-feed-repository.js +198 -0
- package/dist/db/pg-episode-store.d.ts +19 -0
- package/dist/db/pg-episode-store.js +17 -0
- package/dist/db/pg-link-store.d.ts +17 -0
- package/dist/db/pg-link-store.js +14 -0
- package/dist/db/pg-memory-store.d.ts +68 -0
- package/dist/db/pg-memory-store.js +53 -0
- package/dist/db/pg-recap-store.d.ts +13 -0
- package/dist/db/pg-recap-store.js +19 -0
- package/dist/db/pg-representation-store.d.ts +17 -0
- package/dist/db/pg-representation-store.js +17 -0
- package/dist/db/pg-search-store.d.ts +29 -0
- package/dist/db/pg-search-store.js +47 -0
- package/dist/db/pool.d.ts +5 -0
- package/dist/db/pool.js +21 -0
- package/dist/db/ppr.d.ts +56 -0
- package/dist/db/ppr.js +178 -0
- package/dist/db/query-helpers.d.ts +44 -0
- package/dist/db/query-helpers.js +60 -0
- package/dist/db/raw-doc-artifact-sync.d.ts +128 -0
- package/dist/db/raw-doc-artifact-sync.js +259 -0
- package/dist/db/raw-document-blob-repository.d.ts +148 -0
- package/dist/db/raw-document-blob-repository.js +300 -0
- package/dist/db/raw-document-repository.d.ts +104 -0
- package/dist/db/raw-document-repository.js +410 -0
- package/dist/db/raw-document-status-repository.d.ts +122 -0
- package/dist/db/raw-document-status-repository.js +183 -0
- package/dist/db/raw-document-types.d.ts +236 -0
- package/dist/db/raw-document-types.js +10 -0
- package/dist/db/raw-storage-reconciliation-repository.d.ts +110 -0
- package/dist/db/raw-storage-reconciliation-repository.js +200 -0
- package/dist/db/reflection-jobs-repository.d.ts +33 -0
- package/dist/db/reflection-jobs-repository.js +48 -0
- package/dist/db/reflections-repository.d.ts +41 -0
- package/dist/db/reflections-repository.js +83 -0
- package/dist/db/repository-claims.d.ts +141 -0
- package/dist/db/repository-claims.js +376 -0
- package/dist/db/repository-deferred-audn.d.ts +33 -0
- package/dist/db/repository-deferred-audn.js +69 -0
- package/dist/db/repository-document-delete.d.ts +53 -0
- package/dist/db/repository-document-delete.js +156 -0
- package/dist/db/repository-entities.d.ts +114 -0
- package/dist/db/repository-entities.js +317 -0
- package/dist/db/repository-entity-attributes.d.ts +41 -0
- package/dist/db/repository-entity-attributes.js +65 -0
- package/dist/db/repository-entity-graph.d.ts +32 -0
- package/dist/db/repository-entity-graph.js +87 -0
- package/dist/db/repository-first-mentions.d.ts +41 -0
- package/dist/db/repository-first-mentions.js +79 -0
- package/dist/db/repository-lessons.d.ts +51 -0
- package/dist/db/repository-lessons.js +90 -0
- package/dist/db/repository-links.d.ts +26 -0
- package/dist/db/repository-links.js +105 -0
- package/dist/db/repository-observation.d.ts +26 -0
- package/dist/db/repository-observation.js +51 -0
- package/dist/db/repository-read.d.ts +56 -0
- package/dist/db/repository-read.js +271 -0
- package/dist/db/repository-recaps.d.ts +59 -0
- package/dist/db/repository-recaps.js +158 -0
- package/dist/db/repository-representations.d.ts +48 -0
- package/dist/db/repository-representations.js +162 -0
- package/dist/db/repository-temporal-state.d.ts +35 -0
- package/dist/db/repository-temporal-state.js +46 -0
- package/dist/db/repository-tll.d.ts +88 -0
- package/dist/db/repository-tll.js +179 -0
- package/dist/db/repository-types.d.ts +313 -0
- package/dist/db/repository-types.js +142 -0
- package/dist/db/repository-user-profiles.d.ts +17 -0
- package/dist/db/repository-user-profiles.js +28 -0
- package/dist/db/repository-vector-search.d.ts +33 -0
- package/dist/db/repository-vector-search.js +373 -0
- package/dist/db/repository-wipe.d.ts +34 -0
- package/dist/db/repository-wipe.js +94 -0
- package/dist/db/repository-write.d.ts +61 -0
- package/dist/db/repository-write.js +279 -0
- package/dist/db/schema.sql +1355 -0
- package/dist/db/storage-artifact-delete-tx.d.ts +56 -0
- package/dist/db/storage-artifact-delete-tx.js +123 -0
- package/dist/db/storage-artifact-providers.d.ts +21 -0
- package/dist/db/storage-artifact-providers.js +21 -0
- package/dist/db/storage-artifact-recovery-repository.d.ts +66 -0
- package/dist/db/storage-artifact-recovery-repository.js +58 -0
- package/dist/db/storage-artifact-repository.d.ts +329 -0
- package/dist/db/storage-artifact-repository.js +497 -0
- package/dist/db/stores.d.ts +220 -0
- package/dist/db/stores.js +12 -0
- package/dist/db/summaries-repository.d.ts +74 -0
- package/dist/db/summaries-repository.js +125 -0
- package/dist/eval/beam-10m-loader.d.ts +98 -0
- package/dist/eval/beam-10m-loader.js +128 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +17 -0
- package/dist/middleware/require-bearer.d.ts +27 -0
- package/dist/middleware/require-bearer.js +60 -0
- package/dist/middleware/validate-response.d.ts +33 -0
- package/dist/middleware/validate-response.js +55 -0
- package/dist/middleware/validate.d.ts +43 -0
- package/dist/middleware/validate.js +85 -0
- package/dist/routes/agents.d.ts +13 -0
- package/dist/routes/agents.js +89 -0
- package/dist/routes/document-response-formatters.d.ts +98 -0
- package/dist/routes/document-response-formatters.js +243 -0
- package/dist/routes/documents.d.ts +74 -0
- package/dist/routes/documents.js +425 -0
- package/dist/routes/memories.d.ts +29 -0
- package/dist/routes/memories.js +725 -0
- package/dist/routes/memory-response-formatters.d.ts +179 -0
- package/dist/routes/memory-response-formatters.js +210 -0
- package/dist/routes/public-raw-storage-metadata.d.ts +54 -0
- package/dist/routes/public-raw-storage-metadata.js +56 -0
- package/dist/routes/reflect.d.ts +14 -0
- package/dist/routes/reflect.js +19 -0
- package/dist/routes/response-schema-map.d.ts +14 -0
- package/dist/routes/response-schema-map.js +69 -0
- package/dist/routes/route-errors.d.ts +12 -0
- package/dist/routes/route-errors.js +30 -0
- package/dist/routes/storage-error-handlers.d.ts +34 -0
- package/dist/routes/storage-error-handlers.js +185 -0
- package/dist/routes/storage-response-formatters.d.ts +44 -0
- package/dist/routes/storage-response-formatters.js +155 -0
- package/dist/routes/storage.d.ts +38 -0
- package/dist/routes/storage.js +369 -0
- package/dist/routes/upstream-provider-errors.d.ts +19 -0
- package/dist/routes/upstream-provider-errors.js +95 -0
- package/dist/schemas/agents.d.ts +79 -0
- package/dist/schemas/agents.js +126 -0
- package/dist/schemas/common.d.ts +110 -0
- package/dist/schemas/common.js +190 -0
- package/dist/schemas/document-list-responses.d.ts +102 -0
- package/dist/schemas/document-list-responses.js +87 -0
- package/dist/schemas/document-list-schemas.d.ts +123 -0
- package/dist/schemas/document-list-schemas.js +174 -0
- package/dist/schemas/document-response-schemas.d.ts +610 -0
- package/dist/schemas/document-response-schemas.js +264 -0
- package/dist/schemas/document-status-envelope.d.ts +48 -0
- package/dist/schemas/document-status-envelope.js +54 -0
- package/dist/schemas/documents.d.ts +292 -0
- package/dist/schemas/documents.js +449 -0
- package/dist/schemas/errors.d.ts +75 -0
- package/dist/schemas/errors.js +105 -0
- package/dist/schemas/memories.d.ts +378 -0
- package/dist/schemas/memories.js +542 -0
- package/dist/schemas/openapi.d.ts +24 -0
- package/dist/schemas/openapi.js +1038 -0
- package/dist/schemas/response-scalars.d.ts +10 -0
- package/dist/schemas/response-scalars.js +10 -0
- package/dist/schemas/responses.d.ts +536 -0
- package/dist/schemas/responses.js +350 -0
- package/dist/schemas/search-response-parts.d.ts +97 -0
- package/dist/schemas/search-response-parts.js +103 -0
- package/dist/schemas/storage-schemas.d.ts +175 -0
- package/dist/schemas/storage-schemas.js +277 -0
- package/dist/schemas/zod-setup.d.ts +15 -0
- package/dist/schemas/zod-setup.js +17 -0
- package/dist/server.d.ts +13 -0
- package/dist/server.js +57 -0
- package/dist/services/abstract-query-policy.d.ts +13 -0
- package/dist/services/abstract-query-policy.js +50 -0
- package/dist/services/affinity-clustering.d.ts +66 -0
- package/dist/services/affinity-clustering.js +125 -0
- package/dist/services/agentic-retrieval.d.ts +38 -0
- package/dist/services/agentic-retrieval.js +126 -0
- package/dist/services/answer-format.d.ts +56 -0
- package/dist/services/answer-format.js +118 -0
- package/dist/services/answer-rescue.d.ts +72 -0
- package/dist/services/answer-rescue.js +177 -0
- package/dist/services/answer-verifier.d.ts +24 -0
- package/dist/services/answer-verifier.js +73 -0
- package/dist/services/api-retry.d.ts +6 -0
- package/dist/services/api-retry.js +41 -0
- package/dist/services/assistant-turn-filter.d.ts +20 -0
- package/dist/services/assistant-turn-filter.js +69 -0
- package/dist/services/atomicmem-uri.d.ts +33 -0
- package/dist/services/atomicmem-uri.js +86 -0
- package/dist/services/audit-events.d.ts +54 -0
- package/dist/services/audit-events.js +56 -0
- package/dist/services/chunked-extraction.d.ts +21 -0
- package/dist/services/chunked-extraction.js +108 -0
- package/dist/services/claim-slotting.d.ts +27 -0
- package/dist/services/claim-slotting.js +38 -0
- package/dist/services/claude-code-llm.d.ts +19 -0
- package/dist/services/claude-code-llm.js +96 -0
- package/dist/services/composite-dedup.d.ts +50 -0
- package/dist/services/composite-dedup.js +153 -0
- package/dist/services/composite-grouping.d.ts +41 -0
- package/dist/services/composite-grouping.js +111 -0
- package/dist/services/composite-staleness.d.ts +20 -0
- package/dist/services/composite-staleness.js +50 -0
- package/dist/services/conciseness-preference.d.ts +14 -0
- package/dist/services/conciseness-preference.js +42 -0
- package/dist/services/conflict-policy.d.ts +20 -0
- package/dist/services/conflict-policy.js +335 -0
- package/dist/services/consensus-extraction.d.ts +39 -0
- package/dist/services/consensus-extraction.js +147 -0
- package/dist/services/consensus-validation.d.ts +52 -0
- package/dist/services/consensus-validation.js +206 -0
- package/dist/services/consolidation-service.d.ts +60 -0
- package/dist/services/consolidation-service.js +171 -0
- package/dist/services/content-detection.d.ts +18 -0
- package/dist/services/content-detection.js +25 -0
- package/dist/services/contradiction-surfacing.d.ts +62 -0
- package/dist/services/contradiction-surfacing.js +111 -0
- package/dist/services/cost-telemetry.d.ts +39 -0
- package/dist/services/cost-telemetry.js +58 -0
- package/dist/services/counter-evidence.d.ts +34 -0
- package/dist/services/counter-evidence.js +92 -0
- package/dist/services/current-state-ranking.d.ts +21 -0
- package/dist/services/current-state-ranking.js +152 -0
- package/dist/services/deferred-audn.d.ts +47 -0
- package/dist/services/deferred-audn.js +162 -0
- package/dist/services/document-chunker.d.ts +50 -0
- package/dist/services/document-chunker.js +153 -0
- package/dist/services/document-failure-markers.d.ts +91 -0
- package/dist/services/document-failure-markers.js +305 -0
- package/dist/services/document-indexer.d.ts +122 -0
- package/dist/services/document-indexer.js +405 -0
- package/dist/services/document-service.d.ts +245 -0
- package/dist/services/document-service.js +325 -0
- package/dist/services/document-upload-artifact-sync.d.ts +80 -0
- package/dist/services/document-upload-artifact-sync.js +162 -0
- package/dist/services/document-upload-beta2-recovery.d.ts +72 -0
- package/dist/services/document-upload-beta2-recovery.js +94 -0
- package/dist/services/document-upload.d.ts +44 -0
- package/dist/services/document-upload.js +353 -0
- package/dist/services/embedding.d.ts +57 -0
- package/dist/services/embedding.js +416 -0
- package/dist/services/entity-attribute-extractor.d.ts +34 -0
- package/dist/services/entity-attribute-extractor.js +117 -0
- package/dist/services/entity-card-synthesis.d.ts +54 -0
- package/dist/services/entity-card-synthesis.js +92 -0
- package/dist/services/entity-dedup.d.ts +9 -0
- package/dist/services/entity-dedup.js +14 -0
- package/dist/services/entity-graph.d.ts +17 -0
- package/dist/services/entity-graph.js +135 -0
- package/dist/services/entropy-gate.d.ts +52 -0
- package/dist/services/entropy-gate.js +56 -0
- package/dist/services/episode-fetcher.d.ts +47 -0
- package/dist/services/episode-fetcher.js +128 -0
- package/dist/services/event-anchor-facts.d.ts +8 -0
- package/dist/services/event-anchor-facts.js +205 -0
- package/dist/services/event-chain-detector.d.ts +52 -0
- package/dist/services/event-chain-detector.js +83 -0
- package/dist/services/extraction-cache.d.ts +9 -0
- package/dist/services/extraction-cache.js +54 -0
- package/dist/services/extraction-enrichment.d.ts +9 -0
- package/dist/services/extraction-enrichment.js +223 -0
- package/dist/services/extraction.d.ts +69 -0
- package/dist/services/extraction.js +596 -0
- package/dist/services/fact-normalization.d.ts +12 -0
- package/dist/services/fact-normalization.js +248 -0
- package/dist/services/filecoin-observability.d.ts +127 -0
- package/dist/services/filecoin-observability.js +200 -0
- package/dist/services/first-mention-service.d.ts +76 -0
- package/dist/services/first-mention-service.js +186 -0
- package/dist/services/hierarchical-retrieval.d.ts +49 -0
- package/dist/services/hierarchical-retrieval.js +50 -0
- package/dist/services/ingest-fact-pipeline.d.ts +32 -0
- package/dist/services/ingest-fact-pipeline.js +212 -0
- package/dist/services/ingest-post-write.d.ts +50 -0
- package/dist/services/ingest-post-write.js +117 -0
- package/dist/services/ingest-trace.d.ts +32 -0
- package/dist/services/ingest-trace.js +60 -0
- package/dist/services/input-sanitizer.d.ts +41 -0
- package/dist/services/input-sanitizer.js +135 -0
- package/dist/services/iterative-retrieval.d.ts +26 -0
- package/dist/services/iterative-retrieval.js +139 -0
- package/dist/services/keyword-expansion.d.ts +10 -0
- package/dist/services/keyword-expansion.js +26 -0
- package/dist/services/lesson-service.d.ts +68 -0
- package/dist/services/lesson-service.js +178 -0
- package/dist/services/literal-extractor.d.ts +16 -0
- package/dist/services/literal-extractor.js +74 -0
- package/dist/services/literal-list-protection.d.ts +17 -0
- package/dist/services/literal-list-protection.js +134 -0
- package/dist/services/literal-query-expansion.d.ts +20 -0
- package/dist/services/literal-query-expansion.js +181 -0
- package/dist/services/llm.d.ts +61 -0
- package/dist/services/llm.js +265 -0
- package/dist/services/memcell-projection.d.ts +17 -0
- package/dist/services/memcell-projection.js +41 -0
- package/dist/services/memory-audn.d.ts +43 -0
- package/dist/services/memory-audn.js +419 -0
- package/dist/services/memory-crud.d.ts +93 -0
- package/dist/services/memory-crud.js +255 -0
- package/dist/services/memory-ingest.d.ts +21 -0
- package/dist/services/memory-ingest.js +249 -0
- package/dist/services/memory-lifecycle.d.ts +75 -0
- package/dist/services/memory-lifecycle.js +108 -0
- package/dist/services/memory-lineage.d.ts +181 -0
- package/dist/services/memory-lineage.js +232 -0
- package/dist/services/memory-network.d.ts +40 -0
- package/dist/services/memory-network.js +75 -0
- package/dist/services/memory-search-types.d.ts +25 -0
- package/dist/services/memory-search-types.js +10 -0
- package/dist/services/memory-search.d.ts +48 -0
- package/dist/services/memory-search.js +505 -0
- package/dist/services/memory-service-types.d.ts +371 -0
- package/dist/services/memory-service-types.js +8 -0
- package/dist/services/memory-service.d.ts +152 -0
- package/dist/services/memory-service.js +225 -0
- package/dist/services/memory-storage.d.ts +33 -0
- package/dist/services/memory-storage.js +328 -0
- package/dist/services/msr-aggregator.d.ts +38 -0
- package/dist/services/msr-aggregator.js +97 -0
- package/dist/services/msr-detector.d.ts +35 -0
- package/dist/services/msr-detector.js +65 -0
- package/dist/services/namespace-retrieval.d.ts +60 -0
- package/dist/services/namespace-retrieval.js +180 -0
- package/dist/services/observation-date-extraction.d.ts +12 -0
- package/dist/services/observation-date-extraction.js +50 -0
- package/dist/services/observation-service.d.ts +27 -0
- package/dist/services/observation-service.js +84 -0
- package/dist/services/packaging-observability.d.ts +29 -0
- package/dist/services/packaging-observability.js +146 -0
- package/dist/services/query-expansion.d.ts +83 -0
- package/dist/services/query-expansion.js +242 -0
- package/dist/services/query-keyword-matches.d.ts +6 -0
- package/dist/services/query-keyword-matches.js +56 -0
- package/dist/services/query-term-visibility.d.ts +28 -0
- package/dist/services/query-term-visibility.js +100 -0
- package/dist/services/quick-extraction.d.ts +25 -0
- package/dist/services/quick-extraction.js +431 -0
- package/dist/services/quoted-entity-extraction.d.ts +10 -0
- package/dist/services/quoted-entity-extraction.js +161 -0
- package/dist/services/raw-storage-reconciler-backoff.d.ts +8 -0
- package/dist/services/raw-storage-reconciler-backoff.js +14 -0
- package/dist/services/raw-storage-reconciler-scheduler.d.ts +29 -0
- package/dist/services/raw-storage-reconciler-scheduler.js +43 -0
- package/dist/services/raw-storage-reconciler.d.ts +71 -0
- package/dist/services/raw-storage-reconciler.js +278 -0
- package/dist/services/recap-builder.d.ts +49 -0
- package/dist/services/recap-builder.js +157 -0
- package/dist/services/reflect-jobs.d.ts +23 -0
- package/dist/services/reflect-jobs.js +36 -0
- package/dist/services/reflect-prompts.d.ts +71 -0
- package/dist/services/reflect-prompts.js +99 -0
- package/dist/services/reflect-retrieval.d.ts +33 -0
- package/dist/services/reflect-retrieval.js +30 -0
- package/dist/services/reflect.d.ts +49 -0
- package/dist/services/reflect.js +84 -0
- package/dist/services/relative-temporal.d.ts +14 -0
- package/dist/services/relative-temporal.js +163 -0
- package/dist/services/relevance-policy.d.ts +37 -0
- package/dist/services/relevance-policy.js +109 -0
- package/dist/services/rerank.d.ts +32 -0
- package/dist/services/rerank.js +118 -0
- package/dist/services/reranker.d.ts +20 -0
- package/dist/services/reranker.js +99 -0
- package/dist/services/retrieval-channel-rules.d.ts +34 -0
- package/dist/services/retrieval-channel-rules.js +41 -0
- package/dist/services/retrieval-config-overlay.d.ts +36 -0
- package/dist/services/retrieval-config-overlay.js +44 -0
- package/dist/services/retrieval-format.d.ts +119 -0
- package/dist/services/retrieval-format.js +559 -0
- package/dist/services/retrieval-policy.d.ts +69 -0
- package/dist/services/retrieval-policy.js +275 -0
- package/dist/services/retrieval-profiles.d.ts +37 -0
- package/dist/services/retrieval-profiles.js +90 -0
- package/dist/services/retrieval-side-effects.d.ts +14 -0
- package/dist/services/retrieval-side-effects.js +26 -0
- package/dist/services/retrieval-trace.d.ts +108 -0
- package/dist/services/retrieval-trace.js +147 -0
- package/dist/services/rrf-fusion.d.ts +18 -0
- package/dist/services/rrf-fusion.js +34 -0
- package/dist/services/search-pipeline.d.ts +71 -0
- package/dist/services/search-pipeline.js +788 -0
- package/dist/services/session-date.d.ts +20 -0
- package/dist/services/session-date.js +61 -0
- package/dist/services/session-packaging.d.ts +53 -0
- package/dist/services/session-packaging.js +182 -0
- package/dist/services/session-summary-generator.d.ts +53 -0
- package/dist/services/session-summary-generator.js +134 -0
- package/dist/services/specialists/cr-specialist.d.ts +52 -0
- package/dist/services/specialists/cr-specialist.js +121 -0
- package/dist/services/specialists/dispatch.d.ts +53 -0
- package/dist/services/specialists/dispatch.js +102 -0
- package/dist/services/specialists/ie-ku-specialist.d.ts +37 -0
- package/dist/services/specialists/ie-ku-specialist.js +63 -0
- package/dist/services/specialists/msr-specialist.d.ts +61 -0
- package/dist/services/specialists/msr-specialist.js +162 -0
- package/dist/services/specialists/tr-specialist.d.ts +37 -0
- package/dist/services/specialists/tr-specialist.js +146 -0
- package/dist/services/storage-key-prefix.d.ts +42 -0
- package/dist/services/storage-key-prefix.js +45 -0
- package/dist/services/storage-put-recovery.d.ts +71 -0
- package/dist/services/storage-put-recovery.js +269 -0
- package/dist/services/storage-service-errors.d.ts +124 -0
- package/dist/services/storage-service-errors.js +189 -0
- package/dist/services/storage-service.d.ts +176 -0
- package/dist/services/storage-service.js +423 -0
- package/dist/services/subject-aware-ranking.d.ts +19 -0
- package/dist/services/subject-aware-ranking.js +161 -0
- package/dist/services/supplemental-extraction.d.ts +7 -0
- package/dist/services/supplemental-extraction.js +116 -0
- package/dist/services/tbc-execution.d.ts +49 -0
- package/dist/services/tbc-execution.js +284 -0
- package/dist/services/temporal-classifier.d.ts +56 -0
- package/dist/services/temporal-classifier.js +94 -0
- package/dist/services/temporal-endpoint-evidence.d.ts +12 -0
- package/dist/services/temporal-endpoint-evidence.js +313 -0
- package/dist/services/temporal-fingerprint.d.ts +6 -0
- package/dist/services/temporal-fingerprint.js +12 -0
- package/dist/services/temporal-format.d.ts +9 -0
- package/dist/services/temporal-format.js +21 -0
- package/dist/services/temporal-intent.d.ts +39 -0
- package/dist/services/temporal-intent.js +78 -0
- package/dist/services/temporal-query-constraints.d.ts +16 -0
- package/dist/services/temporal-query-constraints.js +107 -0
- package/dist/services/temporal-query-expansion.d.ts +14 -0
- package/dist/services/temporal-query-expansion.js +131 -0
- package/dist/services/temporal-rerank.d.ts +22 -0
- package/dist/services/temporal-rerank.js +47 -0
- package/dist/services/temporal-result-protection.d.ts +7 -0
- package/dist/services/temporal-result-protection.js +60 -0
- package/dist/services/temporal-state-write.d.ts +57 -0
- package/dist/services/temporal-state-write.js +45 -0
- package/dist/services/tiered-context.d.ts +87 -0
- package/dist/services/tiered-context.js +214 -0
- package/dist/services/tiered-loading.d.ts +88 -0
- package/dist/services/tiered-loading.js +263 -0
- package/dist/services/timeline-pack.d.ts +36 -0
- package/dist/services/timeline-pack.js +50 -0
- package/dist/services/timing.d.ts +13 -0
- package/dist/services/timing.js +72 -0
- package/dist/services/tll-augmentation.d.ts +20 -0
- package/dist/services/tll-augmentation.js +125 -0
- package/dist/services/tll-retrieval.d.ts +55 -0
- package/dist/services/tll-retrieval.js +101 -0
- package/dist/services/topic-abstraction.d.ts +36 -0
- package/dist/services/topic-abstraction.js +105 -0
- package/dist/services/trust-scoring.d.ts +43 -0
- package/dist/services/trust-scoring.js +89 -0
- package/dist/services/typed-belief-calculus.d.ts +126 -0
- package/dist/services/typed-belief-calculus.js +204 -0
- package/dist/services/upload-config.d.ts +34 -0
- package/dist/services/upload-config.js +23 -0
- package/dist/services/upload-decision.d.ts +65 -0
- package/dist/services/upload-decision.js +98 -0
- package/dist/services/upload-helpers.d.ts +107 -0
- package/dist/services/upload-helpers.js +148 -0
- package/dist/services/user-profile-builder.d.ts +22 -0
- package/dist/services/user-profile-builder.js +109 -0
- package/dist/services/voyage-embedding.d.ts +22 -0
- package/dist/services/voyage-embedding.js +77 -0
- package/dist/services/write-security.d.ts +31 -0
- package/dist/services/write-security.js +64 -0
- package/dist/storage/artifact-public-redaction.d.ts +34 -0
- package/dist/storage/artifact-public-redaction.js +83 -0
- package/dist/storage/cleanup.d.ts +103 -0
- package/dist/storage/cleanup.js +138 -0
- package/dist/storage/codec-factory.d.ts +17 -0
- package/dist/storage/codec-factory.js +33 -0
- package/dist/storage/codecs/aes-gcm-codec.d.ts +44 -0
- package/dist/storage/codecs/aes-gcm-codec.js +108 -0
- package/dist/storage/codecs/noop-codec.d.ts +16 -0
- package/dist/storage/codecs/noop-codec.js +23 -0
- package/dist/storage/factory.d.ts +44 -0
- package/dist/storage/factory.js +99 -0
- package/dist/storage/filecoin-cid-validation.d.ts +82 -0
- package/dist/storage/filecoin-cid-validation.js +122 -0
- package/dist/storage/filecoin-public-metadata.d.ts +73 -0
- package/dist/storage/filecoin-public-metadata.js +110 -0
- package/dist/storage/local-fs-store.d.ts +39 -0
- package/dist/storage/local-fs-store.js +145 -0
- package/dist/storage/pointer-uri-allowlist.d.ts +38 -0
- package/dist/storage/pointer-uri-allowlist.js +70 -0
- package/dist/storage/provider-metadata-projection.d.ts +27 -0
- package/dist/storage/provider-metadata-projection.js +68 -0
- package/dist/storage/providers/filecoin/backend.d.ts +42 -0
- package/dist/storage/providers/filecoin/backend.js +250 -0
- package/dist/storage/providers/filecoin/config.d.ts +70 -0
- package/dist/storage/providers/filecoin/config.js +275 -0
- package/dist/storage/providers/filecoin/errors.d.ts +45 -0
- package/dist/storage/providers/filecoin/errors.js +56 -0
- package/dist/storage/providers/filecoin/filecoin-pin-car.d.ts +78 -0
- package/dist/storage/providers/filecoin/filecoin-pin-car.js +155 -0
- package/dist/storage/providers/filecoin/filecoin-pin-client.d.ts +92 -0
- package/dist/storage/providers/filecoin/filecoin-pin-client.js +199 -0
- package/dist/storage/providers/filecoin/filecoin-pin-mapping.d.ts +58 -0
- package/dist/storage/providers/filecoin/filecoin-pin-mapping.js +103 -0
- package/dist/storage/providers/filecoin/filecoin-pin-timeout.d.ts +30 -0
- package/dist/storage/providers/filecoin/filecoin-pin-timeout.js +53 -0
- package/dist/storage/providers/filecoin/filecoin-pin-vendor.d.ts +111 -0
- package/dist/storage/providers/filecoin/filecoin-pin-vendor.js +87 -0
- package/dist/storage/providers/filecoin/hints.d.ts +71 -0
- package/dist/storage/providers/filecoin/hints.js +123 -0
- package/dist/storage/providers/filecoin/index.d.ts +51 -0
- package/dist/storage/providers/filecoin/index.js +103 -0
- package/dist/storage/providers/filecoin/ipfs-cid.d.ts +50 -0
- package/dist/storage/providers/filecoin/ipfs-cid.js +64 -0
- package/dist/storage/providers/filecoin/metadata.d.ts +72 -0
- package/dist/storage/providers/filecoin/metadata.js +137 -0
- package/dist/storage/providers/filecoin/piece-cid.d.ts +48 -0
- package/dist/storage/providers/filecoin/piece-cid.js +57 -0
- package/dist/storage/providers/filecoin/provider-client.d.ts +234 -0
- package/dist/storage/providers/filecoin/provider-client.js +27 -0
- package/dist/storage/providers/filecoin/readiness.d.ts +62 -0
- package/dist/storage/providers/filecoin/readiness.js +85 -0
- package/dist/storage/providers/filecoin/retriever.d.ts +82 -0
- package/dist/storage/providers/filecoin/retriever.js +63 -0
- package/dist/storage/providers/filecoin/skeleton-client.d.ts +36 -0
- package/dist/storage/providers/filecoin/skeleton-client.js +55 -0
- package/dist/storage/providers/filecoin/synapse-client.d.ts +169 -0
- package/dist/storage/providers/filecoin/synapse-client.js +343 -0
- package/dist/storage/providers/filecoin/synapse-construction.d.ts +26 -0
- package/dist/storage/providers/filecoin/synapse-construction.js +47 -0
- package/dist/storage/providers/filecoin/synapse-error-mapping.d.ts +23 -0
- package/dist/storage/providers/filecoin/synapse-error-mapping.js +49 -0
- package/dist/storage/providers/filecoin/synapse-readiness.d.ts +37 -0
- package/dist/storage/providers/filecoin/synapse-readiness.js +231 -0
- package/dist/storage/providers/filecoin/uri.d.ts +49 -0
- package/dist/storage/providers/filecoin/uri.js +84 -0
- package/dist/storage/providers/filecoin/verified-fetch-lifecycle.d.ts +77 -0
- package/dist/storage/providers/filecoin/verified-fetch-lifecycle.js +196 -0
- package/dist/storage/providers/filecoin/verified-fetch-retriever.d.ts +54 -0
- package/dist/storage/providers/filecoin/verified-fetch-retriever.js +81 -0
- package/dist/storage/providers/filecoin/verified-fetch-vendor.d.ts +71 -0
- package/dist/storage/providers/filecoin/verified-fetch-vendor.js +94 -0
- package/dist/storage/raw-content-codec.d.ts +89 -0
- package/dist/storage/raw-content-codec.js +47 -0
- package/dist/storage/raw-content-store-backend-adapter.d.ts +28 -0
- package/dist/storage/raw-content-store-backend-adapter.js +67 -0
- package/dist/storage/raw-content-store.d.ts +228 -0
- package/dist/storage/raw-content-store.js +27 -0
- package/dist/storage/s3-store.d.ts +42 -0
- package/dist/storage/s3-store.js +181 -0
- package/dist/storage/storage-backend-registry.d.ts +58 -0
- package/dist/storage/storage-backend-registry.js +56 -0
- package/dist/storage/storage-backend.d.ts +82 -0
- package/dist/storage/storage-backend.js +14 -0
- package/dist/storage/storage-capabilities.d.ts +56 -0
- package/dist/storage/storage-capabilities.js +170 -0
- package/dist/storage/store-registry.d.ts +67 -0
- package/dist/storage/store-registry.js +77 -0
- package/dist/vector-math.d.ts +15 -0
- package/dist/vector-math.js +31 -0
- package/dist/xml-escape.d.ts +5 -0
- package/dist/xml-escape.js +7 -0
- package/openapi.json +15395 -0
- package/openapi.yaml +10794 -0
- package/package.json +119 -0
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Postgres queries for the `storage_artifacts` table.
|
|
3
|
+
*
|
|
4
|
+
* Step 4 of the storage-sibling plan. Single DB seam for everything
|
|
5
|
+
* artifact-shaped. No HTTP routes, no business logic, no response
|
|
6
|
+
* formatters — those land in Step 5.
|
|
7
|
+
*
|
|
8
|
+
* Owner-scoping rule: every public method except `createStorageArtifact`
|
|
9
|
+
* takes `userId` and routes it into the WHERE clause. Cross-user reads
|
|
10
|
+
* and writes return `null` / zero / no rows; they never throw. The
|
|
11
|
+
* route layer (Step 5) is responsible for translating that into the
|
|
12
|
+
* correct 404 envelope.
|
|
13
|
+
*
|
|
14
|
+
* Internal columns:
|
|
15
|
+
* * `plaintext_hash` / `stored_hash` — visible on the repository's
|
|
16
|
+
* row type for diagnostic queries. The Step-5 response formatter
|
|
17
|
+
* is the only call site that decides whether to expose them on
|
|
18
|
+
* the wire (gated by `discloseContentHash`).
|
|
19
|
+
* * `last_error` — internal failure envelope; same story.
|
|
20
|
+
*
|
|
21
|
+
* The repo writes a closed set of `status` values:
|
|
22
|
+
*
|
|
23
|
+
* stored | pending | available | unavailable | deleting | deleted
|
|
24
|
+
* | delete_failed | failed
|
|
25
|
+
*
|
|
26
|
+
* Mirrors the SDK's `StorageArtifactStatus` and is enforced by the
|
|
27
|
+
* `storage_artifacts_status_check` constraint in `schema.sql`.
|
|
28
|
+
*/
|
|
29
|
+
import { randomUUID } from 'node:crypto';
|
|
30
|
+
/** Closed allowlist of statuses that a fresh claim can transition from. */
|
|
31
|
+
const CLAIMABLE_FROM_STATUSES = [
|
|
32
|
+
'stored',
|
|
33
|
+
'pending',
|
|
34
|
+
'available',
|
|
35
|
+
'unavailable',
|
|
36
|
+
'failed',
|
|
37
|
+
'delete_failed',
|
|
38
|
+
];
|
|
39
|
+
const COLUMNS = 'id, user_id, org_id, project_id, provider, mode, uri, status, ' +
|
|
40
|
+
'size_bytes, content_type, plaintext_hash, stored_hash, content_encoding, ' +
|
|
41
|
+
'disclose_content_hash, identifiers, lifecycle, replication, verification, ' +
|
|
42
|
+
'retrieval, provider_details, metadata, last_error, ' +
|
|
43
|
+
'put_attempt_id, delete_attempt_id, ' +
|
|
44
|
+
'created_at, updated_at, deleted_at';
|
|
45
|
+
/**
|
|
46
|
+
* Insert a new artifact row. Status, provider, and mode must already
|
|
47
|
+
* be validated by the caller — the DB-level CHECKs are a backstop, not
|
|
48
|
+
* a substitute for service-layer input validation.
|
|
49
|
+
*/
|
|
50
|
+
export async function createStorageArtifact(q, input) {
|
|
51
|
+
const result = await q.query(`INSERT INTO storage_artifacts (
|
|
52
|
+
user_id, org_id, project_id, provider, mode, uri, status,
|
|
53
|
+
size_bytes, content_type, plaintext_hash, stored_hash,
|
|
54
|
+
content_encoding, disclose_content_hash,
|
|
55
|
+
identifiers, lifecycle, replication, verification,
|
|
56
|
+
retrieval, provider_details, metadata
|
|
57
|
+
) VALUES (
|
|
58
|
+
$1, $2, $3, $4, $5, $6, $7,
|
|
59
|
+
$8, $9, $10, $11,
|
|
60
|
+
$12, $13,
|
|
61
|
+
$14::jsonb, $15::jsonb, $16::jsonb, $17::jsonb,
|
|
62
|
+
$18::jsonb, $19::jsonb, $20::jsonb
|
|
63
|
+
)
|
|
64
|
+
RETURNING ${COLUMNS}`, buildInsertParams(input));
|
|
65
|
+
return mapRow(result.rows[0]);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Serialize a `CreateStorageArtifactInput` into the positional
|
|
69
|
+
* parameter array `createStorageArtifact` passes to `pool.query`.
|
|
70
|
+
* Extracted so the INSERT itself stays under the workspace
|
|
71
|
+
* complexity ceiling; the `nullify` / `nullableJson` helpers keep
|
|
72
|
+
* each column's null-collapse logic to a single path instead of
|
|
73
|
+
* fanning out one ternary per nullable column.
|
|
74
|
+
*/
|
|
75
|
+
function buildInsertParams(input) {
|
|
76
|
+
return [
|
|
77
|
+
input.userId,
|
|
78
|
+
nullify(input.orgId),
|
|
79
|
+
nullify(input.projectId),
|
|
80
|
+
input.provider,
|
|
81
|
+
input.mode,
|
|
82
|
+
input.uri,
|
|
83
|
+
input.status,
|
|
84
|
+
nullify(input.sizeBytes),
|
|
85
|
+
nullify(input.contentType),
|
|
86
|
+
nullify(input.plaintextHash),
|
|
87
|
+
nullify(input.storedHash),
|
|
88
|
+
input.contentEncoding ?? 'identity',
|
|
89
|
+
input.discloseContentHash ?? false,
|
|
90
|
+
JSON.stringify(input.identifiers ?? {}),
|
|
91
|
+
JSON.stringify(input.lifecycle ?? {}),
|
|
92
|
+
nullableJson(input.replication),
|
|
93
|
+
nullableJson(input.verification),
|
|
94
|
+
nullableJson(input.retrieval),
|
|
95
|
+
nullableJson(input.providerDetails),
|
|
96
|
+
JSON.stringify(input.metadata ?? {}),
|
|
97
|
+
];
|
|
98
|
+
}
|
|
99
|
+
/** Collapse `undefined` / `null` into `null` at the SQL boundary. */
|
|
100
|
+
function nullify(value) {
|
|
101
|
+
return value ?? null;
|
|
102
|
+
}
|
|
103
|
+
/** `JSON.stringify` when present; pass-through `null` for absent inputs. */
|
|
104
|
+
function nullableJson(value) {
|
|
105
|
+
return value === null || value === undefined ? null : JSON.stringify(value);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Insert a `status='pending'` managed artifact row with a fresh
|
|
109
|
+
* `put_attempt_id`. The URI is NULL — set by `recordUploadedArtifact`
|
|
110
|
+
* after the backend put succeeds.
|
|
111
|
+
*/
|
|
112
|
+
export async function claimPendingArtifact(q, input) {
|
|
113
|
+
const claimId = randomUUID();
|
|
114
|
+
const result = await q.query(`INSERT INTO storage_artifacts (
|
|
115
|
+
user_id, org_id, project_id, provider, mode, uri, status,
|
|
116
|
+
content_type, content_encoding, disclose_content_hash,
|
|
117
|
+
identifiers, lifecycle, metadata, put_attempt_id
|
|
118
|
+
) VALUES (
|
|
119
|
+
$1, $2, $3, $4, 'managed', NULL, 'pending',
|
|
120
|
+
$5, $6, $7,
|
|
121
|
+
'{}'::jsonb, '{}'::jsonb, $8::jsonb, $9::uuid
|
|
122
|
+
)
|
|
123
|
+
RETURNING ${COLUMNS}`, [
|
|
124
|
+
input.userId,
|
|
125
|
+
nullify(input.orgId),
|
|
126
|
+
nullify(input.projectId),
|
|
127
|
+
input.provider,
|
|
128
|
+
nullify(input.contentType),
|
|
129
|
+
input.contentEncoding ?? 'identity',
|
|
130
|
+
input.discloseContentHash ?? false,
|
|
131
|
+
JSON.stringify(input.metadata ?? {}),
|
|
132
|
+
claimId,
|
|
133
|
+
]);
|
|
134
|
+
return { row: mapRow(result.rows[0]), claimId };
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* CAS-flip a pending row to `status='stored'`. Matches on
|
|
138
|
+
* `(id, user_id, put_attempt_id, status='pending')` so a stale
|
|
139
|
+
* caller cannot finalize someone else's claim. Returns the updated
|
|
140
|
+
* row when the CAS succeeded; `null` when it lost the race.
|
|
141
|
+
*/
|
|
142
|
+
export async function recordUploadedArtifact(q, input) {
|
|
143
|
+
const result = await q.query(`UPDATE storage_artifacts
|
|
144
|
+
SET status = 'stored',
|
|
145
|
+
uri = $4,
|
|
146
|
+
size_bytes = $5,
|
|
147
|
+
plaintext_hash = $6,
|
|
148
|
+
stored_hash = $7,
|
|
149
|
+
identifiers = $8::jsonb,
|
|
150
|
+
provider_details = $9::jsonb,
|
|
151
|
+
put_attempt_id = NULL,
|
|
152
|
+
updated_at = NOW()
|
|
153
|
+
WHERE id = $1
|
|
154
|
+
AND user_id = $2
|
|
155
|
+
AND put_attempt_id = $3
|
|
156
|
+
AND status = 'pending'
|
|
157
|
+
RETURNING ${COLUMNS}`, [
|
|
158
|
+
input.artifactId,
|
|
159
|
+
input.userId,
|
|
160
|
+
input.putAttemptId,
|
|
161
|
+
input.uri,
|
|
162
|
+
input.sizeBytes,
|
|
163
|
+
input.plaintextHash,
|
|
164
|
+
input.storedHash,
|
|
165
|
+
JSON.stringify(input.identifiers ?? {}),
|
|
166
|
+
nullableJson(input.providerDetails),
|
|
167
|
+
]);
|
|
168
|
+
if (result.rowCount === 0)
|
|
169
|
+
return null;
|
|
170
|
+
return mapRow(result.rows[0]);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* CAS-flip a pending row to `status='failed'` with the supplied
|
|
174
|
+
* `last_error` envelope. Same CAS shape as `recordUploadedArtifact`.
|
|
175
|
+
* Returns the failed row when the CAS succeeded; `null` when it
|
|
176
|
+
* lost the race (claim already cleared by another caller).
|
|
177
|
+
*/
|
|
178
|
+
export async function markPutFailed(q, input) {
|
|
179
|
+
const result = await q.query(`UPDATE storage_artifacts
|
|
180
|
+
SET status = 'failed',
|
|
181
|
+
last_error = $4::jsonb,
|
|
182
|
+
put_attempt_id = NULL,
|
|
183
|
+
updated_at = NOW()
|
|
184
|
+
WHERE id = $1
|
|
185
|
+
AND user_id = $2
|
|
186
|
+
AND put_attempt_id = $3
|
|
187
|
+
AND status = 'pending'
|
|
188
|
+
RETURNING ${COLUMNS}`, [input.artifactId, input.userId, input.putAttemptId, JSON.stringify(input.lastError)]);
|
|
189
|
+
if (result.rowCount === 0)
|
|
190
|
+
return null;
|
|
191
|
+
return mapRow(result.rows[0]);
|
|
192
|
+
}
|
|
193
|
+
/** Owner-scoped lookup. Returns `null` for cross-user / missing / deleted rows. */
|
|
194
|
+
export async function getStorageArtifactById(pool, userId, id) {
|
|
195
|
+
const result = await pool.query(`SELECT ${COLUMNS} FROM storage_artifacts
|
|
196
|
+
WHERE id = $1 AND user_id = $2 AND deleted_at IS NULL`, [id, userId]);
|
|
197
|
+
if (result.rowCount === 0)
|
|
198
|
+
return null;
|
|
199
|
+
return mapRow(result.rows[0]);
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Owner-scoped lookup that DOES return soft-deleted rows. Used by
|
|
203
|
+
* the delete-policy state machine so a second `DELETE` on an
|
|
204
|
+
* already-deleted artifact can return the prior terminal envelope
|
|
205
|
+
* (the plan's idempotency contract) instead of a 404.
|
|
206
|
+
*/
|
|
207
|
+
export async function getStorageArtifactByIdIncludingDeleted(pool, userId, id) {
|
|
208
|
+
const result = await pool.query(`SELECT ${COLUMNS} FROM storage_artifacts
|
|
209
|
+
WHERE id = $1 AND user_id = $2`, [id, userId]);
|
|
210
|
+
if (result.rowCount === 0)
|
|
211
|
+
return null;
|
|
212
|
+
return mapRow(result.rows[0]);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Mark an artifact `status='deleted'` + stamp `deleted_at`. Used by
|
|
216
|
+
* Step 7's document-ingestion refactor when a managed upload
|
|
217
|
+
* replaces a previously-linked pointer artifact (or when a managed
|
|
218
|
+
* artifact's bytes are cleaned up post-document-delete). Owner-
|
|
219
|
+
* scoped + idempotent: a row that is already `deleted` stays so.
|
|
220
|
+
*
|
|
221
|
+
* Distinct from `markDeleteSuccess` because there is no claim id
|
|
222
|
+
* to CAS on — the caller has already confirmed the row should be
|
|
223
|
+
* tombstoned (e.g. the upload service holds the document-row
|
|
224
|
+
* advisory lock).
|
|
225
|
+
*/
|
|
226
|
+
export async function softDeleteArtifactByIdWithClient(q, userId, id) {
|
|
227
|
+
await q.query(`UPDATE storage_artifacts
|
|
228
|
+
SET status = 'deleted',
|
|
229
|
+
deleted_at = COALESCE(deleted_at, NOW()),
|
|
230
|
+
delete_attempt_id = NULL,
|
|
231
|
+
updated_at = NOW()
|
|
232
|
+
WHERE id = $1 AND user_id = $2`, [id, userId]);
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Count active `raw_documents` rows that reference this artifact for
|
|
236
|
+
* the supplied owner. The Step-5 delete route uses this to enforce
|
|
237
|
+
* the `artifact_in_use` 409 envelope when callers omit
|
|
238
|
+
* `policy=with_documents`.
|
|
239
|
+
*
|
|
240
|
+
* Owner-scope invariant: the count includes a row only when BOTH the
|
|
241
|
+
* artifact and the referencing document belong to `$userId`. The
|
|
242
|
+
* schema-level composite FK on `raw_documents(storage_artifact_id,
|
|
243
|
+
* user_id) -> storage_artifacts(id, user_id)` makes the cross-user
|
|
244
|
+
* row impossible, but this join restates the invariant explicitly so
|
|
245
|
+
* the query is unambiguous against any DB that pre-dates the
|
|
246
|
+
* composite FK.
|
|
247
|
+
*/
|
|
248
|
+
export async function countReferencingDocuments(pool, userId, artifactId) {
|
|
249
|
+
const result = await pool.query(`SELECT COUNT(*)::text AS count
|
|
250
|
+
FROM raw_documents AS rd
|
|
251
|
+
INNER JOIN storage_artifacts AS sa
|
|
252
|
+
ON sa.id = rd.storage_artifact_id
|
|
253
|
+
WHERE sa.id = $1
|
|
254
|
+
AND sa.user_id = $2
|
|
255
|
+
AND sa.deleted_at IS NULL
|
|
256
|
+
AND rd.user_id = $2
|
|
257
|
+
AND rd.deleted_at IS NULL`, [artifactId, userId]);
|
|
258
|
+
return Number(result.rows[0].count);
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Return the ids of active `raw_documents` rows that reference this
|
|
262
|
+
* artifact for the supplied owner. Same owner-scope invariant as
|
|
263
|
+
* `countReferencingDocuments`. Used by the delete `with_documents`
|
|
264
|
+
* cascade path so the service can soft-delete each referencing
|
|
265
|
+
* document before flipping the artifact's status.
|
|
266
|
+
*/
|
|
267
|
+
export async function listReferencingDocumentIds(pool, userId, artifactId) {
|
|
268
|
+
const result = await pool.query(`SELECT rd.id
|
|
269
|
+
FROM raw_documents AS rd
|
|
270
|
+
INNER JOIN storage_artifacts AS sa
|
|
271
|
+
ON sa.id = rd.storage_artifact_id
|
|
272
|
+
WHERE sa.id = $1
|
|
273
|
+
AND sa.user_id = $2
|
|
274
|
+
AND sa.deleted_at IS NULL
|
|
275
|
+
AND rd.user_id = $2
|
|
276
|
+
AND rd.deleted_at IS NULL`, [artifactId, userId]);
|
|
277
|
+
return result.rows.map((r) => r.id);
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Atomically transition an artifact into `status='deleting'` and stamp
|
|
281
|
+
* a fresh `delete_attempt_id`. Returns the new claim id on success,
|
|
282
|
+
* `null` when the row is missing, cross-user, or already in a state
|
|
283
|
+
* that forbids a new claim (`deleting` / `deleted`).
|
|
284
|
+
*
|
|
285
|
+
* `delete_failed` rows ARE re-claimable so the delete-retry path can
|
|
286
|
+
* make forward progress; the plan defines the second `DELETE` on a
|
|
287
|
+
* `delete_failed` row as a retry, not an error. The retry path also
|
|
288
|
+
* clears the prior `last_error` so the in-flight artifact never
|
|
289
|
+
* reports `status='deleting'` alongside a stale provider error — the
|
|
290
|
+
* field is repopulated only if THIS attempt fails.
|
|
291
|
+
*/
|
|
292
|
+
export async function claimDeleteAttempt(pool, userId, id) {
|
|
293
|
+
const claimId = randomUUID();
|
|
294
|
+
const result = await pool.query(`UPDATE storage_artifacts
|
|
295
|
+
SET status = 'deleting',
|
|
296
|
+
delete_attempt_id = $3,
|
|
297
|
+
last_error = NULL,
|
|
298
|
+
updated_at = NOW()
|
|
299
|
+
WHERE id = $1
|
|
300
|
+
AND user_id = $2
|
|
301
|
+
AND deleted_at IS NULL
|
|
302
|
+
AND status = ANY($4::text[])
|
|
303
|
+
RETURNING delete_attempt_id`, [id, userId, claimId, CLAIMABLE_FROM_STATUSES]);
|
|
304
|
+
if (result.rowCount === 0)
|
|
305
|
+
return null;
|
|
306
|
+
return { claimId: result.rows[0].delete_attempt_id };
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Finalize a successful delete. The row's `status` flips to `deleted`,
|
|
310
|
+
* `deleted_at` is stamped, and the `delete_attempt_id` is cleared.
|
|
311
|
+
* The CAS condition (matching claim id + current `status='deleting'`)
|
|
312
|
+
* means a stale caller cannot finalize someone else's claim.
|
|
313
|
+
*
|
|
314
|
+
* Throws when no row matches — callers MUST hold a current claim.
|
|
315
|
+
*/
|
|
316
|
+
export async function markDeleteSuccess(q, args) {
|
|
317
|
+
const result = await q.query(`UPDATE storage_artifacts
|
|
318
|
+
SET status = 'deleted',
|
|
319
|
+
deleted_at = NOW(),
|
|
320
|
+
delete_attempt_id = NULL,
|
|
321
|
+
last_error = NULL,
|
|
322
|
+
updated_at = NOW()
|
|
323
|
+
WHERE id = $1
|
|
324
|
+
AND user_id = $2
|
|
325
|
+
AND delete_attempt_id = $3
|
|
326
|
+
AND status = 'deleting'
|
|
327
|
+
RETURNING ${COLUMNS}`, [args.id, args.userId, args.claimId]);
|
|
328
|
+
if (result.rowCount === 0) {
|
|
329
|
+
throw new Error(`markDeleteSuccess: no matching claim (id=${args.id} claim=${args.claimId})`);
|
|
330
|
+
}
|
|
331
|
+
return mapRow(result.rows[0]);
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Record a provider-cleanup failure. Same CAS guard as
|
|
335
|
+
* `markDeleteSuccess`; `last_error` is replaced with the supplied
|
|
336
|
+
* envelope so the caller can retry against a clean state. Status
|
|
337
|
+
* lands on `delete_failed`, which is re-claimable by a subsequent
|
|
338
|
+
* `claimDeleteAttempt`.
|
|
339
|
+
*/
|
|
340
|
+
export async function markDeleteFailed(q, args) {
|
|
341
|
+
const result = await q.query(`UPDATE storage_artifacts
|
|
342
|
+
SET status = 'delete_failed',
|
|
343
|
+
delete_attempt_id = NULL,
|
|
344
|
+
last_error = $4::jsonb,
|
|
345
|
+
updated_at = NOW()
|
|
346
|
+
WHERE id = $1
|
|
347
|
+
AND user_id = $2
|
|
348
|
+
AND delete_attempt_id = $3
|
|
349
|
+
AND status = 'deleting'
|
|
350
|
+
RETURNING ${COLUMNS}`, [args.id, args.userId, args.claimId, JSON.stringify(args.lastError)]);
|
|
351
|
+
if (result.rowCount === 0) {
|
|
352
|
+
throw new Error(`markDeleteFailed: no matching claim (id=${args.id} claim=${args.claimId})`);
|
|
353
|
+
}
|
|
354
|
+
return mapRow(result.rows[0]);
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Release a `deleting` claim without finalizing. Used by the
|
|
358
|
+
* delete-cascade race fix (Commit D): claim runs BEFORE the
|
|
359
|
+
* reference-count gate so concurrent INSERTs cannot slip in a new
|
|
360
|
+
* link, but if the gate then throws `ArtifactInUseError` we must
|
|
361
|
+
* revert the row from `deleting` back to its pre-claim status
|
|
362
|
+
* (and restore the pre-claim `last_error`, since `claimDeleteAttempt`
|
|
363
|
+
* cleared it). CAS-scoped to the row's `(user_id, delete_attempt_id,
|
|
364
|
+
* status='deleting')` so a stale caller cannot revert someone
|
|
365
|
+
* else's claim. Returns true on a real revert, false on CAS miss
|
|
366
|
+
* (e.g., concurrent recovery already finalized the row).
|
|
367
|
+
*/
|
|
368
|
+
export async function releaseDeleteClaim(pool, args) {
|
|
369
|
+
const result = await pool.query(`UPDATE storage_artifacts
|
|
370
|
+
SET status = $4,
|
|
371
|
+
last_error = $5::jsonb,
|
|
372
|
+
delete_attempt_id = NULL,
|
|
373
|
+
updated_at = NOW()
|
|
374
|
+
WHERE id = $1
|
|
375
|
+
AND user_id = $2
|
|
376
|
+
AND delete_attempt_id = $3
|
|
377
|
+
AND status = 'deleting'`, [
|
|
378
|
+
args.id, args.userId, args.claimId, args.restoreStatus,
|
|
379
|
+
args.restoreLastError === null ? null : JSON.stringify(args.restoreLastError),
|
|
380
|
+
]);
|
|
381
|
+
return (result.rowCount ?? 0) > 0;
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Closed set of statuses that refuse a new link to the artifact.
|
|
385
|
+
* Used by `assertArtifactLinkable`. The only relink site today is
|
|
386
|
+
* `swapToManagedArtifact` (which observes an existing prior
|
|
387
|
+
* artifact); `registerWithPointerArtifact` ALWAYS creates a fresh
|
|
388
|
+
* artifact in the same transaction and never re-links to an
|
|
389
|
+
* existing row, so the guard is not load-bearing there. The check
|
|
390
|
+
* stays callable from any future path that links to an existing
|
|
391
|
+
* artifact.
|
|
392
|
+
*/
|
|
393
|
+
const UNLINKABLE_STATUSES = [
|
|
394
|
+
'deleting',
|
|
395
|
+
'deleted',
|
|
396
|
+
'delete_failed',
|
|
397
|
+
];
|
|
398
|
+
/**
|
|
399
|
+
* Sentinel thrown by `assertArtifactLinkable` when a caller tries
|
|
400
|
+
* to link a `raw_documents.storage_artifact_id` to an artifact
|
|
401
|
+
* that has already entered (or completed) its delete lifecycle.
|
|
402
|
+
* The link-write sites surface this so a concurrent delete cannot
|
|
403
|
+
* race with a fresh link and end up with a document pointing at
|
|
404
|
+
* deleted content.
|
|
405
|
+
*/
|
|
406
|
+
export class ArtifactNotLinkableError extends Error {
|
|
407
|
+
artifactId;
|
|
408
|
+
status;
|
|
409
|
+
constructor(artifactId, status) {
|
|
410
|
+
super(`storage artifact ${artifactId} is not linkable (status='${status}'); ` +
|
|
411
|
+
'delete has already started or completed');
|
|
412
|
+
this.name = 'ArtifactNotLinkableError';
|
|
413
|
+
this.artifactId = artifactId;
|
|
414
|
+
this.status = status;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Refuse to link a `raw_documents` row to an artifact whose status
|
|
419
|
+
* is in `UNLINKABLE_STATUSES`. Throws `ArtifactNotLinkableError`
|
|
420
|
+
* on a hit; no-ops otherwise. Takes a `PoolClient` so the caller
|
|
421
|
+
* can scope the check to its own transaction.
|
|
422
|
+
*/
|
|
423
|
+
export async function assertArtifactLinkable(q, userId, artifactId) {
|
|
424
|
+
const result = await q.query(`SELECT status FROM storage_artifacts
|
|
425
|
+
WHERE id = $1 AND user_id = $2`, [artifactId, userId]);
|
|
426
|
+
if (result.rowCount === 0)
|
|
427
|
+
return;
|
|
428
|
+
const status = result.rows[0].status;
|
|
429
|
+
if (UNLINKABLE_STATUSES.includes(status)) {
|
|
430
|
+
throw new ArtifactNotLinkableError(artifactId, status);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* List a user's artifacts in `(created_at DESC, id DESC)` order with
|
|
435
|
+
* keyset pagination. The cursor encodes the last row of the previous
|
|
436
|
+
* page; pass `undefined` for the first page. Returns `nextCursor=null`
|
|
437
|
+
* when the caller has reached the end of the user's rows.
|
|
438
|
+
*
|
|
439
|
+
* Soft-deleted rows (`deleted_at IS NOT NULL`) are excluded from the
|
|
440
|
+
* listing — the Step-5 list endpoint does NOT show tombstones by
|
|
441
|
+
* default.
|
|
442
|
+
*/
|
|
443
|
+
export async function listArtifactsForUser(pool, userId, opts) {
|
|
444
|
+
if (!Number.isInteger(opts.limit) || opts.limit <= 0) {
|
|
445
|
+
throw new Error(`listArtifactsForUser: limit must be a positive integer (got ${opts.limit})`);
|
|
446
|
+
}
|
|
447
|
+
const cursorClause = opts.cursor
|
|
448
|
+
? 'AND (created_at, id) < ($2::timestamptz, $3::uuid)'
|
|
449
|
+
: '';
|
|
450
|
+
const params = [userId];
|
|
451
|
+
if (opts.cursor) {
|
|
452
|
+
params.push(opts.cursor.createdAt, opts.cursor.id);
|
|
453
|
+
}
|
|
454
|
+
params.push(opts.limit + 1);
|
|
455
|
+
const limitPlaceholder = `$${params.length}`;
|
|
456
|
+
const result = await pool.query(`SELECT ${COLUMNS} FROM storage_artifacts
|
|
457
|
+
WHERE user_id = $1 AND deleted_at IS NULL ${cursorClause}
|
|
458
|
+
ORDER BY created_at DESC, id DESC
|
|
459
|
+
LIMIT ${limitPlaceholder}`, params);
|
|
460
|
+
const allRows = result.rows.map(mapRow);
|
|
461
|
+
const rows = allRows.slice(0, opts.limit);
|
|
462
|
+
const nextCursor = allRows.length > opts.limit && rows.length > 0
|
|
463
|
+
? { createdAt: rows[rows.length - 1].createdAt.toISOString(), id: rows[rows.length - 1].id }
|
|
464
|
+
: null;
|
|
465
|
+
return { rows, nextCursor };
|
|
466
|
+
}
|
|
467
|
+
function mapRow(row) {
|
|
468
|
+
return {
|
|
469
|
+
id: row.id,
|
|
470
|
+
userId: row.user_id,
|
|
471
|
+
orgId: row.org_id,
|
|
472
|
+
projectId: row.project_id,
|
|
473
|
+
provider: row.provider,
|
|
474
|
+
mode: row.mode,
|
|
475
|
+
uri: row.uri,
|
|
476
|
+
status: row.status,
|
|
477
|
+
sizeBytes: row.size_bytes === null ? null : Number(row.size_bytes),
|
|
478
|
+
contentType: row.content_type,
|
|
479
|
+
plaintextHash: row.plaintext_hash,
|
|
480
|
+
storedHash: row.stored_hash,
|
|
481
|
+
contentEncoding: row.content_encoding,
|
|
482
|
+
discloseContentHash: row.disclose_content_hash,
|
|
483
|
+
identifiers: row.identifiers ?? {},
|
|
484
|
+
lifecycle: row.lifecycle ?? {},
|
|
485
|
+
replication: row.replication,
|
|
486
|
+
verification: row.verification,
|
|
487
|
+
retrieval: row.retrieval,
|
|
488
|
+
providerDetails: row.provider_details,
|
|
489
|
+
metadata: row.metadata ?? {},
|
|
490
|
+
lastError: row.last_error,
|
|
491
|
+
putAttemptId: row.put_attempt_id,
|
|
492
|
+
deleteAttemptId: row.delete_attempt_id,
|
|
493
|
+
createdAt: row.created_at,
|
|
494
|
+
updatedAt: row.updated_at,
|
|
495
|
+
deletedAt: row.deleted_at,
|
|
496
|
+
};
|
|
497
|
+
}
|