@system-inc/cohere 0.1.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-APACHE +202 -0
- package/LICENSE-MIT +21 -0
- package/NOTICE +419 -0
- package/SHA256SUMS +8 -0
- package/THIRD_PARTY_NOTICES.md +3826 -0
- package/bin/cohere +271 -0
- package/package.json +32 -0
- package/schema/CohereSettings.schema.json +1739 -0
- package/schema/NexusCohereSettings.schema.json +1742 -0
package/bin/cohere
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
// The cohere launcher. It finds the binary for this platform and hands the process over to it.
|
|
5
|
+
//
|
|
6
|
+
// The one rule this file exists to hold: a missing binary exits non-zero naming the platform. It
|
|
7
|
+
// never falls through to a bare command name, never resolves a binary built for another platform,
|
|
8
|
+
// and never exits zero having run nothing. The gate cohere replaces printed a green checkmark over
|
|
9
|
+
// zero files for days because a resolver did exactly that, and an empty result is indistinguishable
|
|
10
|
+
// from a clean tree.
|
|
11
|
+
//
|
|
12
|
+
// The second rule: a binary that differs from the checksum this package was released with is never
|
|
13
|
+
// run. A CI that installs cohere executes it on every commit, which is a trust extended to us, so
|
|
14
|
+
// what it runs has to be what the release built.
|
|
15
|
+
|
|
16
|
+
const childProcess = require('node:child_process');
|
|
17
|
+
const crypto = require('node:crypto');
|
|
18
|
+
const fs = require('node:fs');
|
|
19
|
+
const os = require('node:os');
|
|
20
|
+
const path = require('node:path');
|
|
21
|
+
|
|
22
|
+
const platformPackagePrefix = '@system-inc/cohere-';
|
|
23
|
+
const packageScope = '@system-inc';
|
|
24
|
+
const overrideVariable = 'COHERE_BINARY';
|
|
25
|
+
const checksumsFileName = 'SHA256SUMS';
|
|
26
|
+
|
|
27
|
+
// The prefix is the dispatcher's own name and a hyphen.
|
|
28
|
+
const dispatcherPackage = platformPackagePrefix.slice(0, -1);
|
|
29
|
+
|
|
30
|
+
// Node's process.arch and process.platform are already npm's spelling, which is the same spelling
|
|
31
|
+
// the platform packages publish under. No translation belongs here.
|
|
32
|
+
const platformPackage = platformPackagePrefix + process.platform + '-' + process.arch;
|
|
33
|
+
const binaryFileName = process.platform === 'win32' ? 'cohere.exe' : 'cohere';
|
|
34
|
+
|
|
35
|
+
function fail(message) {
|
|
36
|
+
process.stderr.write('cohere: ' + message + '\n');
|
|
37
|
+
process.exit(1);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// resolveOverride validates an explicitly requested binary.
|
|
41
|
+
//
|
|
42
|
+
// Every failure is fatal rather than a fallback. Someone who sets this has stated which binary they
|
|
43
|
+
// want to run, so quietly running a different one would hand them results they would read as their
|
|
44
|
+
// own build's. A stale export in a shell profile is exactly how that happens.
|
|
45
|
+
function resolveOverride(requested) {
|
|
46
|
+
const resolved = path.resolve(requested);
|
|
47
|
+
let information;
|
|
48
|
+
try {
|
|
49
|
+
information = fs.statSync(resolved);
|
|
50
|
+
} catch {
|
|
51
|
+
fail(overrideVariable + ' points at ' + resolved + ', and there is nothing there.');
|
|
52
|
+
}
|
|
53
|
+
if (information.isDirectory()) {
|
|
54
|
+
fail(overrideVariable + ' points at ' + resolved + ', which is a directory rather than a binary.');
|
|
55
|
+
}
|
|
56
|
+
return resolved;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// resolvePlatformPackage asks the package manager where the platform package is.
|
|
60
|
+
//
|
|
61
|
+
// require.resolve is used rather than a walk up node_modules because it is the package manager's
|
|
62
|
+
// own answer: it understands pnpm's isolated store, npm's hoisting, and yarn's layouts without this
|
|
63
|
+
// file having to model any of them. The package.json is resolved rather than the package itself
|
|
64
|
+
// because the platform packages have no entry point — they are a binary and a manifest.
|
|
65
|
+
function resolvePlatformPackage() {
|
|
66
|
+
let manifestPath;
|
|
67
|
+
try {
|
|
68
|
+
manifestPath = require.resolve(platformPackage + '/package.json');
|
|
69
|
+
} catch {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
return path.dirname(manifestPath);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// verifyPlatformPackage checks each file the platform package ships against the checksums this
|
|
76
|
+
// dispatcher was published with, and runs nothing when one differs.
|
|
77
|
+
//
|
|
78
|
+
// The dispatcher pins its platform packages to its own exact version, so its SHA256SUMS describe the
|
|
79
|
+
// only binaries it is ever installed beside. A binary that differs changed after the release built
|
|
80
|
+
// it: a corrupted download or cache, a mirror serving something else, or a compromised registry.
|
|
81
|
+
// Each is refused, naming the file, and none falls back to running the binary anyway.
|
|
82
|
+
//
|
|
83
|
+
// Hashing the binaries costs tens of milliseconds, which every run would pay, so a passing verdict is
|
|
84
|
+
// remembered in the user cache. It holds each file's device, inode, size, and modification and change
|
|
85
|
+
// times, beside the hash it was checked against, and is honored only when all of them still match. Any
|
|
86
|
+
// write to a file moves its change time, which no unprivileged process can set back, and a replaced file
|
|
87
|
+
// has a new inode, so a remembered verdict only ever describes the bytes it was reached on. The files are
|
|
88
|
+
// stat'ed before they are hashed, so a write landing during the hash leaves a verdict that no longer
|
|
89
|
+
// matches. This guards what reaches the machine, not against someone who can already write to
|
|
90
|
+
// node_modules: they could edit this file instead.
|
|
91
|
+
function verifyPlatformPackage(packageDirectory) {
|
|
92
|
+
const checksumsPath = path.join(__dirname, '..', checksumsFileName);
|
|
93
|
+
let checksums;
|
|
94
|
+
try {
|
|
95
|
+
checksums = fs.readFileSync(checksumsPath, 'utf8');
|
|
96
|
+
} catch {
|
|
97
|
+
fail(
|
|
98
|
+
checksumsPath + ' is missing, so ' + platformPackage + ' cannot be checked against the release.\n' +
|
|
99
|
+
' The ' + dispatcherPackage + ' package is incomplete — reinstall it.',
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Lines name files from the directory holding the packages, as "cohere-darwin-arm64/bin/cohere":
|
|
104
|
+
// the package name without its scope, which is how node_modules lays a scope out.
|
|
105
|
+
const directoryName = platformPackage.slice(packageScope.length + 1);
|
|
106
|
+
const expected = [];
|
|
107
|
+
for (const line of checksums.split('\n')) {
|
|
108
|
+
const match = /^([0-9a-f]{64}) {2}(.+)$/.exec(line);
|
|
109
|
+
if (match !== null && match[2].startsWith(directoryName + '/')) {
|
|
110
|
+
expected.push({ hash: match[1], file: path.join(packageDirectory, match[2].slice(directoryName.length + 1)) });
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (expected.length === 0) {
|
|
114
|
+
fail(
|
|
115
|
+
checksumsPath + ' lists no binary for ' + platformPackage + ', so it cannot be checked against the release.\n' +
|
|
116
|
+
' Reinstall ' + dispatcherPackage + '.',
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const identities = expected.map((entry) => {
|
|
121
|
+
let information;
|
|
122
|
+
try {
|
|
123
|
+
information = fs.statSync(entry.file, { bigint: true });
|
|
124
|
+
} catch {
|
|
125
|
+
fail(
|
|
126
|
+
platformPackage + ' is installed but ' + entry.file + ', which the release shipped, is missing.\n' +
|
|
127
|
+
' The package is incomplete — reinstall it.',
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
return [entry.hash, information.dev, information.ino, information.size, information.mtimeNs, information.ctimeNs].join(' ');
|
|
131
|
+
});
|
|
132
|
+
const verdict = identities.join('\n');
|
|
133
|
+
const verdictPath = rememberedVerdictPath(packageDirectory);
|
|
134
|
+
if (verdictPath !== null && readQuietly(verdictPath) === verdict) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
for (const entry of expected) {
|
|
139
|
+
let actual;
|
|
140
|
+
try {
|
|
141
|
+
actual = crypto.createHash('sha256').update(fs.readFileSync(entry.file)).digest('hex');
|
|
142
|
+
} catch (error) {
|
|
143
|
+
fail('could not read ' + entry.file + ' to check it against the release: ' + error.message);
|
|
144
|
+
}
|
|
145
|
+
if (actual !== entry.hash) {
|
|
146
|
+
fail(
|
|
147
|
+
entry.file + ' does not match the checksum ' + dispatcherPackage + ' was released with, so it was not run.\n' +
|
|
148
|
+
' expected ' + entry.hash + '\n' +
|
|
149
|
+
' found ' + actual + '\n' +
|
|
150
|
+
' It changed after the release built it: a corrupted download or cache, or tampering.\n' +
|
|
151
|
+
' Reinstall ' + platformPackage + '. To run a build of your own on purpose, set ' + overrideVariable + '.',
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (verdictPath !== null) {
|
|
157
|
+
rememberQuietly(verdictPath, verdict);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// rememberedVerdictPath is where a passing verdict for one installed package is kept, in the same user
|
|
162
|
+
// cache directory Go's os.UserCacheDir names, or null when this machine has none.
|
|
163
|
+
function rememberedVerdictPath(packageDirectory) {
|
|
164
|
+
let cacheDirectory;
|
|
165
|
+
try {
|
|
166
|
+
if (process.platform === 'win32') {
|
|
167
|
+
cacheDirectory = process.env.LOCALAPPDATA;
|
|
168
|
+
} else if (process.platform === 'darwin') {
|
|
169
|
+
cacheDirectory = path.join(os.homedir(), 'Library', 'Caches');
|
|
170
|
+
} else {
|
|
171
|
+
const configured = process.env.XDG_CACHE_HOME;
|
|
172
|
+
cacheDirectory = configured && path.isAbsolute(configured) ? configured : path.join(os.homedir(), '.cache');
|
|
173
|
+
}
|
|
174
|
+
} catch {
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
if (!cacheDirectory) {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
const key = crypto.createHash('sha256').update(packageDirectory).digest('hex');
|
|
181
|
+
return path.join(cacheDirectory, 'cohere', 'verified-binaries', key);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function readQuietly(file) {
|
|
185
|
+
try {
|
|
186
|
+
return fs.readFileSync(file, 'utf8');
|
|
187
|
+
} catch {
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// rememberQuietly keeps a verdict, written beside its place and renamed in, so two runs at once never
|
|
193
|
+
// read half of one. A cache that cannot be written costs the next run a hash and nothing else, so
|
|
194
|
+
// failures here are ignored: the binary was already checked.
|
|
195
|
+
function rememberQuietly(file, verdict) {
|
|
196
|
+
const temporary = file + '.' + process.pid + '.tmp';
|
|
197
|
+
try {
|
|
198
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
199
|
+
fs.writeFileSync(temporary, verdict);
|
|
200
|
+
fs.renameSync(temporary, file);
|
|
201
|
+
} catch {
|
|
202
|
+
try {
|
|
203
|
+
fs.unlinkSync(temporary);
|
|
204
|
+
} catch {
|
|
205
|
+
// Never written.
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function resolveBinary() {
|
|
211
|
+
// An override is not checked: whoever sets it has named the binary they mean to run, which is
|
|
212
|
+
// usually one they built, and no release checksum describes it.
|
|
213
|
+
const requested = (process.env[overrideVariable] || '').trim();
|
|
214
|
+
if (requested) {
|
|
215
|
+
return resolveOverride(requested);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const packageDirectory = resolvePlatformPackage();
|
|
219
|
+
if (packageDirectory === null) {
|
|
220
|
+
fail(
|
|
221
|
+
'no binary for ' + process.platform + '/' + process.arch + '.\n' +
|
|
222
|
+
' ' + platformPackage + ' is not installed.\n' +
|
|
223
|
+
' If your package manager skipped optional dependencies, reinstall without that flag.\n' +
|
|
224
|
+
' To point at a local build instead, set ' + overrideVariable + '.',
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const candidate = path.join(packageDirectory, 'bin', binaryFileName);
|
|
229
|
+
if (!fs.existsSync(candidate)) {
|
|
230
|
+
// The package resolved but its binary is absent. This is a packaging failure rather than an
|
|
231
|
+
// unsupported platform, and it is named separately so nobody goes hunting the wrong bug.
|
|
232
|
+
fail(
|
|
233
|
+
platformPackage + ' is installed but its binary is missing at ' + candidate + '.\n' +
|
|
234
|
+
' The package is incomplete — reinstall it.',
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
verifyPlatformPackage(packageDirectory);
|
|
239
|
+
return candidate;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const binary = resolveBinary();
|
|
243
|
+
const forwarded = process.argv.slice(2);
|
|
244
|
+
|
|
245
|
+
// Replace this process where the platform allows it, so the binary inherits the terminal directly
|
|
246
|
+
// and its exit status is the one the caller sees, with no layer in between able to lose it. Losing
|
|
247
|
+
// a non-zero exit is how a gate goes quietly green, which is the failure this whole tool exists to
|
|
248
|
+
// make impossible.
|
|
249
|
+
if (process.platform !== 'win32' && typeof process.execve === 'function') {
|
|
250
|
+
try {
|
|
251
|
+
process.execve(binary, [binary, ...forwarded], process.env);
|
|
252
|
+
} catch {
|
|
253
|
+
// Older Node, or a platform that will not execve. The spawn below is the real path.
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const result = childProcess.spawnSync(binary, forwarded, { stdio: 'inherit' });
|
|
258
|
+
|
|
259
|
+
if (result.error) {
|
|
260
|
+
fail('could not run ' + binary + ': ' + result.error.message);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// A process killed by a signal reports a null status. Exiting zero there would report success for a
|
|
264
|
+
// run that was terminated, so the conventional 128 + signal encoding is used instead. This is the
|
|
265
|
+
// case the obvious implementation gets wrong, because null is falsy and reads as "fine".
|
|
266
|
+
if (result.status === null) {
|
|
267
|
+
const signalNumber = os.constants.signals[result.signal] || 0;
|
|
268
|
+
process.exit(signalNumber ? 128 + signalNumber : 1);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
process.exit(result.status);
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"author": "System, Inc.",
|
|
3
|
+
"bin": {
|
|
4
|
+
"c": "bin/cohere",
|
|
5
|
+
"cohere": "bin/cohere"
|
|
6
|
+
},
|
|
7
|
+
"description": "Type-check, lint, fix, and format a TypeScript codebase in one process.",
|
|
8
|
+
"files": [
|
|
9
|
+
"bin/",
|
|
10
|
+
"schema/",
|
|
11
|
+
"SHA256SUMS",
|
|
12
|
+
"LICENSE-APACHE",
|
|
13
|
+
"LICENSE-MIT",
|
|
14
|
+
"NOTICE",
|
|
15
|
+
"THIRD_PARTY_NOTICES.md"
|
|
16
|
+
],
|
|
17
|
+
"license": "MIT OR Apache-2.0",
|
|
18
|
+
"name": "@system-inc/cohere",
|
|
19
|
+
"optionalDependencies": {
|
|
20
|
+
"@system-inc/cohere-darwin-arm64": "0.1.0",
|
|
21
|
+
"@system-inc/cohere-darwin-x64": "0.1.0",
|
|
22
|
+
"@system-inc/cohere-linux-arm64": "0.1.0",
|
|
23
|
+
"@system-inc/cohere-linux-x64": "0.1.0",
|
|
24
|
+
"@system-inc/cohere-win32-arm64": "0.1.0",
|
|
25
|
+
"@system-inc/cohere-win32-x64": "0.1.0"
|
|
26
|
+
},
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/system-inc/cohere.git"
|
|
30
|
+
},
|
|
31
|
+
"version": "0.1.0"
|
|
32
|
+
}
|