@lukstei/dsh-session-handoff 0.2.2-rc.5.7 → 0.2.2-rc.5.9
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 +25 -20
- package/dist/client.js +48 -5
- package/dist/index.js +210 -106
- package/package.json +9 -3
- package/src/client/index.ts +49 -3
- package/src/client/ring.ts +29 -2
- package/src/handoff.ts +196 -86
- package/src/index.ts +104 -74
package/src/handoff.ts
CHANGED
|
@@ -11,14 +11,26 @@ const ASK_HEAD = 500;
|
|
|
11
11
|
const ASK_TAIL = 300;
|
|
12
12
|
const OUTCOME_LIMIT = 240;
|
|
13
13
|
const FAILURE_LIMIT = 200;
|
|
14
|
-
const
|
|
14
|
+
const PATH_LIMIT = 300;
|
|
15
|
+
const TOOL_NAME_LIMIT = 80;
|
|
15
16
|
const RECENT_TURNS = 5;
|
|
16
17
|
const RECENT_HEAD = 1200;
|
|
17
18
|
const RECENT_TAIL = 400;
|
|
19
|
+
/**
|
|
20
|
+
* Bounds that keep a brief small however long the handoff chain is: each section
|
|
21
|
+
* keeps only its newest entries and names how many it dropped.
|
|
22
|
+
*/
|
|
23
|
+
const OLDER_TURNS = 30;
|
|
24
|
+
const FAILURE_MAX = 20;
|
|
25
|
+
const ARTIFACT_MAX = 100;
|
|
26
|
+
const SHORT_ID_LENGTH = 8;
|
|
18
27
|
|
|
19
28
|
/** Tool argument keys that carry a durable file path. */
|
|
20
29
|
const PATH_KEYS = ["path", "file_path", "filePath", "notebook_path"];
|
|
21
30
|
|
|
31
|
+
/** The first line of every handoff follow-up; names the parent session and its brief. */
|
|
32
|
+
const INSTRUCTION_LINE = /^The handoff brief for session (\S+) is at (.+)\.$/;
|
|
33
|
+
|
|
22
34
|
export interface RenderedHandoff {
|
|
23
35
|
readonly markdown: string;
|
|
24
36
|
readonly turns: number;
|
|
@@ -26,90 +38,158 @@ export interface RenderedHandoff {
|
|
|
26
38
|
|
|
27
39
|
interface TurnArc {
|
|
28
40
|
readonly turn: number;
|
|
41
|
+
readonly sessionId: string;
|
|
42
|
+
/** Running number across the whole chain; assigned after collection. */
|
|
43
|
+
index: number;
|
|
29
44
|
ask: string;
|
|
30
45
|
outcome: string;
|
|
31
46
|
reason: TurnEndReason | undefined;
|
|
47
|
+
/** The turn only read a parent brief; it carries no work of its own. */
|
|
48
|
+
isHandoff: boolean;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
interface Failure {
|
|
52
|
+
readonly arc: TurnArc;
|
|
53
|
+
readonly text: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Id of the session whose brief this session was started from, read from the
|
|
58
|
+
* handoff follow-up it received.
|
|
59
|
+
*/
|
|
60
|
+
export function parentSessionId(
|
|
61
|
+
snapshot: SessionLogSnapshot,
|
|
62
|
+
): string | undefined {
|
|
63
|
+
for (const event of snapshot.events) {
|
|
64
|
+
if (event.type !== "user/message" || event.data.source.kind !== "user")
|
|
65
|
+
continue;
|
|
66
|
+
const text = blockText(event.data.content);
|
|
67
|
+
if (text === "") continue;
|
|
68
|
+
// Only the session's first user message can be the handoff follow-up.
|
|
69
|
+
const match = INSTRUCTION_LINE.exec(text.split("\n", 1)[0]);
|
|
70
|
+
return match === null ? undefined : match[1];
|
|
71
|
+
}
|
|
72
|
+
return undefined;
|
|
32
73
|
}
|
|
33
74
|
|
|
34
|
-
|
|
75
|
+
/**
|
|
76
|
+
* Renders one brief for a handoff chain.
|
|
77
|
+
* @param snapshots - Session logs, oldest ancestor first, the handed-off session last.
|
|
78
|
+
*/
|
|
79
|
+
export function renderHandoff(
|
|
80
|
+
snapshots: readonly SessionLogSnapshot[],
|
|
81
|
+
): RenderedHandoff {
|
|
35
82
|
const turns: TurnArc[] = [];
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
const artifacts
|
|
39
|
-
const
|
|
40
|
-
const seenArtifacts = new Set<string>();
|
|
41
|
-
let open: TurnArc | undefined;
|
|
83
|
+
const failures: Failure[] = [];
|
|
84
|
+
// A Map keeps insertion order; re-inserting moves a repeated path to its newest use.
|
|
85
|
+
const artifacts = new Map<string, true>();
|
|
86
|
+
const briefFiles = new Set<string>();
|
|
42
87
|
let goal = "";
|
|
88
|
+
let eventCount = 0;
|
|
43
89
|
|
|
44
|
-
for (const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
90
|
+
for (const snapshot of snapshots) {
|
|
91
|
+
const sessionId = String(snapshot.session.id);
|
|
92
|
+
const byId = new Map<number, TurnArc>();
|
|
93
|
+
const callNames = new Map<string, string>();
|
|
94
|
+
let open: TurnArc | undefined;
|
|
95
|
+
let sawUserMessage = false;
|
|
96
|
+
eventCount += snapshot.events.length;
|
|
97
|
+
|
|
98
|
+
for (const event of snapshot.events) {
|
|
99
|
+
switch (event.type) {
|
|
100
|
+
case "turn/start": {
|
|
101
|
+
const opened: TurnArc = {
|
|
102
|
+
turn: event.data.turn,
|
|
103
|
+
sessionId,
|
|
104
|
+
index: 0,
|
|
105
|
+
ask: "",
|
|
106
|
+
outcome: "",
|
|
107
|
+
reason: undefined,
|
|
108
|
+
isHandoff: false,
|
|
109
|
+
};
|
|
110
|
+
turns.push(opened);
|
|
111
|
+
byId.set(event.data.turn, opened);
|
|
112
|
+
// edge case: `user/message` carries no turn number, so it is attributed
|
|
113
|
+
// to the most recently opened turn.
|
|
114
|
+
open = opened;
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
case "user/message": {
|
|
118
|
+
if (event.data.source.kind !== "user") break;
|
|
119
|
+
const text = blockText(event.data.content);
|
|
120
|
+
if (text === "") break;
|
|
121
|
+
const instruction = sawUserMessage
|
|
122
|
+
? null
|
|
123
|
+
: INSTRUCTION_LINE.exec(text.split("\n", 1)[0]);
|
|
124
|
+
sawUserMessage = true;
|
|
125
|
+
if (instruction !== null) {
|
|
126
|
+
briefFiles.add(instruction[2]);
|
|
127
|
+
if (open !== undefined) open.isHandoff = true;
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
if (goal === "") goal = elide(flatten(text), ASK_HEAD, ASK_TAIL);
|
|
131
|
+
if (open !== undefined && open.ask === "") open.ask = text;
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
case "assistant/message": {
|
|
135
|
+
const arc = byId.get(event.data.turn);
|
|
136
|
+
if (arc === undefined) break;
|
|
137
|
+
// The last text-bearing step of a turn is the answer the user saw.
|
|
138
|
+
arc.outcome = blockText(event.data.message.content);
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
case "turn/end": {
|
|
142
|
+
const arc = byId.get(event.data.turn);
|
|
143
|
+
if (arc === undefined) break;
|
|
144
|
+
arc.reason = event.data.reason;
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
case "tool/call": {
|
|
148
|
+
const arc = byId.get(event.data.turn);
|
|
149
|
+
callNames.set(event.data.callId, event.data.name);
|
|
150
|
+
if (arc === undefined || arc.isHandoff) break;
|
|
151
|
+
collectArtifacts(event.data.arguments, artifacts, briefFiles);
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
case "tool/result": {
|
|
155
|
+
const arc = byId.get(event.data.turn);
|
|
156
|
+
if (event.data.message.isError !== true || arc === undefined) break;
|
|
157
|
+
if (arc.isHandoff) break;
|
|
158
|
+
const callId = event.data.message.toolCallId;
|
|
159
|
+
const name = callNames.get(callId);
|
|
160
|
+
failures.push({
|
|
161
|
+
arc,
|
|
162
|
+
text: `tool \`${clip(name === undefined ? callId : name, TOOL_NAME_LIMIT)}\`: ${clip(errorReason(event.data.error), FAILURE_LIMIT)}`,
|
|
163
|
+
});
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
default:
|
|
167
|
+
break;
|
|
97
168
|
}
|
|
98
|
-
default:
|
|
99
|
-
break;
|
|
100
169
|
}
|
|
101
170
|
}
|
|
102
171
|
|
|
103
|
-
const
|
|
104
|
-
|
|
172
|
+
const work = turns.filter((turn) => !turn.isHandoff);
|
|
173
|
+
work.forEach((turn, index) => {
|
|
174
|
+
turn.index = index + 1;
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
const current = snapshots[snapshots.length - 1];
|
|
178
|
+
const id = String(current.session.id);
|
|
179
|
+
const cwd = current.session.cwd;
|
|
105
180
|
const lines = [
|
|
106
181
|
`# Handoff — ${id}`,
|
|
107
182
|
"",
|
|
108
183
|
`- session: \`${id}\``,
|
|
184
|
+
...(snapshots.length > 1
|
|
185
|
+
? [
|
|
186
|
+
`- chain: ${snapshots.map((snapshot) => `\`${shortId(String(snapshot.session.id))}\``).join(" → ")}`,
|
|
187
|
+
]
|
|
188
|
+
: []),
|
|
109
189
|
`- cwd: \`${cwd === undefined ? "unknown" : cwd}\``,
|
|
110
|
-
`- created: ${new Date(
|
|
111
|
-
`- events: ${
|
|
112
|
-
`- turns: ${
|
|
190
|
+
`- created: ${new Date(current.session.createdAt).toISOString()}`,
|
|
191
|
+
`- events: ${eventCount}`,
|
|
192
|
+
`- turns: ${work.length}`,
|
|
113
193
|
"",
|
|
114
194
|
"## Goal",
|
|
115
195
|
"",
|
|
@@ -117,26 +197,33 @@ export function renderHandoff(snapshot: SessionLogSnapshot): RenderedHandoff {
|
|
|
117
197
|
"",
|
|
118
198
|
"## Turns",
|
|
119
199
|
"",
|
|
120
|
-
|
|
121
|
-
? "none"
|
|
122
|
-
: turns
|
|
123
|
-
.map((turn, index) =>
|
|
124
|
-
turnBlock(turn, index >= turns.length - RECENT_TURNS),
|
|
125
|
-
)
|
|
126
|
-
.join("\n\n"),
|
|
200
|
+
work.length === 0 ? "none" : turnList(work),
|
|
127
201
|
"",
|
|
128
202
|
"## Failures",
|
|
129
203
|
"",
|
|
130
|
-
failures.length === 0
|
|
204
|
+
failures.length === 0
|
|
205
|
+
? "none"
|
|
206
|
+
: bounded(
|
|
207
|
+
failures.map(
|
|
208
|
+
({ arc, text }) =>
|
|
209
|
+
`- **T${arc.index}**${arc.sessionId === id ? "" : ` (${shortId(arc.sessionId)})`} ${text}`,
|
|
210
|
+
),
|
|
211
|
+
FAILURE_MAX,
|
|
212
|
+
"older failures",
|
|
213
|
+
),
|
|
131
214
|
"",
|
|
132
215
|
"## Artifacts",
|
|
133
216
|
"",
|
|
134
|
-
artifacts.
|
|
217
|
+
artifacts.size === 0
|
|
135
218
|
? "none"
|
|
136
|
-
:
|
|
219
|
+
: bounded(
|
|
220
|
+
[...artifacts.keys()].map((file) => `- \`${file}\``),
|
|
221
|
+
ARTIFACT_MAX,
|
|
222
|
+
"older paths",
|
|
223
|
+
),
|
|
137
224
|
];
|
|
138
225
|
|
|
139
|
-
return { markdown: `${lines.join("\n")}\n`, turns:
|
|
226
|
+
return { markdown: `${lines.join("\n")}\n`, turns: work.length };
|
|
140
227
|
}
|
|
141
228
|
|
|
142
229
|
export function handoffInstruction(sessionId: string, file: string): string {
|
|
@@ -152,6 +239,28 @@ export function handoffInstruction(sessionId: string, file: string): string {
|
|
|
152
239
|
].join("\n");
|
|
153
240
|
}
|
|
154
241
|
|
|
242
|
+
/** Last RECENT_TURNS turns quoted at length, OLDER_TURNS before them flattened, the rest counted. */
|
|
243
|
+
function turnList(work: readonly TurnArc[]): string {
|
|
244
|
+
const kept = work.slice(-(RECENT_TURNS + OLDER_TURNS));
|
|
245
|
+
const dropped = work.length - kept.length;
|
|
246
|
+
const blocks = kept.map((turn, index) =>
|
|
247
|
+
turnBlock(turn, index >= kept.length - RECENT_TURNS),
|
|
248
|
+
);
|
|
249
|
+
if (dropped > 0) blocks.unshift(`… ${dropped} earlier turns omitted`);
|
|
250
|
+
return blocks.join("\n\n");
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** Keeps the newest `max` lines, naming how many older ones were dropped. */
|
|
254
|
+
function bounded(lines: string[], max: number, noun: string): string {
|
|
255
|
+
const dropped = lines.length - max;
|
|
256
|
+
if (dropped <= 0) return lines.join("\n");
|
|
257
|
+
return [`- … ${dropped} ${noun} omitted`, ...lines.slice(-max)].join("\n");
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function shortId(id: string): string {
|
|
261
|
+
return id.slice(0, SHORT_ID_LENGTH);
|
|
262
|
+
}
|
|
263
|
+
|
|
155
264
|
/** Recent turns quote the ask and reply at length; older turns are flattened and short. */
|
|
156
265
|
function turnBlock(turn: TurnArc, recent: boolean): string {
|
|
157
266
|
const kind = turn.reason === undefined ? "unfinished" : turn.reason.kind;
|
|
@@ -168,7 +277,7 @@ function turnBlock(turn: TurnArc, recent: boolean): string {
|
|
|
168
277
|
)
|
|
169
278
|
.filter((part) => part !== "")
|
|
170
279
|
.join("\n\n");
|
|
171
|
-
return `### T${turn.
|
|
280
|
+
return `### T${turn.index} [${kind}]\n\n${body === "" ? "(no content)" : body}`;
|
|
172
281
|
}
|
|
173
282
|
|
|
174
283
|
function blockText(blocks: readonly ContentBlock[]): string {
|
|
@@ -211,8 +320,8 @@ function errorReason(
|
|
|
211
320
|
|
|
212
321
|
function collectArtifacts(
|
|
213
322
|
args: string,
|
|
214
|
-
into: string
|
|
215
|
-
|
|
323
|
+
into: Map<string, true>,
|
|
324
|
+
briefFiles: ReadonlySet<string>,
|
|
216
325
|
): void {
|
|
217
326
|
let parsed: unknown;
|
|
218
327
|
try {
|
|
@@ -227,9 +336,10 @@ function collectArtifacts(
|
|
|
227
336
|
const record = parsed as Record<string, unknown>;
|
|
228
337
|
for (const key of PATH_KEYS) {
|
|
229
338
|
const value = record[key];
|
|
230
|
-
if (typeof value !== "string" || value === "" ||
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
339
|
+
if (typeof value !== "string" || value === "" || briefFiles.has(value))
|
|
340
|
+
continue;
|
|
341
|
+
const path = clip(value, PATH_LIMIT);
|
|
342
|
+
into.delete(path);
|
|
343
|
+
into.set(path, true);
|
|
234
344
|
}
|
|
235
345
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { mkdir, writeFile } from "node:fs/promises";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import type { Context } from "@deepseek-ai/cordis";
|
|
4
|
-
import type { Agent } from "@deepseek-ai/dsh-agent";
|
|
4
|
+
import type { Agent, ModelSelection } from "@deepseek-ai/dsh-agent";
|
|
5
5
|
import type { SessionCreateValue } from "@deepseek-ai/dsh-api-session-controller";
|
|
6
6
|
import type {
|
|
7
7
|
CommandInvocation,
|
|
@@ -10,15 +10,16 @@ import type {
|
|
|
10
10
|
import { createUserMessage } from "@deepseek-ai/dsh-llm";
|
|
11
11
|
import type {} from "@deepseek-ai/dsh-session";
|
|
12
12
|
import type { SessionId } from "@deepseek-ai/dsh-session/types";
|
|
13
|
-
import type {
|
|
14
|
-
SessionLogSnapshot,
|
|
15
|
-
SessionRecord,
|
|
16
|
-
} from "@deepseek-ai/dsh-session-query";
|
|
13
|
+
import type { SessionLogSnapshot } from "@deepseek-ai/dsh-session-query";
|
|
17
14
|
import type {} from "@deepseek-ai/dsh-session-title";
|
|
18
15
|
import type {} from "@deepseek-ai/dsh-working-directory";
|
|
19
16
|
import type { Workspace } from "@deepseek-ai/dsh-workspace";
|
|
20
17
|
import { scratchDirectory } from "@lukstei/dsh-scratch-dir";
|
|
21
|
-
import {
|
|
18
|
+
import {
|
|
19
|
+
handoffInstruction,
|
|
20
|
+
parentSessionId,
|
|
21
|
+
renderHandoff,
|
|
22
|
+
} from "./handoff.ts";
|
|
22
23
|
import { formatLaunchResult } from "./launch.ts";
|
|
23
24
|
|
|
24
25
|
export const name = "session-handoff";
|
|
@@ -29,10 +30,10 @@ export const inject = [
|
|
|
29
30
|
"sessionController",
|
|
30
31
|
"sessionTitle",
|
|
31
32
|
"workspaceRegistry",
|
|
33
|
+
"llm",
|
|
32
34
|
];
|
|
33
35
|
|
|
34
36
|
const HANDOFF_FILE = "HANDOFF.md";
|
|
35
|
-
const RECENT_LIMIT = 5;
|
|
36
37
|
const SHORT_ID_LENGTH = 8;
|
|
37
38
|
const RESUMED_SUFFIX = " (Resumed)";
|
|
38
39
|
|
|
@@ -40,26 +41,51 @@ export function apply(ctx: Context): void {
|
|
|
40
41
|
ctx.commands.register({
|
|
41
42
|
name: "handoff",
|
|
42
43
|
description:
|
|
43
|
-
"Hand this session off to a new one
|
|
44
|
+
"Hand this session off to a new one (bare: pick the model from a list)",
|
|
44
45
|
// edge case: without a declared input the client sends the raw line to the
|
|
45
46
|
// model as a prompt and this handler never runs.
|
|
46
|
-
input: { hint: "[
|
|
47
|
+
input: { hint: "[provider/model]" },
|
|
47
48
|
handler: (invocation: CommandInvocation): Promise<CommandResult> =>
|
|
48
49
|
handoff(ctx, invocation),
|
|
49
50
|
});
|
|
50
51
|
}
|
|
51
52
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
);
|
|
59
|
-
if (
|
|
60
|
-
return
|
|
61
|
-
|
|
62
|
-
|
|
53
|
+
/**
|
|
54
|
+
* Split `provider/model` at the first slash: provider ids have none, model ids may.
|
|
55
|
+
* @internal exported for the unit test only.
|
|
56
|
+
*/
|
|
57
|
+
export function parseModelArg(raw: string): ModelSelection | undefined {
|
|
58
|
+
const text = raw.trim();
|
|
59
|
+
const slash = text.indexOf("/");
|
|
60
|
+
if (slash <= 0 || slash === text.length - 1) return undefined;
|
|
61
|
+
return { provider: text.slice(0, slash), model: text.slice(slash + 1) };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Check the route against the registered providers and the provider's own model
|
|
66
|
+
* resolution, the same check subagent routes get, so a typo fails here and not
|
|
67
|
+
* on the new session's first turn.
|
|
68
|
+
* @returns an error text, or `undefined` when the route resolves.
|
|
69
|
+
*/
|
|
70
|
+
async function modelError(
|
|
71
|
+
ctx: Context,
|
|
72
|
+
model: ModelSelection,
|
|
73
|
+
signal: AbortSignal,
|
|
74
|
+
): Promise<string | undefined> {
|
|
75
|
+
try {
|
|
76
|
+
const providers = ctx.llm.listProviders();
|
|
77
|
+
if (!providers.some((provider) => provider.id === model.provider)) {
|
|
78
|
+
const available = providers.map((provider) => provider.id).join(", ");
|
|
79
|
+
return `LLM provider '${model.provider}' is not registered; available providers: ${available || "(none)"}`;
|
|
80
|
+
}
|
|
81
|
+
await ctx.llm.resolveCallConfig(
|
|
82
|
+
{ provider: model.provider, model: model.model },
|
|
83
|
+
signal,
|
|
84
|
+
);
|
|
85
|
+
return undefined;
|
|
86
|
+
} catch (error: unknown) {
|
|
87
|
+
return error instanceof Error ? error.message : String(error);
|
|
88
|
+
}
|
|
63
89
|
}
|
|
64
90
|
|
|
65
91
|
async function handoff(
|
|
@@ -67,72 +93,45 @@ async function handoff(
|
|
|
67
93
|
invocation: CommandInvocation,
|
|
68
94
|
): Promise<CommandResult> {
|
|
69
95
|
const raw = invocation.rawInput.trim();
|
|
70
|
-
// edge case: no
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
let targetId: SessionId;
|
|
76
|
-
if (launch) {
|
|
77
|
-
targetId = agent.session.id;
|
|
78
|
-
} else {
|
|
79
|
-
const sessions = await ctx.sessionQuery.listSessions(invocation.signal);
|
|
80
|
-
const matches = resolveHandoffTarget(sessions, raw);
|
|
81
|
-
if (matches.length === 0) {
|
|
82
|
-
const recent = sessions
|
|
83
|
-
.slice(0, RECENT_LIMIT)
|
|
84
|
-
.map((session) => `- ${String(session.header.id)}`)
|
|
85
|
-
.join("\n");
|
|
96
|
+
// edge case: no argument keeps the source's own model, the ring's path.
|
|
97
|
+
let model: ModelSelection | undefined;
|
|
98
|
+
if (raw !== "") {
|
|
99
|
+
model = parseModelArg(raw);
|
|
100
|
+
if (model === undefined) {
|
|
86
101
|
return {
|
|
87
102
|
kind: "error",
|
|
88
|
-
text: `
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
if (matches.length > 1) {
|
|
92
|
-
const candidates = matches
|
|
93
|
-
.map((session) => `- ${String(session.header.id)}`)
|
|
94
|
-
.join("\n");
|
|
95
|
-
return { kind: "error", text: `"${raw}" is ambiguous:\n${candidates}` };
|
|
96
|
-
}
|
|
97
|
-
targetId = matches[0].header.id;
|
|
98
|
-
if (targetId === agent.session.id) {
|
|
99
|
-
return {
|
|
100
|
-
kind: "error",
|
|
101
|
-
text: "That is this session; there is nothing to hand off.",
|
|
103
|
+
text: `Expected "<provider>/<model>", got "${raw}"; run bare /handoff to pick one.`,
|
|
102
104
|
};
|
|
103
105
|
}
|
|
106
|
+
const invalid = await modelError(ctx, model, invocation.signal);
|
|
107
|
+
if (invalid !== undefined) return { kind: "error", text: invalid };
|
|
104
108
|
}
|
|
109
|
+
const agent = invocation.agent;
|
|
110
|
+
const targetId = agent.session.id;
|
|
105
111
|
|
|
106
112
|
try {
|
|
107
113
|
const snapshot = await ctx.sessionQuery.readSession(targetId);
|
|
108
|
-
const rendered = renderHandoff(
|
|
114
|
+
const rendered = renderHandoff(
|
|
115
|
+
await readChain(ctx, snapshot, invocation.signal),
|
|
116
|
+
);
|
|
109
117
|
if (rendered.turns === 0) {
|
|
110
118
|
return {
|
|
111
119
|
kind: "error",
|
|
112
|
-
text:
|
|
113
|
-
? "This session has no turns to hand off."
|
|
114
|
-
: `Session ${shortId(targetId)} has no turns to hand off.`,
|
|
120
|
+
text: "This session has no turns to hand off.",
|
|
115
121
|
};
|
|
116
122
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
return {
|
|
127
|
-
kind: "success",
|
|
128
|
-
text: formatLaunchResult(created.sessionId),
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
const file = await deliver(ctx, agent, targetId, rendered.markdown);
|
|
123
|
+
const created = await createBeside(
|
|
124
|
+
ctx,
|
|
125
|
+
agent,
|
|
126
|
+
targetId,
|
|
127
|
+
rendered.markdown,
|
|
128
|
+
snapshot,
|
|
129
|
+
invocation.signal,
|
|
130
|
+
model,
|
|
131
|
+
);
|
|
133
132
|
return {
|
|
134
133
|
kind: "success",
|
|
135
|
-
text:
|
|
134
|
+
text: formatLaunchResult(created.sessionId),
|
|
136
135
|
};
|
|
137
136
|
} catch (error: unknown) {
|
|
138
137
|
return {
|
|
@@ -142,6 +141,35 @@ async function handoff(
|
|
|
142
141
|
}
|
|
143
142
|
}
|
|
144
143
|
|
|
144
|
+
/**
|
|
145
|
+
* The handed-off session preceded by every ancestor it was itself handed off from,
|
|
146
|
+
* oldest first, so the brief keeps what earlier briefs summarized.
|
|
147
|
+
*/
|
|
148
|
+
async function readChain(
|
|
149
|
+
ctx: Context,
|
|
150
|
+
snapshot: SessionLogSnapshot,
|
|
151
|
+
signal: AbortSignal,
|
|
152
|
+
): Promise<SessionLogSnapshot[]> {
|
|
153
|
+
const chain = [snapshot];
|
|
154
|
+
const seen = new Set([String(snapshot.session.id)]);
|
|
155
|
+
let parent = parentSessionId(snapshot);
|
|
156
|
+
if (parent === undefined) return chain;
|
|
157
|
+
// `readSession` has no typed not-found error, so deleted parents are found by id.
|
|
158
|
+
const known = new Set(
|
|
159
|
+
(await ctx.sessionQuery.listSessions(signal)).map((session) =>
|
|
160
|
+
String(session.header.id),
|
|
161
|
+
),
|
|
162
|
+
);
|
|
163
|
+
while (parent !== undefined && !seen.has(parent) && known.has(parent)) {
|
|
164
|
+
seen.add(parent);
|
|
165
|
+
// Safe: the id was just matched against the listed session ids.
|
|
166
|
+
const ancestor = await ctx.sessionQuery.readSession(parent as SessionId);
|
|
167
|
+
chain.unshift(ancestor);
|
|
168
|
+
parent = parentSessionId(ancestor);
|
|
169
|
+
}
|
|
170
|
+
return chain;
|
|
171
|
+
}
|
|
172
|
+
|
|
145
173
|
/**
|
|
146
174
|
* Write the brief into the agent's own scratch directory and queue the follow-up
|
|
147
175
|
* turn that tells it to read that file.
|
|
@@ -220,8 +248,8 @@ function shortId(id: string): string {
|
|
|
220
248
|
}
|
|
221
249
|
|
|
222
250
|
/**
|
|
223
|
-
* Create the resumed session beside this one: same workspace, same preset,
|
|
224
|
-
* model, the brief in its own scratch directory, and one follow-up turn that
|
|
251
|
+
* Create the resumed session beside this one: same workspace, same preset, the
|
|
252
|
+
* source's model or the given one, the brief in its own scratch directory, and one follow-up turn that
|
|
225
253
|
* tells it to read that brief.
|
|
226
254
|
* @param ctx - Host context.
|
|
227
255
|
* @param source - Session the command runs in.
|
|
@@ -229,6 +257,7 @@ function shortId(id: string): string {
|
|
|
229
257
|
* @param markdown - Rendered brief.
|
|
230
258
|
* @param snapshot - Source session log, the origin of the kanban links to copy.
|
|
231
259
|
* @param signal - Cancellation signal of the invoking command.
|
|
260
|
+
* @param model - Model the new session starts on; the source's own when absent.
|
|
232
261
|
* @returns the created session identity.
|
|
233
262
|
*/
|
|
234
263
|
async function createBeside(
|
|
@@ -238,8 +267,9 @@ async function createBeside(
|
|
|
238
267
|
markdown: string,
|
|
239
268
|
snapshot: SessionLogSnapshot,
|
|
240
269
|
signal: AbortSignal,
|
|
270
|
+
model: ModelSelection | undefined,
|
|
241
271
|
): Promise<SessionCreateValue> {
|
|
242
|
-
const selection = source.model.current();
|
|
272
|
+
const selection = model === undefined ? source.model.current() : model;
|
|
243
273
|
const agentPreset = source.session.header.agentPreset;
|
|
244
274
|
// edge case: a session created by cwd alone is attached to no workspace, so it
|
|
245
275
|
// would stay out of the sidebar tree until the next Host start.
|