@oyasmi/pipiclaw 0.8.6 → 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.
- package/dist/agent/channel-runner.js +8 -2
- package/dist/agent/session-events.js +17 -5
- package/dist/log.d.ts +12 -20
- package/dist/log.js +195 -198
- package/dist/memory/lifecycle.js +4 -1
- package/dist/runtime/bootstrap.d.ts +1 -0
- package/dist/runtime/bootstrap.js +41 -11
- package/dist/runtime/dingtalk.js +19 -6
- package/dist/runtime/events.d.ts +2 -0
- package/dist/runtime/events.js +14 -7
- package/dist/runtime/store.d.ts +4 -6
- package/dist/runtime/store.js +44 -40
- package/dist/security/logger.d.ts +3 -1
- package/dist/security/logger.js +33 -10
- package/dist/shared/jsonl-appender.d.ts +27 -8
- package/dist/shared/jsonl-appender.js +128 -41
- package/dist/tools/bash.js +1 -1
- package/dist/tools/edit.js +1 -1
- package/dist/tools/grep.js +1 -1
- package/dist/tools/read.js +1 -1
- package/dist/tools/write-content.js +1 -1
- package/dist/tui/app.js +17 -0
- package/dist/usage/ledger.d.ts +2 -0
- package/dist/usage/ledger.js +3 -1
- package/dist/web/client.js +3 -3
- package/package.json +1 -1
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { appendFile } from "node:fs/promises";
|
|
1
|
+
import { appendFile, mkdir, rename, rm, stat } from "node:fs/promises";
|
|
3
2
|
import { dirname } from "node:path";
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
const DEFAULT_MAX_PENDING_RECORDS = 2_000;
|
|
4
|
+
const DEFAULT_MAX_PENDING_BYTES = 8 * 1024 * 1024;
|
|
5
|
+
const DEFAULT_RESERVED_CRITICAL_RECORDS = 100;
|
|
6
|
+
const DEFAULT_RESERVED_CRITICAL_BYTES = 512 * 1024;
|
|
6
7
|
/**
|
|
7
|
-
*
|
|
8
|
+
* Bounded append-only JSONL writer shared by runtime logs, channel archives,
|
|
9
|
+
* audit logs, event history, and the usage ledger.
|
|
8
10
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* the next success) and never rejects.
|
|
11
|
+
* Records are ordered per resolved path. Different paths may write in parallel.
|
|
12
|
+
* Slow or failed observability storage never creates an unbounded in-memory
|
|
13
|
+
* backlog and never rejects into the business path.
|
|
13
14
|
*/
|
|
14
15
|
export function createJsonlAppender(options) {
|
|
15
16
|
if (!options.path && !options.pathFor) {
|
|
@@ -17,26 +18,41 @@ export function createJsonlAppender(options) {
|
|
|
17
18
|
}
|
|
18
19
|
const maxRotations = options.maxRotations ?? 3;
|
|
19
20
|
const mode = options.mode ?? 0o600;
|
|
20
|
-
const
|
|
21
|
+
const maxPendingRecords = options.maxPendingRecords ?? DEFAULT_MAX_PENDING_RECORDS;
|
|
22
|
+
const maxPendingBytes = options.maxPendingBytes ?? DEFAULT_MAX_PENDING_BYTES;
|
|
23
|
+
const reservedCriticalRecords = Math.min(Math.floor(maxPendingRecords / 10), options.reservedCriticalRecords ?? DEFAULT_RESERVED_CRITICAL_RECORDS);
|
|
24
|
+
const reservedCriticalBytes = Math.min(Math.floor(maxPendingBytes / 8), options.reservedCriticalBytes ?? DEFAULT_RESERVED_CRITICAL_BYTES);
|
|
25
|
+
const chains = new Map();
|
|
21
26
|
const sizes = new Map();
|
|
22
27
|
const ensuredDirs = new Set();
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
28
|
+
const pending = new Set();
|
|
29
|
+
let pendingBytes = 0;
|
|
30
|
+
let closed = false;
|
|
31
|
+
let warnedWrite = false;
|
|
32
|
+
let warnedDrop = false;
|
|
33
|
+
const fallbackWarning = (event, message) => {
|
|
34
|
+
try {
|
|
35
|
+
process.stdout.write(`${new Date().toISOString()} WARN ${event} ${message}\n`);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
// Logging fallback failures must not escape into the runtime.
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
const resolvePath = (now, record) => options.pathFor ? options.pathFor(now, record) : options.path;
|
|
42
|
+
const ensureDir = async (filePath) => {
|
|
26
43
|
const dir = dirname(filePath);
|
|
27
44
|
if (ensuredDirs.has(dir))
|
|
28
45
|
return;
|
|
29
|
-
|
|
46
|
+
await mkdir(dir, { recursive: true, mode: 0o700 });
|
|
30
47
|
ensuredDirs.add(dir);
|
|
31
48
|
};
|
|
32
|
-
const currentSize = (filePath) => {
|
|
49
|
+
const currentSize = async (filePath) => {
|
|
33
50
|
const cached = sizes.get(filePath);
|
|
34
51
|
if (cached !== undefined)
|
|
35
52
|
return cached;
|
|
36
53
|
let size = 0;
|
|
37
54
|
try {
|
|
38
|
-
|
|
39
|
-
size = statSync(filePath).size;
|
|
55
|
+
size = (await stat(filePath)).size;
|
|
40
56
|
}
|
|
41
57
|
catch {
|
|
42
58
|
size = 0;
|
|
@@ -44,43 +60,114 @@ export function createJsonlAppender(options) {
|
|
|
44
60
|
sizes.set(filePath, size);
|
|
45
61
|
return size;
|
|
46
62
|
};
|
|
47
|
-
const rotate = (filePath) => {
|
|
63
|
+
const rotate = async (filePath) => {
|
|
64
|
+
if (maxRotations <= 0) {
|
|
65
|
+
await rm(filePath, { force: true });
|
|
66
|
+
sizes.set(filePath, 0);
|
|
67
|
+
await options.onRotate?.(filePath);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
48
70
|
for (let i = maxRotations - 1; i >= 1; i--) {
|
|
49
71
|
const from = `${filePath}.${i}`;
|
|
50
|
-
|
|
51
|
-
|
|
72
|
+
const to = `${filePath}.${i + 1}`;
|
|
73
|
+
try {
|
|
74
|
+
await rm(to, { force: true });
|
|
75
|
+
await rename(from, to);
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
if (!(error instanceof Error && "code" in error && error.code === "ENOENT"))
|
|
79
|
+
throw error;
|
|
52
80
|
}
|
|
53
81
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
82
|
+
await rm(`${filePath}.1`, { force: true });
|
|
83
|
+
await rename(filePath, `${filePath}.1`);
|
|
57
84
|
sizes.set(filePath, 0);
|
|
85
|
+
await options.onRotate?.(filePath);
|
|
58
86
|
};
|
|
59
|
-
const write = async (filePath, line) => {
|
|
60
|
-
ensureDir(filePath);
|
|
61
|
-
const
|
|
62
|
-
if (options.maxSizeBytes && options.maxSizeBytes > 0) {
|
|
63
|
-
|
|
64
|
-
rotate(filePath);
|
|
65
|
-
}
|
|
87
|
+
const write = async (filePath, line, lineBytes) => {
|
|
88
|
+
await ensureDir(filePath);
|
|
89
|
+
const size = await currentSize(filePath);
|
|
90
|
+
if (options.maxSizeBytes && options.maxSizeBytes > 0 && size + lineBytes > options.maxSizeBytes && size > 0) {
|
|
91
|
+
await rotate(filePath);
|
|
66
92
|
}
|
|
67
93
|
await appendFile(filePath, line, { encoding: "utf-8", mode });
|
|
68
|
-
sizes.set(filePath, currentSize(filePath) + lineBytes);
|
|
94
|
+
sizes.set(filePath, (await currentSize(filePath)) + lineBytes);
|
|
69
95
|
};
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const
|
|
73
|
-
|
|
96
|
+
const serialize = (record) => {
|
|
97
|
+
try {
|
|
98
|
+
const json = JSON.stringify(options.recordForWrite ? options.recordForWrite(record) : record);
|
|
99
|
+
if (json === undefined)
|
|
100
|
+
throw new Error("JSONL records must be JSON-serializable values");
|
|
101
|
+
const line = `${json}\n`;
|
|
102
|
+
return { filePath: resolvePath(new Date(), record), line, bytes: Buffer.byteLength(line) };
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
if (!warnedDrop) {
|
|
106
|
+
warnedDrop = true;
|
|
107
|
+
fallbackWarning("runtime.log_sink.dropped", "Failed to serialize JSONL record; record was dropped");
|
|
108
|
+
}
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
const enqueue = (record, priority) => {
|
|
113
|
+
if (closed)
|
|
114
|
+
return null;
|
|
115
|
+
const serialized = serialize(record);
|
|
116
|
+
if (!serialized)
|
|
117
|
+
return null;
|
|
118
|
+
const recordLimit = priority === "critical" ? maxPendingRecords : maxPendingRecords - reservedCriticalRecords;
|
|
119
|
+
const byteLimit = priority === "critical" ? maxPendingBytes : maxPendingBytes - reservedCriticalBytes;
|
|
120
|
+
if (pending.size >= recordLimit || pendingBytes + serialized.bytes > byteLimit) {
|
|
121
|
+
if (!warnedDrop) {
|
|
122
|
+
warnedDrop = true;
|
|
123
|
+
fallbackWarning("runtime.log_sink.dropped", "JSONL queue limit reached; record was dropped");
|
|
124
|
+
}
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
const previous = chains.get(serialized.filePath) ?? Promise.resolve();
|
|
128
|
+
const result = previous
|
|
129
|
+
.catch(() => undefined)
|
|
130
|
+
.then(async () => {
|
|
74
131
|
try {
|
|
75
|
-
await
|
|
76
|
-
|
|
132
|
+
await write(serialized.filePath, serialized.line, serialized.bytes);
|
|
133
|
+
warnedWrite = false;
|
|
77
134
|
}
|
|
78
|
-
catch
|
|
79
|
-
if (!
|
|
80
|
-
|
|
81
|
-
|
|
135
|
+
catch {
|
|
136
|
+
if (!warnedWrite) {
|
|
137
|
+
warnedWrite = true;
|
|
138
|
+
fallbackWarning("runtime.log_sink.failed", "Failed to append JSONL record; continuing without it");
|
|
82
139
|
}
|
|
83
140
|
}
|
|
141
|
+
});
|
|
142
|
+
const completion = result.finally(() => {
|
|
143
|
+
pending.delete(completion);
|
|
144
|
+
pendingBytes -= serialized.bytes;
|
|
145
|
+
if (chains.get(serialized.filePath) === completion)
|
|
146
|
+
chains.delete(serialized.filePath);
|
|
147
|
+
if (pending.size === 0)
|
|
148
|
+
warnedDrop = false;
|
|
149
|
+
});
|
|
150
|
+
chains.set(serialized.filePath, completion);
|
|
151
|
+
pending.add(completion);
|
|
152
|
+
pendingBytes += serialized.bytes;
|
|
153
|
+
return { ...serialized, completion };
|
|
154
|
+
};
|
|
155
|
+
const flush = async () => {
|
|
156
|
+
while (pending.size > 0) {
|
|
157
|
+
await Promise.allSettled([...pending]);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
return {
|
|
161
|
+
async append(record, priority = "critical") {
|
|
162
|
+
await enqueue(record, priority)?.completion;
|
|
163
|
+
},
|
|
164
|
+
tryAppend(record, priority = "normal") {
|
|
165
|
+
return enqueue(record, priority) !== null;
|
|
166
|
+
},
|
|
167
|
+
flush,
|
|
168
|
+
async close() {
|
|
169
|
+
closed = true;
|
|
170
|
+
await flush();
|
|
84
171
|
},
|
|
85
172
|
};
|
|
86
173
|
}
|
package/dist/tools/bash.js
CHANGED
|
@@ -96,7 +96,7 @@ export function createBashTool(executor, options = {}) {
|
|
|
96
96
|
if (securityConfig.enabled && securityConfig.commandGuard.enabled) {
|
|
97
97
|
const guardResult = guardCommand(command, securityConfig.commandGuard);
|
|
98
98
|
if (!guardResult.allowed) {
|
|
99
|
-
logSecurityEvent(securityContext.workspaceDir, securityConfig, {
|
|
99
|
+
await logSecurityEvent(securityContext.workspaceDir, securityConfig, {
|
|
100
100
|
type: "command",
|
|
101
101
|
tool: "bash",
|
|
102
102
|
channelId: options.channelId,
|
package/dist/tools/edit.js
CHANGED
|
@@ -128,7 +128,7 @@ export function createEditTool(executor, options = {}) {
|
|
|
128
128
|
if (securityConfig.enabled && securityConfig.pathGuard.enabled) {
|
|
129
129
|
const readGuard = guardPath(path, "read", { ...securityContext, config: securityConfig.pathGuard });
|
|
130
130
|
if (!readGuard.allowed) {
|
|
131
|
-
logSecurityEvent(securityContext.workspaceDir, securityConfig, {
|
|
131
|
+
await logSecurityEvent(securityContext.workspaceDir, securityConfig, {
|
|
132
132
|
type: "path",
|
|
133
133
|
tool: "edit",
|
|
134
134
|
channelId: options.channelId,
|
package/dist/tools/grep.js
CHANGED
|
@@ -152,7 +152,7 @@ export function createGrepTool(executor, options = {}) {
|
|
|
152
152
|
if (securityConfig.enabled && securityConfig.pathGuard.enabled) {
|
|
153
153
|
const guardResult = guardPath(searchPath, "read", { ...securityContext, config: securityConfig.pathGuard });
|
|
154
154
|
if (!guardResult.allowed) {
|
|
155
|
-
logSecurityEvent(securityContext.workspaceDir, securityConfig, {
|
|
155
|
+
await logSecurityEvent(securityContext.workspaceDir, securityConfig, {
|
|
156
156
|
type: "path",
|
|
157
157
|
tool: "grep",
|
|
158
158
|
channelId: options.channelId,
|
package/dist/tools/read.js
CHANGED
|
@@ -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,
|
|
@@ -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
|
}
|
package/dist/usage/ledger.d.ts
CHANGED
|
@@ -40,6 +40,8 @@ export declare const UNTRACKED_CHANNEL_ID = "(untracked)";
|
|
|
40
40
|
export interface UsageLedger {
|
|
41
41
|
record(entry: Omit<UsageLedgerEntry, "ts">): void;
|
|
42
42
|
summarize(query: UsageSummaryQuery): UsageSummary;
|
|
43
|
+
flush?(): Promise<void>;
|
|
44
|
+
close?(): Promise<void>;
|
|
43
45
|
}
|
|
44
46
|
export interface CreateUsageLedgerOptions {
|
|
45
47
|
baseDir?: string;
|
package/dist/usage/ledger.js
CHANGED
|
@@ -34,8 +34,10 @@ export function createUsageLedger(options = {}) {
|
|
|
34
34
|
...entry,
|
|
35
35
|
channelId: entry.channelId || UNTRACKED_CHANNEL_ID,
|
|
36
36
|
};
|
|
37
|
-
|
|
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();
|
package/dist/web/client.js
CHANGED
|
@@ -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