@yaks/provenance 0.0.0-stage → 0.2.36

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 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 Jeff Peterson
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/README.md CHANGED
@@ -1,3 +1,78 @@
1
- # Temporary Holding Version
1
+ # @yaks/provenance
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Who made an entity and who last changed it, when, and through what: the
4
+ `created` and `updated` stamps the graph fills in on every write. A graph
5
+ composed without this package keeps neither.
6
+
7
+ ## Stored data and exports
8
+
9
+ - `created{at, by, via}` is set once, when the entity is made.
10
+ - `updated{at, by, via}` records the last write after that, and is absent until
11
+ the first one.
12
+ - `authorship{fields}` retains the writers of the words and primitive values
13
+ still held by the row, where a store composes the tracker. `fields` maps
14
+ JSON-encoded component/property/leaf paths to ranges with
15
+ `{from, to, by,
16
+ via}`. String ranges use UTF-16 offsets; other values use
17
+ `[0, 1]`.
18
+
19
+ The tracker matches meaningful words across the whole row, so edits, property
20
+ moves and array shifts keep their writers. Equal repeated words keep all their
21
+ known writers conservatively. Removing their final occurrence removes that
22
+ contribution; whitespace and punctuation alone retain none. Consecutive words
23
+ with the same writers share a range, so an ordinary long field holds one range
24
+ per writer. Nested JSON values are attributed independently.
25
+
26
+ Old rows without `authorship` use both `created` and `updated`. Their first edit
27
+ seeds surviving content conservatively from those two stamps. Writers lost
28
+ before tracking began cannot be recovered from a graph without history.
29
+
30
+ Every property is stamped: clients read them and never write them. The
31
+ [stamp phase](../graph/README.md#stamps-and-actors) of @yaks/graph fills them
32
+ in, `at` from the write's clock and `by` and `via` from its
33
+ [actor](../graph/README.md#data-model). Neither component keeps journal history.
34
+
35
+ The root import exports `provenanceDoc`, a vocabulary document.
36
+ `@yaks/provenance/vocab` also exports `docs: [provenanceDoc]` and `description`
37
+ for plugin loaders. `@yaks/provenance/tracker` exports `authorship(vocab)`, the
38
+ plugin that retains value attribution transactionally through the storage
39
+ interface. It is not a facet, so a graph listing this package keeps only the
40
+ stamps; a store opts in by composing it after other storage trackers such as
41
+ archetypes, as yaks.app's app stores do through @yaks/platform's
42
+ `yak/authorship`. `@yaks/provenance/authorship` exports `authors(row)` to read
43
+ the current writers and `retained(old, next, actor, vocab)` to compute their
44
+ ranges. The package has no storage, tools or effects.
45
+
46
+ ## Example
47
+
48
+ ```ts
49
+ import { provenanceDoc } from '@yaks/provenance'
50
+ import { graph, signed } from '@yaks/graph'
51
+ import { ram } from '@yaks/ram'
52
+ import { loadVocab } from '@yaks/vocab'
53
+ import { equal } from '@yaks/testing'
54
+
55
+ let vocab = loadVocab([provenanceDoc, {
56
+ $defs: {
57
+ book: {
58
+ component: true,
59
+ type: 'object',
60
+ properties: { title: { type: 'string' } },
61
+ },
62
+ },
63
+ }])
64
+ let g = graph({ vocab, storage: ram(vocab) })
65
+ let ada = { by: '00000000-0000-4000-8000-000000000001' }
66
+ let write = (title: string, now: string) =>
67
+ g.apply(signed([{ entity: { eid: 'b1' }, book: { title } }], ada), { now })
68
+ await write('Dune', '2026-01-01T00:00:00.000Z')
69
+ await write('Emma', '2026-01-02T00:00:00.000Z')
70
+ let [book] = await g.get(['b1'])
71
+ equal(book.created, { at: '2026-01-01T00:00:00.000Z', by: ada.by })
72
+ equal(book.updated, { at: '2026-01-02T00:00:00.000Z', by: ada.by })
73
+ ```
74
+
75
+ ## Compatibility
76
+
77
+ Deno, Node and browsers. Its declarations and pure attribution logic make no
78
+ runtime calls.
@@ -0,0 +1,13 @@
1
+ import type { Actor, Bundle } from "@yaks/graph";
2
+ import type { Vocab } from "@yaks/vocab";
3
+ export type Span = Actor & {
4
+ from: number;
5
+ to: number;
6
+ };
7
+ export type Fields = Record<string, Span[]>;
8
+ /** The authors of current values, optionally selecting spans by field, or
9
+ * the surviving stamps for a row that predates live attribution. */ export declare let authors: (row?: Bundle, include?: (path: string[], span: Span) => boolean) => Actor[];
10
+ /** Attribute the row's current words and primitive values, wherever they
11
+ * moved inside it. Changed values name this writer as well as the writers
12
+ * of their surviving words. Removing a word removes its contributions. */ export declare let retained: (old: Bundle | undefined, next: Bundle, by: Actor, vocab: Vocab) => Fields;
13
+ //# sourceMappingURL=authorship.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"authorship.d.ts","sources":["../authorship.ts"],"names":[],"mappings":"AACA,cAAc,KAAK,EAAE,MAAM,2BAAgC;AAE3D,cAAc,KAAK,2BAAgC;AAEnD,YAAY,OAAO;EAAU,MAAM,MAAM;EAAE,IAAI,MAAM;;AACrD,YAAY,SAAS,OAAO,MAAM,EAAE;AAOpC;kEACkE,GAClE,OAAO,YAAI,UACT,MAAM,QACN,WAAW,MAAM,MAAM,IAAI,MAAM,SAAS,OAAO,KAChD,QAUF;AA4GD;;wEAEwE,GACxE,OAAO,YAAI,WACT,KAAK,SAAS,SAAS,EACvB,MAAM,QACN,IAAI,OACJ,OAAO,UACN,OAYF"}
package/_dist/mod.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Who made an entity and who last changed it: `created{at, by, via}` is set
3
+ * once, when the entity is made, and `updated{at, by, via}` on every later
4
+ * write. @yaks/graph's stamp phase fills both in for whatever vocabulary
5
+ * declares them, so a graph composed without this package keeps no stamps.
6
+ *
7
+ * `@yaks/provenance/tracker` retains the writers of surviving words and values
8
+ * as `authorship{fields}` for a store that composes it; it is not a facet, so
9
+ * listing this package keeps just the stamps.
10
+ */ export { provenanceDoc } from "./vocab.js";
11
+ //# sourceMappingURL=mod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.d.ts","sources":["../mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;CASC,GAED,SAAS,aAAa,qBAAoB"}
@@ -0,0 +1,4 @@
1
+ import type { Plugin } from "@yaks/graph";
2
+ import type { Vocab } from "@yaks/vocab";
3
+ /** Retain value writers inside the same transaction as the values. */ export declare let authorship: (vocab: Vocab) => Plugin;
4
+ //# sourceMappingURL=tracker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tracker.d.ts","sources":["../tracker.ts"],"names":[],"mappings":"AAIA,cAAsB,MAAM,2BAA6C;AAEzE,cAAc,KAAK,2BAAgC;AAGnD,oEAAoE,GACpE,OAAO,YAAI,aAAc,OAAO,UAAQ,OAwDtC"}
@@ -0,0 +1,7 @@
1
+ import type { VocabDoc } from "@yaks/vocab";
2
+ /** `created{at, by, via}` and `updated{at, by, via}`, in the form `loadVocab`
3
+ * accepts, alongside the optional `authorship{fields}` value attribution. */ export declare let provenanceDoc: VocabDoc;
4
+ /** What this package is: its manifest's description, the one place it is
5
+ * written, which a host stamps on each document here. */ export declare let description: string;
6
+ /** Every document this plugin declares. */ export declare let docs: VocabDoc[];
7
+ //# sourceMappingURL=vocab.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vocab.d.ts","sources":["../vocab.ts"],"names":[],"mappings":"AAKA,cAAc,QAAQ,2BAAgC;AAItD;2EAC2E,GAC3E,OAAO,YAAI,eAAe,SAA0B;AAEpD;uDACuD,GACvD,OAAO,YAAI,aAAa,MAAM,CAAuB;AAErD,yCAAyC,GACzC,OAAO,YAAI,MAAM,WAA4B"}
package/authorship.js ADDED
@@ -0,0 +1,173 @@
1
+ // The writers of the values an entity still holds, rather than its last editor.
2
+ import { comps } from "@yaks/graph/bundle";
3
+ let identity = ({ by, via })=>JSON.stringify([
4
+ by,
5
+ via
6
+ ]);
7
+ let writers = (actors)=>new Map(actors.map(({ by, via })=>[
8
+ identity({
9
+ by,
10
+ via
11
+ }),
12
+ {
13
+ by,
14
+ via
15
+ }
16
+ ]));
17
+ /** The authors of current values, optionally selecting spans by field, or
18
+ * the surviving stamps for a row that predates live attribution. */ export let authors = (row, include)=>{
19
+ let fields = row?.authorship?.fields;
20
+ if (!fields) return [
21
+ row?.created,
22
+ row?.updated
23
+ ].filter(Boolean);
24
+ let spans = include ? Object.entries(fields).flatMap(([key, spans])=>{
25
+ let path = JSON.parse(key);
26
+ return spans.filter((span)=>include(path, span));
27
+ }) : Object.values(fields).flat();
28
+ return [
29
+ ...writers(spans).values()
30
+ ];
31
+ };
32
+ let leaves = (value, path, into)=>{
33
+ if (value != null && typeof value == 'object') {
34
+ for (let [k, v] of Object.entries(value))leaves(v, [
35
+ ...path,
36
+ k
37
+ ], into);
38
+ } else if (value != null) into.set(JSON.stringify(path), value);
39
+ };
40
+ let values = (row, vocab)=>{
41
+ let out = new Map();
42
+ for (let [c, comp] of comps(row ?? {
43
+ entity: {
44
+ eid: ''
45
+ }
46
+ })){
47
+ if (c == 'authorship') continue;
48
+ for (let [p, v] of Object.entries(comp ?? {})){
49
+ if (!vocab.prop(c, p)?.stamped) leaves(v, [
50
+ c,
51
+ p
52
+ ], out);
53
+ }
54
+ }
55
+ return out;
56
+ };
57
+ // Word matching is deliberately conservative: two occurrences of the same
58
+ // word may be indistinguishable after an edit or move, so both keep its writers.
59
+ // Whitespace and punctuation alone do not keep someone's words in a row.
60
+ let words = /[\p{L}\p{N}\p{S}_]+/gu;
61
+ let tokens = (value)=>typeof value == 'string' ? [
62
+ ...value.matchAll(words)
63
+ ].map((m)=>({
64
+ key: JSON.stringify(m[0]),
65
+ from: m.index,
66
+ to: m.index + m[0].length
67
+ })) : [
68
+ {
69
+ key: JSON.stringify(value),
70
+ from: 0,
71
+ to: 1
72
+ }
73
+ ];
74
+ // Find the first word a span overlaps; walking from there visits only the
75
+ // words that writer contributed, including a legacy stamp spanning a field.
76
+ let start = (words, at)=>{
77
+ let lo = 0, hi = words.length;
78
+ while(lo < hi){
79
+ let mid = lo + hi >>> 1;
80
+ if (words[mid].to <= at) lo = mid + 1;
81
+ else hi = mid;
82
+ }
83
+ return lo;
84
+ };
85
+ let indexed = (before, held, legacy)=>{
86
+ let out = new Map();
87
+ for (let [key, value] of before){
88
+ let words = tokens(value);
89
+ let spans = held ? held[key] ?? [] : legacy.map((a)=>({
90
+ ...a,
91
+ from: 0,
92
+ to: typeof value == 'string' ? value.length : 1
93
+ }));
94
+ for (let span of spans){
95
+ let actor = {
96
+ by: span.by,
97
+ via: span.via
98
+ };
99
+ let id = identity(actor);
100
+ for(let i = start(words, span.from); i < words.length && words[i].from < span.to; i++){
101
+ let word = words[i].key, by = out.get(word);
102
+ if (!by) out.set(word, by = new Map());
103
+ by.set(id, actor);
104
+ }
105
+ }
106
+ }
107
+ return out;
108
+ };
109
+ let extent = (value, { by, via })=>{
110
+ if (typeof value != 'string') return [
111
+ {
112
+ by,
113
+ via,
114
+ from: 0,
115
+ to: 1
116
+ }
117
+ ];
118
+ let from, to = 0;
119
+ for (let m of value.matchAll(words)){
120
+ from ??= m.index;
121
+ to = m.index + m[0].length;
122
+ }
123
+ return from === undefined ? [] : [
124
+ {
125
+ by,
126
+ via,
127
+ from,
128
+ to
129
+ }
130
+ ];
131
+ };
132
+ let attributed = (value, index, by)=>{
133
+ if (!index.size) return extent(value, by);
134
+ let out = [], run = [], previous = '';
135
+ let actor = {
136
+ by: by.by,
137
+ via: by.via
138
+ }, id = identity(actor);
139
+ for (let t of tokens(value)){
140
+ let named = new Map(index.get(t.key));
141
+ named.set(id, actor);
142
+ let ids = [
143
+ ...named.keys()
144
+ ].sort();
145
+ let key = JSON.stringify(ids);
146
+ if (key == previous) {
147
+ for (let s of run)s.to = t.to;
148
+ } else {
149
+ run = ids.map((id)=>({
150
+ ...named.get(id),
151
+ from: t.from,
152
+ to: t.to
153
+ }));
154
+ out.push(...run);
155
+ previous = key;
156
+ }
157
+ }
158
+ return out;
159
+ };
160
+ /** Attribute the row's current words and primitive values, wherever they
161
+ * moved inside it. Changed values name this writer as well as the writers
162
+ * of their surviving words. Removing a word removes its contributions. */ export let retained = (old, next, by, vocab)=>{
163
+ let before = values(old, vocab), after = values(next, vocab);
164
+ let held = old?.authorship?.fields;
165
+ let index;
166
+ return Object.fromEntries([
167
+ ...after
168
+ ].map(([key, value])=>[
169
+ key,
170
+ held && before.get(key) === value ? held[key] ?? [] : attributed(value, index ??= indexed(before, held, held ? [] : authors(old)), by)
171
+ ]));
172
+ };
173
+ //# sourceMappingURL=authorship.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"authorship.js","sources":["./authorship.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAEhF,SAAS,KAAK,kCAAuC;AAOrD,IAAI,WAAW,CAAC,EAAE,EAAE,EAAE,GAAG,EAAS,GAAK,KAAK,SAAS,CAAC;IAAC;IAAI;GAAI;AAC/D,IAAI,UAAU,CAAC,SACb,IAAI,IAAI,OAAO,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,GAAG,EAAE,GAAK;MAAC,SAAS;QAAE;QAAI;MAAI;MAAI;QAAE;QAAI;MAAI;KAAE;AAE1E;kEACkE,GAClE,OAAO,IAAI,UAAU,CACnB,KACA;EAEA,IAAI,SAAU,KAAK,YAAgD;EACnE,IAAI,CAAC,QAAQ,OAAO;IAAC,KAAK;IAAS,KAAK;GAAQ,CAAC,MAAM,CAAC;EACxD,IAAI,QAAQ,UACR,OAAO,OAAO,CAAC,QAAQ,OAAO,CAAC,CAAC,CAAC,KAAK,MAAM;IAC5C,IAAI,OAAO,KAAK,KAAK,CAAC;IACtB,OAAO,MAAM,MAAM,CAAC,CAAC,OAAS,QAAQ,MAAM;EAC9C,KACE,OAAO,MAAM,CAAC,QAAQ,IAAI;EAC9B,OAAO;OAAI,QAAQ,OAAO,MAAM;GAAG;AACrC,EAAC;AAED,IAAI,SAAS,CAAC,OAAgB,MAAgB;EAC5C,IAAI,SAAS,QAAQ,OAAO,SAAS,UAAU;IAC7C,KAAK,IAAI,CAAC,GAAG,EAAE,IAAI,OAAO,OAAO,CAAC,OAAQ,OAAO,GAAG;SAAI;MAAM;KAAE,EAAE;EACpE,OAAO,IAAI,SAAS,MAAM,KAAK,GAAG,CAAC,KAAK,SAAS,CAAC,OAAO;AAC3D;AAEA,IAAI,SAAS,CAAC,KAAyB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,OAAQ;IAC3B,IAAI,QAAQ,IAAI,IAAI,MAAM,GAAG,CAAC,EAAE,GAAG;IACnC,MAAM,GAAG,CAAC,IAAI;IACd,IAAI,MAAM;SAAI,MAAM,IAAI;KAAG,CAAC,IAAI;IAChC,IAAI,MAAM,KAAK,SAAS,CAAC;IACzB,IAAI,OAAO,UAAU;MACnB,KAAK,IAAI,KAAK,IAAK,EAAE,EAAE,GAAG,EAAE,EAAE;IAChC,OAAO;MACL,MAAM,IAAI,GAAG,CAAC,CAAC,KAAO,CAAC;UAAE,GAAG,MAAM,GAAG,CAAC,GAAG;UAAE,MAAM,EAAE,IAAI;UAAE,IAAI,EAAE,EAAE;QAAC,CAAC;MACnE,IAAI,IAAI,IAAI;MACZ,WAAW;IACb;EACF;EACA,OAAO;AACT;AAEA;;wEAEwE,GACxE,OAAO,IAAI,WAAW,CACpB,KACA,MACA,IACA;EAEA,IAAI,SAAS,OAAO,KAAK,QAAQ,QAAQ,OAAO,MAAM;EACtD,IAAI,OAAQ,KAAK,YAAgD;EACjE,IAAI;EACJ,OAAO,OAAO,WAAW,CAAC;OAAI;GAAM,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,MAAM,GAAK;MACzD;MACA,QAAQ,OAAO,GAAG,CAAC,SAAS,QAAQ,IAAI,CAAC,IAAI,IAAI,EAAE,GAAG,WACpD,OACA,UAAU,QAAQ,QAAQ,MAAM,OAAO,EAAE,GAAG,QAAQ,OACpD;KAEH;AACH,EAAC"}
package/authorship.ts ADDED
@@ -0,0 +1,157 @@
1
+ // The writers of the values an entity still holds, rather than its last editor.
2
+ import type { Actor, Bundle } from 'jsr:@yaks/graph@0.2.36'
3
+ import { comps } from '@yaks/graph/bundle'
4
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
5
+
6
+ export type Span = Actor & { from: number; to: number }
7
+ export type Fields = Record<string, Span[]>
8
+ type Writers = Map<string, Actor>
9
+ type Token = { key: string; from: number; to: number }
10
+ let identity = ({ by, via }: Actor) => JSON.stringify([by, via])
11
+ let writers = (actors: Actor[]): Writers =>
12
+ new Map(actors.map(({ by, via }) => [identity({ by, via }), { by, via }]))
13
+
14
+ /** The authors of current values, optionally selecting spans by field, or
15
+ * the surviving stamps for a row that predates live attribution. */
16
+ export let authors = (
17
+ row?: Bundle,
18
+ include?: (path: string[], span: Span) => boolean,
19
+ ): Actor[] => {
20
+ let fields = (row?.authorship as { fields?: Fields } | undefined)?.fields
21
+ if (!fields) return [row?.created, row?.updated].filter(Boolean) as Actor[]
22
+ let spans = include
23
+ ? Object.entries(fields).flatMap(([key, spans]) => {
24
+ let path = JSON.parse(key) as string[]
25
+ return spans.filter((span) => include(path, span))
26
+ })
27
+ : Object.values(fields).flat()
28
+ return [...writers(spans).values()]
29
+ }
30
+
31
+ let leaves = (value: unknown, path: string[], into: Map<string, unknown>) => {
32
+ if (value != null && typeof value == 'object') {
33
+ for (let [k, v] of Object.entries(value)) leaves(v, [...path, k], into)
34
+ } else if (value != null) into.set(JSON.stringify(path), value)
35
+ }
36
+
37
+ let values = (row: Bundle | undefined, vocab: Vocab): Map<string, unknown> => {
38
+ let out = new Map<string, unknown>()
39
+ for (let [c, comp] of comps(row ?? { entity: { eid: '' } })) {
40
+ if (c == 'authorship') continue
41
+ for (let [p, v] of Object.entries(comp ?? {})) {
42
+ if (!vocab.prop(c, p)?.stamped) leaves(v, [c, p], out)
43
+ }
44
+ }
45
+ return out
46
+ }
47
+
48
+ // Word matching is deliberately conservative: two occurrences of the same
49
+ // word may be indistinguishable after an edit or move, so both keep its writers.
50
+ // Whitespace and punctuation alone do not keep someone's words in a row.
51
+ let words = /[\p{L}\p{N}\p{S}_]+/gu
52
+ let tokens = (value: unknown): Token[] =>
53
+ typeof value == 'string'
54
+ ? [...value.matchAll(words)].map((m) => ({
55
+ key: JSON.stringify(m[0]),
56
+ from: m.index,
57
+ to: m.index + m[0].length,
58
+ }))
59
+ : [{ key: JSON.stringify(value), from: 0, to: 1 }]
60
+
61
+ // Find the first word a span overlaps; walking from there visits only the
62
+ // words that writer contributed, including a legacy stamp spanning a field.
63
+ let start = (words: Token[], at: number): number => {
64
+ let lo = 0, hi = words.length
65
+ while (lo < hi) {
66
+ let mid = (lo + hi) >>> 1
67
+ if (words[mid].to <= at) lo = mid + 1
68
+ else hi = mid
69
+ }
70
+ return lo
71
+ }
72
+
73
+ let indexed = (
74
+ before: Map<string, unknown>,
75
+ held: Fields | undefined,
76
+ legacy: Actor[],
77
+ ): Map<string, Writers> => {
78
+ let out = new Map<string, Writers>()
79
+ for (let [key, value] of before) {
80
+ let words = tokens(value)
81
+ let spans = held ? held[key] ?? [] : legacy.map((a) => ({
82
+ ...a,
83
+ from: 0,
84
+ to: typeof value == 'string' ? value.length : 1,
85
+ }))
86
+ for (let span of spans) {
87
+ let actor = { by: span.by, via: span.via }
88
+ let id = identity(actor)
89
+ for (
90
+ let i = start(words, span.from);
91
+ i < words.length && words[i].from < span.to;
92
+ i++
93
+ ) {
94
+ let word = words[i].key, by = out.get(word)
95
+ if (!by) out.set(word, by = new Map())
96
+ by.set(id, actor)
97
+ }
98
+ }
99
+ }
100
+ return out
101
+ }
102
+
103
+ let extent = (value: unknown, { by, via }: Actor): Span[] => {
104
+ if (typeof value != 'string') return [{ by, via, from: 0, to: 1 }]
105
+ let from: number | undefined, to = 0
106
+ for (let m of value.matchAll(words)) {
107
+ from ??= m.index
108
+ to = m.index + m[0].length
109
+ }
110
+ return from === undefined ? [] : [{ by, via, from, to }]
111
+ }
112
+
113
+ let attributed = (
114
+ value: unknown,
115
+ index: Map<string, Writers>,
116
+ by: Actor,
117
+ ): Span[] => {
118
+ if (!index.size) return extent(value, by)
119
+ let out: Span[] = [], run: Span[] = [], previous = ''
120
+ let actor = { by: by.by, via: by.via }, id = identity(actor)
121
+ for (let t of tokens(value)) {
122
+ let named = new Map(index.get(t.key))
123
+ named.set(id, actor)
124
+ let ids = [...named.keys()].sort()
125
+ let key = JSON.stringify(ids)
126
+ if (key == previous) {
127
+ for (let s of run) s.to = t.to
128
+ } else {
129
+ run = ids.map((id) => ({ ...named.get(id), from: t.from, to: t.to }))
130
+ out.push(...run)
131
+ previous = key
132
+ }
133
+ }
134
+ return out
135
+ }
136
+
137
+ /** Attribute the row's current words and primitive values, wherever they
138
+ * moved inside it. Changed values name this writer as well as the writers
139
+ * of their surviving words. Removing a word removes its contributions. */
140
+ export let retained = (
141
+ old: Bundle | undefined,
142
+ next: Bundle,
143
+ by: Actor,
144
+ vocab: Vocab,
145
+ ): Fields => {
146
+ let before = values(old, vocab), after = values(next, vocab)
147
+ let held = (old?.authorship as { fields?: Fields } | undefined)?.fields
148
+ let index: Map<string, Writers> | undefined
149
+ return Object.fromEntries([...after].map(([key, value]) => [
150
+ key,
151
+ held && before.get(key) === value ? held[key] ?? [] : attributed(
152
+ value,
153
+ index ??= indexed(before, held, held ? [] : authors(old)),
154
+ by,
155
+ ),
156
+ ]))
157
+ }
package/deno.json ADDED
@@ -0,0 +1 @@
1
+ {"publish":{"exclude":["**/*_test.ts"]},"name":"@yaks/provenance","version":"0.2.36","license":"Apache-2.0","description":"Who made an entity and who last changed it, when, and through what: the created and updated stamps the graph fills in on every write.","exports":{".":"./mod.ts","./vocab":"./vocab.ts","./tracker":"./tracker.ts","./authorship":"./authorship.ts"},"imports":{"@yaks/provenance":"./mod.ts","@yaks/provenance/vocab":"./vocab.ts","@yaks/provenance/tracker":"./tracker.ts","@yaks/provenance/authorship":"./authorship.ts"}}
package/mod.js ADDED
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Who made an entity and who last changed it: `created{at, by, via}` is set
3
+ * once, when the entity is made, and `updated{at, by, via}` on every later
4
+ * write. @yaks/graph's stamp phase fills both in for whatever vocabulary
5
+ * declares them, so a graph composed without this package keeps no stamps.
6
+ *
7
+ * `@yaks/provenance/tracker` retains the writers of surviving words and values
8
+ * as `authorship{fields}` for a store that composes it; it is not a facet, so
9
+ * listing this package keeps just the stamps.
10
+ */ export { provenanceDoc } from "./vocab.js";
11
+ //# sourceMappingURL=mod.js.map
package/mod.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.js","sources":["./mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;CASC,GAED,SAAS,aAAa,qBAAoB"}
package/mod.ts ADDED
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Who made an entity and who last changed it: `created{at, by, via}` is set
3
+ * once, when the entity is made, and `updated{at, by, via}` on every later
4
+ * write. @yaks/graph's stamp phase fills both in for whatever vocabulary
5
+ * declares them, so a graph composed without this package keeps no stamps.
6
+ *
7
+ * `@yaks/provenance/tracker` retains the writers of surviving words and values
8
+ * as `authorship{fields}` for a store that composes it; it is not a facet, so
9
+ * listing this package keeps just the stamps.
10
+ */
11
+
12
+ export { provenanceDoc } from './vocab.js'
package/package.json CHANGED
@@ -1,6 +1,32 @@
1
1
  {
2
2
  "name": "@yaks/provenance",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.36",
4
+ "homepage": "https://jsr.io/@yaks/provenance",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./_dist/mod.d.ts",
9
+ "default": "./mod.js"
10
+ },
11
+ "./vocab": {
12
+ "types": "./_dist/vocab.d.ts",
13
+ "default": "./vocab.js"
14
+ },
15
+ "./tracker": {
16
+ "types": "./_dist/tracker.d.ts",
17
+ "default": "./tracker.js"
18
+ },
19
+ "./authorship": {
20
+ "types": "./_dist/authorship.d.ts",
21
+ "default": "./authorship.js"
22
+ }
23
+ },
24
+ "license": "Apache-2.0",
25
+ "dependencies": {
26
+ "@yaks/fp": "0.2.36",
27
+ "@yaks/graph": "0.2.36",
28
+ "@yaks/vocab": "0.2.36"
29
+ },
30
+ "description": "Who made an entity and who last changed it, when, and through what: the created and updated stamps the graph fills in on every write.",
31
+ "repository": "https://github.com/yak-sh/yak.git"
32
+ }
package/tracker.js ADDED
@@ -0,0 +1,83 @@
1
+ // Live value attribution, opt-in: a store that wants it composes this tracker
2
+ // (@yaks/platform's `yak/authorship` for app stores). It is not a facet, so a
3
+ // graph that lists this package gets the stamps alone.
4
+ import { after, same } from "@yaks/fp";
5
+ import { writers } from "@yaks/graph/provenance";
6
+ import { retained } from "./authorship.js";
7
+ /** Retain value writers inside the same transaction as the values. */ export let authorship = (vocab)=>({
8
+ name: '@yaks/provenance',
9
+ admission: ()=>true,
10
+ wants: (bundles)=>[
11
+ {
12
+ eids: bundles.map((b)=>b.entity.eid)
13
+ }
14
+ ],
15
+ track: (tx, found, context)=>{
16
+ let before = new Map();
17
+ let deleted = new Set();
18
+ let writer = writers(context?.bundles ?? []);
19
+ let remember = (eids)=>{
20
+ let missing = [
21
+ ...new Set(eids)
22
+ ].filter((eid)=>!before.has(eid));
23
+ let unknown = missing.filter((eid)=>found(eid) === undefined);
24
+ return after(unknown.length ? tx.get(unknown) : [], (rows)=>{
25
+ let read = new Map(rows.map((b)=>[
26
+ b.entity.eid,
27
+ b
28
+ ]));
29
+ for (let eid of missing)before.set(eid, found(eid) ?? read.get(eid));
30
+ });
31
+ };
32
+ let wrapped = {
33
+ ...tx,
34
+ patch: (bundles)=>after(remember(bundles.map((b)=>b.entity.eid)), ()=>tx.patch(bundles)),
35
+ remove: (entities)=>{
36
+ for (let e of entities)deleted.add(e.eid);
37
+ return tx.remove(entities);
38
+ }
39
+ };
40
+ return {
41
+ tx: wrapped,
42
+ flush: (bundles)=>{
43
+ let eids = [
44
+ ...before.keys()
45
+ ].filter((eid)=>!deleted.has(eid));
46
+ if (!eids.length) return bundles;
47
+ return after(tx.get(eids), (rows)=>{
48
+ // Attribution is this facet's own bookkeeping: quiet, so it rides on
49
+ // an entity the write already returns and never adds one to the
50
+ // answer (@yaks/graph `composed`).
51
+ let patches = [];
52
+ let applied = rows.map((row)=>{
53
+ let { eid } = row.entity;
54
+ let fields = retained(before.get(eid), row, writer(eid), vocab);
55
+ let authorship = {
56
+ fields
57
+ };
58
+ if (!same(row.authorship, authorship)) {
59
+ patches.push({
60
+ entity: {
61
+ eid
62
+ },
63
+ $quiet: true,
64
+ authorship
65
+ });
66
+ }
67
+ return {
68
+ entity: row.entity,
69
+ $quiet: true,
70
+ authorship
71
+ };
72
+ });
73
+ before.clear();
74
+ return after(patches.length ? tx.patch(patches) : undefined, ()=>[
75
+ ...bundles,
76
+ ...applied
77
+ ]);
78
+ });
79
+ }
80
+ };
81
+ }
82
+ });
83
+ //# sourceMappingURL=tracker.js.map
package/tracker.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tracker.js","sources":["./tracker.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,8EAA8E;AAC9E,uDAAuD;AACvD,SAAS,KAAK,EAAE,IAAI,wBAA6B;AAEjD,SAAS,OAAO,sCAA2C;AAE3D,SAAS,QAAQ,0BAAyB;AAE1C,oEAAoE,GACpE,OAAO,IAAI,aAAa,CAAC,QAAyB,CAAC;IACjD,MAAM;IACN,WAAW,IAAM;IACjB,OAAO,CAAC,UAAY;QAAC;UAAE,MAAM,QAAQ,GAAG,CAAC,CAAC,IAAM,EAAE,MAAM,CAAC,GAAG;QAAE;OAAE;IAChE,OAAO,CAAC,IAAI,OAAO;MACjB,IAAI,SAAS,IAAI;MACjB,IAAI,UAAU,IAAI;MAClB,IAAI,SAAS,QAAQ,SAAS,WAAW,EAAE;MAC3C,IAAI,WAAW,CAAC;QACd,IAAI,UAAU;aAAI,IAAI,IAAI;SAAM,CAAC,MAAM,CAAC,CAAC,MAAQ,CAAC,OAAO,GAAG,CAAC;QAC7D,IAAI,UAAU,QAAQ,MAAM,CAAC,CAAC,MAAQ,MAAM,SAAS;QACrD,OAAO,MAAM,QAAQ,MAAM,GAAG,GAAG,GAAG,CAAC,WAAW,EAAE,EAAE,CAAC;UACnD,IAAI,OAAO,IAAI,IAAI,KAAK,GAAG,CAAC,CAAC,IAAM;cAAC,EAAE,MAAM,CAAC,GAAG;cAAE;aAAE;UACpD,KAAK,IAAI,OAAO,Q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+ // Live value attribution, opt-in: a store that wants it composes this tracker
2
+ // (@yaks/platform's `yak/authorship` for app stores). It is not a facet, so a
3
+ // graph that lists this package gets the stamps alone.
4
+ import { after, same } from '@yaks/fp'
5
+ import type { Bundle, Plugin, Tracker, Tx } from 'jsr:@yaks/graph@0.2.36'
6
+ import { writers } from '@yaks/graph/provenance'
7
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
8
+ import { retained } from './authorship.js'
9
+
10
+ /** Retain value writers inside the same transaction as the values. */
11
+ export let authorship = (vocab: Vocab): Plugin => ({
12
+ name: '@yaks/provenance',
13
+ admission: () => true,
14
+ wants: (bundles) => [{ eids: bundles.map((b) => b.entity.eid) }],
15
+ track: (tx, found, context): Tracker => {
16
+ let before = new Map<string, Bundle | undefined>()
17
+ let deleted = new Set<string>()
18
+ let writer = writers(context?.bundles ?? [])
19
+ let remember = (eids: string[]) => {
20
+ let missing = [...new Set(eids)].filter((eid) => !before.has(eid))
21
+ let unknown = missing.filter((eid) => found(eid) === undefined)
22
+ return after(unknown.length ? tx.get(unknown) : [], (rows) => {
23
+ let read = new Map(rows.map((b) => [b.entity.eid, b]))
24
+ for (let eid of missing) before.set(eid, found(eid) ?? read.get(eid))
25
+ })
26
+ }
27
+ let wrapped: Tx = {
28
+ ...tx,
29
+ patch: (bundles) =>
30
+ after(
31
+ remember(bundles.map((b) => b.entity.eid)),
32
+ () => tx.patch(bundles),
33
+ ),
34
+ remove: (entities) => {
35
+ for (let e of entities) deleted.add(e.eid)
36
+ return tx.remove(entities)
37
+ },
38
+ }
39
+ return {
40
+ tx: wrapped,
41
+ flush: (bundles) => {
42
+ let eids = [...before.keys()].filter((eid) => !deleted.has(eid))
43
+ if (!eids.length) return bundles
44
+ return after(tx.get(eids), (rows) => {
45
+ // Attribution is this facet's own bookkeeping: quiet, so it rides on
46
+ // an entity the write already returns and never adds one to the
47
+ // answer (@yaks/graph `composed`).
48
+ let patches: Bundle[] = []
49
+ let applied = rows.map((row): Bundle => {
50
+ let { eid } = row.entity
51
+ let fields = retained(before.get(eid), row, writer(eid), vocab)
52
+ let authorship = { fields }
53
+ if (!same(row.authorship, authorship)) {
54
+ patches.push({ entity: { eid }, $quiet: true, authorship })
55
+ }
56
+ return { entity: row.entity, $quiet: true, authorship }
57
+ })
58
+ before.clear()
59
+ return after(
60
+ patches.length ? tx.patch(patches) : undefined,
61
+ () => [...bundles, ...applied],
62
+ )
63
+ })
64
+ },
65
+ }
66
+ },
67
+ })
package/vocab.js ADDED
@@ -0,0 +1,18 @@
1
+ // The component declarations, and nothing else: the module a server or a
2
+ // browser page imports at `@yaks/provenance/vocab`. It reaches no storage, no
3
+ // SQL and no runtime, so a browser tab loading this vocabulary loads nothing
4
+ // else.
5
+ import doc from './vocab.json' with {
6
+ type: 'json'
7
+ };
8
+ import manifest from './package.json' with {
9
+ type: 'json'
10
+ };
11
+ /** `created{at, by, via}` and `updated{at, by, via}`, in the form `loadVocab`
12
+ * accepts, alongside the optional `authorship{fields}` value attribution. */ export let provenanceDoc = doc;
13
+ /** What this package is: its manifest's description, the one place it is
14
+ * written, which a host stamps on each document here. */ export let description = manifest.description;
15
+ /** Every document this plugin declares. */ export let docs = [
16
+ provenanceDoc
17
+ ];
18
+ //# sourceMappingURL=vocab.js.map
package/vocab.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vocab.js","sources":["./vocab.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,8EAA8E;AAC9E,6EAA6E;AAC7E,QAAQ;AAGR,OAAO,SAAS,oBAAoB;EAAE,MAAM;AAAO,EAAC;AACpD,OAAO,cAAc,sBAAsB;EAAE,MAAM;AAAO,EAAC;AAE3D;2EAC2E,GAC3E,OAAO,IAAI,gBAA0B,IAAe;AAEpD;uDACuD,GACvD,OAAO,IAAI,cAAsB,SAAS,WAAW,CAAA;AAErD,yCAAyC,GACzC,OAAO,IAAI,OAAmB;EAAC;CAAc,CAAA"}
package/vocab.json ADDED
@@ -0,0 +1,107 @@
1
+ {
2
+ "$id": "urn:uuid:fe640674-02a0-4cf8-8281-a374152a6e84",
3
+ "$vocabulary": {
4
+ "https://yak.sh/vocab/core": true
5
+ },
6
+ "title": "provenance",
7
+ "$defs": {
8
+ "created": {
9
+ "$id": "urn:uuid:d3a388b2-b026-41dc-b091-ae6d084ce0a4",
10
+ "component": true,
11
+ "type": "object",
12
+ "index": [["by", "at"]],
13
+ "journal": false,
14
+ "properties": {
15
+ "by": {
16
+ "$id": "urn:uuid:60cb742d-47df-468d-958f-f78bdb49549c",
17
+ "type": "string",
18
+ "ref": "entity",
19
+ "death": "keep",
20
+ "stamped": true
21
+ },
22
+ "at": {
23
+ "$id": "urn:uuid:cbccaaab-b8d7-4860-b73b-c092c87bfc4c",
24
+ "type": "string",
25
+ "format": "date-time",
26
+ "stamped": true,
27
+ "index": true
28
+ },
29
+ "via": {
30
+ "$id": "urn:uuid:9edc3533-4ff4-4749-9759-57000d562c70",
31
+ "type": "string",
32
+ "ref": "entity",
33
+ "death": "keep",
34
+ "stamped": true
35
+ }
36
+ },
37
+ "description": "who made it, when, and through what"
38
+ },
39
+ "updated": {
40
+ "$id": "urn:uuid:430587e2-f666-41d8-90b3-119a86653420",
41
+ "component": true,
42
+ "type": "object",
43
+ "index": [["by", "at"]],
44
+ "journal": false,
45
+ "properties": {
46
+ "by": {
47
+ "$id": "urn:uuid:4c43cbde-6060-41b6-bce2-1d4938d70f0d",
48
+ "type": "string",
49
+ "ref": "entity",
50
+ "death": "keep",
51
+ "stamped": true
52
+ },
53
+ "at": {
54
+ "$id": "urn:uuid:f5807155-4539-4058-a0ab-14e19b285aae",
55
+ "type": "string",
56
+ "format": "date-time",
57
+ "stamped": true,
58
+ "index": true
59
+ },
60
+ "via": {
61
+ "$id": "urn:uuid:5dab6c48-1be3-47eb-bb2b-e9caf7dfff9c",
62
+ "type": "string",
63
+ "ref": "entity",
64
+ "death": "keep",
65
+ "stamped": true
66
+ }
67
+ },
68
+ "description": "who last changed it, when, and through what"
69
+ },
70
+ "authorship": {
71
+ "$id": "urn:uuid:f27d1d00-c17d-4f12-8889-260cb7f698d9",
72
+ "component": true,
73
+ "type": "object",
74
+ "wire": false,
75
+ "journal": false,
76
+ "description": "the writers of the primitive values and text spans the entity still holds",
77
+ "properties": {
78
+ "fields": {
79
+ "$id": "urn:uuid:ddecaca4-3002-48a1-a638-47f624ba26ce",
80
+ "type": "object",
81
+ "stamped": true,
82
+ "description": "by JSON-encoded component/property/leaf path, its writer spans; from and to are UTF-16 offsets for strings, otherwise 0 and 1",
83
+ "additionalProperties": {
84
+ "type": "array",
85
+ "items": {
86
+ "type": "object",
87
+ "properties": {
88
+ "from": {
89
+ "type": "integer"
90
+ },
91
+ "to": {
92
+ "type": "integer"
93
+ },
94
+ "by": {
95
+ "type": "string"
96
+ },
97
+ "via": {
98
+ "type": "string"
99
+ }
100
+ }
101
+ }
102
+ }
103
+ }
104
+ }
105
+ }
106
+ }
107
+ }
package/vocab.ts ADDED
@@ -0,0 +1,19 @@
1
+ // The component declarations, and nothing else: the module a server or a
2
+ // browser page imports at `@yaks/provenance/vocab`. It reaches no storage, no
3
+ // SQL and no runtime, so a browser tab loading this vocabulary loads nothing
4
+ // else.
5
+
6
+ import type { VocabDoc } from 'jsr:@yaks/vocab@0.2.36'
7
+ import doc from './vocab.json' with { type: 'json' }
8
+ import manifest from './package.json' with { type: 'json' }
9
+
10
+ /** `created{at, by, via}` and `updated{at, by, via}`, in the form `loadVocab`
11
+ * accepts, alongside the optional `authorship{fields}` value attribution. */
12
+ export let provenanceDoc: VocabDoc = doc as VocabDoc
13
+
14
+ /** What this package is: its manifest's description, the one place it is
15
+ * written, which a host stamps on each document here. */
16
+ export let description: string = manifest.description
17
+
18
+ /** Every document this plugin declares. */
19
+ export let docs: VocabDoc[] = [provenanceDoc]