@leitwerk-dev/domain 0.1.9 → 0.2.1

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.
Files changed (95) hide show
  1. package/dist/actor.d.ts +4 -2
  2. package/dist/actor.d.ts.map +1 -1
  3. package/dist/domain-model.d.ts +457 -60
  4. package/dist/domain-model.d.ts.map +1 -1
  5. package/dist/index.d.ts +16 -14
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +966 -15
  8. package/dist/index.js.map +1 -1
  9. package/dist/launcher-recent-values.d.ts +5 -0
  10. package/dist/launcher-recent-values.d.ts.map +1 -1
  11. package/dist/markdown-normalize.d.ts +3 -0
  12. package/dist/markdown-normalize.d.ts.map +1 -1
  13. package/dist/number-normalize.d.ts +3 -0
  14. package/dist/number-normalize.d.ts.map +1 -0
  15. package/dist/process-definitions.d.ts +37 -8
  16. package/dist/process-definitions.d.ts.map +1 -1
  17. package/dist/process-model-config.d.ts +18 -0
  18. package/dist/process-model-config.d.ts.map +1 -1
  19. package/dist/process-row-slots.d.ts +14 -0
  20. package/dist/process-row-slots.d.ts.map +1 -1
  21. package/dist/process-state-json.d.ts +13 -0
  22. package/dist/process-state-json.d.ts.map +1 -1
  23. package/dist/repo-locator.d.ts +8 -0
  24. package/dist/repo-locator.d.ts.map +1 -1
  25. package/dist/semantic-entry-refs.d.ts +22 -0
  26. package/dist/semantic-entry-refs.d.ts.map +1 -1
  27. package/dist/startup-observation.d.ts +75 -0
  28. package/dist/startup-observation.d.ts.map +1 -0
  29. package/dist/string-normalize.d.ts +10 -0
  30. package/dist/string-normalize.d.ts.map +1 -1
  31. package/dist/turn-acceptance.d.ts +2 -0
  32. package/dist/turn-acceptance.d.ts.map +1 -1
  33. package/dist/turn-annotations.d.ts +27 -0
  34. package/dist/turn-annotations.d.ts.map +1 -1
  35. package/dist/turn-recovery.d.ts +30 -1
  36. package/dist/turn-recovery.d.ts.map +1 -1
  37. package/dist/turn-start-resolution.d.ts +48 -1
  38. package/dist/turn-start-resolution.d.ts.map +1 -1
  39. package/dist/unknown-record.d.ts +8 -0
  40. package/dist/unknown-record.d.ts.map +1 -0
  41. package/package.json +2 -2
  42. package/src/actor.ts +8 -10
  43. package/src/domain-model.ts +511 -80
  44. package/src/index.ts +18 -38
  45. package/src/launcher-recent-values.ts +11 -11
  46. package/src/markdown-normalize.ts +3 -0
  47. package/src/number-normalize.ts +11 -0
  48. package/src/process-definitions.ts +37 -8
  49. package/src/process-model-config.ts +27 -1
  50. package/src/process-row-slots.ts +14 -0
  51. package/src/process-state-json.ts +13 -0
  52. package/src/repo-locator.ts +10 -6
  53. package/src/semantic-entry-refs.ts +40 -22
  54. package/src/startup-observation.ts +106 -0
  55. package/src/string-normalize.ts +38 -0
  56. package/src/turn-acceptance.ts +2 -0
  57. package/src/turn-annotations.ts +34 -21
  58. package/src/turn-recovery.ts +38 -8
  59. package/src/turn-start-resolution.ts +70 -30
  60. package/src/unknown-record.ts +18 -0
  61. package/dist/actor.js +0 -62
  62. package/dist/actor.js.map +0 -1
  63. package/dist/domain-model.js +0 -169
  64. package/dist/domain-model.js.map +0 -1
  65. package/dist/launcher-recent-values.js +0 -60
  66. package/dist/launcher-recent-values.js.map +0 -1
  67. package/dist/markdown-normalize.js +0 -14
  68. package/dist/markdown-normalize.js.map +0 -1
  69. package/dist/process-definitions.js +0 -2
  70. package/dist/process-definitions.js.map +0 -1
  71. package/dist/process-model-config.js +0 -68
  72. package/dist/process-model-config.js.map +0 -1
  73. package/dist/process-row-slots.js +0 -45
  74. package/dist/process-row-slots.js.map +0 -1
  75. package/dist/process-state-json.js +0 -102
  76. package/dist/process-state-json.js.map +0 -1
  77. package/dist/repo-locator.js +0 -82
  78. package/dist/repo-locator.js.map +0 -1
  79. package/dist/semantic-entry-refs.js +0 -79
  80. package/dist/semantic-entry-refs.js.map +0 -1
  81. package/dist/string-normalize.js +0 -28
  82. package/dist/string-normalize.js.map +0 -1
  83. package/dist/turn-acceptance.js +0 -2
  84. package/dist/turn-acceptance.js.map +0 -1
  85. package/dist/turn-annotations.js +0 -49
  86. package/dist/turn-annotations.js.map +0 -1
  87. package/dist/turn-progress.d.ts +0 -4
  88. package/dist/turn-progress.d.ts.map +0 -1
  89. package/dist/turn-progress.js +0 -10
  90. package/dist/turn-progress.js.map +0 -1
  91. package/dist/turn-recovery.js +0 -70
  92. package/dist/turn-recovery.js.map +0 -1
  93. package/dist/turn-start-resolution.js +0 -97
  94. package/dist/turn-start-resolution.js.map +0 -1
  95. package/src/turn-progress.ts +0 -15
