@leitwerk-dev/domain 0.1.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/dist/LICENSE +13 -0
- package/dist/actor.d.ts +18 -0
- package/dist/actor.d.ts.map +1 -0
- package/dist/actor.js +72 -0
- package/dist/actor.js.map +1 -0
- package/dist/domain-model.d.ts +472 -0
- package/dist/domain-model.d.ts.map +1 -0
- package/dist/domain-model.js +171 -0
- package/dist/domain-model.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/launcher-recent-values.d.ts +10 -0
- package/dist/launcher-recent-values.d.ts.map +1 -0
- package/dist/launcher-recent-values.js +60 -0
- package/dist/launcher-recent-values.js.map +1 -0
- package/dist/markdown-normalize.d.ts +4 -0
- package/dist/markdown-normalize.d.ts.map +1 -0
- package/dist/markdown-normalize.js +14 -0
- package/dist/markdown-normalize.js.map +1 -0
- package/dist/process-definitions.d.ts +68 -0
- package/dist/process-definitions.d.ts.map +1 -0
- package/dist/process-definitions.js +2 -0
- package/dist/process-definitions.js.map +1 -0
- package/dist/process-model-config.d.ts +24 -0
- package/dist/process-model-config.d.ts.map +1 -0
- package/dist/process-model-config.js +68 -0
- package/dist/process-model-config.js.map +1 -0
- package/dist/process-row-slots.d.ts +17 -0
- package/dist/process-row-slots.d.ts.map +1 -0
- package/dist/process-row-slots.js +45 -0
- package/dist/process-row-slots.js.map +1 -0
- package/dist/process-state-json.d.ts +16 -0
- package/dist/process-state-json.d.ts.map +1 -0
- package/dist/process-state-json.js +102 -0
- package/dist/process-state-json.js.map +1 -0
- package/dist/repo-locator.d.ts +10 -0
- package/dist/repo-locator.d.ts.map +1 -0
- package/dist/repo-locator.js +82 -0
- package/dist/repo-locator.js.map +1 -0
- package/dist/review-subject.d.ts +9 -0
- package/dist/review-subject.d.ts.map +1 -0
- package/dist/review-subject.js +18 -0
- package/dist/review-subject.js.map +1 -0
- package/dist/semantic-entry-refs.d.ts +34 -0
- package/dist/semantic-entry-refs.d.ts.map +1 -0
- package/dist/semantic-entry-refs.js +79 -0
- package/dist/semantic-entry-refs.js.map +1 -0
- package/dist/string-normalize.d.ts +12 -0
- package/dist/string-normalize.d.ts.map +1 -0
- package/dist/string-normalize.js +28 -0
- package/dist/string-normalize.js.map +1 -0
- package/dist/turn-acceptance.d.ts +3 -0
- package/dist/turn-acceptance.d.ts.map +1 -0
- package/dist/turn-acceptance.js +2 -0
- package/dist/turn-acceptance.js.map +1 -0
- package/dist/turn-annotations.d.ts +33 -0
- package/dist/turn-annotations.d.ts.map +1 -0
- package/dist/turn-annotations.js +49 -0
- package/dist/turn-annotations.js.map +1 -0
- package/dist/turn-recovery.d.ts +26 -0
- package/dist/turn-recovery.d.ts.map +1 -0
- package/dist/turn-recovery.js +70 -0
- package/dist/turn-recovery.js.map +1 -0
- package/dist/turn-start-resolution.d.ts +65 -0
- package/dist/turn-start-resolution.d.ts.map +1 -0
- package/dist/turn-start-resolution.js +97 -0
- package/dist/turn-start-resolution.js.map +1 -0
- package/package.json +40 -0
- package/src/actor.ts +78 -0
- package/src/domain-model.ts +739 -0
- package/src/index.ts +211 -0
- package/src/launcher-recent-values.ts +70 -0
- package/src/markdown-normalize.ts +15 -0
- package/src/process-definitions.ts +79 -0
- package/src/process-model-config.ts +110 -0
- package/src/process-row-slots.ts +64 -0
- package/src/process-state-json.ts +155 -0
- package/src/repo-locator.ts +101 -0
- package/src/review-subject.ts +26 -0
- package/src/semantic-entry-refs.ts +118 -0
- package/src/string-normalize.ts +30 -0
- package/src/turn-acceptance.ts +3 -0
- package/src/turn-annotations.ts +93 -0
- package/src/turn-recovery.ts +98 -0
- package/src/turn-start-resolution.ts +188 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
export type RepoLocatorKind = "remote_url" | "local_path";
|
|
2
|
+
|
|
3
|
+
export interface ParsedRepoLocator {
|
|
4
|
+
kind: RepoLocatorKind;
|
|
5
|
+
value: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function trimRepoLocator(value: string): string {
|
|
9
|
+
return value.trim();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function isFileUrl(value: string): boolean {
|
|
13
|
+
try {
|
|
14
|
+
const url = new URL(value);
|
|
15
|
+
if (url.protocol !== "file:") {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
return (
|
|
19
|
+
(url.hostname === "" || url.hostname === "localhost") &&
|
|
20
|
+
url.pathname.startsWith("/") &&
|
|
21
|
+
decodeURIComponent(url.pathname).length > 1
|
|
22
|
+
);
|
|
23
|
+
} catch {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isLocalFilesystemPath(value: string): boolean {
|
|
29
|
+
return (
|
|
30
|
+
value.startsWith("/") ||
|
|
31
|
+
value.startsWith("./") ||
|
|
32
|
+
value.startsWith("../") ||
|
|
33
|
+
value.startsWith("~/") ||
|
|
34
|
+
isFileUrl(value)
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const scpLikeGitUrlPattern = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+:[^\s]+$/;
|
|
39
|
+
const explicitRemoteGitUrlPattern = /^(?:https?|ssh|git):\/\//;
|
|
40
|
+
|
|
41
|
+
function hasRepositoryPath(pathname: string): boolean {
|
|
42
|
+
return pathname.split("/").filter(Boolean).length >= 1;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function isRemoteGitUrl(value: string): boolean {
|
|
46
|
+
if (scpLikeGitUrlPattern.test(value)) {
|
|
47
|
+
const path = value.slice(value.indexOf(":") + 1);
|
|
48
|
+
return hasRepositoryPath(path);
|
|
49
|
+
}
|
|
50
|
+
if (!explicitRemoteGitUrlPattern.test(value)) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
const url = new URL(value);
|
|
55
|
+
return (
|
|
56
|
+
["http:", "https:", "ssh:", "git:"].includes(url.protocol) &&
|
|
57
|
+
!!url.hostname &&
|
|
58
|
+
hasRepositoryPath(url.pathname)
|
|
59
|
+
);
|
|
60
|
+
} catch {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function detectRepoLocatorKind(value: string): RepoLocatorKind | null {
|
|
66
|
+
const trimmed = trimRepoLocator(value);
|
|
67
|
+
if (trimmed.length === 0) {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
if (isLocalFilesystemPath(trimmed)) {
|
|
71
|
+
return "local_path";
|
|
72
|
+
}
|
|
73
|
+
if (isRemoteGitUrl(trimmed)) {
|
|
74
|
+
return "remote_url";
|
|
75
|
+
}
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function parseRepoLocator(value: unknown): ParsedRepoLocator | null {
|
|
80
|
+
if (typeof value !== "string") {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
const trimmed = trimRepoLocator(value);
|
|
84
|
+
const kind = detectRepoLocatorKind(trimmed);
|
|
85
|
+
if (!kind) {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
return { kind, value: trimmed };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function isRepoLocator(value: unknown): value is string {
|
|
92
|
+
return parseRepoLocator(value) !== null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function assertRepoLocator(value: unknown, fieldName = "repoLocator"): string {
|
|
96
|
+
const parsed = parseRepoLocator(value);
|
|
97
|
+
if (!parsed) {
|
|
98
|
+
throw new Error(`${fieldName} must be a remote URL or local filesystem path`);
|
|
99
|
+
}
|
|
100
|
+
return parsed.value;
|
|
101
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export const REVIEW_SUBJECT_KINDS = ["plan", "implementation"] as const;
|
|
2
|
+
|
|
3
|
+
export type ReviewSubjectKind = (typeof REVIEW_SUBJECT_KINDS)[number];
|
|
4
|
+
|
|
5
|
+
export interface ReviewSubject {
|
|
6
|
+
kind: ReviewSubjectKind;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function isReviewSubjectKind(value: string): value is ReviewSubjectKind {
|
|
10
|
+
return (REVIEW_SUBJECT_KINDS as readonly string[]).includes(value);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function createReviewSubject(kind: ReviewSubjectKind): ReviewSubject {
|
|
14
|
+
return { kind };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function parseReviewSubject(value: unknown): ReviewSubject | null {
|
|
18
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
const record = value as Record<string, unknown>;
|
|
22
|
+
if (typeof record.kind !== "string" || !isReviewSubjectKind(record.kind)) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
return { kind: record.kind };
|
|
26
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
export const PROCESS_SEMANTIC_ENTRY_REF_KEYS = [
|
|
2
|
+
"plan",
|
|
3
|
+
"review",
|
|
4
|
+
"currentPrimaryPathLeaf",
|
|
5
|
+
"rootEntry",
|
|
6
|
+
] as const;
|
|
7
|
+
|
|
8
|
+
export type ProcessSemanticEntryRefKey = (typeof PROCESS_SEMANTIC_ENTRY_REF_KEYS)[number];
|
|
9
|
+
|
|
10
|
+
export interface SemanticEntryRef {
|
|
11
|
+
entryId: string;
|
|
12
|
+
turnRecordId: string | null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type ProcessProductRef = SemanticEntryRef;
|
|
16
|
+
export type ProcessProductRefs = Record<string, ProcessProductRef>;
|
|
17
|
+
|
|
18
|
+
const PRODUCT_NAME_PATTERN = /^[a-z][a-z0-9-]*$/;
|
|
19
|
+
|
|
20
|
+
export function isValidProcessProductName(value: string): boolean {
|
|
21
|
+
return PRODUCT_NAME_PATTERN.test(value);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function assertValidProcessProductName(value: string): void {
|
|
25
|
+
if (!isValidProcessProductName(value)) {
|
|
26
|
+
throw new Error(
|
|
27
|
+
`Invalid process product name '${value}'. Product names must match ${PRODUCT_NAME_PATTERN.source}`,
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Stage 2 keeps these refs as structural placeholders initialized to `null`.
|
|
34
|
+
* Current runtime population:
|
|
35
|
+
* - `rootEntry` during worker bootstrap and subsequent worker tree-fact reporting
|
|
36
|
+
* - `currentPrimaryPathLeaf` during worker bootstrap, successful primary turns,
|
|
37
|
+
* and prompt-style primary-path input delivery
|
|
38
|
+
* - `plan` during successful planning turns; it remains the current plan through
|
|
39
|
+
* implementation until another planning turn replaces it
|
|
40
|
+
* - `review` during successful review turns; it remains the current review until
|
|
41
|
+
* another review turn replaces it
|
|
42
|
+
*/
|
|
43
|
+
export interface ProcessSemanticEntryRefs {
|
|
44
|
+
plan: SemanticEntryRef | null;
|
|
45
|
+
review: SemanticEntryRef | null;
|
|
46
|
+
currentPrimaryPathLeaf: SemanticEntryRef | null;
|
|
47
|
+
rootEntry: SemanticEntryRef | null;
|
|
48
|
+
}
|
|
49
|
+
|
|
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
|
+
export function isProcessSemanticEntryRefKey(value: string): value is ProcessSemanticEntryRefKey {
|
|
59
|
+
return (PROCESS_SEMANTIC_ENTRY_REF_KEYS as readonly string[]).includes(value);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
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
|
+
if (!entryId) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
entryId,
|
|
73
|
+
turnRecordId: parseSemanticEntryId(record.turnRecordId),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function createEmptyProcessSemanticEntryRefs(): ProcessSemanticEntryRefs {
|
|
78
|
+
return {
|
|
79
|
+
plan: null,
|
|
80
|
+
review: null,
|
|
81
|
+
currentPrimaryPathLeaf: null,
|
|
82
|
+
rootEntry: null,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function createEmptyProcessProductRefs(): ProcessProductRefs {
|
|
87
|
+
return {};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function parseProcessProductRefs(value: unknown): ProcessProductRefs {
|
|
91
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
92
|
+
return createEmptyProcessProductRefs();
|
|
93
|
+
}
|
|
94
|
+
const refs: ProcessProductRefs = {};
|
|
95
|
+
for (const [name, rawRef] of Object.entries(value as Record<string, unknown>)) {
|
|
96
|
+
if (!isValidProcessProductName(name)) {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const ref = parseSemanticEntryRef(rawRef);
|
|
100
|
+
if (ref) {
|
|
101
|
+
refs[name] = ref;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return refs;
|
|
105
|
+
}
|
|
106
|
+
|
|
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>;
|
|
112
|
+
return {
|
|
113
|
+
plan: parseSemanticEntryRef(record.plan),
|
|
114
|
+
review: parseSemanticEntryRef(record.review),
|
|
115
|
+
currentPrimaryPathLeaf: parseSemanticEntryRef(record.currentPrimaryPathLeaf),
|
|
116
|
+
rootEntry: parseSemanticEntryRef(record.rootEntry),
|
|
117
|
+
};
|
|
118
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export function trimString(value: unknown): string {
|
|
2
|
+
return typeof value === "string" ? value.trim() : "";
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function trimToNull(value: unknown): string | null {
|
|
6
|
+
const trimmed = trimString(value);
|
|
7
|
+
return trimmed === "" ? null : trimmed;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function normalizeStringArray(value: unknown): string[] {
|
|
11
|
+
if (!Array.isArray(value)) {
|
|
12
|
+
return [];
|
|
13
|
+
}
|
|
14
|
+
return value.map((entry) => trimString(entry)).filter((entry) => entry.length > 0);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Single source of truth for turning an internal identifier (an action id,
|
|
19
|
+
* outcome id, or trigger) into a human-readable Title Case label, e.g.
|
|
20
|
+
* `approve_plan` -> `Approve Plan`. All operator-facing surfaces (action forms,
|
|
21
|
+
* flow diagrams) must route through this so the translation lives in one place
|
|
22
|
+
* and can later be swapped for a localized lookup.
|
|
23
|
+
*/
|
|
24
|
+
export function humanizeProcessLabel(identifier: unknown): string {
|
|
25
|
+
return trimString(identifier)
|
|
26
|
+
.replace(/[_-]+/g, " ")
|
|
27
|
+
.replace(/\s+/g, " ")
|
|
28
|
+
.replace(/\b\w/g, (letter) => letter.toUpperCase())
|
|
29
|
+
.trim();
|
|
30
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isProcessSemanticEntryRefKey,
|
|
3
|
+
type ProcessSemanticEntryRefKey,
|
|
4
|
+
} from "./semantic-entry-refs.js";
|
|
5
|
+
|
|
6
|
+
export const TURN_ANNOTATION_REFERENCE_KINDS = [
|
|
7
|
+
"turn_record",
|
|
8
|
+
"entry",
|
|
9
|
+
"semantic_entry_ref",
|
|
10
|
+
] as const;
|
|
11
|
+
|
|
12
|
+
export type TurnAnnotationReferenceKind = (typeof TURN_ANNOTATION_REFERENCE_KINDS)[number];
|
|
13
|
+
export type TurnAnnotationReferenceRole = string;
|
|
14
|
+
|
|
15
|
+
export interface TurnRecordAnnotationReference {
|
|
16
|
+
kind: "turn_record";
|
|
17
|
+
turnRecordId: string;
|
|
18
|
+
role: TurnAnnotationReferenceRole | null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface EntryTurnAnnotationReference {
|
|
22
|
+
kind: "entry";
|
|
23
|
+
entryId: string;
|
|
24
|
+
role: TurnAnnotationReferenceRole | null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface SemanticEntryRefTurnAnnotationReference {
|
|
28
|
+
kind: "semantic_entry_ref";
|
|
29
|
+
ref: ProcessSemanticEntryRefKey;
|
|
30
|
+
role: TurnAnnotationReferenceRole | null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type TurnAnnotationReference =
|
|
34
|
+
| TurnRecordAnnotationReference
|
|
35
|
+
| EntryTurnAnnotationReference
|
|
36
|
+
| SemanticEntryRefTurnAnnotationReference;
|
|
37
|
+
|
|
38
|
+
export interface ProcessTurnAnnotation {
|
|
39
|
+
id: string;
|
|
40
|
+
instanceId: string;
|
|
41
|
+
annotationType: string;
|
|
42
|
+
annotationKey: string | null;
|
|
43
|
+
references: TurnAnnotationReference[];
|
|
44
|
+
payload: Record<string, unknown>;
|
|
45
|
+
createdAt: string;
|
|
46
|
+
updatedAt: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
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
|
+
export function parseTurnAnnotationReference(value: unknown): TurnAnnotationReference | null {
|
|
62
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
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
|
+
}
|
|
71
|
+
if (kind === "turn_record") {
|
|
72
|
+
const turnRecordId = parseNonEmptyString(record.turnRecordId);
|
|
73
|
+
return turnRecordId ? { kind, turnRecordId, role } : null;
|
|
74
|
+
}
|
|
75
|
+
if (kind === "entry") {
|
|
76
|
+
const entryId = parseNonEmptyString(record.entryId);
|
|
77
|
+
return entryId ? { kind, entryId, role } : null;
|
|
78
|
+
}
|
|
79
|
+
if (kind === "semantic_entry_ref") {
|
|
80
|
+
const ref = parseNonEmptyString(record.ref);
|
|
81
|
+
return ref && isProcessSemanticEntryRefKey(ref) ? { kind, ref, role } : null;
|
|
82
|
+
}
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function parseTurnAnnotationReferences(value: unknown): TurnAnnotationReference[] {
|
|
87
|
+
if (!Array.isArray(value)) {
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
90
|
+
return value
|
|
91
|
+
.map((reference) => parseTurnAnnotationReference(reference))
|
|
92
|
+
.filter((reference): reference is TurnAnnotationReference => reference !== null);
|
|
93
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { normalizeStringArray, trimToNull } from "./string-normalize.js";
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_CONTINUE_PROMPT = "continue";
|
|
4
|
+
|
|
5
|
+
export const FAILED_TURN_RECOVERY_CODES = [
|
|
6
|
+
"generic_continue",
|
|
7
|
+
"missing_outcome_tool",
|
|
8
|
+
"missing_markdown_result",
|
|
9
|
+
"missing_required_tool_calls",
|
|
10
|
+
] as const;
|
|
11
|
+
|
|
12
|
+
export type FailedTurnRecoveryCode = (typeof FAILED_TURN_RECOVERY_CODES)[number];
|
|
13
|
+
|
|
14
|
+
export interface FailedTurnRecoveryContext {
|
|
15
|
+
strategy: "continue";
|
|
16
|
+
suggestedContinuePrompt: string;
|
|
17
|
+
failureCode: FailedTurnRecoveryCode;
|
|
18
|
+
missingToolNames?: string[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type FailedTurnRecoveryOverrides = Partial<Omit<FailedTurnRecoveryContext, "strategy">>;
|
|
22
|
+
|
|
23
|
+
export const FAILED_TURN_RECOVERY_METADATA_KEY = "failedTurnRecovery";
|
|
24
|
+
export const CONTINUE_PROMPT_METADATA_KEY = "continuePrompt";
|
|
25
|
+
|
|
26
|
+
export function isFailedTurnRecoveryCode(value: string): value is FailedTurnRecoveryCode {
|
|
27
|
+
return (FAILED_TURN_RECOVERY_CODES as readonly string[]).includes(value);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function normalizeUniqueStringArray(value: unknown): string[] | undefined {
|
|
31
|
+
const normalized = normalizeStringArray(value);
|
|
32
|
+
return normalized.length > 0 ? [...new Set(normalized)] : undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
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
|
+
}
|
|
42
|
+
|
|
43
|
+
export function createGenericFailedTurnRecoveryContext(
|
|
44
|
+
overrides: FailedTurnRecoveryOverrides = {},
|
|
45
|
+
): FailedTurnRecoveryContext {
|
|
46
|
+
return {
|
|
47
|
+
strategy: "continue",
|
|
48
|
+
suggestedContinuePrompt: DEFAULT_CONTINUE_PROMPT,
|
|
49
|
+
failureCode: "generic_continue",
|
|
50
|
+
...overrides,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function buildFailedTurnRecoveryMetadata(
|
|
55
|
+
turnRecordId: string,
|
|
56
|
+
context: FailedTurnRecoveryOverrides = {},
|
|
57
|
+
) {
|
|
58
|
+
return {
|
|
59
|
+
[FAILED_TURN_RECOVERY_METADATA_KEY]: {
|
|
60
|
+
turnRecordId,
|
|
61
|
+
...createGenericFailedTurnRecoveryContext(context),
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function readFailedTurnRecoveryContext(
|
|
67
|
+
metadata: Record<string, unknown> | null | undefined,
|
|
68
|
+
expectedTurnRecordId?: string | null,
|
|
69
|
+
): FailedTurnRecoveryContext | null {
|
|
70
|
+
if (!metadata) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
const raw = metadata[FAILED_TURN_RECOVERY_METADATA_KEY];
|
|
74
|
+
if (!isRecord(raw)) {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
if (expectedTurnRecordId) {
|
|
78
|
+
const turnRecordId = trimToNull(raw.turnRecordId);
|
|
79
|
+
if (turnRecordId !== expectedTurnRecordId) {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (raw.strategy !== "continue") {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
const suggestedContinuePrompt = normalizeContinuePrompt(raw.suggestedContinuePrompt);
|
|
87
|
+
const failureCode = trimToNull(raw.failureCode);
|
|
88
|
+
if (!suggestedContinuePrompt || !failureCode || !isFailedTurnRecoveryCode(failureCode)) {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
const missingToolNames = normalizeUniqueStringArray(raw.missingToolNames);
|
|
92
|
+
return {
|
|
93
|
+
strategy: "continue",
|
|
94
|
+
suggestedContinuePrompt,
|
|
95
|
+
failureCode,
|
|
96
|
+
...(missingToolNames ? { missingToolNames } : {}),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import type { ProcessTurnRecordPathType } from "./domain-model.js";
|
|
2
|
+
import type {
|
|
3
|
+
ProcessProductRefs,
|
|
4
|
+
ProcessSemanticEntryRefKey,
|
|
5
|
+
ProcessSemanticEntryRefs,
|
|
6
|
+
} from "./semantic-entry-refs.js";
|
|
7
|
+
|
|
8
|
+
export interface ProcessTurnStartFromCurrentLeaf {
|
|
9
|
+
kind: "current_leaf";
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface ProcessTurnStartFromSessionRoot {
|
|
13
|
+
/** Start with no persisted Pi entry in context. The kickoff entry has parentId null. */
|
|
14
|
+
kind: "session_root";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface ProcessTurnStartFromEntry {
|
|
18
|
+
kind: "entry";
|
|
19
|
+
entryId: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type ProcessTurnStartFallback =
|
|
23
|
+
| ProcessTurnStartFromCurrentLeaf
|
|
24
|
+
| ProcessTurnStartFromSessionRoot
|
|
25
|
+
| ProcessTurnStartFromEntry;
|
|
26
|
+
|
|
27
|
+
export interface ProcessTurnStartFromSemanticRef {
|
|
28
|
+
kind: "semantic_ref";
|
|
29
|
+
ref: ProcessSemanticEntryRefKey;
|
|
30
|
+
fallback?: ProcessTurnStartSelection;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface ProcessTurnStartFromProductRef {
|
|
34
|
+
kind: "product_ref";
|
|
35
|
+
productName: string;
|
|
36
|
+
fallback?: ProcessTurnStartSelection;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type ProcessTurnStartSelection =
|
|
40
|
+
| ProcessTurnStartFallback
|
|
41
|
+
| ProcessTurnStartFromSemanticRef
|
|
42
|
+
| ProcessTurnStartFromProductRef;
|
|
43
|
+
|
|
44
|
+
export type ProcessTurnStartTarget =
|
|
45
|
+
| { kind: "current_leaf" }
|
|
46
|
+
| { kind: "entry"; entryId: string }
|
|
47
|
+
| { kind: "root" };
|
|
48
|
+
|
|
49
|
+
export interface ProcessTurnStartResolution {
|
|
50
|
+
forkPiEntryId: string | null;
|
|
51
|
+
startTarget: ProcessTurnStartTarget;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function defaultProcessTurnStartSelection(
|
|
55
|
+
branchType: ProcessTurnRecordPathType,
|
|
56
|
+
): ProcessTurnStartFallback {
|
|
57
|
+
switch (branchType) {
|
|
58
|
+
case "primary":
|
|
59
|
+
case "leaf_branch":
|
|
60
|
+
return { kind: "current_leaf" };
|
|
61
|
+
case "root_branch":
|
|
62
|
+
return { kind: "session_root" };
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function resolveProcessTurnStartSelection(input: {
|
|
67
|
+
branchType: ProcessTurnRecordPathType;
|
|
68
|
+
startFrom?: ProcessTurnStartSelection;
|
|
69
|
+
}): ProcessTurnStartSelection {
|
|
70
|
+
return input.startFrom ?? defaultProcessTurnStartSelection(input.branchType);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function resolveExistingProcessEntryId(
|
|
74
|
+
entryId: string | null,
|
|
75
|
+
entryExists?: ((entryId: string) => boolean) | undefined,
|
|
76
|
+
): string | null {
|
|
77
|
+
if (!entryId) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
return entryExists && !entryExists(entryId) ? null : entryId;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function resolveProcessSemanticRefEntryId(input: {
|
|
84
|
+
ref: keyof ProcessSemanticEntryRefs;
|
|
85
|
+
semanticEntryRefs?: ProcessSemanticEntryRefs | null;
|
|
86
|
+
currentLeafId: string | null;
|
|
87
|
+
rootEntryId: string | null;
|
|
88
|
+
entryExists?: (entryId: string) => boolean;
|
|
89
|
+
}): string | null {
|
|
90
|
+
const fromState = resolveExistingProcessEntryId(
|
|
91
|
+
input.semanticEntryRefs?.[input.ref]?.entryId ?? null,
|
|
92
|
+
input.entryExists,
|
|
93
|
+
);
|
|
94
|
+
if (fromState) {
|
|
95
|
+
return fromState;
|
|
96
|
+
}
|
|
97
|
+
if (input.ref === "currentPrimaryPathLeaf") {
|
|
98
|
+
return resolveExistingProcessEntryId(input.currentLeafId, input.entryExists);
|
|
99
|
+
}
|
|
100
|
+
if (input.ref === "rootEntry") {
|
|
101
|
+
return resolveExistingProcessEntryId(input.rootEntryId, input.entryExists);
|
|
102
|
+
}
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function resolveProcessProductRefEntryId(input: {
|
|
107
|
+
productName: string;
|
|
108
|
+
productRefs?: ProcessProductRefs | null;
|
|
109
|
+
entryExists?: (entryId: string) => boolean;
|
|
110
|
+
}): string | null {
|
|
111
|
+
const productName = input.productName.trim();
|
|
112
|
+
if (!productName) {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
return resolveExistingProcessEntryId(
|
|
116
|
+
input.productRefs?.[productName]?.entryId ?? null,
|
|
117
|
+
input.entryExists,
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function resolveProcessTurnStartTarget(input: {
|
|
122
|
+
branchType: ProcessTurnRecordPathType;
|
|
123
|
+
selection: ProcessTurnStartSelection;
|
|
124
|
+
currentLeafId: string | null;
|
|
125
|
+
rootEntryId: string | null;
|
|
126
|
+
semanticEntryRefs?: ProcessSemanticEntryRefs | null;
|
|
127
|
+
productRefs?: ProcessProductRefs | null;
|
|
128
|
+
entryExists?: (entryId: string) => boolean;
|
|
129
|
+
}): ProcessTurnStartResolution {
|
|
130
|
+
switch (input.selection.kind) {
|
|
131
|
+
case "current_leaf": {
|
|
132
|
+
return {
|
|
133
|
+
forkPiEntryId: resolveExistingProcessEntryId(input.currentLeafId, input.entryExists),
|
|
134
|
+
startTarget: { kind: "current_leaf" },
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
case "session_root": {
|
|
138
|
+
return {
|
|
139
|
+
forkPiEntryId: null,
|
|
140
|
+
startTarget: { kind: "root" },
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
case "entry": {
|
|
144
|
+
return {
|
|
145
|
+
forkPiEntryId: input.selection.entryId,
|
|
146
|
+
startTarget: { kind: "entry", entryId: input.selection.entryId },
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
case "semantic_ref": {
|
|
150
|
+
const entryId = resolveProcessSemanticRefEntryId({
|
|
151
|
+
ref: input.selection.ref,
|
|
152
|
+
semanticEntryRefs: input.semanticEntryRefs,
|
|
153
|
+
currentLeafId: input.currentLeafId,
|
|
154
|
+
rootEntryId: input.rootEntryId,
|
|
155
|
+
entryExists: input.entryExists,
|
|
156
|
+
});
|
|
157
|
+
if (entryId) {
|
|
158
|
+
return {
|
|
159
|
+
forkPiEntryId: entryId,
|
|
160
|
+
startTarget: { kind: "entry", entryId },
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return resolveProcessTurnStartTarget({
|
|
165
|
+
...input,
|
|
166
|
+
selection: input.selection.fallback ?? defaultProcessTurnStartSelection(input.branchType),
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
case "product_ref": {
|
|
170
|
+
const entryId = resolveProcessProductRefEntryId({
|
|
171
|
+
productName: input.selection.productName,
|
|
172
|
+
productRefs: input.productRefs,
|
|
173
|
+
entryExists: input.entryExists,
|
|
174
|
+
});
|
|
175
|
+
if (entryId) {
|
|
176
|
+
return {
|
|
177
|
+
forkPiEntryId: entryId,
|
|
178
|
+
startTarget: { kind: "entry", entryId },
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return resolveProcessTurnStartTarget({
|
|
183
|
+
...input,
|
|
184
|
+
selection: input.selection.fallback ?? defaultProcessTurnStartSelection(input.branchType),
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|