pi-harness-runtime 1.1.150 → 1.1.152

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 (158) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/harness/key-loader.ts +84 -0
  3. package/harness/langchain/agents.ts +65 -15
  4. package/harness/langchain/daemon.ts +58 -30
  5. package/harness/langchain/graph.ts +55 -7
  6. package/harness/langchain/jev-logger.ts +274 -0
  7. package/harness/langchain/run.ts +64 -161
  8. package/harness/langchain/surge.test.ts +42 -0
  9. package/harness/langchain/surge.ts +22 -1
  10. package/index.ts +24 -0
  11. package/package.json +1 -1
  12. package/packages/a2a-adapter/package.json +1 -1
  13. package/packages/agent-policy/dist/extension.d.ts +19 -0
  14. package/packages/agent-policy/dist/extension.d.ts.map +1 -0
  15. package/packages/agent-policy/dist/extension.js +322 -0
  16. package/packages/agent-policy/dist/extension.js.map +1 -0
  17. package/packages/agent-policy/dist/index.d.ts +25 -0
  18. package/packages/agent-policy/dist/index.d.ts.map +1 -0
  19. package/packages/agent-policy/dist/index.js +27 -0
  20. package/packages/agent-policy/dist/index.js.map +1 -0
  21. package/packages/agent-policy/dist/policy-loader.d.ts +90 -0
  22. package/packages/agent-policy/dist/policy-loader.d.ts.map +1 -0
  23. package/packages/agent-policy/dist/policy-loader.js +331 -0
  24. package/packages/agent-policy/dist/policy-loader.js.map +1 -0
  25. package/packages/agent-policy/dist/policy-renderer.d.ts +42 -0
  26. package/packages/agent-policy/dist/policy-renderer.d.ts.map +1 -0
  27. package/packages/agent-policy/dist/policy-renderer.js +295 -0
  28. package/packages/agent-policy/dist/policy-renderer.js.map +1 -0
  29. package/packages/agent-policy/dist/policy-store.d.ts +99 -0
  30. package/packages/agent-policy/dist/policy-store.d.ts.map +1 -0
  31. package/packages/agent-policy/dist/policy-store.js +244 -0
  32. package/packages/agent-policy/dist/policy-store.js.map +1 -0
  33. package/packages/agent-policy/dist/types.d.ts +237 -0
  34. package/packages/agent-policy/dist/types.d.ts.map +1 -0
  35. package/packages/agent-policy/dist/types.js +130 -0
  36. package/packages/agent-policy/dist/types.js.map +1 -0
  37. package/packages/agent-policy/package.json +45 -0
  38. package/packages/agent-policy/src/extension.ts +413 -0
  39. package/packages/agent-policy/src/index.ts +52 -0
  40. package/packages/agent-policy/src/policy-loader.ts +398 -0
  41. package/packages/agent-policy/src/policy-renderer.ts +362 -0
  42. package/packages/agent-policy/src/policy-store.ts +319 -0
  43. package/packages/agent-policy/src/types.ts +385 -0
  44. package/packages/agent-policy/test/policy.test.ts +383 -0
  45. package/packages/architecture-generator/package.json +1 -1
  46. package/packages/auth/package.json +1 -1
  47. package/packages/autonomous-refactor/package.json +1 -1
  48. package/packages/autonomous-runtime/package.json +1 -1
  49. package/packages/cache-strategy/package.json +1 -1
  50. package/packages/capability-registry/package.json +1 -1
  51. package/packages/checkpoint/package.json +1 -1
  52. package/packages/cli-plugin-sdk/package.json +1 -1
  53. package/packages/clipboard/package.json +1 -1
  54. package/packages/clipboard-plugin/package.json +1 -1
  55. package/packages/code-generation/package.json +1 -1
  56. package/packages/code-review/package.json +1 -1
  57. package/packages/codex-adapter/package.json +1 -1
  58. package/packages/codex-watcher/dist/src/parser.d.ts.map +1 -1
  59. package/packages/codex-watcher/dist/src/parser.js +35 -0
  60. package/packages/codex-watcher/dist/src/parser.js.map +1 -1
  61. package/packages/codex-watcher/package.json +1 -1
  62. package/packages/codex-watcher/src/parser.ts +41 -0
  63. package/packages/config-capture/package.json +1 -1
  64. package/packages/context-compiler/package.json +1 -1
  65. package/packages/context-discovery/package.json +1 -1
  66. package/packages/context-manager/package.json +1 -1
  67. package/packages/cookie-sanitizer/package.json +1 -1
  68. package/packages/cost-optimizer/package.json +1 -1
  69. package/packages/dependency-analyzer/package.json +1 -1
  70. package/packages/django-plugin/package.json +1 -1
  71. package/packages/doc-generator/package.json +1 -1
  72. package/packages/evaluation-engine/package.json +1 -1
  73. package/packages/evaluation-runner/package.json +1 -1
  74. package/packages/event-bus/package.json +1 -1
  75. package/packages/event-store/package.json +1 -1
  76. package/packages/experience-replay/package.json +1 -1
  77. package/packages/feedback-collector/package.json +1 -1
  78. package/packages/file-copy-helper/package.json +1 -1
  79. package/packages/framework-detector/package.json +1 -1
  80. package/packages/framework-plugin-sdk/package.json +1 -1
  81. package/packages/frappe-plugin/package.json +1 -1
  82. package/packages/generic-web-plugin/package.json +1 -1
  83. package/packages/health-monitor/package.json +1 -1
  84. package/packages/intent-analyzer/package.json +1 -1
  85. package/packages/jev-judge/package.json +1 -1
  86. package/packages/knowledge-graph/package.json +1 -1
  87. package/packages/knowledge-retrieval/package.json +1 -1
  88. package/packages/laravel-plugin/package.json +1 -1
  89. package/packages/learning-engine/package.json +1 -1
  90. package/packages/mcp-adapter/package.json +1 -1
  91. package/packages/memory-engine/package.json +1 -1
  92. package/packages/milestone-manager/package.json +1 -1
  93. package/packages/model-registry/package.json +1 -1
  94. package/packages/nextjs-plugin/package.json +1 -1
  95. package/packages/notification/dist/adapters/telegram-adapter.d.ts +5 -0
  96. package/packages/notification/dist/adapters/telegram-adapter.d.ts.map +1 -1
  97. package/packages/notification/dist/adapters/telegram-adapter.js +54 -0
  98. package/packages/notification/dist/adapters/telegram-adapter.js.map +1 -1
  99. package/packages/notification/dist/notification-center.d.ts +13 -0
  100. package/packages/notification/dist/notification-center.d.ts.map +1 -1
  101. package/packages/notification/dist/notification-center.js +36 -0
  102. package/packages/notification/dist/notification-center.js.map +1 -1
  103. package/packages/notification/package.json +1 -1
  104. package/packages/notification/src/adapters/telegram-adapter.ts +71 -0
  105. package/packages/notification/src/notification-center.ts +53 -0
  106. package/packages/observability/package.json +1 -1
  107. package/packages/okf-indexer/package.json +1 -1
  108. package/packages/paperclip-integration/package.json +1 -1
  109. package/packages/performance-optimizer/package.json +1 -1
  110. package/packages/privilege-broker/package.json +1 -1
  111. package/packages/project-analyzer/package.json +1 -1
  112. package/packages/project-bootstrap/package.json +1 -1
  113. package/packages/projection-engine/package.json +1 -1
  114. package/packages/prompt-compiler/package.json +1 -1
  115. package/packages/prompt-versioning/package.json +1 -1
  116. package/packages/provider-adapter-sdk/package.json +1 -1
  117. package/packages/provider-router/package.json +1 -1
  118. package/packages/provider-selector/package.json +1 -1
  119. package/packages/providers/package.json +1 -1
  120. package/packages/qdrant-skills/package.json +1 -1
  121. package/packages/quota-manager/package.json +1 -1
  122. package/packages/rate-limiter/package.json +1 -1
  123. package/packages/react-vite-plugin/package.json +1 -1
  124. package/packages/release-manager/package.json +1 -1
  125. package/packages/requirement-compiler/package.json +1 -1
  126. package/packages/runtime/package.json +1 -1
  127. package/packages/scheduler/package.json +1 -1
  128. package/packages/scheduler-adapter/package.json +1 -1
  129. package/packages/session/package.json +1 -1
  130. package/packages/session-api/package.json +1 -1
  131. package/packages/session-export/package.json +1 -1
  132. package/packages/shared-context/package.json +1 -1
  133. package/packages/skill-mcp-client/package.json +1 -1
  134. package/packages/skill-registry/package.json +1 -1
  135. package/packages/skills/package.json +1 -1
  136. package/packages/sprint-planner/package.json +1 -1
  137. package/packages/subscription-engine/package.json +1 -1
  138. package/packages/task-compiler/package.json +1 -1
  139. package/packages/tencentdb-memory/package.json +1 -1
  140. package/packages/tencentdb-sync/package.json +1 -1
  141. package/packages/test-data-generator/package.json +1 -1
  142. package/packages/test-generator/package.json +1 -1
  143. package/packages/todo-bd-sync/package.json +1 -1
  144. package/packages/token-estimation/package.json +1 -1
  145. package/packages/token-optimizer/package.json +1 -1
  146. package/packages/trajectory/package.json +1 -1
  147. package/packages/tui/package.json +1 -1
  148. package/packages/types/package.json +1 -1
  149. package/packages/workflow-events/package.json +1 -1
  150. package/packages/workspace-scanner/package.json +1 -1
  151. package/packages/worktree/package.json +1 -1
  152. package/packages/write-review/package.json +1 -1
  153. package/proactive-compact.ts +2 -2
  154. package/scripts/debug-codex-integration.ts +203 -0
  155. package/scripts/sync-keys-from-pi.ts +188 -0
  156. package/scripts/test-codex-session.ts +23 -0
  157. package/scripts/test-openai.ts +14 -0
  158. package/scripts/test-telegram.ts +34 -0
