@leitwerk-dev/domain 0.1.8 → 0.2.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.
Files changed (72) hide show
  1. package/dist/actor.d.ts.map +1 -1
  2. package/dist/domain-model.d.ts +87 -4
  3. package/dist/domain-model.d.ts.map +1 -1
  4. package/dist/index.d.ts +7 -5
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +985 -15
  7. package/dist/index.js.map +1 -1
  8. package/dist/launcher-recent-values.d.ts.map +1 -1
  9. package/dist/number-normalize.d.ts +3 -0
  10. package/dist/number-normalize.d.ts.map +1 -0
  11. package/dist/repo-locator.d.ts.map +1 -1
  12. package/dist/semantic-entry-refs.d.ts +1 -0
  13. package/dist/semantic-entry-refs.d.ts.map +1 -1
  14. package/dist/startup-observation.d.ts +42 -0
  15. package/dist/startup-observation.d.ts.map +1 -0
  16. package/dist/string-normalize.d.ts +5 -0
  17. package/dist/string-normalize.d.ts.map +1 -1
  18. package/dist/turn-annotations.d.ts.map +1 -1
  19. package/dist/turn-recovery.d.ts +6 -1
  20. package/dist/turn-recovery.d.ts.map +1 -1
  21. package/dist/turn-start-resolution.d.ts.map +1 -1
  22. package/dist/unknown-record.d.ts +6 -0
  23. package/dist/unknown-record.d.ts.map +1 -0
  24. package/package.json +2 -2
  25. package/src/actor.ts +4 -8
  26. package/src/domain-model.ts +115 -17
  27. package/src/index.ts +21 -8
  28. package/src/launcher-recent-values.ts +6 -11
  29. package/src/number-normalize.ts +11 -0
  30. package/src/repo-locator.ts +2 -6
  31. package/src/semantic-entry-refs.ts +19 -22
  32. package/src/startup-observation.ts +73 -0
  33. package/src/string-normalize.ts +33 -0
  34. package/src/turn-annotations.ts +7 -21
  35. package/src/turn-recovery.ts +18 -8
  36. package/src/turn-start-resolution.ts +11 -25
  37. package/src/unknown-record.ts +16 -0
  38. package/dist/actor.js +0 -62
  39. package/dist/actor.js.map +0 -1
  40. package/dist/domain-model.js +0 -169
  41. package/dist/domain-model.js.map +0 -1
  42. package/dist/launcher-recent-values.js +0 -60
  43. package/dist/launcher-recent-values.js.map +0 -1
  44. package/dist/markdown-normalize.js +0 -14
  45. package/dist/markdown-normalize.js.map +0 -1
  46. package/dist/process-definitions.js +0 -2
  47. package/dist/process-definitions.js.map +0 -1
  48. package/dist/process-model-config.js +0 -68
  49. package/dist/process-model-config.js.map +0 -1
  50. package/dist/process-row-slots.js +0 -45
  51. package/dist/process-row-slots.js.map +0 -1
  52. package/dist/process-state-json.js +0 -102
  53. package/dist/process-state-json.js.map +0 -1
  54. package/dist/repo-locator.js +0 -82
  55. package/dist/repo-locator.js.map +0 -1
  56. package/dist/review-subject.d.ts +0 -9
  57. package/dist/review-subject.d.ts.map +0 -1
  58. package/dist/review-subject.js +0 -18
  59. package/dist/review-subject.js.map +0 -1
  60. package/dist/semantic-entry-refs.js +0 -79
  61. package/dist/semantic-entry-refs.js.map +0 -1
  62. package/dist/string-normalize.js +0 -28
  63. package/dist/string-normalize.js.map +0 -1
  64. package/dist/turn-acceptance.js +0 -2
  65. package/dist/turn-acceptance.js.map +0 -1
  66. package/dist/turn-annotations.js +0 -49
  67. package/dist/turn-annotations.js.map +0 -1
  68. package/dist/turn-recovery.js +0 -70
  69. package/dist/turn-recovery.js.map +0 -1
  70. package/dist/turn-start-resolution.js +0 -97
  71. package/dist/turn-start-resolution.js.map +0 -1
  72. package/src/review-subject.ts +0 -26
