@capybara-hq/kernel 0.1.0-rc.56 → 0.1.0-rc.58
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 +3 -3
- package/dist/admin/extensions.controller.d.ts +2 -10
- package/dist/admin/extensions.controller.js +2 -24
- package/dist/admin/extensions.schemas.d.ts +21 -0
- package/dist/admin/extensions.schemas.js +37 -0
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/catalog/org-extension-switch.d.ts +3 -1
- package/dist/catalog/org-extension-switch.js +12 -5
- package/dist/catalog/org-extension.controller.d.ts +2 -21
- package/dist/catalog/org-extension.controller.js +1 -38
- package/dist/catalog/org-extension.schemas.d.ts +31 -0
- package/dist/catalog/org-extension.schemas.js +65 -0
- package/dist/channels/actions.js +2 -0
- package/dist/channels/cards.d.ts +8 -0
- package/dist/channels/cards.js +18 -0
- package/dist/channels/outbound.js +8 -1
- package/dist/channels/phrases.d.ts +10 -0
- package/dist/channels/phrases.js +10 -0
- package/dist/conversations/conversations.module.js +4 -0
- package/dist/conversations/events.d.ts +1 -0
- package/dist/conversations/events.js +5 -0
- package/dist/conversations/leak-check.d.ts +26 -0
- package/dist/conversations/leak-check.js +69 -0
- package/dist/ctx/secrets.d.ts +5 -0
- package/dist/ctx/secrets.js +5 -0
- package/dist/db/kernel-schema.d.ts +15 -0
- package/dist/devices/device-projects.service.d.ts +3 -1
- package/dist/devices/device-projects.service.js +25 -7
- package/dist/errors.d.ts +9 -0
- package/dist/errors.js +9 -0
- package/dist/extensions/deployment-config.d.ts +6 -2
- package/dist/extensions/deployment-config.js +35 -23
- package/dist/extensions/org-config.d.ts +2 -0
- package/dist/extensions/org-config.js +1 -0
- package/dist/extensions/org-overrides.d.ts +2 -0
- package/dist/extensions/org-overrides.js +3 -2
- package/dist/identity/initial-passwords.job.d.ts +3 -2
- package/dist/identity/initial-passwords.job.js +9 -5
- package/dist/platform/exclusions.js +1 -41
- package/dist/platform/method.js +1 -0
- package/dist/platform/operation.d.ts +30 -2
- package/dist/platform/operation.js +1 -1
- package/dist/platform/ops/extensions-admin.ops.d.ts +15 -2
- package/dist/platform/ops/extensions-admin.ops.js +153 -5
- package/dist/platform/ops/extensions.ops.d.ts +19 -5
- package/dist/platform/ops/extensions.ops.js +285 -10
- package/dist/platform/ops/person-inputs.d.ts +35 -0
- package/dist/platform/ops/person-inputs.js +79 -0
- package/dist/platform/ops/plugins.ops.d.ts +2 -0
- package/dist/platform/ops/plugins.ops.js +30 -17
- package/dist/platform/parity.d.ts +6 -2
- package/dist/platform/parity.js +38 -1
- package/dist/platform/person-fields.d.ts +103 -0
- package/dist/platform/person-fields.js +381 -0
- package/dist/platform/platform.module.d.ts +3 -3
- package/dist/platform/platform.module.js +6 -5
- package/dist/platform/sealed-inputs.d.ts +44 -0
- package/dist/platform/sealed-inputs.js +116 -0
- package/dist/runtime/waits.js +8 -2
- package/dist/sandbox/sandbox.service.js +5 -1
- package/dist/tools/approvals.controller.d.ts +1 -0
- package/dist/tools/approvals.controller.js +19 -0
- package/dist/tools/approvals.service.d.ts +50 -1
- package/dist/tools/approvals.service.js +118 -10
- package/dist/tools/builtins/builtin-tools.d.ts +3 -0
- package/dist/tools/builtins/builtin-tools.js +31 -9
- package/dist/tools/builtins/platform.d.ts +20 -0
- package/dist/tools/builtins/platform.js +174 -15
- package/dist/tools/pipeline.d.ts +24 -2
- package/dist/tools/pipeline.js +80 -17
- package/dist/tools/policy.d.ts +3 -0
- package/dist/tools/policy.js +6 -0
- package/dist/tools/registry.d.ts +22 -0
- package/dist/tools/tools.module.js +3 -0
- package/package.json +4 -4
- package/src/migrations/0169_platform_sealed_inputs.sql +23 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
8
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
9
|
+
};
|
|
10
|
+
import { Inject, Injectable } from "@nestjs/common";
|
|
11
|
+
import { sql } from "kysely";
|
|
12
|
+
import { platformSealedInputAad } from "../ctx/secrets.js";
|
|
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";
|
|
16
|
+
import { KEYRING } from "../infra/tokens.js";
|
|
17
|
+
/** How long a person's sealed card values wait for the resumed call, from the decision. */
|
|
18
|
+
export const SEALED_INPUT_TTL_MS = 15 * 60 * 1000;
|
|
19
|
+
/**
|
|
20
|
+
* The sealed values are gone: consumed already, expired, not this recipient's, or no longer
|
|
21
|
+
* unwrappable. Says nothing about which.
|
|
22
|
+
*/
|
|
23
|
+
export class SealedInputsExpiredError extends Error {
|
|
24
|
+
constructor() {
|
|
25
|
+
super("The sealed person inputs are no longer available");
|
|
26
|
+
this.name = "SealedInputsExpiredError";
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Values a person typed on an approval card for a Platform call's secret fields (platform web
|
|
31
|
+
* parity spec §4.3). They are sealed with the master keyring in the decision's transaction and
|
|
32
|
+
* consumed once by the resumed call, in the transaction that applies the write. They never reach
|
|
33
|
+
* the event log, a tool result, the approval row, the audit or a log line.
|
|
34
|
+
*/
|
|
35
|
+
let SealedInputs = class SealedInputs {
|
|
36
|
+
db;
|
|
37
|
+
txHost;
|
|
38
|
+
keyring;
|
|
39
|
+
constructor(db, txHost, keyring) {
|
|
40
|
+
this.db = db;
|
|
41
|
+
this.txHost = txHost;
|
|
42
|
+
this.keyring = keyring;
|
|
43
|
+
}
|
|
44
|
+
/** Seals `values` for the approval, in the caller's (the decision's) transaction. */
|
|
45
|
+
async sealIn(row, values) {
|
|
46
|
+
const paths = Object.keys(values).sort();
|
|
47
|
+
const plain = Buffer.from(JSON.stringify(values), "utf8");
|
|
48
|
+
const { data, masterKeyId } = this.keyring.wrap(plain, platformSealedInputAad(row.approvalId, paths));
|
|
49
|
+
plain.fill(0);
|
|
50
|
+
await this.db
|
|
51
|
+
.insertInto("platform_sealed_inputs")
|
|
52
|
+
.values({
|
|
53
|
+
approval_id: row.approvalId,
|
|
54
|
+
recipient_account_id: row.recipientAccountId,
|
|
55
|
+
conversation_id: row.conversationId,
|
|
56
|
+
run_id: row.runId,
|
|
57
|
+
call_id: row.callId,
|
|
58
|
+
paths,
|
|
59
|
+
master_key_id: masterKeyId,
|
|
60
|
+
ciphertext: data,
|
|
61
|
+
// the DB clock, the same one consume and the cleanup compare against
|
|
62
|
+
expires_at: sql `now() + make_interval(secs => ${SEALED_INPUT_TTL_MS / 1000})`,
|
|
63
|
+
})
|
|
64
|
+
.execute();
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Locks, deletes and unwraps the approval's values for `recipientAccountId`, then runs `apply`
|
|
68
|
+
* with them in the same transaction: when `apply` throws, the row comes back. A row that is
|
|
69
|
+
* missing, expired, another person's or not unwrappable is {@link SealedInputsExpiredError}.
|
|
70
|
+
*/
|
|
71
|
+
async consume(approvalId, recipientAccountId, apply) {
|
|
72
|
+
const row = await this.db
|
|
73
|
+
.deleteFrom("platform_sealed_inputs")
|
|
74
|
+
.where("approval_id", "=", approvalId)
|
|
75
|
+
.where("recipient_account_id", "=", recipientAccountId)
|
|
76
|
+
.where("expires_at", ">", sql `now()`)
|
|
77
|
+
.returning(["approval_id", "paths", "master_key_id", "ciphertext"])
|
|
78
|
+
.executeTakeFirst();
|
|
79
|
+
if (row === undefined)
|
|
80
|
+
throw new SealedInputsExpiredError();
|
|
81
|
+
let values;
|
|
82
|
+
let plain;
|
|
83
|
+
try {
|
|
84
|
+
plain = this.keyring.unwrap(row.master_key_id, Buffer.from(row.ciphertext), platformSealedInputAad(row.approval_id, row.paths));
|
|
85
|
+
values = JSON.parse(plain.toString("utf8"));
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
throw new SealedInputsExpiredError();
|
|
89
|
+
}
|
|
90
|
+
finally {
|
|
91
|
+
plain?.fill(0);
|
|
92
|
+
}
|
|
93
|
+
return apply(values);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
__decorate([
|
|
97
|
+
Transactional({ propagation: "mandatory" })
|
|
98
|
+
], SealedInputs.prototype, "sealIn", null);
|
|
99
|
+
__decorate([
|
|
100
|
+
Transactional()
|
|
101
|
+
], SealedInputs.prototype, "consume", null);
|
|
102
|
+
SealedInputs = __decorate([
|
|
103
|
+
Injectable(),
|
|
104
|
+
__param(0, Inject(KernelDb)),
|
|
105
|
+
__param(1, Inject(TransactionHost)),
|
|
106
|
+
__param(2, Inject(KEYRING))
|
|
107
|
+
], SealedInputs);
|
|
108
|
+
export { SealedInputs };
|
|
109
|
+
/** Deletes the sealed person inputs past `expires_at`; returns how many. */
|
|
110
|
+
export async function deleteExpiredSealedInputs(db) {
|
|
111
|
+
const r = await db
|
|
112
|
+
.deleteFrom("platform_sealed_inputs")
|
|
113
|
+
.where("expires_at", "<=", sql `now()`)
|
|
114
|
+
.executeTakeFirst();
|
|
115
|
+
return Number(r.numDeletedRows);
|
|
116
|
+
}
|
package/dist/runtime/waits.js
CHANGED
|
@@ -20,7 +20,7 @@ import { KernelEvents } from "../infra/events/kernel-events.js";
|
|
|
20
20
|
import { OutboxService } from "../infra/outbox/outbox.service.js";
|
|
21
21
|
import { KERNEL_LOGGER } from "../infra/tokens.js";
|
|
22
22
|
import { ApprovalsService, GIT_ASK_CANCELLED_NOTE, NETWORK_ASK_CANCELLED_NOTE, } from "../tools/approvals.service.js";
|
|
23
|
-
import { APPROVAL_CANCELLED_TEXT, INPUT_CANCELLED_TEXT, RUN_STOPPED_TEXT, ToolPipeline, } from "../tools/pipeline.js";
|
|
23
|
+
import { APPROVAL_CANCELLED_TEXT, INPUT_CANCELLED_TEXT, RUN_STOPPED_TEXT, ToolPipeline, withLeakedOnCard, } from "../tools/pipeline.js";
|
|
24
24
|
import { QuestionsService } from "../tools/questions.service.js";
|
|
25
25
|
import { RunAbort } from "./abort.js";
|
|
26
26
|
import { KyselyCheckpointSaver } from "./checkpointer.js";
|
|
@@ -351,7 +351,13 @@ let Waits = class Waits {
|
|
|
351
351
|
text: `${kept.content}\n\n${a.kind === "git" ? GIT_ASK_CANCELLED_NOTE : NETWORK_ASK_CANCELLED_NOTE}`,
|
|
352
352
|
isError: kept.isError,
|
|
353
353
|
}
|
|
354
|
-
: {
|
|
354
|
+
: {
|
|
355
|
+
toolKey: a.tool_key,
|
|
356
|
+
// a tool ask whose card refused a person's value as leaked says so (§4.5)
|
|
357
|
+
text: withLeakedOnCard(APPROVAL_CANCELLED_TEXT, a.kind === "tool"
|
|
358
|
+
? a.detail?.leakedFields
|
|
359
|
+
: undefined),
|
|
360
|
+
});
|
|
355
361
|
const step = await this.approvalStep(t, conversationId, a.id);
|
|
356
362
|
if (step !== null)
|
|
357
363
|
steps.add(step);
|
|
@@ -1529,7 +1529,11 @@ let SandboxService = class SandboxService {
|
|
|
1529
1529
|
// outside any transaction; a failure releases the lease.
|
|
1530
1530
|
const binder = p.binder;
|
|
1531
1531
|
if (binder)
|
|
1532
|
-
await runOutsideTransaction(() => binder.bind(env, sig))
|
|
1532
|
+
await runOutsideTransaction(() => binder.bind(env, sig)).catch((err) => {
|
|
1533
|
+
// the caller's answer may be generic: the device's own refusal is kept for ops here
|
|
1534
|
+
this.log.warn("cannot bind the device's instance", { deviceId: env, err });
|
|
1535
|
+
throw err;
|
|
1536
|
+
});
|
|
1533
1537
|
const shared = await lease.downgrade();
|
|
1534
1538
|
await this.register(env, shared.token);
|
|
1535
1539
|
return {
|
|
@@ -26,6 +26,7 @@ declare const DecisionBody: z.ZodObject<{
|
|
|
26
26
|
note: z.ZodOptional<z.ZodString>;
|
|
27
27
|
reason: z.ZodOptional<z.ZodString>;
|
|
28
28
|
hosts: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
29
|
+
inputs: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
29
30
|
}, z.core.$strict>;
|
|
30
31
|
/**
|
|
31
32
|
* 工具调用审批(`/api/v1/orgs/:orgId/approvals`,`approvals.decide`:访客起、仅限用户主体——agent 不能审批,R2)。
|
|
@@ -14,10 +14,13 @@ import { CurrentPrincipal, patTokenId, requireUser, } from "../identity/principa
|
|
|
14
14
|
import { createZodDto } from "../infra/validation/create-zod-dto.js";
|
|
15
15
|
import { ZBody, ZQuery } from "../infra/validation/zod-params.js";
|
|
16
16
|
import { CurrentOrg, OrgAction } from "../orgs/org.guard.js";
|
|
17
|
+
import { PERSON_FIELD_MAX_BYTES, PERSON_FIELDS_MAX } from "../platform/person-fields.js";
|
|
17
18
|
import { CONTROL_OR_HIDDEN_RE } from "../text.js";
|
|
18
19
|
import { ApprovalsListQuerySchema } from "./approvals.schemas.js";
|
|
19
20
|
import { APPROVAL_DENY_REASON_MAX_CHARS, ApprovalsService, publicRecord, } from "./approvals.service.js";
|
|
20
21
|
const ListQuery = ApprovalsListQuerySchema;
|
|
22
|
+
/** The longest input path: the person field paths' own limit (`askPerson`). */
|
|
23
|
+
const INPUT_PATH_MAX_CHARS = 200;
|
|
21
24
|
const DecisionBody = z
|
|
22
25
|
.object({
|
|
23
26
|
decision: z.enum(["approve", "deny"]),
|
|
@@ -42,8 +45,24 @@ const DecisionBody = z
|
|
|
42
45
|
.min(1)
|
|
43
46
|
.max(NETWORK_ASK_MAX_HOSTS)
|
|
44
47
|
.optional(),
|
|
48
|
+
/**
|
|
49
|
+
* An approval with person fields only: the values the person entered on the card, by field
|
|
50
|
+
* path (platform web parity spec §4.2). Never logged, never stored but sealed.
|
|
51
|
+
*/
|
|
52
|
+
inputs: z
|
|
53
|
+
// the value's max counts characters (a cheap upper bound); the 16 KiB byte limit is
|
|
54
|
+
// checkPersonValue's, in the decision
|
|
55
|
+
.record(z.string().min(1).max(INPUT_PATH_MAX_CHARS), z.string().max(PERSON_FIELD_MAX_BYTES))
|
|
56
|
+
.refine((r) => Object.keys(r).length <= PERSON_FIELDS_MAX, {
|
|
57
|
+
message: `At most ${PERSON_FIELDS_MAX} inputs`,
|
|
58
|
+
})
|
|
59
|
+
.optional(),
|
|
45
60
|
})
|
|
46
61
|
.strict()
|
|
62
|
+
.refine((b) => b.inputs === undefined || b.decision === "approve", {
|
|
63
|
+
path: ["inputs"],
|
|
64
|
+
message: "Inputs can only be given with approve",
|
|
65
|
+
})
|
|
47
66
|
.refine((b) => b.reason === undefined || b.decision === "deny", {
|
|
48
67
|
path: ["reason"],
|
|
49
68
|
message: "A reason can only be given with deny",
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { type LocalizedName, type ToolDisplay } from "@capybara-hq/plugin-sdk";
|
|
2
2
|
import { EventLog } from "../conversations/event-log.js";
|
|
3
3
|
import { type ReviewRecord, type ToolView } from "../conversations/events.js";
|
|
4
|
+
import { ConversationLeakCheck } from "../conversations/leak-check.js";
|
|
4
5
|
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
5
6
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
6
7
|
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
7
8
|
import { KernelEvents } from "../infra/events/kernel-events.js";
|
|
8
9
|
import { LockService } from "../infra/lock/lock.service.js";
|
|
9
10
|
import { type OrgRole } from "../orgs/roles.js";
|
|
11
|
+
import { type PersonFieldSpec } from "../platform/person-fields.js";
|
|
12
|
+
import { SealedInputs } from "../platform/sealed-inputs.js";
|
|
10
13
|
import { RunResume } from "../runtime/run-jobs.js";
|
|
11
14
|
import { PermissionAuthorityLock } from "../sandbox/permission-authority-lock.js";
|
|
12
15
|
import { PermissionCallGrants } from "../sandbox/permission-call-grants.js";
|
|
@@ -150,6 +153,21 @@ export interface ApprovalRecord {
|
|
|
150
153
|
git: GitAskDetail | null;
|
|
151
154
|
/** A command review ask's facts; null for any other kind. */
|
|
152
155
|
command: CommandAskDetail | null;
|
|
156
|
+
/**
|
|
157
|
+
* A tool ask with person fields (platform web parity spec §4.2): the secret fields the person
|
|
158
|
+
* enters on the card, as specs (path, label, schema node), never values. Absent otherwise.
|
|
159
|
+
*/
|
|
160
|
+
personFields?: PersonFieldSpec[];
|
|
161
|
+
/** The card's target name with {@link personFields} ("Anthropic: deployment config"). */
|
|
162
|
+
personTarget?: {
|
|
163
|
+
zh: string;
|
|
164
|
+
en: string;
|
|
165
|
+
};
|
|
166
|
+
/**
|
|
167
|
+
* The person fields whose entered value was refused as leaked (§4.5; paths only): a call that
|
|
168
|
+
* then ends without running tells the agent to have them rotated.
|
|
169
|
+
*/
|
|
170
|
+
leakedFields?: string[];
|
|
153
171
|
toolKey: string;
|
|
154
172
|
specHash: string;
|
|
155
173
|
connectorId: string | null;
|
|
@@ -258,6 +276,18 @@ export interface ApprovalRequestInput {
|
|
|
258
276
|
};
|
|
259
277
|
/** The scopes the decider may choose; unset: derived (see {@link ApprovalRecord.scopes}). */
|
|
260
278
|
scopes?: ApprovalScope[];
|
|
279
|
+
/** A tool ask's person fields and their target (stored in `detail`; specs only, no values). */
|
|
280
|
+
detail?: ToolAskDetail;
|
|
281
|
+
}
|
|
282
|
+
/** A tool ask's `detail`: the person fields the card asks for and the target they belong to. */
|
|
283
|
+
export interface ToolAskDetail {
|
|
284
|
+
personFields: PersonFieldSpec[];
|
|
285
|
+
target: {
|
|
286
|
+
zh: string;
|
|
287
|
+
en: string;
|
|
288
|
+
};
|
|
289
|
+
/** The paths whose entered value the decision refused as leaked (written by the decision). */
|
|
290
|
+
leakedFields?: string[];
|
|
261
291
|
}
|
|
262
292
|
export interface DecisionInput {
|
|
263
293
|
decision: "approve" | "deny";
|
|
@@ -271,6 +301,11 @@ export interface DecisionInput {
|
|
|
271
301
|
* unchecked ones count as denied (P-D5).
|
|
272
302
|
*/
|
|
273
303
|
hosts?: string[] | undefined;
|
|
304
|
+
/**
|
|
305
|
+
* An approval with person fields only, with `approve`: the values the person entered, by field
|
|
306
|
+
* path (platform web parity spec §4.2). Sealed in the decision; never logged or stored as text.
|
|
307
|
+
*/
|
|
308
|
+
inputs?: Record<string, string> | undefined;
|
|
274
309
|
}
|
|
275
310
|
/** Where a decision was made (the device's local audit line and `device.network_*`). */
|
|
276
311
|
export type DecisionVia = "web" | "im";
|
|
@@ -352,6 +387,10 @@ export declare class ApprovalsService {
|
|
|
352
387
|
private readonly sandbox;
|
|
353
388
|
/** A decision resumes its waiting run in the decision's transaction. */
|
|
354
389
|
private readonly resumes;
|
|
390
|
+
/** An entered person value seen in the conversation is refused (§4.5). */
|
|
391
|
+
private readonly leakCheck;
|
|
392
|
+
/** A person's entered values are sealed in the decision's transaction (§4.3). */
|
|
393
|
+
private readonly sealed;
|
|
355
394
|
private readonly permissionLocks?;
|
|
356
395
|
private readonly permissionGrants?;
|
|
357
396
|
constructor(db: KernelDb, config: Pick<KernelConfigService, "get">, eventLog: Pick<EventLog, "appendIn" | "notify">, events: Pick<KernelEvents, "emit">, txHost: TransactionHost, schedules: Pick<ScheduledTaskStore, "addGrant">,
|
|
@@ -362,7 +401,11 @@ export declare class ApprovalsService {
|
|
|
362
401
|
/** A trial run's 「这次允许」 becomes a task grant only within the org's egress. */
|
|
363
402
|
sandbox: Pick<SandboxService, "ceilingLimits">,
|
|
364
403
|
/** A decision resumes its waiting run in the decision's transaction. */
|
|
365
|
-
resumes: Pick<RunResume, "resumeIn">,
|
|
404
|
+
resumes: Pick<RunResume, "resumeIn">,
|
|
405
|
+
/** An entered person value seen in the conversation is refused (§4.5). */
|
|
406
|
+
leakCheck: Pick<ConversationLeakCheck, "leaked">,
|
|
407
|
+
/** A person's entered values are sealed in the decision's transaction (§4.3). */
|
|
408
|
+
sealed: Pick<SealedInputs, "sealIn">, permissionLocks?: PermissionAuthorityLock | undefined, permissionGrants?: PermissionCallGrants | undefined);
|
|
366
409
|
/**
|
|
367
410
|
* 新建 pending 审批(`expires_at` = 现在 + `tools.approvalTimeoutHours`)并写 `approval_requested`,同一事务。
|
|
368
411
|
* 按 (run_id, call_id, attempt) 幂等:已存在则返回已有记录(不覆盖),事件按 `approval_requested:<approvalId>` 幂等。
|
|
@@ -442,6 +485,12 @@ export declare class ApprovalsService {
|
|
|
442
485
|
/** An account's display name (null when unknown). */
|
|
443
486
|
private displayNameOf;
|
|
444
487
|
private decideIn;
|
|
488
|
+
/**
|
|
489
|
+
* A tool ask's decision in its own transaction. With person fields, a leak refusal commits the
|
|
490
|
+
* leaked paths in `detail.leakedFields` and returns them (the caller refuses after the commit,
|
|
491
|
+
* the approval still pending); the same transaction cannot both keep that write and throw.
|
|
492
|
+
*/
|
|
493
|
+
private decideToolIn;
|
|
445
494
|
/**
|
|
446
495
|
* A decision in the caller's transaction: lock order conversation → approval → run (the resume).
|
|
447
496
|
* Refuses (`approval.already_decided`, `approval.not_found`, …) before any write.
|
|
@@ -14,6 +14,7 @@ import { sql } from "kysely";
|
|
|
14
14
|
import { normalizeStoredDefinition } from "../agents/definition.js";
|
|
15
15
|
import { EventLog } from "../conversations/event-log.js";
|
|
16
16
|
import { COMMAND_ASK_TEXT_MAX_CHARS, GIT_ASK_MAX_REFS, GIT_REF_MAX_CHARS, NETWORK_ASK_MAX_HOSTS, NETWORK_HOST_MAX_CHARS, } from "../conversations/events.js";
|
|
17
|
+
import { ConversationLeakCheck } from "../conversations/leak-check.js";
|
|
17
18
|
import { conversationSourceOf } from "../conversations/source.js";
|
|
18
19
|
import { ApiError } from "../errors.js";
|
|
19
20
|
import { clipText } from "../extensions/hooks.js";
|
|
@@ -27,6 +28,8 @@ import { KernelEvents } from "../infra/events/kernel-events.js";
|
|
|
27
28
|
import { LockService } from "../infra/lock/lock.service.js";
|
|
28
29
|
import { WithLock } from "../infra/lock/with-lock.decorator.js";
|
|
29
30
|
import { ORG_ROLES, roleAtLeast } from "../orgs/roles.js";
|
|
31
|
+
import { checkPersonValue } from "../platform/person-fields.js";
|
|
32
|
+
import { SealedInputs } from "../platform/sealed-inputs.js";
|
|
30
33
|
import { RunResume } from "../runtime/run-jobs.js";
|
|
31
34
|
import { rootConversationOf } from "../runtime/subagents/child.js";
|
|
32
35
|
import { PermissionAuthorityLock } from "../sandbox/permission-authority-lock.js";
|
|
@@ -69,6 +72,21 @@ export function publicRecord(r) {
|
|
|
69
72
|
git: strip(r.git),
|
|
70
73
|
};
|
|
71
74
|
}
|
|
75
|
+
/** A tool ask's person fields from its stored `detail` (none: an ordinary tool ask). */
|
|
76
|
+
function personFieldsOf(detail) {
|
|
77
|
+
if (typeof detail !== "object" || detail === null)
|
|
78
|
+
return null;
|
|
79
|
+
const d = detail;
|
|
80
|
+
if (!Array.isArray(d.personFields) || d.personFields.length === 0 || d.target === undefined) {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
personFields: d.personFields,
|
|
85
|
+
personTarget: d.target,
|
|
86
|
+
...(Array.isArray(d.leakedFields) &&
|
|
87
|
+
d.leakedFields.length > 0 && { leakedFields: d.leakedFields }),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
72
90
|
/** The tool key a network ask is recorded under (the command ran in `Bash`). */
|
|
73
91
|
export const NETWORK_ASK_TOOL_KEY = "Bash";
|
|
74
92
|
/** The spec hash of a network ask (no tool definition is approved). */
|
|
@@ -95,6 +113,7 @@ const commandAskToolKey = (kind) => kind === "write"
|
|
|
95
113
|
? COMMAND_READ_TOOL_KEY
|
|
96
114
|
: COMMAND_ASK_TOOL_KEY;
|
|
97
115
|
const notFound = () => new ApiError("approval.not_found", "Approval not found");
|
|
116
|
+
const inputsNotExpected = () => new ApiError("approval.inputs_not_expected", "The inputs name fields this approval does not ask for.");
|
|
98
117
|
const iso = (v) => new Date(v).toISOString();
|
|
99
118
|
const isRole = (v) => v !== null && ORG_ROLES.includes(v);
|
|
100
119
|
/** The scopes of an approval without stored ones: a role escalation is approved only once. */
|
|
@@ -111,6 +130,7 @@ function toRecord(r) {
|
|
|
111
130
|
detail: r.kind === "network" ? (r.detail ?? null) : null,
|
|
112
131
|
git: r.kind === "git" ? (r.detail ?? null) : null,
|
|
113
132
|
command: r.kind === "command" ? (r.detail ?? null) : null,
|
|
133
|
+
...(r.kind === "tool" && personFieldsOf(r.detail)),
|
|
114
134
|
toolKey: r.tool_key,
|
|
115
135
|
specHash: r.spec_hash,
|
|
116
136
|
connectorId: r.connector_id,
|
|
@@ -166,6 +186,8 @@ let ApprovalsService = class ApprovalsService {
|
|
|
166
186
|
lockService;
|
|
167
187
|
sandbox;
|
|
168
188
|
resumes;
|
|
189
|
+
leakCheck;
|
|
190
|
+
sealed;
|
|
169
191
|
permissionLocks;
|
|
170
192
|
permissionGrants;
|
|
171
193
|
constructor(db, config, eventLog, events, txHost, schedules,
|
|
@@ -176,7 +198,11 @@ let ApprovalsService = class ApprovalsService {
|
|
|
176
198
|
/** A trial run's 「这次允许」 becomes a task grant only within the org's egress. */
|
|
177
199
|
sandbox,
|
|
178
200
|
/** A decision resumes its waiting run in the decision's transaction. */
|
|
179
|
-
resumes,
|
|
201
|
+
resumes,
|
|
202
|
+
/** An entered person value seen in the conversation is refused (§4.5). */
|
|
203
|
+
leakCheck,
|
|
204
|
+
/** A person's entered values are sealed in the decision's transaction (§4.3). */
|
|
205
|
+
sealed, permissionLocks, permissionGrants) {
|
|
180
206
|
this.db = db;
|
|
181
207
|
this.config = config;
|
|
182
208
|
this.eventLog = eventLog;
|
|
@@ -187,6 +213,8 @@ let ApprovalsService = class ApprovalsService {
|
|
|
187
213
|
this.lockService = lockService;
|
|
188
214
|
this.sandbox = sandbox;
|
|
189
215
|
this.resumes = resumes;
|
|
216
|
+
this.leakCheck = leakCheck;
|
|
217
|
+
this.sealed = sealed;
|
|
190
218
|
this.permissionLocks = permissionLocks;
|
|
191
219
|
this.permissionGrants = permissionGrants;
|
|
192
220
|
}
|
|
@@ -231,6 +259,7 @@ let ApprovalsService = class ApprovalsService {
|
|
|
231
259
|
approver_role: input.approverRole,
|
|
232
260
|
summary: input.summary === undefined ? null : JSON.stringify(input.summary),
|
|
233
261
|
scopes: input.scopes ?? null,
|
|
262
|
+
...(input.detail !== undefined && { detail: JSON.stringify(input.detail) }),
|
|
234
263
|
scope: null,
|
|
235
264
|
decided_by: null,
|
|
236
265
|
expires_at: sql `now() + ${hours}::integer * interval '1 hour'`,
|
|
@@ -692,9 +721,13 @@ let ApprovalsService = class ApprovalsService {
|
|
|
692
721
|
if (kind.kind === "command") {
|
|
693
722
|
return this.decideCommand(orgId, id, accountId, body, actorTokenId, via);
|
|
694
723
|
}
|
|
695
|
-
const
|
|
696
|
-
|
|
697
|
-
|
|
724
|
+
const outcome = await this.decideToolIn(orgId, id, accountId, body, actorTokenId, via);
|
|
725
|
+
// a leak refusal committed only the leaked paths (the approval stays pending), then refuses
|
|
726
|
+
if ("leaked" in outcome) {
|
|
727
|
+
throw new ApiError("approval.input_leaked", "This value appeared in the conversation, so it counts as leaked. Create a new one at the provider and enter that.", [{ fields: outcome.leaked }]);
|
|
728
|
+
}
|
|
729
|
+
await this.announceDecided(outcome.record, outcome.toSeq);
|
|
730
|
+
return outcome.record;
|
|
698
731
|
}
|
|
699
732
|
/**
|
|
700
733
|
* A network ask's decision (network ask-to-allow spec §5.2), one at a time per ask across
|
|
@@ -919,13 +952,26 @@ let ApprovalsService = class ApprovalsService {
|
|
|
919
952
|
// its own transaction (as the legacy one was): `approval.decided` is emitted only after the
|
|
920
953
|
// decision committed, even when a caller has a transaction open
|
|
921
954
|
decideIn(orgId, id, accountId, body, actorTokenId, network, git, command) {
|
|
922
|
-
return this.decideTx(orgId, id, accountId, body, actorTokenId, network, git, command)
|
|
955
|
+
return this.decideTx(orgId, id, accountId, body, actorTokenId, network, git, command).then((o) => {
|
|
956
|
+
// only a tool ask takes inputs (refused for every other kind before)
|
|
957
|
+
if ("leaked" in o)
|
|
958
|
+
throw new Error(`approval ${id}: a leak outcome outside a tool ask`);
|
|
959
|
+
return o;
|
|
960
|
+
});
|
|
961
|
+
}
|
|
962
|
+
/**
|
|
963
|
+
* A tool ask's decision in its own transaction. With person fields, a leak refusal commits the
|
|
964
|
+
* leaked paths in `detail.leakedFields` and returns them (the caller refuses after the commit,
|
|
965
|
+
* the approval still pending); the same transaction cannot both keep that write and throw.
|
|
966
|
+
*/
|
|
967
|
+
decideToolIn(orgId, id, accountId, body, actorTokenId, via) {
|
|
968
|
+
return this.decideTx(orgId, id, accountId, body, actorTokenId, undefined, undefined, undefined, via);
|
|
923
969
|
}
|
|
924
970
|
/**
|
|
925
971
|
* A decision in the caller's transaction: lock order conversation → approval → run (the resume).
|
|
926
972
|
* Refuses (`approval.already_decided`, `approval.not_found`, …) before any write.
|
|
927
973
|
*/
|
|
928
|
-
async decideTx(orgId, id, accountId, body, actorTokenId, network, git, command) {
|
|
974
|
+
async decideTx(orgId, id, accountId, body, actorTokenId, network, git, command, via = "web") {
|
|
929
975
|
const t = this.db.db;
|
|
930
976
|
// lock order: the conversation, the approval, then the run (as a stop of the waiting run)
|
|
931
977
|
if (!(await this.lockConversationOf(id, orgId)))
|
|
@@ -973,6 +1019,61 @@ let ApprovalsService = class ApprovalsService {
|
|
|
973
1019
|
await this.permissionGrants.assertCurrent(before.command.permissionCandidate.binding);
|
|
974
1020
|
}
|
|
975
1021
|
const approved = body.decision === "approve";
|
|
1022
|
+
const person = before.kind === "tool" ? before.personFields : undefined;
|
|
1023
|
+
if (body.inputs !== undefined && (person === undefined || !approved)) {
|
|
1024
|
+
throw inputsNotExpected();
|
|
1025
|
+
}
|
|
1026
|
+
// an approval with person fields (platform web parity spec §4.2, §4.3, R5): approved only by
|
|
1027
|
+
// its person, in a web session, with every asked value, valid and never seen in the
|
|
1028
|
+
// conversation; the values are sealed before the status changes. A deny takes no values.
|
|
1029
|
+
let inputsProvided;
|
|
1030
|
+
if (person !== undefined && approved) {
|
|
1031
|
+
if (actorTokenId !== undefined) {
|
|
1032
|
+
throw new ApiError("auth.session_required", "Values for this approval are entered in a signed-in web session, not with a personal access token.");
|
|
1033
|
+
}
|
|
1034
|
+
if (via === "im") {
|
|
1035
|
+
throw new ApiError("auth.session_required", "This approval asks for values entered on the web; it cannot be approved from IM.");
|
|
1036
|
+
}
|
|
1037
|
+
if (before.requestedFor !== accountId)
|
|
1038
|
+
throw notFound();
|
|
1039
|
+
const inputs = body.inputs ?? {};
|
|
1040
|
+
const asked = new Set(person.map((f) => f.path));
|
|
1041
|
+
if (Object.keys(inputs).some((p) => !asked.has(p)))
|
|
1042
|
+
throw inputsNotExpected();
|
|
1043
|
+
const problems = person.flatMap((f) => {
|
|
1044
|
+
const v = Object.hasOwn(inputs, f.path) ? inputs[f.path] : undefined;
|
|
1045
|
+
const problem = v === undefined ? `${f.label} is required.` : checkPersonValue(f, v);
|
|
1046
|
+
return problem === null ? [] : [{ path: f.path, problem }];
|
|
1047
|
+
});
|
|
1048
|
+
if (problems.length > 0) {
|
|
1049
|
+
throw new ApiError("approval.inputs_invalid", "Some entered values are not valid.", [
|
|
1050
|
+
{ fields: problems },
|
|
1051
|
+
]);
|
|
1052
|
+
}
|
|
1053
|
+
// a read in the decision's transaction: one indexed scan of the conversation tree per value
|
|
1054
|
+
const leaked = await this.leakCheck.leaked(before.conversationId, inputs);
|
|
1055
|
+
if (leaked.length > 0) {
|
|
1056
|
+
await t
|
|
1057
|
+
.updateTable("approvals")
|
|
1058
|
+
.set({
|
|
1059
|
+
detail: JSON.stringify({
|
|
1060
|
+
...locked.detail,
|
|
1061
|
+
leakedFields: [...new Set([...(before.leakedFields ?? []), ...leaked])],
|
|
1062
|
+
}),
|
|
1063
|
+
})
|
|
1064
|
+
.where("id", "=", id)
|
|
1065
|
+
.execute();
|
|
1066
|
+
return { leaked };
|
|
1067
|
+
}
|
|
1068
|
+
await this.sealed.sealIn({
|
|
1069
|
+
approvalId: id,
|
|
1070
|
+
recipientAccountId: accountId,
|
|
1071
|
+
conversationId: before.conversationId,
|
|
1072
|
+
runId: before.runId,
|
|
1073
|
+
callId: before.callId,
|
|
1074
|
+
}, inputs);
|
|
1075
|
+
inputsProvided = person.map((f) => f.path);
|
|
1076
|
+
}
|
|
976
1077
|
const reason = approved ? undefined : body.reason?.trim() || undefined;
|
|
977
1078
|
const scope = !approved
|
|
978
1079
|
? null
|
|
@@ -1111,6 +1212,7 @@ let ApprovalsService = class ApprovalsService {
|
|
|
1111
1212
|
...(scope !== null && { scope }),
|
|
1112
1213
|
byAccountId: accountId,
|
|
1113
1214
|
...(reason !== undefined && { reason }),
|
|
1215
|
+
...(inputsProvided !== undefined && { inputsProvided }),
|
|
1114
1216
|
},
|
|
1115
1217
|
},
|
|
1116
1218
|
]);
|
|
@@ -1192,6 +1294,7 @@ let ApprovalsService = class ApprovalsService {
|
|
|
1192
1294
|
scope,
|
|
1193
1295
|
...(body.note !== undefined && { note: body.note }),
|
|
1194
1296
|
...(reason !== undefined && { reason }),
|
|
1297
|
+
...(inputsProvided !== undefined && { inputsProvided }),
|
|
1195
1298
|
},
|
|
1196
1299
|
});
|
|
1197
1300
|
// the run last (lock order: the decided row, then `runs`); a still-running run is resumed by
|
|
@@ -1822,6 +1925,9 @@ __decorate([
|
|
|
1822
1925
|
__decorate([
|
|
1823
1926
|
Transactional({ propagation: "requires_new" })
|
|
1824
1927
|
], ApprovalsService.prototype, "decideIn", null);
|
|
1928
|
+
__decorate([
|
|
1929
|
+
Transactional({ propagation: "requires_new" })
|
|
1930
|
+
], ApprovalsService.prototype, "decideToolIn", null);
|
|
1825
1931
|
__decorate([
|
|
1826
1932
|
Transactional({ propagation: "mandatory" })
|
|
1827
1933
|
], ApprovalsService.prototype, "decideTx", null);
|
|
@@ -1864,9 +1970,11 @@ ApprovalsService = __decorate([
|
|
|
1864
1970
|
__param(7, Inject(LockService)),
|
|
1865
1971
|
__param(8, Inject(SandboxService)),
|
|
1866
1972
|
__param(9, Inject(RunResume)),
|
|
1867
|
-
__param(10,
|
|
1868
|
-
__param(
|
|
1869
|
-
__param(
|
|
1870
|
-
__param(
|
|
1973
|
+
__param(10, Inject(ConversationLeakCheck)),
|
|
1974
|
+
__param(11, Inject(SealedInputs)),
|
|
1975
|
+
__param(12, Optional()),
|
|
1976
|
+
__param(12, Inject(PermissionAuthorityLock)),
|
|
1977
|
+
__param(13, Optional()),
|
|
1978
|
+
__param(13, Inject(PermissionCallGrants))
|
|
1871
1979
|
], ApprovalsService);
|
|
1872
1980
|
export { ApprovalsService };
|
|
@@ -13,6 +13,7 @@ import { ModelPurposesService } from "../../models/purposes.js";
|
|
|
13
13
|
import { PlatformActor } from "../../platform/actor.js";
|
|
14
14
|
import { AgentDocs } from "../../platform/agent-docs.js";
|
|
15
15
|
import { PlatformOperations } from "../../platform/registry.js";
|
|
16
|
+
import { SealedInputs } from "../../platform/sealed-inputs.js";
|
|
16
17
|
import type { HookRunner } from "../../runtime/hook-calls.js";
|
|
17
18
|
import { ProviderRegistry } from "../../runtime/providers.js";
|
|
18
19
|
import { StepLog } from "../../runtime/step-log.js";
|
|
@@ -38,6 +39,8 @@ export declare class BuiltinTools {
|
|
|
38
39
|
purposes: ModelPurposesService, host: {
|
|
39
40
|
readonly hooks?: HookRunner;
|
|
40
41
|
} | undefined, stepLog: StepLog, log: Logger, background: BackgroundTaskStore, finisher: BackgroundTaskFinisher, stopper: BackgroundTaskStopper, platformOps: PlatformOperations, platformActor: PlatformActor, agentDocs: AgentDocs,
|
|
42
|
+
/** The person's sealed approval card values a Platform write with person fields reads. */
|
|
43
|
+
sealedInputs: SealedInputs,
|
|
41
44
|
/** The schedule tools read tasks and grants; their writes go through the task service. */
|
|
42
45
|
scheduleStore: ScheduledTaskStore,
|
|
43
46
|
/** `ScheduleUpdate`'s write: its field change and grant changes in one transaction. */
|
|
@@ -28,6 +28,7 @@ import { readOrgSettings } from "../../orgs/settings.service.js";
|
|
|
28
28
|
import { PlatformActor } from "../../platform/actor.js";
|
|
29
29
|
import { AgentDocs } from "../../platform/agent-docs.js";
|
|
30
30
|
import { PlatformOperations } from "../../platform/registry.js";
|
|
31
|
+
import { SealedInputs } from "../../platform/sealed-inputs.js";
|
|
31
32
|
import { ProviderListError, ProviderRegistry } from "../../runtime/providers.js";
|
|
32
33
|
import { conversationTimeZone, RUN_CONTEXT_CLOCK } from "../../runtime/run-context.service.js";
|
|
33
34
|
import { StepLog } from "../../runtime/step-log.js";
|
|
@@ -61,6 +62,8 @@ let BuiltinTools = class BuiltinTools {
|
|
|
61
62
|
constructor(db, config, outputs, storage, attachments, eventLog, sandbox, devices, points, webPages,
|
|
62
63
|
/** The WebFetch extraction call: the `web_fetch` purpose, its hooks, usage and denials. */
|
|
63
64
|
purposes, host, stepLog, log, background, finisher, stopper, platformOps, platformActor, agentDocs,
|
|
65
|
+
/** The person's sealed approval card values a Platform write with person fields reads. */
|
|
66
|
+
sealedInputs,
|
|
64
67
|
/** The schedule tools read tasks and grants; their writes go through the task service. */
|
|
65
68
|
scheduleStore,
|
|
66
69
|
/** `ScheduleUpdate`'s write: its field change and grant changes in one transaction. */
|
|
@@ -165,6 +168,24 @@ let BuiltinTools = class BuiltinTools {
|
|
|
165
168
|
docs: agentDocs,
|
|
166
169
|
setting: async (orgId) => (await readOrgSettings(db.db, orgId)).platformTool,
|
|
167
170
|
log,
|
|
171
|
+
sealed: sealedInputs,
|
|
172
|
+
approvalPersonPaths: async (approvalId, call) => {
|
|
173
|
+
// only this call's own approved approval in this org (anything else: none, fails closed)
|
|
174
|
+
const row = await db.db
|
|
175
|
+
.selectFrom("approvals")
|
|
176
|
+
.select(["kind", "detail"])
|
|
177
|
+
.where("id", "=", approvalId)
|
|
178
|
+
.where("org_id", "=", call.orgId)
|
|
179
|
+
.where("run_id", "=", call.runId)
|
|
180
|
+
.where("call_id", "=", call.callId)
|
|
181
|
+
.where("status", "=", "approved")
|
|
182
|
+
.executeTakeFirst();
|
|
183
|
+
const fields = row?.detail
|
|
184
|
+
?.personFields;
|
|
185
|
+
if (row?.kind !== "tool" || !Array.isArray(fields) || fields.length === 0)
|
|
186
|
+
return null;
|
|
187
|
+
return fields.map((f) => String(f.path));
|
|
188
|
+
},
|
|
168
189
|
}),
|
|
169
190
|
// the clock in the run context's time zone (the run context carries only the date)
|
|
170
191
|
currentTimeTool({
|
|
@@ -210,16 +231,17 @@ BuiltinTools = __decorate([
|
|
|
210
231
|
__param(17, Inject(PlatformOperations)),
|
|
211
232
|
__param(18, Inject(PlatformActor)),
|
|
212
233
|
__param(19, Inject(AgentDocs)),
|
|
213
|
-
__param(20, Inject(
|
|
214
|
-
__param(21, Inject(
|
|
215
|
-
__param(22, Inject(
|
|
216
|
-
__param(23, Inject(
|
|
217
|
-
__param(24, Inject(
|
|
218
|
-
__param(25, Inject(
|
|
219
|
-
__param(26,
|
|
220
|
-
__param(26, Inject(RUN_CONTEXT_CLOCK)),
|
|
234
|
+
__param(20, Inject(SealedInputs)),
|
|
235
|
+
__param(21, Inject(ScheduledTaskStore)),
|
|
236
|
+
__param(22, Inject(ScheduleUpdates)),
|
|
237
|
+
__param(23, Inject(ScheduleToolsPort)),
|
|
238
|
+
__param(24, Inject(ModelImages)),
|
|
239
|
+
__param(25, Inject(ProviderRegistry)),
|
|
240
|
+
__param(26, Inject(ManagedDeviceContextRecorder)),
|
|
221
241
|
__param(27, Optional()),
|
|
222
|
-
__param(27, Inject(
|
|
242
|
+
__param(27, Inject(RUN_CONTEXT_CLOCK)),
|
|
243
|
+
__param(28, Optional()),
|
|
244
|
+
__param(28, Inject(PERMISSION_CONTEXT_ACTIVATION))
|
|
223
245
|
], BuiltinTools);
|
|
224
246
|
export { BuiltinTools };
|
|
225
247
|
/**
|