@istuen/pt 0.1.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.
Files changed (166) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +219 -0
  3. package/dist/agent/api-bridge.d.ts +8 -0
  4. package/dist/agent/api-bridge.js +83 -0
  5. package/dist/agent/index.d.ts +2 -0
  6. package/dist/agent/index.js +3 -0
  7. package/dist/agent/pi-adapter.d.ts +35 -0
  8. package/dist/agent/pi-adapter.js +251 -0
  9. package/dist/agent/registry.d.ts +4 -0
  10. package/dist/agent/registry.js +40 -0
  11. package/dist/asset-health.d.ts +50 -0
  12. package/dist/asset-health.js +267 -0
  13. package/dist/asset-pack/loader.d.ts +47 -0
  14. package/dist/asset-pack/loader.js +121 -0
  15. package/dist/asset-pack/manifest.d.ts +23 -0
  16. package/dist/asset-pack/manifest.js +96 -0
  17. package/dist/asset-pack/md-file-pack.d.ts +41 -0
  18. package/dist/asset-pack/md-file-pack.js +153 -0
  19. package/dist/asset-pack/validate.d.ts +55 -0
  20. package/dist/asset-pack/validate.js +131 -0
  21. package/dist/builtin/assets/blueprints/dev-knowledge.blueprint.yaml +13 -0
  22. package/dist/builtin/assets/domains/agent-info.md +15 -0
  23. package/dist/builtin/assets/domains/authoring.md +201 -0
  24. package/dist/builtin/assets/domains/pack-repair.md +47 -0
  25. package/dist/builtin/assets/domains/project-analysis.md +52 -0
  26. package/dist/builtin/assets/domains/usage.md +81 -0
  27. package/dist/builtin/assets/domains/user-info.md +15 -0
  28. package/dist/builtin/assets/profiles/guide.profile.md +28 -0
  29. package/dist/commands.d.ts +69 -0
  30. package/dist/commands.js +344 -0
  31. package/dist/compile/agent-context.d.ts +48 -0
  32. package/dist/compile/agent-context.js +423 -0
  33. package/dist/compile/index.d.ts +1 -0
  34. package/dist/compile/index.js +5 -0
  35. package/dist/compile/resolve-use.d.ts +36 -0
  36. package/dist/compile/resolve-use.js +171 -0
  37. package/dist/compile/type-guards.d.ts +29 -0
  38. package/dist/compile/type-guards.js +107 -0
  39. package/dist/config.d.ts +31 -0
  40. package/dist/config.js +133 -0
  41. package/dist/constants.d.ts +52 -0
  42. package/dist/constants.js +81 -0
  43. package/dist/diagnostics.d.ts +14 -0
  44. package/dist/diagnostics.js +29 -0
  45. package/dist/index.d.ts +2 -0
  46. package/dist/index.js +838 -0
  47. package/dist/injection-status.d.ts +45 -0
  48. package/dist/injection-status.js +134 -0
  49. package/dist/log.d.ts +64 -0
  50. package/dist/log.js +139 -0
  51. package/dist/manual-session.d.ts +24 -0
  52. package/dist/manual-session.js +152 -0
  53. package/dist/manual-track.d.ts +35 -0
  54. package/dist/manual-track.js +107 -0
  55. package/dist/parse/blueprint.d.ts +5 -0
  56. package/dist/parse/blueprint.js +59 -0
  57. package/dist/parse/domain-renderers.d.ts +8 -0
  58. package/dist/parse/domain-renderers.js +115 -0
  59. package/dist/parse/domain.d.ts +7 -0
  60. package/dist/parse/domain.js +69 -0
  61. package/dist/parse/index.d.ts +9 -0
  62. package/dist/parse/index.js +223 -0
  63. package/dist/parse/profile.d.ts +18 -0
  64. package/dist/parse/profile.js +117 -0
  65. package/dist/parse/ref-resolver.d.ts +63 -0
  66. package/dist/parse/ref-resolver.js +174 -0
  67. package/dist/parse/shared.d.ts +76 -0
  68. package/dist/parse/shared.js +273 -0
  69. package/dist/profile-persist.d.ts +22 -0
  70. package/dist/profile-persist.js +53 -0
  71. package/dist/render/cache.d.ts +15 -0
  72. package/dist/render/cache.js +135 -0
  73. package/dist/render/index.d.ts +3 -0
  74. package/dist/render/index.js +14 -0
  75. package/dist/render/session-inject.d.ts +5 -0
  76. package/dist/render/session-inject.js +24 -0
  77. package/dist/render/turn-inject.d.ts +36 -0
  78. package/dist/render/turn-inject.js +248 -0
  79. package/dist/schema.d.ts +448 -0
  80. package/dist/schema.js +45 -0
  81. package/dist/session.d.ts +82 -0
  82. package/dist/session.js +102 -0
  83. package/dist/slog.d.ts +3 -0
  84. package/dist/slog.js +27 -0
  85. package/dist/transpile.d.ts +34 -0
  86. package/dist/transpile.js +220 -0
  87. package/dist/verify/file-hash.d.ts +2 -0
  88. package/dist/verify/file-hash.js +30 -0
  89. package/dist/verify/fs-content-match.d.ts +2 -0
  90. package/dist/verify/fs-content-match.js +26 -0
  91. package/dist/verify/fs-exists.d.ts +2 -0
  92. package/dist/verify/fs-exists.js +13 -0
  93. package/dist/verify/fs-not-exists.d.ts +2 -0
  94. package/dist/verify/fs-not-exists.js +13 -0
  95. package/dist/verify/git-status-clean.d.ts +2 -0
  96. package/dist/verify/git-status-clean.js +19 -0
  97. package/dist/verify/index.d.ts +7 -0
  98. package/dist/verify/index.js +46 -0
  99. package/dist/verify/lint-check.d.ts +2 -0
  100. package/dist/verify/lint-check.js +17 -0
  101. package/dist/verify/read-stderr.d.ts +5 -0
  102. package/dist/verify/read-stderr.js +20 -0
  103. package/dist/verify/ref-check.d.ts +13 -0
  104. package/dist/verify/ref-check.js +88 -0
  105. package/dist/verify/test-pass.d.ts +2 -0
  106. package/dist/verify/test-pass.js +17 -0
  107. package/dist/verify/ts-compiles.d.ts +2 -0
  108. package/dist/verify/ts-compiles.js +17 -0
  109. package/package.json +62 -0
  110. package/src/agent/api-bridge.ts +105 -0
  111. package/src/agent/index.ts +4 -0
  112. package/src/agent/pi-adapter.ts +326 -0
  113. package/src/agent/registry.ts +44 -0
  114. package/src/asset-health.ts +327 -0
  115. package/src/asset-pack/loader.ts +141 -0
  116. package/src/asset-pack/manifest.ts +118 -0
  117. package/src/asset-pack/md-file-pack.ts +202 -0
  118. package/src/asset-pack/validate.ts +186 -0
  119. package/src/builtin/assets/blueprints/dev-knowledge.blueprint.yaml +13 -0
  120. package/src/builtin/assets/domains/agent-info.md +15 -0
  121. package/src/builtin/assets/domains/authoring.md +201 -0
  122. package/src/builtin/assets/domains/pack-repair.md +47 -0
  123. package/src/builtin/assets/domains/project-analysis.md +52 -0
  124. package/src/builtin/assets/domains/usage.md +81 -0
  125. package/src/builtin/assets/domains/user-info.md +15 -0
  126. package/src/builtin/assets/profiles/guide.profile.md +28 -0
  127. package/src/commands.ts +405 -0
  128. package/src/compile/agent-context.ts +487 -0
  129. package/src/compile/index.ts +5 -0
  130. package/src/compile/resolve-use.ts +208 -0
  131. package/src/compile/type-guards.ts +132 -0
  132. package/src/config.ts +154 -0
  133. package/src/constants.ts +104 -0
  134. package/src/diagnostics.ts +36 -0
  135. package/src/index.ts +1028 -0
  136. package/src/injection-status.ts +155 -0
  137. package/src/log.ts +173 -0
  138. package/src/manual-session.ts +190 -0
  139. package/src/manual-track.ts +127 -0
  140. package/src/parse/blueprint.ts +82 -0
  141. package/src/parse/domain-renderers.ts +120 -0
  142. package/src/parse/domain.ts +78 -0
  143. package/src/parse/index.ts +266 -0
  144. package/src/parse/profile.ts +139 -0
  145. package/src/parse/ref-resolver.ts +198 -0
  146. package/src/parse/shared.ts +331 -0
  147. package/src/profile-persist.ts +60 -0
  148. package/src/render/cache.ts +150 -0
  149. package/src/render/index.ts +14 -0
  150. package/src/render/session-inject.ts +26 -0
  151. package/src/render/turn-inject.ts +278 -0
  152. package/src/schema.ts +542 -0
  153. package/src/session.ts +190 -0
  154. package/src/slog.ts +32 -0
  155. package/src/transpile.ts +302 -0
  156. package/src/verify/file-hash.ts +29 -0
  157. package/src/verify/fs-content-match.ts +28 -0
  158. package/src/verify/fs-exists.ts +14 -0
  159. package/src/verify/fs-not-exists.ts +17 -0
  160. package/src/verify/git-status-clean.ts +23 -0
  161. package/src/verify/index.ts +57 -0
  162. package/src/verify/lint-check.ts +21 -0
  163. package/src/verify/read-stderr.ts +24 -0
  164. package/src/verify/ref-check.ts +118 -0
  165. package/src/verify/test-pass.ts +18 -0
  166. package/src/verify/ts-compiles.ts +21 -0
