dsh-memsearch-community 0.4.0 → 0.4.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.
Files changed (3) hide show
  1. package/README.md +10 -0
  2. package/lib/tree.js +96 -58
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -15,6 +15,16 @@ computed locally with ONNX.
15
15
 
16
16
  ## Changes from upstream
17
17
 
18
+ **0.4.1 — sessions never share a folder**
19
+
20
+ Two sessions with the same title used to share one folder, so renaming one moved
21
+ the other session's transcript into its own directory. A folder is now claimed per
22
+ session (`周报`, `周报(2)`), and the ownership map also understands state files
23
+ written by 0.4.0 that only recorded a file path.
24
+
25
+ Also fixes the volume helper reading the wrong directory when a session folder was
26
+ disambiguated.
27
+
18
28
  **0.4.0 — one folder per session, numbered volumes**
19
29
 
20
30
  The archive is now `root/<YYYY.MM.DD>/<session title>/<session title>.txt`. When a
package/lib/tree.js CHANGED
@@ -3,13 +3,16 @@
3
3
  *
4
4
  * Layout: `<root>/<YYYY.MM.DD>/<session title>/<session title>.txt`
5
5
  *
6
- * One folder per day, one folder per session title inside it, and one or more
7
- * text files inside that. When a file reaches `maxBytes` the next turn opens a
6
+ * One folder per day, one folder per session inside it, and one or more text
7
+ * files inside that. When a file reaches `maxBytes` the next turn opens a
8
8
  * numbered continuation, `<title>(2).txt`, then `(3)`, and so on. Readers walk
9
9
  * the day folder, then the title folder, then every `.txt` in it.
10
+ *
11
+ * Two sessions may share a title on the same day; the second one gets
12
+ * `<title>(2)` as its folder so renaming one never moves the other's transcript.
10
13
  */
