pi-mega-compact 0.9.2 → 0.10.1
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 +3 -2
- package/dist/extensions/dashboard-server/api-contracts/endpoints.js +95 -47
- package/dist/extensions/dashboard-server/api-contracts/index.js +1 -1
- package/dist/extensions/dashboard-server/api-contracts/turns.js +10 -0
- package/dist/extensions/dashboard-server/api-contracts.test.js +3 -2
- package/dist/extensions/dashboard-server/routes-topics.js +87 -0
- package/dist/extensions/dashboard-server/routes-turns.js +251 -0
- package/dist/extensions/dashboard-server/routes.js +3 -1
- package/dist/extensions/dashboard-server/server.js +6 -2
- package/dist/extensions/mega-compact.js +10 -6
- package/dist/extensions/mega-config.js +4 -0
- package/dist/extensions/mega-events/agent-handlers.js +62 -49
- package/dist/extensions/mega-events/context-handler.js +48 -2
- package/dist/extensions/mega-events/session-handlers.js +24 -0
- package/dist/extensions/mega-metrics-cmds.js +90 -0
- package/dist/extensions/mega-pipeline/recall.js +47 -17
- package/dist/extensions/mega-topics-cmds.js +111 -0
- package/dist/extensions/mega-turn-store.js +110 -0
- package/dist/extensions/mega-turn-store.test.js +68 -0
- package/dist/src/config/turns.js +48 -0
- package/dist/src/fork.js +33 -0
- package/dist/src/fork.test.js +81 -0
- package/dist/src/intent.js +51 -0
- package/dist/src/intent.test.js +78 -0
- package/dist/src/metrics/index.js +4 -0
- package/dist/src/metrics/turns.js +118 -0
- package/dist/src/metrics/turns.test.js +152 -0
- package/dist/src/store/sqlite/raw-transcript.js +5 -3
- package/dist/src/store/sqlite.dbmirror.test.js +9 -0
- package/dist/src/store/turns/connection.js +123 -0
- package/dist/src/store/turns/contract-compliance.test.js +428 -0
- package/dist/src/store/turns/index.js +31 -0
- package/dist/src/store/turns/memory-store.js +357 -0
- package/dist/src/store/turns/memory-store.test.js +9 -0
- package/dist/src/store/turns/migrations.js +178 -0
- package/dist/src/store/turns/migrations.test.js +138 -0
- package/dist/src/store/turns/schema.js +115 -0
- package/dist/src/store/turns/sqlite-store.js +455 -0
- package/dist/src/store/turns/sqlite-store.test.js +144 -0
- package/dist/src/store/turns/types.js +16 -0
- package/dist/src/topics/cluster.js +141 -0
- package/dist/src/topics/cluster.test.js +150 -0
- package/dist/src/topics/index.js +6 -0
- package/dist/src/topics/kselection.js +142 -0
- package/dist/src/topics/labels.js +56 -0
- package/dist/src/topics/store.js +119 -0
- package/dist/src/topics/store.test.js +117 -0
- package/dist/src/topics/types.js +8 -0
- package/dist/src/wiki.js +133 -0
- package/dist/src/wiki.test.js +171 -0
- package/extensions/dashboard-client/dist/assets/{AchievementTiles-QPo46Pwg.js → AchievementTiles-DUulvXUY.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{AchievementTiles-QPo46Pwg.js.map → AchievementTiles-DUulvXUY.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{AchievementsTab-BDJxRXRY.js → AchievementsTab-Dx0YtCNf.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{AchievementsTab-BDJxRXRY.js.map → AchievementsTab-Dx0YtCNf.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{CacheTab-CUMqXQjd.js → CacheTab-DtkaUo1v.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{CacheTab-CUMqXQjd.js.map → CacheTab-DtkaUo1v.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{ConfigTab-j1dRzurG.js → ConfigTab-CLa4KfiZ.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{ConfigTab-j1dRzurG.js.map → ConfigTab-CLa4KfiZ.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{EventsTab-CMglXK4p.js → EventsTab-Q6tEIBBi.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{EventsTab-CMglXK4p.js.map → EventsTab-Q6tEIBBi.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{GameTab-fpaDoyxi.js → GameTab-Hq7GNWSY.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{GameTab-fpaDoyxi.js.map → GameTab-Hq7GNWSY.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MetricsTab-CWG2crG_.js → MetricsTab-BtmBV6lU.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MetricsTab-CWG2crG_.js.map → MetricsTab-BtmBV6lU.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{OverviewTab-BaV2D2nf.js → OverviewTab-CL10PwVU.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{OverviewTab-BaV2D2nf.js.map → OverviewTab-CL10PwVU.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{ReposTab-C-0kuZ_0.js → ReposTab-DAwANwbI.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{ReposTab-C-0kuZ_0.js.map → ReposTab-DAwANwbI.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{SessionsTab-CSYhddCA.js → SessionsTab-Ec08TZRQ.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{SessionsTab-CSYhddCA.js.map → SessionsTab-Ec08TZRQ.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{TimeSavedCard-Dy4KRjSM.js → TimeSavedCard-COb8dpTy.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TimeSavedCard-Dy4KRjSM.js.map → TimeSavedCard-COb8dpTy.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/TopicsTab-kvX3F3dY.js +2 -0
- package/extensions/dashboard-client/dist/assets/TopicsTab-kvX3F3dY.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/TurnsTab-CCUE73Aa.js +2 -0
- package/extensions/dashboard-client/dist/assets/TurnsTab-CCUE73Aa.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{index-D_WtU2TV.js → index-UMrussFX.js} +10 -10
- package/extensions/dashboard-client/dist/assets/index-UMrussFX.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{index-og3ghxyY.css → index-gn66lYD2.css} +1 -1
- package/extensions/dashboard-client/dist/assets/{useSSE-C9yVJlGP.js → useSSE-BDHZ7CeS.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{useSSE-C9yVJlGP.js.map → useSSE-BDHZ7CeS.js.map} +1 -1
- package/extensions/dashboard-client/dist/index.html +2 -2
- package/extensions/dashboard-client/src/App.tsx +10 -2
- package/extensions/dashboard-client/src/api/client.ts +84 -1
- package/extensions/dashboard-client/src/main.tsx +1 -0
- package/extensions/dashboard-client/src/styles/turns.css +142 -0
- package/extensions/dashboard-client/src/tabs/TopicsTab.tsx +185 -0
- package/extensions/dashboard-client/src/tabs/TurnsTab.tsx +326 -0
- package/extensions/dashboard-server/api-contracts/endpoints.ts +389 -291
- package/extensions/dashboard-server/api-contracts/game-types.ts +141 -88
- package/extensions/dashboard-server/api-contracts/index.ts +153 -133
- package/extensions/dashboard-server/api-contracts/turns.ts +114 -0
- package/extensions/dashboard-server/api-contracts.test.ts +920 -919
- package/extensions/dashboard-server/routes-topics.ts +119 -0
- package/extensions/dashboard-server/routes-turns.ts +282 -0
- package/extensions/dashboard-server/routes.ts +8 -1
- package/extensions/dashboard-server/server.ts +6 -2
- package/extensions/mega-compact.ts +75 -59
- package/extensions/mega-config.ts +232 -220
- package/extensions/mega-events/agent-handlers.ts +194 -151
- package/extensions/mega-events/context-handler.ts +50 -1
- package/extensions/mega-events/session-handlers.ts +23 -0
- package/extensions/mega-metrics-cmds.ts +114 -0
- package/extensions/mega-pipeline/recall.ts +215 -152
- package/extensions/mega-topics-cmds.ts +151 -0
- package/extensions/mega-turn-store.test.ts +108 -0
- package/extensions/mega-turn-store.ts +169 -0
- package/package.json +3 -1
- package/src/config/turns.ts +69 -0
- package/src/fork.test.ts +94 -0
- package/src/fork.ts +78 -0
- package/src/intent.test.ts +87 -0
- package/src/intent.ts +115 -0
- package/src/metrics/index.ts +9 -0
- package/src/metrics/turns.test.ts +165 -0
- package/src/metrics/turns.ts +201 -0
- package/src/store/sqlite/raw-transcript.ts +9 -3
- package/src/store/sqlite.dbmirror.test.ts +10 -0
- package/src/store/turns/connection.ts +137 -0
- package/src/store/turns/contract-compliance.test.ts +562 -0
- package/src/store/turns/index.ts +65 -0
- package/src/store/turns/memory-store.test.ts +15 -0
- package/src/store/turns/memory-store.ts +469 -0
- package/src/store/turns/migrations.test.ts +161 -0
- package/src/store/turns/migrations.ts +203 -0
- package/src/store/turns/schema.ts +158 -0
- package/src/store/turns/sqlite-store.test.ts +161 -0
- package/src/store/turns/sqlite-store.ts +656 -0
- package/src/store/turns/types.ts +181 -0
- package/src/topics/cluster.test.ts +176 -0
- package/src/topics/cluster.ts +191 -0
- package/src/topics/index.ts +18 -0
- package/src/topics/kselection.ts +182 -0
- package/src/topics/labels.ts +67 -0
- package/src/topics/store.test.ts +132 -0
- package/src/topics/store.ts +208 -0
- package/src/topics/types.ts +64 -0
- package/src/wiki.test.ts +217 -0
- package/src/wiki.ts +198 -0
- package/dist/extensions/openclaw-mega-compact.js +0 -291
- package/dist/src/minilm.js +0 -92
- package/dist/src/wordpiece.js +0 -129
- package/extensions/dashboard-client/dist/assets/index-D_WtU2TV.js.map +0 -1
|
@@ -17,11 +17,11 @@ import { execSync } from "node:child_process"; // guardrails-allow PREVENT-PI-00
|
|
|
17
17
|
* session trimmed. Explicit MEGACOMPACT_THRESHOLD_TOKENS always wins.
|
|
18
18
|
*/
|
|
19
19
|
export const COMPACT_TIERS = {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
low: 50_000,
|
|
21
|
+
medium: 100_000,
|
|
22
|
+
high: 200_000,
|
|
23
|
+
ultra: 1_000_000,
|
|
24
|
+
mega: 10_000_000,
|
|
25
25
|
} as const;
|
|
26
26
|
export type CompactTier = keyof typeof COMPACT_TIERS;
|
|
27
27
|
|
|
@@ -33,11 +33,11 @@ export type CompactTier = keyof typeof COMPACT_TIERS;
|
|
|
33
33
|
* is NOT scaled by this map — it stays an absolute token count.
|
|
34
34
|
*/
|
|
35
35
|
export const TIER_PCT: Record<CompactTier, number> = {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
36
|
+
low: 0.5,
|
|
37
|
+
medium: 0.6,
|
|
38
|
+
high: 0.7,
|
|
39
|
+
ultra: 0.7,
|
|
40
|
+
mega: 0.75,
|
|
41
41
|
};
|
|
42
42
|
|
|
43
43
|
/**
|
|
@@ -48,125 +48,126 @@ export const TIER_PCT: Record<CompactTier, number> = {
|
|
|
48
48
|
* band (see MegaRuntime.pressureBand), which climbs low→mega as context fills.
|
|
49
49
|
*/
|
|
50
50
|
export interface MegaConfig {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
51
|
+
tier: CompactTier | "custom";
|
|
52
|
+
/**
|
|
53
|
+
* Compaction threshold as a fraction of the model context window (e.g. 0.70
|
|
54
|
+
* for "high"). null for `custom` (explicit MEGACOMPACT_THRESHOLD_TOKENS, which
|
|
55
|
+
* stays an ABSOLUTE token count, never percent-scaled).
|
|
56
|
+
*/
|
|
57
|
+
tierPct: number | null;
|
|
58
|
+
thresholdTokens: number;
|
|
59
|
+
stateDir: string;
|
|
60
|
+
fastGatePct: number;
|
|
61
|
+
anchorUserMessages: number;
|
|
62
|
+
preserveRecent: number;
|
|
63
|
+
/** High-pressure floor for preserveRecent — when context is near the limit
|
|
64
|
+
* we compact deeper, but never below this (keeps recent turns for coherence). */
|
|
65
|
+
preserveRecentMin: number;
|
|
66
|
+
auto: boolean;
|
|
67
|
+
autoInline: boolean;
|
|
68
|
+
autoInlineK: number;
|
|
69
|
+
/** S28: auto-continue the agent after a max-output-token length stop by
|
|
70
|
+
* reusing the existing S16 resume-nudge. Default true. Off = silent (the
|
|
71
|
+
* prior behavior). PREVENT-PI-003: restart via user-role sendUserMessage. */
|
|
72
|
+
autoContinueLengthStop: boolean;
|
|
73
|
+
/** S38: max retries for transient errors (5xx/429/network/max-output-token
|
|
74
|
+
* text that is NOT a length stopReason — S28 owns those). Default 5.
|
|
75
|
+
* `0` disables all transient retries (reverts to S28-only). */
|
|
76
|
+
autoRetryTransientMax: number;
|
|
77
|
+
/** S38: max retries for permanent errors (auth/config/malformed). Default 1.
|
|
78
|
+
* `0` disables permanent-error retries. */
|
|
79
|
+
autoRetryPermanentMax: number;
|
|
80
|
+
/** S38.5: strict race-guard — 30s cooldown + deferred ctx.compact() re-check
|
|
81
|
+
* (closes the first-race-in-burst window). Default true. `false` reverts to
|
|
82
|
+
* the v0.7.4 synchronous 10s-cooldown behavior. */
|
|
83
|
+
raceGuardStrict: boolean;
|
|
84
|
+
/** S38.6: max consecutive errors before circuit-breaker trips (stops retrying).
|
|
85
|
+
* Default 10. When `errorRetryCount` exceeds this across multiple turns,
|
|
86
|
+
* the extension stops retrying until a successful turn resets it. */
|
|
87
|
+
maxConsecutiveErrors: number;
|
|
88
|
+
/** S38.7: hard-stop switch — when true, ALL error retries are disabled.
|
|
89
|
+
* Default false. Set via env to force S28-only behavior (length-stop continues
|
|
90
|
+
* only). */
|
|
91
|
+
errorRetryHardStop: boolean;
|
|
92
|
+
/** R1 (retry redesign): base unit (ms) for errorRetryBackoffMs(count) pacing.
|
|
93
|
+
* The schedule is base, 2*base, 4*base, 6*base (cap) — so the default 5000
|
|
94
|
+
* yields 5s/10s/20s/30s. errorRetryUntil is now GATING (previously it was
|
|
95
|
+
* documented as non-gating); a nudge cannot fire before errorRetryUntil
|
|
96
|
+
* elapses. */
|
|
97
|
+
errorRetryBackoffMs: number;
|
|
98
|
+
/** R2: session-global cap on total S38 nudges across ALL bursts. Hitting it
|
|
99
|
+
* is terminal for the session — the extension stops nudging entirely,
|
|
100
|
+
* independent of the per-burst max and the circuit breaker. Default 3.
|
|
101
|
+
* `0` disables (reverts to per-burst + circuit-breaker only). */
|
|
102
|
+
errorRetrySessionMax: number;
|
|
103
|
+
/** R3: consecutive identical error-text count at which a 'transient'
|
|
104
|
+
* classification is upgraded to 'poisoned-context' (the stateful repeat
|
|
105
|
+
* signal). Default 3. Raise to make the upgrade less aggressive. */
|
|
106
|
+
poisonedContextRepeatThreshold: number;
|
|
107
|
+
/** S29: override the auto-compact fire point for tiered configs, as a
|
|
108
|
+
* fraction of the context window (e.g. 0.85). null = inherit the tier's
|
|
109
|
+
* tierPct (default; preserves existing fire points). The context-handler
|
|
110
|
+
* gate fires on context % (reliable), not token count (under-reported),
|
|
111
|
+
* so it catches the overshoot that causes max-output-token truncation.
|
|
112
|
+
* `custom` (tierPct null) ignores this — it keeps the absolute token gate. */
|
|
113
|
+
autoPctTrigger: number | null;
|
|
114
|
+
dedupSim: number;
|
|
115
|
+
/** RAPTOR hierarchical recall enabled (Fix D). Drives both live recall and
|
|
116
|
+
* the durable-trim summary source (root summary). */
|
|
117
|
+
raptorEnabled: boolean;
|
|
118
|
+
/** Legacy v0.4.28 behavior: auto-trigger calls ctx.compact() (which STOPS
|
|
119
|
+
* the agent). Default false — the S16 redesign uses the live context-event
|
|
120
|
+
* trim + pi native auto-compaction instead (compact and continue). Kept for
|
|
121
|
+
* one release as rollback. */
|
|
122
|
+
legacyDurableTrim: boolean;
|
|
123
|
+
/** S27: durable raw-transcript DB mirror (MEGACOMPACT_DB_MIRROR). When on,
|
|
124
|
+
* raw message bytes + checkpoint-epoch bookkeeping are appended to the
|
|
125
|
+
* SQLite store so a compacted window can be rehydrated locally instead of
|
|
126
|
+
* from the pi runtime transcript. Default OFF — additive, no behavior
|
|
127
|
+
* change until flipped on. legacyDurableTrim takes precedence (the legacy
|
|
128
|
+
* v0.4.28 ctx.compact() path does not emit the S27 mirror hook). */
|
|
129
|
+
dbMirror: boolean;
|
|
130
|
+
/** S49: isolated per-turn store (turns.db). Default ON. OFF = legacy main-db
|
|
131
|
+
* turn path (S48 behavior — byte-identical). Mirrors TurnsConfig.TURNS_DB_ENABLED. */
|
|
132
|
+
turnsDbEnabled: boolean;
|
|
133
|
+
/** S51: auto-categorizing wiki (k-means + TF-IDF over real embeddings). Default ON.
|
|
134
|
+
* Mirrors TurnsConfig.AUTO_WIKI_ENABLED. Rebuild fires every Nth compaction. */
|
|
135
|
+
autoWikiEnabled: boolean;
|
|
136
|
+
/** Cross-repo recall enabled (S17). Resume + /mega-recall --cross-repo can
|
|
137
|
+
* pull checkpoints from OTHER repos via the PGlite HNSW index. Default true. */
|
|
138
|
+
crossRepoEnabled: boolean;
|
|
139
|
+
/** Stricter cosine floor for cross-repo hits (S17). Default 0.90 (trigram) /
|
|
140
|
+
* tighter than same-repo so only genuinely-relevant cross-repo context is
|
|
141
|
+
* injected. */
|
|
142
|
+
crossRepoCosine: number;
|
|
143
|
+
/** Memory-RAG auto-review enabled (S20). Every memoryReviewInterval turns the
|
|
144
|
+
* conversation is auto-reviewed into durable add/replace/remove memories. */
|
|
145
|
+
memoryAutoReview: boolean;
|
|
146
|
+
/** Turn cadence for the auto-review scan (S20). Default 10. */
|
|
147
|
+
memoryReviewInterval: number;
|
|
148
|
+
/** Token ceiling for the re-injected recall block (Fix C). Recall stops
|
|
149
|
+
* adding checkpoints once the block would exceed this — bounds read-path
|
|
150
|
+
* token cost so it can never net-inflate the window. */
|
|
151
|
+
recallMaxTokens: number;
|
|
152
|
+
/** Inline-dedupe recalled checkpoints against the live window (Fix C): drop
|
|
153
|
+
* a hit whose summary is ≥ dedupSim similar to a live message — "dedupe on
|
|
154
|
+
* inline/read" so we never re-inject context already resident. */
|
|
155
|
+
windowDedupe: boolean;
|
|
156
|
+
debug: boolean;
|
|
157
|
+
/** Master reconciliation: TUI shutdown widget (MEGACOMPACT_TUI_WIDGET=0 to disable). */
|
|
158
|
+
tuiWidget: boolean;
|
|
158
159
|
}
|
|
159
160
|
|
|
160
161
|
function envFlag(name: string, fallback: number): number {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
162
|
+
const v = process.env[name];
|
|
163
|
+
if (v == null || v === "") return fallback;
|
|
164
|
+
const n = Number(v);
|
|
165
|
+
return Number.isFinite(n) ? n : fallback;
|
|
165
166
|
}
|
|
166
167
|
function envBool(name: string, fallback: boolean): boolean {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
168
|
+
const v = process.env[name];
|
|
169
|
+
if (v == null || v === "") return fallback;
|
|
170
|
+
return v === "true" || v === "1";
|
|
170
171
|
}
|
|
171
172
|
|
|
172
173
|
/**
|
|
@@ -179,21 +180,22 @@ function envBool(name: string, fallback: boolean): boolean {
|
|
|
179
180
|
* keeps `tierPct: null` and an ABSOLUTE `thresholdTokens` (never percent-scaled).
|
|
180
181
|
*/
|
|
181
182
|
function resolveThreshold(): {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
183
|
+
tier: CompactTier | "custom";
|
|
184
|
+
tierPct: number | null;
|
|
185
|
+
thresholdTokens: number;
|
|
185
186
|
} {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
187
|
+
const explicit = process.env.MEGACOMPACT_THRESHOLD_TOKENS;
|
|
188
|
+
if (explicit != null && explicit !== "") {
|
|
189
|
+
const n = Number(explicit);
|
|
190
|
+
if (Number.isFinite(n))
|
|
191
|
+
return { tier: "custom", tierPct: null, thresholdTokens: n };
|
|
192
|
+
}
|
|
193
|
+
const raw = (process.env.MEGACOMPACT_TIER ?? "low").toLowerCase();
|
|
194
|
+
const tier = (raw in COMPACT_TIERS ? raw : "low") as CompactTier;
|
|
195
|
+
const tierPct = TIER_PCT[tier];
|
|
196
|
+
// Boot fallback: sane gate before the first context event provides a window.
|
|
197
|
+
const thresholdTokens = Math.round(tierPct * 200_000);
|
|
198
|
+
return { tier, tierPct, thresholdTokens };
|
|
197
199
|
}
|
|
198
200
|
|
|
199
201
|
/**
|
|
@@ -209,17 +211,17 @@ function resolveThreshold(): {
|
|
|
209
211
|
* unit-testable without the pi runtime.
|
|
210
212
|
*/
|
|
211
213
|
export function effectiveThresholdTokens(opts: {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
214
|
+
tierPct: number | null;
|
|
215
|
+
fallbackThreshold: number;
|
|
216
|
+
window: number;
|
|
217
|
+
explicitThreshold?: number;
|
|
216
218
|
}): number {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
219
|
+
if (opts.tierPct == null) {
|
|
220
|
+
// custom: absolute threshold, never percent-scaled
|
|
221
|
+
return opts.explicitThreshold ?? opts.fallbackThreshold;
|
|
222
|
+
}
|
|
223
|
+
if (opts.window > 0) return Math.round(opts.tierPct * opts.window);
|
|
224
|
+
return opts.fallbackThreshold;
|
|
223
225
|
}
|
|
224
226
|
|
|
225
227
|
/**
|
|
@@ -229,12 +231,12 @@ export function effectiveThresholdTokens(opts: {
|
|
|
229
231
|
* `custom` (tierPct null) falls back to the legacy 70% default.
|
|
230
232
|
*/
|
|
231
233
|
function resolveFastGatePct(tierPct: number | null): number {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
234
|
+
const raw = process.env.MEGACOMPACT_FAST_GATE_PCT;
|
|
235
|
+
if (raw != null && raw !== "") {
|
|
236
|
+
const n = Number(raw);
|
|
237
|
+
if (Number.isFinite(n)) return n;
|
|
238
|
+
}
|
|
239
|
+
return tierPct != null ? Math.round(tierPct * 100) : 70;
|
|
238
240
|
}
|
|
239
241
|
|
|
240
242
|
/**
|
|
@@ -245,63 +247,73 @@ function resolveFastGatePct(tierPct: number | null): number {
|
|
|
245
247
|
* "how full" signal that drives the tier label, trim depth, and memory cadence.)
|
|
246
248
|
*/
|
|
247
249
|
export {
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
250
|
+
pressureFromPct,
|
|
251
|
+
preserveRecentForPressure,
|
|
252
|
+
pressureRatio,
|
|
253
|
+
pressureBand,
|
|
254
|
+
memoryReviewCadence,
|
|
255
|
+
type PressureBand,
|
|
254
256
|
} from "../src/config.js";
|
|
255
257
|
|
|
256
258
|
/** Build the resolved config from env + defaults. */
|
|
257
259
|
export function loadConfig(): MegaConfig {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
260
|
+
const { tier, tierPct, thresholdTokens } = resolveThreshold();
|
|
261
|
+
// S29: optional percent-based fire-point override for tiered configs.
|
|
262
|
+
// null = inherit tierPct (default; preserves existing fire points). Clamped
|
|
263
|
+
// to [0.1, 1] so a bogus env can't disable or invert the gate. Ignored by
|
|
264
|
+
// the `custom` tier (tierPct null) which keeps the absolute token gate.
|
|
265
|
+
const aptRaw = process.env.MEGACOMPACT_AUTO_PCT_TRIGGER;
|
|
266
|
+
const autoPctTrigger =
|
|
267
|
+
aptRaw && aptRaw !== "" && Number.isFinite(Number(aptRaw))
|
|
268
|
+
? Math.min(1, Math.max(0.1, Number(aptRaw)))
|
|
269
|
+
: null;
|
|
270
|
+
return {
|
|
271
|
+
tier,
|
|
272
|
+
tierPct,
|
|
273
|
+
// Global default; the live store/dashboard are rebound per-repo at runtime
|
|
274
|
+
// via MegaRuntime.bindRepo() so each git repo gets its own isolated state dir.
|
|
275
|
+
stateDir: process.env.MEGACOMPACT_STATE_DIR ?? STATE_DIR_DEFAULT,
|
|
276
|
+
fastGatePct: resolveFastGatePct(tierPct),
|
|
277
|
+
thresholdTokens,
|
|
278
|
+
anchorUserMessages: envFlag("MEGACOMPACT_ANCHOR_USER_MESSAGES", 3),
|
|
279
|
+
preserveRecent: envFlag("MEGACOMPACT_PRESERVE_RECENT", 4),
|
|
280
|
+
preserveRecentMin: envFlag("MEGACOMPACT_PRESERVE_RECENT_MIN", 2),
|
|
281
|
+
auto: envBool("MEGACOMPACT_AUTO", true),
|
|
282
|
+
autoInline: envBool("MEGACOMPACT_AUTO_INLINE", true),
|
|
283
|
+
autoContinueLengthStop: envBool(
|
|
284
|
+
"MEGACOMPACT_AUTO_CONTINUE_LENGTH_STOP",
|
|
285
|
+
true,
|
|
286
|
+
),
|
|
287
|
+
autoRetryTransientMax: envFlag("MEGACOMPACT_AUTO_RETRY_TRANSIENT_MAX", 5),
|
|
288
|
+
autoRetryPermanentMax: envFlag("MEGACOMPACT_AUTO_RETRY_PERMANENT_MAX", 1),
|
|
289
|
+
raceGuardStrict: envBool("MEGACOMPACT_RACE_GUARD_STRICT", true),
|
|
290
|
+
maxConsecutiveErrors: envFlag("MEGACOMPACT_MAX_CONSECUTIVE_ERRORS", 10),
|
|
291
|
+
errorRetryHardStop: envBool("MEGACOMPACT_ERROR_RETRY_HARD_STOP", false),
|
|
292
|
+
errorRetryBackoffMs: envFlag("MEGACOMPACT_ERROR_RETRY_BACKOFF_MS", 5000),
|
|
293
|
+
errorRetrySessionMax: envFlag("MEGACOMPACT_ERROR_RETRY_SESSION_MAX", 3),
|
|
294
|
+
poisonedContextRepeatThreshold: envFlag(
|
|
295
|
+
"MEGACOMPACT_POISONED_REPEAT_THRESHOLD",
|
|
296
|
+
3,
|
|
297
|
+
),
|
|
298
|
+
autoPctTrigger,
|
|
299
|
+
autoInlineK: envFlag("MEGACOMPACT_AUTO_INLINE_K", 3),
|
|
300
|
+
dedupSim: Number(process.env.MEGACOMPACT_DEDUP_SIM ?? "0.9"),
|
|
301
|
+
raptorEnabled: envBool("MEGACOMPACT_RAPTOR_ENABLED", true),
|
|
302
|
+
legacyDurableTrim: envBool("MEGACOMPACT_LEGACY_DURABLE_TRIM", false),
|
|
303
|
+
dbMirror: envBool("MEGACOMPACT_DB_MIRROR", false),
|
|
304
|
+
// S49: isolated per-turn store (turns.db). Default ON. OFF = legacy main-db
|
|
305
|
+
// turn path (S48 behavior). Mirrors src/config/turns.ts TURNS_DB_ENABLED.
|
|
306
|
+
turnsDbEnabled: envBool("MEGACOMPACT_TURNS_DB", true),
|
|
307
|
+
autoWikiEnabled: envBool("MEGACOMPACT_AUTO_WIKI", true),
|
|
308
|
+
crossRepoEnabled: envBool("MEGACOMPACT_CROSSREPO_ENABLED", true),
|
|
309
|
+
crossRepoCosine: Number(process.env.MEGACOMPACT_CROSSREPO_COSINE ?? "0.90"),
|
|
310
|
+
memoryAutoReview: envBool("MEGACOMPACT_MEMORY_AUTO_REVIEW", true),
|
|
311
|
+
memoryReviewInterval: envFlag("MEGACOMPACT_MEMORY_REVIEW_INTERVAL", 10),
|
|
312
|
+
recallMaxTokens: envFlag("MEGACOMPACT_RECALL_MAX_TOKENS", 1500),
|
|
313
|
+
windowDedupe: envBool("MEGACOMPACT_WINDOW_DEDUPE", true),
|
|
314
|
+
tuiWidget: envBool("MEGACOMPACT_TUI_WIDGET", true),
|
|
315
|
+
debug: envBool("MEGACOMPACT_DEBUG", false),
|
|
316
|
+
};
|
|
305
317
|
}
|
|
306
318
|
|
|
307
319
|
/**
|
|
@@ -316,16 +328,16 @@ export function loadConfig(): MegaConfig {
|
|
|
316
328
|
* non-git directory (caller falls back to a global state dir).
|
|
317
329
|
*/
|
|
318
330
|
export function resolveRepoRoot(cwd: string): string | undefined {
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
331
|
+
try {
|
|
332
|
+
const out = execSync("git rev-parse --show-toplevel", {
|
|
333
|
+
cwd,
|
|
334
|
+
encoding: "utf-8",
|
|
335
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
336
|
+
}).trim();
|
|
337
|
+
return out || undefined;
|
|
338
|
+
} catch {
|
|
339
|
+
return undefined;
|
|
340
|
+
}
|
|
329
341
|
}
|
|
330
342
|
|
|
331
343
|
/**
|
|
@@ -334,7 +346,7 @@ export function resolveRepoRoot(cwd: string): string | undefined {
|
|
|
334
346
|
* cwds (the explicit MEGACOMPACT_STATE_DIR override, if set).
|
|
335
347
|
*/
|
|
336
348
|
export function repoStateDir(cwd: string, fallback: string): string {
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
349
|
+
const root = resolveRepoRoot(cwd);
|
|
350
|
+
if (!root) return fallback;
|
|
351
|
+
return join(root, ".pi", "mega-compact");
|
|
340
352
|
}
|