@oyasmi/pipiclaw 0.8.5 → 0.8.7-beta.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 (74) hide show
  1. package/dist/agent/channel-runner.d.ts +1 -0
  2. package/dist/agent/channel-runner.js +27 -10
  3. package/dist/agent/command-extension.d.ts +1 -0
  4. package/dist/agent/command-extension.js +6 -0
  5. package/dist/agent/commands.js +6 -0
  6. package/dist/agent/session-events.js +17 -5
  7. package/dist/agent/types.d.ts +1 -0
  8. package/dist/log.d.ts +12 -20
  9. package/dist/log.js +195 -198
  10. package/dist/memory/bootstrap.d.ts +5 -0
  11. package/dist/memory/bootstrap.js +14 -4
  12. package/dist/memory/candidates.d.ts +2 -0
  13. package/dist/memory/candidates.js +13 -3
  14. package/dist/memory/commands.d.ts +6 -0
  15. package/dist/memory/commands.js +112 -0
  16. package/dist/memory/consolidation.d.ts +3 -0
  17. package/dist/memory/consolidation.js +68 -26
  18. package/dist/memory/files.d.ts +13 -0
  19. package/dist/memory/files.js +65 -8
  20. package/dist/memory/lifecycle.d.ts +2 -1
  21. package/dist/memory/lifecycle.js +38 -7
  22. package/dist/memory/maintenance-jobs.js +48 -23
  23. package/dist/memory/metadata.d.ts +60 -0
  24. package/dist/memory/metadata.js +135 -0
  25. package/dist/memory/policy.d.ts +3 -0
  26. package/dist/memory/policy.js +19 -0
  27. package/dist/memory/post-turn-review.d.ts +11 -1
  28. package/dist/memory/post-turn-review.js +78 -19
  29. package/dist/memory/promotion.d.ts +5 -2
  30. package/dist/memory/promotion.js +3 -1
  31. package/dist/memory/recall.d.ts +3 -0
  32. package/dist/memory/recall.js +27 -5
  33. package/dist/memory/review-log.d.ts +2 -0
  34. package/dist/memory/review-log.js +15 -0
  35. package/dist/memory/session-corpus.js +11 -3
  36. package/dist/memory/session-search.js +8 -4
  37. package/dist/memory/session.d.ts +1 -0
  38. package/dist/memory/session.js +3 -1
  39. package/dist/memory/sidecar-worker.d.ts +1 -0
  40. package/dist/memory/sidecar-worker.js +1 -0
  41. package/dist/memory/source-window.d.ts +25 -0
  42. package/dist/memory/source-window.js +64 -0
  43. package/dist/memory/tombstones.d.ts +12 -0
  44. package/dist/memory/tombstones.js +44 -0
  45. package/dist/memory/transcript.js +26 -3
  46. package/dist/runtime/bootstrap.d.ts +1 -0
  47. package/dist/runtime/bootstrap.js +42 -11
  48. package/dist/runtime/dingtalk.js +19 -6
  49. package/dist/runtime/events.d.ts +2 -0
  50. package/dist/runtime/events.js +14 -7
  51. package/dist/runtime/store.d.ts +4 -6
  52. package/dist/runtime/store.js +44 -40
  53. package/dist/runtime/task-commands.js +1 -1
  54. package/dist/security/logger.d.ts +3 -1
  55. package/dist/security/logger.js +33 -10
  56. package/dist/settings.js +1 -1
  57. package/dist/shared/jsonl-appender.d.ts +27 -8
  58. package/dist/shared/jsonl-appender.js +128 -41
  59. package/dist/tasks/control.js +9 -3
  60. package/dist/tasks/store.d.ts +1 -0
  61. package/dist/tasks/store.js +11 -1
  62. package/dist/tools/bash.js +1 -1
  63. package/dist/tools/edit.js +1 -1
  64. package/dist/tools/grep.js +1 -1
  65. package/dist/tools/memory-manage.d.ts +1 -1
  66. package/dist/tools/memory-manage.js +27 -6
  67. package/dist/tools/read.js +1 -1
  68. package/dist/tools/task-manage.js +2 -2
  69. package/dist/tools/write-content.js +1 -1
  70. package/dist/tui/app.js +17 -0
  71. package/dist/usage/ledger.d.ts +4 -0
  72. package/dist/usage/ledger.js +3 -1
  73. package/dist/web/client.js +3 -3
  74. package/package.json +1 -1
@@ -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, [{ op: "add", content: trimmed }]));
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: "invalidate", targetId: removed.id, reason: "user forget" }]));
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 trail: record what was forgotten so the maintenance log can answer "who dropped what,
121
- // when, and why" — the `.memory-backups/` copy is a recovery aid, not an audit record.
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", target: removed.content, matchedBy: trimmed }],
147
+ actions: [{ op: "forget", entryId: removed.id, contentHash: hashMemoryContent(removed.content) }],
127
148
  }).catch(() => { });
