feat: line-based process logs
This commit is contained in:
@@ -9,7 +9,7 @@ function clampTtl(value: number | undefined) {
|
||||
return Math.min(Math.max(value, MIN_JOB_TTL_MS), MAX_JOB_TTL_MS);
|
||||
}
|
||||
|
||||
const JOB_TTL_MS = clampTtl(
|
||||
let jobTtlMs = clampTtl(
|
||||
Number.parseInt(process.env.PI_BASH_JOB_TTL_MS ?? "", 10),
|
||||
);
|
||||
|
||||
@@ -163,14 +163,18 @@ export function clearFinished() {
|
||||
export function resetProcessRegistryForTests() {
|
||||
runningSessions.clear();
|
||||
finishedSessions.clear();
|
||||
if (sweeper) {
|
||||
clearInterval(sweeper);
|
||||
sweeper = null;
|
||||
}
|
||||
stopSweeper();
|
||||
}
|
||||
|
||||
export function setJobTtlMs(value?: number) {
|
||||
if (value === undefined || Number.isNaN(value)) return;
|
||||
jobTtlMs = clampTtl(value);
|
||||
stopSweeper();
|
||||
startSweeper();
|
||||
}
|
||||
|
||||
function pruneFinishedSessions() {
|
||||
const cutoff = Date.now() - JOB_TTL_MS;
|
||||
const cutoff = Date.now() - jobTtlMs;
|
||||
for (const [id, session] of finishedSessions.entries()) {
|
||||
if (session.endedAt < cutoff) {
|
||||
finishedSessions.delete(id);
|
||||
@@ -180,9 +184,12 @@ function pruneFinishedSessions() {
|
||||
|
||||
function startSweeper() {
|
||||
if (sweeper) return;
|
||||
sweeper = setInterval(
|
||||
pruneFinishedSessions,
|
||||
Math.max(30_000, JOB_TTL_MS / 6),
|
||||
);
|
||||
sweeper = setInterval(pruneFinishedSessions, Math.max(30_000, jobTtlMs / 6));
|
||||
sweeper.unref?.();
|
||||
}
|
||||
|
||||
function stopSweeper() {
|
||||
if (!sweeper) return;
|
||||
clearInterval(sweeper);
|
||||
sweeper = null;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,30 @@
|
||||
import { beforeEach, describe, expect, it } from "vitest";
|
||||
import {
|
||||
bashTool,
|
||||
createBashTool,
|
||||
createProcessTool,
|
||||
processTool,
|
||||
} from "./bash-tools.js";
|
||||
import { resetProcessRegistryForTests } from "./bash-process-registry.js";
|
||||
import { bashTool, processTool } from "./bash-tools.js";
|
||||
|
||||
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||
|
||||
async function waitForCompletion(sessionId: string) {
|
||||
let status = "running";
|
||||
const deadline = Date.now() + 2000;
|
||||
while (Date.now() < deadline && status === "running") {
|
||||
const poll = await processTool.execute("call-wait", {
|
||||
action: "poll",
|
||||
sessionId,
|
||||
});
|
||||
status = (poll.details as { status: string }).status;
|
||||
if (status === "running") {
|
||||
await sleep(20);
|
||||
}
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
resetProcessRegistryForTests();
|
||||
});
|
||||
@@ -54,4 +75,87 @@ describe("bash tool backgrounding", () => {
|
||||
).sessions;
|
||||
expect(sessions.some((s) => s.sessionId === sessionId)).toBe(true);
|
||||
});
|
||||
|
||||
it("derives a session name from the command", async () => {
|
||||
const result = await bashTool.execute("call1", {
|
||||
command: "echo hello",
|
||||
background: true,
|
||||
});
|
||||
const sessionId = (result.details as { sessionId: string }).sessionId;
|
||||
await sleep(25);
|
||||
|
||||
const list = await processTool.execute("call2", { action: "list" });
|
||||
const sessions = (
|
||||
list.details as { sessions: Array<{ sessionId: string; name?: string }> }
|
||||
).sessions;
|
||||
const entry = sessions.find((s) => s.sessionId === sessionId);
|
||||
expect(entry?.name).toBe("echo hello");
|
||||
});
|
||||
|
||||
it("uses default timeout when timeout is omitted", async () => {
|
||||
const customBash = createBashTool({ timeoutSec: 1, backgroundMs: 10 });
|
||||
const customProcess = createProcessTool();
|
||||
|
||||
const result = await customBash.execute("call1", {
|
||||
command: "node -e \"setInterval(() => {}, 1000)\"",
|
||||
background: true,
|
||||
});
|
||||
|
||||
const sessionId = (result.details as { sessionId: string }).sessionId;
|
||||
let status = "running";
|
||||
const deadline = Date.now() + 5000;
|
||||
|
||||
while (Date.now() < deadline && status === "running") {
|
||||
const poll = await customProcess.execute("call2", {
|
||||
action: "poll",
|
||||
sessionId,
|
||||
});
|
||||
status = (poll.details as { status: string }).status;
|
||||
if (status === "running") {
|
||||
await sleep(50);
|
||||
}
|
||||
}
|
||||
|
||||
expect(status).toBe("failed");
|
||||
});
|
||||
|
||||
it("logs line-based slices and defaults to last lines", async () => {
|
||||
const result = await bashTool.execute("call1", {
|
||||
command:
|
||||
"node -e \"console.log('one'); console.log('two'); console.log('three');\"",
|
||||
background: true,
|
||||
});
|
||||
const sessionId = (result.details as { sessionId: string }).sessionId;
|
||||
|
||||
const status = await waitForCompletion(sessionId);
|
||||
|
||||
const log = await processTool.execute("call3", {
|
||||
action: "log",
|
||||
sessionId,
|
||||
limit: 2,
|
||||
});
|
||||
const textBlock = log.content.find((c) => c.type === "text");
|
||||
expect(textBlock?.text).toBe("two\nthree");
|
||||
expect((log.details as { totalLines?: number }).totalLines).toBe(3);
|
||||
expect(status).toBe("completed");
|
||||
});
|
||||
|
||||
it("supports line offsets for log slices", async () => {
|
||||
const result = await bashTool.execute("call1", {
|
||||
command:
|
||||
"node -e \"console.log('alpha'); console.log('beta'); console.log('gamma');\"",
|
||||
background: true,
|
||||
});
|
||||
const sessionId = (result.details as { sessionId: string }).sessionId;
|
||||
await waitForCompletion(sessionId);
|
||||
|
||||
const log = await processTool.execute("call2", {
|
||||
action: "log",
|
||||
sessionId,
|
||||
offset: 1,
|
||||
limit: 1,
|
||||
});
|
||||
const textBlock = log.content.find((c) => c.type === "text");
|
||||
expect(textBlock?.text).toBe("beta");
|
||||
});
|
||||
});
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -325,7 +325,9 @@ export async function runEmbeddedPiAgent(params: {
|
||||
await loadWorkspaceBootstrapFiles(resolvedWorkspace);
|
||||
const contextFiles = buildBootstrapContextFiles(bootstrapFiles);
|
||||
const promptSkills = resolvePromptSkills(skillsSnapshot, skillEntries);
|
||||
const tools = createClawdisCodingTools();
|
||||
const tools = createClawdisCodingTools({
|
||||
bash: params.config?.agent?.bash,
|
||||
});
|
||||
const machineName = await getMachineDisplayName();
|
||||
const runtimeInfo = {
|
||||
host: machineName,
|
||||
|
||||
@@ -32,6 +32,9 @@ describe("createClawdisCodingTools", () => {
|
||||
anyOf?: Array<{ properties?: Record<string, unknown> }>;
|
||||
properties?: Record<string, unknown>;
|
||||
};
|
||||
if (!Array.isArray(parameters.anyOf) || parameters.anyOf.length === 0) {
|
||||
continue;
|
||||
}
|
||||
const actionValues = new Set<string>();
|
||||
for (const variant of parameters.anyOf ?? []) {
|
||||
const action = variant?.properties?.action as
|
||||
@@ -45,6 +48,9 @@ describe("createClawdisCodingTools", () => {
|
||||
}
|
||||
}
|
||||
|
||||
if (actionValues.size <= 1) {
|
||||
continue;
|
||||
}
|
||||
const mergedAction = parameters.properties?.action as
|
||||
| { const?: unknown; enum?: unknown[] }
|
||||
| undefined;
|
||||
|
||||
@@ -4,7 +4,12 @@ import { type TSchema, Type } from "@sinclair/typebox";
|
||||
|
||||
import { detectMime } from "../media/mime.js";
|
||||
import { startWebLoginWithQr, waitForWebLogin } from "../web/login-qr.js";
|
||||
import { bashTool, processTool } from "./bash-tools.js";
|
||||
import {
|
||||
type BashToolDefaults,
|
||||
type ProcessToolDefaults,
|
||||
createBashTool,
|
||||
createProcessTool,
|
||||
} from "./bash-tools.js";
|
||||
import { createClawdisTools } from "./clawdis-tools.js";
|
||||
import { sanitizeToolResultImages } from "./tool-images.js";
|
||||
|
||||
@@ -288,18 +293,24 @@ function createClawdisReadTool(base: AnyAgentTool): AnyAgentTool {
|
||||
};
|
||||
}
|
||||
|
||||
export function createClawdisCodingTools(): AnyAgentTool[] {
|
||||
export function createClawdisCodingTools(options?: {
|
||||
bash?: BashToolDefaults & ProcessToolDefaults;
|
||||
}): AnyAgentTool[] {
|
||||
const bashToolName = "bash";
|
||||
const base = (codingTools as unknown as AnyAgentTool[]).flatMap((tool) => {
|
||||
if (tool.name === readTool.name) return [createClawdisReadTool(tool)];
|
||||
if (tool.name === bashTool.name) return [];
|
||||
if (tool.name === bashToolName) return [];
|
||||
return [tool as AnyAgentTool];
|
||||
});
|
||||
const tools: AnyAgentTool[] = [
|
||||
const bashTool = createBashTool(options?.bash);
|
||||
const processTool = createProcessTool({
|
||||
cleanupMs: options?.bash?.cleanupMs,
|
||||
});
|
||||
return [
|
||||
...base,
|
||||
bashTool as unknown as AnyAgentTool,
|
||||
processTool as unknown as AnyAgentTool,
|
||||
bashTool,
|
||||
processTool,
|
||||
createWhatsAppLoginTool(),
|
||||
...createClawdisTools(),
|
||||
];
|
||||
return tools.map(normalizeToolParameters);
|
||||
].map(normalizeToolParameters);
|
||||
}
|
||||
|
||||
@@ -325,6 +325,15 @@ export type ClawdisConfig = {
|
||||
typingIntervalSeconds?: number;
|
||||
/** Periodic background heartbeat runs (minutes). 0 disables. */
|
||||
heartbeatMinutes?: number;
|
||||
/** Bash tool defaults. */
|
||||
bash?: {
|
||||
/** Default time (ms) before a bash command auto-backgrounds. */
|
||||
backgroundMs?: number;
|
||||
/** Default timeout (seconds) before auto-killing bash commands. */
|
||||
timeoutSec?: number;
|
||||
/** How long to keep finished sessions in memory (ms). */
|
||||
cleanupMs?: number;
|
||||
};
|
||||
};
|
||||
routing?: RoutingConfig;
|
||||
messages?: MessagesConfig;
|
||||
@@ -573,6 +582,13 @@ const ClawdisSchema = z.object({
|
||||
mediaMaxMb: z.number().positive().optional(),
|
||||
typingIntervalSeconds: z.number().int().positive().optional(),
|
||||
heartbeatMinutes: z.number().nonnegative().optional(),
|
||||
bash: z
|
||||
.object({
|
||||
backgroundMs: z.number().int().positive().optional(),
|
||||
timeoutSec: z.number().int().positive().optional(),
|
||||
cleanupMs: z.number().int().positive().optional(),
|
||||
})
|
||||
.optional(),
|
||||
})
|
||||
.optional(),
|
||||
routing: RoutingSchema,
|
||||
|
||||
Reference in New Issue
Block a user