agent-gear 2.12.0 → 2.14.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 +20 -2
- package/dist/acp/bridge.d.ts +4 -0
- package/dist/acp/bridge.d.ts.map +1 -1
- package/dist/acp/bridge.js +72 -45
- package/dist/acp/bridge.js.map +1 -1
- package/dist/acp/bridge.test.js +40 -33
- package/dist/acp/bridge.test.js.map +1 -1
- package/dist/acp/process-registry.d.ts.map +1 -1
- package/dist/acp/process-registry.js +3 -1
- package/dist/acp/process-registry.js.map +1 -1
- package/dist/acp/process-registry.test.js +1 -0
- package/dist/acp/process-registry.test.js.map +1 -1
- package/dist/acp/spawner.d.ts +15 -0
- package/dist/acp/spawner.d.ts.map +1 -1
- package/dist/acp/spawner.js +51 -4
- package/dist/acp/spawner.js.map +1 -1
- package/dist/acp/sync-bridge.d.ts.map +1 -1
- package/dist/acp/sync-bridge.js +3 -1
- package/dist/acp/sync-bridge.js.map +1 -1
- package/dist/cloud-host/supervisor.d.ts +3 -1
- package/dist/cloud-host/supervisor.d.ts.map +1 -1
- package/dist/cloud-host/supervisor.js +18 -12
- package/dist/cloud-host/supervisor.js.map +1 -1
- package/dist/cloud-host/supervisor.test.js +19 -14
- package/dist/cloud-host/supervisor.test.js.map +1 -1
- package/dist/commands/harness.d.ts.map +1 -1
- package/dist/commands/harness.js +4 -2
- package/dist/commands/harness.js.map +1 -1
- package/dist/commands/mcp-proxy.d.ts +2 -1
- package/dist/commands/mcp-proxy.d.ts.map +1 -1
- package/dist/commands/mcp-proxy.js +26 -18
- package/dist/commands/mcp-proxy.js.map +1 -1
- package/dist/commands/mcp.js +1 -1
- package/dist/commands/mcp.js.map +1 -1
- package/dist/gateway.d.ts +3 -0
- package/dist/gateway.d.ts.map +1 -1
- package/dist/gateway.js +14 -8
- package/dist/gateway.js.map +1 -1
- package/dist/gateway.test.js +9 -4
- package/dist/gateway.test.js.map +1 -1
- package/dist/github/commit-identity-manager.d.ts +3 -1
- package/dist/github/commit-identity-manager.d.ts.map +1 -1
- package/dist/github/commit-identity-manager.js +13 -6
- package/dist/github/commit-identity-manager.js.map +1 -1
- package/dist/github/commit-identity-manager.test.js +26 -24
- package/dist/github/commit-identity-manager.test.js.map +1 -1
- package/dist/github/credential-manager.d.ts +3 -1
- package/dist/github/credential-manager.d.ts.map +1 -1
- package/dist/github/credential-manager.js +18 -10
- package/dist/github/credential-manager.js.map +1 -1
- package/dist/github/credential-manager.test.js +36 -33
- package/dist/github/credential-manager.test.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +77 -70
- package/dist/index.js.map +1 -1
- package/dist/mcp/local-server.d.ts +3 -1
- package/dist/mcp/local-server.d.ts.map +1 -1
- package/dist/mcp/local-server.js +19 -9
- package/dist/mcp/local-server.js.map +1 -1
- package/dist/mcp/relay.d.ts +3 -1
- package/dist/mcp/relay.d.ts.map +1 -1
- package/dist/mcp/relay.js +20 -12
- package/dist/mcp/relay.js.map +1 -1
- package/dist/observability/http-context.d.ts +32 -0
- package/dist/observability/http-context.d.ts.map +1 -0
- package/dist/observability/http-context.js +97 -0
- package/dist/observability/http-context.js.map +1 -0
- package/dist/observability/log-context.d.ts +19 -0
- package/dist/observability/log-context.d.ts.map +1 -0
- package/dist/observability/log-context.js +21 -0
- package/dist/observability/log-context.js.map +1 -0
- package/dist/observability/logger.d.ts +31 -0
- package/dist/observability/logger.d.ts.map +1 -0
- package/dist/observability/logger.js +129 -0
- package/dist/observability/logger.js.map +1 -0
- package/dist/observability/no-console.test.d.ts +2 -0
- package/dist/observability/no-console.test.d.ts.map +1 -0
- package/dist/observability/no-console.test.js +78 -0
- package/dist/observability/no-console.test.js.map +1 -0
- package/dist/observability/register.d.ts +2 -0
- package/dist/observability/register.d.ts.map +1 -0
- package/dist/observability/register.js +29 -0
- package/dist/observability/register.js.map +1 -0
- package/dist/observability/telemetry.d.ts +12 -0
- package/dist/observability/telemetry.d.ts.map +1 -0
- package/dist/observability/telemetry.js +164 -0
- package/dist/observability/telemetry.js.map +1 -0
- package/dist/observability/testing.d.ts +24 -0
- package/dist/observability/testing.d.ts.map +1 -0
- package/dist/observability/testing.js +31 -0
- package/dist/observability/testing.js.map +1 -0
- package/dist/observability/trace.d.ts +42 -0
- package/dist/observability/trace.d.ts.map +1 -0
- package/dist/observability/trace.js +123 -0
- package/dist/observability/trace.js.map +1 -0
- package/dist/socketio-client.auth-gate.test.js +0 -3
- package/dist/socketio-client.auth-gate.test.js.map +1 -1
- package/dist/socketio-client.d.ts +3 -0
- package/dist/socketio-client.d.ts.map +1 -1
- package/dist/socketio-client.js +140 -69
- package/dist/socketio-client.js.map +1 -1
- package/dist/socketio-client.test.js +0 -6
- package/dist/socketio-client.test.js.map +1 -1
- package/dist/startup-span.d.ts +15 -0
- package/dist/startup-span.d.ts.map +1 -0
- package/dist/startup-span.js +21 -0
- package/dist/startup-span.js.map +1 -0
- package/dist/startup-span.test.d.ts +2 -0
- package/dist/startup-span.test.d.ts.map +1 -0
- package/dist/startup-span.test.js +71 -0
- package/dist/startup-span.test.js.map +1 -0
- package/dist/worktree-handler.d.ts +25 -2
- package/dist/worktree-handler.d.ts.map +1 -1
- package/dist/worktree-handler.js +95 -5
- package/dist/worktree-handler.js.map +1 -1
- package/dist/worktree-handler.test.js +88 -5
- package/dist/worktree-handler.test.js.map +1 -1
- package/package.json +8 -1
- package/src/acp/bridge.test.ts +63 -34
- package/src/acp/bridge.ts +95 -51
- package/src/acp/process-registry.test.ts +1 -0
- package/src/acp/process-registry.ts +4 -1
- package/src/acp/spawner.ts +68 -4
- package/src/acp/sync-bridge.ts +4 -1
- package/src/cloud-host/supervisor.test.ts +25 -14
- package/src/cloud-host/supervisor.ts +29 -14
- package/src/commands/harness.ts +4 -2
- package/src/commands/mcp-proxy.ts +41 -18
- package/src/commands/mcp.ts +1 -1
- package/src/gateway.test.ts +10 -5
- package/src/gateway.ts +15 -8
- package/src/github/commit-identity-manager.test.ts +30 -24
- package/src/github/commit-identity-manager.ts +19 -9
- package/src/github/credential-manager.test.ts +42 -33
- package/src/github/credential-manager.ts +28 -15
- package/src/index.ts +85 -74
- package/src/mcp/local-server.ts +31 -9
- package/src/mcp/relay.ts +32 -13
- package/src/observability/http-context.ts +122 -0
- package/src/observability/log-context.ts +39 -0
- package/src/observability/logger.ts +160 -0
- package/src/observability/no-console.test.ts +99 -0
- package/src/observability/register.ts +34 -0
- package/src/observability/telemetry.ts +180 -0
- package/src/observability/testing.ts +51 -0
- package/src/observability/trace.ts +163 -0
- package/src/socketio-client.auth-gate.test.ts +0 -3
- package/src/socketio-client.test.ts +0 -6
- package/src/socketio-client.ts +220 -71
- package/src/startup-span.test.ts +83 -0
- package/src/startup-span.ts +27 -0
- package/src/worktree-handler.test.ts +106 -5
- package/src/worktree-handler.ts +116 -6
- package/tests/acp-bridge.test.ts +11 -3
- package/tests/acp-spawner.test.ts +119 -14
- package/tests/acp-wire-elicitation.test.ts +0 -3
- package/tests/acp-wire-mcp.test.ts +9 -6
- package/tests/commands/harness.test.ts +6 -0
- package/tests/commands/mcp-proxy-dispatcher.test.ts +0 -2
- package/tests/commands/mcp-proxy.test.ts +43 -0
- package/tests/commands/mcp.test.ts +4 -0
- package/tests/socketio-client.test.ts +114 -36
- package/vitest.config.ts +3 -0
package/src/gateway.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import net from 'node:net';
|
|
2
2
|
import type { Socket } from 'socket.io-client';
|
|
3
|
+
import { logger as rootLogger, type ObservationLogger } from './observability/logger.js';
|
|
3
4
|
|
|
4
5
|
interface Tunnel {
|
|
5
6
|
id: string;
|
|
@@ -17,6 +18,11 @@ const HTTP_RESPONSE_COLLECT_MS = 100;
|
|
|
17
18
|
export class GatewayManager {
|
|
18
19
|
private tunnels: Map<string, Tunnel> = new Map();
|
|
19
20
|
private reconnectTimers: Map<string, ReturnType<typeof setTimeout>> = new Map();
|
|
21
|
+
private log: ObservationLogger;
|
|
22
|
+
|
|
23
|
+
constructor(logger?: ObservationLogger) {
|
|
24
|
+
this.log = logger ?? rootLogger.child({ component: 'gateway' });
|
|
25
|
+
}
|
|
20
26
|
|
|
21
27
|
async createTunnel(tunnelId: string, port: number, serverSocket: Socket): Promise<void> {
|
|
22
28
|
this.tunnels.set(tunnelId, { id: tunnelId, port, localSocket: null, serverSocket, responseBuffer: [] });
|
|
@@ -32,7 +38,7 @@ export class GatewayManager {
|
|
|
32
38
|
|
|
33
39
|
return new Promise<void>((resolve) => {
|
|
34
40
|
const onError = (err: Error) => {
|
|
35
|
-
|
|
41
|
+
this.log.error({ event: 'gateway_tunnel_error', tunnelId, err }, 'gateway tunnel error');
|
|
36
42
|
localSocket.destroy();
|
|
37
43
|
this.scheduleReconnect(tunnelId);
|
|
38
44
|
resolve();
|
|
@@ -44,7 +50,7 @@ export class GatewayManager {
|
|
|
44
50
|
localSocket.removeListener('error', onError);
|
|
45
51
|
|
|
46
52
|
tunnel.localSocket = localSocket;
|
|
47
|
-
|
|
53
|
+
this.log.info({ event: 'gateway_tunnel_connected', tunnelId, port: tunnel.port }, 'gateway tunnel connected');
|
|
48
54
|
|
|
49
55
|
if (isInitial) {
|
|
50
56
|
tunnel.serverSocket.emit('port-forward:created', { tunnelId, success: true });
|
|
@@ -64,13 +70,13 @@ export class GatewayManager {
|
|
|
64
70
|
});
|
|
65
71
|
|
|
66
72
|
localSocket.on('close', () => {
|
|
67
|
-
|
|
73
|
+
this.log.info({ event: 'gateway_local_socket_closed', tunnelId }, 'gateway local socket closed');
|
|
68
74
|
tunnel.localSocket = null;
|
|
69
75
|
this.scheduleReconnect(tunnelId);
|
|
70
76
|
});
|
|
71
77
|
|
|
72
78
|
localSocket.on('error', (err: Error) => {
|
|
73
|
-
|
|
79
|
+
this.log.error({ event: 'gateway_local_socket_error', tunnelId, err }, 'gateway local socket error');
|
|
74
80
|
tunnel.serverSocket.emit('port-forward:error', { tunnelId, error: err.message });
|
|
75
81
|
});
|
|
76
82
|
|
|
@@ -100,7 +106,8 @@ export class GatewayManager {
|
|
|
100
106
|
private scheduleReconnect(tunnelId: string): void {
|
|
101
107
|
if (!this.tunnels.has(tunnelId) || this.reconnectTimers.has(tunnelId)) return;
|
|
102
108
|
|
|
103
|
-
|
|
109
|
+
// 3s 周期重连属轮询类高频事件 → debug(R6 日志量控制)
|
|
110
|
+
this.log.debug({ event: 'gateway_reconnect_scheduled', tunnelId }, 'gateway scheduling reconnection');
|
|
104
111
|
const timer = setTimeout(() => {
|
|
105
112
|
this.reconnectTimers.delete(tunnelId);
|
|
106
113
|
this.connect(tunnelId, false);
|
|
@@ -112,7 +119,7 @@ export class GatewayManager {
|
|
|
112
119
|
handleTunnelData(tunnelId: string, data: string): void {
|
|
113
120
|
const tunnel = this.tunnels.get(tunnelId);
|
|
114
121
|
if (!tunnel) {
|
|
115
|
-
|
|
122
|
+
this.log.warn({ event: 'gateway_unknown_tunnel', tunnelId }, 'gateway data for unknown tunnel');
|
|
116
123
|
return;
|
|
117
124
|
}
|
|
118
125
|
|
|
@@ -156,7 +163,7 @@ export class GatewayManager {
|
|
|
156
163
|
const tunnel = this.tunnels.get(tunnelId);
|
|
157
164
|
if (!tunnel) return;
|
|
158
165
|
|
|
159
|
-
|
|
166
|
+
this.log.info({ event: 'gateway_tunnel_close', tunnelId }, 'gateway closing tunnel');
|
|
160
167
|
|
|
161
168
|
const timer = this.reconnectTimers.get(tunnelId);
|
|
162
169
|
if (timer) {
|
|
@@ -173,7 +180,7 @@ export class GatewayManager {
|
|
|
173
180
|
}
|
|
174
181
|
|
|
175
182
|
closeAll(): void {
|
|
176
|
-
|
|
183
|
+
this.log.info({ event: 'gateway_close_all', count: this.tunnels.size }, 'gateway closing all tunnels');
|
|
177
184
|
|
|
178
185
|
for (const [tunnelId, tunnel] of [...this.tunnels]) {
|
|
179
186
|
const timer = this.reconnectTimers.get(tunnelId);
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
type CommitIdentityApplier,
|
|
7
7
|
type CommitIdentityRefreshResult,
|
|
8
8
|
} from './commit-identity-manager.js';
|
|
9
|
+
import { createTestLogger, type TestLogger } from '../observability/testing.js';
|
|
9
10
|
|
|
10
11
|
const HOST_KEY = 'zag_k_1_host_secret';
|
|
11
12
|
const BASE_ENV: NodeJS.ProcessEnv = {
|
|
@@ -60,12 +61,12 @@ describe('CommitIdentityManager', () => {
|
|
|
60
61
|
it('T-CIM1: first refresh posts with the host key and applies the returned identity', async () => {
|
|
61
62
|
const fetchFn = fakeFetch(async () => jsonResponse({ mode: 'co_author', ...CO_AUTHOR }));
|
|
62
63
|
const applier = fakeApplier();
|
|
63
|
-
const
|
|
64
|
+
const testLog: TestLogger = createTestLogger();
|
|
64
65
|
const manager = new CommitIdentityManager({
|
|
65
66
|
env: BASE_ENV,
|
|
66
67
|
fetch: fetchFn,
|
|
67
68
|
applier,
|
|
68
|
-
log:
|
|
69
|
+
log: testLog.logger,
|
|
69
70
|
});
|
|
70
71
|
|
|
71
72
|
const result = await manager.refreshNow();
|
|
@@ -85,11 +86,12 @@ describe('CommitIdentityManager', () => {
|
|
|
85
86
|
expect(init.signal).toBeInstanceOf(AbortSignal);
|
|
86
87
|
|
|
87
88
|
// 日志不含身份值(邮箱 / 姓名)与 host key
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
expect(
|
|
92
|
-
expect(
|
|
89
|
+
expect(testLog.records).toContainEqual(
|
|
90
|
+
expect.objectContaining({ component: 'commit-identity', msg: '提交身份已更新', mode: 'co_author' }),
|
|
91
|
+
);
|
|
92
|
+
expect(JSON.stringify(testLog.records)).not.toContain('Alice');
|
|
93
|
+
expect(JSON.stringify(testLog.records)).not.toContain('alice@users.noreply.github.com');
|
|
94
|
+
expect(JSON.stringify(testLog.records)).not.toContain(HOST_KEY);
|
|
93
95
|
});
|
|
94
96
|
|
|
95
97
|
it('T-CIM2: mode=unset removes the managed configuration instead of applying it', async () => {
|
|
@@ -99,7 +101,7 @@ describe('CommitIdentityManager', () => {
|
|
|
99
101
|
env: BASE_ENV,
|
|
100
102
|
fetch: fetchFn,
|
|
101
103
|
applier,
|
|
102
|
-
log: ()
|
|
104
|
+
log: createTestLogger().logger,
|
|
103
105
|
});
|
|
104
106
|
|
|
105
107
|
const result = await manager.refreshNow();
|
|
@@ -117,7 +119,7 @@ describe('CommitIdentityManager', () => {
|
|
|
117
119
|
env: BASE_ENV,
|
|
118
120
|
fetch: fetchFn,
|
|
119
121
|
applier,
|
|
120
|
-
log: ()
|
|
122
|
+
log: createTestLogger().logger,
|
|
121
123
|
setTimeout: scheduler.setTimeoutFn,
|
|
122
124
|
});
|
|
123
125
|
|
|
@@ -144,7 +146,7 @@ describe('CommitIdentityManager', () => {
|
|
|
144
146
|
env: BASE_ENV,
|
|
145
147
|
fetch: fetchFn,
|
|
146
148
|
applier,
|
|
147
|
-
log: ()
|
|
149
|
+
log: createTestLogger().logger,
|
|
148
150
|
sleep: async (ms) => {
|
|
149
151
|
delays.push(ms);
|
|
150
152
|
},
|
|
@@ -171,7 +173,7 @@ describe('CommitIdentityManager', () => {
|
|
|
171
173
|
env: BASE_ENV,
|
|
172
174
|
fetch: fetchFn,
|
|
173
175
|
applier,
|
|
174
|
-
log: ()
|
|
176
|
+
log: createTestLogger().logger,
|
|
175
177
|
});
|
|
176
178
|
|
|
177
179
|
const first = manager.refreshNow();
|
|
@@ -186,20 +188,24 @@ describe('CommitIdentityManager', () => {
|
|
|
186
188
|
|
|
187
189
|
it('T-CIM6: missing env disables startup with a warn that only lists variable names', async () => {
|
|
188
190
|
const fetchFn = fakeFetch(async () => jsonResponse({ mode: 'unset' }));
|
|
189
|
-
const
|
|
191
|
+
const testLog: TestLogger = createTestLogger();
|
|
190
192
|
const manager = new CommitIdentityManager({
|
|
191
193
|
env: { ZAG_HOST_KEY: 'should-not-appear' },
|
|
192
194
|
fetch: fetchFn,
|
|
193
|
-
log:
|
|
195
|
+
log: testLog.logger,
|
|
194
196
|
});
|
|
195
197
|
|
|
196
198
|
manager.start();
|
|
197
199
|
|
|
198
200
|
expect(fetchFn).not.toHaveBeenCalled();
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
201
|
+
expect(testLog.records).toContainEqual(
|
|
202
|
+
expect.objectContaining({
|
|
203
|
+
component: 'commit-identity',
|
|
204
|
+
msg: expect.stringContaining('disabled'),
|
|
205
|
+
missing: expect.arrayContaining(['ZAG_AGENT_API_ENDPOINT', 'ZAG_CONTROLLER_ID']),
|
|
206
|
+
}),
|
|
207
|
+
);
|
|
208
|
+
expect(JSON.stringify(testLog.records)).not.toContain('should-not-appear');
|
|
203
209
|
await expect(manager.refreshNow()).resolves.toEqual({ ok: false, error: 'disabled' });
|
|
204
210
|
});
|
|
205
211
|
|
|
@@ -213,7 +219,7 @@ describe('CommitIdentityManager', () => {
|
|
|
213
219
|
env: BASE_ENV,
|
|
214
220
|
fetch: invalidJson,
|
|
215
221
|
applier,
|
|
216
|
-
log: ()
|
|
222
|
+
log: createTestLogger().logger,
|
|
217
223
|
});
|
|
218
224
|
const invalid = await manager.refreshNow();
|
|
219
225
|
expect(invalid.ok).toBe(false);
|
|
@@ -224,7 +230,7 @@ describe('CommitIdentityManager', () => {
|
|
|
224
230
|
env: BASE_ENV,
|
|
225
231
|
fetch: invalidMode,
|
|
226
232
|
applier,
|
|
227
|
-
log: ()
|
|
233
|
+
log: createTestLogger().logger,
|
|
228
234
|
});
|
|
229
235
|
const modeResult = await modeManager.refreshNow();
|
|
230
236
|
expect(modeResult.ok).toBe(false);
|
|
@@ -237,7 +243,7 @@ describe('CommitIdentityManager', () => {
|
|
|
237
243
|
env: BASE_ENV,
|
|
238
244
|
fetch: network,
|
|
239
245
|
applier,
|
|
240
|
-
log: ()
|
|
246
|
+
log: createTestLogger().logger,
|
|
241
247
|
});
|
|
242
248
|
const failed = await networkManager.refreshNow();
|
|
243
249
|
expect(failed.ok).toBe(false);
|
|
@@ -249,7 +255,7 @@ describe('CommitIdentityManager', () => {
|
|
|
249
255
|
});
|
|
250
256
|
|
|
251
257
|
it('T-CIM8: logs and returned errors never contain the host key', async () => {
|
|
252
|
-
const
|
|
258
|
+
const testLog: TestLogger = createTestLogger();
|
|
253
259
|
const fetchFn = fakeFetch(async () => jsonResponse({ mode: 'co_author', ...CO_AUTHOR }));
|
|
254
260
|
const applier = fakeApplier();
|
|
255
261
|
applier.apply.mockImplementation(async () => {
|
|
@@ -259,14 +265,14 @@ describe('CommitIdentityManager', () => {
|
|
|
259
265
|
env: BASE_ENV,
|
|
260
266
|
fetch: fetchFn,
|
|
261
267
|
applier,
|
|
262
|
-
log:
|
|
268
|
+
log: testLog.logger,
|
|
263
269
|
});
|
|
264
270
|
|
|
265
271
|
const result: CommitIdentityRefreshResult = await manager.refreshNow();
|
|
266
272
|
|
|
267
273
|
expect(result.ok).toBe(false);
|
|
268
274
|
expect(JSON.stringify(result)).not.toContain(HOST_KEY);
|
|
269
|
-
expect(
|
|
270
|
-
expect(
|
|
275
|
+
expect(JSON.stringify(testLog.records)).not.toContain(HOST_KEY);
|
|
276
|
+
expect(JSON.stringify(testLog.records)).not.toContain(CO_AUTHOR.coAuthorEmail);
|
|
271
277
|
});
|
|
272
278
|
});
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { CommitIdentity, type CommitIdentityMode } from './commit-identity.js';
|
|
2
2
|
import { redactSecrets } from '../workload-executor.js';
|
|
3
|
+
import type { Logger as PinoLogger } from 'pino';
|
|
4
|
+
import { logger as rootLogger } from '../observability/logger.js';
|
|
3
5
|
|
|
4
6
|
/** 提交身份同步间隔(50 分钟)。 */
|
|
5
7
|
export const REFRESH_INTERVAL_MS = 50 * 60 * 1000;
|
|
@@ -29,7 +31,8 @@ export interface CommitIdentityManagerDeps {
|
|
|
29
31
|
env?: NodeJS.ProcessEnv;
|
|
30
32
|
fetch?: typeof fetch;
|
|
31
33
|
applier?: CommitIdentityApplier;
|
|
32
|
-
|
|
34
|
+
/** 注入 logger(测试用);缺省进程级单例,`component: 'commit-identity'`。 */
|
|
35
|
+
log?: PinoLogger;
|
|
33
36
|
/** 定时器注入(测试用);缺省使用全局 `setTimeout`。 */
|
|
34
37
|
setTimeout?: typeof setTimeout;
|
|
35
38
|
/** 退避等待注入(测试用);缺省使用 `setTimeout`。 */
|
|
@@ -71,7 +74,7 @@ export class CommitIdentityManager {
|
|
|
71
74
|
private readonly env: NodeJS.ProcessEnv;
|
|
72
75
|
private readonly fetchFn: typeof fetch;
|
|
73
76
|
private readonly applier: CommitIdentityApplier;
|
|
74
|
-
private readonly log:
|
|
77
|
+
private readonly log: PinoLogger;
|
|
75
78
|
private readonly setTimeoutFn: typeof setTimeout;
|
|
76
79
|
private readonly sleepFn: (ms: number) => Promise<void>;
|
|
77
80
|
|
|
@@ -84,7 +87,8 @@ export class CommitIdentityManager {
|
|
|
84
87
|
this.env = deps.env ?? process.env;
|
|
85
88
|
this.fetchFn = deps.fetch ?? ((...args: Parameters<typeof fetch>) => fetch(...args));
|
|
86
89
|
this.applier = deps.applier ?? new CommitIdentity();
|
|
87
|
-
|
|
90
|
+
// component 固定绑定:无论注入哪路 logger,`[CommitIdentity]` 前缀语义由 component 字段承接
|
|
91
|
+
this.log = (deps.log ?? rootLogger).child({ component: 'commit-identity' });
|
|
88
92
|
this.setTimeoutFn = deps.setTimeout ?? setTimeout;
|
|
89
93
|
this.sleepFn =
|
|
90
94
|
deps.sleep ??
|
|
@@ -106,7 +110,7 @@ export class CommitIdentityManager {
|
|
|
106
110
|
this.stopped = false;
|
|
107
111
|
const missing = this.missingEnv();
|
|
108
112
|
if (missing.length > 0) {
|
|
109
|
-
this.log(
|
|
113
|
+
this.log.warn({ event: 'disabled', missing }, 'commit identity manager disabled: missing environment variables');
|
|
110
114
|
return;
|
|
111
115
|
}
|
|
112
116
|
this.started = true;
|
|
@@ -149,9 +153,15 @@ export class CommitIdentityManager {
|
|
|
149
153
|
const result = await this.request(url, hostKey);
|
|
150
154
|
if (result.kind === 'retryable' && attempt < BACKOFF_MS.length) {
|
|
151
155
|
const delay = BACKOFF_MS[attempt]!;
|
|
152
|
-
this.log(
|
|
153
|
-
|
|
154
|
-
|
|
156
|
+
this.log.warn(
|
|
157
|
+
{
|
|
158
|
+
event: 'unauthorized_retry',
|
|
159
|
+
status: result.status,
|
|
160
|
+
delayMs: delay,
|
|
161
|
+
attempt: attempt + 1,
|
|
162
|
+
maxAttempts: BACKOFF_MS.length,
|
|
163
|
+
},
|
|
164
|
+
'提交身份下发未授权,稍后重试',
|
|
155
165
|
);
|
|
156
166
|
await this.sleepFn(delay);
|
|
157
167
|
continue;
|
|
@@ -223,7 +233,7 @@ export class CommitIdentityManager {
|
|
|
223
233
|
} catch (error) {
|
|
224
234
|
return { ok: false, error: this.applyError('移除', error, hostKey) };
|
|
225
235
|
}
|
|
226
|
-
this.log('
|
|
236
|
+
this.log.info({ event: 'removed', mode: 'unset' }, '托管配置已移除');
|
|
227
237
|
return { ok: true, mode: 'unset' };
|
|
228
238
|
}
|
|
229
239
|
|
|
@@ -253,7 +263,7 @@ export class CommitIdentityManager {
|
|
|
253
263
|
}
|
|
254
264
|
}
|
|
255
265
|
|
|
256
|
-
this.log(
|
|
266
|
+
this.log.info({ event: 'updated', mode }, '提交身份已更新');
|
|
257
267
|
return { ok: true, mode: mode as CommitIdentityMode };
|
|
258
268
|
}
|
|
259
269
|
|
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
EXPIRY_SKEW_MS,
|
|
6
6
|
BACKOFF_MS,
|
|
7
7
|
} from './credential-manager.js';
|
|
8
|
+
import { createTestLogger, type TestLogger } from '../observability/testing.js';
|
|
8
9
|
|
|
9
10
|
const TOKEN = 'ghs_credential_manager_secret';
|
|
10
11
|
const BASE_ENV: NodeJS.ProcessEnv = {
|
|
@@ -60,7 +61,7 @@ describe('GithubCredentialManager', () => {
|
|
|
60
61
|
env: BASE_ENV,
|
|
61
62
|
fetch: fetchFn,
|
|
62
63
|
writeGhAuth,
|
|
63
|
-
log: ()
|
|
64
|
+
log: createTestLogger().logger,
|
|
64
65
|
});
|
|
65
66
|
|
|
66
67
|
const result = await manager.refreshNow();
|
|
@@ -86,13 +87,13 @@ describe('GithubCredentialManager', () => {
|
|
|
86
87
|
it('T-CM2: 404 means no binding: no write, no error log, returns no-binding', async () => {
|
|
87
88
|
const fetchFn = fakeFetch(async () => new Response('', { status: 404 }));
|
|
88
89
|
const writeGhAuth = vi.fn(async () => {});
|
|
89
|
-
const
|
|
90
|
+
const testLog: TestLogger = createTestLogger();
|
|
90
91
|
|
|
91
92
|
const manager = new GithubCredentialManager({
|
|
92
93
|
env: BASE_ENV,
|
|
93
94
|
fetch: fetchFn,
|
|
94
95
|
writeGhAuth,
|
|
95
|
-
log:
|
|
96
|
+
log: testLog.logger,
|
|
96
97
|
});
|
|
97
98
|
|
|
98
99
|
const result = await manager.refreshNow();
|
|
@@ -100,20 +101,22 @@ describe('GithubCredentialManager', () => {
|
|
|
100
101
|
expect(result).toEqual({ ok: false, error: 'no-binding' });
|
|
101
102
|
expect(writeGhAuth).not.toHaveBeenCalled();
|
|
102
103
|
expect(fetchFn).toHaveBeenCalledTimes(1);
|
|
103
|
-
|
|
104
|
+
// no-binding 属正常态:无任何告警/错误日志
|
|
105
|
+
expect(testLog.records.filter((r) => r.level === 'warn' || r.level === 'error')).toEqual([]);
|
|
106
|
+
expect(JSON.stringify(testLog.records)).not.toMatch(/失败/);
|
|
104
107
|
});
|
|
105
108
|
|
|
106
109
|
it('T-CM3: 401 retries with 1/2/4 minute backoff and eventually returns a readable error', async () => {
|
|
107
110
|
const fetchFn = fakeFetch(async () => new Response('', { status: 401 }));
|
|
108
111
|
const delays: number[] = [];
|
|
109
|
-
const
|
|
112
|
+
const testLog: TestLogger = createTestLogger();
|
|
110
113
|
const writeGhAuth = vi.fn(async () => {});
|
|
111
114
|
|
|
112
115
|
const manager = new GithubCredentialManager({
|
|
113
116
|
env: BASE_ENV,
|
|
114
117
|
fetch: fetchFn,
|
|
115
118
|
writeGhAuth,
|
|
116
|
-
log:
|
|
119
|
+
log: testLog.logger,
|
|
117
120
|
sleep: async (ms) => {
|
|
118
121
|
delays.push(ms);
|
|
119
122
|
},
|
|
@@ -126,7 +129,7 @@ describe('GithubCredentialManager', () => {
|
|
|
126
129
|
expect(delays).toEqual([...BACKOFF_MS]);
|
|
127
130
|
expect(fetchFn).toHaveBeenCalledTimes(BACKOFF_MS.length + 1);
|
|
128
131
|
expect(writeGhAuth).not.toHaveBeenCalled();
|
|
129
|
-
expect(
|
|
132
|
+
expect(JSON.stringify(testLog.records)).not.toContain(TOKEN);
|
|
130
133
|
});
|
|
131
134
|
|
|
132
135
|
it('T-CM4: schedules every 50 minutes, or earlier when the token expires within the skew', async () => {
|
|
@@ -141,7 +144,7 @@ describe('GithubCredentialManager', () => {
|
|
|
141
144
|
fetch: fetchFn,
|
|
142
145
|
writeGhAuth: async () => {},
|
|
143
146
|
now: () => now,
|
|
144
|
-
log: ()
|
|
147
|
+
log: createTestLogger().logger,
|
|
145
148
|
setTimeout: scheduler.setTimeoutFn,
|
|
146
149
|
});
|
|
147
150
|
|
|
@@ -164,7 +167,7 @@ describe('GithubCredentialManager', () => {
|
|
|
164
167
|
fetch: earlyFetch,
|
|
165
168
|
writeGhAuth: async () => {},
|
|
166
169
|
now: () => now,
|
|
167
|
-
log: ()
|
|
170
|
+
log: createTestLogger().logger,
|
|
168
171
|
setTimeout: earlyScheduler.setTimeoutFn,
|
|
169
172
|
});
|
|
170
173
|
earlyManager.start();
|
|
@@ -189,7 +192,7 @@ describe('GithubCredentialManager', () => {
|
|
|
189
192
|
fetch: fetchFn,
|
|
190
193
|
writeGhAuth: async () => {},
|
|
191
194
|
now: () => now,
|
|
192
|
-
log: ()
|
|
195
|
+
log: createTestLogger().logger,
|
|
193
196
|
setTimeout: scheduler.setTimeoutFn,
|
|
194
197
|
});
|
|
195
198
|
|
|
@@ -227,7 +230,7 @@ describe('GithubCredentialManager', () => {
|
|
|
227
230
|
fetch: fetchFn,
|
|
228
231
|
writeGhAuth,
|
|
229
232
|
now: () => now,
|
|
230
|
-
log: ()
|
|
233
|
+
log: createTestLogger().logger,
|
|
231
234
|
setTimeout: scheduler.setTimeoutFn,
|
|
232
235
|
});
|
|
233
236
|
|
|
@@ -251,27 +254,28 @@ describe('GithubCredentialManager', () => {
|
|
|
251
254
|
];
|
|
252
255
|
|
|
253
256
|
for (const scenario of scenarios) {
|
|
254
|
-
const
|
|
257
|
+
const testLog: TestLogger = createTestLogger();
|
|
255
258
|
const manager = new GithubCredentialManager({
|
|
256
259
|
env: BASE_ENV,
|
|
257
260
|
fetch: fakeFetch(scenario),
|
|
258
261
|
writeGhAuth: async () => {},
|
|
259
|
-
log:
|
|
262
|
+
log: testLog.logger,
|
|
260
263
|
});
|
|
261
264
|
|
|
262
265
|
const result = await manager.refreshNow();
|
|
263
266
|
|
|
264
267
|
expect(result.ok).toBe(false);
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
+
expect(testLog.records).toContainEqual(
|
|
269
|
+
expect.objectContaining({ component: 'github-credential', msg: '凭据续期失败' }),
|
|
270
|
+
);
|
|
271
|
+
expect(JSON.stringify(testLog.records)).not.toContain(TOKEN);
|
|
268
272
|
}
|
|
269
273
|
});
|
|
270
274
|
|
|
271
275
|
it('T-CM12: 进入近过期区首次立即重试、其后按短间隔退避,并留下不含 token 的告警日志', async () => {
|
|
272
276
|
const t0 = Date.parse('2026-10-22T10:00:00.000Z');
|
|
273
277
|
let now = t0;
|
|
274
|
-
const
|
|
278
|
+
const testLog: TestLogger = createTestLogger();
|
|
275
279
|
// 服务端持续返回近过期 token(复现 R1 未修复时的缓存命中)
|
|
276
280
|
const minted = new Date(t0 + 60 * 60 * 1000).toISOString();
|
|
277
281
|
const fetchFn = fakeFetch(async () => jsonResponse({ token: TOKEN, expiresAt: minted }));
|
|
@@ -282,7 +286,7 @@ describe('GithubCredentialManager', () => {
|
|
|
282
286
|
fetch: fetchFn,
|
|
283
287
|
writeGhAuth: async () => {},
|
|
284
288
|
now: () => now,
|
|
285
|
-
log:
|
|
289
|
+
log: testLog.logger,
|
|
286
290
|
setTimeout: scheduler.setTimeoutFn,
|
|
287
291
|
});
|
|
288
292
|
|
|
@@ -301,9 +305,10 @@ describe('GithubCredentialManager', () => {
|
|
|
301
305
|
// 服务端仍返回近过期 token → 退到短间隔重试(60s),避免热循环打爆 agent-api
|
|
302
306
|
expect(scheduler.timers[2].delay).toBe(60_000);
|
|
303
307
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
308
|
+
expect(testLog.records).toContainEqual(
|
|
309
|
+
expect.objectContaining({ component: 'github-credential', msg: '凭据剩余有效期不足' }),
|
|
310
|
+
);
|
|
311
|
+
expect(JSON.stringify(testLog.records)).not.toContain(TOKEN);
|
|
307
312
|
|
|
308
313
|
manager.stop();
|
|
309
314
|
});
|
|
@@ -320,7 +325,7 @@ describe('GithubCredentialManager', () => {
|
|
|
320
325
|
env: BASE_ENV,
|
|
321
326
|
fetch: fetchFn,
|
|
322
327
|
writeGhAuth,
|
|
323
|
-
log: ()
|
|
328
|
+
log: createTestLogger().logger,
|
|
324
329
|
});
|
|
325
330
|
|
|
326
331
|
const first = manager.refreshNow();
|
|
@@ -335,20 +340,24 @@ describe('GithubCredentialManager', () => {
|
|
|
335
340
|
|
|
336
341
|
it('T-CM6: missing env disables startup with a warn that only lists variable names', async () => {
|
|
337
342
|
const fetchFn = fakeFetch(async () => jsonResponse({}));
|
|
338
|
-
const
|
|
343
|
+
const testLog: TestLogger = createTestLogger();
|
|
339
344
|
const manager = new GithubCredentialManager({
|
|
340
345
|
env: { ZAG_HOST_KEY: 'should-not-appear' },
|
|
341
346
|
fetch: fetchFn,
|
|
342
|
-
log:
|
|
347
|
+
log: testLog.logger,
|
|
343
348
|
});
|
|
344
349
|
|
|
345
350
|
manager.start();
|
|
346
351
|
|
|
347
352
|
expect(fetchFn).not.toHaveBeenCalled();
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
353
|
+
expect(testLog.records).toContainEqual(
|
|
354
|
+
expect.objectContaining({
|
|
355
|
+
component: 'github-credential',
|
|
356
|
+
msg: expect.stringContaining('disabled'),
|
|
357
|
+
missing: expect.arrayContaining(['ZAG_AGENT_API_ENDPOINT', 'ZAG_CONTROLLER_ID']),
|
|
358
|
+
}),
|
|
359
|
+
);
|
|
360
|
+
expect(JSON.stringify(testLog.records)).not.toContain('should-not-appear');
|
|
352
361
|
await expect(manager.refreshNow()).resolves.toEqual({ ok: false, error: 'disabled' });
|
|
353
362
|
});
|
|
354
363
|
|
|
@@ -360,7 +369,7 @@ describe('GithubCredentialManager', () => {
|
|
|
360
369
|
env: BASE_ENV,
|
|
361
370
|
fetch: invalidJson,
|
|
362
371
|
writeGhAuth: async () => {},
|
|
363
|
-
log: ()
|
|
372
|
+
log: createTestLogger().logger,
|
|
364
373
|
});
|
|
365
374
|
const invalid = await manager.refreshNow();
|
|
366
375
|
expect(invalid.ok).toBe(false);
|
|
@@ -373,7 +382,7 @@ describe('GithubCredentialManager', () => {
|
|
|
373
382
|
env: BASE_ENV,
|
|
374
383
|
fetch: network,
|
|
375
384
|
writeGhAuth: async () => {},
|
|
376
|
-
log: ()
|
|
385
|
+
log: createTestLogger().logger,
|
|
377
386
|
});
|
|
378
387
|
const failed = await networkManager.refreshNow();
|
|
379
388
|
expect(failed.ok).toBe(false);
|
|
@@ -381,7 +390,7 @@ describe('GithubCredentialManager', () => {
|
|
|
381
390
|
});
|
|
382
391
|
|
|
383
392
|
it('T-CM8: logs and returned errors never contain the token', async () => {
|
|
384
|
-
const
|
|
393
|
+
const testLog: TestLogger = createTestLogger();
|
|
385
394
|
const fetchFn = fakeFetch(async () => jsonResponse({ token: TOKEN, expiresAt: EXPIRES_AT }));
|
|
386
395
|
const manager = new GithubCredentialManager({
|
|
387
396
|
env: BASE_ENV,
|
|
@@ -389,13 +398,13 @@ describe('GithubCredentialManager', () => {
|
|
|
389
398
|
writeGhAuth: async () => {
|
|
390
399
|
throw new Error(`unable to write ${TOKEN}`);
|
|
391
400
|
},
|
|
392
|
-
log:
|
|
401
|
+
log: testLog.logger,
|
|
393
402
|
});
|
|
394
403
|
|
|
395
404
|
const result = await manager.refreshNow();
|
|
396
405
|
|
|
397
406
|
expect(result.ok).toBe(false);
|
|
398
|
-
expect(
|
|
407
|
+
expect(JSON.stringify(testLog.records)).not.toContain(TOKEN);
|
|
399
408
|
// writeGhAuth 的实现负责脱敏;管理器不额外拼接 token
|
|
400
409
|
expect(JSON.stringify(result)).not.toContain(TOKEN);
|
|
401
410
|
});
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { writeGhAuth } from './gh-config.js';
|
|
2
2
|
import { redactSecrets } from '../workload-executor.js';
|
|
3
|
+
import type { Logger as PinoLogger } from 'pino';
|
|
4
|
+
import { logger as rootLogger } from '../observability/logger.js';
|
|
3
5
|
|
|
4
6
|
/** 定时续期间隔(50 分钟,覆盖 Github App installation token 约 1 小时有效期)。 */
|
|
5
7
|
export const REFRESH_INTERVAL_MS = 50 * 60 * 1000;
|
|
@@ -26,7 +28,8 @@ export interface GithubCredentialManagerDeps {
|
|
|
26
28
|
fetch?: typeof fetch;
|
|
27
29
|
writeGhAuth?: (token: string) => Promise<void>;
|
|
28
30
|
now?: () => number;
|
|
29
|
-
|
|
31
|
+
/** 注入 logger(测试用);缺省进程级单例,`component: 'github-credential'`。 */
|
|
32
|
+
log?: PinoLogger;
|
|
30
33
|
/** 定时器注入(测试用);缺省使用全局 `setTimeout`。 */
|
|
31
34
|
setTimeout?: typeof setTimeout;
|
|
32
35
|
/** 退避等待注入(测试用);缺省使用 `setTimeout`。 */
|
|
@@ -60,7 +63,7 @@ export class GithubCredentialManager {
|
|
|
60
63
|
private readonly fetchFn: typeof fetch;
|
|
61
64
|
private readonly writeAuth: (token: string) => Promise<void>;
|
|
62
65
|
private readonly now: () => number;
|
|
63
|
-
private readonly log:
|
|
66
|
+
private readonly log: PinoLogger;
|
|
64
67
|
private readonly setTimeoutFn: typeof setTimeout;
|
|
65
68
|
private readonly sleepFn: (ms: number) => Promise<void>;
|
|
66
69
|
|
|
@@ -77,7 +80,8 @@ export class GithubCredentialManager {
|
|
|
77
80
|
this.fetchFn = deps.fetch ?? ((...args: Parameters<typeof fetch>) => fetch(...args));
|
|
78
81
|
this.writeAuth = deps.writeGhAuth ?? ((token: string) => writeGhAuth(token));
|
|
79
82
|
this.now = deps.now ?? (() => Date.now());
|
|
80
|
-
|
|
83
|
+
// component 固定绑定:无论注入哪路 logger,`[GithubCredential]` 前缀语义由 component 字段承接
|
|
84
|
+
this.log = (deps.log ?? rootLogger).child({ component: 'github-credential' });
|
|
81
85
|
this.setTimeoutFn = deps.setTimeout ?? setTimeout;
|
|
82
86
|
this.sleepFn =
|
|
83
87
|
deps.sleep ??
|
|
@@ -99,7 +103,7 @@ export class GithubCredentialManager {
|
|
|
99
103
|
this.stopped = false;
|
|
100
104
|
const missing = this.missingEnv();
|
|
101
105
|
if (missing.length > 0) {
|
|
102
|
-
this.log(
|
|
106
|
+
this.log.warn({ event: 'disabled', missing }, 'github credential manager disabled: missing environment variables');
|
|
103
107
|
return;
|
|
104
108
|
}
|
|
105
109
|
this.started = true;
|
|
@@ -146,9 +150,15 @@ export class GithubCredentialManager {
|
|
|
146
150
|
const result = await this.request(url, hostKey);
|
|
147
151
|
if (result.kind === 'retryable' && attempt < BACKOFF_MS.length) {
|
|
148
152
|
const delay = BACKOFF_MS[attempt]!;
|
|
149
|
-
this.log(
|
|
150
|
-
|
|
151
|
-
|
|
153
|
+
this.log.warn(
|
|
154
|
+
{
|
|
155
|
+
event: 'credential_invalid_retry',
|
|
156
|
+
status: result.status,
|
|
157
|
+
delayMs: delay,
|
|
158
|
+
attempt: attempt + 1,
|
|
159
|
+
maxAttempts: BACKOFF_MS.length,
|
|
160
|
+
},
|
|
161
|
+
'凭据无效,稍后重试',
|
|
152
162
|
);
|
|
153
163
|
await this.sleepFn(delay);
|
|
154
164
|
continue;
|
|
@@ -161,7 +171,7 @@ export class GithubCredentialManager {
|
|
|
161
171
|
// 非 401/403/404 的失败(5xx / 超时 / 非法 JSON / 写 gh 失败)必须留痕,
|
|
162
172
|
// 否则线上只能看到「凭据没更新」而无任何线索;`disabled`/`no-binding` 属正常态不告警。
|
|
163
173
|
if (!value.ok && value.error && value.error !== 'no-binding' && value.error !== 'disabled') {
|
|
164
|
-
this.log(
|
|
174
|
+
this.log.error({ event: 'renew_failed', error: value.error }, '凭据续期失败');
|
|
165
175
|
}
|
|
166
176
|
return value;
|
|
167
177
|
}
|
|
@@ -225,10 +235,9 @@ export class GithubCredentialManager {
|
|
|
225
235
|
}
|
|
226
236
|
|
|
227
237
|
this.expiresAt = expiresAt ?? null;
|
|
228
|
-
this.log(
|
|
229
|
-
expiresAt
|
|
230
|
-
|
|
231
|
-
: '[GithubCredential] gh 凭据已更新',
|
|
238
|
+
this.log.info(
|
|
239
|
+
{ event: 'credential_updated', ...(expiresAt ? { expiresAt: expiresAt.toISOString() } : {}) },
|
|
240
|
+
'gh 凭据已更新',
|
|
232
241
|
);
|
|
233
242
|
return { kind: 'result', value: { ok: true, expiresAt } };
|
|
234
243
|
} catch (error) {
|
|
@@ -253,9 +262,13 @@ export class GithubCredentialManager {
|
|
|
253
262
|
if (beforeExpiry <= 0) {
|
|
254
263
|
delay = this.nearExpiryImmediateTaken ? NEAR_EXPIRY_RETRY_MS : 0;
|
|
255
264
|
this.nearExpiryImmediateTaken = true;
|
|
256
|
-
this.log(
|
|
257
|
-
|
|
258
|
-
|
|
265
|
+
this.log.warn(
|
|
266
|
+
{
|
|
267
|
+
event: 'near_expiry',
|
|
268
|
+
expiresAt: this.expiresAt.toISOString(),
|
|
269
|
+
delayMs: delay,
|
|
270
|
+
},
|
|
271
|
+
'凭据剩余有效期不足',
|
|
259
272
|
);
|
|
260
273
|
} else {
|
|
261
274
|
this.nearExpiryImmediateTaken = false;
|