@capybara-hq/kernel 0.1.0-rc.32 → 0.1.0-rc.34
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/catalog/marketplaces.service.d.ts +1 -1
- package/dist/catalog/requests.service.d.ts +8 -6
- package/dist/catalog/requests.service.js +52 -29
- package/dist/channels/inbound.d.ts +0 -25
- package/dist/channels/inbound.js +26 -1
- package/dist/channels/phrases.d.ts +28 -0
- package/dist/channels/phrases.js +28 -0
- package/dist/config/schema.d.ts +4 -0
- package/dist/config/schema.js +7 -1
- package/dist/conversations/conversations.controller.d.ts +4 -1
- package/dist/conversations/conversations.controller.js +14 -2
- package/dist/conversations/conversations.service.d.ts +43 -3
- package/dist/conversations/conversations.service.js +93 -19
- package/dist/conversations/event-log.d.ts +3 -3
- package/dist/conversations/event-log.js +3 -3
- package/dist/conversations/events.d.ts +24 -0
- package/dist/conversations/events.js +69 -0
- package/dist/db/kernel-schema.d.ts +20 -0
- package/dist/devices/conversation-devices.d.ts +57 -3
- package/dist/devices/conversation-devices.js +63 -11
- package/dist/devices/device-projects.service.d.ts +162 -0
- package/dist/devices/device-projects.service.js +503 -0
- package/dist/devices/devices.controller.d.ts +15 -2
- package/dist/devices/devices.controller.js +60 -3
- package/dist/devices/devices.module.js +15 -2
- package/dist/devices/devices.schemas.js +9 -1
- package/dist/devices/devices.service.js +8 -4
- package/dist/devices/dto.d.ts +31 -0
- package/dist/devices/dto.js +38 -1
- package/dist/devices/working-root.d.ts +41 -0
- package/dist/devices/working-root.js +50 -0
- package/dist/errors.d.ts +39 -0
- package/dist/errors.js +39 -0
- package/dist/evals/run-replay.d.ts +9 -1
- package/dist/evals/run-replay.js +15 -1
- package/dist/local-devices/local-device-provider.d.ts +37 -1
- package/dist/local-devices/local-device-provider.js +33 -0
- package/dist/local-devices/machines.d.ts +2 -1
- package/dist/local-devices/machines.js +4 -1
- package/dist/local-devices/network-grants.d.ts +1 -1
- package/dist/local-devices/network-grants.js +2 -3
- package/dist/local-devices/policy-summary.js +1 -0
- package/dist/local-devices/relay.d.ts +2 -0
- package/dist/local-devices/relay.js +3 -1
- package/dist/local-devices/targets.d.ts +6 -1
- package/dist/local-devices/targets.js +14 -0
- package/dist/orgs/settings.controller.d.ts +3 -1
- package/dist/orgs/settings.controller.js +7 -3
- package/dist/orgs/settings.service.d.ts +78 -31
- package/dist/orgs/settings.service.js +109 -52
- package/dist/overrides.d.ts +4 -0
- package/dist/overrides.js +2 -0
- package/dist/platform/ops/agent-skills.ops.js +2 -1
- package/dist/platform/ops/devices.ops.d.ts +3 -1
- package/dist/platform/ops/devices.ops.js +136 -3
- package/dist/platform/ops/org-settings.ops.js +20 -10
- package/dist/runtime/context/build.d.ts +3 -0
- package/dist/runtime/context/build.js +14 -0
- package/dist/runtime/context/compaction.js +2 -0
- package/dist/runtime/context/project-root.d.ts +43 -0
- package/dist/runtime/context/project-root.js +85 -0
- package/dist/runtime/model-call.js +25 -1
- package/dist/runtime/project/knowledge.d.ts +129 -0
- package/dist/runtime/project/knowledge.js +248 -0
- package/dist/runtime/project/knowledge.service.d.ts +85 -0
- package/dist/runtime/project/knowledge.service.js +288 -0
- package/dist/runtime/run-worker.d.ts +5 -0
- package/dist/runtime/run-worker.js +27 -6
- package/dist/runtime/runtime.module.js +7 -0
- package/dist/sandbox/modes.d.ts +31 -19
- package/dist/sandbox/modes.js +43 -24
- package/dist/sandbox/review/review.service.d.ts +14 -2
- package/dist/sandbox/review/review.service.js +2 -0
- package/dist/sandbox/review/rules.d.ts +6 -4
- package/dist/sandbox/review/rules.js +185 -62
- package/dist/sandbox/review/types.d.ts +14 -0
- package/dist/sandbox/sandbox.service.d.ts +20 -2
- package/dist/sandbox/sandbox.service.js +86 -3
- package/dist/schedules/grant-rules.d.ts +2 -0
- package/dist/schedules/grant-rules.js +3 -1
- package/dist/schedules/schedule-math.js +21 -2
- package/dist/schedules/scheduled-tasks.service.js +3 -1
- package/dist/skills/catalog.d.ts +6 -1
- package/dist/skills/catalog.js +18 -4
- package/dist/skills/invoke.d.ts +64 -2
- package/dist/skills/invoke.js +214 -6
- package/dist/skills/skills.module.js +5 -0
- package/dist/tools/approvals.service.d.ts +6 -0
- package/dist/tools/builtins/sandbox/bash.js +19 -4
- package/dist/tools/builtins/sandbox/common.d.ts +36 -6
- package/dist/tools/builtins/sandbox/common.js +69 -8
- package/dist/tools/builtins/sandbox/edit.js +2 -2
- package/dist/tools/builtins/sandbox/glob.js +3 -2
- package/dist/tools/builtins/sandbox/grep.js +2 -1
- package/dist/tools/builtins/sandbox/read.js +3 -3
- package/dist/tools/builtins/sandbox/write.js +2 -2
- package/dist/tools/builtins/skill-load.d.ts +47 -2
- package/dist/tools/builtins/skill-load.js +160 -2
- package/dist/tools/network-asks.d.ts +7 -0
- package/dist/tools/network-asks.js +11 -1
- package/dist/tools/pipeline.d.ts +1 -1
- package/dist/tools/pipeline.js +60 -9
- package/dist/tools/registry.d.ts +6 -0
- package/package.json +2 -2
- package/src/migrations/0133_project_root.sql +30 -0
- package/src/migrations/0134_org_cloud_network_delta.sql +38 -0
|
@@ -15,9 +15,11 @@ import { KernelDb } from "../infra/database/kernel-db.js";
|
|
|
15
15
|
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
16
16
|
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
17
17
|
import { isInternalProvider, SandboxProviders } from "../sandbox/providers.js";
|
|
18
|
+
import { homeOf } from "../sandbox/review/rules.js";
|
|
18
19
|
import { deviceNotFound, sandboxUnavailable } from "../sandbox/sandbox.service.js";
|
|
19
20
|
import { DeviceAccess } from "./access.js";
|
|
20
21
|
import { DevicesService } from "./devices.service.js";
|
|
22
|
+
import { workingRootOf } from "./working-root.js";
|
|
21
23
|
/** `details[0].reason` of the error an anonymous IM conversation's sandbox call gets. */
|
|
22
24
|
export const ANONYMOUS_NO_DEVICE = "anonymous";
|
|
23
25
|
/** Anonymous IM senders have no device (spec §4.2 item 8): their sandbox tools are off. */
|
|
@@ -89,21 +91,68 @@ let ConversationDevices = class ConversationDevices {
|
|
|
89
91
|
]);
|
|
90
92
|
if (device === undefined)
|
|
91
93
|
return null;
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
? null
|
|
95
|
-
: await this.recordedWorkspace(rootConversationId)) ??
|
|
96
|
-
(await this.machineWorkspace(deviceId)) ??
|
|
97
|
-
DEFAULT_SANDBOX_ROOTS.workspace;
|
|
94
|
+
const mode = level?.mode ?? "sandbox";
|
|
95
|
+
const { workspace, home, project } = await this.workingFacts(orgId, rootConversationId, conversationId, deviceId);
|
|
98
96
|
return {
|
|
99
97
|
deviceId,
|
|
100
98
|
deviceName: device.name ?? deviceId,
|
|
101
99
|
ownerAccountId: conv.account_id,
|
|
102
|
-
mode
|
|
100
|
+
mode,
|
|
103
101
|
allowedMode: level?.allowedMode ?? "sandbox",
|
|
104
102
|
workspace,
|
|
103
|
+
home,
|
|
104
|
+
deviceHome: homeOf(workspace),
|
|
105
|
+
workingRoot: workingRootOf(project, { mode, workspace, home }),
|
|
105
106
|
};
|
|
106
107
|
}
|
|
108
|
+
/**
|
|
109
|
+
* The {@link WorkingFacts} of a call in `deviceId` by `conversationId` of the tree rooted at
|
|
110
|
+
* `rootConversationId`: the one source of the workspace, home and project snapshot that the
|
|
111
|
+
* sandbox tools and the command review decide the working root from.
|
|
112
|
+
*/
|
|
113
|
+
async workingFacts(orgId, rootConversationId, conversationId, deviceId) {
|
|
114
|
+
const [recorded, recordedRoot, machine, project] = await Promise.all([
|
|
115
|
+
this.recordedWorkspace(conversationId),
|
|
116
|
+
conversationId === rootConversationId ? null : this.recordedWorkspace(rootConversationId),
|
|
117
|
+
this.machineRoots(deviceId),
|
|
118
|
+
this.workingProject(orgId, rootConversationId),
|
|
119
|
+
]);
|
|
120
|
+
const workspace = recorded ?? recordedRoot ?? machine?.workspace ?? DEFAULT_SANDBOX_ROOTS.workspace;
|
|
121
|
+
// never from a project root: the machine's home, else the device workspace's home prefix
|
|
122
|
+
const reported = machine?.home;
|
|
123
|
+
const home = reported?.startsWith("/") ? reported : homeOf(workspace);
|
|
124
|
+
return { workspace, home, project };
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* The project snapshot that decides the working root of the conversation tree rooted at
|
|
128
|
+
* `rootConversationId` (project root spec §4.3): its latest `project_root` event, read only when
|
|
129
|
+
* the conversation is bound to a project (a free conversation reads one row by its key). Null:
|
|
130
|
+
* free, or bound with no snapshot recorded yet.
|
|
131
|
+
*/
|
|
132
|
+
async workingProject(orgId, rootConversationId) {
|
|
133
|
+
const conv = await this.db
|
|
134
|
+
.selectFrom("conversations")
|
|
135
|
+
.select("project_path")
|
|
136
|
+
.where("id", "=", rootConversationId)
|
|
137
|
+
.where("org_id", "=", orgId)
|
|
138
|
+
.executeTakeFirst();
|
|
139
|
+
if (conv?.project_path == null)
|
|
140
|
+
return null;
|
|
141
|
+
const row = await this.db
|
|
142
|
+
.selectFrom("conversation_events")
|
|
143
|
+
.select([
|
|
144
|
+
sql `payload->>'root'`.as("root"),
|
|
145
|
+
sql `payload ? 'unavailable'`.as("unavailable"),
|
|
146
|
+
])
|
|
147
|
+
.where("conversation_id", "=", rootConversationId)
|
|
148
|
+
.where("type", "=", "project_root")
|
|
149
|
+
.orderBy("seq", "desc")
|
|
150
|
+
.limit(1)
|
|
151
|
+
.executeTakeFirst();
|
|
152
|
+
if (row === undefined || typeof row.root !== "string")
|
|
153
|
+
return null;
|
|
154
|
+
return { root: row.root, unavailable: row.unavailable !== false };
|
|
155
|
+
}
|
|
107
156
|
/** The workspace root of a conversation's latest `device_paths` event, or null. */
|
|
108
157
|
async recordedWorkspace(conversationId) {
|
|
109
158
|
const row = await this.db
|
|
@@ -116,15 +165,18 @@ let ConversationDevices = class ConversationDevices {
|
|
|
116
165
|
.executeTakeFirst();
|
|
117
166
|
return row?.workspace ?? null;
|
|
118
167
|
}
|
|
119
|
-
/**
|
|
120
|
-
|
|
168
|
+
/**
|
|
169
|
+
* The workspace root and the owner's home the device's active machine last reported, or null
|
|
170
|
+
* (cloud: no machine).
|
|
171
|
+
*/
|
|
172
|
+
async machineRoots(deviceId) {
|
|
121
173
|
const row = await this.db
|
|
122
174
|
.selectFrom("device_machines")
|
|
123
|
-
.select(sql `roots->>'workspace'`.as("workspace"))
|
|
175
|
+
.select([sql `roots->>'workspace'`.as("workspace"), "home"])
|
|
124
176
|
.where("device_id", "=", deviceId)
|
|
125
177
|
.where("revoked_at", "is", null)
|
|
126
178
|
.executeTakeFirst();
|
|
127
|
-
return row
|
|
179
|
+
return row === undefined ? null : { workspace: row.workspace ?? null, home: row.home ?? null };
|
|
128
180
|
}
|
|
129
181
|
/**
|
|
130
182
|
* The device a new conversation of `accountId` is bound to: the chosen one when it is
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { ApiError } from "../errors.js";
|
|
2
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
3
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
4
|
+
import type { SandboxMode } from "../sandbox/modes.js";
|
|
5
|
+
import { SandboxProviders } from "../sandbox/providers.js";
|
|
6
|
+
import { SandboxService } from "../sandbox/sandbox.service.js";
|
|
7
|
+
import { DeviceAccess } from "./access.js";
|
|
8
|
+
/** The most projects one device keeps (project root §4.1). */
|
|
9
|
+
export declare const MAX_DEVICE_PROJECTS = 100;
|
|
10
|
+
/** A project as the web and the Platform tool see it. */
|
|
11
|
+
export interface DeviceProjectView {
|
|
12
|
+
name: string;
|
|
13
|
+
/** The absolute path on the device. */
|
|
14
|
+
path: string;
|
|
15
|
+
createdAt: string;
|
|
16
|
+
}
|
|
17
|
+
/** `GET /devices/:id/projects`. */
|
|
18
|
+
export interface DeviceProjectList {
|
|
19
|
+
/** The projects the device's current mode can read (the others are kept, only hidden). */
|
|
20
|
+
projects: DeviceProjectView[];
|
|
21
|
+
/** How many stored projects the current mode hides. */
|
|
22
|
+
hidden: number;
|
|
23
|
+
/** The mode the device runs at now. */
|
|
24
|
+
mode: SandboxMode;
|
|
25
|
+
/**
|
|
26
|
+
* The base a relative path is resolved against: the workspace in sandbox mode and on a cloud
|
|
27
|
+
* device, the owner's home in auto mode; null when that home is not known.
|
|
28
|
+
*/
|
|
29
|
+
base: string | null;
|
|
30
|
+
/** The device offers projects (a machine announcing `projects`, a provider with list / scan). */
|
|
31
|
+
supported: boolean;
|
|
32
|
+
}
|
|
33
|
+
/** One suggestion of `GET /devices/:id/projects/candidates`. */
|
|
34
|
+
export interface DeviceProjectCandidateView {
|
|
35
|
+
/** The absolute path on the device. */
|
|
36
|
+
path: string;
|
|
37
|
+
/** `path` relative to `base` (empty for the base itself), what the add form takes. */
|
|
38
|
+
relativePath: string;
|
|
39
|
+
suggestedName: string;
|
|
40
|
+
}
|
|
41
|
+
/** `GET /devices/:id/projects/candidates`. */
|
|
42
|
+
export interface DeviceProjectCandidates {
|
|
43
|
+
candidates: DeviceProjectCandidateView[];
|
|
44
|
+
mode: SandboxMode;
|
|
45
|
+
base: string | null;
|
|
46
|
+
supported: boolean;
|
|
47
|
+
}
|
|
48
|
+
/** What a project's scope is read from: the device's mode now and the bases it gives. */
|
|
49
|
+
export interface DeviceProjectScope {
|
|
50
|
+
source: "cloud" | "local";
|
|
51
|
+
mode: SandboxMode;
|
|
52
|
+
/** The workspace root as last known (a machine's hello, a cloud instance's handle). */
|
|
53
|
+
workspace: string;
|
|
54
|
+
/** The owner's home a local machine reported; null when unknown (and for a cloud device). */
|
|
55
|
+
home: string | null;
|
|
56
|
+
supported: boolean;
|
|
57
|
+
/**
|
|
58
|
+
* A machine whose own policy is auto mode while the organization allows only sandbox mode: exec
|
|
59
|
+
* refuses it (`device.auto_mode_not_allowed`), and the kernel reads nothing on it unasked.
|
|
60
|
+
*/
|
|
61
|
+
widerThanAllowed: boolean;
|
|
62
|
+
}
|
|
63
|
+
type Scope = DeviceProjectScope;
|
|
64
|
+
/**
|
|
65
|
+
* `path` lies inside what the scope's mode may read (sandbox mode and cloud: the workspace, as the
|
|
66
|
+
* held instance reports it when given).
|
|
67
|
+
*/
|
|
68
|
+
export declare const withinMode: (s: Scope, path: string, workspace?: string) => boolean;
|
|
69
|
+
/**
|
|
70
|
+
* A device's projects (project root design §4.1, §4.2, §6): kept in `device_projects`, keyed by the
|
|
71
|
+
* device's id, written only here. Only the device's owner reads or changes them (anyone else gets
|
|
72
|
+
* `device.not_found`). Every project is scoped by the device's current effective mode
|
|
73
|
+
* (`SandboxService.devicePolicy`, what the command review's mode derives from): sandbox mode and a cloud device
|
|
74
|
+
* offer only the workspace, auto mode on a machine whatever its owner can read; a project outside
|
|
75
|
+
* the mode is hidden, never deleted. The device stays the authority on what it can read: adding asks
|
|
76
|
+
* it with `list`, the suggestions with `scan`, both carrying the current mode, both outside any
|
|
77
|
+
* transaction; the insert then runs in its own, with uniqueness from the primary key.
|
|
78
|
+
*/
|
|
79
|
+
export declare class DeviceProjectsService {
|
|
80
|
+
private readonly db;
|
|
81
|
+
readonly txHost: TransactionHost;
|
|
82
|
+
private readonly providers;
|
|
83
|
+
private readonly sandbox;
|
|
84
|
+
private readonly access;
|
|
85
|
+
constructor(db: KernelDb, txHost: TransactionHost, providers: Pick<SandboxProviders, "cloud" | "forName">, sandbox: Pick<SandboxService, "withSandbox" | "devicePolicy">, access: Pick<DeviceAccess, "usable">);
|
|
86
|
+
/** The projects the device's current mode can read; see {@link DeviceProjectList}. */
|
|
87
|
+
list(orgId: string, accountId: string, deviceId: string): Promise<DeviceProjectList>;
|
|
88
|
+
/**
|
|
89
|
+
* The picker's suggestions: one `scan` of the base in the current mode (never file contents).
|
|
90
|
+
* A device that offers no projects suggests none; auto mode without a known home is refused.
|
|
91
|
+
*/
|
|
92
|
+
candidates(orgId: string, accountId: string, deviceId: string, signal?: AbortSignal): Promise<DeviceProjectCandidates>;
|
|
93
|
+
/**
|
|
94
|
+
* The read-only preconditions of {@link add} known before the device is asked (the Platform
|
|
95
|
+
* operation's precheck): the owner, a device that offers projects, a valid and free name when
|
|
96
|
+
* one is given, room for one more.
|
|
97
|
+
*/
|
|
98
|
+
checkAdd(orgId: string, accountId: string, deviceId: string, name: string | undefined): Promise<void>;
|
|
99
|
+
/** {@link checkAdd}, answering the scope it read. */
|
|
100
|
+
private precheck;
|
|
101
|
+
/**
|
|
102
|
+
* Adds a project: `path` is resolved against the base of the device's current mode and must
|
|
103
|
+
* stay inside what that mode reads and not lie above the owner's home ({@link aboveHome},
|
|
104
|
+
* `device.project_path_too_broad`); `name` defaults to the directory's basename. The device is
|
|
105
|
+
* asked `list(path, { limit: 1 })` first, outside any transaction: an answer (even no entries)
|
|
106
|
+
* is a readable directory, null a missing one, an error a file, an unreadable path or one the
|
|
107
|
+
* device refused in its mode; each is a readable refusal. Then the insert, serialized per device.
|
|
108
|
+
*/
|
|
109
|
+
add(orgId: string, accountId: string, deviceId: string, input: {
|
|
110
|
+
path: string;
|
|
111
|
+
name?: string;
|
|
112
|
+
}, actorTokenId?: string, auditDetail?: Record<string, unknown>, signal?: AbortSignal): Promise<DeviceProjectView>;
|
|
113
|
+
/** The insert of {@link add}: the owner's live device locked, the count and the name checked. */
|
|
114
|
+
private insert;
|
|
115
|
+
/** Removes a project of the owner's device; removing one that is not there succeeds. */
|
|
116
|
+
remove(orgId: string, accountId: string, deviceId: string, name: string, actorTokenId?: string, auditDetail?: Record<string, unknown>): Promise<void>;
|
|
117
|
+
/**
|
|
118
|
+
* The project a new conversation of `accountId` binds to (project root §4.3): one of the
|
|
119
|
+
* owner's device's projects that its current mode can read. Refused with `device.not_found`
|
|
120
|
+
* (not the caller's device), `device.projects_unsupported`, `device.project_not_found` or
|
|
121
|
+
* `device.project_outside_mode` or `device.project_path_too_broad`. Reads only (the caller's transaction, if any); the device is
|
|
122
|
+
* not asked: it enforces its mode on every access anyway.
|
|
123
|
+
*/
|
|
124
|
+
resolveForBinding(orgId: string, deviceId: string, accountId: string, name: string): Promise<{
|
|
125
|
+
name: string;
|
|
126
|
+
path: string;
|
|
127
|
+
}>;
|
|
128
|
+
/**
|
|
129
|
+
* Whether the device still keeps the project `name` at `path` (project root §4.3): a binding is
|
|
130
|
+
* the name and the path, so a project removed, or re-added under that name with another path, no
|
|
131
|
+
* longer serves the conversations bound to it. Reads only.
|
|
132
|
+
*/
|
|
133
|
+
keeps(deviceId: string, name: string, path: string): Promise<boolean>;
|
|
134
|
+
/**
|
|
135
|
+
* The owner's live device's scope now ({@link scope}), for a run start's read of its bound
|
|
136
|
+
* project (project root §4.4): the mode to read in and whether the device offers projects.
|
|
137
|
+
* Reads only; anyone else's device (or a missing one) → `device.not_found`.
|
|
138
|
+
*/
|
|
139
|
+
scopeFor(orgId: string, accountId: string, deviceId: string): Promise<DeviceProjectScope>;
|
|
140
|
+
/**
|
|
141
|
+
* The owner's live device's scope now: its source, its effective mode
|
|
142
|
+
* (`SandboxService.devicePolicy`: the platform, the org and the device's own choice; for a cloud
|
|
143
|
+
* device never the wider mode an instance awaiting a files reset still runs at), the workspace and home as last reported, and whether it
|
|
144
|
+
* offers projects. Anyone else's device (or a missing one) → `device.not_found`.
|
|
145
|
+
*/
|
|
146
|
+
private scope;
|
|
147
|
+
/**
|
|
148
|
+
* Runs `fn` on the device (`SandboxService.withSandbox`: its instance made or resumed as for a
|
|
149
|
+
* tool call) with the workspace root its instance reports. Kernel refusals (offline, files lost,
|
|
150
|
+
* busy, unavailable) stay as they are; anything else becomes a readable
|
|
151
|
+
* `device.projects_unavailable`, never a 500.
|
|
152
|
+
*/
|
|
153
|
+
private onDevice;
|
|
154
|
+
/** Locks the owner's live device row (serializes its project writes); else `not_found`. */
|
|
155
|
+
private lockOwn;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* A project read on a device whose sandbox provider cannot be reached (the projects routes, a
|
|
159
|
+
* person's project skill invocation).
|
|
160
|
+
*/
|
|
161
|
+
export declare const providerUnreachable: () => ApiError;
|
|
162
|
+
export {};
|