@abseed/spectra-core 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/LICENSE +202 -0
- package/NOTICE +6 -0
- package/dist/answer.d.ts +40 -0
- package/dist/answer.js +72 -0
- package/dist/backlinks.d.ts +35 -0
- package/dist/backlinks.js +66 -0
- package/dist/changeset.d.ts +25 -0
- package/dist/changeset.js +157 -0
- package/dist/commit.d.ts +42 -0
- package/dist/commit.js +96 -0
- package/dist/conflicts.d.ts +29 -0
- package/dist/conflicts.js +64 -0
- package/dist/coverage.d.ts +83 -0
- package/dist/coverage.js +148 -0
- package/dist/expectationCheck.d.ts +43 -0
- package/dist/expectationCheck.js +86 -0
- package/dist/expectations.d.ts +66 -0
- package/dist/expectations.js +114 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +15 -0
- package/dist/propose.d.ts +19 -0
- package/dist/propose.js +32 -0
- package/dist/raise.d.ts +26 -0
- package/dist/raise.js +42 -0
- package/dist/schema.d.ts +1284 -0
- package/dist/schema.js +222 -0
- package/dist/specStore.d.ts +156 -0
- package/dist/specStore.js +1 -0
- package/dist/transcriptStore.d.ts +83 -0
- package/dist/transcriptStore.js +1 -0
- package/dist/types.d.ts +299 -0
- package/dist/types.js +11 -0
- package/dist/valueType.d.ts +23 -0
- package/dist/valueType.js +41 -0
- package/package.json +35 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Luke Pezet
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
Spectra
|
|
2
|
+
Copyright 2026 Luke Pezet
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
5
|
+
this project except in compliance with the License. You may obtain a copy of the
|
|
6
|
+
License in the LICENSE file, or at http://www.apache.org/licenses/LICENSE-2.0
|
package/dist/answer.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Answering a question, over the {@link SpecStore} seam.
|
|
3
|
+
*
|
|
4
|
+
* Two things happen and both matter: the answer is written back into the question, where it stays as
|
|
5
|
+
* the record of *why* the glossary says what it says, and the chosen option's proposal is minted into
|
|
6
|
+
* the pending changeset queue, where it goes through the same review the human already has. Nothing is
|
|
7
|
+
* applied — answering decides the intent; applying the changeset it produces is a separate act.
|
|
8
|
+
*
|
|
9
|
+
* Pure, so it lives in core beside the seam — every coordinator answers identically.
|
|
10
|
+
*/
|
|
11
|
+
import type { Answer, Author } from './types.js';
|
|
12
|
+
import type { SpecStore } from './specStore.js';
|
|
13
|
+
export type AnswerOutcome = {
|
|
14
|
+
ok: false;
|
|
15
|
+
status: 404;
|
|
16
|
+
error: string;
|
|
17
|
+
} | {
|
|
18
|
+
ok: false;
|
|
19
|
+
status: 400;
|
|
20
|
+
error: string;
|
|
21
|
+
} | {
|
|
22
|
+
ok: false;
|
|
23
|
+
status: 409;
|
|
24
|
+
error: string;
|
|
25
|
+
} | {
|
|
26
|
+
ok: true;
|
|
27
|
+
questionId: string;
|
|
28
|
+
answer: Answer;
|
|
29
|
+
/** Set when the chosen option carried a proposal. */
|
|
30
|
+
changesetId?: string;
|
|
31
|
+
changesetFile?: string;
|
|
32
|
+
};
|
|
33
|
+
export interface AnswerRequest {
|
|
34
|
+
/** Label of the chosen option, or null to answer in prose without taking one. */
|
|
35
|
+
chose: string | null;
|
|
36
|
+
note: string;
|
|
37
|
+
/** ISO timestamp; the caller's clock, so this stays testable. */
|
|
38
|
+
answeredAt: string;
|
|
39
|
+
}
|
|
40
|
+
export declare function answerQuestion(store: SpecStore, id: string, request: AnswerRequest, author: Author): Promise<AnswerOutcome>;
|
package/dist/answer.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/** A short, filename-safe id fragment from an option label. Inlined so this stays free of I/O deps. */
|
|
2
|
+
function slug(value) {
|
|
3
|
+
return (value
|
|
4
|
+
.toLowerCase()
|
|
5
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
6
|
+
.replace(/^-+|-+$/g, '')
|
|
7
|
+
.slice(0, 48) || 'option');
|
|
8
|
+
}
|
|
9
|
+
export async function answerQuestion(store, id, request, author) {
|
|
10
|
+
const question = await store.findQuestion(id);
|
|
11
|
+
if (!question)
|
|
12
|
+
return { ok: false, status: 404, error: `No question with id "${id}".` };
|
|
13
|
+
// A decision is a record, not a setting. Changing your mind means a new question (or a
|
|
14
|
+
// hand-edit), so the reasoning that was acted on cannot be quietly overwritten.
|
|
15
|
+
if (question.answer) {
|
|
16
|
+
return {
|
|
17
|
+
ok: false,
|
|
18
|
+
status: 409,
|
|
19
|
+
error: `"${id}" was already answered on ${question.answer.answeredAt}. Raise a new question rather than overwriting the decision.`,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
const option = request.chose === null
|
|
23
|
+
? null
|
|
24
|
+
: question.options.find((candidate) => candidate.label === request.chose);
|
|
25
|
+
if (request.chose !== null && !option) {
|
|
26
|
+
return { ok: false, status: 400, error: `"${id}" has no option labelled "${request.chose}".` };
|
|
27
|
+
}
|
|
28
|
+
if (request.chose === null && !request.note.trim()) {
|
|
29
|
+
return {
|
|
30
|
+
ok: false,
|
|
31
|
+
status: 400,
|
|
32
|
+
error: 'Answering without choosing an option requires a note saying what to do instead.',
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
const answer = {
|
|
36
|
+
chose: request.chose,
|
|
37
|
+
note: request.note,
|
|
38
|
+
answeredAt: request.answeredAt,
|
|
39
|
+
author,
|
|
40
|
+
};
|
|
41
|
+
let changesetFile;
|
|
42
|
+
if (option?.proposal) {
|
|
43
|
+
const changesetId = `${question.id}-${slug(option.label)}`;
|
|
44
|
+
const changeset = {
|
|
45
|
+
id: changesetId,
|
|
46
|
+
summary: option.proposal.summary,
|
|
47
|
+
ops: option.proposal.ops,
|
|
48
|
+
tests: option.proposal.tests,
|
|
49
|
+
fromQuestion: question.id,
|
|
50
|
+
author,
|
|
51
|
+
};
|
|
52
|
+
// Written before the answer: if the process dies between the two, an unreferenced changeset
|
|
53
|
+
// in the queue is noise someone notices, where an answer citing a changeset that was never
|
|
54
|
+
// written is a dangling reference.
|
|
55
|
+
changesetFile = await store.addChangeset(changeset);
|
|
56
|
+
answer.changesetId = changesetId;
|
|
57
|
+
}
|
|
58
|
+
// No expectedRev here: a question's one mutation is being answered, and the "already answered"
|
|
59
|
+
// guard above is that record's optimistic-concurrency check. writeAnswer still bumps the rev.
|
|
60
|
+
const written = await store.writeAnswer(id, answer);
|
|
61
|
+
if (!written.ok) {
|
|
62
|
+
// Only 'not-found' is reachable without an expectedRev — the question vanished mid-answer.
|
|
63
|
+
return { ok: false, status: 404, error: `No question with id "${id}".` };
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
ok: true,
|
|
67
|
+
questionId: question.id,
|
|
68
|
+
answer,
|
|
69
|
+
...(answer.changesetId ? { changesetId: answer.changesetId } : {}),
|
|
70
|
+
...(changesetFile ? { changesetFile } : {}),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The glossary's edges, derived rather than stored. Nothing in a term file records
|
|
3
|
+
* "who points at me" — it is recomputed from `parent` and `ref:` attribute types on
|
|
4
|
+
* every read, so a hand-edit can never leave a stale backlink behind.
|
|
5
|
+
*/
|
|
6
|
+
import type { Term } from './types.js';
|
|
7
|
+
export type ReferenceKind = 'parent' | 'attribute';
|
|
8
|
+
export interface Reference {
|
|
9
|
+
/** The term doing the referencing. */
|
|
10
|
+
from: string;
|
|
11
|
+
/** The term name being referenced — may not exist, see `dangling`. */
|
|
12
|
+
to: string;
|
|
13
|
+
kind: ReferenceKind;
|
|
14
|
+
/** The attribute carrying the reference, or null for an `is-a` edge. */
|
|
15
|
+
via: string | null;
|
|
16
|
+
array: boolean;
|
|
17
|
+
}
|
|
18
|
+
export interface Backlinks {
|
|
19
|
+
references: Reference[];
|
|
20
|
+
/** Edges pointing *at* a term, keyed by target name. */
|
|
21
|
+
byTarget: Record<string, Reference[]>;
|
|
22
|
+
/** Edges pointing *out of* a term, keyed by source name. */
|
|
23
|
+
bySource: Record<string, Reference[]>;
|
|
24
|
+
/** Subtypes, keyed by supertype name. */
|
|
25
|
+
children: Record<string, string[]>;
|
|
26
|
+
/** Edges whose target does not exist in the glossary. */
|
|
27
|
+
dangling: Reference[];
|
|
28
|
+
}
|
|
29
|
+
export declare function computeBacklinks(terms: Term[]): Backlinks;
|
|
30
|
+
export type HighlightKind = 'selected' | 'parent' | 'child' | 'referrer' | 'referenced';
|
|
31
|
+
/**
|
|
32
|
+
* Everything connected to `name`, in either direction, labelled by how. Drives
|
|
33
|
+
* click-a-term-to-highlight-what-it-touches — the search-first stand-in for wires.
|
|
34
|
+
*/
|
|
35
|
+
export declare function connectionsFor(backlinks: Backlinks, name: string): Map<string, HighlightKind>;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { parseValueType } from './valueType.js';
|
|
2
|
+
export function computeBacklinks(terms) {
|
|
3
|
+
const known = new Set(terms.map((term) => term.name));
|
|
4
|
+
const references = [];
|
|
5
|
+
for (const term of terms) {
|
|
6
|
+
if (term.parent) {
|
|
7
|
+
references.push({ from: term.name, to: term.parent, kind: 'parent', via: null, array: false });
|
|
8
|
+
}
|
|
9
|
+
for (const attribute of term.attributes) {
|
|
10
|
+
const parsed = parseValueType(attribute.valueType);
|
|
11
|
+
if (parsed?.kind === 'ref') {
|
|
12
|
+
references.push({
|
|
13
|
+
from: term.name,
|
|
14
|
+
to: parsed.name,
|
|
15
|
+
kind: 'attribute',
|
|
16
|
+
via: attribute.name,
|
|
17
|
+
array: parsed.array,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const byTarget = {};
|
|
23
|
+
const bySource = {};
|
|
24
|
+
const children = {};
|
|
25
|
+
for (const reference of references) {
|
|
26
|
+
;
|
|
27
|
+
(byTarget[reference.to] ??= []).push(reference);
|
|
28
|
+
(bySource[reference.from] ??= []).push(reference);
|
|
29
|
+
if (reference.kind === 'parent') {
|
|
30
|
+
;
|
|
31
|
+
(children[reference.to] ??= []).push(reference.from);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
references,
|
|
36
|
+
byTarget,
|
|
37
|
+
bySource,
|
|
38
|
+
children,
|
|
39
|
+
dangling: references.filter((reference) => !known.has(reference.to)),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Everything connected to `name`, in either direction, labelled by how. Drives
|
|
44
|
+
* click-a-term-to-highlight-what-it-touches — the search-first stand-in for wires.
|
|
45
|
+
*/
|
|
46
|
+
export function connectionsFor(backlinks, name) {
|
|
47
|
+
const connections = new Map();
|
|
48
|
+
// Weakest first: later writes win, so `selected` and is-a edges beat plain references.
|
|
49
|
+
for (const reference of backlinks.bySource[name] ?? []) {
|
|
50
|
+
if (reference.kind === 'attribute')
|
|
51
|
+
connections.set(reference.to, 'referenced');
|
|
52
|
+
}
|
|
53
|
+
for (const reference of backlinks.byTarget[name] ?? []) {
|
|
54
|
+
if (reference.kind === 'attribute')
|
|
55
|
+
connections.set(reference.from, 'referrer');
|
|
56
|
+
}
|
|
57
|
+
for (const reference of backlinks.bySource[name] ?? []) {
|
|
58
|
+
if (reference.kind === 'parent')
|
|
59
|
+
connections.set(reference.to, 'parent');
|
|
60
|
+
}
|
|
61
|
+
for (const child of backlinks.children[name] ?? []) {
|
|
62
|
+
connections.set(child, 'child');
|
|
63
|
+
}
|
|
64
|
+
connections.set(name, 'selected');
|
|
65
|
+
return connections;
|
|
66
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Diagnostic, Op, Term } from './types.js';
|
|
2
|
+
export type OpKindClass = 'add' | 'remove' | 'modify';
|
|
3
|
+
export interface OpEffect {
|
|
4
|
+
index: number;
|
|
5
|
+
op: Op;
|
|
6
|
+
kind: OpKindClass;
|
|
7
|
+
term: string;
|
|
8
|
+
/** Target term immediately before this op, or null if it did not exist yet. */
|
|
9
|
+
before: Term | null;
|
|
10
|
+
/** Target term immediately after. Equal to `before` when the op failed. */
|
|
11
|
+
after: Term | null;
|
|
12
|
+
ok: boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface ApplyResult {
|
|
15
|
+
terms: Term[];
|
|
16
|
+
diagnostics: Diagnostic[];
|
|
17
|
+
effects: OpEffect[];
|
|
18
|
+
}
|
|
19
|
+
export declare function opKindClass(op: Op): OpKindClass;
|
|
20
|
+
export declare function applyOps(terms: Term[], ops: Op[]): ApplyResult;
|
|
21
|
+
export declare function validateOps(terms: Term[], ops: Op[]): Diagnostic[];
|
|
22
|
+
export declare function hasErrors(diagnostics: Diagnostic[]): boolean;
|
|
23
|
+
export declare function hasWarnings(diagnostics: Diagnostic[]): boolean;
|
|
24
|
+
/** One-line human summary of an op, for the review list. */
|
|
25
|
+
export declare function summarizeOp(op: Op): string;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Replaying changeset ops against the glossary. Pure — no filesystem — so the web app
|
|
3
|
+
* can preview exactly what the server will commit, and both agree on what counts as
|
|
4
|
+
* broken. Ops are applied in order and each one can fail independently; a failed op is
|
|
5
|
+
* skipped rather than aborting the run, so the UI can show every problem at once.
|
|
6
|
+
*/
|
|
7
|
+
import { computeBacklinks } from './backlinks.js';
|
|
8
|
+
export function opKindClass(op) {
|
|
9
|
+
switch (op.op) {
|
|
10
|
+
case 'add_entity':
|
|
11
|
+
case 'add_attribute':
|
|
12
|
+
return 'add';
|
|
13
|
+
case 'remove_entity':
|
|
14
|
+
case 'remove_attribute':
|
|
15
|
+
return 'remove';
|
|
16
|
+
case 'modify_spec':
|
|
17
|
+
return 'modify';
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function referenceKey(reference) {
|
|
21
|
+
return `${reference.from}|${reference.via ?? ''}|${reference.to}`;
|
|
22
|
+
}
|
|
23
|
+
function edgeKey(from, via) {
|
|
24
|
+
return `${from}|${via ?? ''}`;
|
|
25
|
+
}
|
|
26
|
+
function describeEdge(reference) {
|
|
27
|
+
return reference.via ? `${reference.from}.${reference.via}` : `${reference.from}'s parent`;
|
|
28
|
+
}
|
|
29
|
+
export function applyOps(terms, ops) {
|
|
30
|
+
const byName = new Map(terms.map((term) => [term.name, structuredClone(term)]));
|
|
31
|
+
const diagnostics = [];
|
|
32
|
+
const effects = [];
|
|
33
|
+
/** Edges this op selection creates, so a dangling one can be blamed on the op that made it. */
|
|
34
|
+
const introduced = new Map();
|
|
35
|
+
const snapshot = (name) => {
|
|
36
|
+
const term = byName.get(name);
|
|
37
|
+
return term ? structuredClone(term) : null;
|
|
38
|
+
};
|
|
39
|
+
ops.forEach((op, index) => {
|
|
40
|
+
const before = snapshot(op.term);
|
|
41
|
+
const kind = opKindClass(op);
|
|
42
|
+
const fail = (message) => {
|
|
43
|
+
diagnostics.push({ opIndex: index, severity: 'error', message });
|
|
44
|
+
effects.push({ index, op, kind, term: op.term, before, after: before, ok: false });
|
|
45
|
+
};
|
|
46
|
+
const done = () => {
|
|
47
|
+
effects.push({ index, op, kind, term: op.term, before, after: snapshot(op.term), ok: true });
|
|
48
|
+
};
|
|
49
|
+
switch (op.op) {
|
|
50
|
+
case 'add_entity': {
|
|
51
|
+
if (byName.has(op.term))
|
|
52
|
+
return fail(`"${op.term}" already exists.`);
|
|
53
|
+
const created = {
|
|
54
|
+
name: op.term,
|
|
55
|
+
type: op.termType ?? 'entity',
|
|
56
|
+
spec: op.spec,
|
|
57
|
+
parent: op.parent ?? null,
|
|
58
|
+
tags: op.tags ?? [],
|
|
59
|
+
attributes: op.attributes ?? [],
|
|
60
|
+
};
|
|
61
|
+
byName.set(op.term, created);
|
|
62
|
+
if (created.parent)
|
|
63
|
+
introduced.set(edgeKey(op.term, null), index);
|
|
64
|
+
for (const attribute of created.attributes) {
|
|
65
|
+
introduced.set(edgeKey(op.term, attribute.name), index);
|
|
66
|
+
}
|
|
67
|
+
return done();
|
|
68
|
+
}
|
|
69
|
+
case 'remove_entity': {
|
|
70
|
+
if (!byName.has(op.term))
|
|
71
|
+
return fail(`"${op.term}" does not exist.`);
|
|
72
|
+
byName.delete(op.term);
|
|
73
|
+
return done();
|
|
74
|
+
}
|
|
75
|
+
case 'add_attribute': {
|
|
76
|
+
const target = byName.get(op.term);
|
|
77
|
+
if (!target)
|
|
78
|
+
return fail(`"${op.term}" does not exist.`);
|
|
79
|
+
if (target.attributes.some((existing) => existing.name === op.attribute.name)) {
|
|
80
|
+
return fail(`"${op.term}" already has an attribute named "${op.attribute.name}".`);
|
|
81
|
+
}
|
|
82
|
+
target.attributes.push(structuredClone(op.attribute));
|
|
83
|
+
introduced.set(edgeKey(op.term, op.attribute.name), index);
|
|
84
|
+
return done();
|
|
85
|
+
}
|
|
86
|
+
case 'remove_attribute': {
|
|
87
|
+
const target = byName.get(op.term);
|
|
88
|
+
if (!target)
|
|
89
|
+
return fail(`"${op.term}" does not exist.`);
|
|
90
|
+
const at = target.attributes.findIndex((existing) => existing.name === op.attribute);
|
|
91
|
+
if (at === -1)
|
|
92
|
+
return fail(`"${op.term}" has no attribute named "${op.attribute}".`);
|
|
93
|
+
target.attributes.splice(at, 1);
|
|
94
|
+
return done();
|
|
95
|
+
}
|
|
96
|
+
case 'modify_spec': {
|
|
97
|
+
const target = byName.get(op.term);
|
|
98
|
+
if (!target)
|
|
99
|
+
return fail(`"${op.term}" does not exist.`);
|
|
100
|
+
target.spec = op.spec;
|
|
101
|
+
return done();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
const result = [...byName.values()].sort((a, b) => a.name.localeCompare(b.name));
|
|
106
|
+
// Only edges this selection *newly* breaks are its problem — a glossary that was
|
|
107
|
+
// already broken by hand stays broken without blaming the changeset for it.
|
|
108
|
+
const alreadyDangling = new Set(computeBacklinks(terms).dangling.map(referenceKey));
|
|
109
|
+
for (const reference of computeBacklinks(result).dangling) {
|
|
110
|
+
if (alreadyDangling.has(referenceKey(reference)))
|
|
111
|
+
continue;
|
|
112
|
+
const culprit = introduced.get(edgeKey(reference.from, reference.via));
|
|
113
|
+
if (culprit !== undefined) {
|
|
114
|
+
// The selection adds a reference to something it never creates — usually a
|
|
115
|
+
// cherry-pick that left its dependency behind. Nothing to acknowledge; it is wrong.
|
|
116
|
+
diagnostics.push({
|
|
117
|
+
opIndex: culprit,
|
|
118
|
+
severity: 'error',
|
|
119
|
+
message: `${describeEdge(reference)} references "${reference.to}", which does not exist. The op that creates it is not part of this selection.`,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
// A reference that was fine until this selection removed its target. Real, but the
|
|
124
|
+
// human may mean it — surface it and make them say so.
|
|
125
|
+
diagnostics.push({
|
|
126
|
+
opIndex: null,
|
|
127
|
+
severity: 'warning',
|
|
128
|
+
message: `${describeEdge(reference)} references "${reference.to}", which this change removes.`,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return { terms: result, diagnostics, effects };
|
|
133
|
+
}
|
|
134
|
+
export function validateOps(terms, ops) {
|
|
135
|
+
return applyOps(terms, ops).diagnostics;
|
|
136
|
+
}
|
|
137
|
+
export function hasErrors(diagnostics) {
|
|
138
|
+
return diagnostics.some((diagnostic) => diagnostic.severity === 'error');
|
|
139
|
+
}
|
|
140
|
+
export function hasWarnings(diagnostics) {
|
|
141
|
+
return diagnostics.some((diagnostic) => diagnostic.severity === 'warning');
|
|
142
|
+
}
|
|
143
|
+
/** One-line human summary of an op, for the review list. */
|
|
144
|
+
export function summarizeOp(op) {
|
|
145
|
+
switch (op.op) {
|
|
146
|
+
case 'add_entity':
|
|
147
|
+
return `add ${op.termType ?? 'entity'} ${op.term}${op.parent ? ` (is-a ${op.parent})` : ''}`;
|
|
148
|
+
case 'remove_entity':
|
|
149
|
+
return `remove ${op.term}`;
|
|
150
|
+
case 'add_attribute':
|
|
151
|
+
return `add ${op.term}.${op.attribute.name}: ${op.attribute.valueType}`;
|
|
152
|
+
case 'remove_attribute':
|
|
153
|
+
return `remove ${op.term}.${op.attribute}`;
|
|
154
|
+
case 'modify_spec':
|
|
155
|
+
return `rewrite the spec of ${op.term}`;
|
|
156
|
+
}
|
|
157
|
+
}
|
package/dist/commit.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Diagnostic } from './types.js';
|
|
2
|
+
import type { SpecStore } from './specStore.js';
|
|
3
|
+
export type CommitOutcome = {
|
|
4
|
+
ok: false;
|
|
5
|
+
status: 404;
|
|
6
|
+
error: string;
|
|
7
|
+
} | {
|
|
8
|
+
ok: false;
|
|
9
|
+
status: 400;
|
|
10
|
+
error: string;
|
|
11
|
+
} | {
|
|
12
|
+
ok: false;
|
|
13
|
+
status: 409;
|
|
14
|
+
error: string;
|
|
15
|
+
diagnostics: Diagnostic[];
|
|
16
|
+
needsAcknowledgement: boolean;
|
|
17
|
+
} | {
|
|
18
|
+
ok: true;
|
|
19
|
+
appliedOps: number;
|
|
20
|
+
remainingOps: number;
|
|
21
|
+
written: string[];
|
|
22
|
+
deleted: string[];
|
|
23
|
+
resolvedTo: string;
|
|
24
|
+
diagnostics: Diagnostic[];
|
|
25
|
+
};
|
|
26
|
+
export interface ApplyRequest {
|
|
27
|
+
opIndices: number[];
|
|
28
|
+
acknowledgeWarnings?: boolean;
|
|
29
|
+
}
|
|
30
|
+
export declare function applyChangeset(store: SpecStore, id: string, request: ApplyRequest): Promise<CommitOutcome>;
|
|
31
|
+
/**
|
|
32
|
+
* Records that code has been written for an applied changeset — the human presses a button after
|
|
33
|
+
* re-running the implementation pass. (The agent's own `mark_implemented` tool guards this with a
|
|
34
|
+
* snapshot version; this human path just records it.)
|
|
35
|
+
*/
|
|
36
|
+
export declare function markImplemented(store: SpecStore, id: string, at: string): Promise<{
|
|
37
|
+
ok: boolean;
|
|
38
|
+
status?: number;
|
|
39
|
+
error?: string;
|
|
40
|
+
file?: string;
|
|
41
|
+
}>;
|
|
42
|
+
export declare function rejectChangeset(store: SpecStore, id: string): Promise<CommitOutcome>;
|