@narumitw/pi-codex-compact 0.50.0 → 0.50.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/checkpoint.ts +31 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@narumitw/pi-codex-compact",
3
- "version": "0.50.0",
3
+ "version": "0.50.1",
4
4
  "description": "Codex Remote Compaction V2 for Pi's OpenAI Codex provider.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/checkpoint.ts CHANGED
@@ -123,6 +123,15 @@ export function latestCheckpoint(entries: readonly SessionEntry[]):
123
123
  return undefined;
124
124
  }
125
125
 
126
+ function isOlderCompactionSummary(message: AgentMessage, timestamp: number): boolean {
127
+ return (
128
+ message.role === "compactionSummary" &&
129
+ Number.isFinite(message.timestamp) &&
130
+ Number.isFinite(timestamp) &&
131
+ message.timestamp < timestamp
132
+ );
133
+ }
134
+
126
135
  export function projectCheckpointContext(
127
136
  messages: readonly AgentMessage[],
128
137
  details: CodexCheckpointDetails,
@@ -132,21 +141,33 @@ export function projectCheckpointContext(
132
141
  (message) => message.role === "compactionSummary" && message.summary === summary,
133
142
  );
134
143
  if (summaryIndex < 0) return undefined;
135
- const keptStart = summaryIndex + 1;
136
- const keptEnd = keptStart + details.keptMessageFingerprints.length;
137
- if (keptEnd > messages.length) return undefined;
138
- for (let index = keptStart; index < keptEnd; index++) {
139
- if (
140
- fingerprintMessage(messages[index]) !== details.keptMessageFingerprints[index - keptStart]
141
- ) {
142
- return undefined;
144
+ const timestamp = messages[summaryIndex].timestamp;
145
+ let messageIndex = summaryIndex + 1;
146
+ let fingerprintIndex = 0;
147
+ while (fingerprintIndex < details.keptMessageFingerprints.length) {
148
+ if (messageIndex >= messages.length) return undefined;
149
+ const message = messages[messageIndex];
150
+ if (fingerprintMessage(message) === details.keptMessageFingerprints[fingerprintIndex]) {
151
+ messageIndex += 1;
152
+ fingerprintIndex += 1;
153
+ continue;
154
+ }
155
+ if (isOlderCompactionSummary(message, timestamp)) {
156
+ messageIndex += 1;
157
+ continue;
143
158
  }
159
+ return undefined;
160
+ }
161
+ while (
162
+ messageIndex < messages.length &&
163
+ isOlderCompactionSummary(messages[messageIndex], timestamp)
164
+ ) {
165
+ messageIndex += 1;
144
166
  }
145
- const timestamp = messages[summaryIndex].timestamp;
146
167
  return [
147
168
  ...messages.slice(0, summaryIndex),
148
169
  markerMessage(details.checkpointId, timestamp),
149
- ...messages.slice(keptEnd),
170
+ ...messages.slice(messageIndex),
150
171
  ];
151
172
  }
152
173