@downcity/agent 1.1.277 → 1.1.279

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 (47) hide show
  1. package/bin/agent/Agent.d.ts.map +1 -1
  2. package/bin/agent/Agent.js +0 -1
  3. package/bin/agent/Agent.js.map +1 -1
  4. package/bin/agent/AgentSessions.d.ts +0 -4
  5. package/bin/agent/AgentSessions.d.ts.map +1 -1
  6. package/bin/agent/AgentSessions.js +2 -17
  7. package/bin/agent/AgentSessions.js.map +1 -1
  8. package/bin/executor/composer/system/default/assets/core.prompt.d.ts +1 -1
  9. package/bin/executor/composer/system/default/assets/core.prompt.d.ts.map +1 -1
  10. package/bin/executor/composer/system/default/assets/core.prompt.js +1 -1
  11. package/bin/executor/composer/system/default/assets/core.prompt.js.map +1 -1
  12. package/bin/session/Session.d.ts +11 -1
  13. package/bin/session/Session.d.ts.map +1 -1
  14. package/bin/session/Session.js +61 -24
  15. package/bin/session/Session.js.map +1 -1
  16. package/bin/session/storage/Instruction.d.ts +14 -0
  17. package/bin/session/storage/Instruction.d.ts.map +1 -0
  18. package/bin/session/storage/Instruction.js +44 -0
  19. package/bin/session/storage/Instruction.js.map +1 -0
  20. package/bin/session/storage/Paths.d.ts +8 -0
  21. package/bin/session/storage/Paths.d.ts.map +1 -1
  22. package/bin/session/storage/Paths.js +10 -0
  23. package/bin/session/storage/Paths.js.map +1 -1
  24. package/bin/types/agent/SessionActor.d.ts +2 -0
  25. package/bin/types/agent/SessionActor.d.ts.map +1 -1
  26. package/bin/types/session/SessionInstruction.d.ts +22 -0
  27. package/bin/types/session/SessionInstruction.d.ts.map +1 -0
  28. package/bin/types/session/SessionInstruction.js +9 -0
  29. package/bin/types/session/SessionInstruction.js.map +1 -0
  30. package/bin/types/session/SessionOptions.d.ts +6 -2
  31. package/bin/types/session/SessionOptions.d.ts.map +1 -1
  32. package/bin/types/session/SessionQueue.d.ts +2 -18
  33. package/bin/types/session/SessionQueue.d.ts.map +1 -1
  34. package/package.json +1 -1
  35. package/scripts/session-config-turn-boundary.test.mjs +156 -5
  36. package/src/agent/Agent.ts +0 -1
  37. package/src/agent/AgentSessions.ts +2 -21
  38. package/src/executor/composer/system/default/assets/core.prompt.ts +1 -1
  39. package/src/executor/composer/system/default/assets/core.prompt.ts.txt +1 -0
  40. package/src/session/Session.ts +68 -26
  41. package/src/session/storage/Instruction.ts +59 -0
  42. package/src/session/storage/Paths.ts +18 -0
  43. package/src/types/agent/SessionActor.ts +3 -0
  44. package/src/types/session/SessionInstruction.ts +24 -0
  45. package/src/types/session/SessionOptions.ts +6 -2
  46. package/src/types/session/SessionQueue.ts +9 -29
  47. package/tsconfig.tsbuildinfo +1 -1
@@ -74,6 +74,10 @@ import { nanoid } from "nanoid";
74
74
  import { buildSessionInfo } from "@/session/browse/Browse.js";
75
75
  import { ensureSessionTitle } from "@/session/SessionTitle.js";
76
76
  import { readSessionMetadata } from "@/session/storage/Metadata.js";
77
+ import {
78
+ read_session_instruction,
79
+ write_session_instruction,
80
+ } from "@/session/storage/Instruction.js";
77
81
  import { to_executor_history } from "@/session/messages/SessionMessageCodec.js";
78
82
  import type { SessionMessage } from "@/types/session/SessionMessage.js";
