1use super::{scope::TypeScope, TypeChecker};
5use crate::{ast::*, builtin_signatures::TyExt, diagnostic_codes::Code};
6use harn_lexer::Span;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
11#[serde(rename_all = "snake_case")]
12pub enum PredicateSiteKind {
13 Predicate,
15 Evaluation,
17 RuntimeEvaluation,
19}
20
21impl PredicateSiteKind {
22 pub fn outcome_schema(self) -> &'static str {
24 match self {
25 Self::Predicate => "harn.predicate.outcome.v1",
26 Self::Evaluation | Self::RuntimeEvaluation => "harn.evaluation.outcome.v1",
27 }
28 }
29}
30
31#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
34pub struct PredicateSite {
35 pub id: String,
36 pub kind: PredicateSiteKind,
37 pub questions: Vec<super::PredicateQuestionSpec>,
40 pub input_type: TypeExpr,
41 pub line: usize,
42 pub column: usize,
43 pub start: usize,
44 pub end: usize,
45 #[serde(default)]
48 pub model_route: Option<PredicateModelRoute>,
49}
50
51#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
52pub struct PredicateModelRoute {
53 pub provider: String,
54 pub model: String,
55}
56
57fn model_route(policy: &SNode, scope: &TypeScope) -> Option<PredicateModelRoute> {
58 let crate::const_eval::ConstValue::Dict(fields) = scope.const_value(policy)? else {
59 return None;
60 };
61 let string = |name: &str| {
62 fields.iter().find_map(|(key, value)| {
63 if key != name {
64 return None;
65 }
66 match value {
67 crate::const_eval::ConstValue::String(value) => Some(value.clone()),
68 _ => None,
69 }
70 })
71 };
72 Some(PredicateModelRoute {
73 provider: string("provider")?,
74 model: string("model")?,
75 })
76}
77
78pub fn canonical_type(ty: &TypeExpr) -> String {
81 fn normalize(value: &mut serde_json::Value) {
82 match value {
83 serde_json::Value::Object(fields) => {
84 for (name, value) in fields {
85 normalize(value);
86 if matches!(name.as_str(), "Shape" | "Union" | "Intersection") {
87 if let serde_json::Value::Array(items) = value {
88 items.sort_by_key(serde_json::Value::to_string);
89 }
90 }
91 }
92 }
93 serde_json::Value::Array(items) => items.iter_mut().for_each(normalize),
94 _ => {}
95 }
96 }
97 let mut value = serde_json::to_value(ty).expect("type expression serializes");
98 normalize(&mut value);
99 value.to_string()
100}
101
102fn declared_answer_map(mut variant: TypeExpr) -> TypeExpr {
107 let TypeExpr::Shape(fields) = &mut variant else {
108 return variant;
109 };
110 let answered = fields.iter().any(|field| {
111 field.name == "kind"
112 && matches!(&field.type_expr, TypeExpr::LitString(kind)
113 if kind == "answered" || kind == "low_confidence")
114 });
115 if !answered {
116 return variant;
117 }
118 for field in fields.iter_mut() {
119 if matches!(field.name.as_str(), "value" | "candidates") {
120 field.type_expr = declared_answer_map_type().clone();
121 }
122 }
123 variant
124}
125
126fn declared_answer_map_type() -> &'static TypeExpr {
129 static DECLARED: std::sync::OnceLock<TypeExpr> = std::sync::OnceLock::new();
130 DECLARED.get_or_init(|| {
131 let TypeExpr::Union(variants) =
132 harn_builtin_meta::predicate::EVALUATION_OUTCOME.to_type_expr()
133 else {
134 unreachable!("an evaluation outcome is a closed union");
135 };
136 variants
137 .into_iter()
138 .find_map(|variant| {
139 let TypeExpr::Shape(fields) = variant else {
140 return None;
141 };
142 fields.iter().any(|field| {
143 field.name == "kind"
144 && matches!(&field.type_expr, TypeExpr::LitString(kind) if kind == "answered")
145 }).then(|| {
146 fields
147 .into_iter()
148 .find(|field| field.name == "value")
149 .expect("the answered arm carries its answers")
150 .type_expr
151 })
152 })
153 .expect("the evaluation outcome declares an answered arm")
154 })
155}
156
157fn literal_text(node: &SNode) -> Option<String> {
158 match &node.node {
159 Node::StringLiteral(text) | Node::RawStringLiteral(text) if !text.is_empty() => {
160 Some(text.clone())
161 }
162 _ => None,
163 }
164}
165
166fn serializable(ty: &TypeExpr) -> bool {
167 match ty {
168 TypeExpr::Named(name) => {
169 matches!(name.as_str(), "string" | "bool" | "int" | "float" | "nil")
170 }
171 TypeExpr::LitString(_) | TypeExpr::LitInt(_) => true,
172 TypeExpr::Shape(fields) => fields.iter().all(|field| serializable(&field.type_expr)),
173 TypeExpr::List(item) => serializable(item),
174 TypeExpr::Tuple(items) | TypeExpr::Union(items) => {
175 !items.is_empty() && items.iter().all(serializable)
176 }
177 TypeExpr::DictType(key, value) => {
178 matches!(key.as_ref(), TypeExpr::Named(name) if name == "string") && serializable(value)
179 }
180 _ => false,
181 }
182}
183
184impl TypeChecker {
185 fn predicate_method_name(method: &str) -> bool {
186 crate::builtin_signatures::lookup_capability_method(
187 harn_builtin_meta::CapabilityId::Llm,
188 method,
189 )
190 .is_some_and(|signature| {
191 signature.name == harn_builtin_meta::predicate::EVALUATE.name
192 || signature.name == harn_builtin_meta::predicate::EVALUATE_REQUEST.name
193 || signature.name == harn_builtin_meta::predicate::EVALUATE_PREDICATE.name
194 })
195 }
196
197 fn is_predicate_method(&self, object: &SNode, method: &str, scope: &TypeScope) -> bool {
198 if !Self::predicate_method_name(method) {
199 return false;
200 }
201 let Some(ty) = self.infer_type(object, scope) else {
202 return false;
203 };
204 let ty = self.resolve_alias(&ty, scope);
205 let Some(TypeExpr::Named(name)) = super::union::without_nil(&ty) else {
206 return false;
207 };
208 harn_builtin_meta::CapabilityId::from_type_name(&name)
209 == Some(harn_builtin_meta::CapabilityId::Llm)
210 }
211
212 pub(super) fn check_predicate_node(&mut self, node: &SNode, scope: &TypeScope) {
213 let projection = match &node.node {
214 Node::PropertyAccess { object, property }
215 | Node::OptionalPropertyAccess { object, property } => {
216 Some((object, Some(property.as_str())))
217 }
218 Node::SubscriptAccess { object, index }
219 | Node::OptionalSubscriptAccess { object, index } => {
220 let field = match &index.node {
221 Node::StringLiteral(name) | Node::RawStringLiteral(name) => Some(name.as_str()),
222 _ => None,
223 };
224 Some((object, field))
225 }
226 _ => None,
227 };
228 if let Some((object, field)) = projection {
229 if let Some(ty) = self.infer_type(object, scope) {
230 self.check_predicate_field(&ty, field, node.span, scope);
231 }
232 }
233 let named_receiver = match &node.node {
239 Node::MethodCall { object, method, .. }
240 | Node::OptionalMethodCall { object, method, .. } => Some((object, method)),
241 _ => None,
242 };
243 if let Some((object, method)) = named_receiver {
244 if Self::predicate_method_name(method)
245 && self.infer_type(object, scope).is_none_or(|ty| {
246 matches!(self.resolve_alias(&ty, scope), TypeExpr::Named(name)
247 if matches!(name.as_str(), "any" | "unknown" | "dict" | "_"))
248 })
249 {
250 self.error_at_with_help(
251 Code::PredicateSiteInvalid,
252 "predicate method receiver has no statically resolved type".into(),
253 node.span,
254 "retain HarnessLlm in the helper signature instead of erasing it to an unvalidated value".into(),
255 );
256 }
257 }
258 match &node.node {
259 Node::IfElse { condition, .. }
260 | Node::WhileLoop { condition, .. }
261 | Node::GuardStmt { condition, .. }
262 | Node::RequireStmt { condition, .. }
263 | Node::Ternary { condition, .. } => self.check_predicate_boolean(condition, scope),
264 Node::UnaryOp { op, operand } if op == "!" => {
265 self.check_predicate_boolean(operand, scope);
266 }
267 Node::BinaryOp { op, left, right } if op == "&&" || op == "||" => {
268 self.check_predicate_boolean(left, scope);
269 self.check_predicate_boolean(right, scope);
270 }
271 Node::PropertyAccess { object, property }
272 | Node::OptionalPropertyAccess { object, property }
273 if self.is_predicate_method(object, property, scope) =>
274 {
275 self.error_at(Code::PredicateSiteInvalid,
276 "predicate evaluation cannot be captured as a function value; use a typed helper with a literal site".into(), node.span);
277 }
278 Node::OptionalMethodCall { object, method, .. }
279 if self.is_predicate_method(object, method, scope) =>
280 {
281 self.error_at(Code::PredicateSiteInvalid,
282 "predicate evaluation requires an unconditional capability call; handle capability absence explicitly".into(), node.span);
283 }
284 _ => {}
285 }
286 }
287
288 pub(super) fn check_predicate_call(&mut self, args: &[SNode], scope: &TypeScope, span: Span) {
289 let [id, question, input, policy] = args else {
290 return; };
292 let (Some(id), Some(question)) = (literal_text(id), literal_text(question)) else {
293 self.error_at(
294 Code::PredicateSiteInvalid,
295 "predicate id and question must be nonempty string literals".into(),
296 span,
297 );
298 return;
299 };
300 let questions = vec![super::PredicateQuestionSpec {
303 id: id.clone(),
304 kind: super::PredicateQuestionKind::Boolean,
305 instructions: question,
306 labels: Vec::new(),
307 }];
308 self.record_predicate_site(
309 PredicateSiteKind::Predicate,
310 id,
311 questions,
312 input,
313 policy,
314 scope,
315 span,
316 );
317 }
318
319 pub(super) fn check_evaluation_call(&mut self, args: &[SNode], scope: &TypeScope, span: Span) {
320 let [id, state, questions, policy] = args else {
321 return; };
323 let Some(id) = literal_text(id) else {
324 self.error_at(
325 Code::PredicateSiteInvalid,
326 "evaluation id must be a nonempty string literal".into(),
327 span,
328 );
329 return;
330 };
331 let questions = match self.question_set(questions, scope) {
332 Ok(questions) => questions,
333 Err(error) => {
334 self.error_at_with_help(
335 Code::PredicateQuestionSetInvalid,
336 error.message(),
337 questions.span,
338 error.help(),
339 );
340 return;
341 }
342 };
343 self.record_predicate_site(
344 PredicateSiteKind::Evaluation,
345 id,
346 questions,
347 state,
348 policy,
349 scope,
350 span,
351 );
352 }
353
354 pub(super) fn check_evaluation_request_call(
355 &mut self,
356 args: &[SNode],
357 scope: &TypeScope,
358 span: Span,
359 ) {
360 let [id, state, questions, policy] = args else {
361 return;
362 };
363 let Some(id) = literal_text(id) else {
364 self.error_at(
365 Code::PredicateSiteInvalid,
366 "evaluation id must be a nonempty string literal".into(),
367 span,
368 );
369 return;
370 };
371 if !self
372 .infer_type(questions, scope)
373 .is_some_and(|ty| serializable(&self.resolve_alias(&ty, scope)))
374 {
375 self.error_at(
376 Code::PredicateQuestionSetInvalid,
377 "runtime evaluation questions must have a closed typed question map".into(),
378 questions.span,
379 );
380 return;
381 }
382 self.record_predicate_site(
383 PredicateSiteKind::RuntimeEvaluation,
384 id,
385 Vec::new(),
386 state,
387 policy,
388 scope,
389 span,
390 );
391 }
392
393 #[allow(clippy::too_many_arguments)]
394 fn record_predicate_site(
395 &mut self,
396 kind: PredicateSiteKind,
397 id: String,
398 questions: Vec<super::PredicateQuestionSpec>,
399 input: &SNode,
400 policy: &SNode,
401 scope: &TypeScope,
402 span: Span,
403 ) {
404 let Some(input_type) = self.infer_type(input, scope) else {
405 self.predicate_input_error(input.span);
406 return;
407 };
408 let input_type = self.resolve_alias(&input_type, scope);
409 if !serializable(&input_type) {
410 self.predicate_input_error(input.span);
411 return;
412 }
413 let policy_is_closed = self
416 .infer_type(policy, scope)
417 .is_some_and(|ty| serializable(&self.resolve_alias(&ty, scope)));
418 if !policy_is_closed {
419 self.error_at(
420 Code::PredicateInputInvalid,
421 "predicate policy must have a closed typed record".into(),
422 policy.span,
423 );
424 }
425 if self
426 .predicate_sites
427 .iter()
428 .any(|site| site.id == id && (site.start != span.start || site.end != span.end))
429 {
430 self.error_at(
431 Code::PredicateSiteInvalid,
432 format!("predicate id `{id}` is declared by more than one source site"),
433 span,
434 );
435 return;
436 }
437 if !self
438 .predicate_sites
439 .iter()
440 .any(|site| site.start == span.start && site.end == span.end)
441 {
442 self.predicate_sites.push(PredicateSite {
443 model_route: model_route(policy, scope),
444 id,
445 kind,
446 questions,
447 input_type,
448 line: span.line,
449 column: span.column,
450 start: span.start,
451 end: span.end,
452 });
453 }
454 }
455
456 fn predicate_input_error(&mut self, span: Span) {
457 self.error_at(
458 Code::PredicateInputInvalid,
459 "predicate input must have a closed serializable type; functions, handles, open records and unvalidated values are not accepted".into(),
460 span,
461 );
462 }
463
464 pub(super) fn is_predicate_outcome(&self, ty: &TypeExpr, scope: &TypeScope) -> bool {
465 let ty = self.resolve_alias(ty, scope);
466 let Some(ty) = super::union::without_nil(&ty) else {
467 return false;
468 };
469 let members = match &ty {
470 TypeExpr::Union(members) => members.as_slice(),
471 other => std::slice::from_ref(other),
472 };
473 if members.is_empty()
474 || !members.iter().all(|member| {
475 matches!(member, TypeExpr::Shape(fields) if fields.iter().any(|field| field.name == "receipt"))
476 })
477 {
478 return false;
479 }
480 static VARIANTS: std::sync::OnceLock<Vec<String>> = std::sync::OnceLock::new();
481 let variants = VARIANTS.get_or_init(|| {
482 [
486 harn_builtin_meta::predicate::OUTCOME,
487 harn_builtin_meta::predicate::EVALUATION_OUTCOME,
488 ]
489 .into_iter()
490 .flat_map(|outcome| {
491 let TypeExpr::Union(variants) = outcome.to_type_expr() else {
492 unreachable!("an evaluation outcome is a closed union");
493 };
494 variants
495 })
496 .map(|variant| canonical_type(&declared_answer_map(variant)))
497 .collect()
498 });
499 members
500 .iter()
501 .all(|member| variants.contains(&canonical_type(&declared_answer_map(member.clone()))))
502 }
503
504 fn check_predicate_field(
505 &mut self,
506 ty: &TypeExpr,
507 field: Option<&str>,
508 span: Span,
509 scope: &TypeScope,
510 ) {
511 if !self.is_predicate_outcome(ty, scope) {
512 return;
513 }
514 let ty = self.resolve_alias(ty, scope);
515 let Some(ty) = super::union::without_nil(&ty) else {
516 return;
517 };
518 let members = match &ty {
519 TypeExpr::Union(members) => members.as_slice(),
520 other => std::slice::from_ref(other),
521 };
522 if field.is_some_and(|name| members.iter().all(|member| {
523 matches!(member, TypeExpr::Shape(fields) if fields.iter().any(|field| field.name == name))
524 })) { return; }
525 self.error_at_with_help(
526 Code::PredicateOutcomeUnnarrowed,
527 "predicate variant field is not available on every remaining outcome".into(),
528 span,
529 "match outcome.kind before accessing a variant field; use a named field rather than a dynamic index".into(),
530 );
531 }
532
533 pub(super) fn check_predicate_boolean(&mut self, node: &SNode, scope: &TypeScope) {
534 if self
535 .infer_type(node, scope)
536 .is_some_and(|ty| self.is_predicate_outcome(&ty, scope))
537 {
538 self.error_at_with_help(
539 Code::PredicateBooleanUse,
540 "a predicate outcome is not a boolean".into(),
541 node.span,
542 "match outcome.kind, then branch on outcome.value.verdict only in the verdict arm"
543 .into(),
544 );
545 }
546 }
547
548 pub(super) fn record_predicate_binding(
549 &mut self,
550 pattern: &BindingPattern,
551 inferred: Option<&TypeExpr>,
552 span: Span,
553 scope: &TypeScope,
554 ) {
555 if !inferred.is_some_and(|ty| self.is_predicate_outcome(ty, scope)) {
556 return;
557 }
558 if let (BindingPattern::Dict(fields), Some(ty)) = (pattern, inferred) {
559 for field in fields {
560 if !field.is_rest {
561 self.check_predicate_field(ty, Some(&field.key), span, scope);
562 }
563 }
564 }
565 if let BindingPattern::Identifier(name) = pattern {
566 if is_discard_name(name) {
567 self.unused_predicate_error(span);
568 } else {
569 let binding = crate::lexical::BindingId {
570 name: name.clone(),
571 declaration_start: span.start,
572 declaration_end: span.end,
573 };
574 if !self
575 .predicate_bindings
576 .iter()
577 .any(|(existing, _)| *existing == binding)
578 {
579 self.predicate_bindings.push((binding, span));
580 }
581 }
582 }
583 }
584
585 pub(super) fn unused_predicate_error(&mut self, span: Span) {
586 self.error_at_with_help(
587 Code::PredicateOutcomeUnused,
588 "predicate outcome is discarded without a disposition".into(),
589 span,
590 "match the outcome or pass it to a typed outcome policy".into(),
591 );
592 }
593
594 pub(super) fn check_unused_predicate_bindings(&mut self, program: &[SNode]) {
595 let patterns = crate::lexical::module_match_pattern_catalog_with_visible(
596 program,
597 &self.imported_type_decls,
598 );
599 let used = crate::lexical::resolved_identifier_bindings_with_source(
600 &[],
601 program,
602 self.source.as_deref(),
603 &patterns,
604 );
605 let unused: Vec<_> = self
606 .predicate_bindings
607 .iter()
608 .filter(|(binding, _)| !used.values().any(|used| used == binding))
609 .map(|(_, span)| *span)
610 .collect();
611 for span in unused {
612 self.unused_predicate_error(span);
613 }
614 }
615}