@@ -1,3 +1,7 @@
1
+ import { trimToNull } from "./string-normalize.js";
2
+ import { asUnknownRecord, isUnknownRecord } from "./unknown-record.js";
3
+
4
+ /** @public */
1
5
  export const PROCESS_SEMANTIC_ENTRY_REF_KEYS = [
2
6
  "plan",
3
7
  "review",
@@ -5,22 +9,41 @@ export const PROCESS_SEMANTIC_ENTRY_REF_KEYS = [
5
9
  "rootEntry",
6
10
  ] as const;
7
11
 
12
+ /** @public */
8
13
  export type ProcessSemanticEntryRefKey = (typeof PROCESS_SEMANTIC_ENTRY_REF_KEYS)[number];
9
14
 
15
+ /** @internal */
10
16
  export interface SemanticEntryRef {
17
+ /** @internal */
11
18
  entryId: string;
19
+ /** @internal */
12
20
  turnRecordId: string | null;
13
21
  }
14
22
 
23
+ /** @internal */
24
+ export function areSemanticEntryRefsEqual(
25
+ left: SemanticEntryRef | null | undefined,
26
+ right: SemanticEntryRef | null | undefined,
27
+ ): boolean {
28
+ return (
29
+ (left?.entryId ?? null) === (right?.entryId ?? null) &&
30
+ (left?.turnRecordId ?? null) === (right?.turnRecordId ?? null)
31
+ );
32
+ }
33
+
34
+ /** @internal */
15
35
  export type ProcessProductRef = SemanticEntryRef;
36
+ /** @internal */
16
37
  export type ProcessProductRefs = Record<string, ProcessProductRef>;
17
38
 
18
39
  const PRODUCT_NAME_PATTERN = /^[a-z][a-z0-9-]*$/;
19
40
 
41
+ /** @internal */
20
42
  export function isValidProcessProductName(value: string): boolean {
21
43
  return PRODUCT_NAME_PATTERN.test(value);
22
44
  }
23
45
 
46
+ /** @internal */
24
47
  export function assertValidProcessProductName(value: string): void {
25
48
  if (!isValidProcessProductName(value)) {
26
49
  throw new Error(
@@ -40,40 +63,37 @@ export function assertValidProcessProductName(value: string): void {
40
63
  * - `review` during successful review turns; it remains the current review until
41
64
  * another review turn replaces it
42
65
  */
66
+ /** @internal */
43
67
  export interface ProcessSemanticEntryRefs {
68
+ /** @internal */
44
69
  plan: SemanticEntryRef | null;
70
+ /** @internal */
45
71
  review: SemanticEntryRef | null;
72
+ /** @internal */
46
73
  currentPrimaryPathLeaf: SemanticEntryRef | null;
74
+ /** @internal */
47
75
  rootEntry: SemanticEntryRef | null;
48
76
  }
49
77
 
50
- function parseSemanticEntryId(value: unknown): string | null {
51
- if (typeof value !== "string") {
52
- return null;
53
- }
54
- const trimmed = value.trim();
55
- return trimmed.length > 0 ? trimmed : null;
56
- }
57
-
78
+ /** @internal */
58
79
  export function isProcessSemanticEntryRefKey(value: string): value is ProcessSemanticEntryRefKey {
59
80
  return (PROCESS_SEMANTIC_ENTRY_REF_KEYS as readonly string[]).includes(value);
60
81
  }
61
82
 
83
+ /** @internal */
62
84
  export function parseSemanticEntryRef(value: unknown): SemanticEntryRef | null {
63
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
64
- return null;
65
- }
66
- const record = value as Record<string, unknown>;
67
- const entryId = parseSemanticEntryId(record.entryId);
85
+ const record = asUnknownRecord(value);
86
+ const entryId = trimToNull(record?.entryId);
68
87
  if (!entryId) {
69
88
  return null;
70
89
  }
71
90
  return {
72
91
  entryId,
73
- turnRecordId: parseSemanticEntryId(record.turnRecordId),
92
+ turnRecordId: trimToNull(record?.turnRecordId),
74
93
  };
75
94
  }
76
95
 
96
+ /** @internal */
77
97
  export function createEmptyProcessSemanticEntryRefs(): ProcessSemanticEntryRefs {
78
98
  return {
79
99
  plan: null,
@@ -83,16 +103,16 @@ export function createEmptyProcessSemanticEntryRefs(): ProcessSemanticEntryRefs
83
103
  };
84
104
  }
85
105
 
106
+ /** @internal */
86
107
  export function createEmptyProcessProductRefs(): ProcessProductRefs {
87
108
  return {};
88
109
  }
89
110
 
111
+ /** @internal */
90
112
  export function parseProcessProductRefs(value: unknown): ProcessProductRefs {
91
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
92
- return createEmptyProcessProductRefs();
93
- }
113
+ if (!isUnknownRecord(value)) return createEmptyProcessProductRefs();
94
114
  const refs: ProcessProductRefs = {};
95
- for (const [name, rawRef] of Object.entries(value as Record<string, unknown>)) {
115
+ for (const [name, rawRef] of Object.entries(value)) {
96
116
  if (!isValidProcessProductName(name)) {
97
117
  continue;
98
118
  }
@@ -104,11 +124,9 @@ export function parseProcessProductRefs(value: unknown): ProcessProductRefs {
104
124
  return refs;
105
125
  }
106
126
 
127
+ /** @internal */
107
128
  export function parseProcessSemanticEntryRefs(value: unknown): ProcessSemanticEntryRefs {
108
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
109
- return createEmptyProcessSemanticEntryRefs();
110
- }
111
- const record = value as Record<string, unknown>;
129
+ const record = asUnknownRecord(value) ?? {};
112
130
  return {
113
131
  plan: parseSemanticEntryRef(record.plan),
114
132
  review: parseSemanticEntryRef(record.review),
@@ -0,0 +1,106 @@
1
+ /** First server receipt of a physical worker startup milestone. @internal */
2
+ export type StartupMilestone =
3
+ | "pvc_requested"
4
+ | "pvc_acknowledged"
5
+ | "pvc_bound"
6
+ | "pod_requested"
7
+ | "pod_acknowledged"
8
+ | "pod_created"
9
+ | "pod_scheduled"
10
+ | "image_pull_started"
11
+ | "image_pull_finished"
12
+ | "image_cached"
13
+ | "container_started"
14
+ | "prompt_started"
15
+ | "first_text";
16
+ /** @internal */
17
+ export interface StartupObservation {
18
+ /** @internal */
19
+ workerLeaseId: string;
20
+ /** @internal */
21
+ milestone: StartupMilestone;
22
+ /** @internal */
23
+ observedAt: string;
24
+ /** @internal */
25
+ sourceAt: string | null;
26
+ /** @internal */
27
+ sourceKind: "server" | "kubernetes_status" | "kubernetes_event";
28
+ /** @internal */
29
+ objectUid: string | null;
30
+ /** @internal */
31
+ turnRecordId: string | null;
32
+ /** Sampling lower bound, never an exact binding timestamp. @internal */
33
+ notBefore: string | null;
34
+ /** @internal */
35
+ metadata: {
36
+ /** @internal */
37
+ node?: string;
38
+ /** @internal */
39
+ imageId?: string;
40
+ /** @internal */
41
+ image?: string;
42
+ /** @internal */
43
+ storageClass?: string;
44
+ /** @internal */
45
+ cpu?: string;
46
+ /** @internal */
47
+ memory?: string;
48
+ /** @internal */
49
+ namespace?: string;
50
+ /** @internal */
51
+ podName?: string;
52
+ /** @internal */
53
+ precision?: "milliseconds" | "microseconds" | "seconds" | "sampling_interval";
54
+ };
55
+ }
56
+ /** @internal */
57
+ export interface StartupInterval {
58
+ /** @internal */
59
+ start: string | null;
60
+ /** @internal */
61
+ end: string | null;
62
+ /** @internal */
63
+ durationMs: number | null;
64
+ /** @internal */
65
+ status: "available" | "missing" | "invalid_order";
66
+ /** @internal */
67
+ clock: "server" | "kubernetes";
68
+ }
69
+ /** @internal */
70
+ export function startupInterval(
71
+ start: string | null | undefined,
72
+ end: string | null | undefined,
73
+ clock: StartupInterval["clock"] = "server",
74
+ ): StartupInterval {
75
+ const delta = start && end ? Date.parse(end) - Date.parse(start) : NaN;
76
+ const status =
77
+ !start || !end
78
+ ? "missing"
79
+ : !Number.isFinite(delta) || delta < 0
80
+ ? "invalid_order"
81
+ : "available";
82
+ return {
83
+ start: start ?? null,
84
+ end: end ?? null,
85
+ durationMs: status === "available" ? delta : null,
86
+ status,
87
+ clock,
88
+ };
89
+ }
90
+ /** @internal */
91
+ export interface PhysicalWorkerStart {
92
+ /** @internal */
93
+ workerLeaseId: string;
94
+ /** @internal */
95
+ workerId: string;
96
+ /** @internal */
97
+ turnStartRecordId: string | null;
98
+ /** @internal */
99
+ turnRecordId: string | null;
100
+ /** @internal */
101
+ state: string;
102
+ /** @internal */
103
+ observations: StartupObservation[];
104
+ /** @internal */
105
+ intervals: Record<string, StartupInterval>;
106
+ }
@@ -1,12 +1,28 @@
1
+ /** @internal */
2
+ export function toErrorMessage(error: unknown): string {
3
+ if (error instanceof Error && error.message.trim() !== "") {
4
+ return error.message;
5
+ }
6
+ return String(error);
7
+ }
8
+
9
+ /** Return non-blank strings verbatim; unlike trimToNull, preserve surrounding whitespace. @internal */
10
+ export function readNonBlankString(value: unknown): string | null {
11
+ return typeof value === "string" && value.trim() !== "" ? value : null;
12
+ }
13
+
14
+ /** @internal */
1
15
  export function trimString(value: unknown): string {
2
16
  return typeof value === "string" ? value.trim() : "";
3
17
  }
4
18
 
19
+ /** @internal */
5
20
  export function trimToNull(value: unknown): string | null {
6
21
  const trimmed = trimString(value);
7
22
  return trimmed === "" ? null : trimmed;
8
23
  }
9
24
 
25
+ /** @internal */
10
26
  export function normalizeStringArray(value: unknown): string[] {
11
27
  if (!Array.isArray(value)) {
12
28
  return [];
@@ -21,6 +37,7 @@ export function normalizeStringArray(value: unknown): string[] {
21
37
  * flow diagrams) must route through this so the translation lives in one place
22
38
  * and can later be swapped for a localized lookup.
23
39
  */
40
+ /** @internal */
24
41
  export function humanizeProcessLabel(identifier: unknown): string {
25
42
  return trimString(identifier)
26
43
  .replace(/[_-]+/g, " ")
@@ -28,3 +45,24 @@ export function humanizeProcessLabel(identifier: unknown): string {
28
45
  .replace(/\b\w/g, (letter) => letter.toUpperCase())
29
46
  .trim();
30
47
  }
48
+
49
+ const IDENTIFIER_WORD_LABELS: Record<string, string> = {
50
+ api: "API",
51
+ id: "ID",
52
+ llm: "LLM",
53
+ mr: "MR",
54
+ pi: "Pi",
55
+ ui: "UI",
56
+ };
57
+
58
+ /** Display an identifier with known acronyms, preserving the remaining spelling. @internal */
59
+ export function formatProcessIdentifier(value: string): string {
60
+ return value
61
+ .split(/[_-]+/)
62
+ .filter((part) => part.length > 0)
63
+ .map((part) => {
64
+ const normalizedPart = part.toLowerCase();
65
+ return IDENTIFIER_WORD_LABELS[normalizedPart] ?? part.charAt(0).toUpperCase() + part.slice(1);
66
+ })
67
+ .join(" ");
68
+ }
@@ -1,3 +1,5 @@
1
+ /** @public */
1
2
  export const TURN_ACCEPTANCE_STATES = ["accepted", "requires_changes", "neutral"] as const;
2
3
 
4
+ /** @public */
3
5
  export type TurnAcceptanceState = (typeof TURN_ACCEPTANCE_STATES)[number];
@@ -2,92 +2,105 @@ import {
2
2
  isProcessSemanticEntryRefKey,
3
3
  type ProcessSemanticEntryRefKey,
4
4
  } from "./semantic-entry-refs.js";
5
+ import { trimToNull } from "./string-normalize.js";
5
6
 
7
+ /** @internal */
6
8
  export const TURN_ANNOTATION_REFERENCE_KINDS = [
7
9
  "turn_record",
8
10
  "entry",
9
11
  "semantic_entry_ref",
10
12
  ] as const;
11
13
 
14
+ /** @internal */
12
15
  export type TurnAnnotationReferenceKind = (typeof TURN_ANNOTATION_REFERENCE_KINDS)[number];
16
+ /** @internal */
13
17
  export type TurnAnnotationReferenceRole = string;
14
18
 
19
+ /** @internal */
15
20
  export interface TurnRecordAnnotationReference {
21
+ /** @internal */
16
22
  kind: "turn_record";
23
+ /** @internal */
17
24
  turnRecordId: string;
25
+ /** @internal */
18
26
  role: TurnAnnotationReferenceRole | null;
19
27
  }
20
28
 
29
+ /** @internal */
21
30
  export interface EntryTurnAnnotationReference {
31
+ /** @internal */
22
32
  kind: "entry";
33
+ /** @internal */
23
34
  entryId: string;
35
+ /** @internal */
24
36
  role: TurnAnnotationReferenceRole | null;
25
37
  }
26
38
 
39
+ /** @internal */
27
40
  export interface SemanticEntryRefTurnAnnotationReference {
41
+ /** @internal */
28
42
  kind: "semantic_entry_ref";
43
+ /** @internal */
29
44
  ref: ProcessSemanticEntryRefKey;
45
+ /** @internal */
30
46
  role: TurnAnnotationReferenceRole | null;
31
47
  }
32
48
 
49
+ /** @internal */
33
50
  export type TurnAnnotationReference =
34
51
  | TurnRecordAnnotationReference
35
52
  | EntryTurnAnnotationReference
36
53
  | SemanticEntryRefTurnAnnotationReference;
37
54
 
55
+ /** @internal */
38
56
  export interface ProcessTurnAnnotation {
57
+ /** @internal */
39
58
  id: string;
59
+ /** @internal */
40
60
  instanceId: string;
61
+ /** @internal */
41
62
  annotationType: string;
63
+ /** @internal */
42
64
  annotationKey: string | null;
65
+ /** @internal */
43
66
  references: TurnAnnotationReference[];
67
+ /** @internal */
44
68
  payload: Record<string, unknown>;
69
+ /** @internal */
45
70
  createdAt: string;
71
+ /** @internal */
46
72
  updatedAt: string;
47
73
  }
48
74
 
49
- function parseNonEmptyString(value: unknown): string | null {
50
- if (typeof value !== "string") {
51
- return null;
52
- }
53
- const trimmed = value.trim();
54
- return trimmed.length > 0 ? trimmed : null;
55
- }
56
-
57
- function parseTurnAnnotationReferenceRole(value: unknown): TurnAnnotationReferenceRole | null {
58
- return parseNonEmptyString(value);
59
- }
60
-
75
+ /** @internal */
61
76
  export function parseTurnAnnotationReference(value: unknown): TurnAnnotationReference | null {
62
77
  if (typeof value !== "object" || value === null || Array.isArray(value)) {
63
78
  return null;
64
79
  }
65
80
  const record = value as Record<string, unknown>;
66
- const kind = parseNonEmptyString(record.kind);
67
- const role = parseTurnAnnotationReferenceRole(record.role);
68
- if (!kind) {
69
- return null;
70
- }
81
+ const kind = trimToNull(record.kind);
82
+ const role = trimToNull(record.role);
71
83
  if (kind === "turn_record") {
72
- const turnRecordId = parseNonEmptyString(record.turnRecordId);
84
+ const turnRecordId = trimToNull(record.turnRecordId);
73
85
  return turnRecordId ? { kind, turnRecordId, role } : null;
74
86
  }
75
87
  if (kind === "entry") {
76
- const entryId = parseNonEmptyString(record.entryId);
88
+ const entryId = trimToNull(record.entryId);
77
89
  return entryId ? { kind, entryId, role } : null;
78
90
  }
79
91
  if (kind === "semantic_entry_ref") {
80
- const ref = parseNonEmptyString(record.ref);
92
+ const ref = trimToNull(record.ref);
81
93
  return ref && isProcessSemanticEntryRefKey(ref) ? { kind, ref, role } : null;
82
94
  }
83
95
  return null;
84
96
  }
85
97
 
98
+ /** @internal */
86
99
  export function parseTurnAnnotationReferences(value: unknown): TurnAnnotationReference[] {
87
100
  if (!Array.isArray(value)) {
88
101
  return [];
89
102
  }
90
103
  return value
91
- .map((reference) => parseTurnAnnotationReference(reference))
104
+ .map(parseTurnAnnotationReference)
92
105
  .filter((reference): reference is TurnAnnotationReference => reference !== null);
93
106
  }
@@ -1,7 +1,10 @@
1
1
  import { normalizeStringArray, trimToNull } from "./string-normalize.js";
2
+ import { isUnknownRecord as isRecord } from "./unknown-record.js";
2
3
 
4
+ /** @internal */
3
5
  export const DEFAULT_CONTINUE_PROMPT = "continue";
4
6
 
7
+ /** @internal */
5
8
  export const FAILED_TURN_RECOVERY_CODES = [
6
9
  "generic_continue",
7
10
  "missing_outcome_tool",
@@ -9,20 +12,32 @@ export const FAILED_TURN_RECOVERY_CODES = [
9
12
  "missing_required_tool_calls",
10
13
  ] as const;
11
14
 
15
+ /** @internal */
12
16
  export type FailedTurnRecoveryCode = (typeof FAILED_TURN_RECOVERY_CODES)[number];
13
17
 
18
+ /** @internal */
14
19
  export interface FailedTurnRecoveryContext {
20
+ /** @internal */
15
21
  strategy: "continue";
22
+ /** @internal */
16
23
  suggestedContinuePrompt: string;
24
+ /** @internal */
17
25
  failureCode: FailedTurnRecoveryCode;
26
+ /** @internal */
18
27
  missingToolNames?: string[];
19
28
  }
20
29
 
30
+ /** @internal */
21
31
  export type FailedTurnRecoveryOverrides = Partial<Omit<FailedTurnRecoveryContext, "strategy">>;
22
32
 
33
+ /** @internal */
23
34
  export const FAILED_TURN_RECOVERY_METADATA_KEY = "failedTurnRecovery";
35
+ /** @internal */
24
36
  export const CONTINUE_PROMPT_METADATA_KEY = "continuePrompt";
37
+ const TERMINAL_OUTCOME_RECORDING_FAILURE_PREFIX =
38
+ "Server could not durably record worker turn outcome:";
25
39
 
40
+ /** @internal */
26
41
  export function isFailedTurnRecoveryCode(value: string): value is FailedTurnRecoveryCode {
27
42
  return (FAILED_TURN_RECOVERY_CODES as readonly string[]).includes(value);
28
43
  }
@@ -32,14 +47,9 @@ function normalizeUniqueStringArray(value: unknown): string[] | undefined {
32
47
  return normalized.length > 0 ? [...new Set(normalized)] : undefined;
33
48
  }
34
49
 
35
- function isRecord(value: unknown): value is Record<string, unknown> {
36
- return typeof value === "object" && value !== null && !Array.isArray(value);
37
- }
38
-
39
- export function normalizeContinuePrompt(value: unknown): string | null {
40
- return trimToNull(value);
41
- }
50
+ export { trimToNull as normalizeContinuePrompt };
42
51
 
52
+ /** @internal */
43
53
  export function createGenericFailedTurnRecoveryContext(
44
54
  overrides: FailedTurnRecoveryOverrides = {},
45
55
  ): FailedTurnRecoveryContext {
@@ -51,18 +61,38 @@ export function createGenericFailedTurnRecoveryContext(
51
61
  };
52
62
  }
53
63
 
64
+ /** @internal */
65
+ export function inferTerminalRecordingFailedTurnRecoveryContext(input: {
66
+ /** @internal */
67
+ errorSummary?: string | null;
68
+ /** @internal */
69
+ errorClass?: string | null;
70
+ }): FailedTurnRecoveryContext | null {
71
+ if (
72
+ input.errorClass !== "infrastructure" ||
73
+ !input.errorSummary?.startsWith(TERMINAL_OUTCOME_RECORDING_FAILURE_PREFIX)
74
+ ) {
75
+ return null;
76
+ }
77
+ return createGenericFailedTurnRecoveryContext();
78
+ }
79
+
80
+ /** @internal */
54
81
  export function buildFailedTurnRecoveryMetadata(
55
82
  turnRecordId: string,
56
83
  context: FailedTurnRecoveryOverrides = {},
57
84
  ) {
58
85
  return {
86
+ /** @internal */
59
87
  [FAILED_TURN_RECOVERY_METADATA_KEY]: {
88
+ /** @internal */
60
89
  turnRecordId,
61
90
  ...createGenericFailedTurnRecoveryContext(context),
62
91
  },
63
92
  };
64
93
  }
65
94
 
95
+ /** @internal */
66
96
  export function readFailedTurnRecoveryContext(
67
97
  metadata: Record<string, unknown> | null | undefined,
68
98
  expectedTurnRecordId?: string | null,
@@ -83,7 +113,7 @@ export function readFailedTurnRecoveryContext(
83
113
  if (raw.strategy !== "continue") {
84
114
  return null;
85
115
  }
86
- const suggestedContinuePrompt = normalizeContinuePrompt(raw.suggestedContinuePrompt);
116
+ const suggestedContinuePrompt = trimToNull(raw.suggestedContinuePrompt);
87
117
  const failureCode = trimToNull(raw.failureCode);
88
118
  if (!suggestedContinuePrompt || !failureCode || !isFailedTurnRecoveryCode(failureCode)) {
89
119
  return null;