@stigmer/runner 3.12.0 → 3.12.1
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/dist/.build-fingerprint +1 -1
- package/dist/activities/execute-deep-agent/attachment-injector.d.ts +23 -8
- package/dist/activities/execute-deep-agent/attachment-injector.js +104 -105
- package/dist/activities/execute-deep-agent/attachment-injector.js.map +1 -1
- package/dist/activities/execute-deep-agent/prompt-builder.js +11 -1
- package/dist/activities/execute-deep-agent/prompt-builder.js.map +1 -1
- package/dist/activities/execute-deep-agent/setup.js +7 -3
- package/dist/activities/execute-deep-agent/setup.js.map +1 -1
- package/dist/middleware/path-normalization.d.ts +15 -2
- package/dist/middleware/path-normalization.js +39 -5
- package/dist/middleware/path-normalization.js.map +1 -1
- package/dist/shared/mcp-enabled-tools.d.ts +6 -2
- package/dist/shared/mcp-enabled-tools.js +6 -2
- package/dist/shared/mcp-enabled-tools.js.map +1 -1
- package/dist/shared/plan-mode-permissions.d.ts +46 -10
- package/dist/shared/plan-mode-permissions.js +56 -12
- package/dist/shared/plan-mode-permissions.js.map +1 -1
- package/dist/shared/zip-extract.d.ts +24 -6
- package/dist/shared/zip-extract.js +31 -90
- package/dist/shared/zip-extract.js.map +1 -1
- package/dist/shared/zip-structure.d.ts +61 -0
- package/dist/shared/zip-structure.js +128 -0
- package/dist/shared/zip-structure.js.map +1 -0
- package/package.json +2 -2
- package/src/__test-utils__/zip-fixtures.ts +206 -0
- package/src/activities/execute-cursor/__tests__/skill-resolver.test.ts +3 -42
- package/src/activities/execute-deep-agent/__tests__/attachment-injector.test.ts +165 -126
- package/src/activities/execute-deep-agent/__tests__/plan-mode-path-normalization.test.ts +246 -37
- package/src/activities/execute-deep-agent/__tests__/prompt-builder.test.ts +7 -2
- package/src/activities/execute-deep-agent/__tests__/subagent-plan-mode-permissions.test.ts +35 -4
- package/src/activities/execute-deep-agent/attachment-injector.ts +146 -142
- package/src/activities/execute-deep-agent/prompt-builder.ts +11 -1
- package/src/activities/execute-deep-agent/setup.ts +7 -3
- package/src/middleware/__tests__/path-normalization.test.ts +29 -3
- package/src/middleware/path-normalization.ts +42 -5
- package/src/shared/__tests__/plan-mode-permissions.test.ts +58 -0
- package/src/shared/__tests__/zip-extract.test.ts +106 -89
- package/src/shared/mcp-enabled-tools.ts +6 -2
- package/src/shared/plan-mode-permissions.ts +59 -12
- package/src/shared/zip-extract.ts +35 -117
- package/src/shared/zip-structure.ts +181 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Policy-free structural ZIP parsing: end-of-central-directory (EOCD)
|
|
3
|
+
* location plus central-directory walk, producing one record per entry.
|
|
4
|
+
*
|
|
5
|
+
* Parsing is *central-directory-based* — never a front-to-back walk of
|
|
6
|
+
* local file headers. This is a correctness decision, not a style choice
|
|
7
|
+
* (issues #450 and #567): local headers of streaming entries
|
|
8
|
+
* (general-purpose flag bit 3) carry zeroed sizes, and the only
|
|
9
|
+
* local-only way to recover them is scanning the payload for the
|
|
10
|
+
* data-descriptor signature — which silently truncates any stored entry
|
|
11
|
+
* whose *content* happens to contain those four bytes, and
|
|
12
|
+
* desynchronizes every entry after it. The central directory always
|
|
13
|
+
* carries the real sizes, and every consumer here holds the complete
|
|
14
|
+
* archive bytes, so nothing a local-header walk could offer is needed.
|
|
15
|
+
*
|
|
16
|
+
* This layer maps bytes to entry records and nothing else. Policy — what
|
|
17
|
+
* a structural failure means, which entries are acceptable, how payloads
|
|
18
|
+
* are decoded — belongs to the consumers, and they differ on purpose:
|
|
19
|
+
*
|
|
20
|
+
* - shared/zip-extract.ts (skill artifacts): structural failure is
|
|
21
|
+
* NON-FATAL — both editions' push gates validated every artifact
|
|
22
|
+
* with a central-directory-based reader before storage, so a defect
|
|
23
|
+
* here can only be a truncated or corrupted download.
|
|
24
|
+
* - execute-deep-agent/attachment-injector.ts (user attachments):
|
|
25
|
+
* structural failure is FAIL-HARD — the input is an untrusted
|
|
26
|
+
* upload and nothing upstream vouched for it.
|
|
27
|
+
*
|
|
28
|
+
* ZIP64 is deliberately unsupported: both consumers cap input far below
|
|
29
|
+
* every ZIP64 threshold (skill push gates: 100MB / 10,000 files;
|
|
30
|
+
* attachment uploads: 10MB).
|
|
31
|
+
*
|
|
32
|
+
* Throws plain `Error`s on structural defects (no valid EOCD, a central
|
|
33
|
+
* directory or local header record that does not parse, a payload slice
|
|
34
|
+
* that runs past the end of the buffer); consumers translate those into
|
|
35
|
+
* their own error models.
|
|
36
|
+
*/
|
|
37
|
+
// ─── Format constants ────────────────────────────────────────────────────
|
|
38
|
+
const LOCAL_FILE_HEADER_SIG = 0x04034b50;
|
|
39
|
+
const CENTRAL_DIRECTORY_SIG = 0x02014b50;
|
|
40
|
+
const EOCD_SIG = 0x06054b50;
|
|
41
|
+
/** Fixed size of the EOCD record, excluding the variable-length comment. */
|
|
42
|
+
export const EOCD_MIN_SIZE = 22;
|
|
43
|
+
/** The archive comment length is a u16, so the EOCD sits within the last 64KB + 22 bytes. */
|
|
44
|
+
const EOCD_MAX_COMMENT = 0xffff;
|
|
45
|
+
const LOCAL_HEADER_SIZE = 30;
|
|
46
|
+
const CD_RECORD_SIZE = 46;
|
|
47
|
+
// ─── Parsing ─────────────────────────────────────────────────────────────
|
|
48
|
+
/**
|
|
49
|
+
* Enumerate the archive's entries from its central directory.
|
|
50
|
+
*
|
|
51
|
+
* Throws on any structural defect: no valid EOCD, a central directory or
|
|
52
|
+
* local header record that doesn't parse, or a payload slice that runs
|
|
53
|
+
* past the end of the buffer.
|
|
54
|
+
*/
|
|
55
|
+
export function parseZipStructure(data) {
|
|
56
|
+
const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
57
|
+
const eocd = findEndOfCentralDirectory(data, view);
|
|
58
|
+
const entries = [];
|
|
59
|
+
let offset = eocd.centralDirectoryOffset;
|
|
60
|
+
for (let i = 0; i < eocd.entryCount; i++) {
|
|
61
|
+
if (offset + CD_RECORD_SIZE > data.length || view.getUint32(offset, true) !== CENTRAL_DIRECTORY_SIG) {
|
|
62
|
+
throw new Error(`invalid central directory record at offset ${offset}`);
|
|
63
|
+
}
|
|
64
|
+
const compressionMethod = view.getUint16(offset + 10, true);
|
|
65
|
+
const compressedSize = view.getUint32(offset + 20, true);
|
|
66
|
+
const uncompressedSize = view.getUint32(offset + 24, true);
|
|
67
|
+
const fileNameLength = view.getUint16(offset + 28, true);
|
|
68
|
+
const extraFieldLength = view.getUint16(offset + 30, true);
|
|
69
|
+
const commentLength = view.getUint16(offset + 32, true);
|
|
70
|
+
const localHeaderOffset = view.getUint32(offset + 42, true);
|
|
71
|
+
const fileName = new TextDecoder().decode(data.subarray(offset + CD_RECORD_SIZE, offset + CD_RECORD_SIZE + fileNameLength));
|
|
72
|
+
// The local header is consulted for one thing only: the payload start.
|
|
73
|
+
// Its name/extra field lengths can legitimately differ from the central
|
|
74
|
+
// directory's (streaming writers pad the extra field), so the offset
|
|
75
|
+
// cannot be derived from CD fields alone.
|
|
76
|
+
const dataStart = payloadStart(data, view, localHeaderOffset, fileName);
|
|
77
|
+
if (dataStart + compressedSize > data.length) {
|
|
78
|
+
throw new Error(`entry "${fileName}" payload runs past end of archive`);
|
|
79
|
+
}
|
|
80
|
+
entries.push({
|
|
81
|
+
name: fileName,
|
|
82
|
+
isDirectory: fileName.endsWith("/"),
|
|
83
|
+
compressionMethod,
|
|
84
|
+
uncompressedSize,
|
|
85
|
+
compressedData: data.subarray(dataStart, dataStart + compressedSize),
|
|
86
|
+
});
|
|
87
|
+
offset += CD_RECORD_SIZE + fileNameLength + extraFieldLength + commentLength;
|
|
88
|
+
}
|
|
89
|
+
return entries;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Locate and validate the end-of-central-directory record.
|
|
93
|
+
*
|
|
94
|
+
* Scans backward from the end of the archive across the maximum comment
|
|
95
|
+
* span. A signature match alone is not trusted (the four bytes can occur
|
|
96
|
+
* inside a trailing comment): the record must also point at an offset
|
|
97
|
+
* that actually holds a central directory record, or be a genuinely
|
|
98
|
+
* empty archive.
|
|
99
|
+
*/
|
|
100
|
+
function findEndOfCentralDirectory(data, view) {
|
|
101
|
+
const scanFloor = Math.max(0, data.length - EOCD_MIN_SIZE - EOCD_MAX_COMMENT);
|
|
102
|
+
for (let pos = data.length - EOCD_MIN_SIZE; pos >= scanFloor; pos--) {
|
|
103
|
+
if (view.getUint32(pos, true) !== EOCD_SIG)
|
|
104
|
+
continue;
|
|
105
|
+
const entryCount = view.getUint16(pos + 10, true);
|
|
106
|
+
const centralDirectorySize = view.getUint32(pos + 12, true);
|
|
107
|
+
const centralDirectoryOffset = view.getUint32(pos + 16, true);
|
|
108
|
+
const directoryEndsAtRecord = centralDirectoryOffset + centralDirectorySize <= pos;
|
|
109
|
+
const directoryLooksReal = entryCount === 0 ||
|
|
110
|
+
(centralDirectoryOffset + 4 <= data.length &&
|
|
111
|
+
view.getUint32(centralDirectoryOffset, true) === CENTRAL_DIRECTORY_SIG);
|
|
112
|
+
if (directoryEndsAtRecord && directoryLooksReal) {
|
|
113
|
+
return { centralDirectoryOffset, entryCount };
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
throw new Error("no end-of-central-directory record found");
|
|
117
|
+
}
|
|
118
|
+
/** Resolve where an entry's payload begins, from its local file header. */
|
|
119
|
+
function payloadStart(data, view, localHeaderOffset, fileName) {
|
|
120
|
+
if (localHeaderOffset + LOCAL_HEADER_SIZE > data.length ||
|
|
121
|
+
view.getUint32(localHeaderOffset, true) !== LOCAL_FILE_HEADER_SIG) {
|
|
122
|
+
throw new Error(`entry "${fileName}" has no local file header at offset ${localHeaderOffset}`);
|
|
123
|
+
}
|
|
124
|
+
const localNameLength = view.getUint16(localHeaderOffset + 26, true);
|
|
125
|
+
const localExtraLength = view.getUint16(localHeaderOffset + 28, true);
|
|
126
|
+
return localHeaderOffset + LOCAL_HEADER_SIZE + localNameLength + localExtraLength;
|
|
127
|
+
}
|
|
128
|
+
//# sourceMappingURL=zip-structure.js.map
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D,CAAC;AAED,2EAA2E;AAC3E,SAAS,YAAY,CACnB,IAAgB,EAChB,IAAc,EACd,iBAAyB,EACzB,QAAgB;IAEhB,IACE,iBAAiB,GAAG,iBAAiB,GAAG,IAAI,CAAC,MAAM;QACnD,IAAI,CAAC,SAAS,CAAC,iBAAiB,EAAE,IAAI,CAAC,KAAK,qBAAqB,EACjE,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,UAAU,QAAQ,wCAAwC,iBAAiB,EAAE,CAAC,CAAC;IACjG,CAAC;IAED,MAAM,eAAe,GAAG,IAAI,CAAC,SAAS,CAAC,iBAAiB,GAAG,EAAE,EAAE,IAAI,CAAC,CAAC;IACrE,MAAM,gBAAgB,GAAG,IAAI,CAAC,SAAS,CAAC,iBAAiB,GAAG,EAAE,EAAE,IAAI,CAAC,CAAC;IACtE,OAAO,iBAAiB,GAAG,iBAAiB,GAAG,eAAe,GAAG,gBAAgB,CAAC;AACpF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stigmer/runner",
|
|
3
|
-
"version": "3.12.
|
|
3
|
+
"version": "3.12.1",
|
|
4
4
|
"description": "Embeddable Temporal worker for the Stigmer AI agent platform — handles agent execution, workflow orchestration, and MCP server management",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -90,7 +90,7 @@
|
|
|
90
90
|
"@opentelemetry/sdk-metrics": "^2.0.0",
|
|
91
91
|
"@opentelemetry/sdk-trace-base": "^2.0.0",
|
|
92
92
|
"@opentelemetry/sdk-trace-node": "^2.0.0",
|
|
93
|
-
"@stigmer/protos": "3.12.
|
|
93
|
+
"@stigmer/protos": "3.12.1",
|
|
94
94
|
"@temporalio/activity": "^1.11.0",
|
|
95
95
|
"@temporalio/common": "^1.11.0",
|
|
96
96
|
"@temporalio/interceptors-opentelemetry": "~1.16.0",
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared ZIP fixture builder for runner unit tests.
|
|
3
|
+
*
|
|
4
|
+
* Emits complete, real-shaped archives — local file headers (optionally
|
|
5
|
+
* streaming-style, the Go stdlib writer's default), payloads, central
|
|
6
|
+
* directory, and EOCD — because that is the only shape that can reach the
|
|
7
|
+
* runner: both editions' skill push gates validate artifacts with
|
|
8
|
+
* central-directory-based readers (see zip-extract.ts's module doc and
|
|
9
|
+
* design record 017). Earlier fixtures emitted local headers only, a
|
|
10
|
+
* shape no real ZIP writer produces, and were coupled to the old parser's
|
|
11
|
+
* front-to-back walk.
|
|
12
|
+
*
|
|
13
|
+
* Mirrors the conformance suite's `zipFilesStreaming()`
|
|
14
|
+
* (test/conformance/src/support/skills.ts) so unit fixtures and
|
|
15
|
+
* cross-edition fixtures share one shape.
|
|
16
|
+
*
|
|
17
|
+
* Lives in src/__test-utils__/ so `tsc --noEmit` covers it while
|
|
18
|
+
* tsconfig.build.json keeps it out of dist/.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { deflateRawSync } from "node:zlib";
|
|
22
|
+
|
|
23
|
+
export interface ZipFixtureFile {
|
|
24
|
+
name: string;
|
|
25
|
+
/** Text content is UTF-8 encoded; pass bytes directly for binary payloads. */
|
|
26
|
+
content: string | Uint8Array;
|
|
27
|
+
/** Compression method for the entry. Defaults to stored. */
|
|
28
|
+
method?: "stored" | "deflated";
|
|
29
|
+
/**
|
|
30
|
+
* Emit the entry the way Go's archive/zip does by default: general-purpose
|
|
31
|
+
* flag bit 3 set, zeroed sizes in the local header, and a trailing data
|
|
32
|
+
* descriptor (with the conventional signature) carrying the real values.
|
|
33
|
+
*/
|
|
34
|
+
streaming?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Lie about the entry's uncompressed size everywhere the writer would
|
|
37
|
+
* declare it (local header, data descriptor, central directory). Models a
|
|
38
|
+
* crafted archive whose declarations disagree with its actual payload —
|
|
39
|
+
* the input class the attachment injector's declared-size enforcement
|
|
40
|
+
* exists to reject (issue #567).
|
|
41
|
+
*/
|
|
42
|
+
declaredUncompressedSize?: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface ZipFixtureOptions {
|
|
46
|
+
/** Trailing archive comment appended after the EOCD record. */
|
|
47
|
+
comment?: string;
|
|
48
|
+
/**
|
|
49
|
+
* Drop the central directory and EOCD, leaving only local headers and
|
|
50
|
+
* payloads. No real ZIP writer produces this shape — it models a download
|
|
51
|
+
* truncated before the archive's index.
|
|
52
|
+
*/
|
|
53
|
+
omitCentralDirectory?: boolean;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Build a ZIP archive from path + content pairs. */
|
|
57
|
+
export function buildZip(files: ZipFixtureFile[], options?: ZipFixtureOptions): Uint8Array {
|
|
58
|
+
const out = new ByteWriter();
|
|
59
|
+
|
|
60
|
+
interface WrittenEntry {
|
|
61
|
+
nameBytes: Uint8Array;
|
|
62
|
+
payload: Uint8Array;
|
|
63
|
+
declaredUncompressed: number;
|
|
64
|
+
crc: number;
|
|
65
|
+
flags: number;
|
|
66
|
+
method: number;
|
|
67
|
+
localHeaderOffset: number;
|
|
68
|
+
}
|
|
69
|
+
const written: WrittenEntry[] = [];
|
|
70
|
+
|
|
71
|
+
for (const file of files) {
|
|
72
|
+
const contentBytes =
|
|
73
|
+
typeof file.content === "string" ? new TextEncoder().encode(file.content) : file.content;
|
|
74
|
+
const deflated = file.method === "deflated";
|
|
75
|
+
const payload = deflated ? new Uint8Array(deflateRawSync(contentBytes)) : contentBytes;
|
|
76
|
+
const entry: WrittenEntry = {
|
|
77
|
+
nameBytes: new TextEncoder().encode(file.name),
|
|
78
|
+
payload,
|
|
79
|
+
declaredUncompressed: file.declaredUncompressedSize ?? contentBytes.length,
|
|
80
|
+
crc: crc32(contentBytes),
|
|
81
|
+
flags: file.streaming ? 0x0008 : 0,
|
|
82
|
+
method: deflated ? 8 : 0,
|
|
83
|
+
localHeaderOffset: out.length,
|
|
84
|
+
};
|
|
85
|
+
written.push(entry);
|
|
86
|
+
|
|
87
|
+
out.u32(0x04034b50); // local file header signature
|
|
88
|
+
out.u16(20); // version needed to extract
|
|
89
|
+
out.u16(entry.flags);
|
|
90
|
+
out.u16(entry.method);
|
|
91
|
+
out.u16(0); // mod time
|
|
92
|
+
out.u16(0); // mod date
|
|
93
|
+
out.u32(file.streaming ? 0 : entry.crc);
|
|
94
|
+
out.u32(file.streaming ? 0 : payload.length);
|
|
95
|
+
out.u32(file.streaming ? 0 : entry.declaredUncompressed);
|
|
96
|
+
out.u16(entry.nameBytes.length);
|
|
97
|
+
out.u16(0); // extra field length
|
|
98
|
+
out.raw(entry.nameBytes);
|
|
99
|
+
out.raw(payload);
|
|
100
|
+
|
|
101
|
+
if (file.streaming) {
|
|
102
|
+
out.u32(0x08074b50); // data descriptor signature (Go writes it)
|
|
103
|
+
out.u32(entry.crc);
|
|
104
|
+
out.u32(payload.length);
|
|
105
|
+
out.u32(entry.declaredUncompressed);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (options?.omitCentralDirectory) {
|
|
110
|
+
return out.toUint8Array();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const centralDirectoryOffset = out.length;
|
|
114
|
+
for (const entry of written) {
|
|
115
|
+
out.u32(0x02014b50); // central directory record signature
|
|
116
|
+
out.u16(20); // version made by
|
|
117
|
+
out.u16(20); // version needed to extract
|
|
118
|
+
out.u16(entry.flags);
|
|
119
|
+
out.u16(entry.method);
|
|
120
|
+
out.u16(0); // mod time
|
|
121
|
+
out.u16(0); // mod date
|
|
122
|
+
out.u32(entry.crc);
|
|
123
|
+
out.u32(entry.payload.length);
|
|
124
|
+
out.u32(entry.declaredUncompressed);
|
|
125
|
+
out.u16(entry.nameBytes.length);
|
|
126
|
+
out.u16(0); // extra field length
|
|
127
|
+
out.u16(0); // comment length
|
|
128
|
+
out.u16(0); // disk number start
|
|
129
|
+
out.u16(0); // internal attributes
|
|
130
|
+
out.u32(0); // external attributes
|
|
131
|
+
out.u32(entry.localHeaderOffset);
|
|
132
|
+
out.raw(entry.nameBytes);
|
|
133
|
+
}
|
|
134
|
+
const centralDirectorySize = out.length - centralDirectoryOffset;
|
|
135
|
+
|
|
136
|
+
const commentBytes = new TextEncoder().encode(options?.comment ?? "");
|
|
137
|
+
out.u32(0x06054b50); // EOCD signature
|
|
138
|
+
out.u16(0); // disk number
|
|
139
|
+
out.u16(0); // disk with central directory
|
|
140
|
+
out.u16(written.length); // entries on this disk
|
|
141
|
+
out.u16(written.length); // total entries
|
|
142
|
+
out.u32(centralDirectorySize);
|
|
143
|
+
out.u32(centralDirectoryOffset);
|
|
144
|
+
out.u16(commentBytes.length);
|
|
145
|
+
out.raw(commentBytes);
|
|
146
|
+
|
|
147
|
+
return out.toUint8Array();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Chunked assembly instead of a number[]-per-byte accumulator: fixtures at
|
|
151
|
+
// the injector's 100 MB zip-bomb limit are built from payload-sized chunks,
|
|
152
|
+
// which a per-byte spread-push cannot survive (argument-count overflow).
|
|
153
|
+
class ByteWriter {
|
|
154
|
+
private readonly chunks: Uint8Array[] = [];
|
|
155
|
+
private size = 0;
|
|
156
|
+
|
|
157
|
+
get length(): number {
|
|
158
|
+
return this.size;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
u16(v: number): void {
|
|
162
|
+
this.raw(new Uint8Array([v & 0xff, (v >>> 8) & 0xff]));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
u32(v: number): void {
|
|
166
|
+
this.raw(new Uint8Array([v & 0xff, (v >>> 8) & 0xff, (v >>> 16) & 0xff, (v >>> 24) & 0xff]));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
raw(b: Uint8Array): void {
|
|
170
|
+
this.chunks.push(b);
|
|
171
|
+
this.size += b.length;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
toUint8Array(): Uint8Array {
|
|
175
|
+
const result = new Uint8Array(this.size);
|
|
176
|
+
let offset = 0;
|
|
177
|
+
for (const chunk of this.chunks) {
|
|
178
|
+
result.set(chunk, offset);
|
|
179
|
+
offset += chunk.length;
|
|
180
|
+
}
|
|
181
|
+
return result;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Standard CRC-32 (IEEE 802.3, the ZIP checksum), table-driven so fixtures
|
|
186
|
+
// at the injector's 100 MB limit stay cheap to build. Semantically identical
|
|
187
|
+
// to the conformance suite's inline bit-loop helper.
|
|
188
|
+
const CRC_TABLE = (() => {
|
|
189
|
+
const table = new Uint32Array(256);
|
|
190
|
+
for (let n = 0; n < 256; n++) {
|
|
191
|
+
let c = n;
|
|
192
|
+
for (let bit = 0; bit < 8; bit++) {
|
|
193
|
+
c = (c >>> 1) ^ (0xedb88320 & -(c & 1));
|
|
194
|
+
}
|
|
195
|
+
table[n] = c;
|
|
196
|
+
}
|
|
197
|
+
return table;
|
|
198
|
+
})();
|
|
199
|
+
|
|
200
|
+
function crc32(data: Uint8Array): number {
|
|
201
|
+
let crc = 0xffffffff;
|
|
202
|
+
for (let i = 0; i < data.length; i++) {
|
|
203
|
+
crc = (crc >>> 8) ^ CRC_TABLE[(crc ^ data[i]!) & 0xff]!;
|
|
204
|
+
}
|
|
205
|
+
return (crc ^ 0xffffffff) >>> 0;
|
|
206
|
+
}
|
|
@@ -3,6 +3,7 @@ import { mkdtempSync, readFileSync, existsSync, rmSync } from "node:fs";
|
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import { tmpdir } from "node:os";
|
|
5
5
|
import { resolveSkills } from "../skill-resolver.js";
|
|
6
|
+
import { buildZip } from "../../../__test-utils__/zip-fixtures.js";
|
|
6
7
|
|
|
7
8
|
// ─── Helpers ─────────────────────────────────────────────────────────────
|
|
8
9
|
|
|
@@ -10,46 +11,6 @@ function makeTempDir(prefix: string): string {
|
|
|
10
11
|
return mkdtempSync(join(tmpdir(), prefix));
|
|
11
12
|
}
|
|
12
13
|
|
|
13
|
-
/**
|
|
14
|
-
* Build a minimal stored (method 0) ZIP archive for testing.
|
|
15
|
-
*/
|
|
16
|
-
function buildStoredZip(files: { name: string; content: string }[]): Uint8Array {
|
|
17
|
-
const parts: Uint8Array[] = [];
|
|
18
|
-
|
|
19
|
-
for (const file of files) {
|
|
20
|
-
const nameBytes = new TextEncoder().encode(file.name);
|
|
21
|
-
const contentBytes = new TextEncoder().encode(file.content);
|
|
22
|
-
const isDir = file.name.endsWith("/");
|
|
23
|
-
|
|
24
|
-
const header = new ArrayBuffer(30);
|
|
25
|
-
const view = new DataView(header);
|
|
26
|
-
view.setUint32(0, 0x04034b50, true);
|
|
27
|
-
view.setUint16(4, 20, true);
|
|
28
|
-
view.setUint16(6, 0, true);
|
|
29
|
-
view.setUint16(8, 0, true);
|
|
30
|
-
view.setUint16(10, 0, true);
|
|
31
|
-
view.setUint16(12, 0, true);
|
|
32
|
-
view.setUint32(14, 0, true);
|
|
33
|
-
view.setUint32(18, isDir ? 0 : contentBytes.length, true);
|
|
34
|
-
view.setUint32(22, isDir ? 0 : contentBytes.length, true);
|
|
35
|
-
view.setUint16(26, nameBytes.length, true);
|
|
36
|
-
view.setUint16(28, 0, true);
|
|
37
|
-
|
|
38
|
-
parts.push(new Uint8Array(header));
|
|
39
|
-
parts.push(nameBytes);
|
|
40
|
-
if (!isDir) parts.push(contentBytes);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const totalLength = parts.reduce((sum, p) => sum + p.length, 0);
|
|
44
|
-
const result = new Uint8Array(totalLength);
|
|
45
|
-
let offset = 0;
|
|
46
|
-
for (const part of parts) {
|
|
47
|
-
result.set(part, offset);
|
|
48
|
-
offset += part.length;
|
|
49
|
-
}
|
|
50
|
-
return result;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
14
|
function makeSkillProto(overrides: {
|
|
54
15
|
name?: string;
|
|
55
16
|
slug?: string;
|
|
@@ -141,7 +102,7 @@ describe("resolveSkills — artifact extraction", () => {
|
|
|
141
102
|
const skillMd = "# Garden Design Makeover\n\nSee [references/database-schema.md](references/database-schema.md)";
|
|
142
103
|
const schemaContent = "# Database Schema\n\nTable definitions here.";
|
|
143
104
|
|
|
144
|
-
const artifact =
|
|
105
|
+
const artifact = buildZip([
|
|
145
106
|
{ name: "SKILL.md", content: skillMd },
|
|
146
107
|
{ name: "references/", content: "" },
|
|
147
108
|
{ name: "references/database-schema.md", content: schemaContent },
|
|
@@ -232,7 +193,7 @@ describe("resolveSkills — artifact extraction", () => {
|
|
|
232
193
|
const specContent = "# Authoritative SKILL.md from spec";
|
|
233
194
|
const zipContent = "# Stale SKILL.md from ZIP";
|
|
234
195
|
|
|
235
|
-
const artifact =
|
|
196
|
+
const artifact = buildZip([
|
|
236
197
|
{ name: "SKILL.md", content: zipContent },
|
|
237
198
|
{ name: "references/data.md", content: "data" },
|
|
238
199
|
]);
|