dsh-go-codegraph 0.1.0 → 0.1.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/README.md CHANGED
@@ -54,6 +54,17 @@ dsh plugin --profile web add "$DSH_HOME/dsh-go/plugins/dsh-go-codegraph"
54
54
  { "exclude": ["deepinfer/"] }
55
55
  ```
56
56
 
57
+ ## 🧾 同步日志
58
+
59
+ 每次同步都会往 `$DSH_HOME/cache/dsh-go-codegraph/sync.log` 追加一行 JSON:
60
+
61
+ ```json
62
+ {"at":"2026-12-05T08:00:00.000Z","trigger":"panel:sync","cwd":"/root/workspace","ms":1832,"ok":true}
63
+ ```
64
+
65
+ `trigger` 只有四种:`panel:sync`(面板点同步)、`tool:explore|node|impact|status`(工具调用前的 TTL 补同步)、`init`(首次建索引)。
66
+ 所以「索引时间自己走了」时,看这个文件就能知道是谁触发的。面板上的「同步日志」按钮会复制它的路径。
67
+
57
68
  ## ⚙️ 配置
58
69
 
59
70
  | 🔧 键 | 默认 | 说明 |
package/lib/cg.js CHANGED
@@ -5,7 +5,8 @@
5
5
  * 输出裁剪到工具能接受的长度。所有解析函数都是纯函数,方便单测。
6
6
  */
7
7
  import { execFile } from 'node:child_process'
8
- import { access, appendFile, readFile } from 'node:fs/promises'
8
+ import { access, appendFile, mkdir, readFile, rename, stat } from 'node:fs/promises'
9
+ import os from 'node:os'
9
10
  import path from 'node:path'
10
11
 
11
12
  export const DEFAULT_COMMAND = 'codegraph'
@@ -154,3 +155,22 @@ export async function excludeFromGit(cwd) {
154
155
  return false
155
156
  }
156
157
  }
158
+
159
+
160
+ /** 同步日志:记下每次同步是谁触发的,用于解释「索引时间自己走了」。 */
161
+ export function syncLogPath() {
162
+ const home = process.env.DSH_HOME || path.join(os.homedir(), '.dsh')
163
+ return path.join(home, 'cache', 'dsh-go-codegraph', 'sync.log')
164
+ }
165
+
166
+ export async function appendSyncLog(entry) {
167
+ const file = syncLogPath()
168
+ try {
169
+ await mkdir(path.dirname(file), { recursive: true })
170
+ const info = await stat(file).catch(() => null)
171
+ if (info && info.size > 512 * 1024) await rename(file, `${file}.1`)
172
+ await appendFile(file, `${JSON.stringify({ at: new Date().toISOString(), ...entry })}\n`, 'utf8')
173
+ } catch {
174
+ /* 日志写失败不影响主流程 */
175
+ }
176
+ }
package/lib/index.js CHANGED
@@ -9,6 +9,8 @@
9
9
  * 所有上游失败都变成工具结果里的一句话,不抛给桥接,也不让模型看到堆栈。
10
10
  */
11
11
  import {
12
+ appendSyncLog,
13
+ syncLogPath,
12
14
  DEFAULT_COMMAND,
13
15
  STATUS_TIMEOUT_MS,
14
16
  SYNC_TTL_MS,
@@ -90,6 +92,7 @@ export function apply(ctx, config = {}) {
90
92
 
91
93
  async function ensureIndex(cwd, exec) {
92
94
  if (await hasIndex(cwd)) return { ok: true, created: false }
95
+ const initStarted = Date.now()
93
96
  if (!autoIndex) return { ok: false, created: false, reason: `项目 ${cwd} 还没有索引,且插件关闭了自动建索引` }
94
97
  const run = await runCli(command, ['init', cwd, '-y'], { cwd, timeoutMs: INIT_TIMEOUT_MS, signal: exec?.signal })
95
98
  if (!run.ok || run.timedOut) {
@@ -111,16 +114,24 @@ export function apply(ctx, config = {}) {
111
114
  }
112
115
  await excludeFromGit(cwd)
113
116
  lastSync.set(cwd, Date.now())
117
+ void appendSyncLog({ trigger: 'init', cwd, ms: Date.now() - initStarted, ok: true, created: true })
114
118
  return { ok: true, created: true }
115
119
  }
116
120
 
117
- async function syncIfStale(cwd, exec) {
121
+ async function syncIfStale(cwd, exec, trigger = 'tool') {
118
122
  const at = lastSync.get(cwd) ?? 0
119
123
  if (Date.now() - at < SYNC_TTL_MS) return { ok: true, skipped: true }
124
+ const started = Date.now()
120
125
  const run = await runCli(command, ['sync', cwd, '--quiet'], { cwd, timeoutMs: 20_000, signal: exec?.signal })
121
126
  lastSync.set(cwd, Date.now())
122
- if (run.ok) return { ok: true, skipped: false }
123
- return { ok: false, skipped: false, reason: run.timedOut ? '同步超时' : firstLine(run.stderr) || '同步失败' }
127
+ const ms = Date.now() - started
128
+ if (run.ok) {
129
+ void appendSyncLog({ trigger, cwd, ms, ok: true })
130
+ return { ok: true, skipped: false }
131
+ }
132
+ const reason = run.timedOut ? '同步超时' : firstLine(run.stderr) || '同步失败'
133
+ void appendSyncLog({ trigger, cwd, ms, ok: false, reason })
134
+ return { ok: false, skipped: false, reason }
124
135
  }
125
136
 
126
137
  /** 统一的前置:定位目录 → 必要时建索引 → 必要时同步。 */
@@ -130,7 +141,7 @@ export function apply(ctx, config = {}) {
130
141
  if (options.skipIndex && explicit) return { cwd, sync: null }
131
142
  const index = await ensureIndex(cwd, exec)
132
143
  if (!index.ok) return { cwd, error: index.reason }
133
- const sync = await syncIfStale(cwd, exec)
144
+ const sync = await syncIfStale(cwd, exec, options.trigger || 'tool')
134
145
  return { cwd, indexCreated: index.created, sync }
135
146
  }
136
147
 
@@ -159,7 +170,7 @@ export function apply(ctx, config = {}) {
159
170
  async (args, exec) => {
160
171
  const query = String(args?.query ?? '').trim()
161
172
  if (!query) return { text: '请给一个查询,例如 "面板怎么刷新数据"。' }
162
- const prepared = await prepare(exec, args)
173
+ const prepared = await prepare(exec, args, { trigger: 'tool:explore' })
163
174
  if (prepared.error) return { text: prepared.error }
164
175
  const maxFiles = clampInt(args?.maxFiles, 1, 20, 6)
165
176
  const run = await runCli(
@@ -194,7 +205,7 @@ export function apply(ctx, config = {}) {
194
205
  async (args, exec) => {
195
206
  const symbol = String(args?.symbol ?? '').trim()
196
207
  if (!symbol) return { text: '请给一个符号名。' }
197
- const prepared = await prepare(exec, args)
208
+ const prepared = await prepare(exec, args, { trigger: 'tool:node' })
198
209
  if (prepared.error) return { text: prepared.error }
199
210
  const run = await runCli(command, ['node', symbol, '-p', prepared.cwd], {
200
211
  cwd: prepared.cwd,
@@ -229,7 +240,7 @@ export function apply(ctx, config = {}) {
229
240
  async (args, exec) => {
230
241
  const symbol = String(args?.symbol ?? '').trim()
231
242
  if (!symbol) return { text: '请给一个符号名。' }
232
- const prepared = await prepare(exec, args)
243
+ const prepared = await prepare(exec, args, { trigger: 'tool:impact' })
233
244
  if (prepared.error) return { text: prepared.error }
234
245
  const depth = clampInt(args?.depth, 1, 5, 2)
235
246
  const run = await runCli(
@@ -264,7 +275,7 @@ export function apply(ctx, config = {}) {
264
275
  },
265
276
  INIT_TIMEOUT_MS + STATUS_TIMEOUT_MS,
266
277
  async (args, exec) => {
267
- const prepared = await prepare(exec, args)
278
+ const prepared = await prepare(exec, args, { trigger: 'tool:status' })
268
279
  if (prepared.error) return { text: prepared.error }
269
280
  const run = await runCli(command, ['status', prepared.cwd, '--json'], {
270
281
  cwd: prepared.cwd,
@@ -451,13 +462,17 @@ export function apply(ctx, config = {}) {
451
462
  return
452
463
  }
453
464
  lastSync.delete(cwd)
454
- await syncIfStale(cwd, { signal: undefined })
465
+ await syncIfStale(cwd, { signal: undefined }, 'panel:sync')
455
466
  await loadStatus(scope, st)
456
467
  refreshPanel(scope)
457
468
  })()
458
469
  return { toast: '正在同步…' }
459
470
  },
460
471
 
472
+ async copyLog() {
473
+ return { copy: syncLogPath(), toast: '同步日志路径已复制' }
474
+ },
475
+
461
476
  async copyStatus(scope) {
462
477
  const st = stateOf(scope)
463
478
  await loadStatus(scope, st)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-go-codegraph",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
5
  "main": "lib/index.js",
6
6
  "description": "🗺️ dsh 插件:给 agent 装一张本地代码图谱(explore / node / impact),手机上一条状态条加一块搜索面板",