@capybara-hq/kernel 0.1.0-rc.31 → 0.1.0-rc.33
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/dist/catalog/marketplaces.service.d.ts +1 -1
- 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/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/ctx/create-ctx.d.ts +26 -4
- package/dist/ctx/create-ctx.js +54 -34
- 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/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/extensions/permission-set.d.ts +7 -6
- package/dist/extensions/permission-set.js +9 -6
- package/dist/extensions/permission-view.d.ts +4 -4
- package/dist/extensions/permission-view.js +10 -5
- package/dist/http/app.d.ts +8 -2
- package/dist/http/app.js +12 -2
- package/dist/http/plugin-dispatch.d.ts +55 -18
- package/dist/http/plugin-dispatch.js +124 -29
- package/dist/http/plugin-route-access.d.ts +56 -0
- package/dist/http/plugin-route-access.js +192 -0
- package/dist/identity/auth.guard.d.ts +26 -12
- package/dist/identity/auth.guard.js +62 -33
- package/dist/infra/throttler/named-throttlers.d.ts +3 -0
- package/dist/infra/throttler/named-throttlers.js +16 -0
- package/dist/infra/throttler/throttler.guard.d.ts +2 -1
- package/dist/infra/throttler/throttler.guard.js +2 -1
- package/dist/kernel.module.js +3 -0
- 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/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/overrides.d.ts +7 -0
- package/dist/overrides.js +4 -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/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/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 +16 -1
- package/dist/sandbox/sandbox.service.js +75 -3
- 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/pipeline.d.ts +1 -1
- package/dist/tools/pipeline.js +53 -8
- package/dist/tools/registry.d.ts +6 -0
- package/package.json +3 -3
- package/src/migrations/0133_project_root.sql +30 -0
|
@@ -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 {};
|
|
@@ -0,0 +1,503 @@
|
|
|
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 { posix } from "node:path";
|
|
11
|
+
import { DEFAULT_SANDBOX_ROOTS, DeviceProtocolError, isDeviceProjectName, isFileSystemGoneError, isSandboxGoneError, MAX_DEVICE_SCAN_RESULTS, rootsOf, ToolError, validateSandboxPathShape, } from "@capybara-hq/plugin-sdk";
|
|
12
|
+
import { Inject, Injectable } from "@nestjs/common";
|
|
13
|
+
import { sql } from "kysely";
|
|
14
|
+
import { ApiError } from "../errors.js";
|
|
15
|
+
import { audit } from "../identity/audit.js";
|
|
16
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
17
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
18
|
+
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
19
|
+
import { projectsUnsupported } from "../local-devices/local-device-provider.js";
|
|
20
|
+
import { deviceFeaturesOf, localModeOf } from "../local-devices/policy-summary.js";
|
|
21
|
+
import { POLICY_REFUSAL_PREFIX } from "../local-devices/relay.js";
|
|
22
|
+
import { providerForDevice, SandboxProviders } from "../sandbox/providers.js";
|
|
23
|
+
import { deviceNotFound, SandboxService } from "../sandbox/sandbox.service.js";
|
|
24
|
+
import { SandboxProviderUnreachableError } from "../sandbox/unreachable.js";
|
|
25
|
+
import { DeviceAccess } from "./access.js";
|
|
26
|
+
// a path above the owner's home is never stored as a project: the working root would fall back for
|
|
27
|
+
// it anyway (project root §4.3), so both use the one rule; the home itself may be one
|
|
28
|
+
import { aboveHome } from "./working-root.js";
|
|
29
|
+
/** The most projects one device keeps (project root §4.1). */
|
|
30
|
+
export const MAX_DEVICE_PROJECTS = 100;
|
|
31
|
+
/**
|
|
32
|
+
* The holder name the projects service enters a device's sandbox under (`SandboxService`
|
|
33
|
+
* `withSandbox`): it only lists directories, never runs a command, so no conversation holds it.
|
|
34
|
+
*/
|
|
35
|
+
const PROJECTS_HOLDER = "device-projects";
|
|
36
|
+
/** Only the owner's home bounds nothing: auto mode on a machine reads what its owner can. */
|
|
37
|
+
const homeBased = (s) => s.source === "local" && s.mode === "auto";
|
|
38
|
+
/** The base a relative path is resolved against in the scope's mode (project root §4.2). */
|
|
39
|
+
const baseOf = (s, workspace = s.workspace) => homeBased(s) ? s.home : workspace;
|
|
40
|
+
/**
|
|
41
|
+
* `path` lies inside what the scope's mode may read (sandbox mode and cloud: the workspace, as the
|
|
42
|
+
* held instance reports it when given).
|
|
43
|
+
*/
|
|
44
|
+
export const withinMode = (s, path, workspace = s.workspace) => homeBased(s) || path === workspace || path.startsWith(workspace === "/" ? "/" : `${workspace}/`);
|
|
45
|
+
/** `path` relative to `base` ("" for the base itself); `path` must lie under it. */
|
|
46
|
+
const relativeTo = (base, path) => path === base ? "" : posix.relative(base, path);
|
|
47
|
+
const nameInvalid = (name) => new ApiError("device.project_name_invalid", `"${clipped(name)}" is not a valid project name: use 1-64 characters, not . or .., without / or control characters, and without leading or trailing spaces`);
|
|
48
|
+
const nameTaken = (name) => new ApiError("device.project_name_taken", `This device already has a project named "${clipped(name)}"; pick another name`);
|
|
49
|
+
const projectNotFound = (name) => new ApiError("device.project_not_found", `This device has no project named "${clipped(name)}"`);
|
|
50
|
+
const outsideMode = (path, mode) => new ApiError("device.project_outside_mode", mode === "sandbox"
|
|
51
|
+
? `${clipped(path)} is outside the device's workspace, the only directory it can read in sandbox mode`
|
|
52
|
+
: `${clipped(path)} is outside the device's workspace, the only directory a cloud device can read`);
|
|
53
|
+
const tooBroad = (path) => new ApiError("device.project_path_too_broad", `${clipped(path)} holds the owner's home directory (or other people's): pick the project's own directory, the home or below it`);
|
|
54
|
+
const homeUnknown = () => new ApiError("device.project_home_unknown", "In auto mode a project's path is relative to the owner's home directory, which this device has not reported; update capybara-device on that machine and reconnect it");
|
|
55
|
+
/** A device-supplied or person-supplied string inside a message, bounded. */
|
|
56
|
+
function clipped(s, max = 200) {
|
|
57
|
+
return s.length <= max ? s : `${s.slice(0, max)}…`;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* A device's projects (project root design §4.1, §4.2, §6): kept in `device_projects`, keyed by the
|
|
61
|
+
* device's id, written only here. Only the device's owner reads or changes them (anyone else gets
|
|
62
|
+
* `device.not_found`). Every project is scoped by the device's current effective mode
|
|
63
|
+
* (`SandboxService.devicePolicy`, what the command review's mode derives from): sandbox mode and a cloud device
|
|
64
|
+
* offer only the workspace, auto mode on a machine whatever its owner can read; a project outside
|
|
65
|
+
* the mode is hidden, never deleted. The device stays the authority on what it can read: adding asks
|
|
66
|
+
* it with `list`, the suggestions with `scan`, both carrying the current mode, both outside any
|
|
67
|
+
* transaction; the insert then runs in its own, with uniqueness from the primary key.
|
|
68
|
+
*/
|
|
69
|
+
let DeviceProjectsService = class DeviceProjectsService {
|
|
70
|
+
db;
|
|
71
|
+
txHost;
|
|
72
|
+
providers;
|
|
73
|
+
sandbox;
|
|
74
|
+
access;
|
|
75
|
+
constructor(db, txHost, providers, sandbox, access) {
|
|
76
|
+
this.db = db;
|
|
77
|
+
this.txHost = txHost;
|
|
78
|
+
this.providers = providers;
|
|
79
|
+
this.sandbox = sandbox;
|
|
80
|
+
this.access = access;
|
|
81
|
+
}
|
|
82
|
+
/** The projects the device's current mode can read; see {@link DeviceProjectList}. */
|
|
83
|
+
async list(orgId, accountId, deviceId) {
|
|
84
|
+
const scope = await this.scope(orgId, accountId, deviceId);
|
|
85
|
+
const rows = await this.db
|
|
86
|
+
.selectFrom("device_projects")
|
|
87
|
+
.select(["name", "path", "created_at"])
|
|
88
|
+
.where("device_id", "=", deviceId)
|
|
89
|
+
.orderBy("name")
|
|
90
|
+
.execute();
|
|
91
|
+
// a device that offers no projects offers none of the stored ones either
|
|
92
|
+
const visible = scope.supported ? rows.filter((r) => withinMode(scope, r.path)) : [];
|
|
93
|
+
return {
|
|
94
|
+
projects: visible.map((r) => ({
|
|
95
|
+
name: r.name,
|
|
96
|
+
path: r.path,
|
|
97
|
+
createdAt: r.created_at.toISOString(),
|
|
98
|
+
})),
|
|
99
|
+
hidden: rows.length - visible.length,
|
|
100
|
+
mode: scope.mode,
|
|
101
|
+
base: baseOf(scope),
|
|
102
|
+
supported: scope.supported,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The picker's suggestions: one `scan` of the base in the current mode (never file contents).
|
|
107
|
+
* A device that offers no projects suggests none; auto mode without a known home is refused.
|
|
108
|
+
*/
|
|
109
|
+
async candidates(orgId, accountId, deviceId, signal = new AbortController().signal) {
|
|
110
|
+
// ownership is the scope's; whether the provider can serve the device is withSandbox's
|
|
111
|
+
const scope = await this.scope(orgId, accountId, deviceId);
|
|
112
|
+
if (!scope.supported) {
|
|
113
|
+
return { candidates: [], mode: scope.mode, base: baseOf(scope), supported: false };
|
|
114
|
+
}
|
|
115
|
+
if (baseOf(scope) === null)
|
|
116
|
+
throw homeUnknown();
|
|
117
|
+
return this.onDevice(orgId, accountId, deviceId, signal, async (h, p, workspace, sig) => {
|
|
118
|
+
const base = baseOf(scope, workspace);
|
|
119
|
+
const scan = p.scan;
|
|
120
|
+
if (scan === undefined)
|
|
121
|
+
throw projectsUnsupported(false);
|
|
122
|
+
let found;
|
|
123
|
+
try {
|
|
124
|
+
found = await scan.call(p, h, base, { limits: { mode: scope.mode }, signal: sig });
|
|
125
|
+
}
|
|
126
|
+
catch (e) {
|
|
127
|
+
// the base is the device's own workspace or home: a refusal there is not the person's path
|
|
128
|
+
throw passThrough(e) ?? unavailable(e);
|
|
129
|
+
}
|
|
130
|
+
// the device's answer is checked again: inside the base and the mode, a valid name, bounded
|
|
131
|
+
const candidates = found
|
|
132
|
+
.filter((c) => validateSandboxPathShape(c.path).length === 0 &&
|
|
133
|
+
(c.path === base || c.path.startsWith(base === "/" ? "/" : `${base}/`)) &&
|
|
134
|
+
withinMode(scope, c.path, workspace) &&
|
|
135
|
+
isDeviceProjectName(c.suggestedName))
|
|
136
|
+
.slice(0, MAX_DEVICE_SCAN_RESULTS)
|
|
137
|
+
.map((c) => ({
|
|
138
|
+
path: c.path,
|
|
139
|
+
relativePath: relativeTo(base, c.path),
|
|
140
|
+
suggestedName: c.suggestedName,
|
|
141
|
+
}));
|
|
142
|
+
return { candidates, mode: scope.mode, base, supported: true };
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* The read-only preconditions of {@link add} known before the device is asked (the Platform
|
|
147
|
+
* operation's precheck): the owner, a device that offers projects, a valid and free name when
|
|
148
|
+
* one is given, room for one more.
|
|
149
|
+
*/
|
|
150
|
+
async checkAdd(orgId, accountId, deviceId, name) {
|
|
151
|
+
await this.precheck(orgId, accountId, deviceId, name);
|
|
152
|
+
}
|
|
153
|
+
/** {@link checkAdd}, answering the scope it read. */
|
|
154
|
+
async precheck(orgId, accountId, deviceId, name) {
|
|
155
|
+
await this.access.usable(orgId, accountId, deviceId);
|
|
156
|
+
const scope = await this.scope(orgId, accountId, deviceId);
|
|
157
|
+
if (!scope.supported)
|
|
158
|
+
throw projectsUnsupported(scope.source === "local");
|
|
159
|
+
if (baseOf(scope) === null)
|
|
160
|
+
throw homeUnknown();
|
|
161
|
+
if (name !== undefined && !isDeviceProjectName(name))
|
|
162
|
+
throw nameInvalid(name);
|
|
163
|
+
const rows = await this.db
|
|
164
|
+
.selectFrom("device_projects")
|
|
165
|
+
.select("name")
|
|
166
|
+
.where("device_id", "=", deviceId)
|
|
167
|
+
.execute();
|
|
168
|
+
if (name !== undefined && rows.some((r) => r.name === name))
|
|
169
|
+
throw nameTaken(name);
|
|
170
|
+
if (rows.length >= MAX_DEVICE_PROJECTS)
|
|
171
|
+
throw limitReached();
|
|
172
|
+
return scope;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Adds a project: `path` is resolved against the base of the device's current mode and must
|
|
176
|
+
* stay inside what that mode reads and not lie above the owner's home ({@link aboveHome},
|
|
177
|
+
* `device.project_path_too_broad`); `name` defaults to the directory's basename. The device is
|
|
178
|
+
* asked `list(path, { limit: 1 })` first, outside any transaction: an answer (even no entries)
|
|
179
|
+
* is a readable directory, null a missing one, an error a file, an unreadable path or one the
|
|
180
|
+
* device refused in its mode; each is a readable refusal. Then the insert, serialized per device.
|
|
181
|
+
*/
|
|
182
|
+
async add(orgId, accountId, deviceId, input, actorTokenId, auditDetail, signal = new AbortController().signal) {
|
|
183
|
+
const scope = await this.precheck(orgId, accountId, deviceId, input.name);
|
|
184
|
+
// a path too broad for any project is refused before the device is leased (a cloud device
|
|
185
|
+
// started): against the last known base; the held instance's base is checked again inside
|
|
186
|
+
const early = baseOf(scope);
|
|
187
|
+
if (early !== null) {
|
|
188
|
+
const path = trimSlash(posix.normalize(posix.join(early, input.path === "" ? "." : input.path)));
|
|
189
|
+
// outside the mode stays the device-side refusal it is (the held workspace decides it)
|
|
190
|
+
if (withinMode(scope, path) && aboveHome(path, scope.home))
|
|
191
|
+
throw tooBroad(path);
|
|
192
|
+
}
|
|
193
|
+
const resolved = await this.onDevice(orgId, accountId, deviceId, signal, async (h, p, workspace, sig) => {
|
|
194
|
+
const path = resolve(scope, workspace, input.path);
|
|
195
|
+
const name = input.name ?? posix.basename(path);
|
|
196
|
+
if (!isDeviceProjectName(name))
|
|
197
|
+
throw nameInvalid(name);
|
|
198
|
+
const list = p.list;
|
|
199
|
+
if (list === undefined)
|
|
200
|
+
throw projectsUnsupported(false);
|
|
201
|
+
let answer;
|
|
202
|
+
try {
|
|
203
|
+
answer = await list.call(p, h, path, {
|
|
204
|
+
limit: 1,
|
|
205
|
+
limits: { mode: scope.mode },
|
|
206
|
+
signal: sig,
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
catch (e) {
|
|
210
|
+
throw await listFailure(e, p, h, path, scope.mode, sig);
|
|
211
|
+
}
|
|
212
|
+
if (answer === null)
|
|
213
|
+
throw pathMissing(path);
|
|
214
|
+
return { path, name };
|
|
215
|
+
});
|
|
216
|
+
return this.insert(orgId, accountId, deviceId, resolved, actorTokenId, auditDetail);
|
|
217
|
+
}
|
|
218
|
+
/** The insert of {@link add}: the owner's live device locked, the count and the name checked. */
|
|
219
|
+
async insert(orgId, accountId, deviceId, p, actorTokenId, auditDetail) {
|
|
220
|
+
await this.lockOwn(orgId, accountId, deviceId);
|
|
221
|
+
const count = await this.db
|
|
222
|
+
.selectFrom("device_projects")
|
|
223
|
+
.select(sql `count(*)::int`.as("n"))
|
|
224
|
+
.where("device_id", "=", deviceId)
|
|
225
|
+
.executeTakeFirstOrThrow();
|
|
226
|
+
if (count.n >= MAX_DEVICE_PROJECTS)
|
|
227
|
+
throw limitReached();
|
|
228
|
+
const row = await this.db
|
|
229
|
+
.insertInto("device_projects")
|
|
230
|
+
.values({ device_id: deviceId, name: p.name, path: p.path, created_by: accountId })
|
|
231
|
+
.onConflict((c) => c.columns(["device_id", "name"]).doNothing())
|
|
232
|
+
.returning(["name", "path", "created_at"])
|
|
233
|
+
.executeTakeFirst();
|
|
234
|
+
if (row === undefined)
|
|
235
|
+
throw nameTaken(p.name);
|
|
236
|
+
await audit(this.db.db, {
|
|
237
|
+
actorAccountId: accountId,
|
|
238
|
+
...(actorTokenId !== undefined && { actorTokenId }),
|
|
239
|
+
orgId,
|
|
240
|
+
action: "device.project_added",
|
|
241
|
+
target: deviceId,
|
|
242
|
+
detail: { name: p.name, path: p.path, ...auditDetail },
|
|
243
|
+
});
|
|
244
|
+
return { name: row.name, path: row.path, createdAt: row.created_at.toISOString() };
|
|
245
|
+
}
|
|
246
|
+
/** Removes a project of the owner's device; removing one that is not there succeeds. */
|
|
247
|
+
async remove(orgId, accountId, deviceId, name, actorTokenId, auditDetail) {
|
|
248
|
+
await this.lockOwn(orgId, accountId, deviceId);
|
|
249
|
+
const gone = await this.db
|
|
250
|
+
.deleteFrom("device_projects")
|
|
251
|
+
.where("device_id", "=", deviceId)
|
|
252
|
+
.where("name", "=", name)
|
|
253
|
+
.returning("path")
|
|
254
|
+
.executeTakeFirst();
|
|
255
|
+
if (gone === undefined)
|
|
256
|
+
return;
|
|
257
|
+
await audit(this.db.db, {
|
|
258
|
+
actorAccountId: accountId,
|
|
259
|
+
...(actorTokenId !== undefined && { actorTokenId }),
|
|
260
|
+
orgId,
|
|
261
|
+
action: "device.project_removed",
|
|
262
|
+
target: deviceId,
|
|
263
|
+
detail: { name, path: gone.path, ...auditDetail },
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* The project a new conversation of `accountId` binds to (project root §4.3): one of the
|
|
268
|
+
* owner's device's projects that its current mode can read. Refused with `device.not_found`
|
|
269
|
+
* (not the caller's device), `device.projects_unsupported`, `device.project_not_found` or
|
|
270
|
+
* `device.project_outside_mode` or `device.project_path_too_broad`. Reads only (the caller's transaction, if any); the device is
|
|
271
|
+
* not asked: it enforces its mode on every access anyway.
|
|
272
|
+
*/
|
|
273
|
+
async resolveForBinding(orgId, deviceId, accountId, name) {
|
|
274
|
+
const scope = await this.scope(orgId, accountId, deviceId);
|
|
275
|
+
if (!scope.supported)
|
|
276
|
+
throw projectsUnsupported(scope.source === "local");
|
|
277
|
+
const row = await this.db
|
|
278
|
+
.selectFrom("device_projects")
|
|
279
|
+
.select(["name", "path"])
|
|
280
|
+
.where("device_id", "=", deviceId)
|
|
281
|
+
.where("name", "=", name)
|
|
282
|
+
.executeTakeFirst();
|
|
283
|
+
if (row === undefined)
|
|
284
|
+
throw projectNotFound(name);
|
|
285
|
+
if (!withinMode(scope, row.path))
|
|
286
|
+
throw outsideMode(row.path, scope.mode);
|
|
287
|
+
// a home reported after the project was added can put it above the home: the working root
|
|
288
|
+
// would fall back for it, so it is not bound (the add rule, project root §4.3)
|
|
289
|
+
if (aboveHome(row.path, scope.home))
|
|
290
|
+
throw tooBroad(row.path);
|
|
291
|
+
return { name: row.name, path: row.path };
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Whether the device still keeps the project `name` at `path` (project root §4.3): a binding is
|
|
295
|
+
* the name and the path, so a project removed, or re-added under that name with another path, no
|
|
296
|
+
* longer serves the conversations bound to it. Reads only.
|
|
297
|
+
*/
|
|
298
|
+
async keeps(deviceId, name, path) {
|
|
299
|
+
const row = await this.db
|
|
300
|
+
.selectFrom("device_projects")
|
|
301
|
+
.select("name")
|
|
302
|
+
.where("device_id", "=", deviceId)
|
|
303
|
+
.where("name", "=", name)
|
|
304
|
+
.where("path", "=", path)
|
|
305
|
+
.executeTakeFirst();
|
|
306
|
+
return row !== undefined;
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* The owner's live device's scope now ({@link scope}), for a run start's read of its bound
|
|
310
|
+
* project (project root §4.4): the mode to read in and whether the device offers projects.
|
|
311
|
+
* Reads only; anyone else's device (or a missing one) → `device.not_found`.
|
|
312
|
+
*/
|
|
313
|
+
async scopeFor(orgId, accountId, deviceId) {
|
|
314
|
+
return this.scope(orgId, accountId, deviceId);
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* The owner's live device's scope now: its source, its effective mode
|
|
318
|
+
* (`SandboxService.devicePolicy`: the platform, the org and the device's own choice; for a cloud
|
|
319
|
+
* device never the wider mode an instance awaiting a files reset still runs at), the workspace and home as last reported, and whether it
|
|
320
|
+
* offers projects. Anyone else's device (or a missing one) → `device.not_found`.
|
|
321
|
+
*/
|
|
322
|
+
async scope(orgId, accountId, deviceId) {
|
|
323
|
+
const row = await this.db
|
|
324
|
+
.selectFrom("devices as e")
|
|
325
|
+
.leftJoin("device_machines as m", (j) => j.onRef("m.device_id", "=", "e.id").on("m.revoked_at", "is", null))
|
|
326
|
+
.leftJoin("device_instances as i", "i.device_id", "e.id")
|
|
327
|
+
.select([
|
|
328
|
+
"e.source",
|
|
329
|
+
"e.provider",
|
|
330
|
+
"m.features",
|
|
331
|
+
"m.local_policy",
|
|
332
|
+
"m.home",
|
|
333
|
+
sql `m.roots->>'workspace'`.as("machine_workspace"),
|
|
334
|
+
"i.handle",
|
|
335
|
+
])
|
|
336
|
+
.where("e.id", "=", deviceId)
|
|
337
|
+
.where("e.org_id", "=", orgId)
|
|
338
|
+
.where("e.owner_kind", "=", "personal")
|
|
339
|
+
.where("e.owner_account_id", "=", accountId)
|
|
340
|
+
.where("e.deleted_at", "is", null)
|
|
341
|
+
.executeTakeFirst();
|
|
342
|
+
if (row === undefined)
|
|
343
|
+
throw deviceNotFound();
|
|
344
|
+
// `devicePolicy`, not `modeLevel` as the command review reads: a cloud device with a pending
|
|
345
|
+
// files reset is scoped by the narrower mode it will run at, never the wider one its instance
|
|
346
|
+
// still has, on purpose (projects are offered for what the device will read; no effect today)
|
|
347
|
+
const { mode, allowedMode } = await this.sandbox.devicePolicy({ orgId, deviceId });
|
|
348
|
+
if (row.source === "local") {
|
|
349
|
+
return {
|
|
350
|
+
source: "local",
|
|
351
|
+
mode,
|
|
352
|
+
widerThanAllowed: allowedMode === "sandbox" && localModeOf(row.features, row.local_policy) === "auto",
|
|
353
|
+
workspace: row.machine_workspace ?? DEFAULT_SANDBOX_ROOTS.workspace,
|
|
354
|
+
home: row.home,
|
|
355
|
+
supported: deviceFeaturesOf(row.features).includes("projects"),
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
const impl = providerForDevice(this.providers, row.provider)?.impl;
|
|
359
|
+
const handle = row.handle;
|
|
360
|
+
return {
|
|
361
|
+
source: "cloud",
|
|
362
|
+
mode,
|
|
363
|
+
widerThanAllowed: false,
|
|
364
|
+
workspace: handle === null ? DEFAULT_SANDBOX_ROOTS.workspace : rootsOf(handle).workspace,
|
|
365
|
+
home: null,
|
|
366
|
+
supported: typeof impl?.list === "function" && typeof impl.scan === "function",
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Runs `fn` on the device (`SandboxService.withSandbox`: its instance made or resumed as for a
|
|
371
|
+
* tool call) with the workspace root its instance reports. Kernel refusals (offline, files lost,
|
|
372
|
+
* busy, unavailable) stay as they are; anything else becomes a readable
|
|
373
|
+
* `device.projects_unavailable`, never a 500.
|
|
374
|
+
*/
|
|
375
|
+
async onDevice(orgId, accountId, deviceId, signal, fn) {
|
|
376
|
+
try {
|
|
377
|
+
return await this.sandbox.withSandbox({
|
|
378
|
+
orgId,
|
|
379
|
+
deviceId,
|
|
380
|
+
holderConversationId: PROJECTS_HOLDER,
|
|
381
|
+
actor: { accountId, conversationId: PROJECTS_HOLDER },
|
|
382
|
+
}, (h, p, info, sig) => fn(h, p, info.roots.workspace, sig), signal);
|
|
383
|
+
}
|
|
384
|
+
catch (e) {
|
|
385
|
+
if (e instanceof ApiError)
|
|
386
|
+
throw e;
|
|
387
|
+
if (e instanceof SandboxProviderUnreachableError)
|
|
388
|
+
throw providerUnreachable();
|
|
389
|
+
throw unavailable(e);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
/** Locks the owner's live device row (serializes its project writes); else `not_found`. */
|
|
393
|
+
async lockOwn(orgId, accountId, deviceId) {
|
|
394
|
+
const row = await this.db
|
|
395
|
+
.selectFrom("devices")
|
|
396
|
+
.select("id")
|
|
397
|
+
.where("id", "=", deviceId)
|
|
398
|
+
.where("org_id", "=", orgId)
|
|
399
|
+
.where("owner_kind", "=", "personal")
|
|
400
|
+
.where("owner_account_id", "=", accountId)
|
|
401
|
+
.where("deleted_at", "is", null)
|
|
402
|
+
.forNoKeyUpdate()
|
|
403
|
+
.executeTakeFirst();
|
|
404
|
+
if (row === undefined)
|
|
405
|
+
throw deviceNotFound();
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
__decorate([
|
|
409
|
+
Transactional()
|
|
410
|
+
], DeviceProjectsService.prototype, "insert", null);
|
|
411
|
+
__decorate([
|
|
412
|
+
Transactional()
|
|
413
|
+
], DeviceProjectsService.prototype, "remove", null);
|
|
414
|
+
DeviceProjectsService = __decorate([
|
|
415
|
+
Injectable(),
|
|
416
|
+
__param(0, Inject(KernelDb)),
|
|
417
|
+
__param(1, Inject(TransactionHost)),
|
|
418
|
+
__param(2, Inject(SandboxProviders)),
|
|
419
|
+
__param(3, Inject(SandboxService)),
|
|
420
|
+
__param(4, Inject(DeviceAccess))
|
|
421
|
+
], DeviceProjectsService);
|
|
422
|
+
export { DeviceProjectsService };
|
|
423
|
+
/**
|
|
424
|
+
* A project read on a device whose sandbox provider cannot be reached (the projects routes, a
|
|
425
|
+
* person's project skill invocation).
|
|
426
|
+
*/
|
|
427
|
+
export const providerUnreachable = () => new ApiError("device.projects_unavailable", "The device's sandbox provider cannot be reached now; try again shortly");
|
|
428
|
+
const limitReached = () => new ApiError("device.project_limit_reached", `This device already has ${MAX_DEVICE_PROJECTS} projects, the most allowed; remove one first`);
|
|
429
|
+
/** `path` without a trailing `/` (except `/` itself). */
|
|
430
|
+
const trimSlash = (path) => path.length > 1 && path.endsWith("/") ? path.slice(0, -1) : path;
|
|
431
|
+
/**
|
|
432
|
+
* The absolute path of `relative` against the scope's base (the instance's `workspace` in sandbox
|
|
433
|
+
* mode and on a cloud device, the owner's home in auto mode), normalized; refused when the base is
|
|
434
|
+
* unknown, the result is not a valid path, or it leaves what the mode reads.
|
|
435
|
+
*/
|
|
436
|
+
function resolve(scope, workspace, relative) {
|
|
437
|
+
const base = baseOf(scope, workspace);
|
|
438
|
+
if (base === null)
|
|
439
|
+
throw homeUnknown();
|
|
440
|
+
const path = trimSlash(posix.normalize(posix.join(base, relative === "" ? "." : relative)));
|
|
441
|
+
if (validateSandboxPathShape(path).length > 0 || !withinMode(scope, path, workspace)) {
|
|
442
|
+
throw outsideMode(path, scope.mode);
|
|
443
|
+
}
|
|
444
|
+
if (aboveHome(path, scope.home))
|
|
445
|
+
throw tooBroad(path);
|
|
446
|
+
return path;
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* An error the projects service passes on as it is: a lost instance or file system (for
|
|
450
|
+
* `withSandbox` to handle), a kernel refusal (offline, busy, unavailable), an unreachable provider;
|
|
451
|
+
* a malformed device answer becomes `device.projects_unavailable` (the program's fault). Undefined
|
|
452
|
+
* for anything else.
|
|
453
|
+
*/
|
|
454
|
+
function passThrough(e) {
|
|
455
|
+
if (isSandboxGoneError(e) || isFileSystemGoneError(e) || e instanceof ApiError)
|
|
456
|
+
return e;
|
|
457
|
+
if (e instanceof SandboxProviderUnreachableError)
|
|
458
|
+
return e;
|
|
459
|
+
if (e instanceof DeviceProtocolError) {
|
|
460
|
+
return new ApiError("device.projects_unavailable", "The device answered in a form this kernel does not accept; update capybara-device on that machine");
|
|
461
|
+
}
|
|
462
|
+
return undefined;
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* The device could not be asked now (a failure on the machine or at the provider, an abort, a lost
|
|
466
|
+
* lease): 503 `device.projects_unavailable`. Only a `ToolError`'s message (the relay's, written for
|
|
467
|
+
* people) is passed on; a provider's own error text is not.
|
|
468
|
+
*/
|
|
469
|
+
function unavailable(e) {
|
|
470
|
+
return new ApiError("device.projects_unavailable", e instanceof ToolError
|
|
471
|
+
? `The device could not be asked now: ${clipped(e.message)}`
|
|
472
|
+
: "The device could not be asked now; try again shortly");
|
|
473
|
+
}
|
|
474
|
+
const pathMissing = (path) => new ApiError("device.project_path_missing", `${clipped(path)} does not exist on the device; check the path`);
|
|
475
|
+
/**
|
|
476
|
+
* What a failed `list(path)` becomes. A machine's policy refusal is the device refusing the path
|
|
477
|
+
* in its mode (`device.project_outside_mode`). Any other error alone cannot tell a file or an
|
|
478
|
+
* unreadable directory from a failure on the device, so the device is asked `stat(path)`: a file
|
|
479
|
+
* or a directory it cannot list is the path's problem (422 `device.project_path_unreadable`), a
|
|
480
|
+
* missing path `device.project_path_missing`, and a `stat` that fails too means the device could
|
|
481
|
+
* not be asked (503 `device.projects_unavailable`).
|
|
482
|
+
*/
|
|
483
|
+
async function listFailure(e, p, h, path, mode, signal) {
|
|
484
|
+
const passed = passThrough(e);
|
|
485
|
+
if (passed !== undefined)
|
|
486
|
+
return passed;
|
|
487
|
+
const why = e instanceof ToolError ? ` (${clipped(e.message)})` : "";
|
|
488
|
+
if (e instanceof ToolError && e.message.startsWith(POLICY_REFUSAL_PREFIX)) {
|
|
489
|
+
return new ApiError("device.project_outside_mode", `The device refused ${clipped(path)} in ${mode} mode${why}`);
|
|
490
|
+
}
|
|
491
|
+
let st;
|
|
492
|
+
try {
|
|
493
|
+
st = await p.stat(h, path, signal);
|
|
494
|
+
}
|
|
495
|
+
catch (e2) {
|
|
496
|
+
return passThrough(e2) ?? unavailable(e);
|
|
497
|
+
}
|
|
498
|
+
if (st === null)
|
|
499
|
+
return pathMissing(path);
|
|
500
|
+
return new ApiError("device.project_path_unreadable", st.isDir
|
|
501
|
+
? `${clipped(path)} is a directory the device cannot read${why}`
|
|
502
|
+
: `${clipped(path)} is a file, not a directory`);
|
|
503
|
+
}
|