github-router 0.3.378 → 0.3.380
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/README.md +62 -0
- package/dist/ado-tools-CsfbnY8x.js +1 -0
- package/dist/{aic-ledger-CeSXqTEE.js → aic-ledger-6GKI28nY.js} +3 -3
- package/dist/{aic-ledger-CeSXqTEE.js.map → aic-ledger-6GKI28nY.js.map} +1 -1
- package/dist/{aic-statusline-settings-BKveu679.js → aic-statusline-settings-wLL1l_Z6.js} +2 -2
- package/dist/{aic-statusline-settings-BKveu679.js.map → aic-statusline-settings-wLL1l_Z6.js.map} +1 -1
- package/dist/atomic-file-Dif1kLKM.js +198 -0
- package/dist/atomic-file-Dif1kLKM.js.map +1 -0
- package/dist/{attribution-settings-BR5Umj2L.js → attribution-settings-BJ1fjX7V.js} +10 -10
- package/dist/{attribution-settings-BR5Umj2L.js.map → attribution-settings-BJ1fjX7V.js.map} +1 -1
- package/dist/{auth-BZHpTtfF.js → auth-5TKcQbbH.js} +3 -3
- package/dist/{auth-BZHpTtfF.js.map → auth-5TKcQbbH.js.map} +1 -1
- package/dist/bin.js +1479 -0
- package/dist/bin.js.map +1 -0
- package/dist/browser-ext/manifest.json +1 -1
- package/dist/{check-usage-BlIWDuV2.js → check-usage-DM39fdel.js} +4 -4
- package/dist/{check-usage-BlIWDuV2.js.map → check-usage-DM39fdel.js.map} +1 -1
- package/dist/{claude--4O3wHRq.js → claude-BWwkOMi3.js} +26 -25
- package/dist/{claude--4O3wHRq.js.map → claude-BWwkOMi3.js.map} +1 -1
- package/dist/{claude-md-injection-Biq_Xhve.js → claude-md-injection-DNMDcw1W.js} +15 -24
- package/dist/claude-md-injection-DNMDcw1W.js.map +1 -0
- package/dist/{codex-DHRMLpSo.js → codex-C6ix3BaY.js} +8 -7
- package/dist/{codex-DHRMLpSo.js.map → codex-C6ix3BaY.js.map} +1 -1
- package/dist/{copilot-discount-UXC6NQbN.js → copilot-discount-DM98PuAF.js} +2 -2
- package/dist/{copilot-discount-UXC6NQbN.js.map → copilot-discount-DM98PuAF.js.map} +1 -1
- package/dist/{debug-1HUHCWFl.js → debug-Df0Kuzi3.js} +2 -2
- package/dist/{debug-1HUHCWFl.js.map → debug-Df0Kuzi3.js.map} +1 -1
- package/dist/diagnostics-cXjCT_JQ.js +12 -0
- package/dist/diagnostics-cXjCT_JQ.js.map +1 -0
- package/dist/engine-B6la_uIW.js +1 -0
- package/dist/{gate-discovery-DjH9YXoN.js → gate-discovery-2ILpM2fi.js} +5 -5
- package/dist/{gate-discovery-DjH9YXoN.js.map → gate-discovery-2ILpM2fi.js.map} +1 -1
- package/dist/{get-copilot-usage-CKg42gSH.js → get-copilot-usage-BZ6t2hFZ.js} +2 -2
- package/dist/{get-copilot-usage-CKg42gSH.js.map → get-copilot-usage-BZ6t2hFZ.js.map} +1 -1
- package/dist/hooks.mjs +13 -0
- package/dist/hooks.sha256 +1 -1
- package/dist/{index-cmd-sKwchRJz.js → index-cmd-DsK6VFs8.js} +4 -4
- package/dist/{index-cmd-sKwchRJz.js.map → index-cmd-DsK6VFs8.js.map} +1 -1
- package/dist/{internal-aic-status-B_hEeM37.js → internal-aic-status-CUZ7-trf.js} +3 -3
- package/dist/{internal-aic-status-B_hEeM37.js.map → internal-aic-status-CUZ7-trf.js.map} +1 -1
- package/dist/{internal-artifact-open-DbHjipxU.js → internal-artifact-open-DRBg1PpV.js} +2 -2
- package/dist/{internal-artifact-open-DbHjipxU.js.map → internal-artifact-open-DRBg1PpV.js.map} +1 -1
- package/dist/{internal-fast-dispatch-guard-DMzqs-r8.js → internal-fast-dispatch-guard-D6-Ees_6.js} +1 -1
- package/dist/{internal-fast-dispatch-guard-PDdlkPU3.js → internal-fast-dispatch-guard-ZeS9u0A3.js} +2 -2
- package/dist/{internal-fast-dispatch-guard-PDdlkPU3.js.map → internal-fast-dispatch-guard-ZeS9u0A3.js.map} +1 -1
- package/dist/{internal-first-mate-guard-BxPDUsV3.js → internal-first-mate-guard-BijeVpi9.js} +1 -1
- package/dist/{internal-first-mate-guard-4Yro9xNM.js → internal-first-mate-guard-DsEEsLKJ.js} +3 -3
- package/dist/{internal-first-mate-guard-4Yro9xNM.js.map → internal-first-mate-guard-DsEEsLKJ.js.map} +1 -1
- package/dist/{internal-max-dispatch-guard-0teaxIc1.js → internal-max-dispatch-guard-4RLca1i5.js} +2 -2
- package/dist/{internal-max-dispatch-guard-0teaxIc1.js.map → internal-max-dispatch-guard-4RLca1i5.js.map} +1 -1
- package/dist/{internal-max-dispatch-guard-5LSaHN_y.js → internal-max-dispatch-guard-DeEfWqfP.js} +1 -1
- package/dist/{internal-plan-review-BfHLeszV.js → internal-plan-review-bzqGNdtw.js} +3 -3
- package/dist/{internal-plan-review-BfHLeszV.js.map → internal-plan-review-bzqGNdtw.js.map} +1 -1
- package/dist/{internal-prompt-submit-BixNLmM3.js → internal-prompt-submit-BZ2-CExM.js} +4 -4
- package/dist/{internal-prompt-submit-BixNLmM3.js.map → internal-prompt-submit-BZ2-CExM.js.map} +1 -1
- package/dist/{internal-prompt-submit-Ou8xtRXj.js → internal-prompt-submit-ZBhH-SQ1.js} +2 -2
- package/dist/{internal-rebrief-BBGM0PMv.js → internal-rebrief-BIWbN7Sr.js} +4 -4
- package/dist/{internal-rebrief-BBGM0PMv.js.map → internal-rebrief-BIWbN7Sr.js.map} +1 -1
- package/dist/{internal-session-bind-pN1V11Mn.js → internal-session-bind-BL6hFRLO.js} +2 -2
- package/dist/{internal-session-bind-pN1V11Mn.js.map → internal-session-bind-BL6hFRLO.js.map} +1 -1
- package/dist/{internal-stop-hook-DO9Iiaes.js → internal-stop-hook-cDF1MuJZ.js} +5 -5
- package/dist/{internal-stop-hook-DO9Iiaes.js.map → internal-stop-hook-cDF1MuJZ.js.map} +1 -1
- package/dist/{internal-stop-review-BWp-4XWW.js → internal-stop-review-y8zjpK_3.js} +2 -2
- package/dist/{internal-stop-review-BWp-4XWW.js.map → internal-stop-review-y8zjpK_3.js.map} +1 -1
- package/dist/{internal-worker-guard-C9OZPgzu.js → internal-worker-guard-DdyYBZR0.js} +2 -2
- package/dist/{internal-worker-guard-C9OZPgzu.js.map → internal-worker-guard-DdyYBZR0.js.map} +1 -1
- package/dist/{internal-workspace-header-B4TXFBLU.js → internal-workspace-header-BMKbO8yX.js} +2 -2
- package/dist/{internal-workspace-header-B4TXFBLU.js.map → internal-workspace-header-BMKbO8yX.js.map} +1 -1
- package/dist/lib/tree-sitter-pool/worker.js +268 -21
- package/dist/lifecycle-BOjgq9_l.js +1 -0
- package/dist/{lifecycle-vjHAFCaY.js → lifecycle-BlC_kzNt.js} +2 -2
- package/dist/{lifecycle-vjHAFCaY.js.map → lifecycle-BlC_kzNt.js.map} +1 -1
- package/dist/{lifecycle-Dh9y8OF1.js → lifecycle-COZhARmE.js} +2 -2
- package/dist/{lifecycle-Dh9y8OF1.js.map → lifecycle-COZhARmE.js.map} +1 -1
- package/dist/lifecycle-NQvr4flt.js +1 -0
- package/dist/main.js +39 -24
- package/dist/main.js.map +1 -1
- package/dist/{mcp-workspace-header-C5-dOWNa.js → mcp-workspace-header-Bpa4BId8.js} +2 -2
- package/dist/{mcp-workspace-header-C5-dOWNa.js.map → mcp-workspace-header-Bpa4BId8.js.map} +1 -1
- package/dist/{models-B6nH1sS6.js → models-BDV-YLf2.js} +3 -3
- package/dist/{models-B6nH1sS6.js.map → models-BDV-YLf2.js.map} +1 -1
- package/dist/npm-env-H60tX76c.js +130 -0
- package/dist/npm-env-H60tX76c.js.map +1 -0
- package/dist/optional-DGAeyguL.js +586 -0
- package/dist/optional-DGAeyguL.js.map +1 -0
- package/dist/{orchestration-B93jxrWI.js → orchestration-Bto5Nc1Y.js} +2 -2
- package/dist/{orchestration-B93jxrWI.js.map → orchestration-Bto5Nc1Y.js.map} +1 -1
- package/dist/origin-BXtpcKP9.js +382 -0
- package/dist/origin-BXtpcKP9.js.map +1 -0
- package/dist/origin-DbFUCiGp.js +1 -0
- package/dist/paths-CquOjytP.js +1 -0
- package/dist/paths-DK_b2ihK.js +176 -0
- package/dist/paths-DK_b2ihK.js.map +1 -0
- package/dist/{paths-CVEO8jEH.js → paths-DeFmYJhO.js} +16 -3
- package/dist/paths-DeFmYJhO.js.map +1 -0
- package/dist/{peer-mcp-personas-OWsSm6To.js → peer-mcp-personas-Dny0r5Am.js} +14 -14
- package/dist/{peer-mcp-personas-OWsSm6To.js.map → peer-mcp-personas-Dny0r5Am.js.map} +1 -1
- package/dist/{pi-B0EFY6l7.js → pi-YhoA26wi.js} +718 -119
- package/dist/pi-YhoA26wi.js.map +1 -0
- package/dist/pi-foreign-artifacts-CNt6wQ1P.js +660 -0
- package/dist/pi-foreign-artifacts-CNt6wQ1P.js.map +1 -0
- package/dist/pi-memory-bridge-BxmCIIbk.js +618 -0
- package/dist/pi-memory-bridge-BxmCIIbk.js.map +1 -0
- package/dist/pi-memory-bridge-D5A0h_Kf.js +1 -0
- package/dist/pi-version-check-BeBB0Ny9.js +551 -0
- package/dist/pi-version-check-BeBB0Ny9.js.map +1 -0
- package/dist/pi-version-check-D5RvdLsG.js +1 -0
- package/dist/{plan-review-hook-MhjK92wq.js → plan-review-hook-D_yyjOgO.js} +3 -3
- package/dist/{plan-review-hook-MhjK92wq.js.map → plan-review-hook-D_yyjOgO.js.map} +1 -1
- package/dist/{provision-DPL8aGe2.js → provision-BWtuDq_v.js} +30 -36
- package/dist/provision-BWtuDq_v.js.map +1 -0
- package/dist/rerank-cross-encoder-BhjImuwG.js +192 -0
- package/dist/rerank-cross-encoder-BhjImuwG.js.map +1 -0
- package/dist/{self-invocation-DMUnXVGT.js → self-invocation-Dnq4iVD6.js} +4 -31
- package/dist/{self-invocation-DMUnXVGT.js.map → self-invocation-Dnq4iVD6.js.map} +1 -1
- package/dist/{serve-C13q-9w0.js → serve-DrHAX2Bh.js} +15 -14
- package/dist/{serve-C13q-9w0.js.map → serve-DrHAX2Bh.js.map} +1 -1
- package/dist/{server-setup-CptI7SUT.js → server-setup-DLwejgVA.js} +12 -638
- package/dist/server-setup-DLwejgVA.js.map +1 -0
- package/dist/{service-backend-Cdgz6WPx.js → service-backend-BdhWVRSz.js} +17 -62
- package/dist/service-backend-BdhWVRSz.js.map +1 -0
- package/dist/{start-DioCGjM8.js → start-DvV3Lbwy.js} +5 -4
- package/dist/{start-DioCGjM8.js.map → start-DvV3Lbwy.js.map} +1 -1
- package/dist/{static-context-pack-CtnBwHSd.js → static-context-pack-BKFelEbM.js} +3 -3
- package/dist/{static-context-pack-CtnBwHSd.js.map → static-context-pack-BKFelEbM.js.map} +1 -1
- package/dist/{stop-gate-hook-CBVLD8iB.js → stop-gate-hook-DBOj0TqL.js} +3 -3
- package/dist/{stop-gate-hook-CBVLD8iB.js.map → stop-gate-hook-DBOj0TqL.js.map} +1 -1
- package/dist/{stop-gate-policy-BehNiNix.js → stop-gate-policy-Bp2wGh_W.js} +2 -2
- package/dist/{stop-gate-policy-BehNiNix.js.map → stop-gate-policy-Bp2wGh_W.js.map} +1 -1
- package/dist/{token-CPzYxU4Y.js → token-CKPOBdO0.js} +23 -88
- package/dist/{token-CPzYxU4Y.js.map → token-CKPOBdO0.js.map} +1 -1
- package/dist/{token-DHEkiO4g.js → token-Ygd7D32L.js} +3 -3
- package/dist/{token-DHEkiO4g.js.map → token-Ygd7D32L.js.map} +1 -1
- package/dist/update-CB21YRKz.js +1 -0
- package/dist/update-T1L7yFon.js +1548 -0
- package/dist/update-T1L7yFon.js.map +1 -0
- package/dist/update-fEgWTrIx.js +214 -0
- package/dist/update-fEgWTrIx.js.map +1 -0
- package/dist/update-lock-BeS_WmC8.js +1 -0
- package/dist/update-lock-BmXx6QaR.js +171 -0
- package/dist/update-lock-BmXx6QaR.js.map +1 -0
- package/dist/volatile-root-DQNNvQ7Z.js +50 -0
- package/dist/volatile-root-DQNNvQ7Z.js.map +1 -0
- package/dist/{worker-dispatch-C7RtNOXv.js → worker-dispatch-DBWKTMda.js} +2 -2
- package/dist/{worker-dispatch-C7RtNOXv.js.map → worker-dispatch-DBWKTMda.js.map} +1 -1
- package/dist/{workiq-CQauEAMc.js → workiq-D5b7xlbr.js} +2 -2
- package/dist/{workiq-CQauEAMc.js.map → workiq-D5b7xlbr.js.map} +1 -1
- package/dist/{workiq-tools-DL6rC4bP.js → workiq-tools-DdiQ2DOW.js} +1 -1
- package/package.json +16 -4
- package/dist/ado-tools-CRkyU51J.js +0 -1
- package/dist/claude-md-injection-Biq_Xhve.js.map +0 -1
- package/dist/engine-V46un8Xl.js +0 -1
- package/dist/lifecycle-BENRuuX6.js +0 -1
- package/dist/lifecycle-Dli9vGI2.js +0 -1
- package/dist/paths-Be9_ujQ3.js +0 -1
- package/dist/paths-CVEO8jEH.js.map +0 -1
- package/dist/pi-B0EFY6l7.js.map +0 -1
- package/dist/pi-memory-bridge-Dvj1T3e3.js +0 -403
- package/dist/pi-memory-bridge-Dvj1T3e3.js.map +0 -1
- package/dist/provision-DPL8aGe2.js.map +0 -1
- package/dist/rerank-cross-encoder-B87lZgvc.js +0 -110
- package/dist/rerank-cross-encoder-B87lZgvc.js.map +0 -1
- package/dist/server-setup-CptI7SUT.js.map +0 -1
- package/dist/service-backend-Cdgz6WPx.js.map +0 -1
|
@@ -0,0 +1,618 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import fs$1 from "node:fs/promises";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
// src/lib/pi-memory-bridge.ts
|
|
6
|
+
/**
|
|
7
|
+
* Pi memory bridge: repo instruction + user-global + auto-memory surfacing.
|
|
8
|
+
*
|
|
9
|
+
* Pi natively loads per-dir first-win
|
|
10
|
+
* `AGENTS.override.md > AGENTS.md > AGENTS.MD > CLAUDE.md > CLAUDE.MD`
|
|
11
|
+
* (agentDir global + fs-root → cwd, concatenated, no trust gate).
|
|
12
|
+
* It does NOT load `.github/copilot-instructions.md`,
|
|
13
|
+
* `.github/instructions/*.instructions.md` (`applyTo`), `.claude/rules/`
|
|
14
|
+
* (`paths:`), `@path` imports, `CLAUDE.local.md`, `./.claude/CLAUDE.md`,
|
|
15
|
+
* `~/.claude/CLAUDE.md`, or Claude auto-memory.
|
|
16
|
+
*
|
|
17
|
+
* Strategy (per approved plan): synthesize the static repo-wide slice into
|
|
18
|
+
* the per-launch mirror's `AGENTS.md` (a Pi-native candidate, so no new
|
|
19
|
+
* loader is needed) and lazy-attach path-scoped rules via the
|
|
20
|
+
* `gh-router-pi` extension's `tool_result` hook (mirrors
|
|
21
|
+
* `pi-code/claude-rules.ts`). Auto-memory is on-demand only (`/memory`
|
|
22
|
+
* command + explicit read), never auto-injected.
|
|
23
|
+
*
|
|
24
|
+
* The synthesized block is a permanent per-turn cost: Pi loads the mirror's
|
|
25
|
+
* `AGENTS.md` into the `project_context` system-prompt section on EVERY turn
|
|
26
|
+
* (`core/resource-loader.js` → `renderProjectContext`). It is therefore
|
|
27
|
+
* written as instruction text, not as a report: one heading per source group,
|
|
28
|
+
* one `path — body` line per file, no titles, no provenance comments. Measured
|
|
29
|
+
* on a realistic repo: 86% of the pre-compaction block was scaffolding.
|
|
30
|
+
*
|
|
31
|
+
* The bridge does NOT try to replace what Pi loads natively. Pi still reads
|
|
32
|
+
* the repo's own `AGENTS.md`/`CLAUDE.md` (and `AGENTS.override.md`) itself, so
|
|
33
|
+
* a repo that ships both a native context file and a bridged Copilot/Claude
|
|
34
|
+
* file sees the shared guidance twice; that is expected, because suppressing a
|
|
35
|
+
* repo's own file to save bytes would silently drop guidance the user can see.
|
|
36
|
+
*
|
|
37
|
+
* All filesystem access is best-effort: missing/unreadable files are
|
|
38
|
+
* skipped, never fatal. Pure helpers are exported for unit tests.
|
|
39
|
+
*/
|
|
40
|
+
const PI_BRIDGE_STATIC_BUDGET_BYTES = 24e3;
|
|
41
|
+
/**
|
|
42
|
+
* Floor reserved for every group that still has content to write, so one huge
|
|
43
|
+
* first section cannot starve the rest down to a bare truncation marker. A
|
|
44
|
+
* group that cannot fit its floor is written as a named omission instead of a
|
|
45
|
+
* silent gap; see `buildMirrorBridgeSection`.
|
|
46
|
+
*/
|
|
47
|
+
const PI_BRIDGE_SECTION_FLOOR_BYTES = 1500;
|
|
48
|
+
/**
|
|
49
|
+
* Room held back for the "omitted for budget" row, so a body can never consume
|
|
50
|
+
* the space reserved to report that something did not fit.
|
|
51
|
+
*/
|
|
52
|
+
const PI_BRIDGE_OMISSION_ROW_BYTES = 160;
|
|
53
|
+
const PI_BRIDGE_FENCE_BEGIN = "<!-- gh-router memory-bridge begin -->";
|
|
54
|
+
const PI_BRIDGE_FENCE_END = "<!-- gh-router memory-bridge end -->";
|
|
55
|
+
function byteLength(s) {
|
|
56
|
+
return Buffer.byteLength(s, "utf8");
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Truncate to a UTF-8 byte budget without splitting a code point, marker
|
|
60
|
+
* included in the budget.
|
|
61
|
+
*
|
|
62
|
+
* Exported because the foreign-artifact bridge caps with the same measure Pi's
|
|
63
|
+
* own limits are stated in, and `label` keeps the truncation marker honest
|
|
64
|
+
* about which bridge did the cutting.
|
|
65
|
+
*/
|
|
66
|
+
function capBytes(s, max, label = "gh-router memory bridge") {
|
|
67
|
+
if (byteLength(s) <= max) return {
|
|
68
|
+
text: s,
|
|
69
|
+
truncated: false
|
|
70
|
+
};
|
|
71
|
+
const marker = `\n…[truncated by ${label}]`;
|
|
72
|
+
const budget = Math.max(0, max - byteLength(marker));
|
|
73
|
+
let low = 0;
|
|
74
|
+
let high = s.length;
|
|
75
|
+
while (low < high) {
|
|
76
|
+
const mid = Math.floor((low + high) / 2);
|
|
77
|
+
if (byteLength(s.slice(0, mid)) <= budget) low = mid + 1;
|
|
78
|
+
else high = mid;
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
text: `${s.slice(0, Math.max(0, low - 1))}${marker}`,
|
|
82
|
+
truncated: true
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/** Strip YAML frontmatter (`--- ... ---`) and return body + raw frontmatter. */
|
|
86
|
+
function splitFrontmatter(raw) {
|
|
87
|
+
const normalized = raw.replace(/\r\n/g, "\n");
|
|
88
|
+
if (!normalized.startsWith("---")) return {
|
|
89
|
+
frontmatter: "",
|
|
90
|
+
body: normalized
|
|
91
|
+
};
|
|
92
|
+
const end = normalized.indexOf("\n---", 3);
|
|
93
|
+
if (end === -1) return {
|
|
94
|
+
frontmatter: "",
|
|
95
|
+
body: normalized
|
|
96
|
+
};
|
|
97
|
+
return {
|
|
98
|
+
frontmatter: normalized.slice(3, end).trim(),
|
|
99
|
+
body: normalized.slice(end + 4).replace(/^\n/, "")
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Minimal frontmatter glob extraction for `paths:` / `applyTo:` / `globs:`.
|
|
104
|
+
* Supports `key: "glob"`, `key: a, b`, and YAML lists. Case-insensitive keys.
|
|
105
|
+
* Returns [] when no scoping key is present (unscoped rule).
|
|
106
|
+
*/
|
|
107
|
+
function extractScopeGlobs(frontmatter) {
|
|
108
|
+
const out = [];
|
|
109
|
+
const lines = frontmatter.split("\n");
|
|
110
|
+
let i = 0;
|
|
111
|
+
while (i < lines.length) {
|
|
112
|
+
const line = lines[i] ?? "";
|
|
113
|
+
const m = /^\s*(paths|applyTo|globs)\s*:\s*(.*)$/i.exec(line);
|
|
114
|
+
if (!m) {
|
|
115
|
+
i++;
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
const rest = (m[2] ?? "").trim();
|
|
119
|
+
if (rest.startsWith("[") && rest.endsWith("]")) {
|
|
120
|
+
for (const part of rest.slice(1, -1).split(",")) {
|
|
121
|
+
const v = part.trim().replace(/^["']|["']$/g, "");
|
|
122
|
+
if (v) out.push(v);
|
|
123
|
+
}
|
|
124
|
+
i++;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (rest && !rest.startsWith("|") && !rest.startsWith(">")) {
|
|
128
|
+
const v = rest.replace(/^["']|["']$/g, "").split("#")[0]?.trim() ?? "";
|
|
129
|
+
if (v) for (const part of v.split(",")) {
|
|
130
|
+
const p = part.trim();
|
|
131
|
+
if (p) out.push(p);
|
|
132
|
+
}
|
|
133
|
+
i++;
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
i++;
|
|
137
|
+
while (i < lines.length) {
|
|
138
|
+
const item = lines[i] ?? "";
|
|
139
|
+
const im = /^\s+-\s+(.+)$/.exec(item);
|
|
140
|
+
if (!im) break;
|
|
141
|
+
const v = (im[1] ?? "").trim().replace(/^["']|["']$/g, "");
|
|
142
|
+
if (v) out.push(v);
|
|
143
|
+
i++;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return out;
|
|
147
|
+
}
|
|
148
|
+
/** Extract `@path` references (whole-line or trailing) ending in a text extension. */
|
|
149
|
+
function extractAtImports(content) {
|
|
150
|
+
const refs = [];
|
|
151
|
+
const seen = /* @__PURE__ */ new Set();
|
|
152
|
+
const pattern = /@([~.]?[\w./\\-]+\.(?:md|txt|yaml|yml|json|toml))(?:\s|$)/g;
|
|
153
|
+
for (const line of content.split("\n")) {
|
|
154
|
+
const trimmed = line.trim();
|
|
155
|
+
if (!trimmed.includes("@")) continue;
|
|
156
|
+
pattern.lastIndex = 0;
|
|
157
|
+
let m;
|
|
158
|
+
while ((m = pattern.exec(trimmed)) !== null) {
|
|
159
|
+
const ref = m[1] ?? "";
|
|
160
|
+
if (ref && !seen.has(ref)) {
|
|
161
|
+
seen.add(ref);
|
|
162
|
+
refs.push(ref);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return refs;
|
|
167
|
+
}
|
|
168
|
+
/** Redact likely secrets before persisting synthesized bridge content. */
|
|
169
|
+
function redactSecrets(text) {
|
|
170
|
+
return text.replace(/-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----/g, "[REDACTED private key]").replace(/(sk-(live|test)-[A-Za-z0-9_-]{8,})/g, "[REDACTED]").replace(/(gh[pousr]_[A-Za-z0-9_]{8,})/g, "[REDACTED]").replace(/(xox[baprs]-[A-Za-z0-9-]{8,})/g, "[REDACTED]").replace(/eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/g, "[REDACTED jwt]");
|
|
171
|
+
}
|
|
172
|
+
async function readIfExists(file) {
|
|
173
|
+
try {
|
|
174
|
+
return await fs$1.readFile(file, "utf8");
|
|
175
|
+
} catch {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Collect bridge inputs from disk (best-effort). Repo root is the cwd's
|
|
181
|
+
* git top-level when resolvable, else cwd. Only reads files; never writes.
|
|
182
|
+
*/
|
|
183
|
+
async function collectBridgeInputs(cwd = process.cwd(), homeDir = os.homedir()) {
|
|
184
|
+
const collected = {
|
|
185
|
+
copilotInstructions: [],
|
|
186
|
+
unscopedRules: [],
|
|
187
|
+
scopedRules: [],
|
|
188
|
+
userGlobal: [],
|
|
189
|
+
imports: [],
|
|
190
|
+
skipped: []
|
|
191
|
+
};
|
|
192
|
+
let repoRoot = cwd;
|
|
193
|
+
try {
|
|
194
|
+
let dir = path.resolve(cwd);
|
|
195
|
+
for (let depth = 0; depth < 12; depth++) try {
|
|
196
|
+
await fs$1.stat(path.join(dir, ".git"));
|
|
197
|
+
repoRoot = dir;
|
|
198
|
+
break;
|
|
199
|
+
} catch {
|
|
200
|
+
const parent = path.dirname(dir);
|
|
201
|
+
if (parent === dir) break;
|
|
202
|
+
dir = parent;
|
|
203
|
+
}
|
|
204
|
+
} catch {
|
|
205
|
+
repoRoot = cwd;
|
|
206
|
+
}
|
|
207
|
+
const copilotFile = path.join(repoRoot, ".github", "copilot-instructions.md");
|
|
208
|
+
const copilotBody = await readIfExists(copilotFile);
|
|
209
|
+
const baseForImports = [];
|
|
210
|
+
if (copilotBody !== void 0) {
|
|
211
|
+
collected.copilotInstructions.push({
|
|
212
|
+
file: copilotFile,
|
|
213
|
+
content: splitFrontmatter(copilotBody).body
|
|
214
|
+
});
|
|
215
|
+
baseForImports.push({
|
|
216
|
+
file: copilotFile,
|
|
217
|
+
content: copilotBody
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
try {
|
|
221
|
+
const dir = path.join(repoRoot, ".github", "instructions");
|
|
222
|
+
const entries = await fs$1.readdir(dir);
|
|
223
|
+
for (const name of entries.sort()) {
|
|
224
|
+
if (!name.endsWith(".instructions.md")) continue;
|
|
225
|
+
const file = path.join(dir, name);
|
|
226
|
+
const raw = await readIfExists(file);
|
|
227
|
+
if (raw === void 0) continue;
|
|
228
|
+
const { frontmatter, body } = splitFrontmatter(raw);
|
|
229
|
+
const globs = extractScopeGlobs(frontmatter);
|
|
230
|
+
if (globs.length === 0 || globs.includes("**") || globs.includes("**/*")) collected.unscopedRules.push({
|
|
231
|
+
file,
|
|
232
|
+
content: body
|
|
233
|
+
});
|
|
234
|
+
else collected.scopedRules.push({
|
|
235
|
+
file,
|
|
236
|
+
body,
|
|
237
|
+
globs,
|
|
238
|
+
source: "copilot-instructions"
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
} catch {}
|
|
242
|
+
try {
|
|
243
|
+
let dir = null;
|
|
244
|
+
let walk = path.resolve(cwd);
|
|
245
|
+
for (let depth = 0; depth < 12; depth++) try {
|
|
246
|
+
await fs$1.stat(path.join(walk, ".claude", "rules"));
|
|
247
|
+
dir = path.join(walk, ".claude", "rules");
|
|
248
|
+
break;
|
|
249
|
+
} catch {
|
|
250
|
+
const parent = path.dirname(walk);
|
|
251
|
+
if (parent === walk) break;
|
|
252
|
+
walk = parent;
|
|
253
|
+
}
|
|
254
|
+
if (dir) {
|
|
255
|
+
const entries = await fs$1.readdir(dir);
|
|
256
|
+
for (const name of entries.sort()) {
|
|
257
|
+
if (!name.endsWith(".md")) continue;
|
|
258
|
+
const file = path.join(dir, name);
|
|
259
|
+
const raw = await readIfExists(file);
|
|
260
|
+
if (raw === void 0) continue;
|
|
261
|
+
const { frontmatter, body } = splitFrontmatter(raw);
|
|
262
|
+
if (!body.trim()) {
|
|
263
|
+
collected.skipped.push({
|
|
264
|
+
file,
|
|
265
|
+
reason: "empty after frontmatter strip"
|
|
266
|
+
});
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
const globs = extractScopeGlobs(frontmatter);
|
|
270
|
+
if (globs.length === 0) collected.unscopedRules.push({
|
|
271
|
+
file,
|
|
272
|
+
content: body
|
|
273
|
+
});
|
|
274
|
+
else collected.scopedRules.push({
|
|
275
|
+
file,
|
|
276
|
+
body,
|
|
277
|
+
globs,
|
|
278
|
+
source: "claude-rules"
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
} catch {}
|
|
283
|
+
const dotClaude = path.join(repoRoot, ".claude", "CLAUDE.md");
|
|
284
|
+
const dotBody = await readIfExists(dotClaude);
|
|
285
|
+
if (dotBody !== void 0 && dotBody.trim()) {
|
|
286
|
+
collected.unscopedRules.push({
|
|
287
|
+
file: dotClaude,
|
|
288
|
+
content: splitFrontmatter(dotBody).body
|
|
289
|
+
});
|
|
290
|
+
baseForImports.push({
|
|
291
|
+
file: dotClaude,
|
|
292
|
+
content: dotBody
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
for (const file of [path.join(homeDir, ".claude", "CLAUDE.md"), path.join(homeDir, ".copilot", "copilot-instructions.md")]) {
|
|
296
|
+
const raw = await readIfExists(file);
|
|
297
|
+
if (raw !== void 0 && raw.trim()) {
|
|
298
|
+
collected.userGlobal.push({
|
|
299
|
+
file,
|
|
300
|
+
content: splitFrontmatter(raw).body
|
|
301
|
+
});
|
|
302
|
+
baseForImports.push({
|
|
303
|
+
file,
|
|
304
|
+
content: raw
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
const fileMap = /* @__PURE__ */ new Map();
|
|
309
|
+
for (const group of [
|
|
310
|
+
collected.copilotInstructions,
|
|
311
|
+
collected.unscopedRules,
|
|
312
|
+
collected.userGlobal
|
|
313
|
+
]) for (const entry of group) try {
|
|
314
|
+
fileMap.set(path.resolve(entry.file), entry.content);
|
|
315
|
+
} catch {
|
|
316
|
+
fileMap.set(entry.file, entry.content);
|
|
317
|
+
}
|
|
318
|
+
const seen = /* @__PURE__ */ new Set();
|
|
319
|
+
for (const b of baseForImports) try {
|
|
320
|
+
seen.add(path.resolve(b.file));
|
|
321
|
+
} catch {
|
|
322
|
+
seen.add(b.file);
|
|
323
|
+
}
|
|
324
|
+
const queue = baseForImports.map((b) => ({
|
|
325
|
+
importer: b.file,
|
|
326
|
+
content: b.content,
|
|
327
|
+
depth: 1
|
|
328
|
+
}));
|
|
329
|
+
while (queue.length > 0 && collected.imports.length < 10) {
|
|
330
|
+
const head = queue.shift();
|
|
331
|
+
if (!head || head.depth > 4) continue;
|
|
332
|
+
let importerAbs = head.importer;
|
|
333
|
+
try {
|
|
334
|
+
importerAbs = path.resolve(head.importer);
|
|
335
|
+
} catch {}
|
|
336
|
+
const baseDir = path.dirname(importerAbs);
|
|
337
|
+
for (const ref of extractAtImports(head.content)) {
|
|
338
|
+
if (collected.imports.length + seen.size >= 10) break;
|
|
339
|
+
let expanded = ref;
|
|
340
|
+
if (expanded.startsWith("~")) expanded = path.join(homeDir, expanded.slice(1));
|
|
341
|
+
const abs = path.isAbsolute(expanded) ? path.normalize(expanded) : path.resolve(baseDir, expanded);
|
|
342
|
+
if (seen.has(abs)) continue;
|
|
343
|
+
seen.add(abs);
|
|
344
|
+
const hit = fileMap.get(abs) ?? await readIfExists(abs);
|
|
345
|
+
if (hit === void 0) {
|
|
346
|
+
collected.skipped.push({
|
|
347
|
+
file: abs,
|
|
348
|
+
reason: `import of ${head.importer} not found`
|
|
349
|
+
});
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
const body = splitFrontmatter(hit).body;
|
|
353
|
+
collected.imports.push({
|
|
354
|
+
file: abs,
|
|
355
|
+
content: body
|
|
356
|
+
});
|
|
357
|
+
queue.push({
|
|
358
|
+
importer: abs,
|
|
359
|
+
content: hit,
|
|
360
|
+
depth: head.depth + 1
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return dedupeCollected(collected);
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Canonical identity for a collected file: `realpath` when it resolves (so a
|
|
368
|
+
* symlinked rule is not emitted twice under two names), `INO` identity as a
|
|
369
|
+
* fallback for hard links (two names for one inode are one rule), the resolved
|
|
370
|
+
* path otherwise.
|
|
371
|
+
*/
|
|
372
|
+
function fileIdentity(file) {
|
|
373
|
+
const resolved = (() => {
|
|
374
|
+
try {
|
|
375
|
+
return path.resolve(file);
|
|
376
|
+
} catch {
|
|
377
|
+
return file;
|
|
378
|
+
}
|
|
379
|
+
})();
|
|
380
|
+
try {
|
|
381
|
+
const real = fs.realpathSync.native(resolved);
|
|
382
|
+
const { dev, ino } = fs.statSync(real);
|
|
383
|
+
return ino ? `${dev}:${ino}` : real;
|
|
384
|
+
} catch {
|
|
385
|
+
return resolved;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Drop entries that name the same file (symlink, hard link, or an `@` import
|
|
390
|
+
* that re-reaches a base file). First occurrence wins: groups are ordered by
|
|
391
|
+
* authority (repo-wide, then project rules, then user-global, then imports), so
|
|
392
|
+
* the earlier copy is the one with the better provenance.
|
|
393
|
+
*/
|
|
394
|
+
function dedupeCollected(collected) {
|
|
395
|
+
const seen = /* @__PURE__ */ new Set();
|
|
396
|
+
const keep = (entry) => {
|
|
397
|
+
const id = fileIdentity(entry.file);
|
|
398
|
+
if (seen.has(id)) return false;
|
|
399
|
+
seen.add(id);
|
|
400
|
+
return true;
|
|
401
|
+
};
|
|
402
|
+
const imports = collected.imports.filter(keep);
|
|
403
|
+
const unscopedRules = collected.unscopedRules.filter(keep);
|
|
404
|
+
const userGlobal = collected.userGlobal.filter(keep);
|
|
405
|
+
const copilotInstructions = collected.copilotInstructions.filter(keep);
|
|
406
|
+
const scopedRules = collected.scopedRules.filter(keep);
|
|
407
|
+
const skipped = collected.skipped.filter(keep);
|
|
408
|
+
const dropped = collected.imports.length - imports.length + (collected.unscopedRules.length - unscopedRules.length) + (collected.userGlobal.length - userGlobal.length) + (collected.copilotInstructions.length - copilotInstructions.length) + (collected.scopedRules.length - scopedRules.length);
|
|
409
|
+
return {
|
|
410
|
+
copilotInstructions,
|
|
411
|
+
unscopedRules,
|
|
412
|
+
scopedRules,
|
|
413
|
+
userGlobal,
|
|
414
|
+
imports,
|
|
415
|
+
skipped,
|
|
416
|
+
...dropped > 0 ? { deduped: dropped } : {}
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* Display form of a source path: repo-relative when it lives under the repo,
|
|
421
|
+
* `~`-relative when under the home directory, absolute otherwise. Keeps the
|
|
422
|
+
* block free of absolute paths, which cost bytes the model never needs.
|
|
423
|
+
*/
|
|
424
|
+
function displaySource(file, repoRoot, homeDir) {
|
|
425
|
+
const clean = file.replaceAll("\0", "");
|
|
426
|
+
const rel = (root) => {
|
|
427
|
+
const base = root.replace(/[\\/]+$/, "");
|
|
428
|
+
if (!base) return void 0;
|
|
429
|
+
if (clean === base) return void 0;
|
|
430
|
+
if (!clean.startsWith(`${base}/`) && !clean.startsWith(`${base}\\`)) return void 0;
|
|
431
|
+
return clean.slice(base.length + 1);
|
|
432
|
+
};
|
|
433
|
+
const inRepo = repoRoot ? rel(repoRoot) : void 0;
|
|
434
|
+
if (inRepo) return inRepo;
|
|
435
|
+
const inHome = homeDir ? rel(homeDir) : void 0;
|
|
436
|
+
if (inHome) return `~/${inHome}`;
|
|
437
|
+
return clean;
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Build the static mirror `AGENTS.md` bridge section.
|
|
441
|
+
*
|
|
442
|
+
* Every emitted byte is a per-turn prompt cost, so the block carries
|
|
443
|
+
* instruction text and nothing else: one heading per group, one line per file
|
|
444
|
+
* in the form `` `path` — body ``, bodies copied verbatim (a `file:line` list
|
|
445
|
+
* is real AGENTS.md content and must not be flattened), and no title or
|
|
446
|
+
* provenance comments. Returns `section: ""` when nothing was collected, so
|
|
447
|
+
* the caller writes no block at all — and `mergeBridgeIntoAgentsMd` then strips
|
|
448
|
+
* any block a previous launch wrote, rather than leaving stale guidance in the
|
|
449
|
+
* prompt forever.
|
|
450
|
+
*
|
|
451
|
+
* Budget: `PI_BRIDGE_STATIC_BUDGET_BYTES` still bounds the block, but every
|
|
452
|
+
* group with content keeps `PI_BRIDGE_SECTION_FLOOR_BYTES` (capped at an even
|
|
453
|
+
* share of what is left), so the last group cannot be reduced to a bare
|
|
454
|
+
* truncation marker by a large first one. A group that still cannot fit is
|
|
455
|
+
* written as a named omission, which the model can act on, unlike a silence.
|
|
456
|
+
* Redaction runs per body BEFORE the cap, so a cut can never leave the first
|
|
457
|
+
* half of a secret in the prompt.
|
|
458
|
+
*
|
|
459
|
+
* The ceiling is an invariant, not a target: the fence is part of the ledger
|
|
460
|
+
* (it used to be appended after the fact, which let the accounting overshoot)
|
|
461
|
+
* and the omission row is a single capped row for the whole group, because a
|
|
462
|
+
* group of 40 small files could otherwise spend the whole budget on markers
|
|
463
|
+
* alone. That row leads with the omitted COUNT, so it stays informative even
|
|
464
|
+
* when its own list of names is cut.
|
|
465
|
+
*/
|
|
466
|
+
function buildMirrorBridgeSection(collected, opts = {}) {
|
|
467
|
+
const homeDir = opts.homeDir ?? os.homedir();
|
|
468
|
+
const groups = [
|
|
469
|
+
{
|
|
470
|
+
title: "Copilot repo instructions",
|
|
471
|
+
entries: collected.copilotInstructions
|
|
472
|
+
},
|
|
473
|
+
{
|
|
474
|
+
title: "Unscoped project rules",
|
|
475
|
+
entries: collected.unscopedRules
|
|
476
|
+
},
|
|
477
|
+
{
|
|
478
|
+
title: "User-global instructions",
|
|
479
|
+
entries: collected.userGlobal
|
|
480
|
+
},
|
|
481
|
+
{
|
|
482
|
+
title: "Imported (`@path`)",
|
|
483
|
+
entries: collected.imports
|
|
484
|
+
}
|
|
485
|
+
].filter((g) => g.entries.length > 0);
|
|
486
|
+
const scoped = collected.scopedRules;
|
|
487
|
+
let truncated = false;
|
|
488
|
+
const parts = [];
|
|
489
|
+
let used = byteLength(PI_BRIDGE_FENCE_BEGIN) + byteLength(PI_BRIDGE_FENCE_END) + 4;
|
|
490
|
+
const live = () => groups.filter((g) => g.entries.some((e) => e.content.replaceAll("\0", "").trim().length > 0));
|
|
491
|
+
for (const [index, group] of groups.entries()) {
|
|
492
|
+
const remainingGroups = Math.max(1, live().length - index + (scoped.length > 0 ? 1 : 0));
|
|
493
|
+
const share = Math.floor(Math.max(0, PI_BRIDGE_STATIC_BUDGET_BYTES - used - 512) / remainingGroups);
|
|
494
|
+
const floor = Math.min(PI_BRIDGE_SECTION_FLOOR_BYTES, Math.max(200, share));
|
|
495
|
+
const budget = Math.max(0, PI_BRIDGE_STATIC_BUDGET_BYTES - used - 512 - floor * (remainingGroups - 1));
|
|
496
|
+
const heading = `## ${group.title}`;
|
|
497
|
+
const rendered = [];
|
|
498
|
+
const omitted = [];
|
|
499
|
+
let groupTruncated = false;
|
|
500
|
+
const roomLeft = () => budget - byteLength(heading) - 2 - rendered.reduce((n, s) => n + byteLength(s) + 2, 0);
|
|
501
|
+
for (const entry of group.entries) {
|
|
502
|
+
const body = redactSecrets(entry.content.replaceAll("\0", "")).trim();
|
|
503
|
+
if (!body) continue;
|
|
504
|
+
const source = displaySource(entry.file, opts.repoRoot, homeDir);
|
|
505
|
+
const line = `\`${source}\` — ${body}`;
|
|
506
|
+
const room = roomLeft();
|
|
507
|
+
if (room <= PI_BRIDGE_OMISSION_ROW_BYTES) {
|
|
508
|
+
omitted.push(source);
|
|
509
|
+
continue;
|
|
510
|
+
}
|
|
511
|
+
const capped = capBytes(line, room - PI_BRIDGE_OMISSION_ROW_BYTES);
|
|
512
|
+
if (capped.truncated) groupTruncated = true;
|
|
513
|
+
rendered.push(capped.text);
|
|
514
|
+
}
|
|
515
|
+
if (omitted.length > 0) {
|
|
516
|
+
groupTruncated = true;
|
|
517
|
+
const names = omitted.map((source) => `\`${source}\``).join(", ");
|
|
518
|
+
const row = `- Omitted for budget (${omitted.length}): ${names}`;
|
|
519
|
+
const room = roomLeft();
|
|
520
|
+
if (room > 0) rendered.push(capBytes(row, room).text);
|
|
521
|
+
}
|
|
522
|
+
if (rendered.length === 0) continue;
|
|
523
|
+
if (groupTruncated) truncated = true;
|
|
524
|
+
parts.push(heading, "", ...rendered, "");
|
|
525
|
+
used = byteLength(PI_BRIDGE_FENCE_BEGIN) + byteLength(PI_BRIDGE_FENCE_END) + 4 + byteLength(parts.join("\n"));
|
|
526
|
+
if (used >= 23488) break;
|
|
527
|
+
}
|
|
528
|
+
if (scoped.length > 0) {
|
|
529
|
+
const header = "## Path-scoped rules (attached on first matching read/edit/write)";
|
|
530
|
+
const room = Math.max(0, PI_BRIDGE_STATIC_BUDGET_BYTES - used - 512);
|
|
531
|
+
const rows = [];
|
|
532
|
+
const omitted = [];
|
|
533
|
+
let scopedTruncated = false;
|
|
534
|
+
for (const rule of scoped) {
|
|
535
|
+
const source = displaySource(rule.file, opts.repoRoot, homeDir);
|
|
536
|
+
const row = `- \`${source}\` — \`${rule.globs.join(", ").replaceAll("\0", "")}\``;
|
|
537
|
+
const left = room - byteLength(header) - 2 - rows.reduce((n, s) => n + byteLength(s) + 2, 0);
|
|
538
|
+
if (left <= PI_BRIDGE_OMISSION_ROW_BYTES) {
|
|
539
|
+
omitted.push(source);
|
|
540
|
+
continue;
|
|
541
|
+
}
|
|
542
|
+
rows.push(capBytes(row, left - PI_BRIDGE_OMISSION_ROW_BYTES).text);
|
|
543
|
+
}
|
|
544
|
+
if (omitted.length > 0) {
|
|
545
|
+
scopedTruncated = true;
|
|
546
|
+
const names = omitted.map((source) => `\`${source}\``).join(", ");
|
|
547
|
+
const left = room - byteLength(header) - 2 - rows.reduce((n, s) => n + byteLength(s) + 2, 0);
|
|
548
|
+
if (left > 0) rows.push(capBytes(`- Omitted for budget (${omitted.length}): ${names}`, left).text);
|
|
549
|
+
}
|
|
550
|
+
if (rows.length > 0) {
|
|
551
|
+
if (scopedTruncated) truncated = true;
|
|
552
|
+
parts.push(header, "", ...rows, "");
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
if (parts.length === 0) return {
|
|
556
|
+
section: "",
|
|
557
|
+
stats: {
|
|
558
|
+
staticFiles: 0,
|
|
559
|
+
scopedRules: scoped.length,
|
|
560
|
+
imports: collected.imports.length,
|
|
561
|
+
skipped: collected.skipped.length,
|
|
562
|
+
truncated,
|
|
563
|
+
bytes: 0
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
const section = `${PI_BRIDGE_FENCE_BEGIN}\n\n${parts.join("\n").trimEnd()}\n\n${PI_BRIDGE_FENCE_END}\n`;
|
|
567
|
+
return {
|
|
568
|
+
section,
|
|
569
|
+
stats: {
|
|
570
|
+
staticFiles: collected.copilotInstructions.length + collected.unscopedRules.length + collected.userGlobal.length,
|
|
571
|
+
scopedRules: scoped.length,
|
|
572
|
+
imports: collected.imports.length,
|
|
573
|
+
skipped: collected.skipped.length,
|
|
574
|
+
truncated,
|
|
575
|
+
bytes: byteLength(section)
|
|
576
|
+
}
|
|
577
|
+
};
|
|
578
|
+
}
|
|
579
|
+
/**
|
|
580
|
+
* Merge a bridge section into mirror `AGENTS.md`: append when absent,
|
|
581
|
+
* replace the fenced block when present.
|
|
582
|
+
*
|
|
583
|
+
* Byte-stable: merging the same section repeatedly must not grow the file. The
|
|
584
|
+
* obvious implementation (`before + section + after`) is NOT, because `section`
|
|
585
|
+
* ends with a newline and `after` starts with one, so every relaunch appended a
|
|
586
|
+
* blank line that nothing ever removed. The result is normalized here instead:
|
|
587
|
+
* `section` contributes exactly one trailing newline and `after` contributes
|
|
588
|
+
* none of its leading ones.
|
|
589
|
+
*
|
|
590
|
+
* An empty `section` means the bridge found nothing to say: return the existing
|
|
591
|
+
* user content untouched, but STRIP any fence this launcher wrote previously.
|
|
592
|
+
* A stale block advertises guidance that no longer exists and still costs prompt
|
|
593
|
+
* bytes every turn, so leaving it in place is worse than writing nothing.
|
|
594
|
+
*/
|
|
595
|
+
function mergeBridgeIntoAgentsMd(existing, section) {
|
|
596
|
+
const body = (existing ?? "").replace(/\r\n/g, "\n");
|
|
597
|
+
const begin = body.indexOf(PI_BRIDGE_FENCE_BEGIN);
|
|
598
|
+
const end = body.indexOf(PI_BRIDGE_FENCE_END);
|
|
599
|
+
const fenced = begin !== -1 && end !== -1 && end > begin;
|
|
600
|
+
if (!section.trim()) {
|
|
601
|
+
if (!fenced) return existing ?? "";
|
|
602
|
+
const before = body.slice(0, begin).replace(/\n*$/, "");
|
|
603
|
+
const after = body.slice(end + 36).replace(/^\n+/, "").trimEnd();
|
|
604
|
+
if (!before) return after ? `${after}\n` : "";
|
|
605
|
+
return after ? `${before}\n\n${after}\n` : `${before}\n`;
|
|
606
|
+
}
|
|
607
|
+
const block = `${section.trimEnd()}\n`;
|
|
608
|
+
if (!body.trim()) return block;
|
|
609
|
+
if (fenced) {
|
|
610
|
+
const before = body.slice(0, begin).replace(/\n*$/, "");
|
|
611
|
+
const after = body.slice(end + 36).replace(/^\n+/, "").trimEnd();
|
|
612
|
+
return `${before ? `${before}\n\n` : ""}${block}${after ? `\n${after}\n` : ""}`;
|
|
613
|
+
}
|
|
614
|
+
return `${body.replace(/\n*$/, "")}\n\n${block}`;
|
|
615
|
+
}
|
|
616
|
+
export { buildMirrorBridgeSection as a, extractAtImports as c, redactSecrets as d, splitFrontmatter as f, PI_BRIDGE_STATIC_BUDGET_BYTES as i, extractScopeGlobs as l, PI_BRIDGE_FENCE_END as n, capBytes as o, PI_BRIDGE_SECTION_FLOOR_BYTES as r, collectBridgeInputs as s, PI_BRIDGE_FENCE_BEGIN as t, mergeBridgeIntoAgentsMd as u };
|
|
617
|
+
|
|
618
|
+
//# sourceMappingURL=pi-memory-bridge-BxmCIIbk.js.map
|