@@ -0,0 +1,398 @@
1
+ /**
2
+ * PolicyLoader - Loads and hashes harness and scoped project rules.
3
+ *
4
+ * Responsibilities:
5
+ * - Load the immutable harness policy shipped with the package
6
+ * - Walk from project root toward target file collecting scoped AGENTS.md
7
+ * - Normalize newlines and paths, then hash the ordered mandatory content
8
+ * - Build a PolicyManifest with source paths, scope, priority, and hashes
9
+ */
10
+
11
+ import { createHash } from "node:crypto";
12
+ import { existsSync, readFileSync } from "node:fs";
13
+ import { dirname, join, relative, resolve } from "node:path";
14
+ import type { PolicyManifest, PolicySource } from "./types.js";
15
+
16
+ // ---------------------------------------------------------------------------
17
+ // Constants
18
+ // ---------------------------------------------------------------------------
19
+
20
+ /**
21
+ * Supported rule file names in priority order.
22
+ * Earlier names have higher priority.
23
+ */
24
+ const RULE_FILE_NAMES = [
25
+ "AGENTS.md", // Mandatory, harness or project
26
+ "RULES.md", // Advisory
27
+ "PROJECT_RULES.md", // Advisory
28
+ "CLAUDE.md", // Advisory
29
+ "CONTRIBUTING.md", // Advisory (only if explicitly trusted)
30
+ ] as const;
31
+
32
+ /**
33
+ * Maximum walk depth to prevent infinite loops.
34
+ */
35
+ const MAX_WALK_DEPTH = 20;
36
+
37
+ /**
38
+ * Files that indicate project trust.
39
+ */
40
+ const TRUST_INDICATORS = [".git", "package.json", "Cargo.toml", "go.mod"] as const;
41
+
42
+ // ---------------------------------------------------------------------------
43
+ // PolicyLoader
44
+ // ---------------------------------------------------------------------------
45
+
46
+ /**
47
+ * Policy loader configuration.
48
+ */
49
+ export interface PolicyLoaderConfig {
50
+ /** Path to the harness-owned AGENTS.md */
51
+ harnessRulesPath: URL;
52
+ /** Optional additional harness rule files */
53
+ additionalRulesPaths?: URL[];
54
+ }
55
+
56
+ /**
57
+ * Loads and processes agent policies from harness and project sources.
58
+ */
59
+ export class PolicyLoader {
60
+ private harnessRulesPath: URL;
61
+ private additionalRulesPaths: URL[];
62
+ private cache: Map<string, { manifest: PolicyManifest; cachedAt: number }> = new Map();
63
+ private readonly CACHE_TTL_MS = 5000; // 5 second cache for same-cwd loads
64
+
65
+ constructor(config: PolicyLoaderConfig) {
66
+ this.harnessRulesPath = config.harnessRulesPath;
67
+ this.additionalRulesPaths = config.additionalRulesPaths ?? [];
68
+ }
69
+
70
+ /**
71
+ * Load policy for a given project root.
72
+ */
73
+ async load(projectRoot: string): Promise<PolicyManifest> {
74
+ const cacheKey = this.getCacheKey(projectRoot);
75
+ const cached = this.cache.get(cacheKey);
76
+
77
+ if (cached && Date.now() - cached.cachedAt < this.CACHE_TTL_MS) {
78
+ return cached.manifest;
79
+ }
80
+
81
+ const manifest = await this.buildManifest(projectRoot);
82
+ this.cache.set(cacheKey, { manifest, cachedAt: Date.now() });
83
+ return manifest;
84
+ }
85
+
86
+ /**
87
+ * Load policy for a specific target file (considers scoped rules).
88
+ */
89
+ async loadForTarget(projectRoot: string, targetPath?: string): Promise<PolicyManifest> {
90
+ if (!targetPath) {
91
+ return this.load(projectRoot);
92
+ }
93
+
94
+ // Check if target is in a specific scope
95
+ const relativePath = relative(projectRoot, targetPath);
96
+ const scope = this.inferScope(relativePath);
97
+
98
+ const manifest = await this.load(projectRoot);
99
+
100
+ // Filter sources to only those applicable to this scope
101
+ if (scope) {
102
+ manifest.sources = manifest.sources.filter(s =>
103
+ s.scope === "" || scope.startsWith(s.scope) || s.scope.startsWith(scope)
104
+ );
105
+ // Recompute revision if sources changed
106
+ if (manifest.sources.length > 0) {
107
+ manifest.revision = this.computeRevision(manifest.sources);
108
+ }
109
+ }
110
+
111
+ return manifest;
112
+ }
113
+
114
+ /**
115
+ * Invalidate cache for a project root.
116
+ */
117
+ invalidateCache(projectRoot?: string): void {
118
+ if (projectRoot) {
119
+ this.cache.delete(this.getCacheKey(projectRoot));
120
+ } else {
121
+ this.cache.clear();
122
+ }
123
+ }
124
+
125
+ /**
126
+ * Build manifest for a project root.
127
+ */
128
+ private async buildManifest(projectRoot: string): Promise<PolicyManifest> {
129
+ const sources: PolicySource[] = [];
130
+ const errors: string[] = [];
131
+
132
+ // 1. Load harness-owned rules first (highest priority)
133
+ const harnessSources = await this.loadHarnessRules();
134
+ sources.push(...harnessSources);
135
+
136
+ // 2. Load project rules (if project is trusted)
137
+ const trust = this.evaluateTrust(projectRoot);
138
+
139
+ if (trust !== "unknown") {
140
+ const projectSources = await this.loadProjectRules(projectRoot);
141
+ sources.push(...projectSources);
142
+ } else {
143
+ // Project not trusted, but we still try to load AGENTS.md as advisory
144
+ const untrustedSources = await this.loadProjectRules(projectRoot, true);
145
+ sources.push(...untrustedSources);
146
+ }
147
+
148
+ // 3. Compute revision from mandatory sources
149
+ const mandatorySources = sources.filter(s => s.priority === "mandatory");
150
+ const revision = this.computeRevision(mandatorySources);
151
+
152
+ // 4. Determine coverage
153
+ const coverage = this.determineCoverage(sources, errors);
154
+
155
+ return {
156
+ revision,
157
+ projectRoot: resolve(projectRoot),
158
+ trust,
159
+ coverage,
160
+ sources,
161
+ loadedAt: Date.now(),
162
+ loadError: errors.length > 0 ? errors.join("; ") : undefined,
163
+ };
164
+ }
165
+
166
+ /**
167
+ * Load harness-owned rule files.
168
+ */
169
+ private async loadHarnessRules(): Promise<PolicySource[]> {
170
+ const sources: PolicySource[] = [];
171
+
172
+ // Load main harness AGENTS.md
173
+ if (existsSync(this.harnessRulesPath)) {
174
+ try {
175
+ const content = readFileSync(this.harnessRulesPath, "utf8");
176
+ const normalized = this.normalizeContent(content);
177
+ const sha256 = this.hashContent(normalized);
178
+
179
+ sources.push({
180
+ path: this.harnessRulesPath.toString(),
181
+ scope: "", // Root scope - applies everywhere
182
+ priority: "mandatory",
183
+ sha256,
184
+ bytes: Buffer.byteLength(content, "utf8"),
185
+ harnessOwned: true,
186
+ });
187
+ } catch (err) {
188
+ throw new Error(`Failed to load harness rules from ${this.harnessRulesPath}: ${err}`);
189
+ }
190
+ } else {
191
+ throw new Error(`Harness rules not found at ${this.harnessRulesPath}`);
192
+ }
193
+
194
+ // Load additional harness rules
195
+ for (const rulesPath of this.additionalRulesPaths) {
196
+ if (existsSync(rulesPath)) {
197
+ try {
198
+ const content = readFileSync(rulesPath, "utf8");
199
+ const normalized = this.normalizeContent(content);
200
+ const sha256 = this.hashContent(normalized);
201
+
202
+ sources.push({
203
+ path: rulesPath.toString(),
204
+ scope: "", // Root scope
205
+ priority: "mandatory",
206
+ sha256,
207
+ bytes: Buffer.byteLength(content, "utf8"),
208
+ harnessOwned: true,
209
+ });
210
+ } catch (err) {
211
+ console.error(`Failed to load additional rules from ${rulesPath}: ${err}`);
212
+ }
213
+ }
214
+ }
215
+
216
+ return sources;
217
+ }
218
+
219
+ /**
220
+ * Load project-owned rule files, walking from root toward target.
221
+ */
222
+ private async loadProjectRules(
223
+ projectRoot: string,
224
+ forceAdvisory = false
225
+ ): Promise<PolicySource[]> {
226
+ const sources: PolicySource[] = [];
227
+ let dir = projectRoot;
228
+ let depth = 0;
229
+
230
+ while (dir && depth < MAX_WALK_DEPTH) {
231
+ for (const fileName of RULE_FILE_NAMES) {
232
+ const filePath = join(dir, fileName);
233
+
234
+ if (existsSync(filePath)) {
235
+ try {
236
+ const content = readFileSync(filePath, "utf8");
237
+ const normalized = this.normalizeContent(content);
238
+ const sha256 = this.hashContent(normalized);
239
+
240
+ // Determine priority and scope
241
+ const isMandatory = fileName === "AGENTS.md" && !forceAdvisory;
242
+ const scope = dir === projectRoot ? "" : "/" + relative(projectRoot, dir);
243
+
244
+ sources.push({
245
+ path: filePath,
246
+ scope,
247
+ priority: isMandatory ? "mandatory" : "advisory",
248
+ sha256,
249
+ bytes: Buffer.byteLength(content, "utf8"),
250
+ harnessOwned: false,
251
+ });
252
+ } catch (err) {
253
+ console.error(`Failed to load project rule ${filePath}: ${err}`);
254
+ }
255
+ }
256
+ }
257
+
258
+ // Move to parent directory
259
+ const parent = dirname(dir);
260
+ if (parent === dir) break;
261
+ dir = parent;
262
+ depth++;
263
+ }
264
+
265
+ return sources;
266
+ }
267
+
268
+ /**
269
+ * Evaluate trust level for a project.
270
+ */
271
+ private evaluateTrust(projectRoot: string): "trusted" | "untrusted" | "unknown" {
272
+ let trustIndicatorsFound = 0;
273
+ let untrustIndicatorsFound = 0;
274
+
275
+ for (const indicator of TRUST_INDICATORS) {
276
+ if (existsSync(join(projectRoot, indicator))) {
277
+ trustIndicatorsFound++;
278
+ }
279
+ }
280
+
281
+ // Check for untrusted indicators (e.g., downloaded zip, unknown origin)
282
+ // For now, presence of .git or package.json means trusted
283
+ if (trustIndicatorsFound > 0) {
284
+ return "trusted";
285
+ }
286
+
287
+ return "unknown";
288
+ }
289
+
290
+ /**
291
+ * Infer the scope from a relative path.
292
+ */
293
+ private inferScope(relativePath: string): string {
294
+ // Extract directory component
295
+ const segments = relativePath.split("/");
296
+ if (segments.length <= 1) {
297
+ return ""; // Root scope
298
+ }
299
+
300
+ // If first segment is "src" or "packages", use that as scope
301
+ if (segments[0] === "src" || segments[0] === "packages") {
302
+ return segments[0] + "/";
303
+ }
304
+
305
+ // Otherwise, use the directory containing the file
306
+ if (segments.length > 1) {
307
+ return segments.slice(0, -1).join("/") + "/";
308
+ }
309
+
310
+ return "";
311
+ }
312
+
313
+ /**
314
+ * Normalize content for consistent hashing.
315
+ */
316
+ private normalizeContent(content: string): string {
317
+ return content
318
+ .replace(/\r\n/g, "\n") // Normalize line endings
319
+ .replace(/\t/g, " ") // Normalize tabs
320
+ .replace(/[ \t]+$/gm, "") // Trim trailing whitespace
321
+ .replace(/\n{3,}/g, "\n\n"); // Collapse excessive newlines
322
+ }
323
+
324
+ /**
325
+ * Compute SHA-256 hash of content.
326
+ */
327
+ private hashContent(content: string): string {
328
+ return createHash("sha256").update(content, "utf8").digest("hex").slice(0, 16);
329
+ }
330
+
331
+ /**
332
+ * Compute combined revision from sources.
333
+ */
334
+ private computeRevision(sources: PolicySource[]): string {
335
+ // Sort by path for deterministic ordering
336
+ const sorted = [...sources].sort((a, b) => a.path.localeCompare(b.path));
337
+ const combined = sorted.map(s => s.sha256).join("|");
338
+ return this.hashContent(combined);
339
+ }
340
+
341
+ /**
342
+ * Determine coverage based on loaded sources and errors.
343
+ */
344
+ private determineCoverage(sources: PolicySource[], errors: string[]): "complete" | "partial" | "degraded" {
345
+ if (errors.length > 0) {
346
+ return "degraded";
347
+ }
348
+
349
+ const mandatorySources = sources.filter(s => s.priority === "mandatory");
350
+ if (mandatorySources.length === 0) {
351
+ return "degraded"; // No mandatory rules loaded
352
+ }
353
+
354
+ // Check if any mandatory sources failed to load (bytes === 0)
355
+ if (mandatorySources.some(s => s.bytes === 0)) {
356
+ return "partial";
357
+ }
358
+
359
+ return "complete";
360
+ }
361
+
362
+ /**
363
+ * Generate cache key for a project root.
364
+ */
365
+ private getCacheKey(projectRoot: string): string {
366
+ return resolve(projectRoot);
367
+ }
368
+
369
+ /**
370
+ * Read the raw content of a source file.
371
+ */
372
+ readSourceContent(path: string): string | null {
373
+ if (existsSync(path)) {
374
+ try {
375
+ return readFileSync(path, "utf8");
376
+ } catch {
377
+ return null;
378
+ }
379
+ }
380
+ return null;
381
+ }
382
+ }
383
+
384
+ // ---------------------------------------------------------------------------
385
+ // Factory
386
+ // ---------------------------------------------------------------------------
387
+
388
+ /**
389
+ * Create a PolicyLoader with the standard harness configuration.
390
+ */
391
+ export function createPolicyLoader(): PolicyLoader {
392
+ // Resolve harness AGENTS.md relative to the package directory
393
+ const harnessRulesPath = new URL("../../AGENTS.md", import.meta.url);
394
+
395
+ return new PolicyLoader({
396
+ harnessRulesPath,
397
+ });
398
+ }