bailinghub 0.3.4 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitignore +5 -0
- package/README.en.md +6 -1
- package/README.md +6 -0
- package/SECURITY.md +11 -0
- package/docker-compose.images.yml +2 -2
- package/docs/AGENT_AUTH_API.en.md +175 -0
- package/docs/AGENT_AUTH_API.md +190 -0
- package/docs/AGENT_CLIENT_QUICKSTART.en.md +212 -0
- package/docs/AGENT_CLIENT_QUICKSTART.md +206 -0
- package/docs/AGENT_CLIENT_RUNTIME_API.en.md +157 -0
- package/docs/AGENT_CLIENT_RUNTIME_API.md +107 -0
- package/docs/CHANGELOG.en.md +59 -1
- package/docs/CHANGELOG.md +27 -1
- package/docs/COMPATIBILITY.en.md +25 -0
- package/docs/CONTRACT.en.md +79 -8
- package/docs/CONTRACT.md +36 -6
- package/docs/INDEPENDENT_VALIDATION.en.md +4 -4
- package/docs/INDEPENDENT_VALIDATION.md +4 -4
- package/docs/QUICKSTART.en.md +6 -0
- package/docs/QUICKSTART.md +1 -1
- package/docs/README.en.md +8 -0
- package/docs/README.md +8 -0
- package/docs/RELEASE_NOTES_v0.4.0.en.md +39 -0
- package/docs/RELEASE_NOTES_v0.4.0.md +39 -0
- package/docs/RELEASE_NOTES_v0.5.0.en.md +41 -0
- package/docs/RELEASE_NOTES_v0.5.0.md +41 -0
- package/docs/STREAMING.en.md +32 -0
- package/docs/STREAMING.md +8 -2
- package/docs/WIDGET_RENDERERS.en.md +123 -6
- package/docs/WIDGET_RENDERERS.md +75 -6
- package/docs/user-guide/console-map.en.md +1 -2
- package/docs/user-guide/console-map.md +2 -2
- package/docs//345/205/274/345/256/271/346/200/247/344/270/216/345/215/207/347/272/247.md +9 -0
- package/package.json +3 -2
- package/schemas/config/chat-entry.schema.json +2 -1
- package/schemas/config/client.schema.json +7 -0
- package/schemas/config/common.schema.json +30 -0
- package/schemas/config/route.schema.json +11 -0
- package/scripts/check-doc-links.mjs +1 -0
- package/scripts/check-image-tags.sh +1 -1
- package/scripts/check-release-consistency.test.mjs +5 -5
- package/scripts/e2e-console.mjs +144 -0
- package/scripts/export-oss.mjs +5 -12
- package/scripts/install.sh +1 -1
- package/scripts/public-content-denylist.mjs +82 -0
- package/scripts/public-content-denylist.test.mjs +53 -0
- package/scripts/publish-images.sh +1 -1
- package/scripts/release-audit.mjs +8 -12
- package/scripts/sdk-runtime-test.mjs +171 -7
- package/scripts/security-scan.mjs +8 -5
- package/scripts/validate-examples.mjs +3 -1
- package/scripts/verify-github-repo.mjs +7 -11
- package/scripts/verify-oss-export.mjs +6 -12
- package/scripts/widget-renderer-test.mjs +244 -16
- package/sdk/php/README.md +27 -1
- package/sdk/php/src/AgentAuth.php +78 -0
- package/sdk/php/src/HubClient.php +3 -0
- package/sdk/php7/README.md +32 -2
- package/sdk/php7/src/AgentAuth.php +77 -0
- package/sdk/php7/src/HubClient.php +3 -0
- package/sql/055_agent_auth.sql +72 -0
- package/sql/056_agent_client_runtime.sql +60 -0
- package/src/adapters/channels/wecom-api.ts +1 -1
- package/src/app/agent-client-runtime.test.ts +221 -0
- package/src/app/agent-client-runtime.ts +408 -0
- package/src/app/agent-tool-invocations.test.ts +462 -0
- package/src/app/agent-tool-invocations.ts +795 -0
- package/src/app/agent-tool-job.test.ts +62 -0
- package/src/app/agent-tool-job.ts +41 -0
- package/src/app/auth.ts +2 -1
- package/src/app/kernel.ts +2 -0
- package/src/app/runtime-context.ts +8 -0
- package/src/app/tool-approvals.ts +5 -1
- package/src/app/tool-assembly.ts +19 -1
- package/src/app/tool-context.ts +2 -1
- package/src/core/config/config-codec.ts +2 -1
- package/src/core/config/config-models.test.ts +28 -1
- package/src/core/config/config-models.ts +46 -3
- package/src/core/config/config-schema.test.ts +8 -0
- package/src/core/config/route-config.test.ts +15 -0
- package/src/core/config/route-config.ts +36 -0
- package/src/core/config/tools-config.test.ts +58 -1
- package/src/core/config/tools-config.ts +84 -0
- package/src/core/contracts/tools.ts +162 -29
- package/src/core/contracts/types.ts +38 -1
- package/src/core/platform/chat-form.test.ts +147 -0
- package/src/core/platform/chat-form.ts +402 -0
- package/src/core/platform/version.ts +1 -1
- package/src/core/runtime/agent-client-trace.test.ts +154 -0
- package/src/core/runtime/agent-client-trace.ts +288 -0
- package/src/core/runtime/execution-runtime.test.ts +61 -0
- package/src/core/runtime/execution-runtime.ts +17 -0
- package/src/core/runtime/launch-runtime.ts +13 -0
- package/src/core/runtime/trace-runtime.test.ts +18 -1
- package/src/core/runtime/trace-runtime.ts +28 -3
- package/src/core/state/state-codec.ts +2 -0
- package/src/infrastructure/config/config-agent-auth-repository.test.ts +147 -0
- package/src/infrastructure/config/config-agent-auth-repository.ts +356 -0
- package/src/infrastructure/config/config-agent-client-runtime-repository.ts +253 -0
- package/src/infrastructure/config/config-approval-ledger.test.ts +32 -0
- package/src/infrastructure/config/config-approval-ledger.ts +16 -0
- package/src/infrastructure/config/config-client-repository.ts +3 -3
- package/src/infrastructure/config/config-conversation-ledger.test.ts +69 -0
- package/src/infrastructure/config/config-conversation-ledger.ts +8 -7
- package/src/infrastructure/config/config-observability-ledger.test.ts +50 -0
- package/src/infrastructure/config/config-observability-ledger.ts +65 -10
- package/src/infrastructure/config/config-route-repository.ts +4 -3
- package/src/infrastructure/config/configstore.ts +14 -3
- package/src/infrastructure/state/state-mysql.ts +2 -2
- package/src/kernel-api/v1/contracts.ts +8 -0
- package/src/routes/admin-runtime.test.ts +214 -0
- package/src/routes/admin-runtime.ts +82 -2
- package/src/routes/agent-api.test.ts +379 -0
- package/src/routes/agent-api.ts +251 -0
- package/src/routes/agent-auth.test.ts +276 -0
- package/src/routes/agent-auth.ts +395 -0
- package/src/routes/approvals.ts +4 -1
- package/src/routes/chat.test.ts +319 -7
- package/src/routes/chat.ts +166 -21
- package/src/routes/critical-routes.test.ts +235 -1
- package/src/routes/private-default.ts +2 -1
- package/src/routes/private.ts +16 -1
- package/src/routes/run.ts +79 -13
- package/web/connect/bailing-connect-php.tgz +0 -0
- package/web/connect/bailing-connect-php7.tgz +0 -0
- package/web/console/assets/{Accounts-CAwrtBiS.js → Accounts-D4RRm5D1.js} +1 -1
- package/web/console/assets/{Approvals-C2BZ86T3.js → Approvals-CsTTKb8o.js} +1 -1
- package/web/console/assets/{Audit-B-E4Y_Up.js → Audit-BzvZ7JZw.js} +1 -1
- package/web/console/assets/{BrandLockup-C18u8Lx5.js → BrandLockup-CYcA93wh.js} +1 -1
- package/web/console/assets/{Channels-DTy8ylH-.js → Channels-DRQZggqM.js} +1 -1
- package/web/console/assets/ChatEntries-BUB7BWKU.js +7 -0
- package/web/console/assets/ChatEntries-DsYtcXN6.css +1 -0
- package/web/console/assets/Clients-CQ_MonGL.css +1 -0
- package/web/console/assets/Clients-HhCyrxfz.js +3 -0
- package/web/console/assets/{Cost-2SSYs1AT.js → Cost-BZgFf1yM.js} +1 -1
- package/web/console/assets/{Credentials-B79LtxCE.js → Credentials-BTe2Q07j.js} +1 -1
- package/web/console/assets/{Diagnostics-BakcRYvb.js → Diagnostics-WL-buYli.js} +1 -1
- package/web/console/assets/{Executors-BXoyo28u.js → Executors-CGZ-7Xmp.js} +1 -1
- package/web/console/assets/{Kb-ISbzquab.js → Kb-VVw8oGsT.js} +1 -1
- package/web/console/assets/{Login-Dlu6Wgcf.js → Login-rwvmBBPl.js} +1 -1
- package/web/console/assets/{Projects-DFzmtnqg.js → Projects-CexaCEY1.js} +1 -1
- package/web/console/assets/Routes-B9ZHbQAp.js +124 -0
- package/web/console/assets/Routes-BbkFpi6Y.css +1 -0
- package/web/console/assets/Runs-4zDD9VOC.css +1 -0
- package/web/console/assets/Runs-BzFNPTwD.js +3 -0
- package/web/console/assets/{Setup-CrBAV-b9.js → Setup-Djh5yadQ.js} +1 -1
- package/web/console/assets/{Shell-B1a8DvXQ.js → Shell-kn763smL.js} +1 -1
- package/web/console/assets/{StorageBuckets-BRfL_eHb.js → StorageBuckets-CQBR41W1.js} +1 -1
- package/web/console/assets/{SystemSettings-CyirVLbq.js → SystemSettings-DgnopekD.js} +1 -1
- package/web/console/assets/{SystemStatus-DZ_OR7sk.js → SystemStatus-Bj8-kPt2.js} +1 -1
- package/web/console/assets/{Targets-CtIMa66c.js → Targets-BAfCt_nH.js} +1 -1
- package/web/console/assets/{Tools-BfDZHt01.js → Tools-xjd0NZdE.js} +1 -1
- package/web/console/assets/{index-DBiByFiO.js → index-DfJA7iJS.js} +2 -2
- package/web/console/assets/{request-DY2cxQqZ.js → request-DkNPOweH.js} +1 -1
- package/web/console/assets/{schema-6mCQ7rR5.js → schema-CgPWAu1_.js} +1 -1
- package/web/console/index.html +1 -1
- package/web/widget/widget.js +493 -14
- package/web-admin/src/pages/ChatEntries.vue +18 -6
- package/web-admin/src/pages/Clients.vue +11 -3
- package/web-admin/src/pages/Routes.vue +282 -16
- package/web-admin/src/pages/Runs.vue +159 -38
- package/web/console/assets/ChatEntries-9xbRiQov.css +0 -1
- package/web/console/assets/ChatEntries-DvD-4IeE.js +0 -7
- package/web/console/assets/Clients--D7NQJKk.css +0 -1
- package/web/console/assets/Clients-DWs9JFr0.js +0 -3
- package/web/console/assets/Routes-B-hm9HGY.js +0 -124
- package/web/console/assets/Routes-DWGQN5Pv.css +0 -1
- package/web/console/assets/Runs-CUtSsPW-.css +0 -1
- package/web/console/assets/Runs-hMRu1im3.js +0 -3
|
@@ -24,6 +24,8 @@ import type { ToolEmbeddingRepository } from '../../services/tool-index-reposito
|
|
|
24
24
|
import { MysqlKbDatasourceRepository, MysqlKnowledgeRepository } from './config-knowledge-repository';
|
|
25
25
|
import { InstanceBrandingRepository } from './config-instance-branding-repository';
|
|
26
26
|
import type { InstanceBrandingRepositoryContract } from './config-instance-branding-repository';
|
|
27
|
+
import { AgentAuthRepository, type AgentAuthRepositoryContract } from './config-agent-auth-repository';
|
|
28
|
+
import { AgentClientRuntimeRepository } from './config-agent-client-runtime-repository';
|
|
27
29
|
import { MysqlPoolOwner, type MysqlPoolResource } from '../mysql/pool-owner';
|
|
28
30
|
|
|
29
31
|
export type RouteRepositoryContract = Pick<RouteRepository, keyof RouteRepository>;
|
|
@@ -42,7 +44,9 @@ export type StorageBucketRepositoryContract = Pick<StorageBucketRepository, keyo
|
|
|
42
44
|
export type AlertRuleRepositoryContract = Pick<AlertRuleRepository, keyof AlertRuleRepository>;
|
|
43
45
|
export type ChatConfigRepositoryContract = Pick<ChatConfigRepository, keyof ChatConfigRepository>;
|
|
44
46
|
export type RateLimitLedgerContract = Pick<RateLimitLedger, keyof RateLimitLedger>;
|
|
45
|
-
export type ApprovalLedgerContract =
|
|
47
|
+
export type ApprovalLedgerContract =
|
|
48
|
+
Omit<Pick<ApprovalLedger, keyof ApprovalLedger>, 'forJobs'>
|
|
49
|
+
& Partial<Pick<ApprovalLedger, 'forJobs'>>;
|
|
46
50
|
export type ConversationLedgerContract = Pick<ConversationLedger, keyof ConversationLedger>;
|
|
47
51
|
export type ExecutorLedgerContract = Pick<ExecutorLedger, keyof ExecutorLedger>;
|
|
48
52
|
export type ToolCallLedgerContract = Pick<ToolCallLedger, keyof ToolCallLedger>;
|
|
@@ -53,8 +57,9 @@ export type KbDatasourceRepositoryContract = Pick<MysqlKbDatasourceRepository, k
|
|
|
53
57
|
export type { InstanceBrandingRepositoryContract };
|
|
54
58
|
export type DeliveryDlqLedgerContract = Pick<DeliveryDlqLedger, keyof DeliveryDlqLedger>;
|
|
55
59
|
export type ObservabilityLedgerContract =
|
|
56
|
-
Omit<Pick<ObservabilityLedger, keyof ObservabilityLedger>, 'operationalMetricsSnapshot'>
|
|
57
|
-
& Partial<Pick<ObservabilityLedger, 'operationalMetricsSnapshot'>>;
|
|
60
|
+
Omit<Pick<ObservabilityLedger, keyof ObservabilityLedger>, 'operationalMetricsSnapshot' | 'agentToolJobCandidatesForRun' | 'auditsForJobs'>
|
|
61
|
+
& Partial<Pick<ObservabilityLedger, 'operationalMetricsSnapshot' | 'agentToolJobCandidatesForRun' | 'auditsForJobs'>>;
|
|
62
|
+
export type AgentClientRuntimeRepositoryContract = Pick<AgentClientRuntimeRepository, keyof AgentClientRuntimeRepository>;
|
|
58
63
|
|
|
59
64
|
export interface ConfigStoreContract {
|
|
60
65
|
readonly routes: RouteRepositoryContract;
|
|
@@ -80,6 +85,10 @@ export interface ConfigStoreContract {
|
|
|
80
85
|
readonly instanceBranding: InstanceBrandingRepositoryContract;
|
|
81
86
|
readonly deliveryDlq: DeliveryDlqLedgerContract;
|
|
82
87
|
readonly observability: ObservabilityLedgerContract;
|
|
88
|
+
/** 新 Agent Auth 能力对旧宿主仓储保持可选;缺失时 HTTP 面 fail closed。 */
|
|
89
|
+
readonly agentAuth?: AgentAuthRepositoryContract;
|
|
90
|
+
/** Agent Client Runtime v1 对旧宿主保持可选;缺失时新 API fail closed。 */
|
|
91
|
+
readonly agentClientRuntime?: AgentClientRuntimeRepositoryContract;
|
|
83
92
|
init(): Promise<void>;
|
|
84
93
|
close?(): Promise<void>;
|
|
85
94
|
readonly db: Pool;
|
|
@@ -112,6 +121,8 @@ export class ConfigStore implements ConfigStoreContract {
|
|
|
112
121
|
readonly instanceBranding = new InstanceBrandingRepository(() => this.pool);
|
|
113
122
|
readonly deliveryDlq = new DeliveryDlqLedger(() => this.pool);
|
|
114
123
|
readonly observability = new ObservabilityLedger(() => this.pool);
|
|
124
|
+
readonly agentAuth = new AgentAuthRepository(() => this.pool);
|
|
125
|
+
readonly agentClientRuntime = new AgentClientRuntimeRepository(() => this.pool);
|
|
115
126
|
|
|
116
127
|
constructor(cfg: AppConfig['state']['mysql'], poolOwner?: MysqlPoolResource) {
|
|
117
128
|
this.poolOwner = poolOwner ?? new MysqlPoolOwner(cfg);
|
|
@@ -33,10 +33,10 @@ export class MysqlStore implements RuntimeStateStore {
|
|
|
33
33
|
|
|
34
34
|
async createJob(job: Job): Promise<void> {
|
|
35
35
|
await this.pool.query(
|
|
36
|
-
'INSERT INTO bz_jobs (job_id,request_id,status,target,profile,project,source,client_app_id,thread_id,session_id,input_preview,input,dispatch,attempts,run_after,claimed_at,lease_until,report,result,raw_result,`usage`,error,metadata,callback_url,created_at,updated_at) VALUES (
|
|
36
|
+
'INSERT INTO bz_jobs (job_id,request_id,status,target,profile,project,source,client_app_id,agent_session_id,on_behalf_of,thread_id,session_id,input_preview,input,dispatch,attempts,run_after,claimed_at,lease_until,report,result,raw_result,`usage`,error,metadata,callback_url,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
|
|
37
37
|
[
|
|
38
38
|
job.job_id, job.request_id, job.status, job.target ?? null, job.profile, job.project, job.source,
|
|
39
|
-
job.client_app_id ?? null, job.thread_id ?? null,
|
|
39
|
+
job.client_app_id ?? null, job.agent_session_id ?? null, job.on_behalf_of ?? null, job.thread_id ?? null,
|
|
40
40
|
job.session_id ?? null, job.input_preview, job.input ?? null, json(job.dispatch), job.attempts ?? 0,
|
|
41
41
|
job.run_after ? dt(job.run_after) : null, job.claimed_at ? dt(job.claimed_at) : null, job.lease_until ? dt(job.lease_until) : null,
|
|
42
42
|
json(job.report), json(job.result), job.raw_result ?? null,
|
|
@@ -42,6 +42,14 @@ export interface KernelLaunchRequestV1 {
|
|
|
42
42
|
readonly threadScope: string;
|
|
43
43
|
readonly principalId: string | null;
|
|
44
44
|
readonly channel: string;
|
|
45
|
+
/** Trusted local-Agent identity attribution. No plan, payment, entitlement, or usage semantics. */
|
|
46
|
+
readonly agentAttribution?: Readonly<{
|
|
47
|
+
agentSessionId: string;
|
|
48
|
+
clientAppId: string;
|
|
49
|
+
subjectId: string;
|
|
50
|
+
businessTenantRef?: string;
|
|
51
|
+
onBehalfOf: string;
|
|
52
|
+
}>;
|
|
45
53
|
}
|
|
46
54
|
|
|
47
55
|
export interface KernelLaunchGuardDecisionV1 {
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import test from 'node:test';
|
|
3
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
4
|
+
import type { Job, ToolApproval } from '../core/contracts/types';
|
|
5
|
+
import { completeTraceEntry } from '../core/runtime/trace-runtime';
|
|
6
|
+
import type { RuntimeStateStore } from '../core/state/state-contracts';
|
|
7
|
+
import type { ConfigStoreContract } from '../infrastructure/config/configstore';
|
|
8
|
+
import type { AgentClientRunRecord } from '../infrastructure/config/config-agent-client-runtime-repository';
|
|
9
|
+
import { AGENT_TOOL_JOB_MARKER } from '../app/agent-tool-job';
|
|
10
|
+
import { handleAdminRuntimeApiFor } from './admin-runtime';
|
|
11
|
+
|
|
12
|
+
class FakeResponse {
|
|
13
|
+
statusCode = 0;
|
|
14
|
+
headers: Record<string, string | number | string[]> = {};
|
|
15
|
+
body = Buffer.alloc(0);
|
|
16
|
+
setHeader(name: string, value: string | number | string[]): void { this.headers[name.toLowerCase()] = value; }
|
|
17
|
+
writeHead(code: number, headers?: Record<string, string | number | string[]>): void {
|
|
18
|
+
this.statusCode = code;
|
|
19
|
+
for (const [name, value] of Object.entries(headers ?? {})) this.headers[name.toLowerCase()] = value;
|
|
20
|
+
}
|
|
21
|
+
end(value?: string | Buffer): void { this.body = value ? Buffer.from(value) : Buffer.alloc(0); }
|
|
22
|
+
json(): any { return JSON.parse(this.body.toString('utf8')); }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const RUN_ID = '123e4567-e89b-42d3-a456-426614174000';
|
|
26
|
+
const VALID_JOB_ID = '223e4567-e89b-42d3-a456-426614174000';
|
|
27
|
+
const FORGED_JOB_ID = '323e4567-e89b-42d3-a456-426614174000';
|
|
28
|
+
const SESSION_ID = '423e4567-e89b-42d3-a456-426614174000';
|
|
29
|
+
const OTHER_RUN_ID = '523e4567-e89b-42d3-a456-426614174000';
|
|
30
|
+
const OTHER_RUN_JOB_ID = '623e4567-e89b-42d3-a456-426614174000';
|
|
31
|
+
const INVOCATION_ID = 'a'.repeat(64);
|
|
32
|
+
|
|
33
|
+
function run(): AgentClientRunRecord {
|
|
34
|
+
return {
|
|
35
|
+
run_id: RUN_ID,
|
|
36
|
+
session_id: SESSION_ID,
|
|
37
|
+
client_app_id: 'example-business',
|
|
38
|
+
route_key: 'tenant-agent',
|
|
39
|
+
thread_id: 42,
|
|
40
|
+
client_conversation_id: 'conversation-1',
|
|
41
|
+
client_turn_id: 'turn-1',
|
|
42
|
+
user_message_id: 'user-message-1',
|
|
43
|
+
request_hash: 'PRIVATE_REQUEST_HASH',
|
|
44
|
+
user_input: 'PRIVATE_USER_INPUT',
|
|
45
|
+
context: { hidden_reasoning: 'PRIVATE_CONTEXT' },
|
|
46
|
+
status: 'completed',
|
|
47
|
+
completion_hash: 'PRIVATE_COMPLETION_HASH',
|
|
48
|
+
assistant_message_id: 'assistant-message-1',
|
|
49
|
+
final_content: 'PRIVATE_FINAL_CONTENT',
|
|
50
|
+
model: 'deepseek-chat',
|
|
51
|
+
runtime: 'deepseek-harness',
|
|
52
|
+
usage: { total_tokens: 150 },
|
|
53
|
+
created_at: '2026-08-26T05:46:00.000Z',
|
|
54
|
+
updated_at: '2026-08-26T05:46:20.000Z',
|
|
55
|
+
completed_at: '2026-08-26T05:46:20.000Z',
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function toolJob(jobId = VALID_JOB_ID): Job {
|
|
60
|
+
return {
|
|
61
|
+
job_id: jobId,
|
|
62
|
+
request_id: `agent-tool:${'b'.repeat(64)}`,
|
|
63
|
+
status: 'done',
|
|
64
|
+
target: 'agent-tool-v1',
|
|
65
|
+
profile: 'general',
|
|
66
|
+
project: '',
|
|
67
|
+
source: 'agent-tool:example-business',
|
|
68
|
+
client_app_id: 'example-business',
|
|
69
|
+
agent_session_id: SESSION_ID,
|
|
70
|
+
on_behalf_of: 'tenant-2:user-7',
|
|
71
|
+
thread_id: 42,
|
|
72
|
+
session_id: RUN_ID,
|
|
73
|
+
input_preview: 'Agent tool staff_edit',
|
|
74
|
+
result: { state: 'executed', ok: true, business_status: 200, text: 'PRIVATE_JOB_RESULT' },
|
|
75
|
+
metadata: {
|
|
76
|
+
agent_tool_job_marker: AGENT_TOOL_JOB_MARKER,
|
|
77
|
+
agent_tool_call_v1: true,
|
|
78
|
+
agent_invocation_id: INVOCATION_ID,
|
|
79
|
+
agent_run_id: RUN_ID,
|
|
80
|
+
agent_route: 'tenant-agent',
|
|
81
|
+
agent_tool: 'staff_edit',
|
|
82
|
+
agent_args_hash: 'c'.repeat(64),
|
|
83
|
+
agent_capability_revision: 'd'.repeat(64),
|
|
84
|
+
agent_execution_fingerprint: 'e'.repeat(64),
|
|
85
|
+
agent_thread_id: 42,
|
|
86
|
+
},
|
|
87
|
+
dispatch: { route_key: 'tenant-agent' },
|
|
88
|
+
created_at: '2026-08-26T05:46:10.000Z',
|
|
89
|
+
updated_at: '2026-08-26T05:46:12.000Z',
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function depsFor(agentRun = run()) {
|
|
94
|
+
const valid = toolJob();
|
|
95
|
+
const forged = { ...toolJob(FORGED_JOB_ID), metadata: { ...toolJob(FORGED_JOB_ID).metadata, agent_tool_job_marker: 'forged' } };
|
|
96
|
+
const otherRun = {
|
|
97
|
+
...toolJob(OTHER_RUN_JOB_ID),
|
|
98
|
+
session_id: OTHER_RUN_ID,
|
|
99
|
+
metadata: { ...toolJob(OTHER_RUN_JOB_ID).metadata, agent_run_id: OTHER_RUN_ID },
|
|
100
|
+
};
|
|
101
|
+
const approval = {
|
|
102
|
+
id: 8, job_id: VALID_JOB_ID, request_id: 'approval-1', provider: 'business', tool: 'staff_edit',
|
|
103
|
+
scope: 'staff.write', risk: 'medium', args_hash: 'f'.repeat(64), args_json: 'PRIVATE_APPROVAL_ARGS',
|
|
104
|
+
status: 'approved', created_at: '2026-08-26T05:46:10.100Z',
|
|
105
|
+
} as ToolApproval;
|
|
106
|
+
const configStore = {
|
|
107
|
+
agentClientRuntime: { findRunForInvocation: async (id: string) => id === RUN_ID ? agentRun : null },
|
|
108
|
+
observability: {
|
|
109
|
+
agentToolJobCandidatesForRun: async () => ({ jobs: [forged, otherRun, valid], truncated: false }),
|
|
110
|
+
auditForJob: async (id: string) => id === RUN_ID
|
|
111
|
+
? [completeTraceEntry({
|
|
112
|
+
ts: '2026-08-26T05:46:01.000Z', job_id: RUN_ID, request_id: 'turn-1',
|
|
113
|
+
event: 'agent_client_turn_context_ready', detail: { route: 'tenant-agent', active_tools: 4, agent_session_id: 'PRIVATE_SESSION' },
|
|
114
|
+
})]
|
|
115
|
+
: [completeTraceEntry({
|
|
116
|
+
ts: '2026-08-26T05:46:11.000Z', job_id: id, request_id: 'invoke', event: 'tool_call',
|
|
117
|
+
detail: { tool: 'staff_edit', args: '{"name":"PRIVATE_TOOL_ARGUMENT"}', idempotency_key: 'PRIVATE_KEY' },
|
|
118
|
+
})],
|
|
119
|
+
auditsForJobs: async (ids: string[]) => Object.fromEntries(ids.map((id) => [id, [completeTraceEntry({
|
|
120
|
+
ts: '2026-08-26T05:46:11.000Z', job_id: id, request_id: 'invoke', event: 'tool_call',
|
|
121
|
+
detail: { tool: 'staff_edit', args: '{"name":"PRIVATE_TOOL_ARGUMENT"}', idempotency_key: 'PRIVATE_KEY' },
|
|
122
|
+
})]])),
|
|
123
|
+
},
|
|
124
|
+
approvals: {
|
|
125
|
+
forJob: async (id: string) => id === VALID_JOB_ID ? [approval] : [],
|
|
126
|
+
forJobs: async (ids: string[]) => Object.fromEntries(ids.map((id) => [id, id === VALID_JOB_ID ? [approval] : []])),
|
|
127
|
+
},
|
|
128
|
+
} as unknown as ConfigStoreContract;
|
|
129
|
+
const stateStore = {
|
|
130
|
+
getJob: async (id: string) => id === VALID_JOB_ID ? valid : id === FORGED_JOB_ID ? forged : id === OTHER_RUN_JOB_ID ? otherRun : null,
|
|
131
|
+
} as unknown as RuntimeStateStore;
|
|
132
|
+
return {
|
|
133
|
+
configStore,
|
|
134
|
+
stateStore,
|
|
135
|
+
now: () => '2026-08-26T05:47:00.000Z',
|
|
136
|
+
isPaused: () => false,
|
|
137
|
+
queueStats: () => ({}),
|
|
138
|
+
channelSend: async () => ({ ok: true as const }),
|
|
139
|
+
engineRuntime: { requeueForRerun: async () => undefined },
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
test('Agent Run trace 以 thread/run 归属聚合并排除伪造工具 Job', async () => {
|
|
144
|
+
const res = new FakeResponse();
|
|
145
|
+
const handled = await handleAdminRuntimeApiFor(
|
|
146
|
+
depsFor(),
|
|
147
|
+
'GET',
|
|
148
|
+
`/admin/api/threads/42/agent-runs/${RUN_ID}/trace`,
|
|
149
|
+
{} as IncomingMessage,
|
|
150
|
+
res as unknown as ServerResponse,
|
|
151
|
+
{ kind: 'admin', via: 'session', username: 'viewer', role: 'viewer' },
|
|
152
|
+
);
|
|
153
|
+
assert.equal(handled, true);
|
|
154
|
+
assert.equal(res.statusCode, 200);
|
|
155
|
+
const payload = res.json();
|
|
156
|
+
assert.equal(payload.kind, 'agent_client_run');
|
|
157
|
+
assert.deepEqual(payload.invocations.map((item: any) => item.job_id), [VALID_JOB_ID]);
|
|
158
|
+
assert.equal(payload.trace.summary.tool_invocations, 1);
|
|
159
|
+
assert.equal(payload.trace.events.some((event: any) => event.source === 'local_agent'), true);
|
|
160
|
+
assert.equal(payload.trace.events.some((event: any) => event.source === 'hub_governance'), true);
|
|
161
|
+
|
|
162
|
+
const json = JSON.stringify(payload);
|
|
163
|
+
for (const forbidden of [
|
|
164
|
+
'PRIVATE_REQUEST_HASH', 'PRIVATE_USER_INPUT', 'PRIVATE_CONTEXT', 'PRIVATE_COMPLETION_HASH',
|
|
165
|
+
'PRIVATE_FINAL_CONTENT', 'PRIVATE_JOB_RESULT', 'PRIVATE_APPROVAL_ARGS', 'PRIVATE_SESSION',
|
|
166
|
+
'PRIVATE_TOOL_ARGUMENT', 'PRIVATE_KEY',
|
|
167
|
+
]) assert.equal(json.includes(forbidden), false, `${forbidden} must not be exposed`);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test('Agent Run trace 对 thread 不匹配统一返回 404', async () => {
|
|
171
|
+
const res = new FakeResponse();
|
|
172
|
+
await handleAdminRuntimeApiFor(
|
|
173
|
+
depsFor(),
|
|
174
|
+
'GET',
|
|
175
|
+
`/admin/api/threads/43/agent-runs/${RUN_ID}/trace`,
|
|
176
|
+
{} as IncomingMessage,
|
|
177
|
+
res as unknown as ServerResponse,
|
|
178
|
+
{ kind: 'admin', via: 'session', username: 'viewer', role: 'viewer' },
|
|
179
|
+
);
|
|
180
|
+
assert.equal(res.statusCode, 404);
|
|
181
|
+
assert.deepEqual(res.json(), { error: '智能体客户端运行不存在' });
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test('Agent Run trace 在候选查询能力不存在时 fail closed', async () => {
|
|
185
|
+
const deps = depsFor();
|
|
186
|
+
delete (deps.configStore.observability as any).agentToolJobCandidatesForRun;
|
|
187
|
+
const res = new FakeResponse();
|
|
188
|
+
await handleAdminRuntimeApiFor(
|
|
189
|
+
deps,
|
|
190
|
+
'GET',
|
|
191
|
+
`/admin/api/threads/42/agent-runs/${RUN_ID}/trace`,
|
|
192
|
+
{} as IncomingMessage,
|
|
193
|
+
res as unknown as ServerResponse,
|
|
194
|
+
{ kind: 'admin', via: 'session', username: 'viewer', role: 'viewer' },
|
|
195
|
+
);
|
|
196
|
+
assert.equal(res.statusCode, 503);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
test('Agent Run trace 对旧扩展仓储回退到单 job 审计与审批读取', async () => {
|
|
200
|
+
const deps = depsFor();
|
|
201
|
+
delete (deps.configStore.observability as any).auditsForJobs;
|
|
202
|
+
delete (deps.configStore.approvals as any).forJobs;
|
|
203
|
+
const res = new FakeResponse();
|
|
204
|
+
await handleAdminRuntimeApiFor(
|
|
205
|
+
deps,
|
|
206
|
+
'GET',
|
|
207
|
+
`/admin/api/threads/42/agent-runs/${RUN_ID}/trace`,
|
|
208
|
+
{} as IncomingMessage,
|
|
209
|
+
res as unknown as ServerResponse,
|
|
210
|
+
{ kind: 'admin', via: 'session', username: 'viewer', role: 'viewer' },
|
|
211
|
+
);
|
|
212
|
+
assert.equal(res.statusCode, 200);
|
|
213
|
+
assert.equal(res.json().invocations.length, 1);
|
|
214
|
+
});
|
|
@@ -6,13 +6,29 @@ import { send } from '../app/http';
|
|
|
6
6
|
import { redactValue, redactionSummary } from '../core/runtime/redaction-runtime';
|
|
7
7
|
import { renderDebugReport } from '../core/runtime/debug-report';
|
|
8
8
|
import { buildJobTrace } from '../core/runtime/trace-runtime';
|
|
9
|
+
import { buildAgentClientTrace } from '../core/runtime/agent-client-trace';
|
|
9
10
|
import type { RuntimeStateStore } from '../core/state/state-contracts';
|
|
10
11
|
import type { ConfigStoreContract } from '../infrastructure/config/configstore';
|
|
12
|
+
import type { AgentClientRunRecord } from '../infrastructure/config/config-agent-client-runtime-repository';
|
|
11
13
|
import type { Principal } from '../app/auth';
|
|
12
14
|
import type { ChannelMessage, ChannelSendResult } from '../app/channels';
|
|
15
|
+
import { isAgentToolInvocationJob } from '../app/agent-tool-job';
|
|
13
16
|
|
|
14
17
|
export type AdminChannelSender = (channelName: string, recipient: string, message: string | ChannelMessage) => Promise<ChannelSendResult>;
|
|
15
18
|
|
|
19
|
+
function isToolJobForAgentRun(job: any, run: AgentClientRunRecord): boolean {
|
|
20
|
+
const metadata = job?.metadata ?? {};
|
|
21
|
+
return isAgentToolInvocationJob(job)
|
|
22
|
+
&& Number(job.thread_id) === run.thread_id
|
|
23
|
+
&& job.session_id === run.run_id
|
|
24
|
+
&& job.client_app_id === run.client_app_id
|
|
25
|
+
&& job.agent_session_id === run.session_id
|
|
26
|
+
&& metadata['agent_run_id'] === run.run_id
|
|
27
|
+
&& Number(metadata['agent_thread_id']) === run.thread_id
|
|
28
|
+
&& metadata['agent_route'] === run.route_key
|
|
29
|
+
&& job.dispatch?.route_key === run.route_key;
|
|
30
|
+
}
|
|
31
|
+
|
|
16
32
|
function textPreview(v: unknown, limit = 2000): string | null {
|
|
17
33
|
if (v == null) return null;
|
|
18
34
|
const s = typeof v === 'string' ? v : JSON.stringify(v);
|
|
@@ -326,7 +342,7 @@ export async function handleAdminRuntimeApiFor(
|
|
|
326
342
|
return true;
|
|
327
343
|
}
|
|
328
344
|
|
|
329
|
-
// 会话视图(调度台「会话」Tab
|
|
345
|
+
// 会话视图(调度台「会话」Tab):旧中枢轮次按 job_id,Agent Client 轮次按 agent_run_id 懒加载轨迹。
|
|
330
346
|
if (path === '/admin/api/threads' && method === 'GET') {
|
|
331
347
|
const q = new URL(req.url ?? '/', 'http://x').searchParams;
|
|
332
348
|
send(res, 200, await configStore.conversations.listRecentThreads(Number(q.get('limit')) || 80, Number(q.get('offset')) || 0));
|
|
@@ -340,6 +356,66 @@ export async function handleAdminRuntimeApiFor(
|
|
|
340
356
|
return true;
|
|
341
357
|
}
|
|
342
358
|
|
|
359
|
+
// Agent Client 一轮轨迹:本地负责规划,中枢只聚合安全的运行边界、ACC 工具治理、审批与结果。
|
|
360
|
+
// 使用 thread + run 双键防止串线;工具 Job 还要通过服务端 marker 和身份/路由不变量复核。
|
|
361
|
+
const mAgentRunTrace = path.match(/^\/admin\/api\/threads\/(\d+)\/agent-runs\/([0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12})\/trace$/i);
|
|
362
|
+
if (mAgentRunTrace && method === 'GET') {
|
|
363
|
+
const threadId = Number(mAgentRunTrace[1]);
|
|
364
|
+
const runId = mAgentRunTrace[2]!;
|
|
365
|
+
const runtimeRepo = configStore.agentClientRuntime;
|
|
366
|
+
const candidateLookup = configStore.observability.agentToolJobCandidatesForRun;
|
|
367
|
+
if (!runtimeRepo || !candidateLookup) {
|
|
368
|
+
send(res, 503, { error: '智能体客户端执行轨迹在当前配置仓储中不可用' });
|
|
369
|
+
return true;
|
|
370
|
+
}
|
|
371
|
+
const run = await runtimeRepo.findRunForInvocation(runId);
|
|
372
|
+
if (!run || run.thread_id !== threadId) {
|
|
373
|
+
send(res, 404, { error: '智能体客户端运行不存在' });
|
|
374
|
+
return true;
|
|
375
|
+
}
|
|
376
|
+
const [runAudit, candidates] = await Promise.all([
|
|
377
|
+
configStore.observability.auditForJob(runId),
|
|
378
|
+
configStore.observability.agentToolJobCandidatesForRun!(runId, threadId),
|
|
379
|
+
]);
|
|
380
|
+
const jobs = candidates.jobs
|
|
381
|
+
.filter((job) => isToolJobForAgentRun(job, run))
|
|
382
|
+
.sort((a, b) => a.created_at.localeCompare(b.created_at) || a.job_id.localeCompare(b.job_id));
|
|
383
|
+
const jobIds = jobs.map((job) => job.job_id);
|
|
384
|
+
const auditPromise = configStore.observability.auditsForJobs
|
|
385
|
+
? configStore.observability.auditsForJobs(jobIds)
|
|
386
|
+
: Promise.all(jobIds.map((jobId) => configStore.observability.auditForJob(jobId)))
|
|
387
|
+
.then((rows) => Object.fromEntries(jobIds.map((jobId, index) => [jobId, rows[index] ?? []])));
|
|
388
|
+
const approvalPromise = configStore.approvals.forJobs
|
|
389
|
+
? configStore.approvals.forJobs(jobIds)
|
|
390
|
+
: Promise.all(jobIds.map((jobId) => configStore.approvals.forJob(jobId)))
|
|
391
|
+
.then((rows) => Object.fromEntries(jobIds.map((jobId, index) => [jobId, rows[index] ?? []])));
|
|
392
|
+
const [auditByJob, approvalsByJob] = await Promise.all([auditPromise, approvalPromise]);
|
|
393
|
+
const tools = jobs.map((job) => ({
|
|
394
|
+
job,
|
|
395
|
+
audit: auditByJob[job.job_id] ?? [],
|
|
396
|
+
approvals: approvalsByJob[job.job_id] ?? [],
|
|
397
|
+
}));
|
|
398
|
+
send(res, 200, buildAgentClientTrace({
|
|
399
|
+
run: {
|
|
400
|
+
run_id: run.run_id,
|
|
401
|
+
thread_id: run.thread_id,
|
|
402
|
+
client_app_id: run.client_app_id,
|
|
403
|
+
route_key: run.route_key,
|
|
404
|
+
status: run.status,
|
|
405
|
+
model: run.model,
|
|
406
|
+
runtime: run.runtime,
|
|
407
|
+
usage: run.usage,
|
|
408
|
+
created_at: run.created_at,
|
|
409
|
+
updated_at: run.updated_at,
|
|
410
|
+
completed_at: run.completed_at,
|
|
411
|
+
},
|
|
412
|
+
runAudit,
|
|
413
|
+
tools,
|
|
414
|
+
candidatesTruncated: candidates.truncated,
|
|
415
|
+
}));
|
|
416
|
+
return true;
|
|
417
|
+
}
|
|
418
|
+
|
|
343
419
|
// 任务详情 / 审计时间线 / 重跑
|
|
344
420
|
const mRun = path.match(/^\/admin\/api\/runs\/([0-9a-f-]{36})(\/audit|\/rerun|\/trace)?$/);
|
|
345
421
|
if (mRun) {
|
|
@@ -362,6 +438,10 @@ export async function handleAdminRuntimeApiFor(
|
|
|
362
438
|
return true;
|
|
363
439
|
}
|
|
364
440
|
if (method === 'POST' && mRun[2] === '/rerun') {
|
|
441
|
+
if (isAgentToolInvocationJob(job)) {
|
|
442
|
+
send(res, 409, { error: 'Agent 直调记录不能进入中枢模型重跑;请由原本地 Agent 用 invocation_id 续执行' });
|
|
443
|
+
return true;
|
|
444
|
+
}
|
|
365
445
|
if (job.status === 'queued' || job.status === 'running' || job.status === 'dispatched') {
|
|
366
446
|
send(res, 400, { error: `任务在途(${job.status}),无需重跑` });
|
|
367
447
|
return true;
|
|
@@ -399,7 +479,7 @@ export async function handleAdminRuntimeApiFor(
|
|
|
399
479
|
let rerun = false;
|
|
400
480
|
if (action === 'approve') {
|
|
401
481
|
const job = await stateStore.getJob(appr.job_id);
|
|
402
|
-
if (job && (job.status === 'done' || job.status === 'error' || job.status === 'rejected')) {
|
|
482
|
+
if (job && !isAgentToolInvocationJob(job) && (job.status === 'done' || job.status === 'error' || job.status === 'rejected')) {
|
|
403
483
|
await deps.engineRuntime.requeueForRerun(job, by, `approval_${id}`);
|
|
404
484
|
rerun = true;
|
|
405
485
|
}
|