edsger 0.65.0 → 0.66.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.
@@ -0,0 +1,32 @@
1
+ /**
2
+ * `edsger session-serve <channelId>` — long-lived conversational agent for one
3
+ * chat session.
4
+ *
5
+ * Where `session-turn` runs a single turn and exits, `session-serve` prepares
6
+ * the session context once (repo clone, MCP toolbelt, system prompt) and then
7
+ * stays alive, running one turn per command it reads from stdin. The SDK
8
+ * session id is kept in memory and carried into the next turn automatically, so
9
+ * the conversation (and prompt cache) stays warm without a per-turn cold start.
10
+ *
11
+ * Protocol — one JSON object per line on stdin:
12
+ * {"type":"turn"} process pending messages
13
+ * {"type":"run-finished","runId":"…","command":"…"} report a finished cli_* run
14
+ * {"type":"shutdown"} drain + exit cleanly
15
+ *
16
+ * Turns run strictly one at a time (a session never has two turns in flight).
17
+ * When stdin closes, the process exits after the current turn drains. Markers
18
+ * (session id, background cli runs) are written to stdout exactly as the
19
+ * one-shot command emits them, so the desktop's existing parsing is unchanged.
20
+ */
21
+ import { type SessionTurnCliOptions } from '../session-turn/index.js';
22
+ /**
23
+ * Emitted once the session context is built and the process is ready to accept
24
+ * turn commands on stdin. The desktop may use it to know the daemon is live;
25
+ * it is harmless to ignore.
26
+ */
27
+ export declare const SESSION_READY_MARKER = "__EDSGER_SESSION_READY__";
28
+ export type SessionServeCliOptions = Omit<SessionTurnCliOptions, 'runFinished'> & {
29
+ /** SDK session id to resume on the first turn (e.g. after a crash-restart). */
30
+ resumeSessionId?: string;
31
+ };
32
+ export declare function runSessionServeCommand(options: SessionServeCliOptions): Promise<void>;
@@ -0,0 +1,100 @@
1
+ /**
2
+ * `edsger session-serve <channelId>` — long-lived conversational agent for one
3
+ * chat session.
4
+ *
5
+ * Where `session-turn` runs a single turn and exits, `session-serve` prepares
6
+ * the session context once (repo clone, MCP toolbelt, system prompt) and then
7
+ * stays alive, running one turn per command it reads from stdin. The SDK
8
+ * session id is kept in memory and carried into the next turn automatically, so
9
+ * the conversation (and prompt cache) stays warm without a per-turn cold start.
10
+ *
11
+ * Protocol — one JSON object per line on stdin:
12
+ * {"type":"turn"} process pending messages
13
+ * {"type":"run-finished","runId":"…","command":"…"} report a finished cli_* run
14
+ * {"type":"shutdown"} drain + exit cleanly
15
+ *
16
+ * Turns run strictly one at a time (a session never has two turns in flight).
17
+ * When stdin closes, the process exits after the current turn drains. Markers
18
+ * (session id, background cli runs) are written to stdout exactly as the
19
+ * one-shot command emits them, so the desktop's existing parsing is unchanged.
20
+ */
21
+ import { createInterface } from 'node:readline';
22
+ import { logError, logInfo } from '../../utils/logger.js';
23
+ import { prepareSessionContext, runTurn, } from '../session-turn/index.js';
24
+ /**
25
+ * Emitted once the session context is built and the process is ready to accept
26
+ * turn commands on stdin. The desktop may use it to know the daemon is live;
27
+ * it is harmless to ignore.
28
+ */
29
+ export const SESSION_READY_MARKER = '__EDSGER_SESSION_READY__';
30
+ export async function runSessionServeCommand(options) {
31
+ const ctx = await prepareSessionContext(options);
32
+ process.stdout.write(`\n${SESSION_READY_MARKER}\n`);
33
+ logInfo(`Session ${ctx.channelId} ready — awaiting turns on stdin.`);
34
+ // Commands queue up here; we drain them one at a time so a session never has
35
+ // two turns running concurrently. `shuttingDown` stops accepting new work.
36
+ const queue = [];
37
+ let running = false;
38
+ let shuttingDown = false;
39
+ let stdinEnded = false;
40
+ const maybeExit = () => {
41
+ if ((shuttingDown || stdinEnded) && !running && queue.length === 0) {
42
+ logInfo(`Session ${ctx.channelId} shutting down.`);
43
+ process.exit(0);
44
+ }
45
+ };
46
+ const drain = async () => {
47
+ if (running) {
48
+ return;
49
+ }
50
+ running = true;
51
+ try {
52
+ while (queue.length > 0) {
53
+ const cmd = queue.shift();
54
+ if (cmd.type === 'shutdown') {
55
+ shuttingDown = true;
56
+ continue;
57
+ }
58
+ try {
59
+ if (cmd.type === 'run-finished' && cmd.runId) {
60
+ await runTurn(ctx, {
61
+ runFinished: { runId: cmd.runId, command: cmd.command },
62
+ });
63
+ }
64
+ else {
65
+ await runTurn(ctx);
66
+ }
67
+ }
68
+ catch (error) {
69
+ logError(`Turn failed: ${error instanceof Error ? error.message : String(error)}`);
70
+ }
71
+ }
72
+ }
73
+ finally {
74
+ running = false;
75
+ maybeExit();
76
+ }
77
+ };
78
+ const rl = createInterface({ input: process.stdin });
79
+ rl.on('line', (line) => {
80
+ const trimmed = line.trim();
81
+ if (!trimmed || shuttingDown) {
82
+ return;
83
+ }
84
+ let cmd;
85
+ try {
86
+ cmd = JSON.parse(trimmed);
87
+ }
88
+ catch {
89
+ logError(`Ignoring malformed command: ${trimmed}`);
90
+ return;
91
+ }
92
+ queue.push(cmd);
93
+ void drain();
94
+ });
95
+ // stdin closing is the desktop's normal "stop this session" signal.
96
+ rl.on('close', () => {
97
+ stdinEnded = true;
98
+ maybeExit();
99
+ });
100
+ }
@@ -12,6 +12,7 @@ export declare const SESSION_ID_MARKER = "__EDSGER_SESSION_ID__=";
12
12
  * to ask. The JSON payload is `{ run_id, pid, log_path, command }`.
