@tangle-network/sdk-session-persistence 0.7.0 → 0.7.1

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.d.mts CHANGED
@@ -1,3 +1,4 @@
1
+ import { FileHandle } from "node:fs/promises";
1
2
  import { AgentExecutionInput, ToolState } from "@tangle-network/agent-interface";
2
3
 
3
4
  //#region src/durable-write.d.ts
@@ -6,13 +7,30 @@ declare const DURABLE_FILE_MODE = 384;
6
7
  /** The `<file>.tmp-<hex>` sibling a durable write fills before its rename. */
7
8
  declare const temporarySibling: (target: string) => string;
8
9
  declare function syncDirectory(directory: string): Promise<void>;
10
+ interface DurableWriteOptions {
11
+ /** The file's exact mode; owner-only unless the caller shares it. */
12
+ mode?: number;
13
+ /** The uid and gid the file carries before it becomes visible. */
14
+ owner?: {
15
+ uid: number;
16
+ gid: number;
17
+ };
18
+ /**
19
+ * The target's directory, held open. `target` is then a name in it, and the
20
+ * write, rename and sync go through the handle, so an ancestor swapped under
21
+ * the caller cannot redirect them.
22
+ */
23
+ directory?: FileHandle;
24
+ }
9
25
  /**
10
- * Durably replaces `target` with `contents`; its directory must exist. `mode`
11
- * (before the umask) is the new file's: owner-only unless the caller's files
12
- * are shared with another uid. A crash can leave a `.tmp-` sibling that
13
- * readers skip.
26
+ * Durably replaces `target` with `contents`; its directory must exist. A crash
27
+ * can leave a `.tmp-` sibling that readers skip.
14
28
  */
15
- declare function writeFileDurably(target: string, contents: string, mode?: number): Promise<void>;
29
+ declare function writeFileDurably(target: string, contents: string, {
30
+ mode,
31
+ owner,
32
+ directory
33
+ }?: DurableWriteOptions): Promise<void>;
16
34
  //#endregion
17
35
  //#region src/types.d.ts
18
36
  /**
@@ -285,5 +303,5 @@ declare function createMessageStore(config: PersistenceConfig): MessageStore;
285
303
  */
286
304
  declare function createSessionStore(config: PersistenceConfig): SessionStore;
287
305
  //#endregion
288
- export { DURABLE_FILE_MODE, MAX_SECRET_PROFILE_REPLAY_CHARS, type MessagePart, type MessageStore, type PersistedFilePart, type PersistedMessage, type PersistedReasoningPart, type PersistedSession, type PersistedTextPart, type ProviderMessageIds, RESTORED_SESSION_CONTEXT_PREAMBLE, RESTORED_SESSION_CONTEXT_TAG, type SessionStore, buildCompleteForkContextBlock, buildForkContextBlock, createMessageStore, createSessionStore, forkSessionWithMessages, providerMessageIdsForExecution, readProviderMessageIds, syncDirectory, temporarySibling, writeFileDurably };
306
+ export { DURABLE_FILE_MODE, type DurableWriteOptions, MAX_SECRET_PROFILE_REPLAY_CHARS, type MessagePart, type MessageStore, type PersistedFilePart, type PersistedMessage, type PersistedReasoningPart, type PersistedSession, type PersistedTextPart, type ProviderMessageIds, RESTORED_SESSION_CONTEXT_PREAMBLE, RESTORED_SESSION_CONTEXT_TAG, type SessionStore, buildCompleteForkContextBlock, buildForkContextBlock, createMessageStore, createSessionStore, forkSessionWithMessages, providerMessageIdsForExecution, readProviderMessageIds, syncDirectory, temporarySibling, writeFileDurably };
289
307
  //# sourceMappingURL=index.d.mts.map
package/dist/index.mjs CHANGED
@@ -17,27 +17,29 @@ async function syncDirectory(directory) {
17
17
  }
18
18
  }
19
19
  /**
20
- * Durably replaces `target` with `contents`; its directory must exist. `mode`
21
- * (before the umask) is the new file's: owner-only unless the caller's files
22
- * are shared with another uid. A crash can leave a `.tmp-` sibling that
23
- * readers skip.
20
+ * Durably replaces `target` with `contents`; its directory must exist. A crash
21
+ * can leave a `.tmp-` sibling that readers skip.
24
22
  */
