dsh-project-flow 0.10.1 → 0.10.3

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/CHANGELOG.md CHANGED
@@ -14,6 +14,54 @@ dsh-project-flow 的版本变更。版本号遵循语义化版本。
14
14
 
15
15
  ---
16
16
 
17
+ ## [0.10.3] - 2026-12-05
18
+
19
+ ### 带 BOM 的 state.json 让项目从面板上消失
20
+
21
+ 真机上 9 个项目只显示 8 个。少的是 `opc-deck-v4`:它的 `state.json` 开头带 `EF BB BF`(UTF-8 BOM),`JSON.parse` 见到直接抛,`readState` 返回 `undefined`,面板那边 `if (state === undefined) continue` 就把它跳过了——一声不响。
22
+
23
+ Windows 上这玩意儿到处都是:记事本另存、某些 PowerShell 写法、从别的机器拷文件,都会带。以前只有 `splitFrontMatter`(Markdown 那条路)处理过 BOM,JSON 这条路没管。
24
+
25
+ 现在 `shared/util.mjs` 加了 `stripBom`,用在所有 JSON 读取点:`readState`、`writeState`、`readContract`、`readRememberedContext`、`readOverride`、`readLedger`、锁文件、`readEvidence`、以及从会话存储里取 cwd 那一处。
26
+
27
+ ### writeState 里藏着同一个坑
28
+
29
+ `writeState` 读旧 state 失败时 `previous = {}`,然后 `{ ...{}, ...patch }` 写回去——`stage`、`requirements`、`attempts`、`cost` 全没了,只剩这一次的 patch。带 BOM 的 state.json 正好会走到这条路上。
30
+
31
+ 这和 0.10.1 修的 `writeOverride` 是同一个病。那边丢的是配置,这边丢的是运行时状态,更难发现——文件还在、还能解析,只是内容变成了刚写的那几个键。
32
+
33
+ ### 测试
34
+
35
+ `tests/panel-report.test.mjs` 从 9 条加到 10 条:手工拼一个带 BOM 的 `state.json` 丢给 `panelSnapshot`,断言那个项目还在、阶段读得出来。
36
+
37
+ ## [0.10.2] - 2026-12-05
38
+
39
+ 修「工作模式开起来了,状态栏却什么都没变」。三条独立的原因,叠在一起。
40
+
41
+ ### 面板数据写到了错的地方
42
+
43
+ `writePanelFile` 落盘用的是 `join(cwd, '.project-flow', 'panel.json')`——会话在哪个目录,面板数据就写哪儿。而状态栏读的是 `watchRootOf(cwd)` 算出来的目录。两个函数各算一套,于是同一个工作流长出好几份 `panel.json`:外层壳目录一份、项目子目录一份、真正的根目录一份,状态栏读其中一份,看着就是「项目在跑、状态栏没动」。
44
+
45
+ 现在写的时候调的就是 `watchRootOf`——**读哪儿写哪儿,用同一个解析函数**。这一点不能换成 `flowRootTargetOf`:没配绝对 `flowRoot` 时它会算出 `cwd/工作流` 这个根本不存在的路径,而 `watchRootOf` 会正确回到 `cwd` 或找到祖先里那层「工作流」。
46
+
47
+ 真机上清出来的现场:`D:\` 底下一共六个 `.project-flow`,`D:\新建文件夹 (4)` 外层和它下面的 `工作流` 各一份,另外四个散在 `D:\dsh-project-flow`、`D:\工作流`、`D:\C盘去重回收区` 和插件源码目录里。
48
+
49
+ ### 工具路径根本不写面板
50
+
51
+ `writePanelFile` 只被 `/flow` 和 `/flow-set` 两条斜杠命令调用过。`project_run` / `project_resume` 走的是另一条完全独立的代码路径,一次都不写。于是「用斜杠命令开」状态栏会动,「让模型直接调工具开」就不会动——同一个能力的两个入口,表现差这么多。
52
+
53
+ 现在两个工具收尾时都刷一次,不管结果成没成。
54
+
55
+ ### 两套测试在读用户的真实配置
56
+
57
+ `feedback.test.mjs` 和 `stop-resume.test.mjs` 直接 `import` 插件,没隔离 `DSH_HOME`。用户一把 `flowGate` 调成 `strict`(只在自己的工作流目录里开工),测试目录立刻不在允许范围内,16 条断言挂 14 条——看着像代码坏了,其实是配置在起作用。
58
+
59
+ 现在两套都先把 `DSH_HOME` 指到临时目录。同类问题之前 `apply-wiring` 也踩过一次。
60
+
61
+ ### 测试
62
+
63
+ `tests/watch-root.test.mjs` 从 9 条加到 11 条,锁两件事:在用项目子目录里干活时,面板写进工作流根而不是子目录;在别的栏(默认工作区)调用时,一个字节都不写。
64
+
17
65
  ## [0.10.1] - 2026-12-05
18
66
 
19
67
  修 0.10.0 留下的几处真故障。都是「看起来在工作、实际没生效」那一类,所以每条都补了能单独跑的测试,不再只靠 30 套里的端到端用例兜。
@@ -5,7 +5,7 @@ import { join, dirname } from 'node:path'
5
5
  import { homedir } from 'node:os'
6
6
  import { readFileSync } from 'node:fs'
7
7
  import { mkdir, readFile } from 'node:fs/promises'
8
- import { writeFileAtomic } from '../shared/util.mjs'
8
+ import { stripBom, writeFileAtomic } from '../shared/util.mjs'
9
9
  import { DEFAULT_SNAPSHOT_EXCLUDES, FLOAT_SETTINGS } from '../shared/constants.mjs'
10
10
 
11
11
  const pick = (value, fallback) => (value === undefined || value === '' ? fallback : value)
@@ -104,7 +104,7 @@ const readOverride = () => {
104
104
  const merged = {}
105
105
  for (const file of overridePaths().slice().reverse()) {
106
106
  try {
107
- const parsed = JSON.parse(readFileSync(file, 'utf8'))
107
+ const parsed = JSON.parse(stripBom(readFileSync(file, 'utf8')))
108
108
  if (parsed !== null && typeof parsed === 'object' && !Array.isArray(parsed)) {
109
109
  Object.assign(merged, parsed)
110
110
  }
package/evidence.mjs CHANGED
@@ -17,6 +17,7 @@ import { execFile } from 'node:child_process'
17
17
  import { writeFile, readFile, readdir, stat, rename, rm } from 'node:fs/promises'
18
18
  import { homedir } from 'node:os'
19
19
  import { join } from 'node:path'
20
+ import { stripBom } from './shared/util.mjs'
20
21
 
21
22
  export const EVIDENCE_FILE = 'Completion_Evidence.json'
22
23
  export const EVIDENCE_SCHEMA_VERSION = 1
@@ -97,7 +98,7 @@ async function detectIn(cwd) {
97
98
  // "echo \"Error: no test specified\"" 不算测试。
98
99
  if (await has('package.json')) {
99
100
  try {
100
- const pkg = JSON.parse(await readFile(join(cwd, 'package.json'), 'utf8'))
101
+ const pkg = JSON.parse(stripBom(await readFile(join(cwd, 'package.json'), 'utf8')))
101
102
  const script = pkg?.scripts?.test
102
103
  if (typeof script === 'string' && script.trim() !== '' && !/no test specified/i.test(script)) {
103
104
  return { command: 'npm test', why: 'package.json 里有 scripts.test' }
@@ -499,7 +500,7 @@ export async function writeEvidence(artifacts, evidence) {
499
500
  /** 读回来。补丁里给 project_status 之类的只读工具用。 */
500
501
  export async function readEvidence(artifacts) {
501
502
  try {
502
- return JSON.parse(await readFile(join(artifacts, EVIDENCE_FILE), 'utf8'))
503
+ return JSON.parse(stripBom(await readFile(join(artifacts, EVIDENCE_FILE), 'utf8')))
503
504
  } catch {
504
505
  return undefined
505
506
  }
package/index.js CHANGED
@@ -208,7 +208,7 @@ export {
208
208
  listSnapshots,
209
209
  gitAvailable,
210
210
  } from './snapshot/repo.mjs'
211
- export { previewImageParts, collectPreviewParts, panelSnapshot } from './workspace/panel.mjs'
211
+ export { previewImageParts, collectPreviewParts, panelSnapshot, writePanelFile } from './workspace/panel.mjs'
212
212
  export { steerInstruction } from './commands/steer.mjs'
213
213
 
214
214
  // config 里的 settingsOf 对外仍叫这个名字,语义没变。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-project-flow",
3
- "version": "0.10.1",
3
+ "version": "0.10.3",
4
4
  "private": false,
5
5
  "description": "Four-stage project workflow: requirement, plan, build, QA, with an intent gate, a rework loop, self-healing recovery, a watchdog that stops runaway runs, snapshot rollback with a retention cap, a single status light, and a machine-readable Completion_Evidence.json.",
6
6
  "type": "module",
@@ -3,7 +3,7 @@
3
3
  */
4
4
  import { join } from 'node:path'
5
5
  import { readFile, mkdir, rename, writeFile, rm } from 'node:fs/promises'
6
- import { isAlive, nowIso, stateDir, writeFileAtomic } from '../shared/util.mjs'
6
+ import { isAlive, nowIso, stateDir, stripBom, writeFileAtomic } from '../shared/util.mjs'
7
7
 
8
8
  export function lockFile(cwd, project) {
9
9
  return join(stateDir(cwd, project), 'run.lock')
@@ -11,7 +11,7 @@ export function lockFile(cwd, project) {
11
11
 
12
12
  export async function readLock(cwd, project) {
13
13
  try {
14
- const parsed = JSON.parse(await readFile(lockFile(cwd, project), 'utf8'))
14
+ const parsed = JSON.parse(stripBom(await readFile(lockFile(cwd, project), 'utf8')))
15
15
  return parsed !== null && typeof parsed === 'object' ? parsed : undefined
16
16
  } catch {
17
17
  return undefined
@@ -7,7 +7,7 @@ import { createHash } from 'node:crypto'
7
7
  import { BUILTIN_FLOW, loadFlow } from '../flow.schema.mjs'
8
8
  import { parseRequirementChecks, requirementFingerprint } from '../checks/requirement.mjs'
9
9
  import { ARTIFACT, CONTEXT_FILE, CONTRACT_FILE, CONTRACT_MAX_CHARS } from '../shared/constants.mjs'
10
- import { filled, nowIso, stateDir, writeFileAtomic } from '../shared/util.mjs'
10
+ import { filled, nowIso, stateDir, stripBom, writeFileAtomic } from '../shared/util.mjs'
11
11
 
12
12
  export async function writeState(cwd, project, patch) {
13
13
  const dir = stateDir(cwd, project)
@@ -15,7 +15,11 @@ export async function writeState(cwd, project, patch) {
15
15
  const file = join(dir, 'state.json')
16
16
  let previous = {}
17
17
  try {
18
- previous = JSON.parse(await readFile(file, 'utf8'))
18
+ // 必须 stripBom:带 BOM 的 state.json 会让 JSON.parse 抛,然后 previous 被当成
19
+ // {} ——这一轮的 patch 覆盖上去,stage / requirements / attempts / cost 全没了。
20
+ // 这跟 writeOverride 里那个「读失败静默降级成空对象」是同一个坑,只是这边
21
+ // 丢的是运行时状态,比配置更难发现。
22
+ previous = JSON.parse(stripBom(await readFile(file, 'utf8')))
19
23
  } catch {
20
24
  previous = {}
21
25
  }
@@ -26,7 +30,7 @@ export async function writeState(cwd, project, patch) {
26
30
 
27
31
  export async function readState(cwd, project) {
28
32
  try {
29
- return JSON.parse(await readFile(join(stateDir(cwd, project), 'state.json'), 'utf8'))
33
+ return JSON.parse(stripBom(await readFile(join(stateDir(cwd, project), 'state.json'), 'utf8')))
30
34
  } catch {
31
35
  return undefined
32
36
  }
@@ -71,7 +75,7 @@ export function contractSha(contract) {
71
75
 
72
76
  export async function readContract(cwd, project) {
73
77
  try {
74
- const parsed = JSON.parse(await readFile(join(stateDir(cwd, project), CONTRACT_FILE), 'utf8'))
78
+ const parsed = JSON.parse(stripBom(await readFile(join(stateDir(cwd, project), CONTRACT_FILE), 'utf8')))
75
79
  if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) return undefined
76
80
  return parsed
77
81
  } catch {
@@ -163,7 +167,7 @@ export function phaseReadKeys(phase) {
163
167
 
164
168
  export async function readRememberedContext(cwd) {
165
169
  try {
166
- const parsed = JSON.parse(await readFile(join(cwd, '.project-flow', CONTEXT_FILE), 'utf8'))
170
+ const parsed = JSON.parse(stripBom(await readFile(join(cwd, '.project-flow', CONTEXT_FILE), 'utf8')))
167
171
  if (parsed === null || typeof parsed !== 'object') return undefined
168
172
  return parsed
169
173
  } catch {
@@ -4,7 +4,7 @@
4
4
  import { join, isAbsolute, relative } from 'node:path'
5
5
  import { mkdirSync, appendFileSync, readFileSync } from 'node:fs'
6
6
  import { rm } from 'node:fs/promises'
7
- import { fileSha256, messageOf, nowIso, stateDir } from '../shared/util.mjs'
7
+ import { fileSha256, messageOf, nowIso, stateDir, stripBom } from '../shared/util.mjs'
8
8
  import { snapshotCommand } from '../snapshot/repo.mjs'
9
9
 
10
10
  export function ledgerDir(cwd, project) {
@@ -35,7 +35,7 @@ export function readLedger(cwd, project) {
35
35
  const path = ledgerPath(cwd, project)
36
36
  let text
37
37
  try {
38
- text = readFileSync(path, 'utf8')
38
+ text = stripBom(readFileSync(path, 'utf8'))
39
39
  } catch {
40
40
  return []
41
41
  }
@@ -10,7 +10,7 @@ import { isAbsolute } from 'node:path'
10
10
  import { pfState } from '../../state/registry.mjs'
11
11
  import { settingsOf } from '../../config/settings.mjs'
12
12
  import { stateDir } from '../../shared/util.mjs'
13
- import { statusLine } from '../../workspace/panel.mjs'
13
+ import { statusLine, writePanelFile } from '../../workspace/panel.mjs'
14
14
 
15
15
  export function create(ctx, rt) {
16
16
  const executeRun = createExecuteRun(ctx, rt)
@@ -127,7 +127,13 @@ const resumeTool = defineTool({
127
127
  return give(`没有可用的施工任务书(${ARTIFACT.plan} 缺失或格式不对),从实施段接不上。跑 project_doctor 检查产物,或重新 project_run。`)
128
128
  }
129
129
 
130
+ // 续跑收尾也刷一次面板:project_resume 和 /flow-resume 是同一件事的
131
+ // 两个入口,状态栏不该只认其中一个。
130
132
  return executeRun({ ...plan.options, parent, signal: exec.signal })
133
+ .then(result => {
134
+ void writePanelFile(cwd, settings).catch(() => undefined)
135
+ return result
136
+ })
131
137
  },
132
138
  })
133
139
 
@@ -12,7 +12,7 @@ import { pfState } from '../../state/registry.mjs'
12
12
  import { readState, writeRememberedContext } from '../../persistence/state.mjs'
13
13
  import { settingsOf } from '../../config/settings.mjs'
14
14
  import { stateDir } from '../../shared/util.mjs'
15
- import { statusLine } from '../../workspace/panel.mjs'
15
+ import { statusLine, writePanelFile } from '../../workspace/panel.mjs'
16
16
 
17
17
  export function create(ctx, rt) {
18
18
  const executeRun = createExecuteRun(ctx, rt)
@@ -219,6 +219,15 @@ const runTool = defineTool({
219
219
  blindspots: confirmation.blindspots,
220
220
  assumptions: confirmation.assumptions,
221
221
  })
222
+ // 跑完(不管成没成)都刷一次面板数据。
223
+ //
224
+ // 以前只有 /flow 和 /flow-set 这两条斜杠命令会写 panel.json,走工具这条路
225
+ // 一次都不写——于是「已经开了工作模式,状态栏却什么都没变」。斜杠命令和工具
226
+ // 是同一个能力的两个入口,表现不该差这么多。
227
+ const finish = result => {
228
+ void writePanelFile(cwd, settings).catch(() => undefined)
229
+ return result
230
+ }
222
231
  // 幂等入口:同一个项目再调一次 project_run,不该把已经跑过的段推倒重来。
223
232
  // 有未完成的 state 就自动接上去,要真正重开才传 restart: true。
224
233
  if (args.restart !== true) {
@@ -234,7 +243,7 @@ const runTool = defineTool({
234
243
  parent,
235
244
  signal: exec.signal,
236
245
  mode: `${plan.options.mode}(project_run 自动续跑)`,
237
- }).then(result => decorateCost(result, settings, args.requirements, cwd))
246
+ }).then(result => decorateCost(result, settings, args.requirements, cwd)).then(finish)
238
247
  }
239
248
  }
240
249
  }
@@ -253,7 +262,7 @@ const runTool = defineTool({
253
262
  signal: exec.signal,
254
263
  seed: undefined,
255
264
  mode: 'run',
256
- }).then(result => decorateCost(result, settings, args.requirements, cwd))
265
+ }).then(result => decorateCost(result, settings, args.requirements, cwd)).then(finish)
257
266
  },
258
267
  })
259
268
 
package/shared/util.mjs CHANGED
@@ -126,9 +126,25 @@ export function parseJsonBlock(text, marker) {
126
126
 
127
127
  export const filled = value => typeof value === 'string' && value.trim() !== ''
128
128
 
129
+ /**
130
+ * 去掉 UTF-8 BOM。
131
+ *
132
+ * Windows 上这东西到处都是:记事本另存、某些 PowerShell 写法、从别的机器拷过来
133
+ * 的文件,开头都可能带一个 0xFEFF。而 `JSON.parse` 见到它直接抛,报的是
134
+ * 「Unexpected token」,从错误信息里根本看不出是 BOM 惹的。
135
+ *
136
+ * `state.json` 尤其容易中招——它不是插件自己写的,用户编辑过、脚本生成过、
137
+ * 老版本留下过,都可能带。以前 `splitFrontMatter` 里单独处理过一次(Markdown
138
+ * 那条路),JSON 这条路没管:于是带 BOM 的 state.json 读出来是 undefined,
139
+ * 面板上那个项目直接消失,一声不响。
140
+ */
141
+ export function stripBom(text) {
142
+ return typeof text === 'string' && text.charCodeAt(0) === 0xFEFF ? text.slice(1) : text
143
+ }
144
+
129
145
  export function splitFrontMatter(text) {
130
146
  if (typeof text !== 'string') return { front: '', body: '' }
131
- const trimmed = text.replace(/^\uFEFF/u, '')
147
+ const trimmed = stripBom(text)
132
148
  if (!trimmed.startsWith('---')) return { front: '', body: trimmed }
133
149
  const firstBreak = trimmed.indexOf('\n')
134
150
  if (firstBreak === -1) return { front: '', body: trimmed }
@@ -16,6 +16,10 @@ import { LIVE_DIR } from './paths.mjs'
16
16
 
17
17
  const here = dirname(fileURLToPath(import.meta.url))
18
18
  const LIVE = join(LIVE_DIR, 'index.js')
19
+ // 不读用户自己的运行时配置。以前这里直接 import,于是用户一旦把
20
+ // flowGate 调成 strict(只在自己的工作流目录里开工),测试目录立刻不在
21
+ // 允许范围内,16 条断言能挂 14 条——看着像代码坏了,其实是配置在起作用。
22
+ process.env.DSH_HOME = await mkdtemp(join(tmpdir(), 'pf-feedback-home-'))
19
23
  const mod = await import(pathToFileURL(LIVE).href + `?t=${Date.now()}`)
20
24
 
21
25
  const results = []
@@ -10,7 +10,8 @@
10
10
  import { test } from 'node:test'
11
11
  import assert from 'node:assert/strict'
12
12
  import { join } from 'node:path'
13
- import { readFile } from 'node:fs/promises'
13
+ import { readFile, mkdir, writeFile, rm, mkdtemp } from 'node:fs/promises'
14
+ import { tmpdir } from 'node:os'
14
15
  import { pathToFileURL } from 'node:url'
15
16
  import { LIVE_DIR, sourceText } from './paths.mjs'
16
17
 
@@ -126,4 +127,36 @@ test('挂掉的命令单独拎出来,带退出码', () => {
126
127
  const window = source.slice(at, at + 2200)
127
128
  assert.ok(window.includes("item.status === 'fail'"), 'fail 要进 failed')
128
129
  assert.ok(window.includes('exit: Number.isInteger(item.exit)'), '退出码要带上')
129
- })
130
+ })
131
+
132
+ test('★ 带 BOM 的 state.json 也读得出来,项目不会从面板上消失', async () => {
133
+ // 真机踩到过:opc-deck-v4 的 state.json 开头带 0xEF 0xBB 0xBF(记事本另存、
134
+ // PowerShell 写法、从别的机器拷,都会带)。JSON.parse 见到 BOM 直接抛,
135
+ // readState 返回 undefined,面板就把它跳过了——9 个项目只显示 8 个,
136
+ // 而且一声不响。
137
+ const home = await mkdtemp(join(tmpdir(), 'pf-bom-'))
138
+ const prevHome = process.env.DSH_HOME
139
+ process.env.DSH_HOME = home
140
+ try {
141
+ const root = join(home, '工作流')
142
+ const dir = join(root, '.project-flow', 'bomproj')
143
+ await mkdir(dir, { recursive: true })
144
+ const body = JSON.stringify({ stage: 4, status: 'passed', rework: 1 }, null, 2)
145
+ // 手工拼 BOM:0xEF 0xBB 0xBF
146
+ await writeFile(join(dir, 'state.json'), Buffer.concat([
147
+ Buffer.from([0xEF, 0xBB, 0xBF]),
148
+ Buffer.from(body + '\n', 'utf8'),
149
+ ]))
150
+ const settings = { flowGate: 'strict', flowRoot: '', maxRework: 2, maxConcurrentProjects: 2,
151
+ provider: 'spawn', intentModel: '', planModel: '', buildModel: '', checkModel: '',
152
+ planProvider: '', buildProvider: '', checkProvider: '', domain: 'auto', autoCreateFlowDir: true }
153
+ const snap = await mod.panelSnapshot(root, settings)
154
+ assert.equal(snap.projects.length, 1, '带 BOM 的项目必须还在')
155
+ assert.equal(snap.projects[0].project, 'bomproj')
156
+ assert.equal(snap.projects[0].stage, 4)
157
+ } finally {
158
+ if (prevHome === undefined) delete process.env.DSH_HOME
159
+ else process.env.DSH_HOME = prevHome
160
+ await rm(home, { recursive: true, force: true }).catch(() => {})
161
+ }
162
+ })
@@ -20,6 +20,9 @@ void fileURLToPath
20
20
  // 悄悄验到「已经装好的旧副本」——看起来全过,其实验的不是手上这份代码。
21
21
  // 走 paths.mjs 就跟着 PF_LIVE_DIR / 包根走。
22
22
  const LIVE = join(LIVE_DIR, 'index.js')
23
+ // 同上:测试不该受用户 flowGate 影响。strict 时这个目录不在他的工作流根下,
24
+ // project_run 会被门控拒掉,然后卡在「等不到:规划开始」。
25
+ process.env.DSH_HOME = await mkdtemp(join(tmpdir(), 'pf-stop-home-'))
23
26
  const mod = await import(pathToFileURL(LIVE).href + `?t=${Date.now()}`)
24
27
 
25
28
  const results = []
@@ -10,7 +10,7 @@
10
10
  */
11
11
  import { test, before, after } from 'node:test'
12
12
  import assert from 'node:assert/strict'
13
- import { mkdir, writeFile, rm, mkdtemp } from 'node:fs/promises'
13
+ import { mkdir, writeFile, rm, mkdtemp, readFile } from 'node:fs/promises'
14
14
  import { join, dirname } from 'node:path'
15
15
  import { tmpdir } from 'node:os'
16
16
  import { fileURLToPath, pathToFileURL } from 'node:url'
@@ -165,4 +165,34 @@ test('门控没被放松:inside 仍然只看当前目录', async () => {
165
165
  assert.equal(strict.inside, false)
166
166
  const off = await mod.panelSnapshot(otherDir, { ...SETTINGS, flowGate: 'off' })
167
167
  assert.equal(off.inside, true)
168
+ })
169
+
170
+ test('★ 面板写到状态栏正在看的那个目录,不写会话当前目录', async () => {
171
+ // 原来的 bug:在用项目子目录里干活时,writePanelFile 把 panel.json 写进了
172
+ // 那个子目录,而状态栏读的是工作流根——项目在跑、状态栏一动不动。
173
+ const proj = join(flowDir, 'newproj')
174
+ await mkdir(proj, { recursive: true })
175
+ const written = await mod.writePanelFile(proj, SETTINGS)
176
+ assert.ok(written !== undefined, '在工作流范围内就该写')
177
+
178
+ const atRoot = await readFile(join(flowDir, '.project-flow', 'panel.json'), 'utf8')
179
+ assert.equal(JSON.parse(atRoot).routes.plan, '继承主对话')
180
+ // 子目录里不许有自己的那份
181
+ await assert.rejects(
182
+ () => readFile(join(proj, '.project-flow', 'panel.json'), 'utf8'),
183
+ '会话目录下不该再冒出一份 panel.json',
184
+ )
185
+ })
186
+
187
+ test('★ 不在工作流范围内:一个字节都不写', async () => {
188
+ // 默认工作区里改模型不该在那边点亮状态栏
189
+ const before = await readFile(join(flowDir, '.project-flow', 'panel.json'), 'utf8')
190
+ const out = await mod.writePanelFile(otherDir, SETTINGS)
191
+ assert.equal(out, undefined)
192
+ const after = await readFile(join(flowDir, '.project-flow', 'panel.json'), 'utf8')
193
+ assert.equal(after, before, '别处调用不该改动工作流那份')
194
+ await assert.rejects(
195
+ () => readFile(join(otherDir, '.project-flow', 'panel.json'), 'utf8'),
196
+ '别处也不该新建一份',
197
+ )
168
198
  })
@@ -141,6 +141,17 @@ export async function writePanelFile(cwd, settings) {
141
141
  // 只在工作流目录里落这份:否则在普通目录改一下模型,状态栏就会在那边点亮,
142
142
  // 而那个目录其实不接受工作模式。
143
143
  if (!insideFlow(cwd, settings)) return undefined
144
+ // 写到「状态栏正在看的那个目录」,不是会话当前目录。
145
+ //
146
+ // 以前是 join(cwd, '.project-flow', 'panel.json'),于是同一个工作流会长出两份
147
+ // 面板数据:在外层壳目录开一次工落在壳里,项目却在它下面的「工作流」子目录里。
148
+ // 状态栏读一份、项目在另一份,界面上就是「项目在跑但状态栏没动」。
149
+ //
150
+ // 这里必须用 watchRootOf 而不是 flowRootTargetOf:读和写要用同一个解析函数,
151
+ // 否则「读哪儿」和「写哪儿」各算一套,错位比原来更难查。两者在配了绝对
152
+ // flowRoot 时结果相同,在没配时 flowRootTargetOf 会算出 cwd/工作流 这个
153
+ // 根本不存在的路径,而 watchRootOf 会正确回到 cwd 或找到祖先里那层「工作流」。
154
+ const root = await watchRootOf(cwd, settings)
144
155
  const route = (llmProvider, model) => (model === '' ? '继承主对话' : `${llmProvider}/${model}`)
145
156
  const payload = {
146
157
  routes: {
@@ -154,8 +165,8 @@ export async function writePanelFile(cwd, settings) {
154
165
  updatedAt: nowIso(),
155
166
  }
156
167
  try {
157
- await mkdir(join(cwd, '.project-flow'), { recursive: true })
158
- await writeFileAtomic(join(cwd, '.project-flow', 'panel.json'), `${JSON.stringify(payload, null, 2)}\n`)
168
+ await mkdir(join(root, '.project-flow'), { recursive: true })
169
+ await writeFileAtomic(join(root, '.project-flow', 'panel.json'), `${JSON.stringify(payload, null, 2)}\n`)
159
170
  } catch {
160
171
  // 写不进去只影响状态栏显示,不该拖累正在跑的东西
161
172
  }
@@ -7,7 +7,7 @@ import { existsSync } from 'node:fs'
7
7
  import { mkdir, readFile } from 'node:fs/promises'
8
8
  import { tmpdir, homedir } from 'node:os'
9
9
  import { FLOW_DIR_NAME, FLOW_GATE_MODES, INTENT_CONFIDENCE_FLOOR, LAST_ROOT_FILE } from '../shared/constants.mjs'
10
- import { dirExists, messageOf, writeFileAtomic } from '../shared/util.mjs'
10
+ import { dirExists, messageOf, stripBom, writeFileAtomic } from '../shared/util.mjs'
11
11
  import { pfState } from '../state/registry.mjs'
12
12
 
13
13
  export function cwdOf(ctx, agent) {
@@ -332,7 +332,7 @@ export async function cwdFromSessionStore(sid) {
332
332
  if (sid.includes('/') || sid.includes('\\') || sid.includes('..')) return undefined
333
333
  const file = join(stateHome(), 'storages', 'session_projcache', 'sessions', `${sid}.json`)
334
334
  try {
335
- const parsed = JSON.parse(await readFile(file, 'utf8'))
335
+ const parsed = JSON.parse(stripBom(await readFile(file, 'utf8')))
336
336
  const cwd = parsed?.record?.identity?.cwd
337
337
  return typeof cwd === 'string' && cwd !== '' && isAbsolute(cwd) ? cwd : undefined
338
338
  } catch {