@sema-agent/server 1.315.0 → 1.317.0
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 +9 -0
- package/README.zh-CN.md +7 -0
- package/dist/config-types.d.ts +1 -1
- package/dist/config.d.ts +14 -0
- package/dist/config.js +148 -45
- package/dist/elicitation.js +2 -2
- package/dist/http/idempotency.d.ts +1 -0
- package/dist/http/idempotency.js +3 -0
- package/dist/http/principal-gate.d.ts +12 -0
- package/dist/http/principal-gate.js +52 -0
- package/dist/http/route-ctx.d.ts +116 -0
- package/dist/http/route-ctx.js +2 -0
- package/dist/http/routes/approvals-assistant.d.ts +11 -0
- package/dist/http/routes/approvals-assistant.js +530 -0
- package/dist/http/routes/attachments.d.ts +4 -0
- package/dist/http/routes/attachments.js +106 -0
- package/dist/http/routes/capabilities.d.ts +4 -0
- package/dist/http/routes/capabilities.js +111 -0
- package/dist/http/routes/fleet.d.ts +4 -0
- package/dist/http/routes/fleet.js +147 -0
- package/dist/http/routes/images.d.ts +20 -0
- package/dist/http/routes/images.js +572 -0
- package/dist/http/routes/leader.d.ts +4 -0
- package/dist/http/routes/leader.js +48 -0
- package/dist/http/routes/memory-policy.d.ts +4 -0
- package/dist/http/routes/memory-policy.js +105 -0
- package/dist/http/routes/notify-wake.d.ts +4 -0
- package/dist/http/routes/notify-wake.js +133 -0
- package/dist/http/routes/observability.d.ts +4 -0
- package/dist/http/routes/observability.js +63 -0
- package/dist/http/routes/runs.d.ts +19 -0
- package/dist/http/routes/runs.js +967 -0
- package/dist/http/routes/session-sync.d.ts +36 -0
- package/dist/http/routes/session-sync.js +513 -0
- package/dist/http/routes/sessions-list.d.ts +4 -0
- package/dist/http/routes/sessions-list.js +117 -0
- package/dist/http/routes/sessions.d.ts +17 -0
- package/dist/http/routes/sessions.js +746 -0
- package/dist/http/routes/side-query.d.ts +4 -0
- package/dist/http/routes/side-query.js +88 -0
- package/dist/http/routes/tasks.d.ts +4 -0
- package/dist/http/routes/tasks.js +632 -0
- package/dist/http/routes/trace-usage.d.ts +4 -0
- package/dist/http/routes/trace-usage.js +239 -0
- package/dist/http/routes/workflows.d.ts +5 -0
- package/dist/http/routes/workflows.js +337 -0
- package/dist/http/run-meta.d.ts +11 -0
- package/dist/http/run-meta.js +16 -0
- package/dist/http/send.d.ts +7 -0
- package/dist/http/send.js +34 -0
- package/dist/http/server.d.ts +12 -12
- package/dist/http/server.js +289 -5502
- package/dist/http/sse-log.d.ts +25 -0
- package/dist/http/sse-log.js +59 -0
- package/dist/http/wire-types.d.ts +6 -0
- package/dist/http/workspace-content.d.ts +3 -0
- package/dist/http/workspace-content.js +20 -0
- package/dist/leader/endpoint.js +4 -4
- package/dist/main.js +5 -5
- package/dist/plugins/approval-store-sql.d.ts +52 -0
- package/dist/plugins/approval-store-sql.js +119 -0
- package/dist/plugins/breaker-state-sql.d.ts +29 -0
- package/dist/plugins/breaker-state-sql.js +104 -0
- package/dist/plugins/file-snapshot-store-sql.d.ts +38 -0
- package/dist/plugins/file-snapshot-store-sql.js +210 -0
- package/dist/plugins/image-index-sql.d.ts +33 -0
- package/dist/plugins/image-index-sql.js +328 -0
- package/dist/plugins/outcome-ledger-sql.d.ts +70 -0
- package/dist/plugins/outcome-ledger-sql.js +158 -0
- package/dist/plugins/pg-approval-store.d.ts +1 -27
- package/dist/plugins/pg-approval-store.js +1 -83
- package/dist/plugins/pg-breaker-state.d.ts +1 -19
- package/dist/plugins/pg-breaker-state.js +1 -88
- package/dist/plugins/pg-file-snapshot-store.d.ts +1 -27
- package/dist/plugins/pg-file-snapshot-store.js +1 -190
- package/dist/plugins/pg-image-index.d.ts +1 -21
- package/dist/plugins/pg-image-index.js +1 -217
- package/dist/plugins/pg-outcome-ledger.d.ts +2 -22
- package/dist/plugins/pg-outcome-ledger.js +1 -104
- package/dist/plugins/pg-resume-anchor-store.d.ts +1 -13
- package/dist/plugins/pg-resume-anchor-store.js +1 -22
- package/dist/plugins/pg-run-store.d.ts +1 -75
- package/dist/plugins/pg-run-store.js +1 -287
- package/dist/plugins/pg-session-policy-store.d.ts +1 -11
- package/dist/plugins/pg-session-policy-store.js +1 -90
- package/dist/plugins/pg-tool-result-store.d.ts +1 -15
- package/dist/plugins/pg-tool-result-store.js +1 -52
- package/dist/plugins/pg-workflow-journal-store.d.ts +1 -20
- package/dist/plugins/pg-workflow-journal-store.js +1 -34
- package/dist/plugins/pg-workflow-run-store.d.ts +1 -55
- package/dist/plugins/pg-workflow-run-store.js +1 -220
- package/dist/plugins/remote-env-host.js +4 -3
- package/dist/plugins/resume-anchor-store-sql.d.ts +23 -0
- package/dist/plugins/resume-anchor-store-sql.js +38 -0
- package/dist/plugins/run-store-sql.d.ts +89 -0
- package/dist/plugins/run-store-sql.js +397 -0
- package/dist/plugins/session-policy-store-sql.d.ts +22 -0
- package/dist/plugins/session-policy-store-sql.js +109 -0
- package/dist/plugins/tidb-approval-store.d.ts +1 -39
- package/dist/plugins/tidb-approval-store.js +1 -71
- package/dist/plugins/tidb-breaker-state.d.ts +1 -17
- package/dist/plugins/tidb-breaker-state.js +1 -74
- package/dist/plugins/tidb-file-snapshot-store.d.ts +1 -27
- package/dist/plugins/tidb-file-snapshot-store.js +1 -192
- package/dist/plugins/tidb-image-index.d.ts +1 -19
- package/dist/plugins/tidb-image-index.js +1 -149
- package/dist/plugins/tidb-outcome-ledger.d.ts +1 -57
- package/dist/plugins/tidb-outcome-ledger.js +1 -97
- package/dist/plugins/tidb-resume-anchor-store.d.ts +1 -13
- package/dist/plugins/tidb-resume-anchor-store.js +1 -22
- package/dist/plugins/tidb-run-store.d.ts +1 -75
- package/dist/plugins/tidb-run-store.js +1 -306
- package/dist/plugins/tidb-session-policy-store.d.ts +1 -11
- package/dist/plugins/tidb-session-policy-store.js +1 -86
- package/dist/plugins/tidb-tool-result-store.d.ts +1 -13
- package/dist/plugins/tidb-tool-result-store.js +1 -38
- package/dist/plugins/tidb-workflow-journal-store.d.ts +1 -20
- package/dist/plugins/tidb-workflow-journal-store.js +1 -34
- package/dist/plugins/tidb-workflow-run-store.d.ts +1 -61
- package/dist/plugins/tidb-workflow-run-store.js +1 -231
- package/dist/plugins/tool-result-store-sql.d.ts +25 -0
- package/dist/plugins/tool-result-store-sql.js +69 -0
- package/dist/plugins/workflow-journal-store-sql.d.ts +30 -0
- package/dist/plugins/workflow-journal-store-sql.js +53 -0
- package/dist/plugins/workflow-run-store-sql.d.ts +85 -0
- package/dist/plugins/workflow-run-store-sql.js +299 -0
- package/dist/question.js +2 -2
- package/dist/run-local.js +1 -1
- package/dist/tool-approval.js +2 -2
- package/dist/trace/ledger-sink.js +1 -1
- package/dist/trace/project.d.ts +1 -0
- package/dist/trace/project.js +3 -0
- package/package.json +1 -1
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
export interface SseLogProvider<E extends {
|
|
3
|
+
seq: number;
|
|
4
|
+
}> {
|
|
5
|
+
statusOf(id: string): Promise<{
|
|
6
|
+
status: string;
|
|
7
|
+
updatedAt: string;
|
|
8
|
+
} | null | undefined>;
|
|
9
|
+
getEvents(id: string, after: number): Promise<E[]>;
|
|
10
|
+
retainedFrom(id: string): Promise<number>;
|
|
11
|
+
formatEvent(ev: E): {
|
|
12
|
+
id: number;
|
|
13
|
+
event: string;
|
|
14
|
+
data: unknown;
|
|
15
|
+
};
|
|
16
|
+
staleFrame(): {
|
|
17
|
+
event: string;
|
|
18
|
+
data: unknown;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export declare function streamSseLog<E extends {
|
|
22
|
+
seq: number;
|
|
23
|
+
}>(req: IncomingMessage, res: ServerResponse, provider: SseLogProvider<E>, id: string, staleMs: number): Promise<void>;
|
|
24
|
+
export declare function sleep(ms: number): Promise<void>;
|
|
25
|
+
//# sourceMappingURL=sse-log.d.ts.map
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { sendJson, sendError, sseHeaders } from "./send.js";
|
|
2
|
+
export async function streamSseLog(req, res, provider, id, staleMs) {
|
|
3
|
+
const lastId = Number(req.headers["last-event-id"] ?? new URL(req.url ?? "", "http://x").searchParams.get("from") ?? 0);
|
|
4
|
+
let from = Number.isFinite(lastId) ? lastId : 0;
|
|
5
|
+
if (from > 0) {
|
|
6
|
+
const retainedFrom = await provider.retainedFrom(id);
|
|
7
|
+
if (retainedFrom > from + 1) {
|
|
8
|
+
sendError(res, 416, "limit.retention_evicted", "resume point evicted past retention", { retainedFrom });
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
sseHeaders(res);
|
|
13
|
+
let closed = false;
|
|
14
|
+
req.on("close", () => {
|
|
15
|
+
closed = true;
|
|
16
|
+
});
|
|
17
|
+
const start = Date.now();
|
|
18
|
+
const MAX_MS = 15 * 60 * 1000;
|
|
19
|
+
let lastBeat = Date.now();
|
|
20
|
+
const write = (events) => {
|
|
21
|
+
for (const ev of events) {
|
|
22
|
+
const f = provider.formatEvent(ev);
|
|
23
|
+
res.write(`id: ${f.id}\nevent: ${f.event}\ndata: ${JSON.stringify(f.data)}\n\n`);
|
|
24
|
+
from = ev.seq;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
while (!closed) {
|
|
28
|
+
const [events, row] = await Promise.all([provider.getEvents(id, from), provider.statusOf(id)]);
|
|
29
|
+
write(events);
|
|
30
|
+
if (!row)
|
|
31
|
+
break;
|
|
32
|
+
if (row.status !== "running") {
|
|
33
|
+
write(await provider.getEvents(id, from));
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
const stale = Date.now() - new Date(row.updatedAt).getTime() > staleMs;
|
|
37
|
+
if (stale && events.length === 0) {
|
|
38
|
+
const sf = provider.staleFrame();
|
|
39
|
+
res.write(`event: ${sf.event}\ndata: ${JSON.stringify(sf.data)}\n\n`);
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
if (Date.now() - start > MAX_MS) {
|
|
43
|
+
res.write(`event: error\ndata: ${JSON.stringify({ type: "error", code: "STREAM_MAX_DURATION", message: "stream reached its 15-minute cap — reconnect with Last-Event-ID to continue (the run is still active)" })}\n\n`);
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
if (events.length === 0) {
|
|
47
|
+
if (Date.now() - lastBeat > 15_000) {
|
|
48
|
+
res.write(`event: heartbeat\ndata: {}\n\n`);
|
|
49
|
+
lastBeat = Date.now();
|
|
50
|
+
}
|
|
51
|
+
await sleep(250);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
res.end();
|
|
55
|
+
}
|
|
56
|
+
export function sleep(ms) {
|
|
57
|
+
return new Promise((r) => setTimeout(r, ms));
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=sse-log.js.map
|
|
@@ -81,4 +81,10 @@ export interface TaskRequestBody {
|
|
|
81
81
|
enableFork?: boolean;
|
|
82
82
|
[k: string]: unknown;
|
|
83
83
|
}
|
|
84
|
+
export type DecideBinding = {
|
|
85
|
+
checkpointToken?: string;
|
|
86
|
+
boundCallId?: string;
|
|
87
|
+
boundInputHash?: string;
|
|
88
|
+
updatedInput?: unknown;
|
|
89
|
+
};
|
|
84
90
|
//# sourceMappingURL=wire-types.d.ts.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export function workspaceContentType(relPath) {
|
|
2
|
+
const base = relPath.slice(relPath.lastIndexOf("/") + 1);
|
|
3
|
+
const dot = base.lastIndexOf(".");
|
|
4
|
+
const ext = dot > 0 ? base.slice(dot + 1).toLowerCase() : "";
|
|
5
|
+
const TABLE = {
|
|
6
|
+
txt: "text/plain; charset=utf-8", md: "text/plain; charset=utf-8", log: "text/plain; charset=utf-8",
|
|
7
|
+
json: "application/json; charset=utf-8", csv: "text/csv; charset=utf-8",
|
|
8
|
+
png: "image/png", jpg: "image/jpeg", jpeg: "image/jpeg", gif: "image/gif", webp: "image/webp",
|
|
9
|
+
pdf: "application/pdf",
|
|
10
|
+
};
|
|
11
|
+
return TABLE[ext] ?? "application/octet-stream";
|
|
12
|
+
}
|
|
13
|
+
export function looksBinary(bytes) {
|
|
14
|
+
const n = Math.min(bytes.byteLength, 8192);
|
|
15
|
+
for (let i = 0; i < n; i++)
|
|
16
|
+
if (bytes[i] === 0)
|
|
17
|
+
return true;
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=workspace-content.js.map
|
package/dist/leader/endpoint.js
CHANGED
|
@@ -27,12 +27,12 @@ export function createLeaderEndpoint(runLeader, opts = {}) {
|
|
|
27
27
|
if (method === "POST" && url === "/v1/leader") {
|
|
28
28
|
const b = body;
|
|
29
29
|
if (!b || typeof b.objective !== "string" || b.objective.trim() === "") {
|
|
30
|
-
return { status: 400, body: { error: "missing 'objective' string" } };
|
|
30
|
+
return { status: 400, body: { error: "missing 'objective' string", errorCode: "request.body_shape" } };
|
|
31
31
|
}
|
|
32
32
|
if (opts.requiredFields) {
|
|
33
33
|
const missing = opts.requiredFields.filter((f) => typeof b[f] !== "string" || b[f].trim() === "");
|
|
34
34
|
if (missing.length > 0) {
|
|
35
|
-
return { status: 400, body: { error: `missing required field(s): ${missing.join(", ")}
|
|
35
|
+
return { status: 400, body: { error: `missing required field(s): ${missing.join(", ")}`, errorCode: "request.body_shape" } };
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
reap();
|
|
@@ -61,9 +61,9 @@ export function createLeaderEndpoint(runLeader, opts = {}) {
|
|
|
61
61
|
if (m) {
|
|
62
62
|
const run = runs.get(m[1]);
|
|
63
63
|
if (!run)
|
|
64
|
-
return { status: 404, body: { error: "leader run not found" } };
|
|
64
|
+
return { status: 404, body: { error: "leader run not found", errorCode: "not_found.leader_run" } };
|
|
65
65
|
if (run.owner != null && run.owner !== (requester ?? null)) {
|
|
66
|
-
return { status: 404, body: { error: "leader run not found" } };
|
|
66
|
+
return { status: 404, body: { error: "leader run not found", errorCode: "not_found.leader_run" } };
|
|
67
67
|
}
|
|
68
68
|
return {
|
|
69
69
|
status: 200,
|
package/dist/main.js
CHANGED
|
@@ -24,7 +24,7 @@ import { stat as fsStat, readFile as fsReadFile } from "node:fs/promises";
|
|
|
24
24
|
import { acceptShellScratchpadDir, buildEnvFacts, egressForRemoteExec, ensureScratchpadDir, purgeScratchpadDir, sweepStaleScratchpads, resumeFactsForLane } from "./env-facts.js";
|
|
25
25
|
import { normalizeSuggestNextPrompts, normalizeResilience, normalizeAttachments, normalizeResumeAtMode, resolveTaskLimits, taskAgentsSpecFragment, retainBackgroundProcessesFromBody, toolNameListFromBody, promptProfileFromBody } from "./spec-fields.js";
|
|
26
26
|
import { createBrain, brainSummary } from "./brain.js";
|
|
27
|
-
import { loadConfig, logConfigDiagnostics, resolveBindHost } from "./config.js";
|
|
27
|
+
import { configLkgEnabled, loadConfig, logConfigDiagnostics, resolveBindHost } from "./config.js";
|
|
28
28
|
import { resourceSuspendOptIn } from "./resource-suspend.js";
|
|
29
29
|
import { createSessionStore, ensureChildSessionDurableWithPromotion } from "./plugins/session-store.js";
|
|
30
30
|
import { ForkRoutingSessionStore } from "./plugins/fork-routing-session-store.js";
|
|
@@ -247,7 +247,7 @@ async function main() {
|
|
|
247
247
|
}
|
|
248
248
|
};
|
|
249
249
|
let bootConfigPending;
|
|
250
|
-
const lkgEnabled = configProvider?.kind === "remote" &&
|
|
250
|
+
const lkgEnabled = configProvider?.kind === "remote" && configLkgEnabled();
|
|
251
251
|
const lkgPath = process.env.CONFIG_LKG_PATH ?? defaultLkgPath(cc?.worker);
|
|
252
252
|
const lkgSurvivesRestart = lkgEnabled && process.env.CONFIG_LKG_DURABLE === "true";
|
|
253
253
|
let lkgBooted = false;
|
|
@@ -1089,8 +1089,8 @@ async function main() {
|
|
|
1089
1089
|
executionEnvFactory: executionEnvFactory ? executionEnvFactory : undefined,
|
|
1090
1090
|
lspManager: lspManager ? lspManager : undefined,
|
|
1091
1091
|
onBackgroundChildEvent: fleetBackgroundChildPublisher(fleetBus, (msg, fields) => logger.info(msg, fields)),
|
|
1092
|
-
loadProjectMemory: config.requirePrincipal !== true &&
|
|
1093
|
-
probeInstructionSources: config.remoteExec?.provider === "host" && config.requirePrincipal !== true &&
|
|
1092
|
+
loadProjectMemory: config.requirePrincipal !== true && config.projectMemoryEnabled ? makeLoadProjectMemory({ logger }) : undefined,
|
|
1093
|
+
probeInstructionSources: config.remoteExec?.provider === "host" && config.requirePrincipal !== true && config.projectMemoryEnabled
|
|
1094
1094
|
? makeProbeInstructionSources()
|
|
1095
1095
|
: undefined,
|
|
1096
1096
|
hooks: deploymentHooks,
|
|
@@ -1266,7 +1266,7 @@ async function main() {
|
|
|
1266
1266
|
: undefined;
|
|
1267
1267
|
let skills = loadSkills(config.skillsDir);
|
|
1268
1268
|
if (effective?.skills && config.configCenter) {
|
|
1269
|
-
skills = await applyCenterSkills(skills, effective.skills, config.configCenter.baseUrl, config.configCenter.token, logger, undefined,
|
|
1269
|
+
skills = await applyCenterSkills(skills, effective.skills, config.configCenter.baseUrl, config.configCenter.token, logger, undefined, configLkgEnabled() ? defaultSkillCacheDir() : undefined);
|
|
1270
1270
|
}
|
|
1271
1271
|
if (effective?.plugins && config.configCenter) {
|
|
1272
1272
|
const pluginOut = await applyCenterPlugins(skills, effective, {
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Pool as MySqlPool } from "mysql2/promise";
|
|
2
|
+
import type { Pool as PgPool, PoolClient } from "pg";
|
|
3
|
+
import { type SqlDriver } from "./sql-driver.js";
|
|
4
|
+
export type ApprovalStatus = "pending" | "approved" | "denied" | "expired";
|
|
5
|
+
export interface ApprovalRow {
|
|
6
|
+
id: string;
|
|
7
|
+
taskId: string | null;
|
|
8
|
+
sessionId: string | null;
|
|
9
|
+
owner: string | null;
|
|
10
|
+
scope: string | null;
|
|
11
|
+
toolName: string;
|
|
12
|
+
args: unknown;
|
|
13
|
+
status: ApprovalStatus;
|
|
14
|
+
reason: string | null;
|
|
15
|
+
decidedBy: string | null;
|
|
16
|
+
createdAt: string;
|
|
17
|
+
decidedAt: string | null;
|
|
18
|
+
}
|
|
19
|
+
export declare const PG_APPROVAL_SCHEMA: string[];
|
|
20
|
+
export declare function ensureSchema(pool: PgPool | PoolClient): Promise<void>;
|
|
21
|
+
export declare class SqlApprovalStore {
|
|
22
|
+
protected readonly db: SqlDriver;
|
|
23
|
+
constructor(db: SqlDriver);
|
|
24
|
+
private q;
|
|
25
|
+
private json;
|
|
26
|
+
createPending(id: string, scope: string | null, ctx: {
|
|
27
|
+
taskId?: string | null;
|
|
28
|
+
sessionId?: string | null;
|
|
29
|
+
owner?: string | null;
|
|
30
|
+
toolName: string;
|
|
31
|
+
args: unknown;
|
|
32
|
+
}): Promise<void>;
|
|
33
|
+
getStatus(id: string, scope: string | null): Promise<{
|
|
34
|
+
status: ApprovalStatus;
|
|
35
|
+
reason: string | null;
|
|
36
|
+
decidedBy: string | null;
|
|
37
|
+
} | undefined>;
|
|
38
|
+
decide(id: string, scope: string | null, decision: "approved" | "denied" | "expired", reason: string | null, decidedBy: string | null): Promise<boolean>;
|
|
39
|
+
listPending(scope: string | null, owner?: string): Promise<ApprovalRow[]>;
|
|
40
|
+
get(id: string, scope: string | null): Promise<ApprovalRow | undefined>;
|
|
41
|
+
getById(id: string): Promise<ApprovalRow | undefined>;
|
|
42
|
+
listPendingAll(): Promise<ApprovalRow[]>;
|
|
43
|
+
expireStale(olderThanMs: number): Promise<number>;
|
|
44
|
+
}
|
|
45
|
+
export declare function mapRow(r: Record<string, unknown>): ApprovalRow;
|
|
46
|
+
export declare class TiDBApprovalStore extends SqlApprovalStore {
|
|
47
|
+
constructor(pool: MySqlPool);
|
|
48
|
+
}
|
|
49
|
+
export declare class PgApprovalStore extends SqlApprovalStore {
|
|
50
|
+
constructor(pool: PgPool);
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=approval-store-sql.d.ts.map
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { parseJsonStrict as parseJson, toIsoOrNull as iso } from "./sql-row-helpers.js";
|
|
2
|
+
import { pgProtocolJsonStringify } from "./pg-safe-json.js";
|
|
3
|
+
import { mysqlDriver, pgDriver } from "./sql-driver.js";
|
|
4
|
+
export const PG_APPROVAL_SCHEMA = [
|
|
5
|
+
`CREATE TABLE IF NOT EXISTS approval (
|
|
6
|
+
id VARCHAR(64) NOT NULL,
|
|
7
|
+
task_id VARCHAR(64),
|
|
8
|
+
session_id VARCHAR(64),
|
|
9
|
+
owner VARCHAR(190),
|
|
10
|
+
-- Single-DB fleet scope guard (tenant isolation). NULLABLE on purpose: a NULL-scope row is an untenanted
|
|
11
|
+
-- run, and every read/decide path matches it with \`scope IS NOT DISTINCT FROM $n\` (the PG twin of TiDB
|
|
12
|
+
-- \`<=>\`) so NULL matches only NULL and one tenant's scope NEVER matches another's.
|
|
13
|
+
scope VARCHAR(190),
|
|
14
|
+
tool_name VARCHAR(190) NOT NULL,
|
|
15
|
+
args JSONB,
|
|
16
|
+
status VARCHAR(16) NOT NULL,
|
|
17
|
+
reason TEXT,
|
|
18
|
+
decided_by VARCHAR(190),
|
|
19
|
+
created_at TIMESTAMPTZ(3) NOT NULL,
|
|
20
|
+
decided_at TIMESTAMPTZ(3),
|
|
21
|
+
PRIMARY KEY (id)
|
|
22
|
+
)`,
|
|
23
|
+
`CREATE INDEX IF NOT EXISTS idx_approval_owner_status ON approval (owner, status)`,
|
|
24
|
+
`CREATE INDEX IF NOT EXISTS idx_approval_status ON approval (status)`,
|
|
25
|
+
`CREATE INDEX IF NOT EXISTS idx_approval_scope_status ON approval (scope, status)`,
|
|
26
|
+
];
|
|
27
|
+
export async function ensureSchema(pool) {
|
|
28
|
+
for (const stmt of PG_APPROVAL_SCHEMA)
|
|
29
|
+
await pool.query(stmt);
|
|
30
|
+
}
|
|
31
|
+
export class SqlApprovalStore {
|
|
32
|
+
db;
|
|
33
|
+
constructor(db) {
|
|
34
|
+
this.db = db;
|
|
35
|
+
}
|
|
36
|
+
q(tidb, pg) {
|
|
37
|
+
return this.db.dialect === "tidb" ? tidb : pg;
|
|
38
|
+
}
|
|
39
|
+
json(value, label) {
|
|
40
|
+
return this.db.dialect === "tidb" ? JSON.stringify(value) : pgProtocolJsonStringify(value, label);
|
|
41
|
+
}
|
|
42
|
+
async createPending(id, scope, ctx) {
|
|
43
|
+
await this.db.query(this.q("INSERT INTO approval (id, task_id, session_id, owner, scope, tool_name, args, status, created_at) " +
|
|
44
|
+
"VALUES (?,?,?,?,?,?,?,'pending',?)", "INSERT INTO approval (id, task_id, session_id, owner, scope, tool_name, args, status, created_at) " +
|
|
45
|
+
"VALUES ($1,$2,$3,$4,$5,$6,$7::jsonb,'pending',$8)"), [
|
|
46
|
+
id,
|
|
47
|
+
ctx.taskId ?? null,
|
|
48
|
+
ctx.sessionId ?? null,
|
|
49
|
+
ctx.owner ?? null,
|
|
50
|
+
scope,
|
|
51
|
+
ctx.toolName,
|
|
52
|
+
ctx.args == null ? null : this.json(ctx.args, "approval args"),
|
|
53
|
+
new Date(),
|
|
54
|
+
]);
|
|
55
|
+
}
|
|
56
|
+
async getStatus(id, scope) {
|
|
57
|
+
const { rows } = await this.db.query(this.q("SELECT status, reason, decided_by FROM approval WHERE id = ? AND scope <=> ?", "SELECT status, reason, decided_by FROM approval WHERE id = $1 AND scope IS NOT DISTINCT FROM $2"), [id, scope]);
|
|
58
|
+
const r = rows[0];
|
|
59
|
+
if (!r)
|
|
60
|
+
return undefined;
|
|
61
|
+
return { status: r.status, reason: r.reason ?? null, decidedBy: r.decided_by ?? null };
|
|
62
|
+
}
|
|
63
|
+
async decide(id, scope, decision, reason, decidedBy) {
|
|
64
|
+
const res = await this.db.query(this.q("UPDATE approval SET status = ?, reason = ?, decided_by = ?, decided_at = ? WHERE id = ? AND scope <=> ? AND status = 'pending'", "UPDATE approval SET status = $1, reason = $2, decided_by = $3, decided_at = $4 WHERE id = $5 AND scope IS NOT DISTINCT FROM $6 AND status = 'pending'"), [decision, reason, decidedBy, new Date(), id, scope]);
|
|
65
|
+
return res.affected === 1;
|
|
66
|
+
}
|
|
67
|
+
async listPending(scope, owner) {
|
|
68
|
+
const { rows } = owner
|
|
69
|
+
? await this.db.query(this.q("SELECT * FROM approval WHERE status = 'pending' AND scope <=> ? AND owner = ? ORDER BY created_at ASC", "SELECT * FROM approval WHERE status = 'pending' AND scope IS NOT DISTINCT FROM $1 AND owner = $2 ORDER BY created_at ASC"), [scope, owner])
|
|
70
|
+
: await this.db.query(this.q("SELECT * FROM approval WHERE status = 'pending' AND scope <=> ? ORDER BY created_at ASC", "SELECT * FROM approval WHERE status = 'pending' AND scope IS NOT DISTINCT FROM $1 ORDER BY created_at ASC"), [scope]);
|
|
71
|
+
return rows.map(mapRow);
|
|
72
|
+
}
|
|
73
|
+
async get(id, scope) {
|
|
74
|
+
const { rows } = await this.db.query(this.q("SELECT * FROM approval WHERE id = ? AND scope <=> ?", "SELECT * FROM approval WHERE id = $1 AND scope IS NOT DISTINCT FROM $2"), [id, scope]);
|
|
75
|
+
return rows[0] ? mapRow(rows[0]) : undefined;
|
|
76
|
+
}
|
|
77
|
+
async getById(id) {
|
|
78
|
+
const { rows } = await this.db.query(this.q("SELECT * FROM approval WHERE id = ?", "SELECT * FROM approval WHERE id = $1"), [id]);
|
|
79
|
+
return rows[0] ? mapRow(rows[0]) : undefined;
|
|
80
|
+
}
|
|
81
|
+
async listPendingAll() {
|
|
82
|
+
const { rows } = await this.db.query("SELECT * FROM approval WHERE status = 'pending' ORDER BY created_at ASC");
|
|
83
|
+
return rows.map(mapRow);
|
|
84
|
+
}
|
|
85
|
+
async expireStale(olderThanMs) {
|
|
86
|
+
const cutoff = new Date(Date.now() - olderThanMs);
|
|
87
|
+
const res = await this.db.query(this.q("UPDATE approval SET status = 'expired', reason = 'timed out before decision', decided_at = ? " +
|
|
88
|
+
"WHERE status = 'pending' AND created_at < ?", "UPDATE approval SET status = 'expired', reason = 'timed out before decision', decided_at = $1 " +
|
|
89
|
+
"WHERE status = 'pending' AND created_at < $2"), [new Date(), cutoff]);
|
|
90
|
+
return res.affected;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
export function mapRow(r) {
|
|
94
|
+
return {
|
|
95
|
+
id: String(r.id),
|
|
96
|
+
taskId: r.task_id ?? null,
|
|
97
|
+
sessionId: r.session_id ?? null,
|
|
98
|
+
owner: r.owner ?? null,
|
|
99
|
+
scope: r.scope ?? null,
|
|
100
|
+
toolName: String(r.tool_name),
|
|
101
|
+
args: parseJson(r.args),
|
|
102
|
+
status: r.status,
|
|
103
|
+
reason: r.reason ?? null,
|
|
104
|
+
decidedBy: r.decided_by ?? null,
|
|
105
|
+
createdAt: iso(r.created_at),
|
|
106
|
+
decidedAt: iso(r.decided_at),
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
export class TiDBApprovalStore extends SqlApprovalStore {
|
|
110
|
+
constructor(pool) {
|
|
111
|
+
super(mysqlDriver(pool));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
export class PgApprovalStore extends SqlApprovalStore {
|
|
115
|
+
constructor(pool) {
|
|
116
|
+
super(pgDriver(pool));
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
//# sourceMappingURL=approval-store-sql.js.map
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Pool as MySqlPool } from "mysql2/promise";
|
|
2
|
+
import type { Pool as PgPool, PoolClient } from "pg";
|
|
3
|
+
import type { BreakerSnapshot, BreakerState } from "@sema-agent/core";
|
|
4
|
+
import { type SqlDriver } from "./sql-driver.js";
|
|
5
|
+
export declare const PG_BREAKER_STATE_SCHEMA: string[];
|
|
6
|
+
export declare function ensureSchema(pool: PgPool | PoolClient): Promise<void>;
|
|
7
|
+
export declare class SqlBreakerState implements BreakerState {
|
|
8
|
+
protected readonly db: SqlDriver;
|
|
9
|
+
private readonly onWriteFail?;
|
|
10
|
+
private cache;
|
|
11
|
+
private timer?;
|
|
12
|
+
private readonly pending;
|
|
13
|
+
private writeFailStreak;
|
|
14
|
+
constructor(db: SqlDriver, onWriteFail?: ((streak: number) => void) | undefined);
|
|
15
|
+
private q;
|
|
16
|
+
get(key: string): BreakerSnapshot | undefined;
|
|
17
|
+
set(key: string, snap: BreakerSnapshot): void;
|
|
18
|
+
private writeThrough;
|
|
19
|
+
refresh(): Promise<void>;
|
|
20
|
+
startRefresh(intervalMs?: number): this;
|
|
21
|
+
stop(): void;
|
|
22
|
+
}
|
|
23
|
+
export declare class TiDBBreakerState extends SqlBreakerState {
|
|
24
|
+
constructor(pool: MySqlPool, onWriteFail?: (streak: number) => void);
|
|
25
|
+
}
|
|
26
|
+
export declare class PgBreakerState extends SqlBreakerState {
|
|
27
|
+
constructor(pool: PgPool, onWriteFail?: (streak: number) => void);
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=breaker-state-sql.d.ts.map
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { mysqlDriver, pgDriver } from "./sql-driver.js";
|
|
2
|
+
export const PG_BREAKER_STATE_SCHEMA = [
|
|
3
|
+
`CREATE TABLE IF NOT EXISTS circuit_breaker (
|
|
4
|
+
breaker_key VARCHAR(190) NOT NULL,
|
|
5
|
+
phase VARCHAR(16) NOT NULL,
|
|
6
|
+
failures INTEGER NOT NULL DEFAULT 0,
|
|
7
|
+
opened_at TIMESTAMPTZ(3),
|
|
8
|
+
updated_at TIMESTAMPTZ(3) NOT NULL,
|
|
9
|
+
PRIMARY KEY (breaker_key)
|
|
10
|
+
)`,
|
|
11
|
+
];
|
|
12
|
+
export async function ensureSchema(pool) {
|
|
13
|
+
for (const stmt of PG_BREAKER_STATE_SCHEMA)
|
|
14
|
+
await pool.query(stmt);
|
|
15
|
+
}
|
|
16
|
+
export class SqlBreakerState {
|
|
17
|
+
db;
|
|
18
|
+
onWriteFail;
|
|
19
|
+
cache = new Map();
|
|
20
|
+
timer;
|
|
21
|
+
pending = new Set();
|
|
22
|
+
writeFailStreak = 0;
|
|
23
|
+
constructor(db, onWriteFail) {
|
|
24
|
+
this.db = db;
|
|
25
|
+
this.onWriteFail = onWriteFail;
|
|
26
|
+
}
|
|
27
|
+
q(tidb, pg) {
|
|
28
|
+
return this.db.dialect === "tidb" ? tidb : pg;
|
|
29
|
+
}
|
|
30
|
+
get(key) {
|
|
31
|
+
return this.cache.get(key);
|
|
32
|
+
}
|
|
33
|
+
set(key, snap) {
|
|
34
|
+
this.cache.set(key, snap);
|
|
35
|
+
this.pending.add(key);
|
|
36
|
+
void this.writeThrough(key, snap);
|
|
37
|
+
}
|
|
38
|
+
async writeThrough(key, snap) {
|
|
39
|
+
try {
|
|
40
|
+
await this.db.query(this.q("INSERT INTO circuit_breaker (breaker_key, phase, failures, opened_at, updated_at) VALUES (?,?,?,?,?) " +
|
|
41
|
+
"ON DUPLICATE KEY UPDATE phase=VALUES(phase), failures=VALUES(failures), opened_at=VALUES(opened_at), updated_at=VALUES(updated_at)", "INSERT INTO circuit_breaker (breaker_key, phase, failures, opened_at, updated_at) VALUES ($1,$2,$3,$4,$5) " +
|
|
42
|
+
"ON CONFLICT (breaker_key) DO UPDATE SET phase=EXCLUDED.phase, failures=EXCLUDED.failures, opened_at=EXCLUDED.opened_at, updated_at=EXCLUDED.updated_at"), [key, snap.phase, snap.failures, snap.openedAt != null ? new Date(snap.openedAt) : null, new Date()]);
|
|
43
|
+
this.pending.delete(key);
|
|
44
|
+
if (this.writeFailStreak > 0) {
|
|
45
|
+
this.writeFailStreak = 0;
|
|
46
|
+
try {
|
|
47
|
+
this.onWriteFail?.(0);
|
|
48
|
+
}
|
|
49
|
+
catch { }
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
try {
|
|
54
|
+
this.onWriteFail?.(++this.writeFailStreak);
|
|
55
|
+
}
|
|
56
|
+
catch { }
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async refresh() {
|
|
60
|
+
try {
|
|
61
|
+
const pendingAtStart = new Set(this.pending);
|
|
62
|
+
const guarded = (key) => this.pending.has(key) || pendingAtStart.has(key);
|
|
63
|
+
const { rows } = await this.db.query("SELECT breaker_key, phase, failures, opened_at FROM circuit_breaker");
|
|
64
|
+
const seen = new Set();
|
|
65
|
+
for (const r of rows) {
|
|
66
|
+
const key = String(r.breaker_key);
|
|
67
|
+
seen.add(key);
|
|
68
|
+
if (guarded(key))
|
|
69
|
+
continue;
|
|
70
|
+
this.cache.set(key, {
|
|
71
|
+
phase: r.phase,
|
|
72
|
+
failures: Number(r.failures),
|
|
73
|
+
openedAt: r.opened_at != null ? new Date(r.opened_at).getTime() : undefined,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
for (const key of this.cache.keys())
|
|
77
|
+
if (!seen.has(key) && !guarded(key))
|
|
78
|
+
this.cache.delete(key);
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
startRefresh(intervalMs = 5_000) {
|
|
84
|
+
this.timer ??= setInterval(() => void this.refresh(), intervalMs);
|
|
85
|
+
this.timer.unref?.();
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
stop() {
|
|
89
|
+
if (this.timer)
|
|
90
|
+
clearInterval(this.timer);
|
|
91
|
+
this.timer = undefined;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
export class TiDBBreakerState extends SqlBreakerState {
|
|
95
|
+
constructor(pool, onWriteFail) {
|
|
96
|
+
super(mysqlDriver(pool), onWriteFail);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
export class PgBreakerState extends SqlBreakerState {
|
|
100
|
+
constructor(pool, onWriteFail) {
|
|
101
|
+
super(pgDriver(pool), onWriteFail);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=breaker-state-sql.js.map
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type FileSnapshotBounds, type FileSnapshotStore, type FileSnapshotResult, type ExecutionEnv } from "@sema-agent/core";
|
|
2
|
+
import type { Pool as MySqlPool } from "mysql2/promise";
|
|
3
|
+
import type { Pool as PgPool } from "pg";
|
|
4
|
+
import { type BlobBackend } from "./blob-backend.js";
|
|
5
|
+
import { type SqlDriver } from "./sql-driver.js";
|
|
6
|
+
export declare class SqlFileSnapshotStore implements FileSnapshotStore {
|
|
7
|
+
protected readonly db: SqlDriver;
|
|
8
|
+
protected readonly pool: MySqlPool | PgPool;
|
|
9
|
+
protected readonly bounds: FileSnapshotBounds;
|
|
10
|
+
private readonly inFlight;
|
|
11
|
+
private readonly blobs;
|
|
12
|
+
onSkippedVanished: ((count: number) => void | Promise<void>) | undefined;
|
|
13
|
+
constructor(db: SqlDriver, pool: MySqlPool | PgPool, blobBackend?: BlobBackend, bounds?: FileSnapshotBounds);
|
|
14
|
+
private q;
|
|
15
|
+
private putBlobRow;
|
|
16
|
+
putBlob(hash: string, bytes: Uint8Array): Promise<FileSnapshotResult>;
|
|
17
|
+
snapshot(scope: string, key: string, env: ExecutionEnv, root: string, signal?: AbortSignal): Promise<FileSnapshotResult>;
|
|
18
|
+
importManifest(scope: string, key: string, manifest: Map<string, string>, srcGetBlob: (hash: string) => Promise<Uint8Array | undefined>): Promise<FileSnapshotResult>;
|
|
19
|
+
restore(scope: string, key: string, env: ExecutionEnv, root: string, signal?: AbortSignal): Promise<FileSnapshotResult>;
|
|
20
|
+
has(scope: string, key: string): Promise<boolean>;
|
|
21
|
+
listKeys(scope: string): Promise<string[]>;
|
|
22
|
+
exportManifest(scope: string, key: string): Promise<Map<string, string> | null>;
|
|
23
|
+
getBlob(hash: string): Promise<Uint8Array | undefined>;
|
|
24
|
+
hasBlobs(hashes: string[]): Promise<Set<string>>;
|
|
25
|
+
blobSizes(hashes: string[]): Promise<Map<string, number>>;
|
|
26
|
+
reap(scope: string, keepKeys: string[]): Promise<number>;
|
|
27
|
+
deleteBySession(scope: string): Promise<number>;
|
|
28
|
+
private gcOrphanBlobs;
|
|
29
|
+
sweepOrphanBlobs(): Promise<number>;
|
|
30
|
+
private tx;
|
|
31
|
+
}
|
|
32
|
+
export declare class TiDBFileSnapshotStore extends SqlFileSnapshotStore {
|
|
33
|
+
constructor(pool: MySqlPool, blobBackend?: BlobBackend, bounds?: FileSnapshotBounds);
|
|
34
|
+
}
|
|
35
|
+
export declare class PgFileSnapshotStore extends SqlFileSnapshotStore {
|
|
36
|
+
constructor(pool: PgPool, blobBackend?: BlobBackend, bounds?: FileSnapshotBounds);
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=file-snapshot-store-sql.d.ts.map
|