@yaks/persona 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 +202 -0
- package/README.md +401 -2
- package/_dist/comp.d.ts +13 -0
- package/_dist/comp.d.ts.map +1 -0
- package/_dist/effects.d.ts +22 -0
- package/_dist/effects.d.ts.map +1 -0
- package/_dist/mod.d.ts +40 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/name.d.ts +49 -0
- package/_dist/name.d.ts.map +1 -0
- package/_dist/owed.d.ts +56 -0
- package/_dist/owed.d.ts.map +1 -0
- package/_dist/part.d.ts +4 -0
- package/_dist/part.d.ts.map +1 -0
- package/_dist/shipped.d.ts +30 -0
- package/_dist/shipped.d.ts.map +1 -0
- package/_dist/skill-checkout.d.ts +6 -0
- package/_dist/skill-checkout.d.ts.map +1 -0
- package/_dist/skill-effects.d.ts +25 -0
- package/_dist/skill-effects.d.ts.map +1 -0
- package/_dist/skill-mirror-io.d.ts +7 -0
- package/_dist/skill-mirror-io.d.ts.map +1 -0
- package/_dist/skill-mirror.d.ts +15 -0
- package/_dist/skill-mirror.d.ts.map +1 -0
- package/_dist/skill-text.d.ts +28 -0
- package/_dist/skill-text.d.ts.map +1 -0
- package/_dist/skills.d.ts +22 -0
- package/_dist/skills.d.ts.map +1 -0
- package/_dist/tools.d.ts +9 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/ui.d.ts +6 -0
- package/_dist/ui.d.ts.map +1 -0
- package/_dist/views.d.ts +19 -0
- package/_dist/views.d.ts.map +1 -0
- package/_dist/vocab.d.ts +7 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/_dist/voice.d.ts +47 -0
- package/_dist/voice.d.ts.map +1 -0
- package/_dist/worn.d.ts +55 -0
- package/_dist/worn.d.ts.map +1 -0
- package/carried.js +201 -0
- package/carried.js.map +1 -0
- package/carried.ts +269 -0
- package/comp.js +31 -0
- package/comp.js.map +1 -0
- package/comp.ts +38 -0
- package/deno.json +1 -0
- package/desk.js +201 -0
- package/desk.js.map +1 -0
- package/desk.ts +301 -0
- package/effects.js +106 -0
- package/effects.js.map +1 -0
- package/effects.ts +120 -0
- package/files.js +305 -0
- package/files.js.map +1 -0
- package/files.ts +319 -0
- package/inspect.js +66 -0
- package/inspect.js.map +1 -0
- package/inspect.ts +30 -0
- package/mod.js +40 -0
- package/mod.js.map +1 -0
- package/mod.ts +61 -0
- package/name.js +161 -0
- package/name.js.map +1 -0
- package/name.ts +184 -0
- package/owed.js +196 -0
- package/owed.js.map +1 -0
- package/owed.ts +256 -0
- package/package.json +83 -4
- package/part.js +4 -0
- package/part.js.map +1 -0
- package/part.ts +4 -0
- package/shipped.js +130 -0
- package/shipped.js.map +1 -0
- package/shipped.ts +131 -0
- package/skill-checkout.js +92 -0
- package/skill-checkout.js.map +1 -0
- package/skill-checkout.ts +121 -0
- package/skill-effects.js +244 -0
- package/skill-effects.js.map +1 -0
- package/skill-effects.ts +274 -0
- package/skill-mirror-io.js +193 -0
- package/skill-mirror-io.js.map +1 -0
- package/skill-mirror-io.ts +212 -0
- package/skill-mirror.js +659 -0
- package/skill-mirror.js.map +1 -0
- package/skill-mirror.ts +640 -0
- package/skill-text.js +158 -0
- package/skill-text.js.map +1 -0
- package/skill-text.ts +183 -0
- package/skills.js +238 -0
- package/skills.js.map +1 -0
- package/skills.ts +383 -0
- package/text.js +59 -0
- package/text.js.map +1 -0
- package/text.ts +60 -0
- package/tools.js +76 -0
- package/tools.js.map +1 -0
- package/tools.ts +91 -0
- package/ui.js +19 -0
- package/ui.js.map +1 -0
- package/ui.ts +34 -0
- package/views.js +27 -0
- package/views.js.map +1 -0
- package/views.ts +29 -0
- package/vocab.js +14 -0
- package/vocab.js.map +1 -0
- package/vocab.json +270 -0
- package/vocab.ts +16 -0
- package/voice.js +105 -0
- package/voice.js.map +1 -0
- package/voice.ts +133 -0
- package/worn.js +328 -0
- package/worn.js.map +1 -0
- package/worn.ts +336 -0
package/name.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"name.js","sources":["./name.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,2EAA2E;AAC3E,6EAA6E;AAC7E,wCAAwC;AACxC,SAAS,SAAS,kCAAuC;AAEzD;kBACkB,GAClB,OAAO,IAAI,QAAQ;EACjB;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;CACD,CAAA;AAED,mEAAmE;AACnE,IAAI,QAAQ,CAAC;EACX,IAAI,IAAI;EACR,IAAK,IAAI,IAAI,GAAG,IAAI,IAAI,MAAM,EAAE,IAAK;IACnC,IAAI,KAAK,IAAI,CAAC,IAAI,IAAI,UAAU,CAAC,IAAI;EACvC;EACA,OAAO,CAAC,MAAM,CAAC,IAAI,MAAM,MAAM;AACjC;AAEA;uDACuD,GACvD,OAAO,IAAI,OAAO,CAAC,KAAa,QAAqB,IAAI,KAAK;EAC5D,IAAI,KAAK,MAAM;EACf,IAAK,IAAI,IAAI,GAAG,IAAI,MAAM,MAAM,EAAE,IAAK;IACrC,IAAI,OAAO,KAAK,CAAC,CAAC,KAAK,CAAC,IAAI,MAAM,MAAM,CAAC;IACzC,IAAI,CAAC,MAAM,GAAG,CAAC,OAAO,OAAO;EAC/B;EACA,OAAO,KAAK,CAAC,GAAG;AAClB,EAAC;AAED;;;;;;;;;;;;CAYC,GACD,OAAO,IAAI,aAAa,CACtB,KACA;EAEA,IAAI,QAAQ,IAAI,IAAI,OAAO,OAAO,CAAC,CAAC,IAAM,EAAE,IAAI,GAAG;MAAC,EAAE,IAAI;KAAC,GAAG,EAAE;EAChE,KACE,IAAI,KAAK,OAAO,MAAM,CAAC,CAAC,IAAM,CAAC,EAAE,IAAI,IAAI,EAAE,GAAG,GAAG,KAC9C,IAAI,CAAC,CAAC,GAAG,IAAM,EAAE,GAAG,CAAC,aAAa,CAAC,EAAE,GAAG,GAC3C,MAAM,GAAG,CAAC,KAAK,EAAE,GAAG,EAAE;EACxB,OAAO,KAAK,KAAK;AACnB,EAAC;AAED,mCAAmC,GACnC,OAAO,IAAI,UAAU,GAAE;AAEvB;;;;;;;;;;;CAWC,GACD,OAAO,IAAI,QAAQ,CAAC;EAClB,IAAI,OAAO,KAAK,OAAO,CAAC,QAAQ,KAAK,IAAI;EACzC,IAAI,CAAC,MAAM,MAAM,IAAI,UAAU,aAAa;EAC5C,IAAI,KAAK,MAAM,GAAG,SAAS;IACzB,MAAM,IAAI,UAAU,aAAa,CAAC,gBAAgB,EAAE,QAAQ,YAAY,CAAC;EAC3E;EACA,OAAO;AACT,EAAC;AAED;;;;;;;;;;;CAWC,GACD,OAAO,IAAI,QAAQ,CACjB,MACA;EAEA,IAAI,OAAO,MAAM;EACjB,IAAI,OAAO,KAAK,WAAW;EAC3B,IAAI,OAAO,IAAI,CAAC,CAAC,IAAM,CAAC,EAAE,IAAI,IAAI,KAAK,EAAE,GAAG,CAAC,EAAE,WAAW,MAAM,OAAO;IACrE,MAAM,IAAI,UACR,YACA,CAAC,qCAAqC,EAAE,KAAK,wBAAwB,CAAC,GACpE;EAEN;EACA,OAAO;AACT,EAAC"}
|
package/name.ts
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
// What a persona is called: its `doc{title}`, the name alone. A new one is
|
|
2
|
+
// given one of NAMES, clear of the names of those its people already reach
|
|
3
|
+
// (`christened`), and a person may give it another (`given`), or give it one
|
|
4
|
+
// of their own as it is made (`named`).
|
|
5
|
+
import { CallError } from '@yaks/tools/errors'
|
|
6
|
+
|
|
7
|
+
/** The names a new persona is given: short, plain to say aloud, mostly trees
|
|
8
|
+
* and creatures. */
|
|
9
|
+
export let NAMES = [
|
|
10
|
+
'Acorn',
|
|
11
|
+
'Alder',
|
|
12
|
+
'Amos',
|
|
13
|
+
'Arden',
|
|
14
|
+
'Ash',
|
|
15
|
+
'Aspen',
|
|
16
|
+
'Badger',
|
|
17
|
+
'Barley',
|
|
18
|
+
'Bear',
|
|
19
|
+
'Birch',
|
|
20
|
+
'Bison',
|
|
21
|
+
'Boulder',
|
|
22
|
+
'Bracken',
|
|
23
|
+
'Bram',
|
|
24
|
+
'Bramble',
|
|
25
|
+
'Briar',
|
|
26
|
+
'Burr',
|
|
27
|
+
'Cedar',
|
|
28
|
+
'Chestnut',
|
|
29
|
+
'Cinder',
|
|
30
|
+
'Clove',
|
|
31
|
+
'Clover',
|
|
32
|
+
'Copper',
|
|
33
|
+
'Cove',
|
|
34
|
+
'Cypress',
|
|
35
|
+
'Dell',
|
|
36
|
+
'Elm',
|
|
37
|
+
'Ember',
|
|
38
|
+
'Ezra',
|
|
39
|
+
'Felix',
|
|
40
|
+
'Fern',
|
|
41
|
+
'Finch',
|
|
42
|
+
'Flint',
|
|
43
|
+
'Ford',
|
|
44
|
+
'Granite',
|
|
45
|
+
'Hawthorn',
|
|
46
|
+
'Hickory',
|
|
47
|
+
'Jasper',
|
|
48
|
+
'Juniper',
|
|
49
|
+
'Kestrel',
|
|
50
|
+
'Kip',
|
|
51
|
+
'Kit',
|
|
52
|
+
'Larch',
|
|
53
|
+
'Lark',
|
|
54
|
+
'Levi',
|
|
55
|
+
'Linden',
|
|
56
|
+
'Maple',
|
|
57
|
+
'Milo',
|
|
58
|
+
'Moose',
|
|
59
|
+
'Moss',
|
|
60
|
+
'Oak',
|
|
61
|
+
'Ollie',
|
|
62
|
+
'Otto',
|
|
63
|
+
'Pine',
|
|
64
|
+
'Pip',
|
|
65
|
+
'Quill',
|
|
66
|
+
'Reed',
|
|
67
|
+
'Ridge',
|
|
68
|
+
'Rook',
|
|
69
|
+
'Rowan',
|
|
70
|
+
'Rusty',
|
|
71
|
+
'Rye',
|
|
72
|
+
'Sage',
|
|
73
|
+
'Scout',
|
|
74
|
+
'Slate',
|
|
75
|
+
'Sparrow',
|
|
76
|
+
'Spruce',
|
|
77
|
+
'Teal',
|
|
78
|
+
'Tern',
|
|
79
|
+
'Thatch',
|
|
80
|
+
'Theo',
|
|
81
|
+
'Thistle',
|
|
82
|
+
'Tiller',
|
|
83
|
+
'Timber',
|
|
84
|
+
'Walnut',
|
|
85
|
+
'Willow',
|
|
86
|
+
'Yarrow',
|
|
87
|
+
]
|
|
88
|
+
|
|
89
|
+
// FNV-1a over the eid: where in the list an entity starts looking.
|
|
90
|
+
let start = (eid: string) => {
|
|
91
|
+
let h = 0x811c9dc5
|
|
92
|
+
for (let i = 0; i < eid.length; i++) {
|
|
93
|
+
h = Math.imul(h ^ eid.charCodeAt(i), 0x01000193)
|
|
94
|
+
}
|
|
95
|
+
return (h >>> 0) % NAMES.length
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The first name from `eid`'s place in {@link NAMES} that `taken` does not
|
|
99
|
+
* hold; the place's own name when every one is taken. */
|
|
100
|
+
export let pick = (eid: string, taken: Set<string> = new Set()): string => {
|
|
101
|
+
let at = start(eid)
|
|
102
|
+
for (let i = 0; i < NAMES.length; i++) {
|
|
103
|
+
let name = NAMES[(at + i) % NAMES.length]
|
|
104
|
+
if (!taken.has(name)) return name
|
|
105
|
+
}
|
|
106
|
+
return NAMES[at]
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** A new name for `eid`, clear of the names `others` go by: those its people
|
|
110
|
+
* already reach. An unnamed one among them that sorts first picks first, so
|
|
111
|
+
* names given in one pass, which cannot read each other, still choose apart.
|
|
112
|
+
*
|
|
113
|
+
* ```ts
|
|
114
|
+
* import { equal } from '@yaks/testing'
|
|
115
|
+
* import { christened, pick } from './name.ts'
|
|
116
|
+
* let mine = pick('b')
|
|
117
|
+
* equal(christened('b', []), mine)
|
|
118
|
+
* equal(christened('b', [{ eid: 'z', name: mine }]) != mine, true)
|
|
119
|
+
* equal(christened('b', [{ eid: 'a' }]) != pick('a'), true)
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
export let christened = (
|
|
123
|
+
eid: string,
|
|
124
|
+
others: { eid: string; name?: string | null }[],
|
|
125
|
+
): string => {
|
|
126
|
+
let taken = new Set(others.flatMap((o) => o.name ? [o.name] : []))
|
|
127
|
+
for (
|
|
128
|
+
let o of others.filter((o) => !o.name && o.eid < eid)
|
|
129
|
+
.sort((a, b) => a.eid.localeCompare(b.eid))
|
|
130
|
+
) taken.add(pick(o.eid, taken))
|
|
131
|
+
return pick(eid, taken)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** The most characters a name is. */
|
|
135
|
+
export let LONGEST = 40
|
|
136
|
+
|
|
137
|
+
/** A name a person gave: its spaces closed up, up to {@link LONGEST}
|
|
138
|
+
* characters.
|
|
139
|
+
*
|
|
140
|
+
* ```ts
|
|
141
|
+
* import { equal } from '@yaks/testing'
|
|
142
|
+
* import { assertThrows } from '@std/assert'
|
|
143
|
+
* import { given } from './name.ts'
|
|
144
|
+
* equal(given(' Ada Lovelace '), 'Ada Lovelace')
|
|
145
|
+
* assertThrows(() => given(' '), Error, 'Give it a name.')
|
|
146
|
+
* assertThrows(() => given('a'.repeat(41)), Error, 'up to 40 characters')
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
export let given = (said: string): string => {
|
|
150
|
+
let name = said.replace(/\s+/g, ' ').trim()
|
|
151
|
+
if (!name) throw new CallError('arguments', 'Give it a name.')
|
|
152
|
+
if (name.length > LONGEST) {
|
|
153
|
+
throw new CallError('arguments', `A name is up to ${LONGEST} characters.`)
|
|
154
|
+
}
|
|
155
|
+
return name
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** A name `said` for a new persona, as {@link given} takes it, refused when
|
|
159
|
+
* one of `others` (those its people already reach) goes by it, in any case.
|
|
160
|
+
*
|
|
161
|
+
* ```ts
|
|
162
|
+
* import { equal } from '@yaks/testing'
|
|
163
|
+
* import { assertThrows } from '@std/assert'
|
|
164
|
+
* import { named, pick } from './name.ts'
|
|
165
|
+
* equal(named(' Menno ', [{ eid: 'a', name: 'Cedar' }]), 'Menno')
|
|
166
|
+
* assertThrows(() => named('cedar', [{ eid: 'a', name: 'Cedar' }]), Error)
|
|
167
|
+
* assertThrows(() => named(pick('a'), [{ eid: 'a' }]), Error)
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
export let named = (
|
|
171
|
+
said: string,
|
|
172
|
+
others: { eid: string; name?: string | null }[],
|
|
173
|
+
): string => {
|
|
174
|
+
let name = given(said)
|
|
175
|
+
let fold = name.toLowerCase()
|
|
176
|
+
if (others.some((o) => (o.name || pick(o.eid)).toLowerCase() == fold)) {
|
|
177
|
+
throw new CallError(
|
|
178
|
+
'conflict',
|
|
179
|
+
`Another of their Operators is called ${name}. Give another name, or ` +
|
|
180
|
+
'leave it out and one is picked.',
|
|
181
|
+
)
|
|
182
|
+
}
|
|
183
|
+
return name
|
|
184
|
+
}
|
package/owed.js
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
// The persona a checkout carries, and what an agent working in one is owed of
|
|
2
|
+
// it.
|
|
3
|
+
//
|
|
4
|
+
// A project's common persona is the one the project `contains` whose `home` is
|
|
5
|
+
// that project. A sub-project's agents hear every common persona along its
|
|
6
|
+
// lineage (@yaks/project): their own project's first, if it has one, then each
|
|
7
|
+
// one above it, folded into one document ({@link common}). The persona files
|
|
8
|
+
// write a checkout's into the checkout a person keeps as `.tasks/AGENTS.md`
|
|
9
|
+
// (./files.ts), and every checkout of the same repository carries the same
|
|
10
|
+
// persona, whether or not that file reaches it there: an agent's own worktree
|
|
11
|
+
// has the repository's links to `.tasks` but not the ignored directory they
|
|
12
|
+
// point into.
|
|
13
|
+
//
|
|
14
|
+
// So a harness starting an agent asks {@link owed} what to give it beside the
|
|
15
|
+
// instruction files its provider reads. A file says the persona only when its
|
|
16
|
+
// text is exactly the projection the persona files write, banner and all; one
|
|
17
|
+
// that names the same persona but was written before the graph last moved is
|
|
18
|
+
// stale, and the agent reading it is still owed the persona as it stands now.
|
|
19
|
+
// An agent working on a sub-project in its parent's checkout, whose file says
|
|
20
|
+
// the parent's persona, is owed what its own project adds.
|
|
21
|
+
//
|
|
22
|
+
// Pure reads of the graph: the caller reads the files, because only it knows
|
|
23
|
+
// which ones its provider reads.
|
|
24
|
+
import { and, eq, list, present } from "@yaks/query";
|
|
25
|
+
import { Refused } from "@yaks/graph/errors";
|
|
26
|
+
import { linkable } from "@yaks/edge";
|
|
27
|
+
import { DOC, TITLE } from "@yaks/doc";
|
|
28
|
+
import { human } from "@yaks/id";
|
|
29
|
+
import { safe } from "@yaks/text";
|
|
30
|
+
import { lineage } from "@yaks/project";
|
|
31
|
+
import { PERSONA } from "./comp.js";
|
|
32
|
+
import { voice } from "./voice.js";
|
|
33
|
+
import { beside, CONTAINS, fold, wear } from "./worn.js";
|
|
34
|
+
let comp = (b, name)=>b?.[name] ?? {};
|
|
35
|
+
let str = (v)=>v == null ? '' : String(v);
|
|
36
|
+
let value = (eids)=>eids.length == 1 ? eids[0] : list(...eids);
|
|
37
|
+
let eidOf = (b)=>b.entity.eid;
|
|
38
|
+
/** Each project's common persona: the one it `contains` whose home is that
|
|
39
|
+
* project. A project with none is left out. */ export let commons = async (g, projects)=>{
|
|
40
|
+
let out = new Map();
|
|
41
|
+
let tag = linkable(g.vocab)[CONTAINS];
|
|
42
|
+
if (!tag || !projects.length) return out;
|
|
43
|
+
let homed = await g.storage.read(and(present(PERSONA), eq(`${PERSONA}.home`, value(projects))));
|
|
44
|
+
if (!homed.length) return out;
|
|
45
|
+
let held = new Set((await g.storage.read(and(eq('edge.from', value(projects)), eq('edge.to', value(homed.map(eidOf))), present(tag)))).map((b)=>`${comp(b, 'edge').from}|${comp(b, 'edge').to}`));
|
|
46
|
+
for (let p of homed.toSorted((a, b)=>eidOf(a).localeCompare(eidOf(b)))){
|
|
47
|
+
let home = str(comp(p, PERSONA).home);
|
|
48
|
+
if (!out.has(home) && held.has(`${home}|${eidOf(p)}`)) out.set(home, p);
|
|
49
|
+
}
|
|
50
|
+
return out;
|
|
51
|
+
};
|
|
52
|
+
/** The common persona an agent working on `eid` (a task, or a project) hears:
|
|
53
|
+
* each common persona along its lineage, nearest first, folded into one. A
|
|
54
|
+
* sub-project without one of its own hears its parent's. */ export let common = async (g, eid)=>{
|
|
55
|
+
let line = (await lineage(g, eid)).map(eidOf);
|
|
56
|
+
let bases = await commons(g, line);
|
|
57
|
+
let wearing = wear(g.storage, g.vocab);
|
|
58
|
+
let worn = [];
|
|
59
|
+
for (let p of line){
|
|
60
|
+
let base = bases.get(p);
|
|
61
|
+
let w = base && await wearing(eidOf(base));
|
|
62
|
+
if (w) worn.push(w);
|
|
63
|
+
}
|
|
64
|
+
return worn.length ? fold(worn[0], ...worn.slice(1)) : undefined;
|
|
65
|
+
};
|
|
66
|
+
/** The line a persona file opens with: which persona it was generated from,
|
|
67
|
+
* and where to edit it. */ export let banner = (vocab)=>{
|
|
68
|
+
let id = human(vocab);
|
|
69
|
+
return (p)=>`<!-- GENERATED from ${id(p)} (${safe(str(comp(p, DOC)[TITLE]))}) — ` + 'edit it in the graph, never here: the next sync overwrites hand edits. -->';
|
|
70
|
+
};
|
|
71
|
+
/** A persona as the file projecting it into a checkout says it: its
|
|
72
|
+
* {@link banner}, then its {@link voice}. */ export let projection = (vocab)=>{
|
|
73
|
+
let head = banner(vocab);
|
|
74
|
+
let render = voice(vocab);
|
|
75
|
+
return (w)=>`${head(w.persona)}\n\n${render(w)}`;
|
|
76
|
+
};
|
|
77
|
+
// Every directory from `path` up, nearest first.
|
|
78
|
+
let upward = (path)=>{
|
|
79
|
+
let out = [];
|
|
80
|
+
for(let d = path.replace(/\/+$/, ''); d; d = d.slice(0, d.lastIndexOf('/'))){
|
|
81
|
+
out.push(d);
|
|
82
|
+
}
|
|
83
|
+
return out;
|
|
84
|
+
};
|
|
85
|
+
/** The common persona the checkout holding `path` carries: the nearest
|
|
86
|
+
* directory at or above `path` that the graph knows as a worktree, then the
|
|
87
|
+
* first project filed with its repository, by eid, that has one along its
|
|
88
|
+
* lineage. A session started anywhere in a checkout wears this. */ export let carried = async (g, path)=>{
|
|
89
|
+
let needs = [
|
|
90
|
+
'worktree',
|
|
91
|
+
'repo',
|
|
92
|
+
'project',
|
|
93
|
+
PERSONA
|
|
94
|
+
];
|
|
95
|
+
if (!needs.every((c)=>g.vocab.comp(c))) return;
|
|
96
|
+
let dirs = upward(path);
|
|
97
|
+
if (!dirs.length) return;
|
|
98
|
+
let known = await g.get((await g.storage.read(and(present('worktree'), eq('workspace.root', value(dirs))))).map((b)=>b.entity.eid));
|
|
99
|
+
let at = (b)=>str(comp(b, 'workspace').root);
|
|
100
|
+
let nearest = dirs.find((d)=>known.some((b)=>at(b) == d));
|
|
101
|
+
let repos = known.filter((b)=>at(b) == nearest).map((b)=>str(comp(b, 'worktree').repository)).filter(Boolean);
|
|
102
|
+
if (!repos.length) return;
|
|
103
|
+
let projects = (await g.storage.read(and(present('project'), eq('repo.repository', value(repos))))).filter((b)=>!b.archived).map(eidOf).sort();
|
|
104
|
+
for (let p of projects){
|
|
105
|
+
let w = await common(g, p);
|
|
106
|
+
if (w) return w;
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
// Who an agent is: the persona chosen for it; else the common persona of its
|
|
110
|
+
// work, else the one its checkout carries (`here`, which a file in that
|
|
111
|
+
// checkout may already say).
|
|
112
|
+
let resolved = async (g, path, as)=>{
|
|
113
|
+
if (as.persona) {
|
|
114
|
+
let found = await g.address([
|
|
115
|
+
as.persona
|
|
116
|
+
]);
|
|
117
|
+
let worn = await wear(g.storage, g.vocab)(found.get(as.persona) ?? as.persona);
|
|
118
|
+
if (!worn) throw new Refused(`no persona called ${as.persona}`);
|
|
119
|
+
return {
|
|
120
|
+
worn
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
let here = await carried(g, path);
|
|
124
|
+
return {
|
|
125
|
+
worn: as.work && await common(g, as.work) || here,
|
|
126
|
+
here
|
|
127
|
+
};
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* The persona an agent working in the checkout at `path` wears, whole,
|
|
131
|
+
* whatever its instruction files already say of it: the one `as.persona`
|
|
132
|
+
* chooses, else the common persona of `as.work`'s lineage, else the one the
|
|
133
|
+
* checkout carries. Nothing when none of them has one. {@link offer} reads the
|
|
134
|
+
* tools it offers from it.
|
|
135
|
+
*
|
|
136
|
+
* ```ts ignore
|
|
137
|
+
* let tools = offer(await wearing(g, '', { persona: 'N-7' }))
|
|
138
|
+
* ```
|
|
139
|
+
*/ export let wearing = async (g, path, as = {})=>(await resolved(g, path, as)).worn;
|
|
140
|
+
/**
|
|
141
|
+
* What an agent working in the checkout at `path` is owed of its persona,
|
|
142
|
+
* given the text of each instruction file its provider reads there. By
|
|
143
|
+
* default that is the common persona the checkout's repository carries;
|
|
144
|
+
* `as.work` makes it the common persona of the work's lineage, less whatever a
|
|
145
|
+
* file there already says of the checkout's. Nothing when a file already says
|
|
146
|
+
* it all, when the graph knows no checkout holding `path` and no work, or when
|
|
147
|
+
* nothing along the way has a common persona. `as.persona` selects a graph
|
|
148
|
+
* persona instead, even when the checkout is not known to the graph.
|
|
149
|
+
*
|
|
150
|
+
* ```ts ignore
|
|
151
|
+
* let owes = await owed(g, '/srv/runs/S-1', [agentsMd], { work: 'T-7' })
|
|
152
|
+
* if (owes) args.push('--append-system-prompt', owes.text)
|
|
153
|
+
* ```
|
|
154
|
+
*/ export let owed = async (g, path, files, as = {})=>{
|
|
155
|
+
let says = (w)=>files.includes(projection(g.vocab)(w));
|
|
156
|
+
let { worn, here } = await resolved(g, path, as);
|
|
157
|
+
if (!worn || says(worn)) return;
|
|
158
|
+
let w = here && says(here) ? beside(worn, here) : worn;
|
|
159
|
+
return {
|
|
160
|
+
source: human(g.vocab)(w.persona),
|
|
161
|
+
text: voice(g.vocab)(w)
|
|
162
|
+
};
|
|
163
|
+
};
|
|
164
|
+
// Whether `worn` says everything `said` does.
|
|
165
|
+
let within = (worn, said)=>{
|
|
166
|
+
let all = new Set([
|
|
167
|
+
worn.persona,
|
|
168
|
+
...worn.contains
|
|
169
|
+
].map(eidOf));
|
|
170
|
+
return [
|
|
171
|
+
said.persona,
|
|
172
|
+
...said.contains
|
|
173
|
+
].every((b)=>all.has(eidOf(b)));
|
|
174
|
+
};
|
|
175
|
+
/**
|
|
176
|
+
* The instruction files an agent still reads when its host says its persona
|
|
177
|
+
* whole, afresh on every ask ({@link wearing}): each of `files` but one that
|
|
178
|
+
* is exactly the projection of the persona it wears, or of the checkout's
|
|
179
|
+
* where the worn one says all of it, which would say the same twice and go
|
|
180
|
+
* stale beside it.
|
|
181
|
+
*
|
|
182
|
+
* ```ts ignore
|
|
183
|
+
* let kept = await unsaid(g, '/srv/runs/S-1', [agentsMd], { work: 'T-7' })
|
|
184
|
+
* ```
|
|
185
|
+
*/ export let unsaid = async (g, path, files, as = {})=>{
|
|
186
|
+
let { worn, here } = await resolved(g, path, as);
|
|
187
|
+
if (!worn) return files;
|
|
188
|
+
let said = [
|
|
189
|
+
worn,
|
|
190
|
+
...here && within(worn, here) ? [
|
|
191
|
+
here
|
|
192
|
+
] : []
|
|
193
|
+
].map(projection(g.vocab));
|
|
194
|
+
return files.filter((f)=>!said.includes(f));
|
|
195
|
+
};
|
|
196
|
+
//# sourceMappingURL=owed.js.map
|
package/owed.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"owed.js","sources":["./owed.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,MAAM;AACN,EAAE;AACF,+EAA+E;AAC/E,2EAA2E;AAC3E,+EAA+E;AAC/E,6EAA6E;AAC7E,4EAA4E;AAC5E,2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OAAO;IAAE,QAAQ,MAAM,EAAE,KAAK,EAAE,EAAE,OAAO;IAAG,MAAM,MAAM,EAAE,KAAK,EAAE;EAAG;AACtE,EAAC;AAED,8CAA8C;AAC9C,IAAI,SAAS,CAAC,MAAY;EACxB,IAAI,MAAM,IAAI,IAAI;IAAC,KAAK,OAAO;OAAK,KAAK,QAAQ;GAAC,CAAC,GAAG,CAAC;EACvD,OAAO;IAAC,KAAK,OAAO;OAAK,KAAK,QAAQ;GAAC,CAAC,KAAK,CAAC,CAAC,IAAM,IAAI,GAAG,CAAC,MAAM;AACrE;AAEA;;;;;;;;;;CAUC,GACD,OAAO,IAAI,SAAS,OAClB,GACA,MACA,OACA,KAAS,CAAC,CAAC;EAEX,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,MAAM,SAAS,GAAG,MAAM;EAC7C,IAAI,CAAC,MAAM,OAAO;EAClB,IAAI,OAAO;IAAC;OAAS,QAAQ,OAAO,MAAM,QAAQ;MAAC;KAAK,GAAG,EAAE;GAAC,CAC3D,GAAG,CAAC,WAAW,EAAE,KAAK;EACzB,OAAO,MAAM,MAAM,CAAC,CAAC,IAAM,CAAC,KAAK,QAAQ,CAAC;AAC5C,EAAC"}
|
package/owed.ts
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
// The persona a checkout carries, and what an agent working in one is owed of
|
|
2
|
+
// it.
|
|
3
|
+
//
|
|
4
|
+
// A project's common persona is the one the project `contains` whose `home` is
|
|
5
|
+
// that project. A sub-project's agents hear every common persona along its
|
|
6
|
+
// lineage (@yaks/project): their own project's first, if it has one, then each
|
|
7
|
+
// one above it, folded into one document ({@link common}). The persona files
|
|
8
|
+
// write a checkout's into the checkout a person keeps as `.tasks/AGENTS.md`
|
|
9
|
+
// (./files.ts), and every checkout of the same repository carries the same
|
|
10
|
+
// persona, whether or not that file reaches it there: an agent's own worktree
|
|
11
|
+
// has the repository's links to `.tasks` but not the ignored directory they
|
|
12
|
+
// point into.
|
|
13
|
+
//
|
|
14
|
+
// So a harness starting an agent asks {@link owed} what to give it beside the
|
|
15
|
+
// instruction files its provider reads. A file says the persona only when its
|
|
16
|
+
// text is exactly the projection the persona files write, banner and all; one
|
|
17
|
+
// that names the same persona but was written before the graph last moved is
|
|
18
|
+
// stale, and the agent reading it is still owed the persona as it stands now.
|
|
19
|
+
// An agent working on a sub-project in its parent's checkout, whose file says
|
|
20
|
+
// the parent's persona, is owed what its own project adds.
|
|
21
|
+
//
|
|
22
|
+
// Pure reads of the graph: the caller reads the files, because only it knows
|
|
23
|
+
// which ones its provider reads.
|
|
24
|
+
|
|
25
|
+
import { and, eq, type Input, list, present } from '@yaks/query'
|
|
26
|
+
import type { Bundle, Comp, Eid, Graph } from 'jsr:@yaks/graph@0.2.36'
|
|
27
|
+
import { Refused } from '@yaks/graph/errors'
|
|
28
|
+
import { linkable } from '@yaks/edge'
|
|
29
|
+
import { DOC, TITLE } from '@yaks/doc'
|
|
30
|
+
import { human } from '@yaks/id'
|
|
31
|
+
import { safe } from '@yaks/text'
|
|
32
|
+
import { lineage } from '@yaks/project'
|
|
33
|
+
import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
|
|
34
|
+
import { PERSONA } from './comp.js'
|
|
35
|
+
import { voice, type Worn } from './voice.js'
|
|
36
|
+
import { beside, CONTAINS, fold, wear } from './worn.js'
|
|
37
|
+
|
|
38
|
+
let comp = (b: Bundle | undefined, name: string): Comp =>
|
|
39
|
+
(b?.[name] ?? {}) as Comp
|
|
40
|
+
|
|
41
|
+
let str = (v: unknown): string => v == null ? '' : String(v)
|
|
42
|
+
|
|
43
|
+
let value = (eids: Eid[]): Input => eids.length == 1 ? eids[0] : list(...eids)
|
|
44
|
+
|
|
45
|
+
let eidOf = (b: Bundle): Eid => b.entity.eid
|
|
46
|
+
|
|
47
|
+
/** Each project's common persona: the one it `contains` whose home is that
|
|
48
|
+
* project. A project with none is left out. */
|
|
49
|
+
export let commons = async (
|
|
50
|
+
g: Graph,
|
|
51
|
+
projects: Eid[],
|
|
52
|
+
): Promise<Map<Eid, Bundle>> => {
|
|
53
|
+
let out = new Map<Eid, Bundle>()
|
|
54
|
+
let tag = linkable(g.vocab)[CONTAINS]
|
|
55
|
+
if (!tag || !projects.length) return out
|
|
56
|
+
let homed = await g.storage.read(
|
|
57
|
+
and(present(PERSONA), eq(`${PERSONA}.home`, value(projects))),
|
|
58
|
+
)
|
|
59
|
+
if (!homed.length) return out
|
|
60
|
+
let held = new Set(
|
|
61
|
+
(await g.storage.read(
|
|
62
|
+
and(
|
|
63
|
+
eq('edge.from', value(projects)),
|
|
64
|
+
eq('edge.to', value(homed.map(eidOf))),
|
|
65
|
+
present(tag),
|
|
66
|
+
),
|
|
67
|
+
)).map((b) => `${comp(b, 'edge').from}|${comp(b, 'edge').to}`),
|
|
68
|
+
)
|
|
69
|
+
for (let p of homed.toSorted((a, b) => eidOf(a).localeCompare(eidOf(b)))) {
|
|
70
|
+
let home = str(comp(p, PERSONA).home)
|
|
71
|
+
if (!out.has(home) && held.has(`${home}|${eidOf(p)}`)) out.set(home, p)
|
|
72
|
+
}
|
|
73
|
+
return out
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The common persona an agent working on `eid` (a task, or a project) hears:
|
|
77
|
+
* each common persona along its lineage, nearest first, folded into one. A
|
|
78
|
+
* sub-project without one of its own hears its parent's. */
|
|
79
|
+
export let common = async (
|
|
80
|
+
g: Graph,
|
|
81
|
+
eid: Eid,
|
|
82
|
+
): Promise<Worn | undefined> => {
|
|
83
|
+
let line = (await lineage(g, eid)).map(eidOf)
|
|
84
|
+
let bases = await commons(g, line)
|
|
85
|
+
let wearing = wear(g.storage, g.vocab)
|
|
86
|
+
let worn: Worn[] = []
|
|
87
|
+
for (let p of line) {
|
|
88
|
+
let base = bases.get(p)
|
|
89
|
+
let w = base && await wearing(eidOf(base))
|
|
90
|
+
if (w) worn.push(w)
|
|
91
|
+
}
|
|
92
|
+
return worn.length ? fold(worn[0], ...worn.slice(1)) : undefined
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** The line a persona file opens with: which persona it was generated from,
|
|
96
|
+
* and where to edit it. */
|
|
97
|
+
export let banner = (vocab: Vocab): (persona: Bundle) => string => {
|
|
98
|
+
let id = human(vocab)
|
|
99
|
+
return (p) =>
|
|
100
|
+
`<!-- GENERATED from ${id(p)} (${safe(str(comp(p, DOC)[TITLE]))}) — ` +
|
|
101
|
+
'edit it in the graph, never here: the next sync overwrites hand edits. -->'
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** A persona as the file projecting it into a checkout says it: its
|
|
105
|
+
* {@link banner}, then its {@link voice}. */
|
|
106
|
+
export let projection = (vocab: Vocab): (worn: Worn) => string => {
|
|
107
|
+
let head = banner(vocab)
|
|
108
|
+
let render = voice(vocab)
|
|
109
|
+
return (w) => `${head(w.persona)}\n\n${render(w)}`
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** What an agent is owed: the persona's id, and its text. */
|
|
113
|
+
export type Owed = { source: string; text: string }
|
|
114
|
+
|
|
115
|
+
/** Who an agent is: a persona chosen for it, or the work it is on (a task, or
|
|
116
|
+
* a project), whose lineage's common persona it hears. */
|
|
117
|
+
export type As = { persona?: string; work?: Eid }
|
|
118
|
+
|
|
119
|
+
// Every directory from `path` up, nearest first.
|
|
120
|
+
let upward = (path: string): string[] => {
|
|
121
|
+
let out: string[] = []
|
|
122
|
+
for (
|
|
123
|
+
let d = path.replace(/\/+$/, '');
|
|
124
|
+
d;
|
|
125
|
+
d = d.slice(0, d.lastIndexOf('/'))
|
|
126
|
+
) {
|
|
127
|
+
out.push(d)
|
|
128
|
+
}
|
|
129
|
+
return out
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** The common persona the checkout holding `path` carries: the nearest
|
|
133
|
+
* directory at or above `path` that the graph knows as a worktree, then the
|
|
134
|
+
* first project filed with its repository, by eid, that has one along its
|
|
135
|
+
* lineage. A session started anywhere in a checkout wears this. */
|
|
136
|
+
export let carried = async (
|
|
137
|
+
g: Graph,
|
|
138
|
+
path: string,
|
|
139
|
+
): Promise<Worn | undefined> => {
|
|
140
|
+
let needs = ['worktree', 'repo', 'project', PERSONA]
|
|
141
|
+
if (!needs.every((c) => g.vocab.comp(c))) return
|
|
142
|
+
let dirs = upward(path)
|
|
143
|
+
if (!dirs.length) return
|
|
144
|
+
let known = await g.get(
|
|
145
|
+
(await g.storage.read(
|
|
146
|
+
and(present('worktree'), eq('workspace.root', value(dirs))),
|
|
147
|
+
)).map((b) => b.entity.eid),
|
|
148
|
+
)
|
|
149
|
+
let at = (b: Bundle) => str(comp(b, 'workspace').root)
|
|
150
|
+
let nearest = dirs.find((d) => known.some((b) => at(b) == d))
|
|
151
|
+
let repos = known.filter((b) => at(b) == nearest)
|
|
152
|
+
.map((b) => str(comp(b, 'worktree').repository)).filter(Boolean)
|
|
153
|
+
if (!repos.length) return
|
|
154
|
+
let projects = (await g.storage.read(
|
|
155
|
+
and(present('project'), eq('repo.repository', value(repos))),
|
|
156
|
+
)).filter((b) => !b.archived).map(eidOf).sort()
|
|
157
|
+
for (let p of projects) {
|
|
158
|
+
let w = await common(g, p)
|
|
159
|
+
if (w) return w
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Who an agent is: the persona chosen for it; else the common persona of its
|
|
164
|
+
// work, else the one its checkout carries (`here`, which a file in that
|
|
165
|
+
// checkout may already say).
|
|
166
|
+
let resolved = async (
|
|
167
|
+
g: Graph,
|
|
168
|
+
path: string,
|
|
169
|
+
as: As,
|
|
170
|
+
): Promise<{ worn?: Worn; here?: Worn }> => {
|
|
171
|
+
if (as.persona) {
|
|
172
|
+
let found = await g.address([as.persona])
|
|
173
|
+
let worn = await wear(g.storage, g.vocab)(
|
|
174
|
+
found.get(as.persona) ?? as.persona,
|
|
175
|
+
)
|
|
176
|
+
if (!worn) throw new Refused(`no persona called ${as.persona}`)
|
|
177
|
+
return { worn }
|
|
178
|
+
}
|
|
179
|
+
let here = await carried(g, path)
|
|
180
|
+
return { worn: (as.work && await common(g, as.work)) || here, here }
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* The persona an agent working in the checkout at `path` wears, whole,
|
|
185
|
+
* whatever its instruction files already say of it: the one `as.persona`
|
|
186
|
+
* chooses, else the common persona of `as.work`'s lineage, else the one the
|
|
187
|
+
* checkout carries. Nothing when none of them has one. {@link offer} reads the
|
|
188
|
+
* tools it offers from it.
|
|
189
|
+
*
|
|
190
|
+
* ```ts ignore
|
|
191
|
+
* let tools = offer(await wearing(g, '', { persona: 'N-7' }))
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
194
|
+
export let wearing = async (
|
|
195
|
+
g: Graph,
|
|
196
|
+
path: string,
|
|
197
|
+
as: As = {},
|
|
198
|
+
): Promise<Worn | undefined> => (await resolved(g, path, as)).worn
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* What an agent working in the checkout at `path` is owed of its persona,
|
|
202
|
+
* given the text of each instruction file its provider reads there. By
|
|
203
|
+
* default that is the common persona the checkout's repository carries;
|
|
204
|
+
* `as.work` makes it the common persona of the work's lineage, less whatever a
|
|
205
|
+
* file there already says of the checkout's. Nothing when a file already says
|
|
206
|
+
* it all, when the graph knows no checkout holding `path` and no work, or when
|
|
207
|
+
* nothing along the way has a common persona. `as.persona` selects a graph
|
|
208
|
+
* persona instead, even when the checkout is not known to the graph.
|
|
209
|
+
*
|
|
210
|
+
* ```ts ignore
|
|
211
|
+
* let owes = await owed(g, '/srv/runs/S-1', [agentsMd], { work: 'T-7' })
|
|
212
|
+
* if (owes) args.push('--append-system-prompt', owes.text)
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
export let owed = async (
|
|
216
|
+
g: Graph,
|
|
217
|
+
path: string,
|
|
218
|
+
files: string[],
|
|
219
|
+
as: As = {},
|
|
220
|
+
): Promise<Owed | undefined> => {
|
|
221
|
+
let says = (w: Worn) => files.includes(projection(g.vocab)(w))
|
|
222
|
+
let { worn, here } = await resolved(g, path, as)
|
|
223
|
+
if (!worn || says(worn)) return
|
|
224
|
+
let w = here && says(here) ? beside(worn, here) : worn
|
|
225
|
+
return { source: human(g.vocab)(w.persona), text: voice(g.vocab)(w) }
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// Whether `worn` says everything `said` does.
|
|
229
|
+
let within = (worn: Worn, said: Worn): boolean => {
|
|
230
|
+
let all = new Set([worn.persona, ...worn.contains].map(eidOf))
|
|
231
|
+
return [said.persona, ...said.contains].every((b) => all.has(eidOf(b)))
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* The instruction files an agent still reads when its host says its persona
|
|
236
|
+
* whole, afresh on every ask ({@link wearing}): each of `files` but one that
|
|
237
|
+
* is exactly the projection of the persona it wears, or of the checkout's
|
|
238
|
+
* where the worn one says all of it, which would say the same twice and go
|
|
239
|
+
* stale beside it.
|
|
240
|
+
*
|
|
241
|
+
* ```ts ignore
|
|
242
|
+
* let kept = await unsaid(g, '/srv/runs/S-1', [agentsMd], { work: 'T-7' })
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
245
|
+
export let unsaid = async (
|
|
246
|
+
g: Graph,
|
|
247
|
+
path: string,
|
|
248
|
+
files: string[],
|
|
249
|
+
as: As = {},
|
|
250
|
+
): Promise<string[]> => {
|
|
251
|
+
let { worn, here } = await resolved(g, path, as)
|
|
252
|
+
if (!worn) return files
|
|
253
|
+
let said = [worn, ...here && within(worn, here) ? [here] : []]
|
|
254
|
+
.map(projection(g.vocab))
|
|
255
|
+
return files.filter((f) => !said.includes(f))
|
|
256
|
+
}
|