strands-darwin 0.0.37 → 0.0.39
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 +16 -1
- package/README.zh-CN.md +11 -1
- package/dist/src/agent/permission-rules.js +1 -1
- package/dist/src/agent/permission-rules.js.map +1 -1
- package/dist/src/agent/permission.d.ts +4 -1
- package/dist/src/agent/permission.js +21 -4
- package/dist/src/agent/permission.js.map +1 -1
- package/dist/src/agent/runtime.d.ts +6 -2
- package/dist/src/agent/runtime.js +37 -6
- package/dist/src/agent/runtime.js.map +1 -1
- package/dist/src/agent/session-lease.d.ts +31 -0
- package/dist/src/agent/session-lease.js +42 -0
- package/dist/src/agent/session-lease.js.map +1 -0
- package/dist/src/agent/session.d.ts +2 -47
- package/dist/src/agent/session.js +3 -50
- package/dist/src/agent/session.js.map +1 -1
- package/dist/src/cli-list-agents.d.ts +1 -0
- package/dist/src/cli-list-agents.js +12 -0
- package/dist/src/cli-list-agents.js.map +1 -0
- package/dist/src/cli-usage.d.ts +1 -1
- package/dist/src/cli-usage.js +5 -0
- package/dist/src/cli-usage.js.map +1 -1
- package/dist/src/cli.js +9 -1
- package/dist/src/cli.js.map +1 -1
- package/dist/src/collaboration/command.d.ts +5 -0
- package/dist/src/collaboration/command.js +72 -0
- package/dist/src/collaboration/command.js.map +1 -0
- package/dist/src/collaboration/grammar.d.ts +3 -0
- package/dist/src/collaboration/grammar.js +8 -0
- package/dist/src/collaboration/grammar.js.map +1 -0
- package/dist/src/collaboration/local.d.ts +61 -0
- package/dist/src/collaboration/local.js +407 -0
- package/dist/src/collaboration/local.js.map +1 -0
- package/dist/src/collaboration/protocol.d.ts +66 -0
- package/dist/src/collaboration/protocol.js +28 -0
- package/dist/src/collaboration/protocol.js.map +1 -0
- package/dist/src/collaboration/storage.d.ts +36 -0
- package/dist/src/collaboration/storage.js +241 -0
- package/dist/src/collaboration/storage.js.map +1 -0
- package/dist/src/collaboration/tools.d.ts +5 -0
- package/dist/src/collaboration/tools.js +11 -0
- package/dist/src/collaboration/tools.js.map +1 -0
- package/dist/src/commands/custom-commands.d.ts +1 -1
- package/dist/src/commands/custom-commands.js +3 -1
- package/dist/src/commands/custom-commands.js.map +1 -1
- package/dist/src/commands/review-command.d.ts +8 -3
- package/dist/src/commands/review-command.js +17 -3
- package/dist/src/commands/review-command.js.map +1 -1
- package/dist/src/dev-repl.js +8 -1
- package/dist/src/dev-repl.js.map +1 -1
- package/dist/src/headless-protocol.d.ts +6 -2
- package/dist/src/headless-protocol.js +32 -8
- package/dist/src/headless-protocol.js.map +1 -1
- package/dist/src/headless-runner.js +45 -1
- package/dist/src/headless-runner.js.map +1 -1
- package/dist/src/headless.d.ts +2 -1
- package/dist/src/headless.js +5 -5
- package/dist/src/headless.js.map +1 -1
- package/dist/src/list-agents.d.ts +21 -0
- package/dist/src/list-agents.js +219 -0
- package/dist/src/list-agents.js.map +1 -0
- package/dist/src/paths.d.ts +5 -0
- package/dist/src/paths.js +14 -2
- package/dist/src/paths.js.map +1 -1
- package/dist/src/trajectory/record.d.ts +8 -3
- package/dist/src/trajectory/record.js +3 -0
- package/dist/src/trajectory/record.js.map +1 -1
- package/dist/src/trajectory/replay.js +13 -0
- package/dist/src/trajectory/replay.js.map +1 -1
- package/dist/src/trajectory/rewind-history.js +1 -1
- package/dist/src/trajectory/rewind-history.js.map +1 -1
- package/dist/src/trajectory/writer.d.ts +3 -2
- package/dist/src/trajectory/writer.js +4 -0
- package/dist/src/trajectory/writer.js.map +1 -1
- package/dist/src/tui/App.js +83 -1
- package/dist/src/tui/App.js.map +1 -1
- package/dist/src/tui/InputBox.d.ts +1 -1
- package/dist/src/tui/InputBox.js +1 -1
- package/dist/src/tui/help-format.d.ts +2 -2
- package/dist/src/tui/help-format.js +4 -3
- package/dist/src/tui/help-format.js.map +1 -1
- package/package.json +1 -1
package/dist/src/cli-usage.js
CHANGED
|
@@ -19,6 +19,11 @@ export const CLI_USAGE = `Usage: darwin [--resume [<id>]|--session <id>] [--perm
|
|
|
19
19
|
[--continue|--resume [<id>]|--session <id>] [permission flags]
|
|
20
20
|
[--max-model-calls <n>] [--context-offload] [--compact-before]
|
|
21
21
|
darwin sessions
|
|
22
|
+
darwin list-agents
|
|
23
|
+
darwin collaborate [status|list|pending|relations|on|off]
|
|
24
|
+
darwin collaborate send <endpoint-uuid> <literal text>
|
|
25
|
+
darwin collaborate confirm <pending-id> --persist
|
|
26
|
+
darwin collaborate revoke <pair-id>
|
|
22
27
|
darwin permissions test <rule>
|
|
23
28
|
darwin import --from claude-code [--apply]
|
|
24
29
|
darwin doctor
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli-usage.js","sourceRoot":"","sources":["../../src/cli-usage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAE9C,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,QAAQ,EAAE,IAAI,CAAU,CAAC;AACpD,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,WAAW,EAAE,IAAI,CAAU,CAAC;AAE1D,8FAA8F;AAC9F,MAAM,CAAC,MAAM,SAAS,GAAG
|
|
1
|
+
{"version":3,"file":"cli-usage.js","sourceRoot":"","sources":["../../src/cli-usage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAE9C,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,QAAQ,EAAE,IAAI,CAAU,CAAC;AACpD,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,WAAW,EAAE,IAAI,CAAU,CAAC;AAE1D,8FAA8F;AAC9F,MAAM,CAAC,MAAM,SAAS,GAAG;;;;;;;;;;;;;;;;;;;;;CAqBxB,CAAC;AAEF,sEAAsE;AACtE,MAAM,CAAC,MAAM,aAAa,GAAG,gCAAgC,CAAC;AAE9D;;;GAGG;AACH,MAAM,UAAU,cAAc,CAAC,OAAe;IAC5C,OAAO,UAAU,OAAO,KAAK,aAAa,IAAI,CAAC;AACjD,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAC,SAAiB;IAC9C,OAAO,WAAW,SAAS,8BAA8B,SAAS,IAAI,CAAC;AACzE,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,IAAuB;IACpD,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QAAE,OAAO,SAAS,CAAC;IACrE,IAAI,aAAa,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QAAE,OAAO,UAAU,cAAc,IAAI,CAAC;IAC3F,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
package/dist/src/cli.js
CHANGED
|
@@ -26,7 +26,15 @@ else {
|
|
|
26
26
|
const args = process.argv.slice(2);
|
|
27
27
|
if (args[0] === '--')
|
|
28
28
|
args.shift();
|
|
29
|
-
if (args[0] === '
|
|
29
|
+
if (args[0] === 'collaborate') {
|
|
30
|
+
const { runCollaborationCli } = await import('./collaboration/command.js');
|
|
31
|
+
await runCollaborationCli(process.cwd(), args.slice(1));
|
|
32
|
+
}
|
|
33
|
+
else if (args[0] === 'list-agents') {
|
|
34
|
+
const { runListAgentsCli } = await import('./cli-list-agents.js');
|
|
35
|
+
await runListAgentsCli(process.cwd(), args.slice(1));
|
|
36
|
+
}
|
|
37
|
+
else if (args[0] === 'import') {
|
|
30
38
|
const { runImportCli } = await import('./cli-import.js');
|
|
31
39
|
await runImportCli(process.cwd(), args.slice(1));
|
|
32
40
|
}
|
package/dist/src/cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../../src/cli.ts"],"names":[],"mappings":";AACA;;;;;;;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AAE,IAAI,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;QAC9G,MAAM,EAAE,iBAAiB,EAAE,GAAG,MAAM,MAAM,CAAC,oBAAoB,CAAC,CAAC;QACjE,MAAM,iBAAiB,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IACxD,CAAC;SAAM,CAAC;QACN,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,MAAM,CAAC,eAAe,CAAC,CAAC;QAC/C,MAAM,IAAI,EAAE,CAAC;IACf,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { LocalCollaboration } from './local.js';
|
|
2
|
+
export { COLLABORATE_USAGE } from './grammar.js';
|
|
3
|
+
/** Literal user-only command. No invocation, expansion or model-granted trust. */
|
|
4
|
+
export declare function collaborationCommand(local: LocalCollaboration, text: string): Promise<string>;
|
|
5
|
+
export declare function runCollaborationCli(root: string, args: readonly string[]): Promise<void>;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { discoverPeers, LocalCollaboration } from './local.js';
|
|
3
|
+
import { idSchema, policyCommand } from './storage.js';
|
|
4
|
+
import { COLLABORATE_USAGE } from './grammar.js';
|
|
5
|
+
export { COLLABORATE_USAGE } from './grammar.js';
|
|
6
|
+
/** Pure preflight shared by CLI and TUI: no policy, registration or probe on bad grammar. */
|
|
7
|
+
function validateCommand(args) {
|
|
8
|
+
const [verb, id, flag] = args;
|
|
9
|
+
if (args.length === 1 && ['status', 'list', 'pending', 'relations', 'on', 'off'].includes(verb))
|
|
10
|
+
return;
|
|
11
|
+
if (verb === 'confirm' && args.length === 3 && flag === '--persist' && idSchema.safeParse(id).success)
|
|
12
|
+
return;
|
|
13
|
+
if (verb === 'revoke' && args.length === 2 && /^[a-f0-9]{64}$/.test(id ?? ''))
|
|
14
|
+
return;
|
|
15
|
+
if (verb === 'send' && args.length >= 3 && idSchema.safeParse(id).success) {
|
|
16
|
+
const bytes = Buffer.byteLength(args.slice(2).join(' '));
|
|
17
|
+
if (bytes > 0 && bytes <= 4096)
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
throw new Error(COLLABORATE_USAGE);
|
|
21
|
+
}
|
|
22
|
+
/** Literal user-only command. No invocation, expansion or model-granted trust. */
|
|
23
|
+
export async function collaborationCommand(local, text) {
|
|
24
|
+
const command = text.trim();
|
|
25
|
+
const send = /^send\s+(\S+)\s+([\s\S]+)$/.exec(text.trimStart());
|
|
26
|
+
validateCommand(send ? ['send', send[1], send[2]] : (command || 'status').split(/\s+/));
|
|
27
|
+
if (send) {
|
|
28
|
+
local.beginHumanTurn();
|
|
29
|
+
return local.send(send[1], send[2]);
|
|
30
|
+
}
|
|
31
|
+
if (command === 'list')
|
|
32
|
+
return JSON.stringify(await discoverPeers(), null, 2);
|
|
33
|
+
const result = await policyCommand((command || 'status').split(/\s+/));
|
|
34
|
+
if (command === 'off')
|
|
35
|
+
local.close('collaboration off');
|
|
36
|
+
if (command === 'on') {
|
|
37
|
+
local.close('new endpoint requested');
|
|
38
|
+
await local.start();
|
|
39
|
+
}
|
|
40
|
+
return `${result}\nThis endpoint: ${JSON.stringify(local.address ?? null)}; queued: ${local.pending}\n${COLLABORATE_USAGE}`;
|
|
41
|
+
}
|
|
42
|
+
export async function runCollaborationCli(root, args) {
|
|
43
|
+
try {
|
|
44
|
+
// CLI registration exists only for an explicit send. Read projections do not
|
|
45
|
+
// create policy; first startup/on initializes it with no cross-project grants.
|
|
46
|
+
validateCommand(args.length ? args : ['status']);
|
|
47
|
+
if (args[0] === 'list') {
|
|
48
|
+
console.log(JSON.stringify(await discoverPeers(), null, 2));
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (args[0] !== 'send') {
|
|
52
|
+
console.log(await policyCommand(args.length ? args : ['status']));
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const local = new LocalCollaboration(root, `cli-${randomUUID()}`);
|
|
56
|
+
await local.start();
|
|
57
|
+
// argv already separates the target from literal text; do not reparse it as
|
|
58
|
+
// TUI command whitespace and lose leading spaces/newlines in a quoted body.
|
|
59
|
+
try {
|
|
60
|
+
local.beginHumanTurn();
|
|
61
|
+
console.log(await local.send(args[1], args.slice(2).join(' ')));
|
|
62
|
+
}
|
|
63
|
+
finally {
|
|
64
|
+
local.close('CLI send finished');
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
console.error(`collaborate: ${error instanceof Error ? error.message : 'local operation failed'}`);
|
|
69
|
+
process.exitCode = 1;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=command.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"command.js","sourceRoot":"","sources":["../../../src/collaboration/command.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAC/D,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAEvD,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEjD,6FAA6F;AAC7F,SAAS,eAAe,CAAC,IAAuB;IAC9C,MAAM,CAAC,IAAI,EAAE,EAAE,EAAE,IAAI,CAAC,GAAG,IAAI,CAAC;IAC9B,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,IAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,WAAW,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC,QAAQ,CAAC,IAAK,CAAC;QAAE,OAAO;IACzG,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,IAAI,I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C,CAAC,CAAC;YAAC,OAAO;QAAC,CAAC;QACtG,MAAM,KAAK,GAAG,IAAI,kBAAkB,CAAC,IAAI,EAAE,OAAO,UAAU,EAAE,EAAE,CAAC,CAAC;QAClE,MAAM,KAAK,CAAC,KAAK,EAAE,CAAC;QACpB,4EAA4E;QAC5E,4EAA4E;QAC5E,IAAI,CAAC;YAAC,KAAK,CAAC,cAAc,EAAE,CAAC;YAAC,OAAO,CAAC,GAAG,CAAC,MAAM,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAE,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QAAC,CAAC;gBACzF,CAAC;YAAC,KAAK,CAAC,KAAK,CAAC,mBAAmB,CAAC,CAAC;QAAC,CAAC;IAC/C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,KAAK,CAAC,gBAAgB,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,wBAAwB,EAAE,CAAC,CAAC;QACnG,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC;IACvB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/** Pure canonical user grammar; safe to import from bounded local help. */
|
|
2
|
+
export declare const COLLABORATION_GRAMMAR: readonly ['/collaborate [status|list|pending|relations|on|off]', '/collaborate send <endpoint-uuid> <literal text>', '/collaborate confirm <pending-id> --persist; /collaborate revoke <pair-id>'];
|
|
3
|
+
export declare const COLLABORATE_USAGE: string;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Pure canonical user grammar; safe to import from bounded local help. */
|
|
2
|
+
export const COLLABORATION_GRAMMAR = [
|
|
3
|
+
'/collaborate [status|list|pending|relations|on|off]',
|
|
4
|
+
'/collaborate send <endpoint-uuid> <literal text>',
|
|
5
|
+
'/collaborate confirm <pending-id> --persist; /collaborate revoke <pair-id>',
|
|
6
|
+
];
|
|
7
|
+
export const COLLABORATE_USAGE = COLLABORATION_GRAMMAR.join('; ');
|
|
8
|
+
//# sourceMappingURL=grammar.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grammar.js","sourceRoot":"","sources":["../../../src/collaboration/grammar.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,MAAM,CAAC,MAAM,qBAAqB,GAAG;IACnC,qDAAqD;IACrD,kDAAkD;IAClD,4EAA4E;CACpE,CAAC;AACX,MAAM,CAAC,MAAM,iBAAiB,GAAG,qBAAqB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { type PeerAddress, type PeerInput, type PeerTransport } from './protocol.js';
|
|
2
|
+
export declare function socketPath(id: string): string;
|
|
3
|
+
/** Bounded metadata-only endpoint projection, separate from immutable lease discovery. */
|
|
4
|
+
export declare function discoverPeers(): Promise<{
|
|
5
|
+
endpoints: PeerAddress[];
|
|
6
|
+
omitted: number;
|
|
7
|
+
uninspected: number;
|
|
8
|
+
scanLimited: boolean;
|
|
9
|
+
scope: string;
|
|
10
|
+
}>;
|
|
11
|
+
/** One parent session, never a daemon or SDK loop. Only drivers drain the inbox. */
|
|
12
|
+
export declare class LocalCollaboration implements PeerTransport {
|
|
13
|
+
private readonly projectRoot;
|
|
14
|
+
readonly session: string;
|
|
15
|
+
private readonly readOnly;
|
|
16
|
+
private server;
|
|
17
|
+
private registration;
|
|
18
|
+
private sockets;
|
|
19
|
+
private inbox;
|
|
20
|
+
private listeners;
|
|
21
|
+
private notices;
|
|
22
|
+
private seen;
|
|
23
|
+
private chainCounts;
|
|
24
|
+
private cause?;
|
|
25
|
+
private outgoing;
|
|
26
|
+
private peerTurn;
|
|
27
|
+
private lastPeerId?;
|
|
28
|
+
private denied;
|
|
29
|
+
private admitted;
|
|
30
|
+
private window;
|
|
31
|
+
private accepting;
|
|
32
|
+
private stopped;
|
|
33
|
+
private generation;
|
|
34
|
+
private canonicalRoot;
|
|
35
|
+
constructor(projectRoot: string, session: string, readOnly?: () => boolean);
|
|
36
|
+
get project(): string;
|
|
37
|
+
get address(): PeerAddress | undefined;
|
|
38
|
+
get pending(): number;
|
|
39
|
+
get active(): boolean;
|
|
40
|
+
subscribe(listener: () => void): () => void;
|
|
41
|
+
takeNotices(): string[];
|
|
42
|
+
private changed;
|
|
43
|
+
private notice;
|
|
44
|
+
start(): Promise<void>;
|
|
45
|
+
/** Headless closes admission before draining a finite snapshot; not an idle daemon. */
|
|
46
|
+
stopAdmission(): void;
|
|
47
|
+
close(reason: string): void;
|
|
48
|
+
private receive;
|
|
49
|
+
private respond;
|
|
50
|
+
private fresh;
|
|
51
|
+
private admit;
|
|
52
|
+
/** Only a driver at idle takes input. Runtime rechecks it immediately before invocation. */
|
|
53
|
+
take(): PeerInput | undefined;
|
|
54
|
+
validateDelivery(input: PeerInput): void;
|
|
55
|
+
beginHumanTurn(): void;
|
|
56
|
+
beginPeerTurn(input: PeerInput): void;
|
|
57
|
+
permissionDenied(): void;
|
|
58
|
+
get fromPeer(): boolean;
|
|
59
|
+
get peerReadOnly(): boolean;
|
|
60
|
+
send(target: string, text: string): Promise<string>;
|
|
61
|
+
}
|
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
import { randomBytes, randomUUID } from 'node:crypto';
|
|
2
|
+
import { chmodSync, lstatSync, opendirSync, unlinkSync } from 'node:fs';
|
|
3
|
+
import net from 'node:net';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
import { collaborationDir } from '../paths.js';
|
|
7
|
+
import { authorization, canonicalProject, checkStore, idSchema, readPolicy, readPrivate, requestCooperation, withPolicy, writePrivate } from './storage.js';
|
|
8
|
+
import { addressSchema, authentic, CHAIN_TTL_MS, envelopeSchema, IO_TIMEOUT_MS, MAX_FRAME_BYTES, MAX_HOPS, MAX_QUEUE, MESSAGE_TTL_MS, sign } from './protocol.js';
|
|
9
|
+
const registrationSchema = z.object({ address: addressSchema, pid: z.number().int().positive(), secret: z.string().regex(/^[a-f0-9]{64}$/) }).strict();
|
|
10
|
+
const wireSchema = z.object({ body: z.unknown(), mac: z.string() }).strict();
|
|
11
|
+
const probeSchema = z.object({ kind: z.literal('probe'), target: idSchema, nonce: idSchema }).strict();
|
|
12
|
+
const messageSchema = z.object({ kind: z.literal('message'), envelope: envelopeSchema }).strict();
|
|
13
|
+
const responseSchema = z.object({ nonce: idSchema, result: z.string().max(4096) }).strict();
|
|
14
|
+
const sameAddress = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
15
|
+
export function socketPath(id) {
|
|
16
|
+
idSchema.parse(id);
|
|
17
|
+
const file = path.join(collaborationDir(), `${id}.sock`);
|
|
18
|
+
// sockaddr_un portability: leave room for NUL on supported POSIX systems.
|
|
19
|
+
if (Buffer.byteLength(file) > 103)
|
|
20
|
+
throw new Error('Collaboration socket path exceeds 103 bytes; use a shorter HOME (no TCP fallback)');
|
|
21
|
+
return file;
|
|
22
|
+
}
|
|
23
|
+
function registration(id) {
|
|
24
|
+
idSchema.parse(id);
|
|
25
|
+
const record = registrationSchema.parse(readPrivate(`${id}.json`));
|
|
26
|
+
if (record.address.endpoint !== id || record.address.node !== readPolicy().node
|
|
27
|
+
|| canonicalProject(record.address.project) !== record.address.project)
|
|
28
|
+
throw new Error('Endpoint identity mismatch');
|
|
29
|
+
const info = lstatSync(socketPath(id));
|
|
30
|
+
if (!info.isSocket() || info.uid !== process.getuid?.() || (info.mode & 0o077))
|
|
31
|
+
throw new Error('Unsafe endpoint socket');
|
|
32
|
+
// PID is diagnostic only. A signed live challenge, not signal 0, proves identity.
|
|
33
|
+
return record;
|
|
34
|
+
}
|
|
35
|
+
function readFrame(socket) {
|
|
36
|
+
return new Promise((resolve, reject) => {
|
|
37
|
+
let bytes = Buffer.alloc(0);
|
|
38
|
+
const timer = setTimeout(() => finish(new Error('Collaboration frame timeout')), IO_TIMEOUT_MS);
|
|
39
|
+
const finish = (error, value) => {
|
|
40
|
+
clearTimeout(timer);
|
|
41
|
+
socket.off('data', data);
|
|
42
|
+
socket.off('error', fail);
|
|
43
|
+
socket.off('end', end);
|
|
44
|
+
socket.off('close', end);
|
|
45
|
+
if (error) {
|
|
46
|
+
socket.destroy();
|
|
47
|
+
reject(error);
|
|
48
|
+
}
|
|
49
|
+
else
|
|
50
|
+
resolve(value);
|
|
51
|
+
};
|
|
52
|
+
const fail = (error) => finish(error);
|
|
53
|
+
const end = () => finish(new Error('Partial collaboration frame'));
|
|
54
|
+
const data = (chunk) => {
|
|
55
|
+
if (bytes.length + chunk.length > MAX_FRAME_BYTES) {
|
|
56
|
+
finish(new Error('Collaboration frame too large'));
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
bytes = Buffer.concat([bytes, chunk]);
|
|
60
|
+
const newline = bytes.indexOf(10);
|
|
61
|
+
if (newline < 0)
|
|
62
|
+
return;
|
|
63
|
+
if (newline !== bytes.length - 1) {
|
|
64
|
+
finish(new Error('Only one collaboration frame per connection'));
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
finish(undefined, JSON.parse(bytes.subarray(0, newline).toString('utf8')));
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
finish(new Error('Malformed collaboration JSON'));
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
socket.on('data', data);
|
|
75
|
+
socket.once('error', fail);
|
|
76
|
+
socket.once('end', end);
|
|
77
|
+
socket.once('close', end);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
async function exchange(record, body, secret, nonce) {
|
|
81
|
+
const socket = net.createConnection(socketPath(record.address.endpoint));
|
|
82
|
+
socket.on('error', () => { });
|
|
83
|
+
const response = readFrame(socket);
|
|
84
|
+
socket.once('connect', () => socket.write(JSON.stringify({ body, mac: sign(secret, body) }) + '\n'));
|
|
85
|
+
try {
|
|
86
|
+
const wire = wireSchema.parse(await response);
|
|
87
|
+
const parsed = responseSchema.parse(wire.body);
|
|
88
|
+
if (parsed.nonce !== nonce || !authentic(record.secret, parsed, wire.mac))
|
|
89
|
+
throw new Error('Forged endpoint response');
|
|
90
|
+
return parsed.result;
|
|
91
|
+
}
|
|
92
|
+
finally {
|
|
93
|
+
socket.destroy();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
async function probe(record) {
|
|
97
|
+
const nonce = randomUUID();
|
|
98
|
+
const result = await exchange(record, { kind: 'probe', target: record.address.endpoint, nonce }, record.secret, nonce);
|
|
99
|
+
if (result !== 'live')
|
|
100
|
+
throw new Error('Endpoint is not live');
|
|
101
|
+
}
|
|
102
|
+
/** Bounded metadata-only endpoint projection, separate from immutable lease discovery. */
|
|
103
|
+
export async function discoverPeers() {
|
|
104
|
+
const dir = opendirSync(checkStore());
|
|
105
|
+
const candidates = [];
|
|
106
|
+
let omitted = 0;
|
|
107
|
+
let uninspected = 0;
|
|
108
|
+
let scanLimited = false;
|
|
109
|
+
try {
|
|
110
|
+
for (let n = 0; n < 256; n++) {
|
|
111
|
+
const entry = dir.readSync();
|
|
112
|
+
if (!entry)
|
|
113
|
+
break;
|
|
114
|
+
if (/^[a-f0-9-]{36}\.json$/.test(entry.name)) {
|
|
115
|
+
try {
|
|
116
|
+
// Cheap private metadata/socket checks precede the expensive challenge
|
|
117
|
+
// budget. Crash remnants must not hide current live endpoints.
|
|
118
|
+
const record = registration(entry.name.slice(0, -5));
|
|
119
|
+
if (candidates.length < 32)
|
|
120
|
+
candidates.push(record);
|
|
121
|
+
else {
|
|
122
|
+
omitted++;
|
|
123
|
+
uninspected++;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
omitted++;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (n === 255)
|
|
131
|
+
scanLimited = dir.readSync() !== null;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
finally {
|
|
135
|
+
dir.closeSync();
|
|
136
|
+
}
|
|
137
|
+
const endpoints = [];
|
|
138
|
+
// Eight probes at a time, at most 32 endpoints / six seconds. Never reads history.
|
|
139
|
+
for (let start = 0; start < candidates.length; start += 8) {
|
|
140
|
+
await Promise.all(candidates.slice(start, start + 8).map(async (record) => {
|
|
141
|
+
try {
|
|
142
|
+
await probe(record);
|
|
143
|
+
endpoints.push(record.address);
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
omitted++;
|
|
147
|
+
}
|
|
148
|
+
}));
|
|
149
|
+
}
|
|
150
|
+
return { endpoints: endpoints.sort((a, b) => a.endpoint.localeCompare(b.endpoint)), omitted, uninspected, scanLimited, scope: 'Messaging-capable local endpoints only. Omitted counts scanned registrations not returned; uninspected counts metadata-valid candidates not challenged. scanLimited means further directory entries were not inspected (count unknown). /list-agents separately lists leases; a lease without an endpoint (including older Darwin) cannot receive messages. Discovery starts no model work.' };
|
|
151
|
+
}
|
|
152
|
+
/** One parent session, never a daemon or SDK loop. Only drivers drain the inbox. */
|
|
153
|
+
export class LocalCollaboration {
|
|
154
|
+
projectRoot;
|
|
155
|
+
session;
|
|
156
|
+
readOnly;
|
|
157
|
+
server;
|
|
158
|
+
registration;
|
|
159
|
+
sockets = new Set();
|
|
160
|
+
inbox = [];
|
|
161
|
+
listeners = new Set();
|
|
162
|
+
notices = [];
|
|
163
|
+
seen = new Map();
|
|
164
|
+
chainCounts = new Map();
|
|
165
|
+
cause;
|
|
166
|
+
outgoing = 0;
|
|
167
|
+
peerTurn = false;
|
|
168
|
+
lastPeerId;
|
|
169
|
+
denied = false;
|
|
170
|
+
admitted = 0;
|
|
171
|
+
window = Date.now();
|
|
172
|
+
accepting = false;
|
|
173
|
+
stopped = false;
|
|
174
|
+
generation = 0;
|
|
175
|
+
canonicalRoot;
|
|
176
|
+
constructor(projectRoot, session, readOnly = () => false) {
|
|
177
|
+
this.projectRoot = projectRoot;
|
|
178
|
+
this.session = session;
|
|
179
|
+
this.readOnly = readOnly;
|
|
180
|
+
}
|
|
181
|
+
get project() {
|
|
182
|
+
if (this.canonicalRoot === undefined)
|
|
183
|
+
throw new Error('Canonical collaboration project unavailable');
|
|
184
|
+
return this.canonicalRoot;
|
|
185
|
+
}
|
|
186
|
+
get address() { return this.registration?.address; }
|
|
187
|
+
get pending() { return this.inbox.length; }
|
|
188
|
+
get active() { return this.accepting; }
|
|
189
|
+
subscribe(listener) { this.listeners.add(listener); return () => { this.listeners.delete(listener); }; }
|
|
190
|
+
takeNotices() { return this.notices.splice(0); }
|
|
191
|
+
changed() { for (const listener of this.listeners) {
|
|
192
|
+
try {
|
|
193
|
+
listener();
|
|
194
|
+
}
|
|
195
|
+
catch { /* observer */ }
|
|
196
|
+
} }
|
|
197
|
+
notice(text) { if (this.notices.length < 16)
|
|
198
|
+
this.notices.push(text); this.changed(); }
|
|
199
|
+
async start() {
|
|
200
|
+
if (this.server)
|
|
201
|
+
return;
|
|
202
|
+
this.stopped = false;
|
|
203
|
+
const generation = ++this.generation;
|
|
204
|
+
try {
|
|
205
|
+
this.canonicalRoot = canonicalProject(this.projectRoot);
|
|
206
|
+
const node = await withPolicy(state => state.node);
|
|
207
|
+
if (this.stopped || generation !== this.generation)
|
|
208
|
+
return;
|
|
209
|
+
if (!readPolicy().enabled) {
|
|
210
|
+
this.notice('Collaboration is off. User: /collaborate on');
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
const record = { address: { version: 1, transport: 'local', node, endpoint: randomUUID(), project: this.project, session: this.session }, pid: process.pid, secret: randomBytes(32).toString('hex') };
|
|
214
|
+
const file = socketPath(record.address.endpoint);
|
|
215
|
+
const server = net.createServer(socket => {
|
|
216
|
+
socket.on('error', () => { });
|
|
217
|
+
if (this.stopped || this.sockets.size >= 16) {
|
|
218
|
+
socket.destroy();
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
this.sockets.add(socket);
|
|
222
|
+
const deadline = setTimeout(() => socket.destroy(), IO_TIMEOUT_MS * 3);
|
|
223
|
+
socket.once('close', () => { clearTimeout(deadline); this.sockets.delete(socket); });
|
|
224
|
+
void this.receive(socket, record).catch(() => socket.destroy());
|
|
225
|
+
});
|
|
226
|
+
this.server = server;
|
|
227
|
+
await new Promise((resolve, reject) => { server.once('error', reject); server.listen(file, resolve); });
|
|
228
|
+
server.on('error', () => {
|
|
229
|
+
if (generation !== this.generation || this.server !== server)
|
|
230
|
+
return;
|
|
231
|
+
this.notice('Collaboration listener failed; endpoint retired');
|
|
232
|
+
this.close('listener failure');
|
|
233
|
+
});
|
|
234
|
+
if (this.stopped || generation !== this.generation) {
|
|
235
|
+
server.close(() => { });
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
chmodSync(file, 0o600);
|
|
239
|
+
writePrivate(`${record.address.endpoint}.json`, record);
|
|
240
|
+
this.registration = record;
|
|
241
|
+
this.accepting = true;
|
|
242
|
+
}
|
|
243
|
+
catch (error) {
|
|
244
|
+
if (generation !== this.generation)
|
|
245
|
+
return;
|
|
246
|
+
this.close('startup unavailable');
|
|
247
|
+
const reason = error instanceof Error ? error.message.slice(0, 512) : 'local startup failed';
|
|
248
|
+
this.notice(`Collaboration unavailable: ${JSON.stringify(reason)}. User: darwin collaborate status. No TCP fallback.`);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
/** Headless closes admission before draining a finite snapshot; not an idle daemon. */
|
|
252
|
+
stopAdmission() { this.accepting = false; }
|
|
253
|
+
close(reason) {
|
|
254
|
+
this.generation++;
|
|
255
|
+
this.stopped = true;
|
|
256
|
+
this.accepting = false;
|
|
257
|
+
const count = this.inbox.length;
|
|
258
|
+
this.inbox = [];
|
|
259
|
+
for (const socket of this.sockets)
|
|
260
|
+
socket.destroy();
|
|
261
|
+
this.sockets.clear();
|
|
262
|
+
this.server?.close();
|
|
263
|
+
this.server = undefined;
|
|
264
|
+
const record = this.registration;
|
|
265
|
+
this.registration = undefined;
|
|
266
|
+
if (record) {
|
|
267
|
+
// UUID never reused. Refuse to remove a replaced registration or symlink.
|
|
268
|
+
try {
|
|
269
|
+
const current = registrationSchema.parse(readPrivate(`${record.address.endpoint}.json`));
|
|
270
|
+
if (current.secret === record.secret && sameAddress(current.address, record.address))
|
|
271
|
+
unlinkSync(path.join(checkStore(), `${record.address.endpoint}.json`));
|
|
272
|
+
}
|
|
273
|
+
catch { /* stale record is never proof of a live endpoint */ }
|
|
274
|
+
}
|
|
275
|
+
if (count)
|
|
276
|
+
this.notice(`peer messages dropped: ${count} (${reason}); not forwarded to a successor`);
|
|
277
|
+
this.changed();
|
|
278
|
+
}
|
|
279
|
+
async receive(socket, own) {
|
|
280
|
+
const wire = wireSchema.parse(await readFrame(socket));
|
|
281
|
+
const probeRequest = probeSchema.safeParse(wire.body);
|
|
282
|
+
if (probeRequest.success) {
|
|
283
|
+
if (probeRequest.data.target !== own.address.endpoint || !authentic(own.secret, wire.body, wire.mac))
|
|
284
|
+
throw new Error('Invalid challenge');
|
|
285
|
+
this.respond(socket, own, probeRequest.data.nonce, 'live');
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
const { envelope } = messageSchema.parse(wire.body);
|
|
289
|
+
if (!sameAddress(envelope.target, own.address) || envelope.sender.endpoint === own.address.endpoint)
|
|
290
|
+
throw new Error('Invalid target');
|
|
291
|
+
const sender = registration(envelope.sender.endpoint);
|
|
292
|
+
if (!sameAddress(sender.address, envelope.sender) || !authentic(sender.secret, wire.body, wire.mac))
|
|
293
|
+
throw new Error('Forged sender');
|
|
294
|
+
await probe(sender);
|
|
295
|
+
if (!this.accepting || this.registration !== own)
|
|
296
|
+
throw new Error('Retired endpoint');
|
|
297
|
+
const result = await this.admit(envelope);
|
|
298
|
+
this.respond(socket, own, envelope.id, result);
|
|
299
|
+
}
|
|
300
|
+
respond(socket, own, nonce, result) {
|
|
301
|
+
const body = { nonce, result };
|
|
302
|
+
socket.end(JSON.stringify({ body, mac: sign(own.secret, body) }) + '\n');
|
|
303
|
+
}
|
|
304
|
+
fresh(envelope) {
|
|
305
|
+
const now = Date.now();
|
|
306
|
+
return envelope.sent <= now + 1000 && envelope.sent > now - MESSAGE_TTL_MS
|
|
307
|
+
&& envelope.chain.started <= envelope.sent && envelope.chain.started > now - CHAIN_TTL_MS;
|
|
308
|
+
}
|
|
309
|
+
async admit(envelope) {
|
|
310
|
+
if (!this.fresh(envelope))
|
|
311
|
+
return 'Not queued: expired message/chain';
|
|
312
|
+
const auth = authorization(this.project, envelope.sender.project);
|
|
313
|
+
if (!auth)
|
|
314
|
+
return requestCooperation(this.project, envelope.sender.project);
|
|
315
|
+
const now = Date.now();
|
|
316
|
+
for (const [id, expiry] of this.seen)
|
|
317
|
+
if (expiry <= now)
|
|
318
|
+
this.seen.delete(id);
|
|
319
|
+
for (const [id, entry] of this.chainCounts)
|
|
320
|
+
if (entry.expires <= now)
|
|
321
|
+
this.chainCounts.delete(id);
|
|
322
|
+
if (this.seen.has(envelope.id))
|
|
323
|
+
return `Already queued ${envelope.id}; duplicate not delivered again (not proof of processing)`;
|
|
324
|
+
if (now - this.window >= MESSAGE_TTL_MS) {
|
|
325
|
+
this.window = now;
|
|
326
|
+
this.admitted = 0;
|
|
327
|
+
}
|
|
328
|
+
const chain = this.chainCounts.get(envelope.chain.id);
|
|
329
|
+
if (this.inbox.length >= MAX_QUEUE || this.admitted >= 16 || this.seen.size >= 256 || this.chainCounts.size >= 256)
|
|
330
|
+
return 'Not queued: inbox/flood capacity (8 queued, 16 admitted/minute); pause until capacity changes';
|
|
331
|
+
if ((chain?.count ?? 0) >= 2)
|
|
332
|
+
return 'Not queued: causal chain limit; a human must explicitly resume work';
|
|
333
|
+
this.seen.set(envelope.id, envelope.sent + MESSAGE_TTL_MS);
|
|
334
|
+
this.chainCounts.set(envelope.chain.id, { count: (chain?.count ?? 0) + 1, expires: envelope.chain.started + CHAIN_TTL_MS });
|
|
335
|
+
this.inbox.push({ kind: 'peer', envelope, authorization: auth });
|
|
336
|
+
this.admitted++;
|
|
337
|
+
this.changed();
|
|
338
|
+
return `Queued ${envelope.id}; not processed. Expires after 60 seconds; revocation, cancellation, shutdown or capacity policy may drop it. No automatic retry on ambiguous acknowledgement.`;
|
|
339
|
+
}
|
|
340
|
+
/** Only a driver at idle takes input. Runtime rechecks it immediately before invocation. */
|
|
341
|
+
take() {
|
|
342
|
+
while (this.inbox.length) {
|
|
343
|
+
const input = this.inbox.shift();
|
|
344
|
+
try {
|
|
345
|
+
this.validateDelivery(input);
|
|
346
|
+
this.changed();
|
|
347
|
+
return input;
|
|
348
|
+
}
|
|
349
|
+
catch {
|
|
350
|
+
this.notice(`peer message ${input.envelope.id} dropped: expired, revoked or retired`);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
return undefined;
|
|
354
|
+
}
|
|
355
|
+
validateDelivery(input) {
|
|
356
|
+
if (!this.registration || !sameAddress(input.envelope.target, this.registration.address) || !this.fresh(input.envelope)
|
|
357
|
+
|| authorization(this.project, input.envelope.sender.project) !== input.authorization)
|
|
358
|
+
throw new Error('Peer delivery expired, revoked or retired');
|
|
359
|
+
}
|
|
360
|
+
beginHumanTurn() {
|
|
361
|
+
this.cause = { id: randomUUID(), started: Date.now(), hop: 0, readOnly: this.readOnly() };
|
|
362
|
+
this.outgoing = 0;
|
|
363
|
+
this.peerTurn = false;
|
|
364
|
+
this.denied = false;
|
|
365
|
+
}
|
|
366
|
+
beginPeerTurn(input) {
|
|
367
|
+
this.validateDelivery(input);
|
|
368
|
+
this.cause = input.envelope.chain;
|
|
369
|
+
if (this.lastPeerId !== input.envelope.id)
|
|
370
|
+
this.outgoing = 0;
|
|
371
|
+
this.lastPeerId = input.envelope.id;
|
|
372
|
+
this.peerTurn = true;
|
|
373
|
+
// A local denial remains latched through synthetic turns; only a human can reset it.
|
|
374
|
+
}
|
|
375
|
+
permissionDenied() { this.denied = true; }
|
|
376
|
+
get fromPeer() { return this.peerTurn; }
|
|
377
|
+
get peerReadOnly() { return this.peerTurn && this.cause?.readOnly === true; }
|
|
378
|
+
async send(target, text) {
|
|
379
|
+
const own = this.registration;
|
|
380
|
+
if (!own || this.stopped)
|
|
381
|
+
throw new Error('Collaboration endpoint inactive; user: /collaborate on');
|
|
382
|
+
idSchema.parse(target);
|
|
383
|
+
if (target === own.address.endpoint)
|
|
384
|
+
throw new Error('Self-send refused');
|
|
385
|
+
if (this.denied)
|
|
386
|
+
throw new Error('Peer send paused after a local permission denial; never route denied work to a peer. A human must explicitly resume.');
|
|
387
|
+
const cause = this.cause;
|
|
388
|
+
if (!cause || cause.started <= Date.now() - CHAIN_TTL_MS || (this.peerTurn && cause.hop >= MAX_HOPS) || this.outgoing >= (this.peerTurn ? 1 : 4))
|
|
389
|
+
throw new Error('Peer causal/outgoing limit; a human must explicitly resume work (no invented new chain)');
|
|
390
|
+
const recipient = registration(target);
|
|
391
|
+
const envelope = envelopeSchema.parse({ version: 1, id: randomUUID(), sender: own.address, target: recipient.address, sent: Date.now(), chain: { ...cause, readOnly: cause.readOnly || this.readOnly(), hop: cause.hop + (this.peerTurn ? 1 : 0) }, text });
|
|
392
|
+
const body = { kind: 'message', envelope };
|
|
393
|
+
if (Buffer.byteLength(JSON.stringify({ body, mac: sign(own.secret, body) }) + '\n') > MAX_FRAME_BYTES)
|
|
394
|
+
throw new Error('Encoded peer frame exceeds 16 KiB; shorten text/identities');
|
|
395
|
+
// Reserve synchronously before network or lock waits; parallel tools share the cap.
|
|
396
|
+
this.outgoing++;
|
|
397
|
+
if (!authorization(this.project, recipient.address.project))
|
|
398
|
+
return requestCooperation(this.project, recipient.address.project);
|
|
399
|
+
try {
|
|
400
|
+
return await exchange(recipient, body, own.secret, envelope.id);
|
|
401
|
+
}
|
|
402
|
+
catch {
|
|
403
|
+
throw new Error(`Peer send ${envelope.id}: acknowledgement unavailable; delivery may have queued. Do not replay automatically. Inspect the receiving session before a human retries.`);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
//# sourceMappingURL=local.js.map
|