@zhushanwen/pi-ext-guards 0.4.2 → 0.4.4

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-ext-guards",
3
- "version": "0.4.2",
3
+ "version": "0.4.4",
4
4
  "description": "Shared runtime-environment guards for Pi extensions — oncePerProcess(key, fn) dedupes cross-session side-effect operations against pi factory re-invocation and handler accumulation (shared library, not a Pi extension, zero dependencies).",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -1,14 +1,16 @@
1
1
  // src/__tests__/predicates.test.ts
2
2
  //
3
- // isRecord / isEnoentError / isSubagentProcess 单测(ext-simplify-17 D3/D2/D4 新导出):
3
+ // isRecord / isEnoentError / isErrnoWithCode / isSubagentProcess 单测
4
+ //(ext-simplify-17 D3/D2/D4 新导出 + isErrnoWithCode errno 守卫上收):
4
5
  // - isRecord 排数组严版:数组必须 false(钉值,防回退到允数组宽版)
5
6
  // - isEnoentError scheduler 严版:仅 Error 子类 + code === "ENOENT" 才 true,
6
7
  // 非 Error 对象带 code 是宽版才接受的形态,严版必须 false
8
+ // - isErrnoWithCode:isEnoentError 的 code 参数化形态,收窄纪律同源
7
9
  // - isSubagentProcess 三态 + env 注入隔离 + 旧键宁缺勿污钉值(D4 重锚定)
8
10
 
9
11
  import { describe, expect, it } from "vitest";
10
12
 
11
- import { isEnoentError, isRecord, isSubagentProcess } from "../index.ts";
13
+ import { isEnoentError, isErrnoWithCode, isRecord, isSubagentProcess } from "../index.ts";
12
14
 
13
15
  describe("isRecord", () => {
14
16
  it("对象 true", () => {
@@ -58,6 +60,32 @@ describe("isEnoentError", () => {
58
60
  });
59
61
  });
60
62
 
63
+ describe("isErrnoWithCode", () => {
64
+ it("Error 带 code=期望值 true(Node fs 错误的构造形态)", () => {
65
+ expect(isErrnoWithCode(Object.assign(new Error("exists"), { code: "EEXIST" }), "EEXIST")).toBe(
66
+ true,
67
+ );
68
+ });
69
+
70
+ it("code 为其他值 false", () => {
71
+ expect(isErrnoWithCode(Object.assign(new Error("exists"), { code: "EEXIST" }), "EACCES")).toBe(
72
+ false,
73
+ );
74
+ });
75
+
76
+ it("普通 Error false(无 code 属性)", () => {
77
+ expect(isErrnoWithCode(new Error("x"), "EEXIST")).toBe(false);
78
+ });
79
+
80
+ it("非 Error 对象带同名 code false(与 isEnoentError 同一严版口径)", () => {
81
+ expect(isErrnoWithCode({ code: "EEXIST" }, "EEXIST")).toBe(false);
82
+ });
83
+
84
+ it("undefined false", () => {
85
+ expect(isErrnoWithCode(undefined, "EEXIST")).toBe(false);
86
+ });
87
+ });
88
+
61
89
  describe("isSubagentProcess", () => {
62
90
  it("TAIJI_AGENT_SUBAGENT=1 命中(引擎 spawn 链恒注入的现行标记)", () => {
63
91
  expect(isSubagentProcess({ TAIJI_AGENT_SUBAGENT: "1" })).toBe(true);
package/src/index.ts CHANGED
@@ -116,6 +116,18 @@ export function isEnoentError(err: unknown): boolean {
116
116
  return err instanceof Error && "code" in err && err.code === "ENOENT";
117
117
  }
118
118
 
119
+ /**
120
+ * 判断 error 是否为带指定 errno code 的 Node 系统调用错误(`Error` 实例且
121
+ * `code === 期望值`)。
122
+ *
123
+ * 与 isEnoentError 同一收窄纪律(`"code" in err` 零断言——taste/no-unsafe-cast),
124
+ * code 参数化:isEnoentError = code 钉死 "ENOENT" 的特化形态。非 Error 对象即使带
125
+ * 同名 code 也 false(与 isEnoentError 的严版口径一致——Node fs 抛错恒为 Error 实例)。
126
+ */
127
+ export function isErrnoWithCode(error: unknown, code: string): boolean {
128
+ return error instanceof Error && "code" in error && error.code === code;
129
+ }
130
+
119
131
  // ── subagent 进程判据(isSubagentProcess,ext-simplify-17 D4)────────────
120
132
  //
121
133
  // 背景:extension 需要知道「当前 pi 进程是否引擎链上的 subagent 子进程」来决定降级
@@ -163,9 +175,10 @@ export function isSubagentProcess(
163
175
  /**
164
176
  * pi ExtensionRunner 在 session 替换后标记 stale ctx 的错误文案片段。
165
177
  *
166
- * pi 语义断言(0.84.4 实装:runner.js `invalidate()` 默认 message + `assertActive()`
167
- * `throw new Error(this.staleMessage)`),登记于 docs/pi-semantics.json PS-30
168
- * (探针:extensions/shared/ext-guards/src/__tests__/pi-semantics-stale-ctx-wording.test.ts),
178
+ * pi 语义断言(语义登记 docs/pi-semantics.json PS-30,verifiedWith 以该登记为准:
179
+ * runner.js `invalidate()` 默认 message + `assertActive()`
180
+ * `throw new Error(this.staleMessage)`;
181
+ * 探针:extensions/shared/ext-guards/src/__tests__/pi-semantics-stale-ctx-wording.test.ts),
169
182
  * 随 C-proc-08 pi 版本门禁自动重验。文案变更时守卫退化为「全部上抛」(回到现状崩溃链路,
170
183
  * 有 pi-crash log 取证,不更危险),门禁报红提示更新分诊词。
171
184
  */