@eminent337/aery-core 0.67.120 → 0.67.122

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.
Files changed (127) hide show
  1. package/README.md +14 -0
  2. package/dist/agent-loop.d.ts.map +1 -1
  3. package/dist/agent-loop.js +31 -1
  4. package/dist/agent-loop.js.map +1 -1
  5. package/dist/agent.d.ts +4 -3
  6. package/dist/agent.d.ts.map +1 -1
  7. package/dist/agent.js +7 -3
  8. package/dist/agent.js.map +1 -1
  9. package/dist/harness/agent-harness.d.ts +103 -0
  10. package/dist/harness/agent-harness.d.ts.map +1 -0
  11. package/dist/harness/agent-harness.js +788 -0
  12. package/dist/harness/agent-harness.js.map +1 -0
  13. package/dist/harness/compaction/branch-summarization.d.ts +88 -0
  14. package/dist/harness/compaction/branch-summarization.d.ts.map +1 -0
  15. package/dist/harness/compaction/branch-summarization.js +243 -0
  16. package/dist/harness/compaction/branch-summarization.js.map +1 -0
  17. package/dist/harness/compaction/compaction.d.ts +122 -0
  18. package/dist/harness/compaction/compaction.d.ts.map +1 -0
  19. package/dist/harness/compaction/compaction.js +631 -0
  20. package/dist/harness/compaction/compaction.js.map +1 -0
  21. package/dist/harness/compaction/utils.d.ts +38 -0
  22. package/dist/harness/compaction/utils.d.ts.map +1 -0
  23. package/dist/harness/compaction/utils.js +153 -0
  24. package/dist/harness/compaction/utils.js.map +1 -0
  25. package/dist/harness/env/nodejs.d.ts +50 -0
  26. package/dist/harness/env/nodejs.d.ts.map +1 -0
  27. package/dist/harness/env/nodejs.js +487 -0
  28. package/dist/harness/env/nodejs.js.map +1 -0
  29. package/dist/harness/execution-env.d.ts +4 -0
  30. package/dist/harness/execution-env.d.ts.map +1 -0
  31. package/dist/harness/execution-env.js +3 -0
  32. package/dist/harness/execution-env.js.map +1 -0
  33. package/dist/harness/factory.d.ts +6 -0
  34. package/dist/harness/factory.d.ts.map +1 -0
  35. package/dist/harness/factory.js +9 -0
  36. package/dist/harness/factory.js.map +1 -0
  37. package/dist/harness/messages.d.ts +51 -0
  38. package/dist/harness/messages.d.ts.map +1 -0
  39. package/dist/harness/messages.js +102 -0
  40. package/dist/harness/messages.js.map +1 -0
  41. package/dist/harness/prompt-templates.d.ts +47 -0
  42. package/dist/harness/prompt-templates.d.ts.map +1 -0
  43. package/dist/harness/prompt-templates.js +201 -0
  44. package/dist/harness/prompt-templates.js.map +1 -0
  45. package/dist/harness/session/jsonl-repo.d.ts +26 -0
  46. package/dist/harness/session/jsonl-repo.d.ts.map +1 -0
  47. package/dist/harness/session/jsonl-repo.js +97 -0
  48. package/dist/harness/session/jsonl-repo.js.map +1 -0
  49. package/dist/harness/session/jsonl-storage.d.ts +33 -0
  50. package/dist/harness/session/jsonl-storage.d.ts.map +1 -0
  51. package/dist/harness/session/jsonl-storage.js +159 -0
  52. package/dist/harness/session/jsonl-storage.js.map +1 -0
  53. package/dist/harness/session/memory-repo.d.ts +18 -0
  54. package/dist/harness/session/memory-repo.d.ts.map +1 -0
  55. package/dist/harness/session/memory-repo.js +42 -0
  56. package/dist/harness/session/memory-repo.js.map +1 -0
  57. package/dist/harness/session/memory-storage.d.ts +26 -0
  58. package/dist/harness/session/memory-storage.d.ts.map +1 -0
  59. package/dist/harness/session/memory-storage.js +89 -0
  60. package/dist/harness/session/memory-storage.js.map +1 -0
  61. package/dist/harness/session/repo/jsonl.d.ts +20 -0
  62. package/dist/harness/session/repo/jsonl.d.ts.map +1 -0
  63. package/dist/harness/session/repo/jsonl.js +92 -0
  64. package/dist/harness/session/repo/jsonl.js.map +1 -0
  65. package/dist/harness/session/repo/memory.d.ts +18 -0
  66. package/dist/harness/session/repo/memory.d.ts.map +1 -0
  67. package/dist/harness/session/repo/memory.js +42 -0
  68. package/dist/harness/session/repo/memory.js.map +1 -0
  69. package/dist/harness/session/repo/shared.d.ts +10 -0
  70. package/dist/harness/session/repo/shared.d.ts.map +1 -0
  71. package/dist/harness/session/repo/shared.js +31 -0
  72. package/dist/harness/session/repo/shared.js.map +1 -0
  73. package/dist/harness/session/repo-utils.d.ts +10 -0
  74. package/dist/harness/session/repo-utils.d.ts.map +1 -0
  75. package/dist/harness/session/repo-utils.js +31 -0
  76. package/dist/harness/session/repo-utils.js.map +1 -0
  77. package/dist/harness/session/session.d.ts +32 -0
  78. package/dist/harness/session/session.d.ts.map +1 -0
  79. package/dist/harness/session/session.js +196 -0
  80. package/dist/harness/session/session.js.map +1 -0
  81. package/dist/harness/session/storage/jsonl.d.ts +30 -0
  82. package/dist/harness/session/storage/jsonl.d.ts.map +1 -0
  83. package/dist/harness/session/storage/jsonl.js +170 -0
  84. package/dist/harness/session/storage/jsonl.js.map +1 -0
  85. package/dist/harness/session/storage/memory.d.ts +26 -0
  86. package/dist/harness/session/storage/memory.d.ts.map +1 -0
  87. package/dist/harness/session/storage/memory.js +90 -0
  88. package/dist/harness/session/storage/memory.js.map +1 -0
  89. package/dist/harness/session/uuid.d.ts +2 -0
  90. package/dist/harness/session/uuid.d.ts.map +1 -0
  91. package/dist/harness/session/uuid.js +50 -0
  92. package/dist/harness/session/uuid.js.map +1 -0
  93. package/dist/harness/skills.d.ts +43 -0
  94. package/dist/harness/skills.d.ts.map +1 -0
  95. package/dist/harness/skills.js +255 -0
  96. package/dist/harness/skills.js.map +1 -0
  97. package/dist/harness/system-prompt.d.ts +3 -0
  98. package/dist/harness/system-prompt.d.ts.map +1 -0
  99. package/dist/harness/system-prompt.js +30 -0
  100. package/dist/harness/system-prompt.js.map +1 -0
  101. package/dist/harness/types.d.ts +578 -0
  102. package/dist/harness/types.d.ts.map +1 -0
  103. package/dist/harness/types.js +56 -0
  104. package/dist/harness/types.js.map +1 -0
  105. package/dist/harness/utils/shell-output.d.ts +14 -0
  106. package/dist/harness/utils/shell-output.d.ts.map +1 -0
  107. package/dist/harness/utils/shell-output.js +125 -0
  108. package/dist/harness/utils/shell-output.js.map +1 -0
  109. package/dist/harness/utils/truncate.d.ts +70 -0
  110. package/dist/harness/utils/truncate.d.ts.map +1 -0
  111. package/dist/harness/utils/truncate.js +288 -0
  112. package/dist/harness/utils/truncate.js.map +1 -0
  113. package/dist/index.d.ts +15 -0
  114. package/dist/index.d.ts.map +1 -1
  115. package/dist/index.js +16 -0
  116. package/dist/index.js.map +1 -1
  117. package/dist/node.d.ts +3 -0
  118. package/dist/node.d.ts.map +1 -0
  119. package/dist/node.js +3 -0
  120. package/dist/node.js.map +1 -0
  121. package/dist/proxy.d.ts.map +1 -1
  122. package/dist/proxy.js +5 -2
  123. package/dist/proxy.js.map +1 -1
  124. package/dist/types.d.ts +50 -4
  125. package/dist/types.d.ts.map +1 -1
  126. package/dist/types.js.map +1 -1
  127. package/package.json +19 -2
