@codelia/storage 0.1.83 → 0.1.85
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/index.cjs +2097 -306
- package/dist/index.d.cts +223 -12
- package/dist/index.d.ts +223 -12
- package/dist/index.js +2112 -301
- package/package.json +3 -3
package/dist/index.d.cts
CHANGED
|
@@ -1,15 +1,23 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { StoragePaths, ResolveStorageOptions, RunEventStoreFactory, RunEventStoreInit, SessionStore,
|
|
1
|
+
import * as _codelia_core from '@codelia/core';
|
|
2
|
+
import { StoragePaths, ResolveStorageOptions, RunEventStoreFactory, RunEventStoreInit, SessionStore, ConversationSearchResult, ConversationMessage, SessionStateStore, SessionState, SessionStateSummary, StoragePathService, SessionRecord, ToolOutputCacheStore, ToolOutputCacheRecord, ToolOutputRef, ToolOutputCacheReadOptions, ToolOutputCacheSearchOptions } from '@codelia/core';
|
|
3
3
|
export { ResolveStorageOptions, StorageLayout, StoragePathService, StoragePaths } from '@codelia/core';
|
|
4
|
+
import { GeneratedImageMetadata, ImageArtifactRef, ImageMediaType, AttachedImageMetadata, ImageArtifactMetadata, AttachmentRef, ApprovalMode, GoalRecord, SubagentLineage, AgentMessage, TaskTerminationReason, TaskUsage } from '@codelia/shared-types';
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
type AttachmentWriteOptions = {
|
|
7
|
+
artifactId?: string;
|
|
8
|
+
ownerSessionId?: string;
|
|
9
|
+
};
|
|
10
|
+
/** Workspace image storage with session-scoped access for copied files. */
|
|
6
11
|
declare class ImageArtifactStore {
|
|
7
12
|
readonly root: string;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
13
|
+
private readonly getSessionId?;
|
|
14
|
+
constructor(root: string, getSessionId?: (() => string | null) | undefined);
|
|
15
|
+
save(bytes: Buffer, provenance: Omit<GeneratedImageMetadata, keyof ImageArtifactRef | "created_at">, mediaType?: "image/png" | "image/jpeg"): Promise<GeneratedImageMetadata>;
|
|
16
|
+
saveAttachment(bytes: Buffer, mediaType: ImageMediaType, { artifactId, ownerSessionId, }?: AttachmentWriteOptions): Promise<AttachedImageMetadata>;
|
|
17
|
+
/** Roll back a newly uploaded attachment before its ownership batch is published. */
|
|
18
|
+
discardUnpublishedAttachment(ref: ImageArtifactRef, sessionId: string): Promise<void>;
|
|
11
19
|
private persist;
|
|
12
|
-
read(artifactId: string): Promise<{
|
|
20
|
+
read(artifactId: string, sessionId?: string | null): Promise<{
|
|
13
21
|
metadata: ImageArtifactMetadata;
|
|
14
22
|
bytes: Buffer;
|
|
15
23
|
}>;
|
|
@@ -24,6 +32,47 @@ declare class ImageArtifactStore {
|
|
|
24
32
|
}>;
|
|
25
33
|
}
|
|
26
34
|
|
|
35
|
+
type StoredAttachment = {
|
|
36
|
+
ref: AttachmentRef;
|
|
37
|
+
imageRef?: ImageArtifactRef;
|
|
38
|
+
};
|
|
39
|
+
/** Host-owned originals and ownership index. commit must publish the entire batch atomically. */
|
|
40
|
+
type AttachmentStore = {
|
|
41
|
+
commit(sessionId: string, entries: readonly {
|
|
42
|
+
record: StoredAttachment;
|
|
43
|
+
bytes: Buffer;
|
|
44
|
+
}[]): Promise<void>;
|
|
45
|
+
/** Validate ownership and integrity before returning an admitted reference. */
|
|
46
|
+
lookup(attachmentId: string, sessionId: string): Promise<StoredAttachment | null>;
|
|
47
|
+
/** Check ownership and integrity of the returned byte range. */
|
|
48
|
+
read(attachmentId: string, sessionId: string, offset: number, length: number): Promise<Uint8Array>;
|
|
49
|
+
resolveImage?: ImageArtifactStore["resolve"];
|
|
50
|
+
};
|
|
51
|
+
declare const createAttachmentId: () => string;
|
|
52
|
+
declare const isAttachmentId: (id: string) => boolean;
|
|
53
|
+
/** An ownership marker makes every member of a batch visible at once. */
|
|
54
|
+
declare class FileAttachmentStore implements AttachmentStore {
|
|
55
|
+
readonly root: string;
|
|
56
|
+
private readonly images;
|
|
57
|
+
constructor(root: string, images: ImageArtifactStore);
|
|
58
|
+
resolveImage: NonNullable<AttachmentStore["resolveImage"]>;
|
|
59
|
+
commit(sessionId: string, entries: readonly {
|
|
60
|
+
record: StoredAttachment;
|
|
61
|
+
bytes: Buffer;
|
|
62
|
+
}[]): Promise<void>;
|
|
63
|
+
lookup(attachmentId: string, sessionId: string): Promise<StoredAttachment | null>;
|
|
64
|
+
read(attachmentId: string, sessionId: string, offset: number, length: number): Promise<Uint8Array>;
|
|
65
|
+
private metadata;
|
|
66
|
+
private readImage;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
declare function detectImageAttachmentType(bytes: Buffer): ImageMediaType | undefined;
|
|
70
|
+
/** Bounded header validation; attachment originals are not decoded or re-encoded. */
|
|
71
|
+
declare function validateImageAttachment(bytes: Buffer, mediaType: ImageMediaType): {
|
|
72
|
+
width: number;
|
|
73
|
+
height: number;
|
|
74
|
+
};
|
|
75
|
+
|
|
27
76
|
declare const IMAGE_MAX_BYTES: number;
|
|
28
77
|
/** Validate chunks, CRC and bounded decompression before accepting provider bytes. */
|
|
29
78
|
declare function validateImagePng(bytes: Buffer): Promise<{
|
|
@@ -54,6 +103,11 @@ declare class McpAuthStore {
|
|
|
54
103
|
declare function resolveStoragePaths(options?: ResolveStorageOptions): StoragePaths;
|
|
55
104
|
declare function ensureStorageDirs(paths: StoragePaths): Promise<void>;
|
|
56
105
|
|
|
106
|
+
/** Identity guards PID reuse. Failure is unknown, never permission to kill or remove a live-writer marker. */
|
|
107
|
+
declare const getProcessIdentity: (pid: number, options?: {
|
|
108
|
+
canonicalEnvironment?: boolean;
|
|
109
|
+
}) => Promise<string | null>;
|
|
110
|
+
|
|
57
111
|
type ProjectsPolicyFile = {
|
|
58
112
|
version: 1;
|
|
59
113
|
default?: {
|
|
@@ -71,12 +125,97 @@ declare class ProjectsPolicyStore {
|
|
|
71
125
|
save(data: ProjectsPolicyFile): Promise<void>;
|
|
72
126
|
}
|
|
73
127
|
|
|
128
|
+
/** One run log of a session, as recorded in `state.db` (`session_runs`). */
|
|
129
|
+
type SessionRunEntry = {
|
|
130
|
+
session_id: string;
|
|
131
|
+
run_id: string;
|
|
132
|
+
/** Canonical `YYYY/MM/DD/<name>.jsonl` path relative to `sessionsDir`. */
|
|
133
|
+
rel_path: string;
|
|
134
|
+
started_at: string;
|
|
135
|
+
};
|
|
136
|
+
/** Narrow write capability handed to run log writers. */
|
|
137
|
+
type SessionRunRecorder = {
|
|
138
|
+
recordSessionRun: (entry: SessionRunEntry) => Promise<void>;
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* Resolves an indexed run log to a readable path: plain wins, then `.gz`.
|
|
142
|
+
* Only regular files qualify (no symlink/FIFO/directory, as the directory scan
|
|
143
|
+
* admitted only `isFile()` entries). Returns undefined otherwise.
|
|
144
|
+
*/
|
|
145
|
+
declare const resolveIndexedRunLog: (sessionsDir: string, relPath: string) => string | undefined;
|
|
146
|
+
/**
|
|
147
|
+
* Reads every run log header under `sessionsDir` (the legacy, store-wide scan)
|
|
148
|
+
* and returns one entry per log whose header names a session. Unreadable or
|
|
149
|
+
* header-less files are skipped, as the legacy history scan did.
|
|
150
|
+
*/
|
|
151
|
+
declare const scanSessionRunLogs: (sessionsDir: string) => Promise<SessionRunEntry[]>;
|
|
152
|
+
|
|
74
153
|
declare class RunEventStoreFactoryImpl implements RunEventStoreFactory {
|
|
154
|
+
private runIndex?;
|
|
155
|
+
private readonly onRunIndexError?;
|
|
156
|
+
/**
|
|
157
|
+
* `runIndex` records each run in state.db (`session_runs`). When omitted, a
|
|
158
|
+
* default-path SessionStateStoreImpl is created on the first run so every
|
|
159
|
+
* normal run log stays indexed.
|
|
160
|
+
*/
|
|
161
|
+
constructor(options?: {
|
|
162
|
+
runIndex?: SessionRunRecorder;
|
|
163
|
+
onRunIndexError?: (error: unknown) => void;
|
|
164
|
+
});
|
|
75
165
|
create(init: RunEventStoreInit): SessionStore;
|
|
76
166
|
}
|
|
77
167
|
|
|
78
|
-
|
|
79
|
-
|
|
168
|
+
/** Plain wins if both exist after interrupted compression; never replay twice. */
|
|
169
|
+
declare const resolveRunLogPath: (file: string) => string;
|
|
170
|
+
declare function readRunLogLines(file: string): AsyncGenerator<string>;
|
|
171
|
+
|
|
172
|
+
declare function searchOriginalMessages(file: string, options: {
|
|
173
|
+
query: string;
|
|
174
|
+
cursor?: string;
|
|
175
|
+
limit?: number;
|
|
176
|
+
}): Promise<ConversationSearchResult>;
|
|
177
|
+
declare function readOriginalMessage(file: string, options: {
|
|
178
|
+
id: string;
|
|
179
|
+
offset?: number;
|
|
180
|
+
limit?: number;
|
|
181
|
+
}): Promise<ConversationMessage | null>;
|
|
182
|
+
|
|
183
|
+
type GoalRunClaim = {
|
|
184
|
+
run_id: string;
|
|
185
|
+
revision: number;
|
|
186
|
+
activation_id: string;
|
|
187
|
+
continuation_id?: string;
|
|
188
|
+
owner_pid: number;
|
|
189
|
+
owner_identity: string | null;
|
|
190
|
+
pending_seq?: number;
|
|
191
|
+
accounted_seq?: number;
|
|
192
|
+
};
|
|
193
|
+
type StoredSessionGoal = {
|
|
194
|
+
goal: GoalRecord;
|
|
195
|
+
claim?: GoalRunClaim;
|
|
196
|
+
blocker?: {
|
|
197
|
+
key: string;
|
|
198
|
+
runs: number;
|
|
199
|
+
};
|
|
200
|
+
};
|
|
201
|
+
type Sqlite = {
|
|
202
|
+
exec(sql: string): void;
|
|
203
|
+
run(sql: string, params?: unknown[]): void;
|
|
204
|
+
get<T>(sql: string, params?: unknown[]): T | undefined;
|
|
205
|
+
};
|
|
206
|
+
type GoalTransaction = {
|
|
207
|
+
entry: StoredSessionGoal | null;
|
|
208
|
+
consume(continuationId: string, goalId: string, runId: string): void;
|
|
209
|
+
};
|
|
210
|
+
declare class SessionGoalStore {
|
|
211
|
+
private readonly getDb;
|
|
212
|
+
constructor(getDb: () => Promise<Sqlite>);
|
|
213
|
+
private load;
|
|
214
|
+
read(sessionId: string, continuationId?: string): Promise<{
|
|
215
|
+
entry: StoredSessionGoal | null;
|
|
216
|
+
acceptedRunId?: string;
|
|
217
|
+
}>;
|
|
218
|
+
update<T>(sessionId: string, update: (tx: GoalTransaction) => T): Promise<T>;
|
|
80
219
|
}
|
|
81
220
|
|
|
82
221
|
type SessionStateStoreOptions = {
|
|
@@ -86,19 +225,27 @@ type SessionStateStoreOptions = {
|
|
|
86
225
|
detail?: string;
|
|
87
226
|
}) => void;
|
|
88
227
|
};
|
|
89
|
-
declare class SessionStateStoreImpl implements SessionStateStore {
|
|
228
|
+
declare class SessionStateStoreImpl implements SessionStateStore, SessionRunRecorder {
|
|
229
|
+
/** Root that `session_runs.rel_path` values are relative to. */
|
|
230
|
+
readonly sessionsDir: string;
|
|
231
|
+
readonly goals: SessionGoalStore;
|
|
90
232
|
private readonly legacyStateDir;
|
|
91
233
|
private readonly messagesDir;
|
|
234
|
+
private readonly contextsDir;
|
|
92
235
|
private readonly stateDbPath;
|
|
93
236
|
private readonly ensureDirs;
|
|
94
237
|
private db;
|
|
95
238
|
private schemaInit;
|
|
96
239
|
private readonly onError?;
|
|
97
240
|
private lastDbUnavailableError;
|
|
241
|
+
private runIndexBackfill;
|
|
98
242
|
constructor(options?: SessionStateStoreOptions);
|
|
99
243
|
private getOrCreateDb;
|
|
100
244
|
private resolveLegacyPath;
|
|
101
245
|
private resolveMessagePath;
|
|
246
|
+
private contextPath;
|
|
247
|
+
private writeContext;
|
|
248
|
+
private ensureArchive;
|
|
102
249
|
private openDatabase;
|
|
103
250
|
private initDatabaseSchema;
|
|
104
251
|
private tryGetDb;
|
|
@@ -106,21 +253,85 @@ declare class SessionStateStoreImpl implements SessionStateStore {
|
|
|
106
253
|
private loadLegacy;
|
|
107
254
|
private loadFromIndex;
|
|
108
255
|
private saveToIndex;
|
|
256
|
+
appendMessages(sessionId: string, messages: SessionState["messages"]): Promise<void>;
|
|
257
|
+
searchMessages(sessionId: string, options: Parameters<typeof searchOriginalMessages>[1]): Promise<_codelia_core.ConversationSearchResult>;
|
|
258
|
+
readMessage(sessionId: string, options: Parameters<typeof readOriginalMessage>[1]): Promise<_codelia_core.ConversationMessage | null>;
|
|
109
259
|
load(sessionId: string): Promise<SessionState | null>;
|
|
110
260
|
save(state: SessionState): Promise<void>;
|
|
261
|
+
getSummary(sessionId: string): Promise<SessionStateSummary | null>;
|
|
262
|
+
/** Serializes pin changes with the final, synchronous unlink decision. */
|
|
263
|
+
withRetentionGuard<T>(sessionId: string, action: (summary: SessionStateSummary | null) => T): Promise<T>;
|
|
264
|
+
updateMetadata(sessionId: string, patch: {
|
|
265
|
+
pinned?: boolean;
|
|
266
|
+
archived?: boolean;
|
|
267
|
+
}): Promise<SessionStateSummary>;
|
|
268
|
+
/** Records a run log; idempotent, safe across concurrent runtimes. */
|
|
269
|
+
recordSessionRun(entry: SessionRunEntry): Promise<void>;
|
|
270
|
+
/** True when run_id is indexed for the session or was already rescanned. */
|
|
271
|
+
private isRunKnown;
|
|
272
|
+
/**
|
|
273
|
+
* The index answers for a session once a store-wide backfill finished and
|
|
274
|
+
* the session's latest run_id is either indexed or was already rescanned.
|
|
275
|
+
* The run_id check catches logs written by runtimes that predate the index.
|
|
276
|
+
*/
|
|
277
|
+
private isSessionRunIndexComplete;
|
|
278
|
+
/** One store-wide header scan per process at a time; rows are idempotent. */
|
|
279
|
+
private backfillSessionRuns;
|
|
280
|
+
/**
|
|
281
|
+
* Returns the session's run logs, newest first. Falls back to one
|
|
282
|
+
* store-wide header scan (which indexes every session) only while the
|
|
283
|
+
* index cannot answer for this session.
|
|
284
|
+
*/
|
|
285
|
+
listSessionRuns(sessionId: string): Promise<SessionRunEntry[]>;
|
|
111
286
|
private listLegacySummaries;
|
|
112
287
|
list(): Promise<SessionStateSummary[]>;
|
|
113
288
|
}
|
|
114
289
|
|
|
290
|
+
declare const RUN_LOG_RETENTION_MS: number;
|
|
291
|
+
type RunLogCleanupResult = {
|
|
292
|
+
deleted: number;
|
|
293
|
+
compressed: number;
|
|
294
|
+
skipped: number;
|
|
295
|
+
failed: number;
|
|
296
|
+
reclaimed_bytes: number;
|
|
297
|
+
};
|
|
298
|
+
declare function cleanupRunLogs(options: {
|
|
299
|
+
paths: StoragePaths;
|
|
300
|
+
sessions: SessionStateStoreImpl;
|
|
301
|
+
now?: number;
|
|
302
|
+
sessionId?: string;
|
|
303
|
+
}): Promise<RunLogCleanupResult>;
|
|
304
|
+
|
|
305
|
+
declare class StoragePathServiceImpl implements StoragePathService {
|
|
306
|
+
resolvePaths(options?: ResolveStorageOptions): StoragePaths;
|
|
307
|
+
}
|
|
308
|
+
|
|
115
309
|
declare class SessionStoreWriterImpl implements SessionStore {
|
|
116
310
|
readonly filePath: string;
|
|
117
|
-
|
|
311
|
+
readonly usageFilePath: string;
|
|
312
|
+
readonly diagnosticFilePath?: string;
|
|
313
|
+
private readonly paths;
|
|
314
|
+
private readonly runId;
|
|
315
|
+
private readonly startedAt;
|
|
316
|
+
private sessionId?;
|
|
317
|
+
private model?;
|
|
318
|
+
private readonly requestModels;
|
|
319
|
+
private activePath?;
|
|
118
320
|
private writeChain;
|
|
321
|
+
private readonly runIndex?;
|
|
322
|
+
private readonly onRunIndexError?;
|
|
119
323
|
constructor(options: {
|
|
120
324
|
runId: string;
|
|
121
325
|
startedAt: string;
|
|
122
326
|
paths?: StoragePaths;
|
|
327
|
+
dump?: boolean;
|
|
328
|
+
/** Records `session -> run` in state.db when the header is appended. */
|
|
329
|
+
runIndex?: SessionRunRecorder;
|
|
330
|
+
onRunIndexError?: (error: unknown) => void;
|
|
123
331
|
});
|
|
332
|
+
private projectNormalRecord;
|
|
333
|
+
/** Indexed before the log line exists so a written log is never unindexed. */
|
|
334
|
+
private recordRun;
|
|
124
335
|
append(record: SessionRecord): Promise<void>;
|
|
125
336
|
}
|
|
126
337
|
|
|
@@ -228,4 +439,4 @@ declare class ToolOutputCacheStoreImpl implements ToolOutputCacheStore {
|
|
|
228
439
|
private resolvePath;
|
|
229
440
|
}
|
|
230
441
|
|
|
231
|
-
export { IMAGE_MAX_BYTES, ImageArtifactStore, type McpAuthFile, McpAuthStore, type McpOAuthTokens, type ProjectsPolicyFile, ProjectsPolicyStore, RunEventStoreFactoryImpl, SessionStateStoreImpl, SessionStoreWriterImpl, StoragePathServiceImpl, type TaskArtifact, type TaskKind, type TaskRecord, TaskRegistryStore, type TaskResult, type TaskState, type TaskTruncatedOutput, type TaskWorkspaceMode, ToolOutputCacheStoreImpl, ensureStorageDirs, resolveStoragePaths, validateImagePng };
|
|
442
|
+
export { type AttachmentStore, FileAttachmentStore, type GoalRunClaim, IMAGE_MAX_BYTES, ImageArtifactStore, type McpAuthFile, McpAuthStore, type McpOAuthTokens, type ProjectsPolicyFile, ProjectsPolicyStore, RUN_LOG_RETENTION_MS, RunEventStoreFactoryImpl, type RunLogCleanupResult, SessionGoalStore, type SessionRunEntry, type SessionRunRecorder, SessionStateStoreImpl, SessionStoreWriterImpl, StoragePathServiceImpl, type StoredAttachment, type StoredSessionGoal, type TaskArtifact, type TaskKind, type TaskRecord, TaskRegistryStore, type TaskResult, type TaskState, type TaskTruncatedOutput, type TaskWorkspaceMode, ToolOutputCacheStoreImpl, cleanupRunLogs, createAttachmentId, detectImageAttachmentType, ensureStorageDirs, getProcessIdentity, isAttachmentId, readRunLogLines, resolveIndexedRunLog, resolveRunLogPath, resolveStoragePaths, scanSessionRunLogs, validateImageAttachment, validateImagePng };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,23 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { StoragePaths, ResolveStorageOptions, RunEventStoreFactory, RunEventStoreInit, SessionStore,
|
|
1
|
+
import * as _codelia_core from '@codelia/core';
|
|
2
|
+
import { StoragePaths, ResolveStorageOptions, RunEventStoreFactory, RunEventStoreInit, SessionStore, ConversationSearchResult, ConversationMessage, SessionStateStore, SessionState, SessionStateSummary, StoragePathService, SessionRecord, ToolOutputCacheStore, ToolOutputCacheRecord, ToolOutputRef, ToolOutputCacheReadOptions, ToolOutputCacheSearchOptions } from '@codelia/core';
|
|
3
3
|
export { ResolveStorageOptions, StorageLayout, StoragePathService, StoragePaths } from '@codelia/core';
|
|
4
|
+
import { GeneratedImageMetadata, ImageArtifactRef, ImageMediaType, AttachedImageMetadata, ImageArtifactMetadata, AttachmentRef, ApprovalMode, GoalRecord, SubagentLineage, AgentMessage, TaskTerminationReason, TaskUsage } from '@codelia/shared-types';
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
type AttachmentWriteOptions = {
|
|
7
|
+
artifactId?: string;
|
|
8
|
+
ownerSessionId?: string;
|
|
9
|
+
};
|
|
10
|
+
/** Workspace image storage with session-scoped access for copied files. */
|
|
6
11
|
declare class ImageArtifactStore {
|
|
7
12
|
readonly root: string;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
13
|
+
private readonly getSessionId?;
|
|
14
|
+
constructor(root: string, getSessionId?: (() => string | null) | undefined);
|
|
15
|
+
save(bytes: Buffer, provenance: Omit<GeneratedImageMetadata, keyof ImageArtifactRef | "created_at">, mediaType?: "image/png" | "image/jpeg"): Promise<GeneratedImageMetadata>;
|
|
16
|
+
saveAttachment(bytes: Buffer, mediaType: ImageMediaType, { artifactId, ownerSessionId, }?: AttachmentWriteOptions): Promise<AttachedImageMetadata>;
|
|
17
|
+
/** Roll back a newly uploaded attachment before its ownership batch is published. */
|
|
18
|
+
discardUnpublishedAttachment(ref: ImageArtifactRef, sessionId: string): Promise<void>;
|
|
11
19
|
private persist;
|
|
12
|
-
read(artifactId: string): Promise<{
|
|
20
|
+
read(artifactId: string, sessionId?: string | null): Promise<{
|
|
13
21
|
metadata: ImageArtifactMetadata;
|
|
14
22
|
bytes: Buffer;
|
|
15
23
|
}>;
|
|
@@ -24,6 +32,47 @@ declare class ImageArtifactStore {
|
|
|
24
32
|
}>;
|
|
25
33
|
}
|
|
26
34
|
|
|
35
|
+
type StoredAttachment = {
|
|
36
|
+
ref: AttachmentRef;
|
|
37
|
+
imageRef?: ImageArtifactRef;
|
|
38
|
+
};
|
|
39
|
+
/** Host-owned originals and ownership index. commit must publish the entire batch atomically. */
|
|
40
|
+
type AttachmentStore = {
|
|
41
|
+
commit(sessionId: string, entries: readonly {
|
|
42
|
+
record: StoredAttachment;
|
|
43
|
+
bytes: Buffer;
|
|
44
|
+
}[]): Promise<void>;
|
|
45
|
+
/** Validate ownership and integrity before returning an admitted reference. */
|
|
46
|
+
lookup(attachmentId: string, sessionId: string): Promise<StoredAttachment | null>;
|
|
47
|
+
/** Check ownership and integrity of the returned byte range. */
|
|
48
|
+
read(attachmentId: string, sessionId: string, offset: number, length: number): Promise<Uint8Array>;
|
|
49
|
+
resolveImage?: ImageArtifactStore["resolve"];
|
|
50
|
+
};
|
|
51
|
+
declare const createAttachmentId: () => string;
|
|
52
|
+
declare const isAttachmentId: (id: string) => boolean;
|
|
53
|
+
/** An ownership marker makes every member of a batch visible at once. */
|
|
54
|
+
declare class FileAttachmentStore implements AttachmentStore {
|
|
55
|
+
readonly root: string;
|
|
56
|
+
private readonly images;
|
|
57
|
+
constructor(root: string, images: ImageArtifactStore);
|
|
58
|
+
resolveImage: NonNullable<AttachmentStore["resolveImage"]>;
|
|
59
|
+
commit(sessionId: string, entries: readonly {
|
|
60
|
+
record: StoredAttachment;
|
|
61
|
+
bytes: Buffer;
|
|
62
|
+
}[]): Promise<void>;
|
|
63
|
+
lookup(attachmentId: string, sessionId: string): Promise<StoredAttachment | null>;
|
|
64
|
+
read(attachmentId: string, sessionId: string, offset: number, length: number): Promise<Uint8Array>;
|
|
65
|
+
private metadata;
|
|
66
|
+
private readImage;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
declare function detectImageAttachmentType(bytes: Buffer): ImageMediaType | undefined;
|
|
70
|
+
/** Bounded header validation; attachment originals are not decoded or re-encoded. */
|
|
71
|
+
declare function validateImageAttachment(bytes: Buffer, mediaType: ImageMediaType): {
|
|
72
|
+
width: number;
|
|
73
|
+
height: number;
|
|
74
|
+
};
|
|
75
|
+
|
|
27
76
|
declare const IMAGE_MAX_BYTES: number;
|
|
28
77
|
/** Validate chunks, CRC and bounded decompression before accepting provider bytes. */
|
|
29
78
|
declare function validateImagePng(bytes: Buffer): Promise<{
|
|
@@ -54,6 +103,11 @@ declare class McpAuthStore {
|
|
|
54
103
|
declare function resolveStoragePaths(options?: ResolveStorageOptions): StoragePaths;
|
|
55
104
|
declare function ensureStorageDirs(paths: StoragePaths): Promise<void>;
|
|
56
105
|
|
|
106
|
+
/** Identity guards PID reuse. Failure is unknown, never permission to kill or remove a live-writer marker. */
|
|
107
|
+
declare const getProcessIdentity: (pid: number, options?: {
|
|
108
|
+
canonicalEnvironment?: boolean;
|
|
109
|
+
}) => Promise<string | null>;
|
|
110
|
+
|
|
57
111
|
type ProjectsPolicyFile = {
|
|
58
112
|
version: 1;
|
|
59
113
|
default?: {
|
|
@@ -71,12 +125,97 @@ declare class ProjectsPolicyStore {
|
|
|
71
125
|
save(data: ProjectsPolicyFile): Promise<void>;
|
|
72
126
|
}
|
|
73
127
|
|
|
128
|
+
/** One run log of a session, as recorded in `state.db` (`session_runs`). */
|
|
129
|
+
type SessionRunEntry = {
|
|
130
|
+
session_id: string;
|
|
131
|
+
run_id: string;
|
|
132
|
+
/** Canonical `YYYY/MM/DD/<name>.jsonl` path relative to `sessionsDir`. */
|
|
133
|
+
rel_path: string;
|
|
134
|
+
started_at: string;
|
|
135
|
+
};
|
|
136
|
+
/** Narrow write capability handed to run log writers. */
|
|
137
|
+
type SessionRunRecorder = {
|
|
138
|
+
recordSessionRun: (entry: SessionRunEntry) => Promise<void>;
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* Resolves an indexed run log to a readable path: plain wins, then `.gz`.
|
|
142
|
+
* Only regular files qualify (no symlink/FIFO/directory, as the directory scan
|
|
143
|
+
* admitted only `isFile()` entries). Returns undefined otherwise.
|
|
144
|
+
*/
|
|
145
|
+
declare const resolveIndexedRunLog: (sessionsDir: string, relPath: string) => string | undefined;
|
|
146
|
+
/**
|
|
147
|
+
* Reads every run log header under `sessionsDir` (the legacy, store-wide scan)
|
|
148
|
+
* and returns one entry per log whose header names a session. Unreadable or
|
|
149
|
+
* header-less files are skipped, as the legacy history scan did.
|
|
150
|
+
*/
|
|
151
|
+
declare const scanSessionRunLogs: (sessionsDir: string) => Promise<SessionRunEntry[]>;
|
|
152
|
+
|
|
74
153
|
declare class RunEventStoreFactoryImpl implements RunEventStoreFactory {
|
|
154
|
+
private runIndex?;
|
|
155
|
+
private readonly onRunIndexError?;
|
|
156
|
+
/**
|
|
157
|
+
* `runIndex` records each run in state.db (`session_runs`). When omitted, a
|
|
158
|
+
* default-path SessionStateStoreImpl is created on the first run so every
|
|
159
|
+
* normal run log stays indexed.
|
|
160
|
+
*/
|
|
161
|
+
constructor(options?: {
|
|
162
|
+
runIndex?: SessionRunRecorder;
|
|
163
|
+
onRunIndexError?: (error: unknown) => void;
|
|
164
|
+
});
|
|
75
165
|
create(init: RunEventStoreInit): SessionStore;
|
|
76
166
|
}
|
|
77
167
|
|
|
78
|
-
|
|
79
|
-
|
|
168
|
+
/** Plain wins if both exist after interrupted compression; never replay twice. */
|
|
169
|
+
declare const resolveRunLogPath: (file: string) => string;
|
|
170
|
+
declare function readRunLogLines(file: string): AsyncGenerator<string>;
|
|
171
|
+
|
|
172
|
+
declare function searchOriginalMessages(file: string, options: {
|
|
173
|
+
query: string;
|
|
174
|
+
cursor?: string;
|
|
175
|
+
limit?: number;
|
|
176
|
+
}): Promise<ConversationSearchResult>;
|
|
177
|
+
declare function readOriginalMessage(file: string, options: {
|
|
178
|
+
id: string;
|
|
179
|
+
offset?: number;
|
|
180
|
+
limit?: number;
|
|
181
|
+
}): Promise<ConversationMessage | null>;
|
|
182
|
+
|
|
183
|
+
type GoalRunClaim = {
|
|
184
|
+
run_id: string;
|
|
185
|
+
revision: number;
|
|
186
|
+
activation_id: string;
|
|
187
|
+
continuation_id?: string;
|
|
188
|
+
owner_pid: number;
|
|
189
|
+
owner_identity: string | null;
|
|
190
|
+
pending_seq?: number;
|
|
191
|
+
accounted_seq?: number;
|
|
192
|
+
};
|
|
193
|
+
type StoredSessionGoal = {
|
|
194
|
+
goal: GoalRecord;
|
|
195
|
+
claim?: GoalRunClaim;
|
|
196
|
+
blocker?: {
|
|
197
|
+
key: string;
|
|
198
|
+
runs: number;
|
|
199
|
+
};
|
|
200
|
+
};
|
|
201
|
+
type Sqlite = {
|
|
202
|
+
exec(sql: string): void;
|
|
203
|
+
run(sql: string, params?: unknown[]): void;
|
|
204
|
+
get<T>(sql: string, params?: unknown[]): T | undefined;
|
|
205
|
+
};
|
|
206
|
+
type GoalTransaction = {
|
|
207
|
+
entry: StoredSessionGoal | null;
|
|
208
|
+
consume(continuationId: string, goalId: string, runId: string): void;
|
|
209
|
+
};
|
|
210
|
+
declare class SessionGoalStore {
|
|
211
|
+
private readonly getDb;
|
|
212
|
+
constructor(getDb: () => Promise<Sqlite>);
|
|
213
|
+
private load;
|
|
214
|
+
read(sessionId: string, continuationId?: string): Promise<{
|
|
215
|
+
entry: StoredSessionGoal | null;
|
|
216
|
+
acceptedRunId?: string;
|
|
217
|
+
}>;
|
|
218
|
+
update<T>(sessionId: string, update: (tx: GoalTransaction) => T): Promise<T>;
|
|
80
219
|
}
|
|
81
220
|
|
|
82
221
|
type SessionStateStoreOptions = {
|
|
@@ -86,19 +225,27 @@ type SessionStateStoreOptions = {
|
|
|
86
225
|
detail?: string;
|
|
87
226
|
}) => void;
|
|
88
227
|
};
|
|
89
|
-
declare class SessionStateStoreImpl implements SessionStateStore {
|
|
228
|
+
declare class SessionStateStoreImpl implements SessionStateStore, SessionRunRecorder {
|
|
229
|
+
/** Root that `session_runs.rel_path` values are relative to. */
|
|
230
|
+
readonly sessionsDir: string;
|
|
231
|
+
readonly goals: SessionGoalStore;
|
|
90
232
|
private readonly legacyStateDir;
|
|
91
233
|
private readonly messagesDir;
|
|
234
|
+
private readonly contextsDir;
|
|
92
235
|
private readonly stateDbPath;
|
|
93
236
|
private readonly ensureDirs;
|
|
94
237
|
private db;
|
|
95
238
|
private schemaInit;
|
|
96
239
|
private readonly onError?;
|
|
97
240
|
private lastDbUnavailableError;
|
|
241
|
+
private runIndexBackfill;
|
|
98
242
|
constructor(options?: SessionStateStoreOptions);
|
|
99
243
|
private getOrCreateDb;
|
|
100
244
|
private resolveLegacyPath;
|
|
101
245
|
private resolveMessagePath;
|
|
246
|
+
private contextPath;
|
|
247
|
+
private writeContext;
|
|
248
|
+
private ensureArchive;
|
|
102
249
|
private openDatabase;
|
|
103
250
|
private initDatabaseSchema;
|
|
104
251
|
private tryGetDb;
|
|
@@ -106,21 +253,85 @@ declare class SessionStateStoreImpl implements SessionStateStore {
|
|
|
106
253
|
private loadLegacy;
|
|
107
254
|
private loadFromIndex;
|
|
108
255
|
private saveToIndex;
|
|
256
|
+
appendMessages(sessionId: string, messages: SessionState["messages"]): Promise<void>;
|
|
257
|
+
searchMessages(sessionId: string, options: Parameters<typeof searchOriginalMessages>[1]): Promise<_codelia_core.ConversationSearchResult>;
|
|
258
|
+
readMessage(sessionId: string, options: Parameters<typeof readOriginalMessage>[1]): Promise<_codelia_core.ConversationMessage | null>;
|
|
109
259
|
load(sessionId: string): Promise<SessionState | null>;
|
|
110
260
|
save(state: SessionState): Promise<void>;
|
|
261
|
+
getSummary(sessionId: string): Promise<SessionStateSummary | null>;
|
|
262
|
+
/** Serializes pin changes with the final, synchronous unlink decision. */
|
|
263
|
+
withRetentionGuard<T>(sessionId: string, action: (summary: SessionStateSummary | null) => T): Promise<T>;
|
|
264
|
+
updateMetadata(sessionId: string, patch: {
|
|
265
|
+
pinned?: boolean;
|
|
266
|
+
archived?: boolean;
|
|
267
|
+
}): Promise<SessionStateSummary>;
|
|
268
|
+
/** Records a run log; idempotent, safe across concurrent runtimes. */
|
|
269
|
+
recordSessionRun(entry: SessionRunEntry): Promise<void>;
|
|
270
|
+
/** True when run_id is indexed for the session or was already rescanned. */
|
|
271
|
+
private isRunKnown;
|
|
272
|
+
/**
|
|
273
|
+
* The index answers for a session once a store-wide backfill finished and
|
|
274
|
+
* the session's latest run_id is either indexed or was already rescanned.
|
|
275
|
+
* The run_id check catches logs written by runtimes that predate the index.
|
|
276
|
+
*/
|
|
277
|
+
private isSessionRunIndexComplete;
|
|
278
|
+
/** One store-wide header scan per process at a time; rows are idempotent. */
|
|
279
|
+
private backfillSessionRuns;
|
|
280
|
+
/**
|
|
281
|
+
* Returns the session's run logs, newest first. Falls back to one
|
|
282
|
+
* store-wide header scan (which indexes every session) only while the
|
|
283
|
+
* index cannot answer for this session.
|
|
284
|
+
*/
|
|
285
|
+
listSessionRuns(sessionId: string): Promise<SessionRunEntry[]>;
|
|
111
286
|
private listLegacySummaries;
|
|
112
287
|
list(): Promise<SessionStateSummary[]>;
|
|
113
288
|
}
|
|
114
289
|
|
|
290
|
+
declare const RUN_LOG_RETENTION_MS: number;
|
|
291
|
+
type RunLogCleanupResult = {
|
|
292
|
+
deleted: number;
|
|
293
|
+
compressed: number;
|
|
294
|
+
skipped: number;
|
|
295
|
+
failed: number;
|
|
296
|
+
reclaimed_bytes: number;
|
|
297
|
+
};
|
|
298
|
+
declare function cleanupRunLogs(options: {
|
|
299
|
+
paths: StoragePaths;
|
|
300
|
+
sessions: SessionStateStoreImpl;
|
|
301
|
+
now?: number;
|
|
302
|
+
sessionId?: string;
|
|
303
|
+
}): Promise<RunLogCleanupResult>;
|
|
304
|
+
|
|
305
|
+
declare class StoragePathServiceImpl implements StoragePathService {
|
|
306
|
+
resolvePaths(options?: ResolveStorageOptions): StoragePaths;
|
|
307
|
+
}
|
|
308
|
+
|
|
115
309
|
declare class SessionStoreWriterImpl implements SessionStore {
|
|
116
310
|
readonly filePath: string;
|
|
117
|
-
|
|
311
|
+
readonly usageFilePath: string;
|
|
312
|
+
readonly diagnosticFilePath?: string;
|
|
313
|
+
private readonly paths;
|
|
314
|
+
private readonly runId;
|
|
315
|
+
private readonly startedAt;
|
|
316
|
+
private sessionId?;
|
|
317
|
+
private model?;
|
|
318
|
+
private readonly requestModels;
|
|
319
|
+
private activePath?;
|
|
118
320
|
private writeChain;
|
|
321
|
+
private readonly runIndex?;
|
|
322
|
+
private readonly onRunIndexError?;
|
|
119
323
|
constructor(options: {
|
|
120
324
|
runId: string;
|
|
121
325
|
startedAt: string;
|
|
122
326
|
paths?: StoragePaths;
|
|
327
|
+
dump?: boolean;
|
|
328
|
+
/** Records `session -> run` in state.db when the header is appended. */
|
|
329
|
+
runIndex?: SessionRunRecorder;
|
|
330
|
+
onRunIndexError?: (error: unknown) => void;
|
|
123
331
|
});
|
|
332
|
+
private projectNormalRecord;
|
|
333
|
+
/** Indexed before the log line exists so a written log is never unindexed. */
|
|
334
|
+
private recordRun;
|
|
124
335
|
append(record: SessionRecord): Promise<void>;
|
|
125
336
|
}
|
|
126
337
|
|
|
@@ -228,4 +439,4 @@ declare class ToolOutputCacheStoreImpl implements ToolOutputCacheStore {
|
|
|
228
439
|
private resolvePath;
|
|
229
440
|
}
|
|
230
441
|
|
|
231
|
-
export { IMAGE_MAX_BYTES, ImageArtifactStore, type McpAuthFile, McpAuthStore, type McpOAuthTokens, type ProjectsPolicyFile, ProjectsPolicyStore, RunEventStoreFactoryImpl, SessionStateStoreImpl, SessionStoreWriterImpl, StoragePathServiceImpl, type TaskArtifact, type TaskKind, type TaskRecord, TaskRegistryStore, type TaskResult, type TaskState, type TaskTruncatedOutput, type TaskWorkspaceMode, ToolOutputCacheStoreImpl, ensureStorageDirs, resolveStoragePaths, validateImagePng };
|
|
442
|
+
export { type AttachmentStore, FileAttachmentStore, type GoalRunClaim, IMAGE_MAX_BYTES, ImageArtifactStore, type McpAuthFile, McpAuthStore, type McpOAuthTokens, type ProjectsPolicyFile, ProjectsPolicyStore, RUN_LOG_RETENTION_MS, RunEventStoreFactoryImpl, type RunLogCleanupResult, SessionGoalStore, type SessionRunEntry, type SessionRunRecorder, SessionStateStoreImpl, SessionStoreWriterImpl, StoragePathServiceImpl, type StoredAttachment, type StoredSessionGoal, type TaskArtifact, type TaskKind, type TaskRecord, TaskRegistryStore, type TaskResult, type TaskState, type TaskTruncatedOutput, type TaskWorkspaceMode, ToolOutputCacheStoreImpl, cleanupRunLogs, createAttachmentId, detectImageAttachmentType, ensureStorageDirs, getProcessIdentity, isAttachmentId, readRunLogLines, resolveIndexedRunLog, resolveRunLogPath, resolveStoragePaths, scanSessionRunLogs, validateImageAttachment, validateImagePng };
|