@zyaochen/weaverbird 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.en.md +418 -0
- package/README.md +418 -2
- package/apps/wb-bot/README.md +118 -0
- package/dist/apps/wb-bot/src/agent-display.js +24 -0
- package/dist/apps/wb-bot/src/agent-registry.js +188 -0
- package/dist/apps/wb-bot/src/approval-gate/audit.js +37 -0
- package/dist/apps/wb-bot/src/approval-gate/cards.js +94 -0
- package/dist/apps/wb-bot/src/approval-gate/hook-env.js +15 -0
- package/dist/apps/wb-bot/src/approval-gate/hook-files.js +311 -0
- package/dist/apps/wb-bot/src/approval-gate/hook-main.js +147 -0
- package/dist/apps/wb-bot/src/approval-gate/notifier.js +29 -0
- package/dist/apps/wb-bot/src/approval-gate/store.js +157 -0
- package/dist/apps/wb-bot/src/binding/migration-runner.js +35 -0
- package/dist/apps/wb-bot/src/binding/migrations/001-add-run-id-agent-id.js +8 -0
- package/dist/apps/wb-bot/src/binding/migrations/002-add-bot-id.js +34 -0
- package/dist/apps/wb-bot/src/binding/migrations/003-inflight-card-agent-id-pk.js +31 -0
- package/dist/apps/wb-bot/src/binding/migrations/004-thread-alias-topic-key.js +31 -0
- package/dist/apps/wb-bot/src/binding/migrations/005-card-entity.js +21 -0
- package/dist/apps/wb-bot/src/binding/migrations/006-group-setting.js +17 -0
- package/dist/apps/wb-bot/src/binding/schema.js +58 -0
- package/dist/apps/wb-bot/src/binding/sleep-sync.js +5 -0
- package/dist/apps/wb-bot/src/binding/store.js +270 -0
- package/dist/apps/wb-bot/src/binding/task-index.js +127 -0
- package/dist/apps/wb-bot/src/boot/entry-path.js +9 -0
- package/dist/apps/wb-bot/src/boot/integrity.js +47 -0
- package/dist/apps/wb-bot/src/boot/logger.js +44 -0
- package/dist/apps/wb-bot/src/captain/bot-discovery.js +42 -0
- package/dist/apps/wb-bot/src/captain/captain-context.js +26 -0
- package/dist/apps/wb-bot/src/captain/captain-gate.js +9 -0
- package/dist/apps/wb-bot/src/captain/captain-status.js +101 -0
- package/dist/apps/wb-bot/src/captain/captain-stop-all.js +72 -0
- package/dist/apps/wb-bot/src/captain/list-bots.js +39 -0
- package/dist/apps/wb-bot/src/cli/doctor.js +105 -0
- package/dist/apps/wb-bot/src/cli/init.js +122 -0
- package/dist/apps/wb-bot/src/cli/parse-args.js +31 -0
- package/dist/apps/wb-bot/src/cli/preflight.js +9 -0
- package/dist/apps/wb-bot/src/cli/setup-checklist.js +10 -0
- package/dist/apps/wb-bot/src/commands/health.js +328 -0
- package/dist/apps/wb-bot/src/commands/history.js +100 -0
- package/dist/apps/wb-bot/src/commands/logs.js +91 -0
- package/dist/apps/wb-bot/src/commands/parser.js +43 -0
- package/dist/apps/wb-bot/src/commands/queue-metrics.js +24 -0
- package/dist/apps/wb-bot/src/commands/resume.js +48 -0
- package/dist/apps/wb-bot/src/commands/router.js +299 -0
- package/dist/apps/wb-bot/src/commands/status.js +77 -0
- package/dist/apps/wb-bot/src/commands/tasks.js +106 -0
- package/dist/apps/wb-bot/src/commands/wb-approval.js +86 -0
- package/dist/apps/wb-bot/src/commands/wb-audit.js +46 -0
- package/dist/apps/wb-bot/src/commands/wb-mute.js +84 -0
- package/dist/apps/wb-bot/src/commands/wb-sync.js +58 -0
- package/dist/apps/wb-bot/src/config/approval-code-owners.js +95 -0
- package/dist/apps/wb-bot/src/config/bot-env.js +80 -0
- package/dist/apps/wb-bot/src/config/config-paths.js +16 -0
- package/dist/apps/wb-bot/src/config/env.js +43 -0
- package/dist/apps/wb-bot/src/config/lark-credentials.js +14 -0
- package/dist/apps/wb-bot/src/e2e/preflight-env.js +147 -0
- package/dist/apps/wb-bot/src/e2e/preflight-main.js +12 -0
- package/dist/apps/wb-bot/src/feishu/admin-alert.js +94 -0
- package/dist/apps/wb-bot/src/feishu/artifacts.js +95 -0
- package/dist/apps/wb-bot/src/feishu/bot-identity.js +38 -0
- package/dist/apps/wb-bot/src/feishu/card-pipeline.js +121 -0
- package/dist/apps/wb-bot/src/feishu/card-renderer.js +360 -0
- package/dist/apps/wb-bot/src/feishu/card-transport.js +294 -0
- package/dist/apps/wb-bot/src/feishu/chat-mode-cache.js +47 -0
- package/dist/apps/wb-bot/src/feishu/completion-push.js +30 -0
- package/dist/apps/wb-bot/src/feishu/doc-comment.js +183 -0
- package/dist/apps/wb-bot/src/feishu/event-handler.js +2634 -0
- package/dist/apps/wb-bot/src/feishu/events-file-reader.js +162 -0
- package/dist/apps/wb-bot/src/feishu/inbound-context.js +53 -0
- package/dist/apps/wb-bot/src/feishu/keepalive.js +85 -0
- package/dist/apps/wb-bot/src/feishu/log-redact.js +20 -0
- package/dist/apps/wb-bot/src/feishu/markdown-style.js +95 -0
- package/dist/apps/wb-bot/src/feishu/media-cache.js +92 -0
- package/dist/apps/wb-bot/src/feishu/message-parser.js +126 -0
- package/dist/apps/wb-bot/src/feishu/pending-queue.js +133 -0
- package/dist/apps/wb-bot/src/feishu/reaction.js +44 -0
- package/dist/apps/wb-bot/src/feishu/reverse-at-im-handler.js +345 -0
- package/dist/apps/wb-bot/src/feishu/run-state.js +30 -0
- package/dist/apps/wb-bot/src/feishu/session-short-id.js +9 -0
- package/dist/apps/wb-bot/src/feishu/stream-state.js +72 -0
- package/dist/apps/wb-bot/src/feishu/topic-key.js +34 -0
- package/dist/apps/wb-bot/src/feishu/transport.js +768 -0
- package/dist/apps/wb-bot/src/feishu/wb-runner-spawner.js +84 -0
- package/dist/apps/wb-bot/src/heartbeat/bot-heartbeat.js +20 -0
- package/dist/apps/wb-bot/src/main.js +144 -0
- package/dist/apps/wb-bot/src/multi/discovery.js +53 -0
- package/dist/apps/wb-bot/src/multi/main.js +57 -0
- package/dist/apps/wb-bot/src/multi/parse-args.js +17 -0
- package/dist/apps/wb-bot/src/multi/supervisor.js +288 -0
- package/dist/apps/wb-bot/src/run-observability/orchestrator.js +61 -0
- package/dist/apps/wb-bot/src/security/bot-id-validator.js +18 -0
- package/dist/apps/wb-bot/src/security/cwd-guard.js +72 -0
- package/dist/apps/wb-bot/src/security/env-whitelist.js +94 -0
- package/dist/apps/wb-bot/src/server/wire-up.js +125 -0
- package/dist/apps/wb-cli/src/commands/approval.js +101 -0
- package/dist/apps/wb-cli/src/commands/attach.js +31 -0
- package/dist/apps/wb-cli/src/commands/audit.js +71 -0
- package/dist/apps/wb-cli/src/commands/captain.js +72 -0
- package/dist/apps/wb-cli/src/commands/debug.js +22 -0
- package/dist/apps/wb-cli/src/commands/history.js +75 -0
- package/dist/apps/wb-cli/src/commands/list.js +24 -0
- package/dist/apps/wb-cli/src/commands/logs.js +122 -0
- package/dist/apps/wb-cli/src/commands/resume.js +50 -0
- package/dist/apps/wb-cli/src/commands/runs.js +75 -0
- package/dist/apps/wb-cli/src/commands/status.js +35 -0
- package/dist/apps/wb-cli/src/commands/watch.js +73 -0
- package/dist/apps/wb-cli/src/main.js +312 -0
- package/dist/packages/core/src/agent/claude-code.js +354 -0
- package/dist/packages/core/src/agent/cli-subprocess.js +244 -0
- package/dist/packages/core/src/agent/codex.js +231 -0
- package/dist/packages/core/src/agent/index.js +5 -0
- package/dist/packages/core/src/agent/registry.js +47 -0
- package/dist/packages/core/src/agent/types.js +1 -0
- package/dist/packages/core/src/approval-gate/audit.js +16 -0
- package/dist/packages/core/src/approval-gate/contracts.js +26 -0
- package/dist/packages/core/src/approval-gate/matcher.js +112 -0
- package/dist/packages/core/src/audit-pipe/audit-pipe-main.js +38 -0
- package/dist/packages/core/src/audit-pipe/bitable-sink.js +156 -0
- package/dist/packages/core/src/audit-pipe/config.js +105 -0
- package/dist/packages/core/src/audit-pipe/contracts.js +33 -0
- package/dist/packages/core/src/audit-pipe/index.js +2 -0
- package/dist/packages/core/src/audit-pipe/mirror-sink.js +21 -0
- package/dist/packages/core/src/audit-pipe/normalizer.js +129 -0
- package/dist/packages/core/src/audit-pipe/orchestrator.js +252 -0
- package/dist/packages/core/src/audit-pipe/parser.js +24 -0
- package/dist/packages/core/src/audit-pipe/tailer.js +46 -0
- package/dist/packages/core/src/audit-pipe/wal.js +71 -0
- package/dist/packages/core/src/calendar-hook/config.js +45 -0
- package/dist/packages/core/src/calendar-hook/contracts.js +66 -0
- package/dist/packages/core/src/calendar-hook/index.js +5 -0
- package/dist/packages/core/src/calendar-hook/scheduler.js +169 -0
- package/dist/packages/core/src/calendar-hook/service.js +189 -0
- package/dist/packages/core/src/calendar-hook/transport.js +66 -0
- package/dist/packages/core/src/captain/captain-service.js +160 -0
- package/dist/packages/core/src/captain/command-catalog.js +41 -0
- package/dist/packages/core/src/captain/concurrency-config.js +10 -0
- package/dist/packages/core/src/captain/concurrency-queue-index.js +94 -0
- package/dist/packages/core/src/captain/concurrency-queue-store.js +43 -0
- package/dist/packages/core/src/captain/contracts.js +102 -0
- package/dist/packages/core/src/captain/cwd-inference.js +29 -0
- package/dist/packages/core/src/captain/enqueue-or-spawn.js +63 -0
- package/dist/packages/core/src/captain/index.js +27 -0
- package/dist/packages/core/src/captain/queue-metrics/aggregator.js +98 -0
- package/dist/packages/core/src/captain/queue-metrics/contracts.js +42 -0
- package/dist/packages/core/src/captain/queue-metrics/event-store.js +26 -0
- package/dist/packages/core/src/captain/session-store.js +429 -0
- package/dist/packages/core/src/captain/toml-loader.js +86 -0
- package/dist/packages/core/src/config/errors.js +15 -0
- package/dist/packages/core/src/config/registry.js +34 -0
- package/dist/packages/core/src/errors/codes.js +98 -0
- package/dist/packages/core/src/group-sync/autodoc.js +166 -0
- package/dist/packages/core/src/group-sync/config.js +45 -0
- package/dist/packages/core/src/group-sync/contracts.js +126 -0
- package/dist/packages/core/src/group-sync/dispatch.js +163 -0
- package/dist/packages/core/src/group-sync/index.js +9 -0
- package/dist/packages/core/src/group-sync/revdiff.js +138 -0
- package/dist/packages/core/src/group-sync/scheduler.js +93 -0
- package/dist/packages/core/src/group-sync/service.js +79 -0
- package/dist/packages/core/src/group-sync/transport.js +97 -0
- package/dist/packages/core/src/group-sync/xref.js +139 -0
- package/dist/packages/core/src/index.js +23 -0
- package/dist/packages/core/src/minutes/adapter.js +187 -0
- package/dist/packages/core/src/minutes/client.js +46 -0
- package/dist/packages/core/src/minutes/contracts.js +36 -0
- package/dist/packages/core/src/minutes/index.js +3 -0
- package/dist/packages/core/src/paths.js +158 -0
- package/dist/packages/core/src/periodic-tasks/config.js +45 -0
- package/dist/packages/core/src/periodic-tasks/contracts.js +75 -0
- package/dist/packages/core/src/periodic-tasks/index.js +4 -0
- package/dist/packages/core/src/periodic-tasks/orchestrator.js +249 -0
- package/dist/packages/core/src/periodic-tasks/state.js +29 -0
- package/dist/packages/core/src/reverse-channel/attach-handler.js +108 -0
- package/dist/packages/core/src/reverse-channel/audit-encoder.js +61 -0
- package/dist/packages/core/src/reverse-channel/contracts.js +136 -0
- package/dist/packages/core/src/reverse-channel/echo-tailer.js +36 -0
- package/dist/packages/core/src/reverse-channel/handoff.js +238 -0
- package/dist/packages/core/src/reverse-channel/index.js +5 -0
- package/dist/packages/core/src/reverse-channel/ipc-client.js +110 -0
- package/dist/packages/core/src/reverse-channel/ipc-control-handler.js +27 -0
- package/dist/packages/core/src/reverse-channel/ipc-server.js +127 -0
- package/dist/packages/core/src/reverse-channel/mute-registry.js +113 -0
- package/dist/packages/core/src/reverse-channel/pid-resolver.js +41 -0
- package/dist/packages/core/src/reverse-channel/reverse-at-handler.js +19 -0
- package/dist/packages/core/src/reverse-channel/sla-tracker.js +51 -0
- package/dist/packages/core/src/run-observability/alert-card-renderer.js +24 -0
- package/dist/packages/core/src/run-observability/alert-pusher.js +47 -0
- package/dist/packages/core/src/run-observability/card-refresher.js +31 -0
- package/dist/packages/core/src/run-observability/card-renderer-progress.js +33 -0
- package/dist/packages/core/src/run-observability/config-loader.js +23 -0
- package/dist/packages/core/src/run-observability/contracts.js +107 -0
- package/dist/packages/core/src/run-observability/exit-code-detector.js +16 -0
- package/dist/packages/core/src/run-observability/finalizer.js +114 -0
- package/dist/packages/core/src/run-observability/index.js +12 -0
- package/dist/packages/core/src/run-observability/obs-toggle-handler.js +29 -0
- package/dist/packages/core/src/run-observability/progress-collector.js +53 -0
- package/dist/packages/core/src/run-observability/progress-store.js +100 -0
- package/dist/packages/core/src/run-observability/silent-timeout-detector.js +12 -0
- package/dist/packages/core/src/run-registry/bitable-writer.js +308 -0
- package/dist/packages/core/src/run-registry/config.js +77 -0
- package/dist/packages/core/src/run-registry/contracts.js +114 -0
- package/dist/packages/core/src/run-registry/doc-sink-classifier.js +0 -0
- package/dist/packages/core/src/run-registry/doc-sink-transport.js +63 -0
- package/dist/packages/core/src/run-registry/doc-sink.js +200 -0
- package/dist/packages/core/src/run-registry/extractor.js +40 -0
- package/dist/packages/core/src/run-registry/index.js +11 -0
- package/dist/packages/core/src/run-registry/inherit.js +11 -0
- package/dist/packages/core/src/run-registry/orchestrator.js +86 -0
- package/dist/packages/core/src/run-registry/run-registry-main.js +38 -0
- package/dist/packages/core/src/run-registry/tailer.js +390 -0
- package/dist/packages/core/src/run-registry/wal.js +106 -0
- package/dist/packages/core/src/runner/event-replay.js +65 -0
- package/dist/packages/core/src/runner/wb-runner-main.js +27 -0
- package/dist/packages/core/src/runner/wb-runner.js +237 -0
- package/dist/packages/core/src/util/entry-module.js +21 -0
- package/dist/packages/core/src/util/ndjson-rotation.js +99 -0
- package/package.json +76 -4
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { existsSync, statSync } from 'node:fs';
|
|
2
|
+
import net, {} from 'node:net';
|
|
3
|
+
import { resolve } from 'node:path';
|
|
4
|
+
import { ATTACH_TIMEOUT_MS, IPC_SOCKET_PATH, IpcMessageSchema, REVERSE_CHANNEL_ERROR_CODES, ReverseChannelError } from './contracts.js';
|
|
5
|
+
function resolveSocketPath(socketPath) {
|
|
6
|
+
return socketPath.startsWith('~/') ? resolve(process.env['HOME'] ?? '', socketPath.slice(2)) : resolve(socketPath);
|
|
7
|
+
}
|
|
8
|
+
class SessionConnection {
|
|
9
|
+
socket;
|
|
10
|
+
buffer = '';
|
|
11
|
+
listeners = new Set();
|
|
12
|
+
constructor(socket) {
|
|
13
|
+
this.socket = socket;
|
|
14
|
+
socket.setEncoding('utf8');
|
|
15
|
+
socket.on('data', (chunk) => {
|
|
16
|
+
this.buffer += chunk;
|
|
17
|
+
const lines = this.buffer.split('\n');
|
|
18
|
+
this.buffer = lines.pop() ?? '';
|
|
19
|
+
for (const line of lines) {
|
|
20
|
+
if (!line.trim())
|
|
21
|
+
continue;
|
|
22
|
+
const message = IpcMessageSchema.parse(JSON.parse(line));
|
|
23
|
+
for (const listener of this.listeners)
|
|
24
|
+
listener(message);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
async send(message, options = {}) {
|
|
29
|
+
return new Promise((resolvePromise, reject) => {
|
|
30
|
+
const timeout = setTimeout(() => reject(new ReverseChannelError(REVERSE_CHANNEL_ERROR_CODES.E_IPC_SEND_TIMEOUT, 'IPC send timed out')), options.timeoutMs ?? ATTACH_TIMEOUT_MS);
|
|
31
|
+
const handler = (incoming) => {
|
|
32
|
+
if (message.kind === 'ping' && incoming.kind === 'pong') {
|
|
33
|
+
clearTimeout(timeout);
|
|
34
|
+
this.listeners.delete(handler);
|
|
35
|
+
resolvePromise(incoming);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (incoming.kind === 'ack' && incoming.run_id === ('run_id' in message ? message.run_id : incoming.run_id)) {
|
|
39
|
+
clearTimeout(timeout);
|
|
40
|
+
this.listeners.delete(handler);
|
|
41
|
+
resolvePromise(incoming);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
this.listeners.add(handler);
|
|
45
|
+
this.socket.write(`${JSON.stringify(message)}\n`);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
on(event, listener) {
|
|
49
|
+
if (event === 'push')
|
|
50
|
+
this.listeners.add(listener);
|
|
51
|
+
}
|
|
52
|
+
async close() {
|
|
53
|
+
await new Promise((resolvePromise) => {
|
|
54
|
+
this.socket.end(() => resolvePromise());
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
export class IpcClient {
|
|
59
|
+
socketPath;
|
|
60
|
+
constructor(socketPath = IPC_SOCKET_PATH) {
|
|
61
|
+
this.socketPath = resolveSocketPath(socketPath);
|
|
62
|
+
}
|
|
63
|
+
async requestOnce(message, options = {}) {
|
|
64
|
+
const connection = await this.openSession();
|
|
65
|
+
try {
|
|
66
|
+
return await connection.send(message, options);
|
|
67
|
+
}
|
|
68
|
+
finally {
|
|
69
|
+
await connection.close();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async openSession(options = {}) {
|
|
73
|
+
if (!existsSync(this.socketPath)) {
|
|
74
|
+
throw new ReverseChannelError(REVERSE_CHANNEL_ERROR_CODES.E_IPC_CONNECT_FAILED, 'IPC_SOCKET_NOT_FOUND');
|
|
75
|
+
}
|
|
76
|
+
statSync(this.socketPath);
|
|
77
|
+
const socket = await this.connect();
|
|
78
|
+
const connection = new SessionConnection(socket);
|
|
79
|
+
if (options.runId !== undefined) {
|
|
80
|
+
const ack = await connection.send({
|
|
81
|
+
kind: 'hello',
|
|
82
|
+
run_id: options.runId,
|
|
83
|
+
pid: options.pid ?? process.pid,
|
|
84
|
+
});
|
|
85
|
+
if (ack.kind !== 'ack' || !ack.ok) {
|
|
86
|
+
await connection.close();
|
|
87
|
+
throw new ReverseChannelError(REVERSE_CHANNEL_ERROR_CODES.E_IPC_CONNECT_FAILED, `hello ack failed: ${ack.kind === 'ack' ? (ack.reason ?? 'unknown') : 'non-ack response'}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return connection;
|
|
91
|
+
}
|
|
92
|
+
async connect() {
|
|
93
|
+
const socket = new net.Socket();
|
|
94
|
+
await new Promise((resolvePromise, reject) => {
|
|
95
|
+
const timeout = setTimeout(() => {
|
|
96
|
+
socket.destroy();
|
|
97
|
+
reject(new ReverseChannelError(REVERSE_CHANNEL_ERROR_CODES.E_IPC_CONNECT_FAILED, 'IPC hello/connect timeout'));
|
|
98
|
+
}, ATTACH_TIMEOUT_MS);
|
|
99
|
+
socket.once('error', (error) => {
|
|
100
|
+
clearTimeout(timeout);
|
|
101
|
+
reject(new ReverseChannelError(REVERSE_CHANNEL_ERROR_CODES.E_IPC_CONNECT_FAILED, error instanceof Error ? error.message : String(error), error));
|
|
102
|
+
});
|
|
103
|
+
socket.connect(this.socketPath, () => {
|
|
104
|
+
clearTimeout(timeout);
|
|
105
|
+
resolvePromise();
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
return socket;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { REVERSE_CHANNEL_ERROR_CODES, ReverseChannelError } from './contracts.js';
|
|
2
|
+
import { encodeAuditResource } from './audit-encoder.js';
|
|
3
|
+
const SIGNAL_BY_COMMAND = {
|
|
4
|
+
pause: 'SIGSTOP',
|
|
5
|
+
resume: 'SIGCONT',
|
|
6
|
+
cancel: 'SIGTERM',
|
|
7
|
+
};
|
|
8
|
+
export async function sendControlSignal(command, pid) {
|
|
9
|
+
const signal = SIGNAL_BY_COMMAND[command];
|
|
10
|
+
try {
|
|
11
|
+
process.kill(pid, signal);
|
|
12
|
+
return { signal };
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
throw new ReverseChannelError(REVERSE_CHANNEL_ERROR_CODES.E_SIGNAL_FAILED, `failed to send ${signal} to pid ${pid}: ${error instanceof Error ? error.message : String(error)}`, error);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export function reverseAtAuditResource(input) {
|
|
19
|
+
return encodeAuditResource({
|
|
20
|
+
verb: input.command,
|
|
21
|
+
targetId: input.runId,
|
|
22
|
+
senderOpenId: input.senderOpenId,
|
|
23
|
+
signal: SIGNAL_BY_COMMAND[input.command],
|
|
24
|
+
result: input.result,
|
|
25
|
+
reasonCode: input.reasonCode,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { chmodSync, existsSync, rmSync, statSync } from 'node:fs';
|
|
2
|
+
import { dirname, resolve } from 'node:path';
|
|
3
|
+
import { mkdirSync } from 'node:fs';
|
|
4
|
+
import net, {} from 'node:net';
|
|
5
|
+
import { EventEmitter } from 'node:events';
|
|
6
|
+
import { IPC_SOCKET_PATH, IpcMessageSchema, REVERSE_CHANNEL_ERROR_CODES, ReverseChannelError } from './contracts.js';
|
|
7
|
+
function resolveSocketPath(socketPath) {
|
|
8
|
+
return socketPath.startsWith('~/') ? resolve(process.env['HOME'] ?? '', socketPath.slice(2)) : resolve(socketPath);
|
|
9
|
+
}
|
|
10
|
+
export class IpcServer extends EventEmitter {
|
|
11
|
+
socketPath;
|
|
12
|
+
server = null;
|
|
13
|
+
connections = new Map();
|
|
14
|
+
constructor(socketPath = IPC_SOCKET_PATH) {
|
|
15
|
+
super();
|
|
16
|
+
this.socketPath = resolveSocketPath(socketPath);
|
|
17
|
+
}
|
|
18
|
+
on(event, listener) {
|
|
19
|
+
return super.on(event, listener);
|
|
20
|
+
}
|
|
21
|
+
async start() {
|
|
22
|
+
mkdirSync(dirname(this.socketPath), { recursive: true });
|
|
23
|
+
this.resetStaleSocket();
|
|
24
|
+
this.server = net.createServer((socket) => this.handleSocket(socket));
|
|
25
|
+
await new Promise((resolvePromise, reject) => {
|
|
26
|
+
this.server.once('error', reject);
|
|
27
|
+
this.server.listen(this.socketPath, () => {
|
|
28
|
+
this.server.off('error', reject);
|
|
29
|
+
chmodSync(this.socketPath, 0o600);
|
|
30
|
+
resolvePromise();
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
async stop() {
|
|
35
|
+
for (const connection of this.connections.values()) {
|
|
36
|
+
connection.socket.destroy();
|
|
37
|
+
}
|
|
38
|
+
this.connections.clear();
|
|
39
|
+
if (!this.server)
|
|
40
|
+
return;
|
|
41
|
+
await new Promise((resolvePromise) => {
|
|
42
|
+
this.server.close(() => resolvePromise());
|
|
43
|
+
});
|
|
44
|
+
this.server = null;
|
|
45
|
+
if (existsSync(this.socketPath))
|
|
46
|
+
rmSync(this.socketPath, { force: true });
|
|
47
|
+
}
|
|
48
|
+
async close() {
|
|
49
|
+
await this.stop();
|
|
50
|
+
}
|
|
51
|
+
send(runId, message) {
|
|
52
|
+
const connection = this.connections.get(runId);
|
|
53
|
+
if (!connection)
|
|
54
|
+
return false;
|
|
55
|
+
connection.socket.write(`${JSON.stringify(message)}\n`);
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
resetStaleSocket() {
|
|
59
|
+
if (!existsSync(this.socketPath))
|
|
60
|
+
return;
|
|
61
|
+
const mode = statSync(this.socketPath).mode & 0o777;
|
|
62
|
+
if (mode !== 0o600) {
|
|
63
|
+
console.warn(`[reverse-channel] socket mode ${mode.toString(8)} != 600, recreating`);
|
|
64
|
+
}
|
|
65
|
+
rmSync(this.socketPath, { force: true });
|
|
66
|
+
}
|
|
67
|
+
handleSocket(socket) {
|
|
68
|
+
const record = { socket, pid: 0, runId: '', buffer: '' };
|
|
69
|
+
socket.setEncoding('utf8');
|
|
70
|
+
socket.on('data', (chunk) => {
|
|
71
|
+
record.buffer += chunk;
|
|
72
|
+
const lines = record.buffer.split('\n');
|
|
73
|
+
record.buffer = lines.pop() ?? '';
|
|
74
|
+
for (const line of lines) {
|
|
75
|
+
if (!line.trim())
|
|
76
|
+
continue;
|
|
77
|
+
this.handleLine(record, line);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
socket.on('close', () => {
|
|
81
|
+
if (record.runId)
|
|
82
|
+
this.connections.delete(record.runId);
|
|
83
|
+
});
|
|
84
|
+
socket.on('error', () => {
|
|
85
|
+
if (record.runId)
|
|
86
|
+
this.connections.delete(record.runId);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
handleLine(connection, line) {
|
|
90
|
+
let parsed;
|
|
91
|
+
try {
|
|
92
|
+
parsed = IpcMessageSchema.parse(JSON.parse(line));
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
connection.socket.write(`${JSON.stringify({ kind: 'ack', command: 'parse', run_id: connection.runId || 'unknown', ok: false, reason: REVERSE_CHANNEL_ERROR_CODES.E_IPC_PARSE })}\n`, () => connection.socket.end());
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (parsed.kind === 'hello') {
|
|
99
|
+
connection.pid = parsed.pid;
|
|
100
|
+
connection.runId = parsed.run_id;
|
|
101
|
+
this.connections.set(parsed.run_id, connection);
|
|
102
|
+
this.emit('hello', parsed);
|
|
103
|
+
connection.socket.write(`${JSON.stringify({ kind: 'ack', command: 'hello', run_id: parsed.run_id, ok: true, reason: 'hello-ack' })}\n`);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (parsed.kind === 'ping') {
|
|
107
|
+
connection.socket.write(`${JSON.stringify({ kind: 'pong' })}\n`);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (parsed.kind === 'control') {
|
|
111
|
+
this.emit('control', parsed);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
if (parsed.kind === 'attach') {
|
|
115
|
+
this.emit('attach', parsed);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (parsed.kind === 'bye') {
|
|
119
|
+
this.emit('bye', parsed);
|
|
120
|
+
if (parsed.run_id)
|
|
121
|
+
this.connections.delete(parsed.run_id);
|
|
122
|
+
connection.socket.write(`${JSON.stringify({ kind: 'ack', command: 'bye', run_id: parsed.run_id, ok: true })}\n`, () => {
|
|
123
|
+
connection.socket.end();
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { dirname } from 'node:path';
|
|
3
|
+
const DEFAULT_FILE_VERSION = 1;
|
|
4
|
+
function isExpired(entry, nowIso) {
|
|
5
|
+
return entry.expiresAtIso <= nowIso;
|
|
6
|
+
}
|
|
7
|
+
function parseDuration(duration, fromIso) {
|
|
8
|
+
const match = duration.trim().match(/^(\d+)(s|m|h|d)$/i);
|
|
9
|
+
if (!match)
|
|
10
|
+
return null;
|
|
11
|
+
const value = Number.parseInt(match[1], 10);
|
|
12
|
+
if (!Number.isFinite(value) || value <= 0)
|
|
13
|
+
return null;
|
|
14
|
+
const unit = match[2].toLowerCase();
|
|
15
|
+
const factorMs = unit === 's' ? 1_000 :
|
|
16
|
+
unit === 'm' ? 60_000 :
|
|
17
|
+
unit === 'h' ? 3_600_000 :
|
|
18
|
+
unit === 'd' ? 86_400_000 : 0;
|
|
19
|
+
if (factorMs === 0)
|
|
20
|
+
return null;
|
|
21
|
+
const fromMs = Date.parse(fromIso);
|
|
22
|
+
if (Number.isNaN(fromMs))
|
|
23
|
+
return null;
|
|
24
|
+
return new Date(fromMs + value * factorMs).toISOString();
|
|
25
|
+
}
|
|
26
|
+
function readFileSafe(path) {
|
|
27
|
+
if (!existsSync(path))
|
|
28
|
+
return { version: DEFAULT_FILE_VERSION, entries: [] };
|
|
29
|
+
try {
|
|
30
|
+
const raw = JSON.parse(readFileSync(path, 'utf8'));
|
|
31
|
+
if (!raw || typeof raw !== 'object')
|
|
32
|
+
return { version: DEFAULT_FILE_VERSION, entries: [] };
|
|
33
|
+
const entries = Array.isArray(raw.entries)
|
|
34
|
+
? raw.entries.filter((e) => !!e &&
|
|
35
|
+
typeof e === 'object' &&
|
|
36
|
+
typeof e.sessionId === 'string' &&
|
|
37
|
+
typeof e.expiresAtIso === 'string' &&
|
|
38
|
+
typeof e.createdAtIso === 'string')
|
|
39
|
+
: [];
|
|
40
|
+
return { version: DEFAULT_FILE_VERSION, entries };
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return { version: DEFAULT_FILE_VERSION, entries: [] };
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export class MuteRegistry {
|
|
47
|
+
filePath;
|
|
48
|
+
state;
|
|
49
|
+
constructor(filePath) {
|
|
50
|
+
this.filePath = filePath;
|
|
51
|
+
this.state = readFileSafe(filePath);
|
|
52
|
+
}
|
|
53
|
+
isMuted(query) {
|
|
54
|
+
const nowIso = query.nowIso ?? new Date().toISOString();
|
|
55
|
+
const entry = this.state.entries.find((e) => e.sessionId === query.sessionId && !isExpired(e, nowIso));
|
|
56
|
+
if (!entry)
|
|
57
|
+
return { sessionId: query.sessionId, muted: false };
|
|
58
|
+
return {
|
|
59
|
+
sessionId: query.sessionId,
|
|
60
|
+
muted: true,
|
|
61
|
+
expiresAtIso: entry.expiresAtIso,
|
|
62
|
+
reason: entry.reason,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
mute(sessionId, duration, opts = {}) {
|
|
66
|
+
const nowIso = opts.nowIso ?? new Date().toISOString();
|
|
67
|
+
const expiresAtIso = parseDuration(duration, nowIso);
|
|
68
|
+
if (!expiresAtIso) {
|
|
69
|
+
return { ok: false, error: `invalid duration: ${duration} (expect 30s / 15m / 2h / 7d)` };
|
|
70
|
+
}
|
|
71
|
+
const next = {
|
|
72
|
+
sessionId,
|
|
73
|
+
expiresAtIso,
|
|
74
|
+
createdAtIso: nowIso,
|
|
75
|
+
...(opts.reason ? { reason: opts.reason } : {}),
|
|
76
|
+
};
|
|
77
|
+
this.state = {
|
|
78
|
+
version: DEFAULT_FILE_VERSION,
|
|
79
|
+
entries: [...this.state.entries.filter((e) => e.sessionId !== sessionId), next],
|
|
80
|
+
};
|
|
81
|
+
this.persist();
|
|
82
|
+
return { ok: true, expiresAtIso };
|
|
83
|
+
}
|
|
84
|
+
unmute(sessionId) {
|
|
85
|
+
const before = this.state.entries.length;
|
|
86
|
+
this.state = {
|
|
87
|
+
version: DEFAULT_FILE_VERSION,
|
|
88
|
+
entries: this.state.entries.filter((e) => e.sessionId !== sessionId),
|
|
89
|
+
};
|
|
90
|
+
const removed = before - this.state.entries.length;
|
|
91
|
+
if (removed > 0)
|
|
92
|
+
this.persist();
|
|
93
|
+
return { ok: removed > 0, removed };
|
|
94
|
+
}
|
|
95
|
+
list(opts = {}) {
|
|
96
|
+
const nowIso = opts.nowIso ?? new Date().toISOString();
|
|
97
|
+
if (opts.includeExpired)
|
|
98
|
+
return [...this.state.entries];
|
|
99
|
+
return this.state.entries.filter((e) => !isExpired(e, nowIso));
|
|
100
|
+
}
|
|
101
|
+
persist() {
|
|
102
|
+
const dir = dirname(this.filePath);
|
|
103
|
+
if (!existsSync(dir))
|
|
104
|
+
mkdirSync(dir, { recursive: true });
|
|
105
|
+
const tmp = `${this.filePath}.tmp`;
|
|
106
|
+
writeFileSync(tmp, JSON.stringify(this.state, null, 2), 'utf8');
|
|
107
|
+
renameSync(tmp, this.filePath);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
export function defaultMuteRegistryPath(dataDir) {
|
|
111
|
+
return `${dataDir}/mute-registry.json`;
|
|
112
|
+
}
|
|
113
|
+
export { parseDuration };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { runPidPath, runsRootDir } from '../paths.js';
|
|
4
|
+
/**
|
|
5
|
+
* Resolve cli subprocess pid from wb-runner's pid file convention.
|
|
6
|
+
* Layout: <dataDir>/runs/<botId>/<runId>/pid
|
|
7
|
+
*/
|
|
8
|
+
export function resolveCliPid(dataDir, runId) {
|
|
9
|
+
const runsDir = runsRootDir(dataDir);
|
|
10
|
+
if (!existsSync(runsDir))
|
|
11
|
+
return null;
|
|
12
|
+
let botIds;
|
|
13
|
+
try {
|
|
14
|
+
botIds = readdirSync(runsDir).filter((name) => {
|
|
15
|
+
try {
|
|
16
|
+
return statSync(join(runsDir, name)).isDirectory();
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
for (const botId of botIds) {
|
|
27
|
+
const pidPath = runPidPath(dataDir, runId, botId);
|
|
28
|
+
if (!existsSync(pidPath))
|
|
29
|
+
continue;
|
|
30
|
+
try {
|
|
31
|
+
const raw = readFileSync(pidPath, 'utf8').trim();
|
|
32
|
+
const pid = Number.parseInt(raw, 10);
|
|
33
|
+
if (Number.isFinite(pid) && pid > 0)
|
|
34
|
+
return pid;
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
// try next botId directory
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { REVERSE_AT_PATTERN, REVERSE_CHANNEL_ERROR_CODES, ReverseChannelError } from './contracts.js';
|
|
2
|
+
export function parseReverseAt(text, requesterOpenId, messageId, requestedAt = new Date().toISOString()) {
|
|
3
|
+
const match = text.match(REVERSE_AT_PATTERN);
|
|
4
|
+
if (!match)
|
|
5
|
+
return null;
|
|
6
|
+
return {
|
|
7
|
+
command: match[1].toLowerCase(),
|
|
8
|
+
run_id: match[2],
|
|
9
|
+
requester_open_id: requesterOpenId,
|
|
10
|
+
requested_at: requestedAt,
|
|
11
|
+
message_id: messageId,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export function validateReverseAtRun(captainService, runId) {
|
|
15
|
+
const exists = captainService.listActiveSessions().some((entry) => entry.run_ids.includes(runId));
|
|
16
|
+
if (!exists) {
|
|
17
|
+
throw new ReverseChannelError(REVERSE_CHANNEL_ERROR_CODES.E_REVERSE_AT_RUN_GONE, `run ${runId} not found`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { closeSync, fsyncSync, mkdirSync, openSync, writeSync } from 'node:fs';
|
|
2
|
+
import { dirname } from 'node:path';
|
|
3
|
+
function p95(values) {
|
|
4
|
+
if (values.length === 0)
|
|
5
|
+
return 0;
|
|
6
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
7
|
+
const index = Math.max(0, Math.ceil(sorted.length * 0.95) - 1);
|
|
8
|
+
return sorted[index] ?? 0;
|
|
9
|
+
}
|
|
10
|
+
function atomicAppend(path, content) {
|
|
11
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
12
|
+
const fd = openSync(path, 'a');
|
|
13
|
+
try {
|
|
14
|
+
writeSync(fd, content);
|
|
15
|
+
fsyncSync(fd);
|
|
16
|
+
}
|
|
17
|
+
finally {
|
|
18
|
+
closeSync(fd);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export class SlaTracker {
|
|
22
|
+
path;
|
|
23
|
+
constructor(path) {
|
|
24
|
+
this.path = path;
|
|
25
|
+
}
|
|
26
|
+
record(kind, elapsedMs, targetMs) {
|
|
27
|
+
const entry = {
|
|
28
|
+
kind,
|
|
29
|
+
elapsed_ms: elapsedMs,
|
|
30
|
+
sla_ok: elapsedMs <= targetMs,
|
|
31
|
+
ts: new Date().toISOString(),
|
|
32
|
+
};
|
|
33
|
+
const line = `${JSON.stringify(entry)}\n`;
|
|
34
|
+
if (line.length > 1024) {
|
|
35
|
+
throw new Error('SLA metric line too long');
|
|
36
|
+
}
|
|
37
|
+
atomicAppend(this.path, line);
|
|
38
|
+
return entry;
|
|
39
|
+
}
|
|
40
|
+
summarize(entries) {
|
|
41
|
+
const attach = entries.filter((entry) => entry.kind === 'attach').map((entry) => entry.elapsed_ms);
|
|
42
|
+
const echo = entries.filter((entry) => entry.kind === 'echo').map((entry) => entry.elapsed_ms);
|
|
43
|
+
const reverse = entries.filter((entry) => entry.kind === 'reverse_at');
|
|
44
|
+
const reverseOk = reverse.filter((entry) => entry.sla_ok).length;
|
|
45
|
+
return {
|
|
46
|
+
attach_p95_ms: p95(attach),
|
|
47
|
+
echo_p95_ms: p95(echo),
|
|
48
|
+
reverse_at_hit_rate: reverse.length > 0 ? reverseOk / reverse.length : 0,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export function renderAtUser(openId) {
|
|
2
|
+
if (!openId.startsWith('ou_'))
|
|
3
|
+
return openId;
|
|
4
|
+
return `<at id="${openId}"></at>`;
|
|
5
|
+
}
|
|
6
|
+
export function renderAlertCard(payload, operatorOpenId) {
|
|
7
|
+
const mention = renderAtUser(operatorOpenId);
|
|
8
|
+
const lines = [
|
|
9
|
+
`${mention} run ${payload.run_id} alert`,
|
|
10
|
+
`reason: ${payload.reason}`,
|
|
11
|
+
`elapsed_s: ${payload.elapsed_s}`,
|
|
12
|
+
payload.exit_code !== null ? `exit_code: ${payload.exit_code}` : null,
|
|
13
|
+
payload.signal !== null ? `signal: ${payload.signal}` : null,
|
|
14
|
+
'suggest: cancel / check logs',
|
|
15
|
+
].filter(Boolean);
|
|
16
|
+
return {
|
|
17
|
+
schema: '2.0',
|
|
18
|
+
config: { wide_screen_mode: true },
|
|
19
|
+
header: { title: { tag: 'plain_text', content: `⚠️ Run Alert · ${payload.run_id}` } },
|
|
20
|
+
body: {
|
|
21
|
+
elements: lines.map((line) => ({ tag: 'markdown', content: String(line) })),
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { createMirrorSink } from '../audit-pipe/mirror-sink.js';
|
|
3
|
+
import { RUN_OBSERVABILITY_ERROR_CODES } from './contracts.js';
|
|
4
|
+
import { renderAlertCard } from './alert-card-renderer.js';
|
|
5
|
+
export async function pushAlert(payload, options) {
|
|
6
|
+
const sink = createMirrorSink({ dataDir: options.dataDir });
|
|
7
|
+
const writeAudit = (resource, skipped = false) => {
|
|
8
|
+
const record = {
|
|
9
|
+
audit_record_id: randomUUID(),
|
|
10
|
+
session_id: options.session.session_id,
|
|
11
|
+
run_id: payload.run_id,
|
|
12
|
+
revision: 0,
|
|
13
|
+
actor: options.session.operator,
|
|
14
|
+
action: 'network_call',
|
|
15
|
+
resource,
|
|
16
|
+
ts: new Date().toISOString(),
|
|
17
|
+
entry_form: 'im',
|
|
18
|
+
entry_detail: {
|
|
19
|
+
source_message_id: payload.run_id,
|
|
20
|
+
user_open_id: options.session.operator,
|
|
21
|
+
thread_id: options.session.home_group_id ?? '',
|
|
22
|
+
},
|
|
23
|
+
agent_id: 'claude-code',
|
|
24
|
+
raw_tool_use_id: `${payload.run_id}:${payload.reason}:${skipped ? 'skipped' : 'send'}`,
|
|
25
|
+
raw_tool_name: 'alert-pusher',
|
|
26
|
+
};
|
|
27
|
+
sink.write(record);
|
|
28
|
+
};
|
|
29
|
+
if (options.alreadyAlerted)
|
|
30
|
+
return;
|
|
31
|
+
if (!options.alertEnabled) {
|
|
32
|
+
writeAudit(`run-observability:alert:${payload.run_id}:${payload.reason}:skipped`, true);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (!options.session.home_group_id)
|
|
36
|
+
return;
|
|
37
|
+
const card = renderAlertCard(payload, options.session.operator);
|
|
38
|
+
try {
|
|
39
|
+
await options.client.sendMessage(options.session.home_group_id, card);
|
|
40
|
+
options.onAlerted?.();
|
|
41
|
+
writeAudit(`run-observability:alert:${payload.run_id}:${payload.reason}`);
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
writeAudit(`run-observability:alert:${payload.run_id}:${payload.reason}:failed`);
|
|
45
|
+
throw new Error(RUN_OBSERVABILITY_ERROR_CODES.E_IM_SEND_FAILED);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { CardPipeline } from '../../../../apps/wb-bot/src/feishu/card-pipeline.js';
|
|
2
|
+
import { RUN_OBSERVABILITY_ERROR_CODES } from './contracts.js';
|
|
3
|
+
import { renderProgressCard } from './card-renderer-progress.js';
|
|
4
|
+
export class CardRefresher {
|
|
5
|
+
pipeline;
|
|
6
|
+
markPatchFailed;
|
|
7
|
+
nowFn;
|
|
8
|
+
constructor(pipeline, markPatchFailed, nowFn = () => new Date().toISOString()) {
|
|
9
|
+
this.pipeline = pipeline;
|
|
10
|
+
this.markPatchFailed = markPatchFailed;
|
|
11
|
+
this.nowFn = nowFn;
|
|
12
|
+
}
|
|
13
|
+
async refresh(entry, latestLine) {
|
|
14
|
+
const card = renderProgressCard(entry, latestLine, this.nowFn());
|
|
15
|
+
try {
|
|
16
|
+
await this.pipeline.flush(card);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
await this.markPatchFailed(entry, RUN_OBSERVABILITY_ERROR_CODES.E_IM_PATCH_FAILED);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
async finalize(entry, latestLine) {
|
|
23
|
+
const card = renderProgressCard(entry, latestLine, this.nowFn());
|
|
24
|
+
try {
|
|
25
|
+
await this.pipeline.force(card);
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
await this.markPatchFailed(entry, RUN_OBSERVABILITY_ERROR_CODES.E_IM_PATCH_FAILED);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { CARD_LINE_MAX_CHARS } from './contracts.js';
|
|
2
|
+
function truncateLine(text, maxChars = CARD_LINE_MAX_CHARS) {
|
|
3
|
+
return text.length <= maxChars ? text : `${text.slice(0, maxChars)}…`;
|
|
4
|
+
}
|
|
5
|
+
function formatElapsed(createdAt, nowIso) {
|
|
6
|
+
const elapsedMs = Math.max(0, Date.parse(nowIso) - Date.parse(createdAt));
|
|
7
|
+
const seconds = Math.floor(elapsedMs / 1000);
|
|
8
|
+
const mins = Math.floor(seconds / 60);
|
|
9
|
+
const secs = seconds % 60;
|
|
10
|
+
return `${String(mins).padStart(2, '0')}:${String(secs).padStart(2, '0')}`;
|
|
11
|
+
}
|
|
12
|
+
export function renderProgressCard(entry, latestLine, nowIso = new Date().toISOString()) {
|
|
13
|
+
const text = latestLine ? truncateLine(latestLine) : '(no output yet)';
|
|
14
|
+
return {
|
|
15
|
+
schema: '2.0',
|
|
16
|
+
config: { wide_screen_mode: true },
|
|
17
|
+
header: {
|
|
18
|
+
title: {
|
|
19
|
+
tag: 'plain_text',
|
|
20
|
+
content: `📡 Run Progress · ${entry.run_id}`,
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
body: {
|
|
24
|
+
elements: [
|
|
25
|
+
{ tag: 'markdown', content: `status: ${entry.status}` },
|
|
26
|
+
{ tag: 'markdown', content: `step_index: ${entry.step_index}` },
|
|
27
|
+
{ tag: 'markdown', content: `token_usage: ${entry.token_usage}` },
|
|
28
|
+
{ tag: 'markdown', content: `elapsed: ${formatElapsed(entry.created_at, nowIso)}` },
|
|
29
|
+
{ tag: 'markdown', content: text },
|
|
30
|
+
],
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { CODES } from '../config/errors.js';
|
|
3
|
+
import { loadConfig } from '../config/registry.js';
|
|
4
|
+
import { configFile } from '../paths.js';
|
|
5
|
+
import { ObservabilityConfigSchema } from './contracts.js';
|
|
6
|
+
export function loadObservabilityConfig(dataDir) {
|
|
7
|
+
const defaults = ObservabilityConfigSchema.parse({});
|
|
8
|
+
const path = configFile(dataDir, 'run-observability');
|
|
9
|
+
const missing = !existsSync(path);
|
|
10
|
+
try {
|
|
11
|
+
const config = loadConfig(dataDir, 'run-observability', ObservabilityConfigSchema, defaults);
|
|
12
|
+
return {
|
|
13
|
+
config,
|
|
14
|
+
errorCode: missing ? CODES.E_CONFIG_PARSE : null,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return {
|
|
19
|
+
config: defaults,
|
|
20
|
+
errorCode: CODES.E_CONFIG_PARSE,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
}
|