Repository navigation
Expand file tree
/
Copy pathsource-test-preload.ts
More file actions
89 lines (83 loc) · 4.39 KB
/
Copy pathsource-test-preload.ts
File metadata and controls
89 lines (83 loc) · 4.39 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
/**
* Hermetic source-test preload.
*
* Production modules may construct model-backed agents before a focused spec
* can inject its own model. This harness-only structural double prevents that
* construction from reading credentials or opening a provider connection.
* Agent-keyed responses provide only the structural data each production
* boundary requires. Specs that assert a behavior use their own module mock
* and therefore retain control of their semantic fixture.
*
* The direct model.config contract specs are intentionally left unmocked —
* they assert model.config's own behaviour, so replacing it with the structural
* double would make them assert the double instead of the contract.
*/
import { mock } from "bun:test";
import { AsyncLocalStorage } from "async_hooks";
import { setRequestContextStore } from "./src/internal/shared/observability/request-context.js";
// Tests are a host too: each isolated worker provides Node request storage so
// cancellation and tracing semantics are exercised without shipping that
// implementation in the package runtime.
setRequestContextStore(new AsyncLocalStorage());
const modelConfigSpecs = [
"/shared/agent/tests/model.config.spec.ts",
"/shared/agent/tests/model-overrides.spec.ts",
];
const localModelMockSpecs = [
"/chat/tests/chat.agent.persona.spec.ts",
"/chat/tests/chat.agent.spec.ts",
"/intents/tests/intent.graph.spec.ts",
"/intents/tests/intent.inferrer.spec.ts",
"/intents/tests/intent.reconciler.spec.ts",
"/intents/tests/intent.verifier.spec.ts",
"/negotiations/tests/negotiation.agent.spec.ts",
"/negotiations/tests/negotiation.summarizer.spec.ts",
"/negotiations/tests/negotiator-timeout.spec.ts",
];
const runsSpec = (spec: string) => process.argv.some((arg) => arg.endsWith(spec));
const runsModelConfigSpec = modelConfigSpecs.some(runsSpec);
const runsLocalModelMockSpec = localModelMockSpecs.some(runsSpec);
// Bun merges the package bunfig preload with an explicit config's preload.
// Clearing here keeps the source gate hermetic even when the regular preload
// has already loaded a developer-only root .env.test.
delete process.env.OPENROUTER_API_KEY;
delete process.env.OPENAI_API_KEY;
if (!runsModelConfigSpec && !runsLocalModelMockSpec) {
const structuredResponse = (agent: string): Record<string, unknown> => {
if (agent === "intentReconciler") return { actions: [] };
if (agent === "intentVerifier") {
return { reasoning: "Deterministic source-test verification.", classification: "ASSERTIVE", felicity_scores: { clarity: 80, authority: 80, sincerity: 80 }, semantic_entropy: 0.2, referential_anchor: null, referential_breadth: "narrow", missing_selectional_constraints: [], specificity_warning: null, flags: [] };
}
if (agent === "opportunityPresenter") {
return { presentation: { headline: "A relevant connection", personalizedSummary: "This is a relevant opportunity.", suggestedAction: "Comparing notes on shared work", greeting: "I would like to compare notes." } };
}
if (agent === "homeCategorizer") return { sections: [] };
if (agent === "suggestionGenerator") {
return { suggestions: [{ label: "Share more context", type: "prompt", followupText: null, prefill: "I am looking for " }] };
}
if (agent === "chatTitleGenerator") return { title: "Conversation" };
if (agent === "negotiationReflector") return { memory: "Deterministic source-test reflection.", shouldContinue: true };
if (agent === "negotiationSummarizer") return { summary: "Deterministic source-test summary." };
if (agent === "negotiator") return { hasOpportunity: false, agreedRoles: [], reasoning: "Deterministic source-test negotiation.", turnCount: 0 };
return {};
};
const modelFor = (agent: string) => {
const model = {
invoke: async () => structuredResponse(agent),
bind: () => model,
bindTools: () => model,
withStructuredOutput: () => model,
stream: async function* (): AsyncGenerator<{ content: string }> {
yield { content: "Deterministic source-test response." };
},
};
return model;
};
mock.module(import.meta.resolve("./src/internal/shared/agent/model.config.js"), () => ({
createModel: (agent: string) => modelFor(agent),
createFallbackModel: () => undefined,
createStructuredModel: (agent: string) => modelFor(agent),
createResilientModel: (agent: string) => modelFor(agent),
getModelName: () => "source-test-model",
}));
}