dsh-memsearch-community 0.3.3 → 0.3.5

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 +14 -0
  2. package/lib/tree.js +72 -18
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -12,6 +12,20 @@ computed locally with ONNX.
12
12
 
13
13
  ## Changes from upstream
14
14
 
15
+ **0.3.5 — one file per session, and renames never overwrite**
16
+
17
+ Two sessions can share a title on the same day. They used to append into a
18
+ single file, and a rename could even replace another session's file — on Windows
19
+ `rename` overwrites silently, which destroyed data. Names are now claimed per
20
+ session (`周报.txt`, `周报 (2).txt`), a rename picks a free name, and an existing
21
+ target aborts the rename instead of clobbering it.
22
+
23
+ **0.3.4 — escape timestamp-like lines in archived text**
24
+
25
+ A reply that quoted a line such as `[2026.12.04 09:00]` looked like a turn
26
+ boundary and split one exchange into several when the archive was parsed.
27
+ Captured text now escapes a leading timestamp.
28
+
15
29
  **0.3.3 — no duplicate rows from continuation turns**
16
30
 
17
31
  A continuation turn (goal round, steered re-run) ends without a new user
package/lib/tree.js CHANGED
@@ -31,6 +31,18 @@ export function safeFileBase(title, fallback) {
31
31
  return base.length > 80 ? base.slice(0, 80).trim() : base;
32
32
  }
33
33
 
34
+ /**
35
+ * Escape timestamp-looking lines inside captured text.
36
+ *
37
+ * `appendTurn` separates turns with a line that starts with `[YYYY.MM.DD HH:MM]`.
38
+ * A reply that quotes such a line — an example, another archive, a log — would
39
+ * read as a turn boundary and split one exchange into several. Prefixing the
40
+ * line with a backslash keeps it visible but no longer a separator.
41
+ */
42
+ export function defuseStamps(text) {
43
+ return String(text).replace(/^(\[\d{4}\.\d{2}\.\d{2} \d{2}:\d{2}\])/gm, "\\$1");
44
+ }
45
+
34
46
  export class Archive {
35
47
  constructor(root) {
36
48
  this.root = root;
@@ -106,6 +118,29 @@ export class Archive {
106
118
  }
107
119
  }
108
120
 
121
+ /**
122
+ * Pick the file a session owns inside `dir` for `base`.
123
+ *
124
+ * One file per session per day: when another session already claimed the name,
125
+ * fall back to `base (2).txt`, `base (3).txt`, and so on instead of appending
126
+ * two conversations into one file. `selfPath` is the caller's current file: it
127
+ * counts as free because the caller is about to move away from it.
128
+ */
129
+ #claimName(dir, base, sessionId, selfPath) {
130
+ const owner = new Map();
131
+ for (const [id, record] of Object.entries(this.state.sessions)) {
132
+ if (record && record.path) owner.set(record.path, id);
133
+ }
134
+ for (let n = 1; n < 1000; n++) {
135
+ const candidate = join(dir, n === 1 ? `${base}.txt` : `${base} (${n}).txt`);
136
+ if (candidate === selfPath) return candidate;
137
+ if (!existsSync(candidate)) return candidate;
138
+ const holder = owner.get(candidate);
139
+ if (holder === undefined || holder === sessionId) return candidate;
140
+ }
141
+ return join(dir, `${base} (${Date.now()}).txt`);
142
+ }
143
+
109
144
  /**
110
145
  * Append one exchange. Returns the file path, renaming an existing file when
111
146
  * the session title changed since the last write.
@@ -115,22 +150,34 @@ export class Archive {
115
150
  const day = dayName(date);
116
151
  const dir = this.dayDir(day);
117
152
  const base = safeFileBase(title, `会话-${String(sessionId).slice(-6)}`);
118
- const target = join(dir, `${base}.txt`);
119
153
 
120
154
  const record = this.state.sessions[sessionId];
121
- if (record && record.path !== target && existsSync(record.path) && !existsSync(target)) {
122
- try {
123
- renameSync(record.path, target);
124
- this.#pruneEmpty(record.path);
125
- } catch {
126
- /* leave the old file in place */
155
+ let target = this.#claimName(dir, base, sessionId);
156
+
157
+ // The session already owns a file from earlier today; keep appending to it,
158
+ // renaming only when the title changed.
159
+ if (record && record.day === day && existsSync(record.path)) {
160
+ const desired = target;
161
+ if (record.path !== desired) {
162
+ try {
163
+ if (!existsSync(desired)) {
164
+ renameSync(record.path, desired);
165
+ target = desired;
166
+ } else {
167
+ target = record.path;
168
+ }
169
+ } catch {
170
+ target = record.path;
171
+ }
172
+ } else {
173
+ target = record.path;
127
174
  }
128
175
  }
129
176
 
130
177
  const block = [
131
178
  `[${dayStamp(date)}]`,
132
- `[User]: ${userText}`,
133
- `[DSH]: ${assistantText || "(无回答)"}`,
179
+ `[User]: ${defuseStamps(userText)}`,
180
+ `[DSH]: ${defuseStamps(assistantText || "(无回答)")}`,
134
181
  "",
135
182
  ].join("\n");
136
183
  appendFileSync(target, block, "utf8");
@@ -144,18 +191,25 @@ export class Archive {
144
191
  const record = this.state.sessions[sessionId];
145
192
  if (!record || !existsSync(record.path)) return null;
146
193
  const dir = join(this.root, record.day);
147
- const target = join(dir, `${safeFileBase(title, `会话-${String(sessionId).slice(-6)}`)}.txt`);
148
- if (target === record.path) return record.path;
149
- try {
150
- if (existsSync(target)) return record.path; // keep both rather than overwrite
151
- renameSync(record.path, target);
152
- record.path = target;
153
- record.title = title;
194
+ const base = safeFileBase(title, `会话-${String(sessionId).slice(-6)}`);
195
+ const previous = record.path;
196
+ const target = this.#claimName(dir, base, sessionId, previous);
197
+ if (target === previous) {
198
+ record.title = base;
154
199
  this.save();
155
- return target;
200
+ return previous;
201
+ }
202
+ // Never let a rename replace another session's file: Windows overwrites.
203
+ if (existsSync(target)) return previous;
204
+ try {
205
+ renameSync(previous, target);
156
206
  } catch {
157
- return record.path;
207
+ return previous;
158
208
  }
209
+ record.path = target;
210
+ record.title = base;
211
+ this.save();
212
+ return target;
159
213
  }
160
214
 
161
215
  #pruneEmpty(oldPath) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-memsearch-community",
3
- "version": "0.3.3",
3
+ "version": "0.3.5",
4
4
  "description": "Automatic local memory for DeepSeek Harness: per-day conversation archive by session title, semantic search index, first-run setup prompts, optional history import.",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",