agent-gear 2.5.0 → 2.7.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/AGENTS.md +53 -5
- package/dist/acp/__fixtures__/opencode-2.0.31-capabilities.json +25 -0
- package/dist/acp/bridge.d.ts +23 -1
- package/dist/acp/bridge.d.ts.map +1 -1
- package/dist/acp/bridge.js +152 -17
- package/dist/acp/bridge.js.map +1 -1
- package/dist/acp/bridge.test.js +366 -1
- package/dist/acp/bridge.test.js.map +1 -1
- package/dist/acp/capabilities.d.ts +81 -0
- package/dist/acp/capabilities.d.ts.map +1 -0
- package/dist/acp/capabilities.js +211 -0
- package/dist/acp/capabilities.js.map +1 -0
- package/dist/acp/capabilities.test.d.ts +2 -0
- package/dist/acp/capabilities.test.d.ts.map +1 -0
- package/dist/acp/capabilities.test.js +156 -0
- package/dist/acp/capabilities.test.js.map +1 -0
- package/dist/acp/elicitation.integration.test.d.ts +2 -0
- package/dist/acp/elicitation.integration.test.d.ts.map +1 -0
- package/dist/acp/elicitation.integration.test.js +98 -0
- package/dist/acp/elicitation.integration.test.js.map +1 -0
- package/dist/acp/prober.d.ts +4 -0
- package/dist/acp/prober.d.ts.map +1 -1
- package/dist/acp/prober.js +1 -0
- package/dist/acp/prober.js.map +1 -1
- package/dist/acp/spawner.d.ts.map +1 -1
- package/dist/acp/spawner.js +3 -0
- package/dist/acp/spawner.js.map +1 -1
- package/dist/acp/sync-bridge.d.ts.map +1 -1
- package/dist/acp/sync-bridge.js +5 -1
- package/dist/acp/sync-bridge.js.map +1 -1
- package/dist/cloud-host/supervisor.d.ts +13 -0
- package/dist/cloud-host/supervisor.d.ts.map +1 -1
- package/dist/cloud-host/supervisor.js +21 -0
- package/dist/cloud-host/supervisor.js.map +1 -1
- package/dist/cloud-host/supervisor.test.js +71 -0
- package/dist/cloud-host/supervisor.test.js.map +1 -1
- package/dist/github/credential-manager.d.ts +70 -0
- package/dist/github/credential-manager.d.ts.map +1 -0
- package/dist/github/credential-manager.js +205 -0
- package/dist/github/credential-manager.js.map +1 -0
- package/dist/github/credential-manager.test.d.ts +2 -0
- package/dist/github/credential-manager.test.d.ts.map +1 -0
- package/dist/github/credential-manager.test.js +215 -0
- package/dist/github/credential-manager.test.js.map +1 -0
- package/dist/github/gh-config.d.ts +17 -0
- package/dist/github/gh-config.d.ts.map +1 -0
- package/dist/github/gh-config.js +68 -0
- package/dist/github/gh-config.js.map +1 -0
- package/dist/github/gh-config.test.d.ts +2 -0
- package/dist/github/gh-config.test.d.ts.map +1 -0
- package/dist/github/gh-config.test.js +90 -0
- package/dist/github/gh-config.test.js.map +1 -0
- package/dist/harness/env.d.ts +8 -0
- package/dist/harness/env.d.ts.map +1 -1
- package/dist/harness/env.js +30 -0
- package/dist/harness/env.js.map +1 -1
- package/dist/harness/env.test.js +40 -1
- package/dist/harness/env.test.js.map +1 -1
- package/dist/harness/opencode-config.d.ts +6 -3
- package/dist/harness/opencode-config.d.ts.map +1 -1
- package/dist/harness/opencode-config.js +6 -2
- package/dist/harness/opencode-config.js.map +1 -1
- package/dist/harness/opencode-config.test.js +12 -0
- package/dist/harness/opencode-config.test.js.map +1 -1
- package/dist/harness/prepare.test.js +19 -0
- package/dist/harness/prepare.test.js.map +1 -1
- package/dist/socketio-client.d.ts +12 -0
- package/dist/socketio-client.d.ts.map +1 -1
- package/dist/socketio-client.github.test.d.ts +2 -0
- package/dist/socketio-client.github.test.d.ts.map +1 -0
- package/dist/socketio-client.github.test.js +66 -0
- package/dist/socketio-client.github.test.js.map +1 -0
- package/dist/socketio-client.js +115 -65
- package/dist/socketio-client.js.map +1 -1
- package/dist/socketio-client.test.js +251 -1
- package/dist/socketio-client.test.js.map +1 -1
- package/dist/workload-executor.d.ts +14 -1
- package/dist/workload-executor.d.ts.map +1 -1
- package/dist/workload-executor.js +33 -3
- package/dist/workload-executor.js.map +1 -1
- package/dist/workload-executor.test.js +106 -2
- package/dist/workload-executor.test.js.map +1 -1
- package/package.json +1 -1
- package/src/acp/__fixtures__/fake-elicitation-agent.mjs +113 -0
- package/src/acp/__fixtures__/opencode-2.0.31-capabilities.json +25 -0
- package/src/acp/bridge.test.ts +480 -1
- package/src/acp/bridge.ts +163 -18
- package/src/acp/capabilities.test.ts +200 -0
- package/src/acp/capabilities.ts +274 -0
- package/src/acp/elicitation.integration.test.ts +126 -0
- package/src/acp/prober.ts +4 -0
- package/src/acp/spawner.ts +3 -0
- package/src/acp/sync-bridge.ts +5 -1
- package/src/cloud-host/supervisor.test.ts +96 -1
- package/src/cloud-host/supervisor.ts +37 -0
- package/src/github/credential-manager.test.ts +268 -0
- package/src/github/credential-manager.ts +251 -0
- package/src/github/gh-config.test.ts +103 -0
- package/src/github/gh-config.ts +76 -0
- package/src/harness/env.test.ts +44 -1
- package/src/harness/env.ts +32 -0
- package/src/harness/opencode-config.test.ts +13 -0
- package/src/harness/opencode-config.ts +12 -6
- package/src/harness/prepare.test.ts +22 -0
- package/src/socketio-client.github.test.ts +76 -0
- package/src/socketio-client.test.ts +306 -1
- package/src/socketio-client.ts +113 -53
- package/src/workload-executor.test.ts +144 -2
- package/src/workload-executor.ts +48 -2
- package/tests/acp-sync-bridge.test.ts +5 -1
- package/tests/socketio-client.test.ts +2 -2
- package/vitest.config.ts +5 -1
package/src/socketio-client.ts
CHANGED
|
@@ -21,8 +21,9 @@ import { LocalMcpServer } from './mcp/local-server.js';
|
|
|
21
21
|
import { WorkloadExecutor, type WorkloadRunPayload, collectSecretValues, redactSecrets } from './workload-executor.js';
|
|
22
22
|
|
|
23
23
|
// 与 package.json 版本保持一致(register 上报的完整版本;服务端按 MIN_WORKER_VERSION=2.2.0 门禁)。
|
|
24
|
-
// 2.
|
|
25
|
-
|
|
24
|
+
// 2.6.0 起支持 `workload:run.githubAuth` 与云电脑 Github 凭据管理器(MIN_HARNESS_WORKER_VERSION=2.6.0);
|
|
25
|
+
// 2.5.0 起支持云电脑双连接(`/worker` + controller-api)与 `harness prepare`。
|
|
26
|
+
const CONTROLLER_VERSION = '2.6.0';
|
|
26
27
|
// 进程内全局单实例:工作负载批次严格串行(新批次打断旧批次,见 workload-executor)。
|
|
27
28
|
const workloadExecutor = new WorkloadExecutor();
|
|
28
29
|
// shutdown 到达时若仍有 post 批次在跑,最多等待该时长后退出(契约 §2.8)。
|
|
@@ -53,6 +54,29 @@ function createSpawner(serverType: string, sessionId: string, cwd: string): AcpS
|
|
|
53
54
|
return new AcpSpawner({ sessionId, cwd, command: config.command, args: config.args });
|
|
54
55
|
}
|
|
55
56
|
|
|
57
|
+
/**
|
|
58
|
+
* `acp:initialized` → `acp:start:result` / `acp:resume:result` 的唯一出口。
|
|
59
|
+
* start/resume 共用,避免两处逐字段拼装漂移(漏字段会导致前端永远拿不到能力快照)。
|
|
60
|
+
* 旧 payload 不带 `capabilities` 时不输出该键(零退化)。
|
|
61
|
+
*/
|
|
62
|
+
export function buildAcpInitResult(payload: Record<string, unknown>): Record<string, unknown> {
|
|
63
|
+
const result: Record<string, unknown> = {};
|
|
64
|
+
result.modes = payload.modes ?? [];
|
|
65
|
+
if (payload.currentModeId) result.currentModeId = payload.currentModeId;
|
|
66
|
+
if (payload.currentModel) result.currentModel = payload.currentModel;
|
|
67
|
+
result.availableModels = payload.availableModels ?? [];
|
|
68
|
+
if (payload.currentModelId) result.currentModelId = payload.currentModelId;
|
|
69
|
+
result.configOptions = payload.configOptions ?? [];
|
|
70
|
+
if (payload.promptCapabilities) result.promptCapabilities = payload.promptCapabilities;
|
|
71
|
+
if (payload.agentInfo) result.agentInfo = payload.agentInfo;
|
|
72
|
+
if (payload.authMethods) result.authMethods = payload.authMethods;
|
|
73
|
+
if (payload.supportsAdditionalDirectories != null) {
|
|
74
|
+
result.supportsAdditionalDirectories = payload.supportsAdditionalDirectories;
|
|
75
|
+
}
|
|
76
|
+
if (payload.capabilities) result.capabilities = payload.capabilities;
|
|
77
|
+
return result;
|
|
78
|
+
}
|
|
79
|
+
|
|
56
80
|
interface RegisteredPayload {
|
|
57
81
|
controllerId: string;
|
|
58
82
|
resumeToken?: string;
|
|
@@ -86,6 +110,11 @@ export interface ConnectToServerOptions {
|
|
|
86
110
|
onRegistered?: (payload: RegisteredPayload) => void;
|
|
87
111
|
/** `workload:run` 批次结束回调(成功/失败都回调;用于 init 完成后建立标准连接)。 */
|
|
88
112
|
onWorkloadBatchComplete?: (summary: WorkloadBatchSummary) => void;
|
|
113
|
+
/**
|
|
114
|
+
* `workload:run` 批次开始前的预处理(仅 `githubAuth === true` 批次触发)。
|
|
115
|
+
* 云电脑 worker 连接用于申请 Github 凭据;抛错时本批所有 item 回报 failed 且不执行脚本。
|
|
116
|
+
*/
|
|
117
|
+
onBeforeWorkloadBatch?: (payload: WorkloadRunPayload) => Promise<void>;
|
|
89
118
|
/**
|
|
90
119
|
* 不启动本地 MCP 回环服务(双连接时仅由 controller-api 标准连接启动一次,
|
|
91
120
|
* 避免 worker 连接重复监听同一端口)。
|
|
@@ -565,7 +594,11 @@ export function connectToServer(
|
|
|
565
594
|
status: 'completed',
|
|
566
595
|
};
|
|
567
596
|
workloadExecutor
|
|
568
|
-
.runBatch(
|
|
597
|
+
.runBatch(
|
|
598
|
+
data,
|
|
599
|
+
(result) => socket.emit('workload:result', result),
|
|
600
|
+
{ beforeGithubAuth: options?.onBeforeWorkloadBatch },
|
|
601
|
+
)
|
|
569
602
|
.then((outcome) => {
|
|
570
603
|
const status: WorkloadBatchSummary['status'] = outcome.cancelled
|
|
571
604
|
? 'cancelled'
|
|
@@ -630,7 +663,8 @@ export function connectToServer(
|
|
|
630
663
|
if (existingBridge) {
|
|
631
664
|
console.warn(`[ACP ctrl] acp:start for session ${data.sessionId} already has a bridge, stopping the previous one`);
|
|
632
665
|
acpBridges.delete(data.sessionId);
|
|
633
|
-
|
|
666
|
+
// silent:同 sessionId 重建时旧 bridge 不得向服务端发 stopped(会删除新会话映射)
|
|
667
|
+
await existingBridge.stop({ silent: true }).catch(() => {});
|
|
634
668
|
}
|
|
635
669
|
const spawner = createSpawner(data.serverType, data.sessionId, data.cwd);
|
|
636
670
|
if (data.mcpEndpoint) {
|
|
@@ -665,10 +699,12 @@ export function connectToServer(
|
|
|
665
699
|
// Registry lifecycle is bound to the bridge: any stop exit (user close,
|
|
666
700
|
// agent exit, error) deregisters the PID so stale entries cannot pile up
|
|
667
701
|
bridge.on('acp:stopped', (payload: { sessionId: string }) => {
|
|
668
|
-
if (payload.sessionId
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
702
|
+
if (payload.sessionId !== data.sessionId) return;
|
|
703
|
+
// 持有权守卫(与 onStopped 同口径):旧 bridge 的迟到 stopped 不得注销新会话的
|
|
704
|
+
// process registry / MCP relay 注册(否则对账盲区 + MCP 断链)。
|
|
705
|
+
if (acpBridges.get(data.sessionId) !== bridge) return;
|
|
706
|
+
processRegistry.deregister(data.sessionId);
|
|
707
|
+
mcpRelay.unregisterSession(data.sessionId);
|
|
672
708
|
});
|
|
673
709
|
|
|
674
710
|
// Register PID in process registry after spawner starts
|
|
@@ -704,34 +740,35 @@ export function connectToServer(
|
|
|
704
740
|
elicitationRequestBridge.set(requestId, bridge);
|
|
705
741
|
});
|
|
706
742
|
|
|
743
|
+
// 超时 / 用户响应 / stop / complete 收敛后清理请求级映射,避免 Map 单调增长并释放 bridge
|
|
744
|
+
bridge.on('permission:settled', (requestId: string) => {
|
|
745
|
+
permissionRequestBridge.delete(requestId);
|
|
746
|
+
});
|
|
747
|
+
bridge.on('elicitation:settled', (requestId: string) => {
|
|
748
|
+
elicitationRequestBridge.delete(requestId);
|
|
749
|
+
});
|
|
750
|
+
|
|
707
751
|
const onInitialized = (payload: Record<string, unknown>) => {
|
|
708
752
|
if (payload.sessionId !== data.sessionId) return;
|
|
709
753
|
bridge.off('acp:initialized', onInitialized);
|
|
710
754
|
bridge.off('acp:stopped', onStopped);
|
|
711
755
|
console.log(`[ACP ctrl] emitting acp:start:result: requestId=${data.requestId} sessionId=${data.sessionId} success=true externalSessionId=${payload.acpSessionId}`);
|
|
712
|
-
const result
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
};
|
|
718
|
-
result.modes = payload.modes ?? [];
|
|
719
|
-
if (payload.currentModeId) result.currentModeId = payload.currentModeId;
|
|
720
|
-
if (payload.currentModel) result.currentModel = payload.currentModel;
|
|
721
|
-
result.availableModels = payload.availableModels ?? [];
|
|
722
|
-
if (payload.currentModelId) result.currentModelId = payload.currentModelId;
|
|
723
|
-
result.configOptions = payload.configOptions ?? [];
|
|
724
|
-
if (payload.promptCapabilities) result.promptCapabilities = payload.promptCapabilities;
|
|
725
|
-
if (payload.agentInfo) result.agentInfo = payload.agentInfo;
|
|
726
|
-
if (payload.authMethods) result.authMethods = payload.authMethods;
|
|
727
|
-
if (payload.supportsAdditionalDirectories != null) {
|
|
728
|
-
result.supportsAdditionalDirectories = payload.supportsAdditionalDirectories;
|
|
729
|
-
}
|
|
756
|
+
const result = buildAcpInitResult(payload);
|
|
757
|
+
result.requestId = data.requestId;
|
|
758
|
+
result.sessionId = data.sessionId;
|
|
759
|
+
result.success = true;
|
|
760
|
+
result.externalSessionId = payload.acpSessionId;
|
|
730
761
|
socket.emit('acp:start:result', result);
|
|
731
762
|
};
|
|
732
763
|
|
|
733
764
|
const onStopped = (payload: { sessionId: string; reason: string; error?: string }) => {
|
|
734
765
|
if (payload.sessionId !== data.sessionId) return;
|
|
766
|
+
// 持有权守卫:同 sessionId 可能已被新 bridge 接管(stop 后立即 resume),
|
|
767
|
+
// 旧 bridge 的迟到 stopped 不得删除新会话的注册/路由。
|
|
768
|
+
// current === undefined 也视为「已不是当前持有者」(替换路径 delete→set 窗口),
|
|
769
|
+
// 否则旧 bridge 会注销新会话刚注册的 MCP relay / process registry。
|
|
770
|
+
const current = acpBridges.get(data.sessionId);
|
|
771
|
+
if (current !== bridge) return;
|
|
735
772
|
bridge.off('acp:initialized', onInitialized);
|
|
736
773
|
bridge.off('acp:stopped', onStopped);
|
|
737
774
|
acpBridges.delete(data.sessionId);
|
|
@@ -772,7 +809,8 @@ export function connectToServer(
|
|
|
772
809
|
if (existingBridge) {
|
|
773
810
|
console.warn(`[ACP ctrl] acp:resume for session ${data.sessionId} already has a bridge, stopping the previous one`);
|
|
774
811
|
acpBridges.delete(data.sessionId);
|
|
775
|
-
|
|
812
|
+
// silent:同 sessionId 重建时旧 bridge 不得向服务端发 stopped(会删除新会话映射)
|
|
813
|
+
await existingBridge.stop({ silent: true }).catch(() => {});
|
|
776
814
|
}
|
|
777
815
|
const spawner = createSpawner(data.serverType, data.sessionId, data.cwd);
|
|
778
816
|
if (data.mcpEndpoint) {
|
|
@@ -804,10 +842,12 @@ export function connectToServer(
|
|
|
804
842
|
|
|
805
843
|
fsHandler.registerAllowedPath(data.cwd);
|
|
806
844
|
bridge.on('acp:stopped', (payload: { sessionId: string }) => {
|
|
807
|
-
if (payload.sessionId
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
845
|
+
if (payload.sessionId !== data.sessionId) return;
|
|
846
|
+
// 持有权守卫(与 onStopped 同口径):旧 bridge 的迟到 stopped 不得注销新会话的
|
|
847
|
+
// process registry / MCP relay 注册(否则对账盲区 + MCP 断链)。
|
|
848
|
+
if (acpBridges.get(data.sessionId) !== bridge) return;
|
|
849
|
+
processRegistry.deregister(data.sessionId);
|
|
850
|
+
mcpRelay.unregisterSession(data.sessionId);
|
|
811
851
|
});
|
|
812
852
|
|
|
813
853
|
bridge.on('permission:sent', (requestId: string) => {
|
|
@@ -818,29 +858,33 @@ export function connectToServer(
|
|
|
818
858
|
elicitationRequestBridge.set(requestId, bridge);
|
|
819
859
|
});
|
|
820
860
|
|
|
861
|
+
// 超时 / 用户响应 / stop / complete 收敛后清理请求级映射,避免 Map 单调增长并释放 bridge
|
|
862
|
+
bridge.on('permission:settled', (requestId: string) => {
|
|
863
|
+
permissionRequestBridge.delete(requestId);
|
|
864
|
+
});
|
|
865
|
+
bridge.on('elicitation:settled', (requestId: string) => {
|
|
866
|
+
elicitationRequestBridge.delete(requestId);
|
|
867
|
+
});
|
|
868
|
+
|
|
821
869
|
const onInitialized = (payload: Record<string, unknown>) => {
|
|
822
870
|
if (payload.sessionId !== data.sessionId) return;
|
|
823
871
|
bridge.off('acp:initialized', onInitialized);
|
|
824
872
|
bridge.off('acp:stopped', onStopped);
|
|
825
|
-
const result
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
};
|
|
830
|
-
result.modes = payload.modes ?? [];
|
|
831
|
-
if (payload.currentModeId) result.currentModeId = payload.currentModeId;
|
|
832
|
-
if (payload.currentModel) result.currentModel = payload.currentModel;
|
|
833
|
-
result.availableModels = payload.availableModels ?? [];
|
|
834
|
-
if (payload.currentModelId) result.currentModelId = payload.currentModelId;
|
|
835
|
-
result.configOptions = payload.configOptions ?? [];
|
|
836
|
-
if (payload.promptCapabilities) result.promptCapabilities = payload.promptCapabilities;
|
|
837
|
-
if (payload.agentInfo) result.agentInfo = payload.agentInfo;
|
|
838
|
-
if (payload.authMethods) result.authMethods = payload.authMethods;
|
|
873
|
+
const result = buildAcpInitResult(payload);
|
|
874
|
+
result.requestId = data.requestId;
|
|
875
|
+
result.sessionId = data.sessionId;
|
|
876
|
+
result.success = true;
|
|
839
877
|
socket.emit('acp:resume:result', result);
|
|
840
878
|
};
|
|
841
879
|
|
|
842
880
|
const onStopped = (payload: { sessionId: string; reason: string; error?: string }) => {
|
|
843
881
|
if (payload.sessionId !== data.sessionId) return;
|
|
882
|
+
// 持有权守卫:同 sessionId 可能已被新 bridge 接管(stop 后立即 resume),
|
|
883
|
+
// 旧 bridge 的迟到 stopped 不得删除新会话的注册/路由。
|
|
884
|
+
// current === undefined 也视为「已不是当前持有者」(替换路径 delete→set 窗口),
|
|
885
|
+
// 否则旧 bridge 会注销新会话刚注册的 MCP relay / process registry。
|
|
886
|
+
const current = acpBridges.get(data.sessionId);
|
|
887
|
+
if (current !== bridge) return;
|
|
844
888
|
bridge.off('acp:initialized', onInitialized);
|
|
845
889
|
bridge.off('acp:stopped', onStopped);
|
|
846
890
|
acpBridges.delete(data.sessionId);
|
|
@@ -1018,7 +1062,6 @@ export function connectToServer(
|
|
|
1018
1062
|
let killedByRegistry = false;
|
|
1019
1063
|
if (bridge) {
|
|
1020
1064
|
await bridge.stop();
|
|
1021
|
-
acpBridges.delete(data.sessionId);
|
|
1022
1065
|
} else {
|
|
1023
1066
|
// Bridge not found: fallback to registry PID kill
|
|
1024
1067
|
const entry = processRegistry.get(data.sessionId);
|
|
@@ -1043,13 +1086,21 @@ export function connectToServer(
|
|
|
1043
1086
|
processRegistry.deregister(data.sessionId);
|
|
1044
1087
|
}
|
|
1045
1088
|
}
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1089
|
+
// await 期间同 sessionId 可能已被新 bridge 接管(stop 后立即 resume):
|
|
1090
|
+
// 仅当自己仍是当前持有者时才清理映射/回 ack,避免误杀新会话(R6c)。
|
|
1091
|
+
const stillCurrent = bridge
|
|
1092
|
+
? acpBridges.get(data.sessionId) === bridge
|
|
1093
|
+
: !acpBridges.get(data.sessionId);
|
|
1094
|
+
if (stillCurrent) {
|
|
1095
|
+
if (bridge) acpBridges.delete(data.sessionId);
|
|
1096
|
+
mcpRelay.unregisterSession(data.sessionId);
|
|
1097
|
+
if (data.ackRequired) {
|
|
1098
|
+
socket.emit('acp:stopped', {
|
|
1099
|
+
sessionId: data.sessionId,
|
|
1100
|
+
reason: 'user_close',
|
|
1101
|
+
killed: killedByRegistry,
|
|
1102
|
+
});
|
|
1103
|
+
}
|
|
1053
1104
|
}
|
|
1054
1105
|
});
|
|
1055
1106
|
|
|
@@ -1322,7 +1373,11 @@ export function connectToServer(
|
|
|
1322
1373
|
// Also clean up bridge if present
|
|
1323
1374
|
const bridge = acpBridges.get(entry.sessionId);
|
|
1324
1375
|
if (bridge) {
|
|
1376
|
+
// 先同步收敛 pending(在 stop 的 5s race 之前),再停止进程;
|
|
1377
|
+
// silent:对账杀掉的是旧会话,不得发 stopped 删除同 sessionId 的新映射。
|
|
1378
|
+
bridge.settlePendingOnDisconnect();
|
|
1325
1379
|
acpBridges.delete(entry.sessionId);
|
|
1380
|
+
void bridge.stop({ silent: true }).catch(() => { /* ignore */ });
|
|
1326
1381
|
}
|
|
1327
1382
|
}
|
|
1328
1383
|
}
|
|
@@ -1374,6 +1429,11 @@ export function connectToServer(
|
|
|
1374
1429
|
console.log(`Disconnected: ${reason}`);
|
|
1375
1430
|
heartbeat.stop();
|
|
1376
1431
|
gatewayManager.closeAll();
|
|
1432
|
+
// 断线后 pending 计时器必须就地收敛:socket.io 会缓存断线期间 emit,
|
|
1433
|
+
// 不收敛会在重连后补发 65s timeout(待办被误标 expired)。
|
|
1434
|
+
for (const bridge of acpBridges.values()) {
|
|
1435
|
+
bridge.settlePendingOnDisconnect();
|
|
1436
|
+
}
|
|
1377
1437
|
permissionRequestBridge.clear();
|
|
1378
1438
|
elicitationRequestBridge.clear();
|
|
1379
1439
|
mcpRelay.handleDisconnect();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { describe, it, expect, afterEach } from 'vitest';
|
|
2
|
-
import { mkdtemp, mkdir, rm, access } from 'node:fs/promises';
|
|
1
|
+
import { describe, it, expect, afterEach, vi } from 'vitest';
|
|
2
|
+
import { mkdtemp, mkdir, rm, access, writeFile } from 'node:fs/promises';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
4
|
import { setTimeout as delay } from 'node:timers/promises';
|
|
5
5
|
import {
|
|
@@ -676,3 +676,145 @@ describe('WorkloadExecutor container env + secret redaction (T-AG13/T-AG14)', ()
|
|
|
676
676
|
expect(redactSecrets('nothing to hide', secrets)).toBe('nothing to hide');
|
|
677
677
|
});
|
|
678
678
|
});
|
|
679
|
+
|
|
680
|
+
describe('WorkloadExecutor githubAuth prefetch (T-WE-G)', () => {
|
|
681
|
+
const dirs: string[] = [];
|
|
682
|
+
|
|
683
|
+
afterEach(async () => {
|
|
684
|
+
await Promise.all(dirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true })));
|
|
685
|
+
});
|
|
686
|
+
|
|
687
|
+
async function tmp(): Promise<string> {
|
|
688
|
+
const dir = await makeTmpDir();
|
|
689
|
+
dirs.push(dir);
|
|
690
|
+
return dir;
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
it('T-WE-G1: githubAuth=true calls the hook once before the first script runs', async () => {
|
|
694
|
+
const executor = new WorkloadExecutor();
|
|
695
|
+
const dir = await tmp();
|
|
696
|
+
const marker = join(dir, 'hook.marker');
|
|
697
|
+
const { results, sink } = collect();
|
|
698
|
+
const calls: string[] = [];
|
|
699
|
+
|
|
700
|
+
const outcome = await executor.runBatch(
|
|
701
|
+
{
|
|
702
|
+
phase: 'init',
|
|
703
|
+
githubAuth: true,
|
|
704
|
+
items: [
|
|
705
|
+
{ logId: 'g1', workloadName: 'One', script: `test -f "${marker}" && echo ready`, timeoutMs: 5000 },
|
|
706
|
+
{ logId: 'g2', workloadName: 'Two', script: `test -f "${marker}" && echo ready`, timeoutMs: 5000 },
|
|
707
|
+
],
|
|
708
|
+
},
|
|
709
|
+
sink,
|
|
710
|
+
{
|
|
711
|
+
beforeGithubAuth: async () => {
|
|
712
|
+
calls.push('hook');
|
|
713
|
+
await writeFile(marker, 'written');
|
|
714
|
+
},
|
|
715
|
+
},
|
|
716
|
+
);
|
|
717
|
+
|
|
718
|
+
expect(calls).toEqual(['hook']);
|
|
719
|
+
expect(outcome.failedCount).toBe(0);
|
|
720
|
+
expect(results.map((result) => result.status)).toEqual(['success', 'success']);
|
|
721
|
+
expect(results.map((result) => result.log.trim())).toEqual(['ready', 'ready']);
|
|
722
|
+
});
|
|
723
|
+
|
|
724
|
+
it('T-WE-G2: hook failure reports failed for every item and never spawns scripts', async () => {
|
|
725
|
+
const executor = new WorkloadExecutor();
|
|
726
|
+
const dir = await tmp();
|
|
727
|
+
const marker = join(dir, 'should-not-exist');
|
|
728
|
+
const { results, sink } = collect();
|
|
729
|
+
|
|
730
|
+
const outcome = await executor.runBatch(
|
|
731
|
+
{
|
|
732
|
+
phase: 'init',
|
|
733
|
+
githubAuth: true,
|
|
734
|
+
items: [
|
|
735
|
+
{ logId: 'g1', workloadName: 'One', script: `touch "${marker}"`, timeoutMs: 5000 },
|
|
736
|
+
{ logId: 'g2', workloadName: 'Two', script: `touch "${marker}"`, timeoutMs: 5000 },
|
|
737
|
+
],
|
|
738
|
+
},
|
|
739
|
+
sink,
|
|
740
|
+
{
|
|
741
|
+
beforeGithubAuth: async () => {
|
|
742
|
+
throw new Error('未设置 Github 项目');
|
|
743
|
+
},
|
|
744
|
+
},
|
|
745
|
+
);
|
|
746
|
+
|
|
747
|
+
expect(outcome.failedCount).toBe(2);
|
|
748
|
+
expect(outcome.total).toBe(2);
|
|
749
|
+
expect(results.map((result) => result.status)).toEqual(['failed', 'failed']);
|
|
750
|
+
expect(results.map((result) => result.log)).toEqual([
|
|
751
|
+
'Github 凭据申请失败:未设置 Github 项目',
|
|
752
|
+
'Github 凭据申请失败:未设置 Github 项目',
|
|
753
|
+
]);
|
|
754
|
+
expect(await fileExists(marker)).toBe(false);
|
|
755
|
+
});
|
|
756
|
+
|
|
757
|
+
it('T-WE-G3: no githubAuth field keeps legacy behavior and never calls the hook', async () => {
|
|
758
|
+
const executor = new WorkloadExecutor();
|
|
759
|
+
const { results, sink } = collect();
|
|
760
|
+
const hook = vi.fn(async () => {});
|
|
761
|
+
|
|
762
|
+
const outcome = await executor.runBatch(
|
|
763
|
+
{
|
|
764
|
+
phase: 'init',
|
|
765
|
+
items: [{ logId: 'legacy', workloadName: 'Legacy', script: 'echo legacy', timeoutMs: 5000 }],
|
|
766
|
+
},
|
|
767
|
+
sink,
|
|
768
|
+
{ beforeGithubAuth: hook },
|
|
769
|
+
);
|
|
770
|
+
|
|
771
|
+
expect(hook).not.toHaveBeenCalled();
|
|
772
|
+
expect(outcome.failedCount).toBe(0);
|
|
773
|
+
expect(results.map((result) => result.status)).toEqual(['success']);
|
|
774
|
+
expect(results[0].log.trim()).toBe('legacy');
|
|
775
|
+
});
|
|
776
|
+
|
|
777
|
+
it('T-WE-G4: githubAuth=false is accepted and behaves like the legacy payload', async () => {
|
|
778
|
+
const executor = new WorkloadExecutor();
|
|
779
|
+
const { results, sink } = collect();
|
|
780
|
+
const hook = vi.fn(async () => {});
|
|
781
|
+
|
|
782
|
+
const outcome = await executor.runBatch(
|
|
783
|
+
{
|
|
784
|
+
phase: 'post',
|
|
785
|
+
githubAuth: false,
|
|
786
|
+
items: [{ logId: 'off', workloadName: 'Off', script: 'echo off', timeoutMs: 5000 }],
|
|
787
|
+
},
|
|
788
|
+
sink,
|
|
789
|
+
{ beforeGithubAuth: hook },
|
|
790
|
+
);
|
|
791
|
+
|
|
792
|
+
expect(hook).not.toHaveBeenCalled();
|
|
793
|
+
expect(outcome.failedCount).toBe(0);
|
|
794
|
+
expect(results[0].status).toBe('success');
|
|
795
|
+
});
|
|
796
|
+
|
|
797
|
+
it('T-WE-G2b: redacts secrets from the hook failure reason', async () => {
|
|
798
|
+
const executor = new WorkloadExecutor();
|
|
799
|
+
const { results, sink } = collect();
|
|
800
|
+
const secret = 'zag_k_1_super_secret';
|
|
801
|
+
|
|
802
|
+
await executor.runBatch(
|
|
803
|
+
{
|
|
804
|
+
phase: 'init',
|
|
805
|
+
githubAuth: true,
|
|
806
|
+
env: { ZAG_HOST_KEY: secret },
|
|
807
|
+
items: [{ logId: 'redact', workloadName: 'Redact', script: 'echo nope', timeoutMs: 5000 }],
|
|
808
|
+
},
|
|
809
|
+
sink,
|
|
810
|
+
{
|
|
811
|
+
beforeGithubAuth: async () => {
|
|
812
|
+
throw new Error(`hook failed with ${secret}`);
|
|
813
|
+
},
|
|
814
|
+
},
|
|
815
|
+
);
|
|
816
|
+
|
|
817
|
+
expect(results[0].log).toContain(REDACTED);
|
|
818
|
+
expect(results[0].log).not.toContain(secret);
|
|
819
|
+
});
|
|
820
|
+
});
|
package/src/workload-executor.ts
CHANGED
|
@@ -44,6 +44,20 @@ export interface WorkloadRunPayload {
|
|
|
44
44
|
* 对批次内所有 item 生效;`item.params` 派生的 `WORKLOAD_PARAM_*` 同名时优先。
|
|
45
45
|
*/
|
|
46
46
|
env?: Record<string, string>;
|
|
47
|
+
/**
|
|
48
|
+
* `true` 时必须在执行本批第一条脚本前完成 gh 凭据申请与写入(由 `beforeGithubAuth` 承担);
|
|
49
|
+
* 缺省/false 行为与旧版完全一致。
|
|
50
|
+
*/
|
|
51
|
+
githubAuth?: boolean;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** `runBatch` 的可选行为注入(默认空实现,保证旧调用方零变化)。 */
|
|
55
|
+
export interface WorkloadRunOptions {
|
|
56
|
+
/**
|
|
57
|
+
* `payload.githubAuth === true` 时、执行首条脚本前调用一次。
|
|
58
|
+
* 抛错时本批所有 item 直接回报 `failed`,且不启动任何脚本。
|
|
59
|
+
*/
|
|
60
|
+
beforeGithubAuth?: (payload: WorkloadRunPayload) => Promise<void>;
|
|
47
61
|
}
|
|
48
62
|
|
|
49
63
|
/** 需要在日志/错误/脚本输出中脱敏的环境变量名(契约 §2.9「云电脑初始化 env」)。 */
|
|
@@ -273,12 +287,16 @@ export class WorkloadExecutor {
|
|
|
273
287
|
/**
|
|
274
288
|
* 执行一个批次。若已有批次在跑,先终止其进程组并停止推进,再执行新批次。
|
|
275
289
|
*/
|
|
276
|
-
runBatch(
|
|
290
|
+
runBatch(
|
|
291
|
+
payload: WorkloadRunPayload,
|
|
292
|
+
onResult: WorkloadResultSink,
|
|
293
|
+
options: WorkloadRunOptions = {},
|
|
294
|
+
): Promise<WorkloadBatchOutcome> {
|
|
277
295
|
this.interruptActive();
|
|
278
296
|
|
|
279
297
|
const run: ActiveRun = { cancelled: false, child: null };
|
|
280
298
|
this.active = run;
|
|
281
|
-
const promise = this.executeBatch(payload, onResult, run).finally(() => {
|
|
299
|
+
const promise = this.executeBatch(payload, onResult, run, options).finally(() => {
|
|
282
300
|
// 仅当自己仍是当前批次时才清空(被打断时 active 已指向新批次)
|
|
283
301
|
if (this.active === run) {
|
|
284
302
|
this.active = null;
|
|
@@ -293,6 +311,7 @@ export class WorkloadExecutor {
|
|
|
293
311
|
payload: WorkloadRunPayload,
|
|
294
312
|
onResult: WorkloadResultSink,
|
|
295
313
|
run: ActiveRun,
|
|
314
|
+
options: WorkloadRunOptions = {},
|
|
296
315
|
): Promise<WorkloadBatchOutcome> {
|
|
297
316
|
const items = Array.isArray(payload?.items) ? payload.items : [];
|
|
298
317
|
const total = items.length;
|
|
@@ -310,6 +329,33 @@ export class WorkloadExecutor {
|
|
|
310
329
|
const containerEnv = normalizeContainerEnv(payload.env);
|
|
311
330
|
const secrets = collectSecretValues(process.env, containerEnv);
|
|
312
331
|
|
|
332
|
+
// Github 凭据预取:仅在显式 `githubAuth === true` 时触发,失败则整批逐条 failed、不启动脚本。
|
|
333
|
+
// 缺省/false 时不调用 hook,行为与旧版完全一致。
|
|
334
|
+
if (payload.githubAuth === true && options.beforeGithubAuth) {
|
|
335
|
+
try {
|
|
336
|
+
await options.beforeGithubAuth(payload);
|
|
337
|
+
} catch (error) {
|
|
338
|
+
if (run.cancelled) return outcome();
|
|
339
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
340
|
+
const message = redactSecrets(`Github 凭据申请失败:${reason}`, secrets);
|
|
341
|
+
const now = nowIso();
|
|
342
|
+
for (const item of items) {
|
|
343
|
+
if (run.cancelled) return outcome();
|
|
344
|
+
failedCount += 1;
|
|
345
|
+
this.report(onResult, {
|
|
346
|
+
logId: item.logId,
|
|
347
|
+
status: 'failed',
|
|
348
|
+
exitCode: null,
|
|
349
|
+
log: message,
|
|
350
|
+
startedAt: now,
|
|
351
|
+
finishedAt: nowIso(),
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
return outcome();
|
|
355
|
+
}
|
|
356
|
+
if (run.cancelled) return outcome();
|
|
357
|
+
}
|
|
358
|
+
|
|
313
359
|
for (const item of items) {
|
|
314
360
|
if (run.cancelled) return outcome();
|
|
315
361
|
|
|
@@ -65,7 +65,11 @@ describe('AcpSyncBridge', () => {
|
|
|
65
65
|
expect(mockSpawner.start).toHaveBeenCalledOnce();
|
|
66
66
|
expect(mc().initialize).toHaveBeenCalledWith({
|
|
67
67
|
protocolVersion: 1,
|
|
68
|
-
|
|
68
|
+
// 显式值断言(非 CLIENT_CAPABILITIES 自引用)
|
|
69
|
+
clientCapabilities: {
|
|
70
|
+
session: { configOptions: { boolean: {} } },
|
|
71
|
+
elicitation: { form: {}, url: {} },
|
|
72
|
+
},
|
|
69
73
|
clientInfo: { name: 'agent-gear', version: '1.0.0' },
|
|
70
74
|
});
|
|
71
75
|
});
|
|
@@ -229,7 +229,7 @@ describe('SocketIO Client', () => {
|
|
|
229
229
|
expect(mockSocket.emit).toHaveBeenCalledWith('register', {
|
|
230
230
|
key: 'zag_k_1_abc123',
|
|
231
231
|
hostname: 'my-machine',
|
|
232
|
-
version: '2.
|
|
232
|
+
version: '2.6.0',
|
|
233
233
|
resumeToken: undefined,
|
|
234
234
|
homeDir: '/home/user',
|
|
235
235
|
platform: 'linux',
|
|
@@ -247,7 +247,7 @@ describe('SocketIO Client', () => {
|
|
|
247
247
|
expect(mockSocket.emit).toHaveBeenCalledWith('register', {
|
|
248
248
|
key: 'zag_k_1_abc123',
|
|
249
249
|
hostname: 'test-machine',
|
|
250
|
-
version: '2.
|
|
250
|
+
version: '2.6.0',
|
|
251
251
|
resumeToken: undefined,
|
|
252
252
|
homeDir: '/home/user',
|
|
253
253
|
platform: 'linux',
|
package/vitest.config.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import { defineConfig } from 'vitest/config';
|
|
1
|
+
import { configDefaults, defineConfig } from 'vitest/config';
|
|
2
2
|
|
|
3
3
|
export default defineConfig({
|
|
4
4
|
test: {
|
|
5
5
|
globals: true,
|
|
6
6
|
environment: 'node',
|
|
7
|
+
// tsconfig 会把 src/**/*.test.ts 一并编译进 dist;在 vitest 默认排除项之外
|
|
8
|
+
// 追加 **/dist/**,避免构建后重复执行编译副本(且 sidecar fixture 如 .mjs
|
|
9
|
+
// 不会被 tsc 复制而必然失败)。不得用自定义数组覆盖默认值(会丢 .git 等默认排除)。
|
|
10
|
+
exclude: [...configDefaults.exclude, '**/dist/**'],
|
|
7
11
|
coverage: {
|
|
8
12
|
provider: 'v8',
|
|
9
13
|
reporter: ['text', 'text-summary'],
|