13
13
  */
14
14
  export declare const CLI_RUN_MARKER = "__EDSGER_CLI_RUN__=";
15
+ import { loadExternalMcpServers } from 'edsger-tools';
15
16
  export interface SessionTurnCliOptions {
16
17
  channelId: string;
17
18
  productId: string;
@@ -30,4 +31,43 @@ export interface SessionTurnCliOptions {
30
31
  };
31
32
  verbose?: boolean;
32
33
  }
34
+ /**
35
+ * Everything a session needs to run turns, built once and reused. The daemon
36
+ * keeps a single instance alive across turns so repo clones, the MCP toolbelt,
37
+ * the system prompt, and the SDK session id all persist.
38
+ */
39
+ export interface SessionContext {
40
+ channelId: string;
41
+ productId: string;
42
+ repositoryIds: string[];
43
+ verbose: boolean;
44
+ sessionDir?: string;
45
+ systemPrompt: string;
46
+ sessionServer: any;
47
+ external: ReturnType<typeof loadExternalMcpServers>;
48
+ /** Latest SDK session id; carried into the next turn's `resume`. Mutable. */
49
+ sdkSessionId: string;
50
+ }
51
+ /**
52
+ * Build the one-time session context: clone repos, assemble the MCP toolbelt
53
+ * and external servers, and build the system prompt. Cheap to call once per
54
+ * session process; expensive to call per turn (which is why the daemon doesn't).
55
+ */
56
+ export declare function prepareSessionContext(options: SessionTurnCliOptions): Promise<SessionContext>;
57
+ /**
58
+ * Run a single agent turn against an already-prepared {@link SessionContext}.
59
+ * Mutates `ctx.sdkSessionId` so the next turn resumes the same SDK conversation
60
+ * (and prompt cache). Safe to call repeatedly for the life of the process.
61
+ */
62
+ export declare function runTurn(ctx: SessionContext, opts?: {
63
+ runFinished?: {
64
+ runId: string;
65
+ command?: string;
66
+ };
67
+ }): Promise<void>;
68
+ /**
69
+ * One-shot `session-turn`: prepare the context and run a single turn. Retained
70
+ * for callers that want the previous per-turn-process behaviour (and as the
71
+ * building block the daemon reuses).
72
+ */
33
73
  export declare function runSessionTurnCommand(options: SessionTurnCliOptions): Promise<void>;
@@ -23,6 +23,12 @@ export const CLI_RUN_MARKER = '__EDSGER_CLI_RUN__=';
23
23
  * to the channel and marks the messages processed. The agent decides what to
24
24
  * do (answer, generate stories/test cases, launch cli_* analyses, open PRs);
25
25
  * there is no fixed pipeline.
