@rhei-team/rhei 1.0.0-beta.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/README.md +1048 -0
- package/bin/rhei-mcp.js +3 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +86366 -0
- package/dist/premium/contracts.d.ts +445 -0
- package/dist/premium/contracts.js +97 -0
- package/dist/vendor/rhei-core/briefs.js +1276 -0
- package/dist/vendor/rhei-core/codeAgent.js +615 -0
- package/dist/vendor/rhei-core/codeEditSession.js +293 -0
- package/dist/vendor/rhei-core/codeIntelligence.js +4287 -0
- package/dist/vendor/rhei-core/codeMarket.js +8946 -0
- package/dist/vendor/rhei-core/codeReviewIntelligence.js +5918 -0
- package/dist/vendor/rhei-core/codeSemantics.js +172427 -0
- package/dist/vendor/rhei-core/codeStory.js +667 -0
- package/dist/vendor/rhei-core/codeStrategyPlan.js +663 -0
- package/dist/vendor/rhei-core/codeTrail.js +2781 -0
- package/dist/vendor/rhei-core/codeWorkHandoff.js +281 -0
- package/dist/vendor/rhei-core/contextQuery.js +1119 -0
- package/dist/vendor/rhei-core/contextRouting.js +2052 -0
- package/dist/vendor/rhei-core/evidenceLedger.js +5336 -0
- package/dist/vendor/rhei-core/executionSafety.js +0 -0
- package/dist/vendor/rhei-core/goalIntelligence.js +2218 -0
- package/dist/vendor/rhei-core/model-lanes.js +75 -0
- package/dist/vendor/rhei-core/now.js +127 -0
- package/dist/vendor/rhei-core/package.json +29 -0
- package/dist/vendor/rhei-core/programPlan.js +3153 -0
- package/dist/vendor/rhei-core/search.js +196 -0
- package/dist/vendor/rhei-core/serviceIntelligence.js +1734 -0
- package/dist/vendor/rhei-core/workflowPlan.js +1660 -0
- package/package.json +41 -0
|
@@ -0,0 +1,615 @@
|
|
|
1
|
+
// ../core/src/codeAgent/types.ts
|
|
2
|
+
var RHEI_CODE_AGENT_SCHEMA_VERSION = 1;
|
|
3
|
+
var RHEI_CODE_AGENT_HANDOFF_KIND = "rhei_code_agent_handoff";
|
|
4
|
+
var RHEI_CODE_AGENT_RESULT_KIND = "rhei_code_agent_result";
|
|
5
|
+
var RHEI_CODE_AGENT_RESULT_SUMMARY_KIND = "rhei_code_agent_result_summary";
|
|
6
|
+
var RHEI_CODE_AGENT_RESULT_RANKING_KIND = "rhei_code_agent_result_ranking";
|
|
7
|
+
var RHEI_CODE_AGENT_TOKEN_ESTIMATE_VERSION = "char-div-4-v1";
|
|
8
|
+
// ../core/src/codeAgent/builders.ts
|
|
9
|
+
var RHEI_CODE_AGENT_ROLES_V1 = [
|
|
10
|
+
"explore_agent",
|
|
11
|
+
"engineer_agent",
|
|
12
|
+
"review_agent",
|
|
13
|
+
"validation_agent",
|
|
14
|
+
"design_agent"
|
|
15
|
+
];
|
|
16
|
+
var RHEI_CODE_AGENT_DESIGN_OUTPUT_KINDS_V1 = [
|
|
17
|
+
"architecture_review",
|
|
18
|
+
"ux_review",
|
|
19
|
+
"workflow_review",
|
|
20
|
+
"design_doc",
|
|
21
|
+
"decision_options",
|
|
22
|
+
"recommended_slice",
|
|
23
|
+
"risk_notes"
|
|
24
|
+
];
|
|
25
|
+
var DEFAULT_GENERATED_AT = Date.parse("2026-05-18T00:00:00.000Z");
|
|
26
|
+
var RHEI_CODE_AGENT_REPORT_ONLY_GUARDRAILS_V1 = {
|
|
27
|
+
reportOnly: true,
|
|
28
|
+
advisoryOnly: true,
|
|
29
|
+
noAuthority: true,
|
|
30
|
+
noProviderCalls: true,
|
|
31
|
+
noWrites: true,
|
|
32
|
+
noNetworkCalls: true,
|
|
33
|
+
noHiddenExecution: true,
|
|
34
|
+
noProcessExecution: true,
|
|
35
|
+
noFilesystemSourceScan: true,
|
|
36
|
+
noSourceReads: true,
|
|
37
|
+
noSessionMutation: true,
|
|
38
|
+
sourceContextGrantsNoAuthority: true,
|
|
39
|
+
applyRequiresSeparateGatedTool: true,
|
|
40
|
+
summarizeIsUtilityModeNotAgentRole: true
|
|
41
|
+
};
|
|
42
|
+
function buildRheiCodeAgentHandoffV1(args) {
|
|
43
|
+
const generatedAt = args.generatedAt ?? DEFAULT_GENERATED_AT;
|
|
44
|
+
const workflowPlan = args.workflowPlan ?? args.codeWorkHandoff?.workflowPlan;
|
|
45
|
+
const sessionSelect = args.sessionSelect ?? workflowPlan?.sessionSelect;
|
|
46
|
+
const oracleContextBundles = uniqueById([args.oracleContextBundle, ...args.oracleContextBundles ?? []], (bundle) => bundle.bundleId);
|
|
47
|
+
const contextBundleIds = uniqueStrings([
|
|
48
|
+
...args.contextBundleIds ?? [],
|
|
49
|
+
...args.sourceRefs?.oracleContextBundleIds ?? [],
|
|
50
|
+
...oracleContextBundles.map((bundle) => bundle.bundleId)
|
|
51
|
+
]);
|
|
52
|
+
const sourceRefs = buildSourceRefs(args, contextBundleIds);
|
|
53
|
+
const contextRefs = buildContextRefs(args, sessionSelect);
|
|
54
|
+
const expectedOutputKinds = uniqueOutputKinds(args.expectedOutputKinds ?? defaultExpectedOutputKinds(args.assignedRole));
|
|
55
|
+
const roleDefinition = roleDefinitionFor(args.assignedRole);
|
|
56
|
+
const goal = args.goal ?? args.objective ?? "Rhei Code Agent handoff";
|
|
57
|
+
const handoffId = args.handoffId ?? `rhei-code-agent-handoff:${stableSlug(args.assignedRole)}:${stableSlug(goal)}`;
|
|
58
|
+
const parentSessionId = args.parentSessionId ?? sourceRefs.codeSessionId;
|
|
59
|
+
const workflowPlanId = args.workflowPlanId ?? sourceRefs.workflowPlanId;
|
|
60
|
+
const codeWorkHandoffId = args.codeWorkHandoffId ?? sourceRefs.codeWorkHandoffId;
|
|
61
|
+
const oracleSessionId = args.oracleSessionId ?? sourceRefs.oracleSessionId;
|
|
62
|
+
const receipts = uniqueStrings([...args.receipts ?? [], ...sourceRefs.receiptRefs]);
|
|
63
|
+
const scope = buildScope(args, sessionSelect, contextRefs, workflowPlan?.sourceRefs.serviceConnectionIds ?? []);
|
|
64
|
+
const context = buildContext({ workflowPlan, codeWorkHandoff: args.codeWorkHandoff, oracleContextBundles, sessionSelect });
|
|
65
|
+
const instructions = buildInstructions(args, goal, expectedOutputKinds);
|
|
66
|
+
return {
|
|
67
|
+
schemaVersion: RHEI_CODE_AGENT_SCHEMA_VERSION,
|
|
68
|
+
kind: RHEI_CODE_AGENT_HANDOFF_KIND,
|
|
69
|
+
mode: "handoff",
|
|
70
|
+
handoffId,
|
|
71
|
+
generatedAt,
|
|
72
|
+
parentSessionId,
|
|
73
|
+
workflowPlanId,
|
|
74
|
+
codeWorkHandoffId,
|
|
75
|
+
oracleSessionId,
|
|
76
|
+
contextBundleIds,
|
|
77
|
+
goal,
|
|
78
|
+
objective: args.objective ?? goal,
|
|
79
|
+
assignedRole: args.assignedRole,
|
|
80
|
+
roleDefinition,
|
|
81
|
+
task: args.task ?? goal,
|
|
82
|
+
expectedOutputKinds,
|
|
83
|
+
scope,
|
|
84
|
+
context,
|
|
85
|
+
instructions,
|
|
86
|
+
instructionNotes: buildInstructionNotes(args),
|
|
87
|
+
gates: buildGates(args, expectedOutputKinds),
|
|
88
|
+
receipts,
|
|
89
|
+
sourceRefs,
|
|
90
|
+
contextRefs,
|
|
91
|
+
workflowPlan,
|
|
92
|
+
sessionSelect,
|
|
93
|
+
codeWorkHandoff: args.codeWorkHandoff,
|
|
94
|
+
oracleContextBundles,
|
|
95
|
+
modelProfile: args.modelProfile ? modelProfileRef(args.modelProfile) : undefined,
|
|
96
|
+
riskNotes: uniqueStrings(args.riskNotes ?? []),
|
|
97
|
+
warnings: uniqueStrings([
|
|
98
|
+
...args.warnings ?? [],
|
|
99
|
+
...(args.codeWorkHandoff?.warnings ?? []).map((warning) => `code_work_handoff:${warning}`),
|
|
100
|
+
...(workflowPlan?.warnings ?? []).map((warning) => `workflow_plan:${warning}`)
|
|
101
|
+
]),
|
|
102
|
+
execution: noExecution(),
|
|
103
|
+
guardrails: reportOnlyGuardrails(),
|
|
104
|
+
...safetyFlags()
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
function buildRheiCodeAgentResultV1(args) {
|
|
108
|
+
const generatedAt = args.generatedAt ?? DEFAULT_GENERATED_AT;
|
|
109
|
+
const assignedRole = args.assignedRole ?? args.sourceHandoff?.assignedRole ?? "explore_agent";
|
|
110
|
+
const objective = args.objective ?? args.sourceHandoff?.goal ?? args.sourceHandoff?.objective ?? "Rhei Code Agent result";
|
|
111
|
+
const sourceHandoffId = args.sourceHandoffId ?? args.sourceHandoff?.handoffId;
|
|
112
|
+
const outputs = (args.outputs ?? []).map((output, index) => buildOutput(output, index));
|
|
113
|
+
const status = args.status ?? inferStatus(outputs, args.riskNotes ?? []);
|
|
114
|
+
const score = buildScore({ status, outputs, riskNotes: args.riskNotes ?? [], overrides: args.score });
|
|
115
|
+
const rankingSignals = buildRankingSignals({ score, outputs, riskNotes: args.riskNotes ?? [], overrides: args.rankingSignals });
|
|
116
|
+
const resultId = args.resultId ?? `rhei-code-agent-result:${stableSlug(assignedRole)}:${stableSlug(sourceHandoffId ?? objective)}`;
|
|
117
|
+
return {
|
|
118
|
+
schemaVersion: RHEI_CODE_AGENT_SCHEMA_VERSION,
|
|
119
|
+
kind: RHEI_CODE_AGENT_RESULT_KIND,
|
|
120
|
+
mode: "import_result",
|
|
121
|
+
resultId,
|
|
122
|
+
generatedAt,
|
|
123
|
+
sourceHandoffId,
|
|
124
|
+
assignedRole,
|
|
125
|
+
objective,
|
|
126
|
+
status,
|
|
127
|
+
summary: args.summary,
|
|
128
|
+
outputs,
|
|
129
|
+
evidenceRefs: uniqueStrings(args.evidenceRefs ?? args.sourceHandoff?.contextRefs.evidenceRefs ?? []),
|
|
130
|
+
receiptRefs: uniqueStrings(args.receiptRefs ?? args.sourceHandoff?.sourceRefs.receiptRefs ?? []),
|
|
131
|
+
riskNotes: uniqueStrings(args.riskNotes ?? []),
|
|
132
|
+
warnings: uniqueStrings(args.warnings ?? []),
|
|
133
|
+
score,
|
|
134
|
+
rankingSignals,
|
|
135
|
+
execution: noExecution(),
|
|
136
|
+
guardrails: reportOnlyGuardrails(),
|
|
137
|
+
...safetyFlags()
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
function summarizeRheiCodeAgentResultsV1(args) {
|
|
141
|
+
const generatedAt = args.generatedAt ?? DEFAULT_GENERATED_AT;
|
|
142
|
+
const results = args.results.map(summaryItem);
|
|
143
|
+
const aggregateScore = averageScore(args.results.map((result) => result.score), "Aggregate score across imported Rhei Code Agent results.");
|
|
144
|
+
const summaryId = args.summaryId ?? `rhei-code-agent-result-summary:${stableSlug(args.objective)}:${args.results.length}`;
|
|
145
|
+
return {
|
|
146
|
+
schemaVersion: RHEI_CODE_AGENT_SCHEMA_VERSION,
|
|
147
|
+
kind: RHEI_CODE_AGENT_RESULT_SUMMARY_KIND,
|
|
148
|
+
mode: "summarize_result",
|
|
149
|
+
summaryId,
|
|
150
|
+
generatedAt,
|
|
151
|
+
objective: args.objective,
|
|
152
|
+
resultCount: args.results.length,
|
|
153
|
+
sourceResultIds: args.results.map((result) => result.resultId),
|
|
154
|
+
statusCounts: statusCounts(args.results),
|
|
155
|
+
outputKindCounts: outputKindCounts(args.results),
|
|
156
|
+
topRisks: uniqueStrings(args.results.flatMap((result) => result.riskNotes)).slice(0, 10),
|
|
157
|
+
recommendedNextSlice: args.recommendedNextSlice,
|
|
158
|
+
aggregateScore,
|
|
159
|
+
results,
|
|
160
|
+
warnings: uniqueStrings(args.warnings ?? []),
|
|
161
|
+
execution: noExecution(),
|
|
162
|
+
guardrails: reportOnlyGuardrails(),
|
|
163
|
+
...safetyFlags()
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
function rankRheiCodeAgentResultsV1(args) {
|
|
167
|
+
const generatedAt = args.generatedAt ?? DEFAULT_GENERATED_AT;
|
|
168
|
+
const sorted = [...args.results].sort(compareResults);
|
|
169
|
+
const recommendedResultId = sorted.find((result) => ["completed", "partial"].includes(result.status))?.resultId;
|
|
170
|
+
const rankingId = args.rankingId ?? `rhei-code-agent-result-ranking:${stableSlug(args.objective)}:${args.results.length}`;
|
|
171
|
+
return {
|
|
172
|
+
schemaVersion: RHEI_CODE_AGENT_SCHEMA_VERSION,
|
|
173
|
+
kind: RHEI_CODE_AGENT_RESULT_RANKING_KIND,
|
|
174
|
+
mode: "rank_results",
|
|
175
|
+
rankingId,
|
|
176
|
+
generatedAt,
|
|
177
|
+
objective: args.objective,
|
|
178
|
+
sourceResultIds: args.results.map((result) => result.resultId),
|
|
179
|
+
rankings: sorted.map((result, index) => ({
|
|
180
|
+
rank: index + 1,
|
|
181
|
+
resultId: result.resultId,
|
|
182
|
+
assignedRole: result.assignedRole,
|
|
183
|
+
status: result.status,
|
|
184
|
+
score: result.score,
|
|
185
|
+
rankingSignals: result.rankingSignals,
|
|
186
|
+
rationale: result.score.rationale,
|
|
187
|
+
recommended: result.resultId === recommendedResultId,
|
|
188
|
+
riskNotes: result.riskNotes,
|
|
189
|
+
...safetyFlags()
|
|
190
|
+
})),
|
|
191
|
+
recommendedResultId,
|
|
192
|
+
warnings: uniqueStrings(args.warnings ?? []),
|
|
193
|
+
execution: noExecution(),
|
|
194
|
+
guardrails: reportOnlyGuardrails(),
|
|
195
|
+
...safetyFlags()
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
function roleDefinitionFor(role) {
|
|
199
|
+
switch (role) {
|
|
200
|
+
case "explore_agent":
|
|
201
|
+
return roleDefinition(role, "Explore agent", "Map relevant context and report findings without edits.", ["context_map", "finding", "risk_notes"]);
|
|
202
|
+
case "engineer_agent":
|
|
203
|
+
return roleDefinition(role, "Engineer agent", "Plan implementation slices and patch proposals without applying changes.", ["implementation_plan", "patch_plan", "recommended_slice", "risk_notes"]);
|
|
204
|
+
case "review_agent":
|
|
205
|
+
return roleDefinition(role, "Review agent", "Review proposed work and identify correctness, maintainability, and safety risks.", ["review_notes", "finding", "risk_notes"]);
|
|
206
|
+
case "validation_agent":
|
|
207
|
+
return roleDefinition(role, "Validation agent", "Plan or summarize validation evidence without running hidden commands.", ["validation_plan", "validation_evidence", "risk_notes"]);
|
|
208
|
+
case "design_agent":
|
|
209
|
+
return roleDefinition(role, "Design agent", "Critique architecture, product, UX, and workflow options; recommend a bounded slice while staying report-only.", [...RHEI_CODE_AGENT_DESIGN_OUTPUT_KINDS_V1], ["architecture", "product", "ux", "workflow"]);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
function roleDefinition(role, title, purpose, defaultExpectedOutputKinds, designFocus) {
|
|
213
|
+
return {
|
|
214
|
+
role,
|
|
215
|
+
title,
|
|
216
|
+
purpose,
|
|
217
|
+
defaultExpectedOutputKinds,
|
|
218
|
+
designFocus,
|
|
219
|
+
...safetyFlags()
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
function defaultExpectedOutputKinds(role) {
|
|
223
|
+
return roleDefinitionFor(role).defaultExpectedOutputKinds;
|
|
224
|
+
}
|
|
225
|
+
function buildScope(args, sessionSelect, contextRefs, workflowServiceConnectionIds) {
|
|
226
|
+
return {
|
|
227
|
+
exactRefs: uniqueStrings([...args.scope?.exactRefs ?? [], ...contextRefs.exactRefs]),
|
|
228
|
+
readSlices: uniqueReadSlices([
|
|
229
|
+
...args.scope?.readSlices ?? [],
|
|
230
|
+
...(sessionSelect?.readSlices ?? []).map((slice) => ({
|
|
231
|
+
readSliceId: slice.readSliceId,
|
|
232
|
+
path: slice.path,
|
|
233
|
+
lines: slice.lines,
|
|
234
|
+
role: mapReadSliceRole(slice.role),
|
|
235
|
+
reason: slice.reason,
|
|
236
|
+
tokenEstimate: slice.tokenEstimate
|
|
237
|
+
}))
|
|
238
|
+
]),
|
|
239
|
+
avoidRefs: uniqueStrings([...args.scope?.avoidRefs ?? [], ...contextRefs.avoidRefs]),
|
|
240
|
+
serviceConnectionIds: uniqueStrings([
|
|
241
|
+
...args.scope?.serviceConnectionIds ?? [],
|
|
242
|
+
...workflowServiceConnectionIds,
|
|
243
|
+
...args.codeWorkHandoff?.serviceConnections.ids ?? []
|
|
244
|
+
])
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
function buildContext(args) {
|
|
248
|
+
return {
|
|
249
|
+
workflowPlan: args.workflowPlan,
|
|
250
|
+
codeWorkHandoff: args.codeWorkHandoff,
|
|
251
|
+
oracleContextBundles: args.oracleContextBundles ?? [],
|
|
252
|
+
sessionSelect: args.sessionSelect
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
function buildInstructions(args, goal, expectedReturn) {
|
|
256
|
+
const input = Array.isArray(args.instructions) ? undefined : args.instructions;
|
|
257
|
+
return {
|
|
258
|
+
prompt: input?.prompt ?? defaultPrompt(args.assignedRole, goal),
|
|
259
|
+
stopWhen: uniqueStrings([
|
|
260
|
+
...input?.stopWhen ?? [],
|
|
261
|
+
"A report-only result artifact can be returned.",
|
|
262
|
+
"Required uncertainty, blockers, and risk notes are explicit."
|
|
263
|
+
]),
|
|
264
|
+
allowedActions: uniqueStrings([
|
|
265
|
+
...input?.allowedActions ?? [],
|
|
266
|
+
"read supplied handoff context",
|
|
267
|
+
"produce report-only findings",
|
|
268
|
+
"return structured result outputs"
|
|
269
|
+
]),
|
|
270
|
+
deniedActions: uniqueStrings([
|
|
271
|
+
...input?.deniedActions ?? [],
|
|
272
|
+
"provider calls",
|
|
273
|
+
"network calls",
|
|
274
|
+
"filesystem writes",
|
|
275
|
+
"source edits",
|
|
276
|
+
"session mutation",
|
|
277
|
+
"apply or commit changes"
|
|
278
|
+
]),
|
|
279
|
+
expectedReturn: uniqueOutputKinds(input?.expectedReturn ?? expectedReturn)
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
function buildInstructionNotes(args) {
|
|
283
|
+
const notes = uniqueStrings([
|
|
284
|
+
...Array.isArray(args.instructions) ? args.instructions : [],
|
|
285
|
+
...args.instructionNotes ?? [],
|
|
286
|
+
"This Rhei Code Agent handoff is report-only and grants no write, provider, session, or apply authority.",
|
|
287
|
+
"summarize_result is a utility mode, not an agent role.",
|
|
288
|
+
...args.assignedRole === "design_agent" ? ["design_agent critiques architecture, product, UX, and workflow trade-offs; prefer decision options and a recommended slice over direct implementation."] : []
|
|
289
|
+
]);
|
|
290
|
+
return notes.length > 0 ? notes : undefined;
|
|
291
|
+
}
|
|
292
|
+
function buildGates(args, expectedReturn) {
|
|
293
|
+
const implementationLike = args.assignedRole === "engineer_agent" || expectedReturn.includes("patch_plan");
|
|
294
|
+
const validationLike = args.assignedRole === "validation_agent" || expectedReturn.some((kind) => kind.startsWith("validation_"));
|
|
295
|
+
return {
|
|
296
|
+
impactRequired: args.gates?.impactRequired ?? implementationLike,
|
|
297
|
+
proofRequired: args.gates?.proofRequired ?? implementationLike,
|
|
298
|
+
dryRunRequired: args.gates?.dryRunRequired ?? implementationLike,
|
|
299
|
+
validationRequired: args.gates?.validationRequired ?? (implementationLike || validationLike),
|
|
300
|
+
writerArbitrationRequired: true
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
function defaultPrompt(role, goal) {
|
|
304
|
+
if (role === "design_agent") {
|
|
305
|
+
return `Report-only design critique for: ${goal}. Focus on architecture, product, UX, workflow options, risks, and a recommended slice.`;
|
|
306
|
+
}
|
|
307
|
+
return `Report-only ${role} handoff for: ${goal}. Return structured findings and risk notes only; do not execute, call providers, or write.`;
|
|
308
|
+
}
|
|
309
|
+
function mapReadSliceRole(role) {
|
|
310
|
+
if (role === "source" || role === "test" || role === "doc" || role === "config")
|
|
311
|
+
return role;
|
|
312
|
+
if (role === "service_connection_evidence")
|
|
313
|
+
return "contract";
|
|
314
|
+
return "unknown";
|
|
315
|
+
}
|
|
316
|
+
function uniqueReadSlices(values) {
|
|
317
|
+
const output = [];
|
|
318
|
+
const seen = new Set;
|
|
319
|
+
for (const value of values) {
|
|
320
|
+
if (seen.has(value.readSliceId))
|
|
321
|
+
continue;
|
|
322
|
+
seen.add(value.readSliceId);
|
|
323
|
+
output.push(value);
|
|
324
|
+
}
|
|
325
|
+
return output;
|
|
326
|
+
}
|
|
327
|
+
function buildSourceRefs(args, oracleContextBundleIds) {
|
|
328
|
+
const codeWorkHandoff = args.codeWorkHandoff;
|
|
329
|
+
const workflowPlan = args.workflowPlan ?? codeWorkHandoff?.workflowPlan;
|
|
330
|
+
return {
|
|
331
|
+
handoffId: args.sourceRefs?.handoffId,
|
|
332
|
+
codeWorkHandoffId: args.codeWorkHandoffId ?? args.sourceRefs?.codeWorkHandoffId ?? codeWorkHandoff?.handoffId,
|
|
333
|
+
workflowPlanId: args.workflowPlanId ?? args.sourceRefs?.workflowPlanId ?? workflowPlan?.workflowPlanId ?? codeWorkHandoff?.sourceRefs.workflowPlanId,
|
|
334
|
+
strategyId: args.sourceRefs?.strategyId ?? workflowPlan?.sourceRefs.strategyId ?? codeWorkHandoff?.sourceRefs.strategyId,
|
|
335
|
+
codeSessionId: args.parentSessionId ?? args.sourceRefs?.codeSessionId ?? codeWorkHandoff?.sourceRefs.sessionId,
|
|
336
|
+
oracleSessionId: args.oracleSessionId ?? args.sourceRefs?.oracleSessionId,
|
|
337
|
+
contextPacketIds: uniqueStrings([
|
|
338
|
+
...args.sourceRefs?.contextPacketIds ?? [],
|
|
339
|
+
...workflowPlan?.sourceRefs.contextPacketIds ?? [],
|
|
340
|
+
...codeWorkHandoff?.sourceRefs.contextPacketIds ?? []
|
|
341
|
+
]),
|
|
342
|
+
oracleContextBundleIds: uniqueStrings([...args.sourceRefs?.oracleContextBundleIds ?? [], ...oracleContextBundleIds]),
|
|
343
|
+
oraclePromptPacketIds: uniqueStrings([
|
|
344
|
+
...args.sourceRefs?.oraclePromptPacketIds ?? [],
|
|
345
|
+
...(workflowPlan?.oraclePromptPackets ?? []).map((packet) => packet.packetId),
|
|
346
|
+
...(codeWorkHandoff?.oracle.promptPackets ?? []).map((packet) => packet.packetId)
|
|
347
|
+
]),
|
|
348
|
+
receiptRefs: uniqueStrings([...args.sourceRefs?.receiptRefs ?? [], ...codeWorkHandoff?.receipts.refs ?? []])
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
function buildContextRefs(args, sessionSelect) {
|
|
352
|
+
const codeWorkContext = args.codeWorkHandoff?.contextRefs;
|
|
353
|
+
return {
|
|
354
|
+
exactRefs: uniqueStrings([...args.contextRefs?.exactRefs ?? [], ...sessionSelect?.exactRefs ?? [], ...codeWorkContext?.exactRefs ?? []]),
|
|
355
|
+
sourceRefs: uniqueStrings([...args.contextRefs?.sourceRefs ?? [], ...codeWorkContext?.sourceRefs ?? []]),
|
|
356
|
+
evidenceRefs: uniqueStrings([...args.contextRefs?.evidenceRefs ?? [], ...codeWorkContext?.evidenceRefs ?? []]),
|
|
357
|
+
avoidRefs: uniqueStrings([...args.contextRefs?.avoidRefs ?? [], ...sessionSelect?.avoidRefs ?? [], ...codeWorkContext?.avoidRefs ?? []]),
|
|
358
|
+
pathOnlyRefs: uniqueStrings([...args.contextRefs?.pathOnlyRefs ?? [], ...sessionSelect?.pathOnlyRefs ?? [], ...codeWorkContext?.pathOnlyRefs ?? []]),
|
|
359
|
+
deferredRefs: uniqueStrings([...args.contextRefs?.deferredRefs ?? [], ...sessionSelect?.deferredRefs ?? [], ...codeWorkContext?.deferredRefs ?? []]),
|
|
360
|
+
readSliceIds: uniqueStrings([
|
|
361
|
+
...args.contextRefs?.readSliceIds ?? [],
|
|
362
|
+
...(sessionSelect?.readSlices ?? []).map((slice) => slice.readSliceId),
|
|
363
|
+
...codeWorkContext?.readSliceIds ?? []
|
|
364
|
+
])
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
function modelProfileRef(modelProfile) {
|
|
368
|
+
if (!modelProfile)
|
|
369
|
+
return;
|
|
370
|
+
return {
|
|
371
|
+
profileId: modelProfile.profileId,
|
|
372
|
+
role: modelProfile.role,
|
|
373
|
+
provider: modelProfile.provider,
|
|
374
|
+
modelRef: modelProfile.modelRef,
|
|
375
|
+
reasoningEffort: modelProfile.reasoningEffort,
|
|
376
|
+
configurable: modelProfile.configurable,
|
|
377
|
+
...safetyFlags()
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
function buildOutput(args, index) {
|
|
381
|
+
return {
|
|
382
|
+
outputId: args.outputId ?? `agent-output:${index + 1}:${stableSlug(args.outputKind)}`,
|
|
383
|
+
outputKind: args.outputKind,
|
|
384
|
+
title: args.title,
|
|
385
|
+
summary: args.summary,
|
|
386
|
+
content: args.content,
|
|
387
|
+
refs: uniqueStrings(args.refs ?? []),
|
|
388
|
+
riskNotes: uniqueStrings(args.riskNotes ?? []),
|
|
389
|
+
confidence: clamp01(args.confidence ?? 0.5),
|
|
390
|
+
...safetyFlags()
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
function inferStatus(outputs, riskNotes) {
|
|
394
|
+
if (outputs.length === 0)
|
|
395
|
+
return riskNotes.length > 0 ? "blocked" : "not_run";
|
|
396
|
+
return "completed";
|
|
397
|
+
}
|
|
398
|
+
function buildScore(args) {
|
|
399
|
+
const averageConfidence = args.outputs.length > 0 ? args.outputs.reduce((sum, output) => sum + output.confidence, 0) / args.outputs.length : 0;
|
|
400
|
+
const relevance = clamp01(args.overrides?.relevance ?? averageConfidence);
|
|
401
|
+
const completeness = clamp01(args.overrides?.completeness ?? statusCompleteness(args.status));
|
|
402
|
+
const evidenceStrength = clamp01(args.overrides?.evidenceStrength ?? evidenceStrengthFromOutputs(args.outputs));
|
|
403
|
+
const riskPenalty = clamp01(args.overrides?.riskPenalty ?? Math.min(args.riskNotes.length * 0.08, 0.4));
|
|
404
|
+
const blockedPenalty = clamp01(args.overrides?.blockedPenalty ?? (args.status === "blocked" || args.status === "failed" ? 0.35 : 0));
|
|
405
|
+
const score = clamp01(args.overrides?.score ?? (relevance + completeness + evidenceStrength) / 3 - riskPenalty - blockedPenalty);
|
|
406
|
+
return {
|
|
407
|
+
score,
|
|
408
|
+
confidence: clamp01(args.overrides?.confidence ?? averageConfidence),
|
|
409
|
+
relevance,
|
|
410
|
+
completeness,
|
|
411
|
+
evidenceStrength,
|
|
412
|
+
riskPenalty,
|
|
413
|
+
blockedPenalty,
|
|
414
|
+
rationale: args.overrides?.rationale ?? "Deterministic report-only score from status, output confidence, evidence refs, and risk notes.",
|
|
415
|
+
...safetyFlags()
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
function buildRankingSignals(args) {
|
|
419
|
+
const proposalReady = args.outputs.some((output) => [
|
|
420
|
+
"implementation_plan",
|
|
421
|
+
"patch_plan",
|
|
422
|
+
"review_notes",
|
|
423
|
+
"validation_plan",
|
|
424
|
+
"validation_evidence",
|
|
425
|
+
"architecture_review",
|
|
426
|
+
"ux_review",
|
|
427
|
+
"workflow_review",
|
|
428
|
+
"design_doc",
|
|
429
|
+
"decision_options",
|
|
430
|
+
"recommended_slice"
|
|
431
|
+
].includes(output.outputKind));
|
|
432
|
+
const patchPreviewQuality = args.outputs.some((output) => output.outputKind === "patch_plan") ? args.score.completeness : 0;
|
|
433
|
+
const highRisk = args.riskNotes.some((note) => /high|broad|violation|write|provider|network|spawn/i.test(note));
|
|
434
|
+
const complianceViolations = uniqueStrings(args.overrides?.complianceViolations ?? args.riskNotes.filter((note) => note.startsWith("compliance_violation:")).map((note) => note.replace(/^compliance_violation:/, "")));
|
|
435
|
+
const complianceNoWriteNoProvider = args.overrides?.complianceNoWriteNoProvider ?? !complianceViolations.some((violation) => ["writes", "apply_executed", "provider_calls"].includes(violation));
|
|
436
|
+
const complianceNoNetwork = args.overrides?.complianceNoNetwork ?? !complianceViolations.includes("network_calls");
|
|
437
|
+
const repeatedReadCount = Math.max(0, Math.floor(args.overrides?.repeatedReadCount ?? 0));
|
|
438
|
+
const wastedReadPenalty = clamp01(args.overrides?.wastedReadPenalty ?? Math.min(0.5, repeatedReadCount * 0.1));
|
|
439
|
+
const efficiency = clamp01(args.overrides?.efficiency ?? 1 - wastedReadPenalty - args.score.blockedPenalty);
|
|
440
|
+
return {
|
|
441
|
+
requiredRefRecall: clamp01(args.overrides?.requiredRefRecall ?? args.score.relevance),
|
|
442
|
+
proposalSafetyQuality: clamp01(args.overrides?.proposalSafetyQuality ?? (proposalReady && !highRisk ? args.score.completeness : args.score.completeness * 0.6)),
|
|
443
|
+
proposalReady: args.overrides?.proposalReady ?? proposalReady,
|
|
444
|
+
patchPreviewQuality: clamp01(args.overrides?.patchPreviewQuality ?? patchPreviewQuality),
|
|
445
|
+
proofRiskValidationStrength: clamp01(args.overrides?.proofRiskValidationStrength ?? Math.max(0, args.score.evidenceStrength - args.score.riskPenalty)),
|
|
446
|
+
efficiency,
|
|
447
|
+
tokenEstimate: args.overrides?.tokenEstimate,
|
|
448
|
+
repeatedReadCount,
|
|
449
|
+
wastedReadPenalty,
|
|
450
|
+
complianceNoWriteNoProvider,
|
|
451
|
+
complianceNoNetwork,
|
|
452
|
+
noLiveCompliance: args.overrides?.noLiveCompliance ?? (complianceNoWriteNoProvider && complianceNoNetwork && complianceViolations.length === 0),
|
|
453
|
+
complianceViolations,
|
|
454
|
+
rationale: args.overrides?.rationale ?? "Deterministic ranking signals from required-ref recall, proposal quality, patch/proof/validation strength, efficiency, and no-live compliance.",
|
|
455
|
+
...safetyFlags()
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
function summaryItem(result) {
|
|
459
|
+
return {
|
|
460
|
+
resultId: result.resultId,
|
|
461
|
+
assignedRole: result.assignedRole,
|
|
462
|
+
status: result.status,
|
|
463
|
+
summary: result.summary,
|
|
464
|
+
outputKinds: uniqueOutputKinds(result.outputs.map((output) => output.outputKind)),
|
|
465
|
+
score: result.score.score,
|
|
466
|
+
riskNotes: result.riskNotes,
|
|
467
|
+
...safetyFlags()
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
function averageScore(scores, rationale) {
|
|
471
|
+
if (scores.length === 0) {
|
|
472
|
+
return {
|
|
473
|
+
score: 0,
|
|
474
|
+
confidence: 0,
|
|
475
|
+
relevance: 0,
|
|
476
|
+
completeness: 0,
|
|
477
|
+
evidenceStrength: 0,
|
|
478
|
+
riskPenalty: 0,
|
|
479
|
+
blockedPenalty: 0,
|
|
480
|
+
rationale,
|
|
481
|
+
...safetyFlags()
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
return {
|
|
485
|
+
score: average(scores.map((score) => score.score)),
|
|
486
|
+
confidence: average(scores.map((score) => score.confidence)),
|
|
487
|
+
relevance: average(scores.map((score) => score.relevance)),
|
|
488
|
+
completeness: average(scores.map((score) => score.completeness)),
|
|
489
|
+
evidenceStrength: average(scores.map((score) => score.evidenceStrength)),
|
|
490
|
+
riskPenalty: average(scores.map((score) => score.riskPenalty)),
|
|
491
|
+
blockedPenalty: average(scores.map((score) => score.blockedPenalty)),
|
|
492
|
+
rationale,
|
|
493
|
+
...safetyFlags()
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
function statusCounts(results) {
|
|
497
|
+
return {
|
|
498
|
+
completed: results.filter((result) => result.status === "completed").length,
|
|
499
|
+
partial: results.filter((result) => result.status === "partial").length,
|
|
500
|
+
blocked: results.filter((result) => result.status === "blocked").length,
|
|
501
|
+
failed: results.filter((result) => result.status === "failed").length,
|
|
502
|
+
not_run: results.filter((result) => result.status === "not_run").length
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
function outputKindCounts(results) {
|
|
506
|
+
const counts = {};
|
|
507
|
+
for (const output of results.flatMap((result) => result.outputs)) {
|
|
508
|
+
counts[output.outputKind] = (counts[output.outputKind] ?? 0) + 1;
|
|
509
|
+
}
|
|
510
|
+
return counts;
|
|
511
|
+
}
|
|
512
|
+
function compareResults(a, b) {
|
|
513
|
+
return b.score.score - a.score.score || b.rankingSignals.requiredRefRecall - a.rankingSignals.requiredRefRecall || b.rankingSignals.proposalSafetyQuality - a.rankingSignals.proposalSafetyQuality || b.rankingSignals.proofRiskValidationStrength - a.rankingSignals.proofRiskValidationStrength || b.rankingSignals.efficiency - a.rankingSignals.efficiency || b.score.evidenceStrength - a.score.evidenceStrength || a.generatedAt - b.generatedAt;
|
|
514
|
+
}
|
|
515
|
+
function statusCompleteness(status) {
|
|
516
|
+
switch (status) {
|
|
517
|
+
case "completed":
|
|
518
|
+
return 1;
|
|
519
|
+
case "partial":
|
|
520
|
+
return 0.65;
|
|
521
|
+
case "blocked":
|
|
522
|
+
return 0.25;
|
|
523
|
+
case "failed":
|
|
524
|
+
return 0.1;
|
|
525
|
+
case "not_run":
|
|
526
|
+
return 0;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
function evidenceStrengthFromOutputs(outputs) {
|
|
530
|
+
if (outputs.length === 0)
|
|
531
|
+
return 0;
|
|
532
|
+
const refs = uniqueStrings(outputs.flatMap((output) => output.refs));
|
|
533
|
+
return clamp01(refs.length / Math.max(outputs.length * 2, 1));
|
|
534
|
+
}
|
|
535
|
+
function noExecution() {
|
|
536
|
+
return {
|
|
537
|
+
attempted: false,
|
|
538
|
+
providerCalls: false,
|
|
539
|
+
networkCalls: false,
|
|
540
|
+
processExecution: false,
|
|
541
|
+
hiddenExecution: false,
|
|
542
|
+
sourceReads: false,
|
|
543
|
+
writes: false,
|
|
544
|
+
sessionMutation: false
|
|
545
|
+
};
|
|
546
|
+
}
|
|
547
|
+
function reportOnlyGuardrails() {
|
|
548
|
+
return { ...RHEI_CODE_AGENT_REPORT_ONLY_GUARDRAILS_V1 };
|
|
549
|
+
}
|
|
550
|
+
function safetyFlags() {
|
|
551
|
+
return {
|
|
552
|
+
reportOnly: true,
|
|
553
|
+
advisoryOnly: true,
|
|
554
|
+
noAuthority: true,
|
|
555
|
+
noProviderCalls: true,
|
|
556
|
+
noWrites: true
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
function uniqueOutputKinds(values) {
|
|
560
|
+
return [...new Set(values)];
|
|
561
|
+
}
|
|
562
|
+
function uniqueById(values, idFor) {
|
|
563
|
+
const output = [];
|
|
564
|
+
const seen = new Set;
|
|
565
|
+
for (const value of values) {
|
|
566
|
+
if (!value)
|
|
567
|
+
continue;
|
|
568
|
+
const id = idFor(value);
|
|
569
|
+
if (seen.has(id))
|
|
570
|
+
continue;
|
|
571
|
+
seen.add(id);
|
|
572
|
+
output.push(value);
|
|
573
|
+
}
|
|
574
|
+
return output;
|
|
575
|
+
}
|
|
576
|
+
function uniqueStrings(values) {
|
|
577
|
+
const output = [];
|
|
578
|
+
const seen = new Set;
|
|
579
|
+
for (const value of values) {
|
|
580
|
+
const text = typeof value === "string" ? value.trim() : "";
|
|
581
|
+
if (!text || seen.has(text))
|
|
582
|
+
continue;
|
|
583
|
+
seen.add(text);
|
|
584
|
+
output.push(text);
|
|
585
|
+
}
|
|
586
|
+
return output;
|
|
587
|
+
}
|
|
588
|
+
function stableSlug(value) {
|
|
589
|
+
return value.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "").slice(0, 96) || "agent";
|
|
590
|
+
}
|
|
591
|
+
function clamp01(value) {
|
|
592
|
+
if (Number.isNaN(value))
|
|
593
|
+
return 0;
|
|
594
|
+
return Math.max(0, Math.min(1, value));
|
|
595
|
+
}
|
|
596
|
+
function average(values) {
|
|
597
|
+
if (values.length === 0)
|
|
598
|
+
return 0;
|
|
599
|
+
return values.reduce((sum, value) => sum + value, 0) / values.length;
|
|
600
|
+
}
|
|
601
|
+
export {
|
|
602
|
+
summarizeRheiCodeAgentResultsV1,
|
|
603
|
+
rankRheiCodeAgentResultsV1,
|
|
604
|
+
buildRheiCodeAgentResultV1,
|
|
605
|
+
buildRheiCodeAgentHandoffV1,
|
|
606
|
+
RHEI_CODE_AGENT_TOKEN_ESTIMATE_VERSION,
|
|
607
|
+
RHEI_CODE_AGENT_SCHEMA_VERSION,
|
|
608
|
+
RHEI_CODE_AGENT_ROLES_V1,
|
|
609
|
+
RHEI_CODE_AGENT_RESULT_SUMMARY_KIND,
|
|
610
|
+
RHEI_CODE_AGENT_RESULT_RANKING_KIND,
|
|
611
|
+
RHEI_CODE_AGENT_RESULT_KIND,
|
|
612
|
+
RHEI_CODE_AGENT_REPORT_ONLY_GUARDRAILS_V1,
|
|
613
|
+
RHEI_CODE_AGENT_HANDOFF_KIND,
|
|
614
|
+
RHEI_CODE_AGENT_DESIGN_OUTPUT_KINDS_V1
|
|
615
|
+
};
|