machine-bridge-mcp 0.16.1 → 0.17.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.
Files changed (41) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/CONTRIBUTING.md +14 -0
  3. package/README.md +1 -1
  4. package/browser-extension/manifest.json +2 -2
  5. package/browser-extension/service-worker.js +52 -29
  6. package/docs/ENGINEERING.md +3 -1
  7. package/docs/PROJECT_STANDARDS.md +143 -0
  8. package/docs/RELEASING.md +12 -0
  9. package/docs/TESTING.md +7 -5
  10. package/docs/TOOL_REFERENCE.md +2836 -0
  11. package/package.json +14 -5
  12. package/scripts/commit-message-check.mjs +63 -0
  13. package/scripts/coverage-check.mjs +9 -1
  14. package/scripts/generate-policy-reference.mjs +1 -4
  15. package/scripts/generate-tool-reference.mjs +87 -0
  16. package/scripts/generate-worker-types.mjs +19 -0
  17. package/scripts/markdown.mjs +9 -0
  18. package/src/local/agent-context.mjs +8 -15
  19. package/src/local/cli.mjs +1 -107
  20. package/src/local/daemon-process.mjs +16 -4
  21. package/src/local/default-instructions.mjs +2 -45
  22. package/src/local/git-service.mjs +4 -9
  23. package/src/local/managed-job-plan.mjs +2 -7
  24. package/src/local/managed-jobs.mjs +13 -11
  25. package/src/local/numbers.mjs +9 -0
  26. package/src/local/process-execution.mjs +4 -9
  27. package/src/local/process-sessions.mjs +5 -10
  28. package/src/local/project-metadata.mjs +50 -0
  29. package/src/local/project-package.mjs +2 -45
  30. package/src/local/records.mjs +5 -0
  31. package/src/local/runtime.mjs +5 -12
  32. package/src/local/state-inventory.mjs +139 -0
  33. package/src/local/workspace-file-service.mjs +6 -12
  34. package/src/worker/errors.ts +41 -0
  35. package/src/worker/http.ts +252 -0
  36. package/src/worker/index.ts +33 -17
  37. package/src/worker/oauth-state.ts +170 -0
  38. package/src/worker/observability.ts +111 -0
  39. package/src/worker/pending-calls.ts +109 -0
  40. package/src/worker/policy.ts +79 -0
  41. package/tsconfig.json +1 -1