128
- return textResult(`Forgot: ${removed.content}`, { kind: "memory_manage", op: "forget", forgotten: true });
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",
@@ -101,7 +101,7 @@ export function createReadTool(executor, options = {}) {
101
101
  if (securityConfig.enabled && securityConfig.pathGuard.enabled) {
102
102
  const guardResult = guardPath(path, "read", { ...securityContext, config: securityConfig.pathGuard });
103
103
  if (!guardResult.allowed) {
104
- logSecurityEvent(securityContext.workspaceDir, securityConfig, {
104
+ await logSecurityEvent(securityContext.workspaceDir, securityConfig, {
105
105
  type: "path",
106
106
  tool: "read",
107
107
  channelId: options.channelId,
@@ -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 !== "granted") {
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
  }
@@ -21,7 +21,7 @@ export async function writeContent(executor, path, content, signal, options) {
21
21
  if (securityConfig.enabled && securityConfig.pathGuard.enabled) {
22
22
  const guardResult = guardPath(path, "write", { ...securityContext, config: securityConfig.pathGuard });
23
23
  if (!guardResult.allowed) {
24
- logSecurityEvent(securityContext.workspaceDir, securityConfig, {
24
+ await logSecurityEvent(securityContext.workspaceDir, securityConfig, {
25
25
  type: "path",
26
26
  tool: options?.toolName ?? "write",
27
27
  channelId: options?.channelId,
package/dist/tui/app.js CHANGED
@@ -18,6 +18,7 @@ import { finalDeliveryOf, progressStyleOf } from "../runtime/dingtalk.js";
18
18
  import { handleEventsCommand } from "../runtime/event-commands.js";
19
19
  import { ChannelStore } from "../runtime/store.js";
20
20
  import { handleTasksCommand } from "../runtime/task-commands.js";
21
+ import { flushSecurityLogs } from "../security/logger.js";
21
22
  import { getUsageLedger } from "../usage/ledger.js";
22
23
  import { parseUsageMode, renderUsageReport } from "../usage/render.js";
23
24
  import { TUI_SLASH_COMMANDS } from "./commands.js";
@@ -25,6 +26,20 @@ import { createFrontend } from "./renderer.js";
25
26
  import { TurnController } from "./turn-controller.js";
26
27
  const DEFAULT_CHANNEL_ID = "tui_local";
27
28
  const CHANNEL_ID_PATTERN = /^[A-Za-z0-9._-]+$/;
29
+ const STORAGE_FLUSH_TIMEOUT_MS = 10_000;
30
+ async function waitForStorageFlush(task) {
31
+ await new Promise((resolve) => {
32
+ const timer = setTimeout(resolve, STORAGE_FLUSH_TIMEOUT_MS);
33
+ timer.unref?.();
34
+ void task.then(() => {
35
+ clearTimeout(timer);
36
+ resolve();
37
+ }, () => {
38
+ clearTimeout(timer);
39
+ resolve();
40
+ });
41
+ });
42
+ }
28
43
  export function resolveChannelId(raw, io = console) {
29
44
  const channel = raw?.trim() || DEFAULT_CHANNEL_ID;
30
45
  if (!CHANNEL_ID_PATTERN.test(channel)) {
@@ -107,5 +122,7 @@ export async function runTuiApp(options) {
107
122
  }
108
123
  finally {
109
124
  resetRunner(channelId);
125
+ await waitForStorageFlush(Promise.allSettled([store.close(), getUsageLedger().flush?.() ?? Promise.resolve(), flushSecurityLogs()]));
126
+ await waitForStorageFlush(log.flushLogging());
110
127
  }
111
128
  }
@@ -19,6 +19,8 @@ export interface UsageLedgerEntry {
19
19
  kind: UsageKind;
20
20
  model: string;
21
21
  label?: string;
22
+ /** Joins sidecar cost to the memory source window and review-log outcome. */
23
+ correlationId?: string;
22
24
  usage: UsageTokens;
23
25
  cost: UsageCost;
24
26
  }
@@ -38,6 +40,8 @@ export declare const UNTRACKED_CHANNEL_ID = "(untracked)";
38
40
  export interface UsageLedger {
39
41
  record(entry: Omit<UsageLedgerEntry, "ts">): void;
40
42
  summarize(query: UsageSummaryQuery): UsageSummary;
43
+ flush?(): Promise<void>;
44
+ close?(): Promise<void>;
41
45
  }
42
46
  export interface CreateUsageLedgerOptions {
43
47
  baseDir?: string;
@@ -34,8 +34,10 @@ export function createUsageLedger(options = {}) {
34
34
  ...entry,
35
35
  channelId: entry.channelId || UNTRACKED_CHANNEL_ID,
36
36
  };
37
- void appender.append(full);
37
+ appender.tryAppend(full);
38
38
  },
39
+ flush: () => appender.flush(),
40
+ close: () => appender.close(),
39
41
  summarize(query) {
40
42
  const until = query.until ?? new Date();
41
43
  const sinceMs = query.since.getTime();
@@ -65,8 +65,8 @@ function getProxyAgent(requestUrl, explicitProxy) {
65
65
  agentCache.set(cacheKey, agent);
66
66
  return agent;
67
67
  }
68
- function logBlockedRequest(context, error) {
69
- logSecurityEvent(context.workspaceDir, context.securityConfig, {
68
+ async function logBlockedRequest(context, error) {
69
+ await logSecurityEvent(context.workspaceDir, context.securityConfig, {
70
70
  type: "network",
71
71
  tool: "web",
72
72
  channelId: context.channelId,
@@ -104,7 +104,7 @@ export class WebHttpClient {
104
104
  }
105
105
  catch (error) {
106
106
  if (error instanceof NetworkGuardError) {
107
- logBlockedRequest(this.context, error);
107
+ await logBlockedRequest(this.context, error);
108
108
  }
109
109
  throw error;
110
110
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oyasmi/pipiclaw",
3
- "version": "0.8.5",
3
+ "version": "0.8.7-beta.1",
4
4
  "description": "An AI assistant runtime for coding and team workflows, with DingTalk AI Cards, sub-agents, memory, and scheduled events.",
5
5
  "type": "module",
6
6
  "bin": {