@renxqoo/renx-code 0.0.4 → 0.0.6
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 +82 -51
- package/bin/renx.cjs +16 -0
- package/package.json +2 -45
- package/src/agent/runtime/runtime.context-usage.test.ts +4 -5
- package/src/agent/runtime/runtime.error-handling.test.ts +4 -5
- package/src/agent/runtime/runtime.test.ts +7 -4
- package/src/agent/runtime/runtime.ts +3 -9
- package/src/agent/runtime/runtime.usage-forwarding.test.ts +4 -5
- package/src/agent/runtime/source-modules.test.ts +16 -35
- package/src/agent/runtime/source-modules.ts +17 -0
- package/vendor/agent-root/src/agent/ENTERPRISE_ACCEPTANCE_CHECKLIST.md +95 -0
- package/vendor/agent-root/src/agent/ENTERPRISE_REALTIME.html +1345 -0
- package/vendor/agent-root/src/agent/ENTERPRISE_REALTIME.md +1353 -0
- package/vendor/agent-root/src/agent/ERROR_CONTRACT.md +60 -0
- package/vendor/agent-root/src/agent/TEST_COVERAGE_ANALYSIS.md +278 -0
- package/vendor/agent-root/src/agent/__test__/error-contract.test.ts +72 -0
- package/vendor/agent-root/src/agent/__test__/types.test.ts +137 -0
- package/vendor/agent-root/src/agent/agent/__test__/abort-runtime.test.ts +83 -0
- package/vendor/agent-root/src/agent/agent/__test__/callback-safety.test.ts +34 -0
- package/vendor/agent-root/src/agent/agent/__test__/compaction.test.ts +323 -0
- package/vendor/agent-root/src/agent/agent/__test__/concurrency.test.ts +290 -0
- package/vendor/agent-root/src/agent/agent/__test__/error-normalizer.test.ts +377 -0
- package/vendor/agent-root/src/agent/agent/__test__/error.test.ts +212 -0
- package/vendor/agent-root/src/agent/agent/__test__/fault-injection.test.ts +295 -0
- package/vendor/agent-root/src/agent/agent/__test__/index.test.ts +3607 -0
- package/vendor/agent-root/src/agent/agent/__test__/logger.test.ts +35 -0
- package/vendor/agent-root/src/agent/agent/__test__/message-utils.test.ts +517 -0
- package/vendor/agent-root/src/agent/agent/__test__/telemetry.test.ts +97 -0
- package/vendor/agent-root/src/agent/agent/__test__/timeout-budget.test.ts +479 -0
- package/vendor/agent-root/src/agent/agent/__test__/tool-call-merge.test.ts +80 -0
- package/vendor/agent-root/src/agent/agent/__test__/tool-execution-ledger.test.ts +76 -0
- package/vendor/agent-root/src/agent/agent/__test__/write-buffer.test.ts +173 -0
- package/vendor/agent-root/src/agent/agent/__test__/write-file-session.test.ts +109 -0
- package/vendor/agent-root/src/agent/agent/abort-runtime.ts +71 -0
- package/vendor/agent-root/src/agent/agent/callback-safety.ts +33 -0
- package/vendor/agent-root/src/agent/agent/compaction.ts +291 -0
- package/vendor/agent-root/src/agent/agent/concurrency.ts +103 -0
- package/vendor/agent-root/src/agent/agent/error-normalizer.ts +190 -0
- package/vendor/agent-root/src/agent/agent/error.ts +198 -0
- package/vendor/agent-root/src/agent/agent/index.ts +1772 -0
- package/vendor/agent-root/src/agent/agent/logger.ts +65 -0
- package/vendor/agent-root/src/agent/agent/message-utils.ts +101 -0
- package/vendor/agent-root/src/agent/agent/stream-events.ts +61 -0
- package/vendor/agent-root/src/agent/agent/telemetry.ts +123 -0
- package/vendor/agent-root/src/agent/agent/timeout-budget.ts +227 -0
- package/vendor/agent-root/src/agent/agent/tool-call-merge.ts +111 -0
- package/vendor/agent-root/src/agent/agent/tool-execution-ledger.ts +164 -0
- package/vendor/agent-root/src/agent/agent/write-buffer.ts +188 -0
- package/vendor/agent-root/src/agent/agent/write-file-session.ts +238 -0
- package/vendor/agent-root/src/agent/app/__test__/agent-app-service.test.ts +1053 -0
- package/vendor/agent-root/src/agent/app/__test__/minimal-agent-application.test.ts +158 -0
- package/vendor/agent-root/src/agent/app/__test__/sqlite-agent-app-store.test.ts +437 -0
- package/vendor/agent-root/src/agent/app/agent-app-service.ts +748 -0
- package/vendor/agent-root/src/agent/app/contracts.ts +109 -0
- package/vendor/agent-root/src/agent/app/index.ts +5 -0
- package/vendor/agent-root/src/agent/app/minimal-agent-application.ts +151 -0
- package/vendor/agent-root/src/agent/app/ports.ts +72 -0
- package/vendor/agent-root/src/agent/app/sqlite-agent-app-store.ts +1182 -0
- package/vendor/agent-root/src/agent/app/sqlite-client.ts +177 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/00-README.md +36 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/01-scope-and-goals.md +33 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/02-architecture-overview.md +40 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/03-domain-model-and-contracts.md +91 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/04-ports-and-interfaces.md +116 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/05-run-orchestration-and-state-machine.md +52 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/06-cli-commands-and-ux.md +53 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/07-storage-design-local.md +52 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/08-error-and-observability.md +40 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/09-security-and-policy-boundary.md +19 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/10-test-plan-and-acceptance.md +28 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/11-implementation-phases.md +26 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/12-open-questions-and-risks.md +30 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/13-sqlite-schema-fields-and-rationale.md +567 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/14-project-flow-mermaid.md +583 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/15-openclaw-style-project-blueprint.md +972 -0
- package/vendor/agent-root/src/agent/error-contract.ts +154 -0
- package/vendor/agent-root/src/agent/prompts/system.ts +246 -0
- package/vendor/agent-root/src/agent/prompts/system1.ts +208 -0
- package/vendor/agent-root/src/agent/storage/__test__/file-history-store.test.ts +98 -0
- package/vendor/agent-root/src/agent/storage/file-history-store.ts +313 -0
- package/vendor/agent-root/src/agent/storage/file-storage-config.ts +94 -0
- package/vendor/agent-root/src/agent/storage/file-system.ts +31 -0
- package/vendor/agent-root/src/agent/storage/file-write-service.ts +21 -0
- package/vendor/agent-root/src/agent/tool/__test__/base-tool.test.ts +413 -0
- package/vendor/agent-root/src/agent/tool/__test__/bash-policy.test.ts +356 -0
- package/vendor/agent-root/src/agent/tool/__test__/bash.mocked-coverage.test.ts +375 -0
- package/vendor/agent-root/src/agent/tool/__test__/bash.test.ts +372 -0
- package/vendor/agent-root/src/agent/tool/__test__/error.test.ts +108 -0
- package/vendor/agent-root/src/agent/tool/__test__/file-edit-tool.test.ts +258 -0
- package/vendor/agent-root/src/agent/tool/__test__/file-history-tools.test.ts +121 -0
- package/vendor/agent-root/src/agent/tool/__test__/file-read-tool.test.ts +210 -0
- package/vendor/agent-root/src/agent/tool/__test__/glob.test.ts +139 -0
- package/vendor/agent-root/src/agent/tool/__test__/grep.mocked-coverage.test.ts +456 -0
- package/vendor/agent-root/src/agent/tool/__test__/grep.test.ts +192 -0
- package/vendor/agent-root/src/agent/tool/__test__/lsp.test.ts +300 -0
- package/vendor/agent-root/src/agent/tool/__test__/outside-workspace-confirmation.test.ts +214 -0
- package/vendor/agent-root/src/agent/tool/__test__/path-security.test.ts +336 -0
- package/vendor/agent-root/src/agent/tool/__test__/skill-loader.test.ts +494 -0
- package/vendor/agent-root/src/agent/tool/__test__/skill-parser.test.ts +543 -0
- package/vendor/agent-root/src/agent/tool/__test__/skill-tool.test.ts +172 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-concurrency-and-version.test.ts +116 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-create-get-list-update.test.ts +267 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-create.test.ts +519 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-errors.test.ts +225 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-output-blocking.test.ts +223 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-output.test.ts +184 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-parent-abort.test.ts +287 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-real-runner-adapter.test.ts +190 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-run-lifecycle.test.ts +352 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-store-runner-branches.test.ts +395 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-store.test.ts +391 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-subagent-config-integration.test.ts +176 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-subagent-config.test.ts +68 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-tools-core-edges.test.ts +630 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-tools-runtime-edges.test.ts +732 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-types.test.ts +494 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-utils-branches.test.ts +175 -0
- package/vendor/agent-root/src/agent/tool/__test__/tool-manager.test.ts +505 -0
- package/vendor/agent-root/src/agent/tool/__test__/types.test.ts +55 -0
- package/vendor/agent-root/src/agent/tool/__test__/web-fetch.test.ts +244 -0
- package/vendor/agent-root/src/agent/tool/__test__/web-search.test.ts +290 -0
- package/vendor/agent-root/src/agent/tool/__test__/write-file.test.ts +368 -0
- package/vendor/agent-root/src/agent/tool/base-tool.ts +345 -0
- package/vendor/agent-root/src/agent/tool/bash-policy.ts +636 -0
- package/vendor/agent-root/src/agent/tool/bash.ts +688 -0
- package/vendor/agent-root/src/agent/tool/error.ts +131 -0
- package/vendor/agent-root/src/agent/tool/file-edit-tool.ts +264 -0
- package/vendor/agent-root/src/agent/tool/file-history-list.ts +103 -0
- package/vendor/agent-root/src/agent/tool/file-history-restore.ts +149 -0
- package/vendor/agent-root/src/agent/tool/file-read-tool.ts +211 -0
- package/vendor/agent-root/src/agent/tool/glob.ts +171 -0
- package/vendor/agent-root/src/agent/tool/grep.ts +496 -0
- package/vendor/agent-root/src/agent/tool/lsp.ts +481 -0
- package/vendor/agent-root/src/agent/tool/path-security.ts +117 -0
- package/vendor/agent-root/src/agent/tool/search/common.ts +153 -0
- package/vendor/agent-root/src/agent/tool/skill/index.ts +13 -0
- package/vendor/agent-root/src/agent/tool/skill/loader.ts +229 -0
- package/vendor/agent-root/src/agent/tool/skill/parser.ts +124 -0
- package/vendor/agent-root/src/agent/tool/skill/types.ts +27 -0
- package/vendor/agent-root/src/agent/tool/skill-tool.ts +143 -0
- package/vendor/agent-root/src/agent/tool/task-create.ts +186 -0
- package/vendor/agent-root/src/agent/tool/task-errors.ts +42 -0
- package/vendor/agent-root/src/agent/tool/task-get.ts +116 -0
- package/vendor/agent-root/src/agent/tool/task-graph.ts +78 -0
- package/vendor/agent-root/src/agent/tool/task-list.ts +141 -0
- package/vendor/agent-root/src/agent/tool/task-mock-runner-adapter.ts +232 -0
- package/vendor/agent-root/src/agent/tool/task-output.ts +223 -0
- package/vendor/agent-root/src/agent/tool/task-parent-abort.ts +115 -0
- package/vendor/agent-root/src/agent/tool/task-real-runner-adapter.ts +336 -0
- package/vendor/agent-root/src/agent/tool/task-runner-adapter.ts +55 -0
- package/vendor/agent-root/src/agent/tool/task-stop.ts +187 -0
- package/vendor/agent-root/src/agent/tool/task-store.ts +217 -0
- package/vendor/agent-root/src/agent/tool/task-subagent-config.ts +149 -0
- package/vendor/agent-root/src/agent/tool/task-types.ts +264 -0
- package/vendor/agent-root/src/agent/tool/task-update.ts +315 -0
- package/vendor/agent-root/src/agent/tool/task.ts +209 -0
- package/vendor/agent-root/src/agent/tool/tool-manager.ts +362 -0
- package/vendor/agent-root/src/agent/tool/tool-prompts.ts +242 -0
- package/vendor/agent-root/src/agent/tool/types.ts +116 -0
- package/vendor/agent-root/src/agent/tool/web-fetch.ts +227 -0
- package/vendor/agent-root/src/agent/tool/web-search.ts +208 -0
- package/vendor/agent-root/src/agent/tool/write-file.ts +497 -0
- package/vendor/agent-root/src/agent/types.ts +232 -0
- package/vendor/agent-root/src/agent/utils/__tests__/index.test.ts +18 -0
- package/vendor/agent-root/src/agent/utils/__tests__/message-utils.test.ts +610 -0
- package/vendor/agent-root/src/agent/utils/__tests__/message.test.ts +223 -0
- package/vendor/agent-root/src/agent/utils/__tests__/token.test.ts +42 -0
- package/vendor/agent-root/src/agent/utils/index.ts +16 -0
- package/vendor/agent-root/src/agent/utils/message.ts +171 -0
- package/vendor/agent-root/src/agent/utils/token.ts +28 -0
- package/vendor/agent-root/src/config/__tests__/load-config-to-env.test.ts +129 -0
- package/vendor/agent-root/src/config/__tests__/loader.test.ts +247 -0
- package/vendor/agent-root/src/config/__tests__/runtime.test.ts +88 -0
- package/vendor/agent-root/src/config/index.ts +54 -0
- package/vendor/agent-root/src/config/loader.ts +431 -0
- package/vendor/agent-root/src/config/paths.ts +30 -0
- package/vendor/agent-root/src/config/runtime.ts +163 -0
- package/vendor/agent-root/src/config/types.ts +70 -0
- package/vendor/agent-root/src/logger/index.ts +57 -0
- package/vendor/agent-root/src/logger/logger.ts +819 -0
- package/vendor/agent-root/src/logger/types.ts +150 -0
- package/vendor/agent-root/src/providers/__tests__/errors.test.ts +441 -0
- package/vendor/agent-root/src/providers/__tests__/index.test.ts +16 -0
- package/vendor/agent-root/src/providers/__tests__/openai-compatible.options.test.ts +318 -0
- package/vendor/agent-root/src/providers/__tests__/openai-compatible.test.ts +600 -0
- package/vendor/agent-root/src/providers/__tests__/registry.test.ts +449 -0
- package/vendor/agent-root/src/providers/__tests__/responses-adapter.test.ts +298 -0
- package/vendor/agent-root/src/providers/adapters/__tests__/anthropic.test.ts +354 -0
- package/vendor/agent-root/src/providers/adapters/__tests__/kimi.test.ts +58 -0
- package/vendor/agent-root/src/providers/adapters/__tests__/standard.test.ts +261 -0
- package/vendor/agent-root/src/providers/adapters/anthropic.ts +572 -0
- package/vendor/agent-root/src/providers/adapters/base.ts +131 -0
- package/vendor/agent-root/src/providers/adapters/kimi.ts +48 -0
- package/vendor/agent-root/src/providers/adapters/responses.ts +732 -0
- package/vendor/agent-root/src/providers/adapters/standard.ts +120 -0
- package/vendor/agent-root/src/providers/http/__tests__/client.timeout.test.ts +313 -0
- package/vendor/agent-root/src/providers/http/client.ts +289 -0
- package/vendor/agent-root/src/providers/http/stream-parser.ts +109 -0
- package/vendor/agent-root/src/providers/index.ts +76 -0
- package/vendor/agent-root/src/providers/kimi-headers.ts +177 -0
- package/vendor/agent-root/src/providers/openai-compatible.ts +387 -0
- package/vendor/agent-root/src/providers/registry/model-config.ts +230 -0
- package/vendor/agent-root/src/providers/registry/provider-factory.ts +123 -0
- package/vendor/agent-root/src/providers/registry.ts +135 -0
- package/vendor/agent-root/src/providers/types/api.ts +284 -0
- package/vendor/agent-root/src/providers/types/config.ts +58 -0
- package/vendor/agent-root/src/providers/types/errors.ts +323 -0
- package/vendor/agent-root/src/providers/types/index.ts +72 -0
- package/vendor/agent-root/src/providers/types/provider.ts +45 -0
- package/vendor/agent-root/src/providers/types/registry.ts +88 -0
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import * as fs from 'node:fs/promises';
|
|
2
|
+
import { createRequire } from 'node:module';
|
|
3
|
+
import * as path from 'node:path';
|
|
4
|
+
|
|
5
|
+
interface SqliteStatementLike {
|
|
6
|
+
run: (...params: unknown[]) => unknown;
|
|
7
|
+
get: (...params: unknown[]) => unknown;
|
|
8
|
+
all: (...params: unknown[]) => unknown[];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface SqliteDatabaseLike {
|
|
12
|
+
exec: (sql: string) => void;
|
|
13
|
+
prepare: (sql: string) => SqliteStatementLike;
|
|
14
|
+
close: () => void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface BunSqliteStatementLike {
|
|
18
|
+
run: (...params: unknown[]) => unknown;
|
|
19
|
+
get: (...params: unknown[]) => unknown;
|
|
20
|
+
all: (...params: unknown[]) => unknown[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface BunSqliteDatabaseLike {
|
|
24
|
+
exec: (sql: string) => unknown;
|
|
25
|
+
query: (sql: string) => BunSqliteStatementLike;
|
|
26
|
+
close: () => unknown;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
class BunSqliteDatabaseAdapter implements SqliteDatabaseLike {
|
|
30
|
+
constructor(private readonly db: BunSqliteDatabaseLike) {}
|
|
31
|
+
|
|
32
|
+
exec(sql: string): void {
|
|
33
|
+
this.db.exec(sql);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
prepare(sql: string): SqliteStatementLike {
|
|
37
|
+
const statement = this.db.query(sql);
|
|
38
|
+
return {
|
|
39
|
+
run: (...params: unknown[]) => statement.run(...params),
|
|
40
|
+
get: (...params: unknown[]) => statement.get(...params),
|
|
41
|
+
all: (...params: unknown[]) => statement.all(...params),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
close(): void {
|
|
46
|
+
this.db.close();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Agent-v4 app 专用 SQLite 客户端。
|
|
52
|
+
* 只负责连接与事务,不执行通用 storage 模块的旧迁移。
|
|
53
|
+
*/
|
|
54
|
+
export class AgentAppSqliteClient {
|
|
55
|
+
private static readonly require = createRequire(import.meta.url);
|
|
56
|
+
private db: SqliteDatabaseLike | null = null;
|
|
57
|
+
private initialized = false;
|
|
58
|
+
private initializePromise: Promise<void> | null = null;
|
|
59
|
+
|
|
60
|
+
constructor(private readonly dbPath: string) {}
|
|
61
|
+
|
|
62
|
+
async prepare(): Promise<void> {
|
|
63
|
+
if (this.initialized) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (this.initializePromise) {
|
|
67
|
+
return this.initializePromise;
|
|
68
|
+
}
|
|
69
|
+
this.initializePromise = this.doPrepare();
|
|
70
|
+
try {
|
|
71
|
+
await this.initializePromise;
|
|
72
|
+
} finally {
|
|
73
|
+
this.initializePromise = null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private async doPrepare(): Promise<void> {
|
|
78
|
+
await fs.mkdir(path.dirname(this.dbPath), { recursive: true });
|
|
79
|
+
|
|
80
|
+
this.db = this.tryCreateNodeSqlite() ?? this.tryCreateBunSqlite();
|
|
81
|
+
if (!this.db) {
|
|
82
|
+
throw new Error(
|
|
83
|
+
'Unable to initialize sqlite backend: neither "node:sqlite" nor "bun:sqlite" is available.'
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
this.db.exec('PRAGMA foreign_keys = ON;');
|
|
88
|
+
this.db.exec('PRAGMA journal_mode = WAL;');
|
|
89
|
+
this.db.exec('PRAGMA synchronous = NORMAL;');
|
|
90
|
+
this.db.exec('PRAGMA temp_store = MEMORY;');
|
|
91
|
+
this.db.exec('PRAGMA busy_timeout = 5000;');
|
|
92
|
+
this.initialized = true;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async exec(sql: string): Promise<void> {
|
|
96
|
+
this.requireDb().exec(sql);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async run(sql: string, params: unknown[] = []): Promise<void> {
|
|
100
|
+
this.requireDb()
|
|
101
|
+
.prepare(sql)
|
|
102
|
+
.run(...params);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async get<T>(sql: string, params: unknown[] = []): Promise<T | undefined> {
|
|
106
|
+
return this.requireDb()
|
|
107
|
+
.prepare(sql)
|
|
108
|
+
.get(...params) as T | undefined;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async all<T>(sql: string, params: unknown[] = []): Promise<T[]> {
|
|
112
|
+
return this.requireDb()
|
|
113
|
+
.prepare(sql)
|
|
114
|
+
.all(...params) as T[];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async transaction<T>(fn: () => Promise<T>): Promise<T> {
|
|
118
|
+
const db = this.requireDb();
|
|
119
|
+
db.exec('BEGIN IMMEDIATE;');
|
|
120
|
+
try {
|
|
121
|
+
const result = await fn();
|
|
122
|
+
db.exec('COMMIT;');
|
|
123
|
+
return result;
|
|
124
|
+
} catch (error) {
|
|
125
|
+
db.exec('ROLLBACK;');
|
|
126
|
+
throw error;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async close(): Promise<void> {
|
|
131
|
+
if (this.initializePromise) {
|
|
132
|
+
await this.initializePromise.catch(() => undefined);
|
|
133
|
+
}
|
|
134
|
+
if (this.db) {
|
|
135
|
+
try {
|
|
136
|
+
this.db.exec('PRAGMA wal_checkpoint(TRUNCATE);');
|
|
137
|
+
} catch {
|
|
138
|
+
// Ignore checkpoint errors during shutdown and still close the handle.
|
|
139
|
+
}
|
|
140
|
+
this.db.close();
|
|
141
|
+
this.db = null;
|
|
142
|
+
}
|
|
143
|
+
this.initialized = false;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
private tryCreateNodeSqlite(): SqliteDatabaseLike | null {
|
|
147
|
+
try {
|
|
148
|
+
const sqliteModule = AgentAppSqliteClient.require('node:sqlite') as {
|
|
149
|
+
DatabaseSync: new (location: string) => SqliteDatabaseLike;
|
|
150
|
+
};
|
|
151
|
+
return new sqliteModule.DatabaseSync(this.dbPath);
|
|
152
|
+
} catch {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
private tryCreateBunSqlite(): SqliteDatabaseLike | null {
|
|
158
|
+
try {
|
|
159
|
+
const sqliteModule = AgentAppSqliteClient.require('bun:sqlite') as {
|
|
160
|
+
Database: new (location: string, options?: { create?: boolean }) => BunSqliteDatabaseLike;
|
|
161
|
+
};
|
|
162
|
+
const db = new sqliteModule.Database(this.dbPath, { create: true });
|
|
163
|
+
return new BunSqliteDatabaseAdapter(db);
|
|
164
|
+
} catch {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
private requireDb(): SqliteDatabaseLike {
|
|
170
|
+
if (!this.db) {
|
|
171
|
+
throw new Error('SQLite client not initialized. Call prepare() first.');
|
|
172
|
+
}
|
|
173
|
+
return this.db;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export const AGENT_APP_SQLITE_CLIENT_MODULE = 'renx-app-sqlite-client';
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# Agent-V4 CLI 应用层技术文档总览
|
|
2
|
+
|
|
3
|
+
本目录用于沉淀 `agent-v4` 的应用层实现设计,目标是让 `StatelessAgent` 内核可稳定服务 CLI 场景,并保持无状态架构原则。
|
|
4
|
+
|
|
5
|
+
## 文档清单(按编号)
|
|
6
|
+
|
|
7
|
+
- `01-scope-and-goals.md`:目标、范围、非目标、约束
|
|
8
|
+
- `02-architecture-overview.md`:总体分层架构与依赖方向
|
|
9
|
+
- `03-domain-model-and-contracts.md`:领域模型与核心数据契约
|
|
10
|
+
- `04-ports-and-interfaces.md`:应用层 Port 接口定义
|
|
11
|
+
- `05-run-orchestration-and-state-machine.md`:执行编排、状态机与时序
|
|
12
|
+
- `06-cli-commands-and-ux.md`:CLI 命令与交互规范
|
|
13
|
+
- `07-storage-design-local.md`:Phase 1 本地存储设计
|
|
14
|
+
- `08-error-and-observability.md`:错误策略、日志、指标、追踪
|
|
15
|
+
- `09-security-and-policy-boundary.md`:安全边界与策略层职责
|
|
16
|
+
- `10-test-plan-and-acceptance.md`:测试计划与验收标准
|
|
17
|
+
- `11-implementation-phases.md`:分阶段实施计划
|
|
18
|
+
- `12-open-questions-and-risks.md`:开放问题与风险登记
|
|
19
|
+
- `13-sqlite-schema-fields-and-rationale.md`:SQLite 表结构、字段释义与存储理由
|
|
20
|
+
- `14-project-flow-mermaid.md`:项目级详细流程图(Mermaid)
|
|
21
|
+
|
|
22
|
+
## 使用方式
|
|
23
|
+
|
|
24
|
+
1. 先阅读 `01` 到 `05`,统一架构和执行语义。
|
|
25
|
+
2. 再阅读 `06` 到 `09`,明确 CLI 落地方式与运维安全边界。
|
|
26
|
+
3. 使用 `10` 和 `11` 驱动开发计划与验收。
|
|
27
|
+
4. `12` 用于实现前对齐决策,避免反复返工。
|
|
28
|
+
|
|
29
|
+
## 当前状态
|
|
30
|
+
|
|
31
|
+
- 文档为“可实现级别”的设计稿。
|
|
32
|
+
- 默认不改动 `agent-v4` 内核契约。
|
|
33
|
+
- 应用层实现应以 Port + Adapter 方式扩展,避免反向侵入内核。
|
|
34
|
+
## 补充文档
|
|
35
|
+
|
|
36
|
+
- `15-openclaw-style-project-blueprint.md`:说明如何在 `agent-v4` 之上实现一个类似 OpenClaw 的网关化、多渠道、插件化系统。
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# 01. 范围与目标
|
|
2
|
+
|
|
3
|
+
## 1. 背景
|
|
4
|
+
|
|
5
|
+
`agent-v4` 已具备内核执行能力(LLM 调用、工具执行、流式事件、错误契约、超时预算)。当前缺口是可被 CLI 直接使用的应用层。
|
|
6
|
+
|
|
7
|
+
## 2. 目标
|
|
8
|
+
|
|
9
|
+
- 提供可复用的应用层入口,将内核能力组装为 CLI 可用服务。
|
|
10
|
+
- 保持无状态原则:执行真相进入存储,不依赖进程内全局状态。
|
|
11
|
+
- 支持前台流式执行(Phase 1),并为后续后台化与恢复预留接口。
|
|
12
|
+
- 提供清晰的执行生命周期:创建、运行、完成、失败、取消。
|
|
13
|
+
|
|
14
|
+
## 3. 范围(In Scope)
|
|
15
|
+
|
|
16
|
+
- 应用层服务接口:`runForeground/getRun/listRuns`。
|
|
17
|
+
- 事件桥接:`runStream` 流事件 + `onMessage/onCheckpoint/onCompaction/onError` 回调统一落库与发布。
|
|
18
|
+
- 本地存储实现(Phase 1 统一采用 SQLite,文件存储仅作历史兼容方案)。
|
|
19
|
+
- CLI 命令协议与输出事件模型。
|
|
20
|
+
- 测试与验收规范。
|
|
21
|
+
|
|
22
|
+
## 4. 非目标(Out of Scope)
|
|
23
|
+
|
|
24
|
+
- P0 级安全策略重构(如 `bash` 重写)
|
|
25
|
+
- 分布式任务队列与多 Worker 编排
|
|
26
|
+
- 跨主机高可用部署方案
|
|
27
|
+
- Redis/DB 幂等账本生产实现(仅保留接口)
|
|
28
|
+
|
|
29
|
+
## 5. 关键约束
|
|
30
|
+
|
|
31
|
+
- 不破坏 `StatelessAgent` 核心接口。
|
|
32
|
+
- 应用层必须依赖抽象 Port,不直接耦合具体存储细节。
|
|
33
|
+
- CLI 层仅做解析与展示,不承载业务编排逻辑。
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# 02. 总体架构
|
|
2
|
+
|
|
3
|
+
## 1. 分层
|
|
4
|
+
|
|
5
|
+
- Kernel Layer:`StatelessAgent` 与工具内核。
|
|
6
|
+
- Application Layer:执行编排、状态流转、回调桥接。
|
|
7
|
+
- Port Layer:存储、上下文、事件、账本抽象。
|
|
8
|
+
- Adapter Layer:本地文件/SQLite、CLI 事件输出实现。
|
|
9
|
+
- Interface Layer:CLI 命令。
|
|
10
|
+
|
|
11
|
+
## 2. 依赖方向
|
|
12
|
+
|
|
13
|
+
仅允许:`CLI -> App -> Port -> Adapter`,以及 `App -> Kernel`。
|
|
14
|
+
|
|
15
|
+
禁止:
|
|
16
|
+
- CLI 直接依赖 Kernel 内部模块。
|
|
17
|
+
- Adapter 直接调用 Kernel。
|
|
18
|
+
- Kernel 反向依赖 App/Adapter。
|
|
19
|
+
|
|
20
|
+
## 3. 核心设计原则
|
|
21
|
+
|
|
22
|
+
- 单一职责:内核只执行,应用层只编排。
|
|
23
|
+
- 无状态优先:跨执行状态一律外置到存储。
|
|
24
|
+
- 事件源优先:`runStream` 事件作为事实源,读模型可重建。
|
|
25
|
+
- 双通道桥接:`runStream` + `tool_chunk`(桥接为 `tool_stream`)。
|
|
26
|
+
- 事件驱动:统一 `CliEvent`,屏蔽内核事件差异。
|
|
27
|
+
- 可替换:所有外部依赖通过 Port 注入。
|
|
28
|
+
|
|
29
|
+
## 4. 逻辑结构图
|
|
30
|
+
|
|
31
|
+
- `AgentAppService`:外部 Facade。
|
|
32
|
+
- `RunOrchestrator`:单次执行调度器。
|
|
33
|
+
- `ExecutionStorePort`:执行状态持久化。
|
|
34
|
+
- `ExecutionStepStorePort`:步骤级状态持久化。
|
|
35
|
+
- `EventStorePort`:事件事实源持久化。
|
|
36
|
+
- `MessageProjectionStorePort`:消息读模型投影。
|
|
37
|
+
- `CheckpointStorePort`:检查点持久化。
|
|
38
|
+
- `ConversationStorePort`:会话存在性与元信息读取。
|
|
39
|
+
- `EventSinkPort`:事件发布。
|
|
40
|
+
- `ContextProviderPort`:上下文加载。
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# 03. 领域模型与数据契约
|
|
2
|
+
|
|
3
|
+
## 1. 核心实体
|
|
4
|
+
|
|
5
|
+
### RunRecord
|
|
6
|
+
- `executionId: string`
|
|
7
|
+
- `runId: string`(持久化层别名,默认与 `executionId` 一致)
|
|
8
|
+
- `conversationId: string`
|
|
9
|
+
- `status: CREATED | QUEUED | RUNNING | COMPLETED | FAILED | CANCELLED`(Phase 1 可不启用 `QUEUED`)
|
|
10
|
+
- `createdAt: number`
|
|
11
|
+
- `updatedAt: number`
|
|
12
|
+
- `stepIndex: number`
|
|
13
|
+
- `lastCheckpointSeq?: number`
|
|
14
|
+
- `terminalReason?: stop | max_steps | error | aborted | timeout | rate_limit | max_retries`
|
|
15
|
+
- `errorCode?: string`
|
|
16
|
+
- `errorCategory?: string`
|
|
17
|
+
- `errorMessage?: string`
|
|
18
|
+
|
|
19
|
+
### RunRequest
|
|
20
|
+
- `conversationId: string`
|
|
21
|
+
- `userInput: string`
|
|
22
|
+
- `executionId?: string`
|
|
23
|
+
- `maxSteps?: number`
|
|
24
|
+
|
|
25
|
+
### ExecutionStepRecord
|
|
26
|
+
- `executionId: string`
|
|
27
|
+
- `stepIndex: number`
|
|
28
|
+
- `status: pending | executing | completed | failed | skipped`
|
|
29
|
+
- `stage?: llm | tool | checkpoint`
|
|
30
|
+
- `errorCode?: string`
|
|
31
|
+
- `startedAt?: number`
|
|
32
|
+
- `completedAt?: number`
|
|
33
|
+
|
|
34
|
+
### ConversationRecord
|
|
35
|
+
- `conversationId: string`
|
|
36
|
+
- `title?: string`
|
|
37
|
+
- `status: active | archived`
|
|
38
|
+
- `workspacePath?: string`
|
|
39
|
+
- `createdAt: number`
|
|
40
|
+
- `updatedAt: number`
|
|
41
|
+
|
|
42
|
+
### CliEvent(内核 `StreamEvent` + 应用层扩展)
|
|
43
|
+
- `user_message`(应用层扩展)
|
|
44
|
+
- `assistant_message`(应用层扩展)
|
|
45
|
+
- `chunk`
|
|
46
|
+
- `reasoning_chunk`
|
|
47
|
+
- `tool_call`
|
|
48
|
+
- `tool_result`
|
|
49
|
+
- `tool_stream`(应用层由 `agent.on('tool_chunk')` 归一化桥接)
|
|
50
|
+
- `progress`
|
|
51
|
+
- `checkpoint`
|
|
52
|
+
- `compaction`
|
|
53
|
+
- `done`
|
|
54
|
+
- `error`
|
|
55
|
+
|
|
56
|
+
### CliEventEnvelope
|
|
57
|
+
- `conversationId: string`
|
|
58
|
+
- `executionId: string`
|
|
59
|
+
- `seq: number`(会话内单调递增)
|
|
60
|
+
- `eventType: CliEvent['type']`
|
|
61
|
+
- `data: unknown`
|
|
62
|
+
- `createdAt: number`
|
|
63
|
+
|
|
64
|
+
## 2. 终止语义与状态映射
|
|
65
|
+
|
|
66
|
+
- `done.finishReason=stop|max_steps` -> `RunRecord.status=COMPLETED`
|
|
67
|
+
- `error.errorCode=AGENT_ABORTED` -> `RunRecord.status=CANCELLED`,`terminalReason=aborted`
|
|
68
|
+
- `error.errorCode=AGENT_TIMEOUT_BUDGET_EXCEEDED` -> `RunRecord.status=FAILED`,`terminalReason=timeout`
|
|
69
|
+
- `error.errorCode=AGENT_UPSTREAM_TIMEOUT` -> `RunRecord.status=FAILED`,`terminalReason=timeout`
|
|
70
|
+
- `error.errorCode=AGENT_UPSTREAM_RATE_LIMIT` -> `RunRecord.status=FAILED`,`terminalReason=rate_limit`
|
|
71
|
+
- `error.errorCode=AGENT_MAX_RETRIES_REACHED` -> `RunRecord.status=FAILED`,`terminalReason=max_retries`
|
|
72
|
+
- 其他 `error` -> `RunRecord.status=FAILED`,`terminalReason=error`
|
|
73
|
+
|
|
74
|
+
## 3. 状态变更约束
|
|
75
|
+
|
|
76
|
+
- `createdAt` 不可变。
|
|
77
|
+
- `updatedAt` 每次状态更新必须刷新。
|
|
78
|
+
- `stepIndex` 只能增长。
|
|
79
|
+
- `COMPLETED/FAILED/CANCELLED` 为终态,不允许再切回 `RUNNING`。
|
|
80
|
+
|
|
81
|
+
## 4. ID 约束
|
|
82
|
+
|
|
83
|
+
- `executionId` 全局唯一(可采用 `trace_` 风格时间戳+随机串)。
|
|
84
|
+
- `conversationId` 由上层会话管理系统提供。
|
|
85
|
+
- `runId` 是存储层主键名;对外 CLI/API 统一使用 `executionId`。
|
|
86
|
+
|
|
87
|
+
## 5. 兼容性约束
|
|
88
|
+
|
|
89
|
+
- 应用层事件必须保留内核错误包:`errorCode/category/httpStatus/retryable`。
|
|
90
|
+
- 对外事件字段采用增量扩展,不删除既有字段。
|
|
91
|
+
- 应用层的持久化事实源以 `events` 为准,`messages` 为投影视图。
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# 04. Port 接口设计
|
|
2
|
+
|
|
3
|
+
## 1. ExecutionStorePort
|
|
4
|
+
|
|
5
|
+
职责:执行生命周期持久化。
|
|
6
|
+
|
|
7
|
+
接口:
|
|
8
|
+
- `create(run: RunRecord): Promise<void>`
|
|
9
|
+
- `patch(executionId: string, patch: Partial<RunRecord>): Promise<void>`
|
|
10
|
+
- `get(executionId: string): Promise<RunRecord | null>`
|
|
11
|
+
- `listByConversation(conversationId: string, opts?: { statuses?: RunRecord['status'][]; limit?: number; cursor?: string }): Promise<{ items: RunRecord[]; nextCursor?: string }>`
|
|
12
|
+
|
|
13
|
+
## 2. EventStorePort(事实源,必需)
|
|
14
|
+
|
|
15
|
+
职责:持久化 `runStream` 与回调产生的原始事件。
|
|
16
|
+
|
|
17
|
+
接口:
|
|
18
|
+
- `appendAutoSeq(event: Omit<CliEventEnvelope, 'seq'>): Promise<CliEventEnvelope>`
|
|
19
|
+
- `append(event: CliEventEnvelope): Promise<void>`(仅回放/导入使用)
|
|
20
|
+
- `listByRun(executionId: string): Promise<CliEventEnvelope[]>`
|
|
21
|
+
- `listByConversation(conversationId: string, opts?: { fromSeq?: number; limit?: number }): Promise<CliEventEnvelope[]>`
|
|
22
|
+
|
|
23
|
+
约束:
|
|
24
|
+
- append-only,不允许覆盖历史事件。
|
|
25
|
+
- 会话内序号必须单调递增。
|
|
26
|
+
- `appendAutoSeq` 必须在单事务内原子分配 `seq`(禁止先查后写)。
|
|
27
|
+
|
|
28
|
+
## 3. MessageProjectionStorePort(投影层,可选但推荐)
|
|
29
|
+
|
|
30
|
+
职责:维护消息读模型(由事件投影构建)。
|
|
31
|
+
|
|
32
|
+
接口:
|
|
33
|
+
- `upsertFromEvent(event: CliEventEnvelope): Promise<void>`
|
|
34
|
+
- `list(conversationId: string): Promise<Message[]>`
|
|
35
|
+
|
|
36
|
+
## 4. ExecutionStepStorePort(推荐)
|
|
37
|
+
|
|
38
|
+
职责:步骤级状态明细(`run-status --verbose` 与调试)。
|
|
39
|
+
|
|
40
|
+
接口:
|
|
41
|
+
- `upsert(step: ExecutionStepRecord): Promise<void>`
|
|
42
|
+
- `listByRun(executionId: string, opts?: { limit?: number }): Promise<ExecutionStepRecord[]>`
|
|
43
|
+
- `listLatestByRuns(executionIds: string[]): Promise<Record<string, ExecutionStepRecord | undefined>>`
|
|
44
|
+
|
|
45
|
+
## 5. CheckpointStorePort(可选)
|
|
46
|
+
|
|
47
|
+
职责:断点恢复位置存储。
|
|
48
|
+
|
|
49
|
+
接口:
|
|
50
|
+
- `save(checkpoint: ExecutionCheckpoint): Promise<void>`
|
|
51
|
+
- `getLatest(executionId: string): Promise<ExecutionCheckpoint | null>`
|
|
52
|
+
- `clear(executionId: string): Promise<void>`
|
|
53
|
+
|
|
54
|
+
说明:
|
|
55
|
+
- 若不建独立 `checkpoints` 表,可从 `events(event_type=checkpoint)` 投影实现。
|
|
56
|
+
|
|
57
|
+
## 6. ContextSnapshotStorePort(推荐)
|
|
58
|
+
|
|
59
|
+
职责:存储每步实际喂给模型的上下文快照。
|
|
60
|
+
|
|
61
|
+
接口:
|
|
62
|
+
- `saveSnapshot(snapshot: ContextSnapshotRecord, items: ContextSnapshotItemRecord[]): Promise<void>`
|
|
63
|
+
- `getByRun(executionId: string): Promise<ContextSnapshotRecord[]>`
|
|
64
|
+
|
|
65
|
+
## 7. SummaryStorePort(推荐)
|
|
66
|
+
|
|
67
|
+
职责:存储压缩摘要产物。
|
|
68
|
+
|
|
69
|
+
接口:
|
|
70
|
+
- `append(summary: SummaryRecord): Promise<void>`
|
|
71
|
+
- `listByConversation(conversationId: string): Promise<SummaryRecord[]>`
|
|
72
|
+
|
|
73
|
+
## 8. RunLogStorePort(可选)
|
|
74
|
+
|
|
75
|
+
职责:存储运维技术日志(`warn/error` 为主)。
|
|
76
|
+
|
|
77
|
+
接口:
|
|
78
|
+
- `append(log: RunLogRecord): Promise<void>`
|
|
79
|
+
- `listByRun(executionId: string, opts?: { level?: string; limit?: number }): Promise<RunLogRecord[]>`
|
|
80
|
+
|
|
81
|
+
## 9. ConversationStorePort(推荐)
|
|
82
|
+
|
|
83
|
+
职责:会话存在性与会话元信息读取。
|
|
84
|
+
|
|
85
|
+
接口:
|
|
86
|
+
- `exists(conversationId: string): Promise<boolean>`
|
|
87
|
+
- `get(conversationId: string): Promise<ConversationRecord | null>`
|
|
88
|
+
|
|
89
|
+
## 10. ContextProviderPort
|
|
90
|
+
|
|
91
|
+
职责:加载会话上下文输入。
|
|
92
|
+
|
|
93
|
+
接口:
|
|
94
|
+
- `load(conversationId: string): Promise<{ messages: Message[]; systemPrompt?: string; tools?: Tool[] }>`
|
|
95
|
+
|
|
96
|
+
## 11. EventSinkPort
|
|
97
|
+
|
|
98
|
+
职责:发布应用层事件到 CLI。
|
|
99
|
+
|
|
100
|
+
接口:
|
|
101
|
+
- `publish(executionId: string, event: CliEvent): Promise<void>`
|
|
102
|
+
- `publishToolStream(executionId: string, chunk: { toolCallId: string; chunkType: 'stdout' | 'stderr' | 'progress'; chunk: string }): Promise<void>`
|
|
103
|
+
|
|
104
|
+
说明:
|
|
105
|
+
- `tool_stream` 来自 `agent.on('tool_chunk')` 桥接,不是当前 `runStream` 直接产物。
|
|
106
|
+
|
|
107
|
+
## 12. LedgerProviderPort(预留)
|
|
108
|
+
|
|
109
|
+
职责:提供可选幂等账本实现。
|
|
110
|
+
|
|
111
|
+
接口:
|
|
112
|
+
- `getLedger(conversationId: string): ToolExecutionLedger`
|
|
113
|
+
|
|
114
|
+
约束:
|
|
115
|
+
- 默认返回 `NoopToolExecutionLedger`。
|
|
116
|
+
- 生产环境可注入 Redis/DB 账本。
|
package/vendor/agent-root/src/agent/docs/cli-app-layer/05-run-orchestration-and-state-machine.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# 05. 执行编排与状态机
|
|
2
|
+
|
|
3
|
+
## 1. RunOrchestrator 职责
|
|
4
|
+
|
|
5
|
+
- 构建单次执行 `AgentInput`。
|
|
6
|
+
- 消费 `runStream` 事件并桥接到存储与 CLI。
|
|
7
|
+
- 消费 `agent.on('tool_chunk')` 并桥接为 `tool_stream`。
|
|
8
|
+
- 处理 `onMessage/onCheckpoint/onCompaction/onError` 回调。
|
|
9
|
+
- 维护运行状态机并保证终态收敛。
|
|
10
|
+
|
|
11
|
+
## 2. 执行主流程(事件源优先)
|
|
12
|
+
|
|
13
|
+
1. 创建 `RunRecord(CREATED)`。
|
|
14
|
+
2. 读取上下文与历史消息。
|
|
15
|
+
3. 追加用户输入(写 `events`,并更新消息投影)。
|
|
16
|
+
4. 更新状态为 `RUNNING`。
|
|
17
|
+
5. 调用 `runStream`,逐条消费事件并写 `EventStore`。
|
|
18
|
+
6. 并行消费 `tool_chunk`,归一化为 `tool_stream` 后写 `EventStore`。
|
|
19
|
+
7. 对关键事件更新投影与状态:
|
|
20
|
+
- `progress`:更新 `stepIndex`
|
|
21
|
+
- `checkpoint`:保存 checkpoint
|
|
22
|
+
- `done/error`:先写 `events`,再收敛终态
|
|
23
|
+
8. 终态后停止接收后续事件并发布最终结果。
|
|
24
|
+
|
|
25
|
+
## 3. 桥接规则(按真实内核行为)
|
|
26
|
+
|
|
27
|
+
- `runStream event=*`:`EventStore.append` + `EventSink.publish`(当前不含 `tool_stream`)
|
|
28
|
+
- `agent.on('tool_chunk')`:归一化为 `tool_stream`,再 `EventStore.append` + `EventSink.publish`
|
|
29
|
+
- `onMessage`:补充消息持久化(或校验事件投影一致性)
|
|
30
|
+
- `onCheckpoint`:`CheckpointStore.save` + `ExecutionStore.patch(stepIndex)`
|
|
31
|
+
- `onCompaction`:仅更新摘要/上下文快照派生层(不重复 append 事件)
|
|
32
|
+
- `onError`:记录错误决策,不直接替代终态收敛逻辑
|
|
33
|
+
|
|
34
|
+
说明:
|
|
35
|
+
- `progress` 以 `runStream` 事件为准,不依赖 `callbacks.onProgress`。
|
|
36
|
+
- 终态收敛遵循“`events` 先写,`runs` 后更新”。
|
|
37
|
+
|
|
38
|
+
## 4. 状态机与终态映射
|
|
39
|
+
|
|
40
|
+
- CREATED -> RUNNING
|
|
41
|
+
- RUNNING -> COMPLETED(`done.finishReason=stop|max_steps`)
|
|
42
|
+
- RUNNING -> FAILED(`error` 且非 `AGENT_ABORTED`)
|
|
43
|
+
- RUNNING -> CANCELLED(`error.errorCode=AGENT_ABORTED`)
|
|
44
|
+
|
|
45
|
+
非法转换应拒绝并记录告警日志。
|
|
46
|
+
|
|
47
|
+
## 5. 并发与幂等
|
|
48
|
+
|
|
49
|
+
- 同 `executionId` 并发启动应被拒绝或去重。
|
|
50
|
+
- `toolExecutionLedger` 是否缓存由注入实现决定。
|
|
51
|
+
- 应用层不得假定默认有跨进程幂等能力。
|
|
52
|
+
- 事实层(`events`)必须可重放,投影层可重建。
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# 06. CLI 命令与交互规范
|
|
2
|
+
|
|
3
|
+
## 1. 命令集合(Phase 1)
|
|
4
|
+
|
|
5
|
+
- `agent run --conversation <id> --message "..."`
|
|
6
|
+
- `agent run-status --execution <id>`
|
|
7
|
+
- `agent run-list --conversation <id>`
|
|
8
|
+
|
|
9
|
+
## 2. 输出策略
|
|
10
|
+
|
|
11
|
+
- `chunk`:实时增量输出文本。
|
|
12
|
+
- `reasoning_chunk`:默认隐藏,可通过 flag 打开。
|
|
13
|
+
- `tool_stream`:工具 stdout/stderr/progress 流(由 `tool_chunk` 桥接,默认紧凑显示)。
|
|
14
|
+
- `tool_call/tool_result`:结构化一行输出。
|
|
15
|
+
- `progress`:紧凑模式刷新(可覆盖上行)。
|
|
16
|
+
- `compaction`:输出压缩动作摘要(移除条数、stepIndex)。
|
|
17
|
+
- `done/error`:尾部输出总结。
|
|
18
|
+
|
|
19
|
+
## 3. 退出码规范
|
|
20
|
+
|
|
21
|
+
- `0`:完成(含 `finishReason=max_steps`)。
|
|
22
|
+
- `1`:运行失败。
|
|
23
|
+
- `130`:用户中断(SIGINT)。
|
|
24
|
+
|
|
25
|
+
终态映射建议:
|
|
26
|
+
- `done.stop` -> `COMPLETED` + `0`
|
|
27
|
+
- `done.max_steps` -> `COMPLETED` + `0`(提示可 `resume`)
|
|
28
|
+
- `error.AGENT_ABORTED` -> `CANCELLED` + `130`
|
|
29
|
+
- `error.AGENT_TIMEOUT_BUDGET_EXCEEDED` -> `FAILED(timeout)` + `1`
|
|
30
|
+
- `error.AGENT_MAX_RETRIES_REACHED` -> `FAILED(max_retries)` + `1`
|
|
31
|
+
- 其他 `error` -> `FAILED(error)` + `1`
|
|
32
|
+
|
|
33
|
+
## 4. 可观测输出字段
|
|
34
|
+
|
|
35
|
+
推荐统一打印:
|
|
36
|
+
- `executionId`
|
|
37
|
+
- `stepIndex`
|
|
38
|
+
- `eventType`
|
|
39
|
+
- `toolCallId`(可选)
|
|
40
|
+
- `errorCode`(错误时)
|
|
41
|
+
|
|
42
|
+
## 5. 查询命令输出约定(`run-status` / `run-list`)
|
|
43
|
+
|
|
44
|
+
- `run-status` 默认输出:`status/terminalReason/stepIndex/createdAt/updatedAt/completedAt`。
|
|
45
|
+
- `run-status --verbose` 追加:最近 `execution_steps` 与尾部 `events`。
|
|
46
|
+
- `run-status --watch`:按固定间隔刷新,终态后自动退出。
|
|
47
|
+
- `run-list` 默认输出:`executionId/status/terminalReason/stepIndex/updatedAt`(按 `updatedAt` 倒序)。
|
|
48
|
+
- `run-list --cursor <token>`:分页读取下一页;无下一页时返回空 `next_cursor`。
|
|
49
|
+
|
|
50
|
+
建议错误码:
|
|
51
|
+
- 参数错误:`2`
|
|
52
|
+
- 资源不存在(例如 execution 不存在):`2`
|
|
53
|
+
- 查询成功(含空列表):`0`
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# 07. Phase 1 本地存储设计(SQLite 基线)
|
|
2
|
+
|
|
3
|
+
## 1. 目标
|
|
4
|
+
|
|
5
|
+
在不引入外部基础设施的前提下,让 CLI 应用层可落地并支持:
|
|
6
|
+
|
|
7
|
+
- 事件级回放
|
|
8
|
+
- 运行状态查询
|
|
9
|
+
- 断点与上下文可追溯
|
|
10
|
+
|
|
11
|
+
## 2. 存储位置
|
|
12
|
+
|
|
13
|
+
`<workspace>/.agent-v4/agent.db`
|
|
14
|
+
|
|
15
|
+
说明:
|
|
16
|
+
- Phase 1 采用单库 SQLite。
|
|
17
|
+
- JSON/JSONL 文件存储仅保留为迁移兼容,不作为默认主路径。
|
|
18
|
+
|
|
19
|
+
## 3. 最小必需表(Phase 1)
|
|
20
|
+
|
|
21
|
+
- `runs`:执行状态
|
|
22
|
+
- `events`:事实事件流(真相源)
|
|
23
|
+
- `messages`:消息读模型(投影)
|
|
24
|
+
- `tool_ledger`:工具幂等账本(可选注入)
|
|
25
|
+
- `checkpoints`:断点恢复索引(推荐独立表;可由 `events` 投影重建)
|
|
26
|
+
|
|
27
|
+
建议同时落:
|
|
28
|
+
- `meta`(schema/version/feature flags)
|
|
29
|
+
|
|
30
|
+
## 4. 写入策略
|
|
31
|
+
|
|
32
|
+
- 统一事务:`events` 先写,再更新投影表(`messages/runs/...`)。
|
|
33
|
+
- append-only:`events` 不更新不删除(仅归档/TTL 清理)。
|
|
34
|
+
- 状态更新幂等:重复写同一终态不报错。
|
|
35
|
+
- 开启 SQLite 保护:
|
|
36
|
+
- `PRAGMA journal_mode=WAL;`
|
|
37
|
+
- `PRAGMA busy_timeout=5000;`
|
|
38
|
+
- `PRAGMA foreign_keys=ON;`
|
|
39
|
+
|
|
40
|
+
## 5. 读取策略
|
|
41
|
+
|
|
42
|
+
- `run-status`:查询 `runs`。
|
|
43
|
+
- `run-list`:按 `conversationId + updatedAt` 查询 `runs`。
|
|
44
|
+
- 会话重建:
|
|
45
|
+
- 优先读 `messages`(快)
|
|
46
|
+
- 需要严格审计时读 `events` 并重放(准)
|
|
47
|
+
|
|
48
|
+
## 6. 兼容与升级
|
|
49
|
+
|
|
50
|
+
- 使用 `meta` / `schema_migrations` 管理版本。
|
|
51
|
+
- 投影可重建:当 `messages` 损坏时从 `events` 重算。
|
|
52
|
+
- 后续切换 Redis/DB 时,Port 接口保持不变。
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# 08. 错误与可观测性设计
|
|
2
|
+
|
|
3
|
+
## 1. 错误模型
|
|
4
|
+
|
|
5
|
+
沿用 `agent-v4` 错误契约:
|
|
6
|
+
- `errorCode`
|
|
7
|
+
- `category`
|
|
8
|
+
- `retryable`
|
|
9
|
+
- `httpStatus`
|
|
10
|
+
|
|
11
|
+
应用层新增:
|
|
12
|
+
- `executionId`
|
|
13
|
+
- `stage`(bootstrap/run/callback/persist)
|
|
14
|
+
|
|
15
|
+
## 2. 错误处理原则
|
|
16
|
+
|
|
17
|
+
- 内核错误:保留原始错误码,不做语义降级。
|
|
18
|
+
- 存储错误:标记 `APP_STORAGE_*`,并尽量不中断 stdout 事件流。
|
|
19
|
+
- 回调错误:不传播到用户主流程,但要记录日志。
|
|
20
|
+
|
|
21
|
+
## 3. 日志规范
|
|
22
|
+
|
|
23
|
+
每条日志最小字段:
|
|
24
|
+
- `executionId`
|
|
25
|
+
- `conversationId`
|
|
26
|
+
- `stepIndex`
|
|
27
|
+
- `event`
|
|
28
|
+
- `latencyMs`
|
|
29
|
+
- `errorCode`
|
|
30
|
+
|
|
31
|
+
落库建议:
|
|
32
|
+
- 业务事实进入 `events`(真相源)。
|
|
33
|
+
- 运维技术日志进入 `run_logs`(可选,默认 `warn/error`)。
|
|
34
|
+
|
|
35
|
+
## 4. 指标建议(后续)
|
|
36
|
+
|
|
37
|
+
- `app.run.duration_ms`
|
|
38
|
+
- `app.run.error.count`
|
|
39
|
+
- `app.store.write.error.count`
|
|
40
|
+
- `app.cli.render.delay_ms`
|