@x-otto/space 0.0.0-stage → 0.4.0-beta.0
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/build-revision.json +4 -0
- package/dist/index.d.ts +150 -0
- package/dist/index.js +933 -0
- package/package.json +26 -4
- package/README.md +0 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { InstalledItem, ProjectTrustPort, SettingsManager, SpaceCapability, SpaceManifest, SpacePluginLoad, SpaceScope, SpaceValidationIssue, SpaceWorkspaceTrustPolicy } from "@x-otto/kernel";
|
|
2
|
+
import { ArtifactFile } from "@x-otto/workspace-artifacts";
|
|
3
|
+
import { ContributionRiskClass } from "@x-otto/install";
|
|
4
|
+
|
|
5
|
+
//#region src/manifest.d.ts
|
|
6
|
+
type SpaceManifestReadFailure = {
|
|
7
|
+
kind: 'missing';
|
|
8
|
+
} | {
|
|
9
|
+
kind: 'malformed';
|
|
10
|
+
error: string;
|
|
11
|
+
} | {
|
|
12
|
+
kind: 'unsupported-schema';
|
|
13
|
+
schema: number;
|
|
14
|
+
supported: number;
|
|
15
|
+
};
|
|
16
|
+
type SpaceManifestReadResult = {
|
|
17
|
+
ok: true;
|
|
18
|
+
manifest: SpaceManifest;
|
|
19
|
+
} | {
|
|
20
|
+
ok: false;
|
|
21
|
+
failure: SpaceManifestReadFailure;
|
|
22
|
+
};
|
|
23
|
+
declare function spaceManifestPath(spaceRoot: string): string;
|
|
24
|
+
declare function readSpaceManifest(spaceRoot: string): Promise<SpaceManifestReadResult>;
|
|
25
|
+
interface SpaceCapabilityLoad extends SpaceCapability {
|
|
26
|
+
issues?: readonly SpaceValidationIssue[];
|
|
27
|
+
}
|
|
28
|
+
interface WriteSpaceManifestInput {
|
|
29
|
+
spaceRoot: string;
|
|
30
|
+
manifest: SpaceManifest;
|
|
31
|
+
}
|
|
32
|
+
declare function createSpaceManifestWriter(): {
|
|
33
|
+
write(input: WriteSpaceManifestInput): Promise<void>;
|
|
34
|
+
update(input: WriteSpaceManifestInput, mutate: (manifest: SpaceManifest) => SpaceManifest): Promise<void>;
|
|
35
|
+
};
|
|
36
|
+
type SpaceManifestApplyDecision = 'applied' | 'retry';
|
|
37
|
+
interface SpaceManifestWatch {
|
|
38
|
+
dispose(): void;
|
|
39
|
+
}
|
|
40
|
+
type SpaceWatchDiagnosticCode = 'SPACE_WATCH_INCOMPLETE' | 'SPACE_WATCH_RECOVERED' | 'SPACE_WATCH_APPLY_FAILED' | 'SPACE_WATCH_APPLY_RETRIED';
|
|
41
|
+
interface SpaceManifestWatchOptions {
|
|
42
|
+
intervalMs?: number;
|
|
43
|
+
retryMaxMs?: number;
|
|
44
|
+
isApplied?: () => boolean;
|
|
45
|
+
onDiagnostic?: (code: SpaceWatchDiagnosticCode, detail: string) => void;
|
|
46
|
+
}
|
|
47
|
+
declare function watchSpaceManifest(spaceRoot: string, apply: () => SpaceManifestApplyDecision | Promise<SpaceManifestApplyDecision>, options?: SpaceManifestWatchOptions): SpaceManifestWatch;
|
|
48
|
+
//#endregion
|
|
49
|
+
//#region src/install-items.d.ts
|
|
50
|
+
type InstalledSpaceItem = InstalledItem & {
|
|
51
|
+
pluginId: string;
|
|
52
|
+
spaceId: string;
|
|
53
|
+
};
|
|
54
|
+
declare function spaceIdOf(item: InstalledItem): string | undefined;
|
|
55
|
+
declare function spacePluginIdOf(item: InstalledItem): string | undefined;
|
|
56
|
+
declare function spacePrivateInstalledItems(spaceRoot: string, spaceId: string, plugins: readonly SpacePluginLoad[]): Promise<readonly InstalledSpaceItem[]>;
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/plugin-tree.d.ts
|
|
59
|
+
interface PluginTextFile {
|
|
60
|
+
readonly path: string;
|
|
61
|
+
readonly content: string;
|
|
62
|
+
}
|
|
63
|
+
declare function readPluginTextFiles(directory: string): readonly PluginTextFile[];
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/admission.d.ts
|
|
66
|
+
type AdmissionOutcome = 'allow' | 'deny' | 'defer';
|
|
67
|
+
type WorkspaceTrustState = 'default' | 'trusted' | 'untrusted' | 'opted-out' | 'pending';
|
|
68
|
+
interface AdmissionArtifactFacts {
|
|
69
|
+
readonly skillCount?: number;
|
|
70
|
+
}
|
|
71
|
+
interface AdmissionInput {
|
|
72
|
+
readonly pluginId: string;
|
|
73
|
+
readonly scope: SpaceScope;
|
|
74
|
+
readonly manifest: unknown;
|
|
75
|
+
readonly artifact?: AdmissionArtifactFacts;
|
|
76
|
+
readonly workspaceRoot?: string;
|
|
77
|
+
readonly workspaceTrust?: WorkspaceTrustState;
|
|
78
|
+
}
|
|
79
|
+
interface AdmissionDecision {
|
|
80
|
+
readonly outcome: AdmissionOutcome;
|
|
81
|
+
readonly reason: string;
|
|
82
|
+
readonly risks: readonly ContributionRiskClass[];
|
|
83
|
+
readonly optedOut?: true;
|
|
84
|
+
}
|
|
85
|
+
declare function decideAdmission(input: AdmissionInput): AdmissionDecision;
|
|
86
|
+
//#endregion
|
|
87
|
+
//#region src/trust.d.ts
|
|
88
|
+
type SpaceAdoptionPolicy = 'always' | 'require-workspace-trust';
|
|
89
|
+
interface WorkspaceTrustInputs {
|
|
90
|
+
readonly projectTrust: ProjectTrustPort | undefined;
|
|
91
|
+
readonly policy: SpaceWorkspaceTrustPolicy;
|
|
92
|
+
readonly spaceAdoption?: SpaceAdoptionPolicy;
|
|
93
|
+
}
|
|
94
|
+
declare function workspaceTrustState(inputs: WorkspaceTrustInputs): WorkspaceTrustState;
|
|
95
|
+
declare function spaceWorkspaceTrustPolicyOf(manager: SettingsManager | undefined): SpaceWorkspaceTrustPolicy;
|
|
96
|
+
//#endregion
|
|
97
|
+
//#region src/plugins.d.ts
|
|
98
|
+
declare function spaceExtensionsDirectory(spaceRoot: string): string;
|
|
99
|
+
declare function spacePluginDirectory(spaceRoot: string, pluginId: string): string;
|
|
100
|
+
declare function readPluginSkillDirectories(pluginDirectory: string): readonly string[];
|
|
101
|
+
interface SpacePluginReadInput {
|
|
102
|
+
spaceRoot: string;
|
|
103
|
+
scope: SpaceScope;
|
|
104
|
+
disabledPlugins: readonly string[];
|
|
105
|
+
workspaceTrust?: WorkspaceTrustState;
|
|
106
|
+
}
|
|
107
|
+
interface SpacePluginsRead {
|
|
108
|
+
readonly plugins: readonly SpacePluginLoad[];
|
|
109
|
+
readonly capabilities: readonly SpaceCapabilityLoad[];
|
|
110
|
+
}
|
|
111
|
+
declare function readSpacePlugins(input: SpacePluginReadInput): Promise<SpacePluginsRead>;
|
|
112
|
+
//#endregion
|
|
113
|
+
//#region src/plugin-history.d.ts
|
|
114
|
+
declare const SPACE_HISTORY_LIMIT = 5;
|
|
115
|
+
interface PluginRevision {
|
|
116
|
+
readonly digest: string;
|
|
117
|
+
readonly generation: number;
|
|
118
|
+
readonly createdAt: string;
|
|
119
|
+
readonly files: readonly string[];
|
|
120
|
+
}
|
|
121
|
+
type PluginRestoreResult = {
|
|
122
|
+
readonly ok: true;
|
|
123
|
+
readonly revision: PluginRevision;
|
|
124
|
+
} | {
|
|
125
|
+
readonly ok: false;
|
|
126
|
+
readonly reason: 'no-history' | 'unknown-revision';
|
|
127
|
+
};
|
|
128
|
+
declare function capturePluginRevision(input: {
|
|
129
|
+
spaceRoot: string;
|
|
130
|
+
scope: SpaceScope;
|
|
131
|
+
pluginId: string;
|
|
132
|
+
directory: string;
|
|
133
|
+
limit?: number;
|
|
134
|
+
}): Promise<PluginRevision | undefined>;
|
|
135
|
+
declare function pluginRevisionsOf(spaceRoot: string, pluginId: string): Promise<readonly PluginRevision[]>;
|
|
136
|
+
declare function restorePluginRevision(input: {
|
|
137
|
+
spaceRoot: string;
|
|
138
|
+
pluginId: string;
|
|
139
|
+
directory: string;
|
|
140
|
+
digest?: string;
|
|
141
|
+
}): Promise<PluginRestoreResult>;
|
|
142
|
+
//#endregion
|
|
143
|
+
//#region src/revisions.d.ts
|
|
144
|
+
interface StoredContent {
|
|
145
|
+
directory: string;
|
|
146
|
+
digest: string;
|
|
147
|
+
}
|
|
148
|
+
declare function storePluginContent(spaceRoot: string, id: string, files: readonly ArtifactFile[], itemKind?: string, capabilities?: readonly string[]): Promise<StoredContent>;
|
|
149
|
+
//#endregion
|
|
150
|
+
export { SPACE_HISTORY_LIMIT, type SpaceManifestApplyDecision, type SpaceManifestReadFailure, type SpaceManifestReadResult, type SpaceManifestWatch, type SpaceManifestWatchOptions, type StoredContent, type WorkspaceTrustState, capturePluginRevision, createSpaceManifestWriter, decideAdmission, pluginRevisionsOf, readPluginSkillDirectories, readPluginTextFiles, readSpaceManifest, readSpacePlugins, restorePluginRevision, spaceExtensionsDirectory, spaceIdOf, spaceManifestPath, spacePluginDirectory, spacePluginIdOf, spacePrivateInstalledItems, spaceWorkspaceTrustPolicyOf, storePluginContent, watchSpaceManifest, workspaceTrustState };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,933 @@
|
|
|
1
|
+
import { withStoreMutex, writeFileAtomic } from "@x-otto/atomic-fs";
|
|
2
|
+
import { HOOKS_CAPABILITY, SKILL_FILENAME, SPACE_DIR, SPACE_MANIFEST_FILENAME, SPACE_MANIFEST_SCHEMA, SPACE_SKILL_KIND, SPACE_SOURCE_KIND, SPACE_WORKSPACE_TRUST_FIELD_SPEC, contentSurfaceOf, hasDeclaredEntrypoint, spaceCapabilityKey, spacePluginItemId, spaceScopeOf } from "@x-otto/kernel";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
5
|
+
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
6
|
+
import { ArtifactRevisionStore, digestOf } from "@x-otto/workspace-artifacts";
|
|
7
|
+
import { classifyContributionRisk, readPluginManifest, readPluginManifestSync } from "@x-otto/install";
|
|
8
|
+
|
|
9
|
+
//#region src/plugin-tree.ts
|
|
10
|
+
const PLUGIN_TREE_IGNORED = new Set([
|
|
11
|
+
"node_modules",
|
|
12
|
+
".git",
|
|
13
|
+
"dist",
|
|
14
|
+
".cache",
|
|
15
|
+
".history",
|
|
16
|
+
".revisions"
|
|
17
|
+
]);
|
|
18
|
+
const PLUGIN_TREE_FILE_LIMIT = 2e3;
|
|
19
|
+
function entriesOf(directory) {
|
|
20
|
+
try {
|
|
21
|
+
return [...readdirSync(directory, {
|
|
22
|
+
withFileTypes: true,
|
|
23
|
+
encoding: "utf8"
|
|
24
|
+
})].sort((left, right) => left.name.localeCompare(right.name));
|
|
25
|
+
} catch {
|
|
26
|
+
return [];
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function listPluginFiles(directory, limit = PLUGIN_TREE_FILE_LIMIT) {
|
|
30
|
+
const files = [];
|
|
31
|
+
const walk = (current, prefix) => {
|
|
32
|
+
for (const entry of entriesOf(current)) {
|
|
33
|
+
if (files.length >= limit) throw new Error(`PLUGIN_TREE_TOO_LARGE:${String(limit)}`);
|
|
34
|
+
if (entry.isSymbolicLink() || PLUGIN_TREE_IGNORED.has(entry.name)) continue;
|
|
35
|
+
const path = prefix === "" ? entry.name : `${prefix}/${entry.name}`;
|
|
36
|
+
if (entry.isDirectory()) {
|
|
37
|
+
walk(join(current, entry.name), path);
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (entry.isFile()) files.push(path);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
walk(directory, "");
|
|
44
|
+
return files;
|
|
45
|
+
}
|
|
46
|
+
function fingerprintTree(directory) {
|
|
47
|
+
return listPluginFiles(directory).map((path) => {
|
|
48
|
+
try {
|
|
49
|
+
const info = statSync(join(directory, path));
|
|
50
|
+
return `${path}:${info.mtimeMs}:${info.size}:${info.ino}`;
|
|
51
|
+
} catch {
|
|
52
|
+
return `${path}:missing`;
|
|
53
|
+
}
|
|
54
|
+
}).join("\n");
|
|
55
|
+
}
|
|
56
|
+
function directoryStampOf(directory) {
|
|
57
|
+
try {
|
|
58
|
+
const info = statSync(directory);
|
|
59
|
+
return `dir:${info.mtimeMs}:${info.ino}`;
|
|
60
|
+
} catch {
|
|
61
|
+
return "dir:missing";
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function fingerprintTreeOf(directory) {
|
|
65
|
+
try {
|
|
66
|
+
return {
|
|
67
|
+
value: `${directoryStampOf(directory)}\n${fingerprintTree(directory)}`,
|
|
68
|
+
isComplete: true
|
|
69
|
+
};
|
|
70
|
+
} catch (error) {
|
|
71
|
+
return {
|
|
72
|
+
value: `incomplete:${directoryStampOf(directory)}:${error instanceof Error ? error.message : String(error)}`,
|
|
73
|
+
isComplete: false
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function readPluginTextFiles(directory) {
|
|
78
|
+
return listPluginFiles(directory).flatMap((path) => {
|
|
79
|
+
const content = readFileSync(join(directory, path));
|
|
80
|
+
return content.includes(0) ? [] : [{
|
|
81
|
+
path,
|
|
82
|
+
content: content.toString("utf8")
|
|
83
|
+
}];
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/manifest.ts
|
|
89
|
+
function spaceManifestPath(spaceRoot) {
|
|
90
|
+
return join(spaceRoot, ".otto", "space", SPACE_MANIFEST_FILENAME);
|
|
91
|
+
}
|
|
92
|
+
function spaceDirectory(spaceRoot) {
|
|
93
|
+
return join(spaceRoot, ".otto", "space");
|
|
94
|
+
}
|
|
95
|
+
function isRecord(value) {
|
|
96
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
97
|
+
}
|
|
98
|
+
function stringListOf(value) {
|
|
99
|
+
if (value === void 0) return [];
|
|
100
|
+
if (!Array.isArray(value)) return;
|
|
101
|
+
return [...new Set(value.filter((item) => typeof item === "string" && item !== ""))];
|
|
102
|
+
}
|
|
103
|
+
function parseSpaceManifest(raw) {
|
|
104
|
+
if (!isRecord(raw)) return {
|
|
105
|
+
ok: false,
|
|
106
|
+
failure: {
|
|
107
|
+
kind: "malformed",
|
|
108
|
+
error: "manifest must be an object"
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
const schema = raw["schema"];
|
|
112
|
+
if (typeof schema !== "number" || !Number.isInteger(schema)) return {
|
|
113
|
+
ok: false,
|
|
114
|
+
failure: {
|
|
115
|
+
kind: "malformed",
|
|
116
|
+
error: "schema must be an integer"
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
if (schema !== SPACE_MANIFEST_SCHEMA) return {
|
|
120
|
+
ok: false,
|
|
121
|
+
failure: {
|
|
122
|
+
kind: "unsupported-schema",
|
|
123
|
+
schema,
|
|
124
|
+
supported: SPACE_MANIFEST_SCHEMA
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
const id = raw["id"];
|
|
128
|
+
if (typeof id !== "string" || id === "") return {
|
|
129
|
+
ok: false,
|
|
130
|
+
failure: {
|
|
131
|
+
kind: "malformed",
|
|
132
|
+
error: "id must be a non-empty string"
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
const disabledPlugins = stringListOf(raw["disabledPlugins"]);
|
|
136
|
+
if (disabledPlugins === void 0) return {
|
|
137
|
+
ok: false,
|
|
138
|
+
failure: {
|
|
139
|
+
kind: "malformed",
|
|
140
|
+
error: "disabledPlugins must be an array"
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
const enabledGlobalExtensions = stringListOf(raw["enabledGlobalExtensions"]);
|
|
144
|
+
if (enabledGlobalExtensions === void 0) return {
|
|
145
|
+
ok: false,
|
|
146
|
+
failure: {
|
|
147
|
+
kind: "malformed",
|
|
148
|
+
error: "enabledGlobalExtensions must be an array"
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
const name = raw["name"];
|
|
152
|
+
const createdAt = raw["createdAt"];
|
|
153
|
+
return {
|
|
154
|
+
ok: true,
|
|
155
|
+
manifest: {
|
|
156
|
+
schema,
|
|
157
|
+
id,
|
|
158
|
+
name: typeof name === "string" && name !== "" ? name : id,
|
|
159
|
+
createdAt: typeof createdAt === "string" ? createdAt : "",
|
|
160
|
+
disabledPlugins,
|
|
161
|
+
enabledGlobalExtensions
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
async function readSpaceManifest(spaceRoot) {
|
|
166
|
+
const path = spaceManifestPath(spaceRoot);
|
|
167
|
+
let raw;
|
|
168
|
+
try {
|
|
169
|
+
raw = await readFile(path, "utf-8");
|
|
170
|
+
} catch (error) {
|
|
171
|
+
if (error.code === "ENOENT") return {
|
|
172
|
+
ok: false,
|
|
173
|
+
failure: { kind: "missing" }
|
|
174
|
+
};
|
|
175
|
+
throw error;
|
|
176
|
+
}
|
|
177
|
+
let parsed;
|
|
178
|
+
try {
|
|
179
|
+
parsed = JSON.parse(raw);
|
|
180
|
+
} catch (error) {
|
|
181
|
+
return {
|
|
182
|
+
ok: false,
|
|
183
|
+
failure: {
|
|
184
|
+
kind: "malformed",
|
|
185
|
+
error: `${path}: ${error instanceof Error ? error.message : String(error)}`
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
return parseSpaceManifest(parsed);
|
|
190
|
+
}
|
|
191
|
+
const SPACE_WATCH_INTERVAL_MS = 2e3;
|
|
192
|
+
function failureMessageOf(failure) {
|
|
193
|
+
if (failure.kind === "malformed") return failure.error;
|
|
194
|
+
if (failure.kind === "unsupported-schema") return `schema ${String(failure.schema)} is not supported (expected ${String(failure.supported)})`;
|
|
195
|
+
return "manifest is missing";
|
|
196
|
+
}
|
|
197
|
+
const WRITE_DEADLINE_MS = 5e3;
|
|
198
|
+
function createSpaceManifestWriter() {
|
|
199
|
+
const serialize = (manifest) => `${JSON.stringify(manifest, null, 2)}\n`;
|
|
200
|
+
return {
|
|
201
|
+
async write({ spaceRoot, manifest }) {
|
|
202
|
+
await withStoreMutex(spaceDirectory(spaceRoot), async () => {
|
|
203
|
+
await writeFileAtomic(spaceManifestPath(spaceRoot), serialize(manifest));
|
|
204
|
+
}, { deadlineMs: WRITE_DEADLINE_MS });
|
|
205
|
+
},
|
|
206
|
+
async update({ spaceRoot, manifest }, mutate) {
|
|
207
|
+
await withStoreMutex(spaceDirectory(spaceRoot), async () => {
|
|
208
|
+
const current = await readSpaceManifest(spaceRoot);
|
|
209
|
+
if (!current.ok && current.failure.kind !== "missing") throw new Error(`${spaceManifestPath(spaceRoot)} is not writable: ${failureMessageOf(current.failure)}`);
|
|
210
|
+
const base = current.ok ? current.manifest : manifest;
|
|
211
|
+
await writeFileAtomic(spaceManifestPath(spaceRoot), serialize(mutate(base)));
|
|
212
|
+
}, { deadlineMs: WRITE_DEADLINE_MS });
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
const SPACE_WATCH_RETRY_MAX_MS = 3e4;
|
|
217
|
+
function watchSpaceManifest(spaceRoot, apply, options = {}) {
|
|
218
|
+
const directory = spaceDirectory(spaceRoot);
|
|
219
|
+
const intervalMs = options.intervalMs ?? SPACE_WATCH_INTERVAL_MS;
|
|
220
|
+
const retryMaxMs = Math.max(intervalMs, options.retryMaxMs ?? SPACE_WATCH_RETRY_MAX_MS);
|
|
221
|
+
const initial = fingerprintTreeOf(directory);
|
|
222
|
+
let disposed = false;
|
|
223
|
+
let isEvaluating = false;
|
|
224
|
+
let isRetrying = false;
|
|
225
|
+
let retryDelayMs = intervalMs;
|
|
226
|
+
let nextAttemptAt = 0;
|
|
227
|
+
let isIncomplete = !initial.isComplete;
|
|
228
|
+
let lastApplied = initial.value;
|
|
229
|
+
let retryFingerprint = initial.value;
|
|
230
|
+
let reportedFailure = "";
|
|
231
|
+
let timer;
|
|
232
|
+
const schedule = (delayMs) => {
|
|
233
|
+
if (disposed) return;
|
|
234
|
+
timer = setTimeout(() => {
|
|
235
|
+
timer = void 0;
|
|
236
|
+
tick();
|
|
237
|
+
}, delayMs);
|
|
238
|
+
timer.unref?.();
|
|
239
|
+
};
|
|
240
|
+
const run = async (fingerprint) => {
|
|
241
|
+
isEvaluating = true;
|
|
242
|
+
let decision = "retry";
|
|
243
|
+
try {
|
|
244
|
+
decision = await apply();
|
|
245
|
+
} catch {
|
|
246
|
+
decision = "retry";
|
|
247
|
+
} finally {
|
|
248
|
+
isEvaluating = false;
|
|
249
|
+
}
|
|
250
|
+
if (disposed) return;
|
|
251
|
+
if (decision === "applied") {
|
|
252
|
+
if (isRetrying) options.onDiagnostic?.("SPACE_WATCH_APPLY_RETRIED", fingerprint.value);
|
|
253
|
+
isRetrying = false;
|
|
254
|
+
retryDelayMs = intervalMs;
|
|
255
|
+
nextAttemptAt = 0;
|
|
256
|
+
lastApplied = fingerprint.value;
|
|
257
|
+
retryFingerprint = "";
|
|
258
|
+
reportedFailure = "";
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
isRetrying = true;
|
|
262
|
+
retryDelayMs = Math.min(retryMaxMs, retryDelayMs * 2);
|
|
263
|
+
retryFingerprint = fingerprint.value;
|
|
264
|
+
nextAttemptAt = Date.now() + retryDelayMs;
|
|
265
|
+
if (reportedFailure !== fingerprint.value) {
|
|
266
|
+
reportedFailure = fingerprint.value;
|
|
267
|
+
options.onDiagnostic?.("SPACE_WATCH_APPLY_FAILED", fingerprint.value);
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
const observe = (fingerprint) => {
|
|
271
|
+
if (fingerprint.isComplete === isIncomplete) {
|
|
272
|
+
isIncomplete = !fingerprint.isComplete;
|
|
273
|
+
options.onDiagnostic?.(isIncomplete ? "SPACE_WATCH_INCOMPLETE" : "SPACE_WATCH_RECOVERED", fingerprint.value);
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
const tick = async () => {
|
|
277
|
+
if (disposed) return;
|
|
278
|
+
if (isEvaluating) {
|
|
279
|
+
schedule(intervalMs);
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
const next = fingerprintTreeOf(directory);
|
|
283
|
+
observe(next);
|
|
284
|
+
const isUnapplied = options.isApplied?.() === false;
|
|
285
|
+
const isFingerprintChanged = next.value !== lastApplied && next.value !== retryFingerprint;
|
|
286
|
+
if (isFingerprintChanged) {
|
|
287
|
+
retryDelayMs = intervalMs;
|
|
288
|
+
isRetrying = false;
|
|
289
|
+
nextAttemptAt = 0;
|
|
290
|
+
}
|
|
291
|
+
const isDue = nextAttemptAt === 0 || Date.now() >= nextAttemptAt;
|
|
292
|
+
const isPending = isRetrying || isUnapplied || next.value !== lastApplied;
|
|
293
|
+
if (isPending && (isFingerprintChanged || isDue)) await run(next);
|
|
294
|
+
else if (isPending) isRetrying = true;
|
|
295
|
+
if (disposed) return;
|
|
296
|
+
schedule(intervalMs);
|
|
297
|
+
};
|
|
298
|
+
schedule(intervalMs);
|
|
299
|
+
return { dispose() {
|
|
300
|
+
disposed = true;
|
|
301
|
+
if (timer !== void 0) {
|
|
302
|
+
clearTimeout(timer);
|
|
303
|
+
timer = void 0;
|
|
304
|
+
}
|
|
305
|
+
} };
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
//#endregion
|
|
309
|
+
//#region src/admission.ts
|
|
310
|
+
function declaresHooks(manifest) {
|
|
311
|
+
const declared = manifest?.capabilities;
|
|
312
|
+
return Array.isArray(declared) && declared.includes(HOOKS_CAPABILITY);
|
|
313
|
+
}
|
|
314
|
+
function trustCommandOf(input) {
|
|
315
|
+
return input.workspaceRoot === void 0 ? "otto trust <root>" : `otto trust ${input.workspaceRoot}`;
|
|
316
|
+
}
|
|
317
|
+
function decideAdmission(input) {
|
|
318
|
+
const risks = [...classifyContributionRisk(input.manifest)];
|
|
319
|
+
if ((input.artifact?.skillCount ?? 0) > 0 && !risks.includes("prompt-injection")) risks.push("prompt-injection");
|
|
320
|
+
if (declaresHooks(input.manifest)) return {
|
|
321
|
+
outcome: "deny",
|
|
322
|
+
reason: `"${input.pluginId}" declares the hooks capability, which can rewrite the permission chain; it is refused inside a workspace`,
|
|
323
|
+
risks
|
|
324
|
+
};
|
|
325
|
+
const trust = input.workspaceTrust ?? "default";
|
|
326
|
+
if (trust === "opted-out") return {
|
|
327
|
+
outcome: "deny",
|
|
328
|
+
reason: `"${input.pluginId}" is turned off for this workspace on this machine`,
|
|
329
|
+
risks,
|
|
330
|
+
optedOut: true
|
|
331
|
+
};
|
|
332
|
+
if (trust === "pending") return {
|
|
333
|
+
outcome: "defer",
|
|
334
|
+
reason: `trust for the workspace of "${input.pluginId}" is still being read`,
|
|
335
|
+
risks
|
|
336
|
+
};
|
|
337
|
+
if (trust === "untrusted") return {
|
|
338
|
+
outcome: "deny",
|
|
339
|
+
reason: `"${input.pluginId}" lives in a workspace this machine has not trusted, and space.workspaceTrust=require is set; run ${trustCommandOf(input)} to load it`,
|
|
340
|
+
risks
|
|
341
|
+
};
|
|
342
|
+
if (trust === "default") return {
|
|
343
|
+
outcome: "allow",
|
|
344
|
+
reason: `"${input.pluginId}" is a local workspace plugin, trusted by default`,
|
|
345
|
+
risks
|
|
346
|
+
};
|
|
347
|
+
return {
|
|
348
|
+
outcome: "allow",
|
|
349
|
+
reason: `"${input.pluginId}" stays inside this trusted workspace; loading needs no approval`,
|
|
350
|
+
risks
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
//#endregion
|
|
355
|
+
//#region src/plugins.ts
|
|
356
|
+
const SPACE_EXTENSIONS_DIRECTORY_NAME = "extensions";
|
|
357
|
+
const SPACE_SKILLS_DIRECTORY_NAME = "skills";
|
|
358
|
+
function spaceExtensionsDirectory(spaceRoot) {
|
|
359
|
+
return join(spaceRoot, SPACE_DIR, SPACE_EXTENSIONS_DIRECTORY_NAME);
|
|
360
|
+
}
|
|
361
|
+
function spacePluginDirectory(spaceRoot, pluginId) {
|
|
362
|
+
return join(spaceExtensionsDirectory(spaceRoot), pluginId);
|
|
363
|
+
}
|
|
364
|
+
function directoriesIn(directory) {
|
|
365
|
+
try {
|
|
366
|
+
return readdirSync(directory, { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => entry.name).sort();
|
|
367
|
+
} catch {
|
|
368
|
+
return [];
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
function readPluginSkillDirectories(pluginDirectory) {
|
|
372
|
+
const skills = join(pluginDirectory, SPACE_SKILLS_DIRECTORY_NAME);
|
|
373
|
+
return directoriesIn(skills).filter((name) => existsSync(join(skills, name, SKILL_FILENAME)));
|
|
374
|
+
}
|
|
375
|
+
function declaresSkills(manifest) {
|
|
376
|
+
return (manifest.capabilities ?? []).includes(SPACE_SKILLS_DIRECTORY_NAME);
|
|
377
|
+
}
|
|
378
|
+
function recordOf(value) {
|
|
379
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : void 0;
|
|
380
|
+
}
|
|
381
|
+
function stringField(record, key) {
|
|
382
|
+
const value = record[key];
|
|
383
|
+
return typeof value === "string" && value !== "" ? value : void 0;
|
|
384
|
+
}
|
|
385
|
+
const SKILL_MAPPING = {
|
|
386
|
+
kind: SPACE_SKILL_KIND,
|
|
387
|
+
declares: declaresSkills,
|
|
388
|
+
enumerate(pluginDirectory, manifest) {
|
|
389
|
+
return readPluginSkillDirectories(pluginDirectory).map((skillId) => ({
|
|
390
|
+
id: skillId,
|
|
391
|
+
title: skillId,
|
|
392
|
+
declared: manifest.contributes ?? {},
|
|
393
|
+
directory: join(pluginDirectory, SPACE_SKILLS_DIRECTORY_NAME, skillId)
|
|
394
|
+
}));
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
const APP_KIND = "app";
|
|
398
|
+
const APP_CONTRIBUTION_KEY = "app";
|
|
399
|
+
const AGENT_KIND = "agent";
|
|
400
|
+
const TOOL_KIND = "tool";
|
|
401
|
+
const TOOL_CONTRIBUTION_KEY = "tools";
|
|
402
|
+
const TOOL_MAPPING = {
|
|
403
|
+
kind: TOOL_KIND,
|
|
404
|
+
declares: (manifest) => Array.isArray(recordOf(manifest.contributes ?? {})?.[TOOL_CONTRIBUTION_KEY]),
|
|
405
|
+
enumerate: (pluginDirectory, manifest) => {
|
|
406
|
+
const list = recordOf(manifest.contributes ?? {})?.[TOOL_CONTRIBUTION_KEY];
|
|
407
|
+
if (!Array.isArray(list)) return [];
|
|
408
|
+
return list.flatMap((element) => {
|
|
409
|
+
const record = recordOf(element);
|
|
410
|
+
if (record === void 0) return [];
|
|
411
|
+
const id = stringField(record, "id");
|
|
412
|
+
const entry = stringField(record, "entry");
|
|
413
|
+
if (id === void 0 || entry === void 0) return [];
|
|
414
|
+
return [{
|
|
415
|
+
id,
|
|
416
|
+
title: stringField(record, "title") ?? id,
|
|
417
|
+
declared: {
|
|
418
|
+
...record,
|
|
419
|
+
id,
|
|
420
|
+
entry
|
|
421
|
+
},
|
|
422
|
+
directory: pluginDirectory
|
|
423
|
+
}];
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
};
|
|
427
|
+
const UI_KIND = "ui";
|
|
428
|
+
const TUI_CONTRIBUTION_KEY = "tui";
|
|
429
|
+
const WEB_CONTRIBUTION_KEY = "web";
|
|
430
|
+
const PANEL_CONTRIBUTION_KEY = "panels";
|
|
431
|
+
const PANEL_DECLARATION_KEY = "panel";
|
|
432
|
+
const PERSONA_CONTRIBUTION_KEY = "personas";
|
|
433
|
+
const REGION_ITEM_CONTRIBUTION_KEY = "regionItems";
|
|
434
|
+
function entriesFromList(list) {
|
|
435
|
+
if (!Array.isArray(list)) return [];
|
|
436
|
+
return list.flatMap((element) => {
|
|
437
|
+
const record = recordOf(element);
|
|
438
|
+
const id = record === void 0 ? void 0 : stringField(record, "id");
|
|
439
|
+
if (record === void 0 || id === void 0) return [];
|
|
440
|
+
const { id: _id, title: _title, ...declared } = record;
|
|
441
|
+
return [{
|
|
442
|
+
id,
|
|
443
|
+
title: stringField(record, "title") ?? id,
|
|
444
|
+
declared
|
|
445
|
+
}];
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
function appEntryOf(pluginDirectory) {
|
|
449
|
+
for (const candidate of [
|
|
450
|
+
"app.ts",
|
|
451
|
+
"app.tsx",
|
|
452
|
+
"app.mjs"
|
|
453
|
+
]) if (existsSync(`${pluginDirectory}/${candidate}`)) return `./${candidate}`;
|
|
454
|
+
return "./app.mjs";
|
|
455
|
+
}
|
|
456
|
+
const APP_MAPPING = {
|
|
457
|
+
kind: APP_KIND,
|
|
458
|
+
declares: (manifest) => (manifest.capabilities ?? []).includes(APP_KIND) || recordOf(manifest.contributes?.[APP_CONTRIBUTION_KEY]) !== void 0,
|
|
459
|
+
enumerate(pluginDirectory, manifest) {
|
|
460
|
+
const entry = stringField(recordOf(manifest.contributes?.[APP_CONTRIBUTION_KEY]) ?? {}, "entry") ?? manifest.main ?? appEntryOf(pluginDirectory);
|
|
461
|
+
return [{
|
|
462
|
+
id: manifest.id,
|
|
463
|
+
title: manifest.name ?? manifest.id,
|
|
464
|
+
declared: { entry },
|
|
465
|
+
directory: pluginDirectory
|
|
466
|
+
}];
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
const AGENT_MAPPING = {
|
|
470
|
+
kind: AGENT_KIND,
|
|
471
|
+
declares: (manifest) => Array.isArray(manifest.contributes?.[PERSONA_CONTRIBUTION_KEY]),
|
|
472
|
+
enumerate: (_pluginDirectory, manifest) => entriesFromList(manifest.contributes?.[PERSONA_CONTRIBUTION_KEY])
|
|
473
|
+
};
|
|
474
|
+
function tuiContributionOf(manifest) {
|
|
475
|
+
const contributes = manifest.contributes ?? {};
|
|
476
|
+
return recordOf(contributes[TUI_CONTRIBUTION_KEY] ?? contributes[UI_KIND] ?? {});
|
|
477
|
+
}
|
|
478
|
+
function webPanelsOf(manifest) {
|
|
479
|
+
const panels = recordOf((manifest.contributes ?? {})[WEB_CONTRIBUTION_KEY])?.[PANEL_CONTRIBUTION_KEY];
|
|
480
|
+
if (!Array.isArray(panels)) return [];
|
|
481
|
+
return panels.flatMap((raw) => {
|
|
482
|
+
const panel = recordOf(raw);
|
|
483
|
+
return panel === void 0 ? [] : [panel];
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
function panelCapabilityOf(pluginDirectory, manifest, panel) {
|
|
487
|
+
const id = stringField(panel, "id");
|
|
488
|
+
const entry = stringField(panel, "entry");
|
|
489
|
+
if (id === void 0 || entry === void 0) return;
|
|
490
|
+
return {
|
|
491
|
+
id,
|
|
492
|
+
title: stringField(panel, "title") ?? manifest.name ?? id,
|
|
493
|
+
declared: { [PANEL_DECLARATION_KEY]: {
|
|
494
|
+
...panel,
|
|
495
|
+
id,
|
|
496
|
+
entry
|
|
497
|
+
} },
|
|
498
|
+
directory: pluginDirectory
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
const PLUGIN_KIND_MAPPINGS = [
|
|
502
|
+
APP_MAPPING,
|
|
503
|
+
SKILL_MAPPING,
|
|
504
|
+
TOOL_MAPPING,
|
|
505
|
+
AGENT_MAPPING,
|
|
506
|
+
{
|
|
507
|
+
kind: UI_KIND,
|
|
508
|
+
declares: (manifest) => {
|
|
509
|
+
const ui = tuiContributionOf(manifest);
|
|
510
|
+
return Array.isArray(ui?.[REGION_ITEM_CONTRIBUTION_KEY]) || recordOf(ui?.["settings"]) !== void 0 || Array.isArray(ui?.["settings"]) || Array.isArray(ui?.[PANEL_CONTRIBUTION_KEY]) || webPanelsOf(manifest).length > 0;
|
|
511
|
+
},
|
|
512
|
+
enumerate: (pluginDirectory, manifest) => {
|
|
513
|
+
const ui = tuiContributionOf(manifest) ?? {};
|
|
514
|
+
const entries = [...entriesFromList(ui[REGION_ITEM_CONTRIBUTION_KEY])];
|
|
515
|
+
const settingsRaw = ui["settings"];
|
|
516
|
+
const settingsList = Array.isArray(settingsRaw) ? settingsRaw : settingsRaw === void 0 ? [] : [settingsRaw];
|
|
517
|
+
for (const raw of settingsList) {
|
|
518
|
+
const setting = recordOf(raw);
|
|
519
|
+
if (setting === void 0) continue;
|
|
520
|
+
const id = stringField(setting, "id") ?? `${manifest.id}.settings`;
|
|
521
|
+
const title = stringField(setting, "title") ?? manifest.name ?? id;
|
|
522
|
+
const order = typeof setting["order"] === "number" ? setting["order"] : 0;
|
|
523
|
+
entries.push({
|
|
524
|
+
id,
|
|
525
|
+
title,
|
|
526
|
+
declared: { settings: {
|
|
527
|
+
...setting,
|
|
528
|
+
order
|
|
529
|
+
} },
|
|
530
|
+
directory: pluginDirectory
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
const panelIds = /* @__PURE__ */ new Set();
|
|
534
|
+
const panels = Array.isArray(ui[PANEL_CONTRIBUTION_KEY]) ? ui[PANEL_CONTRIBUTION_KEY] : [];
|
|
535
|
+
for (const raw of panels) {
|
|
536
|
+
const panel = recordOf(raw);
|
|
537
|
+
if (panel === void 0) continue;
|
|
538
|
+
const capability = panelCapabilityOf(pluginDirectory, manifest, panel);
|
|
539
|
+
if (capability === void 0 || panelIds.has(capability.id)) continue;
|
|
540
|
+
panelIds.add(capability.id);
|
|
541
|
+
entries.push(capability);
|
|
542
|
+
}
|
|
543
|
+
for (const panel of webPanelsOf(manifest)) {
|
|
544
|
+
const capability = panelCapabilityOf(pluginDirectory, manifest, panel);
|
|
545
|
+
if (capability === void 0 || panelIds.has(capability.id)) continue;
|
|
546
|
+
panelIds.add(capability.id);
|
|
547
|
+
entries.push(capability);
|
|
548
|
+
}
|
|
549
|
+
return entries;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
];
|
|
553
|
+
const ALLOWED_BARE_IMPORT = /^(@x-otto\/|node:|react(\/|$))/;
|
|
554
|
+
const IMPORT_PATTERN = /(?:^|[;\n])\s*(?:import|export)\s[^'"]*?from\s*['"]([^'"]+)['"]|(?:^|[;\n])\s*import\s*['"]([^'"]+)['"]|import\(\s*['"]([^'"]+)['"]\s*\)/g;
|
|
555
|
+
function importSpecifiersOf(source) {
|
|
556
|
+
return [...source.matchAll(IMPORT_PATTERN)].flatMap((match) => {
|
|
557
|
+
const specifier = match[1] ?? match[2] ?? match[3];
|
|
558
|
+
return specifier === void 0 ? [] : [specifier];
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
function importIssuesOf(directory, manifest) {
|
|
562
|
+
return contentSurfaceOf(manifest).files.flatMap((entry) => {
|
|
563
|
+
const path = join(directory, entry);
|
|
564
|
+
let source;
|
|
565
|
+
try {
|
|
566
|
+
source = readFileSync(path, "utf8");
|
|
567
|
+
} catch {
|
|
568
|
+
return [{
|
|
569
|
+
path,
|
|
570
|
+
code: "entry-missing",
|
|
571
|
+
message: `declared entry ${entry} does not exist`
|
|
572
|
+
}];
|
|
573
|
+
}
|
|
574
|
+
return importSpecifiersOf(source).filter((specifier) => !specifier.startsWith(".") && !specifier.startsWith("/") && !ALLOWED_BARE_IMPORT.test(specifier)).map((specifier) => ({
|
|
575
|
+
path,
|
|
576
|
+
code: "import-not-allowed",
|
|
577
|
+
message: `import "${specifier}" is not allowed in a workspace plugin (allowed: @x-otto/*, node:*, react)`
|
|
578
|
+
}));
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
function kindsOf(manifest) {
|
|
582
|
+
return PLUGIN_KIND_MAPPINGS.filter((mapping) => mapping.declares(manifest)).map((mapping) => mapping.kind);
|
|
583
|
+
}
|
|
584
|
+
const CONTRIBUTION_CAPABILITIES = {
|
|
585
|
+
tools: TOOL_KIND,
|
|
586
|
+
app: APP_KIND,
|
|
587
|
+
personas: AGENT_KIND,
|
|
588
|
+
tui: UI_KIND,
|
|
589
|
+
ui: UI_KIND,
|
|
590
|
+
web: UI_KIND
|
|
591
|
+
};
|
|
592
|
+
function capabilityIssuesOf(directory, manifest) {
|
|
593
|
+
const declared = new Set(manifest.capabilities ?? []);
|
|
594
|
+
const contributes = manifest.contributes ?? {};
|
|
595
|
+
const issues = [];
|
|
596
|
+
for (const [key, capability] of Object.entries(CONTRIBUTION_CAPABILITIES)) {
|
|
597
|
+
if (contributes[key] === void 0 || declared.has(capability)) continue;
|
|
598
|
+
issues.push({
|
|
599
|
+
path: join(directory, "otto-plugin.json"),
|
|
600
|
+
code: "capability-required",
|
|
601
|
+
message: `contributes.${key} needs capabilities: ["${capability}"]; the declaration is ignored without it`
|
|
602
|
+
});
|
|
603
|
+
}
|
|
604
|
+
return issues;
|
|
605
|
+
}
|
|
606
|
+
function layoutIssuesOf(directory, manifest) {
|
|
607
|
+
if (kindsOf(manifest).length > 0 || hasDeclaredEntrypoint(manifest)) return [];
|
|
608
|
+
return [{
|
|
609
|
+
path: join(directory, "otto-plugin.json"),
|
|
610
|
+
code: "plugin-declares-nothing",
|
|
611
|
+
message: `declares no capability (${PLUGIN_KIND_MAPPINGS.map((mapping) => mapping.kind).join(", ")}) and no component entry; nothing would load`
|
|
612
|
+
}];
|
|
613
|
+
}
|
|
614
|
+
function admissionOf(input, pluginId, directory, manifest) {
|
|
615
|
+
const skillCount = readPluginSkillDirectories(directory).length;
|
|
616
|
+
const decision = decideAdmission({
|
|
617
|
+
pluginId,
|
|
618
|
+
scope: input.scope,
|
|
619
|
+
manifest,
|
|
620
|
+
artifact: { skillCount },
|
|
621
|
+
workspaceRoot: input.spaceRoot,
|
|
622
|
+
...input.workspaceTrust === void 0 ? {} : { workspaceTrust: input.workspaceTrust }
|
|
623
|
+
});
|
|
624
|
+
const issue = decision.outcome === "deny" ? [{
|
|
625
|
+
path: join(directory, "otto-plugin.json"),
|
|
626
|
+
code: decision.optedOut === true ? "admission-opted-out" : "admission-denied",
|
|
627
|
+
message: decision.reason
|
|
628
|
+
}] : [];
|
|
629
|
+
return {
|
|
630
|
+
outcome: decision.outcome,
|
|
631
|
+
issues: issue
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
async function readSpacePlugins(input) {
|
|
635
|
+
const plugins = [];
|
|
636
|
+
const capabilities = [];
|
|
637
|
+
const seen = /* @__PURE__ */ new Set();
|
|
638
|
+
const disabled = new Set(input.disabledPlugins);
|
|
639
|
+
for (const pluginId of directoriesIn(spaceExtensionsDirectory(input.spaceRoot))) {
|
|
640
|
+
const directory = spacePluginDirectory(input.spaceRoot, pluginId);
|
|
641
|
+
const read = await readPluginManifest(directory);
|
|
642
|
+
if (read.kind !== "ok") {
|
|
643
|
+
const message = read.kind === "missing" ? "otto-plugin.json is missing" : read.errors.join("; ");
|
|
644
|
+
const issue = {
|
|
645
|
+
path: join(directory, "otto-plugin.json"),
|
|
646
|
+
code: "plugin-manifest-invalid",
|
|
647
|
+
message
|
|
648
|
+
};
|
|
649
|
+
plugins.push({
|
|
650
|
+
id: pluginId,
|
|
651
|
+
title: pluginId,
|
|
652
|
+
enabled: false,
|
|
653
|
+
admission: "deny",
|
|
654
|
+
kinds: [],
|
|
655
|
+
issues: [issue]
|
|
656
|
+
});
|
|
657
|
+
continue;
|
|
658
|
+
}
|
|
659
|
+
const manifest = read.manifest;
|
|
660
|
+
const admission = admissionOf(input, pluginId, directory, manifest);
|
|
661
|
+
const issues = [
|
|
662
|
+
...admission.issues,
|
|
663
|
+
...layoutIssuesOf(directory, manifest),
|
|
664
|
+
...capabilityIssuesOf(directory, manifest),
|
|
665
|
+
...importIssuesOf(directory, manifest)
|
|
666
|
+
];
|
|
667
|
+
const enabled = !disabled.has(pluginId) && issues.length === 0 && admission.outcome === "allow";
|
|
668
|
+
const kinds = kindsOf(manifest);
|
|
669
|
+
plugins.push({
|
|
670
|
+
id: pluginId,
|
|
671
|
+
title: manifest.name ?? pluginId,
|
|
672
|
+
enabled,
|
|
673
|
+
admission: admission.outcome,
|
|
674
|
+
kinds,
|
|
675
|
+
issues
|
|
676
|
+
});
|
|
677
|
+
for (const mapping of PLUGIN_KIND_MAPPINGS) {
|
|
678
|
+
if (!mapping.declares(manifest)) continue;
|
|
679
|
+
for (const shape of mapping.enumerate(directory, manifest)) {
|
|
680
|
+
const key = spaceCapabilityKey(mapping.kind, shape.id);
|
|
681
|
+
if (seen.has(key)) continue;
|
|
682
|
+
seen.add(key);
|
|
683
|
+
const entry = {
|
|
684
|
+
kind: mapping.kind,
|
|
685
|
+
id: shape.id,
|
|
686
|
+
title: shape.title,
|
|
687
|
+
scope: input.scope,
|
|
688
|
+
enabled,
|
|
689
|
+
pluginId
|
|
690
|
+
};
|
|
691
|
+
capabilities.push({
|
|
692
|
+
entry,
|
|
693
|
+
declared: shape.declared,
|
|
694
|
+
...shape.directory === void 0 ? {} : { directory: shape.directory }
|
|
695
|
+
});
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
return {
|
|
700
|
+
plugins,
|
|
701
|
+
capabilities
|
|
702
|
+
};
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
//#endregion
|
|
706
|
+
//#region src/install-items.ts
|
|
707
|
+
function spaceIdOf(item) {
|
|
708
|
+
return item.spaceId;
|
|
709
|
+
}
|
|
710
|
+
function spacePluginIdOf(item) {
|
|
711
|
+
return item.pluginId;
|
|
712
|
+
}
|
|
713
|
+
function declaredFilesOf(parsed) {
|
|
714
|
+
const files = parsed["files"];
|
|
715
|
+
if (!Array.isArray(files)) return;
|
|
716
|
+
return files.filter((file) => typeof file === "string");
|
|
717
|
+
}
|
|
718
|
+
async function installedItemOf(spaceRoot, spaceId, plugin) {
|
|
719
|
+
const pluginDirectory = spacePluginDirectory(spaceRoot, plugin.id);
|
|
720
|
+
const pluginRead = readPluginManifestSync(pluginDirectory);
|
|
721
|
+
if (pluginRead.kind !== "ok") return;
|
|
722
|
+
const declaredFiles = declaredFilesOf(pluginRead.raw);
|
|
723
|
+
const all = readPluginTextFiles(pluginDirectory);
|
|
724
|
+
const files = declaredFiles === void 0 ? all : all.filter((file) => declaredFiles.some((entry) => file.path === entry || file.path.startsWith(`${entry}/`)));
|
|
725
|
+
if (files.length === 0) return;
|
|
726
|
+
const store = new ArtifactRevisionStore(spaceRoot);
|
|
727
|
+
const staged = {
|
|
728
|
+
kind: "module",
|
|
729
|
+
id: plugin.id,
|
|
730
|
+
scope: spaceScopeOf(spaceId),
|
|
731
|
+
capabilities: [],
|
|
732
|
+
files
|
|
733
|
+
};
|
|
734
|
+
const revision = await store.write(staged);
|
|
735
|
+
return {
|
|
736
|
+
id: spacePluginItemId(spaceId, plugin.id),
|
|
737
|
+
pluginId: plugin.id,
|
|
738
|
+
spaceId,
|
|
739
|
+
directory: join(store.directoryOf("module", plugin.id, revision.digest), "files"),
|
|
740
|
+
manifest: pluginRead.manifest,
|
|
741
|
+
scope: spaceScopeOf(spaceId),
|
|
742
|
+
origin: {
|
|
743
|
+
sourceKind: SPACE_SOURCE_KIND,
|
|
744
|
+
ref: pluginDirectory,
|
|
745
|
+
installedAt: ""
|
|
746
|
+
}
|
|
747
|
+
};
|
|
748
|
+
}
|
|
749
|
+
async function spacePrivateInstalledItems(spaceRoot, spaceId, plugins) {
|
|
750
|
+
const items = [];
|
|
751
|
+
for (const plugin of plugins) {
|
|
752
|
+
if (!plugin.enabled) continue;
|
|
753
|
+
const item = await installedItemOf(spaceRoot, spaceId, plugin);
|
|
754
|
+
if (item !== void 0) items.push(item);
|
|
755
|
+
}
|
|
756
|
+
return items;
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
//#endregion
|
|
760
|
+
//#region src/trust.ts
|
|
761
|
+
function workspaceTrustState(inputs) {
|
|
762
|
+
const isStrict = inputs.policy !== "default" || inputs.spaceAdoption === "require-workspace-trust";
|
|
763
|
+
const port = inputs.projectTrust;
|
|
764
|
+
if (port === void 0) return isStrict ? "untrusted" : "default";
|
|
765
|
+
const state = port.state();
|
|
766
|
+
if (state.status !== "decided") return "pending";
|
|
767
|
+
if (state.record === "readonly") return "opted-out";
|
|
768
|
+
if (state.record === "trust") return "trusted";
|
|
769
|
+
return isStrict ? "untrusted" : "default";
|
|
770
|
+
}
|
|
771
|
+
function spaceWorkspaceTrustPolicyOf(manager) {
|
|
772
|
+
if (manager === void 0) return SPACE_WORKSPACE_TRUST_FIELD_SPEC.default;
|
|
773
|
+
try {
|
|
774
|
+
const raw = manager.get(SPACE_WORKSPACE_TRUST_FIELD_SPEC.key);
|
|
775
|
+
return SPACE_WORKSPACE_TRUST_FIELD_SPEC.parse?.(raw) ?? SPACE_WORKSPACE_TRUST_FIELD_SPEC.default;
|
|
776
|
+
} catch {
|
|
777
|
+
return "require";
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
//#endregion
|
|
782
|
+
//#region src/plugin-history.ts
|
|
783
|
+
const SPACE_HISTORY_LIMIT = 5;
|
|
784
|
+
const PLUGIN_REVISION_KIND = "module";
|
|
785
|
+
const PRESERVED_DIRECTORY_NAMES = ["node_modules", ".git"];
|
|
786
|
+
const REMOVE_TREE = {
|
|
787
|
+
recursive: true,
|
|
788
|
+
force: true
|
|
789
|
+
};
|
|
790
|
+
const REVISION_PREFIX_MINIMUM_LENGTH = 8;
|
|
791
|
+
async function summariesOf(store, pluginId) {
|
|
792
|
+
return (await store.list(PLUGIN_REVISION_KIND, pluginId)).sort((left, right) => left.ref.generation - right.ref.generation);
|
|
793
|
+
}
|
|
794
|
+
function revisionOf(summary, staged) {
|
|
795
|
+
const files = staged.files.map((file) => file.path);
|
|
796
|
+
return {
|
|
797
|
+
digest: summary.ref.digest,
|
|
798
|
+
generation: summary.ref.generation,
|
|
799
|
+
createdAt: summary.createdAt,
|
|
800
|
+
files
|
|
801
|
+
};
|
|
802
|
+
}
|
|
803
|
+
function removeRevision(store, pluginId, digest) {
|
|
804
|
+
return rm(store.directoryOf(PLUGIN_REVISION_KIND, pluginId, digest), REMOVE_TREE);
|
|
805
|
+
}
|
|
806
|
+
function digestOrUndefined(staged) {
|
|
807
|
+
try {
|
|
808
|
+
return digestOf(staged);
|
|
809
|
+
} catch {
|
|
810
|
+
return;
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
async function capturePluginRevision(input) {
|
|
814
|
+
let files;
|
|
815
|
+
try {
|
|
816
|
+
files = readPluginTextFiles(input.directory);
|
|
817
|
+
} catch {
|
|
818
|
+
return;
|
|
819
|
+
}
|
|
820
|
+
if (files.length === 0) return;
|
|
821
|
+
const staged = {
|
|
822
|
+
kind: PLUGIN_REVISION_KIND,
|
|
823
|
+
id: input.pluginId,
|
|
824
|
+
scope: input.scope,
|
|
825
|
+
capabilities: [],
|
|
826
|
+
files
|
|
827
|
+
};
|
|
828
|
+
const digest = digestOrUndefined(staged);
|
|
829
|
+
if (digest === void 0) return;
|
|
830
|
+
const store = new ArtifactRevisionStore(input.spaceRoot);
|
|
831
|
+
const before = await summariesOf(store, input.pluginId);
|
|
832
|
+
if (before.at(-1)?.ref.digest !== digest && before.some((summary) => summary.ref.digest === digest)) await removeRevision(store, input.pluginId, digest);
|
|
833
|
+
const written = await store.write(staged);
|
|
834
|
+
const after = await summariesOf(store, input.pluginId);
|
|
835
|
+
const limit = input.limit ?? 5;
|
|
836
|
+
for (const summary of after.slice(0, Math.max(0, after.length - limit))) await removeRevision(store, input.pluginId, summary.ref.digest);
|
|
837
|
+
const { generation, createdAt, files: paths } = written;
|
|
838
|
+
return {
|
|
839
|
+
digest,
|
|
840
|
+
generation,
|
|
841
|
+
createdAt,
|
|
842
|
+
files: paths
|
|
843
|
+
};
|
|
844
|
+
}
|
|
845
|
+
async function pluginRevisionsOf(spaceRoot, pluginId) {
|
|
846
|
+
const store = new ArtifactRevisionStore(spaceRoot);
|
|
847
|
+
const revisions = [];
|
|
848
|
+
for (const summary of await summariesOf(store, pluginId)) {
|
|
849
|
+
const staged = await store.read(summary.ref);
|
|
850
|
+
if (staged !== void 0) revisions.push(revisionOf(summary, staged));
|
|
851
|
+
}
|
|
852
|
+
return revisions;
|
|
853
|
+
}
|
|
854
|
+
function selectRevision(summaries, digest) {
|
|
855
|
+
if (digest === void 0) return summaries.at(-1);
|
|
856
|
+
const exact = summaries.find((summary) => summary.ref.digest === digest);
|
|
857
|
+
if (exact !== void 0 || digest.length < REVISION_PREFIX_MINIMUM_LENGTH) return exact;
|
|
858
|
+
const matches = summaries.filter((summary) => summary.ref.digest.startsWith(digest));
|
|
859
|
+
return matches.length === 1 ? matches[0] : void 0;
|
|
860
|
+
}
|
|
861
|
+
async function moveIfPresent(source, target) {
|
|
862
|
+
try {
|
|
863
|
+
await rename(source, target);
|
|
864
|
+
return true;
|
|
865
|
+
} catch (error) {
|
|
866
|
+
if (error.code === "ENOENT") return false;
|
|
867
|
+
throw error;
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
async function stageFiles(staging, files) {
|
|
871
|
+
await rm(staging, REMOVE_TREE);
|
|
872
|
+
await mkdir(staging, { recursive: true });
|
|
873
|
+
for (const file of files) {
|
|
874
|
+
const target = join(staging, file.path);
|
|
875
|
+
await mkdir(dirname(target), { recursive: true });
|
|
876
|
+
await writeFile(target, file.content, "utf8");
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
async function replaceDirectory(directory, files) {
|
|
880
|
+
const staging = `${directory}.restore-${process.pid}`;
|
|
881
|
+
const previous = `${directory}.old-${process.pid}`;
|
|
882
|
+
await stageFiles(staging, files);
|
|
883
|
+
await rm(previous, REMOVE_TREE);
|
|
884
|
+
const hadPrevious = await moveIfPresent(directory, previous);
|
|
885
|
+
if (hadPrevious) for (const name of PRESERVED_DIRECTORY_NAMES) await moveIfPresent(join(previous, name), join(staging, name));
|
|
886
|
+
try {
|
|
887
|
+
await rename(staging, directory);
|
|
888
|
+
} catch (error) {
|
|
889
|
+
if (hadPrevious) await rename(previous, directory);
|
|
890
|
+
await rm(staging, REMOVE_TREE);
|
|
891
|
+
throw error;
|
|
892
|
+
}
|
|
893
|
+
await rm(previous, REMOVE_TREE);
|
|
894
|
+
}
|
|
895
|
+
async function restorePluginRevision(input) {
|
|
896
|
+
const store = new ArtifactRevisionStore(input.spaceRoot);
|
|
897
|
+
const summaries = await summariesOf(store, input.pluginId);
|
|
898
|
+
if (summaries.length === 0) return {
|
|
899
|
+
ok: false,
|
|
900
|
+
reason: "no-history"
|
|
901
|
+
};
|
|
902
|
+
const selected = selectRevision(summaries, input.digest);
|
|
903
|
+
const staged = selected === void 0 ? void 0 : await store.read(selected.ref);
|
|
904
|
+
if (selected === void 0 || staged === void 0) return {
|
|
905
|
+
ok: false,
|
|
906
|
+
reason: "unknown-revision"
|
|
907
|
+
};
|
|
908
|
+
await replaceDirectory(input.directory, staged.files);
|
|
909
|
+
return {
|
|
910
|
+
ok: true,
|
|
911
|
+
revision: revisionOf(selected, staged)
|
|
912
|
+
};
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
//#endregion
|
|
916
|
+
//#region src/revisions.ts
|
|
917
|
+
async function storePluginContent(spaceRoot, id, files, itemKind = "module", capabilities = []) {
|
|
918
|
+
const store = new ArtifactRevisionStore(spaceRoot);
|
|
919
|
+
const revision = await store.write({
|
|
920
|
+
kind: itemKind,
|
|
921
|
+
id,
|
|
922
|
+
scope: "space",
|
|
923
|
+
capabilities,
|
|
924
|
+
files
|
|
925
|
+
});
|
|
926
|
+
return {
|
|
927
|
+
directory: join(store.directoryOf("module", id, revision.digest), "files"),
|
|
928
|
+
digest: revision.digest
|
|
929
|
+
};
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
//#endregion
|
|
933
|
+
export { SPACE_HISTORY_LIMIT, capturePluginRevision, createSpaceManifestWriter, decideAdmission, pluginRevisionsOf, readPluginSkillDirectories, readPluginTextFiles, readSpaceManifest, readSpacePlugins, restorePluginRevision, spaceExtensionsDirectory, spaceIdOf, spaceManifestPath, spacePluginDirectory, spacePluginIdOf, spacePrivateInstalledItems, spaceWorkspaceTrustPolicyOf, storePluginContent, watchSpaceManifest, workspaceTrustState };
|
package/package.json
CHANGED
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-otto/space",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.4.0-beta.0",
|
|
4
|
+
"description": "空间能力容器:清单读写(原子写 + 锁 + schema 裁决)、能力校验、目录投影(workspace-hub-rfc M1)",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist"
|
|
7
|
+
],
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@x-otto/atomic-fs": "^0.4.0-beta.0",
|
|
19
|
+
"@x-otto/install": "^0.4.0-beta.0",
|
|
20
|
+
"@x-otto/kernel": "^0.4.0-beta.0",
|
|
21
|
+
"@x-otto/universal": "^0.4.0-beta.0",
|
|
22
|
+
"@x-otto/workspace-artifacts": "^0.4.0-beta.0"
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public",
|
|
26
|
+
"tag": "beta"
|
|
27
|
+
}
|
|
28
|
+
}
|
package/README.md
DELETED