25
- async function writeFileDurably(target, contents, mode = 384) {
26
- const temporary = temporarySibling(target);
23
+ async function writeFileDurably(target, contents, { mode = 384, owner, directory } = {}) {
24
+ if (directory && !/^(?!\.\.?$)[^/]+$/.test(target)) throw new Error(`Not an entry name in the open directory: ${target}`);
25
+ const path = directory ? `/proc/self/fd/${directory.fd}/${target}` : target;
26
+ const temporary = temporarySibling(path);
27
27
  try {
28
28
  const handle = await fs.open(temporary, "wx", mode);
29
29
  try {
30
+ if (owner) await handle.chown(owner.uid, owner.gid);
31
+ await handle.chmod(mode);
30
32
  await handle.writeFile(contents, "utf-8");
31
33
  await handle.sync();
32
34
  } finally {
33
35
  await handle.close();
34
36
  }
35
- await fs.rename(temporary, target);
37
+ await fs.rename(temporary, path);
36
38
  } catch (error) {
37
39
  await fs.rm(temporary, { force: true }).catch(() => void 0);
38
40
  throw error;
39
41
  }
40
- await syncDirectory(dirname(target));
42
+ await (directory ? directory.sync() : syncDirectory(dirname(path)));
41
43
  }
42
44
  //#endregion
43
45
  //#region src/fork.ts
@@ -189,10 +191,7 @@ function storageId(value) {
189
191
  function isMissing(error) {
190
192
  return error.code === "ENOENT";
191
193
  }
192
- /**
193
- * Reads and parses a JSON file.
194
- * Returns null if the file doesn't exist.
195
- */
194
+ /** Parses a JSON file; a missing file reads as null. */
196
195
  async function readJsonFile(filePath) {
197
196
  try {
198
197
  return JSON.parse(await fs$1.readFile(filePath, "utf-8"));
@@ -203,17 +202,13 @@ async function readJsonFile(filePath) {
203
202
  }
204
203
  /**
205
204
  * Durably replaces a pretty-printed JSON file, creating parent directories.
206
- * Files keep the process umask: the store may live in a workspace another uid
207
- * reads.
205
+ * The store may live in a workspace another uid reads.
208
206
  */
209
207
  async function writeJsonFile(filePath, data) {
210
208
  await fs$1.mkdir(path.dirname(filePath), { recursive: true });
211
- await writeFileDurably(filePath, JSON.stringify(data, null, 2), 438);
209
+ await writeFileDurably(filePath, JSON.stringify(data, null, 2), { mode: 420 });
212
210
  }
213
- /**
214
- * Deletes a file if it exists.
215
- * Returns true if the file was deleted, false if it didn't exist.
216
- */
211
+ /** Deletes a file; true when it existed. */
217
212
  async function deleteFile(filePath) {
218
213
  try {
219
214
  await fs$1.unlink(filePath);
@@ -223,13 +218,6 @@ async function deleteFile(filePath) {
223
218
  throw error;
224
219
  }
225
220
  }
226
- /** Deletes a directory and all its contents; a missing directory is a no-op. */
227
- async function deleteDir(dirPath) {
228
- await fs$1.rm(dirPath, {
229
- recursive: true,
230
- force: true
231
- });
232
- }
233
221
  /** Whether a path exists, without following a final symlink. */
234
222
  async function pathExists(targetPath) {
235
223
  try {
@@ -240,10 +228,7 @@ async function pathExists(targetPath) {
240
228
  throw error;
241
229
  }
242
230
  }
243
- /**
244
- * Lists files in a directory.
245
- * Returns empty array if the directory doesn't exist.
246
- */
231
+ /** Lists a directory's files; a missing directory lists as empty. */
247
232
  async function listFiles(dirPath) {
248
233
  try {
249
234
  return (await fs$1.readdir(dirPath, { withFileTypes: true })).filter((e) => e.isFile()).map((e) => e.name);
@@ -318,7 +303,10 @@ function createMessageStore(config) {
318
303
  return messages;
319
304
  },
320
305
  async clear(sessionId) {
321
- await deleteDir(messagesDir(sessionId));
306
+ await fs$1.rm(messagesDir(sessionId), {
307
+ recursive: true,
308
+ force: true
309
+ });
322
310
  },
323
311
  async initialize(sessionId) {
324
312
  if (await readJsonFile(indexPath(sessionId)) !== null) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tangle-network/sdk-session-persistence",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "File-based session and message storage for CLI providers without built-in persistence",
5
5
  "type": "module",
6
6
  "main": "./dist/index.mjs",
@@ -16,7 +16,7 @@
16
16
  "!dist/**/*.map"
17
17
  ],
18
18
  "dependencies": {
19
- "@tangle-network/agent-interface": "^5.9.0"
19
+ "@tangle-network/agent-interface": "^5.10.13"
20
20
  },
21
21
  "devDependencies": {
22
22
  "@types/node": "25.6.0",
@@ -29,8 +29,8 @@
29
29
  "registry": "https://registry.npmjs.org"
30
30
  },
31
31
  "tangleRelease": {
32
- "sourceSha": "e962211a1ce5f2e11a99b60421e45fca5547ca2d",
33
- "mergedAt": "2026-10-27T03:53:58-06:00"
32
+ "sourceSha": "4f0c9345ec23e3dd680cc869ca18da47afd0af48",
33
+ "mergedAt": "2026-11-02T07:39:16-08:00"
34
34
  },
35
35
  "scripts": {
36
36
  "build": "tsdown",