pi-fold 0.0.1 → 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.
@@ -0,0 +1,233 @@
1
+ import type { EvidenceRef } from "../json.ts";
2
+
3
+ export const DEFAULT_ACTIVE_CONTEXT_TOOL_NAME = "active_context";
4
+ export const DEFAULT_ACTIVE_CONTEXT_ENTRY_TYPE_PREFIX = "pi-fold-active-context";
5
+ export const DEFAULT_ACTIVE_CONTEXT_TOOL_LABEL = "Active Context";
6
+ export const DEFAULT_ACTIVE_CONTEXT_BRAND_NOUN = "active-context";
7
+ export const DEFAULT_ACTIVE_CONTEXT_COMMAND_NAMES = Object.freeze({
8
+ status: "context",
9
+ fold: "fold-context",
10
+ });
11
+
12
+ export function entryTypeNamespace(entryTypePrefix: string): string {
13
+ const suffix = "-active-context";
14
+ return entryTypePrefix.endsWith(suffix) && entryTypePrefix.length > suffix.length
15
+ ? entryTypePrefix.slice(0, -suffix.length)
16
+ : entryTypePrefix;
17
+ }
18
+
19
+ export function activeContextSource(entryTypePrefix: string): string {
20
+ const namespace = entryTypeNamespace(entryTypePrefix);
21
+ return namespace === entryTypePrefix ? entryTypePrefix : `${namespace}/active-context`;
22
+ }
23
+
24
+ export function activeContextBrand(brandNoun: string): string {
25
+ return /active-context$/i.test(brandNoun) ? brandNoun : `${brandNoun} active-context`;
26
+ }
27
+
28
+ export function contextBrand(brandNoun: string): string {
29
+ return /context$/i.test(brandNoun) ? brandNoun : `${brandNoun} context`;
30
+ }
31
+
32
+ const DEFAULT_ENTRY_NAMESPACE = entryTypeNamespace(DEFAULT_ACTIVE_CONTEXT_ENTRY_TYPE_PREFIX);
33
+ export const ACTIVE_CONTEXT_STATE_ENTRY = `${DEFAULT_ACTIVE_CONTEXT_ENTRY_TYPE_PREFIX}-state`;
34
+ export const ACTIVE_CONTEXT_FOLD_RECORD_ENTRY = `${DEFAULT_ACTIVE_CONTEXT_ENTRY_TYPE_PREFIX}-fold-record`;
35
+ export const NATIVE_COMPACTION_RECEIPT_ENTRY = `${DEFAULT_ENTRY_NAMESPACE}-native-compaction-receipt`;
36
+ export const NATIVE_COMPACTION_DECISION_ENTRY = `${DEFAULT_ENTRY_NAMESPACE}-native-compaction-decision`;
37
+ export const PROVIDER_CONTEXT_MEASUREMENT_ENTRY = `${DEFAULT_ENTRY_NAMESPACE}-provider-context-measurement`;
38
+ export const ACTIVE_CONTEXT_STATUS_KEY = DEFAULT_ACTIVE_CONTEXT_ENTRY_TYPE_PREFIX;
39
+ export const ACTIVE_CONTEXT_TOOL_ACTIONS = Object.freeze([
40
+ "status", "fold", "expand", "refold", "protect", "unprotect",
41
+ ] as const);
42
+ export type ActiveContextToolAction = typeof ACTIVE_CONTEXT_TOOL_ACTIONS[number];
43
+ export const USER_RESCUE_MAX_SOURCE_CHARS = 512_000;
44
+ export const DEFAULT_CONTEXT_WINDOW = 272_000;
45
+ export const TOOL_FOLD_CADENCE_MIN_TOKENS = 12_000;
46
+ export const TOOL_FOLD_CADENCE_WINDOW_FRACTION = 0.06;
47
+ export const EXPAND_LEASE_GENERATIONS = 8;
48
+ export const MAX_EXPAND_LEASES = 64;
49
+ export const CONSOLIDATION_WIDTH_THRESHOLD = 10;
50
+ export const MAX_ADVISORY_DELIVERIES_PER_MILESTONE = 16;
51
+
52
+ // Conservative LOWER bound on UTF-8 bytes per provider token, used only to cap
53
+ // the protected byte tail as a share of a small window — never to estimate usage.
54
+ export const BYTES_PER_TOKEN_FLOOR = 2;
55
+
56
+ export const ACTIVE_CONTEXT_POLICY = Object.freeze({
57
+ freshTurns: 3,
58
+ freshBytes: 24_000,
59
+ freshWindowShare: 0.25,
60
+ warningRatio: 0.65,
61
+ toolFoldRatio: 0.75,
62
+ refoldRatio: 0.85,
63
+ prepareRatio: 0.90,
64
+ warmRatio: 0.55,
65
+ responseReserve: 16_384,
66
+ consolidationRatio: 0.85,
67
+ consolidationChildren: 5,
68
+ maxConsolidationChildren: 8,
69
+ minToolChars: 2_000,
70
+ minChapterChars: 4_000,
71
+ maxChapterChars: 128_000,
72
+ maxChapterTurns: 4,
73
+ maxSourceChars: 200_000,
74
+ maxFoldSourceRefs: 256,
75
+ maxBriefChars: 1_200,
76
+ briefTimeoutMs: 120_000,
77
+ orientationMessages: 2,
78
+ maxOrientationChars: 12_000,
79
+ });
80
+
81
+ export type FoldKind = "tool-result" | "chapter" | "consolidation";
82
+ export type FoldPart = { kind: "raw"; ref: EvidenceRef } | { kind: "fold"; foldId: string };
83
+ export type BriefProvenance =
84
+ | { kind: "supplied" }
85
+ | { kind: "deterministic" }
86
+ | {
87
+ kind: "model" | "luna";
88
+ provider: string;
89
+ model: string;
90
+ effort: string;
91
+ launchContractDigest?: string;
92
+ };
93
+
94
+ export interface ActiveFold {
95
+ id: string;
96
+ kind: FoldKind;
97
+ parentId: string | null;
98
+ parts: FoldPart[];
99
+ brief: string;
100
+ provenance: BriefProvenance;
101
+ sourceSha256: string;
102
+ sourceChars: number;
103
+ placeholderChars: number;
104
+ createdAt: number;
105
+ }
106
+
107
+ export interface PreparedFold {
108
+ id: string;
109
+ sessionId: string;
110
+ generation: number;
111
+ branchSha256: string;
112
+ topologySha256: string;
113
+ protectionSha256: string;
114
+ sourceRefs: EvidenceRef[];
115
+ sourceSha256: string;
116
+ beforeRefs: EvidenceRef[];
117
+ beforeSha256: string;
118
+ afterRefs: EvidenceRef[];
119
+ afterSha256: string;
120
+ fold: ActiveFold;
121
+ }
122
+
123
+ export interface ActiveContextState {
124
+ version: 1;
125
+ sessionId: string;
126
+ revision: number;
127
+ folds: ActiveFold[];
128
+ expanded: string[];
129
+ protected: EvidenceRef[];
130
+ tokensSinceToolFold: number;
131
+ leases: Record<string, number>;
132
+ prepared?: PreparedFold;
133
+ advisory?: {
134
+ highWater: number;
135
+ delivered: Record<string, number>;
136
+ armed?: { milestone: AdvisoryMilestone; threshold: number; scheduleKey: string };
137
+ };
138
+ }
139
+
140
+ export interface FoldRecordRef {
141
+ id: string;
142
+ sha256: string;
143
+ }
144
+
145
+ export interface FoldRecordEntry {
146
+ version: 1;
147
+ sessionId: string;
148
+ foldId: string;
149
+ recordSha256: string;
150
+ fold: ActiveFold;
151
+ }
152
+
153
+ export interface ActiveContextCheckpointV2 {
154
+ version: 2;
155
+ kind: "checkpoint";
156
+ sessionId: string;
157
+ revision: number;
158
+ foldRefs: FoldRecordRef[];
159
+ expanded: string[];
160
+ protected: EvidenceRef[];
161
+ prepared: PreparedFold | null;
162
+ tokensSinceToolFold?: number;
163
+ leases?: Record<string, number>;
164
+ advisory?: NonNullable<ActiveContextState["advisory"]>;
165
+ stateSha256: string;
166
+ }
167
+
168
+ export interface ActiveContextDeltaV2 {
169
+ version: 2;
170
+ kind: "delta";
171
+ sessionId: string;
172
+ revision: number;
173
+ baseRevision: number;
174
+ baseStateSha256: string;
175
+ addFoldRefs: FoldRecordRef[];
176
+ removeFoldIds: string[];
177
+ expanded: string[];
178
+ protected: EvidenceRef[];
179
+ prepared: PreparedFold | null;
180
+ tokensSinceToolFold?: number;
181
+ leases?: Record<string, number>;
182
+ advisory?: NonNullable<ActiveContextState["advisory"]>;
183
+ stateSha256: string;
184
+ }
185
+
186
+ export type ActiveContextStateWireV2 = ActiveContextCheckpointV2 | ActiveContextDeltaV2;
187
+
188
+ export interface MappedMessage {
189
+ index: number;
190
+ message: unknown;
191
+ ref: EvidenceRef | null;
192
+ }
193
+
194
+ export interface BranchObject {
195
+ branchIndex: number;
196
+ message: unknown;
197
+ ref: EvidenceRef;
198
+ }
199
+
200
+ export interface CompleteTurn {
201
+ start: number;
202
+ end: number;
203
+ }
204
+
205
+ export interface ActiveContextSnapshot {
206
+ sessionId: string;
207
+ messages: unknown[];
208
+ mapped: MappedMessage[];
209
+ branchObjects: BranchObject[];
210
+ completeTurns: CompleteTurn[];
211
+ freshBoundary: number;
212
+ protectedIndices: Set<number>;
213
+ toolProtectedIndices: Set<number>;
214
+ policy: typeof ACTIVE_CONTEXT_POLICY;
215
+ toolName: string;
216
+ brandNoun: string;
217
+ entryTypePrefix: string;
218
+ readOnlyTools: ReadonlySet<string>;
219
+ contextWindow: number;
220
+ windowSource: "reported" | "fallback";
221
+ }
222
+
223
+ export interface FoldCandidate {
224
+ kind: FoldKind;
225
+ parts: FoldPart[];
226
+ sourceRefs: EvidenceRef[];
227
+ }
228
+
229
+ export const READ_ONLY_TOOLS_DEFAULT = new Set([
230
+ "read", "grep", "find", "ls",
231
+ ]);
232
+
233
+ export type AdvisoryMilestone = "notice" | "tools" | "chapters" | "urgent";