@oyasmi/pipiclaw 0.8.9 → 0.8.10-beta.2
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 -3
- package/dist/agent/channel-runner.d.ts +1 -0
- package/dist/agent/channel-runner.js +6 -10
- package/dist/agent/maintenance-context.d.ts +0 -1
- package/dist/agent/maintenance-context.js +0 -7
- package/dist/agent/session-events.d.ts +1 -0
- package/dist/agent/session-events.js +10 -0
- package/dist/agent/types.d.ts +7 -0
- package/dist/agent/types.js +2 -0
- package/dist/index.d.ts +16 -34
- package/dist/index.js +14 -32
- package/dist/memory/consolidation.js +0 -1
- package/dist/memory/extraction.d.ts +1 -7
- package/dist/memory/extraction.js +2 -46
- package/dist/memory/lifecycle.js +3 -3
- package/dist/memory/maintenance-gates.d.ts +4 -16
- package/dist/memory/maintenance-gates.js +11 -51
- package/dist/memory/maintenance-jobs.d.ts +3 -14
- package/dist/memory/maintenance-jobs.js +12 -103
- package/dist/memory/maintenance-state.d.ts +2 -6
- package/dist/memory/maintenance-state.js +14 -10
- package/dist/memory/maintenance-tuning.d.ts +37 -0
- package/dist/memory/maintenance-tuning.js +46 -0
- package/dist/memory/promotion.d.ts +0 -22
- package/dist/memory/promotion.js +1 -8
- package/dist/memory/review-log.d.ts +1 -1
- package/dist/memory/scheduler.d.ts +1 -9
- package/dist/memory/scheduler.js +3 -15
- package/dist/memory/source-window.d.ts +2 -2
- package/dist/memory/task-digest.js +2 -5
- package/dist/models/utils.d.ts +5 -0
- package/dist/models/utils.js +10 -0
- package/dist/paths.d.ts +0 -1
- package/dist/paths.js +0 -1
- package/dist/playbooks/task-closeout.md +2 -0
- package/dist/playbooks/task-delegation.md +8 -14
- package/dist/playbooks/task-driving.md +3 -4
- package/dist/playbooks/task-planning.md +8 -9
- package/dist/runtime/bootstrap.d.ts +14 -0
- package/dist/runtime/bootstrap.js +9 -4
- package/dist/runtime/task-commands.js +46 -73
- package/dist/runtime/task-driver.js +6 -23
- package/dist/settings.d.ts +39 -40
- package/dist/settings.js +87 -107
- package/dist/shared/task-ledger.d.ts +19 -2
- package/dist/shared/task-ledger.js +194 -5
- package/dist/subagents/discovery.d.ts +41 -7
- package/dist/subagents/discovery.js +74 -24
- package/dist/subagents/tool.d.ts +3 -12
- package/dist/subagents/tool.js +11 -115
- package/dist/tasks/control.d.ts +57 -27
- package/dist/tasks/control.js +94 -93
- package/dist/tasks/store.d.ts +1 -4
- package/dist/tasks/store.js +1 -39
- package/dist/tasks/transitions.d.ts +1 -1
- package/dist/tasks/transitions.js +1 -0
- package/dist/tasks/verification.d.ts +10 -6
- package/dist/tasks/verification.js +0 -6
- package/dist/tools/memory-manage.js +46 -24
- package/dist/tools/task-manage/create.js +1 -2
- package/dist/tools/task-manage/lifecycle.d.ts +7 -0
- package/dist/tools/task-manage/lifecycle.js +52 -32
- package/dist/tools/task-manage/schema.d.ts +2 -9
- package/dist/tools/task-manage/schema.js +9 -19
- package/dist/tools/task-manage/shared.d.ts +0 -2
- package/dist/tools/task-manage/shared.js +8 -69
- package/dist/tools/task-manage/types.d.ts +1 -1
- package/dist/tools/task-manage/verification.js +5 -6
- package/dist/tools/task-manage.js +4 -2
- package/examples/sub-agents/README.md +1 -1
- package/package.json +3 -1
- package/dist/memory/post-turn-review.d.ts +0 -43
- package/dist/memory/post-turn-review.js +0 -136
- package/dist/memory/promotion-signals.d.ts +0 -5
- package/dist/memory/promotion-signals.js +0 -33
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import type { AgentMessage } from "@earendil-works/pi-agent-core";
|
|
2
2
|
import type { Api, Model } from "@earendil-works/pi-ai";
|
|
3
3
|
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
4
|
-
import type {
|
|
4
|
+
import type { PipiclawMemoryMaintenanceSettings, PipiclawSessionMemorySettings } from "../settings.js";
|
|
5
5
|
import { type ChannelMemoryQueue } from "./channel-maintenance-queue.js";
|
|
6
6
|
import { type MaintenanceJobKind } from "./maintenance-gates.js";
|
|
7
7
|
export interface MaintenanceJobSettings {
|
|
8
8
|
sessionMemory: PipiclawSessionMemorySettings;
|
|
9
|
-
memoryGrowth: PipiclawMemoryGrowthSettings;
|
|
10
9
|
memoryMaintenance: PipiclawMemoryMaintenanceSettings;
|
|
11
10
|
}
|
|
12
11
|
interface BaseMaintenanceJobInput {
|
|
@@ -24,16 +23,7 @@ interface BaseMaintenanceJobInput {
|
|
|
24
23
|
}
|
|
25
24
|
export interface SessionRefreshJobInput extends BaseMaintenanceJobInput {
|
|
26
25
|
}
|
|
27
|
-
export interface
|
|
28
|
-
}
|
|
29
|
-
export interface GrowthReviewJobInput extends BaseMaintenanceJobInput {
|
|
30
|
-
workspaceDir: string;
|
|
31
|
-
loadedSkills: Array<{
|
|
32
|
-
name: string;
|
|
33
|
-
description?: string;
|
|
34
|
-
}>;
|
|
35
|
-
emitNotice?: (notice: string) => Promise<void>;
|
|
36
|
-
refreshWorkspaceResources?: () => Promise<void>;
|
|
26
|
+
export interface MemoryCheckpointJobInput extends BaseMaintenanceJobInput {
|
|
37
27
|
}
|
|
38
28
|
export interface StructuralMaintenanceJobInput extends BaseMaintenanceJobInput {
|
|
39
29
|
}
|
|
@@ -45,7 +35,6 @@ export interface MaintenanceJobResult {
|
|
|
45
35
|
error?: string;
|
|
46
36
|
}
|
|
47
37
|
export declare function runSessionRefreshJob(input: SessionRefreshJobInput): Promise<MaintenanceJobResult>;
|
|
48
|
-
export declare function
|
|
49
|
-
export declare function runGrowthReviewJob(input: GrowthReviewJobInput): Promise<MaintenanceJobResult>;
|
|
38
|
+
export declare function runMemoryCheckpointJob(input: MemoryCheckpointJobInput): Promise<MaintenanceJobResult>;
|
|
50
39
|
export declare function runStructuralMaintenanceJob(input: StructuralMaintenanceJobInput): Promise<MaintenanceJobResult>;
|
|
51
40
|
export {};
|
|
@@ -2,10 +2,8 @@ import { errorMessage } from "../shared/text-utils.js";
|
|
|
2
2
|
import { getDefaultChannelMemoryQueue } from "./channel-maintenance-queue.js";
|
|
3
3
|
import { cleanupChannelMemory, foldChannelHistory, getStructuralMaintenanceStats, runInlineConsolidation, } from "./consolidation.js";
|
|
4
4
|
import { readChannelHistory, readChannelMemory } from "./files.js";
|
|
5
|
-
import {
|
|
5
|
+
import { shouldRunMemoryCheckpoint, shouldRunSessionRefresh, shouldRunStructuralMaintenance, } from "./maintenance-gates.js";
|
|
6
6
|
import { readMemoryMaintenanceState, updateMemoryMaintenanceState } from "./maintenance-state.js";
|
|
7
|
-
import { runPostTurnReview } from "./post-turn-review.js";
|
|
8
|
-
import { scanPromotionSignals } from "./promotion-signals.js";
|
|
9
7
|
import { appendMemoryReviewLog } from "./review-log.js";
|
|
10
8
|
import { updateChannelSessionMemory } from "./session.js";
|
|
11
9
|
import { buildIncrementalMemorySourceWindow } from "./source-window.js";
|
|
@@ -48,9 +46,6 @@ function hasMeaningfulMessages(messages) {
|
|
|
48
46
|
}
|
|
49
47
|
return false;
|
|
50
48
|
}
|
|
51
|
-
function renderMessagesForSignalScan(messages) {
|
|
52
|
-
return sanitizeMessagesForMemory(messages).map(messageToText).join("\n");
|
|
53
|
-
}
|
|
54
49
|
function makeRunOptions(input, usageCorrelationId) {
|
|
55
50
|
return {
|
|
56
51
|
channelId: input.channelId,
|
|
@@ -60,8 +55,7 @@ function makeRunOptions(input, usageCorrelationId) {
|
|
|
60
55
|
messages: input.messages,
|
|
61
56
|
sessionEntries: input.sessionEntries,
|
|
62
57
|
usageCorrelationId,
|
|
63
|
-
|
|
64
|
-
minAutoWriteConfidence: input.settings.memoryGrowth.minMemoryAutoWriteConfidence,
|
|
58
|
+
minAutoWriteConfidence: input.settings.memoryMaintenance.minMemoryAutoWriteConfidence,
|
|
65
59
|
};
|
|
66
60
|
}
|
|
67
61
|
function backoffUntil(now, settings) {
|
|
@@ -144,19 +138,18 @@ export async function runSessionRefreshJob(input) {
|
|
|
144
138
|
}
|
|
145
139
|
});
|
|
146
140
|
}
|
|
147
|
-
export async function
|
|
141
|
+
export async function runMemoryCheckpointJob(input) {
|
|
148
142
|
return runQueued(input, async () => {
|
|
149
143
|
const now = input.now ?? new Date();
|
|
150
144
|
const state = await readMemoryMaintenanceState(input.appHomeDir, input.channelId);
|
|
151
145
|
const sourceWindow = buildIncrementalMemorySourceWindow({
|
|
152
146
|
entries: input.sessionEntries,
|
|
153
|
-
lastEntryId: state.
|
|
147
|
+
lastEntryId: state.lastCheckpointEntryId,
|
|
154
148
|
sourceKind: "idle",
|
|
155
149
|
fallbackMessages: input.messages,
|
|
156
150
|
});
|
|
157
151
|
const newEntries = sourceWindow.entries;
|
|
158
|
-
const
|
|
159
|
-
const decision = shouldRunDurableConsolidation({
|
|
152
|
+
const decision = shouldRunMemoryCheckpoint({
|
|
160
153
|
now,
|
|
161
154
|
state,
|
|
162
155
|
maintenance: input.settings.memoryMaintenance,
|
|
@@ -164,10 +157,9 @@ export async function runDurableConsolidationJob(input) {
|
|
|
164
157
|
hasNewEntry: newEntries.length > 0,
|
|
165
158
|
hasMeaningfulExchange: hasMeaningfulMessages(sourceWindow.messages),
|
|
166
159
|
batchSize: newEntries.length,
|
|
167
|
-
coveredByGrowthReview: Boolean(latestId && state.lastReviewedEntryId === latestId),
|
|
168
160
|
});
|
|
169
161
|
if (!decision.allowed) {
|
|
170
|
-
await appendJobReviewLog(input.channelDir, input.channelId, "
|
|
162
|
+
await appendJobReviewLog(input.channelDir, input.channelId, "memory-checkpoint-job", { skipped: [{ target: "consolidation", reason: decision.skipReason }] }, now);
|
|
171
163
|
return skipped(decision.jobKind, decision.skipReason ?? "skipped");
|
|
172
164
|
}
|
|
173
165
|
try {
|
|
@@ -178,11 +170,11 @@ export async function runDurableConsolidationJob(input) {
|
|
|
178
170
|
});
|
|
179
171
|
await updateMemoryMaintenanceState(input.appHomeDir, input.channelId, (current) => ({
|
|
180
172
|
...current,
|
|
181
|
-
|
|
182
|
-
|
|
173
|
+
lastCheckpointAt: now.toISOString(),
|
|
174
|
+
lastCheckpointEntryId: sourceWindow.throughEntryId ?? current.lastCheckpointEntryId,
|
|
183
175
|
failureBackoffUntil: null,
|
|
184
176
|
}));
|
|
185
|
-
await appendJobReviewLog(input.channelDir, input.channelId, "
|
|
177
|
+
await appendJobReviewLog(input.channelDir, input.channelId, "memory-checkpoint-job", result.skipped
|
|
186
178
|
? {
|
|
187
179
|
skipped: [{ target: "consolidation", reason: "no meaningful snapshot" }],
|
|
188
180
|
correlationId: sourceWindow.windowId,
|
|
@@ -196,98 +188,15 @@ export async function runDurableConsolidationJob(input) {
|
|
|
196
188
|
})),
|
|
197
189
|
correlationId: sourceWindow.windowId,
|
|
198
190
|
}, now);
|
|
199
|
-
return ran("
|
|
200
|
-
}
|
|
201
|
-
catch (error) {
|
|
202
|
-
await updateMemoryMaintenanceState(input.appHomeDir, input.channelId, (current) => ({
|
|
203
|
-
...current,
|
|
204
|
-
failureBackoffUntil: backoffUntil(now, input.settings.memoryMaintenance),
|
|
205
|
-
}));
|
|
206
|
-
const result = failed("durable-consolidation", error);
|
|
207
|
-
await appendJobReviewLog(input.channelDir, input.channelId, "durable-consolidation-job", { error: result.error, skipped: [{ target: "consolidation", reason: "failed" }] }, now);
|
|
208
|
-
return result;
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
export async function runGrowthReviewJob(input) {
|
|
213
|
-
return runQueued(input, async () => {
|
|
214
|
-
const now = input.now ?? new Date();
|
|
215
|
-
const state = await readMemoryMaintenanceState(input.appHomeDir, input.channelId);
|
|
216
|
-
const sourceWindow = buildIncrementalMemorySourceWindow({
|
|
217
|
-
entries: input.sessionEntries,
|
|
218
|
-
lastEntryId: state.lastReviewedEntryId,
|
|
219
|
-
sourceKind: "growth-review",
|
|
220
|
-
fallbackMessages: input.messages,
|
|
221
|
-
});
|
|
222
|
-
const newEntries = sourceWindow.entries;
|
|
223
|
-
const signalScan = scanPromotionSignals(renderMessagesForSignalScan(sourceWindow.messages));
|
|
224
|
-
const decision = shouldRunGrowthReview({
|
|
225
|
-
now,
|
|
226
|
-
state,
|
|
227
|
-
memoryGrowth: input.settings.memoryGrowth,
|
|
228
|
-
maintenance: input.settings.memoryMaintenance,
|
|
229
|
-
channelActive: input.channelActive,
|
|
230
|
-
hasNewEntry: newEntries.length > 0,
|
|
231
|
-
hasMeaningfulMaterial: hasMeaningfulMessages(sourceWindow.messages),
|
|
232
|
-
hasPromotionSignal: signalScan.hasSignal,
|
|
233
|
-
});
|
|
234
|
-
if (!decision.allowed) {
|
|
235
|
-
await appendJobReviewLog(input.channelDir, input.channelId, "growth-review-job", { skipped: [{ target: "post-turn-review", reason: decision.skipReason }] }, now);
|
|
236
|
-
return skipped(decision.jobKind, decision.skipReason ?? "skipped");
|
|
237
|
-
}
|
|
238
|
-
try {
|
|
239
|
-
const notices = [];
|
|
240
|
-
const result = await runPostTurnReview({
|
|
241
|
-
channelId: input.channelId,
|
|
242
|
-
channelDir: input.channelDir,
|
|
243
|
-
workspaceDir: input.workspaceDir,
|
|
244
|
-
messages: sourceWindow.messages,
|
|
245
|
-
sourceEntryIds: sourceWindow.entries.map((entry) => entry.id),
|
|
246
|
-
suppressAutomaticWrites: sourceWindow.hasExternalToolContent,
|
|
247
|
-
model: input.model,
|
|
248
|
-
resolveApiKey: input.resolveApiKey,
|
|
249
|
-
timeoutMs: input.settings.sessionMemory.timeoutMs,
|
|
250
|
-
autoWriteChannelMemory: input.settings.memoryGrowth.autoWriteChannelMemory,
|
|
251
|
-
autoWriteWorkspaceSkills: input.settings.memoryGrowth.autoWriteWorkspaceSkills,
|
|
252
|
-
minMemoryAutoWriteConfidence: input.settings.memoryGrowth.minMemoryAutoWriteConfidence,
|
|
253
|
-
minSkillAutoWriteConfidence: input.settings.memoryGrowth.minSkillAutoWriteConfidence,
|
|
254
|
-
loadedSkills: input.loadedSkills,
|
|
255
|
-
correlationId: sourceWindow.windowId,
|
|
256
|
-
emitNotice: async (notice) => {
|
|
257
|
-
notices.push(notice);
|
|
258
|
-
},
|
|
259
|
-
refreshWorkspaceResources: input.refreshWorkspaceResources,
|
|
260
|
-
});
|
|
261
|
-
if (result.status === "failed") {
|
|
262
|
-
throw new Error(`Growth review failed: ${result.error}. Retry this memory window after backoff.`);
|
|
263
|
-
}
|
|
264
|
-
const appliedResult = result.result;
|
|
265
|
-
await updateMemoryMaintenanceState(input.appHomeDir, input.channelId, (current) => ({
|
|
266
|
-
...current,
|
|
267
|
-
lastGrowthReviewAt: now.toISOString(),
|
|
268
|
-
turnsSinceGrowthReview: 0,
|
|
269
|
-
toolCallsSinceGrowthReview: 0,
|
|
270
|
-
lastReviewedEntryId: sourceWindow.throughEntryId ?? current.lastReviewedEntryId,
|
|
271
|
-
failureBackoffUntil: null,
|
|
272
|
-
}));
|
|
273
|
-
if (notices.length > 0) {
|
|
274
|
-
const uniqueNotices = Array.from(new Set(notices));
|
|
275
|
-
await input.emitNotice?.(uniqueNotices.join("\n"));
|
|
276
|
-
}
|
|
277
|
-
await appendJobReviewLog(input.channelDir, input.channelId, "growth-review-job", {
|
|
278
|
-
actions: appliedResult.actions,
|
|
279
|
-
skipped: appliedResult.skipped,
|
|
280
|
-
correlationId: sourceWindow.windowId,
|
|
281
|
-
}, now);
|
|
282
|
-
return ran("growth-review");
|
|
191
|
+
return ran("memory-checkpoint");
|
|
283
192
|
}
|
|
284
193
|
catch (error) {
|
|
285
194
|
await updateMemoryMaintenanceState(input.appHomeDir, input.channelId, (current) => ({
|
|
286
195
|
...current,
|
|
287
196
|
failureBackoffUntil: backoffUntil(now, input.settings.memoryMaintenance),
|
|
288
197
|
}));
|
|
289
|
-
const result = failed("
|
|
290
|
-
await appendJobReviewLog(input.channelDir, input.channelId, "
|
|
198
|
+
const result = failed("memory-checkpoint", error);
|
|
199
|
+
await appendJobReviewLog(input.channelDir, input.channelId, "memory-checkpoint-job", { error: result.error, skipped: [{ target: "consolidation", reason: "failed" }] }, now);
|
|
291
200
|
return result;
|
|
292
201
|
}
|
|
293
202
|
});
|
|
@@ -4,17 +4,13 @@ export interface MemoryMaintenanceState {
|
|
|
4
4
|
lastActivityAt?: string;
|
|
5
5
|
eligibleAfter?: string;
|
|
6
6
|
lastSessionRefreshAt?: string;
|
|
7
|
-
|
|
8
|
-
lastGrowthReviewAt?: string;
|
|
7
|
+
lastCheckpointAt?: string;
|
|
9
8
|
lastStructuralMaintenanceAt?: string;
|
|
10
9
|
turnsSinceSessionRefresh: number;
|
|
11
10
|
toolCallsSinceSessionRefresh: number;
|
|
12
|
-
turnsSinceGrowthReview: number;
|
|
13
|
-
toolCallsSinceGrowthReview: number;
|
|
14
11
|
lastSessionEntryId?: string;
|
|
15
12
|
lastSessionRefreshedEntryId?: string;
|
|
16
|
-
|
|
17
|
-
lastReviewedEntryId?: string;
|
|
13
|
+
lastCheckpointEntryId?: string;
|
|
18
14
|
failureBackoffUntil?: string | null;
|
|
19
15
|
}
|
|
20
16
|
export type MemoryActivityKind = "user-turn-started" | "tool-call" | "assistant-turn-completed" | "boundary";
|
|
@@ -17,8 +17,6 @@ function createDefaultState(channelId) {
|
|
|
17
17
|
dirty: false,
|
|
18
18
|
turnsSinceSessionRefresh: 0,
|
|
19
19
|
toolCallsSinceSessionRefresh: 0,
|
|
20
|
-
turnsSinceGrowthReview: 0,
|
|
21
|
-
toolCallsSinceGrowthReview: 0,
|
|
22
20
|
failureBackoffUntil: null,
|
|
23
21
|
};
|
|
24
22
|
}
|
|
@@ -34,6 +32,13 @@ function normalizeOptionalNullableString(value) {
|
|
|
34
32
|
function normalizeCounter(value) {
|
|
35
33
|
return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.floor(value) : 0;
|
|
36
34
|
}
|
|
35
|
+
function laterTimestamp(a, b) {
|
|
36
|
+
if (!a)
|
|
37
|
+
return b;
|
|
38
|
+
if (!b)
|
|
39
|
+
return a;
|
|
40
|
+
return Date.parse(a) >= Date.parse(b) ? a : b;
|
|
41
|
+
}
|
|
37
42
|
function normalizeState(channelId, value) {
|
|
38
43
|
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
39
44
|
return createDefaultState(channelId);
|
|
@@ -45,17 +50,18 @@ function normalizeState(channelId, value) {
|
|
|
45
50
|
lastActivityAt: normalizeOptionalString(record.lastActivityAt),
|
|
46
51
|
eligibleAfter: normalizeOptionalString(record.eligibleAfter),
|
|
47
52
|
lastSessionRefreshAt: normalizeOptionalString(record.lastSessionRefreshAt),
|
|
48
|
-
|
|
49
|
-
|
|
53
|
+
// Legacy states carried separate consolidation/growth-review fields; fold them
|
|
54
|
+
// into the merged checkpoint so cadence and cursor survive the migration.
|
|
55
|
+
lastCheckpointAt: normalizeOptionalString(record.lastCheckpointAt) ??
|
|
56
|
+
laterTimestamp(normalizeOptionalString(record.lastDurableConsolidationAt), normalizeOptionalString(record.lastGrowthReviewAt)),
|
|
50
57
|
lastStructuralMaintenanceAt: normalizeOptionalString(record.lastStructuralMaintenanceAt),
|
|
51
58
|
turnsSinceSessionRefresh: normalizeCounter(record.turnsSinceSessionRefresh),
|
|
52
59
|
toolCallsSinceSessionRefresh: normalizeCounter(record.toolCallsSinceSessionRefresh),
|
|
53
|
-
turnsSinceGrowthReview: normalizeCounter(record.turnsSinceGrowthReview),
|
|
54
|
-
toolCallsSinceGrowthReview: normalizeCounter(record.toolCallsSinceGrowthReview),
|
|
55
60
|
lastSessionEntryId: normalizeOptionalString(record.lastSessionEntryId),
|
|
56
61
|
lastSessionRefreshedEntryId: normalizeOptionalString(record.lastSessionRefreshedEntryId),
|
|
57
|
-
|
|
58
|
-
|
|
62
|
+
lastCheckpointEntryId: normalizeOptionalString(record.lastCheckpointEntryId) ??
|
|
63
|
+
normalizeOptionalString(record.lastConsolidatedEntryId) ??
|
|
64
|
+
normalizeOptionalString(record.lastReviewedEntryId),
|
|
59
65
|
failureBackoffUntil: normalizeOptionalNullableString(record.failureBackoffUntil) ?? null,
|
|
60
66
|
};
|
|
61
67
|
}
|
|
@@ -94,12 +100,10 @@ export function applyMemoryActivityToState(state, event) {
|
|
|
94
100
|
if (event.kind === "tool-call") {
|
|
95
101
|
next.dirty = true;
|
|
96
102
|
next.toolCallsSinceSessionRefresh += 1;
|
|
97
|
-
next.toolCallsSinceGrowthReview += 1;
|
|
98
103
|
}
|
|
99
104
|
if (event.kind === "assistant-turn-completed") {
|
|
100
105
|
next.dirty = true;
|
|
101
106
|
next.turnsSinceSessionRefresh += 1;
|
|
102
|
-
next.turnsSinceGrowthReview += 1;
|
|
103
107
|
}
|
|
104
108
|
if (event.kind === "boundary") {
|
|
105
109
|
next.dirty = true;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cadence and threshold constants for the background memory maintenance
|
|
3
|
+
* pipeline (spec 035 D1/D2).
|
|
4
|
+
*
|
|
5
|
+
* These were `settings.json` keys until 0.8.10. They are algorithm parameters,
|
|
6
|
+
* not product intent: a user can decide *whether* background maintenance runs
|
|
7
|
+
* (`memoryMaintenance.enabled` is still a setting), but has no basis for
|
|
8
|
+
* deciding whether the checkpoint interval should be 20 minutes or 25.
|
|
9
|
+
*/
|
|
10
|
+
export interface MemoryMaintenanceTuning {
|
|
11
|
+
/** How long a channel must be quiet before background LLM work is allowed. */
|
|
12
|
+
minIdleMinutesBeforeLlmWork: number;
|
|
13
|
+
sessionRefreshIntervalMinutes: number;
|
|
14
|
+
/** Idle cadence of the merged memory checkpoint (durable extraction) job. */
|
|
15
|
+
checkpointIntervalMinutes: number;
|
|
16
|
+
/** Confidence bar a candidate must clear before it is written to MEMORY.md. */
|
|
17
|
+
minMemoryAutoWriteConfidence: number;
|
|
18
|
+
structuralMaintenanceIntervalHours: number;
|
|
19
|
+
maxConcurrentChannels: number;
|
|
20
|
+
failureBackoffMinutes: number;
|
|
21
|
+
/** Reject a MEMORY.md cleanup whose result shrinks below this ratio of the original. */
|
|
22
|
+
cleanupShrinkGuardMinRatio: number;
|
|
23
|
+
/** Only apply the shrink guard when the original exceeds this length (chars). */
|
|
24
|
+
cleanupShrinkGuardMinChars: number;
|
|
25
|
+
}
|
|
26
|
+
export declare function getMemoryMaintenanceTuning(env?: NodeJS.ProcessEnv): MemoryMaintenanceTuning;
|
|
27
|
+
/**
|
|
28
|
+
* How much conversation must accumulate before a scheduled `SESSION.md` refresh
|
|
29
|
+
* is allowed (`maintenance-gates.ts` reads these). Constants like the tuning
|
|
30
|
+
* above, and lowered by the same test hook so a single-message E2E turn can
|
|
31
|
+
* trip the gate instead of waiting for a natural multi-turn exchange.
|
|
32
|
+
*/
|
|
33
|
+
export interface SessionRefreshCadence {
|
|
34
|
+
minTurnsBetweenUpdate: number;
|
|
35
|
+
minToolCallsBetweenUpdate: number;
|
|
36
|
+
}
|
|
37
|
+
export declare function getSessionRefreshCadence(env?: NodeJS.ProcessEnv): SessionRefreshCadence;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cadence and threshold constants for the background memory maintenance
|
|
3
|
+
* pipeline (spec 035 D1/D2).
|
|
4
|
+
*
|
|
5
|
+
* These were `settings.json` keys until 0.8.10. They are algorithm parameters,
|
|
6
|
+
* not product intent: a user can decide *whether* background maintenance runs
|
|
7
|
+
* (`memoryMaintenance.enabled` is still a setting), but has no basis for
|
|
8
|
+
* deciding whether the checkpoint interval should be 20 minutes or 25.
|
|
9
|
+
*/
|
|
10
|
+
const PRODUCTION_TUNING = {
|
|
11
|
+
minIdleMinutesBeforeLlmWork: 10,
|
|
12
|
+
sessionRefreshIntervalMinutes: 10,
|
|
13
|
+
checkpointIntervalMinutes: 20,
|
|
14
|
+
minMemoryAutoWriteConfidence: 0.85,
|
|
15
|
+
structuralMaintenanceIntervalHours: 6,
|
|
16
|
+
maxConcurrentChannels: 1,
|
|
17
|
+
failureBackoffMinutes: 30,
|
|
18
|
+
cleanupShrinkGuardMinRatio: 0.4,
|
|
19
|
+
cleanupShrinkGuardMinChars: 2_000,
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Test-only escape hatch. E2E and behavior-eval runs assert that `SESSION.md`
|
|
23
|
+
* refreshes within tens of seconds, which the production idle/interval gates
|
|
24
|
+
* make impossible. Set by `test/support/setup.ts`; deliberately undocumented in
|
|
25
|
+
* `docs/configuration.md` — it is a test hook, not a supported knob.
|
|
26
|
+
*/
|
|
27
|
+
const FAST_MAINTENANCE_TUNING = {
|
|
28
|
+
...PRODUCTION_TUNING,
|
|
29
|
+
minIdleMinutesBeforeLlmWork: 0,
|
|
30
|
+
sessionRefreshIntervalMinutes: 0,
|
|
31
|
+
failureBackoffMinutes: 1,
|
|
32
|
+
};
|
|
33
|
+
export function getMemoryMaintenanceTuning(env = process.env) {
|
|
34
|
+
return env.PIPICLAW_TEST_FAST_MAINTENANCE === "1" ? FAST_MAINTENANCE_TUNING : PRODUCTION_TUNING;
|
|
35
|
+
}
|
|
36
|
+
const PRODUCTION_SESSION_REFRESH = {
|
|
37
|
+
minTurnsBetweenUpdate: 2,
|
|
38
|
+
minToolCallsBetweenUpdate: 4,
|
|
39
|
+
};
|
|
40
|
+
const FAST_SESSION_REFRESH = {
|
|
41
|
+
minTurnsBetweenUpdate: 1,
|
|
42
|
+
minToolCallsBetweenUpdate: 1,
|
|
43
|
+
};
|
|
44
|
+
export function getSessionRefreshCadence(env = process.env) {
|
|
45
|
+
return env.PIPICLAW_TEST_FAST_MAINTENANCE === "1" ? FAST_SESSION_REFRESH : PRODUCTION_SESSION_REFRESH;
|
|
46
|
+
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
export type MemoryPromotionTarget = "channel-memory";
|
|
2
|
-
export type SkillPromotionAction = "create" | "patch" | "write_file";
|
|
3
2
|
export interface MemoryPromotionCandidate {
|
|
4
3
|
target: MemoryPromotionTarget;
|
|
5
4
|
op: "add" | "supersede" | "invalidate";
|
|
@@ -10,26 +9,5 @@ export interface MemoryPromotionCandidate {
|
|
|
10
9
|
reason: string;
|
|
11
10
|
necessity: "low" | "medium" | "high";
|
|
12
11
|
}
|
|
13
|
-
export interface SkillPromotionCandidate {
|
|
14
|
-
action: SkillPromotionAction;
|
|
15
|
-
name: string;
|
|
16
|
-
content?: string;
|
|
17
|
-
filePath?: string;
|
|
18
|
-
find?: string;
|
|
19
|
-
replace?: string;
|
|
20
|
-
confidence: number;
|
|
21
|
-
necessity: "low" | "medium" | "high";
|
|
22
|
-
reason: string;
|
|
23
|
-
}
|
|
24
|
-
export interface PostTurnReviewResult {
|
|
25
|
-
memoryOps: MemoryPromotionCandidate[];
|
|
26
|
-
skillCandidates: SkillPromotionCandidate[];
|
|
27
|
-
discarded: Array<{
|
|
28
|
-
content: string;
|
|
29
|
-
reason: string;
|
|
30
|
-
}>;
|
|
31
|
-
}
|
|
32
12
|
export declare const DEFAULT_MEMORY_AUTO_WRITE_CONFIDENCE = 0.85;
|
|
33
|
-
export declare const DEFAULT_SKILL_AUTO_WRITE_CONFIDENCE = 0.9;
|
|
34
13
|
export declare function shouldAutoWriteMemory(candidate: MemoryPromotionCandidate, threshold?: number): boolean;
|
|
35
|
-
export declare function shouldAutoWriteSkill(candidate: SkillPromotionCandidate, threshold?: number): boolean;
|
package/dist/memory/promotion.js
CHANGED
|
@@ -1,13 +1,6 @@
|
|
|
1
1
|
export const DEFAULT_MEMORY_AUTO_WRITE_CONFIDENCE = 0.85;
|
|
2
|
-
export const DEFAULT_SKILL_AUTO_WRITE_CONFIDENCE = 0.9;
|
|
3
|
-
function isHighNecessity(value) {
|
|
4
|
-
return value === "high";
|
|
5
|
-
}
|
|
6
2
|
export function shouldAutoWriteMemory(candidate, threshold = DEFAULT_MEMORY_AUTO_WRITE_CONFIDENCE) {
|
|
7
3
|
return (candidate.confidence >= threshold &&
|
|
8
|
-
|
|
4
|
+
candidate.necessity === "high" &&
|
|
9
5
|
(candidate.op === "invalidate" ? Boolean(candidate.targetId) : Boolean(candidate.content?.trim())));
|
|
10
6
|
}
|
|
11
|
-
export function shouldAutoWriteSkill(candidate, threshold = DEFAULT_SKILL_AUTO_WRITE_CONFIDENCE) {
|
|
12
|
-
return candidate.confidence >= threshold && candidate.necessity === "high";
|
|
13
|
-
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type MemoryReviewReason = "idle" | "compaction" | "new-session" | "shutdown" | "
|
|
1
|
+
export type MemoryReviewReason = "idle" | "compaction" | "new-session" | "shutdown" | "session-refresh-job" | "memory-checkpoint-job" | "structural-maintenance-job" | "user-forget";
|
|
2
2
|
export interface MemoryReviewLogEntry {
|
|
3
3
|
timestamp: string;
|
|
4
4
|
channelId: string;
|
|
@@ -1,25 +1,18 @@
|
|
|
1
1
|
import type { AgentMessage } from "@earendil-works/pi-agent-core";
|
|
2
2
|
import type { Api, Model } from "@earendil-works/pi-ai";
|
|
3
3
|
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
4
|
-
import type {
|
|
4
|
+
import type { PipiclawMemoryMaintenanceSettings, PipiclawSessionMemorySettings } from "../settings.js";
|
|
5
5
|
export interface MemoryMaintenanceRuntimeContext {
|
|
6
6
|
channelId: string;
|
|
7
7
|
channelDir: string;
|
|
8
|
-
workspaceDir: string;
|
|
9
8
|
messages: AgentMessage[];
|
|
10
9
|
sessionEntries: SessionEntry[];
|
|
11
10
|
model: Model<Api>;
|
|
12
11
|
resolveApiKey: (model: Model<Api>) => Promise<string>;
|
|
13
12
|
settings: {
|
|
14
13
|
sessionMemory: PipiclawSessionMemorySettings;
|
|
15
|
-
memoryGrowth: PipiclawMemoryGrowthSettings;
|
|
16
14
|
memoryMaintenance: PipiclawMemoryMaintenanceSettings;
|
|
17
15
|
};
|
|
18
|
-
loadedSkills: Array<{
|
|
19
|
-
name: string;
|
|
20
|
-
description?: string;
|
|
21
|
-
}>;
|
|
22
|
-
refreshWorkspaceResources?: () => Promise<void>;
|
|
23
16
|
}
|
|
24
17
|
export interface MemoryMaintenanceSchedulerOptions {
|
|
25
18
|
appHomeDir: string;
|
|
@@ -30,7 +23,6 @@ export interface MemoryMaintenanceSchedulerOptions {
|
|
|
30
23
|
getSettings: () => {
|
|
31
24
|
memoryMaintenance: PipiclawMemoryMaintenanceSettings;
|
|
32
25
|
};
|
|
33
|
-
emitNotice?: (channelId: string, notice: string) => Promise<void>;
|
|
34
26
|
intervalMs?: number;
|
|
35
27
|
}
|
|
36
28
|
export declare function discoverMemoryMaintenanceChannels(input: {
|
package/dist/memory/scheduler.js
CHANGED
|
@@ -2,7 +2,7 @@ import { readdir } from "node:fs/promises";
|
|
|
2
2
|
import { basename } from "node:path";
|
|
3
3
|
import * as log from "../log.js";
|
|
4
4
|
import { errorMessage } from "../shared/text-utils.js";
|
|
5
|
-
import {
|
|
5
|
+
import { runMemoryCheckpointJob, runSessionRefreshJob, runStructuralMaintenanceJob } from "./maintenance-jobs.js";
|
|
6
6
|
import { getMemoryMaintenanceStateDir } from "./maintenance-state.js";
|
|
7
7
|
const DEFAULT_TICK_INTERVAL_MS = 60_000;
|
|
8
8
|
const CHANNEL_ID_PATTERN = /^(dm|group)_[A-Za-z0-9._:-]+$/;
|
|
@@ -131,20 +131,8 @@ export class MemoryMaintenanceScheduler {
|
|
|
131
131
|
if (session.ran) {
|
|
132
132
|
return;
|
|
133
133
|
}
|
|
134
|
-
const
|
|
135
|
-
if (
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
const growth = await runGrowthReviewJob({
|
|
139
|
-
...common,
|
|
140
|
-
workspaceDir: context.workspaceDir,
|
|
141
|
-
loadedSkills: context.loadedSkills,
|
|
142
|
-
emitNotice: this.options.emitNotice
|
|
143
|
-
? async (notice) => this.options.emitNotice?.(channelId, notice)
|
|
144
|
-
: undefined,
|
|
145
|
-
refreshWorkspaceResources: context.refreshWorkspaceResources,
|
|
146
|
-
});
|
|
147
|
-
if (growth.ran) {
|
|
134
|
+
const checkpoint = await runMemoryCheckpointJob(common);
|
|
135
|
+
if (checkpoint.ran) {
|
|
148
136
|
return;
|
|
149
137
|
}
|
|
150
138
|
await runStructuralMaintenanceJob(common);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { AgentMessage } from "@earendil-works/pi-agent-core";
|
|
2
2
|
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
3
|
-
export type MemorySourceKind = "idle" | "compaction" | "new-session" | "shutdown"
|
|
3
|
+
export type MemorySourceKind = "idle" | "compaction" | "new-session" | "shutdown";
|
|
4
4
|
export interface MemorySourceWindow {
|
|
5
5
|
sourceKind: MemorySourceKind;
|
|
6
6
|
fromEntryId?: string;
|
|
@@ -14,7 +14,7 @@ export declare function entriesSince(entries: SessionEntry[], lastEntryId: strin
|
|
|
14
14
|
export declare function buildIncrementalMemorySourceWindow(options: {
|
|
15
15
|
entries: SessionEntry[];
|
|
16
16
|
lastEntryId?: string;
|
|
17
|
-
sourceKind: Extract<MemorySourceKind, "idle" | "new-session" | "shutdown"
|
|
17
|
+
sourceKind: Extract<MemorySourceKind, "idle" | "new-session" | "shutdown">;
|
|
18
18
|
fallbackMessages?: AgentMessage[];
|
|
19
19
|
}): MemorySourceWindow;
|
|
20
20
|
export declare function buildCompactionMemorySourceWindow(options: {
|
|
@@ -24,16 +24,13 @@ function renderLine(entry, now) {
|
|
|
24
24
|
if (control) {
|
|
25
25
|
parts.push(`priority ${control.priority}`);
|
|
26
26
|
parts.push(`attempt ${control.usage.attempts}/${control.budget.maxAttempts}`);
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
parts.push(`isolation ${control.isolation}`);
|
|
27
|
+
if (control.verification.required)
|
|
28
|
+
parts.push(`verify required/${control.verification.status}`);
|
|
30
29
|
if (control.sideEffects !== "workspace") {
|
|
31
30
|
parts.push(`effects ${control.sideEffects}/${control.externalApproval}`);
|
|
32
31
|
}
|
|
33
32
|
if (control.deadline)
|
|
34
33
|
parts.push(`deadline ${control.deadline}`);
|
|
35
|
-
if (control.dependsOn.length > 0)
|
|
36
|
-
parts.push(`depends ${control.dependsOn.join(",")}`);
|
|
37
34
|
if (control.nextAction)
|
|
38
35
|
parts.push(`next ${control.nextAction}`);
|
|
39
36
|
}
|
package/dist/models/utils.d.ts
CHANGED
|
@@ -17,6 +17,11 @@ export declare function createModelRuntime(options: {
|
|
|
17
17
|
*/
|
|
18
18
|
export declare function wrapModelRegistry(modelRuntime: ModelRuntime): ModelRegistry;
|
|
19
19
|
export declare function formatModelReference(model: Model<Api>): string;
|
|
20
|
+
/**
|
|
21
|
+
* Pi fills omitted custom-model pricing with zero rates. An all-zero table therefore
|
|
22
|
+
* means "price unavailable", not reliably "free".
|
|
23
|
+
*/
|
|
24
|
+
export declare function hasKnownModelPricing(model: Model<Api> | undefined): boolean;
|
|
20
25
|
export declare function findExactModelReferenceMatch(modelReference: string, availableModels: Model<Api>[]): {
|
|
21
26
|
match?: Model<Api>;
|
|
22
27
|
ambiguous: boolean;
|
package/dist/models/utils.js
CHANGED
|
@@ -23,6 +23,16 @@ export function wrapModelRegistry(modelRuntime) {
|
|
|
23
23
|
export function formatModelReference(model) {
|
|
24
24
|
return `${model.provider}/${model.id}`;
|
|
25
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Pi fills omitted custom-model pricing with zero rates. An all-zero table therefore
|
|
28
|
+
* means "price unavailable", not reliably "free".
|
|
29
|
+
*/
|
|
30
|
+
export function hasKnownModelPricing(model) {
|
|
31
|
+
if (!model?.cost)
|
|
32
|
+
return false;
|
|
33
|
+
const rates = [model.cost, ...(model.cost.tiers ?? [])];
|
|
34
|
+
return rates.some((rate) => rate.input > 0 || rate.output > 0 || rate.cacheRead > 0 || rate.cacheWrite > 0);
|
|
35
|
+
}
|
|
26
36
|
export function findExactModelReferenceMatch(modelReference, availableModels) {
|
|
27
37
|
const trimmedReference = modelReference.trim();
|
|
28
38
|
if (!trimmedReference) {
|
package/dist/paths.d.ts
CHANGED
|
@@ -18,7 +18,6 @@ export declare const LOG_STATE_DIR: string;
|
|
|
18
18
|
export declare const RUNTIME_LOG_PATH: string;
|
|
19
19
|
export declare const USAGE_STATE_DIR: string;
|
|
20
20
|
export declare const SUB_AGENTS_DIR_NAME = "sub-agents";
|
|
21
|
-
export declare const SUB_AGENTS_DIR: string;
|
|
22
21
|
export declare const CHANNEL_CONFIG_PATH: string;
|
|
23
22
|
export declare const AUTH_CONFIG_PATH: string;
|
|
24
23
|
export declare const MODELS_CONFIG_PATH: string;
|
package/dist/paths.js
CHANGED
|
@@ -29,7 +29,6 @@ export const LOG_STATE_DIR = join(STATE_DIR, "logs");
|
|
|
29
29
|
export const RUNTIME_LOG_PATH = join(LOG_STATE_DIR, "runtime.jsonl");
|
|
30
30
|
export const USAGE_STATE_DIR = join(STATE_DIR, "usage");
|
|
31
31
|
export const SUB_AGENTS_DIR_NAME = "sub-agents";
|
|
32
|
-
export const SUB_AGENTS_DIR = join(WORKSPACE_DIR, SUB_AGENTS_DIR_NAME);
|
|
33
32
|
export const CHANNEL_CONFIG_PATH = join(APP_HOME_DIR, "channel.json");
|
|
34
33
|
export const AUTH_CONFIG_PATH = join(APP_HOME_DIR, "auth.json");
|
|
35
34
|
export const MODELS_CONFIG_PATH = join(APP_HOME_DIR, "models.json");
|
|
@@ -43,6 +43,8 @@ FAIL 后用 progress 记录失败证据、status 回 active,修复后重新 ca
|
|
|
43
43
|
|
|
44
44
|
`task_manage done` 需要 summary 和可查证 evidence,可附 residualRisk。它检查 checkbox、依赖、child、外部授权、独立 PASS,再记录收尾、清理临时 task events;一次性任务归档,有 `schedule` frontmatter 的周期任务算出下一次 `wake` 并原地睡眠。
|
|
45
45
|
|
|
46
|
+
周期 occurrence 因去重、同一产物已由更早触发完成等原因无需执行时,用 `task_manage skip` + reason。skip 只关闭本次 occurrence,不要求虚勾 DoD、不写 completion evidence,也不会取消后续 schedule。
|
|
47
|
+
|
|
46
48
|
放弃用 `cancel` + reason,不伪装 done。cancel 要求先处理 child,随后归档并删除全部 task-owned events。
|
|
47
49
|
|
|
48
50
|
完成结论必须来自证据:具体命令、测试结果、review、外部确认或明确的未运行理由。“看起来可以”不是 evidence。
|