@sentropic/artifacts 0.2.12 → 0.2.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/CHANGELOG.md +93 -0
  2. package/README.md +45 -10
  3. package/dist/adapters/local-blob.d.ts +3 -0
  4. package/dist/adapters/local-blob.js +93 -0
  5. package/dist/adapters/postgres-management.js +3 -2
  6. package/dist/adapters/postgres-reader-consent.d.ts +11 -0
  7. package/dist/adapters/postgres-reader-consent.js +109 -0
  8. package/dist/adapters/postgres-uploads.d.ts +14 -0
  9. package/dist/adapters/postgres-uploads.js +174 -0
  10. package/dist/adapters/postgres.js +4 -3
  11. package/dist/adapters/s3-uploads.d.ts +33 -0
  12. package/dist/adapters/s3-uploads.js +105 -0
  13. package/dist/adapters/s3.js +23 -0
  14. package/dist/adapters/sqlite-activation.d.ts +20 -0
  15. package/dist/adapters/sqlite-activation.js +205 -0
  16. package/dist/adapters/sqlite-commit.d.ts +15 -0
  17. package/dist/adapters/sqlite-commit.js +89 -0
  18. package/dist/adapters/sqlite-management.d.ts +10 -0
  19. package/dist/adapters/sqlite-management.js +60 -0
  20. package/dist/adapters/sqlite-sql.d.ts +32 -0
  21. package/dist/adapters/sqlite-sql.js +36 -0
  22. package/dist/adapters/sqlite-store.d.ts +12 -0
  23. package/dist/adapters/sqlite-store.js +410 -0
  24. package/dist/adapters/sqlite.d.ts +33 -0
  25. package/dist/adapters/sqlite.js +95 -0
  26. package/dist/content/handler.d.ts +5 -0
  27. package/dist/content/handler.js +80 -9
  28. package/dist/content/headers.js +12 -0
  29. package/dist/http.d.ts +32 -2
  30. package/dist/http.js +615 -13
  31. package/dist/mcp-core.d.ts +3 -3
  32. package/dist/mcp-core.js +90 -4
  33. package/dist/profile/contracts.d.ts +1 -1
  34. package/dist/profile/contracts.js +7 -0
  35. package/dist/profile/index.js +43 -2
  36. package/dist/profile/radar.js +7 -0
  37. package/dist/profile/schemas.js +119 -0
  38. package/dist/profile/validation.d.ts +11 -0
  39. package/dist/profile/validation.js +98 -6
  40. package/dist/server/access.d.ts +2 -0
  41. package/dist/server/command-identity.d.ts +1 -1
  42. package/dist/server/command-identity.js +6 -3
  43. package/dist/server/consent.d.ts +65 -0
  44. package/dist/server/consent.js +42 -0
  45. package/dist/server/gc.d.ts +42 -0
  46. package/dist/server/gc.js +98 -0
  47. package/dist/server/management.d.ts +3 -1
  48. package/dist/server/management.js +48 -7
  49. package/dist/server/metrics.d.ts +21 -0
  50. package/dist/server/metrics.js +6 -0
  51. package/dist/server/mime.d.ts +26 -2
  52. package/dist/server/mime.js +124 -2
  53. package/dist/server/ports.d.ts +29 -0
  54. package/dist/server/ports.js +11 -0
  55. package/dist/server/publication-limits.d.ts +20 -0
  56. package/dist/server/publication-limits.js +27 -1
  57. package/dist/server/publication.d.ts +14 -0
  58. package/dist/server/publication.js +10 -2
  59. package/dist/server/reader-store.d.ts +58 -0
  60. package/dist/server/reader-store.js +109 -0
  61. package/dist/server/source-read.d.ts +12 -9
  62. package/dist/server/source-read.js +17 -5
  63. package/dist/server/templates.js +50 -2
  64. package/dist/server/upload-page.d.ts +157 -0
  65. package/dist/server/upload-page.js +438 -0
  66. package/dist/server/uploads.d.ts +229 -19
  67. package/dist/server/uploads.js +387 -80
  68. package/migrations/0007_uploads.sql +43 -0
  69. package/migrations/0008_reader_consent.sql +48 -0
  70. package/migrations/0009_upload_pages.sql +25 -0
  71. package/package.json +34 -1
  72. package/sql/reader-consent.sql +47 -0
  73. package/sql/sqlite/0001_artifacts.sql +156 -0
  74. package/sql/sqlite/0002_activation.sql +73 -0
