@dsmm/dsmm 0.1.1 → 0.1.4
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/README.md +24 -13
- package/agent-presets/dsmm-builder/agent.cordis.yml +14 -12
- package/agent-presets/dsmm-builder/preset.yml +1 -1
- package/agent-presets/dsmm-clarifier/agent.cordis.yml +4 -2
- package/agent-presets/dsmm-clarifier/preset.yml +1 -1
- package/agent-presets/dsmm-code-search/agent.cordis.yml +4 -2
- package/agent-presets/dsmm-code-search/preset.yml +1 -1
- package/agent-presets/dsmm-creative/agent.cordis.yml +3 -1
- package/agent-presets/dsmm-creative/preset.yml +1 -1
- package/agent-presets/dsmm-doc-search/agent.cordis.yml +4 -2
- package/agent-presets/dsmm-doc-search/preset.yml +1 -1
- package/agent-presets/dsmm-media-reader/agent.cordis.yml +4 -2
- package/agent-presets/dsmm-media-reader/preset.yml +1 -1
- package/agent-presets/dsmm-oracle/agent.cordis.yml +3 -1
- package/agent-presets/dsmm-oracle/preset.yml +1 -1
- package/agent-presets/dsmm-oracle-2nd/agent.cordis.yml +3 -1
- package/agent-presets/dsmm-oracle-2nd/preset.yml +1 -1
- package/agent-presets/dsmm-orchestrator/agent.cordis.yml +15 -13
- package/agent-presets/dsmm-orchestrator/preset.yml +2 -2
- package/agent-presets/dsmm-plan-critic/agent.cordis.yml +4 -2
- package/agent-presets/dsmm-plan-critic/preset.yml +1 -1
- package/agent-presets/dsmm-planner/agent.cordis.yml +4 -2
- package/agent-presets/dsmm-planner/preset.yml +1 -1
- package/agent-presets/dsmm-reviewer/agent.cordis.yml +4 -2
- package/agent-presets/dsmm-reviewer/preset.yml +1 -1
- package/docs/agent-presets.md +15 -5
- package/docs/compatibility.md +64 -11
- package/docs/model-routing.md +10 -6
- package/docs/profiles.md +83 -0
- package/docs/releasing.md +58 -19
- package/docs/settings-status.md +13 -5
- package/lib/client/ProfilesSection.d.ts +11 -0
- package/lib/client/ProfilesSection.js +62 -0
- package/lib/client/controller.d.ts +77 -0
- package/lib/client/controller.js +198 -0
- package/lib/client/index.d.ts +11 -0
- package/lib/client/index.js +34 -0
- package/lib/client/locales.d.ts +56 -0
- package/lib/client/locales.js +48 -0
- package/lib/client/styles.d.ts +3 -0
- package/lib/client/styles.js +25 -0
- package/lib/client.js +1930 -0
- package/lib/commands.d.ts +3 -3
- package/lib/commands.js +4 -4
- package/lib/dsh-types.d.ts +4 -0
- package/lib/guards.d.ts +2 -2
- package/lib/guards.js +3 -2
- package/lib/index.d.ts +12 -4
- package/lib/index.js +72 -20
- package/lib/mode.d.ts +2 -2
- package/lib/mode.js +1 -1
- package/lib/model-routing.d.ts +2 -2
- package/lib/model-routing.js +17 -2
- package/lib/preset-registry.js +5 -3
- package/lib/profile-remote.d.ts +31 -0
- package/lib/profile-remote.js +106 -0
- package/lib/profile-rpc.d.ts +27 -0
- package/lib/profile-rpc.js +90 -0
- package/lib/profile-runtime.d.ts +44 -0
- package/lib/profile-runtime.js +159 -0
- package/lib/profile-store.d.ts +42 -0
- package/lib/profile-store.js +478 -0
- package/lib/profile-types.d.ts +40 -0
- package/lib/profile-types.js +2 -0
- package/lib/profiles.d.ts +26 -0
- package/lib/profiles.js +237 -0
- package/lib/prompts.d.ts +2 -2
- package/lib/prompts.js +4 -4
- package/lib/role-providers.d.ts +2 -2
- package/lib/role-providers.js +39 -6
- package/lib/role-routing.d.ts +2 -2
- package/lib/role-routing.js +4 -1
- package/lib/role-subagents.d.ts +2 -2
- package/lib/role-subagents.js +6 -4
- package/lib/roles.d.ts +4 -0
- package/lib/roles.js +43 -27
- package/lib/runtime-recovery.d.ts +2 -2
- package/lib/runtime-recovery.js +3 -3
- package/lib/session-metadata.d.ts +13 -0
- package/lib/session-metadata.js +34 -0
- package/lib/session-persistence.d.ts +35 -0
- package/lib/session-persistence.js +304 -0
- package/lib/settings.d.ts +8 -1
- package/lib/settings.js +4 -0
- package/lib/state.d.ts +1 -0
- package/lib/state.js +4 -0
- package/lib/status.js +6 -5
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +62 -15
- package/patches/agent-presets-root.example.cordis.patch.yml +6 -10
- package/prompts/deepseek-v4-pro.md +1 -1
- package/prompts/deepwork.md +3 -3
- package/scripts/repair-session-log.mjs +476 -0
- package/scripts/session-repair-native-verifier.mjs +367 -0
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
import { constants, closeSync, fstatSync, fsyncSync, lstatSync, mkdirSync, openSync, opendirSync, readSync, realpathSync, renameSync, unlinkSync, writeSync } from "node:fs";
|
|
2
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
3
|
+
import { basename, dirname, isAbsolute, join, parse, resolve } from "node:path";
|
|
4
|
+
import { parseTree } from "jsonc-parser";
|
|
5
|
+
import { ABSENT_PROFILE_SELECTION_REVISION } from "./profile-types.js";
|
|
6
|
+
import { DsmmProfileError, MAX_PROFILE_BYTES, MAX_PROFILE_COUNT, MAX_PROFILE_DIRECTORY_ENTRIES, MAX_PROFILE_REVISIONS, parseProfileDocument, profileErrorInfo, validateProfileId, validateProfileRevision } from "./profiles.js";
|
|
7
|
+
/** This directory is supplied by the Host, never by a remote method parameter. */
|
|
8
|
+
export class ProfileStore {
|
|
9
|
+
profileDir;
|
|
10
|
+
timeoutMs;
|
|
11
|
+
pollMs;
|
|
12
|
+
rename;
|
|
13
|
+
rootIdentity;
|
|
14
|
+
constructor(profileDir, options = {}) {
|
|
15
|
+
if (!isAbsolute(profileDir) || dirname(resolve(profileDir)) === resolve(profileDir) || basename(resolve(profileDir)) !== "dsmm-profiles") {
|
|
16
|
+
throw new DsmmProfileError("unsafe-path", "The Host must supply its native profile's dsmm-profiles directory.");
|
|
17
|
+
}
|
|
18
|
+
this.profileDir = resolve(profileDir);
|
|
19
|
+
this.timeoutMs = boundedOption(options.lockTimeoutMs, 2000, 0, 30000);
|
|
20
|
+
this.pollMs = boundedOption(options.lockPollMs, 25, 1, 1000);
|
|
21
|
+
this.rename = options.rename ?? renameSync;
|
|
22
|
+
}
|
|
23
|
+
async describe() {
|
|
24
|
+
return this.locked(() => {
|
|
25
|
+
const entries = this.inventory();
|
|
26
|
+
const profiles = entries.map((name) => {
|
|
27
|
+
const id = name.slice(0, -6);
|
|
28
|
+
try {
|
|
29
|
+
validateProfileId(id);
|
|
30
|
+
if (name !== `${id}.jsonc`)
|
|
31
|
+
throw new DsmmProfileError("unsafe-path", "Profile filename must use lowercase .jsonc and match its ID.");
|
|
32
|
+
const draft = this.draft(id, entries);
|
|
33
|
+
return { id, ...(draft.document.label === undefined ? {} : { label: draft.document.label }), revision: draft.revision };
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
return { id: safeInventoryId(id), revision: null, error: profileErrorInfo(error) };
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
try {
|
|
40
|
+
const selected = this.selection();
|
|
41
|
+
return { profiles, selectedId: selected.selectedId, appliedRevision: selected.appliedRevision, selectionRevision: selected.selectionRevision };
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
// An error is not a successful fallback to the baseline.
|
|
45
|
+
return { profiles, selectedId: null, appliedRevision: null, selectionRevision: "unavailable", selectionError: profileErrorInfo(error, "corrupt-selection") };
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
async read(id) {
|
|
50
|
+
validateProfileId(id);
|
|
51
|
+
return this.locked(() => this.readResult(this.draft(id, this.inventory())));
|
|
52
|
+
}
|
|
53
|
+
async save(request) {
|
|
54
|
+
validateProfileId(request.id);
|
|
55
|
+
const document = parseProfileDocument(request.content, request.id);
|
|
56
|
+
if (request.expectedRevision !== null)
|
|
57
|
+
validateProfileRevision(request.expectedRevision);
|
|
58
|
+
return this.locked((assertLock) => {
|
|
59
|
+
const entries = this.inventory();
|
|
60
|
+
const name = this.draftName(request.id, entries);
|
|
61
|
+
const current = name === undefined ? undefined : this.draft(request.id, entries);
|
|
62
|
+
if ((current?.revision ?? null) !== request.expectedRevision)
|
|
63
|
+
conflict("The profile changed since it was opened. Reload it before saving.");
|
|
64
|
+
if (current === undefined && entries.length >= MAX_PROFILE_COUNT)
|
|
65
|
+
throw new DsmmProfileError("limit", "At most 128 Deepwork profiles are supported.");
|
|
66
|
+
const bytes = Buffer.from(request.content, "utf8");
|
|
67
|
+
const target = join(this.profileDir, `${request.id}.jsonc`);
|
|
68
|
+
this.atomicReplace(target, bytes, () => {
|
|
69
|
+
const latestNames = this.inventory();
|
|
70
|
+
const latest = this.draftName(request.id, latestNames) === undefined ? null : this.draft(request.id, latestNames).revision;
|
|
71
|
+
if (latest !== request.expectedRevision)
|
|
72
|
+
conflict("The profile was edited externally during save. Reload it before saving.");
|
|
73
|
+
}, assertLock);
|
|
74
|
+
return this.readResult({ bytes, content: request.content, revision: digest(bytes), document });
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
async loadSelection() {
|
|
78
|
+
return this.locked(() => this.selection());
|
|
79
|
+
}
|
|
80
|
+
async select(request, validateCandidate) {
|
|
81
|
+
if (request.id !== null) {
|
|
82
|
+
validateProfileId(request.id);
|
|
83
|
+
validateProfileRevision(request.expectedRevision);
|
|
84
|
+
}
|
|
85
|
+
else if (request.expectedRevision !== undefined) {
|
|
86
|
+
throw new DsmmProfileError("validation", "Baseline selection must not include a draft revision.", "expectedRevision");
|
|
87
|
+
}
|
|
88
|
+
if (request.expectedSelectionRevision !== ABSENT_PROFILE_SELECTION_REVISION)
|
|
89
|
+
validateProfileRevision(request.expectedSelectionRevision, "expectedSelectionRevision");
|
|
90
|
+
return this.locked(async (assertLock) => {
|
|
91
|
+
const current = this.selection();
|
|
92
|
+
if (current.selectionRevision !== request.expectedSelectionRevision)
|
|
93
|
+
conflict("The selected profile changed. Refresh before selecting again.");
|
|
94
|
+
const draft = request.id === null ? undefined : this.draft(request.id, this.inventory());
|
|
95
|
+
if (draft !== undefined && draft.revision !== request.expectedRevision)
|
|
96
|
+
conflict("The profile changed before selection. Reload and select its latest revision.");
|
|
97
|
+
let prepared;
|
|
98
|
+
try {
|
|
99
|
+
prepared = await validateCandidate?.(draft === undefined ? null : structuredClone(draft.document));
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
if (error instanceof DsmmProfileError)
|
|
103
|
+
throw error;
|
|
104
|
+
throw new DsmmProfileError("activation", "The candidate profile could not be activated; the previous selection was retained.");
|
|
105
|
+
}
|
|
106
|
+
// The audit may await native work. Recheck security and CAS afterwards.
|
|
107
|
+
assertLock();
|
|
108
|
+
this.assertSelectionRevision(request.expectedSelectionRevision);
|
|
109
|
+
if (draft !== undefined && this.draft(request.id, this.inventory()).revision !== draft.revision)
|
|
110
|
+
conflict("The draft changed during activation. Reload and select again.");
|
|
111
|
+
if (draft !== undefined)
|
|
112
|
+
this.publishRevision(draft, assertLock);
|
|
113
|
+
const pointer = { version: 1, id: request.id, revision: draft?.revision ?? null };
|
|
114
|
+
const bytes = Buffer.from(`${JSON.stringify(pointer)}\n`, "utf8");
|
|
115
|
+
this.atomicReplace(join(this.profileDir, ".selection.json"), bytes, () => {
|
|
116
|
+
this.assertSelectionRevision(request.expectedSelectionRevision);
|
|
117
|
+
if (draft !== undefined && this.draft(request.id, this.inventory()).revision !== draft.revision)
|
|
118
|
+
conflict("The draft changed during selection. Reload and select again.");
|
|
119
|
+
}, assertLock);
|
|
120
|
+
return {
|
|
121
|
+
selection: { selectedId: request.id, appliedRevision: draft?.revision ?? null, selectionRevision: digest(bytes), document: draft?.document ?? null, content: draft?.content ?? null },
|
|
122
|
+
prepared
|
|
123
|
+
};
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
readResult(draft) {
|
|
127
|
+
return { id: draft.document.id, ...(draft.document.label === undefined ? {} : { label: draft.document.label }), revision: draft.revision, content: draft.content };
|
|
128
|
+
}
|
|
129
|
+
inventory() {
|
|
130
|
+
this.validateRoot(false);
|
|
131
|
+
const names = boundedEntries(this.profileDir, MAX_PROFILE_DIRECTORY_ENTRIES).filter((name) => !name.startsWith(".") && /\.jsonc$/iu.test(name)).sort();
|
|
132
|
+
if (names.length > MAX_PROFILE_COUNT)
|
|
133
|
+
throw new DsmmProfileError("limit", "Profile inventory exceeds the 128-profile limit.");
|
|
134
|
+
const seen = new Set();
|
|
135
|
+
for (const name of names) {
|
|
136
|
+
const key = name.toLowerCase();
|
|
137
|
+
if (seen.has(key))
|
|
138
|
+
throw new DsmmProfileError("unsafe-path", "Profile filenames collide case-insensitively; rename the conflicting files manually.");
|
|
139
|
+
seen.add(key);
|
|
140
|
+
}
|
|
141
|
+
return names;
|
|
142
|
+
}
|
|
143
|
+
draftName(id, entries) {
|
|
144
|
+
const expected = `${id}.jsonc`;
|
|
145
|
+
const found = entries.find((name) => name.toLowerCase() === expected);
|
|
146
|
+
if (found !== undefined && found !== expected)
|
|
147
|
+
throw new DsmmProfileError("unsafe-path", "An existing profile has a conflicting filename case; rename it manually.");
|
|
148
|
+
return found;
|
|
149
|
+
}
|
|
150
|
+
draft(id, entries) {
|
|
151
|
+
validateProfileId(id);
|
|
152
|
+
const name = this.draftName(id, entries);
|
|
153
|
+
if (name === undefined)
|
|
154
|
+
throw new DsmmProfileError("not-found", "The requested Deepwork profile does not exist.");
|
|
155
|
+
const file = regularFile(join(this.profileDir, name), MAX_PROFILE_BYTES);
|
|
156
|
+
return { ...file, document: parseProfileDocument(file.content, id) };
|
|
157
|
+
}
|
|
158
|
+
selection() {
|
|
159
|
+
this.validateRoot(false);
|
|
160
|
+
const path = join(this.profileDir, ".selection.json");
|
|
161
|
+
if (status(path) === undefined)
|
|
162
|
+
return { selectedId: null, appliedRevision: null, selectionRevision: ABSENT_PROFILE_SELECTION_REVISION, document: null, content: null };
|
|
163
|
+
try {
|
|
164
|
+
const file = regularFile(path, 1024);
|
|
165
|
+
const pointer = parseSelectionPointer(file.content);
|
|
166
|
+
if (pointer.id === null)
|
|
167
|
+
return { selectedId: null, appliedRevision: null, selectionRevision: file.revision, document: null, content: null };
|
|
168
|
+
validateDirectories(join(this.profileDir, ".revisions"), false);
|
|
169
|
+
const immutable = regularFile(join(this.profileDir, ".revisions", `${pointer.revision}.jsonc`), MAX_PROFILE_BYTES);
|
|
170
|
+
if (immutable.revision !== pointer.revision)
|
|
171
|
+
throw new DsmmProfileError("corrupt-selection", "The selected immutable profile revision failed its SHA256 verification.");
|
|
172
|
+
const document = parseProfileDocument(immutable.content, pointer.id);
|
|
173
|
+
return { selectedId: pointer.id, appliedRevision: pointer.revision, selectionRevision: file.revision, document, content: immutable.content };
|
|
174
|
+
}
|
|
175
|
+
catch (error) {
|
|
176
|
+
if (error instanceof DsmmProfileError && error.code === "unsafe-path")
|
|
177
|
+
throw error;
|
|
178
|
+
throw new DsmmProfileError("corrupt-selection", "The selected profile pointer or immutable revision is missing or invalid. Repair it explicitly; the baseline was not silently selected.");
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
assertSelectionRevision(expected) {
|
|
182
|
+
if (this.selection().selectionRevision !== expected)
|
|
183
|
+
conflict("The selection was edited externally. Refresh before selecting again.");
|
|
184
|
+
}
|
|
185
|
+
publishRevision(draft, assertLock) {
|
|
186
|
+
assertLock();
|
|
187
|
+
const directory = join(this.profileDir, ".revisions");
|
|
188
|
+
validateDirectories(directory, true);
|
|
189
|
+
const path = join(directory, `${draft.revision}.jsonc`);
|
|
190
|
+
if (status(path) !== undefined) {
|
|
191
|
+
const existing = regularFile(path, MAX_PROFILE_BYTES);
|
|
192
|
+
if (existing.revision !== draft.revision || !existing.bytes.equals(draft.bytes))
|
|
193
|
+
throw new DsmmProfileError("corrupt-selection", "An immutable profile revision has been altered; no selection was committed.");
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
if (boundedEntries(directory, MAX_PROFILE_REVISIONS).length >= MAX_PROFILE_REVISIONS)
|
|
197
|
+
throw new DsmmProfileError("limit", "The immutable revision inventory is full; no selection was committed.");
|
|
198
|
+
let fd;
|
|
199
|
+
let created = false;
|
|
200
|
+
try {
|
|
201
|
+
assertLock();
|
|
202
|
+
fd = openSync(path, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | (constants.O_NOFOLLOW ?? 0), 0o600);
|
|
203
|
+
created = true;
|
|
204
|
+
writeAll(fd, draft.bytes);
|
|
205
|
+
fsyncSync(fd);
|
|
206
|
+
closeSync(fd);
|
|
207
|
+
fd = undefined;
|
|
208
|
+
}
|
|
209
|
+
catch (error) {
|
|
210
|
+
if (fd !== undefined)
|
|
211
|
+
closeSync(fd);
|
|
212
|
+
if (created)
|
|
213
|
+
unlinkSync(path);
|
|
214
|
+
throw error;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
atomicReplace(target, bytes, beforeRename, assertLock) {
|
|
218
|
+
assertLock();
|
|
219
|
+
if (status(target) !== undefined)
|
|
220
|
+
assertRegular(status(target));
|
|
221
|
+
const temporary = join(dirname(target), `.dsmm-tmp-${randomUUID()}`);
|
|
222
|
+
let fd;
|
|
223
|
+
let created = false;
|
|
224
|
+
try {
|
|
225
|
+
fd = openSync(temporary, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | (constants.O_NOFOLLOW ?? 0), 0o600);
|
|
226
|
+
created = true;
|
|
227
|
+
writeAll(fd, bytes);
|
|
228
|
+
fsyncSync(fd);
|
|
229
|
+
closeSync(fd);
|
|
230
|
+
fd = undefined;
|
|
231
|
+
this.validateRoot(false);
|
|
232
|
+
if (status(target) !== undefined)
|
|
233
|
+
assertRegular(status(target));
|
|
234
|
+
beforeRename();
|
|
235
|
+
assertLock();
|
|
236
|
+
this.rename(temporary, target);
|
|
237
|
+
created = false;
|
|
238
|
+
// rename is the transaction commit. No fallible work follows it.
|
|
239
|
+
}
|
|
240
|
+
finally {
|
|
241
|
+
if (fd !== undefined)
|
|
242
|
+
closeSync(fd);
|
|
243
|
+
if (created) {
|
|
244
|
+
try {
|
|
245
|
+
unlinkSync(temporary);
|
|
246
|
+
}
|
|
247
|
+
catch { /* Never remove another or previous file to recover a failed commit. */ }
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
validateRoot(create) {
|
|
252
|
+
validateDirectories(this.profileDir, create);
|
|
253
|
+
const current = lstatSync(this.profileDir);
|
|
254
|
+
if (this.rootIdentity !== undefined && !sameFile(this.rootIdentity, current)) {
|
|
255
|
+
throw new DsmmProfileError("unsafe-path", "The profile storage directory was replaced during this Host's lifetime; no operation was committed.");
|
|
256
|
+
}
|
|
257
|
+
this.rootIdentity ??= current;
|
|
258
|
+
}
|
|
259
|
+
async locked(operation) {
|
|
260
|
+
let fd;
|
|
261
|
+
let ownedStatus;
|
|
262
|
+
let ownedBytes;
|
|
263
|
+
const lockPath = join(this.profileDir, ".lock");
|
|
264
|
+
const assertLock = () => {
|
|
265
|
+
this.validateRoot(false);
|
|
266
|
+
const current = status(lockPath);
|
|
267
|
+
if (fd === undefined || ownedStatus === undefined || ownedBytes === undefined || current === undefined || !sameFile(ownedStatus, current) || !sameFile(ownedStatus, fstatSync(fd))) {
|
|
268
|
+
conflict("Profile lock ownership changed during this operation. No selection was committed; retry after the current owner finishes.");
|
|
269
|
+
}
|
|
270
|
+
assertRegular(current);
|
|
271
|
+
assertRegular(fstatSync(fd));
|
|
272
|
+
if (!regularFile(lockPath, 256).bytes.equals(ownedBytes))
|
|
273
|
+
conflict("The profile lock was altered during this operation. No selection was committed; retry after the current owner finishes.");
|
|
274
|
+
};
|
|
275
|
+
try {
|
|
276
|
+
this.validateRoot(true);
|
|
277
|
+
const deadline = performance.now() + this.timeoutMs;
|
|
278
|
+
for (;;) {
|
|
279
|
+
this.validateRoot(false);
|
|
280
|
+
try {
|
|
281
|
+
fd = openSync(lockPath, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | (constants.O_NOFOLLOW ?? 0), 0o600);
|
|
282
|
+
ownedStatus = fstatSync(fd);
|
|
283
|
+
ownedBytes = Buffer.from(`${process.pid} ${randomUUID()}\n`, "utf8");
|
|
284
|
+
writeAll(fd, ownedBytes);
|
|
285
|
+
fsyncSync(fd);
|
|
286
|
+
break;
|
|
287
|
+
}
|
|
288
|
+
catch (error) {
|
|
289
|
+
if (fd !== undefined || errorCode(error) !== "EEXIST")
|
|
290
|
+
throw error;
|
|
291
|
+
const existing = status(lockPath);
|
|
292
|
+
if (existing !== undefined)
|
|
293
|
+
assertRegular(existing);
|
|
294
|
+
if (performance.now() >= deadline)
|
|
295
|
+
throw new DsmmProfileError("lock-timeout", "Another process owns the profile lock. Retry after it finishes; locks are never stolen.");
|
|
296
|
+
await new Promise((done) => setTimeout(done, Math.min(this.pollMs, Math.max(1, deadline - performance.now()))));
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
assertLock();
|
|
300
|
+
return await operation(assertLock);
|
|
301
|
+
}
|
|
302
|
+
catch (error) {
|
|
303
|
+
if (error instanceof DsmmProfileError)
|
|
304
|
+
throw error;
|
|
305
|
+
throw new DsmmProfileError("io", "Deepwork profile storage failed. Existing files and the previous selection were retained.");
|
|
306
|
+
}
|
|
307
|
+
finally {
|
|
308
|
+
if (fd !== undefined) {
|
|
309
|
+
try {
|
|
310
|
+
closeSync(fd);
|
|
311
|
+
}
|
|
312
|
+
catch { /* A committed pointer must remain a successful transaction. */ }
|
|
313
|
+
try {
|
|
314
|
+
this.validateRoot(false);
|
|
315
|
+
const current = status(lockPath);
|
|
316
|
+
if (ownedStatus !== undefined && ownedBytes !== undefined && current !== undefined && sameFile(ownedStatus, current) && current.isFile() && !current.isSymbolicLink() && regularFile(lockPath, 256).bytes.equals(ownedBytes))
|
|
317
|
+
unlinkSync(lockPath);
|
|
318
|
+
}
|
|
319
|
+
catch { /* Do not delete a replacement lock or traverse an altered root. */ }
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
function boundedOption(value, fallback, minimum, maximum) {
|
|
325
|
+
if (value === undefined)
|
|
326
|
+
return fallback;
|
|
327
|
+
if (!Number.isSafeInteger(value) || value < minimum || value > maximum)
|
|
328
|
+
throw new DsmmProfileError("validation", "Profile lock timing is outside supported bounds.");
|
|
329
|
+
return value;
|
|
330
|
+
}
|
|
331
|
+
function digest(bytes) {
|
|
332
|
+
return createHash("sha256").update(bytes).digest("hex");
|
|
333
|
+
}
|
|
334
|
+
function errorCode(error) {
|
|
335
|
+
return typeof error === "object" && error !== null && "code" in error && typeof error.code === "string" ? error.code : undefined;
|
|
336
|
+
}
|
|
337
|
+
function status(path) {
|
|
338
|
+
try {
|
|
339
|
+
return lstatSync(path);
|
|
340
|
+
}
|
|
341
|
+
catch (error) {
|
|
342
|
+
if (errorCode(error) === "ENOENT")
|
|
343
|
+
return undefined;
|
|
344
|
+
throw error;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
function validateDirectories(directory, create) {
|
|
348
|
+
const absolute = resolve(directory);
|
|
349
|
+
const root = parse(absolute).root;
|
|
350
|
+
const parts = [];
|
|
351
|
+
for (let current = absolute; current !== root; current = dirname(current))
|
|
352
|
+
parts.unshift(current);
|
|
353
|
+
parts.unshift(root);
|
|
354
|
+
for (const path of parts) {
|
|
355
|
+
let found = status(path);
|
|
356
|
+
if (found === undefined && create) {
|
|
357
|
+
try {
|
|
358
|
+
mkdirSync(path, { mode: 0o700 });
|
|
359
|
+
}
|
|
360
|
+
catch (error) {
|
|
361
|
+
if (errorCode(error) !== "EEXIST")
|
|
362
|
+
throw error;
|
|
363
|
+
}
|
|
364
|
+
found = status(path);
|
|
365
|
+
}
|
|
366
|
+
if (found === undefined || found.isSymbolicLink() || !found.isDirectory())
|
|
367
|
+
throw new DsmmProfileError("unsafe-path", "Profile directories and every ancestor must be real directories, not links or junctions.");
|
|
368
|
+
const canonical = resolve(realpathSync(path));
|
|
369
|
+
if ((process.platform === "win32" ? canonical.toLowerCase() : canonical) !== (process.platform === "win32" ? path.toLowerCase() : path)) {
|
|
370
|
+
throw new DsmmProfileError("unsafe-path", "Profile storage must not traverse redirected directories or reparse points.");
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
function assertRegular(found) {
|
|
375
|
+
if (found.isSymbolicLink() || !found.isFile() || found.nlink !== 1)
|
|
376
|
+
throw new DsmmProfileError("unsafe-path", "Profile files and lock files must be regular, unlinked files, not symlinks, junctions, directories or hard links.");
|
|
377
|
+
}
|
|
378
|
+
function sameFile(first, second) {
|
|
379
|
+
return first.dev === second.dev && first.ino === second.ino;
|
|
380
|
+
}
|
|
381
|
+
function regularFile(path, maximum) {
|
|
382
|
+
const before = status(path);
|
|
383
|
+
if (before === undefined)
|
|
384
|
+
throw new DsmmProfileError("not-found", "The required profile file does not exist.");
|
|
385
|
+
assertRegular(before);
|
|
386
|
+
if (before.size > maximum)
|
|
387
|
+
throw new DsmmProfileError("limit", "Profile storage contains an oversized file.");
|
|
388
|
+
const fd = openSync(path, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
|
|
389
|
+
try {
|
|
390
|
+
const opened = fstatSync(fd);
|
|
391
|
+
assertRegular(opened);
|
|
392
|
+
if (!sameFile(before, opened))
|
|
393
|
+
conflict("The profile file changed while it was opened. Retry the operation.");
|
|
394
|
+
const buffer = Buffer.alloc(maximum + 1);
|
|
395
|
+
let size = 0;
|
|
396
|
+
while (size < buffer.length) {
|
|
397
|
+
const count = readSync(fd, buffer, size, buffer.length - size, size);
|
|
398
|
+
if (count === 0)
|
|
399
|
+
break;
|
|
400
|
+
size += count;
|
|
401
|
+
}
|
|
402
|
+
if (size > maximum)
|
|
403
|
+
throw new DsmmProfileError("limit", "Profile storage contains an oversized file.");
|
|
404
|
+
const after = fstatSync(fd);
|
|
405
|
+
const named = status(path);
|
|
406
|
+
if (named === undefined || !sameFile(opened, named) || opened.size !== after.size || opened.mtimeMs !== after.mtimeMs || opened.ctimeMs !== after.ctimeMs)
|
|
407
|
+
conflict("The profile file was edited while being read. Retry the operation.");
|
|
408
|
+
const bytes = buffer.subarray(0, size);
|
|
409
|
+
let content;
|
|
410
|
+
try {
|
|
411
|
+
content = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(bytes);
|
|
412
|
+
}
|
|
413
|
+
catch {
|
|
414
|
+
throw new DsmmProfileError("validation", "Profile files must contain valid UTF-8 text.", "content");
|
|
415
|
+
}
|
|
416
|
+
return { bytes, content, revision: digest(bytes) };
|
|
417
|
+
}
|
|
418
|
+
finally {
|
|
419
|
+
closeSync(fd);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
function boundedEntries(directory, maximum) {
|
|
423
|
+
const handle = opendirSync(directory);
|
|
424
|
+
const entries = [];
|
|
425
|
+
try {
|
|
426
|
+
for (;;) {
|
|
427
|
+
const entry = handle.readSync();
|
|
428
|
+
if (entry === null)
|
|
429
|
+
break;
|
|
430
|
+
if (entries.length >= maximum)
|
|
431
|
+
throw new DsmmProfileError("limit", "Profile storage inventory exceeds its supported limit.");
|
|
432
|
+
entries.push(entry.name);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
finally {
|
|
436
|
+
handle.closeSync();
|
|
437
|
+
}
|
|
438
|
+
return entries;
|
|
439
|
+
}
|
|
440
|
+
function writeAll(fd, bytes) {
|
|
441
|
+
let offset = 0;
|
|
442
|
+
while (offset < bytes.length) {
|
|
443
|
+
const count = writeSync(fd, bytes, offset, bytes.length - offset);
|
|
444
|
+
if (count === 0)
|
|
445
|
+
throw new Error("Incomplete profile file write");
|
|
446
|
+
offset += count;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
function parseSelectionPointer(content) {
|
|
450
|
+
const errors = [];
|
|
451
|
+
const tree = parseTree(content, errors, { disallowComments: true, allowTrailingComma: false, allowEmptyContent: false });
|
|
452
|
+
if (tree?.type !== "object" || errors.length > 0 || tree.children?.length !== 3)
|
|
453
|
+
throw new DsmmProfileError("corrupt-selection", "The selected profile pointer must contain exactly three unique fields in valid JSON.");
|
|
454
|
+
let input;
|
|
455
|
+
try {
|
|
456
|
+
input = JSON.parse(content);
|
|
457
|
+
}
|
|
458
|
+
catch {
|
|
459
|
+
throw new DsmmProfileError("corrupt-selection", "The selected profile pointer is not valid JSON.");
|
|
460
|
+
}
|
|
461
|
+
if (typeof input !== "object" || input === null || Array.isArray(input))
|
|
462
|
+
throw new DsmmProfileError("corrupt-selection", "The selected profile pointer must be a versioned object.");
|
|
463
|
+
const record = input;
|
|
464
|
+
if (Object.keys(record).length !== 3 || !["version", "id", "revision"].every((key) => Object.hasOwn(record, key)) || record.version !== 1)
|
|
465
|
+
throw new DsmmProfileError("corrupt-selection", "The selected profile pointer has an unsupported shape or version.");
|
|
466
|
+
if (record.id === null && record.revision === null)
|
|
467
|
+
return { version: 1, id: null, revision: null };
|
|
468
|
+
validateProfileId(record.id);
|
|
469
|
+
validateProfileRevision(record.revision, "revision");
|
|
470
|
+
return { version: 1, id: record.id, revision: record.revision };
|
|
471
|
+
}
|
|
472
|
+
function conflict(message) {
|
|
473
|
+
throw new DsmmProfileError("conflict", message);
|
|
474
|
+
}
|
|
475
|
+
function safeInventoryId(id) {
|
|
476
|
+
return /^[a-zA-Z0-9_-]{1,64}$/u.test(id) ? id : "invalid-profile";
|
|
477
|
+
}
|
|
478
|
+
//# sourceMappingURL=profile-store.js.map
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/** Wire-only profile data: safe to import from the native browser client. */
|
|
2
|
+
export type ProfileErrorCode = "validation" | "conflict" | "not-found" | "lock-timeout" | "unsafe-path" | "io" | "activation" | "corrupt-selection" | "limit";
|
|
3
|
+
export interface ProfileErrorInfo {
|
|
4
|
+
code: ProfileErrorCode;
|
|
5
|
+
message: string;
|
|
6
|
+
field?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface ProfileSummary {
|
|
9
|
+
id: string;
|
|
10
|
+
label?: string;
|
|
11
|
+
revision: string | null;
|
|
12
|
+
error?: ProfileErrorInfo;
|
|
13
|
+
}
|
|
14
|
+
export interface ProfileSelectionState {
|
|
15
|
+
selectedId: string | null;
|
|
16
|
+
appliedRevision: string | null;
|
|
17
|
+
selectionRevision: string;
|
|
18
|
+
}
|
|
19
|
+
export interface ProfileSnapshot extends ProfileSelectionState {
|
|
20
|
+
profiles: ProfileSummary[];
|
|
21
|
+
selectionError?: ProfileErrorInfo;
|
|
22
|
+
}
|
|
23
|
+
export interface ProfileReadResult {
|
|
24
|
+
id: string;
|
|
25
|
+
label?: string;
|
|
26
|
+
revision: string;
|
|
27
|
+
content: string;
|
|
28
|
+
}
|
|
29
|
+
export interface ProfileSaveRequest {
|
|
30
|
+
id: string;
|
|
31
|
+
content: string;
|
|
32
|
+
expectedRevision: string | null;
|
|
33
|
+
}
|
|
34
|
+
export interface ProfileSelectRequest {
|
|
35
|
+
id: string | null;
|
|
36
|
+
expectedRevision?: string;
|
|
37
|
+
expectedSelectionRevision: string;
|
|
38
|
+
}
|
|
39
|
+
export declare const ABSENT_PROFILE_SELECTION_REVISION = "absent";
|
|
40
|
+
//# sourceMappingURL=profile-types.d.ts.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { DsmmPluginConfig, DsmmSettings } from "./settings.js";
|
|
2
|
+
import type { ProfileErrorCode, ProfileErrorInfo } from "./profile-types.js";
|
|
3
|
+
export declare const MAX_PROFILE_BYTES: number;
|
|
4
|
+
export declare const MAX_PROFILE_COUNT = 128;
|
|
5
|
+
export declare const MAX_PROFILE_DIRECTORY_ENTRIES = 1024;
|
|
6
|
+
export declare const MAX_PROFILE_REVISIONS = 1024;
|
|
7
|
+
export type DsmmProfileOverlay = Pick<DsmmPluginConfig, "defaultActive" | "roleRouting" | "workflow" | "guards" | "runtimeRecovery" | "deepseekV4ProCalibration" | "deepseekV4ProDefaultReasoningEffort" | "deepseekV4ProMaxReasoningPresets" | "deepseekFlashCalibration" | "deepseekFlashDefaultReasoningEffort" | "deepseekFlashMaxReasoningPresets">;
|
|
8
|
+
export interface DsmmProfileDocument {
|
|
9
|
+
version: 1;
|
|
10
|
+
id: string;
|
|
11
|
+
label?: string;
|
|
12
|
+
settings: DsmmProfileOverlay;
|
|
13
|
+
}
|
|
14
|
+
export declare class DsmmProfileError extends Error {
|
|
15
|
+
readonly code: ProfileErrorCode;
|
|
16
|
+
readonly field?: string;
|
|
17
|
+
constructor(code: ProfileErrorCode, message: string, field?: string);
|
|
18
|
+
toInfo(): ProfileErrorInfo;
|
|
19
|
+
}
|
|
20
|
+
export declare function profileErrorInfo(error: unknown, fallback?: ProfileErrorCode): ProfileErrorInfo;
|
|
21
|
+
export declare function validateProfileId(id: unknown): asserts id is string;
|
|
22
|
+
export declare function validateProfileRevision(revision: unknown, field?: string): asserts revision is string;
|
|
23
|
+
export declare function parseProfileDocument(content: string, expectedId?: string): DsmmProfileDocument;
|
|
24
|
+
export declare function mergeProfileConfig(baseline: DsmmPluginConfig, overlay: DsmmProfileOverlay): DsmmPluginConfig;
|
|
25
|
+
export declare function resolveProfileSettings(baseline: DsmmPluginConfig, overlay: DsmmProfileOverlay): DsmmSettings;
|
|
26
|
+
//# sourceMappingURL=profiles.d.ts.map
|