@zhushanwen/pi-system-prompt-trace 0.1.0 → 0.1.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhushanwen/pi-system-prompt-trace",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "System prompt trace extension: appends xyz:system-prompt entries to the session JSONL whenever the effective system prompt is established or changes",
5
5
  "type": "module",
6
6
  "main": "index.ts",
@@ -17,18 +17,24 @@
17
17
  "src/**/*.ts",
18
18
  "README.md"
19
19
  ],
20
+ "xyz-agent": {
21
+ "role": "taiji"
22
+ },
20
23
  "pi": {
21
24
  "extensions": [
22
25
  "./index.ts"
23
26
  ]
24
27
  },
25
28
  "peerDependencies": {
26
- "@earendil-works/pi-coding-agent": "*"
29
+ "@earendil-works/pi-coding-agent": "^0.84.1"
27
30
  },
28
31
  "devDependencies": {
29
32
  "@vitest/coverage-v8": "^4.1.9",
30
33
  "vitest": "^4.1.8"
31
34
  },
35
+ "dependencies": {
36
+ "@zhushanwen/pi-extension-logger": "0.3.0"
37
+ },
32
38
  "scripts": {
33
39
  "typecheck": "npx tsc --noEmit",
34
40
  "test": "vitest run"
@@ -25,6 +25,15 @@ import type { SystemPromptTraceEntryData } from "../types.js";
25
25
  const P1 = "wiring prompt\nline-1";
26
26
  const P2 = "wiring prompt\nline-1\nline-2-added";
27
27
 
28
+ const { loggerMock } = vi.hoisted(() => {
29
+ const mock = { debug: vi.fn(), warn: vi.fn(), error: vi.fn() };
30
+ return { loggerMock: mock };
31
+ })
32
+ vi.mock("@zhushanwen/pi-extension-logger", () => ({
33
+ getLogger: () => loggerMock,
34
+ createLogger: () => loggerMock,
35
+ }))
36
+
28
37
  const agentDirRef = vi.hoisted(() => ({ current: "" }));
29
38
 
30
39
  vi.mock("@earendil-works/pi-coding-agent", () => ({
@@ -277,17 +286,15 @@ describe("index.ts wiring SDK 契约", () => {
277
286
  throw new Error("prompt boom");
278
287
  }, "sess-w7");
279
288
 
280
- let logged = 0;
281
- const errSpy = vi.spyOn(console, "error").mockImplementation(() => {
282
- logged++;
283
- });
289
+ loggerMock.error.mockClear();
284
290
  try {
285
291
  await emit(h, "session_start", { type: "session_start", reason: "startup" }, boomCtx);
286
292
  await emit(h, "turn_start", { type: "turn_start", turnIndex: 0, timestamp: 0 }, boomCtx);
287
293
  } finally {
288
- errSpy.mockRestore();
294
+ // no-op: cleanup handled after assertions
289
295
  }
290
296
  expect(h.entries).toHaveLength(0);
291
- expect(logged).toBe(1);
297
+ expect(loggerMock.error).toHaveBeenCalledTimes(1);
298
+ loggerMock.error.mockClear();
292
299
  });
293
300
  });
package/src/baseline.ts CHANGED
@@ -6,13 +6,16 @@
6
6
  * - readPersistedBaseline / writePersistedBaseline:dataDir 自持久化小文件
7
7
  * (app 重启直 spawn resume 时唯一可用的基线来源),原子写入。
8
8
  *
9
- * 所有函数不抛错:读失败返回 null、写失败 console.error 后静默——基线丢失的代价只是
9
+ * 所有函数不抛错:读失败返回 null、写失败 logger.error 后静默——基线丢失的代价只是
10
10
  * 下次 resume 多写一条留痕(设计 D2 已接受),不允许影响 agent 主流程。
11
11
  */
12
12
 
13
13
  import { existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
14
14
  import { dirname } from "node:path";
15
15
 
16
+ import { getLogger } from "@zhushanwen/pi-extension-logger";
17
+
18
+ const logger = getLogger("pi-system-prompt-trace");
16
19
  import { isRecord, isSystemPromptTraceEntryData, SYSTEM_PROMPT_CUSTOM_TYPE } from "./types.js";
17
20
  import type { PromptBaseline } from "./types.js";
18
21
 
@@ -152,8 +155,8 @@ export function writePersistedBaseline(
152
155
  }
153
156
  } catch (e) {
154
157
  // best-effort 降级:基线写失败只影响下次 app 重启 resume 的去重(多写一条留痕,
155
- // 设计 D2 已接受),不阻断 agent 主流程;错误进 pi stdout 随日志落盘供排查
156
- console.error("[pi-system-prompt-trace] write baseline failed:", e);
158
+ // 设计 D2 已接受),不阻断 agent 主流程;logger.error appendEntry 持久化供排查
159
+ logger.error("write baseline failed", { detail: String(e) });
157
160
  }
158
161
  }
159
162
 
package/src/trace.ts CHANGED
@@ -12,6 +12,9 @@
12
12
 
13
13
  import { createHash } from "node:crypto";
14
14
 
15
+ import { getLogger } from "@zhushanwen/pi-extension-logger";
16
+
17
+ const logger = getLogger("pi-system-prompt-trace");
15
18
  import { summarizePromptDiff } from "./diff.js";
16
19
  import {
17
20
  mapReasonForFirstWrite,
@@ -145,8 +148,8 @@ export function createSystemPromptTrace(env: TraceEnv, stash: SwitchStash): Syst
145
148
  write(ctx, text, hash, reason, version, parentFullText);
146
149
  current = { version, hash, fullText: text };
147
150
  } catch (e) {
148
- // 留痕是诊断性旁路:任何失败都不允许影响 agent 主流程(错误进 pi stdout,随日志落盘)
149
- console.error("[pi-system-prompt-trace] turn_start handler failed:", e);
151
+ // 留痕是诊断性旁路:任何失败都不允许影响 agent 主流程(logger.error appendEntry 持久化)
152
+ logger.error("turn_start handler failed", { detail: String(e) });
150
153
  }
151
154
  },
152
155
  };