dsh-session-history 0.2.6 → 0.3.0

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/README.md CHANGED
@@ -31,7 +31,7 @@
31
31
  ## 功能
32
32
 
33
33
  - **多家 agent**:Claude Code、Codex、Antigravity、Pi、DSH、OpenCode、LobsterAI,以及 WorkBuddy(实验性)。装好就自动检测,后装的也会自动出现。会话格式与其中某一家相同的其他 agent,或同一 agent 的第二套配置,可以「自动查找」或手动添加。
34
- - **检索以会话为单位**:会话标题和你的原话权重最高;命中太多时只列标题,并提示可以加哪些词收窄。繁简、全半角、大小写自动统一。
34
+ - **检索以会话为单位**:会话标题和你的原话权重最高,只记得标题、正文里没有那些词也能找到;命中太多时只列标题,并提示可以加哪些词收窄。繁简、全半角、大小写自动统一。
35
35
  - **原始记录兜底**:命令、参数、报错原文多藏在工具调用里,可以在单个会话的原始记录中定点查找。
36
36
  - **Claude Code 记忆提示**:检索时顺带列出对题的 Claude Code 记忆(整理过的结论),可先读这些。
37
37
  - **技能说明自适应**:SKILL.md 只写你电脑上实际有的 agent,不让模型去查不存在的东西。
@@ -82,6 +82,7 @@ dsh plugin --profile desktop add dsh-session-history
82
82
  - **只读不改**:插件只读取各 agent 的会话文件,从不修改它们;日志里你的原话一字不改。
83
83
  - **全部在本地**:不调用模型、不联网;插件新建的数据目录只有你自己的账户能读。
84
84
  - **日志不是指令**:日志里出现的命令和要求是历史记录,SKILL.md 已要求 agent 不得照做,回答时要注明日期和出处。
85
+ - **从 0.2.x 升级到 0.3**:第一次整理会重建索引(一般十几秒到一分钟);Codex、OpenCode 的日志改用完整会话 ID,开了 Git 快照会看到一次性的标题行改动;此前记下的块 ID 需重新检索。
85
86
 
86
87
  ### SKILL.md 是怎么来的
87
88
 
@@ -94,7 +95,7 @@ dsh plugin --profile desktop add dsh-session-history
94
95
 
95
96
  ## 插件页
96
97
 
97
- - **数据目录**:位置、最近整理时间、会话与日志数量、整理是否出错;可打开目录或立即整理。
98
+ - **数据目录**:位置、最近整理时间、会话与日志数量、整理是否正常(某一家出错、索引没更新上、技能文件或快照写不了都会列出,与命令行同一判定);可打开目录或立即整理。
98
99
  - **Agents**:只列检测到的 agent(以及你添加的),每次打开都会重新检测。每家显示会话来源与数量、是否收录、技能状态(已接入/待接入/冲突/不接入)。关掉「收录」只是不再整理它的新会话,已有日志照常检索。
99
100
  - **技能接入**:只有检测到、但还没接入的 agent 才需要操作。按它们实际读取的文件夹归组,一组一个按钮,写明点了之后哪些 agent 生效;已建的软链列在这里,可以移除。
100
101
  - **自动查找/手动添加**:见下节。
@@ -190,7 +191,7 @@ session-history refresh | status | setup | link all | unlink claude | config
190
191
  Git 子目录地址(不提供原生更新检查):
191
192
 
192
193
  ```text
193
- git+https://github.com/fujingzhai/youxia-dsh-plugins.git#dsh-session-history@0.2.6&path:/dsh-session-history
194
+ git+https://github.com/fujingzhai/youxia-dsh-plugins.git#dsh-session-history@0.3.0&path:/dsh-session-history
194
195
  ```
195
196
 
196
197
  ## 兼容与限制
@@ -17,7 +17,7 @@ const { resolveHome, loadConfig, saveConfig, layout, readJson, DEFAULTS } = awai
17
17
  const { connect, updateIndex } = await import('../lib/store.js');
18
18
  const { search, get, outline, timeline, UsageError } = await import('../lib/recall.js');
19
19
  const { raw } = await import('../lib/raw.js');
20
- const { refresh, refreshIfStale } = await import('../lib/refresh.js');
20
+ const { refresh, refreshIfStale, problems } = await import('../lib/refresh.js');
21
21
  const { allSources, byId, byFolder, enabled } = await import('../lib/agents.js');
22
22
  const { installSkill, logFolders, VERSION } = await import('../lib/skill.js');
23
23
  const { skillStatus, link, linkAll, unlink } = await import('../lib/links.js');
@@ -100,6 +100,10 @@ function statusText(home) {
100
100
  const { agents, targets, links } = skillStatus(home, config);
101
101
  const lines = [`Session History ${VERSION}|数据目录 ${tilde(home)}`];
102
102
  lines.push(last ? `最近整理 ${last.run}(${(last.ms / 1000).toFixed(1)} 秒)|索引 ${last.index?.sessions ?? '?'} 个会话、${last.index?.files ?? '?'} 份日志` : '尚未整理过:先跑 refresh');
103
+ // 与插件页同一份判定;各家的整理错误在下面各自那行显示,这里列其余的。
104
+ const issues = problems(last);
105
+ if (last) lines.push(issues.length ? `状态:${issues.length} 处问题` : '状态:正常');
106
+ for (const p of issues.filter(x => x.scope !== 'agent')) lines.push(` ${p.text}`);
103
107
  for (const a of agents.filter(x => x.installed || x.custom)) {
104
108
  const run = last?.agents?.[a.folder];
105
109
  const src = !a.installed ? '未检测到' : enabled(byId(a.id), config) ? (run ? (run.ok ? `${run.files} 个会话文件 · 日志 ${logDaysOf(a.folder)} 天` : `整理出错:${run.error}`) : '已启用,待整理') : '已检测到,未启用整理';
@@ -155,13 +159,16 @@ async function main() {
155
159
  case 'refresh': {
156
160
  const only = agent ? [byFolder(agent, config)?.id].filter(Boolean) : null;
157
161
  const status = refresh({ home, only, log });
158
- if (args.json) return console.log(JSON.stringify(status, null, 2));
162
+ const issues = problems(status);
163
+ // 与插件页、status 同一份判定:有任何一处问题就以 1 退出(--json 也一样,宿主据输出内容显示问题)。
164
+ if (issues.length) process.exitCode = 1;
165
+ if (args.json) return console.log(JSON.stringify({ ...status, problems: issues }, null, 2));
159
166
  if (status.skipped) return console.log(status.reason);
160
167
  for (const [name, r] of Object.entries(status.agents)) console.log(`${name}: ${r.ok ? `OK 会话文件 ${r.files}(重读 ${r.parsed})· 日志 ${r.days} 天 · 变更 ${r.changed} 天` : 'ERROR ' + r.error}`);
161
- console.log(`索引: ${status.index.ok ? `${status.index.sessions} 个会话 · 更新 ${status.index.updated} 份` : 'ERROR ' + status.index.error}|${(status.ms / 1000).toFixed(1)} 秒`);
168
+ console.log(`索引: ${status.index.ok ? `${status.index.sessions} 个会话 · 更新 ${status.index.updated} 份` : 'ERROR 日志已保存,索引没更新上:' + status.index.error}|${(status.ms / 1000).toFixed(1)} 秒`);
169
+ for (const p of issues.filter(x => x.scope === 'skill' || x.scope === 'git')) console.log(p.text);
162
170
  const warnings = Object.values(status.agents).flatMap(r => r.warnings || []);
163
171
  if (warnings.length) console.error(warnings.slice(0, 20).join('\n') + (warnings.length > 20 ? `\n……另有 ${warnings.length - 20} 条` : ''));
164
- if (Object.values(status.agents).some(r => !r.ok) || !status.index.ok) process.exitCode = 1;
165
172
  return;
166
173
  }
167
174
  case 'status':
package/client.js CHANGED
@@ -100,7 +100,8 @@ window.__ModuleLoader__.load({
100
100
  }
101
101
 
102
102
  function Home({ data, pending }) {
103
- const last = data.last, bad = Object.entries(last?.agents || {}).filter(([, r]) => !r.ok).map(([k]) => k);
103
+ // 是否正常由宿主按整理状态统一判定(与命令行同一份):某一家出错、索引没更新上、技能文件或快照写不了,都算。
104
+ const last = data.last, issues = data.problems || [];
104
105
  const busy = pending === 'refresh' || data.running;
105
106
  return h('section', null,
106
107
  h('div', { className: 'yxsh-head' }, h('h3', { className: 'yxsh-grow' }, '数据目录'),
@@ -115,8 +116,9 @@ window.__ModuleLoader__.load({
115
116
  h('div', { className: 'yxsh-stat' }, h('span', { className: 'yxsh-muted' }, '会话'), h('b', null, last?.index ? num(last.index.sessions) : '—'),
116
117
  h('span', { className: 'yxsh-muted' }, last?.index ? `${num(last.index.files)} 份日志` : '整理后生成')),
117
118
  h('div', { className: 'yxsh-stat' }, h('span', { className: 'yxsh-muted' }, '状态'),
118
- bad.length ? h('b', { className: 'yxsh-error' }, `${bad.join('、')} 出错`) : h('b', null, last ? '正常' : '等待首次整理'),
119
- h('span', { className: 'yxsh-muted' }, data.config.refreshMinutes ? `定时:${({ 30: '每 30 分钟', 60: '每小时整点', 120: '每 2 小时', 360: '每 6 小时' })[data.config.refreshMinutes] || `每 ${data.config.refreshMinutes} 分钟`}` : '定时整理已关闭')))));
119
+ issues.length ? h('b', { className: 'yxsh-error' }, `${issues.length} 处问题`) : h('b', null, last ? '正常' : '等待首次整理'),
120
+ h('span', { className: 'yxsh-muted' }, data.config.refreshMinutes ? `定时:${({ 30: '每 30 分钟', 60: '每小时整点', 120: '每 2 小时', 360: '每 6 小时' })[data.config.refreshMinutes] || `每 ${data.config.refreshMinutes} 分钟`}` : '定时整理已关闭'))),
121
+ issues.length ? h('div', { className: 'yxsh-error' }, issues.map(p => p.text).join('\n')) : null));
120
122
  }
121
123
 
122
124
  function AgentRow({ a, pending }) {
package/index.js CHANGED
@@ -9,6 +9,7 @@ import { installSkill, skillMarkdown, logFolders, findNode, ENGINE, VERSION } fr
9
9
  import { skillStatus, linkTarget, linkAll, unlinkTarget } from './lib/links.js';
10
10
  import { tilde, expand, isDir } from './lib/files.js';
11
11
  import { scan } from './lib/scan.js';
12
+ import { problems } from './lib/refresh.js';
12
13
 
13
14
  export const name = 'session-history';
14
15
  export const ROUTE = '/youxia-session-history/api';
@@ -66,7 +67,8 @@ export function createRunner(home, { node = findNode(), engine = ENGINE } = {})
66
67
  child.on('close', code => {
67
68
  let status = null;
68
69
  try { status = JSON.parse(out); } catch { /* 非 JSON 输出 */ }
69
- resolve(status ? { ok: code === 0 || !!status.skipped, status } : { ok: false, error: (err || out || `退出码 ${code}`).trim().slice(-800) });
70
+ // 拿到状态就算跑完了:某一家或索引出错时引擎以 1 退出,问题写在状态里,由插件页显示。
71
+ resolve(status ? { ok: true, status } : { ok: false, error: (err || out || `退出码 ${code}`).trim().slice(-800) });
70
72
  });
71
73
  }).finally(() => { running = null; });
