@kirkchinese/harness-forge 0.4.0 → 0.5.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/README.md +4 -0
- package/docs/DETAIL.zh.md +47 -0
- package/lib/agents/presets.d.ts +7 -0
- package/lib/agents/presets.js +11 -1
- package/lib/agents/profiles.js +3 -2
- package/lib/agents/row.js +3 -1
- package/lib/client/paths.d.ts +12 -0
- package/lib/client/paths.js +68 -0
- package/lib/client/typert.d.ts +12 -1
- package/lib/client/typert.js +54 -6
- package/lib/client.js +1593 -270
- package/lib/commands/row.js +1 -1
- package/lib/context/instructions.js +5 -5
- package/lib/dsh/host.d.ts +3 -0
- package/lib/dsh/host.js +3 -0
- package/lib/forge/contract.d.ts +42 -2
- package/lib/forge/contract.js +3 -1
- package/lib/forge/follow.d.ts +30 -0
- package/lib/forge/follow.js +73 -0
- package/lib/forge/ledger.d.ts +70 -7
- package/lib/forge/ledger.js +314 -42
- package/lib/forge/projects.d.ts +5 -2
- package/lib/forge/projects.js +23 -16
- package/lib/forge/remote/archive.d.ts +65 -0
- package/lib/forge/remote/archive.js +565 -0
- package/lib/forge/remote/code-parts.d.ts +18 -0
- package/lib/forge/remote/code-parts.js +76 -0
- package/lib/forge/remote/fetch.d.ts +265 -0
- package/lib/forge/remote/fetch.js +1133 -0
- package/lib/forge/remote/service.d.ts +256 -0
- package/lib/forge/remote/service.js +868 -0
- package/lib/forge/service.d.ts +19 -9
- package/lib/forge/service.js +47 -31
- package/lib/forge/settings.d.ts +5 -3
- package/lib/forge/settings.js +11 -4
- package/lib/forge/state.d.ts +60 -0
- package/lib/forge/state.js +174 -58
- package/lib/hooks/broker/engine.js +2 -8
- package/lib/hooks/broker/session-events.d.ts +5 -0
- package/lib/hooks/broker/session-events.js +12 -3
- package/lib/hooks/broker/types.d.ts +7 -5
- package/lib/hooks/condition.d.ts +78 -2
- package/lib/hooks/condition.js +494 -73
- package/lib/hooks/config.d.ts +1 -1
- package/lib/hooks/config.js +4 -0
- package/lib/hooks/glob.d.ts +41 -0
- package/lib/hooks/glob.js +114 -0
- package/lib/hooks/permissions.d.ts +8 -3
- package/lib/hooks/permissions.js +119 -26
- package/lib/hooks/powershell-aliases.d.ts +19 -0
- package/lib/hooks/powershell-aliases.js +58 -0
- package/lib/hooks/process.d.ts +17 -0
- package/lib/hooks/process.js +156 -1
- package/lib/hooks/row.js +2 -0
- package/lib/hooks/runner.d.ts +17 -4
- package/lib/hooks/runner.js +42 -13
- package/lib/hooks/shell-commands.d.ts +10 -1
- package/lib/hooks/shell-commands.js +74 -10
- package/lib/hooks/shell.d.ts +3 -2
- package/lib/hooks/shell.js +5 -2
- package/lib/hooks/vocabulary.js +3 -1
- package/lib/mcp/catalog.d.ts +5 -2
- package/lib/mcp/catalog.js +18 -10
- package/lib/mcp/mount.d.ts +40 -2
- package/lib/mcp/mount.js +131 -31
- package/lib/mcp/policy.d.ts +42 -0
- package/lib/mcp/policy.js +43 -0
- package/lib/mcp/remote.js +5 -15
- package/lib/mcp/row.js +111 -17
- package/lib/mods/loader.js +4 -0
- package/lib/mods/service/turns.d.ts +8 -7
- package/lib/mods/service/turns.js +10 -35
- package/lib/opencode/bun.js +14 -2
- package/lib/opencode/row.d.ts +14 -1
- package/lib/opencode/row.js +38 -20
- package/lib/opencode/runtime/server.js +2 -1
- package/lib/porting/adapter-paths.d.ts +13 -0
- package/lib/porting/adapter-paths.js +120 -0
- package/lib/porting/port-gap.d.ts +10 -4
- package/lib/porting/port-gap.js +43 -7
- package/lib/porting/prompt.js +9 -7
- package/lib/porting/row.js +7 -5
- package/lib/porting/verify-port.d.ts +3 -10
- package/lib/porting/verify-port.js +12 -34
- package/lib/sessions/chat-import.d.ts +48 -0
- package/lib/sessions/chat-import.js +44 -0
- package/lib/sessions/claude.d.ts +16 -10
- package/lib/sessions/claude.js +76 -55
- package/lib/sessions/codex-discover.d.ts +34 -0
- package/lib/sessions/codex-discover.js +205 -0
- package/lib/sessions/codex-meta.d.ts +63 -0
- package/lib/sessions/codex-meta.js +148 -0
- package/lib/sessions/codex-text.d.ts +9 -0
- package/lib/sessions/codex-text.js +44 -0
- package/lib/sessions/codex.d.ts +34 -22
- package/lib/sessions/codex.js +150 -183
- package/lib/sessions/opencode.d.ts +15 -9
- package/lib/sessions/opencode.js +77 -58
- package/lib/sessions/persist.d.ts +162 -0
- package/lib/sessions/persist.js +377 -0
- package/lib/sessions/row.d.ts +2 -1
- package/lib/sessions/row.js +75 -9
- package/lib/sessions/turns.d.ts +31 -0
- package/lib/sessions/turns.js +63 -0
- package/lib/sessions/zstd.d.ts +15 -0
- package/lib/sessions/zstd.js +152 -0
- package/lib/shared/render.d.ts +30 -1
- package/lib/shared/render.js +813 -48
- package/lib/skills/provider.d.ts +2 -0
- package/lib/skills/provider.js +8 -2
- package/lib/skills/row.js +1 -1
- package/lib/workbench/adapter-coverage.d.ts +20 -2
- package/lib/workbench/adapter-coverage.js +46 -7
- package/lib/workbench/overview.d.ts +77 -27
- package/lib/workbench/overview.js +99 -12
- package/lib/workbench/remote.d.ts +64 -21
- package/lib/workbench/remote.js +181 -42
- package/lib/workbench/settings.d.ts +5 -1
- package/lib/workbench/settings.js +6 -2
- package/lib/workbench/workbench.d.ts +34 -4
- package/lib/workbench/workbench.js +147 -46
- package/locale/zh.json +2 -1
- package/package.json +3 -2
- package/lib/forge/marketplace-install.d.ts +0 -3
- package/lib/forge/marketplace-install.js +0 -64
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The hand-off of a synthesized session to DSH, shared by every importer: write it through
|
|
3
|
+
* `sessionPersistence`, place it in the workspace of its directory, and make it live, so the sidebar lists it
|
|
4
|
+
* with its title under its project and "Open" works at once. Writing the log alone leaves it unknown to the
|
|
5
|
+
* running DSH (`sessions.retain: unknown session`, "Untitled" under "Ungrouped") until a restart.
|
|
6
|
+
* @module @kirkchinese/harness-forge
|
|
7
|
+
*/
|
|
8
|
+
import { realpathSync, statSync } from 'node:fs';
|
|
9
|
+
import { homedir, tmpdir } from 'node:os';
|
|
10
|
+
import { resolve } from 'node:path';
|
|
11
|
+
import { synthesizeDshSession } from '@kirkchinese/harness-forge-core';
|
|
12
|
+
import { dshService } from '../dsh/host.js';
|
|
13
|
+
import { compareTurns, replyText, turnReader, turnText } from './turns.js';
|
|
14
|
+
/**
|
|
15
|
+
* The same for listing and import: find the stored session that holds the source session, by id and provenance.
|
|
16
|
+
*
|
|
17
|
+
* With provenance, a session under this version's own ids holds the source unless its creation time says otherwise.
|
|
18
|
+
* One under an earlier version's id holds it when it was created when the source was, and no other source session
|
|
19
|
+
* created then may be stored under that id; else only when, of all those source sessions, this is the one whose
|
|
20
|
+
* every turn (prompt and answer) is the stored session's ({@link uniqueFullMatch}). A session whose time is unknown,
|
|
21
|
+
* or that more than one or none of them match in full, is left to the person.
|
|
22
|
+
*/
|
|
23
|
+
export async function matchStored(identity, stored) {
|
|
24
|
+
const provenance = identity.provenance;
|
|
25
|
+
const own = provenance === undefined ? [identity.id] : [identity.id, provenance.alternateId];
|
|
26
|
+
let maybe;
|
|
27
|
+
for (const id of new Set([...own, ...identity.legacyIds])) {
|
|
28
|
+
const header = stored.get(id);
|
|
29
|
+
if (header === undefined)
|
|
30
|
+
continue;
|
|
31
|
+
if (provenance === undefined)
|
|
32
|
+
return { kind: 'imported', id };
|
|
33
|
+
const same = sameOrigin(header.createdAt, provenance.createdAt);
|
|
34
|
+
// Without times to compare, this version's own ids are taken at their word: it gives them to this session only.
|
|
35
|
+
if (own.includes(id) && same !== false)
|
|
36
|
+
return { kind: 'imported', id };
|
|
37
|
+
if (same === undefined)
|
|
38
|
+
maybe ??= id;
|
|
39
|
+
if (same !== true)
|
|
40
|
+
continue;
|
|
41
|
+
const rivals = await provenance.rivals(id);
|
|
42
|
+
if (rivals.length === 0)
|
|
43
|
+
return { kind: 'imported', id };
|
|
44
|
+
const verdict = await uniqueFullMatch(stored.turns(id), provenance, rivals);
|
|
45
|
+
if (verdict === 'this')
|
|
46
|
+
return { kind: 'imported', id };
|
|
47
|
+
if (verdict === 'unknown')
|
|
48
|
+
maybe ??= id;
|
|
49
|
+
}
|
|
50
|
+
const free = own.find(id => !stored.has(id));
|
|
51
|
+
if (free === undefined)
|
|
52
|
+
throw new Error(`cannot import as ${own.join(' or ')}: DSH holds other sessions under those ids`);
|
|
53
|
+
return maybe !== undefined ? { kind: 'maybe', id: maybe, newId: free } : { kind: 'new', id: free };
|
|
54
|
+
}
|
|
55
|
+
/** Whether two creation times are one session's; `undefined` when one is not known (a source without timestamps). */
|
|
56
|
+
function sameOrigin(stored, source) {
|
|
57
|
+
if (stored === undefined || !Number.isFinite(stored) || stored <= 0 || !Number.isFinite(source) || source <= 0)
|
|
58
|
+
return undefined;
|
|
59
|
+
return stored === source;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Which of the source sessions that may be stored under one id it holds: `this` one, when its every turn is the
|
|
63
|
+
* stored session's and no rival's is; `other` when its turns part ways with the stored session's, or a rival alone
|
|
64
|
+
* matches in full; else `unknown` (several match, or none does in full: one only begins like the other).
|
|
65
|
+
*/
|
|
66
|
+
async function uniqueFullMatch(stored, provenance, rivals) {
|
|
67
|
+
const compare = async (open) => {
|
|
68
|
+
const source = open();
|
|
69
|
+
try {
|
|
70
|
+
return await compareTurns(stored, source);
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
await source.close();
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
const mine = await compare(() => provenance.turns());
|
|
77
|
+
if (mine === 'different')
|
|
78
|
+
return 'other';
|
|
79
|
+
let others = 0;
|
|
80
|
+
for (const rival of rivals) {
|
|
81
|
+
if (await compare(rival) === 'same')
|
|
82
|
+
others += 1;
|
|
83
|
+
// A second full match settles it: no one of them is told apart.
|
|
84
|
+
if (others + (mine === 'same' ? 1 : 0) > 1)
|
|
85
|
+
return 'unknown';
|
|
86
|
+
}
|
|
87
|
+
if (mine === 'same')
|
|
88
|
+
return others === 0 ? 'this' : 'unknown';
|
|
89
|
+
return others === 1 ? 'other' : 'unknown';
|
|
90
|
+
}
|
|
91
|
+
/** A session stored under an id that may or may not hold the one being imported; the person decides. */
|
|
92
|
+
export class SessionMaybeImportedError extends Error {
|
|
93
|
+
existingId;
|
|
94
|
+
code = 'session-maybe-imported';
|
|
95
|
+
constructor(existingId) {
|
|
96
|
+
super(`DSH session ${existingId} may already hold this session; open it, or import this one as a new session.`);
|
|
97
|
+
this.existingId = existingId;
|
|
98
|
+
this.name = 'SessionMaybeImportedError';
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/** The identity of an import into a named session: that one, reused when it is stored. */
|
|
102
|
+
export function targetIdentity(targetId) {
|
|
103
|
+
return { id: targetId, legacyIds: [] };
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Persist a normalized session, or reuse the one an earlier import wrote, and make it live. The session is written
|
|
107
|
+
* under the id `identity` resolves to, whatever id it carries.
|
|
108
|
+
*/
|
|
109
|
+
export async function persistImported(ctx, session, identity) {
|
|
110
|
+
const persistence = dshService(ctx, 'sessionPersistence');
|
|
111
|
+
if (persistence === undefined)
|
|
112
|
+
throw new Error('sessionPersistence service is not available');
|
|
113
|
+
const stored = await StoredSessions.of(persistence);
|
|
114
|
+
let match;
|
|
115
|
+
try {
|
|
116
|
+
match = await matchStored(identity, stored);
|
|
117
|
+
}
|
|
118
|
+
finally {
|
|
119
|
+
await stored.close();
|
|
120
|
+
}
|
|
121
|
+
if (match.kind === 'maybe')
|
|
122
|
+
throw new SessionMaybeImportedError(match.id);
|
|
123
|
+
const id = match.id;
|
|
124
|
+
const workspace = await workspaceFor(ctx, session.cwd);
|
|
125
|
+
const synthesized = synthesizeDshSession({ ...session, id, source: { ...session.source, sessionId: id } });
|
|
126
|
+
if (match.kind === 'new') {
|
|
127
|
+
const cwd = workspace?.path ?? synthesized.meta.cwd;
|
|
128
|
+
const handle = await persistence.create({
|
|
129
|
+
version: 4,
|
|
130
|
+
id,
|
|
131
|
+
createdAt: synthesized.meta.createdAt,
|
|
132
|
+
isSeeded: false,
|
|
133
|
+
...(cwd !== undefined ? { cwd } : {}),
|
|
134
|
+
delegationDepth: synthesized.meta.delegationDepth,
|
|
135
|
+
...(synthesized.meta.origin !== undefined ? { origin: synthesized.meta.origin } : {}),
|
|
136
|
+
...(synthesized.meta.parentSession !== undefined ? { parentSession: synthesized.meta.parentSession } : {}),
|
|
137
|
+
});
|
|
138
|
+
try {
|
|
139
|
+
await handle.append(synthesized.events);
|
|
140
|
+
await handle.flush();
|
|
141
|
+
}
|
|
142
|
+
finally {
|
|
143
|
+
await handle.close();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const grouped = workspace !== undefined && await attach(workspace, id);
|
|
147
|
+
const persisted = {
|
|
148
|
+
id,
|
|
149
|
+
reused: match.kind === 'imported',
|
|
150
|
+
live: await makeLive(ctx, id),
|
|
151
|
+
...(grouped ? { workspace: workspace.path } : {}),
|
|
152
|
+
};
|
|
153
|
+
return { persisted, synthesized };
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Make a session an import stored known to DSH again, so opening it shows it: placed in the workspace of its
|
|
157
|
+
* directory and live, as a new import does, and out of the archive when the person archived it (an archived
|
|
158
|
+
* session is hidden from every grouping, and opening it lands on an empty new-session page).
|
|
159
|
+
*/
|
|
160
|
+
export async function openStored(ctx, id) {
|
|
161
|
+
const stored = await storedSessions(ctx);
|
|
162
|
+
const header = stored.get(id);
|
|
163
|
+
await stored.close();
|
|
164
|
+
if (header === undefined)
|
|
165
|
+
throw new Error(`DSH does not hold a session ${id}`);
|
|
166
|
+
const workspace = await workspaceFor(ctx, header.cwd);
|
|
167
|
+
if (workspace !== undefined)
|
|
168
|
+
await attach(workspace, id);
|
|
169
|
+
const registry = dshService(ctx, 'workspaceRegistry');
|
|
170
|
+
let unarchived = false;
|
|
171
|
+
if (registry?.archivedSessionIds?.includes(id) === true && typeof registry.unarchiveSession === 'function') {
|
|
172
|
+
await registry.unarchiveSession(id);
|
|
173
|
+
unarchived = true;
|
|
174
|
+
}
|
|
175
|
+
return { id, live: await makeLive(ctx, id), unarchived, archived: registry?.archivedSessionIds?.includes(id) === true };
|
|
176
|
+
}
|
|
177
|
+
/** The report of an import that {@link persistImported} carried out. */
|
|
178
|
+
export function importReport(sourcePath, title, result) {
|
|
179
|
+
const { persisted, synthesized } = result;
|
|
180
|
+
return {
|
|
181
|
+
...persisted,
|
|
182
|
+
sourcePath,
|
|
183
|
+
...(title !== undefined ? { title } : {}),
|
|
184
|
+
turns: synthesized.stats.turns,
|
|
185
|
+
events: synthesized.events.length,
|
|
186
|
+
stats: {
|
|
187
|
+
steps: synthesized.stats.steps,
|
|
188
|
+
messages: synthesized.stats.messages,
|
|
189
|
+
toolCalls: synthesized.stats.toolCalls,
|
|
190
|
+
toolResults: synthesized.stats.toolResults,
|
|
191
|
+
synthesizedToolResults: synthesized.stats.synthesizedToolResults,
|
|
192
|
+
},
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
/** How many events of a stored session's log are read at once: a page holds a few turns, and reading stops at the turn that tells. */
|
|
196
|
+
const STORED_EVENTS_PAGE = 256;
|
|
197
|
+
/** The sessions DSH stores, by id; none when it cannot list them. */
|
|
198
|
+
export async function storedSessions(ctx) {
|
|
199
|
+
return StoredSessions.of(dshService(ctx, 'sessionPersistence'));
|
|
200
|
+
}
|
|
201
|
+
/** The sessions DSH stores: their headers, listed once, and what was said in them, read when asked. */
|
|
202
|
+
export class StoredSessions {
|
|
203
|
+
persistence;
|
|
204
|
+
headers;
|
|
205
|
+
readers = new Map();
|
|
206
|
+
constructor(persistence, headers) {
|
|
207
|
+
this.persistence = persistence;
|
|
208
|
+
this.headers = headers;
|
|
209
|
+
}
|
|
210
|
+
static async of(persistence) {
|
|
211
|
+
const headers = new Map();
|
|
212
|
+
if (typeof persistence?.list === 'function') {
|
|
213
|
+
try {
|
|
214
|
+
for (const entry of await persistence.list()) {
|
|
215
|
+
// Current hosts list `{ header, revision, sizeBytes }`; older ones listed the headers.
|
|
216
|
+
const record = entry;
|
|
217
|
+
const header = record?.header ?? record;
|
|
218
|
+
const id = header?.id;
|
|
219
|
+
const createdAt = header?.createdAt;
|
|
220
|
+
const cwd = header?.cwd;
|
|
221
|
+
if (typeof id === 'string')
|
|
222
|
+
headers.set(id, { id, ...(typeof createdAt === 'number' ? { createdAt } : {}), ...(typeof cwd === 'string' ? { cwd } : {}) });
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
catch {
|
|
226
|
+
// An unreadable listing only means the import cannot tell it ran before.
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return new StoredSessions(persistence, headers);
|
|
230
|
+
}
|
|
231
|
+
get(id) {
|
|
232
|
+
return this.headers.get(id);
|
|
233
|
+
}
|
|
234
|
+
has(id) {
|
|
235
|
+
return this.headers.has(id);
|
|
236
|
+
}
|
|
237
|
+
/** A reader of a stored session's turns, shared by every comparison until {@link close}; its log is read in pages. */
|
|
238
|
+
turns(id) {
|
|
239
|
+
let reader = this.readers.get(id);
|
|
240
|
+
if (reader === undefined) {
|
|
241
|
+
reader = turnReader(this.storedTurns(id));
|
|
242
|
+
this.readers.set(id, reader);
|
|
243
|
+
}
|
|
244
|
+
return reader;
|
|
245
|
+
}
|
|
246
|
+
/** Let go of the stored sessions read. */
|
|
247
|
+
async close() {
|
|
248
|
+
const readers = [...this.readers.values()];
|
|
249
|
+
this.readers.clear();
|
|
250
|
+
await Promise.all(readers.map(reader => reader.close()));
|
|
251
|
+
}
|
|
252
|
+
/** The turns of a stored session's log (`turn/start`, its first user message, the assistant's messages), as they are read. */
|
|
253
|
+
async *storedTurns(id) {
|
|
254
|
+
if (!this.headers.has(id) || typeof this.persistence?.open !== 'function')
|
|
255
|
+
throw new Error(`cannot read DSH session ${id}`);
|
|
256
|
+
const handle = await this.persistence.open(id, 'read');
|
|
257
|
+
try {
|
|
258
|
+
let turn;
|
|
259
|
+
for (let offset = 0;;) {
|
|
260
|
+
const { events } = await handle.read(offset, STORED_EVENTS_PAGE);
|
|
261
|
+
offset += events.length;
|
|
262
|
+
for (const entry of events) {
|
|
263
|
+
const event = entry;
|
|
264
|
+
if (event?.type === 'turn/start') {
|
|
265
|
+
if (turn !== undefined)
|
|
266
|
+
yield turnText(turn.prompt ?? '', turn.replies);
|
|
267
|
+
turn = { prompt: undefined, replies: [] };
|
|
268
|
+
}
|
|
269
|
+
else if (turn !== undefined && turn.prompt === undefined && event?.type === 'user/message' && event.data?.role === 'user') {
|
|
270
|
+
turn.prompt = replyText(Array.isArray(event.data.content) ? event.data.content : []).join('\n');
|
|
271
|
+
}
|
|
272
|
+
else if (turn !== undefined && event?.type === 'assistant/message') {
|
|
273
|
+
const content = event.data?.message?.content;
|
|
274
|
+
turn.replies.push(...replyText(Array.isArray(content) ? content : []));
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if (events.length < STORED_EVENTS_PAGE)
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
if (turn !== undefined)
|
|
281
|
+
yield turnText(turn.prompt ?? '', turn.replies);
|
|
282
|
+
}
|
|
283
|
+
finally {
|
|
284
|
+
await handle.close();
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
/** The workspace of a session's directory: DSH groups a session only under the workspace whose path is its cwd. */
|
|
289
|
+
async function workspaceFor(ctx, cwd) {
|
|
290
|
+
const registry = dshService(ctx, 'workspaceRegistry');
|
|
291
|
+
if (registry === undefined || cwd === undefined || !isDirectory(cwd) || isSharedFolder(cwd))
|
|
292
|
+
return undefined;
|
|
293
|
+
try {
|
|
294
|
+
return await registry.create(cwd);
|
|
295
|
+
}
|
|
296
|
+
catch {
|
|
297
|
+
return undefined;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* The home folder and the system's temporary folders (`os.tmpdir()`, `/tmp`, `/var/tmp`, macOS's per-user
|
|
302
|
+
* `/var/folders/…/T`), which hold everyone's odds and ends and are no project: a session there gets no workspace.
|
|
303
|
+
* A folder inside one is a project of its own.
|
|
304
|
+
*/
|
|
305
|
+
export function isSharedFolder(path) {
|
|
306
|
+
const target = resolve(path);
|
|
307
|
+
const shared = [homedir(), tmpdir()].map(folder => resolve(folder));
|
|
308
|
+
for (const folder of [...shared]) {
|
|
309
|
+
try {
|
|
310
|
+
shared.push(realpathSync(folder));
|
|
311
|
+
}
|
|
312
|
+
catch {
|
|
313
|
+
// A temp folder that cannot be resolved is compared as named.
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
if (shared.includes(target))
|
|
317
|
+
return true;
|
|
318
|
+
if (process.platform === 'win32')
|
|
319
|
+
return false;
|
|
320
|
+
return /^(?:\/private)?\/(?:var\/)?tmp$/u.test(target) || /^(?:\/private)?\/var\/folders\/[^/]+\/[^/]+\/T$/u.test(target);
|
|
321
|
+
}
|
|
322
|
+
async function attach(workspace, id) {
|
|
323
|
+
if (workspace.sessionIds?.includes(id) === true)
|
|
324
|
+
return true;
|
|
325
|
+
try {
|
|
326
|
+
await workspace.attachSession(id);
|
|
327
|
+
return true;
|
|
328
|
+
}
|
|
329
|
+
catch {
|
|
330
|
+
return false;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Resume the session the way opening it in the browser does (`sessionController.resolveAgent`), which also
|
|
335
|
+
* announces it to the session list; without the controller (a headless profile), through `agents.resume`.
|
|
336
|
+
*/
|
|
337
|
+
async function makeLive(ctx, id) {
|
|
338
|
+
const agents = dshService(ctx, 'agents');
|
|
339
|
+
const live = () => agents?.get?.(id) !== undefined;
|
|
340
|
+
if (live())
|
|
341
|
+
return true;
|
|
342
|
+
const controller = dshService(ctx, 'sessionController');
|
|
343
|
+
try {
|
|
344
|
+
if (typeof controller?.resolveAgent === 'function') {
|
|
345
|
+
const result = await controller.resolveAgent(id);
|
|
346
|
+
if (result.error === undefined)
|
|
347
|
+
return true;
|
|
348
|
+
}
|
|
349
|
+
else if (typeof agents?.resume === 'function') {
|
|
350
|
+
const defaults = dshService(ctx, 'agentDefaultModel');
|
|
351
|
+
await agents.resume({
|
|
352
|
+
resumeSessionId: id,
|
|
353
|
+
...(defaults !== undefined
|
|
354
|
+
? {
|
|
355
|
+
agentOptions: {
|
|
356
|
+
get provider() { return defaults.currentSelection().provider; },
|
|
357
|
+
get model() { return defaults.currentSelection().model; },
|
|
358
|
+
get reasoningEffort() { return defaults.currentSelection().reasoningEffort; },
|
|
359
|
+
},
|
|
360
|
+
}
|
|
361
|
+
: {}),
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
catch {
|
|
366
|
+
// The session is stored either way; it becomes live when it is opened after a restart.
|
|
367
|
+
}
|
|
368
|
+
return live();
|
|
369
|
+
}
|
|
370
|
+
function isDirectory(path) {
|
|
371
|
+
try {
|
|
372
|
+
return statSync(path).isDirectory();
|
|
373
|
+
}
|
|
374
|
+
catch {
|
|
375
|
+
return false;
|
|
376
|
+
}
|
|
377
|
+
}
|
package/lib/sessions/row.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The sessions row: Claude Code, Codex and OpenCode sessions imported into
|
|
3
|
-
* DSH on demand, from the workbench. It holds no listener.
|
|
3
|
+
* DSH on demand, from the workbench. It holds no listener. While
|
|
4
|
+
* dsh-chat-import runs, it yields: session import is that plugin's.
|
|
4
5
|
* @module @kirkchinese/harness-forge/sessions
|
|
5
6
|
*/
|
|
6
7
|
import type { Context } from '@deepseek-ai/cordis';
|
package/lib/sessions/row.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MAX_SESSION_BYTES } from '@kirkchinese/harness-forge-core';
|
|
2
|
+
import { defaultCodexSessionsRoot, discoverCodexSessions, importCodexSession, rolloutIdentity } from './codex.js';
|
|
3
|
+
import { codexThreads } from './codex-discover.js';
|
|
2
4
|
import { FORGE_SERVICE } from '../forge/service.js';
|
|
3
|
-
import { discoverOpenCodeSessions, importOpenCodeSession } from './opencode.js';
|
|
4
|
-
import {
|
|
5
|
+
import { discoverOpenCodeSessions, importOpenCodeSession, openCodeIdentity } from './opencode.js';
|
|
6
|
+
import { matchStored, openStored, storedSessions } from './persist.js';
|
|
7
|
+
import { claudeIdentity, discoverClaudeSessions, importClaudeSession } from './claude.js';
|
|
5
8
|
import { rowEffect } from '../forge/row.js';
|
|
9
|
+
import { chatImportRunning, YIELD_MESSAGE } from './chat-import.js';
|
|
6
10
|
export const name = 'harness-forge-sessions';
|
|
7
11
|
export const inject = [FORGE_SERVICE];
|
|
8
12
|
/** The sessions row. */
|
|
@@ -12,14 +16,55 @@ export function apply(ctx) {
|
|
|
12
16
|
/** Start the sessions row on the shared service. Returns its teardown. */
|
|
13
17
|
export function startSessions(ctx, forge) {
|
|
14
18
|
const api = {
|
|
15
|
-
sessions: async (root, source) => {
|
|
16
|
-
if (
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
sessions: async (root, source, page) => {
|
|
20
|
+
if (chatImportRunning(ctx))
|
|
21
|
+
throw new Error(YIELD_MESSAGE);
|
|
22
|
+
// The ones DSH already holds: found the way an import finds them, under the id of this version or of earlier ones.
|
|
23
|
+
const stored = await storedSessions(ctx);
|
|
24
|
+
const known = { threads: sharedThreads() };
|
|
25
|
+
// One row at a time: rows that compare turns share the stored session's reader.
|
|
26
|
+
const matched = new Map();
|
|
27
|
+
const matchOf = (session) => {
|
|
28
|
+
let match = matched.get(session.sourceId);
|
|
29
|
+
if (match === undefined) {
|
|
30
|
+
match = matchStored(identityOf(source, session, known), stored);
|
|
31
|
+
matched.set(session.sourceId, match);
|
|
32
|
+
}
|
|
33
|
+
return match;
|
|
34
|
+
};
|
|
35
|
+
let found;
|
|
36
|
+
const matches = [];
|
|
37
|
+
try {
|
|
38
|
+
// A subagent's thread an earlier version imported is listed anyway: that copy has to stay reachable.
|
|
39
|
+
found = source === 'codex'
|
|
40
|
+
? await discoverCodexSessions(root ?? defaultCodexSessionsRoot(), page, async (subagent) => (await matchOf(subagent)).kind === 'imported')
|
|
41
|
+
: source === 'opencode'
|
|
42
|
+
? await discoverOpenCodeSessions(root, page)
|
|
43
|
+
: await discoverClaudeSessions(root, page);
|
|
44
|
+
for (const session of found.sessions)
|
|
45
|
+
matches.push(await matchOf(session));
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
await stored.close();
|
|
49
|
+
}
|
|
50
|
+
const sessions = found.sessions.map((session, index) => {
|
|
51
|
+
const match = matches[index];
|
|
52
|
+
// Too large to import: said here so the list can show why its button is off. A compressed rollout's decoded size is not known until it is read.
|
|
53
|
+
const tooLarge = session.bytes !== undefined && session.bytes > MAX_SESSION_BYTES;
|
|
54
|
+
return {
|
|
55
|
+
...session,
|
|
56
|
+
source,
|
|
57
|
+
...(tooLarge ? { tooLarge, limit: MAX_SESSION_BYTES } : {}),
|
|
58
|
+
...(match.kind === 'imported' ? { importedId: match.id } : {}),
|
|
59
|
+
// Stored under an id it may hold, and nothing tells: the person opens that one, or imports this one as `newId`.
|
|
60
|
+
...(match.kind === 'maybe' ? { maybeImportedId: match.id, newId: match.newId } : {}),
|
|
61
|
+
};
|
|
62
|
+
});
|
|
63
|
+
return { sessions, ...(found.next !== undefined ? { next: found.next } : {}) };
|
|
21
64
|
},
|
|
22
65
|
importSession: (path, targetId, source, sourceId) => {
|
|
66
|
+
if (chatImportRunning(ctx))
|
|
67
|
+
throw new Error(YIELD_MESSAGE);
|
|
23
68
|
const options = targetId === undefined ? {} : { targetId };
|
|
24
69
|
if (source === 'codex')
|
|
25
70
|
return importCodexSession(ctx, path, options);
|
|
@@ -30,6 +75,27 @@ export function startSessions(ctx, forge) {
|
|
|
30
75
|
}
|
|
31
76
|
return importClaudeSession(ctx, path, options);
|
|
32
77
|
},
|
|
78
|
+
open: id => openStored(ctx, id),
|
|
33
79
|
};
|
|
34
80
|
return forge.provide('sessions', api);
|
|
35
81
|
}
|
|
82
|
+
/** How a listed session is found among the stored ones: the identity its import uses. */
|
|
83
|
+
function identityOf(source, session, known) {
|
|
84
|
+
if (source === 'codex')
|
|
85
|
+
return rolloutIdentity(session.path, { id: session.sourceId, sessionId: session.parentId ?? session.sourceId }, session.createdAt ?? 0, known);
|
|
86
|
+
if (source === 'opencode')
|
|
87
|
+
return openCodeIdentity(session.sourceId);
|
|
88
|
+
return claudeIdentity(session.sourceId);
|
|
89
|
+
}
|
|
90
|
+
/** The threads under a sessions folder, read once for all the rows of one list. */
|
|
91
|
+
function sharedThreads() {
|
|
92
|
+
const read = new Map();
|
|
93
|
+
return (root) => {
|
|
94
|
+
let threads = read.get(root);
|
|
95
|
+
if (threads === undefined) {
|
|
96
|
+
threads = codexThreads(root);
|
|
97
|
+
read.set(root, threads);
|
|
98
|
+
}
|
|
99
|
+
return threads;
|
|
100
|
+
};
|
|
101
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turns as a stored session and a source session are compared, to tell which source session a stored one holds:
|
|
3
|
+
* what the person said and what the assistant answered, in text, read one turn at a time and only as far as the
|
|
4
|
+
* comparison needs.
|
|
5
|
+
* @module @kirkchinese/harness-forge
|
|
6
|
+
*/
|
|
7
|
+
/** One turn, as compared: the person's prompt and the assistant's text answer, whitespace aside. */
|
|
8
|
+
export interface TurnText {
|
|
9
|
+
readonly prompt: string;
|
|
10
|
+
readonly reply: string;
|
|
11
|
+
}
|
|
12
|
+
/** The turns of one session, read in order and only as far as asked. */
|
|
13
|
+
export interface TurnReader {
|
|
14
|
+
/** Turn `index` (0-based); `undefined` past the last. Rejects when the session cannot be read. */
|
|
15
|
+
at(index: number): Promise<TurnText | undefined>;
|
|
16
|
+
/** Let go of what it reads from. */
|
|
17
|
+
close(): Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
/** How two sessions' turns compare: the same throughout, parting ways, or not known (unread, or one only begins like the other). */
|
|
20
|
+
export type TurnComparison = 'same' | 'different' | 'unknown';
|
|
21
|
+
/** One turn as compared, from what it says. */
|
|
22
|
+
export declare function turnText(prompt: string, replies: readonly string[]): TurnText;
|
|
23
|
+
/** The text of the assistant's answer in one step's content: its `text` blocks (not its reasoning nor its tool calls). */
|
|
24
|
+
export declare function replyText(content: readonly unknown[]): string[];
|
|
25
|
+
/**
|
|
26
|
+
* A reader over turns handed out one by one (a generator that reads its source as it goes); the turns read are
|
|
27
|
+
* kept, so several comparisons read the source once.
|
|
28
|
+
*/
|
|
29
|
+
export declare function turnReader(source: AsyncIterator<TurnText, unknown>): TurnReader;
|
|
30
|
+
/** Compare two sessions turn by turn, prompt and reply, stopping at the first turn that differs. */
|
|
31
|
+
export declare function compareTurns(a: TurnReader, b: TurnReader): Promise<TurnComparison>;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turns as a stored session and a source session are compared, to tell which source session a stored one holds:
|
|
3
|
+
* what the person said and what the assistant answered, in text, read one turn at a time and only as far as the
|
|
4
|
+
* comparison needs.
|
|
5
|
+
* @module @kirkchinese/harness-forge
|
|
6
|
+
*/
|
|
7
|
+
/** One turn as compared, from what it says. */
|
|
8
|
+
export function turnText(prompt, replies) {
|
|
9
|
+
return { prompt: collapse(prompt), reply: collapse(replies.join('\n')) };
|
|
10
|
+
}
|
|
11
|
+
function collapse(text) {
|
|
12
|
+
return text.trim().replace(/\s+/gu, ' ');
|
|
13
|
+
}
|
|
14
|
+
/** The text of the assistant's answer in one step's content: its `text` blocks (not its reasoning nor its tool calls). */
|
|
15
|
+
export function replyText(content) {
|
|
16
|
+
return content.flatMap((block) => {
|
|
17
|
+
const value = block;
|
|
18
|
+
return value?.type === 'text' && typeof value.text === 'string' ? [value.text] : [];
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* A reader over turns handed out one by one (a generator that reads its source as it goes); the turns read are
|
|
23
|
+
* kept, so several comparisons read the source once.
|
|
24
|
+
*/
|
|
25
|
+
export function turnReader(source) {
|
|
26
|
+
const turns = [];
|
|
27
|
+
let done = false;
|
|
28
|
+
return {
|
|
29
|
+
async at(index) {
|
|
30
|
+
while (!done && turns.length <= index) {
|
|
31
|
+
const next = await source.next();
|
|
32
|
+
if (next.done === true)
|
|
33
|
+
done = true;
|
|
34
|
+
else
|
|
35
|
+
turns.push(next.value);
|
|
36
|
+
}
|
|
37
|
+
return turns[index];
|
|
38
|
+
},
|
|
39
|
+
async close() {
|
|
40
|
+
if (!done)
|
|
41
|
+
await source.return?.(undefined);
|
|
42
|
+
done = true;
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/** Compare two sessions turn by turn, prompt and reply, stopping at the first turn that differs. */
|
|
47
|
+
export async function compareTurns(a, b) {
|
|
48
|
+
try {
|
|
49
|
+
for (let index = 0;; index++) {
|
|
50
|
+
const [left, right] = await Promise.all([a.at(index), b.at(index)]);
|
|
51
|
+
if (left === undefined && right === undefined)
|
|
52
|
+
return index === 0 ? 'unknown' : 'same';
|
|
53
|
+
// One goes on past the other: it went on after the import, or the stored one did in DSH.
|
|
54
|
+
if (left === undefined || right === undefined)
|
|
55
|
+
return 'unknown';
|
|
56
|
+
if (left.prompt !== right.prompt || left.reply !== right.reply)
|
|
57
|
+
return 'different';
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return 'unknown';
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** Whether this Node can read compressed rollouts (`zlib.createZstdDecompress`, Node 22.15+). */
|
|
2
|
+
export declare function zstdAvailable(): boolean;
|
|
3
|
+
/**
|
|
4
|
+
* The first `maxBytes` of a zstd file's text, without decoding the rest: the compressed bytes are streamed through the
|
|
5
|
+
* decoder, which is dropped once enough text came out. A stream cut inside a frame (the read stops early) is fine,
|
|
6
|
+
* and so is the decoder's complaint about it.
|
|
7
|
+
*/
|
|
8
|
+
export declare function decodeZstdHead(compressed: Uint8Array, maxBytes: number): Promise<string>;
|
|
9
|
+
/**
|
|
10
|
+
* The decoded bytes of a zstd file, streamed. Node's decoder stops after the first frame of a stream just as
|
|
11
|
+
* the one-shot decoder does, so the frames are found by walking their headers and block sizes as the file is read,
|
|
12
|
+
* and each is decoded by a decoder of its own: nothing but one frame's current block is held. Throws on a malformed
|
|
13
|
+
* or truncated file, naming `path`.
|
|
14
|
+
*/
|
|
15
|
+
export declare function zstdChunks(path: string): AsyncGenerator<Uint8Array>;
|