skillar 0.2.1 → 0.3.1
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/NOTICE +280 -0
- package/README.md +37 -5
- package/dist/analysis/finding.js +8 -190
- package/dist/analysis/kernel.js +896 -0
- package/dist/analysis/tree.js +7 -154
- package/dist/analysis/usage.js +9 -394
- package/dist/api/flows.js +42 -212
- package/dist/api/report-kernel.js +1063 -0
- package/dist/api/routes.js +12 -2
- package/dist/catalog/fields.js +138 -146
- package/dist/catalog/watcher.js +7 -2
- package/dist/cli/init.js +45 -0
- package/dist/cli/main.js +76 -3
- package/dist/cli/private-skills.js +122 -0
- package/dist/cli/serve.js +15 -1
- package/dist/cli/sync.js +124 -0
- package/dist/dashboard/NOTICE +280 -0
- package/dist/dashboard/assets/app.js +737 -275
- package/dist/dashboard/assets/astryx-reset.css +407 -0
- package/dist/dashboard/assets/astryx.css +1077 -0
- package/dist/dashboard/assets/charts.css +54 -0
- package/dist/dashboard/assets/chunk-TPGDA2J4.js +1 -0
- package/dist/dashboard/assets/chunk-YLWHVDMP.js +36 -0
- package/dist/dashboard/assets/controls.css +207 -0
- package/dist/dashboard/assets/overlays.css +132 -0
- package/dist/dashboard/assets/p1.css +135 -0
- package/dist/dashboard/assets/shell.css +71 -0
- package/dist/dashboard/assets/skillar-theme.css +692 -0
- package/dist/dashboard/assets/styles.css +224 -121
- package/dist/dashboard/assets/tokens.css +278 -0
- package/dist/dashboard/assets/widgets.css +83 -0
- package/dist/dashboard/index.html +11 -2
- package/dist/dashboard/viewers/viewers.css +61 -0
- package/dist/ingest/attribution.js +5 -1
- package/dist/ingest/pipeline.js +2 -0
- package/dist/ingest/request-recorder.js +39 -0
- package/dist/mcp/dispatch.js +2 -2
- package/dist/mcp/http.js +43 -3
- package/dist/mcp/skills-extension.js +42 -15
- package/dist/mcp/stdio-proxy.js +24 -3
- package/dist/mcp/tools.js +38 -24
- package/dist/packs/claude-code.js +22 -11
- package/dist/packs/cursor.js +9 -4
- package/dist/packs/gemini.js +9 -4
- package/dist/packs/json-config.js +41 -0
- package/dist/packs/omo.js +15 -6
- package/dist/request-status.js +81 -0
- package/dist/search/fts.js +93 -58
- package/dist/search/fusion.js +49 -47
- package/dist/search/guidance.js +18 -6
- package/dist/search/index.js +291 -70
- package/dist/search/lexical.js +71 -63
- package/dist/server.js +12 -0
- package/dist/skills/apply.js +130 -0
- package/dist/skills/capture.js +54 -0
- package/dist/skills/cloud-plan.js +57 -0
- package/dist/skills/cloud.js +81 -0
- package/dist/skills/fs.js +133 -0
- package/dist/skills/package.js +734 -0
- package/dist/skills/plan.js +83 -0
- package/dist/skills/receipt.js +44 -0
- package/dist/skills/rollback.js +119 -0
- package/dist/skills/transfer.js +51 -0
- package/dist/store/db.js +7 -1
- package/dist/store/execution.js +2 -34
- package/dist/store/export.js +25 -4
- package/dist/store/migrations/007_sync.sql +49 -0
- package/dist/store/migrations/008_connector_commands.sql +12 -0
- package/dist/store/queries.js +33 -7
- package/dist/store/writes.js +38 -5
- package/dist/sync/cloud-blobs.js +175 -0
- package/dist/sync/cloud-commands.js +101 -0
- package/dist/sync/cloud-connection.js +105 -0
- package/dist/sync/cloud-delivery.js +114 -0
- package/dist/sync/cloud-documents.js +103 -0
- package/dist/sync/cloud-export.js +67 -0
- package/dist/sync/cloud-http.js +150 -0
- package/dist/sync/cloud-jobs.js +94 -0
- package/dist/sync/cloud-pending.js +143 -0
- package/dist/sync/cloud-prepare.js +101 -0
- package/dist/sync/cloud-restore.js +136 -0
- package/dist/sync/cloud-rollback.js +24 -0
- package/dist/sync/cloud-runner.js +86 -0
- package/dist/sync/cloud-runtime.js +293 -0
- package/dist/sync/cloud-schema.js +32 -0
- package/dist/sync/cloud-stage.js +112 -0
- package/dist/sync/cloud-wire.js +693 -0
- package/dist/sync/cloud-worker.js +57 -0
- package/dist/sync/cloud.js +157 -0
- package/dist/sync/command-apply.js +140 -0
- package/dist/sync/command-control.js +25 -0
- package/dist/sync/command-wire.js +437 -0
- package/dist/sync/consent.js +130 -0
- package/dist/sync/journal.js +43 -0
- package/dist/sync/outbox.js +134 -0
- package/dist/sync/restore-rows.js +125 -0
- package/dist/sync/restore.js +174 -0
- package/dist/sync/snapshot.js +78 -0
- package/dist/sync/spool-request.js +103 -0
- package/dist/sync/tables.js +27 -0
- package/dist/sync/triggers.js +34 -0
- package/dist/workflow/compare.js +3 -43
- package/docs/cloud-sync.md +94 -0
- package/docs/private-skills.md +75 -0
- package/docs/sync-p4.md +102 -0
- package/docs/sync-restore.md +167 -0
- package/package.json +18 -5
- package/src/store/migrations/007_sync.sql +49 -0
- package/src/store/migrations/008_connector_commands.sql +12 -0
- package/dist/dashboard/assets/chunk-5DFWNJC2.js +0 -1
- package/dist/dashboard/assets/chunk-S6B2HJQA.js +0 -17
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { linkSync, mkdirSync, renameSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { checkDirectories, directoryIdentity, readRegular, requiredFile, sameIdentity, sameState, SkillApplyError, statPath, writeExclusive, } from "./fs.js";
|
|
5
|
+
import { checkBefore, loadPlan } from "./plan.js";
|
|
6
|
+
import { PRIVATE_SKILL_MAX_RESOURCE_BYTES } from "./package.js";
|
|
7
|
+
import { displacedPath, MAX_RECEIPT_BYTES, receiptSchema } from "./receipt.js";
|
|
8
|
+
import { checkReceiptDirectories, restoreReceipt } from "./rollback.js";
|
|
9
|
+
/**
|
|
10
|
+
* Optimistic checkpoints, not a hostile-writer transaction. Move old names into
|
|
11
|
+
* retained storage, then hard-link staged files with no replacement. Node has no
|
|
12
|
+
* portable dirfd-relative rename/CAS; ancestor and check/syscall races remain.
|
|
13
|
+
*/
|
|
14
|
+
export async function applySkill(planPath, hook) {
|
|
15
|
+
const plan = await loadPlan(planPath);
|
|
16
|
+
checkBefore(plan);
|
|
17
|
+
const runId = randomUUID();
|
|
18
|
+
const recovery = join(plan.root, `.skillar-private-${runId}`);
|
|
19
|
+
mkdirSync(recovery, { mode: 0o700 });
|
|
20
|
+
const recoveryIdentity = directoryIdentity(recovery);
|
|
21
|
+
if (recoveryIdentity === null)
|
|
22
|
+
throw new SkillApplyError("ancestor_changed", recovery);
|
|
23
|
+
// These arrays are construction accumulators; the persisted receipt is readonly.
|
|
24
|
+
const directories = [...plan.directories];
|
|
25
|
+
for (const [index, dir] of directories.entries()) {
|
|
26
|
+
checkDirectories(directories);
|
|
27
|
+
if (dir.identity === null) {
|
|
28
|
+
mkdirSync(dir.path, { mode: 0o700 });
|
|
29
|
+
const created = directoryIdentity(dir.path);
|
|
30
|
+
if (created === null)
|
|
31
|
+
throw new SkillApplyError("ancestor_changed", dir.path);
|
|
32
|
+
directories[index] = { path: dir.path, identity: created };
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
checkBefore(plan, directories);
|
|
36
|
+
const entries = [];
|
|
37
|
+
for (const [index, file] of plan.files.entries()) {
|
|
38
|
+
checkDirectories([...directories, { path: recovery, identity: recoveryIdentity }]);
|
|
39
|
+
const before = readRegular(join(plan.root, file.path), PRIVATE_SKILL_MAX_RESOURCE_BYTES);
|
|
40
|
+
if (!sameState(before?.state ?? null, file.before))
|
|
41
|
+
throw new SkillApplyError("target_changed", file.path);
|
|
42
|
+
const content = plan.exported.package.contents[index];
|
|
43
|
+
if (content === undefined)
|
|
44
|
+
throw new SkillApplyError("invalid_plan", file.path);
|
|
45
|
+
const stage = join(recovery, `stage-${index}`);
|
|
46
|
+
writeExclusive(stage, Buffer.from(content.body, "base64"));
|
|
47
|
+
const staged = requiredFile(stage, PRIVATE_SKILL_MAX_RESOURCE_BYTES);
|
|
48
|
+
entries.push({
|
|
49
|
+
beforeBody: before?.bytes.toString("base64") ?? null,
|
|
50
|
+
writtenIdentity: staged.state.identity, writtenMtime: staged.state.mtime,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
const receipt = receiptSchema.parse({ schemaVersion: 1, runId, plan, recoveryIdentity, directories, entries });
|
|
54
|
+
const receiptPath = join(recovery, "receipt.json");
|
|
55
|
+
const text = JSON.stringify(receipt);
|
|
56
|
+
if (Buffer.byteLength(text) > MAX_RECEIPT_BYTES)
|
|
57
|
+
throw new SkillApplyError("quota", "receipt");
|
|
58
|
+
checkReceiptDirectories(receipt);
|
|
59
|
+
// Durable write-ahead receipt exists before the first target file mutation.
|
|
60
|
+
writeExclusive(receiptPath, text);
|
|
61
|
+
const displacedOnly = new Set();
|
|
62
|
+
try {
|
|
63
|
+
checkBefore(plan, directories);
|
|
64
|
+
for (const [index, file] of plan.files.entries()) {
|
|
65
|
+
const target = join(plan.root, file.path);
|
|
66
|
+
hook?.({ phase: "before_apply", index });
|
|
67
|
+
checkReceiptDirectories(receipt);
|
|
68
|
+
const current = readRegular(target, PRIVATE_SKILL_MAX_RESOURCE_BYTES);
|
|
69
|
+
if (!sameState(current?.state ?? null, file.before))
|
|
70
|
+
throw new SkillApplyError("target_changed", target);
|
|
71
|
+
const stage = requiredFile(join(recovery, `stage-${index}`), PRIVATE_SKILL_MAX_RESOURCE_BYTES);
|
|
72
|
+
const resource = plan.exported.package.manifest.resources[index];
|
|
73
|
+
const entry = receipt.entries[index];
|
|
74
|
+
if (resource === undefined || entry === undefined || stage.state.digest !== resource.digest ||
|
|
75
|
+
stage.state.mode !== 0o600 || stage.state.mtime !== entry.writtenMtime ||
|
|
76
|
+
!sameIdentity(stage.state.identity, entry.writtenIdentity))
|
|
77
|
+
throw new SkillApplyError("stage_changed", target);
|
|
78
|
+
checkReceiptDirectories(receipt);
|
|
79
|
+
if (!sameState(readRegular(target, PRIVATE_SKILL_MAX_RESOURCE_BYTES)?.state ?? null, file.before)) {
|
|
80
|
+
throw new SkillApplyError("target_changed", target);
|
|
81
|
+
}
|
|
82
|
+
if (file.before !== null) {
|
|
83
|
+
if (statPath(displacedPath(receipt, index)) !== null)
|
|
84
|
+
throw new SkillApplyError("backup_changed", target);
|
|
85
|
+
renameSync(target, displacedPath(receipt, index));
|
|
86
|
+
displacedOnly.add(index);
|
|
87
|
+
hook?.({ phase: "after_displace", index });
|
|
88
|
+
checkReceiptDirectories(receipt);
|
|
89
|
+
const moved = requiredFile(displacedPath(receipt, index), PRIVATE_SKILL_MAX_RESOURCE_BYTES);
|
|
90
|
+
if (moved.state.digest !== file.before.digest || moved.state.mode !== file.before.mode ||
|
|
91
|
+
moved.state.mtime !== file.before.mtime ||
|
|
92
|
+
!sameIdentity(moved.state.identity, file.before.identity)) {
|
|
93
|
+
// Never replace a new occupant. The displaced bytes remain in recovery.
|
|
94
|
+
linkSync(displacedPath(receipt, index), target);
|
|
95
|
+
throw new SkillApplyError("target_changed", target);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
checkReceiptDirectories(receipt);
|
|
99
|
+
linkSync(join(recovery, `stage-${index}`), target);
|
|
100
|
+
displacedOnly.delete(index);
|
|
101
|
+
hook?.({ phase: "after_apply", index });
|
|
102
|
+
checkReceiptDirectories(receipt);
|
|
103
|
+
const installed = requiredFile(target, PRIVATE_SKILL_MAX_RESOURCE_BYTES);
|
|
104
|
+
if (installed.state.digest !== resource.digest || installed.state.mode !== 0o600 ||
|
|
105
|
+
installed.state.mtime !== entry.writtenMtime || !sameIdentity(installed.state.identity, entry.writtenIdentity)) {
|
|
106
|
+
throw new SkillApplyError("target_changed", target);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// Earlier writes can change while a later operation runs; never report those as success.
|
|
110
|
+
checkReceiptDirectories(receipt);
|
|
111
|
+
for (const [index, file] of plan.files.entries()) {
|
|
112
|
+
const current = requiredFile(join(plan.root, file.path), PRIVATE_SKILL_MAX_RESOURCE_BYTES);
|
|
113
|
+
if (current.state.digest !== plan.exported.package.manifest.resources[index]?.digest ||
|
|
114
|
+
current.state.mode !== 0o600 || current.state.mtime !== receipt.entries[index]?.writtenMtime ||
|
|
115
|
+
!sameIdentity(current.state.identity, receipt.entries[index]?.writtenIdentity ?? null)) {
|
|
116
|
+
throw new SkillApplyError("target_changed", file.path);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return { status: "applied", receipt: receiptPath, outcomes: [] };
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
if (!(error instanceof Error))
|
|
123
|
+
throw error;
|
|
124
|
+
const outcomes = restoreReceipt(receipt, { hook, displacedOnly });
|
|
125
|
+
return {
|
|
126
|
+
status: outcomes.some(outcome => outcome.status === "conflict") ? "conflict" : "rolled_back",
|
|
127
|
+
receipt: receiptPath, outcomes, error: error.message,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { readdirSync, realpathSync } from "node:fs";
|
|
2
|
+
import { join, relative, resolve, sep } from "node:path";
|
|
3
|
+
import { PRIVATE_SKILL_MAX_BYTES, PRIVATE_SKILL_MAX_RESOURCE_BYTES, PRIVATE_SKILL_MAX_RESOURCES, createPrivateSkillPackage, privateSkillMediaType, privateSkillPathSchema, } from "./package.js";
|
|
4
|
+
import { ancestors, checkDirectories, directoryIdentity, requiredFile, sameState, SkillApplyError, } from "./fs.js";
|
|
5
|
+
/** Read only an explicitly selected directory; never follow a picked resource symlink. */
|
|
6
|
+
export async function capturePrivateSkill(source) {
|
|
7
|
+
const picked = resolve(source);
|
|
8
|
+
if (directoryIdentity(picked) === null)
|
|
9
|
+
throw new SkillApplyError("missing_directory", picked);
|
|
10
|
+
const root = realpathSync(picked), rootIdentity = directoryIdentity(root);
|
|
11
|
+
if (rootIdentity === null)
|
|
12
|
+
throw new SkillApplyError("missing_directory", root);
|
|
13
|
+
const directories = ancestors(root).map(path => ({ path, identity: directoryIdentity(path) }));
|
|
14
|
+
const states = new Map();
|
|
15
|
+
const files = [];
|
|
16
|
+
let total = 0, visited = 0;
|
|
17
|
+
const walk = (directory, parents) => {
|
|
18
|
+
for (const entry of readdirSync(directory, { withFileTypes: true })) {
|
|
19
|
+
if (++visited > 4096)
|
|
20
|
+
throw new SkillApplyError("quota", "source directory has too many entries");
|
|
21
|
+
checkDirectories(parents);
|
|
22
|
+
const path = join(directory, entry.name), resource = relative(root, path).split(sep).join("/");
|
|
23
|
+
if (entry.isDirectory()) {
|
|
24
|
+
privateSkillPathSchema.parse(`${resource}/x`);
|
|
25
|
+
const identity = directoryIdentity(path);
|
|
26
|
+
if (identity === null)
|
|
27
|
+
throw new SkillApplyError("directory_changed", path);
|
|
28
|
+
const child = { path, identity };
|
|
29
|
+
directories.push(child);
|
|
30
|
+
walk(path, [...parents, child]);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
const canonical = privateSkillPathSchema.parse(resource);
|
|
34
|
+
if (files.length >= PRIVATE_SKILL_MAX_RESOURCES)
|
|
35
|
+
throw new SkillApplyError("quota", "too many resources");
|
|
36
|
+
const file = requiredFile(path, PRIVATE_SKILL_MAX_RESOURCE_BYTES);
|
|
37
|
+
total += file.bytes.length;
|
|
38
|
+
if (total > PRIVATE_SKILL_MAX_BYTES)
|
|
39
|
+
throw new SkillApplyError("quota", "source exceeds package bytes");
|
|
40
|
+
states.set(path, file.state);
|
|
41
|
+
files.push({ path: canonical, mediaType: privateSkillMediaType(canonical), bytes: file.bytes });
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
walk(root, [...directories]);
|
|
46
|
+
checkDirectories(directories);
|
|
47
|
+
for (const [path, before] of states) {
|
|
48
|
+
if (!sameState(requiredFile(path, PRIVATE_SKILL_MAX_RESOURCE_BYTES).state, before)) {
|
|
49
|
+
throw new SkillApplyError("file_changed", path);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
checkDirectories(directories);
|
|
53
|
+
return { sourceRoot: root, rootIdentity, package: await createPrivateSkillPackage(files) };
|
|
54
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { realpathSync } from "node:fs";
|
|
2
|
+
import { basename, dirname, isAbsolute, join, resolve } from "node:path";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { CloudRequestError, cloudOriginSchema } from "../sync/cloud-http.js";
|
|
5
|
+
import { capturePrivateSkill } from "./capture.js";
|
|
6
|
+
import { identitySchema, inside, readJson, SkillApplyError, writeExclusive } from "./fs.js";
|
|
7
|
+
import { privateSkillCloud } from "./cloud.js";
|
|
8
|
+
import { PRIVATE_SKILL_MAX_REQUEST_BYTES, privateSkillDetailSchema, privateSkillHeadSchema, privateSkillIdSchema, privateSkillPackageSchema, verifyPrivateSkillPackage, } from "./package.js";
|
|
9
|
+
export const privatePublicationPlanSchema = z.strictObject({
|
|
10
|
+
kind: z.literal("private-skill-publication"), schemaVersion: z.literal(1),
|
|
11
|
+
origin: cloudOriginSchema, instanceId: z.string().uuid(),
|
|
12
|
+
accountId: z.string().min(1), generation: z.number().int().positive().safe(),
|
|
13
|
+
deviceId: z.string().min(1), skillId: privateSkillIdSchema,
|
|
14
|
+
sourceRoot: z.string().max(4096).refine(isAbsolute), rootIdentity: identitySchema,
|
|
15
|
+
base: privateSkillHeadSchema.nullable(), package: privateSkillPackageSchema,
|
|
16
|
+
}).readonly();
|
|
17
|
+
function artifactPath(path) {
|
|
18
|
+
const absolute = resolve(path);
|
|
19
|
+
return join(realpathSync(dirname(absolute)), basename(absolute));
|
|
20
|
+
}
|
|
21
|
+
export async function readPublicationPlan(path) {
|
|
22
|
+
const plan = privatePublicationPlanSchema.parse(readJson(artifactPath(path), PRIVATE_SKILL_MAX_REQUEST_BYTES + 8192));
|
|
23
|
+
await verifyPrivateSkillPackage(plan.package);
|
|
24
|
+
return plan;
|
|
25
|
+
}
|
|
26
|
+
/** Reads current metadata only. No document bytes leave the machine during preview. */
|
|
27
|
+
export async function previewPrivatePublication(home, input, fetcher = fetch) {
|
|
28
|
+
const skillId = privateSkillIdSchema.parse(input.skillId);
|
|
29
|
+
const captured = await capturePrivateSkill(input.sourceRoot);
|
|
30
|
+
const target = artifactPath(input.planPath);
|
|
31
|
+
if (inside(captured.sourceRoot, target)) {
|
|
32
|
+
throw new SkillApplyError("artifact_inside_source", "save the publication plan outside its source directory");
|
|
33
|
+
}
|
|
34
|
+
const client = await privateSkillCloud(home, true, fetcher);
|
|
35
|
+
let base = null;
|
|
36
|
+
try {
|
|
37
|
+
const detail = privateSkillDetailSchema.parse(await client.request(`/${skillId}`));
|
|
38
|
+
if (detail.skill.skillId !== skillId)
|
|
39
|
+
throw new SkillApplyError("identity_mismatch", "unexpected skill metadata");
|
|
40
|
+
base = detail.skill.head;
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
if (!(error instanceof CloudRequestError && error.status === 404 && error.message === "not_found"))
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
const plan = privatePublicationPlanSchema.parse({
|
|
47
|
+
kind: "private-skill-publication", schemaVersion: 1, ...client.identity,
|
|
48
|
+
skillId, sourceRoot: captured.sourceRoot, rootIdentity: captured.rootIdentity, base, package: captured.package,
|
|
49
|
+
});
|
|
50
|
+
if (Buffer.byteLength(JSON.stringify({
|
|
51
|
+
base: plan.base, acknowledgePrivateUpload: true, package: plan.package,
|
|
52
|
+
})) > PRIVATE_SKILL_MAX_REQUEST_BYTES) {
|
|
53
|
+
throw new SkillApplyError("quota", "the encoded publication exceeds the request limit");
|
|
54
|
+
}
|
|
55
|
+
writeExclusive(target, JSON.stringify(plan));
|
|
56
|
+
return plan;
|
|
57
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { deviceSelfSchema, readConnection } from "../sync/cloud-connection.js";
|
|
2
|
+
import { CloudHttp } from "../sync/cloud-http.js";
|
|
3
|
+
import { CloudSyncError } from "../sync/cloud-blobs.js";
|
|
4
|
+
import { connectCloudDeviceFlow, PENDING_SKILLS_READ_SCOPES, PENDING_SKILLS_WRITE_SCOPES, } from "../sync/cloud-pending.js";
|
|
5
|
+
import { PRIVATE_SKILL_MAX_REQUEST_BYTES, privateSkillIdentitySchema } from "./package.js";
|
|
6
|
+
const DEFAULT_CLOUD_ORIGIN = "https://skillar.sh";
|
|
7
|
+
/** A private request never depends on activity capture or releases its bearer token. */
|
|
8
|
+
export async function privateSkillCloud(home, write, fetcher = fetch) {
|
|
9
|
+
const connection = readConnection(home);
|
|
10
|
+
if (connection === null)
|
|
11
|
+
throw new CloudSyncError("not_connected");
|
|
12
|
+
if (connection.expiresAt <= Math.floor(Date.now() / 1000))
|
|
13
|
+
throw new CloudSyncError("credentials_expired");
|
|
14
|
+
const http = new CloudHttp(connection.origin, connection.token, fetcher);
|
|
15
|
+
const current = privateSkillIdentitySchema.parse(await http.request("/v1/skills/identity"));
|
|
16
|
+
if (current.accountId !== connection.accountId || current.deviceId !== connection.deviceId ||
|
|
17
|
+
current.generation !== connection.generation)
|
|
18
|
+
throw new CloudSyncError("device_identity_mismatch");
|
|
19
|
+
if (!current.scopes.includes("skills:read") || (write && !current.scopes.includes("skills:write"))) {
|
|
20
|
+
throw new CloudSyncError("private_skill_permission_required");
|
|
21
|
+
}
|
|
22
|
+
return {
|
|
23
|
+
identity: {
|
|
24
|
+
accountId: connection.accountId, generation: connection.generation, deviceId: connection.deviceId,
|
|
25
|
+
origin: connection.origin, instanceId: connection.instanceId,
|
|
26
|
+
},
|
|
27
|
+
request: (suffix, body) => http.request(`/v1/skills${suffix}`, {
|
|
28
|
+
...(body === undefined ? {} : { body: JSON.stringify(body) }),
|
|
29
|
+
maxRequestBytes: PRIVATE_SKILL_MAX_REQUEST_BYTES, maxResponseBytes: PRIVATE_SKILL_MAX_REQUEST_BYTES,
|
|
30
|
+
}),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Publish/pull need skills scopes on this device's connection. When they are
|
|
35
|
+
* missing, the CLI starts the same browser approval as sync connect — one
|
|
36
|
+
* link, one approval — then the caller retries the operation. A device that
|
|
37
|
+
* is already connected keeps it: the approval adds scopes to the same device
|
|
38
|
+
* row, so the union of current and needed scopes is requested.
|
|
39
|
+
*/
|
|
40
|
+
/** SKILLAR_CLOUD_ORIGIN overrides the default site, like `sync connect --origin`. */
|
|
41
|
+
function ensureOrigin(options) {
|
|
42
|
+
return options.origin ?? options.env?.SKILLAR_CLOUD_ORIGIN ?? DEFAULT_CLOUD_ORIGIN;
|
|
43
|
+
}
|
|
44
|
+
export async function ensurePrivateSkillAccess(home, options) {
|
|
45
|
+
const needed = options.write ? PENDING_SKILLS_WRITE_SCOPES : PENDING_SKILLS_READ_SCOPES;
|
|
46
|
+
const connection = readConnection(home);
|
|
47
|
+
if (connection === null) {
|
|
48
|
+
options.io?.out("비공개 스킬 권한이 없습니다. 이 링크를 열어 승인하세요.\n");
|
|
49
|
+
await connectCloudDeviceFlow(home, {
|
|
50
|
+
origin: ensureOrigin(options), deviceLabel: options.deviceLabel,
|
|
51
|
+
scopes: needed, open: options.open, env: options.env, io: options.io,
|
|
52
|
+
fetcher: options.fetcher, wait: options.wait,
|
|
53
|
+
});
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
let current;
|
|
57
|
+
if (connection.scopes !== undefined) {
|
|
58
|
+
current = connection.scopes;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
// Older connection files carry no scopes: probe the device-self boundary
|
|
62
|
+
// for the full list, and fall back to the scopes sync connect always
|
|
63
|
+
// requested when the probe cannot answer.
|
|
64
|
+
try {
|
|
65
|
+
const http = new CloudHttp(connection.origin, connection.token, options.fetcher ?? fetch);
|
|
66
|
+
current = deviceSelfSchema.parse(await http.request("/v1/devices/self")).scopes;
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
current = ["activity:read", "activity:ingest"];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (needed.every(scope => current.includes(scope)))
|
|
73
|
+
return;
|
|
74
|
+
const union = [...new Set([...current, ...needed])];
|
|
75
|
+
options.io?.out("이 기기에 비공개 스킬 권한을 추가합니다. 이 링크를 열어 승인하세요.\n");
|
|
76
|
+
await connectCloudDeviceFlow(home, {
|
|
77
|
+
origin: connection.origin, deviceLabel: options.deviceLabel, scopes: union,
|
|
78
|
+
upgradeDeviceId: connection.deviceId, open: options.open, env: options.env,
|
|
79
|
+
io: options.io, fetcher: options.fetcher, wait: options.wait,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { closeSync, constants, fstatSync, fsyncSync, lstatSync, openSync, readSync, writeFileSync, } from "node:fs";
|
|
3
|
+
import { dirname, isAbsolute, relative, resolve } from "node:path";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
export class SkillApplyError extends Error {
|
|
6
|
+
code;
|
|
7
|
+
name = "SkillApplyError";
|
|
8
|
+
constructor(code, message) {
|
|
9
|
+
super(`${code}: ${message}`);
|
|
10
|
+
this.code = code;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
const integerString = z.string().regex(/^\d+$/).max(40);
|
|
14
|
+
export const identitySchema = z.strictObject({
|
|
15
|
+
dev: integerString, ino: integerString, birth: integerString,
|
|
16
|
+
}).readonly();
|
|
17
|
+
export const stateSchema = z.strictObject({
|
|
18
|
+
identity: identitySchema, size: z.number().int().nonnegative().safe(),
|
|
19
|
+
mode: z.number().int().min(0).max(0o777), mtime: integerString, ctime: integerString,
|
|
20
|
+
digest: z.string().regex(/^[a-f0-9]{64}$/),
|
|
21
|
+
}).readonly();
|
|
22
|
+
export const digest = (bytes) => createHash("sha256").update(bytes).digest("hex");
|
|
23
|
+
export const identity = (stat) => ({
|
|
24
|
+
dev: String(stat.dev), ino: String(stat.ino), birth: String(stat.birthtimeNs),
|
|
25
|
+
});
|
|
26
|
+
export function sameIdentity(a, b) {
|
|
27
|
+
return a === null || b === null ? a === b : a.dev === b.dev && a.ino === b.ino && a.birth === b.birth;
|
|
28
|
+
}
|
|
29
|
+
export function sameState(a, b) {
|
|
30
|
+
return a === null || b === null ? a === b : sameIdentity(a.identity, b.identity) &&
|
|
31
|
+
a.size === b.size && a.mode === b.mode && a.mtime === b.mtime && a.ctime === b.ctime && a.digest === b.digest;
|
|
32
|
+
}
|
|
33
|
+
export function missing(error) {
|
|
34
|
+
return error instanceof Error && "code" in error && error.code === "ENOENT";
|
|
35
|
+
}
|
|
36
|
+
export function statPath(path) {
|
|
37
|
+
try {
|
|
38
|
+
return lstatSync(path, { bigint: true });
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
if (missing(error))
|
|
42
|
+
return null;
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export function directoryIdentity(path) {
|
|
47
|
+
const stat = statPath(path);
|
|
48
|
+
if (stat === null)
|
|
49
|
+
return null;
|
|
50
|
+
if (!stat.isDirectory() || stat.isSymbolicLink())
|
|
51
|
+
throw new SkillApplyError("unsafe_directory", path);
|
|
52
|
+
return identity(stat);
|
|
53
|
+
}
|
|
54
|
+
export function ancestors(path) {
|
|
55
|
+
const result = [];
|
|
56
|
+
for (let current = resolve(path);; current = dirname(current)) {
|
|
57
|
+
result.unshift(current);
|
|
58
|
+
if (dirname(current) === current)
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export function checkDirectories(dirs) {
|
|
63
|
+
for (const dir of dirs) {
|
|
64
|
+
if (!sameIdentity(directoryIdentity(dir.path), dir.identity)) {
|
|
65
|
+
throw new SkillApplyError("ancestor_changed", dir.path);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
export function inside(root, path) {
|
|
70
|
+
const rel = relative(root, path);
|
|
71
|
+
return rel === "" || (!isAbsolute(rel) && rel !== ".." && !rel.startsWith(`..${process.platform === "win32" ? "\\" : "/"}`));
|
|
72
|
+
}
|
|
73
|
+
/** Bounded descriptor read, rejecting links/devices and changes observed during the read. */
|
|
74
|
+
export function readRegular(path, limit) {
|
|
75
|
+
const initial = statPath(path);
|
|
76
|
+
if (initial === null)
|
|
77
|
+
return null;
|
|
78
|
+
if (!initial.isFile())
|
|
79
|
+
throw new SkillApplyError("not_regular", path);
|
|
80
|
+
const fd = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
81
|
+
try {
|
|
82
|
+
const stat = fstatSync(fd, { bigint: true });
|
|
83
|
+
if (!stat.isFile() || !sameIdentity(identity(initial), identity(stat)))
|
|
84
|
+
throw new SkillApplyError("file_changed", path);
|
|
85
|
+
if (stat.size > BigInt(limit))
|
|
86
|
+
throw new SkillApplyError("quota", path);
|
|
87
|
+
const bytes = Buffer.alloc(Number(stat.size) + 1);
|
|
88
|
+
let length = 0;
|
|
89
|
+
for (;;) {
|
|
90
|
+
const count = readSync(fd, bytes, length, bytes.length - length, null);
|
|
91
|
+
length += count;
|
|
92
|
+
if (count === 0 || length === bytes.length)
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
const end = fstatSync(fd, { bigint: true });
|
|
96
|
+
const named = statPath(path);
|
|
97
|
+
if (length !== Number(stat.size) || stat.mtimeNs !== end.mtimeNs || stat.ctimeNs !== end.ctimeNs ||
|
|
98
|
+
stat.mode !== end.mode || named === null || !sameIdentity(identity(stat), identity(named))) {
|
|
99
|
+
throw new SkillApplyError("file_changed", path);
|
|
100
|
+
}
|
|
101
|
+
return { bytes: bytes.subarray(0, length), state: {
|
|
102
|
+
identity: identity(stat), size: length, mode: Number(stat.mode) & 0o777,
|
|
103
|
+
mtime: String(stat.mtimeNs), ctime: String(stat.ctimeNs), digest: digest(bytes.subarray(0, length)),
|
|
104
|
+
} };
|
|
105
|
+
}
|
|
106
|
+
finally {
|
|
107
|
+
closeSync(fd);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
export function requiredFile(path, limit) {
|
|
111
|
+
const file = readRegular(path, limit);
|
|
112
|
+
if (file === null)
|
|
113
|
+
throw new SkillApplyError("missing_input", path);
|
|
114
|
+
return file;
|
|
115
|
+
}
|
|
116
|
+
export function readJson(path, limit) {
|
|
117
|
+
for (const parent of ancestors(dirname(resolve(path)))) {
|
|
118
|
+
if (directoryIdentity(parent) === null)
|
|
119
|
+
throw new SkillApplyError("missing_directory", parent);
|
|
120
|
+
}
|
|
121
|
+
return JSON.parse(requiredFile(path, limit).bytes.toString("utf8"));
|
|
122
|
+
}
|
|
123
|
+
/** Exclusive artifact creation: never replace an existing plan, receipt, or stage. */
|
|
124
|
+
export function writeExclusive(path, bytes) {
|
|
125
|
+
const fd = openSync(path, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
|
|
126
|
+
try {
|
|
127
|
+
writeFileSync(fd, bytes);
|
|
128
|
+
fsyncSync(fd);
|
|
129
|
+
}
|
|
130
|
+
finally {
|
|
131
|
+
closeSync(fd);
|
|
132
|
+
}
|
|
133
|
+
}
|