@oyasmi/pipiclaw 0.8.5 → 0.8.6
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/dist/agent/channel-runner.d.ts +1 -0
- package/dist/agent/channel-runner.js +19 -8
- package/dist/agent/command-extension.d.ts +1 -0
- package/dist/agent/command-extension.js +6 -0
- package/dist/agent/commands.js +6 -0
- package/dist/agent/types.d.ts +1 -0
- package/dist/memory/bootstrap.d.ts +5 -0
- package/dist/memory/bootstrap.js +14 -4
- package/dist/memory/candidates.d.ts +2 -0
- package/dist/memory/candidates.js +13 -3
- package/dist/memory/commands.d.ts +6 -0
- package/dist/memory/commands.js +112 -0
- package/dist/memory/consolidation.d.ts +3 -0
- package/dist/memory/consolidation.js +68 -26
- package/dist/memory/files.d.ts +13 -0
- package/dist/memory/files.js +65 -8
- package/dist/memory/lifecycle.d.ts +2 -1
- package/dist/memory/lifecycle.js +34 -6
- package/dist/memory/maintenance-jobs.js +48 -23
- package/dist/memory/metadata.d.ts +60 -0
- package/dist/memory/metadata.js +135 -0
- package/dist/memory/policy.d.ts +3 -0
- package/dist/memory/policy.js +19 -0
- package/dist/memory/post-turn-review.d.ts +11 -1
- package/dist/memory/post-turn-review.js +78 -19
- package/dist/memory/promotion.d.ts +5 -2
- package/dist/memory/promotion.js +3 -1
- package/dist/memory/recall.d.ts +3 -0
- package/dist/memory/recall.js +27 -5
- package/dist/memory/review-log.d.ts +2 -0
- package/dist/memory/review-log.js +15 -0
- package/dist/memory/session-corpus.js +11 -3
- package/dist/memory/session-search.js +8 -4
- package/dist/memory/session.d.ts +1 -0
- package/dist/memory/session.js +3 -1
- package/dist/memory/sidecar-worker.d.ts +1 -0
- package/dist/memory/sidecar-worker.js +1 -0
- package/dist/memory/source-window.d.ts +25 -0
- package/dist/memory/source-window.js +64 -0
- package/dist/memory/tombstones.d.ts +12 -0
- package/dist/memory/tombstones.js +44 -0
- package/dist/memory/transcript.js +26 -3
- package/dist/runtime/bootstrap.js +1 -0
- package/dist/runtime/task-commands.js +1 -1
- package/dist/settings.js +1 -1
- package/dist/tasks/control.js +9 -3
- package/dist/tasks/store.d.ts +1 -0
- package/dist/tasks/store.js +11 -1
- package/dist/tools/memory-manage.d.ts +1 -1
- package/dist/tools/memory-manage.js +27 -6
- package/dist/tools/task-manage.js +2 -2
- package/dist/usage/ledger.d.ts +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { appendFile, mkdir, readFile } from "node:fs/promises";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { createSerialQueue } from "../shared/serial-queue.js";
|
|
5
|
+
const appendQueue = createSerialQueue();
|
|
6
|
+
function normalizeMemoryContent(content) {
|
|
7
|
+
return content.normalize("NFKC").replace(/\s+/g, " ").trim().toLowerCase();
|
|
8
|
+
}
|
|
9
|
+
export function hashMemoryContent(content) {
|
|
10
|
+
return createHash("sha256").update(normalizeMemoryContent(content)).digest("hex");
|
|
11
|
+
}
|
|
12
|
+
export function getMemoryTombstonesPath(channelDir) {
|
|
13
|
+
return join(channelDir, ".memory", "tombstones.jsonl");
|
|
14
|
+
}
|
|
15
|
+
export async function appendMemoryTombstone(channelDir, tombstone) {
|
|
16
|
+
const path = getMemoryTombstonesPath(channelDir);
|
|
17
|
+
await appendQueue.run(path, async () => {
|
|
18
|
+
await mkdir(dirname(path), { recursive: true });
|
|
19
|
+
await appendFile(path, `${JSON.stringify(tombstone)}\n`, "utf-8");
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
export async function readMemoryTombstones(channelDir) {
|
|
23
|
+
try {
|
|
24
|
+
const raw = await readFile(getMemoryTombstonesPath(channelDir), "utf-8");
|
|
25
|
+
return raw
|
|
26
|
+
.split("\n")
|
|
27
|
+
.filter(Boolean)
|
|
28
|
+
.flatMap((line) => {
|
|
29
|
+
try {
|
|
30
|
+
const value = JSON.parse(line);
|
|
31
|
+
return value.entryId && value.contentHash ? [value] : [];
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return [];
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
40
|
+
return [];
|
|
41
|
+
}
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { buildStandardMessages } from "../shared/type-guards.js";
|
|
2
|
+
import { redactSecrets } from "./policy.js";
|
|
2
3
|
// The channel runner prepends the channel capsule, recalled memory, the task agenda
|
|
3
4
|
// and the durable bootstrap to the raw user input, then wraps the input itself in
|
|
4
5
|
// <user_message>. If that combined text flows back into consolidation / session
|
|
@@ -14,11 +15,25 @@ export function stripInjectedMemoryContext(text) {
|
|
|
14
15
|
}
|
|
15
16
|
function sanitizeUserMessage(message) {
|
|
16
17
|
if (typeof message.content === "string") {
|
|
17
|
-
return { ...message, content: stripInjectedMemoryContext(message.content) };
|
|
18
|
+
return { ...message, content: redactSecrets(stripInjectedMemoryContext(message.content)) };
|
|
18
19
|
}
|
|
19
20
|
return {
|
|
20
21
|
...message,
|
|
21
|
-
content: message.content.map((part) => part.type === "text" ? { ...part, text: stripInjectedMemoryContext(part.text) } : part),
|
|
22
|
+
content: message.content.map((part) => part.type === "text" ? { ...part, text: redactSecrets(stripInjectedMemoryContext(part.text)) } : part),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function sanitizeAssistantMessage(message) {
|
|
26
|
+
return {
|
|
27
|
+
...message,
|
|
28
|
+
content: message.content.map((part) => {
|
|
29
|
+
if (part.type === "text") {
|
|
30
|
+
return { ...part, text: redactSecrets(part.text) };
|
|
31
|
+
}
|
|
32
|
+
if (part.type === "thinking") {
|
|
33
|
+
return { ...part, thinking: redactSecrets(part.thinking) };
|
|
34
|
+
}
|
|
35
|
+
return part;
|
|
36
|
+
}),
|
|
22
37
|
};
|
|
23
38
|
}
|
|
24
39
|
/**
|
|
@@ -27,5 +42,13 @@ function sanitizeUserMessage(message) {
|
|
|
27
42
|
* transcript, so recalled memory is never folded back into durable memory.
|
|
28
43
|
*/
|
|
29
44
|
export function sanitizeMessagesForMemory(messages) {
|
|
30
|
-
return buildStandardMessages(messages)
|
|
45
|
+
return buildStandardMessages(messages)
|
|
46
|
+
.filter((message) => message.role !== "toolResult")
|
|
47
|
+
.map((message) => {
|
|
48
|
+
if (message.role === "user")
|
|
49
|
+
return sanitizeUserMessage(message);
|
|
50
|
+
if (message.role === "assistant")
|
|
51
|
+
return sanitizeAssistantMessage(message);
|
|
52
|
+
return message;
|
|
53
|
+
});
|
|
31
54
|
}
|
|
@@ -588,6 +588,7 @@ export function createRuntimeContext(options) {
|
|
|
588
588
|
costUsd: result.usage.cost.total,
|
|
589
589
|
wallTimeMinutes: result.durationMs / 60_000,
|
|
590
590
|
failed: result.stopReason === "error" || result.stopReason === "aborted",
|
|
591
|
+
silent: result.silent,
|
|
591
592
|
finishedAt: new Date(),
|
|
592
593
|
});
|
|
593
594
|
}
|
|
@@ -435,7 +435,7 @@ async function doctor(options) {
|
|
|
435
435
|
issues.push(issue(`tasks/${entry.id}.md points to missing related task ${relatedId}.`, `Create ${relatedId}, or remove it from parent/dependsOn with task_manage set.`));
|
|
436
436
|
}
|
|
437
437
|
}
|
|
438
|
-
if (control.sideEffects === "external" && control.externalApproval
|
|
438
|
+
if (control.sideEffects === "external" && control.externalApproval === "required") {
|
|
439
439
|
issues.push(issue(`tasks/${entry.id}.md requires external side effects but has no user approval.`, `After reviewing the proposed action, a user must run /tasks approve ${entry.id}.`));
|
|
440
440
|
}
|
|
441
441
|
if (control.externalApproval === "granted" &&
|
package/dist/settings.js
CHANGED
|
@@ -55,7 +55,7 @@ const DEFAULT_SESSION_MEMORY = {
|
|
|
55
55
|
const DEFAULT_MEMORY_GROWTH = {
|
|
56
56
|
postTurnReviewEnabled: true,
|
|
57
57
|
autoWriteChannelMemory: true,
|
|
58
|
-
autoWriteWorkspaceSkills:
|
|
58
|
+
autoWriteWorkspaceSkills: false,
|
|
59
59
|
minSkillAutoWriteConfidence: 0.9,
|
|
60
60
|
minMemoryAutoWriteConfidence: 0.85,
|
|
61
61
|
idleWritesHistory: false,
|
package/dist/tasks/control.js
CHANGED
|
@@ -166,7 +166,11 @@ export function resetTaskControlForCycle(control, cycleId) {
|
|
|
166
166
|
nextAction: undefined,
|
|
167
167
|
lastOutcome: "pending",
|
|
168
168
|
blockedReason: undefined,
|
|
169
|
-
|
|
169
|
+
// A cycle invalidates a one-time grant, but preserves an explicit policy
|
|
170
|
+
// exemption for recurring automation.
|
|
171
|
+
externalApproval: control.sideEffects === "external" && control.externalApproval !== "not-required"
|
|
172
|
+
? "required"
|
|
173
|
+
: "not-required",
|
|
170
174
|
approvalBy: undefined,
|
|
171
175
|
approvedAt: undefined,
|
|
172
176
|
approvalBodyHash: undefined,
|
|
@@ -193,6 +197,7 @@ function patchPositive(current, value, field) {
|
|
|
193
197
|
export function applyTaskControlPatch(control, patch) {
|
|
194
198
|
const next = structuredClone(control);
|
|
195
199
|
const invalidatesApproval = control.externalApproval === "granted" && Object.keys(patch).length > 0;
|
|
200
|
+
const explicitlySetsExternalApproval = patch.externalApproval !== undefined;
|
|
196
201
|
if (patch.priority !== undefined)
|
|
197
202
|
next.priority = patch.priority;
|
|
198
203
|
if (patch.deadline?.trim() && !Number.isFinite(new Date(patch.deadline).getTime())) {
|
|
@@ -239,11 +244,12 @@ export function applyTaskControlPatch(control, patch) {
|
|
|
239
244
|
next.approvedAt = undefined;
|
|
240
245
|
next.approvalBodyHash = undefined;
|
|
241
246
|
}
|
|
242
|
-
if (
|
|
247
|
+
if (control.sideEffects !== "external" && next.sideEffects === "external" && !explicitlySetsExternalApproval) {
|
|
243
248
|
next.externalApproval = "required";
|
|
244
249
|
}
|
|
245
|
-
if (invalidatesApproval && next.sideEffects === "external")
|
|
250
|
+
if (invalidatesApproval && next.sideEffects === "external" && !explicitlySetsExternalApproval) {
|
|
246
251
|
next.externalApproval = "required";
|
|
252
|
+
}
|
|
247
253
|
if (next.externalApproval !== "granted") {
|
|
248
254
|
next.approvalBy = undefined;
|
|
249
255
|
next.approvedAt = undefined;
|
package/dist/tasks/store.d.ts
CHANGED
|
@@ -23,6 +23,7 @@ export declare function finishTaskAttempt(channelDir: string, id: string, result
|
|
|
23
23
|
costUsd: number;
|
|
24
24
|
wallTimeMinutes: number;
|
|
25
25
|
failed: boolean;
|
|
26
|
+
silent?: boolean;
|
|
26
27
|
finishedAt: Date;
|
|
27
28
|
}): Promise<void>;
|
|
28
29
|
export declare function escalateTask(channelDir: string, id: string, reason: string): Promise<boolean>;
|
package/dist/tasks/store.js
CHANGED
|
@@ -87,7 +87,17 @@ export async function finishTaskAttempt(channelDir, id, result) {
|
|
|
87
87
|
control.usage.costUsd += Math.max(0, result.costUsd);
|
|
88
88
|
control.usage.wallTimeMinutes += Math.max(0, result.wallTimeMinutes);
|
|
89
89
|
control.lastFinishedAt = result.finishedAt.toISOString();
|
|
90
|
-
if (result.
|
|
90
|
+
if (result.silent) {
|
|
91
|
+
// The driver claimed before dispatch to prevent concurrent work. A silent
|
|
92
|
+
// turn performed no task advancement, so retain its cost audit but not its
|
|
93
|
+
// attempt charge.
|
|
94
|
+
control.usage.attempts = Math.max(0, control.usage.attempts - 1);
|
|
95
|
+
if (control.lastOutcome === "running") {
|
|
96
|
+
control.lastOutcome = "pending";
|
|
97
|
+
control.blockedReason = undefined;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
else if (result.failed) {
|
|
91
101
|
control.lastOutcome = "failed";
|
|
92
102
|
control.blockedReason = "The previous agent run failed; inspect the runtime error before retrying.";
|
|
93
103
|
}
|
|
@@ -9,7 +9,7 @@ declare const memoryManageSchema: Type.TObject<{
|
|
|
9
9
|
content: Type.TOptional<Type.TString>;
|
|
10
10
|
query: Type.TOptional<Type.TString>;
|
|
11
11
|
target: Type.TOptional<Type.TString>;
|
|
12
|
-
kind: Type.TOptional<Type.TUnion<[Type.TLiteral<"preference">, Type.TLiteral<"fact">, Type.TLiteral<"decision">, Type.TLiteral<"constraint">, Type.TLiteral<"open-loop">]>>;
|
|
12
|
+
kind: Type.TOptional<Type.TUnion<[Type.TLiteral<"preference">, Type.TLiteral<"fact">, Type.TLiteral<"decision">, Type.TLiteral<"constraint">, Type.TLiteral<"open-loop">, Type.TLiteral<"lesson">]>>;
|
|
13
13
|
}>;
|
|
14
14
|
export interface MemoryManageToolOptions {
|
|
15
15
|
channelId: string;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { Type } from "typebox";
|
|
2
2
|
import { getDefaultChannelMemoryQueue } from "../memory/channel-maintenance-queue.js";
|
|
3
3
|
import { applyChannelMemoryOps, getChannelMemoryPath, parseChannelMemoryEntries } from "../memory/files.js";
|
|
4
|
+
import { containsSecret } from "../memory/policy.js";
|
|
4
5
|
import { recallRelevantMemory } from "../memory/recall.js";
|
|
5
6
|
import { appendMemoryReviewLog } from "../memory/review-log.js";
|
|
7
|
+
import { hashMemoryContent } from "../memory/tombstones.js";
|
|
6
8
|
import { readOptionalTextFile } from "../shared/fs-utils.js";
|
|
7
9
|
const memoryManageSchema = Type.Object({
|
|
8
10
|
label: Type.String({ description: "Brief description of the memory change (shown to user)" }),
|
|
@@ -22,8 +24,18 @@ const memoryManageSchema = Type.Object({
|
|
|
22
24
|
Type.Literal("decision"),
|
|
23
25
|
Type.Literal("constraint"),
|
|
24
26
|
Type.Literal("open-loop"),
|
|
27
|
+
Type.Literal("lesson"),
|
|
25
28
|
], { description: "For save: what kind of durable memory this is." })),
|
|
26
29
|
});
|
|
30
|
+
function normalizeMemoryKind(kind) {
|
|
31
|
+
return kind === "preference" ||
|
|
32
|
+
kind === "decision" ||
|
|
33
|
+
kind === "constraint" ||
|
|
34
|
+
kind === "open-loop" ||
|
|
35
|
+
kind === "lesson"
|
|
36
|
+
? kind
|
|
37
|
+
: "fact";
|
|
38
|
+
}
|
|
27
39
|
function textResult(text, details) {
|
|
28
40
|
return { content: [{ type: "text", text }], details };
|
|
29
41
|
}
|
|
@@ -38,9 +50,18 @@ export function createMemoryManageTool(options) {
|
|
|
38
50
|
saved: false,
|
|
39
51
|
});
|
|
40
52
|
}
|
|
53
|
+
if (containsSecret(trimmed)) {
|
|
54
|
+
return textResult("This content looks like a credential or secret, so it was not saved. Store the secret in an approved secret manager and remember only its location.", { kind: "memory_manage", op: "save", saved: false, blockedReason: "secret" });
|
|
55
|
+
}
|
|
41
56
|
// Serialize through the shared channel memory queue so this never races with background
|
|
42
57
|
// consolidation/maintenance on the same channel's files.
|
|
43
|
-
const result = await queue.run(options.channelId, () => applyChannelMemoryOps(options.channelDir, [
|
|
58
|
+
const result = await queue.run(options.channelId, () => applyChannelMemoryOps(options.channelDir, [
|
|
59
|
+
{
|
|
60
|
+
op: "add",
|
|
61
|
+
content: trimmed,
|
|
62
|
+
metadata: { kind: normalizeMemoryKind(kind), sourceType: "user", trust: "explicit" },
|
|
63
|
+
},
|
|
64
|
+
]));
|
|
44
65
|
options.memoryCandidateStore.invalidate(getChannelMemoryPath(options.channelDir));
|
|
45
66
|
return textResult(`Saved to channel memory${kind ? ` (${kind})` : ""}.`, {
|
|
46
67
|
kind: "memory_manage",
|
|
@@ -115,17 +136,17 @@ export function createMemoryManageTool(options) {
|
|
|
115
136
|
throw new Error(`"${trimmed}" matched ${matches.length} entries; be more specific so only one is removed:\n${candidates}`);
|
|
116
137
|
}
|
|
117
138
|
const removed = matches[0];
|
|
118
|
-
await queue.run(options.channelId, () => applyChannelMemoryOps(options.channelDir, [{ op: "
|
|
139
|
+
await queue.run(options.channelId, () => applyChannelMemoryOps(options.channelDir, [{ op: "forget", targetId: removed.id, reason: "user forget" }]));
|
|
119
140
|
options.memoryCandidateStore.invalidate(memoryPath);
|
|
120
|
-
// Audit
|
|
121
|
-
//
|
|
141
|
+
// Audit by id/hash only. Copying the forgotten text into the review log would
|
|
142
|
+
// create a second active disclosure surface for the very content being removed.
|
|
122
143
|
await appendMemoryReviewLog(options.channelDir, {
|
|
123
144
|
timestamp: new Date().toISOString(),
|
|
124
145
|
channelId: options.channelId,
|
|
125
146
|
reason: "user-forget",
|
|
126
|
-
actions: [{ op: "forget",
|
|
147
|
+
actions: [{ op: "forget", entryId: removed.id, contentHash: hashMemoryContent(removed.content) }],
|
|
127
148
|
}).catch(() => { });
|
|
128
|
-
return textResult(
|
|
149
|
+
return textResult("Removed the entry from active channel memory and recorded a tombstone so automatic maintenance will not restore it. Original session history and retention backups are unchanged.", { kind: "memory_manage", op: "forget", forgotten: true, entryId: removed.id });
|
|
129
150
|
}
|
|
130
151
|
return {
|
|
131
152
|
name: "memory_manage",
|
|
@@ -496,10 +496,10 @@ async function doneTask(options, request) {
|
|
|
496
496
|
throw new Error(`Task "${id}" still has unfinished child tasks: ${children.join(", ")}. Finish or cancel them first.`);
|
|
497
497
|
}
|
|
498
498
|
if (fields.control?.sideEffects === "external") {
|
|
499
|
-
if (fields.control.externalApproval
|
|
499
|
+
if (fields.control.externalApproval === "required") {
|
|
500
500
|
throw new Error(`Task "${id}" requires explicit external-action approval before it can be completed.`);
|
|
501
501
|
}
|
|
502
|
-
if (fields.control.approvalBodyHash !== taskBodyHash(body)) {
|
|
502
|
+
if (fields.control.externalApproval === "granted" && fields.control.approvalBodyHash !== taskBodyHash(body)) {
|
|
503
503
|
throw new Error(`Task "${id}" changed after external-action approval; ask the user to run /tasks approve ${id} again.`);
|
|
504
504
|
}
|
|
505
505
|
}
|
package/dist/usage/ledger.d.ts
CHANGED
package/package.json
CHANGED