11
14
  import { appendFileSync, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
12
- import { join } from "node:path";
15
+ import { basename, join } from "node:path";
13
16
 
14
17
  const pad = (n) => String(n).padStart(2, "0");
15
18
 
@@ -123,13 +126,6 @@ export class Archive {
123
126
  return dir;
124
127
  }
125
128
 
126
- /** `<root>/<day>/<base>` — the folder holding one session's volumes. */
127
- sessionDir(day, base) {
128
- const dir = join(this.dayDir(day), base);
129
- mkdirSync(dir, { recursive: true });
130
- return dir;
131
- }
132
-
133
129
  /** Day folders currently on disk, oldest first. */
134
130
  listDays() {
135
131
  try {
@@ -162,23 +158,50 @@ export class Archive {
162
158
  }
163
159
  }
164
160
 
161
+ /**
162
+ * Pick the folder a session owns inside a day.
163
+ *
164
+ * Folders are named after the session title, but two sessions can share a
165
+ * title: the second one gets `标题(2)`. `selfFolder` is the caller's current
166
+ * folder and counts as free because the caller is moving away from it.
167
+ */
168
+ #claimFolder(day, base, sessionId, selfFolder) {
169
+ const owner = new Map();
170
+ for (const [id, record] of Object.entries(this.state.sessions)) {
171
+ if (!record) continue;
172
+ // Older state records only carry `path`; their folder is its parent.
173
+ const folder = record.folder || (record.path ? record.path.slice(0, Math.max(record.path.lastIndexOf("\\"), record.path.lastIndexOf("/"))) : null);
174
+ if (folder) owner.set(folder, id);
175
+ }
176
+ const dayDir = this.dayDir(day);
177
+ for (let n = 1; n < 1000; n++) {
178
+ const candidate = join(dayDir, n === 1 ? base : `${base}(${n})`);
179
+ if (candidate === selfFolder) return candidate;
180
+ if (!existsSync(candidate)) return candidate;
181
+ const holder = owner.get(candidate);
182
+ if (holder === undefined || holder === sessionId) return candidate;
183
+ }
184
+ return join(dayDir, `${base}(${Date.now()})`);
185
+ }
186
+
165
187
  /**
166
188
  * Pick the volume a new block belongs in: keep filling the session's current
167
189
  * file while it stays under the limit, otherwise open the next volume.
168
190
  */
169
- #targetVolume(day, base, record, blockBytes) {
170
- const dir = this.sessionDir(day, base);
171
- const existing = this.volumes(day, base);
172
-
173
- if (record && record.day === day && record.path && existsSync(record.path)) {
174
- if (this.#sizeOf(record.path) + blockBytes <= this.maxBytes) return record.path;
191
+ #targetVolume(folder, base, currentPath, blockBytes) {
192
+ const files = readdirSync(folder)
193
+ .filter((f) => volumeIndexOf(f, base) > 0)
194
+ .sort((a, b) => volumeIndexOf(a, base) - volumeIndexOf(b, base))
195
+ .map((f) => join(folder, f));
196
+
197
+ if (currentPath && existsSync(currentPath) && this.#sizeOf(currentPath) + blockBytes <= this.maxBytes) {
198
+ return currentPath;
175
199
  }
176
-
177
- if (existing.length === 0) return join(dir, volumeName(base, 1));
178
- const last = existing[existing.length - 1];
200
+ if (files.length === 0) return join(folder, volumeName(base, 1));
201
+ const last = files[files.length - 1];
179
202
  if (this.#sizeOf(last) + blockBytes <= this.maxBytes) return last;
180
- const nextIndex = volumeIndexOf(last.split(/[\\/]/).pop(), base) + 1;
181
- return join(dir, volumeName(base, nextIndex));
203
+ const nextIndex = volumeIndexOf(basename(last), base) + 1;
204
+ return join(folder, volumeName(base, nextIndex));
182
205
  }
183
206
 
184
207
  /**
@@ -198,14 +221,26 @@ export class Archive {
198
221
  ].join("\n");
199
222
 
200
223
  const record = this.state.sessions[sessionId];
201
- // A title change moves the whole session into its new folder first.
202
- if (record && record.title && record.title !== base && record.path && existsSync(record.path)) {
203
- this.renameForTitle(sessionId, base);
224
+ let folder;
225
+ let currentPath;
226
+
227
+ if (record && record.day === day && record.folder && existsSync(record.folder)) {
228
+ folder = record.folder;
229
+ currentPath = record.path;
230
+ if (record.title !== base) {
231
+ folder = this.#renameFolder(sessionId, record, day, base);
232
+ currentPath = this.state.sessions[sessionId].path;
233
+ }
234
+ } else {
235
+ folder = this.#claimFolder(day, base, sessionId);
236
+ mkdirSync(folder, { recursive: true });
237
+ currentPath = null;
204
238
  }
205
239
 
206
- const target = this.#targetVolume(day, base, this.state.sessions[sessionId], Buffer.byteLength(block, "utf8"));
240
+ const fileBase = basename(folder);
241
+ const target = this.#targetVolume(folder, fileBase, currentPath, Buffer.byteLength(block, "utf8"));
207
242
  appendFileSync(target, block, "utf8");
208
- this.state.sessions[sessionId] = { path: target, day, title: base };
243
+ this.state.sessions[sessionId] = { path: target, day, title: fileBase, folder };
209
244
  this.save();
210
245
  return target;
211
246
  }
@@ -213,49 +248,52 @@ export class Archive {
213
248
  /** Move a session's folder and volumes after a title change. */
214
249
  renameForTitle(sessionId, title) {
215
250
  const record = this.state.sessions[sessionId];
216
- if (!record || !record.path || !existsSync(record.path)) return null;
217
-
218
- const currentBase = record.title || record.path.split(/[\\/]/).slice(-2)[0];
219
- const nextBase = safeFileBase(title, currentBase);
220
- if (nextBase === currentBase) return record.path;
251
+ if (!record || !record.folder || !existsSync(record.folder)) return null;
252
+ const nextBase = safeFileBase(title, record.title || basename(record.folder));
253
+ if (nextBase === record.title) return record.path;
254
+ return this.#renameFolder(sessionId, record, record.day, nextBase);
255
+ }
221
256
 
222
- const day = record.day;
223
- const fromDir = join(this.root, day, currentBase);
224
- let toDir = join(this.root, day, nextBase);
257
+ #renameFolder(sessionId, record, day, nextBase) {
258
+ const previousFolder = record.folder;
259
+ const previousBase = basename(previousFolder);
260
+ const targetFolder = this.#claimFolder(day, nextBase, sessionId, previousFolder);
261
+ const targetBase = basename(targetFolder);
225
262
 
226
- try {
227
- if (existsSync(fromDir)) {
228
- if (existsSync(toDir)) {
229
- // Another session already uses this title today: keep folders apart.
230
- let n = 2;
231
- while (existsSync(`${toDir}(${n})`)) n++;
232
- toDir = `${toDir}(${n})`;
263
+ if (targetFolder !== previousFolder) {
264
+ try {
265
+ if (existsSync(targetFolder)) {
266
+ mkdirSync(targetFolder, { recursive: true });
233
267
  }
234
- renameSync(fromDir, toDir);
235
- } else {
236
- mkdirSync(toDir, { recursive: true });
268
+ renameSync(previousFolder, targetFolder);
269
+ } catch {
270
+ return previousFolder; // keep the old folder rather than lose the data
237
271
  }
272
+ }
238
273
 
239
- // Rename the volumes to the new base name, keeping their numbers.
240
- for (const file of readdirSync(toDir)) {
241
- const index = volumeIndexOf(file, currentBase);
274
+ // Rename the volumes to the new base name, keeping their numbers.
275
+ try {
276
+ for (const file of readdirSync(targetFolder)) {
277
+ const index = volumeIndexOf(file, previousBase);
242
278
  if (index === 0) continue;
243
- const next = volumeName(nextBase, index);
279
+ const next = volumeName(targetBase, index);
244
280
  if (next === file) continue;
245
- const target = join(toDir, next);
281
+ const target = join(targetFolder, next);
246
282
  if (existsSync(target)) continue;
247
- renameSync(join(toDir, file), target);
283
+ renameSync(join(targetFolder, file), target);
248
284
  }
249
-
250
- const volumes = readdirSync(toDir).filter((f) => volumeIndexOf(f, nextBase) > 0);
251
- const first = volumes.sort((a, b) => volumeIndexOf(a, nextBase) - volumeIndexOf(b, nextBase))[0];
252
- record.path = first ? join(toDir, first) : join(toDir, volumeName(nextBase, 1));
253
- record.title = nextBase;
254
- this.save();
255
- return record.path;
256
285
  } catch {
257
- return record.path;
286
+ /* leave whatever was renamed renamed */
258
287
  }
288
+
289
+ const files = readdirSync(targetFolder)
290
+ .filter((f) => volumeIndexOf(f, targetBase) > 0)
291
+ .sort((a, b) => volumeIndexOf(a, targetBase) - volumeIndexOf(b, targetBase));
292
+
293
+ const path = files.length ? join(targetFolder, files[0]) : join(targetFolder, volumeName(targetBase, 1));
294
+ this.state.sessions[sessionId] = { ...record, path, title: targetBase, folder: targetFolder, day };
295
+ this.save();
296
+ return path;
259
297
  }
260
298
 
261
299
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-memsearch-community",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "Automatic local memory for DeepSeek Harness: per-day, per-session conversation archive with numbered volumes, semantic search index, first-run setup prompts, optional history import.",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",