@@ -0,0 +1,170 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { createReadStream } from "node:fs";
3
+ import { appendFile, mkdir, readFile, writeFile } from "node:fs/promises";
4
+ import { dirname, resolve } from "node:path";
5
+ import { createInterface } from "node:readline";
6
+ function updateLabelCache(labelsById, entry) {
7
+ if (entry.type !== "label")
8
+ return;
9
+ const label = entry.label?.trim();
10
+ if (label) {
11
+ labelsById.set(entry.targetId, label);
12
+ }
13
+ else {
14
+ labelsById.delete(entry.targetId);
15
+ }
16
+ }
17
+ function buildLabelsById(entries) {
18
+ const labelsById = new Map();
19
+ for (const entry of entries) {
20
+ updateLabelCache(labelsById, entry);
21
+ }
22
+ return labelsById;
23
+ }
24
+ function generateEntryId(byId) {
25
+ for (let i = 0; i < 100; i++) {
26
+ const id = randomUUID().slice(0, 8);
27
+ if (!byId.has(id))
28
+ return id;
29
+ }
30
+ return randomUUID();
31
+ }
32
+ function headerToSessionMetadata(header, path) {
33
+ return {
34
+ id: header.id,
35
+ createdAt: header.timestamp,
36
+ cwd: header.cwd,
37
+ path,
38
+ parentSessionPath: header.parentSession,
39
+ };
40
+ }
41
+ export async function loadJsonlSessionMetadata(filePath) {
42
+ const stream = createReadStream(filePath, { encoding: "utf8" });
43
+ const lines = createInterface({ input: stream, crlfDelay: Infinity });
44
+ try {
45
+ for await (const line of lines) {
46
+ if (!line.trim())
47
+ break;
48
+ try {
49
+ const header = JSON.parse(line);
50
+ return headerToSessionMetadata(header, resolve(filePath));
51
+ }
52
+ catch {
53
+ throw new Error(`Invalid JSONL session file ${filePath}: first line is not a valid session header`);
54
+ }
55
+ }
56
+ throw new Error(`Invalid JSONL session file ${filePath}: missing session header`);
57
+ }
58
+ finally {
59
+ lines.close();
60
+ stream.destroy();
61
+ }
62
+ }
63
+ async function loadJsonlStorage(filePath) {
64
+ const content = await readFile(filePath, "utf8");
65
+ const lines = content.split("\n").filter((line) => line.trim());
66
+ if (lines.length === 0) {
67
+ throw new Error(`Invalid JSONL session file ${filePath}: missing session header`);
68
+ }
69
+ let header;
70
+ try {
71
+ header = JSON.parse(lines[0]);
72
+ }
73
+ catch {
74
+ throw new Error(`Invalid JSONL session file ${filePath}: first line is not a valid session header`);
75
+ }
76
+ const entries = [];
77
+ let leafId = null;
78
+ for (const line of lines.slice(1)) {
79
+ try {
80
+ const entry = JSON.parse(line);
81
+ entries.push(entry);
82
+ leafId = entry.id;
83
+ }
84
+ catch {
85
+ // ignore malformed entry lines
86
+ }
87
+ }
88
+ return { header, entries, leafId };
89
+ }
90
+ export class JsonlSessionStorage {
91
+ filePath;
92
+ metadata;
93
+ entries;
94
+ byId;
95
+ labelsById;
96
+ currentLeafId;
97
+ constructor(filePath, header, entries, leafId) {
98
+ this.filePath = resolve(filePath);
99
+ this.metadata = headerToSessionMetadata(header, this.filePath);
100
+ this.entries = entries;
101
+ this.byId = new Map(entries.map((entry) => [entry.id, entry]));
102
+ this.labelsById = buildLabelsById(entries);
103
+ this.currentLeafId = leafId;
104
+ }
105
+ static async open(filePath) {
106
+ const resolvedPath = resolve(filePath);
107
+ const loaded = await loadJsonlStorage(resolvedPath);
108
+ return new JsonlSessionStorage(resolvedPath, loaded.header, loaded.entries, loaded.leafId);
109
+ }
110
+ static async create(filePath, options) {
111
+ const resolvedPath = resolve(filePath);
112
+ const header = {
113
+ type: "session",
114
+ version: 3,
115
+ id: options.sessionId,
116
+ timestamp: new Date().toISOString(),
117
+ cwd: options.cwd,
118
+ parentSession: options.parentSessionPath,
119
+ };
120
+ await mkdir(dirname(resolvedPath), { recursive: true });
121
+ await writeFile(resolvedPath, `${JSON.stringify(header)}\n`);
122
+ return new JsonlSessionStorage(resolvedPath, header, [], null);
123
+ }
124
+ async getMetadata() {
125
+ return this.metadata;
126
+ }
127
+ async getLeafId() {
128
+ return this.currentLeafId;
129
+ }
130
+ async setLeafId(leafId) {
131
+ if (leafId !== null && !this.byId.has(leafId)) {
132
+ throw new Error(`Entry ${leafId} not found`);
133
+ }
134
+ this.currentLeafId = leafId;
135
+ }
136
+ async createEntryId() {
137
+ return generateEntryId(this.byId);
138
+ }
139
+ async appendEntry(entry) {
140
+ await appendFile(this.filePath, `${JSON.stringify(entry)}\n`);
141
+ this.entries.push(entry);
142
+ this.byId.set(entry.id, entry);
143
+ updateLabelCache(this.labelsById, entry);
144
+ this.currentLeafId = entry.id;
145
+ }
146
+ async getEntry(id) {
147
+ return this.byId.get(id);
148
+ }
149
+ async findEntries(type) {
150
+ return this.entries.filter((entry) => entry.type === type);
151
+ }
152
+ async getLabel(id) {
153
+ return this.labelsById.get(id);
154
+ }
155
+ async getPathToRoot(leafId) {
156
+ if (leafId === null)
157
+ return [];
158
+ const path = [];
159
+ let current = this.byId.get(leafId);
160
+ while (current) {
161
+ path.unshift(current);
162
+ current = current.parentId ? this.byId.get(current.parentId) : undefined;
163
+ }
164
+ return path;
165
+ }
166
+ async getEntries() {
167
+ return [...this.entries];
168
+ }
169
+ }
170
+ //# sourceMappingURL=jsonl.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsonl.js","sourceRoot":"","sources":["../../../../src/harness/session/storage/jsonl.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAC3C,OA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C,MAAM,CAAC,CAAC,KAAK,EAAuD,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC;IAAA,CAChH;IAED,KAAK,CAAC,QAAQ,CAAC,EAAU,EAA+B;QACvD,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAAA,CAC/B;IAED,KAAK,CAAC,aAAa,CAAC,MAAqB,EAA+B;QACvE,IAAI,MAAM,KAAK,IAAI;YAAE,OAAO,EAAE,CAAC;QAC/B,MAAM,IAAI,GAAuB,EAAE,CAAC;QACpC,IAAI,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACpC,OAAO,OAAO,EAAE,CAAC;YAChB,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;YACtB,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QAC1E,CAAC;QACD,OAAO,IAAI,CAAC;IAAA,CACZ;IAED,KAAK,CAAC,UAAU,GAAgC;QAC/C,OAAO,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC;IAAA,CACzB;CACD","sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport { createReadStream } from \"node:fs\";\nimport { appendFile, mkdir, readFile, writeFile } from \"node:fs/promises\";\nimport { dirname, resolve } from \"node:path\";\nimport { createInterface } from \"node:readline\";\nimport type { JsonlSessionMetadata, SessionStorage, SessionTreeEntry } from \"../../types.js\";\n\ninterface SessionHeader {\n\ttype: \"session\";\n\tversion: 3;\n\tid: string;\n\ttimestamp: string;\n\tcwd: string;\n\tparentSession?: string;\n}\n\nfunction updateLabelCache(labelsById: Map<string, string>, entry: SessionTreeEntry): void {\n\tif (entry.type !== \"label\") return;\n\tconst label = entry.label?.trim();\n\tif (label) {\n\t\tlabelsById.set(entry.targetId, label);\n\t} else {\n\t\tlabelsById.delete(entry.targetId);\n\t}\n}\n\nfunction buildLabelsById(entries: SessionTreeEntry[]): Map<string, string> {\n\tconst labelsById = new Map<string, string>();\n\tfor (const entry of entries) {\n\t\tupdateLabelCache(labelsById, entry);\n\t}\n\treturn labelsById;\n}\n\nfunction generateEntryId(byId: { has(id: string): boolean }): string {\n\tfor (let i = 0; i < 100; i++) {\n\t\tconst id = randomUUID().slice(0, 8);\n\t\tif (!byId.has(id)) return id;\n\t}\n\treturn randomUUID();\n}\n\nfunction headerToSessionMetadata(header: SessionHeader, path: string): JsonlSessionMetadata {\n\treturn {\n\t\tid: header.id,\n\t\tcreatedAt: header.timestamp,\n\t\tcwd: header.cwd,\n\t\tpath,\n\t\tparentSessionPath: header.parentSession,\n\t};\n}\n\nexport async function loadJsonlSessionMetadata(filePath: string): Promise<JsonlSessionMetadata> {\n\tconst stream = createReadStream(filePath, { encoding: \"utf8\" });\n\tconst lines = createInterface({ input: stream, crlfDelay: Infinity });\n\ttry {\n\t\tfor await (const line of lines) {\n\t\t\tif (!line.trim()) break;\n\t\t\ttry {\n\t\t\t\tconst header = JSON.parse(line) as SessionHeader;\n\t\t\t\treturn headerToSessionMetadata(header, resolve(filePath));\n\t\t\t} catch {\n\t\t\t\tthrow new Error(`Invalid JSONL session file ${filePath}: first line is not a valid session header`);\n\t\t\t}\n\t\t}\n\t\tthrow new Error(`Invalid JSONL session file ${filePath}: missing session header`);\n\t} finally {\n\t\tlines.close();\n\t\tstream.destroy();\n\t}\n}\n\nasync function loadJsonlStorage(filePath: string): Promise<{\n\theader: SessionHeader;\n\tentries: SessionTreeEntry[];\n\tleafId: string | null;\n}> {\n\tconst content = await readFile(filePath, \"utf8\");\n\tconst lines = content.split(\"\\n\").filter((line) => line.trim());\n\tif (lines.length === 0) {\n\t\tthrow new Error(`Invalid JSONL session file ${filePath}: missing session header`);\n\t}\n\n\tlet header: SessionHeader;\n\ttry {\n\t\theader = JSON.parse(lines[0]!) as SessionHeader;\n\t} catch {\n\t\tthrow new Error(`Invalid JSONL session file ${filePath}: first line is not a valid session header`);\n\t}\n\n\tconst entries: SessionTreeEntry[] = [];\n\tlet leafId: string | null = null;\n\tfor (const line of lines.slice(1)) {\n\t\ttry {\n\t\t\tconst entry = JSON.parse(line) as SessionTreeEntry;\n\t\t\tentries.push(entry);\n\t\t\tleafId = entry.id;\n\t\t} catch {\n\t\t\t// ignore malformed entry lines\n\t\t}\n\t}\n\treturn { header, entries, leafId };\n}\n\nexport class JsonlSessionStorage implements SessionStorage<JsonlSessionMetadata> {\n\tprivate readonly filePath: string;\n\tprivate readonly metadata: JsonlSessionMetadata;\n\tprivate entries: SessionTreeEntry[];\n\tprivate byId: Map<string, SessionTreeEntry>;\n\tprivate labelsById: Map<string, string>;\n\tprivate currentLeafId: string | null;\n\n\tprivate constructor(filePath: string, header: SessionHeader, entries: SessionTreeEntry[], leafId: string | null) {\n\t\tthis.filePath = resolve(filePath);\n\t\tthis.metadata = headerToSessionMetadata(header, this.filePath);\n\t\tthis.entries = entries;\n\t\tthis.byId = new Map(entries.map((entry) => [entry.id, entry]));\n\t\tthis.labelsById = buildLabelsById(entries);\n\t\tthis.currentLeafId = leafId;\n\t}\n\n\tstatic async open(filePath: string): Promise<JsonlSessionStorage> {\n\t\tconst resolvedPath = resolve(filePath);\n\t\tconst loaded = await loadJsonlStorage(resolvedPath);\n\t\treturn new JsonlSessionStorage(resolvedPath, loaded.header, loaded.entries, loaded.leafId);\n\t}\n\n\tstatic async create(\n\t\tfilePath: string,\n\t\toptions: {\n\t\t\tcwd: string;\n\t\t\tsessionId: string;\n\t\t\tparentSessionPath?: string;\n\t\t},\n\t): Promise<JsonlSessionStorage> {\n\t\tconst resolvedPath = resolve(filePath);\n\t\tconst header: SessionHeader = {\n\t\t\ttype: \"session\",\n\t\t\tversion: 3,\n\t\t\tid: options.sessionId,\n\t\t\ttimestamp: new Date().toISOString(),\n\t\t\tcwd: options.cwd,\n\t\t\tparentSession: options.parentSessionPath,\n\t\t};\n\t\tawait mkdir(dirname(resolvedPath), { recursive: true });\n\t\tawait writeFile(resolvedPath, `${JSON.stringify(header)}\\n`);\n\t\treturn new JsonlSessionStorage(resolvedPath, header, [], null);\n\t}\n\n\tasync getMetadata(): Promise<JsonlSessionMetadata> {\n\t\treturn this.metadata;\n\t}\n\n\tasync getLeafId(): Promise<string | null> {\n\t\treturn this.currentLeafId;\n\t}\n\n\tasync setLeafId(leafId: string | null): Promise<void> {\n\t\tif (leafId !== null && !this.byId.has(leafId)) {\n\t\t\tthrow new Error(`Entry ${leafId} not found`);\n\t\t}\n\t\tthis.currentLeafId = leafId;\n\t}\n\n\tasync createEntryId(): Promise<string> {\n\t\treturn generateEntryId(this.byId);\n\t}\n\n\tasync appendEntry(entry: SessionTreeEntry): Promise<void> {\n\t\tawait appendFile(this.filePath, `${JSON.stringify(entry)}\\n`);\n\t\tthis.entries.push(entry);\n\t\tthis.byId.set(entry.id, entry);\n\t\tupdateLabelCache(this.labelsById, entry);\n\t\tthis.currentLeafId = entry.id;\n\t}\n\n\tasync getEntry(id: string): Promise<SessionTreeEntry | undefined> {\n\t\treturn this.byId.get(id);\n\t}\n\n\tasync findEntries<TType extends SessionTreeEntry[\"type\"]>(\n\t\ttype: TType,\n\t): Promise<Array<Extract<SessionTreeEntry, { type: TType }>>> {\n\t\treturn this.entries.filter((entry): entry is Extract<SessionTreeEntry, { type: TType }> => entry.type === type);\n\t}\n\n\tasync getLabel(id: string): Promise<string | undefined> {\n\t\treturn this.labelsById.get(id);\n\t}\n\n\tasync getPathToRoot(leafId: string | null): Promise<SessionTreeEntry[]> {\n\t\tif (leafId === null) return [];\n\t\tconst path: SessionTreeEntry[] = [];\n\t\tlet current = this.byId.get(leafId);\n\t\twhile (current) {\n\t\t\tpath.unshift(current);\n\t\t\tcurrent = current.parentId ? this.byId.get(current.parentId) : undefined;\n\t\t}\n\t\treturn path;\n\t}\n\n\tasync getEntries(): Promise<SessionTreeEntry[]> {\n\t\treturn [...this.entries];\n\t}\n}\n"]}
@@ -0,0 +1,26 @@
1
+ import type { SessionMetadata, SessionStorage, SessionTreeEntry } from "../../types.js";
2
+ export declare class InMemorySessionStorage implements SessionStorage {
3
+ private readonly metadata;
4
+ private entries;
5
+ private byId;
6
+ private labelsById;
7
+ private leafId;
8
+ constructor(options?: {
9
+ entries?: SessionTreeEntry[];
10
+ leafId?: string | null;
11
+ metadata?: SessionMetadata;
12
+ });
13
+ getMetadata(): Promise<SessionMetadata>;
14
+ getLeafId(): Promise<string | null>;
15
+ setLeafId(leafId: string | null): Promise<void>;
16
+ createEntryId(): Promise<string>;
17
+ appendEntry(entry: SessionTreeEntry): Promise<void>;
18
+ getEntry(id: string): Promise<SessionTreeEntry | undefined>;
19
+ findEntries<TType extends SessionTreeEntry["type"]>(type: TType): Promise<Array<Extract<SessionTreeEntry, {
20
+ type: TType;
21
+ }>>>;
22
+ getLabel(id: string): Promise<string | undefined>;
23
+ getPathToRoot(leafId: string | null): Promise<SessionTreeEntry[]>;
24
+ getEntries(): Promise<SessionTreeEntry[]>;
25
+ }
26
+ //# sourceMappingURL=memory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../../../../src/harness/session/storage/memory.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AA6BxF,qBAAa,sBAAuB,YAAW,cAAc;IAC5D,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAkB;IAC3C,OAAO,CAAC,OAAO,CAAqB;IACpC,OAAO,CAAC,IAAI,CAAgC;IAC5C,OAAO,CAAC,UAAU,CAAsB;IACxC,OAAO,CAAC,MAAM,CAAgB;IAE9B,YAAY,OAAO,CAAC,EAAE;QAAE,OAAO,CAAC,EAAE,gBAAgB,EAAE,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QAAC,QAAQ,CAAC,EAAE,eAAe,CAAA;KAAE,EASzG;IAEK,WAAW,IAAI,OAAO,CAAC,eAAe,CAAC,CAE5C;IAEK,SAAS,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAExC;IAEK,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAKpD;IAEK,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC,CAErC;IAEK,WAAW,CAAC,KAAK,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAKxD;IAEK,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,GAAG,SAAS,CAAC,CAEhE;IAEK,WAAW,CAAC,KAAK,SAAS,gBAAgB,CAAC,MAAM,CAAC,EACvD,IAAI,EAAE,KAAK,GACT,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,gBAAgB,EAAE;QAAE,IAAI,EAAE,KAAK,CAAA;KAAE,CAAC,CAAC,CAAC,CAE5D;IAEK,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAEtD;IAEK,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,gBAAgB,EAAE,CAAC,CAStE;IAEK,UAAU,IAAI,OAAO,CAAC,gBAAgB,EAAE,CAAC,CAE9C;CACD","sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport type { SessionMetadata, SessionStorage, SessionTreeEntry } from \"../../types.js\";\nimport { uuidv7 } from \"../uuid.js\";\n\nfunction updateLabelCache(labelsById: Map<string, string>, entry: SessionTreeEntry): void {\n\tif (entry.type !== \"label\") return;\n\tconst label = entry.label?.trim();\n\tif (label) {\n\t\tlabelsById.set(entry.targetId, label);\n\t} else {\n\t\tlabelsById.delete(entry.targetId);\n\t}\n}\n\nfunction buildLabelsById(entries: SessionTreeEntry[]): Map<string, string> {\n\tconst labelsById = new Map<string, string>();\n\tfor (const entry of entries) {\n\t\tupdateLabelCache(labelsById, entry);\n\t}\n\treturn labelsById;\n}\n\nfunction generateEntryId(byId: { has(id: string): boolean }): string {\n\tfor (let i = 0; i < 100; i++) {\n\t\tconst id = randomUUID().slice(0, 8);\n\t\tif (!byId.has(id)) return id;\n\t}\n\treturn randomUUID();\n}\n\nexport class InMemorySessionStorage implements SessionStorage {\n\tprivate readonly metadata: SessionMetadata;\n\tprivate entries: SessionTreeEntry[];\n\tprivate byId: Map<string, SessionTreeEntry>;\n\tprivate labelsById: Map<string, string>;\n\tprivate leafId: string | null;\n\n\tconstructor(options?: { entries?: SessionTreeEntry[]; leafId?: string | null; metadata?: SessionMetadata }) {\n\t\tthis.entries = options?.entries ? [...options.entries] : [];\n\t\tthis.byId = new Map(this.entries.map((entry) => [entry.id, entry]));\n\t\tthis.labelsById = buildLabelsById(this.entries);\n\t\tthis.leafId = options?.leafId ?? this.entries[this.entries.length - 1]?.id ?? null;\n\t\tif (this.leafId !== null && !this.byId.has(this.leafId)) {\n\t\t\tthrow new Error(`Entry ${this.leafId} not found`);\n\t\t}\n\t\tthis.metadata = options?.metadata ?? { id: uuidv7(), createdAt: new Date().toISOString() };\n\t}\n\n\tasync getMetadata(): Promise<SessionMetadata> {\n\t\treturn this.metadata;\n\t}\n\n\tasync getLeafId(): Promise<string | null> {\n\t\treturn this.leafId;\n\t}\n\n\tasync setLeafId(leafId: string | null): Promise<void> {\n\t\tif (leafId !== null && !this.byId.has(leafId)) {\n\t\t\tthrow new Error(`Entry ${leafId} not found`);\n\t\t}\n\t\tthis.leafId = leafId;\n\t}\n\n\tasync createEntryId(): Promise<string> {\n\t\treturn generateEntryId(this.byId);\n\t}\n\n\tasync appendEntry(entry: SessionTreeEntry): Promise<void> {\n\t\tthis.entries.push(entry);\n\t\tthis.byId.set(entry.id, entry);\n\t\tupdateLabelCache(this.labelsById, entry);\n\t\tthis.leafId = entry.id;\n\t}\n\n\tasync getEntry(id: string): Promise<SessionTreeEntry | undefined> {\n\t\treturn this.byId.get(id);\n\t}\n\n\tasync findEntries<TType extends SessionTreeEntry[\"type\"]>(\n\t\ttype: TType,\n\t): Promise<Array<Extract<SessionTreeEntry, { type: TType }>>> {\n\t\treturn this.entries.filter((entry): entry is Extract<SessionTreeEntry, { type: TType }> => entry.type === type);\n\t}\n\n\tasync getLabel(id: string): Promise<string | undefined> {\n\t\treturn this.labelsById.get(id);\n\t}\n\n\tasync getPathToRoot(leafId: string | null): Promise<SessionTreeEntry[]> {\n\t\tif (leafId === null) return [];\n\t\tconst path: SessionTreeEntry[] = [];\n\t\tlet current = this.byId.get(leafId);\n\t\twhile (current) {\n\t\t\tpath.unshift(current);\n\t\t\tcurrent = current.parentId ? this.byId.get(current.parentId) : undefined;\n\t\t}\n\t\treturn path;\n\t}\n\n\tasync getEntries(): Promise<SessionTreeEntry[]> {\n\t\treturn [...this.entries];\n\t}\n}\n"]}
@@ -0,0 +1,90 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { uuidv7 } from "../uuid.js";
3
+ function updateLabelCache(labelsById, entry) {
4
+ if (entry.type !== "label")
5
+ return;
6
+ const label = entry.label?.trim();
7
+ if (label) {
8
+ labelsById.set(entry.targetId, label);
9
+ }
10
+ else {
11
+ labelsById.delete(entry.targetId);
12
+ }
13
+ }
14
+ function buildLabelsById(entries) {
15
+ const labelsById = new Map();
16
+ for (const entry of entries) {
17
+ updateLabelCache(labelsById, entry);
18
+ }
19
+ return labelsById;
20
+ }
21
+ function generateEntryId(byId) {
22
+ for (let i = 0; i < 100; i++) {
23
+ const id = randomUUID().slice(0, 8);
24
+ if (!byId.has(id))
25
+ return id;
26
+ }
27
+ return randomUUID();
28
+ }
29
+ export class InMemorySessionStorage {
30
+ metadata;
31
+ entries;
32
+ byId;
33
+ labelsById;
34
+ leafId;
35
+ constructor(options) {
36
+ this.entries = options?.entries ? [...options.entries] : [];
37
+ this.byId = new Map(this.entries.map((entry) => [entry.id, entry]));
38
+ this.labelsById = buildLabelsById(this.entries);
39
+ this.leafId = options?.leafId ?? this.entries[this.entries.length - 1]?.id ?? null;
40
+ if (this.leafId !== null && !this.byId.has(this.leafId)) {
41
+ throw new Error(`Entry ${this.leafId} not found`);
42
+ }
43
+ this.metadata = options?.metadata ?? { id: uuidv7(), createdAt: new Date().toISOString() };
44
+ }
45
+ async getMetadata() {
46
+ return this.metadata;
47
+ }
48
+ async getLeafId() {
49
+ return this.leafId;
50
+ }
51
+ async setLeafId(leafId) {
52
+ if (leafId !== null && !this.byId.has(leafId)) {
53
+ throw new Error(`Entry ${leafId} not found`);
54
+ }
55
+ this.leafId = leafId;
56
+ }
57
+ async createEntryId() {
58
+ return generateEntryId(this.byId);
59
+ }
60
+ async appendEntry(entry) {
61
+ this.entries.push(entry);
62
+ this.byId.set(entry.id, entry);
63
+ updateLabelCache(this.labelsById, entry);
64
+ this.leafId = entry.id;
65
+ }
66
+ async getEntry(id) {
67
+ return this.byId.get(id);
68
+ }
69
+ async findEntries(type) {
70
+ return this.entries.filter((entry) => entry.type === type);
71
+ }
72
+ async getLabel(id) {
73
+ return this.labelsById.get(id);
74
+ }
75
+ async getPathToRoot(leafId) {
76
+ if (leafId === null)
77
+ return [];
78
+ const path = [];
79
+ let current = this.byId.get(leafId);
80
+ while (current) {
81
+ path.unshift(current);
82
+ current = current.parentId ? this.byId.get(current.parentId) : undefined;
83
+ }
84
+ return path;
85
+ }
86
+ async getEntries() {
87
+ return [...this.entries];
88
+ }
89
+ }
90
+ //# sourceMappingURL=memory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memory.js","sourceRoot":"","sources":["../../../../src/harness/session/storage/memory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEpC,SAAS,gBAAgB,CAAC,UAA+B,EAAE,KAAuB,EAAQ;IACzF,IAAI,KAAK,CAAC,IAAI,KAAK,OA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K,CAAC,UAAU,GAAgC;QAC/C,OAAO,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC;IAAA,CACzB;CACD","sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport type { SessionMetadata, SessionStorage, SessionTreeEntry } from \"../../types.js\";\nimport { uuidv7 } from \"../uuid.js\";\n\nfunction updateLabelCache(labelsById: Map<string, string>, entry: SessionTreeEntry): void {\n\tif (entry.type !== \"label\") return;\n\tconst label = entry.label?.trim();\n\tif (label) {\n\t\tlabelsById.set(entry.targetId, label);\n\t} else {\n\t\tlabelsById.delete(entry.targetId);\n\t}\n}\n\nfunction buildLabelsById(entries: SessionTreeEntry[]): Map<string, string> {\n\tconst labelsById = new Map<string, string>();\n\tfor (const entry of entries) {\n\t\tupdateLabelCache(labelsById, entry);\n\t}\n\treturn labelsById;\n}\n\nfunction generateEntryId(byId: { has(id: string): boolean }): string {\n\tfor (let i = 0; i < 100; i++) {\n\t\tconst id = randomUUID().slice(0, 8);\n\t\tif (!byId.has(id)) return id;\n\t}\n\treturn randomUUID();\n}\n\nexport class InMemorySessionStorage implements SessionStorage {\n\tprivate readonly metadata: SessionMetadata;\n\tprivate entries: SessionTreeEntry[];\n\tprivate byId: Map<string, SessionTreeEntry>;\n\tprivate labelsById: Map<string, string>;\n\tprivate leafId: string | null;\n\n\tconstructor(options?: { entries?: SessionTreeEntry[]; leafId?: string | null; metadata?: SessionMetadata }) {\n\t\tthis.entries = options?.entries ? [...options.entries] : [];\n\t\tthis.byId = new Map(this.entries.map((entry) => [entry.id, entry]));\n\t\tthis.labelsById = buildLabelsById(this.entries);\n\t\tthis.leafId = options?.leafId ?? this.entries[this.entries.length - 1]?.id ?? null;\n\t\tif (this.leafId !== null && !this.byId.has(this.leafId)) {\n\t\t\tthrow new Error(`Entry ${this.leafId} not found`);\n\t\t}\n\t\tthis.metadata = options?.metadata ?? { id: uuidv7(), createdAt: new Date().toISOString() };\n\t}\n\n\tasync getMetadata(): Promise<SessionMetadata> {\n\t\treturn this.metadata;\n\t}\n\n\tasync getLeafId(): Promise<string | null> {\n\t\treturn this.leafId;\n\t}\n\n\tasync setLeafId(leafId: string | null): Promise<void> {\n\t\tif (leafId !== null && !this.byId.has(leafId)) {\n\t\t\tthrow new Error(`Entry ${leafId} not found`);\n\t\t}\n\t\tthis.leafId = leafId;\n\t}\n\n\tasync createEntryId(): Promise<string> {\n\t\treturn generateEntryId(this.byId);\n\t}\n\n\tasync appendEntry(entry: SessionTreeEntry): Promise<void> {\n\t\tthis.entries.push(entry);\n\t\tthis.byId.set(entry.id, entry);\n\t\tupdateLabelCache(this.labelsById, entry);\n\t\tthis.leafId = entry.id;\n\t}\n\n\tasync getEntry(id: string): Promise<SessionTreeEntry | undefined> {\n\t\treturn this.byId.get(id);\n\t}\n\n\tasync findEntries<TType extends SessionTreeEntry[\"type\"]>(\n\t\ttype: TType,\n\t): Promise<Array<Extract<SessionTreeEntry, { type: TType }>>> {\n\t\treturn this.entries.filter((entry): entry is Extract<SessionTreeEntry, { type: TType }> => entry.type === type);\n\t}\n\n\tasync getLabel(id: string): Promise<string | undefined> {\n\t\treturn this.labelsById.get(id);\n\t}\n\n\tasync getPathToRoot(leafId: string | null): Promise<SessionTreeEntry[]> {\n\t\tif (leafId === null) return [];\n\t\tconst path: SessionTreeEntry[] = [];\n\t\tlet current = this.byId.get(leafId);\n\t\twhile (current) {\n\t\t\tpath.unshift(current);\n\t\t\tcurrent = current.parentId ? this.byId.get(current.parentId) : undefined;\n\t\t}\n\t\treturn path;\n\t}\n\n\tasync getEntries(): Promise<SessionTreeEntry[]> {\n\t\treturn [...this.entries];\n\t}\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export declare function uuidv7(): string;
2
+ //# sourceMappingURL=uuid.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uuid.d.ts","sourceRoot":"","sources":["../../../src/harness/session/uuid.ts"],"names":[],"mappings":"AAcA,wBAAgB,MAAM,IAAI,MAAM,CAkC/B","sourcesContent":["let lastTimestamp = -Infinity;\nlet sequence = 0;\n\nfunction fillRandomBytes(bytes: Uint8Array): void {\n\tconst crypto = globalThis.crypto;\n\tif (crypto?.getRandomValues) {\n\t\tcrypto.getRandomValues(bytes);\n\t\treturn;\n\t}\n\tfor (let i = 0; i < bytes.length; i++) {\n\t\tbytes[i] = Math.floor(Math.random() * 256);\n\t}\n}\n\nexport function uuidv7(): string {\n\tconst random = new Uint8Array(16);\n\tfillRandomBytes(random);\n\tconst timestamp = Date.now();\n\n\tif (timestamp > lastTimestamp) {\n\t\tsequence = random[6] * 0x1000000 + random[7] * 0x10000 + random[8] * 0x100 + random[9];\n\t\tlastTimestamp = timestamp;\n\t} else {\n\t\tsequence = (sequence + 1) >>> 0;\n\t\tif (sequence === 0) {\n\t\t\tlastTimestamp++;\n\t\t}\n\t}\n\n\tconst bytes = new Uint8Array(16);\n\tbytes[0] = (lastTimestamp / 0x10000000000) & 0xff;\n\tbytes[1] = (lastTimestamp / 0x100000000) & 0xff;\n\tbytes[2] = (lastTimestamp / 0x1000000) & 0xff;\n\tbytes[3] = (lastTimestamp / 0x10000) & 0xff;\n\tbytes[4] = (lastTimestamp / 0x100) & 0xff;\n\tbytes[5] = lastTimestamp & 0xff;\n\tbytes[6] = 0x70 | ((sequence >>> 28) & 0x0f);\n\tbytes[7] = (sequence >>> 20) & 0xff;\n\tbytes[8] = 0x80 | ((sequence >>> 14) & 0x3f);\n\tbytes[9] = (sequence >>> 6) & 0xff;\n\tbytes[10] = ((sequence & 0x3f) << 2) | (random[10] & 0x03);\n\tbytes[11] = random[11];\n\tbytes[12] = random[12];\n\tbytes[13] = random[13];\n\tbytes[14] = random[14];\n\tbytes[15] = random[15];\n\n\treturn formatUuid(bytes);\n}\n\nfunction formatUuid(bytes: Uint8Array): string {\n\tconst hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, \"0\"));\n\treturn `${hex.slice(0, 4).join(\"\")}-${hex.slice(4, 6).join(\"\")}-${hex.slice(6, 8).join(\"\")}-${hex.slice(8, 10).join(\"\")}-${hex.slice(10, 16).join(\"\")}`;\n}\n"]}
@@ -0,0 +1,50 @@
1
+ let lastTimestamp = -Infinity;
2
+ let sequence = 0;
3
+ function fillRandomBytes(bytes) {
4
+ const crypto = globalThis.crypto;
5
+ if (crypto?.getRandomValues) {
6
+ crypto.getRandomValues(bytes);
7
+ return;
8
+ }
9
+ for (let i = 0; i < bytes.length; i++) {
10
+ bytes[i] = Math.floor(Math.random() * 256);
11
+ }
12
+ }
13
+ export function uuidv7() {
14
+ const random = new Uint8Array(16);
15
+ fillRandomBytes(random);
16
+ const timestamp = Date.now();
17
+ if (timestamp > lastTimestamp) {
18
+ sequence = random[6] * 0x1000000 + random[7] * 0x10000 + random[8] * 0x100 + random[9];
19
+ lastTimestamp = timestamp;
20
+ }
21
+ else {
22
+ sequence = (sequence + 1) >>> 0;
23
+ if (sequence === 0) {
24
+ lastTimestamp++;
25
+ }
26
+ }
27
+ const bytes = new Uint8Array(16);
28
+ bytes[0] = (lastTimestamp / 0x10000000000) & 0xff;
29
+ bytes[1] = (lastTimestamp / 0x100000000) & 0xff;
30
+ bytes[2] = (lastTimestamp / 0x1000000) & 0xff;
31
+ bytes[3] = (lastTimestamp / 0x10000) & 0xff;
32
+ bytes[4] = (lastTimestamp / 0x100) & 0xff;
33
+ bytes[5] = lastTimestamp & 0xff;
34
+ bytes[6] = 0x70 | ((sequence >>> 28) & 0x0f);
35
+ bytes[7] = (sequence >>> 20) & 0xff;
36
+ bytes[8] = 0x80 | ((sequence >>> 14) & 0x3f);
37
+ bytes[9] = (sequence >>> 6) & 0xff;
38
+ bytes[10] = ((sequence & 0x3f) << 2) | (random[10] & 0x03);
39
+ bytes[11] = random[11];
40
+ bytes[12] = random[12];
41
+ bytes[13] = random[13];
42
+ bytes[14] = random[14];
43
+ bytes[15] = random[15];
44
+ return formatUuid(bytes);
45
+ }
46
+ function formatUuid(bytes) {
47
+ const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0"));
48
+ return `${hex.slice(0, 4).join("")}-${hex.slice(4, 6).join("")}-${hex.slice(6, 8).join("")}-${hex.slice(8, 10).join("")}-${hex.slice(10, 16).join("")}`;
49
+ }
50
+ //# sourceMappingURL=uuid.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uuid.js","sourceRoot":"","sources":["../../../src/harness/session/uuid.ts"],"names":[],"mappings":"AAAA,IAAI,aAAa,GAAG,CAAC,QAAQ,CAAC;AAC9B,IAAI,QAAQ,GAAG,CAAC,CAAC;AAEjB,SAAS,eAAe,CAAC,KAAiB,EAAQ;IACjD,MAAM,MAAM,GAAG,UAAU,CAAC,MAAM,CAAC;IACjC,IAAI,MAAM,EAAE,eAAe,EAAE,CAAC;QAC7B,MAAM,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;QAC9B,OAAO;IACR,CAAC;IACD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACvC,KAAK,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,GAAG,CAAC,CAAC;IAC5C,CAAC;AAAA,CACD;AAED,MAAM,UAAU,MAAM,GAAW;IAChC,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC;IAClC,eAAe,CAAC,MAAM,CAAC,CAAC;IACxB,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IAE7B,IAAI,SAAS,GAAG,aAAa,EAAE,CAAC;QAC/B,QAAQ,GAAG,MAAM,CAAC,CAAC,CAAC,GAAG,SAAS,GAAG,MAAM,CAAC,CAAC,CAAC,GAAG,OAAO,GAAG,MAAM,CAAC,CAAC,CAAC,GAAG,KAAK,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;QACvF,aAAa,GAAG,SAAS,CAAC;IAC3B,CAAC;SAAM,CAAC;QACP,QAAQ,GA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AC3D,KAAK,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,EAAE,CAAC,CAAC;IACvB,KAAK,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,EAAE,CAAC,CAAC;IACvB,KAAK,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,EAAE,CAAC,CAAC;IACvB,KAAK,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,EAAE,CAAC,CAAC;IACvB,KAAK,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,EAAE,CAAC,CAAC;IAEvB,OAAO,UAAU,CAAC,KAAK,CAAC,CAAC;AAAA,CACzB;AAED,SAAS,UAAU,CAAC,KAAiB,EAAU;IAC9C,MAAM,GAAG,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC;IAC5E,OAAO,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC;AAAA,CACxJ","sourcesContent":["let lastTimestamp = -Infinity;\nlet sequence = 0;\n\nfunction fillRandomBytes(bytes: Uint8Array): void {\n\tconst crypto = globalThis.crypto;\n\tif (crypto?.getRandomValues) {\n\t\tcrypto.getRandomValues(bytes);\n\t\treturn;\n\t}\n\tfor (let i = 0; i < bytes.length; i++) {\n\t\tbytes[i] = Math.floor(Math.random() * 256);\n\t}\n}\n\nexport function uuidv7(): string {\n\tconst random = new Uint8Array(16);\n\tfillRandomBytes(random);\n\tconst timestamp = Date.now();\n\n\tif (timestamp > lastTimestamp) {\n\t\tsequence = random[6] * 0x1000000 + random[7] * 0x10000 + random[8] * 0x100 + random[9];\n\t\tlastTimestamp = timestamp;\n\t} else {\n\t\tsequence = (sequence + 1) >>> 0;\n\t\tif (sequence === 0) {\n\t\t\tlastTimestamp++;\n\t\t}\n\t}\n\n\tconst bytes = new Uint8Array(16);\n\tbytes[0] = (lastTimestamp / 0x10000000000) & 0xff;\n\tbytes[1] = (lastTimestamp / 0x100000000) & 0xff;\n\tbytes[2] = (lastTimestamp / 0x1000000) & 0xff;\n\tbytes[3] = (lastTimestamp / 0x10000) & 0xff;\n\tbytes[4] = (lastTimestamp / 0x100) & 0xff;\n\tbytes[5] = lastTimestamp & 0xff;\n\tbytes[6] = 0x70 | ((sequence >>> 28) & 0x0f);\n\tbytes[7] = (sequence >>> 20) & 0xff;\n\tbytes[8] = 0x80 | ((sequence >>> 14) & 0x3f);\n\tbytes[9] = (sequence >>> 6) & 0xff;\n\tbytes[10] = ((sequence & 0x3f) << 2) | (random[10] & 0x03);\n\tbytes[11] = random[11];\n\tbytes[12] = random[12];\n\tbytes[13] = random[13];\n\tbytes[14] = random[14];\n\tbytes[15] = random[15];\n\n\treturn formatUuid(bytes);\n}\n\nfunction formatUuid(bytes: Uint8Array): string {\n\tconst hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, \"0\"));\n\treturn `${hex.slice(0, 4).join(\"\")}-${hex.slice(4, 6).join(\"\")}-${hex.slice(6, 8).join(\"\")}-${hex.slice(8, 10).join(\"\")}-${hex.slice(10, 16).join(\"\")}`;\n}\n"]}
@@ -0,0 +1,43 @@
1
+ import { type ExecutionEnv, type Skill } from "./types.js";
2
+ /** Warning produced while loading skills. */
3
+ export interface SkillDiagnostic {
4
+ /** Diagnostic severity. Currently only warnings are emitted. */
5
+ type: "warning";
6
+ /** Human-readable diagnostic message. */
7
+ message: string;
8
+ /** Path associated with the diagnostic. */
9
+ path: string;
10
+ }
11
+ /** Format a skill invocation prompt, optionally appending additional user instructions. */
12
+ export declare function formatSkillInvocation(skill: Skill, additionalInstructions?: string): string;
13
+ /** Compatibility alias for older harness consumers. */
14
+ export declare const expandSkillCommand: typeof formatSkillInvocation;
15
+ /**
16
+ * Load skills from one or more directories.
17
+ *
18
+ * Traverses directories recursively, loads `SKILL.md` files, loads direct root `.md` files as skills, honors ignore files,
19
+ * and returns diagnostics for invalid skill files. Missing input directories are skipped.
20
+ */
21
+ export declare function loadSkills(env: ExecutionEnv, dirs: string | string[]): Promise<{
22
+ skills: Skill[];
23
+ diagnostics: SkillDiagnostic[];
24
+ }>;
25
+ /**
26
+ * Load skills from source-tagged directories.
27
+ *
28
+ * Source values are preserved exactly and attached to every loaded skill and diagnostic. The agent package does not
29
+ * interpret source values; applications define their own provenance shape.
30
+ */
31
+ export declare function loadSourcedSkills<TSource, TSkill extends Skill = Skill>(env: ExecutionEnv, inputs: Array<{
32
+ path: string;
33
+ source: TSource;
34
+ }>, mapSkill?: (skill: Skill, source: TSource) => TSkill): Promise<{
35
+ skills: Array<{
36
+ skill: TSkill;
37
+ source: TSource;
38
+ }>;
39
+ diagnostics: Array<SkillDiagnostic & {
40
+ source: TSource;
41
+ }>;
42
+ }>;
43
+ //# sourceMappingURL=skills.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"skills.d.ts","sourceRoot":"","sources":["../../src/harness/skills.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,YAAY,EAA8C,KAAK,KAAK,EAAE,MAAM,YAAY,CAAC;AAQvG,6CAA6C;AAC7C,MAAM,WAAW,eAAe;IAC/B,gEAAgE;IAChE,IAAI,EAAE,SAAS,CAAC;IAChB,yCAAyC;IACzC,OAAO,EAAE,MAAM,CAAC;IAChB,2CAA2C;IAC3C,IAAI,EAAE,MAAM,CAAC;CACb;AASD,2FAA2F;AAC3F,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,KAAK,EAAE,sBAAsB,CAAC,EAAE,MAAM,GAAG,MAAM,CAG3F;AAED,uDAAuD;AACvD,eAAO,MAAM,kBAAkB,8BAAwB,CAAC;AAExD;;;;;GAKG;AACH,wBAAsB,UAAU,CAC/B,GAAG,EAAE,YAAY,EACjB,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,GACrB,OAAO,CAAC;IAAE,MAAM,EAAE,KAAK,EAAE,CAAC;IAAC,WAAW,EAAE,eAAe,EAAE,CAAA;CAAE,CAAC,CAW9D;AAED;;;;;GAKG;AACH,wBAAsB,iBAAiB,CAAC,OAAO,EAAE,MAAM,SAAS,KAAK,GAAG,KAAK,EAC5E,GAAG,EAAE,YAAY,EACjB,MAAM,EAAE,KAAK,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,OAAO,CAAA;CAAE,CAAC,EAChD,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,KAAK,MAAM,GAClD,OAAO,CAAC;IACV,MAAM,EAAE,KAAK,CAAC;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;IAClD,WAAW,EAAE,KAAK,CAAC,eAAe,GAAG;QAAE,MAAM,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;CAC1D,CAAC,CAWD","sourcesContent":["import ignore from \"ignore\";\nimport { parse } from \"yaml\";\nimport { type ExecutionEnv, type FileInfo, getOrUndefined, type Result, type Skill } from \"./types.js\";\n\nconst MAX_NAME_LENGTH = 64;\nconst MAX_DESCRIPTION_LENGTH = 1024;\nconst IGNORE_FILE_NAMES = [\".gitignore\", \".ignore\", \".fdignore\"];\n\ntype IgnoreMatcher = ReturnType<typeof ignore>;\n\n/** Warning produced while loading skills. */\nexport interface SkillDiagnostic {\n\t/** Diagnostic severity. Currently only warnings are emitted. */\n\ttype: \"warning\";\n\t/** Human-readable diagnostic message. */\n\tmessage: string;\n\t/** Path associated with the diagnostic. */\n\tpath: string;\n}\n\ninterface SkillFrontmatter {\n\tname?: string;\n\tdescription?: string;\n\t\"disable-model-invocation\"?: boolean;\n\t[key: string]: unknown;\n}\n\n/** Format a skill invocation prompt, optionally appending additional user instructions. */\nexport function formatSkillInvocation(skill: Skill, additionalInstructions?: string): string {\n\tconst skillBlock = `<skill name=\"${skill.name}\" location=\"${skill.filePath}\">\\nReferences are relative to ${dirnameEnvPath(skill.filePath)}.\\n\\n${skill.content}\\n</skill>`;\n\treturn additionalInstructions ? `${skillBlock}\\n\\n${additionalInstructions}` : skillBlock;\n}\n\n/** Compatibility alias for older harness consumers. */\nexport const expandSkillCommand = formatSkillInvocation;\n\n/**\n * Load skills from one or more directories.\n *\n * Traverses directories recursively, loads `SKILL.md` files, loads direct root `.md` files as skills, honors ignore files,\n * and returns diagnostics for invalid skill files. Missing input directories are skipped.\n */\nexport async function loadSkills(\n\tenv: ExecutionEnv,\n\tdirs: string | string[],\n): Promise<{ skills: Skill[]; diagnostics: SkillDiagnostic[] }> {\n\tconst skills: Skill[] = [];\n\tconst diagnostics: SkillDiagnostic[] = [];\n\tfor (const dir of Array.isArray(dirs) ? dirs : [dirs]) {\n\t\tconst rootInfo = getOrUndefined(await env.fileInfo(dir));\n\t\tif (!rootInfo || (await resolveKind(env, rootInfo)) !== \"directory\") continue;\n\t\tconst result = await loadSkillsFromDirInternal(env, rootInfo.path, true, ignore(), rootInfo.path);\n\t\tskills.push(...result.skills);\n\t\tdiagnostics.push(...result.diagnostics);\n\t}\n\treturn { skills, diagnostics };\n}\n\n/**\n * Load skills from source-tagged directories.\n *\n * Source values are preserved exactly and attached to every loaded skill and diagnostic. The agent package does not\n * interpret source values; applications define their own provenance shape.\n */\nexport async function loadSourcedSkills<TSource, TSkill extends Skill = Skill>(\n\tenv: ExecutionEnv,\n\tinputs: Array<{ path: string; source: TSource }>,\n\tmapSkill?: (skill: Skill, source: TSource) => TSkill,\n): Promise<{\n\tskills: Array<{ skill: TSkill; source: TSource }>;\n\tdiagnostics: Array<SkillDiagnostic & { source: TSource }>;\n}> {\n\tconst skills: Array<{ skill: TSkill; source: TSource }> = [];\n\tconst diagnostics: Array<SkillDiagnostic & { source: TSource }> = [];\n\tfor (const input of inputs) {\n\t\tconst result = await loadSkills(env, input.path);\n\t\tfor (const skill of result.skills) {\n\t\t\tskills.push({ skill: mapSkill ? mapSkill(skill, input.source) : (skill as TSkill), source: input.source });\n\t\t}\n\t\tfor (const diagnostic of result.diagnostics) diagnostics.push({ ...diagnostic, source: input.source });\n\t}\n\treturn { skills, diagnostics };\n}\n\nasync function loadSkillsFromDirInternal(\n\tenv: ExecutionEnv,\n\tdir: string,\n\tincludeRootFiles: boolean,\n\tignoreMatcher: IgnoreMatcher,\n\trootDir: string,\n): Promise<{ skills: Skill[]; diagnostics: SkillDiagnostic[] }> {\n\tconst skills: Skill[] = [];\n\tconst diagnostics: SkillDiagnostic[] = [];\n\n\tconst dirInfo = getOrUndefined(await env.fileInfo(dir));\n\tif (!dirInfo || (await resolveKind(env, dirInfo)) !== \"directory\") return { skills, diagnostics };\n\n\tawait addIgnoreRules(env, ignoreMatcher, dir, rootDir);\n\n\tconst entries = getOrUndefined(await env.listDir(dir));\n\tif (!entries) return { skills, diagnostics };\n\n\tfor (const entry of entries) {\n\t\tif (entry.name !== \"SKILL.md\") continue;\n\t\tconst fullPath = entry.path;\n\t\tconst kind = await resolveKind(env, entry);\n\t\tif (kind !== \"file\") continue;\n\t\tconst relPath = relativeEnvPath(rootDir, fullPath);\n\t\tif (ignoreMatcher.ignores(relPath)) continue;\n\n\t\tconst result = await loadSkillFromFile(env, fullPath);\n\t\tif (result.skill) skills.push(result.skill);\n\t\tdiagnostics.push(...result.diagnostics);\n\t\treturn { skills, diagnostics };\n\t}\n\n\tfor (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {\n\t\tif (entry.name.startsWith(\".\") || entry.name === \"node_modules\") continue;\n\t\tconst fullPath = entry.path;\n\t\tconst kind = await resolveKind(env, entry);\n\t\tif (!kind) continue;\n\n\t\tconst relPath = relativeEnvPath(rootDir, fullPath);\n\t\tconst ignorePath = kind === \"directory\" ? `${relPath}/` : relPath;\n\t\tif (ignoreMatcher.ignores(ignorePath)) continue;\n\n\t\tif (kind === \"directory\") {\n\t\t\tconst result = await loadSkillsFromDirInternal(env, fullPath, false, ignoreMatcher, rootDir);\n\t\t\tskills.push(...result.skills);\n\t\t\tdiagnostics.push(...result.diagnostics);\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (kind !== \"file\" || !includeRootFiles || !entry.name.endsWith(\".md\")) continue;\n\t\tconst result = await loadSkillFromFile(env, fullPath);\n\t\tif (result.skill) skills.push(result.skill);\n\t\tdiagnostics.push(...result.diagnostics);\n\t}\n\n\treturn { skills, diagnostics };\n}\n\nasync function addIgnoreRules(env: ExecutionEnv, ig: IgnoreMatcher, dir: string, rootDir: string): Promise<void> {\n\tconst relativeDir = relativeEnvPath(rootDir, dir);\n\tconst prefix = relativeDir ? `${relativeDir}/` : \"\";\n\n\tfor (const filename of IGNORE_FILE_NAMES) {\n\t\tconst ignorePath = joinEnvPath(dir, filename);\n\t\tconst info = getOrUndefined(await env.fileInfo(ignorePath));\n\t\tif (info?.kind !== \"file\") continue;\n\t\tconst content = await env.readTextFile(ignorePath);\n\t\tif (!content.ok) continue;\n\t\tconst patterns = content.value\n\t\t\t.split(/\\r?\\n/)\n\t\t\t.map((line) => prefixIgnorePattern(line, prefix))\n\t\t\t.filter((line): line is string => Boolean(line));\n\t\tif (patterns.length > 0) ig.add(patterns);\n\t}\n}\n\nfunction prefixIgnorePattern(line: string, prefix: string): string | null {\n\tconst trimmed = line.trim();\n\tif (!trimmed) return null;\n\tif (trimmed.startsWith(\"#\") && !trimmed.startsWith(\"\\\\#\")) return null;\n\n\tlet pattern = line;\n\tlet negated = false;\n\tif (pattern.startsWith(\"!\")) {\n\t\tnegated = true;\n\t\tpattern = pattern.slice(1);\n\t} else if (pattern.startsWith(\"\\\\!\")) {\n\t\tpattern = pattern.slice(1);\n\t}\n\tif (pattern.startsWith(\"/\")) pattern = pattern.slice(1);\n\tconst prefixed = prefix ? `${prefix}${pattern}` : pattern;\n\treturn negated ? `!${prefixed}` : prefixed;\n}\n\nasync function loadSkillFromFile(\n\tenv: ExecutionEnv,\n\tfilePath: string,\n): Promise<{ skill: Skill | null; diagnostics: SkillDiagnostic[] }> {\n\tconst diagnostics: SkillDiagnostic[] = [];\n\tconst rawContent = await env.readTextFile(filePath);\n\tif (!rawContent.ok) {\n\t\tdiagnostics.push({ type: \"warning\", message: rawContent.error.message, path: filePath });\n\t\treturn { skill: null, diagnostics };\n\t}\n\n\tconst parsed = parseFrontmatter<SkillFrontmatter>(rawContent.value);\n\tif (!parsed.ok) {\n\t\tdiagnostics.push({ type: \"warning\", message: parsed.error.message, path: filePath });\n\t\treturn { skill: null, diagnostics };\n\t}\n\n\tconst { frontmatter, body } = parsed.value;\n\tconst skillDir = dirnameEnvPath(filePath);\n\tconst parentDirName = basenameEnvPath(skillDir);\n\tconst description = typeof frontmatter.description === \"string\" ? frontmatter.description : undefined;\n\n\tfor (const error of validateDescription(description)) {\n\t\tdiagnostics.push({ type: \"warning\", message: error, path: filePath });\n\t}\n\n\tconst frontmatterName = typeof frontmatter.name === \"string\" ? frontmatter.name : undefined;\n\tconst name = frontmatterName || parentDirName;\n\tfor (const error of validateName(name, parentDirName)) {\n\t\tdiagnostics.push({ type: \"warning\", message: error, path: filePath });\n\t}\n\n\tif (!description || description.trim() === \"\") {\n\t\treturn { skill: null, diagnostics };\n\t}\n\n\treturn {\n\t\tskill: {\n\t\t\tname,\n\t\t\tdescription,\n\t\t\tcontent: body,\n\t\t\tfilePath,\n\t\t\tdisableModelInvocation: frontmatter[\"disable-model-invocation\"] === true,\n\t\t},\n\t\tdiagnostics,\n\t};\n}\n\nfunction validateName(name: string, parentDirName: string): string[] {\n\tconst errors: string[] = [];\n\tif (name !== parentDirName) errors.push(`name \"${name}\" does not match parent directory \"${parentDirName}\"`);\n\tif (name.length > MAX_NAME_LENGTH) errors.push(`name exceeds ${MAX_NAME_LENGTH} characters (${name.length})`);\n\tif (!/^[a-z0-9-]+$/.test(name)) {\n\t\terrors.push(\"name contains invalid characters (must be lowercase a-z, 0-9, hyphens only)\");\n\t}\n\tif (name.startsWith(\"-\") || name.endsWith(\"-\")) errors.push(\"name must not start or end with a hyphen\");\n\tif (name.includes(\"--\")) errors.push(\"name must not contain consecutive hyphens\");\n\treturn errors;\n}\n\nfunction validateDescription(description: string | undefined): string[] {\n\tconst errors: string[] = [];\n\tif (!description || description.trim() === \"\") {\n\t\terrors.push(\"description is required\");\n\t} else if (description.length > MAX_DESCRIPTION_LENGTH) {\n\t\terrors.push(`description exceeds ${MAX_DESCRIPTION_LENGTH} characters (${description.length})`);\n\t}\n\treturn errors;\n}\n\nfunction parseFrontmatter<T extends Record<string, unknown>>(\n\tcontent: string,\n): Result<{ frontmatter: T; body: string }, Error> {\n\ttry {\n\t\tconst normalized = content.replace(/\\r\\n/g, \"\\n\").replace(/\\r/g, \"\\n\");\n\t\tif (!normalized.startsWith(\"---\")) return { ok: true, value: { frontmatter: {} as T, body: normalized } };\n\t\tconst endIndex = normalized.indexOf(\"\\n---\", 3);\n\t\tif (endIndex === -1) return { ok: true, value: { frontmatter: {} as T, body: normalized } };\n\t\tconst yamlString = normalized.slice(4, endIndex);\n\t\tconst body = normalized.slice(endIndex + 4).trim();\n\t\treturn { ok: true, value: { frontmatter: (parse(yamlString) ?? {}) as T, body } };\n\t} catch (error) {\n\t\treturn { ok: false, error: error instanceof Error ? error : new Error(String(error)) };\n\t}\n}\n\nasync function resolveKind(env: ExecutionEnv, info: FileInfo): Promise<\"file\" | \"directory\" | undefined> {\n\tif (info.kind === \"file\" || info.kind === \"directory\") return info.kind;\n\tconst canonicalPath = await env.canonicalPath(info.path);\n\tif (!canonicalPath.ok) return undefined;\n\tconst target = getOrUndefined(await env.fileInfo(canonicalPath.value));\n\tif (!target) return undefined;\n\treturn target.kind === \"file\" || target.kind === \"directory\" ? target.kind : undefined;\n}\n\nfunction joinEnvPath(base: string, child: string): string {\n\treturn `${base.replace(/\\/+$/, \"\")}/${child.replace(/^\\/+/, \"\")}`;\n}\n\nfunction dirnameEnvPath(path: string): string {\n\tconst normalized = path.replace(/\\/+$/, \"\");\n\tconst slashIndex = normalized.lastIndexOf(\"/\");\n\treturn slashIndex <= 0 ? \"/\" : normalized.slice(0, slashIndex);\n}\n\nfunction basenameEnvPath(path: string): string {\n\tconst normalized = path.replace(/\\/+$/, \"\");\n\tconst slashIndex = normalized.lastIndexOf(\"/\");\n\treturn slashIndex === -1 ? normalized : normalized.slice(slashIndex + 1);\n}\n\nfunction relativeEnvPath(root: string, path: string): string {\n\tconst normalizedRoot = root.replace(/\\/+$/, \"\");\n\tconst normalizedPath = path.replace(/\\/+$/, \"\");\n\tif (normalizedPath === normalizedRoot) return \"\";\n\treturn normalizedPath.startsWith(`${normalizedRoot}/`)\n\t\t? normalizedPath.slice(normalizedRoot.length + 1)\n\t\t: normalizedPath.replace(/^\\/+/, \"\");\n}\n"]}