@cotal-ai/workspace 0.7.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/LICENSE +202 -0
- package/dist/auth-paths.d.ts +13 -0
- package/dist/auth-paths.d.ts.map +1 -0
- package/dist/auth-paths.js +46 -0
- package/dist/auth-paths.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/mesh-registry.d.ts +45 -0
- package/dist/mesh-registry.d.ts.map +1 -0
- package/dist/mesh-registry.js +78 -0
- package/dist/mesh-registry.js.map +1 -0
- package/dist/mesh-target.d.ts +81 -0
- package/dist/mesh-target.d.ts.map +1 -0
- package/dist/mesh-target.js +129 -0
- package/dist/mesh-target.js.map +1 -0
- package/dist/preflight.d.ts +46 -0
- package/dist/preflight.d.ts.map +1 -0
- package/dist/preflight.js +52 -0
- package/dist/preflight.js.map +1 -0
- package/dist/render.d.ts +29 -0
- package/dist/render.d.ts.map +1 -0
- package/dist/render.js +50 -0
- package/dist/render.js.map +1 -0
- package/package.json +34 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { SpaceAuth } from "@cotal-ai/core";
|
|
2
|
+
export declare function authDir(root: string): string;
|
|
3
|
+
/** Find the project's `.cotal/` by walking up from `start` (like git finds `.git`), returning the
|
|
4
|
+
* directory that *contains* `.cotal/`. Falls back to `start` when none is found up the tree (a
|
|
5
|
+
* fresh setup creates `.cotal/` there). Lets `cotal` run from any subdirectory of a project. */
|
|
6
|
+
export declare function findCotalRoot(start?: string): string;
|
|
7
|
+
/** Persist the space trust material. The file holds the data-account signing seed — treat as a secret.
|
|
8
|
+
* The system-account `sys.signingSeed` is STRIPPED before writing: it is broker-admin minting capability,
|
|
9
|
+
* so it never lands on disk (it lives only in the in-memory {@link createSpaceAuth} result). */
|
|
10
|
+
export declare function saveSpaceAuth(dir: string, auth: SpaceAuth): void;
|
|
11
|
+
/** Load the space trust material, or undefined if auth was never set up here. */
|
|
12
|
+
export declare function loadSpaceAuth(dir: string): SpaceAuth | undefined;
|
|
13
|
+
//# sourceMappingURL=auth-paths.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-paths.d.ts","sourceRoot":"","sources":["../src/auth-paths.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAehD,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE5C;AAED;;iGAEiG;AACjG,wBAAgB,aAAa,CAAC,KAAK,GAAE,MAAsB,GAAG,MAAM,CAQnE;AAED;;iGAEiG;AACjG,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,GAAG,IAAI,CAIhE;AAED,iFAAiF;AACjF,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS,CAIhE"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join, dirname, resolve } from "node:path";
|
|
3
|
+
/**
|
|
4
|
+
* On-disk auth-material I/O for a local checkout's `.cotal/` — machine-local path resolution plus
|
|
5
|
+
* reading/writing the space trust material. Lives in `@cotal-ai/workspace` (not core) because it's a
|
|
6
|
+
* workstation concern — *where a checkout's `.cotal/` is on THIS disk* — not part of the wire
|
|
7
|
+
* protocol. The minting / JWT / `nats-server` config machinery stays in `@cotal-ai/core`; these
|
|
8
|
+
* helpers persist its output. `SpaceAuth` is core's type — imported here, owned there.
|
|
9
|
+
*
|
|
10
|
+
* Deliberately explicit-root/path APIs: no ambient `findAndMint()` that fuses root discovery with
|
|
11
|
+
* signing. File modes (0700 dirs / 0600 files) and the no-arbitrary-delete posture are preserved.
|
|
12
|
+
*/
|
|
13
|
+
const AUTH_FILE = "auth.json";
|
|
14
|
+
export function authDir(root) {
|
|
15
|
+
return join(root, ".cotal", "auth");
|
|
16
|
+
}
|
|
17
|
+
/** Find the project's `.cotal/` by walking up from `start` (like git finds `.git`), returning the
|
|
18
|
+
* directory that *contains* `.cotal/`. Falls back to `start` when none is found up the tree (a
|
|
19
|
+
* fresh setup creates `.cotal/` there). Lets `cotal` run from any subdirectory of a project. */
|
|
20
|
+
export function findCotalRoot(start = process.cwd()) {
|
|
21
|
+
let dir = resolve(start);
|
|
22
|
+
for (;;) {
|
|
23
|
+
if (existsSync(join(dir, ".cotal")))
|
|
24
|
+
return dir;
|
|
25
|
+
const parent = dirname(dir);
|
|
26
|
+
if (parent === dir)
|
|
27
|
+
return resolve(start);
|
|
28
|
+
dir = parent;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/** Persist the space trust material. The file holds the data-account signing seed — treat as a secret.
|
|
32
|
+
* The system-account `sys.signingSeed` is STRIPPED before writing: it is broker-admin minting capability,
|
|
33
|
+
* so it never lands on disk (it lives only in the in-memory {@link createSpaceAuth} result). */
|
|
34
|
+
export function saveSpaceAuth(dir, auth) {
|
|
35
|
+
mkdirSync(dir, { recursive: true });
|
|
36
|
+
const onDisk = { ...auth, sys: { pub: auth.sys.pub, jwt: auth.sys.jwt } };
|
|
37
|
+
writeFileSync(join(dir, AUTH_FILE), JSON.stringify(onDisk, null, 2), { mode: 0o600 });
|
|
38
|
+
}
|
|
39
|
+
/** Load the space trust material, or undefined if auth was never set up here. */
|
|
40
|
+
export function loadSpaceAuth(dir) {
|
|
41
|
+
const f = join(dir, AUTH_FILE);
|
|
42
|
+
if (!existsSync(f))
|
|
43
|
+
return undefined;
|
|
44
|
+
return JSON.parse(readFileSync(f, "utf8"));
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=auth-paths.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-paths.js","sourceRoot":"","sources":["../src/auth-paths.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC7E,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAGnD;;;;;;;;;GASG;AAEH,MAAM,SAAS,GAAG,WAAW,CAAC;AAE9B,MAAM,UAAU,OAAO,CAAC,IAAY;IAClC,OAAO,IAAI,CAAC,IAAI,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;AACtC,CAAC;AAED;;iGAEiG;AACjG,MAAM,UAAU,aAAa,CAAC,QAAgB,OAAO,CAAC,GAAG,EAAE;IACzD,IAAI,GAAG,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;IACzB,SAAS,CAAC;QACR,IAAI,UAAU,CAAC,IAAI,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;YAAE,OAAO,GAAG,CAAC;QAChD,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;QAC5B,IAAI,MAAM,KAAK,GAAG;YAAE,OAAO,OAAO,CAAC,KAAK,CAAC,CAAC;QAC1C,GAAG,GAAG,MAAM,CAAC;IACf,CAAC;AACH,CAAC;AAED;;iGAEiG;AACjG,MAAM,UAAU,aAAa,CAAC,GAAW,EAAE,IAAe;IACxD,SAAS,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IACpC,MAAM,MAAM,GAAc,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,CAAC;IACrF,aAAa,CAAC,IAAI,CAAC,GAAG,EAAE,SAAS,CAAC,EAAE,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;AACxF,CAAC;AAED,iFAAiF;AACjF,MAAM,UAAU,aAAa,CAAC,GAAW;IACvC,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;IAC/B,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC;QAAE,OAAO,SAAS,CAAC;IACrC,OAAO,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,EAAE,MAAM,CAAC,CAAc,CAAC;AAC1D,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The registry of running meshes: one record per broker `cotal up` started on this machine, so a
|
|
3
|
+
* `cotal spawn` from *any* directory can find which mesh to join, with which creds and personas.
|
|
4
|
+
*
|
|
5
|
+
* Stored as **one JSON file per mesh** (`~/.cotal/meshes/<space>.json`) rather than a single
|
|
6
|
+
* `meshes.json`: concurrent `up`/`down` never read-modify-write the same file (no lost-update race),
|
|
7
|
+
* a crash damages at most one entry, and it mirrors the existing per-process pid files under
|
|
8
|
+
* `~/.cotal`. A separate `~/.cotal/current-mesh` holds the default space for the N-running case
|
|
9
|
+
* (the kubectl `current-context` analogue).
|
|
10
|
+
*
|
|
11
|
+
* Each record stores the mesh's **root path**, not its secrets — trust material stays in that
|
|
12
|
+
* project's `.cotal/auth`; the registry just makes it findable from elsewhere.
|
|
13
|
+
*/
|
|
14
|
+
export interface MeshEntry {
|
|
15
|
+
/** The space name — also the registry filename stem. */
|
|
16
|
+
space: string;
|
|
17
|
+
/** The broker URL, e.g. `nats://127.0.0.1:4222`. */
|
|
18
|
+
server: string;
|
|
19
|
+
/** Absolute path whose `.cotal/{auth,agents}` hold this mesh's trust material + personas. */
|
|
20
|
+
root: string;
|
|
21
|
+
mode: "auth" | "open";
|
|
22
|
+
/** ISO timestamp of when the record was written. */
|
|
23
|
+
ts: string;
|
|
24
|
+
}
|
|
25
|
+
/** The cotal machine-home dir (`~/.cotal`), overridable via `COTAL_HOME` so tests sandbox it and
|
|
26
|
+
* never touch the real one. The single source of that path for the registry, the current pointer,
|
|
27
|
+
* and the onboard marker. */
|
|
28
|
+
export declare function homeCotalDir(): string;
|
|
29
|
+
/** Directory holding the per-mesh registry files (`~/.cotal/meshes`). */
|
|
30
|
+
export declare function meshesDir(): string;
|
|
31
|
+
/** Record (or refresh) a running mesh — atomic write, 0600 (the file points at a secrets dir). */
|
|
32
|
+
export declare function recordMesh(m: MeshEntry): void;
|
|
33
|
+
/** Drop a mesh from the registry (on `cotal down` / a stale-entry prune). Absent ⇒ no-op. */
|
|
34
|
+
export declare function removeMesh(space: string): void;
|
|
35
|
+
/** All currently-recorded meshes. An unparseable/partially-written entry is skipped, not fatal —
|
|
36
|
+
* one bad file must not hide the rest. */
|
|
37
|
+
export declare function loadMeshes(): MeshEntry[];
|
|
38
|
+
export declare function findMesh(space: string): MeshEntry | undefined;
|
|
39
|
+
/** The default mesh's space name, set by `cotal use` (and by the first `cotal up`). Undefined when
|
|
40
|
+
* unset or empty. The pointer can dangle (its mesh went down); callers treat a `findMesh` miss as
|
|
41
|
+
* "no current". */
|
|
42
|
+
export declare function getCurrent(): string | undefined;
|
|
43
|
+
export declare function setCurrent(space: string): void;
|
|
44
|
+
export declare function clearCurrent(): void;
|
|
45
|
+
//# sourceMappingURL=mesh-registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mesh-registry.d.ts","sourceRoot":"","sources":["../src/mesh-registry.ts"],"names":[],"mappings":"AAWA;;;;;;;;;;;;GAYG;AACH,MAAM,WAAW,SAAS;IACxB,wDAAwD;IACxD,KAAK,EAAE,MAAM,CAAC;IACd,oDAAoD;IACpD,MAAM,EAAE,MAAM,CAAC;IACf,6FAA6F;IAC7F,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;IACtB,oDAAoD;IACpD,EAAE,EAAE,MAAM,CAAC;CACZ;AAED;;8BAE8B;AAC9B,wBAAgB,YAAY,IAAI,MAAM,CAErC;AAED,yEAAyE;AACzE,wBAAgB,SAAS,IAAI,MAAM,CAElC;AAUD,kGAAkG;AAClG,wBAAgB,UAAU,CAAC,CAAC,EAAE,SAAS,GAAG,IAAI,CAU7C;AAED,6FAA6F;AAC7F,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAE9C;AAED;2CAC2C;AAC3C,wBAAgB,UAAU,IAAI,SAAS,EAAE,CAgBxC;AAED,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS,CAE7D;AAED;;oBAEoB;AACpB,wBAAgB,UAAU,IAAI,MAAM,GAAG,SAAS,CAM/C;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAG9C;AAED,wBAAgB,YAAY,IAAI,IAAI,CAEnC"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync, readdirSync, renameSync, rmSync, writeFileSync, } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
/** The cotal machine-home dir (`~/.cotal`), overridable via `COTAL_HOME` so tests sandbox it and
|
|
5
|
+
* never touch the real one. The single source of that path for the registry, the current pointer,
|
|
6
|
+
* and the onboard marker. */
|
|
7
|
+
export function homeCotalDir() {
|
|
8
|
+
return process.env.COTAL_HOME ?? join(homedir(), ".cotal");
|
|
9
|
+
}
|
|
10
|
+
/** Directory holding the per-mesh registry files (`~/.cotal/meshes`). */
|
|
11
|
+
export function meshesDir() {
|
|
12
|
+
return join(homeCotalDir(), "meshes");
|
|
13
|
+
}
|
|
14
|
+
function meshFile(space) {
|
|
15
|
+
return join(meshesDir(), `${encodeURIComponent(space)}.json`);
|
|
16
|
+
}
|
|
17
|
+
function currentFile() {
|
|
18
|
+
return join(homeCotalDir(), "current-mesh");
|
|
19
|
+
}
|
|
20
|
+
/** Record (or refresh) a running mesh — atomic write, 0600 (the file points at a secrets dir). */
|
|
21
|
+
export function recordMesh(m) {
|
|
22
|
+
// 0700: the filenames in here ARE the space names, so a world-traversable dir would leak them to
|
|
23
|
+
// other local users even though the file contents are 0600. Keep the dir readable only by us.
|
|
24
|
+
mkdirSync(meshesDir(), { recursive: true, mode: 0o700 });
|
|
25
|
+
const file = meshFile(m.space);
|
|
26
|
+
// Per-process temp name so two concurrent `up`s for the same space can't stomp each other's
|
|
27
|
+
// half-written file before the rename.
|
|
28
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
29
|
+
writeFileSync(tmp, JSON.stringify(m, null, 2), { mode: 0o600 });
|
|
30
|
+
renameSync(tmp, file); // atomic replace — a reader never sees a half-written record
|
|
31
|
+
}
|
|
32
|
+
/** Drop a mesh from the registry (on `cotal down` / a stale-entry prune). Absent ⇒ no-op. */
|
|
33
|
+
export function removeMesh(space) {
|
|
34
|
+
rmSync(meshFile(space), { force: true });
|
|
35
|
+
}
|
|
36
|
+
/** All currently-recorded meshes. An unparseable/partially-written entry is skipped, not fatal —
|
|
37
|
+
* one bad file must not hide the rest. */
|
|
38
|
+
export function loadMeshes() {
|
|
39
|
+
let files;
|
|
40
|
+
try {
|
|
41
|
+
files = readdirSync(meshesDir()).filter((f) => f.endsWith(".json"));
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return []; // no registry yet
|
|
45
|
+
}
|
|
46
|
+
const out = [];
|
|
47
|
+
for (const f of files.sort()) {
|
|
48
|
+
try {
|
|
49
|
+
out.push(JSON.parse(readFileSync(join(meshesDir(), f), "utf8")));
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
/* skip a corrupt/half-written entry rather than fail the whole listing */
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
export function findMesh(space) {
|
|
58
|
+
return loadMeshes().find((m) => m.space === space);
|
|
59
|
+
}
|
|
60
|
+
/** The default mesh's space name, set by `cotal use` (and by the first `cotal up`). Undefined when
|
|
61
|
+
* unset or empty. The pointer can dangle (its mesh went down); callers treat a `findMesh` miss as
|
|
62
|
+
* "no current". */
|
|
63
|
+
export function getCurrent() {
|
|
64
|
+
try {
|
|
65
|
+
return readFileSync(currentFile(), "utf8").trim() || undefined;
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
export function setCurrent(space) {
|
|
72
|
+
mkdirSync(homeCotalDir(), { recursive: true, mode: 0o700 });
|
|
73
|
+
writeFileSync(currentFile(), space, { mode: 0o600 });
|
|
74
|
+
}
|
|
75
|
+
export function clearCurrent() {
|
|
76
|
+
rmSync(currentFile(), { force: true });
|
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AO,YAAY,CAAC,WAAW,EAAE,EAAE,MAAM,CAAC,CAAC,IAAI,EAAE,IAAI,SAAS,CAAC;IACjE,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,KAAa;IACtC,SAAS,CAAC,YAAY,EAAE,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;IAC5D,aAAa,CAAC,WAAW,EAAE,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;AACvD,CAAC;AAED,MAAM,UAAU,YAAY;IAC1B,MAAM,CAAC,WAAW,EAAE,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;AACzC,CAAC"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { type SpaceAuth } from "@cotal-ai/core";
|
|
2
|
+
/**
|
|
3
|
+
* One coherent answer to "which mesh does this command act on, and where do its creds + personas
|
|
4
|
+
* live" — resolved identically for both, so a spawn can never authenticate to mesh A while loading
|
|
5
|
+
* mesh B's persona. Replaces the scattered `loadSpaceAuth(authDir(cotalRoot()))` + `resolveSpace` +
|
|
6
|
+
* `DEFAULT_SERVER` guesswork that silently mistook `~/.cotal` for a space.
|
|
7
|
+
*
|
|
8
|
+
* Pure and offline (no network): `<TAB>` completion uses it as-is; `spawn` adds a reachability
|
|
9
|
+
* preflight (`probeConnect`) on top.
|
|
10
|
+
*/
|
|
11
|
+
export interface MeshTarget {
|
|
12
|
+
/** Absolute dir whose `.cotal/{auth,agents}` hold trust + personas. */
|
|
13
|
+
root: string;
|
|
14
|
+
server: string;
|
|
15
|
+
space: string;
|
|
16
|
+
/** Trust material, or undefined for an open mesh. */
|
|
17
|
+
auth?: SpaceAuth;
|
|
18
|
+
/** `<root>/.cotal/agents` — the persona catalog for this mesh. */
|
|
19
|
+
personaRoot: string;
|
|
20
|
+
/** Where the target came from — this also carries OWNERSHIP for pruning. `registry`/`current`/
|
|
21
|
+
* `flag-space`/`local-recorded` mean the server + mode came from a registry record, so a
|
|
22
|
+
* stale-broker failure prunes the entry. `local-space`/`flag-server`/`flag-space-override` are the
|
|
23
|
+
* non-registry escape hatches — NEVER pruned: the probed server is operator-supplied (a raw
|
|
24
|
+
* `--server`, or a `--space` whose `--server` overrides the recorded broker), so a failure against
|
|
25
|
+
* it must not delete the recorded entry. `local-recorded` is a local project matched to a registry
|
|
26
|
+
* entry by root: registry-owned for pruning, but quiet on the success line (self-evident from cwd). */
|
|
27
|
+
source: "flag-server" | "flag-space" | "flag-space-override" | "local-space" | "local-recorded" | "registry" | "current";
|
|
28
|
+
}
|
|
29
|
+
export interface ResolveFlags {
|
|
30
|
+
/** `--server <url>` — raw broker escape hatch. */
|
|
31
|
+
server?: string;
|
|
32
|
+
/** `--space <name>` — pick a specific running mesh from the registry. */
|
|
33
|
+
space?: string;
|
|
34
|
+
}
|
|
35
|
+
/** The distinct, command-agnostic reasons {@link resolveMeshTarget} can't pick a single mesh. Each
|
|
36
|
+
* maps 1:1 to a former prose-throw; the recovery copy (`cotal up`/`meshes`/`use`) is the renderer's
|
|
37
|
+
* job ({@link renderWorkspaceError}), not the protocol's. */
|
|
38
|
+
export type MeshTargetErrorCode = "no-meshes" | "unknown-space" | "ambiguous-target" | "default-occupied" | "stale-auth-root";
|
|
39
|
+
/** Structured context for a {@link MeshTargetError} — enough for any surface to render its own
|
|
40
|
+
* recovery affordance (a CLI sentence, a web button, an SDK embed that wants no command at all). */
|
|
41
|
+
export interface MeshTargetErrorDetails {
|
|
42
|
+
/** The space/mesh the failure concerns. */
|
|
43
|
+
space?: string;
|
|
44
|
+
/** A broker URL involved (e.g. the occupant of the default port). */
|
|
45
|
+
server?: string;
|
|
46
|
+
/** A checkout root involved. */
|
|
47
|
+
root?: string;
|
|
48
|
+
/** Running meshes, for the ambiguous case — each formatted `"<space> (<root>)"`. */
|
|
49
|
+
available?: string[];
|
|
50
|
+
/** What the caller asked for that didn't match (e.g. an unknown `--space`). */
|
|
51
|
+
requested?: string;
|
|
52
|
+
/** For `stale-auth-root`: the space the on-disk auth now claims, diverging from the record. */
|
|
53
|
+
found?: string;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* A typed mesh-target resolution failure. `message` is **log/dev-safe and command-agnostic** — never
|
|
57
|
+
* the canonical product copy (that's {@link renderWorkspaceError}'s job; don't let `message` become
|
|
58
|
+
* an accidental string API). `code` + structured `details` are the surfaces consumers read.
|
|
59
|
+
*/
|
|
60
|
+
export declare class MeshTargetError extends Error {
|
|
61
|
+
/** Brand for cross-package-safe detection — see {@link isWorkspaceTargetError}. */
|
|
62
|
+
readonly brand = "cotal:workspace:mesh-target-error";
|
|
63
|
+
readonly code: MeshTargetErrorCode;
|
|
64
|
+
readonly details: MeshTargetErrorDetails;
|
|
65
|
+
constructor(code: MeshTargetErrorCode, message: string, details?: MeshTargetErrorDetails, options?: {
|
|
66
|
+
cause?: unknown;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
/** Detect a {@link MeshTargetError} across the core/workspace/consumer package boundary without a
|
|
70
|
+
* brittle `instanceof` (which breaks if two copies of this package are installed) — a brand check. */
|
|
71
|
+
export declare function isWorkspaceTargetError(e: unknown): e is MeshTargetError;
|
|
72
|
+
/**
|
|
73
|
+
* Resolve the mesh target by precedence (first match wins):
|
|
74
|
+
* 1. `--space` — registry lookup (errors if that space isn't running).
|
|
75
|
+
* 2. `--server` — registry entry on that server (for creds/personas), else the local project.
|
|
76
|
+
* 3. A genuine local project (`cwd` walks up to a real `.cotal/`) — local wins, like `git config`.
|
|
77
|
+
* 4. The registry: 0 ⇒ error; 1 ⇒ use it; N ⇒ `current` if set, else error naming each + its root.
|
|
78
|
+
* No silent fallback — an unresolved target throws one human sentence.
|
|
79
|
+
*/
|
|
80
|
+
export declare function resolveMeshTarget(cwd: string, flags?: ResolveFlags): MeshTarget;
|
|
81
|
+
//# sourceMappingURL=mesh-target.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mesh-target.d.ts","sourceRoot":"","sources":["../src/mesh-target.ts"],"names":[],"mappings":"AAEA,OAAO,EAAiC,KAAK,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAW/E;;;;;;;;GAQG;AACH,MAAM,WAAW,UAAU;IACzB,uEAAuE;IACvE,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,qDAAqD;IACrD,IAAI,CAAC,EAAE,SAAS,CAAC;IACjB,kEAAkE;IAClE,WAAW,EAAE,MAAM,CAAC;IACpB;;;;;;4GAMwG;IACxG,MAAM,EACF,aAAa,GACb,YAAY,GACZ,qBAAqB,GACrB,aAAa,GACb,gBAAgB,GAChB,UAAU,GACV,SAAS,CAAC;CACf;AAED,MAAM,WAAW,YAAY;IAC3B,kDAAkD;IAClD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,yEAAyE;IACzE,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;8DAE8D;AAC9D,MAAM,MAAM,mBAAmB,GAC3B,WAAW,GACX,eAAe,GACf,kBAAkB,GAClB,kBAAkB,GAClB,iBAAiB,CAAC;AAEtB;qGACqG;AACrG,MAAM,WAAW,sBAAsB;IACrC,2CAA2C;IAC3C,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,qEAAqE;IACrE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,gCAAgC;IAChC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,oFAAoF;IACpF,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;IACrB,+EAA+E;IAC/E,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,+FAA+F;IAC/F,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAID;;;;GAIG;AACH,qBAAa,eAAgB,SAAQ,KAAK;IACxC,mFAAmF;IACnF,QAAQ,CAAC,KAAK,uCAA0B;IACxC,QAAQ,CAAC,IAAI,EAAE,mBAAmB,CAAC;IACnC,QAAQ,CAAC,OAAO,EAAE,sBAAsB,CAAC;gBAEvC,IAAI,EAAE,mBAAmB,EACzB,OAAO,EAAE,MAAM,EACf,OAAO,GAAE,sBAA2B,EACpC,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE;CAOhC;AAED;uGACuG;AACvG,wBAAgB,sBAAsB,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,eAAe,CAMvE;AAgDD;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,GAAE,YAAiB,GAAG,UAAU,CAyDnF"}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
|
+
import { DEFAULT_SERVER, DEFAULT_SPACE } from "@cotal-ai/core";
|
|
4
|
+
import { authDir, findCotalRoot, loadSpaceAuth } from "./auth-paths.js";
|
|
5
|
+
import { findMesh, getCurrent, homeCotalDir, loadMeshes, removeMesh, } from "./mesh-registry.js";
|
|
6
|
+
const WORKSPACE_TARGET_ERROR = "cotal:workspace:mesh-target-error";
|
|
7
|
+
/**
|
|
8
|
+
* A typed mesh-target resolution failure. `message` is **log/dev-safe and command-agnostic** — never
|
|
9
|
+
* the canonical product copy (that's {@link renderWorkspaceError}'s job; don't let `message` become
|
|
10
|
+
* an accidental string API). `code` + structured `details` are the surfaces consumers read.
|
|
11
|
+
*/
|
|
12
|
+
export class MeshTargetError extends Error {
|
|
13
|
+
/** Brand for cross-package-safe detection — see {@link isWorkspaceTargetError}. */
|
|
14
|
+
brand = WORKSPACE_TARGET_ERROR;
|
|
15
|
+
code;
|
|
16
|
+
details;
|
|
17
|
+
constructor(code, message, details = {}, options) {
|
|
18
|
+
super(message, options);
|
|
19
|
+
this.name = "MeshTargetError";
|
|
20
|
+
this.code = code;
|
|
21
|
+
this.details = details;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/** Detect a {@link MeshTargetError} across the core/workspace/consumer package boundary without a
|
|
25
|
+
* brittle `instanceof` (which breaks if two copies of this package are installed) — a brand check. */
|
|
26
|
+
export function isWorkspaceTargetError(e) {
|
|
27
|
+
return (typeof e === "object" &&
|
|
28
|
+
e !== null &&
|
|
29
|
+
e.brand === WORKSPACE_TARGET_ERROR);
|
|
30
|
+
}
|
|
31
|
+
function personaRoot(root) {
|
|
32
|
+
return join(root, ".cotal", "agents");
|
|
33
|
+
}
|
|
34
|
+
function targetFromEntry(m, server, source) {
|
|
35
|
+
// Honor the recorded mode: an OPEN mesh connects credlessly even if its root still has auth
|
|
36
|
+
// material on disk (e.g. a root that once ran auth mode). Loading it would make `spawn` mint
|
|
37
|
+
// creds against a broker that takes none.
|
|
38
|
+
let auth;
|
|
39
|
+
if (m.mode === "auth") {
|
|
40
|
+
auth = loadSpaceAuth(authDir(m.root));
|
|
41
|
+
// Defense in depth: the root's on-disk auth must still be for THIS space. A divergence (the root
|
|
42
|
+
// was re-`up`ed as a different space without re-recording) would otherwise mint mesh-A creds
|
|
43
|
+
// against the entry for space B. Prune the stale entry and fail loud rather than connect wrong.
|
|
44
|
+
if (auth && auth.space !== m.space) {
|
|
45
|
+
removeMesh(m.space);
|
|
46
|
+
throw new MeshTargetError("stale-auth-root", `registry entry "${m.space}" points at ${m.root}, whose on-disk auth is now for "${auth.space}"`, { space: m.space, root: m.root, found: auth.space });
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
root: m.root,
|
|
51
|
+
server,
|
|
52
|
+
space: m.space,
|
|
53
|
+
auth,
|
|
54
|
+
personaRoot: personaRoot(m.root),
|
|
55
|
+
source,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function localTarget(root, server, source) {
|
|
59
|
+
const auth = loadSpaceAuth(authDir(root));
|
|
60
|
+
return { root, server, space: auth?.space ?? DEFAULT_SPACE, auth, personaRoot: personaRoot(root), source };
|
|
61
|
+
}
|
|
62
|
+
/** A `.cotal/` that a user actually created here — not the machine-home dir the cwd walk-up lands on
|
|
63
|
+
* from outside any project (which has no space, just the daemon's pid/onboard files). */
|
|
64
|
+
function isGenuineSpace(root) {
|
|
65
|
+
// Normalize both sides — COTAL_HOME may be relative or non-canonical, and a raw string compare
|
|
66
|
+
// would then let the real `~/.cotal` masquerade as a project space (or vice-versa).
|
|
67
|
+
return resolve(join(root, ".cotal")) !== resolve(homeCotalDir()) && existsSync(join(root, ".cotal"));
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Resolve the mesh target by precedence (first match wins):
|
|
71
|
+
* 1. `--space` — registry lookup (errors if that space isn't running).
|
|
72
|
+
* 2. `--server` — registry entry on that server (for creds/personas), else the local project.
|
|
73
|
+
* 3. A genuine local project (`cwd` walks up to a real `.cotal/`) — local wins, like `git config`.
|
|
74
|
+
* 4. The registry: 0 ⇒ error; 1 ⇒ use it; N ⇒ `current` if set, else error naming each + its root.
|
|
75
|
+
* No silent fallback — an unresolved target throws one human sentence.
|
|
76
|
+
*/
|
|
77
|
+
export function resolveMeshTarget(cwd, flags = {}) {
|
|
78
|
+
if (flags.space) {
|
|
79
|
+
const m = findMesh(flags.space);
|
|
80
|
+
if (!m)
|
|
81
|
+
throw new MeshTargetError("unknown-space", `no mesh named "${flags.space}" is running`, {
|
|
82
|
+
requested: flags.space,
|
|
83
|
+
});
|
|
84
|
+
// An explicit `--server` that overrides the recorded broker is an operator-supplied endpoint, not
|
|
85
|
+
// the registry's — probing IT must never prune the recorded entry (a dead override would otherwise
|
|
86
|
+
// delete a live registered mesh, and pre-pruning would block a live-override recovery). Mark it so
|
|
87
|
+
// preflight classifies the failure as non-registry / no-prune.
|
|
88
|
+
const overriding = flags.server !== undefined && flags.server !== m.server;
|
|
89
|
+
return targetFromEntry(m, flags.server ?? m.server, overriding ? "flag-space-override" : "flag-space");
|
|
90
|
+
}
|
|
91
|
+
if (flags.server) {
|
|
92
|
+
const m = loadMeshes().find((e) => e.server === flags.server);
|
|
93
|
+
if (m)
|
|
94
|
+
return targetFromEntry(m, flags.server, "flag-server");
|
|
95
|
+
return localTarget(findCotalRoot(cwd), flags.server, "flag-server");
|
|
96
|
+
}
|
|
97
|
+
const root = findCotalRoot(cwd);
|
|
98
|
+
if (isGenuineSpace(root)) {
|
|
99
|
+
// Local project wins by root — but if its mesh is in the registry, use the RECORDED server +
|
|
100
|
+
// mode, not DEFAULT_SERVER: a project started with `--server …:4333` must spawn against :4333,
|
|
101
|
+
// and a recorded OPEN mesh must not mint creds off stale `.cotal/auth` left on disk. Fall back
|
|
102
|
+
// to the local default only when nothing is recorded for this root.
|
|
103
|
+
const recorded = loadMeshes().find((m) => resolve(m.root) === resolve(root));
|
|
104
|
+
if (recorded)
|
|
105
|
+
return targetFromEntry(recorded, recorded.server, "local-recorded");
|
|
106
|
+
// No record for this root (migration, or our broker went down and the entry was just pruned).
|
|
107
|
+
// Before guessing DEFAULT_SERVER, refuse if a DIFFERENT mesh is recorded there — otherwise the
|
|
108
|
+
// fallback would silently join someone else's mesh on the default port with our persona (the
|
|
109
|
+
// exact silent-wrong-mesh outcome this feature exists to prevent).
|
|
110
|
+
const onDefault = loadMeshes().find((m) => m.server === DEFAULT_SERVER && resolve(m.root) !== resolve(root));
|
|
111
|
+
if (onDefault)
|
|
112
|
+
throw new MeshTargetError("default-occupied", `another mesh ("${onDefault.space}") is running at ${DEFAULT_SERVER}`, { space: onDefault.space, server: DEFAULT_SERVER });
|
|
113
|
+
return localTarget(root, DEFAULT_SERVER, "local-space");
|
|
114
|
+
}
|
|
115
|
+
const meshes = loadMeshes();
|
|
116
|
+
if (meshes.length === 0)
|
|
117
|
+
throw new MeshTargetError("no-meshes", "no mesh running");
|
|
118
|
+
if (meshes.length === 1)
|
|
119
|
+
return targetFromEntry(meshes[0], meshes[0].server, "registry");
|
|
120
|
+
const current = getCurrent();
|
|
121
|
+
const cur = current ? findMesh(current) : undefined;
|
|
122
|
+
if (cur)
|
|
123
|
+
return targetFromEntry(cur, cur.server, "current");
|
|
124
|
+
const names = meshes.map((m) => `${m.space} (${m.root})`);
|
|
125
|
+
throw new MeshTargetError("ambiguous-target", `multiple meshes running: ${names.join(", ")}`, {
|
|
126
|
+
available: names,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=mesh-target.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mesh-target.js","sourceRoot":"","sources":["../src/mesh-target.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,cAAc,EAAE,aAAa,EAAkB,MAAM,gBAAgB,CAAC;AAC/E,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AACxE,OAAO,EACL,QAAQ,EACR,UAAU,EACV,YAAY,EACZ,UAAU,EACV,UAAU,GAEX,MAAM,oBAAoB,CAAC;AAuE5B,MAAM,sBAAsB,GAAG,mCAAmC,CAAC;AAEnE;;;;GAIG;AACH,MAAM,OAAO,eAAgB,SAAQ,KAAK;IACxC,mFAAmF;IAC1E,KAAK,GAAG,sBAAsB,CAAC;IAC/B,IAAI,CAAsB;IAC1B,OAAO,CAAyB;IACzC,YACE,IAAyB,EACzB,OAAe,EACf,UAAkC,EAAE,EACpC,OAA6B;QAE7B,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;QAC9B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;CACF;AAED;uGACuG;AACvG,MAAM,UAAU,sBAAsB,CAAC,CAAU;IAC/C,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a,CAAC,CAAC;IAC1D,CAAC;IAED,MAAM,MAAM,GAAG,UAAU,EAAE,CAAC;IAC5B,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC;QAAE,MAAM,IAAI,eAAe,CAAC,WAAW,EAAE,iBAAiB,CAAC,CAAC;IACnF,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,eAAe,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IAEzF,MAAM,OAAO,GAAG,UAAU,EAAE,CAAC;IAC7B,MAAM,GAAG,GAAG,OAAO,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IACpD,IAAI,GAAG;QAAE,OAAO,eAAe,CAAC,GAAG,EAAE,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IAE5D,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,KAAK,KAAK,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC;IAC1D,MAAM,IAAI,eAAe,CAAC,kBAAkB,EAAE,4BAA4B,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,EAAE;QAC5F,SAAS,EAAE,KAAK;KACjB,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { MeshTarget } from "./mesh-target.js";
|
|
2
|
+
/**
|
|
3
|
+
* Liveness verification for a resolved mesh target — the companion to {@link resolveMeshTarget}
|
|
4
|
+
* ("which mesh") that answers "is it actually up, and does the registry still reflect reality".
|
|
5
|
+
*
|
|
6
|
+
* Lives in `@cotal-ai/workspace` (beside the registry and target resolution, over core's
|
|
7
|
+
* `probeConnect`) so every surface shares ONE preflight rule instead of re-deriving it: the CLI's
|
|
8
|
+
* `connectOrExit` and the manager's control commands both wrap these helpers. It owns the MECHANICS
|
|
9
|
+
* only — the classify decision and the probe — never the I/O or the copy: the canonical `cotal …`
|
|
10
|
+
* wording is {@link renderWorkspaceError}'s job, colour and `process.exit` stay at each call site,
|
|
11
|
+
* and pruning is the caller's explicit act, not a side effect of probing.
|
|
12
|
+
*/
|
|
13
|
+
/** The five distinct ways a preflight fails. Each also carries whether the target OWNS its registry
|
|
14
|
+
* entry (→ prune): `fromRegistry` means the server+mode came from a registry record (incl. a
|
|
15
|
+
* `local-recorded` project matched by root), so a definitive failure is a stale-entry signal. */
|
|
16
|
+
export type PreflightFailure = "unreachable" | "registry-creds-rejected" | "registry-open-now-auth" | "creds-rejected" | "open-wants-auth";
|
|
17
|
+
/** Pure decision tree — separated from I/O so the whole branch tree is unit-testable (it's the
|
|
18
|
+
* riskiest logic: a wrong branch prunes a LIVE registry entry). Only a registry-OWNED source
|
|
19
|
+
* (`registry`/`current`/`flag-space`/`local-recorded`) is ever pruned. A non-registry source —
|
|
20
|
+
* `flag-server`/`local-space`, a raw `--creds` connection, or `flag-space-override` (a `--space`
|
|
21
|
+
* whose `--server` overrides the recorded broker) — is NEVER pruned: the probed endpoint is
|
|
22
|
+
* operator-supplied, so a failure there is the user's to diagnose, not a stale-registry signal. */
|
|
23
|
+
export declare function classifyPreflightFailure(source: MeshTarget["source"], reason: "auth-required" | "unreachable", hasAuth: boolean): {
|
|
24
|
+
prune: boolean;
|
|
25
|
+
kind: PreflightFailure;
|
|
26
|
+
};
|
|
27
|
+
/** Probe a resolved target and, on failure, classify it — WITHOUT touching the registry. Returns the
|
|
28
|
+
* decision (incl. whether the caller SHOULD prune); the caller owns the `removeMesh` + message +
|
|
29
|
+
* exit. Probes with `probeCreds` when given (the caller's `--creds`/minted creds); otherwise mints
|
|
30
|
+
* a throwaway identity from the target's own trust material to test mere liveness. */
|
|
31
|
+
export declare function preflightTarget(target: MeshTarget, probeCreds?: string): Promise<{
|
|
32
|
+
ok: true;
|
|
33
|
+
} | {
|
|
34
|
+
ok: false;
|
|
35
|
+
kind: PreflightFailure;
|
|
36
|
+
prune: boolean;
|
|
37
|
+
}>;
|
|
38
|
+
/**
|
|
39
|
+
* Drop registry entries whose broker is gone — a `cotal up` that crashed or was `kill -9`'d without
|
|
40
|
+
* `cotal down` leaves a record behind. Probe each in parallel; only `unreachable` (refused/timeout)
|
|
41
|
+
* is stale, an auth broker answering `auth-required` is alive. An EXPLICIT call (never wired into
|
|
42
|
+
* resolution itself), so registry mutation stays opt-in: callers that act on the registry
|
|
43
|
+
* (`spawn`/`use`/`meshes`, the manager control commands) invoke it; `<TAB>` completion must not.
|
|
44
|
+
*/
|
|
45
|
+
export declare function pruneStaleMeshes(): Promise<void>;
|
|
46
|
+
//# sourceMappingURL=preflight.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"preflight.d.ts","sourceRoot":"","sources":["../src/preflight.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD;;;;;;;;;;GAUG;AAEH;;kGAEkG;AAClG,MAAM,MAAM,gBAAgB,GACxB,aAAa,GACb,yBAAyB,GACzB,wBAAwB,GACxB,gBAAgB,GAChB,iBAAiB,CAAC;AAEtB;;;;;oGAKoG;AACpG,wBAAgB,wBAAwB,CACtC,MAAM,EAAE,UAAU,CAAC,QAAQ,CAAC,EAC5B,MAAM,EAAE,eAAe,GAAG,aAAa,EACvC,OAAO,EAAE,OAAO,GACf;IAAE,KAAK,EAAE,OAAO,CAAC;IAAC,IAAI,EAAE,gBAAgB,CAAA;CAAE,CAa5C;AAED;;;uFAGuF;AACvF,wBAAsB,eAAe,CACnC,MAAM,EAAE,UAAU,EAClB,UAAU,CAAC,EAAE,MAAM,GAClB,OAAO,CAAC;IAAE,EAAE,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,IAAI,EAAE,gBAAgB,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,CAAC,CAO/E;AAED;;;;;;GAMG;AACH,wBAAsB,gBAAgB,IAAI,OAAO,CAAC,IAAI,CAAC,CAOtD"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { mintCreds, newIdentity, probeConnect } from "@cotal-ai/core";
|
|
2
|
+
import { loadMeshes, removeMesh } from "./mesh-registry.js";
|
|
3
|
+
/** Pure decision tree — separated from I/O so the whole branch tree is unit-testable (it's the
|
|
4
|
+
* riskiest logic: a wrong branch prunes a LIVE registry entry). Only a registry-OWNED source
|
|
5
|
+
* (`registry`/`current`/`flag-space`/`local-recorded`) is ever pruned. A non-registry source —
|
|
6
|
+
* `flag-server`/`local-space`, a raw `--creds` connection, or `flag-space-override` (a `--space`
|
|
7
|
+
* whose `--server` overrides the recorded broker) — is NEVER pruned: the probed endpoint is
|
|
8
|
+
* operator-supplied, so a failure there is the user's to diagnose, not a stale-registry signal. */
|
|
9
|
+
export function classifyPreflightFailure(source, reason, hasAuth) {
|
|
10
|
+
// `flag-space-override` and `flag-server` are deliberately absent: the probe hit an operator-named
|
|
11
|
+
// endpoint, not the registry-recorded broker, so its failure must not delete the recorded entry.
|
|
12
|
+
const fromRegistry = source === "registry" ||
|
|
13
|
+
source === "current" ||
|
|
14
|
+
source === "flag-space" ||
|
|
15
|
+
source === "local-recorded";
|
|
16
|
+
if (reason === "unreachable")
|
|
17
|
+
return { prune: fromRegistry, kind: "unreachable" };
|
|
18
|
+
if (fromRegistry && hasAuth)
|
|
19
|
+
return { prune: true, kind: "registry-creds-rejected" };
|
|
20
|
+
if (fromRegistry)
|
|
21
|
+
return { prune: true, kind: "registry-open-now-auth" };
|
|
22
|
+
if (hasAuth)
|
|
23
|
+
return { prune: false, kind: "creds-rejected" };
|
|
24
|
+
return { prune: false, kind: "open-wants-auth" };
|
|
25
|
+
}
|
|
26
|
+
/** Probe a resolved target and, on failure, classify it — WITHOUT touching the registry. Returns the
|
|
27
|
+
* decision (incl. whether the caller SHOULD prune); the caller owns the `removeMesh` + message +
|
|
28
|
+
* exit. Probes with `probeCreds` when given (the caller's `--creds`/minted creds); otherwise mints
|
|
29
|
+
* a throwaway identity from the target's own trust material to test mere liveness. */
|
|
30
|
+
export async function preflightTarget(target, probeCreds) {
|
|
31
|
+
const creds = probeCreds ?? (target.auth ? await mintCreds(target.auth, newIdentity(), "manager") : undefined);
|
|
32
|
+
const probe = await probeConnect(target.server, creds ? { creds } : {});
|
|
33
|
+
if (probe.ok)
|
|
34
|
+
return { ok: true };
|
|
35
|
+
const { prune, kind } = classifyPreflightFailure(target.source, probe.reason, Boolean(target.auth));
|
|
36
|
+
return { ok: false, kind, prune };
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Drop registry entries whose broker is gone — a `cotal up` that crashed or was `kill -9`'d without
|
|
40
|
+
* `cotal down` leaves a record behind. Probe each in parallel; only `unreachable` (refused/timeout)
|
|
41
|
+
* is stale, an auth broker answering `auth-required` is alive. An EXPLICIT call (never wired into
|
|
42
|
+
* resolution itself), so registry mutation stays opt-in: callers that act on the registry
|
|
43
|
+
* (`spawn`/`use`/`meshes`, the manager control commands) invoke it; `<TAB>` completion must not.
|
|
44
|
+
*/
|
|
45
|
+
export async function pruneStaleMeshes() {
|
|
46
|
+
await Promise.all(loadMeshes().map(async (m) => {
|
|
47
|
+
const r = await probeConnect(m.server);
|
|
48
|
+
if (!r.ok && r.reason === "unreachable")
|
|
49
|
+
removeMesh(m.space);
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=preflight.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"preflight.js","sourceRoot":"","sources":["../src/preflight.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AACtE,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAyB5D;;;;;oGAKoG;AACpG,MAAM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B,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;IACpG,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;AACpC,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB;IACpC,MAAM,OAAO,CAAC,GAAG,CACf,UAAU,EAAE,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,EAAE,EAAE;QAC3B,MAAM,CAAC,GAAG,MAAM,YAAY,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;QACvC,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,MAAM,KAAK,aAAa;YAAE,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;IAC/D,CAAC,CAAC,CACH,CAAC;AACJ,CAAC"}
|
package/dist/render.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { MeshTarget, MeshTargetError } from "./mesh-target.js";
|
|
2
|
+
import type { PreflightFailure } from "./preflight.js";
|
|
3
|
+
/**
|
|
4
|
+
* The single home for the toolchain's `cotal …` wording — optional and presentation-only.
|
|
5
|
+
*
|
|
6
|
+
* This is NOT part of the typed contract. Workspace internals never call it to decide behavior, no
|
|
7
|
+
* control flow parses its output, and the rendered string is never baked back into a thrown error or
|
|
8
|
+
* a result. A consumer that wants its own affordance — a web UI with a button, an SDK embed with no
|
|
9
|
+
* command at all — reads the structured `{code, details}` / `{kind, …}` and ignores this entirely,
|
|
10
|
+
* losing nothing. It exists so the CLI, the manager, and the delivery daemon speak the canonical
|
|
11
|
+
* command copy with ONE voice instead of each hand-rolling it (the drift that motivated the split).
|
|
12
|
+
* No colour, no `process`, no exit — the caller owns those.
|
|
13
|
+
*/
|
|
14
|
+
export type WorkspaceError = {
|
|
15
|
+
kind: "target";
|
|
16
|
+
error: MeshTargetError;
|
|
17
|
+
} | {
|
|
18
|
+
kind: "preflight";
|
|
19
|
+
failure: PreflightFailure;
|
|
20
|
+
target: MeshTarget;
|
|
21
|
+
pruned: boolean;
|
|
22
|
+
} | {
|
|
23
|
+
kind: "reachable";
|
|
24
|
+
reason: "auth-required" | "unreachable";
|
|
25
|
+
server: string;
|
|
26
|
+
};
|
|
27
|
+
/** Render a workspace failure as the canonical one-line `cotal …` sentence. */
|
|
28
|
+
export declare function renderWorkspaceError(e: WorkspaceError): string;
|
|
29
|
+
//# sourceMappingURL=render.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"render.d.ts","sourceRoot":"","sources":["../src/render.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AACpE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAEvD;;;;;;;;;;GAUG;AACH,MAAM,MAAM,cAAc,GACtB;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,KAAK,EAAE,eAAe,CAAA;CAAE,GAC1C;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,OAAO,EAAE,gBAAgB,CAAC;IAAC,MAAM,EAAE,UAAU,CAAC;IAAC,MAAM,EAAE,OAAO,CAAA;CAAE,GACrF;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,MAAM,EAAE,eAAe,GAAG,aAAa,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAEnF,+EAA+E;AAC/E,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,cAAc,GAAG,MAAM,CAS9D"}
|
package/dist/render.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/** Render a workspace failure as the canonical one-line `cotal …` sentence. */
|
|
2
|
+
export function renderWorkspaceError(e) {
|
|
3
|
+
switch (e.kind) {
|
|
4
|
+
case "target":
|
|
5
|
+
return renderTargetError(e.error);
|
|
6
|
+
case "preflight":
|
|
7
|
+
return renderPreflightFailure(e.failure, e.target, e.pruned);
|
|
8
|
+
case "reachable":
|
|
9
|
+
return renderReachable(e.reason, e.server);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/** "Which mesh" resolution failures — maps a {@link MeshTargetError}'s `{code, details}` to copy. */
|
|
13
|
+
function renderTargetError(err) {
|
|
14
|
+
const d = err.details;
|
|
15
|
+
switch (err.code) {
|
|
16
|
+
case "no-meshes":
|
|
17
|
+
return "✗ no mesh running — run `cotal up` in a project, or pass `--server`";
|
|
18
|
+
case "unknown-space":
|
|
19
|
+
return `✗ no mesh named "${d.requested}" is running — see \`cotal meshes\``;
|
|
20
|
+
case "ambiguous-target":
|
|
21
|
+
return `✗ multiple meshes running — ${(d.available ?? []).join(", ")}. Pick one with \`--space <name>\` or set a default with \`cotal use <name>\`.`;
|
|
22
|
+
case "default-occupied":
|
|
23
|
+
return `✗ another mesh ("${d.space}") is running at ${d.server} — run \`cotal up\` here to start yours, or \`--space ${d.space}\` to join it`;
|
|
24
|
+
case "stale-auth-root":
|
|
25
|
+
return `✗ registry entry "${d.space}" points at ${d.root}, whose auth is now for "${d.found}" — stale entry removed; re-run \`cotal up\` or check \`cotal meshes\``;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/** "Is it live" failures on a registry-resolved target — the classified preflight sentence. */
|
|
29
|
+
function renderPreflightFailure(kind, t, pruned) {
|
|
30
|
+
switch (kind) {
|
|
31
|
+
case "unreachable":
|
|
32
|
+
return `✗ no mesh running at ${t.server}${pruned ? " (stale registry entry — removed)" : ""} — run \`cotal up\``;
|
|
33
|
+
case "registry-creds-rejected":
|
|
34
|
+
return `✗ mesh "${t.space}" at ${t.server} no longer matches its registry entry (credentials rejected — port reused?) — re-run \`cotal up\` from ${t.root}, or \`cotal meshes\` to see what's live`;
|
|
35
|
+
case "registry-open-now-auth":
|
|
36
|
+
return `✗ open mesh "${t.space}" at ${t.server} no longer matches its registry entry (broker now requires auth — port reused?) — re-run \`cotal up\` from ${t.root}, or \`cotal meshes\` to see what's live`;
|
|
37
|
+
case "creds-rejected":
|
|
38
|
+
return `✗ credentials for "${t.space}" were rejected at ${t.server} — a different mesh may be running there. Run \`cotal meshes\` to check, or \`cotal up\` here to start yours`;
|
|
39
|
+
case "open-wants-auth":
|
|
40
|
+
return `✗ broker at ${t.server} requires auth, but this mesh is open (no trust material) — use \`--space <name>\` for an auth mesh, or run \`cotal up\` here without \`--open\``;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** Plain reachability for a RAW (off-registry) probe — the `--creds` / `--server`+unregistered-`--space`
|
|
44
|
+
* escape hatch, which never touches the registry (no prune, no stale-entry wording). */
|
|
45
|
+
function renderReachable(reason, server) {
|
|
46
|
+
return reason === "auth-required"
|
|
47
|
+
? `✗ credentials rejected at ${server} — check your creds, or the broker wants different auth`
|
|
48
|
+
: `✗ can't reach a broker at ${server} — is it running? (\`cotal up\`)`;
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=render.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"render.js","sourceRoot":"","sources":["../src/render.ts"],"names":[],"mappings":"AAmBA,+EAA+E;AAC/E,MAAM,UAAU,oBAAoB,CAAC,CAAiB;IACpD,QAAQ,CAAC,CAAC,IAAI,EAAE,CAAC;QACf,KAAK,QAAQ;YACX,OAAO,iBAAiB,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;QACpC,KAAK,WAAW;YACd,OAAO,sBAAsB,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC;QAC/D,KAAK,WAAW;YACd,OAAO,eAAe,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC;IAC/C,CAAC;AACH,CAAC;AAED,qGAAqG;AACrG,SAAS,iBAAiB,CAAC,GAAoB;IAC7C,MAAM,CAAC,GAAG,GAAG,CAA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C;IACrL,CAAC;AACH,CAAC;AAED;yFACyF;AACzF,SAAS,eAAe,CAAC,MAAuC,EAAE,MAAc;IAC9E,OAAO,MAAM,KAAK,eAAe;QAC/B,CAAC,CAAC,6BAA6B,MAAM,yDAAyD;QAC9F,CAAC,CAAC,6BAA6B,MAAM,kCAAkC,CAAC;AAC5E,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cotal-ai/workspace",
|
|
3
|
+
"description": "Cotal machine-local workstation layer: the ~/.cotal mesh registry, target resolution, preflight, on-disk auth-path I/O, and the command-copy renderer — operator concerns over a local checkout, kept separate from the wire protocol in @cotal-ai/core.",
|
|
4
|
+
"version": "0.7.0",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/Cotal-AI/Cotal.git",
|
|
9
|
+
"directory": "packages/workspace"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"import": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@cotal-ai/core": "0.7.0"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
31
|
+
"build": "tsc -p tsconfig.json",
|
|
32
|
+
"test": "tsx smoke/preflight.smoke.ts"
|
|
33
|
+
}
|
|
34
|
+
}
|