@@ -1,5 +1,6 @@
1
1
  import type { RepoLocatorKind } from "./repo-locator.js";
2
2
  import type { ProcessSemanticEntryRefKey } from "./semantic-entry-refs.js";
3
+ import { trimToNull } from "./string-normalize.js";
3
4
  import type { FailedTurnRecoveryContext } from "./turn-recovery.js";
4
5
  import type { ProcessTurnStartTarget } from "./turn-start-resolution.js";
5
6
 
@@ -73,6 +74,39 @@ export interface QuestionAnswerDraft {
73
74
  comment: string;
74
75
  }
75
76
 
77
+ export interface ProcessRelation {
78
+ parentInstanceId: string;
79
+ childInstanceId: string;
80
+ kind: "derived";
81
+ purpose: string;
82
+ createdAt: string;
83
+ createdBy: Actor;
84
+ }
85
+
86
+ export type ToolApprovalStatus = "open" | "accepted" | "feedback" | "declined" | "cancelled";
87
+
88
+ export interface ProcessToolApprovalDestination {
89
+ id: string;
90
+ displayName: string;
91
+ group?: string;
92
+ description?: string;
93
+ }
94
+
95
+ export interface ProcessToolApprovalRequest {
96
+ id: string;
97
+ instanceId: string;
98
+ turnRecordId: string;
99
+ toolCallId: string;
100
+ toolName: string;
101
+ arguments: Record<string, unknown>;
102
+ destination: ProcessToolApprovalDestination | null;
103
+ status: ToolApprovalStatus;
104
+ requestedAt: string;
105
+ resolvedAt: string | null;
106
+ resolvedBy: Actor | null;
107
+ feedback: string | null;
108
+ }
109
+
76
110
  export interface ProcessQuestionRequest {
77
111
  id: string;
78
112
  instanceId: string;
@@ -88,10 +122,11 @@ export interface ProcessQuestionRequest {
88
122
  }
89
123
 
90
124
  function requiredTrimmedString(value: unknown, field: string): string {
91
- if (typeof value !== "string" || value.trim() === "") {
125
+ const trimmed = trimToNull(value);
126
+ if (!trimmed) {
92
127
  throw new Error(`${field} must be a non-empty string`);
93
128
  }
94
- return value.trim();
129
+ return trimmed;
95
130
  }
96
131
 
97
132
  /** Normalize an LLM-authored request into immutable, position-stable identities. */
@@ -133,10 +168,7 @@ export function normalizeAskQuestionsInput(value: unknown): NormalizedQuestion[]
133
168
  return {
134
169
  id: `${questionId}_option_${optionIndex + 1}`,
135
170
  label,
136
- details:
137
- typeof option.details === "string" && option.details.trim() !== ""
138
- ? option.details.trim()
139
- : null,
171
+ details: trimToNull(option.details),
140
172
  };
141
173
  });
142
174
  return { id: questionId, question, selection: candidate.selection, options };