@@ -1,32 +1,242 @@
1
- export interface BlobRef {
2
- digest: string;
3
- size: number;
1
+ import type { ArtifactsMetrics } from './metrics.js';
2
+ import type { RefcountStore } from './gc.js';
3
+ import type { PublicationBlobStore } from './publication.js';
4
+ export declare const UPLOAD_URL_TTL_SECONDS = 600;
5
+ export declare const UPLOAD_SESSION_TTL_MS: number;
6
+ /**
7
+ * Files parity LOT 2 §D — staged-file TTL (host option, ceiling 7 days),
8
+ * orphan staging-key grace, and the per-run orphan bound.
9
+ */
10
+ export declare const STAGED_FILE_TTL_MS: number;
11
+ export declare const STAGED_FILE_TTL_MAX_MS: number;
12
+ export declare const ORPHAN_STAGING_GRACE_MS: number;
13
+ export declare const ORPHAN_SWEEP_MAX_KEYS = 500;
14
+ export type UploadSessionState = 'open' | 'committed' | 'discarded' | 'expired';
15
+ export interface UploadSession {
16
+ readonly uploadId: string;
17
+ readonly artifactId: string;
18
+ readonly storageScopeId: string;
19
+ readonly ownerId: string;
20
+ readonly mimeType: string;
21
+ readonly maxSize: number;
22
+ readonly state: UploadSessionState;
23
+ readonly expiresAt: string;
24
+ readonly createdAt: string;
25
+ /** Browser page that minted this session (agent-channel rows stay null). */
26
+ readonly pageSessionId?: string;
27
+ }
28
+ export interface StagedFileRecord {
29
+ readonly uploadId: string;
30
+ readonly artifactId: string;
31
+ readonly path: string;
32
+ readonly mimeType: string;
33
+ readonly digest: string;
34
+ readonly size: number;
35
+ readonly createdAt: string;
36
+ /** Browser page the file was committed through (agent-channel rows stay null). */
37
+ readonly pageSessionId?: string;
38
+ }
39
+ /** Durable session ledger (Postgres in the host via migration 0007; memory double in tests). */
40
+ export interface UploadSessionStore {
41
+ create(session: UploadSession): Promise<void>;
42
+ get(uploadId: string): Promise<UploadSession | undefined>;
43
+ /** Open sessions of one principal in one scope (quota check). */
44
+ countOpen(storageScopeId: string, ownerId: string): Promise<number>;
45
+ /** Sessions still open whose expires_at passed (sweeper input). */
46
+ listExpiredOpen(now: Date): Promise<UploadSession[]>;
47
+ markCommitted(uploadId: string): Promise<void>;
48
+ markExpired(uploadId: string): Promise<void>;
49
+ recordStagedFile(record: StagedFileRecord): Promise<void>;
50
+ getStagedFile(uploadId: string): Promise<StagedFileRecord | undefined>;
51
+ listStagedFiles(artifactId: string): Promise<StagedFileRecord[]>;
52
+ /** LOT 2 §D.2: reserved bytes of open sessions in one scope (quota check). */
53
+ sumOpenMaxSize(storageScopeId: string): Promise<number>;
54
+ /** LOT 2 §D.2: staged bytes in one scope (quota check). */
55
+ sumStagedBytesByScope(storageScopeId: string): Promise<number>;
56
+ /** LOT 2 §A.3: sessions minted through one browser page (file-count bound). */
57
+ listPageSessions(pageSessionId: string): Promise<UploadSession[]>;
58
+ /**
59
+ * LOT 2 §D.2: serialize the quota check and insert per scope (race-proof
60
+ * backstop). The PG store runs `action` on a transaction holding the scope
61
+ * advisory lock; the memory double runs it under a per-scope mutex. The
62
+ * service performs every quota read and the insert through `locked`.
63
+ */
64
+ withScopeLock<T>(storageScopeId: string, action: (locked: UploadSessionStore) => Promise<T>): Promise<T>;
65
+ /** LOT 2 §D.1: staged rows older than the TTL (sweeper pass 2 input). */
66
+ listExpiredStaged(now: Date, ttlMs: number): Promise<StagedFileRecord[]>;
67
+ deleteStagedFile(uploadId: string): Promise<void>;
4
68
  }
69
+ /** Byte store under the staging prefix (S3 in the host; memory double in tests). */
5
70
  export interface StagingStore {
6
- write(uploadId: string, bytes: Uint8Array): Promise<void>;
7
- read(uploadId: string): Promise<Uint8Array>;
8
- discard(uploadId: string): Promise<void>;
71
+ put(key: string, bytes: Uint8Array): Promise<void>;
72
+ get(key: string): Promise<Uint8Array | undefined>;
73
+ delete(key: string): Promise<void>;
74
+ listKeys(prefix: string): Promise<string[]>;
75
+ /** LOT 2 §D.1 orphan pass: keys with last write time (optional; absent ⇒ pass skipped). */
76
+ listEntries?(prefix: string): Promise<{
77
+ key: string;
78
+ lastModified: string;
79
+ }[]>;
9
80
  }
81
+ /**
82
+ * Presigner port. The signature binds content-type, the EXACT content-length
83
+ * (declared size) and, when known at signing time, x-amz-checksum-sha256;
84
+ * no content-length range exists in a SigV4 presigned PUT (review finding 3).
85
+ */
86
+ export interface UploadSignerPort {
87
+ signPut(input: {
88
+ key: string;
89
+ contentType: string;
90
+ contentLength: number;
91
+ /** Canonical `sha256:<hex>` digest, when known at signing time. */
92
+ checksumSha256?: string;
93
+ expiresInSeconds: number;
94
+ }): Promise<{
95
+ url: string;
96
+ method: 'PUT';
97
+ /** Headers the client must send with exactly these values. */
98
+ headers: Record<string, string>;
99
+ expiresAt: string;
100
+ }>;
101
+ }
102
+ export declare const STAGING_KEY_PATTERN: RegExp;
103
+ /** Staging key of one upload session. */
104
+ export declare function stagingKey(storageScopeId: string, uploadId: string): string;
105
+ /** Staging prefix of one scope (LOT 2 §D.1 orphan pass enumerates it). */
106
+ export declare function stagingPrefix(storageScopeId: string): string;
107
+ /**
108
+ * Value of the `x-amz-checksum-sha256` header for a canonical `sha256:<hex>`
109
+ * digest: S3 expects the base64 of the raw digest bytes (no `sha256:` prefix).
110
+ */
111
+ export declare function checksumHeaderValue(digest: string): string;
112
+ /** Reject anything but a staging key, before any I/O. */
113
+ export declare function assertStagingKey(key: string): void;
114
+ /** Reject empty or escaping staging prefixes before enumeration. */
115
+ export declare function assertStagingPrefix(prefix: string): void;
10
116
  /** In-memory StagingStore double for unit tests. */
11
- export declare function createMemoryStaging(): StagingStore;
12
- export interface BeginUploadInput {
117
+ export declare function createMemoryStaging(now?: () => Date): StagingStore;
118
+ /** In-memory UploadSessionStore double for unit tests. */
119
+ export declare function createMemoryUploadSessionStore(now?: () => Date): UploadSessionStore;
120
+ /**
121
+ * Memory signer double: records every signed PUT and simulates S3's
122
+ * enforcement of the signed headers (exact content-length equality) and of
123
+ * the checksum header (body must hash to it when present).
124
+ */
125
+ export declare function createMemoryUploadSigner(): {
126
+ signer: UploadSignerPort;
127
+ signed: {
128
+ key: string;
129
+ contentType: string;
130
+ contentLength: number;
131
+ checksumSha256?: string | undefined;
132
+ expiresAt: string;
133
+ url: string;
134
+ }[];
135
+ /** Simulate the client PUT against "S3": signed headers are enforced exactly. */
136
+ put(signedPut: {
137
+ key: string;
138
+ contentType: string;
139
+ contentLength: number;
140
+ checksumSha256?: string;
141
+ }, bytes: Uint8Array, headers: Record<string, string>): void;
142
+ };
143
+ export interface CreateUploadInput {
144
+ artifactId: string;
145
+ storageScopeId: string;
146
+ ownerId: string;
13
147
  mimeType: string;
148
+ /** Declared size; clamped to the host budget. Bound as the exact signed Content-Length. */
14
149
  maxSize?: number;
150
+ /** Browser page that mints this session (agent-channel rows stay null). */
151
+ pageSessionId?: string;
152
+ /** Page file bound enforced with the quota check; required when pageSessionId is set. */
153
+ pageFileLimit?: number;
154
+ }
155
+ /** Digest-addressed CAS blob key of one scope and canonical `sha256:<hex>` digest. */
156
+ export declare function casBlobKey(storageScopeId: string, digest: string): string;
157
+ /** LOT 2 §D.1: true when a staged file is older than the staged-file TTL. */
158
+ export declare function stagedFileExpired(record: Pick<StagedFileRecord, 'createdAt'>, nowMs: number, ttlMs: number): boolean;
159
+ export interface CreateUploadResult {
160
+ uploadId: string;
161
+ uploadUrl: string;
162
+ method: 'PUT';
163
+ /** Presigned-URL expiry (≤ 10 minutes). */
164
+ expiresAt: string;
165
+ maxSize: number;
166
+ /** Headers the client must send with exactly these values (bound by the signature). */
167
+ requiredHeaders: Record<string, string>;
168
+ /**
169
+ * The PUT must also carry this header with the base64 SHA-256 of the body;
170
+ * S3 validates it against the body (BadDigest on mismatch) and commit
171
+ * re-verifies size and digest server-side.
172
+ */
173
+ checksumHeader: 'x-amz-checksum-sha256';
174
+ stagingKey: string;
175
+ }
176
+ export interface CommitUploadInput {
177
+ uploadId: string;
178
+ artifactId: string;
179
+ storageScopeId: string;
180
+ ownerId: string;
181
+ path: string;
182
+ mimeType: string;
183
+ digest: string;
184
+ size: number;
15
185
  }
16
186
  export interface UploadServiceOptions {
187
+ /** Host ceiling for one upload (min of profile releaseFileBytesMax and the server maximum). */
17
188
  maxUploadBytes: number;
189
+ maxSessionsPerPrincipal: number;
190
+ /** LOT 2 §D.2: per-scope staging aggregate (reserved open + staged bytes). Absent ⇒ no aggregate bound. */
191
+ stagingQuotaBytesPerScope?: number;
192
+ /** LOT 2 §D.1: staged-file TTL (host option, ceiling 7 days). Absent ⇒ 24 h. */
193
+ stagedFileTtlMs?: number;
194
+ /** Metric channel of sessions minted through this service (`agent` or `browser`). */
195
+ channel?: string;
196
+ metrics?: ArtifactsMetrics;
197
+ now?: () => Date;
198
+ ids?: () => string;
18
199
  }
19
200
  export interface UploadService {
20
- begin(input: BeginUploadInput): Promise<{
21
- uploadId: string;
22
- }>;
23
- /** Check that the session is open and optionally validate its sequential offset before decoding. */
24
- remainingBytes(uploadId: string, offset?: number): number;
25
- append(uploadId: string, offset: number, chunk: Uint8Array): Promise<{
201
+ create(input: CreateUploadInput): Promise<CreateUploadResult>;
202
+ /**
203
+ * Load the session for commit: must exist, be open, unexpired and bound to
204
+ * the same artifact + scope + owner. Returns the session (caller reads the
205
+ * staged bytes once and verifies size/digest/MIME before pinning).
206
+ */
207
+ sessionForCommit(input: CommitUploadInput): Promise<UploadSession>;
208
+ /** Verify the staged bytes against the declared size, digest and MIME type. */
209
+ verifyStaged(session: UploadSession, bytes: Uint8Array, expected: {
210
+ digest: string;
26
211
  size: number;
27
- }>;
28
- /** Validate the commit snapshot before consuming the session; rejected validation remains retryable. */
29
- commit(uploadId: string, expected: BlobRef, validate?: (bytes: Uint8Array) => void): Promise<BlobRef>;
212
+ mimeType: string;
213
+ }): void;
214
+ /** Record the staged file and close the session after a successful promotion. */
215
+ completeCommit(session: UploadSession, record: Omit<StagedFileRecord, 'uploadId' | 'artifactId' | 'createdAt'>): Promise<StagedFileRecord>;
216
+ }
217
+ export declare function createUploadService(deps: {
218
+ sessions: UploadSessionStore;
219
+ staging: StagingStore;
220
+ signer: UploadSignerPort;
221
+ }, options: UploadServiceOptions): UploadService;
222
+ /**
223
+ * Files parity LOT 2 — shared commit core for both channels (agent MCP and
224
+ * browser page). Verification and pin logic live here once: the MCP handler
225
+ * and the page service both call it, so neither duplicates the other.
226
+ */
227
+ export interface CommitStagedUploadDeps {
228
+ sessions: UploadSessionStore;
229
+ staging: StagingStore;
230
+ blobs: PublicationBlobStore;
231
+ refs: RefcountStore;
232
+ /** Per-artifact staged caps (`releaseFilesMax`/`releaseTotalBytesMax`). */
233
+ budget: {
234
+ filesMax: number;
235
+ totalBytesMax: number;
236
+ };
237
+ metrics?: ArtifactsMetrics;
238
+ }
239
+ export interface CommitStagedUploadInput extends CommitUploadInput {
240
+ pageSessionId?: string;
30
241
  }
31
- /** Create an upload service over an injected staging store (no S3 imports). */
32
- export declare function createUploadService(staging: StagingStore, options: UploadServiceOptions, ids?: () => string): UploadService;
242
+ export declare function commitStagedUpload(service: UploadService, deps: CommitStagedUploadDeps, input: CommitStagedUploadInput, channel?: string): Promise<StagedFileRecord>;