@capybara-hq/kernel 0.1.0-rc.32 → 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/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/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 +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/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 +2 -2
- package/src/migrations/0133_project_root.sql +30 -0
|
@@ -707,6 +707,30 @@ export declare const ConversationEventSchema: z.ZodDiscriminatedUnion<[z.ZodObje
|
|
|
707
707
|
}>>;
|
|
708
708
|
local: z.ZodOptional<z.ZodBoolean>;
|
|
709
709
|
}, z.core.$strip>;
|
|
710
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
711
|
+
type: z.ZodLiteral<"project_root">;
|
|
712
|
+
payload: z.ZodObject<{
|
|
713
|
+
project: z.ZodString;
|
|
714
|
+
root: z.ZodString;
|
|
715
|
+
agentsMd: z.ZodOptional<z.ZodString>;
|
|
716
|
+
agentsMdTruncated: z.ZodOptional<z.ZodLiteral<true>>;
|
|
717
|
+
unavailable: z.ZodOptional<z.ZodObject<{
|
|
718
|
+
reason: z.ZodEnum<{
|
|
719
|
+
gone: "gone";
|
|
720
|
+
outside_mode: "outside_mode";
|
|
721
|
+
removed: "removed";
|
|
722
|
+
unreadable: "unreadable";
|
|
723
|
+
unsupported: "unsupported";
|
|
724
|
+
}>;
|
|
725
|
+
message: z.ZodString;
|
|
726
|
+
}, z.core.$strict>>;
|
|
727
|
+
skills: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
728
|
+
name: z.ZodString & z.ZodType<string, string, z.core.$ZodTypeInternals<string, string>>;
|
|
729
|
+
description: z.ZodString;
|
|
730
|
+
digest: z.ZodString;
|
|
731
|
+
}, z.core.$strip>>>;
|
|
732
|
+
skillsTruncated: z.ZodOptional<z.ZodLiteral<true>>;
|
|
733
|
+
}, z.core.$strict>;
|
|
710
734
|
}, z.core.$strip>, z.ZodObject<{
|
|
711
735
|
type: z.ZodLiteral<"tool_call_started">;
|
|
712
736
|
payload: z.ZodObject<{
|
|
@@ -642,6 +642,75 @@ export const ConversationEventSchema = z.discriminatedUnion("type", [
|
|
|
642
642
|
allowedMode: z.enum(["auto", "sandbox"]).optional(),
|
|
643
643
|
local: z.boolean().optional(),
|
|
644
644
|
})),
|
|
645
|
+
/**
|
|
646
|
+
* The project a bound conversation's run read at its start (project root spec §4.4): the
|
|
647
|
+
* project's name and root, its `AGENTS.md` text, and per skill its name, description and digest;
|
|
648
|
+
* or, for a project the run cannot use, why (`unavailable`). The payload holds everything the
|
|
649
|
+
* event renders, so the request is reconstructable from the log. A run writes it before its first
|
|
650
|
+
* model call when it differs from the conversation's latest one; within a run the snapshot is
|
|
651
|
+
* fixed, so the request stays append-only. **Model-visible**: rendered at its position as a
|
|
652
|
+
* system message, after the agent's own instructions (`project root` spec §4.5); a summary
|
|
653
|
+
* covering it is followed by the latest one again. A new event type's new content needs no
|
|
654
|
+
* `DriftReason`.
|
|
655
|
+
*/
|
|
656
|
+
ev("project_root", z
|
|
657
|
+
.object({
|
|
658
|
+
/** The project's name, as the conversation's binding resolved it. */
|
|
659
|
+
project: z.string().min(1).max(255),
|
|
660
|
+
/** Its root on the device, the run's working directory. */
|
|
661
|
+
root: z.string().min(1).max(4096),
|
|
662
|
+
/**
|
|
663
|
+
* The project's `AGENTS.md` as read at run start, cut at 64 KiB by the reader (the visible
|
|
664
|
+
* marker is part of that cap). Absent when the project has none (no reason, no notice:
|
|
665
|
+
* spec §4.7) or is unavailable.
|
|
666
|
+
*/
|
|
667
|
+
agentsMd: z.string().optional(),
|
|
668
|
+
/** `agentsMd` ends at the reader's cap and carries the marker. Absent: not cut. */
|
|
669
|
+
agentsMdTruncated: z.literal(true).optional(),
|
|
670
|
+
/**
|
|
671
|
+
* The run cannot use the project: its root is `gone`, `outside_mode` (the device's current
|
|
672
|
+
* mode cannot read it), `unreadable`, the device offers no projects (`unsupported`), or the
|
|
673
|
+
* device no longer keeps the bound name and path (`removed`); `message` says so in English
|
|
674
|
+
* for the model. Set alone: no `agentsMd`, no skills.
|
|
675
|
+
*/
|
|
676
|
+
unavailable: z
|
|
677
|
+
.object({
|
|
678
|
+
reason: z.enum(["gone", "outside_mode", "unreadable", "unsupported", "removed"]),
|
|
679
|
+
message: z.string().min(1).max(500),
|
|
680
|
+
})
|
|
681
|
+
.strict()
|
|
682
|
+
.optional(),
|
|
683
|
+
/**
|
|
684
|
+
* The project's `.agents/skills/*` SKILL.md files: name, description and a digest of the
|
|
685
|
+
* file. Bodies are never recorded (`Skill` reads one on demand, and its result is a
|
|
686
|
+
* `tool_result`). Empty for a project without skills; the reader keeps the first 100.
|
|
687
|
+
*/
|
|
688
|
+
skills: z
|
|
689
|
+
.array(z.object({
|
|
690
|
+
name: z.string().refine(isSkillName, "invalid skill name"),
|
|
691
|
+
description: z.string().max(4096),
|
|
692
|
+
digest: z.string().min(1).max(128),
|
|
693
|
+
}))
|
|
694
|
+
.max(SKILLS_CATALOG_MAX)
|
|
695
|
+
.default([]),
|
|
696
|
+
/** The project has more skill directories than `skills` was read from. Absent: all read. */
|
|
697
|
+
skillsTruncated: z.literal(true).optional(),
|
|
698
|
+
})
|
|
699
|
+
.strict()
|
|
700
|
+
.superRefine((p, ctx) => {
|
|
701
|
+
if (p.unavailable === undefined)
|
|
702
|
+
return;
|
|
703
|
+
if (p.agentsMd !== undefined ||
|
|
704
|
+
p.agentsMdTruncated !== undefined ||
|
|
705
|
+
p.skillsTruncated !== undefined ||
|
|
706
|
+
p.skills.length > 0) {
|
|
707
|
+
ctx.addIssue({
|
|
708
|
+
code: "custom",
|
|
709
|
+
path: ["unavailable"],
|
|
710
|
+
message: "an unavailable project carries no AGENTS.md and no skills",
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
})),
|
|
645
714
|
/** 工具调用开始执行之前写入(续跑时「有开始无结果」的调用记为结果未知,不再执行)。 */
|
|
646
715
|
ev("tool_call_started", z.object({ step: count, callId: z.string(), toolKey: z.string(), specHash: z.string() })),
|
|
647
716
|
/**
|
|
@@ -273,6 +273,13 @@ export interface ConversationsTable {
|
|
|
273
273
|
* root next starts a run. While set the tree's wakes are held (agent map spec §6).
|
|
274
274
|
*/
|
|
275
275
|
tree_stopped_at: ColumnType<Date | null, Date | string | null | undefined, Date | string | null>;
|
|
276
|
+
/**
|
|
277
|
+
* 0133: the project of its device it is bound to at creation (project root §4.3); null: free.
|
|
278
|
+
* Written only by the insert, never updated (`never`: the binding is immutable).
|
|
279
|
+
*/
|
|
280
|
+
project_name: ColumnType<string | null, string | null | undefined, never>;
|
|
281
|
+
/** 0133: the absolute path the bound project resolved to at creation; null: free mode. */
|
|
282
|
+
project_path: ColumnType<string | null, string | null | undefined, never>;
|
|
276
283
|
}
|
|
277
284
|
/** 0060: an org's order of file-preview@1 previewers (UI refresh B Task 11). */
|
|
278
285
|
export interface FilePreviewersTable {
|
|
@@ -976,6 +983,18 @@ export interface DeviceMachinesTable {
|
|
|
976
983
|
local_policy: ColumnType<unknown | null, string | null | undefined, string | null>;
|
|
977
984
|
/** 0118: the `policy` ops sent whose answer is not recorded yet (`PendingPolicyOp[]`). */
|
|
978
985
|
pending_policy_ops: ColumnType<unknown, string | undefined, string>;
|
|
986
|
+
/** 0133: the owner's home directory the program reported in its hello; null when not reported. */
|
|
987
|
+
home: ColumnType<string | null, string | null | undefined, string | null>;
|
|
988
|
+
}
|
|
989
|
+
/** 0133: a device's projects (project root design §4.1), written only by the kernel. */
|
|
990
|
+
export interface DeviceProjectsTable {
|
|
991
|
+
device_id: string;
|
|
992
|
+
/** The project's identity on its device (`isDeviceProjectName`). */
|
|
993
|
+
name: string;
|
|
994
|
+
/** The project's absolute path on the device. */
|
|
995
|
+
path: string;
|
|
996
|
+
created_by: string;
|
|
997
|
+
created_at: Ts;
|
|
979
998
|
}
|
|
980
999
|
/** 0088_environments.sql, renamed in 0105: the device a member's new conversations use by default. */
|
|
981
1000
|
export interface DeviceDefaultsTable {
|
|
@@ -1320,6 +1339,7 @@ export interface KernelDatabase {
|
|
|
1320
1339
|
device_instances: DeviceInstancesTable;
|
|
1321
1340
|
device_defaults: DeviceDefaultsTable;
|
|
1322
1341
|
device_machines: DeviceMachinesTable;
|
|
1342
|
+
device_projects: DeviceProjectsTable;
|
|
1323
1343
|
background_tasks: BackgroundTasksTable;
|
|
1324
1344
|
scheduled_tasks: ScheduledTasksTable;
|
|
1325
1345
|
scheduled_task_runs: ScheduledTaskRunsTable;
|
|
@@ -6,6 +6,7 @@ import type { SandboxMode } from "../sandbox/modes.js";
|
|
|
6
6
|
import { SandboxProviders } from "../sandbox/providers.js";
|
|
7
7
|
import { DeviceAccess } from "./access.js";
|
|
8
8
|
import { DevicesService } from "./devices.service.js";
|
|
9
|
+
import { type WorkingProject } from "./working-root.js";
|
|
9
10
|
/** `details[0].reason` of the error an anonymous IM conversation's sandbox call gets. */
|
|
10
11
|
export declare const ANONYMOUS_NO_DEVICE = "anonymous";
|
|
11
12
|
/** Anonymous IM senders have no device (spec §4.2 item 8): their sandbox tools are off. */
|
|
@@ -16,6 +17,12 @@ export interface NewConversationDevice {
|
|
|
16
17
|
deviceId?: string;
|
|
17
18
|
/** An IM thread: the person's IM default (`device_defaults`) when still usable. */
|
|
18
19
|
im?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* The name of one of the chosen device's projects to bind the conversation to (project root
|
|
22
|
+
* design §4.3): resolved with `DeviceProjectsService.resolveForBinding` in the transaction that
|
|
23
|
+
* writes the conversation, its name and path stored and never changed. Absent: free mode.
|
|
24
|
+
*/
|
|
25
|
+
project?: string;
|
|
19
26
|
}
|
|
20
27
|
/** What the command review needs to know about a call's device (auto mode spec §5.1, §5.4). */
|
|
21
28
|
export interface CommandReviewFacts {
|
|
@@ -25,8 +32,39 @@ export interface CommandReviewFacts {
|
|
|
25
32
|
ownerAccountId: string;
|
|
26
33
|
mode: SandboxMode;
|
|
27
34
|
allowedMode: SandboxMode;
|
|
28
|
-
/** The workspace root
|
|
35
|
+
/** The device's workspace root; absent: no device bound (the review is skipped). */
|
|
29
36
|
workspace: string | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* The owner's home ({@link WorkingFacts.home}): `~` and the home-level sensitive paths are
|
|
39
|
+
* judged against it, never against a project root. Null: unknown.
|
|
40
|
+
*/
|
|
41
|
+
home: string | null;
|
|
42
|
+
/**
|
|
43
|
+
* The home prefix of the device's workspace (`homeOf`), judged by the review beside `home`: a
|
|
44
|
+
* project outside every home with an implausible reported home still protects the real one.
|
|
45
|
+
*/
|
|
46
|
+
deviceHome: string | null;
|
|
47
|
+
/**
|
|
48
|
+
* The conversation's working root ({@link workingRootOf}): the project's root when the root
|
|
49
|
+
* conversation's latest `project_root` is usable, else `workspace`. The review's `workspace`
|
|
50
|
+
* fact: commands start here, and a write inside it needs no ask (project root spec §4.3).
|
|
51
|
+
*/
|
|
52
|
+
workingRoot: string | undefined;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* What decides the working root of a call (project root spec §4.3), read the same way for a
|
|
56
|
+
* sandbox tool and for the command review, so both decide the same root.
|
|
57
|
+
*/
|
|
58
|
+
export interface WorkingFacts {
|
|
59
|
+
/**
|
|
60
|
+
* The device's workspace: the latest `device_paths` roots recorded in the calling conversation,
|
|
61
|
+
* else the root's, else the machine's reported roots, else the default.
|
|
62
|
+
*/
|
|
63
|
+
workspace: string;
|
|
64
|
+
/** The owner's home: the machine's reported home, else the workspace's home prefix; null: unknown. */
|
|
65
|
+
home: string | null;
|
|
66
|
+
/** The root conversation's latest project snapshot; null: free, or none recorded yet. */
|
|
67
|
+
project: WorkingProject | null;
|
|
30
68
|
}
|
|
31
69
|
/**
|
|
32
70
|
* Which device a conversation works in (spec 2026-11-07-environments-design §4.1 items 1, 3;
|
|
@@ -58,10 +96,26 @@ export declare class ConversationDevices {
|
|
|
58
96
|
* and the tool call fails on its own.
|
|
59
97
|
*/
|
|
60
98
|
reviewFacts(orgId: string, rootConversationId: string, conversationId?: string): Promise<CommandReviewFacts | null>;
|
|
99
|
+
/**
|
|
100
|
+
* The {@link WorkingFacts} of a call in `deviceId` by `conversationId` of the tree rooted at
|
|
101
|
+
* `rootConversationId`: the one source of the workspace, home and project snapshot that the
|
|
102
|
+
* sandbox tools and the command review decide the working root from.
|
|
103
|
+
*/
|
|
104
|
+
workingFacts(orgId: string, rootConversationId: string, conversationId: string, deviceId: string): Promise<WorkingFacts>;
|
|
105
|
+
/**
|
|
106
|
+
* The project snapshot that decides the working root of the conversation tree rooted at
|
|
107
|
+
* `rootConversationId` (project root spec §4.3): its latest `project_root` event, read only when
|
|
108
|
+
* the conversation is bound to a project (a free conversation reads one row by its key). Null:
|
|
109
|
+
* free, or bound with no snapshot recorded yet.
|
|
110
|
+
*/
|
|
111
|
+
workingProject(orgId: string, rootConversationId: string): Promise<WorkingProject | null>;
|
|
61
112
|
/** The workspace root of a conversation's latest `device_paths` event, or null. */
|
|
62
113
|
private recordedWorkspace;
|
|
63
|
-
/**
|
|
64
|
-
|
|
114
|
+
/**
|
|
115
|
+
* The workspace root and the owner's home the device's active machine last reported, or null
|
|
116
|
+
* (cloud: no machine).
|
|
117
|
+
*/
|
|
118
|
+
private machineRoots;
|
|
65
119
|
/**
|
|
66
120
|
* The device a new conversation of `accountId` is bound to: the chosen one when it is
|
|
67
121
|
* usable by the person (else `device.not_found` / `device.unavailable`, and nothing
|
|
@@ -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 {};
|