@@ -240,17 +272,12 @@ export function isTurnFailureCode(value: string): value is TurnFailureCode {
240
272
  /** Extension-defined stable turn identifier. */
241
273
  export type TurnId = string;
242
274
 
243
- export const PROCESS_TURN_TYPES = [
244
- "llm",
245
- "human",
246
- "external",
247
- "automatic",
248
- "server_automatic",
249
- ] as const;
275
+ export const PROCESS_TURN_TYPES = ["llm", "human", "external", "automatic"] as const;
250
276
 
251
277
  export type ProcessTurnType = (typeof PROCESS_TURN_TYPES)[number];
252
278
 
253
279
  export interface TurnOutcomePayload {
280
+ resultSummary?: string;
254
281
  instanceId: string;
255
282
  turnRecordId: string;
256
283
  turnId: TurnId;
@@ -352,10 +379,7 @@ export interface FutureExecutionBlockReason {
352
379
  export type JsonObject = Record<string, unknown>;
353
380
 
354
381
  /** The one durable technical execution currently owned by a process. */
355
- export type CurrentExecutionRef =
356
- | { kind: "worker_start"; id: string }
357
- | { kind: "server_turn"; id: string }
358
- | null;
382
+ export type CurrentExecutionRef = { kind: "worker_start"; id: string } | null;
359
383
 
360
384
  /** Immutable non-secret inputs resolved for a worker-owned turn start. */
361
385
  export type ResolvedTurnStart =
@@ -575,6 +599,45 @@ export interface ProcessInput {
575
599
  consumedAt: string | null;
576
600
  }
577
601
 
602
+ export type LaunchOrigin = "ui" | "programmatic" | "watcher" | "scheduled" | "startup_retry";
603
+ export type LaunchRunStatus =
604
+ | "preparing"
605
+ | "process_created"
606
+ | "starting"
607
+ | "completed"
608
+ | "failed"
609
+ | "cancelled";
610
+ export type LaunchChecklistStepStatus =
611
+ | "pending"
612
+ | "in_progress"
613
+ | "completed"
614
+ | "failed"
615
+ | "skipped";
616
+
617
+ /** Presentation-safe durable progress for one process launch attempt. */
618
+ export interface LaunchChecklistStep {
619
+ id: string;
620
+ label: string;
621
+ status: LaunchChecklistStepStatus;
622
+ safeSummary?: string;
623
+ startedAt?: string;
624
+ completedAt?: string;
625
+ }
626
+
627
+ /** Durable launch attempt. Secrets and runner identifiers never belong here. */
628
+ export interface LaunchRun {
629
+ id: string;
630
+ launcherId: string | null;
631
+ origin: LaunchOrigin;
632
+ instanceId: string | null;
633
+ status: LaunchRunStatus;
634
+ steps: LaunchChecklistStep[];
635
+ createdAt: string;
636
+ updatedAt: string;
637
+ completedAt: string | null;
638
+ revision: number;
639
+ }
640
+
578
641
  export type FutureExecutionKind = "launch" | "action";
579
642
  export type FutureExecutionScheduleKind = "once" | "cron";
580
643
 
@@ -601,6 +664,8 @@ export interface FutureExecution {
601
664
  }
602
665
 
603
666
  export interface ProcessEvent {
667
+ /** Persisted ingestion order. Absent only on legacy in-memory fixtures. */
668
+ eventSequence?: number;
604
669
  id: string;
605
670
  instanceId: string;
606
671
  eventType: string;
@@ -628,6 +693,31 @@ export interface ProcessLeafOutcomeSnapshot {
628
693
 
629
694
  export type ProcessTurnRecordStatus = "running" | "succeeded" | "failed" | "superseded";
630
695
 
696
+ export type TurnProgressStepStatus = "incomplete" | "in_progress" | "completed" | "failed";
697
+
698
+ export interface TurnProgressStep {
699
+ id: string;
700
+ label: string;
701
+ status: TurnProgressStepStatus;
702
+ detail?: string | null;
703
+ }
704
+
705
+ export interface TurnProgressLink {
706
+ id: string;
707
+ label: string;
708
+ url: string;
709
+ kind?: "pull_request" | "merge_request" | "commit" | "pipeline" | "other";
710
+ }
711
+
712
+ /** A complete operator-facing snapshot. Reporters replace, rather than patch, this value. */
713
+ export interface TurnProgressReport {
714
+ title: string;
715
+ /** Attempt-specific progress or waiting explanation; not an operation receipt. */
716
+ summary?: string;
717
+ steps: TurnProgressStep[];
718
+ links?: TurnProgressLink[];
719
+ }
720
+
631
721
  /**
632
722
  * Structural tree position for a turn record.
633
723
  * - `primary`: continue the primary path from the current leaf
@@ -666,6 +756,8 @@ export interface WorkerLease {
666
756
  id: string;
667
757
  instanceId: string;
668
758
  workerId: string;
759
+ /** Worker start this physical lease was created to execute. */
760
+ turnStartRecordId?: string | null;
669
761
  /** Durable server-owned supervision state for the current worker lease. */
670
762
  state: WorkerState;
671
763
  /** Server epoch this lease belongs to, used to reject stale snapshot writes. */
@@ -681,6 +773,12 @@ export interface WorkerLease {
681
773
  /** Updated from heartbeats for liveness/recovery diagnostics. */
682
774
  lastHeartbeatAt: string | null;
683
775
  startedAt: string;
776
+ /** First valid server-observed worker handshake. */
777
+ connectedAt?: string | null;
778
+ /** First valid server-observed workspace preparation progress. */
779
+ workspacePreparationStartedAt?: string | null;
780
+ /** First accepted server-observed worker readiness. */
781
+ readyAt?: string | null;
684
782
  /** Set once the lease is no longer active for supervision. */
685
783
  exitedAt: string | null;
686
784
  }
package/src/index.ts CHANGED
@@ -18,6 +18,11 @@ export type {
18
18
  InputKind,
19
19
  InputSource,
20
20
  JsonObject,
21
+ LaunchChecklistStep,
22
+ LaunchChecklistStepStatus,
23
+ LaunchOrigin,
24
+ LaunchRun,
25
+ LaunchRunStatus,
21
26
  LlmContextMode,
22
27
  ModelPolicyFailureCode,
23
28
  ModelSelectionKind,
@@ -35,18 +40,26 @@ export type {
35
40
  ProcessLifecycleStatus,
36
41
  ProcessProject,
37
42
  ProcessQuestionRequest,
43
+ ProcessRelation,
38
44
  ProcessSelectedTurnModelSource,
45
+ ProcessToolApprovalDestination,
46
+ ProcessToolApprovalRequest,
39
47
  ProcessTurnRecord,
40
48
  ProcessTurnRecordPathType,
41
49
  ProcessTurnRecordStatus,
42
50
  ProcessTurnType,
43
51
  QuestionAnswerDraft,
44
52
  ResolvedTurnStart,
53
+ ToolApprovalStatus,
45
54
  TransitionTrigger,
46
55
  TurnFailedPayload,
47
56
  TurnFailureCode,
48
57
  TurnId,
49
58
  TurnOutcomePayload,
59
+ TurnProgressLink,
60
+ TurnProgressReport,
61
+ TurnProgressStep,
62
+ TurnProgressStepStatus,
50
63
  TurnStartContinuation,
51
64
  TurnStartKind,
52
65
  TurnStartPreparationFailureCode,
@@ -88,6 +101,7 @@ export {
88
101
  normalizeMarkdownText,
89
102
  normalizeOptionalMarkdown,
90
103
  } from "./markdown-normalize.js";
104
+ export { readFiniteNumber } from "./number-normalize.js";
91
105
  export type {
92
106
  ProcessFlowEdge,
93
107
  ProcessFlowEdgeKind,
@@ -136,14 +150,7 @@ export {
136
150
  type RepoLocatorKind,
137
151
  } from "./repo-locator.js";
138
152
  export {
139
- createReviewSubject,
140
- isReviewSubjectKind,
141
- parseReviewSubject,
142
- REVIEW_SUBJECT_KINDS,
143
- type ReviewSubject,
144
- type ReviewSubjectKind,
145
- } from "./review-subject.js";
146
- export {
153
+ areSemanticEntryRefsEqual,
147
154
  assertValidProcessProductName,
148
155
  createEmptyProcessProductRefs,
149
156
  createEmptyProcessSemanticEntryRefs,
@@ -159,9 +166,13 @@ export {
159
166
  parseSemanticEntryRef,
160
167
  type SemanticEntryRef,
161
168
  } from "./semantic-entry-refs.js";
169
+ export * from "./startup-observation.js";
162
170
  export {
171
+ formatProcessIdentifier,
163
172
  humanizeProcessLabel,
164
173
  normalizeStringArray,
174
+ readNonBlankString,
175
+ toErrorMessage,
165
176
  trimString,
166
177
  trimToNull,
167
178
  } from "./string-normalize.js";
@@ -187,6 +198,7 @@ export {
187
198
  FAILED_TURN_RECOVERY_METADATA_KEY,
188
199
  type FailedTurnRecoveryCode,
189
200
  type FailedTurnRecoveryContext,
201
+ inferTerminalRecordingFailedTurnRecoveryContext,
190
202
  isFailedTurnRecoveryCode,
191
203
  normalizeContinuePrompt,
192
204
  readFailedTurnRecoveryContext,
@@ -208,3 +220,4 @@ export {
208
220
  resolveProcessTurnStartSelection,
209
221
  resolveProcessTurnStartTarget,
210
222
  } from "./turn-start-resolution.js";
223
+ export { asUnknownRecord, copiedUnknownRecordSchema, isUnknownRecord } from "./unknown-record.js";
@@ -28,10 +28,7 @@ export function redactCredentialBearingAbsoluteUrl(value: string): string {
28
28
 
29
29
  export function normalizeLauncherRecentValue(value: unknown): string | null {
30
30
  const normalizedValue = typeof value === "string" ? value.trim() : "";
31
- if (!normalizedValue) {
32
- return null;
33
- }
34
- if (hasCredentialBearingAbsoluteUrl(normalizedValue)) {
31
+ if (!normalizedValue || hasCredentialBearingAbsoluteUrl(normalizedValue)) {
35
32
  return null;
36
33
  }
37
34
  return normalizedValue;
@@ -41,20 +38,18 @@ export function normalizeLauncherRecentValues(
41
38
  values: readonly unknown[],
42
39
  limit = DEFAULT_LAUNCHER_RECENT_VALUE_LIMIT,
43
40
  ): string[] {
44
- const nextValues: string[] = [];
45
- const seen = new Set<string>();
41
+ const nextValues = new Set<string>();
46
42
  for (const value of values) {
47
43
  const normalizedValue = normalizeLauncherRecentValue(value);
48
- if (!normalizedValue || seen.has(normalizedValue)) {
44
+ if (!normalizedValue || nextValues.has(normalizedValue)) {
49
45
  continue;
50
46
  }
51
- seen.add(normalizedValue);
52
- nextValues.push(normalizedValue);
53
- if (nextValues.length >= limit) {
47
+ nextValues.add(normalizedValue);
48
+ if (nextValues.size >= limit) {
54
49
  break;
55
50
  }
56
51
  }
57
- return nextValues;
52
+ return [...nextValues];
58
53
  }
59
54
 
60
55
  export function addLauncherRecentValue(
@@ -0,0 +1,11 @@
1
+ /** Accept finite numbers and non-blank numeric strings, without coercing other values. */
2
+ export function readFiniteNumber(value: unknown): number | null {
3
+ if (typeof value === "number" && Number.isFinite(value)) {
4
+ return value;
5
+ }
6
+ if (typeof value === "string" && value.trim() !== "") {
7
+ const parsed = Number(value);
8
+ return Number.isFinite(parsed) ? parsed : null;
9
+ }
10
+ return null;
11
+ }
@@ -5,10 +5,6 @@ export interface ParsedRepoLocator {
5
5
  value: string;
6
6
  }
7
7
 
8
- function trimRepoLocator(value: string): string {
9
- return value.trim();
10
- }
11
-
12
8
  function isFileUrl(value: string): boolean {
13
9
  try {
14
10
  const url = new URL(value);
@@ -63,7 +59,7 @@ function isRemoteGitUrl(value: string): boolean {
63
59
  }
64
60
 
65
61
  export function detectRepoLocatorKind(value: string): RepoLocatorKind | null {
66
- const trimmed = trimRepoLocator(value);
62
+ const trimmed = value.trim();
67
63
  if (trimmed.length === 0) {
68
64
  return null;
69
65
  }
@@ -80,7 +76,7 @@ export function parseRepoLocator(value: unknown): ParsedRepoLocator | null {
80
76
  if (typeof value !== "string") {
81
77
  return null;
82
78
  }
83
- const trimmed = trimRepoLocator(value);
79
+ const trimmed = value.trim();
84
80
  const kind = detectRepoLocatorKind(trimmed);
85
81
  if (!kind) {
86
82
  return null;
@@ -1,3 +1,6 @@
1
+ import { trimToNull } from "./string-normalize.js";
2
+ import { asUnknownRecord, isUnknownRecord } from "./unknown-record.js";
3
+
1
4
  export const PROCESS_SEMANTIC_ENTRY_REF_KEYS = [
2
5
  "plan",
3
6
  "review",
@@ -12,6 +15,16 @@ export interface SemanticEntryRef {
12
15
  turnRecordId: string | null;
13
16
  }
14
17
 
18
+ export function areSemanticEntryRefsEqual(
19
+ left: SemanticEntryRef | null | undefined,
20
+ right: SemanticEntryRef | null | undefined,
21
+ ): boolean {
22
+ return (
23
+ (left?.entryId ?? null) === (right?.entryId ?? null) &&
24
+ (left?.turnRecordId ?? null) === (right?.turnRecordId ?? null)
25
+ );
26
+ }
27
+
15
28
  export type ProcessProductRef = SemanticEntryRef;
16
29
  export type ProcessProductRefs = Record<string, ProcessProductRef>;
17
30
 
@@ -47,30 +60,19 @@ export interface ProcessSemanticEntryRefs {
47
60
  rootEntry: SemanticEntryRef | null;
48
61
  }
49
62
 
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
-
58
63
  export function isProcessSemanticEntryRefKey(value: string): value is ProcessSemanticEntryRefKey {
59
64
  return (PROCESS_SEMANTIC_ENTRY_REF_KEYS as readonly string[]).includes(value);
60
65
  }
61
66
 
62
67
  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);
68
+ const record = asUnknownRecord(value);
69
+ const entryId = trimToNull(record?.entryId);
68
70
  if (!entryId) {
69
71
  return null;
70
72
  }
71
73
  return {
72
74
  entryId,
73
- turnRecordId: parseSemanticEntryId(record.turnRecordId),
75
+ turnRecordId: trimToNull(record?.turnRecordId),
74
76
  };
75
77
  }
76
78
 
@@ -88,11 +90,9 @@ export function createEmptyProcessProductRefs(): ProcessProductRefs {
88
90
  }
89
91
 
90
92
  export function parseProcessProductRefs(value: unknown): ProcessProductRefs {
91
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
92
- return createEmptyProcessProductRefs();
93
- }
93
+ if (!isUnknownRecord(value)) return createEmptyProcessProductRefs();
94
94
  const refs: ProcessProductRefs = {};
95
- for (const [name, rawRef] of Object.entries(value as Record<string, unknown>)) {
95
+ for (const [name, rawRef] of Object.entries(value)) {
96
96
  if (!isValidProcessProductName(name)) {
97
97
  continue;
98
98
  }
@@ -105,10 +105,7 @@ export function parseProcessProductRefs(value: unknown): ProcessProductRefs {
105
105
  }
106
106
 
107
107
  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>;
108
+ const record = asUnknownRecord(value) ?? {};
112
109
  return {
113
110
  plan: parseSemanticEntryRef(record.plan),
114
111
  review: parseSemanticEntryRef(record.review),
@@ -0,0 +1,73 @@
1
+ /** First server receipt of a physical worker startup milestone. */
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
+ export interface StartupObservation {
17
+ workerLeaseId: string;
18
+ milestone: StartupMilestone;
19
+ observedAt: string;
20
+ sourceAt: string | null;
21
+ sourceKind: "server" | "kubernetes_status" | "kubernetes_event";
22
+ objectUid: string | null;
23
+ turnRecordId: string | null;
24
+ /** Sampling lower bound, never an exact binding timestamp. */
25
+ notBefore: string | null;
26
+ metadata: {
27
+ node?: string;
28
+ imageId?: string;
29
+ image?: string;
30
+ storageClass?: string;
31
+ cpu?: string;
32
+ memory?: string;
33
+ namespace?: string;
34
+ podName?: string;
35
+ precision?: "milliseconds" | "microseconds" | "seconds" | "sampling_interval";
36
+ };
37
+ }
38
+ export interface StartupInterval {
39
+ start: string | null;
40
+ end: string | null;
41
+ durationMs: number | null;
42
+ status: "available" | "missing" | "invalid_order";
43
+ clock: "server" | "kubernetes";
44
+ }
45
+ export function startupInterval(
46
+ start: string | null | undefined,
47
+ end: string | null | undefined,
48
+ clock: StartupInterval["clock"] = "server",
49
+ ): StartupInterval {
50
+ const delta = start && end ? Date.parse(end) - Date.parse(start) : NaN;
51
+ const status =
52
+ !start || !end
53
+ ? "missing"
54
+ : !Number.isFinite(delta) || delta < 0
55
+ ? "invalid_order"
56
+ : "available";
57
+ return {
58
+ start: start ?? null,
59
+ end: end ?? null,
60
+ durationMs: status === "available" ? delta : null,
61
+ status,
62
+ clock,
63
+ };
64
+ }
65
+ export interface PhysicalWorkerStart {
66
+ workerLeaseId: string;
67
+ workerId: string;
68
+ turnStartRecordId: string | null;
69
+ turnRecordId: string | null;
70
+ state: string;
71
+ observations: StartupObservation[];
72
+ intervals: Record<string, StartupInterval>;
73
+ }
@@ -1,3 +1,15 @@
1
+ export function toErrorMessage(error: unknown): string {
2
+ if (error instanceof Error && error.message.trim() !== "") {
3
+ return error.message;
4
+ }
5
+ return String(error);
6
+ }
7
+
8
+ /** Return non-blank strings verbatim; unlike trimToNull, preserve surrounding whitespace. */
9
+ export function readNonBlankString(value: unknown): string | null {
10
+ return typeof value === "string" && value.trim() !== "" ? value : null;
11
+ }
12
+
1
13
  export function trimString(value: unknown): string {
2
14
  return typeof value === "string" ? value.trim() : "";
3
15
  }
@@ -28,3 +40,24 @@ export function humanizeProcessLabel(identifier: unknown): string {
28
40
  .replace(/\b\w/g, (letter) => letter.toUpperCase())
29
41
  .trim();
30
42
  }
43
+
44
+ const IDENTIFIER_WORD_LABELS: Record<string, string> = {
45
+ api: "API",
46
+ id: "ID",
47
+ llm: "LLM",
48
+ mr: "MR",
49
+ pi: "Pi",
50
+ ui: "UI",
51
+ };
52
+
53
+ /** Display an identifier with known acronyms, preserving the remaining spelling. */
54
+ export function formatProcessIdentifier(value: string): string {
55
+ return value
56
+ .split(/[_-]+/)
57
+ .filter((part) => part.length > 0)
58
+ .map((part) => {
59
+ const normalizedPart = part.toLowerCase();
60
+ return IDENTIFIER_WORD_LABELS[normalizedPart] ?? part.charAt(0).toUpperCase() + part.slice(1);
61
+ })
62
+ .join(" ");
63
+ }
@@ -2,6 +2,7 @@ import {
2
2
  isProcessSemanticEntryRefKey,
3
3
  type ProcessSemanticEntryRefKey,
4
4
  } from "./semantic-entry-refs.js";
5
+ import { trimToNull } from "./string-normalize.js";
5
6
 
6
7
  export const TURN_ANNOTATION_REFERENCE_KINDS = [
7
8
  "turn_record",
@@ -46,38 +47,23 @@ export interface ProcessTurnAnnotation {
46
47
  updatedAt: string;
47
48
  }
48
49
 
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
-
61
50
  export function parseTurnAnnotationReference(value: unknown): TurnAnnotationReference | null {
62
51
  if (typeof value !== "object" || value === null || Array.isArray(value)) {
63
52
  return null;
64
53
  }
65
54
  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
- }
55
+ const kind = trimToNull(record.kind);
56
+ const role = trimToNull(record.role);
71
57
  if (kind === "turn_record") {
72
- const turnRecordId = parseNonEmptyString(record.turnRecordId);
58
+ const turnRecordId = trimToNull(record.turnRecordId);
73
59
  return turnRecordId ? { kind, turnRecordId, role } : null;
74
60
  }
75
61
  if (kind === "entry") {
76
- const entryId = parseNonEmptyString(record.entryId);
62
+ const entryId = trimToNull(record.entryId);
77
63
  return entryId ? { kind, entryId, role } : null;
78
64
  }
79
65
  if (kind === "semantic_entry_ref") {
80
- const ref = parseNonEmptyString(record.ref);
66
+ const ref = trimToNull(record.ref);
81
67
  return ref && isProcessSemanticEntryRefKey(ref) ? { kind, ref, role } : null;
82
68
  }
83
69
  return null;
@@ -88,6 +74,6 @@ export function parseTurnAnnotationReferences(value: unknown): TurnAnnotationRef
88
74
  return [];
89
75
  }
90
76
  return value
91
- .map((reference) => parseTurnAnnotationReference(reference))
77
+ .map(parseTurnAnnotationReference)
92
78
  .filter((reference): reference is TurnAnnotationReference => reference !== null);
93
79
  }
@@ -1,4 +1,5 @@
1
1
  import { normalizeStringArray, trimToNull } from "./string-normalize.js";
2
+ import { isUnknownRecord as isRecord } from "./unknown-record.js";
2
3
 
3
4
  export const DEFAULT_CONTINUE_PROMPT = "continue";
4
5
 
@@ -22,6 +23,8 @@ export type FailedTurnRecoveryOverrides = Partial<Omit<FailedTurnRecoveryContext
22
23
 
23
24
  export const FAILED_TURN_RECOVERY_METADATA_KEY = "failedTurnRecovery";
24
25
  export const CONTINUE_PROMPT_METADATA_KEY = "continuePrompt";
26
+ const TERMINAL_OUTCOME_RECORDING_FAILURE_PREFIX =
27
+ "Server could not durably record worker turn outcome:";
25
28
 
26
29
  export function isFailedTurnRecoveryCode(value: string): value is FailedTurnRecoveryCode {
27
30
  return (FAILED_TURN_RECOVERY_CODES as readonly string[]).includes(value);
@@ -32,13 +35,7 @@ function normalizeUniqueStringArray(value: unknown): string[] | undefined {
32
35
  return normalized.length > 0 ? [...new Set(normalized)] : undefined;
33
36
  }
34
37
 
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
- }
38
+ export { trimToNull as normalizeContinuePrompt };
42
39
 
43
40
  export function createGenericFailedTurnRecoveryContext(
44
41
  overrides: FailedTurnRecoveryOverrides = {},
@@ -51,6 +48,19 @@ export function createGenericFailedTurnRecoveryContext(
51
48
  };
52
49
  }
53
50
 
51
+ export function inferTerminalRecordingFailedTurnRecoveryContext(input: {
52
+ errorSummary?: string | null;
53
+ errorClass?: string | null;
54
+ }): FailedTurnRecoveryContext | null {
55
+ if (
56
+ input.errorClass !== "infrastructure" ||
57
+ !input.errorSummary?.startsWith(TERMINAL_OUTCOME_RECORDING_FAILURE_PREFIX)
58
+ ) {
59
+ return null;
60
+ }
61
+ return createGenericFailedTurnRecoveryContext();
62
+ }
63
+
54
64
  export function buildFailedTurnRecoveryMetadata(
55
65
  turnRecordId: string,
56
66
  context: FailedTurnRecoveryOverrides = {},
@@ -83,7 +93,7 @@ export function readFailedTurnRecoveryContext(
83
93
  if (raw.strategy !== "continue") {
84
94
  return null;
85
95
  }
86
- const suggestedContinuePrompt = normalizeContinuePrompt(raw.suggestedContinuePrompt);
96
+ const suggestedContinuePrompt = trimToNull(raw.suggestedContinuePrompt);
87
97
  const failureCode = trimToNull(raw.failureCode);
88
98
  if (!suggestedContinuePrompt || !failureCode || !isFailedTurnRecoveryCode(failureCode)) {
89
99
  return null;