-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjob-comms.ts
More file actions
143 lines (116 loc) · 3.66 KB
/
job-comms.ts
File metadata and controls
143 lines (116 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import type { JobSpec } from './plan-types';
import { sendPrompt, waitForServer } from './sdk-client';
export interface RelayContext {
finding: string;
filePath?: string;
lineNumber?: number;
severity?: 'info' | 'warning' | 'error';
}
export interface RelayMessage {
from: string;
to: string;
context: RelayContext;
timestamp: string;
}
export class JobComms {
private messageBus: Map<string, RelayMessage[]> = new Map();
private relayPatterns: Map<string, Bun.Glob[]> = new Map();
private relayPatternSources: Map<string, string[]> = new Map();
registerJob(job: JobSpec): void {
if (job.relayPatterns && job.relayPatterns.length > 0) {
const patterns: Bun.Glob[] = [];
const sources: string[] = [];
for (const pattern of job.relayPatterns) {
const normalized = pattern.endsWith('/') ? `${pattern}**` : pattern;
patterns.push(new Bun.Glob(normalized));
sources.push(pattern);
}
this.relayPatterns.set(job.name, patterns);
this.relayPatternSources.set(job.name, sources);
}
if (!this.messageBus.has(job.name)) {
this.messageBus.set(job.name, []);
}
}
unregisterJob(jobName: string): void {
this.relayPatterns.delete(jobName);
this.relayPatternSources.delete(jobName);
this.messageBus.delete(jobName);
}
relayFinding(from: string, to: string, context: RelayContext): void {
const message: RelayMessage = {
from,
to,
context,
timestamp: new Date().toISOString(),
};
const messages = this.messageBus.get(to) ?? [];
messages.push(message);
this.messageBus.set(to, messages);
}
getMessagesForJob(jobName: string): RelayMessage[] {
return this.messageBus.get(jobName) ?? [];
}
clearMessagesForJob(jobName: string): void {
this.messageBus.set(jobName, []);
}
shouldRelayForFile(jobName: string, filePath: string): boolean {
const patterns = this.relayPatterns.get(jobName);
if (!patterns || patterns.length === 0) {
return false;
}
return patterns.some((pattern) => pattern.match(filePath));
}
async deliverMessages(
job: JobSpec,
options?: { filterFrom?: string[] },
): Promise<number> {
const messages = this.getMessagesForJob(job.name);
if (messages.length === 0) {
return 0;
}
const filtered = options?.filterFrom
? messages.filter((m) => options.filterFrom!.includes(m.from))
: messages;
if (filtered.length === 0) {
return 0;
}
if (!job.port) {
return 0;
}
try {
const client = await waitForServer(job.port, { timeoutMs: 5000 });
for (const message of filtered) {
const prompt = this.formatRelayPrompt(message);
await sendPrompt(client, job.launchSessionID ?? '', prompt);
}
this.clearMessagesForJob(job.name);
return filtered.length;
} catch {
return 0;
}
}
private formatRelayPrompt(message: RelayMessage): string {
const { from, context } = message;
const { finding, filePath, lineNumber, severity } = context;
const parts: string[] = [`[Inter-Job Communication from ${from}]`];
if (severity) {
parts.push(`Severity: ${severity.toUpperCase()}`);
}
parts.push(`Finding: ${finding}`);
if (filePath) {
parts.push(`File: ${filePath}`);
}
if (lineNumber) {
parts.push(`Line: ${lineNumber}`);
}
parts.push('\nConsider how this finding may affect your current work.');
return parts.join('\n');
}
getAllRegisteredJobs(): string[] {
return Array.from(this.messageBus.keys());
}
getRelayPatternsForJob(jobName: string): string[] | undefined {
return this.relayPatternSources.get(jobName);
}
}