megacode-agent-sdk 0.0.0-stage → 0.32.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 +176 -0
- package/LICENSE-MIT +23 -0
- package/README.md +38 -2
- package/dist/binary/archive.d.ts +7 -0
- package/dist/binary/archive.js +215 -0
- package/dist/binary/cache.d.ts +21 -0
- package/dist/binary/cache.js +256 -0
- package/dist/binary/download.d.ts +16 -0
- package/dist/binary/download.js +174 -0
- package/dist/binary/index.d.ts +49 -0
- package/dist/binary/index.js +160 -0
- package/dist/client.d.ts +94 -0
- package/dist/client.js +868 -0
- package/dist/errors.d.ts +58 -0
- package/dist/errors.js +97 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.js +42 -0
- package/dist/options.d.ts +48 -0
- package/dist/options.js +49 -0
- package/dist/pins.d.ts +5 -0
- package/dist/pins.js +22 -0
- package/dist/process.d.ts +72 -0
- package/dist/process.js +255 -0
- package/dist/tools.d.ts +74 -0
- package/dist/tools.js +256 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +3 -0
- package/dist/wire.d.ts +241 -0
- package/dist/wire.js +620 -0
- package/package.json +33 -4
package/LICENSE-APACHE
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
package/LICENSE-MIT
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
Permission is hereby granted, free of charge, to any
|
|
2
|
+
person obtaining a copy of this software and associated
|
|
3
|
+
documentation files (the "Software"), to deal in the
|
|
4
|
+
Software without restriction, including without
|
|
5
|
+
limitation the rights to use, copy, modify, merge,
|
|
6
|
+
publish, distribute, sublicense, and/or sell copies of
|
|
7
|
+
the Software, and to permit persons to whom the Software
|
|
8
|
+
is furnished to do so, subject to the following
|
|
9
|
+
conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice
|
|
12
|
+
shall be included in all copies or substantial portions
|
|
13
|
+
of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
|
16
|
+
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
17
|
+
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
18
|
+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
|
19
|
+
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
20
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
|
22
|
+
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
23
|
+
DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,39 @@
|
|
|
1
|
-
#
|
|
1
|
+
# megacode-agent-sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Drive a MEGA harness agent session from a TypeScript program: start a session, give the model tools your
|
|
4
|
+
program runs, answer its approval requests, and read what it does. One session is one `harness` process. Use
|
|
5
|
+
it to build an agent that is not a coding agent.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install megacode-agent-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Node 20 or newer; an ES module; no runtime dependencies.
|
|
12
|
+
|
|
13
|
+
The package needs a `harness` binary of the same major and minor version. It uses the `binary` option, then the
|
|
14
|
+
`MEGACODE_AGENT_SDK_BINARY` environment variable, then a verified copy in its cache, and otherwise downloads
|
|
15
|
+
the matching release and checks it against digests built into the package.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { Client, plain, type Tool } from "megacode-agent-sdk";
|
|
19
|
+
|
|
20
|
+
const search: Tool = {
|
|
21
|
+
name: "search_trains",
|
|
22
|
+
description: "Find trains to a destination.",
|
|
23
|
+
inputSchema: { type: "object", properties: { to: { type: "string" } }, required: ["to"] },
|
|
24
|
+
handler: async (args) => `to ${(args as { to: string }).to}: 9:10 for 42 pounds`,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const client = await Client.start(plain("You are Pip, a personal assistant. Be brief.", { tools: [search] }));
|
|
28
|
+
try {
|
|
29
|
+
const turn = await client.send("find me a train to Leeds");
|
|
30
|
+
for await (const event of turn) console.log(event);
|
|
31
|
+
} finally {
|
|
32
|
+
await client.close(); // always: an open client keeps Node running
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
The options, tools, approvals, testing with a mock file, versions, the download and its trust rules are in the
|
|
37
|
+
[agent SDK guide](https://megacode-harness.vercel.app/docs/sdk.html) (`docs/sdk.md` in the source).
|
|
38
|
+
|
|
39
|
+
Licensed under either of MIT or Apache-2.0, at your option.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** The largest executable accepted. */
|
|
2
|
+
export declare const MAX_EXECUTABLE: number;
|
|
3
|
+
/** The archive is refused; the text says why. */
|
|
4
|
+
export declare class ArchiveError extends Error {
|
|
5
|
+
}
|
|
6
|
+
/** The executable's bytes from a gzipped tar; `ArchiveError` otherwise. */
|
|
7
|
+
export declare function executable(gz: Uint8Array): Promise<Buffer>;
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
// The release archive reader (spec slice-ar R12.2), a port of the Rust
|
|
2
|
+
// crate's `sdk/rust/src/binary/tar.rs`: gzip, then tar; the one regular file
|
|
3
|
+
// `harness` (or `./harness`) is the executable. Pax headers (`x`, `g`) and
|
|
4
|
+
// AppleDouble members (`._*`) are skipped; any other member type, a second
|
|
5
|
+
// regular file, or a path with `..` is an error. Nothing is written to disk.
|
|
6
|
+
//
|
|
7
|
+
// Only the first gzip member is read, as the crate's `GzDecoder` reads it:
|
|
8
|
+
// the gzip header is parsed here and its deflate stream inflated raw, which
|
|
9
|
+
// stops at the stream's end (`gunzip` would go on into later members). Input
|
|
10
|
+
// cut short inflates as far as it goes (`Z_SYNC_FLUSH`), so the tar reader
|
|
11
|
+
// refuses what is missing first, as the crate's reader does; then the
|
|
12
|
+
// member's header CRC (when present) and its trailer (CRC-32 and length) are
|
|
13
|
+
// checked, as every reader of the release archive checks them. Inflating and
|
|
14
|
+
// hashing run off the event loop or in slices, so other sessions keep going.
|
|
15
|
+
import * as zlib from "node:zlib";
|
|
16
|
+
/** The largest executable accepted. */
|
|
17
|
+
export const MAX_EXECUTABLE = 256 * 1024 * 1024;
|
|
18
|
+
/** What the skipped members (pax headers, AppleDouble files, empty folders)
|
|
19
|
+
* may add up to. */
|
|
20
|
+
const MAX_SKIPPED = 16 * 1024 * 1024;
|
|
21
|
+
/** The most the first member may inflate to: the executable, the skipped
|
|
22
|
+
* members, and 16 MiB of headers and padding. */
|
|
23
|
+
const MAX_INFLATED = MAX_EXECUTABLE + MAX_SKIPPED + 16 * 1024 * 1024;
|
|
24
|
+
const BLOCK = 512;
|
|
25
|
+
/** The archive is refused; the text says why. */
|
|
26
|
+
export class ArchiveError extends Error {
|
|
27
|
+
}
|
|
28
|
+
const FHCRC = 2;
|
|
29
|
+
const FEXTRA = 4;
|
|
30
|
+
const FNAME = 8;
|
|
31
|
+
const FCOMMENT = 16;
|
|
32
|
+
const CRC_TABLE = (() => {
|
|
33
|
+
const t = new Uint32Array(256);
|
|
34
|
+
for (let n = 0; n < 256; n++) {
|
|
35
|
+
let c = n;
|
|
36
|
+
for (let k = 0; k < 8; k++)
|
|
37
|
+
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
38
|
+
t[n] = c >>> 0;
|
|
39
|
+
}
|
|
40
|
+
return t;
|
|
41
|
+
})();
|
|
42
|
+
/** The CRC-32 of gzip (ISO-HDLC) over `data`, continuing from `crc`: zlib's
|
|
43
|
+
* own where Node has it (20.15, 22.2), else a table. */
|
|
44
|
+
function crc32(data, crc = 0) {
|
|
45
|
+
const native = zlib.crc32;
|
|
46
|
+
if (native !== undefined)
|
|
47
|
+
return native(data, crc);
|
|
48
|
+
let c = (crc ^ 0xffffffff) >>> 0;
|
|
49
|
+
for (const b of data)
|
|
50
|
+
c = CRC_TABLE[(c ^ b) & 0xff] ^ (c >>> 8);
|
|
51
|
+
return (c ^ 0xffffffff) >>> 0;
|
|
52
|
+
}
|
|
53
|
+
/** The first gzip member (RFC 1952): its deflate stream inflated, and a
|
|
54
|
+
* check of its header CRC and trailer to run once the tar is read. */
|
|
55
|
+
async function firstMember(gz) {
|
|
56
|
+
const bad = new ArchiveError("the archive ends early: invalid gzip header");
|
|
57
|
+
if (gz.length < 10 || gz[0] !== 0x1f || gz[1] !== 0x8b || gz[2] !== 8)
|
|
58
|
+
throw bad;
|
|
59
|
+
const flags = gz[3];
|
|
60
|
+
if (flags & 0xe0)
|
|
61
|
+
throw bad;
|
|
62
|
+
let at = 10;
|
|
63
|
+
if (flags & FEXTRA) {
|
|
64
|
+
if (at + 2 > gz.length)
|
|
65
|
+
throw bad;
|
|
66
|
+
at += 2 + (gz[at] | (gz[at + 1] << 8));
|
|
67
|
+
}
|
|
68
|
+
for (const field of [FNAME, FCOMMENT]) {
|
|
69
|
+
if (!(flags & field))
|
|
70
|
+
continue;
|
|
71
|
+
while (at < gz.length && gz[at] !== 0)
|
|
72
|
+
at++;
|
|
73
|
+
at++;
|
|
74
|
+
}
|
|
75
|
+
let headerCrc = null;
|
|
76
|
+
if (flags & FHCRC) {
|
|
77
|
+
if (at + 2 > gz.length)
|
|
78
|
+
throw bad;
|
|
79
|
+
headerCrc = gz[at] | (gz[at + 1] << 8);
|
|
80
|
+
if ((crc32(gz.subarray(0, at)) & 0xffff) !== headerCrc) {
|
|
81
|
+
throw new ArchiveError("the archive is corrupt: its gzip header CRC does not match");
|
|
82
|
+
}
|
|
83
|
+
at += 2;
|
|
84
|
+
}
|
|
85
|
+
if (at > gz.length)
|
|
86
|
+
throw bad;
|
|
87
|
+
const deflate = gz.subarray(at);
|
|
88
|
+
const { tar, crc, consumed } = await new Promise((resolve, reject) => {
|
|
89
|
+
const inflate = zlib.createInflateRaw({ finishFlush: zlib.constants.Z_SYNC_FLUSH });
|
|
90
|
+
const chunks = [];
|
|
91
|
+
let size = 0;
|
|
92
|
+
let crc = 0;
|
|
93
|
+
inflate.on("data", (chunk) => {
|
|
94
|
+
size += chunk.length;
|
|
95
|
+
if (size > MAX_INFLATED) {
|
|
96
|
+
inflate.destroy(new ArchiveError(`the archive unpacks to over ${MAX_INFLATED / 1024 / 1024} MiB`));
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
crc = crc32(chunk, crc);
|
|
100
|
+
chunks.push(chunk);
|
|
101
|
+
});
|
|
102
|
+
inflate.on("end", () => resolve({ tar: Buffer.concat(chunks, size), crc, consumed: inflate.bytesWritten }));
|
|
103
|
+
inflate.on("error", (e) => reject(e instanceof ArchiveError ? e : new ArchiveError(`the archive ends early: ${e.message}`)));
|
|
104
|
+
inflate.end(deflate);
|
|
105
|
+
});
|
|
106
|
+
const checkTrailer = () => {
|
|
107
|
+
const trailer = deflate.subarray(consumed, consumed + 8);
|
|
108
|
+
if (trailer.length < 8)
|
|
109
|
+
throw new ArchiveError("the archive ends early: the gzip stream is cut short");
|
|
110
|
+
const view = Buffer.from(trailer.buffer, trailer.byteOffset, 8);
|
|
111
|
+
if (view.readUInt32LE(0) !== crc)
|
|
112
|
+
throw new ArchiveError("the archive is corrupt: its gzip CRC-32 does not match");
|
|
113
|
+
if (view.readUInt32LE(4) !== tar.length % 2 ** 32) {
|
|
114
|
+
throw new ArchiveError("the archive is corrupt: its gzip length does not match");
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
return { tar, checkTrailer };
|
|
118
|
+
}
|
|
119
|
+
/** A NUL- or space-ended octal field; `null` when it is not one (a base-256
|
|
120
|
+
* size is over any limit here, so it is refused as malformed). */
|
|
121
|
+
function octal(field) {
|
|
122
|
+
let i = 0;
|
|
123
|
+
while (i < field.length && field[i] === 0x20)
|
|
124
|
+
i++;
|
|
125
|
+
let digits = "";
|
|
126
|
+
for (; i < field.length && field[i] !== 0 && field[i] !== 0x20; i++)
|
|
127
|
+
digits += String.fromCharCode(field[i]);
|
|
128
|
+
if (digits === "")
|
|
129
|
+
return 0;
|
|
130
|
+
if (digits.length > 21 || !/^[0-7]+$/.test(digits))
|
|
131
|
+
return null;
|
|
132
|
+
// At most 21 octal digits: 63 bits, exact as a bigint.
|
|
133
|
+
const n = BigInt(`0o${digits}`);
|
|
134
|
+
return n > BigInt(Number.MAX_SAFE_INTEGER) ? Number.MAX_SAFE_INTEGER : Number(n);
|
|
135
|
+
}
|
|
136
|
+
function text(field) {
|
|
137
|
+
const end = field.indexOf(0);
|
|
138
|
+
return Buffer.from(end === -1 ? field : field.subarray(0, end)).toString("utf8");
|
|
139
|
+
}
|
|
140
|
+
/** A member type as the Rust crate's `{:?}` of a `char` prints it. */
|
|
141
|
+
function kindText(k) {
|
|
142
|
+
const escapes = { 0x09: "\\t", 0x0a: "\\n", 0x0d: "\\r", 0x27: "\\'", 0x5c: "\\\\", 0x00: "\\0" };
|
|
143
|
+
if (escapes[k] !== undefined)
|
|
144
|
+
return `'${escapes[k]}'`;
|
|
145
|
+
// Rust prints a char as itself unless it is a control, a format character
|
|
146
|
+
// or a space other than ' ': in Latin-1, 0x7f–0xa0 and 0xad are escaped.
|
|
147
|
+
const printable = (k >= 0x20 && k < 0x7f) || (k > 0xa0 && k !== 0xad);
|
|
148
|
+
return printable ? `'${String.fromCharCode(k)}'` : `'\\u{${k.toString(16)}}'`;
|
|
149
|
+
}
|
|
150
|
+
/** `[path, kind, size]` from a header block, its checksum checked. */
|
|
151
|
+
function header(h) {
|
|
152
|
+
const stored = octal(h.subarray(148, 156));
|
|
153
|
+
let sum = 8 * 32;
|
|
154
|
+
for (let i = 0; i < BLOCK; i++)
|
|
155
|
+
if (i < 148 || i >= 156)
|
|
156
|
+
sum += h[i];
|
|
157
|
+
if (stored === null || sum !== stored)
|
|
158
|
+
throw new ArchiveError("the archive has a bad header checksum");
|
|
159
|
+
const size = octal(h.subarray(124, 136));
|
|
160
|
+
if (size === null)
|
|
161
|
+
throw new ArchiveError("the archive has a bad member size");
|
|
162
|
+
const name = text(h.subarray(0, 100));
|
|
163
|
+
const prefix = h.subarray(257, 262).toString("latin1") === "ustar" ? text(h.subarray(345, 500)) : "";
|
|
164
|
+
return [prefix !== "" ? `${prefix}/${name}` : name, h[156], size];
|
|
165
|
+
}
|
|
166
|
+
const ZERO = Buffer.alloc(BLOCK);
|
|
167
|
+
/** The executable's bytes from a gzipped tar; `ArchiveError` otherwise. */
|
|
168
|
+
export async function executable(gz) {
|
|
169
|
+
const { tar, checkTrailer } = await firstMember(gz);
|
|
170
|
+
let at = 0;
|
|
171
|
+
const take = (n) => {
|
|
172
|
+
if (at + n > tar.length)
|
|
173
|
+
throw new ArchiveError("the archive ends early: failed to fill whole buffer");
|
|
174
|
+
const out = tar.subarray(at, at + n);
|
|
175
|
+
at += n;
|
|
176
|
+
return out;
|
|
177
|
+
};
|
|
178
|
+
let found = null;
|
|
179
|
+
let skipped = 0;
|
|
180
|
+
for (;;) {
|
|
181
|
+
const block = take(BLOCK);
|
|
182
|
+
if (block.equals(ZERO))
|
|
183
|
+
break;
|
|
184
|
+
const [path, kind, size] = header(block);
|
|
185
|
+
if (path.split("/").includes(".."))
|
|
186
|
+
throw new ArchiveError(`the archive holds a path with ..: ${path}`);
|
|
187
|
+
const name = path.startsWith("./") ? path.slice(2) : path;
|
|
188
|
+
const base = name.slice(name.lastIndexOf("/") + 1);
|
|
189
|
+
const regular = kind === 0x30 || kind === 0;
|
|
190
|
+
const padded = Math.ceil(size / BLOCK) * BLOCK;
|
|
191
|
+
if (kind === 0x78 || kind === 0x67 || (regular && base.startsWith("._")) || (kind === 0x35 && size === 0)) {
|
|
192
|
+
skipped += size;
|
|
193
|
+
if (skipped > MAX_SKIPPED)
|
|
194
|
+
throw new ArchiveError("the archive's other members are over 16 MiB");
|
|
195
|
+
take(padded);
|
|
196
|
+
}
|
|
197
|
+
else if (regular && name === "harness") {
|
|
198
|
+
if (found !== null)
|
|
199
|
+
throw new ArchiveError("the archive holds a second harness");
|
|
200
|
+
if (size > MAX_EXECUTABLE)
|
|
201
|
+
throw new ArchiveError(`the archive's harness is ${size} bytes, over 256 MiB`);
|
|
202
|
+
found = take(padded).subarray(0, size);
|
|
203
|
+
}
|
|
204
|
+
else if (regular) {
|
|
205
|
+
throw new ArchiveError(`the archive holds a regular file other than harness: ${path}`);
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
throw new ArchiveError(`the archive holds a member of type ${kindText(kind)} that is not a regular file: ${path}`);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
if (found === null)
|
|
212
|
+
throw new ArchiveError("the archive holds no harness");
|
|
213
|
+
checkTrailer();
|
|
214
|
+
return Buffer.from(found);
|
|
215
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** The environment variable naming a harness binary (R12.1). */
|
|
2
|
+
export declare const BINARY_ENV = "MEGACODE_AGENT_SDK_BINARY";
|
|
3
|
+
/** Where one version's executable for one target lives. */
|
|
4
|
+
export declare class Layout {
|
|
5
|
+
readonly root: string;
|
|
6
|
+
/** `<cache>` and each folder below it, top down, the last holding the file. */
|
|
7
|
+
readonly folders: readonly string[];
|
|
8
|
+
readonly dir: string;
|
|
9
|
+
readonly file: string;
|
|
10
|
+
constructor(root: string, version: string, target: string);
|
|
11
|
+
}
|
|
12
|
+
/** What the cache holds: the file with the pinned digest; no file (or a
|
|
13
|
+
* folder on its path not made yet); a file with another digest. */
|
|
14
|
+
export type Cached = "verified" | "missing" | "mismatch";
|
|
15
|
+
/** R12.4: opens the cached file, checks it on the open file, and hashes it. */
|
|
16
|
+
export declare function check(layout: Layout, executableSha256: string): Promise<Cached>;
|
|
17
|
+
/** R12.3 writing: makes the missing folders (`0700`), writes `executable` to
|
|
18
|
+
* `.harness.<pid>.<random>.tmp` (created exclusively), checks it against the
|
|
19
|
+
* pinned digest, makes it `0755`, and renames it into place, over any file
|
|
20
|
+
* there. The temporary file is removed on any failure. */
|
|
21
|
+
export declare function install(layout: Layout, executable: Uint8Array, executableSha256: string): Promise<void>;
|