pi-knowledge 0.15.0 → 0.16.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/CHANGELOG.md +23 -0
- package/README.md +59 -22
- package/dist/index.d.ts +10 -4
- package/dist/index.js +145 -166
- package/dist/src/coverage-snapshot.d.ts +23 -0
- package/dist/src/coverage-snapshot.js +53 -0
- package/dist/src/daemon/client.d.ts +60 -0
- package/dist/src/daemon/client.js +360 -0
- package/dist/src/daemon/main.d.ts +2 -0
- package/dist/src/daemon/main.js +109 -0
- package/dist/src/daemon/protocol.d.ts +71 -0
- package/dist/src/daemon/protocol.js +170 -0
- package/dist/src/daemon/runtime.d.ts +26 -0
- package/dist/src/daemon/runtime.js +192 -0
- package/dist/src/daemon/server.d.ts +46 -0
- package/dist/src/daemon/server.js +500 -0
- package/dist/src/daemon/service.d.ts +49 -0
- package/dist/src/daemon/service.js +527 -0
- package/dist/src/diagnostics/health.d.ts +6 -0
- package/dist/src/diagnostics/health.js +135 -2
- package/dist/src/engine.d.ts +13 -8
- package/dist/src/engine.js +569 -100
- package/dist/src/filesystem-access.d.ts +9 -0
- package/dist/src/filesystem-access.js +100 -0
- package/dist/src/indexer/chunker.d.ts +10 -0
- package/dist/src/indexer/chunker.js +284 -9
- package/dist/src/mcp/http.d.ts +47 -0
- package/dist/src/mcp/http.js +564 -0
- package/dist/src/mcp/main.d.ts +2 -0
- package/dist/src/mcp/main.js +97 -0
- package/dist/src/mcp/protocol.d.ts +81 -0
- package/dist/src/mcp/protocol.js +444 -0
- package/dist/src/mcp/server.d.ts +42 -0
- package/dist/src/mcp/server.js +378 -0
- package/dist/src/model-worker-client.js +9 -0
- package/dist/src/model-worker-contract.d.ts +3 -0
- package/dist/src/model-worker-contract.js +8 -0
- package/dist/src/model-worker.js +76 -4
- package/dist/src/network/safe-fetch.d.ts +7 -0
- package/dist/src/network/safe-fetch.js +199 -0
- package/dist/src/node-runtime.d.ts +3 -0
- package/dist/src/node-runtime.js +78 -0
- package/dist/src/profiles.d.ts +15 -0
- package/dist/src/profiles.js +160 -0
- package/dist/src/runtime-config.js +5 -6
- package/dist/src/secure-files.d.ts +17 -0
- package/dist/src/secure-files.js +184 -0
- package/dist/src/status.js +12 -7
- package/dist/src/storage/sqlite.d.ts +4 -1
- package/dist/src/storage/sqlite.js +46 -3
- package/dist/src/type-guards.d.ts +1 -0
- package/dist/src/type-guards.js +3 -0
- package/dist/src/watcher/file-watcher.d.ts +3 -1
- package/dist/src/watcher/file-watcher.js +166 -32
- package/dist/src/windows-security.d.ts +4 -0
- package/dist/src/windows-security.js +47 -0
- package/docs/configuration.md +39 -9
- package/docs/daemon-profiles.md +302 -0
- package/docs/mcp-clients.md +266 -0
- package/docs/offline-mode.md +6 -6
- package/package.json +28 -2
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { isObjectRecord } from "../type-guards.js";
|
|
6
|
+
export const DAEMON_PROTOCOL_VERSION = 1;
|
|
7
|
+
export const MAX_RPC_FRAME_BYTES = 16 * 1024 * 1024;
|
|
8
|
+
export const MAX_RPC_CONNECTIONS = 64;
|
|
9
|
+
export const MAX_RPC_IN_FLIGHT = 32;
|
|
10
|
+
export const MAX_PROGRESS_FRAMES = 1_000;
|
|
11
|
+
export const DAEMON_START_TIMEOUT_MS = 20_000;
|
|
12
|
+
export const DAEMON_REQUEST_TIMEOUT_MS = 24 * 60 * 60_000;
|
|
13
|
+
export const DAEMON_SHUTDOWN_GRACE_MS = 10_000;
|
|
14
|
+
export const DAEMON_SHUTDOWN_SETTLE_MS = 2_000;
|
|
15
|
+
const RUNTIME_SIGNATURE_EXCLUSIONS = {
|
|
16
|
+
PI_KNOWLEDGE_AUTO_INJECT: true,
|
|
17
|
+
PI_KNOWLEDGE_DIR: true,
|
|
18
|
+
PI_KNOWLEDGE_MCP_HTTP_TOKEN: true,
|
|
19
|
+
PI_KNOWLEDGE_PROFILE: true,
|
|
20
|
+
PI_KNOWLEDGE_PROFILE_ROOT: true,
|
|
21
|
+
PI_KNOWLEDGE_RUNTIME_DIR: true,
|
|
22
|
+
PI_KNOWLEDGE_WATCH: true,
|
|
23
|
+
};
|
|
24
|
+
export const DAEMON_METHOD_SCOPE = {
|
|
25
|
+
ping: "read",
|
|
26
|
+
shutdown: "admin",
|
|
27
|
+
enable_watchers: "admin",
|
|
28
|
+
plan: "source-read",
|
|
29
|
+
configure: "admin",
|
|
30
|
+
add: "write",
|
|
31
|
+
update: "write",
|
|
32
|
+
remove: "admin",
|
|
33
|
+
clear: "admin",
|
|
34
|
+
import: "write",
|
|
35
|
+
export: "write",
|
|
36
|
+
search: "read",
|
|
37
|
+
symbol_search: "read",
|
|
38
|
+
list: "read",
|
|
39
|
+
status: "read",
|
|
40
|
+
doctor: "read",
|
|
41
|
+
};
|
|
42
|
+
const DAEMON_METHODS = {
|
|
43
|
+
ping: true,
|
|
44
|
+
shutdown: true,
|
|
45
|
+
enable_watchers: true,
|
|
46
|
+
plan: true,
|
|
47
|
+
configure: true,
|
|
48
|
+
add: true,
|
|
49
|
+
update: true,
|
|
50
|
+
remove: true,
|
|
51
|
+
clear: true,
|
|
52
|
+
import: true,
|
|
53
|
+
export: true,
|
|
54
|
+
search: true,
|
|
55
|
+
symbol_search: true,
|
|
56
|
+
list: true,
|
|
57
|
+
status: true,
|
|
58
|
+
doctor: true,
|
|
59
|
+
};
|
|
60
|
+
export function isDaemonMethod(value) {
|
|
61
|
+
return typeof value === "string" && Object.hasOwn(DAEMON_METHODS, value);
|
|
62
|
+
}
|
|
63
|
+
export function parseDaemonClientFrame(value) {
|
|
64
|
+
if (!isObjectRecord(value) || value.version !== DAEMON_PROTOCOL_VERSION || typeof value.type !== "string") {
|
|
65
|
+
throw new Error("Invalid daemon RPC frame");
|
|
66
|
+
}
|
|
67
|
+
if (value.type === "cancel") {
|
|
68
|
+
if (typeof value.id !== "string" || typeof value.token !== "string")
|
|
69
|
+
throw new Error("Invalid daemon cancel frame");
|
|
70
|
+
return value;
|
|
71
|
+
}
|
|
72
|
+
if (value.type !== "request" ||
|
|
73
|
+
typeof value.id !== "string" ||
|
|
74
|
+
typeof value.token !== "string" ||
|
|
75
|
+
typeof value.package_version !== "string" ||
|
|
76
|
+
typeof value.build_identity !== "string" ||
|
|
77
|
+
!isDaemonMethod(value.method) ||
|
|
78
|
+
!isObjectRecord(value.params)) {
|
|
79
|
+
throw new Error("Invalid daemon request frame");
|
|
80
|
+
}
|
|
81
|
+
return value;
|
|
82
|
+
}
|
|
83
|
+
export function parseDaemonServerFrame(value) {
|
|
84
|
+
if (!isObjectRecord(value) || typeof value.type !== "string" || typeof value.id !== "string") {
|
|
85
|
+
throw new Error("Invalid daemon response frame");
|
|
86
|
+
}
|
|
87
|
+
if (value.type === "progress" && typeof value.message === "string")
|
|
88
|
+
return value;
|
|
89
|
+
if (value.type === "result" && "result" in value)
|
|
90
|
+
return value;
|
|
91
|
+
if (value.type === "error" && typeof value.code === "string" && typeof value.message === "string") {
|
|
92
|
+
return value;
|
|
93
|
+
}
|
|
94
|
+
throw new Error("Invalid daemon response frame");
|
|
95
|
+
}
|
|
96
|
+
export function runtimeConfigurationSignature(env = process.env) {
|
|
97
|
+
const entries = Object.entries(env)
|
|
98
|
+
.filter((entry) => entry[1] !== undefined &&
|
|
99
|
+
(entry[0].startsWith("PI_KNOWLEDGE_") || entry[0].startsWith("OPENAI_")) &&
|
|
100
|
+
!RUNTIME_SIGNATURE_EXCLUSIONS[entry[0]])
|
|
101
|
+
.sort(([left], [right]) => left.localeCompare(right));
|
|
102
|
+
const hash = createHash("sha256");
|
|
103
|
+
for (const [key, value] of entries)
|
|
104
|
+
hash.update(key).update("\0").update(value).update("\0");
|
|
105
|
+
return hash.digest("hex");
|
|
106
|
+
}
|
|
107
|
+
let cachedPackageRoot;
|
|
108
|
+
let cachedPackageBuildIdentity;
|
|
109
|
+
function packageRoot() {
|
|
110
|
+
if (cachedPackageRoot)
|
|
111
|
+
return cachedPackageRoot;
|
|
112
|
+
let current = dirname(fileURLToPath(import.meta.url));
|
|
113
|
+
for (let depth = 0; depth < 6; depth++) {
|
|
114
|
+
const candidate = join(current, "package.json");
|
|
115
|
+
if (existsSync(candidate)) {
|
|
116
|
+
const parsed = JSON.parse(readFileSync(candidate, "utf8"));
|
|
117
|
+
if (parsed.name === "pi-knowledge") {
|
|
118
|
+
cachedPackageRoot = current;
|
|
119
|
+
return current;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
const parent = dirname(current);
|
|
123
|
+
if (parent === current)
|
|
124
|
+
break;
|
|
125
|
+
current = parent;
|
|
126
|
+
}
|
|
127
|
+
throw new Error("Unable to resolve pi-knowledge package root");
|
|
128
|
+
}
|
|
129
|
+
function executableFiles(root) {
|
|
130
|
+
const files = [];
|
|
131
|
+
const visit = (directory) => {
|
|
132
|
+
for (const entry of readdirSync(directory, { withFileTypes: true }).sort((left, right) => left.name.localeCompare(right.name))) {
|
|
133
|
+
const path = join(directory, entry.name);
|
|
134
|
+
if (entry.isDirectory())
|
|
135
|
+
visit(path);
|
|
136
|
+
else if (entry.isFile() && (path.endsWith(".js") || path.endsWith(".ts")))
|
|
137
|
+
files.push(path);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
visit(root);
|
|
141
|
+
return files;
|
|
142
|
+
}
|
|
143
|
+
export function packageBuildIdentity() {
|
|
144
|
+
if (cachedPackageBuildIdentity)
|
|
145
|
+
return cachedPackageBuildIdentity;
|
|
146
|
+
const root = packageRoot();
|
|
147
|
+
const compiledRoot = join(root, "dist");
|
|
148
|
+
const sourceRoot = join(root, "src");
|
|
149
|
+
const hash = createHash("sha256");
|
|
150
|
+
const roots = fileURLToPath(import.meta.url).endsWith(".js") && existsSync(compiledRoot) ? [compiledRoot] : [sourceRoot];
|
|
151
|
+
const rootEntry = join(root, fileURLToPath(import.meta.url).endsWith(".js") ? "extension.js" : "index.ts");
|
|
152
|
+
const files = [...(existsSync(rootEntry) ? [rootEntry] : []), ...roots.flatMap(executableFiles)];
|
|
153
|
+
for (const path of files) {
|
|
154
|
+
hash.update(path.slice(root.length)).update("\0").update(readFileSync(path)).update("\0");
|
|
155
|
+
}
|
|
156
|
+
cachedPackageBuildIdentity = hash.digest("hex");
|
|
157
|
+
return cachedPackageBuildIdentity;
|
|
158
|
+
}
|
|
159
|
+
let cachedPackageVersion;
|
|
160
|
+
export function packageVersion() {
|
|
161
|
+
if (cachedPackageVersion)
|
|
162
|
+
return cachedPackageVersion;
|
|
163
|
+
const candidate = join(packageRoot(), "package.json");
|
|
164
|
+
const parsed = JSON.parse(readFileSync(candidate, "utf8"));
|
|
165
|
+
if (typeof parsed.version === "string") {
|
|
166
|
+
cachedPackageVersion = parsed.version;
|
|
167
|
+
return parsed.version;
|
|
168
|
+
}
|
|
169
|
+
throw new Error("Unable to resolve pi-knowledge package version");
|
|
170
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { KnowledgeProfile } from "../profiles.ts";
|
|
2
|
+
import { type DaemonManifest, type DaemonOwnerRecord } from "./protocol.ts";
|
|
3
|
+
export declare class DaemonLockExistsError extends Error {
|
|
4
|
+
constructor();
|
|
5
|
+
}
|
|
6
|
+
export interface DaemonOwnership {
|
|
7
|
+
owner: DaemonOwnerRecord;
|
|
8
|
+
authToken: string;
|
|
9
|
+
lockPath: string;
|
|
10
|
+
manifestPath: string;
|
|
11
|
+
}
|
|
12
|
+
export declare function daemonManifestPath(profile: KnowledgeProfile): string;
|
|
13
|
+
export declare function daemonLockPath(profile: KnowledgeProfile): string;
|
|
14
|
+
export declare function readDaemonManifest(profile: KnowledgeProfile): DaemonManifest | undefined;
|
|
15
|
+
export declare function readDaemonOwner(profile: KnowledgeProfile): DaemonOwnerRecord | undefined;
|
|
16
|
+
export declare function acquireDaemonOwnership(profile: KnowledgeProfile): DaemonOwnership;
|
|
17
|
+
export declare function publishDaemonManifest(profile: KnowledgeProfile, ownership: DaemonOwnership, state: {
|
|
18
|
+
ready: boolean;
|
|
19
|
+
watcherEnabled: boolean;
|
|
20
|
+
lastSnapshotError?: string;
|
|
21
|
+
}): DaemonManifest;
|
|
22
|
+
export declare function processIsAlive(pid: number): boolean;
|
|
23
|
+
export declare function removeStaleOwnership(profile: KnowledgeProfile, expected: DaemonOwnerRecord): void;
|
|
24
|
+
export declare function releaseDaemonOwnership(profile: KnowledgeProfile, ownership: DaemonOwnership, options?: {
|
|
25
|
+
removeEndpoint?: boolean;
|
|
26
|
+
}): void;
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { randomBytes, randomUUID } from "node:crypto";
|
|
2
|
+
import { existsSync, lstatSync, renameSync, rmSync } from "node:fs";
|
|
3
|
+
import { homedir, tmpdir } from "node:os";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import { assertOwnedDirectory, ensureOwnerOnlyDirectory, fsyncDirectory, readPrivateJson, writeJsonAtomic, writeJsonExclusive, } from "../secure-files.js";
|
|
6
|
+
import { isObjectRecord } from "../type-guards.js";
|
|
7
|
+
import { DAEMON_PROTOCOL_VERSION, packageBuildIdentity, packageVersion, runtimeConfigurationSignature, } from "./protocol.js";
|
|
8
|
+
export class DaemonLockExistsError extends Error {
|
|
9
|
+
constructor() {
|
|
10
|
+
super("Knowledge profile daemon ownership is already claimed");
|
|
11
|
+
this.name = "DaemonLockExistsError";
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function parseOwnerRecord(value) {
|
|
15
|
+
if (!isObjectRecord(value) ||
|
|
16
|
+
value.protocol_version !== DAEMON_PROTOCOL_VERSION ||
|
|
17
|
+
typeof value.profile_id !== "string" ||
|
|
18
|
+
typeof value.instance_id !== "string" ||
|
|
19
|
+
typeof value.pid !== "number" ||
|
|
20
|
+
!Number.isInteger(value.pid) ||
|
|
21
|
+
typeof value.started_at !== "number" ||
|
|
22
|
+
typeof value.endpoint !== "string") {
|
|
23
|
+
throw new Error("Invalid daemon owner record");
|
|
24
|
+
}
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
function parseManifest(value) {
|
|
28
|
+
const owner = parseOwnerRecord(value);
|
|
29
|
+
if (!isObjectRecord(value) ||
|
|
30
|
+
typeof value.profile_name !== "string" ||
|
|
31
|
+
typeof value.storage_dir !== "string" ||
|
|
32
|
+
typeof value.package_version !== "string" ||
|
|
33
|
+
typeof value.auth_token !== "string" ||
|
|
34
|
+
typeof value.runtime_configuration_signature !== "string" ||
|
|
35
|
+
typeof value.build_identity !== "string" ||
|
|
36
|
+
typeof value.ready !== "boolean" ||
|
|
37
|
+
typeof value.watcher_enabled !== "boolean" ||
|
|
38
|
+
("last_snapshot_error" in value &&
|
|
39
|
+
value.last_snapshot_error !== undefined &&
|
|
40
|
+
typeof value.last_snapshot_error !== "string")) {
|
|
41
|
+
throw new Error("Invalid daemon manifest");
|
|
42
|
+
}
|
|
43
|
+
return { ...owner, ...value };
|
|
44
|
+
}
|
|
45
|
+
export function daemonManifestPath(profile) {
|
|
46
|
+
return join(profile.runtimeDir, "daemon.json");
|
|
47
|
+
}
|
|
48
|
+
export function daemonLockPath(profile) {
|
|
49
|
+
return join(profile.runtimeDir, "daemon.lock");
|
|
50
|
+
}
|
|
51
|
+
export function readDaemonManifest(profile) {
|
|
52
|
+
const path = daemonManifestPath(profile);
|
|
53
|
+
try {
|
|
54
|
+
if (!existsSync(path))
|
|
55
|
+
return undefined;
|
|
56
|
+
const manifest = parseManifest(readPrivateJson(path, "daemon runtime file"));
|
|
57
|
+
if (manifest.profile_id !== profile.id || manifest.storage_dir !== profile.storageDir) {
|
|
58
|
+
throw new Error(`Daemon manifest does not belong to knowledge profile "${profile.name}"`);
|
|
59
|
+
}
|
|
60
|
+
return manifest;
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
if (error.code === "ENOENT")
|
|
64
|
+
return undefined;
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
export function readDaemonOwner(profile) {
|
|
69
|
+
const path = daemonLockPath(profile);
|
|
70
|
+
try {
|
|
71
|
+
if (!existsSync(path))
|
|
72
|
+
return undefined;
|
|
73
|
+
const owner = parseOwnerRecord(readPrivateJson(path, "daemon runtime file"));
|
|
74
|
+
if (owner.profile_id !== profile.id)
|
|
75
|
+
throw new Error("Daemon lock belongs to a different knowledge profile");
|
|
76
|
+
return owner;
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
if (error.code === "ENOENT")
|
|
80
|
+
return undefined;
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function runtimeSocketRoot() {
|
|
85
|
+
if (process.platform === "win32")
|
|
86
|
+
return "";
|
|
87
|
+
const configured = process.env.PI_KNOWLEDGE_RUNTIME_DIR?.trim();
|
|
88
|
+
const preferred = configured || join(homedir(), ".pi-knowledge", "run");
|
|
89
|
+
const suffixLength = 1 + 12 + 1 + 43 + 5;
|
|
90
|
+
const candidates = [
|
|
91
|
+
preferred,
|
|
92
|
+
join(tmpdir(), `pi-knowledge-${process.getuid?.() ?? "user"}`),
|
|
93
|
+
join("/tmp", `pk-${process.getuid?.() ?? "user"}`),
|
|
94
|
+
];
|
|
95
|
+
for (const candidate of candidates) {
|
|
96
|
+
if (Buffer.byteLength(candidate) + suffixLength > 96)
|
|
97
|
+
continue;
|
|
98
|
+
const canonical = ensureOwnerOnlyDirectory(candidate);
|
|
99
|
+
if (Buffer.byteLength(canonical) + suffixLength <= 96)
|
|
100
|
+
return canonical;
|
|
101
|
+
}
|
|
102
|
+
throw new Error("Unable to create a bounded Unix daemon socket path");
|
|
103
|
+
}
|
|
104
|
+
function createEndpoint(profile) {
|
|
105
|
+
const random = randomBytes(32).toString("base64url");
|
|
106
|
+
if (process.platform === "win32")
|
|
107
|
+
return `\\\\.\\pipe\\pi-knowledge-${profile.id}-${random}`;
|
|
108
|
+
return join(runtimeSocketRoot(), `${profile.id.slice(0, 12)}-${random}.sock`);
|
|
109
|
+
}
|
|
110
|
+
export function acquireDaemonOwnership(profile) {
|
|
111
|
+
ensureOwnerOnlyDirectory(profile.runtimeDir);
|
|
112
|
+
const owner = {
|
|
113
|
+
protocol_version: DAEMON_PROTOCOL_VERSION,
|
|
114
|
+
profile_id: profile.id,
|
|
115
|
+
instance_id: randomUUID(),
|
|
116
|
+
pid: process.pid,
|
|
117
|
+
started_at: Date.now(),
|
|
118
|
+
endpoint: createEndpoint(profile),
|
|
119
|
+
};
|
|
120
|
+
const lockPath = daemonLockPath(profile);
|
|
121
|
+
if (!writeJsonExclusive(lockPath, owner))
|
|
122
|
+
throw new DaemonLockExistsError();
|
|
123
|
+
return {
|
|
124
|
+
owner,
|
|
125
|
+
authToken: randomBytes(32).toString("hex"),
|
|
126
|
+
lockPath,
|
|
127
|
+
manifestPath: daemonManifestPath(profile),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
export function publishDaemonManifest(profile, ownership, state) {
|
|
131
|
+
const manifest = {
|
|
132
|
+
...ownership.owner,
|
|
133
|
+
profile_name: profile.name,
|
|
134
|
+
storage_dir: profile.storageDir,
|
|
135
|
+
package_version: packageVersion(),
|
|
136
|
+
build_identity: packageBuildIdentity(),
|
|
137
|
+
auth_token: ownership.authToken,
|
|
138
|
+
runtime_configuration_signature: runtimeConfigurationSignature(),
|
|
139
|
+
ready: state.ready,
|
|
140
|
+
watcher_enabled: state.watcherEnabled,
|
|
141
|
+
last_snapshot_error: state.lastSnapshotError,
|
|
142
|
+
};
|
|
143
|
+
writeJsonAtomic(ownership.manifestPath, manifest);
|
|
144
|
+
return manifest;
|
|
145
|
+
}
|
|
146
|
+
export function processIsAlive(pid) {
|
|
147
|
+
try {
|
|
148
|
+
process.kill(pid, 0);
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
return error.code === "EPERM";
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function removeUnixEndpoint(endpoint) {
|
|
156
|
+
if (process.platform === "win32" || !existsSync(endpoint))
|
|
157
|
+
return;
|
|
158
|
+
assertOwnedDirectory(dirname(endpoint));
|
|
159
|
+
const info = lstatSync(endpoint);
|
|
160
|
+
if (!info.isSocket())
|
|
161
|
+
throw new Error(`Refusing to remove a non-socket daemon endpoint: ${endpoint}`);
|
|
162
|
+
rmSync(endpoint);
|
|
163
|
+
}
|
|
164
|
+
export function removeStaleOwnership(profile, expected) {
|
|
165
|
+
const current = readDaemonOwner(profile);
|
|
166
|
+
if (!current || current.instance_id !== expected.instance_id || current.endpoint !== expected.endpoint) {
|
|
167
|
+
throw new Error("Daemon ownership changed during stale recovery");
|
|
168
|
+
}
|
|
169
|
+
if (processIsAlive(current.pid))
|
|
170
|
+
throw new Error(`Daemon owner PID ${current.pid} is still alive`);
|
|
171
|
+
const stale = join(profile.runtimeDir, `.daemon-lock-stale-${randomUUID()}`);
|
|
172
|
+
renameSync(daemonLockPath(profile), stale);
|
|
173
|
+
fsyncDirectory(profile.runtimeDir);
|
|
174
|
+
const manifest = readDaemonManifest(profile);
|
|
175
|
+
if (manifest?.instance_id === expected.instance_id)
|
|
176
|
+
rmSync(daemonManifestPath(profile), { force: true });
|
|
177
|
+
rmSync(stale, { force: true });
|
|
178
|
+
fsyncDirectory(profile.runtimeDir);
|
|
179
|
+
// A stale endpoint uses a per-instance random path. Leave it in place: unlinking after a liveness
|
|
180
|
+
// check has an unavoidable bind/unlink race that could remove a foreign live socket.
|
|
181
|
+
}
|
|
182
|
+
export function releaseDaemonOwnership(profile, ownership, options = {}) {
|
|
183
|
+
const current = readDaemonOwner(profile);
|
|
184
|
+
if (!current || current.instance_id !== ownership.owner.instance_id)
|
|
185
|
+
return;
|
|
186
|
+
if (options.removeEndpoint !== false)
|
|
187
|
+
removeUnixEndpoint(ownership.owner.endpoint);
|
|
188
|
+
const manifest = readDaemonManifest(profile);
|
|
189
|
+
if (manifest?.instance_id === ownership.owner.instance_id)
|
|
190
|
+
rmSync(ownership.manifestPath, { force: true });
|
|
191
|
+
rmSync(ownership.lockPath, { force: true });
|
|
192
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { KnowledgeProfile } from "../profiles.ts";
|
|
2
|
+
import { type DaemonManifest } from "./protocol.ts";
|
|
3
|
+
export declare class DaemonAlreadyRunningError extends Error {
|
|
4
|
+
constructor(message: string);
|
|
5
|
+
}
|
|
6
|
+
export declare class DaemonStartupCancelledError extends Error {
|
|
7
|
+
constructor();
|
|
8
|
+
}
|
|
9
|
+
export interface ProfileDaemonServerOptions {
|
|
10
|
+
watchersEnabled?: boolean;
|
|
11
|
+
failStop?: (message: string) => never;
|
|
12
|
+
}
|
|
13
|
+
export declare class ProfileDaemonServer {
|
|
14
|
+
private readonly controlCandidates;
|
|
15
|
+
private readonly sockets;
|
|
16
|
+
private readonly requestControllers;
|
|
17
|
+
private readonly inFlight;
|
|
18
|
+
private readonly service;
|
|
19
|
+
private readonly failStop;
|
|
20
|
+
private readonly profile;
|
|
21
|
+
private readonly options;
|
|
22
|
+
private server;
|
|
23
|
+
private ownership;
|
|
24
|
+
private manifest;
|
|
25
|
+
private accepting;
|
|
26
|
+
private connectionCount;
|
|
27
|
+
private shutdownPromise;
|
|
28
|
+
private startingPromise;
|
|
29
|
+
private stopping;
|
|
30
|
+
private startFailed;
|
|
31
|
+
private controlTrimScheduled;
|
|
32
|
+
private readonly stopped;
|
|
33
|
+
constructor(profile: KnowledgeProfile, options?: ProfileDaemonServerOptions);
|
|
34
|
+
start(): Promise<DaemonManifest>;
|
|
35
|
+
private assertStartupActive;
|
|
36
|
+
private startInternal;
|
|
37
|
+
private writeFrame;
|
|
38
|
+
private scheduleControlCandidateTrim;
|
|
39
|
+
private acceptConnection;
|
|
40
|
+
private refreshManifestDiagnostics;
|
|
41
|
+
private handleRequest;
|
|
42
|
+
private waitForQuiescence;
|
|
43
|
+
waitUntilStopped(): Promise<void>;
|
|
44
|
+
private publishStoppingState;
|
|
45
|
+
shutdown(): Promise<void>;
|
|
46
|
+
}
|