72
74
  return running;
@@ -92,7 +94,7 @@ export function snapshot(home, runner) {
92
94
  // 只剩旧日志:日志文件夹对应的 agent 不在列表里(已卸载,或来自不再支持的来源)。新用户一般没有。
93
95
  const listed = new Set(shown.map(a => a.folder));
94
96
  return {
95
- version: VERSION, home: tilde(home), homeAbs: home, config, last, running: runner?.running ?? false,
97
+ version: VERSION, home: tilde(home), homeAbs: home, config, last, problems: problems(last), running: runner?.running ?? false,
96
98
  agents: shown,
97
99
  targets: status.targets, links: status.links, conflicts: status.conflicts,
98
100
  legacy: logFolders(home).filter(f => !listed.has(f)),
package/lib/markdown.js CHANGED
@@ -6,21 +6,36 @@ import { splitlines, strip, rstrip, words, cut } from './py.js';
6
6
 
7
7
  export const SEP = ' · '; // 会话标题行「# ID · 原生标题」
8
8
  const SLUG = /^[a-z0-9]+(?:-[a-z0-9]+)+$/; // clear-conversation-state 之类的机器名
9
- const FENCE_TOGGLE = /^\s*(```|~~~)/;
10
- const FENCE = /^\s*(`{3,}|~{3,})/;
9
+ // 代码围栏:行首至多缩进 3 格(同 CommonMark),缩进更深的不算,比如 Python 报错回溯里标出错位置的 ~~~~^^。
10
+ // 收尾=同种符号、不短于开头;带信息串的也算收尾(与 0.2.x 相同)。模型常把代码块嵌在 ```md 里,
11
+ // 按 CommonMark 严格认收尾,旧日志里这类发言反而会一路开到底;本机全部旧日志对比过,这样认出错最少。
12
+ const FENCE = /^ {0,3}(`{3,}|~{3,})/;
11
13
  const HEADING = /^(#{1,6})(\s+\S)/;
12
14
 
13
- /** 正文标题整体降 levels 级(# → ###),嵌在「## 时刻 角色」之下;代码围栏内不动。 */
15
+ /** 读入一行后的围栏状态:null=不在围栏里,否则是开头的围栏符。写日志、分段、建索引共用这一条规则。 */
16
+ export function fenceStep(fence, line) {
17
+ const m = FENCE.exec(line);
18
+ if (!m) return fence;
19
+ const token = m[1];
20
+ if (fence === null) return token;
21
+ return token[0] === fence[0] && token.length >= fence.length ? null : fence;
22
+ }
23
+
24
+ /**
25
+ * 一条发言的正文:标题整体降 levels 级(# → ###),嵌在「## 时刻 角色」之下;代码围栏内不动。
26
+ * 发言停在代码块中间(回复被打断、截断)时补上收尾,否则后面的发言头和会话标题都会被当成代码。
27
+ */
14
28
  export function demote(text, levels = 2) {
15
- let inFence = false;
16
- return text.split('\n').map(line => {
17
- if (FENCE_TOGGLE.test(line)) { inFence = !inFence; return line; }
18
- if (!inFence) {
19
- const m = HEADING.exec(line);
20
- if (m) return '#'.repeat(Math.min(m[1].length + levels, 6)) + line.slice(m[1].length);
21
- }
22
- return line;
23
- }).join('\n');
29
+ let fence = null;
30
+ const lines = text.split('\n').map(line => {
31
+ const inside = fence !== null;
32
+ fence = fenceStep(fence, line);
33
+ if (inside) return line;
34
+ const m = HEADING.exec(line);
35
+ return m ? '#'.repeat(Math.min(m[1].length + levels, 6)) + line.slice(m[1].length) : line;
36
+ });
37
+ if (fence !== null) lines.splice(lines[lines.length - 1] === '' ? -1 : lines.length, 0, fence);
38
+ return lines.join('\n');
24
39
  }
25
40
 
26
41
  /** 原生标题整理:空白归并、去引号、截 60 字;连字符机器名不用。 */
@@ -36,17 +51,10 @@ export function headId(line) {
36
51
  return at >= 0 ? key.slice(0, at) : key;
37
52
  }
38
53
 
54
+ /** 逐行喂入,返回读完这一行后是否在围栏外(围栏的开头、收尾行本身不会是标题行)。 */
39
55
  function fenceTracker() {
40
56
  let fence = null;
41
- return line => {
42
- const m = FENCE.exec(line);
43
- if (m) {
44
- const token = m[1];
45
- if (fence === null) fence = token;
46
- else if (token[0] === fence[0] && token.length >= fence.length) fence = null;
47
- }
48
- return fence === null;
49
- };
57
+ return line => (fence = fenceStep(fence, line)) === null;
50
58
  }
51
59
 
52
60
  /** 给会话标题行补上原生标题;判定标题行的规则与 sections 相同。 */
@@ -79,10 +87,24 @@ export function sections(text) {
79
87
  }
80
88
 
81
89
 
90
+ // 旧版写的短会话 ID:Codex 只取前 8 位;OpenCode 写成「标题(ses_ 加 4 位)」。新 ID 以它开头即视为同一会话。
91
+ const LEGACY_CODEX = /^[0-9a-f]{8}$/;
92
+ const LEGACY_OPENCODE = /((ses_[0-9A-Za-z]{4}))$/;
93
+
94
+ /** 旧日志里的会话 ID 在新渲染中是否还在(含上述两种旧写法)。 */
95
+ export function stillPresent(key, current) {
96
+ if (current.has(key)) return true;
97
+ const m = LEGACY_OPENCODE.exec(key);
98
+ const prefix = m ? m[1] : LEGACY_CODEX.test(key) ? key + '-' : null;
99
+ if (!prefix) return false;
100
+ for (const k of current.keys()) if (k.startsWith(prefix) && k.length > prefix.length) return true;
101
+ return false;
102
+ }
103
+
82
104
  /** 来源里已没有的旧会话段原样保留(应用清理了历史也不丢日志);返回 [正文, 保留段数]。 */
83
105
  export function preserveMissing(old, next) {
84
106
  const current = sections(next);
85
- const missing = [...sections(old)].filter(([key]) => !current.has(key)).map(([, body]) => body);
107
+ const missing = [...sections(old)].filter(([key]) => !stillPresent(key, current)).map(([, body]) => body);
86
108
  if (!missing.length) return [next, 0];
87
109
  // 段间只留一个空行。
88
110
  return [rstrip(next) + '\n\n' + missing.map(m => rstrip(m, '\n')).join('\n\n') + '\n', missing.length];
package/lib/raw.js CHANGED
@@ -6,7 +6,7 @@ import { readText } from './files.js';
6
6
  import { parseJson, isObject } from './py.js';
7
7
  import { norm, trad } from './normalize.js';
8
8
  import { createClock } from './clock.js';
9
- import { shortSid } from './text.js';
9
+ import { sessionLabeler } from './text.js';
10
10
  import { parseQuery, pickSession, excerpt, budgeted, occurrences, UsageError } from './recall.js';
11
11
 
12
12
  const SKIP = new Set(['signature', 'data', 'encrypted_content']); // 签名、图片、加密思考:大段无字内容
@@ -82,7 +82,7 @@ export function raw(db, args, ctx) {
82
82
  const clock = createClock(config);
83
83
  const [about, message] = pickSession(db, args.target, args.agent);
84
84
  if (!about) return message;
85
- const tag = `${about.agent} ${shortSid(about.session)}「${about.title}」`;
85
+ const tag = `${about.agent} ${sessionLabeler(db)(about.agent, about.session)}「${about.title}」`;
86
86
  const source = byFolder(about.agent, config);
87
87
  const key = about.session.startsWith('session-') ? about.session.slice(8) : about.session;
88
88
  const files = source ? source.rawFiles(pathsOf(source, config), key) : [];
package/lib/recall.js CHANGED
@@ -5,7 +5,7 @@ import { norm, normChar } from './normalize.js';
5
5
  import { mask, OVERLAP } from './store.js';
6
6
  import { createClock } from './clock.js';
7
7
  import { tilde, home } from './files.js';
8
- import { roleTools, clip, plain, machine, speaker, shortSid, span, mark, phrases } from './text.js';
8
+ import { roleTools, clip, plain, machine, speaker, sessionLabeler, span, mark, phrases, safeAt, skipChars } from './text.js';
9
9
 
10
10
  export class UsageError extends Error {}
11
11
 
@@ -36,10 +36,15 @@ export function parseQuery(words) {
36
36
 
37
37
  export function resolveSessions(db, token, agent = null) {
38
38
  const t = String(token).trim();
39
- const sql = "SELECT * FROM sessions WHERE (session=? OR substr(session,1,length(?))=? OR substr(session,1,length(?)+8)='session-'||? OR instr(session,'('||?||')')>0)";
40
- const rows = db.prepare(sql + (agent ? ' AND agent=?' : '')).all(...Array(6).fill(t), ...(agent ? [agent] : []));
39
+ if (!t) return [];
40
+ const scope = agent ? ' AND agent=?' : '', extra = agent ? [agent] : [];
41
+ const sql = "SELECT * FROM sessions WHERE (session=? OR substr(session,1,length(?))=? OR substr(session,1,length(?)+8)='session-'||? OR instr(session,'('||?)>0)";
42
+ const rows = db.prepare(sql + scope).all(...Array(6).fill(t), ...extra);
41
43
  const exact = rows.filter(r => r.session === t);
42
- return exact.length ? exact : rows;
44
+ if (exact.length) return exact;
45
+ if (rows.length || t.length < 8) return rows;
46
+ // 短 ID 也可能是会话 ID 中间的一段,比如 IM 会话末尾的 UUID。
47
+ return db.prepare('SELECT * FROM sessions WHERE instr(session, ?)>0' + scope).all(t, ...extra);
43
48
  }
44
49
 
45
50
  export function findChunk(db, token, strict = true) {
@@ -54,6 +59,8 @@ export function findChunk(db, token, strict = true) {
54
59
  const escapeRe = s => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
55
60
  const isShortWord = alt => /^[a-z0-9]{1,3}$/.test(alt);
56
61
  const bounded = alt => new RegExp(`(?<![a-z0-9])${escapeRe(alt)}(?![a-z0-9])`, 'g');
62
+ /** 题目里有没有这个词;短英文词同样按词边界。 */
63
+ const inTitle = (title, alt) => isShortWord(alt) ? bounded(alt).test(title) : title.includes(alt);
57
64
 
58
65
  function countOf(text, alt) {
59
66
  if (isShortWord(alt)) return (text.match(bounded(alt)) || []).length;
@@ -142,7 +149,7 @@ export function excerpt(row, groups, width) {
142
149
  const radius = Math.max(24, Math.floor((Math.floor(width / spots.length) - Math.max(...spots.map(s => s[1]))) / 2));
143
150
  const windows = [];
144
151
  for (const [pos, n] of spots) {
145
- const a = Math.max(0, pos - radius), b = Math.min(body.length, pos + n + radius);
152
+ const a = safeAt(body, Math.max(0, pos - radius)), b = safeAt(body, Math.min(body.length, pos + n + radius));
146
153
  if (windows.length && a <= windows[windows.length - 1][1]) windows[windows.length - 1][1] = Math.max(windows[windows.length - 1][1], b);
147
154
  else windows.push([a, b]);
148
155
  }
@@ -205,10 +212,13 @@ export function search(db, args, ctx) {
205
212
  const clock = createClock(config), tools = roleTools(config.userLabel);
206
213
  const groups = parseQuery(args.query);
207
214
  if (!groups.length) throw new UsageError('请提供至少一个关键词。');
215
+ const label = sessionLabeler(db);
208
216
  let keys = null;
209
217
  if (args.session) {
210
- keys = resolveSessions(db, args.session, args.agent);
211
- if (!keys.length) return `没有匹配 ${args.session} 的会话。`;
218
+ // 与 outline、raw 同一套:匹配到多场就列出来请对方选,不把几场的命中混在一起。
219
+ const [about, message] = pickSession(db, args.session, args.agent);
220
+ if (!about) return message;
221
+ keys = [about];
212
222
  }
213
223
  const [where, params] = chunkFilters(args, keys, clock, tools);
214
224
  const total = db.prepare('SELECT count(*) n FROM chunks').get().n || 1;
@@ -226,11 +236,29 @@ export function search(db, args, ctx) {
226
236
  }
227
237
  df.push(matched.size);
228
238
  });
239
+ // 只有题目对上的会话也算候选:题目常是概括,用词未必出现在正文里。限定了会话或发言人时不看题目。
240
+ const titleHits = new Map();
241
+ if (!keys && !args.role) {
242
+ const cond = [], p = [];
243
+ const since = parseDay(args.since, clock), until = parseDay(args.until, clock);
244
+ if (args.agent) { cond.push('agent=?'); p.push(args.agent); }
245
+ if (since) { cond.push('last_day>=?'); p.push(since); }
246
+ if (until) { cond.push('first_day<=?'); p.push(until); }
247
+ const titled = db.prepare(['SELECT agent, session, title_norm FROM sessions WHERE instr(title_norm, ?)>0', ...cond].join(' AND '));
248
+ groups.forEach(([, alts], g) => {
249
+ for (const alt of alts) for (const r of titled.all(alt, ...p)) {
250
+ if (!inTitle(r.title_norm, alt)) continue;
251
+ const key = `${r.agent}\u0000${r.session}`;
252
+ if (!titleHits.has(key)) titleHits.set(key, new Set());
253
+ titleHits.get(key).add(g);
254
+ }
255
+ });
256
+ }
229
257
  const idf = df.map(n => Math.log(1 + total / (n + 0.5)));
230
258
  const head = '词频 ' + groups.map(([label], i) => `${label} ${df[i]}块`).join(' · ');
231
259
  // 记忆是跨会话的结论,限定了会话、agent 或发言人时不列。
232
260
  const notes = keys || args.agent || args.role || !config.memoryHints ? '' : memoryNotes(db, groups);
233
- if (!hits.size) {
261
+ if (!hits.size && !titleHits.size) {
234
262
  return [head, notes, (notes ? '日志' : '') + '未查到记录。可换同义词(词内用 | 连接任选)、减少词或放宽日期;不能据此断言从未发生。'
235
263
  + `接口名、参数、报错原文等多只在工具里:对相关会话用 ${cmd} raw <会话ID> 词 查原始记录。`].filter(Boolean).join('\n');
236
264
  }
@@ -246,7 +274,7 @@ export function search(db, args, ctx) {
246
274
  if (keys) {
247
275
  const best = [...hits.keys()].sort(desc(weight)).slice(0, args.limit);
248
276
  const info = keys[0];
249
- const out = [head + `|会话 ${info.agent} ${shortSid(info.session)}「${info.title}」内按时间列 ${best.length}/${hits.size} 块`];
277
+ const out = [head + `|会话 ${info.agent} ${label(info.agent, info.session)}「${info.title}」内按时间列 ${best.length}/${hits.size} 块`];
250
278
  for (const r of fetch(best)) out.push(lineOf(r, excerpt(r, groups, args.snippet)));
251
279
  return budgeted(out, args.budget);
252
280
  }
@@ -254,21 +282,23 @@ export function search(db, args, ctx) {
254
282
  const count = groups.length;
255
283
  const info = new Map(db.prepare('SELECT * FROM sessions').all().map(r => [`${r.agent}\u0000${r.session}`, r]));
256
284
  const stats = new Map();
285
+ const blank = () => ({ tf: Array(count).fill(0), n: Array(count).fill(0), asked: Array.from({ length: count }, () => new Set()), turns: new Map(), cooc: 0, rids: [] });
257
286
  for (const [rid, found] of hits) {
258
287
  const r = meta.get(rid), key = `${r.agent}\u0000${r.session}`;
259
- if (!stats.has(key)) stats.set(key, { tf: Array(count).fill(0), n: Array(count).fill(0), asked: Array.from({ length: count }, () => new Set()), turns: new Map(), cooc: 0, rids: [] });
288
+ if (!stats.has(key)) stats.set(key, blank());
260
289
  const s = stats.get(key), spot = `${r.day}|${r.turn}`, user = tools.said(r);
261
290
  for (const [g, tf] of found) { s.tf[g] += tf; s.n[g] += 1; if (user) s.asked[g].add(spot); }
262
291
  if (user) { if (!s.turns.has(spot)) s.turns.set(spot, new Set()); for (const g of found.keys()) s.turns.get(spot).add(g); }
263
292
  s.cooc += found.size === count ? 1 : 0;
264
293
  s.rids.push(rid);
265
294
  }
295
+ for (const key of titleHits.keys()) if (!stats.has(key) && info.has(key)) stats.set(key, blank());
266
296
  const todayMs = Date.parse(clock.today() + 'T00:00:00Z');
267
297
  for (const [key, s] of stats) {
268
298
  const about = info.get(key);
269
299
  // 题目(各家自起的标题或用户开场的请求)和用户原话最能说明"这场会话是关于什么的",助手顺口提到的词权重低。
270
- const titled = new Set([...Array(count).keys()].filter(g => groups[g][1].some(a => about.title_norm.includes(a))));
271
- const covered = [...Array(count).keys()].filter(g => s.n[g]);
300
+ const titled = new Set([...Array(count).keys()].filter(g => groups[g][1].some(a => inTitle(about.title_norm, a))));
301
+ const covered = [...Array(count).keys()].filter(g => s.n[g] || titled.has(g));
272
302
  let rel = 0;
273
303
  for (const g of covered) rel += idf[g] * (1 + 0.5 * Math.log1p(s.tf[g]) + Math.min(1, s.n[g] / Math.max(about.chunks, 1)) + Math.log1p(s.asked[g].size) + 2 * titled.has(g));
274
304
  if (count > 1) {
@@ -314,10 +344,11 @@ export function search(db, args, ctx) {
314
344
  }
315
345
  shown.forEach((key, i) => {
316
346
  const s = stats.get(key), about = info.get(key);
317
- const missing = full.length ? '' : ' 缺:' + [...Array(count).keys()].filter(g => !s.n[g]).map(g => groups[g][0]).join('、');
347
+ const missing = full.length ? '' : ' 缺:' + [...Array(count).keys()].filter(g => !s.n[g] && !s.titled.has(g)).map(g => groups[g][0]).join('、');
318
348
  const same = twins.get(key) ? `(另有 ${twins.get(key)} 个同题会话)` : '';
319
- const lines = [`${mark(i)} ${span(about.first_day, about.last_day)} ${about.agent} ${shortSid(about.session)} `
320
- + `${about.turns}轮 命中${s.rids.length}块「${about.title}」${same}${missing}`];
349
+ const id = label(about.agent, about.session);
350
+ const lines = [`${mark(i)} ${span(about.first_day, about.last_day)} ${about.agent} ${id} `
351
+ + `${about.turns}轮 ${s.rids.length ? `命中${s.rids.length}块` : '仅题目命中'}「${about.title}」${same}${missing}`];
321
352
  if (broad) {
322
353
  if (!s.titled.size) {
323
354
  let best = null, bestKey = null;
@@ -332,7 +363,8 @@ export function search(db, args, ctx) {
332
363
  if (!turnsSeen.has(spot)) { picked.push(rid); turnsSeen.add(spot); }
333
364
  if (picked.length >= (i < 3 ? 2 : 1)) break;
334
365
  }
335
- for (const r of fetch(picked)) lines.push(' ' + lineOf(r, excerpt(r, groups, args.snippet)));
366
+ if (picked.length) for (const r of fetch(picked)) lines.push(' ' + lineOf(r, excerpt(r, groups, args.snippet)));
367
+ else lines.push(` ↳ 正文里没有这些词;看这一场用 outline ${id}`);
336
368
  }
337
369
  out.push(lines.join('\n'));
338
370
  });
@@ -346,7 +378,7 @@ export function get(db, args, ctx) {
346
378
  const rows = db.prepare('SELECT * FROM chunks WHERE path=? AND session=? AND turn BETWEEN ? AND ? ORDER BY turn, part')
347
379
  .all(hit.path, hit.session, hit.turn - span_, hit.turn + span_);
348
380
  const about = resolveSessions(db, hit.session, hit.agent);
349
- const head = `${hit.path}|${hit.agent} ${shortSid(hit.session)}「${about.length ? about[0].title : ''}」`;
381
+ const head = `${hit.path}|${hit.agent} ${sessionLabeler(db)(hit.agent, hit.session)}「${about.length ? about[0].title : ''}」`;
350
382
  // 预算先给命中块,再按距离向外扩;同距离时用户发言看后文,助手发言看前文。
351
383
  const laterFirst = tools.isUser(hit.role);
352
384
  const priority = r => {
@@ -368,10 +400,11 @@ export function get(db, args, ctx) {
368
400
  for (const r of rows) {
369
401
  if (!chosen.has(r.rid)) { gap = prev !== null; continue; }
370
402
  let body = r.body;
371
- if (r.rid === hit.rid && body.length > args.budget - head.length - 80) body = body.slice(0, Math.max(0, args.budget - head.length - 100)) + '…';
403
+ if (r.rid === hit.rid && body.length > args.budget - head.length - 80) body = body.slice(0, safeAt(body, Math.max(0, args.budget - head.length - 100))) + '…';
372
404
  const last = out[out.length - 1];
373
405
  if (prev !== null && !gap && prev.turn === r.turn && prev.part === r.part - 1 && typeof last === 'object') {
374
- last.end = r.end; last.text += body.slice(OVERLAP); last.hit ||= r.rid === hit.rid;
406
+ // 分块重叠按字数算,扩展区汉字与 emoji 在 JS 里占两个单位,不能按长度截。
407
+ last.end = r.end; last.text += skipChars(body, OVERLAP); last.hit ||= r.rid === hit.rid;
375
408
  } else {
376
409
  if (gap) out.push('……');
377
410
  out.push({ row: r, end: r.end, text: body, hit: r.rid === hit.rid });
@@ -409,7 +442,7 @@ export function outline(db, args, ctx) {
409
442
  const first = rows.filter(r => r.part === 0);
410
443
  const notices = first.filter(r => tools.isUser(r.role) && machine(r.body)).length;
411
444
  const paths = [...new Set(rows.map(r => r.path))].join('、');
412
- const head = `${about.agent} ${shortSid(about.session)}「${about.title}」|${span(about.first_day, about.last_day)} `
445
+ const head = `${about.agent} ${sessionLabeler(db)(about.agent, about.session)}「${about.title}」|${span(about.first_day, about.last_day)} `
413
446
  + `${about.first_clock} 起|${about.turns}轮(${ctx.config.userLabel} ${about.user_turns})|${paths}`
414
447
  + (notices ? `|略去 ${notices} 条后台任务通知` : '');
415
448
  const shown = first.filter(r => (args.all || tools.isUser(r.role)) && !(tools.isUser(r.role) && machine(r.body)));
@@ -434,11 +467,11 @@ export function timeline(db, args, ctx) {
434
467
  const sql = 'SELECT * FROM sessions WHERE last_day>=? AND first_day<=?' + (args.agent ? ' AND agent=?' : '') + ' ORDER BY first_day DESC, first_clock, agent, session';
435
468
  const rows = db.prepare(sql).all(since, until, ...(args.agent ? [args.agent] : []));
436
469
  const shown = rows.filter(r => args.all || r.chars >= 300);
437
- const out = [];
470
+ const out = [], label = sessionLabeler(db);
438
471
  let day = null;
439
472
  for (const r of shown) {
440
473
  if (r.first_day !== day) { day = r.first_day; out.push(day); }
441
- out.push(` ${r.first_clock} ${r.agent} ${shortSid(r.session)} ${r.turns}轮「${r.title}」`);
474
+ out.push(` ${r.first_clock} ${r.agent} ${label(r.agent, r.session)} ${r.turns}轮「${r.title}」`);
442
475
  }
443
476
  const hidden = rows.length - shown.length;
444
477
  const note = `(${since} 起共 ${rows.length} 个会话` + (hidden ? `,隐藏 ${hidden} 个不足 300 字的,加 --all 显示)` : ')');
package/lib/refresh.js CHANGED
@@ -149,20 +149,35 @@ export function refresh({ home, only = null, log = () => {}, engine = null } = {
149
149
  log(`${source.folder}: ${agents[source.folder].ok ? 'OK' : 'ERROR ' + agents[source.folder].error}`);
150
150
  }
151
151
  } finally { cache.close(); }
152
+ const previous = readJson(dirs.status, {});
152
153
  let index;
153
154
  try { index = { ok: true, ...updateIndex(home, config) }; }
154
- catch (error) { index = { ok: false, error: error.message }; }
155
+ // 写入失败会整体回滚,检索仍用上次的索引;计数也沿用上次的,面板不至于显示成空。
156
+ catch (error) { index = { ok: false, error: error.message, sessions: previous.index?.sessions, files: previous.index?.files }; }
155
157
  const git = config.gitSnapshots ? snapshot(home, clock) : null;
156
158
  let skill = null;
157
159
  try { skill = installSkill(home, { engine }); } catch (error) { skill = { ok: false, error: error.message }; }
158
160
  const status = { run: clock.iso(), ms: Date.now() - started, agents, index, ...(git ? { git } : {}), skill };
159
- const previous = readJson(dirs.status, {});
160
161
  if (only) status.agents = { ...(previous.agents || {}), ...agents };
161
162
  atomicWrite(dirs.status, JSON.stringify(status, null, 2) + '\n');
162
163
  return status;
163
164
  } finally { release(); }
164
165
  }
165
166
 
167
+ /**
168
+ * 一次整理留下的问题:CLI 的输出与退出码、status、插件页都按这一份判断是否正常。
169
+ * scope:agent(某一家整理出错)、index(日志已写好、索引没更新上)、skill、git。
170
+ */
171
+ export function problems(status) {
172
+ if (!status || status.skipped) return [];
173
+ const out = [];
174
+ for (const [name, r] of Object.entries(status.agents || {})) if (r && r.ok === false) out.push({ scope: 'agent', agent: name, text: `${name} 整理出错:${r.error}` });
175
+ if (status.index && status.index.ok === false) out.push({ scope: 'index', text: `日志已保存,检索索引更新失败(检索仍用上次的索引):${status.index.error}` });
176
+ if (status.skill && status.skill.ok === false) out.push({ scope: 'skill', text: `技能文件写入失败:${status.skill.error}` });
177
+ if (status.git && status.git.ok === false) out.push({ scope: 'git', text: `日志快照失败:${status.git.reason || status.git.error || 'git 出错'}` });
178
+ return out;
179
+ }
180
+
166
181
  /** 距上次整理超过 staleMinutes 才补整理;拿不到锁(别处正在整理)就直接用现有索引。 */
167
182
  export function refreshIfStale({ home, log, engine } = {}) {
168
183
  const config = loadConfig(home);
package/lib/skill.js CHANGED
@@ -76,11 +76,11 @@ export function skillMarkdown(home, { config = loadConfig(home), platform = proc
76
76
  `**1. search 找会话**:查询词=**主体词+事件词**,如 \`${cmd} search 插件 更新失败\`、\`${cmd} search "VPN|openconnect" 断线 --agent ${first} --since 08-01\`;只给主体词必然过宽。`,
77
77
  '',
78
78
  '- 空格分隔=AND(同一会话内即可);词内 `|` 列同义词、别称、异写,含 `|` 须加引号。繁简、全半角、大小写自动统一;≤3 字符的英文数字词按整词匹配(`pi` 不中 `api`)。',
79
- '- 排名以题目(各家自起的会话标题,没有才取用户开场请求)和用户原话为重,助手顺口提到的权重低。范围宽时只列题目+常同现的词,照提示加词收窄;范围窄时每个会话附一两段原文,行首是块 ID。',
79
+ '- 排名以题目(各家自起的会话标题,没有才取用户开场请求)和用户原话为重,助手顺口提到的权重低。范围宽时只列题目+常同现的词,照提示加词收窄;范围窄时每个会话附一两段原文,行首是块 ID;标「仅题目命中」的是正文里没有这些词、题目对上的,用 outline 看。',
80
80
  ...(has('claude') && config.memoryHints ? ['- 不加 `--agent`/`--session`/`--role` 时可能先列至多 2 条 Claude Code 记忆(整理过的结论):对题先读,常比翻会话省;与较新的会话冲突以新的为准。'] : []),
81
81
  `- 收窄:\`--agent\`(${folders.join('|') || first});\`--since\`/\`--until\`(\`2026-09-01\`、\`09-01\`、\`today\`、\`7d\`;\`--until\` 含当天);\`--role user\` 只看用户原话;\`--sort recent\` 要最新说法(默认只轻度看时间)。`,
82
82
  '',
83
- `**2. 会话内定位**:\`${cmd} search 更新失败 --session 01a06cc6\` 按时间列该会话命中块;或 \`${cmd} outline 01a06cc6\` 看用户每条发言+末轮回复(\`--all\` 含助手发言)。会话 ID 给前缀即可,outline 也收块 ID。`,
83
+ `**2. 会话内定位**:\`${cmd} search 更新失败 --session 01a06cc6\` 按时间列该会话命中块;或 \`${cmd} outline 01a06cc6\` 看用户每条发言+末轮回复(\`--all\` 含助手发言)。会话 ID 照结果里的抄,给前缀也行(对上多场会列出来请你挑),outline 也收块 ID。`,
84
84
  '',
85
85
  `**3. 读原文**:\`${cmd} get 2ddbae3eed --context 1 --budget 2000\`,块 ID 至少 6 位;按剩余预算调整 \`--budget\`(命令默认 4000 字符)。仍不够就按首行路径和行号局部读日志文件。`,
86
86
  '',
@@ -81,7 +81,9 @@ export function parse(path) {
81
81
  turns.push([ev.timestamp, payload.role, text]);
82
82
  }
83
83
  meta ??= {};
84
- return { cwd: meta.cwd ?? null, sid: String(meta.id || basename(path).split('.')[0]).slice(0, 8), turns };
84
+ // 会话 ID 存全:Codex 用 UUIDv7,前 8 位只是时间,一分钟内开的两场会被并成一场(0.2.x 只存前 8 位)。
85
+ const named = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/.exec(basename(path));
86
+ return { cwd: meta.cwd ?? null, sid: String(meta.id || (named ? named[0] : basename(path).split('.')[0])), turns };
85
87
  }
86
88
 
87
89
  /**
@@ -101,7 +103,7 @@ export function build(entries, options = {}, paths) {
101
103
  if (index && exists(index)) {
102
104
  for (const line of jsonLines(readFileSync(index, 'utf8'))) {
103
105
  const e = parseJson(line);
104
- if (isObject(e) && typeof e.id === 'string' && tidy(e.thread_name)) titles[e.id.slice(0, 8)] = tidy(e.thread_name);
106
+ if (isObject(e) && typeof e.id === 'string' && tidy(e.thread_name)) titles[e.id] = tidy(e.thread_name);
105
107
  }
106
108
  }
107
109
  return { turns: all, titles, count: all.length };
@@ -115,4 +117,4 @@ export function rawFiles(paths, key) {
115
117
  return files(paths).filter(p => basename(p).includes(key));
116
118
  }
117
119
 
118
- export default { id: 'codex', folder: 'Codex', name: 'Codex', version: 1, locate, files, parse, build, render, rawFiles };
120
+ export default { id: 'codex', folder: 'Codex', name: 'Codex', version: 2, locate, files, parse, build, render, rawFiles };
@@ -1,11 +1,12 @@
1
1
  // OpenCode:会话存在 SQLite 库 ~/.local/share/opencode/opencode.db(session/message/part 三表),只读打开。
2
- // 标题行沿用 OpenCode 自带的会话标题:「# 标题(ses_xxxx)」。
2
+ // 标题行「# ses_ID · 标题」,标题取 OpenCode 自带的。0.2.x 写成「# 标题(ses_ 加 4 位)」:标题一改就成了另一场,
3
+ // 前 4 位又只是时间,同题的会话会并到一起;旧日志照常检索,整理时认作同一会话(markdown.js stillPresent)。
3
4
  import { join } from 'node:path';
4
5
  import { statSync } from 'node:fs';
5
6
  import { DatabaseSync } from 'node:sqlite';
6
7
  import { home, expand, isFile } from '../files.js';
7
8
  import { parseJson, strip, isObject } from '../py.js';
8
- import { demote } from '../markdown.js';
9
+ import { demote, tidy } from '../markdown.js';
9
10
  import { excluded, str } from './common.js';
10
11
 
11
12
  const dataHome = env => env.XDG_DATA_HOME ? expand(env.XDG_DATA_HOME) : join(home(), '.local/share');
@@ -71,13 +72,15 @@ export function parse(path) {
71
72
  }
72
73
 
73
74
  export function build(entries, options = {}) {
74
- const all = [];
75
+ const all = [], titles = {};
75
76
  for (const { data } of entries) for (const s of data.sessions) {
76
77
  if (TEMP.some(p => s.directory.includes(p)) || excluded(s.directory, options.excludeCwds)) continue;
77
- const key = `${strip(s.title) || s.sid.slice(0, 8)}(${s.sid.slice(0, 8)})`;
78
- for (const [ms, role, text] of s.turns) all.push([ms, role, text, key]);
78
+ // 「New session - 时间」是生成标题前的占位,不当标题。
79
+ const title = /^New session\b/.test(strip(s.title)) ? '' : tidy(s.title);
80
+ if (title) titles[s.sid] = title;
81
+ for (const [ms, role, text] of s.turns) all.push([ms, role, text, s.sid]);
79
82
  }
80
- return { turns: all, titles: {}, count: all.length };
83
+ return { turns: all, titles, count: all.length };
81
84
  }
82
85
 
83
86
  const core = text => text.split('\n').map(l => strip(l)).filter(s => s && !s.startsWith('MEDIA:') && !s.startsWith('[🖼️') && !s.startsWith('[📎')).join('\n');
package/lib/store.js CHANGED
@@ -7,15 +7,15 @@ import { DatabaseSync } from 'node:sqlite';
7
7
  import { layout } from './config.js';
8
8
  import { entries, isDir } from './files.js';
9
9
  import { sortPaths } from './py.js';
10
- import { SEP } from './markdown.js';
10
+ import { SEP, fenceStep } from './markdown.js';
11
11
  import { norm } from './normalize.js';
12
12
  import claude from './sources/claude.js';
13
- import { roleTools, clip, sessionTitle, toDay } from './text.js';
13
+ import { roleTools, clip, sessionTitle, toDay, skipChars } from './text.js';
14
14
 
15
- export const SCHEMA = 1;
15
+ // 2:代码围栏不再认缩进超过 3 格的行(0.3.0),旧索引须按新规则重新切分。
16
+ export const SCHEMA = 2;
16
17
  export const CHUNK = 1200, OVERLAP = 160;
17
18
  const HEADER = /^## (\d\d:\d\d)\s+(.+)$/;
18
- const FENCE = /^\s*(`{3,}|~{3,})/;
19
19
  const META = /<!--[\s\S]*?-->/g;
20
20
  const FRONT = /^---\n([\s\S]*?)\n---\n/;
21
21
 
@@ -54,14 +54,8 @@ export function* turns(text, heads = null) {
54
54
  let session = '', turn = 0, role = '', clock = '', start = null, fence = null;
55
55
  for (let i = 0; i < lines.length; i++) {
56
56
  const line = lines[i];
57
- const fm = FENCE.exec(line);
58
- if (fm) {
59
- const token = fm[1];
60
- if (fence === null) fence = token;
61
- else if (token[0] === fence[0] && token.length >= fence.length) fence = null;
62
- continue;
63
- }
64
- if (fence !== null) continue;
57
+ const next = fenceStep(fence, line);
58
+ if (fence !== null || next !== null) { fence = next; continue; }
65
59
  const header = HEADER.exec(line);
66
60
  if (line.startsWith('# ') || header) {
67
61
  if (start !== null) yield [session, turn, role, clock, start + 1, lines.slice(start, i)];
@@ -109,67 +103,81 @@ function logFiles(home) {
109
103
 
110
104
  const sig = path => { const s = statSync(path, { bigint: true }); return `${s.mtimeNs}:${s.size}`; };
111
105
 
112
- /** 增量更新:日志文件 mtime/大小变了才重新分块,并刷新涉及的会话目录。 */
106
+ /**
107
+ * 增量更新:日志文件 mtime/大小变了才重新分块,并刷新涉及的会话目录。
108
+ * 先只读地核对一遍,什么都没变就不开写事务:search → outline → get 连着查时不必次次抢写锁。
109
+ */
113
110
  export function updateIndex(home, config, db = null) {
114
111
  const own = !db;
115
112
  db ??= connect(home);
116
113
  const tools = roleTools(config.userLabel);
117
114
  try {
118
115
  const paths = logFiles(home);
119
- const old = new Map(db.prepare('SELECT path, signature FROM files').all().map(r => [r.path, r.signature]));
120
- const touched = new Map();
121
- let changed = 0;
122
- const touch = (agent, session) => touched.set(`${agent}\u0000${session}`, [agent, session]);
123
- const q = {
124
- sessionsOf: db.prepare('SELECT DISTINCT agent, session FROM chunks WHERE path=?'),
125
- ftsDelete: db.prepare("INSERT INTO terms(terms, rowid, normalized) SELECT 'delete', rid, normalized FROM chunks WHERE path=?"),
126
- chunksDelete: db.prepare('DELETE FROM chunks WHERE path=?'),
127
- headsDelete: db.prepare('DELETE FROM heads WHERE path=?'),
128
- filesDelete: db.prepare('DELETE FROM files WHERE path=?'),
129
- chunkInsert: db.prepare('INSERT INTO chunks(id,path,agent,day,session,turn,part,role,clock,start,end,body,normalized) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)'),
130
- ftsInsert: db.prepare('INSERT INTO terms(rowid, normalized) VALUES(?,?)'),
131
- headInsert: db.prepare('INSERT OR REPLACE INTO heads VALUES(?,?,?,?)'),
132
- fileInsert: db.prepare('INSERT OR REPLACE INTO files VALUES(?,?)'),
133
- };
134
- const drop = rel => {
135
- for (const r of q.sessionsOf.all(rel)) touch(r.agent, r.session);
136
- q.ftsDelete.run(rel); q.chunksDelete.run(rel); q.headsDelete.run(rel);
116
+ const plan = () => {
117
+ const old = new Map(db.prepare('SELECT path, signature FROM files').all().map(r => [r.path, r.signature]));
118
+ const removed = [...old.keys()].filter(rel => !paths.has(rel));
119
+ const changed = [];
120
+ for (const [rel, path] of paths) { const signature = sig(path); if (old.get(rel) !== signature) changed.push([rel, path, signature]); }
121
+ return { removed, changed, memories: memoryPlan(db, config) };
137
122
  };
138
- db.exec('BEGIN IMMEDIATE');
139
- try {
140
- for (const rel of old.keys()) if (!paths.has(rel)) { drop(rel); q.filesDelete.run(rel); changed++; }
141
- for (const [rel, path] of paths) {
142
- const signature = sig(path);
143
- if (old.get(rel) === signature) continue;
144
- drop(rel);
145
- const agent = basename(dirname(path)), day = basename(path).slice(0, 10);
146
- const heads = new Map();
147
- for (const [session, turn, role, clock, base, lines] of turns(readFileSync(path, 'utf8'), heads)) {
148
- touch(agent, session);
149
- for (const [part, start, end, body] of pieces(lines, base)) {
150
- const key = createHash('sha256').update(`${rel}:${session}:${turn}:${part}:${body}`).digest('hex').slice(0, 20);
151
- const normalized = norm(mask(body));
152
- const { lastInsertRowid } = q.chunkInsert.run(key, rel, agent, day, session, turn, part, role, clock, start, end, body, normalized);
153
- q.ftsInsert.run(lastInsertRowid, normalized);
154
- }
155
- }
156
- for (const [k, v] of heads) q.headInsert.run(rel, agent, k, v);
157
- q.fileInsert.run(rel, signature);
158
- changed++;
123
+ let work = plan(), updated = 0;
124
+ if (work.removed.length || work.changed.length || work.memories.stale) {
125
+ db.exec('BEGIN IMMEDIATE');
126
+ try {
127
+ work = plan(); // 拿到写锁后重新核对:等锁期间别的进程可能已经更新过
128
+ updated = applyPlan(db, work, tools);
129
+ db.exec('COMMIT');
130
+ } catch (error) {
131
+ db.exec('ROLLBACK');
132
+ throw error;
159
133
  }
160
- refreshSessions(db, [...touched.values()], tools);
161
- updateMemories(db, config);
162
- db.exec('COMMIT');
163
- } catch (error) {
164
- db.exec('ROLLBACK');
165
- throw error;
166
134
  }
167
135
  const count = sql => db.prepare(sql).get().n;
168
- return { files: paths.size, updated: changed, chunks: count('SELECT count(*) n FROM chunks'),
136
+ return { files: paths.size, updated, chunks: count('SELECT count(*) n FROM chunks'),
169
137
  sessions: count('SELECT count(*) n FROM sessions'), memories: count('SELECT count(*) n FROM memories') };
170
138
  } finally { if (own) db.close(); }
171
139
  }
172
140
 
141
+ function applyPlan(db, { removed, changed, memories }, tools) {
142
+ const touched = new Map();
143
+ const touch = (agent, session) => touched.set(`${agent}\u0000${session}`, [agent, session]);
144
+ const q = {
145
+ sessionsOf: db.prepare('SELECT DISTINCT agent, session FROM chunks WHERE path=?'),
146
+ ftsDelete: db.prepare("INSERT INTO terms(terms, rowid, normalized) SELECT 'delete', rid, normalized FROM chunks WHERE path=?"),
147
+ chunksDelete: db.prepare('DELETE FROM chunks WHERE path=?'),
148
+ headsDelete: db.prepare('DELETE FROM heads WHERE path=?'),
149
+ filesDelete: db.prepare('DELETE FROM files WHERE path=?'),
150
+ chunkInsert: db.prepare('INSERT INTO chunks(id,path,agent,day,session,turn,part,role,clock,start,end,body,normalized) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)'),
151
+ ftsInsert: db.prepare('INSERT INTO terms(rowid, normalized) VALUES(?,?)'),
152
+ headInsert: db.prepare('INSERT OR REPLACE INTO heads VALUES(?,?,?,?)'),
153
+ fileInsert: db.prepare('INSERT OR REPLACE INTO files VALUES(?,?)'),
154
+ };
155
+ const drop = rel => {
156
+ for (const r of q.sessionsOf.all(rel)) touch(r.agent, r.session);
157
+ q.ftsDelete.run(rel); q.chunksDelete.run(rel); q.headsDelete.run(rel);
158
+ };
159
+ for (const rel of removed) { drop(rel); q.filesDelete.run(rel); }
160
+ for (const [rel, path, signature] of changed) {
161
+ drop(rel);
162
+ const agent = basename(dirname(path)), day = basename(path).slice(0, 10);
163
+ const heads = new Map();
164
+ for (const [session, turn, role, clock, base, lines] of turns(readFileSync(path, 'utf8'), heads)) {
165
+ touch(agent, session);
166
+ for (const [part, start, end, body] of pieces(lines, base)) {
167
+ const key = createHash('sha256').update(`${rel}:${session}:${turn}:${part}:${body}`).digest('hex').slice(0, 20);
168
+ const normalized = norm(mask(body));
169
+ const { lastInsertRowid } = q.chunkInsert.run(key, rel, agent, day, session, turn, part, role, clock, start, end, body, normalized);
170
+ q.ftsInsert.run(lastInsertRowid, normalized);
171
+ }
172
+ }
173
+ for (const [k, v] of heads) q.headInsert.run(rel, agent, k, v);
174
+ q.fileInsert.run(rel, signature);
175
+ }
176
+ refreshSessions(db, [...touched.values()], tools);
177
+ applyMemories(db, memories);
178
+ return removed.length + changed.length;
179
+ }
180
+
173
181
  function refreshSessions(db, keys, tools) {
174
182
  const del = db.prepare('DELETE FROM sessions WHERE agent=? AND session=?');
175
183
  const rowsOf = db.prepare('SELECT day, clock, turn, part, role, body FROM chunks WHERE agent=? AND session=? ORDER BY day, turn, part');
@@ -181,7 +189,7 @@ function refreshSessions(db, keys, tools) {
181
189
  if (!rows.length) continue;
182
190
  const spoken = new Set(rows.map(r => `${r.day}|${r.turn}`));
183
191
  const asked = new Set(rows.filter(r => tools.isUser(r.role)).map(r => `${r.day}|${r.turn}`));
184
- const chars = rows.reduce((sum, r) => sum + r.body.length - (r.part ? OVERLAP : 0), 0);
192
+ const chars = rows.reduce((sum, r) => sum + (r.part ? skipChars(r.body, OVERLAP) : r.body).length, 0);
185
193
  const native = nativeOf.get(agent, session);
186
194
  const title = native ? clip(native.title, 50) : sessionTitle(session, rows.filter(r => tools.isUser(r.role) && r.part === 0).map(r => r.body));
187
195
  insert.run(agent, session, rows[0].day, rows[rows.length - 1].day, rows[0].clock, rows[rows.length - 1].clock,
@@ -189,28 +197,37 @@ function refreshSessions(db, keys, tools) {
189
197
  }
190
198
  }
191
199
 
192
- /** Claude Code 记忆文件不切块:题目=名称+描述,正文另存归一化文本,只用于判断是否列出。 */
193
- function updateMemories(db, config) {
194
- const files = new Map();
200
+ /** Claude Code 记忆文件不切块:题目=名称+描述,正文另存归一化文本,只用于判断是否列出。这里只核对哪些变了。 */
201
+ function memoryPlan(db, config) {
202
+ const files = [];
195
203
  if (config.memoryHints) {
196
204
  const root = claude.locate().memory;
197
205
  for (const d of entries(root)) {
198
206
  const dir = join(root, d.name, 'memory');
199
207
  if (!isDir(dir)) continue;
200
- for (const f of entries(dir)) if (f.name.endsWith('.md') && f.name !== 'MEMORY.md') files.set(join(dir, f.name), null);
208
+ for (const f of entries(dir)) if (f.name.endsWith('.md') && f.name !== 'MEMORY.md') files.push(join(dir, f.name));
201
209
  }
202
210
  }
203
- const sorted = sortPaths([...files.keys()]);
211
+ const sorted = sortPaths(files), keep = new Set(sorted);
204
212
  const old = new Map(db.prepare('SELECT path, signature FROM memories').all().map(r => [r.path, r.signature]));
205
- const del = db.prepare('DELETE FROM memories WHERE path=?');
206
- for (const gone of old.keys()) if (!files.has(gone)) del.run(gone);
207
- const insert = db.prepare('INSERT OR REPLACE INTO memories VALUES(?,?,?,?,?,?)');
213
+ const gone = [...old.keys()].filter(path => !keep.has(path));
214
+ const fresh = [];
208
215
  for (const path of sorted) {
209
216
  let stat;
210
217
  try { stat = statSync(path, { bigint: true }); } catch { continue; }
211
218
  const signature = `${stat.mtimeNs}:${stat.size}`;
212
- if (old.get(path) === signature) continue;
213
- const text = readFileSync(path, 'utf8');
219
+ if (old.get(path) !== signature) fresh.push([path, signature, stat]);
220
+ }
221
+ return { gone, fresh, stale: gone.length + fresh.length > 0 };
222
+ }
223
+
224
+ function applyMemories(db, { gone, fresh }) {
225
+ const del = db.prepare('DELETE FROM memories WHERE path=?');
226
+ for (const path of gone) del.run(path);
227
+ const insert = db.prepare('INSERT OR REPLACE INTO memories VALUES(?,?,?,?,?,?)');
228
+ for (const [path, signature, stat] of fresh) {
229
+ let text;
230
+ try { text = readFileSync(path, 'utf8'); } catch { continue; }
214
231
  const m = FRONT.exec(text);
215
232
  const front = m ? m[1] : '', body = m ? text.slice(m[0].length) : text;
216
233
  const field = name => {
package/lib/text.js CHANGED
@@ -9,9 +9,20 @@ const META = /<!--[\s\S]*?-->/g;
9
9
  const mask = body => body.replace(META, m => ' '.repeat(m.length));
10
10
  const PLACEHOLDER = /^\s*(\[(?:🖼|📎)[^\]]*\]|#{1,6} Files mentioned by the user:?|#{1,6} .+\.(?:png|jpe?g|gif|webp|heic|pdf)\s*:.*|#{1,6} My request for Codex:?)\s*$/i;
11
11
 
12
+ // 扩展区汉字(如𠮷)与 emoji 在 JS 字符串里占两个单位;按位置截取时不能从中间切开。
13
+ const LOW = c => c >= 0xdc00 && c <= 0xdfff;
14
+ /** 截断位置若落在一个字的两半之间,退回到这个字之前。 */
15
+ export const safeAt = (s, i) => (i > 0 && i < s.length && LOW(s.charCodeAt(i)) ? i - 1 : i);
16
+ /** 去掉开头 n 个字(按字数,不按 JS 长度);分块重叠是按字数算的,拼回时要用它。 */
17
+ export function skipChars(s, n) {
18
+ let i = 0;
19
+ for (let k = 0; k < n && i < s.length; k++) i += s.codePointAt(i) > 0xffff ? 2 : 1;
20
+ return s.slice(i);
21
+ }
22
+
12
23
  export function clip(text, width) {
13
24
  const t = String(text).replace(/\s+/g, ' ').trim();
14
- return t.length <= width ? t : t.slice(0, Math.max(1, width - 1)) + '…';
25
+ return t.length <= width ? t : t.slice(0, safeAt(t, Math.max(1, width - 1))) + '…';
15
26
  }
16
27
 
17
28
  export function plain(text) {
@@ -31,11 +42,32 @@ export function roleTools(userLabel = '用户') {
31
42
 
32
43
  export const speaker = role => String(role).replace(/^[^\p{L}\p{N}_]+/u, '').trim();
33
44
 
45
+ const UUID = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/g;
46
+
47
+ /**
48
+ * 会话的短 ID,给 outline/search --session 用,前缀或片段都能找回。
49
+ * UUID 取前 13 位(UUIDv7 的前 8 位只是时间,一分钟内开的会话会相同);
50
+ * 微信等 IM 会话形如 weixin-<用户>-im-wechat-<UUID>,前缀全都一样,取末尾的 UUID;
51
+ * OpenCode 取 ses_ 后 12 位,旧日志「标题(ses_xxxx)」照旧取括号里的。
52
+ */
34
53
  export function shortSid(session) {
35
54
  const m = /((ses_[0-9A-Za-z]+))$/.exec(session);
36
55
  if (m) return m[1];
37
56
  const s = session.startsWith('session-') ? session.slice(8) : session;
38
- return /^[0-9a-f]{8}-[0-9a-f]{4}-/.test(s) ? s.slice(0, 13) : s.slice(0, 8);
57
+ if (s.startsWith('ses_')) return s.slice(0, 16);
58
+ if (/^[0-9a-f]{8}-[0-9a-f]{4}-/.test(s)) return s.slice(0, 13);
59
+ const ids = s.match(UUID);
60
+ return ids ? ids[ids.length - 1].slice(0, 13) : s.slice(0, 8);
61
+ }
62
+
63
+ /** 展示用的会话 ID:短 ID 在同一 agent 里撞了(旧日志只存了几位)就给完整 ID,保证照着抄能找回这一场。 */
64
+ export function sessionLabeler(db) {
65
+ const seen = new Map();
66
+ for (const r of db.prepare('SELECT agent, session FROM sessions').all()) {
67
+ const k = `${r.agent}\u0000${shortSid(r.session)}`;
68
+ seen.set(k, (seen.get(k) || 0) + 1);
69
+ }
70
+ return (agent, session) => { const short = shortSid(session); return seen.get(`${agent}\u0000${short}`) > 1 ? session : short; };
39
71
  }
40
72
 
41
73
  export const span = (first, last) => first === last ? first : `${first}—${last.slice(5)}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-session-history",
3
- "version": "0.2.6",
3
+ "version": "0.3.0",
4
4
  "description": "把各个 agent(Claude Code、Codex 等)的对话整理成可检索的原文日志,以一个共用技能提供给所有 agent;保留原话,不做摘要,不用向量,不调用模型。",
5
5
  "author": "youxia",
6
6
  "repository": {