@capybara-hq/kernel 0.1.0-rc.23 → 0.1.0-rc.24
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/bootstrap.json +25 -25
- package/dist/admin/admin.module.d.ts +2 -1
- package/dist/admin/admin.module.js +7 -3
- package/dist/admin/outbox-dead.controller.d.ts +23 -0
- package/dist/admin/outbox-dead.controller.js +62 -0
- package/dist/admin/outbox-dead.schemas.d.ts +8 -0
- package/dist/admin/outbox-dead.schemas.js +18 -0
- package/dist/admin/outbox-dead.service.d.ts +66 -0
- package/dist/admin/outbox-dead.service.js +179 -0
- package/dist/admin/overview.service.d.ts +40 -5
- package/dist/admin/overview.service.js +41 -7
- package/dist/agents/from-template.js +1 -1
- package/dist/background/finish.d.ts +5 -2
- package/dist/background/finish.js +14 -3
- package/dist/background/launch.d.ts +2 -1
- package/dist/background/launch.js +5 -2
- package/dist/background/store.d.ts +8 -0
- package/dist/background/store.js +24 -0
- package/dist/catalog/admin-catalog.controller.d.ts +21 -2
- package/dist/catalog/admin-catalog.controller.js +45 -6
- package/dist/catalog/admin-catalog.schemas.d.ts +5 -0
- package/dist/catalog/admin-catalog.schemas.js +8 -0
- package/dist/catalog/catalog.module.d.ts +2 -1
- package/dist/catalog/catalog.module.js +5 -1
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/catalog/platform-import-update.d.ts +50 -0
- package/dist/catalog/platform-import-update.js +116 -0
- package/dist/catalog/plugin-import.d.ts +36 -1
- package/dist/catalog/plugin-import.js +23 -1
- package/dist/catalog/plugin-upgrade.d.ts +18 -0
- package/dist/catalog/plugin-upgrade.js +19 -0
- package/dist/channels/channels.module.d.ts +5 -2
- package/dist/channels/channels.module.js +9 -2
- package/dist/channels/deliver-job.d.ts +25 -0
- package/dist/channels/deliver-job.js +25 -0
- package/dist/channels/inbound.d.ts +1 -1
- package/dist/channels/inbound.js +2 -15
- package/dist/channels/jobs.d.ts +28 -18
- package/dist/channels/jobs.js +63 -26
- package/dist/config/schema.d.ts +0 -3
- package/dist/config/schema.js +11 -20
- package/dist/conversations/agent-tree.d.ts +16 -0
- package/dist/conversations/agent-tree.js +36 -0
- package/dist/conversations/conversations.service.d.ts +44 -14
- package/dist/conversations/conversations.service.js +52 -38
- package/dist/conversations/event-log.d.ts +7 -10
- package/dist/conversations/event-log.js +24 -15
- package/dist/conversations/events.js +2 -1
- package/dist/conversations/queue.service.d.ts +8 -3
- package/dist/conversations/queue.service.js +37 -13
- package/dist/conversations/titles.d.ts +21 -8
- package/dist/conversations/titles.js +42 -15
- package/dist/db/kernel-schema.d.ts +26 -8
- package/dist/devices/devices.service.d.ts +12 -3
- package/dist/devices/devices.service.js +18 -3
- package/dist/errors.d.ts +11 -0
- package/dist/errors.js +11 -0
- package/dist/evals/run-replay.js +14 -8
- package/dist/extensions/extensions.module.js +3 -0
- package/dist/extensions/lifecycle.d.ts +6 -1
- package/dist/extensions/lifecycle.js +9 -2
- package/dist/extensions/usage-subscribers.d.ts +43 -0
- package/dist/extensions/usage-subscribers.js +108 -0
- package/dist/http/api.module.js +2 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/infra/cli/kernel-cli.module.js +5 -0
- package/dist/infra/events/kernel-events.d.ts +9 -35
- package/dist/infra/outbox/outbox-relay.module.d.ts +6 -0
- package/dist/infra/outbox/outbox-relay.module.js +18 -0
- package/dist/infra/outbox/outbox.dispatcher.d.ts +103 -0
- package/dist/infra/outbox/outbox.dispatcher.js +310 -0
- package/dist/infra/outbox/outbox.module.d.ts +24 -0
- package/dist/infra/outbox/outbox.module.js +50 -0
- package/dist/infra/outbox/outbox.relay.d.ts +31 -0
- package/dist/infra/outbox/outbox.relay.js +76 -0
- package/dist/infra/outbox/outbox.service.d.ts +51 -0
- package/dist/infra/outbox/outbox.service.js +131 -0
- package/dist/infra/outbox/registry.d.ts +120 -0
- package/dist/infra/outbox/registry.js +108 -0
- package/dist/infra/queue/queue-names.d.ts +8 -0
- package/dist/infra/queue/queue-names.js +16 -0
- package/dist/kernel.module.js +3 -0
- package/dist/local-devices/device-policy.service.d.ts +4 -0
- package/dist/local-devices/device-policy.service.js +61 -3
- package/dist/local-devices/machines.d.ts +13 -5
- package/dist/local-devices/machines.js +45 -7
- package/dist/local-devices/policy-summary.d.ts +44 -1
- package/dist/local-devices/policy-summary.js +69 -0
- package/dist/models/embeddings.d.ts +9 -9
- package/dist/models/embeddings.js +11 -18
- package/dist/models/models.module.js +3 -0
- package/dist/orgs/orgs.service.js +7 -0
- package/dist/overrides.d.ts +20 -3
- package/dist/overrides.js +10 -2
- package/dist/platform/exclusions.js +5 -0
- package/dist/platform/ops/admin.ops.js +18 -2
- package/dist/platform/ops/catalog-admin.ops.d.ts +3 -1
- package/dist/platform/ops/catalog-admin.ops.js +32 -4
- package/dist/platform/ops/outbox.ops.d.ts +21 -0
- package/dist/platform/ops/outbox.ops.js +125 -0
- package/dist/platform/ops/schedules.ops.js +40 -1
- package/dist/platform/platform-ops.module.d.ts +2 -1
- package/dist/platform/platform-ops.module.js +3 -0
- package/dist/platform/route-schemas.js +3 -0
- package/dist/roles/worker.module.js +9 -4
- package/dist/runtime/langgraph-loop.js +13 -5
- package/dist/runtime/limits.d.ts +15 -0
- package/dist/runtime/limits.js +25 -0
- package/dist/runtime/loop.d.ts +8 -3
- package/dist/runtime/loop.js +2 -1
- package/dist/runtime/maintenance.d.ts +4 -2
- package/dist/runtime/maintenance.js +17 -2
- package/dist/runtime/model-call.d.ts +14 -16
- package/dist/runtime/model-call.js +14 -24
- package/dist/runtime/queue-lost-jobs.d.ts +24 -0
- package/dist/runtime/queue-lost-jobs.js +51 -0
- package/dist/runtime/run-control.d.ts +42 -5
- package/dist/runtime/run-control.js +66 -12
- package/dist/runtime/run-finish.d.ts +39 -12
- package/dist/runtime/run-finish.js +97 -14
- package/dist/runtime/run-jobs.d.ts +95 -13
- package/dist/runtime/run-jobs.js +199 -28
- package/dist/runtime/run-orphan-check.job.d.ts +142 -0
- package/dist/runtime/run-orphan-check.job.js +339 -0
- package/dist/runtime/run-processors.d.ts +16 -9
- package/dist/runtime/run-processors.js +1 -1
- package/dist/runtime/run-queue.d.ts +64 -55
- package/dist/runtime/run-queue.js +147 -284
- package/dist/runtime/run-steps.d.ts +1 -2
- package/dist/runtime/run-steps.js +8 -11
- package/dist/runtime/run-worker.d.ts +6 -17
- package/dist/runtime/run-worker.js +32 -34
- package/dist/runtime/runtime.module.d.ts +2 -3
- package/dist/runtime/runtime.module.js +5 -6
- package/dist/runtime/step-log.d.ts +16 -4
- package/dist/runtime/step-log.js +26 -6
- package/dist/runtime/stream.controller.d.ts +2 -4
- package/dist/runtime/stream.controller.js +5 -11
- package/dist/runtime/subagents/child.js +1 -1
- package/dist/runtime/subagents/definition.d.ts +2 -1
- package/dist/runtime/subagents/definition.js +4 -2
- package/dist/runtime/subagents/deliveries.d.ts +6 -3
- package/dist/runtime/subagents/deliveries.js +16 -7
- package/dist/runtime/subagents/dispatch.d.ts +12 -12
- package/dist/runtime/subagents/dispatch.js +13 -38
- package/dist/runtime/subagents/messages.d.ts +5 -1
- package/dist/runtime/subagents/messages.js +11 -3
- package/dist/runtime/subagents/skill-fork.d.ts +3 -3
- package/dist/runtime/subagents/skill-fork.js +5 -5
- package/dist/runtime/subagents/subagents.service.js +1 -1
- package/dist/runtime/subagents/tree-jobs.d.ts +61 -0
- package/dist/runtime/subagents/tree-jobs.js +86 -0
- package/dist/runtime/usage-report-jobs.d.ts +23 -48
- package/dist/runtime/usage-report-jobs.js +67 -119
- package/dist/runtime/usage-report.d.ts +28 -77
- package/dist/runtime/usage-report.js +35 -213
- package/dist/runtime/waits.d.ts +48 -198
- package/dist/runtime/waits.js +103 -805
- package/dist/sandbox/sandbox.service.d.ts +17 -4
- package/dist/sandbox/sandbox.service.js +55 -14
- package/dist/schedules/calendar.d.ts +134 -0
- package/dist/schedules/calendar.js +410 -0
- package/dist/schedules/fire.d.ts +1 -3
- package/dist/schedules/fire.js +5 -10
- package/dist/schedules/run-tracker.d.ts +0 -5
- package/dist/schedules/run-tracker.js +0 -13
- package/dist/schedules/scan.job.js +15 -20
- package/dist/schedules/schedule-math.d.ts +42 -0
- package/dist/schedules/schedule-math.js +52 -0
- package/dist/schedules/scheduled-tasks.service.d.ts +16 -0
- package/dist/schedules/scheduled-tasks.service.js +53 -0
- package/dist/schedules/schedules.controller.d.ts +5 -1
- package/dist/schedules/schedules.controller.js +15 -1
- package/dist/schedules/schedules.schemas.d.ts +35 -0
- package/dist/schedules/schedules.schemas.js +27 -0
- package/dist/schedules/store.d.ts +5 -0
- package/dist/schedules/store.js +20 -0
- package/dist/tools/approvals.service.d.ts +26 -7
- package/dist/tools/approvals.service.js +88 -10
- package/dist/tools/builtin-names.d.ts +5 -3
- package/dist/tools/builtin-names.js +12 -4
- package/dist/tools/builtins/builtin-tools.d.ts +1 -2
- package/dist/tools/builtins/builtin-tools.js +21 -17
- package/dist/tools/builtins/sandbox/bash.js +9 -3
- package/dist/tools/builtins/sandbox/common.d.ts +18 -1
- package/dist/tools/builtins/sandbox/common.js +14 -1
- package/dist/tools/builtins/sandbox/index.d.ts +2 -1
- package/dist/tools/builtins/sandbox/index.js +4 -1
- package/dist/tools/builtins/sandbox/task-list.d.ts +12 -0
- package/dist/tools/builtins/sandbox/task-list.js +130 -0
- package/dist/tools/builtins/sandbox/task-output.d.ts +8 -1
- package/dist/tools/builtins/sandbox/task-output.js +21 -3
- package/dist/tools/builtins/web/extract.d.ts +2 -4
- package/dist/tools/builtins/web/extract.js +2 -5
- package/dist/tools/pipeline.d.ts +1 -1
- package/dist/tools/pipeline.js +1 -1
- package/dist/tools/questions.service.d.ts +15 -6
- package/dist/tools/questions.service.js +20 -8
- package/dist/tools/registry.d.ts +4 -2
- package/dist/tools/registry.js +4 -3
- package/dist/tools/tools.module.js +4 -3
- package/package.json +3 -3
- package/src/migrations/0118_device_policy_ops.sql +6 -0
- package/src/migrations/0119_outbox.sql +22 -0
- package/src/migrations/0120_drop_usage_report_sweep.sql +7 -0
package/dist/channels/jobs.js
CHANGED
|
@@ -11,14 +11,14 @@ import { createHash, randomUUID } from "node:crypto";
|
|
|
11
11
|
import { getQueueToken } from "@nestjs/bullmq";
|
|
12
12
|
import { Inject, Injectable } from "@nestjs/common";
|
|
13
13
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
14
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
15
|
+
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
14
16
|
import { KernelEvents } from "../infra/events/kernel-events.js";
|
|
17
|
+
import { OutboxService } from "../infra/outbox/outbox.service.js";
|
|
18
|
+
import { OUTBOX_JOBS } from "../infra/outbox/registry.js";
|
|
15
19
|
import { KERNEL_LOGGER } from "../infra/tokens.js";
|
|
16
|
-
|
|
17
|
-
export
|
|
18
|
-
/** 投递任务名。 */
|
|
19
|
-
export const DELIVER_JOB = "deliver";
|
|
20
|
-
/** 投递任务 id(不含 `:`):同一会话、同一日志位置只排一个任务。 */
|
|
21
|
-
export const deliverJobId = (conversationId, seq) => `deliver-${conversationId}-${seq}`;
|
|
20
|
+
import { CHANNEL_QUEUE_NAME, DELIVER_JOB, deliverJobId, oweDelivery, } from "./deliver-job.js";
|
|
21
|
+
export { CHANNEL_QUEUE_NAME, DELIVER_JOB, deliverJobId, oweDelivery, } from "./deliver-job.js";
|
|
22
22
|
/**
|
|
23
23
|
* 结束后复核排的任务 id:每次唯一(带随机后缀,不含 `:`)。不能复用 `deliver-<会话>-<seq>`——同 id 的任务
|
|
24
24
|
* 可能正因锁被占而即将直接结束,BullMQ 会把新任务去重进它,于是没人投递。
|
|
@@ -32,7 +32,7 @@ export function idempotencyKey(raw) {
|
|
|
32
32
|
return createHash("sha256").update(raw).digest("base64url").slice(0, 32);
|
|
33
33
|
}
|
|
34
34
|
/** 可重试失败(限流、5xx、网络、超时)的总尝试次数。 */
|
|
35
|
-
export const DELIVER_ATTEMPTS =
|
|
35
|
+
export const DELIVER_ATTEMPTS = OUTBOX_JOBS.deliver.opts.attempts;
|
|
36
36
|
/** 入站任务名。 */
|
|
37
37
|
export const INBOUND_JOB = "inbound";
|
|
38
38
|
/**
|
|
@@ -40,7 +40,7 @@ export const INBOUND_JOB = "inbound";
|
|
|
40
40
|
* 用户发来的消息不能排在它们后面。
|
|
41
41
|
*/
|
|
42
42
|
export const INBOUND_PRIORITY = 1;
|
|
43
|
-
export const DELIVER_PRIORITY =
|
|
43
|
+
export const DELIVER_PRIORITY = OUTBOX_JOBS.deliver.opts.priority;
|
|
44
44
|
/** 入站任务的总尝试次数(处理失败由 BullMQ 指数退避重试:1s、2s、4s、8s)。 */
|
|
45
45
|
export const INBOUND_ATTEMPTS = 5;
|
|
46
46
|
const INBOUND_BACKOFF_MS = 1000;
|
|
@@ -53,30 +53,31 @@ export const inboundJobId = (connectionId, messageId) => `inbound-${connectionId
|
|
|
53
53
|
* The producer side of the channel queue `cap-kernel-channels` (any role): delivery jobs for a
|
|
54
54
|
* conversation with an IM target, inbound jobs (Ruling A21), and the catch-up of a connection that
|
|
55
55
|
* can deliver again. Its consumer is {@link ChannelOutbound} through the `@Processor` of the worker
|
|
56
|
-
* role.
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
56
|
+
* role. A delivery is owed (transactional outbox spec §8.1): every event append of a conversation
|
|
57
|
+
* with a channel target adds its deliver job to the outbox in the append's transaction (a reply, an
|
|
58
|
+
* approval decision, a question answer, a wait stop all append), and {@link bind} owes the first
|
|
59
|
+
* one with the target. The direct {@link enqueue} is left for hints and catch-ups: a run starting
|
|
60
|
+
* here (`run.started`, so its streaming drafts start at once), a finished delivery job's re-check,
|
|
61
|
+
* a card click's thread and a connection that can deliver again.
|
|
60
62
|
*/
|
|
61
63
|
let ChannelJobs = class ChannelJobs {
|
|
62
64
|
db;
|
|
63
65
|
queue;
|
|
64
66
|
log;
|
|
67
|
+
outbox;
|
|
68
|
+
txHost;
|
|
65
69
|
/** Stopped (the stop's channel-outbound phase): enqueues do nothing, inbound enqueues throw. */
|
|
66
70
|
closed = false;
|
|
67
71
|
unsubscribe;
|
|
68
|
-
constructor(db, queue, events, log) {
|
|
72
|
+
constructor(db, queue, events, log, outbox, txHost) {
|
|
69
73
|
this.db = db;
|
|
70
74
|
this.queue = queue;
|
|
71
75
|
this.log = log;
|
|
76
|
+
this.outbox = outbox;
|
|
77
|
+
this.txHost = txHost;
|
|
72
78
|
queue.on("error", (err) => this.log.error("channel outbound queue error", { err }));
|
|
73
|
-
const soon = ({ conversationId }) => this.enqueueSoon(conversationId);
|
|
74
79
|
this.unsubscribe = [
|
|
75
|
-
events.on("
|
|
76
|
-
events.on("run.started", soon),
|
|
77
|
-
events.on("run.wait-stopped", soon),
|
|
78
|
-
events.on("approval.decided", soon),
|
|
79
|
-
events.on("question.answered", soon),
|
|
80
|
+
events.on("run.started", ({ conversationId }) => this.enqueueSoon(conversationId)),
|
|
80
81
|
];
|
|
81
82
|
}
|
|
82
83
|
/** Unsubscribes after the stop phases: events emitted while they drain are still handled. */
|
|
@@ -84,11 +85,46 @@ let ChannelJobs = class ChannelJobs {
|
|
|
84
85
|
for (const off of this.unsubscribe)
|
|
85
86
|
off();
|
|
86
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* Makes `target` (on `connectionId`) the conversation's delivery target and owes its delivery
|
|
90
|
+
* from the current log position, in one transaction. A new target starts delivering after
|
|
91
|
+
* `seq` (the message that bound it); a known one keeps its progress. The conversation row is
|
|
92
|
+
* locked first, as an append locks it: an append either committed before (its events are below
|
|
93
|
+
* the owed position) or reads the target (and owes its own job).
|
|
94
|
+
*/
|
|
95
|
+
async bind(orgId, conversationId, connectionId, target, seq) {
|
|
96
|
+
const conv = await this.db
|
|
97
|
+
.selectFrom("conversations")
|
|
98
|
+
.select("last_seq")
|
|
99
|
+
.where("id", "=", conversationId)
|
|
100
|
+
.forUpdate()
|
|
101
|
+
.executeTakeFirst();
|
|
102
|
+
if (!conv)
|
|
103
|
+
return;
|
|
104
|
+
await this.db
|
|
105
|
+
.insertInto("channel_deliveries")
|
|
106
|
+
.values({
|
|
107
|
+
conversation_id: conversationId,
|
|
108
|
+
connection_id: connectionId,
|
|
109
|
+
target: JSON.stringify(target),
|
|
110
|
+
last_seq: seq,
|
|
111
|
+
})
|
|
112
|
+
.onConflict((oc) => oc.column("conversation_id").doUpdateSet({
|
|
113
|
+
connection_id: connectionId,
|
|
114
|
+
target: JSON.stringify(target),
|
|
115
|
+
updated_at: new Date(),
|
|
116
|
+
}))
|
|
117
|
+
.execute();
|
|
118
|
+
await oweDelivery(this.outbox, { conversationId, orgId, seq: Number(conv.last_seq) });
|
|
119
|
+
}
|
|
87
120
|
/** 会话有 IM 投递目标时排一个出站任务,不等入队完成;失败只记日志。 */
|
|
88
121
|
enqueueSoon(conversationId) {
|
|
89
122
|
this.enqueue(conversationId).catch((err) => this.log.warn("cannot enqueue channel delivery", { conversationId, err }));
|
|
90
123
|
}
|
|
91
|
-
/**
|
|
124
|
+
/**
|
|
125
|
+
* A hint (not owed work, see the class): a delivery job for a conversation with an IM target (one
|
|
126
|
+
* per log position; `followUp` gets a fresh id); does nothing once closed.
|
|
127
|
+
*/
|
|
92
128
|
async enqueue(conversationId, opts = {}) {
|
|
93
129
|
if (this.closed)
|
|
94
130
|
return;
|
|
@@ -101,14 +137,10 @@ let ChannelJobs = class ChannelJobs {
|
|
|
101
137
|
if (!row || this.closed)
|
|
102
138
|
return;
|
|
103
139
|
await this.queue.add(DELIVER_JOB, { conversationId }, {
|
|
140
|
+
...OUTBOX_JOBS.deliver.opts,
|
|
104
141
|
jobId: opts.followUp
|
|
105
142
|
? followUpJobId(conversationId, Number(row.last_seq))
|
|
106
143
|
: deliverJobId(conversationId, Number(row.last_seq)),
|
|
107
|
-
attempts: DELIVER_ATTEMPTS,
|
|
108
|
-
backoff: { type: "deliver" },
|
|
109
|
-
priority: DELIVER_PRIORITY,
|
|
110
|
-
removeOnComplete: true,
|
|
111
|
-
removeOnFail: true,
|
|
112
144
|
});
|
|
113
145
|
}
|
|
114
146
|
/**
|
|
@@ -147,11 +179,16 @@ let ChannelJobs = class ChannelJobs {
|
|
|
147
179
|
this.closed = true;
|
|
148
180
|
}
|
|
149
181
|
};
|
|
182
|
+
__decorate([
|
|
183
|
+
Transactional()
|
|
184
|
+
], ChannelJobs.prototype, "bind", null);
|
|
150
185
|
ChannelJobs = __decorate([
|
|
151
186
|
Injectable(),
|
|
152
187
|
__param(0, Inject(KernelDb)),
|
|
153
188
|
__param(1, Inject(getQueueToken(CHANNEL_QUEUE_NAME))),
|
|
154
189
|
__param(2, Inject(KernelEvents)),
|
|
155
|
-
__param(3, Inject(KERNEL_LOGGER))
|
|
190
|
+
__param(3, Inject(KERNEL_LOGGER)),
|
|
191
|
+
__param(4, Inject(OutboxService)),
|
|
192
|
+
__param(5, Inject(TransactionHost))
|
|
156
193
|
], ChannelJobs);
|
|
157
194
|
export { ChannelJobs };
|
package/dist/config/schema.d.ts
CHANGED
|
@@ -11,8 +11,6 @@ export interface FieldMeta {
|
|
|
11
11
|
* 用法:`z.string().meta(meta({ secret: true, description: "..." }))`。
|
|
12
12
|
*/
|
|
13
13
|
export declare function meta(opts: FieldMeta): FieldMeta;
|
|
14
|
-
/** `runtime.maxRunSeconds` 的默认值。 */
|
|
15
|
-
export declare const DEFAULT_MAX_RUN_SECONDS = 900;
|
|
16
14
|
/** `sandbox.limits.maxCommandSeconds` 的默认值。 */
|
|
17
15
|
export declare const DEFAULT_MAX_COMMAND_SECONDS = 7200;
|
|
18
16
|
/** access token 寿命上限(秒);会话吊销标记的 TTL 按它计算,与实例当前配置无关。 */
|
|
@@ -268,7 +266,6 @@ export declare const KernelConfigSchema: z.ZodObject<{
|
|
|
268
266
|
hooks: z.ZodPrefault<z.ZodObject<{
|
|
269
267
|
timeoutMs: z.ZodDefault<z.ZodNumber>;
|
|
270
268
|
usageReportConcurrency: z.ZodDefault<z.ZodNumber>;
|
|
271
|
-
usageReportKeepHours: z.ZodDefault<z.ZodNumber>;
|
|
272
269
|
}, z.core.$strip>>;
|
|
273
270
|
log: z.ZodPrefault<z.ZodObject<{
|
|
274
271
|
level: z.ZodDefault<z.ZodEnum<{
|
package/dist/config/schema.js
CHANGED
|
@@ -22,8 +22,6 @@ function isValidMasterKey(value) {
|
|
|
22
22
|
* 插件名同清单规则(不能是 `local`),租户 id 非空、不含空白;与 identity/external.ts 的身份源键规则一致。
|
|
23
23
|
*/
|
|
24
24
|
const TRUSTED_SOURCE_RE = /^(?!local:)[a-z0-9][a-z0-9._-]{0,213}:\S{1,256}$/;
|
|
25
|
-
/** `runtime.maxRunSeconds` 的默认值。 */
|
|
26
|
-
export const DEFAULT_MAX_RUN_SECONDS = 900;
|
|
27
25
|
/** `sandbox.limits.maxCommandSeconds` 的默认值。 */
|
|
28
26
|
export const DEFAULT_MAX_COMMAND_SECONDS = 7200;
|
|
29
27
|
/** access token 寿命上限(秒);会话吊销标记的 TTL 按它计算,与实例当前配置无关。 */
|
|
@@ -573,9 +571,11 @@ export const KernelConfigSchema = z.object({
|
|
|
573
571
|
maxSteps: z
|
|
574
572
|
.number()
|
|
575
573
|
.int()
|
|
576
|
-
.
|
|
577
|
-
.default(
|
|
578
|
-
.meta(meta({
|
|
574
|
+
.nonnegative()
|
|
575
|
+
.default(0)
|
|
576
|
+
.meta(meta({
|
|
577
|
+
description: "Maximum model steps per agent run before the final wrap-up; 0 (the default) means no limit: a run goes on until the model stops calling tools or someone stops it",
|
|
578
|
+
})),
|
|
579
579
|
wakeOnBackgroundTask: z
|
|
580
580
|
.boolean()
|
|
581
581
|
.default(true)
|
|
@@ -585,10 +585,10 @@ export const KernelConfigSchema = z.object({
|
|
|
585
585
|
maxRunSeconds: z
|
|
586
586
|
.number()
|
|
587
587
|
.int()
|
|
588
|
-
.
|
|
589
|
-
.default(
|
|
588
|
+
.nonnegative()
|
|
589
|
+
.default(0)
|
|
590
590
|
.meta(meta({
|
|
591
|
-
description: "Maximum wall-clock duration of one agent run in seconds (time spent waiting for people is not counted).
|
|
591
|
+
description: "Maximum wall-clock duration of one agent run in seconds (time spent waiting for people is not counted); 0 (the default) means no limit. With a limit, sandbox commands are limited to the time left in the run, so agents that need long commands need maxRunSeconds >= sandbox.limits.maxCommandSeconds; without one, commands are limited only by sandbox.limits.maxCommandSeconds and the tool timeouts",
|
|
592
592
|
})),
|
|
593
593
|
streamIdleMs: z
|
|
594
594
|
.number()
|
|
@@ -669,9 +669,9 @@ export const KernelConfigSchema = z.object({
|
|
|
669
669
|
})),
|
|
670
670
|
})
|
|
671
671
|
.default({
|
|
672
|
-
maxSteps:
|
|
672
|
+
maxSteps: 0,
|
|
673
673
|
wakeOnBackgroundTask: true,
|
|
674
|
-
maxRunSeconds:
|
|
674
|
+
maxRunSeconds: 0,
|
|
675
675
|
streamIdleMs: 60000,
|
|
676
676
|
maxOutputTokens: 16000,
|
|
677
677
|
compactionThreshold: 0.8,
|
|
@@ -1178,7 +1178,7 @@ export const KernelConfigSchema = z.object({
|
|
|
1178
1178
|
.positive()
|
|
1179
1179
|
.default(DEFAULT_MAX_COMMAND_SECONDS)
|
|
1180
1180
|
.meta(meta({
|
|
1181
|
-
description: "Longest a single sandbox command may run, in seconds.
|
|
1181
|
+
description: "Longest a single sandbox command may run, in seconds. When runtime.maxRunSeconds is set (not 0), a command also stops when its run is about to reach it, so long commands need runtime.maxRunSeconds >= maxCommandSeconds (a startup warning is logged otherwise)",
|
|
1182
1182
|
})),
|
|
1183
1183
|
suspendAfterIdleMinutes: z
|
|
1184
1184
|
.number()
|
|
@@ -1443,15 +1443,6 @@ export const KernelConfigSchema = z.object({
|
|
|
1443
1443
|
.meta(meta({
|
|
1444
1444
|
description: "usage.report delivery jobs one worker replica processes at the same time",
|
|
1445
1445
|
})),
|
|
1446
|
-
usageReportKeepHours: z
|
|
1447
|
-
.number()
|
|
1448
|
-
.int()
|
|
1449
|
-
.min(1)
|
|
1450
|
-
.max(720)
|
|
1451
|
-
.default(24)
|
|
1452
|
-
.meta(meta({
|
|
1453
|
-
description: "Hours a finished usage.report job is kept in Redis so re-enqueueing the same usage event (resumed step, sweeper) is deduplicated; at least 1, well above the sweeper's 10-minute overlap",
|
|
1454
|
-
})),
|
|
1455
1446
|
})
|
|
1456
1447
|
.prefault({}),
|
|
1457
1448
|
log: z
|
|
@@ -44,3 +44,19 @@ export interface AgentTreeNode {
|
|
|
44
44
|
* check of the root.
|
|
45
45
|
*/
|
|
46
46
|
export declare function readAgentTree(db: KernelSchemaDb, orgId: string, rootId: string): Promise<AgentTreeNode | null>;
|
|
47
|
+
/** One sub-agent a conversation dispatched, as `TaskList` lists it. */
|
|
48
|
+
export interface ChildAgent {
|
|
49
|
+
/** The child conversation's id: the agentId the parent saw. */
|
|
50
|
+
id: string;
|
|
51
|
+
/** The dispatched description (the child's title when no dispatch event has one). */
|
|
52
|
+
description: string;
|
|
53
|
+
/** The latest run's status; `queued` before the first run is recorded. */
|
|
54
|
+
status: "queued" | "running" | "waiting" | "completed" | "failed" | "aborted";
|
|
55
|
+
/** When the latest run ended; null while it is active. */
|
|
56
|
+
finishedAt: Date | null;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The sub-agents `parentId` of org `orgId` dispatched itself (not their own sub-agents), oldest
|
|
60
|
+
* first: live sub-agent conversations with a dispatch row, as {@link readAgentTree} counts them.
|
|
61
|
+
*/
|
|
62
|
+
export declare function readChildAgents(db: KernelSchemaDb, orgId: string, parentId: string): Promise<ChildAgent[]>;
|
|
@@ -90,3 +90,39 @@ function toNode(r, isRoot) {
|
|
|
90
90
|
};
|
|
91
91
|
}
|
|
92
92
|
const modelOf = (m) => m ? { provider: m.provider, model: m.model } : null;
|
|
93
|
+
/**
|
|
94
|
+
* The sub-agents `parentId` of org `orgId` dispatched itself (not their own sub-agents), oldest
|
|
95
|
+
* first: live sub-agent conversations with a dispatch row, as {@link readAgentTree} counts them.
|
|
96
|
+
*/
|
|
97
|
+
export async function readChildAgents(db, orgId, parentId) {
|
|
98
|
+
const { rows } = await sql `
|
|
99
|
+
SELECT c.id, c.title,
|
|
100
|
+
(SELECT e.payload->>'description' FROM capybara.conversation_events e
|
|
101
|
+
WHERE e.conversation_id = c.parent_conversation_id AND e.type = 'subagent_dispatched'
|
|
102
|
+
AND e.payload->>'subagentId' = c.id
|
|
103
|
+
ORDER BY e.seq DESC LIMIT 1) AS description,
|
|
104
|
+
latest.status AS last_status, latest.finished_at AS last_finished_at
|
|
105
|
+
FROM capybara.conversations c
|
|
106
|
+
LEFT JOIN LATERAL (
|
|
107
|
+
SELECT r.status, r.finished_at FROM capybara.runs r
|
|
108
|
+
WHERE r.conversation_id = c.id
|
|
109
|
+
ORDER BY r.created_at DESC, r.id DESC
|
|
110
|
+
LIMIT 1
|
|
111
|
+
) latest ON true
|
|
112
|
+
WHERE c.parent_conversation_id = ${parentId} AND c.org_id = ${orgId}
|
|
113
|
+
AND c.kind = 'subagent' AND c.deleted_at IS NULL
|
|
114
|
+
AND EXISTS (
|
|
115
|
+
SELECT 1 FROM capybara.subagent_dispatches sd WHERE sd.child_conversation_id = c.id
|
|
116
|
+
)
|
|
117
|
+
ORDER BY c.created_at, c.id
|
|
118
|
+
`.execute(db);
|
|
119
|
+
return rows.map((r) => {
|
|
120
|
+
const status = r.last_status ?? "queued";
|
|
121
|
+
return {
|
|
122
|
+
id: r.id,
|
|
123
|
+
description: r.description ?? r.title ?? "",
|
|
124
|
+
status,
|
|
125
|
+
finishedAt: ACTIVE.has(status) ? null : (r.last_finished_at ?? null),
|
|
126
|
+
};
|
|
127
|
+
});
|
|
128
|
+
}
|
|
@@ -7,7 +7,6 @@ import { ConversationDevices, type NewConversationDevice } from "../devices/conv
|
|
|
7
7
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
8
8
|
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
9
9
|
import { KernelEvents } from "../infra/events/kernel-events.js";
|
|
10
|
-
import type { KernelLogger } from "../log.js";
|
|
11
10
|
import { ProviderRegistry } from "../runtime/providers.js";
|
|
12
11
|
import type { ScheduleMarker } from "../schedules/types.js";
|
|
13
12
|
import { type RenderedInvocation, SkillInvoker } from "../skills/invoke.js";
|
|
@@ -118,10 +117,30 @@ export interface ReplySkips {
|
|
|
118
117
|
export declare const NO_REPLY_SKIPS: ReplySkips;
|
|
119
118
|
/** 会话服务用到的运行控制:投递运行(RunQueue)与跨副本中止(RunAbort)。 */
|
|
120
119
|
export interface RunControl {
|
|
121
|
-
/**
|
|
122
|
-
|
|
120
|
+
/**
|
|
121
|
+
* The direct `ensureRun` after a commit that owes the ensure-run backstop: a failure is logged,
|
|
122
|
+
* never thrown (transactional outbox spec §6).
|
|
123
|
+
*/
|
|
124
|
+
ensureRunAfterCommit(conversationId: string): Promise<void>;
|
|
125
|
+
/**
|
|
126
|
+
* Owes the ensure-run backstop in the caller's transaction (transactional outbox spec §6): `seq`
|
|
127
|
+
* is the event it wrote (or the conversation's newest), `orgId` the conversation's.
|
|
128
|
+
*/
|
|
129
|
+
oweEnsureRun(conversationId: string, seq: number | string, orgId: string): Promise<boolean>;
|
|
123
130
|
/** After a root's queue was unpaused: the wakes it held for its tree (agent map spec §5.3). */
|
|
124
131
|
wakeHeldTree(rootId: string): Promise<void>;
|
|
132
|
+
/**
|
|
133
|
+
* In the transaction that may release a root's hold: owes the ensure-run backstop of each
|
|
134
|
+
* sub-agent {@link wakeHeldTree} wakes after the commit (transactional outbox spec §6).
|
|
135
|
+
*/
|
|
136
|
+
oweHeldTreeIn(rootId: string): Promise<void>;
|
|
137
|
+
/**
|
|
138
|
+
* In a decision's transaction, after its decided rows: resumes the waiting run when it waits on
|
|
139
|
+
* nothing else (lock order: conversations, decided rows, runs).
|
|
140
|
+
*/
|
|
141
|
+
resumeIn(runId: string, waitId: string): Promise<unknown>;
|
|
142
|
+
/** In a root's delete transaction: owes the stop of its sub-agent tree (`stop-tree`). */
|
|
143
|
+
oweStopDeletedTree(conversationId: string, byAccountId: string | null, orgId: string): Promise<boolean>;
|
|
125
144
|
/**
|
|
126
145
|
* 运行是孤儿(会话锁空闲、任务已不存在,没有 worker 会收到中止广播)时直接把它写成 aborted,返回 true;
|
|
127
146
|
* 否则返回 false。
|
|
@@ -222,7 +241,6 @@ export declare class ConversationsService {
|
|
|
222
241
|
private readonly runs;
|
|
223
242
|
/** Resolves a switched-to model and its declared parameters (UI refresh B). */
|
|
224
243
|
private readonly providers;
|
|
225
|
-
private readonly log;
|
|
226
244
|
/** Reads `@` resources with the sender's credentials. */
|
|
227
245
|
private readonly content;
|
|
228
246
|
/** Binds uploaded files to the conversation in the message's transaction. */
|
|
@@ -243,7 +261,7 @@ export declare class ConversationsService {
|
|
|
243
261
|
/** Starts and stops the conversation's runs. */
|
|
244
262
|
runs: RunControl,
|
|
245
263
|
/** Resolves a switched-to model and its declared parameters (UI refresh B). */
|
|
246
|
-
providers: Pick<ProviderRegistry, "resolve">,
|
|
264
|
+
providers: Pick<ProviderRegistry, "resolve">,
|
|
247
265
|
/** Reads `@` resources with the sender's credentials. */
|
|
248
266
|
content: Pick<ConnectorContent, "resolveAttachments">,
|
|
249
267
|
/** Binds uploaded files to the conversation in the message's transaction. */
|
|
@@ -258,11 +276,6 @@ export declare class ConversationsService {
|
|
|
258
276
|
questions: Pick<QuestionsService, "skipPendingIn">,
|
|
259
277
|
/** ... and its pending network asks (network ask-to-allow spec §6.7). */
|
|
260
278
|
approvals: Pick<ApprovalsService, "skipNetworkIn">, events: Pick<KernelEvents, "emit">, txHost: TransactionHost);
|
|
261
|
-
/**
|
|
262
|
-
* 不阻塞地调用一次 `ensureRun`(打开 SSE、读事件时):它幂等,并顺带清理孤儿运行,
|
|
263
|
-
* 会话不会因为某次运行结束后的投递失败而永远停在「运行中」。失败只记日志。
|
|
264
|
-
*/
|
|
265
|
-
kickRun(conversationId: string): void;
|
|
266
279
|
/**
|
|
267
280
|
* Creates an empty conversation bound to its device (`pick`, see
|
|
268
281
|
* {@link ConversationDevices.forNew}); an agent the account cannot use →
|
|
@@ -297,6 +310,11 @@ export declare class ConversationsService {
|
|
|
297
310
|
* the other conversation is never returned).
|
|
298
311
|
*/
|
|
299
312
|
private findCreated;
|
|
313
|
+
/**
|
|
314
|
+
* The direct `ensureRun` after a message's commit ({@link RunControl.ensureRunAfterCommit}): a
|
|
315
|
+
* failure never fails the request, since the message's transaction owes the ensure-run backstop.
|
|
316
|
+
*/
|
|
317
|
+
private startRun;
|
|
300
318
|
private rowView;
|
|
301
319
|
/**
|
|
302
320
|
* Renames a conversation (owner only, as {@link get}): `title_source = 'manual'`, so the title
|
|
@@ -399,12 +417,15 @@ export declare class ConversationsService {
|
|
|
399
417
|
* only after an access check.
|
|
400
418
|
*/
|
|
401
419
|
sandboxConversationOf(orgId: string, conversationId: string): Promise<string>;
|
|
402
|
-
/**
|
|
420
|
+
/**
|
|
421
|
+
* 读事件(先做 `get` 的可见性判定). A read never changes run state (transactional outbox spec
|
|
422
|
+
* §6): a run nobody runs is the `run-orphan-check`'s to repair.
|
|
423
|
+
*/
|
|
403
424
|
listEvents(orgId: string, conversationId: string, viewerAccountId: string, afterSeq: number, limit: number): Promise<ConversationEvent[]>;
|
|
404
425
|
/**
|
|
405
426
|
* The turn-aligned page of events before `beforeSeq` (`null`: the log's last page), with how
|
|
406
427
|
* many runs started before it ({@link EventLog.readTurnPageBefore}; history paging spec §2).
|
|
407
|
-
* Visibility as {@link get};
|
|
428
|
+
* Visibility as {@link get}; a read only, as {@link listEvents}.
|
|
408
429
|
*/
|
|
409
430
|
listEventsBefore(orgId: string, conversationId: string, viewerAccountId: string, beforeSeq: number | null, limit: number): Promise<{
|
|
410
431
|
events: ConversationEvent[];
|
|
@@ -413,7 +434,8 @@ export declare class ConversationsService {
|
|
|
413
434
|
/**
|
|
414
435
|
* 追加一条用户消息;带 `clientMessageId` 时重复提交返回同一事件。写入后调用 `ensureRun`:
|
|
415
436
|
* 没有进行中的运行时投递一轮;运行中则由当前运行在步骤间隙吸收,或在它结束时再次 `ensureRun`。
|
|
416
|
-
*
|
|
437
|
+
* A failed `ensureRun` is logged, not thrown: the message's transaction owes the ensure-run
|
|
438
|
+
* backstop, which starts the run once the queue is reachable (transactional outbox spec §6).
|
|
417
439
|
*
|
|
418
440
|
* `attachments`(`@` 资源引用):内核以发送者的凭据读取资源(打码、限长),作为用户文本之后的额外文本块写入同一条
|
|
419
441
|
* user_message,并在 `payload.attachments` 记录元数据(第 i 个附件对应 `content[i + 1]`)。读取失败整条不写。
|
|
@@ -452,6 +474,13 @@ export declare class ConversationsService {
|
|
|
452
474
|
* conversations (network ask-to-allow spec §6.7). The queue's "send now" uses it as the direct
|
|
453
475
|
* send does; announce after the commit with {@link announceSkipped}. `toSeq` is the highest seq
|
|
454
476
|
* written in this conversation (null: none).
|
|
477
|
+
*
|
|
478
|
+
* Lock order: this (root) conversation (the caller's), its pending questions, the sub-agents'
|
|
479
|
+
* conversations with a pending network ask (id order), the asks themselves (the root's and the
|
|
480
|
+
* sub-agents'), and only then the run rows, as each skipped wait's run resumes in this
|
|
481
|
+
* transaction. Every run row comes after every conversation and decided row (ruling R19); a
|
|
482
|
+
* sub-agent's conversation comes after the root's questions, which only this path locks with a
|
|
483
|
+
* sub-agent conversation after them.
|
|
455
484
|
*/
|
|
456
485
|
skipWaitsIn(conversationId: string, accountId: string, messageSeq: number): Promise<{
|
|
457
486
|
skipped: ReplySkips;
|
|
@@ -460,7 +489,8 @@ export declare class ConversationsService {
|
|
|
460
489
|
/**
|
|
461
490
|
* After the commit: announces each skipped question (`question.answered`, outcome `skipped`) and
|
|
462
491
|
* network ask (`approval.decided`, status `skipped`; a sub-agent's conversation is notified
|
|
463
|
-
* first)
|
|
492
|
+
* first) as domain events with no kernel subscriber: the IM card updates are owed by the skips'
|
|
493
|
+
* appends (transactional outbox spec §8.1); the run resumed in the message's transaction.
|
|
464
494
|
*/
|
|
465
495
|
announceSkipped(skipped: ReplySkips): Promise<void>;
|
|
466
496
|
/**
|