@@ -0,0 +1,448 @@
1
+ /** 业务术语原子(Domain.## Scene / ## Term / ## Glossary 等场景段的内容) */
2
+ export interface Term {
3
+ name: string;
4
+ /** 描述(人类可读语义) */
5
+ desc?: string;
6
+ /** 可选标签,不强制:作者判定放哪层(公理 = 业务语义源;定理 = 公理的组合或业务具体描述) */
7
+ level?: "axiom" | "theorem";
8
+ /** 结构化字段清单(如 task-description 的 [必读, 设计原则, 步骤, ...])。
9
+ * 来自资产 `- fields: [a, b, c]` 行;renderer 输出 `(字段:a/b/c)`。
10
+ * v9.2 扩展:之前作者写的 fields/purpose/rule 等附加信息在 parse 阶段被静默丢弃,
11
+ * 加此字段让作者的结构化表达进入 IR。 */
12
+ fields?: string[];
13
+ /** 补充说明(purpose / rule 等非 desc 的语义信息统一收纳)。
14
+ * renderer 在 desc 后追加 ` — note`。
15
+ * v9.2 扩展:与 fields 同步,让作者写在 Scene 段的 purpose/rule 不再被丢。 */
16
+ note?: string;
17
+ /** Phase term-P9.1:外部数据源路径(替代 workflow Scene 的 ExternalRef)。
18
+ * Scene 段统一为 Term[] 后,workflow 的 externals 用带 path 的 Term 表示;
19
+ * 渲染:有 path 输出 `- name: path — desc`,无 path 输出 `- name: desc`。 */
20
+ path?: string;
21
+ }
22
+ /** 外部数据源引用(声明式:路径 + 协议,不在此处拉取) */
23
+ export interface ExternalRef {
24
+ name: string;
25
+ path: string;
26
+ /** 描述(workflow Scene 概念项无 path 时用 desc 承载语义) */
27
+ desc?: string;
28
+ protocol?: "file" | "api" | "db";
29
+ }
30
+ /** 业务规则(挂到步骤或全局) */
31
+ export interface Rule {
32
+ /** 规则名(H3 标题,如 modules-type-safety);renderer 输出 `name: check` 格式 */
33
+ name: string;
34
+ /** 挂到哪个步骤的 slot 名;"global" 表示跨模块聚合(hybrid 模式下抽到全局段) */
35
+ slot: string;
36
+ /** ban = 禁止项;invariant = 不变量 */
37
+ type: "ban" | "invariant";
38
+ /** 规则描述(人类可读) */
39
+ check: string;
40
+ /** ban 类型时列出具体禁止项 */
41
+ items?: string[];
42
+ }
43
+ /** 手册步骤(workflow-Domain.## Manual 段内容) */
44
+ export interface FlowStep {
45
+ /** 做什么 */
46
+ desc: string;
47
+ /** 从哪取数据(数据语义层) */
48
+ dataSource?: ExternalRef;
49
+ /** 套哪条规则(引用知识库的 Rule) */
50
+ rule?: string;
51
+ /** 期望产出什么 */
52
+ output?: string;
53
+ /** 验证参照(Probe 名列表,如 ["fs-content-match", "ts-compiles"])。
54
+ * P0 新增:声明这步执行后用什么 probe 验证。probe 实现在 src/verify/(P1)。 */
55
+ observe?: string[];
56
+ }
57
+ /** 验证结果三态(借鉴 OXN ADR-0066/0067,简化为纯枚举 + 消息,不引入 strategy 模式)。
58
+ * - COMPLETED:执行符合预期
59
+ * - DEVIATED:偏离预期(不是失败,是偏了,仍可继续)
60
+ * - INCONCLUSIVE:无法判定(如人工评估、probe 缺参数) */
61
+ export type ProbeOutcomeKind = "COMPLETED" | "DEVIATED" | "INCONCLUSIVE";
62
+ /** verify 函数返回值(P1 的 src/verify/ 模块用)。 */
63
+ export interface ProbeOutcome {
64
+ outcome: ProbeOutcomeKind;
65
+ message: string;
66
+ actual?: string;
67
+ }
68
+ /** Manual 实例文档的步骤执行记录(buildManualDoc 生成表头,执行者用 edit 填值)。 */
69
+ export interface StepResult {
70
+ stepIndex: number;
71
+ outcome: ProbeOutcomeKind;
72
+ message?: string;
73
+ }
74
+ /** 手册模板(Domain.## Flows 段声明,实例化后进 Turn Inject) */
75
+ export interface FlowTemplate {
76
+ /** /name 触发 */
77
+ name: string;
78
+ /** 参数提示,如 "<客户ID> <金额>" */
79
+ argumentHint?: string;
80
+ /** 数据语义层:带 {{}} 占位符的前提 */
81
+ intent: string;
82
+ /** 手册结构层:步骤 + 数据源 + 期望产出 */
83
+ steps: FlowStep[];
84
+ /** 数据语义层:引用知识库的数据源 */
85
+ externals: ExternalRef[];
86
+ }
87
+ /** Phase term-P9.2:验收清单(Domain.## Checklists 段内容)。
88
+ * 与 Rule[] 不同:Checklist 只列条目,不带 slot/type/check——是“待验证项列表”。
89
+ * Renderer:``### name\\n- item1\\n- item2``。 */
90
+ export interface Checklist {
91
+ name: string;
92
+ items: string[];
93
+ }
94
+ /** 编排策略。BlueprintGroup.mode 决定段落拼接顺序。 */
95
+ export interface StructureLayout {
96
+ mode: "byDomain" | "byType" | "hybrid";
97
+ /** byDomain / hybrid 时的模块顺序;未指定则原序 */
98
+ domainOrder?: string[];
99
+ }
100
+ /** 流程节点(保留类型,v9 不再使用——Boundaries 丢弃,但类型留作未来扩展参考)。 */
101
+ export interface BoundaryNode {
102
+ /** 步骤名 */
103
+ slot: string;
104
+ /** 依赖的前置 slot 名列表 */
105
+ deps: string[];
106
+ /** 步骤描述 */
107
+ desc: string;
108
+ }
109
+ /** Agent 注入位置(结构层术语,由 AgentAdapter 映射到 Agent 技术 API 名)。
110
+ * v9(Phase term-P4.3):语义值 "session"/"turn" 取代旧 "system_prompt"/"context_message"——session
111
+ * 对应 LLM 失忆后重注入(system_prompt 级),turn 对应按需触发(context_message 级)。
112
+ * Agent-agnostic:AgentAdapter 内部映射到 Pi 的 system_prompt/context_message 等技术名。
113
+ * Phase term-naming:字段名 target → inject(更直白表达动作意图)。InjectTarget 类型名。 */
114
+ export type InjectTarget = "session" | "turn" | string;
115
+ /** Blueprint 的聚合组定义(对应 Blueprint yaml 的 groups 项,聚合组名=人类自定义语义名)。
116
+ * v9.1(modules-to-profile 迁移):BlueprintGroup 删 `modules` 字段——Blueprint 退化为插槽契约
117
+ * (name + inject + mode),modules 由 ProfileGroup 提供。Profile 通过 H2 匹配插槽名,用
118
+ * `### Modules` 段填 modules 列表。Blueprint 加新插槽向后兼容(旧 Profile 无 H2 → 产出空段)。 */
119
+ export interface BlueprintGroup {
120
+ /** 聚合组名(语义名,Blueprint 配置项,如 "会话背景"/"参考手册")。 */
121
+ name: string;
122
+ /** 注入位置(session / turn / 扩展)——值语义名,经由 AgentAdapter 映射到具体 Agent Runtime API。 */
123
+ inject: InjectTarget;
124
+ /** 聚合方式(仅 session 类聚合组有意义)。 */
125
+ mode?: StructureLayout["mode"];
126
+ }
127
+ /** modules-to-profile-complete:modName 解析形态。2 形态:
128
+ * - 段名("Scene" / "User" / "Agent")— 跨所有引用域找该段,整段聚合
129
+ * - 段.项("User.user-profile" / "Agent.senior-developer")— 跨所有引用域找该段下 H3 项
130
+ * 限定到单个 domain 通过专用段名(User / Agent)实现,不引入形态 3(domain:段.项)——避免 profile 重复写引用。
131
+ * v9.1(迁移后)原本用裸 string 名("Scene" / "User"),v9.1+ 用本结构对象。 */
132
+ export interface ModName {
133
+ /** H2 段名(命名空间) */
134
+ section: string;
135
+ /** H3 项名(可选)。undefined = 段粒度匹配;string = 该段下 H3 项名 */
136
+ item?: string;
137
+ }
138
+ /** Profile 的聚合组实例化(对应 Profile md 的 H2,与 Blueprint 的 BlueprintGroup 同名)。
139
+ * v9:domains(追加到本聚合组的 Domain 名列表)。trigger/boundaries 删除。
140
+ * v9.1(modules-to-profile 迁移):加 `modules` 字段——Profile H2 下的 `### Modules` 列表
141
+ * 填本插槽的聚合模块(具名身份段 + 段类型)。原 BlueprintGroup.modules 已删除。
142
+ * v9.1+(modules-to-profile-complete):modules 元素从 `string` 改为 `ModName` 对象——
143
+ * 支持段粒度 + 段.项粒度两种形态。 */
144
+ export interface ProfileGroup {
145
+ /** 聚合组名(与 Blueprint 的 BlueprintGroup.name 对应)。 */
146
+ name: string;
147
+ /** 追加到本聚合组的 Domain 名列表(只贡献该聚合组)。 */
148
+ domains: string[];
149
+ /** 本插槽填的聚合模块列表(Profile `### Modules` 段下的项解析为 ModName 对象)。
150
+ * - 段名:跨所有引用域该段整段聚合
151
+ * - 段.项:跨所有引用域该段下 H3 项(精确选)
152
+ * 为空数组 = 该 Profile 故意不填此插槽("缺填"——log debug only 不 notify,见设计文档 §5)。 */
153
+ modules: ModName[];
154
+ }
155
+ /**
156
+ * v9 Domain:内容层模块。承载语义定义与上下文模块内容。
157
+ * - (Phase term-P9.3)type 字段已删除——H2 段名直接决定 schema(一个 H2 段一个 schema)。
158
+ * 原 type: term/workflow/stack 是「知识性质标签」+「schema 选择器」一身二任,
159
+ * 拆 Type 后由 H2 段名 + Term.path 等字段直接表达。
160
+ * - modules:H2 段名 → 段内容(key = "Scene"/"Trigger"/"Manual"/...,开放扩展)。
161
+ *
162
+ * H2 段名是开放的——加新模块类型 = 加新 H2 段名 + Blueprint 声明该模块。
163
+ * type 决定 H2 段内部格式,H2 段名决定内容去向——两者独立扩展。
164
+ */
165
+ export interface Domain {
166
+ name: string;
167
+ /** Phase term-P9.3:删除 type 字段——Type 一身二任(知识性质标签 + schema 选择器)导致 stack 死类型
168
+ * + 加新 Type 要改所有 H2 段的 renderer。Type 拆后由 H2 段名直接决定 schema(一个 H2 段一个 schema)。
169
+ * 不再加 frontmatter.type;Domain 不再有 type 字段。 */
170
+ /** H2 段名 → 段内容。Pt 核心按 H2 名读段,不硬编码段名。 */
171
+ modules: Record<string, unknown>;
172
+ }
173
+ /**
174
+ * v9 Blueprint:Agent 端聚合组结构,结构层模块。
175
+ * - (Phase term-P4.1)移除 agent 字段:Blueprint 应 Agent-agnostic,agent 是运行时选择不是结构定义。
176
+ * 消费方 fallback 硬编码 "pi"(见 src/index.ts transpileActive);等第二个 Adapter(OpenCodeAdapter)
177
+ * 落实后改 transpile(profile, agent) 编译维度参数(§11 实现节奏)。
178
+ * - compilation:编译方式(缓存目录 + 拆分策略)。P4.2 将移除。
179
+ * - groups:聚合组列表(YAML groups: [...] 项),定义 inject + Modules 聚合点 + mode。
180
+ *
181
+ * 跨项目复用。加新 Agent 只加 AgentAdapter;加新聚合组 = Blueprint groups 加一项 + modules 列。
182
+ */
183
+ export interface Blueprint {
184
+ name: string;
185
+ /** 聚合组列表(YAML groups: [...] 项),定义 inject + Modules + mode。 */
186
+ groups: BlueprintGroup[];
187
+ }
188
+ /**
189
+ * v9 Profile:业务端实例,配置层模块。
190
+ * - blueprint:引用哪个 Blueprint(结构复用)。
191
+ * - domains:YAML 全局 Domain 列表,自动分发到所有聚合组(有匹配 H2 段则贡献)。
192
+ * - groups:聚合组实例化(H2 = 聚合组名,与 Blueprint 同名),其下 ### Domains 是追加列表。
193
+ * - tagline:身份一句话介绍(选填)。展示路径:
194
+ * - /pt-profile 选择器选项中:`name — tagline`
195
+ * - /pt status:pt tagline 行
196
+ * - footer:`pt: <profile>: <tagline> <state>`
197
+ * 建议 ≤ 30 字符(footer 会截断);超过 80 字符应拆成多行场景描述走 Domain。
198
+ *
199
+ * 项目级,不跨项目复用。
200
+ */
201
+ export interface Profile {
202
+ name: string;
203
+ /** 引用的 Blueprint 名(结构复用)。 */
204
+ blueprint: string;
205
+ /** 全局 Domain 列表,自动分发到所有聚合组。 */
206
+ domains: string[];
207
+ /** 聚合组实例化(与 Blueprint 的 BlueprintGroup 同名)。 */
208
+ groups: ProfileGroup[];
209
+ /**
210
+ * v14.x:身份一句话介绍(选填)。例如:
211
+ * tagline: Senior dev + QA + Reviewer (3 agents)
212
+ * tagline: Product owner + Architect
213
+ *
214
+ * 解析:frontmatter `tagline: <string>` → schema 字段。
215
+ * 展示路径:/pt-profile 选择器、/pt status、footer 三处。
216
+ * back-compat:缺省 = undefined(旧 Profile 不填照样可加载)。
217
+ */
218
+ tagline?: string;
219
+ /**
220
+ * v15.x PR3(§4.4.1):MdFilePack 加载时打上——不限定 ref 自动绑定用。
221
+ * back-compat:未加载的新代码可能缺省(undefined);parseRef 用 `?? ""` 容错后抛
222
+ * "unqualified ref ... but profile has no sourcePack" 错误——保留配置错误可见性。 */
223
+ sourcePack?: string;
224
+ /**
225
+ * v15.x PR5(§5.1):use 单继承——引用另一 Profile 作为基础,递归展开。
226
+ * - 不写 = 完全独立 Profile(back-compat 干净,expandProfile 步骤 4 早退)
227
+ * - 写了 = 增量继承:blueprint 覆盖 / tagline 覆盖 / domains 追加 / groups 替换(§5.2 表)
228
+ * 格式:`@pack/name`(限定)或 `name`(无限定→self.sourcePack/name)
229
+ * 解析:parseRef(PR3a 已实现,含 @prj/@gbl/@pt 别名归一)
230
+ * 循环检测 + 菱形处理:路径 visited(每层 new Set,§5.3.3 M4)
231
+ * 越权校验:use 场景 error(§5.5.1 S7) */
232
+ use?: string;
233
+ }
234
+ /**
235
+ * v9 AgentContext(Phase term-P1 前名 Context):产物层,Profile 编译输出。
236
+ * - sourceHash:hash(Profile + Blueprint + Domains) 组合——任一变化即失效。
237
+ * - modules :聚合组名(语义名)→ 聚合后的 markdown 字符串。
238
+ *
239
+ * AgentContext 是物理文件(.pt/cache/agent-contexts/*.agent-context.md),缓存复用。
240
+ * Pt 读取 AgentContext 时比 sourceHash:一致用缓存,不一致重编译覆盖。
241
+ *
242
+ * v9 相对 v8 变化:sourceHash 输入从 (Blueprint + Channel + Domains) 改为 (Profile + Blueprint + Domains)。
243
+ */
244
+ export interface AgentContext {
245
+ /** Profile 名(AgentContext 跟 Profile 一对一)。 */
246
+ name: string;
247
+ /** Blueprint 名(AgentContext 来源 Blueprint,缓存标识 + YAML 头)。 */
248
+ blueprint: string;
249
+ /** hash(profile + blueprint + domains + packs),缓存失效依据。 */
250
+ sourceHash: string;
251
+ /** 聚合组名(语义名)→ 聚合后的 markdown 字符串。 */
252
+ modules: Record<string, string>;
253
+ /** v15.x PR2(§8.3):cache 文件名 <pack>__<profile> 用——所属 pack name。
254
+ * PR3 接通 @pack/name 后改为 Profile.sourcePack;PR2 用 pack name 字符串。 */
255
+ packName: string;
256
+ }
257
+ /**
258
+ * v9 SchemaBundle:编译期内存态,包含所有加载的 IR。
259
+ * - domains:所有 Domain(按 type 区分承载内容)。
260
+ * - blueprints:所有 Blueprint(结构层模块,Agent 端)。
261
+ * - profiles:所有 Profile(配置层模块,业务端)。
262
+ * - activeProfile:当前激活的 Profile 名,决定产物走哪个组合。
263
+ *
264
+ * (v8 的 channels 字段删除——Channel 保留为未来 Connector,本版本不实现)
265
+ */
266
+ /** v15.x §4.4.2(双层语义):三类 asset 的 working set 通用双索引结构。
267
+ * - location:按位置 alias(prj/gbl/pt)——reserved pack(project/global/builtin)才有;settings pack 不进
268
+ * - identity:按 pack.name(manifest.name 或退化别名)——所有 pack 都进
269
+ * 双索引保证:
270
+ * - @prj/foo(位置 alias)走 location 索引
271
+ * - @<manifest-name>/foo(身份 alias)走 identity 索引
272
+ * - 不限定 `foo` 走 identity 索引(self.sourcePack 是 manifest.name)
273
+ * back-compat(缺口 1-b):reserved pack 无 manifest 时 pack.name=位置别名,identity 与 location 索引 key 重合——双入口命中同一 asset。 */
274
+ export interface WorkingSet<T> {
275
+ location: Map<string, {
276
+ pack: AssetPack;
277
+ asset: T;
278
+ }>;
279
+ identity: Map<string, {
280
+ pack: AssetPack;
281
+ asset: T;
282
+ }>;
283
+ }
284
+ export interface SchemaBundle {
285
+ domains: Domain[];
286
+ blueprints: Blueprint[];
287
+ profiles: Profile[];
288
+ /** 当前激活的 Profile 名。 */
289
+ activeProfile: string;
290
+ /** v15.x PR6(fix pt-active-profile-fallback-mismatch):activeProfile 由来——
291
+ * - "exact":用户请求的 profile 名在某个 pack 找到
292
+ * - "fallback":未找到,fallback 到 allProfiles[0]
293
+ * caller 层依据 origin 决定是否同步改写 s.activeProfile / loadedFrom / notify。 */
294
+ activeProfileOrigin: "exact" | "fallback";
295
+ /** 当 origin="fallback":记录用户原始请求的 profile 名(用于 notify 文案 / log / debug)。
296
+ * origin="exact" 时不设。 */
297
+ originalProfileName?: string;
298
+ /** v15.x PR2(§8.1):加载的所有 AssetPack——sourceHash + cache 文件名用。 */
299
+ packs: AssetPack[];
300
+ /** v15.x PR2(§8.3):激活 Profile 所属的 pack name(cache 文件名 <pack>__<profile> 用)。
301
+ * PR3 接通 @pack/name 后改为 Profile.sourcePack;PR2 用 pack name 字符串。 */
302
+ activeProfilePack: string;
303
+ /** v15.x §4.4.2(双层语义):三类 asset 的 working set,dedup 下推到引用层。
304
+ * 双索引:
305
+ * - location:按位置 alias(prj/gbl/pt)——reserved pack 才有,非 reserved 不进
306
+ * - identity:按 pack.name(manifest.name 或退化别名)——所有 pack 都进
307
+ * 场景 G(@prj/foo + @pt-internal/foo 命中同一 asset)依赖双索引。 */
308
+ workingSet: {
309
+ domains: WorkingSet<Domain>;
310
+ blueprints: WorkingSet<Blueprint>;
311
+ profiles: WorkingSet<Profile>;
312
+ };
313
+ }
314
+ /** Adapter load 上下文(v9.1: 传 notify 上去代替 console.error,符合 pt-quality #9)。
315
+ * v10.x:增 assetDir 让测试夹具可指向 tests/fixtures/assets 而不污染 .pt/assets/。
316
+ * v10.x:增 log 让 adapter 把 trace 持久化到 .pt/logs/pt.log(PtLogger 提供)。
317
+ * v15.x PR1:增 globalPackDir 全局 Pack 路径覆盖(§6.4.1 测试逃逸口)。 */
318
+ export interface SourceAdapterContext {
319
+ /** 错误/警告通知回调(可选;不传则走 console fallback)。 */
320
+ notify?: (msg: string, level: "warning" | "error") => void;
321
+ /** 资产根目录覆盖(默认 `.pt/assets`)。测试夹具可传 `tests/fixtures/assets`。 */
322
+ assetDir?: string;
323
+ /** 持久化日志 writer(可选;不传则不写盘,仅走 notify)。
324
+ * 形态参考 PtLogger.toWriter()——debug/info/warn/error 四方法。
325
+ * adapter 拿到后只需按级别调用,无需关心文件路径。 */
326
+ log?: {
327
+ debug(msg: string, ctx?: Record<string, unknown>): void;
328
+ info(msg: string, ctx?: Record<string, unknown>): void;
329
+ warn(msg: string, ctx?: Record<string, unknown>): void;
330
+ error(msg: string, ctx?: Record<string, unknown>): void;
331
+ };
332
+ /** v15.x PR1(§6.4.1):全局 Pack 路径覆盖。默认 `~/.pt/assets/`;
333
+ * 测试可传临时目录(如 `/tmp/pt-test-empty`)验证"全局 pack 不存在"场景。 */
334
+ globalPackDir?: string;
335
+ }
336
+ /** 反转后的 SourceAdapter:load() 返回 SchemaBundle 而非 string。
337
+ * Pt 核心只认 SchemaBundle,不认任何来源格式。
338
+ * v9:参数是 profileName(用户面是 Profile,不是 Blueprint)。
339
+ * v9.1:增 adapterCtx 参数(可选)——adapter 可选传 notify 代替 console。 */
340
+ export interface SourceAdapter {
341
+ name: string;
342
+ load(cwd: string, profileName: string, adapterCtx?: SourceAdapterContext): Promise<SchemaBundle>;
343
+ }
344
+ /** Pack 来源类型(诊断显示用,pt-asset-pack.md §2.1)。 */
345
+ export type PackSource = "project" | "settings" | "global" | "builtin";
346
+ /**
347
+ * v15.x PR1:AssetPack 是单个资产来源的抽象。
348
+ * mdAdapter.load() 内部构造 AssetPack[],依次 load → 合并 SchemaBundle。
349
+ *
350
+ * PR1 范围:name 解析走简化版(reserved 固定名 / basename 兜底),
351
+ * manifest 字段(version/description)全空——manifest 解析是 PR2。
352
+ * settings pack 加载走 stub 返空数组(PR4 接通)。
353
+ */
354
+ export interface AssetPack {
355
+ /** Pack 身份(PR1:reserved 固定名 "prj"/"gbl"/"pt",或目录 basename 兜底) */
356
+ readonly name: string;
357
+ /** Pack 版本(PR1:固定 "0.0.0";PR2 从 manifest 读) */
358
+ readonly version: string;
359
+ /** Pack 根目录绝对路径 */
360
+ readonly rootDir: string;
361
+ /** manifest 描述(PR1:undefined;PR2 从 manifest 读) */
362
+ readonly description?: string;
363
+ /** Pack 来源类型(诊断显示用) */
364
+ readonly source: PackSource;
365
+ /** 加载本 Pack 的所有 Domain */
366
+ loadDomains(): Promise<Domain[]>;
367
+ /** 加载本 Pack 的所有 Blueprint */
368
+ loadBlueprints(): Promise<Blueprint[]>;
369
+ /** 加载本 Pack 的所有 Profile */
370
+ loadProfiles(): Promise<Profile[]>;
371
+ }
372
+ /** Agent UI 能力(可选,adapter 按需用)。 */
373
+ export interface AgentUI {
374
+ notify(msg: string, level: "info" | "warning" | "error"): void;
375
+ setStatus(name: string, text: string): void;
376
+ }
377
+ /** Agent 注入 API 的最小接口(AgentAdapter 用,不直接依赖 Pi ExtensionAPI)。
378
+ * v9.1 (P1.1):加 ui 可选能力——adapter 可报错 / 设状态,不必。
379
+ * v10.x:加 log 可选能力——adapter 内的 try/catch 异常可走 logger。
380
+ * 注:log 接口与 SourceAdapterContext.log 同形,便于 toAgentAPI 复用同一个 logger。 */
381
+ export interface AgentAPI {
382
+ on(event: string, handler: (...args: unknown[]) => unknown): void;
383
+ registerCommand(name: string, spec: unknown): void;
384
+ registerFlag(name: string, spec: unknown): void;
385
+ getFlag(name: string): unknown;
386
+ /** v9.1 可选 UI:index.ts 传入 Pi ctx.ui 适配后的对象。Adapter 可选。 */
387
+ ui?: AgentUI;
388
+ /** v10.x 可选 log:per-session PtLogger 适配。Adapter 可选。 */
389
+ log?: {
390
+ debug(msg: string, ctx?: Record<string, unknown>): void;
391
+ info(msg: string, ctx?: Record<string, unknown>): void;
392
+ warn(msg: string, ctx?: Record<string, unknown>): void;
393
+ error(msg: string, ctx?: Record<string, unknown>): void;
394
+ };
395
+ /** Adapter 可选回调:完成一次 system prompt 注入后通知编排层更新观测状态。 */
396
+ onInjected?: (systemPrompt: string) => void;
397
+ }
398
+ /** Agent 适配器——适配不同 Agent 的注入机制。
399
+ * Pt 核心调 Adapter 接口,不直接调 Agent API。加新 Agent 只加 Adapter。 */
400
+ export interface AgentAdapter {
401
+ /** Agent 名(pi / codex / opencode / ...) */
402
+ name: string;
403
+ /** 该 Agent 支持的技术注入 API 名(Pi: system_prompt, context_message)——
404
+ * 注意:这是 AgentAdapter 映射边界。Blueprint 用 session/turn 语义值,Adapter 内部映射到此字段声明的 Pi API 名。 */
405
+ supportedTargets: string[];
406
+ /** 设置编译产物(compile 后调)。
407
+ * v13.x(issue pt-turn-inject-not-profile-scoped):加 profile 参数——Adapter 持有 profile
408
+ * 后,turn 路径(renderTurnInject / listManuals)内部统一按 Profile scope 过滤 domains,
409
+ * 消除"触发用全集 / 列表预过滤"的双轨。 */
410
+ setAgentContext(ctx: AgentContext, blueprint: Blueprint, domains: Domain[], profile: Profile): void;
411
+ /** 启动时注册:把 AgentContext 注入到 Agent(session_start 调用)。
412
+ * v13.x(issue pt-turn-inject-not-profile-scoped):加 profile 参数——与 setAgentContext 对齐,
413
+ * Adapter 持有 profile 后 turn 路径自过滤。 */
414
+ registerInject(api: AgentAPI, ctx: AgentContext, blueprint: Blueprint, domains?: Domain[], profile?: Profile | null): void;
415
+ /** 清理 session 上下文;handler 仍可由当前 Pi runtime 复用。 */
416
+ resetInjection?(): void;
417
+ /** 查询可用手册(/pt flows 命令 + /manual:xxx 触发 共同消费)。
418
+ *
419
+ * 参数语义:
420
+ * - `ctx`:当前激活的 AgentContext IR(含缓存 sourceHash / 各聚合组 modules 内容)
421
+ * - `blueprint`:当前 Profile 引用的 Blueprint(遍历 groups 找 inject=turn 聚合组)
422
+ * - `domains`:Profile 引用的 Domain 全集——Adapter 内部用 setAgentContext 时存下的
423
+ * profile 调 filterDomainsByProfile 自行过滤(issue pt-turn-inject-not-profile-scoped 修复),
424
+ * 调用方无需预过滤
425
+ *
426
+ * 返回值:可触发手册列表。每项含 name(FlowTemplate.name / Rule.name)+ hint(argumentHint)+ domain(来源 Domain)。
427
+ * - term-Domain 的 Rule[] 也作为 /manual:<domain> 暴露
428
+ * - workflow-Domain 的 FlowTemplate[] 作为 /<flow-name> 暴露
429
+ *
430
+ * 可选方法——Adapter 不实现时 /pt flows 返空。 */
431
+ listManuals?(ctx: AgentContext, blueprint: Blueprint, domains: Domain[]): Array<{
432
+ name: string;
433
+ hint?: string;
434
+ domain: string;
435
+ }>;
436
+ }
437
+ /** 从 Blueprint 列表里找指定名的 Blueprint。未找到返 undefined。 */
438
+ export declare function findBlueprint(blueprints: Blueprint[], name: string): Blueprint | undefined;
439
+ /** 从 Profile 列表里找指定名的 Profile。未找到返 undefined。 */
440
+ export declare function findProfile(profiles: Profile[], name: string): Profile | undefined;
441
+ /** 按 Profile 引用范围过滤 domains(turn 路径 scope 过滤用)。
442
+ * 规则:domain 在 Profile YAML 全局 domains 列表 或 任一聚合组 ProfileGroup.domains 追加列表中 → 保留。
443
+ * v13.x(issue pt-turn-inject-not-profile-scoped):从 commands.ts 挪到 schema.ts——
444
+ * render 层(turn-inject)需要反向依赖它,放 schema.ts 避免层次倒挂。commands.ts 改 import。
445
+ * profile 为 null 时返 domains 原样(向后兼容——无激活 Profile 时不限制)。 */
446
+ export declare function filterDomainsByProfile<T extends {
447
+ name: string;
448
+ }>(domains: T[], profile: Profile | null): T[];
package/dist/schema.js ADDED
@@ -0,0 +1,45 @@
1
+ // src/schema.ts — v9 IR 契约
2
+ //
3
+ // 设计原则(见 .pt/docs/designs/pt-asset-layering.md §0):
4
+ // 1. Schema 是语义化的,不带任何格式痕迹(无 Section/Item/raw/heading)
5
+ // 2. Pt 定义契约,来源(OXN/YAML/...)实现 SourceAdapter
6
+ // 3. 三段式编译架构:parse(前端)→ compile(中端)→ render(后端)
7
+ //
8
+ // Phase 9.2:v9 四层模型重写(Blueprint 吸收 v8 Channel 结构 + 新增 Profile 配置层 + AgentAdapter 抽象)。
9
+ //
10
+ // 四层语义:
11
+ // - Domain :内容层。异构领域知识,按 H2 切模块(Scene/Trigger/Manual/...),Type 标签区分内容性质。
12
+ // - Blueprint :结构层。Agent 端聚合组结构(YAML groups 项),声明聚合组 inject/Modules/mode。
13
+ // - Profile :配置层。业务端实例,引用 Blueprint + 选 Domains(YAML 全局 + 聚合组追加)。
14
+ // - AgentContext :产物层。Profile 编译输出,按聚合组聚合多 Domain 内容,物理文件 + hash 缓存。
15
+ // (Channel 保留为未来 Connector,预留层不实现)
16
+ //
17
+ // v9 相对 v8 的核心变化:
18
+ // - Blueprint 吸收 v8 Channel 的 groups(结构层职责从 Channel 迁到 Blueprint)
19
+ // - v8 Blueprint 的配置层职责 → Profile(用户面是 Profile)
20
+ // - ProfileGroup 删 trigger/boundaries 字段(Boundaries 丢弃;Trigger 移到 Domain H2 段)
21
+ // - 新增 AgentAdapter 接口(Pt 核心不感知 Agent API)
22
+ // - SchemaBundle 加 profiles/activeProfile,去 channels
23
+ // ==================== v9:Blueprint/Profile 名解析辅助 ====================
24
+ /** 从 Blueprint 列表里找指定名的 Blueprint。未找到返 undefined。 */
25
+ export function findBlueprint(blueprints, name) {
26
+ return blueprints.find((b) => b.name === name);
27
+ }
28
+ /** 从 Profile 列表里找指定名的 Profile。未找到返 undefined。 */
29
+ export function findProfile(profiles, name) {
30
+ return profiles.find((p) => p.name === name);
31
+ }
32
+ /** 按 Profile 引用范围过滤 domains(turn 路径 scope 过滤用)。
33
+ * 规则:domain 在 Profile YAML 全局 domains 列表 或 任一聚合组 ProfileGroup.domains 追加列表中 → 保留。
34
+ * v13.x(issue pt-turn-inject-not-profile-scoped):从 commands.ts 挪到 schema.ts——
35
+ * render 层(turn-inject)需要反向依赖它,放 schema.ts 避免层次倒挂。commands.ts 改 import。
36
+ * profile 为 null 时返 domains 原样(向后兼容——无激活 Profile 时不限制)。 */
37
+ export function filterDomainsByProfile(domains, profile) {
38
+ if (!profile)
39
+ return domains;
40
+ return domains.filter((d) => {
41
+ if (profile.domains.includes(d.name))
42
+ return true;
43
+ return profile.groups.some((g) => g.domains.includes(d.name));
44
+ });
45
+ }
@@ -0,0 +1,82 @@
1
+ import type { AssetHealthIssue } from "./asset-health.js";
2
+ import type { ValidationResult } from "./asset-pack/validate.js";
3
+ import type { AgentAdapter, AgentContext, Blueprint, Domain, Profile, SchemaBundle } from "./schema.js";
4
+ import type { PtLogger } from "./log.js";
5
+ import type { ManualProgress } from "./manual-track.js";
6
+ /** activeProfile 的来源(session_start fallback 命中点)。 */
7
+ export type ProfileLoadSource = "flag" | "settings" | "session" | "auto" | "default" | "fallback" | null;
8
+ /** pt 注入到 Session Inject 的状态(自报,非检测 Pi)。
9
+ * - idle:未激活 / 无 segment
10
+ * - pending:Profile 已加载但还没轮到下一轮 before_agent_start
11
+ * - injected:本轮 before_agent_start 成功返回注入结果
12
+ * - failed:本轮 before_agent_start catch 异常 */
13
+ export type InjectionState = "idle" | "pending" | "injected" | "failed";
14
+ /** 当前追踪的 Manual 实例(LLM 调 pt_manual 写入后触发)。
15
+ * 进度(stepDone/stepTotal)不存 session——文件是 single source of truth,
16
+ * 每次 widget 渲染时 parse 文件重新计算。 */
17
+ export interface ActiveManual {
18
+ filePath: string;
19
+ procedure: string;
20
+ args: string;
21
+ activatedAt: number;
22
+ }
23
+ /** Session 全量状态。 */
24
+ export interface SessionState {
25
+ activeProfile: string | null;
26
+ cachedSegment: string | null;
27
+ cachedBundles: SchemaBundle[] | null;
28
+ cachedAgentContext: AgentContext | null;
29
+ cachedBlueprint: Blueprint | null;
30
+ cachedDomains: Domain[];
31
+ cachedProfile: Profile | null;
32
+ lastCwd: string;
33
+ lastBuiltPrompt: string | null;
34
+ lastCacheHit: boolean;
35
+ activeAdapter: AgentAdapter | null;
36
+ /** v10.x:session 唯一短 id,8 hex(4.3B 组合空间,足够区分并发 pi 进程)。 */
37
+ sessionId: string;
38
+ /** v10.x:per-session 单例 logger(替代 per-transpile 实例化)。 */
39
+ logger: PtLogger | null;
40
+ /** v10.x:当前 activeProfile 的来源(可观测性)。null 表示未加载。 */
41
+ loadedFrom: ProfileLoadSource;
42
+ /** pt 注入到 Session Inject 的状态(footer 三态文字 + widget 依据)。 */
43
+ injectionState: InjectionState;
44
+ /** failed 时存错误消息(footer 追加)。其它状态 null。 */
45
+ injectionError: string | null;
46
+ /** 当前追踪的 Manual 实例(widget + footer 后缀 + 持久化恢复)。 */
47
+ activeManual: ActiveManual | null;
48
+ /** v12.x:当前 manual widget 的 async parse 缓存(替代原 module-level `cachedManualProgress`)。 */
49
+ cachedManualProgress: ManualProgress | null;
50
+ /** v14.x(issue pt-asset-migration-visibility Layer 2):
51
+ * session_start 批量体检结果——footer 追加 ⚠ N issues + /pt status 暴露。
52
+ * null = 未扫描(用户加载内置 profile 后才扫描过项目 profile)。 */
53
+ assetHealthIssues: AssetHealthIssue[] | null;
54
+ /** v15.x PR1(§6.7.1):session_start 一次性 pack 校验结果——/pt status 展示用。 */
55
+ packValidation: ValidationResult[] | null;
56
+ /** v15.x PR1(§6.7.3):project pack 校验失败时标 true,transpileActive 强制回 guide。 */
57
+ projectPackDegraded: boolean;
58
+ /** v15.x PR1(§7.5):全局 Pack 初始化引导一次性提示标记。 */
59
+ globalPackGuideShown: boolean;
60
+ }
61
+ /** 默认空 SessionState。 */
62
+ export declare function createSessionState(): SessionState;
63
+ /** 重置编译产物字段(catch 路径用)。
64
+ * v13.x(issue pt-no-agent-context-reset-session-state 修复):
65
+ * - transpile/session_start/switchProfile 三处 catch 调用本函数
66
+ * - 避免 stale cachedAgentContext/cachedBlueprint/cachedDomains/cachedProfile/activeAdapter
67
+ * 误导 /pt flows / /pt manual 返回旧 Profile 的手册列表(掩盖真实失败)
68
+ * - 不清 sessionId/logger/lastCwd/activeProfile/loadedFrom/activeManual(生命周期不同:
69
+ * session_id 标识当前会话、logger 持续写日志、lastCwd 是项目根、activeProfile 是用户意图、
70
+ * loadedFrom 是来源、activeManual 是手动追踪)
71
+ */
72
+ export declare function resetSessionState(s: SessionState): void;
73
+ /** 取指定 sessionId 的 state(lazy create)。TUI 模式下只有一个 entry。 */
74
+ export declare function getSessionById(sessionId: string): SessionState;
75
+ /** 列表所有 session state(debug / 诊断用,如 /pt sessions 列表)。 */
76
+ export declare function listAllSessions(): SessionState[];
77
+ /** 清空指定 sessionId 的 state(session_shutdown 调用)。 */
78
+ export declare function clearSessionById(sessionId: string): void;
79
+ /** 清空所有 session state(仅测试 / 重启进程等需要)。 */
80
+ export declare function clearAllSessions(): void;
81
+ /** 测试 / 诊断:Map 的当前大小。 */
82
+ export declare function sessionCount(): number;