@@ -0,0 +1,109 @@
1
+ export interface PendingCallRecord {
2
+ id: string;
3
+ socket: WebSocket;
4
+ clientRequestKey?: string;
5
+ timeout: ReturnType<typeof setTimeout>;
6
+ resolve: (value: unknown) => void;
7
+ reject: (error: Error) => void;
8
+ }
9
+
10
+ interface RegisterPendingCall {
11
+ id: string;
12
+ socket: WebSocket;
13
+ clientRequestKey?: string;
14
+ timeoutMs: number;
15
+ onTimeout: (record: PendingCallRecord) => Error;
16
+ }
17
+
18
+ export class PendingCallRegistry {
19
+ private readonly maximum: number;
20
+ private readonly byId = new Map<string, PendingCallRecord>();
21
+ private readonly byRequestKey = new Map<string, string>();
22
+
23
+ constructor(maximum: number) {
24
+ this.maximum = maximum;
25
+ }
26
+
27
+ get size(): number {
28
+ return this.byId.size;
29
+ }
30
+
31
+ hasRequestKey(requestKey?: string): boolean {
32
+ return Boolean(requestKey && this.byRequestKey.has(requestKey));
33
+ }
34
+
35
+ register(input: RegisterPendingCall): Promise<unknown> {
36
+ if (this.byId.size >= this.maximum) throw new Error("too many concurrent daemon tool calls");
37
+ if (this.byId.has(input.id)) throw new Error("duplicate internal daemon call id");
38
+ if (input.clientRequestKey && this.byRequestKey.has(input.clientRequestKey)) {
39
+ throw new Error("duplicate in-flight JSON-RPC request id for this access token");
40
+ }
41
+ return new Promise((resolve, reject) => {
42
+ const timeout = setTimeout(() => {
43
+ const record = this.take(input.id);
44
+ if (!record) return;
45
+ let error: unknown;
46
+ try { error = input.onTimeout(record); }
47
+ catch { error = new Error("pending daemon call timed out"); }
48
+ reject(error instanceof Error ? error : new Error("pending daemon call timed out"));
49
+ }, input.timeoutMs);
50
+ const record: PendingCallRecord = {
51
+ id: input.id,
52
+ socket: input.socket,
53
+ clientRequestKey: input.clientRequestKey,
54
+ timeout,
55
+ resolve,
56
+ reject,
57
+ };
58
+ this.byId.set(input.id, record);
59
+ if (input.clientRequestKey) this.byRequestKey.set(input.clientRequestKey, input.id);
60
+ });
61
+ }
62
+
63
+ resolve(id: string, socket: WebSocket, value: unknown): boolean {
64
+ const record = this.byId.get(id);
65
+ if (!record || record.socket !== socket) return false;
66
+ this.take(id)?.resolve(value);
67
+ return true;
68
+ }
69
+
70
+ reject(id: string, error: Error, socket?: WebSocket): boolean {
71
+ const record = this.byId.get(id);
72
+ if (!record || (socket && record.socket !== socket)) return false;
73
+ this.take(id)?.reject(error);
74
+ return true;
75
+ }
76
+
77
+ cancelRequest(requestKey: string, onCancel: (record: PendingCallRecord) => Error): boolean {
78
+ const id = this.byRequestKey.get(requestKey);
79
+ if (!id) return false;
80
+ const record = this.take(id);
81
+ if (!record) return false;
82
+ record.reject(onCancel(record));
83
+ return true;
84
+ }
85
+
86
+ rejectSocket(socket: WebSocket, createError: (record: PendingCallRecord) => Error): number {
87
+ const ids = [...this.byId.values()].filter((record) => record.socket === socket).map((record) => record.id);
88
+ for (const id of ids) {
89
+ const record = this.take(id);
90
+ if (record) record.reject(createError(record));
91
+ }
92
+ return ids.length;
93
+ }
94
+
95
+ snapshot(): { active: number; request_keys: number; maximum: number } {
96
+ return { active: this.byId.size, request_keys: this.byRequestKey.size, maximum: this.maximum };
97
+ }
98
+
99
+ private take(id: string): PendingCallRecord | undefined {
100
+ const record = this.byId.get(id);
101
+ if (!record) return undefined;
102
+ clearTimeout(record.timeout);
103
+ this.byId.delete(id);
104
+ if (record.clientRequestKey && this.byRequestKey.get(record.clientRequestKey) === id) {
105
+ this.byRequestKey.delete(record.clientRequestKey);
106
+ }
107
+ return record;
108
+ }
109
+ }
@@ -0,0 +1,79 @@
1
+ import contract from "../shared/policy-contract.json" with { type: "json" };
2
+ import toolCatalog from "../shared/tool-catalog.json" with { type: "json" };
3
+
4
+ export type DaemonPolicy = {
5
+ profile: string;
6
+ origin: string;
7
+ revision: number;
8
+ allowWrite: boolean;
9
+ allowExec: boolean;
10
+ execMode: "off" | "direct" | "shell";
11
+ unrestrictedPaths: boolean;
12
+ minimalEnv: boolean;
13
+ exposeAbsolutePaths: boolean;
14
+ };
15
+
16
+ type ToolDefinition = Record<string, unknown> & { name: string; availability?: string };
17
+ type AvailabilityRequirement = {
18
+ profile?: string;
19
+ allowWrite?: boolean;
20
+ execModes?: string[];
21
+ unrestrictedPaths?: boolean;
22
+ minimalEnv?: boolean;
23
+ exposeAbsolutePaths?: boolean;
24
+ };
25
+
26
+ const tools = toolCatalog as ToolDefinition[];
27
+ const definitions = new Map(tools.map((tool) => [tool.name, tool]));
28
+ const origins = new Set(contract.origins.map(String));
29
+ const availability = contract.availability as Record<string, AvailabilityRequirement>;
30
+
31
+ export function sanitizeDaemonPolicy(value: unknown): DaemonPolicy {
32
+ const policy = asObject(value);
33
+ const execMode: DaemonPolicy["execMode"] = policy.execMode === "shell" || policy.execMode === "direct" ? policy.execMode : "off";
34
+ const origin = sanitizeMetadataText(policy.origin, 32);
35
+ return {
36
+ profile: sanitizeMetadataText(policy.profile, 32) ?? "custom",
37
+ origin: origins.has(origin ?? "") ? origin! : "custom",
38
+ revision: Number.isInteger(policy.revision) && Number(policy.revision) > 0
39
+ ? Math.min(Number(policy.revision), 1_000_000)
40
+ : Number(contract.revision),
41
+ allowWrite: policy.allowWrite === true,
42
+ allowExec: execMode !== "off",
43
+ execMode,
44
+ unrestrictedPaths: policy.unrestrictedPaths === true,
45
+ minimalEnv: policy.minimalEnv !== false,
46
+ exposeAbsolutePaths: policy.exposeAbsolutePaths === true,
47
+ };
48
+ }
49
+
50
+ export function policyAllowsAvailability(policy: DaemonPolicy, name: unknown): boolean {
51
+ const requirement = availability[String(name || "")];
52
+ if (!requirement) return false;
53
+ if (requirement.profile && policy.profile !== requirement.profile) return false;
54
+ if (requirement.allowWrite === true && policy.allowWrite !== true) return false;
55
+ if (Array.isArray(requirement.execModes) && !requirement.execModes.includes(policy.execMode)) return false;
56
+ if (typeof requirement.unrestrictedPaths === "boolean" && policy.unrestrictedPaths !== requirement.unrestrictedPaths) return false;
57
+ if (typeof requirement.minimalEnv === "boolean" && policy.minimalEnv !== requirement.minimalEnv) return false;
58
+ if (typeof requirement.exposeAbsolutePaths === "boolean" && policy.exposeAbsolutePaths !== requirement.exposeAbsolutePaths) return false;
59
+ return true;
60
+ }
61
+
62
+ export function sanitizeDaemonTools(value: unknown, policy: DaemonPolicy): string[] {
63
+ if (!Array.isArray(value)) return [];
64
+ return [...new Set(value.filter((item): item is string => {
65
+ if (typeof item !== "string" || item === "server_info") return false;
66
+ const definition = definitions.get(item);
67
+ return Boolean(definition && policyAllowsAvailability(policy, definition.availability));
68
+ }))];
69
+ }
70
+
71
+ function sanitizeMetadataText(value: unknown, maxLength: number): string | undefined {
72
+ if (typeof value !== "string") return undefined;
73
+ const normalized = value.replace(/[\u0000-\u001f\u007f\u200B-\u200F\u202A-\u202E\u2066-\u2069\uFEFF]/g, " ").replace(/\s+/g, " ").trim();
74
+ return normalized ? normalized.slice(0, maxLength) : undefined;
75
+ }
76
+
77
+ function asObject(value: unknown): Record<string, unknown> {
78
+ return value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : {};
79
+ }
package/tsconfig.json CHANGED
@@ -17,7 +17,7 @@
17
17
  },
18
18
  "include": [
19
19
  "src/worker/**/*.ts",
20
- "src/worker/**/*.d.ts",
20
+ ".wrangler/worker-configuration.d.ts",
21
21
  "src/shared/**/*.json"
22
22
  ]
23
23
  }