26
+ *
27
+ * The per-turn work is split into {@link prepareSessionContext} (one-time
28
+ * workspace clone + toolbelt + prompt build) and {@link runTurn} (one agent
29
+ * turn against that context). The one-shot `session-turn` command does both
30
+ * once; the long-lived `session-serve` daemon prepares the context once and
31
+ * calls {@link runTurn} repeatedly, keeping the SDK session warm across turns.
26
32
  */
27
33
  import { query } from '@anthropic-ai/claude-agent-sdk';
28
34
  import { buildSessionAgentOptions, buildSessionSystemPrompt, buildSessionUserPrompt, createSessionMcpServer, listActiveCliRuns, loadExternalMcpServers, SESSION_MAX_TURNS, } from 'edsger-tools';
@@ -53,54 +59,21 @@ async function prepareSessionWorkspace(opts) {
53
59
  repoScopeNote: workspace ? describeSessionRepos(workspace.repos) : '',
54
60
  };
55
61
  }
56
- export async function runSessionTurnCommand(options) {
57
- const { channelId, productId, repositoryIds = [], resumeSessionId, runFinished, verbose = false, } = options;
58
- // Emit the SDK session id so the desktop can persist it for the next turn.
59
- const emitSessionId = (id) => {
60
- if (id) {
61
- process.stdout.write(`\n${SESSION_ID_MARKER}${id}\n`);
62
- }
63
- };
64
- // Emit a marker for every background cli_* run still in flight, so the
65
- // desktop watcher can poll each to completion and re-engage the agent.
66
- const emitActiveCliRuns = () => {
67
- for (const run of listActiveCliRuns()) {
68
- const payload = JSON.stringify({
69
- run_id: run.run_id,
70
- pid: run.pid,
71
- log_path: run.log_path,
72
- command: run.command,
73
- });
74
- process.stdout.write(`\n${CLI_RUN_MARKER}${payload}\n`);
75
- }
76
- };
77
- // 1. Decide what drives this turn. A normal turn replays the channel's
78
- // pending human messages; a watcher-triggered follow-up replays a
79
- // synthetic prompt about the background run that just finished.
80
- let messages = [];
81
- if (runFinished) {
82
- logInfo(`Reporting completion of background run ${runFinished.runId} for session ${channelId}`);
83
- }
84
- else {
85
- const pendingResult = (await callMcpEndpoint('chat/messages/pending', {
86
- channel_id: channelId,
87
- }));
88
- messages = pendingResult.messages ?? [];
89
- if (messages.length === 0) {
90
- logInfo('No pending messages for this session — nothing to do.');
91
- return;
92
- }
93
- logInfo(`Processing ${messages.length} message(s) for session ${channelId}`);
94
- }
95
- // 2. Clone the product's in-scope repositories into a local session
96
- // directory so the agent's Read/Grep/Glob can inspect the real code.
62
+ /**
63
+ * Build the one-time session context: clone repos, assemble the MCP toolbelt
64
+ * and external servers, and build the system prompt. Cheap to call once per
65
+ * session process; expensive to call per turn (which is why the daemon doesn't).
66
+ */
67
+ export async function prepareSessionContext(options) {
68
+ const { channelId, productId, repositoryIds = [], resumeSessionId, verbose = false, } = options;
69
+ // Clone the product's in-scope repositories into a local session directory
70
+ // so the agent's Read/Grep/Glob can inspect the real code.
97
71
  const { sessionDir, repoScopeNote } = await prepareSessionWorkspace({
98
72
  channelId,
99
73
  productId,
100
74
  repositoryIds,
101
75
  verbose,
102
76
  });
103
- // 3. Build the session toolbelt + prompts.
104
77
  const deps = getToolDeps({
105
78
  verbose,
106
79
  context: { productId, channelId, repositoryIds },
@@ -118,38 +91,95 @@ export async function runSessionTurnCommand(options) {
118
91
  repoScopeNote,
119
92
  externalMcpNames: external.names,
120
93
  });
94
+ return {
95
+ channelId,
96
+ productId,
97
+ repositoryIds,
98
+ verbose,
99
+ sessionDir,
100
+ systemPrompt,
101
+ sessionServer,
102
+ external,
103
+ sdkSessionId: resumeSessionId ?? '',
104
+ };
105
+ }
106
+ /** Emit the SDK session id so the desktop can persist it for the next turn. */
107
+ function emitSessionId(id) {
108
+ if (id) {
109
+ process.stdout.write(`\n${SESSION_ID_MARKER}${id}\n`);
110
+ }
111
+ }
112
+ /**
113
+ * Emit a marker for every background cli_* run still in flight, so the desktop
114
+ * watcher can poll each to completion and re-engage the agent.
115
+ */
116
+ function emitActiveCliRuns() {
117
+ for (const run of listActiveCliRuns()) {
118
+ const payload = JSON.stringify({
119
+ run_id: run.run_id,
120
+ pid: run.pid,
121
+ log_path: run.log_path,
122
+ command: run.command,
123
+ });
124
+ process.stdout.write(`\n${CLI_RUN_MARKER}${payload}\n`);
125
+ }
126
+ }
127
+ /**
128
+ * Run a single agent turn against an already-prepared {@link SessionContext}.
129
+ * Mutates `ctx.sdkSessionId` so the next turn resumes the same SDK conversation
130
+ * (and prompt cache). Safe to call repeatedly for the life of the process.
131
+ */
132
+ export async function runTurn(ctx, opts = {}) {
133
+ const { runFinished } = opts;
134
+ // 1. Decide what drives this turn. A normal turn replays the channel's
135
+ // pending human messages; a watcher-triggered follow-up replays a
136
+ // synthetic prompt about the background run that just finished.
137
+ let messages = [];
138
+ if (runFinished) {
139
+ logInfo(`Reporting completion of background run ${runFinished.runId} for session ${ctx.channelId}`);
140
+ }
141
+ else {
142
+ const pendingResult = (await callMcpEndpoint('chat/messages/pending', {
143
+ channel_id: ctx.channelId,
144
+ }));
145
+ messages = pendingResult.messages ?? [];
146
+ if (messages.length === 0) {
147
+ logInfo('No pending messages for this session — nothing to do.');
148
+ return;
149
+ }
150
+ logInfo(`Processing ${messages.length} message(s) for session ${ctx.channelId}`);
151
+ }
121
152
  const userPrompt = runFinished
122
153
  ? buildRunFinishedPrompt(runFinished)
123
154
  : buildSessionUserPrompt(messages);
124
- // 4. Run one SDK turn. Resume the prior SDK session when we have its id so
155
+ // 2. Run one SDK turn. Resume the prior SDK session when we have its id so
125
156
  // the conversation (and prompt cache) carries across turns. Point the
126
157
  // agent's cwd at the session directory so its read-only file tools resolve
127
158
  // against the cloned repos. Prompt + tool/MCP wiring come from the shared
128
159
  // session core (edsger-tools) so CLI and worker can't drift apart.
129
160
  let finalResponse = '';
130
- let sdkSessionId = resumeSessionId ?? '';
131
161
  try {
132
162
  for await (const message of query({
133
163
  prompt: userPrompt,
134
164
  options: {
135
- ...buildSessionAgentOptions(systemPrompt, {
136
- sessionServer,
137
- externalServers: external.servers,
138
- externalNames: external.names,
139
- sessionDir,
165
+ ...buildSessionAgentOptions(ctx.systemPrompt, {
166
+ sessionServer: ctx.sessionServer,
167
+ externalServers: ctx.external.servers,
168
+ externalNames: ctx.external.names,
169
+ sessionDir: ctx.sessionDir,
140
170
  maxTurns: SESSION_MAX_TURNS,
141
171
  }),
142
172
  model: DEFAULT_MODEL,
143
- ...(resumeSessionId ? { resume: resumeSessionId } : {}),
173
+ ...(ctx.sdkSessionId ? { resume: ctx.sdkSessionId } : {}),
144
174
  },
145
175
  })) {
146
176
  const msg = message;
147
177
  // The system/init and result messages carry the (possibly new) session
148
178
  // id — keep the latest so we persist what the next turn should resume.
149
179
  if (msg.session_id) {
150
- sdkSessionId = msg.session_id;
180
+ ctx.sdkSessionId = msg.session_id;
151
181
  }
152
- if (verbose && msg.type === 'assistant') {
182
+ if (ctx.verbose && msg.type === 'assistant') {
153
183
  logInfo('· agent step');
154
184
  }
155
185
  if (msg.type === 'result') {
@@ -162,22 +192,22 @@ export async function runSessionTurnCommand(options) {
162
192
  const reason = error instanceof Error ? error.message : String(error);
163
193
  logError(`Session turn failed: ${reason}`);
164
194
  await callMcpEndpoint('chat/messages/send_ai', {
165
- channel_id: channelId,
195
+ channel_id: ctx.channelId,
166
196
  content: `I hit an error processing that: ${reason}`,
167
197
  message_type: 'text',
168
198
  metadata: {},
169
199
  }).catch(() => undefined);
170
- emitSessionId(sdkSessionId);
200
+ emitSessionId(ctx.sdkSessionId);
171
201
  emitActiveCliRuns();
172
202
  await markProcessed(messages);
173
203
  return;
174
204
  }
175
- // 5. Post the final reply (the agent may also have posted via
205
+ // 3. Post the final reply (the agent may also have posted via
176
206
  // send_chat_message during the turn; this carries the closing summary).
177
207
  const reply = finalResponse.trim();
178
208
  if (reply) {
179
209
  await callMcpEndpoint('chat/messages/send_ai', {
180
- channel_id: channelId,
210
+ channel_id: ctx.channelId,
181
211
  content: reply,
182
212
  message_type: 'text',
183
213
  metadata: {},
@@ -185,16 +215,25 @@ export async function runSessionTurnCommand(options) {
185
215
  logError(`Failed to post reply: ${e instanceof Error ? e.message : String(e)}`);
186
216
  });
187
217
  }
188
- // 6. Mark the triggering messages processed so they aren't re-run. (No-op for
218
+ // 4. Mark the triggering messages processed so they aren't re-run. (No-op for
189
219
  // a watcher follow-up, which has no triggering human messages.)
190
220
  await markProcessed(messages);
191
- // 7. Emit the SDK session id so the desktop persists it for the next turn,
221
+ // 5. Emit the SDK session id so the desktop persists it for the next turn,
192
222
  // plus a marker for any background run launched this turn so the watcher
193
223
  // keeps following it to completion.
194
- emitSessionId(sdkSessionId);
224
+ emitSessionId(ctx.sdkSessionId);
195
225
  emitActiveCliRuns();
196
226
  logSuccess('Session turn complete.');
197
227
  }
228
+ /**
229
+ * One-shot `session-turn`: prepare the context and run a single turn. Retained
230
+ * for callers that want the previous per-turn-process behaviour (and as the
231
+ * building block the daemon reuses).
232
+ */
233
+ export async function runSessionTurnCommand(options) {
234
+ const ctx = await prepareSessionContext(options);
235
+ await runTurn(ctx, { runFinished: options.runFinished });
236
+ }
198
237
  /**
199
238
  * The synthetic user prompt for a watcher-triggered follow-up turn. Steers the
200
239
  * agent to inspect what the finished run produced and report it — explicitly
package/dist/index.js CHANGED
@@ -32,6 +32,7 @@ import { runRefactor } from './commands/refactor/refactor.js';
32
32
  import { runReleaseSyncCommand } from './commands/release-sync/index.js';
33
33
  import { runRunSheetCommand } from './commands/run-sheet/index.js';
34
34
  import { runScreenFlow } from './commands/screen-flow/index.js';
35
+ import { runSessionServeCommand } from './commands/session-serve/index.js';
35
36
  import { runSessionTurnCommand } from './commands/session-turn/index.js';
36
37
  import { runSmokeTestCommand } from './commands/smoke-test/index.js';
37
38
  import { runSyncAws } from './commands/sync-aws/index.js';
@@ -466,6 +467,34 @@ program
466
467
  }
467
468
  });
468
469
  // ============================================================
470
+ // Subcommand: edsger session-serve <channelId>
471
+ // ============================================================
472
+ program
473
+ .command('session-serve <channelId>')
474
+ .description('Run a long-lived conversational agent for a chat session, taking one turn per command read from stdin')
475
+ .requiredOption('--product <productId>', 'Product the session is bound to')
476
+ .option('--repos <ids>', 'Comma-separated repository IDs the agent may touch', '')
477
+ .option('--resume <sessionId>', 'SDK session id to resume on the first turn (e.g. after a crash-restart)')
478
+ .option('-v, --verbose', 'Verbose output')
479
+ .action(async (channelId, opts) => {
480
+ try {
481
+ await runSessionServeCommand({
482
+ channelId,
483
+ productId: opts.product,
484
+ repositoryIds: (opts.repos ?? '')
485
+ .split(',')
486
+ .map((s) => s.trim())
487
+ .filter(Boolean),
488
+ resumeSessionId: opts.resume,
489
+ verbose: opts.verbose,
490
+ });
491
+ }
492
+ catch (error) {
493
+ logError(error instanceof Error ? error.message : String(error));
494
+ process.exit(1);
495
+ }
496
+ });
497
+ // ============================================================
469
498
  // Subcommand: edsger user-stories-analysis <issueId>
470
499
  // ============================================================
471
500
  program
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "edsger",
3
- "version": "0.65.0",
3
+ "version": "0.66.0",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "edsger": "dist/index.js"