79
83
  import type {
@@ -91,7 +95,6 @@ export class Session implements AgentSession {
91
95
  private readonly project_root: string;
92
96
  private readonly tools: Record<string, Tool>;
93
97
  private readonly logger: SessionOptions["logger"];
94
- private readonly get_instruction_system_blocks: SessionOptions["getInstructionSystemBlocks"];
95
98
  private readonly get_managed_plugin_system_blocks: SessionOptions["getManagedPluginSystemBlocks"];
96
99
  private readonly ensure_configured_hook?: SessionOptions["ensureConfigured"];
97
100
  private readonly composer: SessionComposer;
@@ -105,6 +108,7 @@ export class Session implements AgentSession {
105
108
  private readonly get_agent_model: SessionOptions["getAgentModel"];
106
109
  private readonly get_agent_plugins: SessionOptions["get_agent_plugins"];
107
110
  private effective_instruction_system_blocks: AgentSessionSystemBlock[];
111
+ private instruction_initialize_promise: Promise<void> | null = null;
108
112
  private effective_agent_env: Record<string, string>;
109
113
  private effective_agent_plugins: AgentPluginExecutionRuntime;
110
114
  private readonly state: SessionState;
@@ -117,12 +121,11 @@ export class Session implements AgentSession {
117
121
  this.project_root = String(options.projectRoot || "").trim();
118
122
  this.tools = options.tools;
119
123
  this.logger = options.logger;
120
- this.get_instruction_system_blocks = options.getInstructionSystemBlocks;
121
124
  this.get_agent_env = options.getAgentEnv;
122
125
  this.get_agent_model = options.getAgentModel;
123
126
  this.get_agent_plugins = options.get_agent_plugins;
124
127
  this.effective_instruction_system_blocks = options
125
- .getInstructionSystemBlocks()
128
+ .instruction_system_blocks
126
129
  .map((block) => ({ ...block }));
127
130
  this.effective_agent_env = { ...options.getAgentEnv() };
128
131
  this.effective_agent_plugins = options.get_agent_plugins();
@@ -188,11 +191,35 @@ export class Session implements AgentSession {
188
191
  * 初始化当前 session。
189
192
  */
190
193
  async initialize(): Promise<this> {
191
- await this.session_messages.initialize();
192
- await this.state.initialize();
194
+ await Promise.all([
195
+ this.initialize_instruction(),
196
+ this.session_messages.initialize(),
197
+ this.state.initialize(),
198
+ ]);
193
199
  return this;
194
200
  }
195
201
 
202
+ /**
203
+ * 把当前 Session 生效的自定义 instruction 显式固化到 instruction.md。
204
+ *
205
+ * 关键点(中文)
206
+ * - 只写入 `instruction` 来源的 block,不包含 SDK core 与 plugin system。
207
+ * - 多个 instruction block 按原顺序合并为一个 Markdown 文档。
208
+ */
209
+ async snapshot(): Promise<void> {
210
+ await this.initialize_instruction();
211
+ const instruction = this.effective_instruction_system_blocks
212
+ .filter((block) => block.source === "instruction")
213
+ .map((block) => block.content)
214
+ .join("\n\n");
215
+ await write_session_instruction({
216
+ project_root: this.project_root,
217
+ agent_id: this.agentId,
218
+ session_id: this.id,
219
+ instruction,
220
+ });
221
+ }
222
+
196
223
  /**
197
224
  * 读取当前 session 配置快照。
198
225
  */
@@ -214,6 +241,7 @@ export class Session implements AgentSession {
214
241
  * 追加一条新的 Session prompt。
215
242
  */
216
243
  async prompt(input: AgentSessionPromptInput): Promise<AgentSessionTurnHandle> {
244
+ await this.initialize_instruction();
217
245
  return await this.session_turn.prompt(input);
218
246
  }
219
247
 
@@ -236,14 +264,6 @@ export class Session implements AgentSession {
236
264
  * 把 Agent configured state command 加入当前 Session 的统一输入队列。
237
265
  */
238
266
  enqueue_agent_command(command: AgentSessionCommand): void {
239
- if (command.type === "instruction") {
240
- this.session_turn.enqueue_command({
241
- type: "agent_instruction",
242
- command_id: command.command_id,
243
- instruction_blocks: command.instruction_blocks,
244
- });
245
- return;
246
- }
247
267
  if (command.type === "env") {
248
268
  this.session_turn.enqueue_command({
249
269
  type: "agent_env",
@@ -277,18 +297,6 @@ export class Session implements AgentSession {
277
297
  }
278
298
  return;
279
299
  }
280
- if (command.type === "agent_instruction") {
281
- this.effective_instruction_system_blocks = command.instruction_blocks.map(
282
- (block) => ({ ...block }),
283
- );
284
- await this.emit_config_action_event({
285
- id: `agent-instruction:${this.id}:${command.command_id}`,
286
- title: "Agent instruction updated",
287
- state: "completed",
288
- turnId: turn_id,
289
- });
290
- return;
291
- }
292
300
  if (command.type === "agent_env") {
293
301
  this.effective_agent_env = { ...command.env };
294
302
  await this.emit_config_action_event({
@@ -406,6 +414,7 @@ export class Session implements AgentSession {
406
414
  * 读取当前 session 生效的 system 快照。
407
415
  */
408
416
  async system(): Promise<AgentSessionSystemSnapshot> {
417
+ await this.initialize_instruction();
409
418
  const composed = await this.compose_for_view();
410
419
  const blocks = resolve_composed_system_blocks(composed);
411
420
  return {
@@ -533,6 +542,7 @@ export class Session implements AgentSession {
533
542
  * 在执行前确保 session 已完成初始化与宿主装配。
534
543
  */
535
544
  async ensureReadyForExecution(): Promise<void> {
545
+ await this.initialize_instruction();
536
546
  await this.state.ensure_ready_for_execution();
537
547
  }
538
548
 
@@ -543,7 +553,9 @@ export class Session implements AgentSession {
543
553
  sessionId: session_id,
544
554
  tools: this.tools,
545
555
  logger: this.logger,
546
- getInstructionSystemBlocks: this.get_instruction_system_blocks,
556
+ instruction_system_blocks: this.effective_instruction_system_blocks.map(
557
+ (block) => ({ ...block }),
558
+ ),
547
559
  getAgentEnv: this.get_agent_env,
548
560
  get_agent_plugins: this.get_agent_plugins,
549
561
  getManagedPluginSystemBlocks: this.get_managed_plugin_system_blocks,
@@ -567,6 +579,36 @@ export class Session implements AgentSession {
567
579
  return new SessionClass(options) as this;
568
580
  }
569
581
 
582
+ /** 恢复显式固化的 instruction.md;文件不存在时保留创建时 instruction。 */
583
+ private async initialize_instruction(): Promise<void> {
584
+ if (!this.instruction_initialize_promise) {
585
+ this.instruction_initialize_promise = (async () => {
586
+ const persisted_instruction = await read_session_instruction({
587
+ project_root: this.project_root,
588
+ agent_id: this.agentId,
589
+ session_id: this.id,
590
+ });
591
+ if (persisted_instruction === null) return;
592
+
593
+ const instruction = persisted_instruction.trim();
594
+ const stable_system_blocks = this.effective_instruction_system_blocks
595
+ .filter((block) => block.source !== "instruction")
596
+ .map((block) => ({ ...block }));
597
+ this.effective_instruction_system_blocks = [
598
+ ...(instruction
599
+ ? [{
600
+ source: "instruction" as const,
601
+ name: "agent",
602
+ content: instruction,
603
+ }]
604
+ : []),
605
+ ...stable_system_blocks,
606
+ ];
607
+ })();
608
+ }
609
+ await this.instruction_initialize_promise;
610
+ }
611
+
570
612
  private create_message_store(): JsonlSessionMessageStore {
571
613
  const session_dir_path = getSdkAgentSessionDirPath(
572
614
  this.project_root,
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Session instruction 显式快照存储。
3
+ *
4
+ * 关键点(中文)
5
+ * - instruction.md 是可选文件,不存在时由 Session 使用 Agent 当前 instruction。
6
+ * - 空文件也是有效快照,用于显式固化没有自定义 instruction 的状态。
7
+ * - 写入使用同目录临时文件替换,避免进程中断留下半份内容。
8
+ */
9
+
10
+ import { randomUUID } from "node:crypto";
11
+ import path from "node:path";
12
+ import fs from "fs-extra";
13
+ import { getSdkAgentSessionInstructionPath } from "@/session/storage/Paths.js";
14
+ import type {
15
+ SessionInstructionStorageLocation,
16
+ WriteSessionInstructionInput,
17
+ } from "@/types/session/SessionInstruction.js";
18
+
19
+ /** 读取 Session 显式固化的 instruction;文件不存在时返回 null。 */
20
+ export async function read_session_instruction(
21
+ input: SessionInstructionStorageLocation,
22
+ ): Promise<string | null> {
23
+ const instruction_path = getSdkAgentSessionInstructionPath(
24
+ input.project_root,
25
+ input.agent_id,
26
+ input.session_id,
27
+ );
28
+ try {
29
+ return await fs.readFile(instruction_path, "utf8");
30
+ } catch (error) {
31
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return null;
32
+ throw error;
33
+ }
34
+ }
35
+
36
+ /** 原子覆盖当前 Session 的 instruction.md。 */
37
+ export async function write_session_instruction(
38
+ input: WriteSessionInstructionInput,
39
+ ): Promise<void> {
40
+ const instruction_path = getSdkAgentSessionInstructionPath(
41
+ input.project_root,
42
+ input.agent_id,
43
+ input.session_id,
44
+ );
45
+ const temporary_path = [
46
+ instruction_path,
47
+ process.pid,
48
+ Date.now(),
49
+ randomUUID(),
50
+ "tmp",
51
+ ].join(".");
52
+ await fs.ensureDir(path.dirname(instruction_path));
53
+ try {
54
+ await fs.writeFile(temporary_path, input.instruction, "utf8");
55
+ await fs.move(temporary_path, instruction_path, { overwrite: true });
56
+ } finally {
57
+ await fs.remove(temporary_path);
58
+ }
59
+ }
@@ -129,6 +129,24 @@ export function getSdkAgentSessionDirPath(
129
129
  );
130
130
  }
131
131
 
132
+ /**
133
+ * 单个 session 显式固化的 instruction.md 路径。
134
+ *
135
+ * 关键点(中文)
136
+ * - 文件不存在表示 Session 恢复时继续采用 Agent 当前 instruction。
137
+ * - 空文件表示调用方显式固化了空 instruction。
138
+ */
139
+ export function getSdkAgentSessionInstructionPath(
140
+ projectRoot: string,
141
+ agentId: string,
142
+ sessionId: string,
143
+ ): string {
144
+ return path.join(
145
+ getSdkAgentSessionDirPath(projectRoot, agentId, sessionId),
146
+ "instruction.md",
147
+ );
148
+ }
149
+
132
150
  /**
133
151
  * 单个 session 的消息目录路径。
134
152
  */
@@ -125,6 +125,9 @@ export interface AgentSession extends AgentSessionActor {
125
125
  /** 写入当前 session 默认配置。 */
126
126
  set(input: AgentSessionSetInput): Promise<void>;
127
127
 
128
+ /** 把当前 Session 生效的自定义 instruction 显式固化到 instruction.md。 */
129
+ snapshot(): Promise<void>;
130
+
128
131
  /** 从当前 session 创建一个分叉会话。 */
129
132
  fork(input?: AgentSessionForkInput | string): Promise<AgentSession>;
130
133
  }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Session instruction 显式快照存储类型。
3
+ *
4
+ * 关键点(中文)
5
+ * - 这里只描述 instruction.md 的定位与写入输入。
6
+ * - 文件存在性本身用于表达 Session 是否启用显式 instruction 快照。
7
+ */
8
+
9
+ /** Session instruction.md 的稳定定位信息。 */
10
+ export interface SessionInstructionStorageLocation {
11
+ /** 当前项目根目录。 */
12
+ project_root: string;
13
+ /** 当前 Agent 稳定标识。 */
14
+ agent_id: string;
15
+ /** 当前 Session 稳定标识。 */
16
+ session_id: string;
17
+ }
18
+
19
+ /** 原子写入 Session instruction.md 使用的完整输入。 */
20
+ export interface WriteSessionInstructionInput
21
+ extends SessionInstructionStorageLocation {
22
+ /** 当前 Session 生效的自定义 instruction Markdown。 */
23
+ instruction: string;
24
+ }
@@ -74,9 +74,13 @@ export interface SessionOptions {
74
74
  logger: Logger;
75
75
 
76
76
  /**
77
- * 读取当前 SDK 调用方传入的 instruction system blocks。
77
+ * 当前 Session 创建时绑定的 instruction system blocks。
78
+ *
79
+ * 关键点(中文)
80
+ * - Session 创建后不再动态读取 Agent instruction。
81
+ * - `session.snapshot()` 只会把其中 `instruction` 来源的内容显式写入本地文件。
78
82
  */
79
- getInstructionSystemBlocks: () => AgentSessionSystemBlock[];
83
+ instruction_system_blocks: AgentSessionSystemBlock[];
80
84
 
81
85
  /**
82
86
  * 读取当前 Agent configured env。
@@ -4,7 +4,7 @@
4
4
  * 队列只保存明确的输入事实,不保存可执行闭包。SessionTurn 是这些输入的唯一解释者。
5
5
  */
6
6
 
7
- import type { AgentSessionConfigSnapshot, AgentSessionSystemBlock } from "@/types/agent/SessionTypes.js";
7
+ import type { AgentSessionConfigSnapshot } from "@/types/agent/SessionTypes.js";
8
8
  import type { AgentPluginExecutionRuntime } from "@/types/plugin/PluginRuntime.js";
9
9
  import type { AgentSessionPromptInput } from "@/types/sdk/AgentSessionPrompt.js";
10
10
  import type { AgentSessionTurnHandle } from "@/types/sdk/AgentSessionTurn.js";
@@ -23,16 +23,6 @@ export interface SessionModelQueueCommand {
23
23
  action_title?: string;
24
24
  }
25
25
 
26
- /** Agent instruction 在 Session Step 检查点生效的命令。 */
27
- export interface SessionInstructionQueueCommand {
28
- /** 命令种类固定为 Agent instruction 更新。 */
29
- type: "agent_instruction";
30
- /** 当前命令的稳定唯一标识。 */
31
- command_id: string;
32
- /** 下一 Step 使用的完整 instruction blocks。 */
33
- instruction_blocks: AgentSessionSystemBlock[];
34
- }
35
-
36
26
  /** Agent env 在 Session Step 检查点生效的命令。 */
37
27
  export interface SessionEnvQueueCommand {
38
28
  /** 命令种类固定为 Agent env 更新。 */
@@ -66,29 +56,19 @@ export interface SessionCompactQueueCommand {
66
56
  /** Session FIFO 中允许出现的领域命令。 */
67
57
  export type SessionQueueCommand =
68
58
  | SessionModelQueueCommand
69
- | SessionInstructionQueueCommand
70
59
  | SessionEnvQueueCommand
71
60
  | SessionPluginsQueueCommand
72
61
  | SessionCompactQueueCommand;
73
62
 
74
63
  /** Agent configured state 广播给既有 Session 的输入。 */
75
- export type AgentSessionCommand =
76
- | {
77
- /** 当前修改固定为 instruction。 */
78
- type: "instruction";
79
- /** 当前命令唯一标识。 */
80
- command_id: string;
81
- /** 下一 Session Step 使用的 instruction blocks。 */
82
- instruction_blocks: AgentSessionSystemBlock[];
83
- }
84
- | {
85
- /** 当前修改固定为 env。 */
86
- type: "env";
87
- /** 当前命令唯一标识。 */
88
- command_id: string;
89
- /** 下一 Session Step 使用的完整 Agent env。 */
90
- env: Record<string, string>;
91
- }
64
+ export type AgentSessionCommand = {
65
+ /** 当前修改固定为 env。 */
66
+ type: "env";
67
+ /** 当前命令唯一标识。 */
68
+ command_id: string;
69
+ /** 下一 Session Step 使用的完整 Agent env。 */
70
+ env: Record<string, string>;
71
+ }
92
72
  | {
93
73
  /** 当前修改固定为 plugins。 */
94
74
  type: "plugins";