@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/skill-mirror.ts
ADDED
|
@@ -0,0 +1,640 @@
|
|
|
1
|
+
// Skills import from the primary checkout. Graph exports are proposals in a
|
|
2
|
+
// dedicated linked worktree: no mirror write is allowed to touch main.
|
|
3
|
+
|
|
4
|
+
import * as fs from '@yaks/file/io'
|
|
5
|
+
import type { Bundle, Comp, Graph } from 'jsr:@yaks/graph@0.2.36'
|
|
6
|
+
import { edgeEid } from '@yaks/edge'
|
|
7
|
+
import { fileOf } from '@yaks/file'
|
|
8
|
+
import { Refused, Stale } from '@yaks/graph/errors'
|
|
9
|
+
import { token } from '@yaks/graph/sha256'
|
|
10
|
+
import { land, LandError, type Run, run } from '@yaks/git/land'
|
|
11
|
+
import {
|
|
12
|
+
localFilesystem,
|
|
13
|
+
primary as primaryRoots,
|
|
14
|
+
repositoryEid,
|
|
15
|
+
} from '@yaks/git/host'
|
|
16
|
+
import {
|
|
17
|
+
type Agreed,
|
|
18
|
+
type Binding,
|
|
19
|
+
blobOf,
|
|
20
|
+
plan,
|
|
21
|
+
type Report,
|
|
22
|
+
} from '@yaks/mirror'
|
|
23
|
+
import { parseSkill, renderSkill } from './skill-text.js'
|
|
24
|
+
import { agreement, exact, prepareTree } from './skill-mirror-io.js'
|
|
25
|
+
import { renderFiles, skillScope } from './skills.js'
|
|
26
|
+
|
|
27
|
+
export type SkillSync = Report & { landed?: string; worktree?: string }
|
|
28
|
+
export type SkillSyncOpts = { worktree?: string; run?: Run }
|
|
29
|
+
|
|
30
|
+
let comp = (b: Bundle | undefined, name: string): Comp =>
|
|
31
|
+
(b?.[name] ?? {}) as Comp
|
|
32
|
+
let str = (b: Bundle | undefined, name: string, prop: string): string =>
|
|
33
|
+
String(comp(b, name)[prop] ?? '')
|
|
34
|
+
let skillPath = (title: string) => `.claude/skills/${title}/SKILL.md`
|
|
35
|
+
let covered = (path: string) =>
|
|
36
|
+
/^\.claude\/skills\/[a-z0-9][a-z0-9_-]*\/.+/i.test(path) &&
|
|
37
|
+
!/[\\:]/.test(path) &&
|
|
38
|
+
![...path].some((c) => c.charCodeAt(0) < 32 || c.charCodeAt(0) == 127) &&
|
|
39
|
+
path.split('/').every((p) => p && p != '.' && p != '..')
|
|
40
|
+
let primary = (path: string) =>
|
|
41
|
+
path.split('/').length == 4 && path.endsWith('/SKILL.md')
|
|
42
|
+
let titleOf = (path: string) => path.split('/')[2]
|
|
43
|
+
let owned: Record<string, string[]> = {
|
|
44
|
+
skill: ['invoke', 'arguments', 'paths', 'fork', 'options'],
|
|
45
|
+
doc: ['title', 'body'],
|
|
46
|
+
content: ['body'],
|
|
47
|
+
file: ['path', 'root'],
|
|
48
|
+
edge: ['from', 'to'],
|
|
49
|
+
references: [],
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// State every property we own, including absent values: a concurrent editor
|
|
53
|
+
// may add a field as well as replace one. The entire import is one apply.
|
|
54
|
+
let guarded = (before: Bundle | undefined, change: Bundle): Bundle => {
|
|
55
|
+
let was: Record<string, Record<string, string | null>> = {}
|
|
56
|
+
for (let name of Object.keys(change)) {
|
|
57
|
+
if (!owned[name]) continue
|
|
58
|
+
was[name] = Object.fromEntries(
|
|
59
|
+
owned[name].map((p) => [p, token(comp(before, name)[p])]),
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
return { ...change, $was: was }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Current local primary checkouts, including repositories using master. */
|
|
66
|
+
export let skillRoots = primaryRoots
|
|
67
|
+
|
|
68
|
+
// An export lands the bytes the graph renders, which agreement records.
|
|
69
|
+
// Formatting them would leave every exported file disagreeing with its source.
|
|
70
|
+
let format: Run = () => Promise.resolve({ ok: true, code: 0, out: '', err: '' })
|
|
71
|
+
|
|
72
|
+
/** Mirror tracked skill text, keeping agreement outside the graph. Expected
|
|
73
|
+
* conflicts and Git refusals are reports. A failed landing retains its linked
|
|
74
|
+
* worktree for inspection and never acknowledges unlanded graph writes. */
|
|
75
|
+
let syncRepository = async (
|
|
76
|
+
g: Graph,
|
|
77
|
+
root: string,
|
|
78
|
+
memory: string,
|
|
79
|
+
opts: SkillSyncOpts = {},
|
|
80
|
+
): Promise<SkillSync> => {
|
|
81
|
+
let out: SkillSync = {
|
|
82
|
+
read: [],
|
|
83
|
+
wrote: [],
|
|
84
|
+
removed: [],
|
|
85
|
+
conflicts: [],
|
|
86
|
+
failed: [],
|
|
87
|
+
}
|
|
88
|
+
let command = opts.run ?? run
|
|
89
|
+
let git = async (at: string, args: string[]) => {
|
|
90
|
+
let r = await command(args, at)
|
|
91
|
+
if (!r.ok) {
|
|
92
|
+
throw new Error(r.err || r.out || `git ${args.join(' ')} failed`)
|
|
93
|
+
}
|
|
94
|
+
return r.out
|
|
95
|
+
}
|
|
96
|
+
let fail = (e: unknown) => out.failed.push(String(e))
|
|
97
|
+
let lock: fs.FsFile | undefined
|
|
98
|
+
try {
|
|
99
|
+
root = await fs.realPath(root)
|
|
100
|
+
// Ask only about this checkout. Listing every linked worktree makes each
|
|
101
|
+
// ordinary poll pay for unrelated agent checkouts in the repository.
|
|
102
|
+
let check = await command([
|
|
103
|
+
'rev-parse',
|
|
104
|
+
'--is-bare-repository',
|
|
105
|
+
'--path-format=absolute',
|
|
106
|
+
'--show-toplevel',
|
|
107
|
+
'--git-common-dir',
|
|
108
|
+
'--absolute-git-dir',
|
|
109
|
+
'--symbolic-full-name',
|
|
110
|
+
'--verify',
|
|
111
|
+
'--quiet',
|
|
112
|
+
'HEAD',
|
|
113
|
+
], root)
|
|
114
|
+
let [bare, top, shared, own, branch] = check.out.trimEnd().split('\n')
|
|
115
|
+
if (bare == 'true') {
|
|
116
|
+
fail('skills: root must be a primary checkout on a branch')
|
|
117
|
+
return out
|
|
118
|
+
}
|
|
119
|
+
if (!check.ok && check.code != 1) {
|
|
120
|
+
throw new Error(
|
|
121
|
+
check.err || check.out || 'skills: cannot inspect checkout',
|
|
122
|
+
)
|
|
123
|
+
}
|
|
124
|
+
let common = await fs.realPath(shared)
|
|
125
|
+
let gitdir = await fs.realPath(own)
|
|
126
|
+
// An unborn branch has no HEAD object yet; symbolic-ref still names it.
|
|
127
|
+
if (!check.ok) branch = (await git(root, ['symbolic-ref', 'HEAD'])).trim()
|
|
128
|
+
if (top != root || gitdir != common || !branch?.startsWith('refs/heads/')) {
|
|
129
|
+
fail('skills: root must be a primary checkout on a branch')
|
|
130
|
+
return out
|
|
131
|
+
}
|
|
132
|
+
// Keep the inode: unlinking it races with waiters. The kernel releases
|
|
133
|
+
// this advisory lock even when a CLI/effects process crashes.
|
|
134
|
+
lock = await fs.open(`${common}/skill-mirror.lock`, {
|
|
135
|
+
create: true,
|
|
136
|
+
write: true,
|
|
137
|
+
})
|
|
138
|
+
await lock.lock(true)
|
|
139
|
+
let repository = repositoryEid(
|
|
140
|
+
common,
|
|
141
|
+
(await localFilesystem(g)).entity.eid,
|
|
142
|
+
)
|
|
143
|
+
let { skills, places, edges, files } = await skillScope(g, repository, true)
|
|
144
|
+
let locations = new Map<string, Bundle>()
|
|
145
|
+
for (let skill of skills) {
|
|
146
|
+
let location = places.get(skill.entity.eid)
|
|
147
|
+
if (location) locations.set(str(location, 'file', 'path'), skill)
|
|
148
|
+
}
|
|
149
|
+
for (let [old, skill] of locations) {
|
|
150
|
+
let next = skillPath(str(skill, 'doc', 'title'))
|
|
151
|
+
if (next != old && locations.has(next)) {
|
|
152
|
+
throw new Refused(`skills: rename destination is occupied ${next}`)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
let before = new Map([...files, ...skills, ...edges].map((b) => [
|
|
156
|
+
b.entity.eid,
|
|
157
|
+
b,
|
|
158
|
+
]))
|
|
159
|
+
// A locator is known by its ends. The row holding them may predate the id
|
|
160
|
+
// @yaks/edge derives, and an old id can be a name for it (@yaks/alias), so
|
|
161
|
+
// a guard read from a computed id would refuse that row on every pass.
|
|
162
|
+
let located = (skill: string, file: string): Bundle => {
|
|
163
|
+
let held = edges.find((b) =>
|
|
164
|
+
str(b, 'edge', 'from') == skill && str(b, 'edge', 'to') == file
|
|
165
|
+
)
|
|
166
|
+
return guarded(held, {
|
|
167
|
+
entity: { eid: held?.entity.eid ?? edgeEid(skill, 'references', file) },
|
|
168
|
+
edge: { from: skill, to: file },
|
|
169
|
+
references: {},
|
|
170
|
+
})
|
|
171
|
+
}
|
|
172
|
+
let values = renderFiles(skills, places, files)
|
|
173
|
+
for (let path of values.keys()) {
|
|
174
|
+
if (!covered(path)) throw new Refused(`skills: unsafe path ${path}`)
|
|
175
|
+
}
|
|
176
|
+
let tracked = (await git(root, [
|
|
177
|
+
'ls-files',
|
|
178
|
+
'--stage',
|
|
179
|
+
'-z',
|
|
180
|
+
'--',
|
|
181
|
+
'.claude/skills',
|
|
182
|
+
])).split('\0').filter(Boolean)
|
|
183
|
+
let raw = new Map<string, string>()
|
|
184
|
+
let blobs = new Map<string, string>()
|
|
185
|
+
for (let entry of tracked) {
|
|
186
|
+
let [metadata, path] = entry.split('\t')
|
|
187
|
+
if (!covered(path)) {
|
|
188
|
+
throw new Refused(`skills: unsafe tracked path ${path}`)
|
|
189
|
+
}
|
|
190
|
+
if (!/^100(644|755) [a-f0-9]+ 0$/.test(metadata)) {
|
|
191
|
+
throw new Refused(
|
|
192
|
+
`skills: non-regular or unmerged tracked file ${path}`,
|
|
193
|
+
)
|
|
194
|
+
}
|
|
195
|
+
let full = `${root}/${path}`
|
|
196
|
+
try {
|
|
197
|
+
// Check every ancestor too; a directory symlink must not escape root.
|
|
198
|
+
let at = root
|
|
199
|
+
for (let part of path.split('/')) {
|
|
200
|
+
at += `/${part}`
|
|
201
|
+
if ((await fs.lstat(at)).isSymlink) {
|
|
202
|
+
throw new Refused(`skills: symlink ${at}`)
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
let bytes = await fs.readFile(full)
|
|
206
|
+
if (bytes.includes(0)) throw new Refused(`skills: binary file ${path}`)
|
|
207
|
+
let text: string
|
|
208
|
+
try {
|
|
209
|
+
text = new TextDecoder('utf-8', { fatal: true }).decode(bytes)
|
|
210
|
+
} catch (e) {
|
|
211
|
+
if (!(e instanceof TypeError)) throw e
|
|
212
|
+
throw new Refused(`skills: invalid UTF-8 ${path}`)
|
|
213
|
+
}
|
|
214
|
+
raw.set(path, text)
|
|
215
|
+
blobs.set(full, await blobOf(text))
|
|
216
|
+
} catch (e) {
|
|
217
|
+
if (!fs.isNotFound(e)) throw e
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
// Parsing all tracked documents before mutation avoids half an import.
|
|
221
|
+
for (let [path, text] of raw) {
|
|
222
|
+
if (primary(path)) parseSkill(text, titleOf(path))
|
|
223
|
+
else if (
|
|
224
|
+
!raw.has(skillPath(titleOf(path))) &&
|
|
225
|
+
!skills.some((b) => str(b, 'doc', 'title') == titleOf(path))
|
|
226
|
+
) {
|
|
227
|
+
throw new Refused(`skills: support file has no skill ${path}`)
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
let memoryStore = agreement(memory)
|
|
231
|
+
let agreed = await memoryStore.agreed()
|
|
232
|
+
// A memo belongs to one root. Never let a stale key become a write target.
|
|
233
|
+
for (let path of agreed.keys()) {
|
|
234
|
+
if (
|
|
235
|
+
!path.startsWith(`${root}/`) || !covered(path.slice(root.length + 1))
|
|
236
|
+
) {
|
|
237
|
+
throw new Refused(`skills: unsafe agreement path ${path}`)
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
let absolute = new Map([...values].map(([p, t]) => [`${root}/${p}`, t]))
|
|
241
|
+
let binding: Binding = {
|
|
242
|
+
name: 'skills',
|
|
243
|
+
files: () => Promise.resolve(blobs),
|
|
244
|
+
values: () => Promise.resolve(absolute),
|
|
245
|
+
agreed: () => Promise.resolve(agreed),
|
|
246
|
+
read: () => Promise.resolve(),
|
|
247
|
+
}
|
|
248
|
+
let decisions = await plan(binding)
|
|
249
|
+
out.conflicts = decisions.filter((d) => d.act == 'conflict').map((d) =>
|
|
250
|
+
d.path
|
|
251
|
+
)
|
|
252
|
+
// A pending title rename is one proposal in BOTH directions. No read or
|
|
253
|
+
// same acknowledgement may change one half while another half conflicts.
|
|
254
|
+
let groups = new Map<string, string[]>()
|
|
255
|
+
let blocked = new Set<string>()
|
|
256
|
+
for (let [old, skill] of locations) {
|
|
257
|
+
let next = skillPath(str(skill, 'doc', 'title'))
|
|
258
|
+
if (old == next) continue
|
|
259
|
+
let dirs = [old.slice(0, -8), next.slice(0, -8)]
|
|
260
|
+
let group = decisions.filter((d) =>
|
|
261
|
+
dirs.some((dir) => d.path.startsWith(`${root}/${dir}`))
|
|
262
|
+
)
|
|
263
|
+
groups.set(skill.entity.eid, group.map((d) => d.path))
|
|
264
|
+
if (group.some((d) => d.act == 'conflict')) {
|
|
265
|
+
for (let d of group) blocked.add(d.path)
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
let reads = decisions.filter((d) => d.act == 'read' && !blocked.has(d.path))
|
|
269
|
+
let pending = new Map<string, Agreed | null>()
|
|
270
|
+
let bundles: Bundle[] = []
|
|
271
|
+
let removedSkills = new Set<string>()
|
|
272
|
+
let renamed = new Set<string>()
|
|
273
|
+
let readPaths = new Set(reads.map((d) => d.path.slice(root.length + 1)))
|
|
274
|
+
for (let d of reads) {
|
|
275
|
+
let path = d.path.slice(root.length + 1)
|
|
276
|
+
let text = raw.get(path)
|
|
277
|
+
let file = files.find((b) => str(b, 'file', 'path') == path)
|
|
278
|
+
let skill = locations.get(path)
|
|
279
|
+
if (text != undefined && primary(path)) {
|
|
280
|
+
let parsed = parseSkill(text, titleOf(path))
|
|
281
|
+
if (!skill) {
|
|
282
|
+
// A file rename can preserve identity when its instructions identify
|
|
283
|
+
// one deleted document unambiguously. Ambiguous matches must wait.
|
|
284
|
+
let candidates = [...locations].filter(([old, b]) =>
|
|
285
|
+
readPaths.has(old) && !raw.has(old) &&
|
|
286
|
+
str(b, 'content', 'body') == parsed.content.body &&
|
|
287
|
+
str(b, 'doc', 'body') == parsed.doc.body
|
|
288
|
+
)
|
|
289
|
+
if (candidates.length > 1) {
|
|
290
|
+
throw new Refused(`skills: ambiguous rename ${path}`)
|
|
291
|
+
}
|
|
292
|
+
skill = candidates[0]?.[1]
|
|
293
|
+
if (skill) renamed.add(skill.entity.eid)
|
|
294
|
+
}
|
|
295
|
+
let eid = skill?.entity.eid ?? crypto.randomUUID()
|
|
296
|
+
bundles.push(guarded(skill, { entity: { eid }, ...parsed }))
|
|
297
|
+
let fid = fileOf(repository, path)
|
|
298
|
+
bundles.push(guarded(file, {
|
|
299
|
+
entity: { eid: fid },
|
|
300
|
+
file: { path, root: repository },
|
|
301
|
+
}))
|
|
302
|
+
bundles.push(located(eid, fid))
|
|
303
|
+
pending.set(d.path, {
|
|
304
|
+
blob: blobs.get(d.path)!,
|
|
305
|
+
hash: await blobOf(renderSkill(parsed)),
|
|
306
|
+
})
|
|
307
|
+
} else if (text != undefined) {
|
|
308
|
+
bundles.push(guarded(file, {
|
|
309
|
+
entity: { eid: fileOf(repository, path) },
|
|
310
|
+
file: { path, root: repository },
|
|
311
|
+
content: { body: text },
|
|
312
|
+
}))
|
|
313
|
+
pending.set(d.path, {
|
|
314
|
+
blob: blobs.get(d.path)!,
|
|
315
|
+
hash: await blobOf(text),
|
|
316
|
+
})
|
|
317
|
+
} else {
|
|
318
|
+
if (file) {
|
|
319
|
+
bundles.push(guarded(file, {
|
|
320
|
+
entity: { eid: file.entity.eid },
|
|
321
|
+
file: null,
|
|
322
|
+
content: null,
|
|
323
|
+
}))
|
|
324
|
+
}
|
|
325
|
+
if (skill) removedSkills.add(skill.entity.eid)
|
|
326
|
+
for (
|
|
327
|
+
let edge of edges.filter((b) =>
|
|
328
|
+
str(b, 'edge', 'to') == file?.entity.eid
|
|
329
|
+
)
|
|
330
|
+
) {
|
|
331
|
+
bundles.push(
|
|
332
|
+
guarded(edge, {
|
|
333
|
+
entity: { eid: edge.entity.eid },
|
|
334
|
+
edge: null,
|
|
335
|
+
references: null,
|
|
336
|
+
}),
|
|
337
|
+
)
|
|
338
|
+
}
|
|
339
|
+
pending.set(d.path, null)
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
for (let eid of removedSkills) {
|
|
343
|
+
if (!renamed.has(eid)) {
|
|
344
|
+
bundles.push(guarded(before.get(eid), {
|
|
345
|
+
entity: { eid },
|
|
346
|
+
skill: null,
|
|
347
|
+
doc: null,
|
|
348
|
+
content: null,
|
|
349
|
+
}))
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
if (bundles.length) {
|
|
353
|
+
try {
|
|
354
|
+
await g.apply(bundles)
|
|
355
|
+
out.read = reads.map((d) => d.path)
|
|
356
|
+
} catch (e) {
|
|
357
|
+
if (!(e instanceof Refused || e instanceof Stale)) throw e
|
|
358
|
+
out.conflicts.push(e.message)
|
|
359
|
+
return out
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
// Imports remember raw bytes and canonical graph text separately. Mirror's
|
|
363
|
+
// generic read callback cannot infer a renderer's normalization.
|
|
364
|
+
for (
|
|
365
|
+
let d of decisions.filter((d) => d.act == 'same' && !blocked.has(d.path))
|
|
366
|
+
) {
|
|
367
|
+
if (d.sides.file != undefined) {
|
|
368
|
+
pending.set(d.path, { blob: d.sides.file, hash: d.sides.value })
|
|
369
|
+
} else pending.set(d.path, null)
|
|
370
|
+
}
|
|
371
|
+
if (pending.size) await memoryStore.remember!(pending)
|
|
372
|
+
// Exports commit in a linked worktree and land by fast-forward, which
|
|
373
|
+
// leaves unrelated edits in the primary checkout alone. Only a skill file
|
|
374
|
+
// someone is changing there waits: committing or reverting it moves the
|
|
375
|
+
// stamp, and the next pass writes it.
|
|
376
|
+
let changing = new Set(
|
|
377
|
+
(await git(root, [
|
|
378
|
+
'status',
|
|
379
|
+
'--porcelain=v1',
|
|
380
|
+
'-z',
|
|
381
|
+
'--no-renames',
|
|
382
|
+
'--untracked-files=all',
|
|
383
|
+
'--',
|
|
384
|
+
'.claude/skills',
|
|
385
|
+
])).split('\0').filter(Boolean).map((l) => `${root}/${l.slice(3)}`),
|
|
386
|
+
)
|
|
387
|
+
let writes = decisions.filter((d) =>
|
|
388
|
+
d.act == 'write' && !blocked.has(d.path) && !changing.has(d.path)
|
|
389
|
+
)
|
|
390
|
+
if (!writes.length) return out
|
|
391
|
+
let head = (await git(root, ['rev-parse', 'HEAD'])).trim()
|
|
392
|
+
let tree = opts.worktree ?? `${common}/skill-mirror`
|
|
393
|
+
out.worktree = tree
|
|
394
|
+
await prepareTree(root, common, head, tree, git, command)
|
|
395
|
+
// Only planned writes enter the rebased binding. Imports always read main.
|
|
396
|
+
let stagedFiles = new Map<string, string>()
|
|
397
|
+
let stagedValues = new Map<string, string>()
|
|
398
|
+
let stagedAgreed = new Map<string, Agreed>()
|
|
399
|
+
for (let d of writes) {
|
|
400
|
+
let p = `${tree}/${d.path.slice(root.length + 1)}`
|
|
401
|
+
if (d.sides.file) stagedFiles.set(p, d.sides.file)
|
|
402
|
+
if (d.text != undefined) stagedValues.set(p, d.text)
|
|
403
|
+
if (d.sides.agreed) stagedAgreed.set(p, d.sides.agreed)
|
|
404
|
+
}
|
|
405
|
+
let learned = new Map<string, Agreed | null>()
|
|
406
|
+
let result: Report = {
|
|
407
|
+
read: [],
|
|
408
|
+
wrote: [],
|
|
409
|
+
removed: [],
|
|
410
|
+
conflicts: [],
|
|
411
|
+
failed: [],
|
|
412
|
+
}
|
|
413
|
+
let staged = await plan({
|
|
414
|
+
name: 'skills',
|
|
415
|
+
files: () => Promise.resolve(stagedFiles),
|
|
416
|
+
values: () => Promise.resolve(stagedValues),
|
|
417
|
+
agreed: () => Promise.resolve(stagedAgreed),
|
|
418
|
+
read: () => Promise.resolve(),
|
|
419
|
+
})
|
|
420
|
+
for (let d of staged) {
|
|
421
|
+
if (d.act != 'write') {
|
|
422
|
+
if (d.act != 'same') result.conflicts.push(d.path)
|
|
423
|
+
continue
|
|
424
|
+
}
|
|
425
|
+
// Graph-created paths can have a tracked symlink ancestor outside the
|
|
426
|
+
// skill subtree. Never follow it out of the dedicated checkout.
|
|
427
|
+
let target = tree
|
|
428
|
+
for (let part of d.path.slice(tree.length + 1).split('/')) {
|
|
429
|
+
target += `/${part}`
|
|
430
|
+
try {
|
|
431
|
+
if ((await fs.lstat(target)).isSymlink) {
|
|
432
|
+
throw new Refused(`skills: symlink ${target}`)
|
|
433
|
+
}
|
|
434
|
+
} catch (e) {
|
|
435
|
+
if (!fs.isNotFound(e)) throw e
|
|
436
|
+
break
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
if (d.text == undefined) {
|
|
440
|
+
await fs.remove(d.path)
|
|
441
|
+
learned.set(d.path, null)
|
|
442
|
+
result.removed.push(d.path)
|
|
443
|
+
} else {
|
|
444
|
+
let folder = d.path.slice(0, d.path.lastIndexOf('/'))
|
|
445
|
+
await fs.mkdir(folder, { recursive: true })
|
|
446
|
+
let tmp = `${d.path}.${crypto.randomUUID()}.tmp`
|
|
447
|
+
await fs.writeTextFile(tmp, d.text)
|
|
448
|
+
await fs.rename(tmp, d.path)
|
|
449
|
+
learned.set(d.path, { blob: d.sides.value!, hash: d.sides.value })
|
|
450
|
+
result.wrote.push(d.path)
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
if (result.conflicts.length) {
|
|
454
|
+
out.conflicts.push(...result.conflicts)
|
|
455
|
+
return out
|
|
456
|
+
}
|
|
457
|
+
await git(tree, ['add', '--all', '--', '.claude/skills'])
|
|
458
|
+
await git(tree, ['commit', '-m', 'Mirror graph skills'])
|
|
459
|
+
let outcome = await land({
|
|
460
|
+
cwd: tree,
|
|
461
|
+
run: command,
|
|
462
|
+
format,
|
|
463
|
+
write: () => {},
|
|
464
|
+
})
|
|
465
|
+
// These commits contain validated skill text only. A clean rebase does not
|
|
466
|
+
// require TS tests, but must validate the newly combined documents again.
|
|
467
|
+
if ('diverged' in outcome && !outcome.conflict) {
|
|
468
|
+
let paths = (await git(tree, ['ls-files', '-z', '--', '.claude/skills']))
|
|
469
|
+
.split('\0').filter(Boolean)
|
|
470
|
+
for (let path of paths.filter(primary)) {
|
|
471
|
+
parseSkill(await fs.readTextFile(`${tree}/${path}`), titleOf(path))
|
|
472
|
+
}
|
|
473
|
+
outcome = await land({ cwd: tree, run: command, format, write: () => {} })
|
|
474
|
+
}
|
|
475
|
+
if (!('landed' in outcome)) {
|
|
476
|
+
out.conflicts.push(
|
|
477
|
+
'skills: main diverged; linked worktree remains unresolved',
|
|
478
|
+
)
|
|
479
|
+
return out
|
|
480
|
+
}
|
|
481
|
+
out.landed = outcome.landed
|
|
482
|
+
// Landing may have rebased onto concurrent edits. Acknowledge only exact
|
|
483
|
+
// landed bytes/absence, excluding an entire rename if any member differs.
|
|
484
|
+
let invalid = new Set<string>()
|
|
485
|
+
let verifying = new Set(
|
|
486
|
+
[...learned.keys()].map((path) =>
|
|
487
|
+
`${root}/${path.slice(tree.length + 1)}`
|
|
488
|
+
),
|
|
489
|
+
)
|
|
490
|
+
for (let [old, skill] of locations) {
|
|
491
|
+
let next = skillPath(str(skill, 'doc', 'title'))
|
|
492
|
+
if (
|
|
493
|
+
result.removed.includes(`${tree}/${old}`) &&
|
|
494
|
+
result.wrote.includes(`${tree}/${next}`)
|
|
495
|
+
) {
|
|
496
|
+
for (let path of groups.get(skill.entity.eid) ?? []) {
|
|
497
|
+
verifying.add(path)
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
for (let original of verifying) {
|
|
502
|
+
if (!await exact(root, original, absolute.get(original))) {
|
|
503
|
+
invalid.add(original)
|
|
504
|
+
out.conflicts.push(
|
|
505
|
+
`skills: landed export differs from proposal ${original}`,
|
|
506
|
+
)
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
for (let paths of groups.values()) {
|
|
510
|
+
if (paths.some((path) => invalid.has(path))) {
|
|
511
|
+
for (let path of paths) invalid.add(path)
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
for (let path of invalid) {
|
|
515
|
+
learned.delete(`${tree}/${path.slice(root.length + 1)}`)
|
|
516
|
+
}
|
|
517
|
+
// Rename locator rows only after exact exported bytes were verified.
|
|
518
|
+
let moves: Bundle[] = []
|
|
519
|
+
for (let [old, skill] of locations) {
|
|
520
|
+
let next = skillPath(str(skill, 'doc', 'title'))
|
|
521
|
+
if (
|
|
522
|
+
old == next || groups.get(skill.entity.eid)?.some((p) =>
|
|
523
|
+
invalid.has(p)
|
|
524
|
+
) ||
|
|
525
|
+
!result.removed.includes(`${tree}/${old}`) ||
|
|
526
|
+
!result.wrote.includes(`${tree}/${next}`)
|
|
527
|
+
) continue
|
|
528
|
+
moves.push(
|
|
529
|
+
guarded(skill, {
|
|
530
|
+
entity: { eid: skill.entity.eid },
|
|
531
|
+
doc: {},
|
|
532
|
+
content: {},
|
|
533
|
+
skill: {},
|
|
534
|
+
}),
|
|
535
|
+
)
|
|
536
|
+
for (
|
|
537
|
+
let file of files.filter((b) =>
|
|
538
|
+
str(b, 'file', 'path').startsWith(old.slice(0, -8))
|
|
539
|
+
)
|
|
540
|
+
) {
|
|
541
|
+
let path = str(file, 'file', 'path')
|
|
542
|
+
let destination = next.slice(0, -8) + path.slice(old.length - 8)
|
|
543
|
+
let fid = fileOf(repository, destination)
|
|
544
|
+
let body = comp(file, 'content').body
|
|
545
|
+
moves.push(
|
|
546
|
+
guarded(file, {
|
|
547
|
+
entity: { eid: file.entity.eid },
|
|
548
|
+
file: null,
|
|
549
|
+
content: null,
|
|
550
|
+
}),
|
|
551
|
+
)
|
|
552
|
+
moves.push(guarded(before.get(fid), {
|
|
553
|
+
entity: { eid: fid },
|
|
554
|
+
file: { path: destination, root: repository },
|
|
555
|
+
...(body == undefined ? {} : { content: { body } }),
|
|
556
|
+
}))
|
|
557
|
+
if (path == old) {
|
|
558
|
+
for (
|
|
559
|
+
let edge of edges.filter((b) =>
|
|
560
|
+
str(b, 'edge', 'from') == skill.entity.eid &&
|
|
561
|
+
str(b, 'edge', 'to') == file.entity.eid
|
|
562
|
+
)
|
|
563
|
+
) {
|
|
564
|
+
moves.push(
|
|
565
|
+
guarded(edge, {
|
|
566
|
+
entity: { eid: edge.entity.eid },
|
|
567
|
+
edge: null,
|
|
568
|
+
references: null,
|
|
569
|
+
}),
|
|
570
|
+
)
|
|
571
|
+
}
|
|
572
|
+
moves.push(located(skill.entity.eid, fid))
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
if (moves.length) await g.apply(moves)
|
|
577
|
+
let landed = new Map([...learned].map(([p, a]) => [
|
|
578
|
+
`${root}/${p.slice(tree.length + 1)}`,
|
|
579
|
+
a,
|
|
580
|
+
]))
|
|
581
|
+
await memoryStore.remember!(landed)
|
|
582
|
+
out.wrote = result.wrote.map((p) => `${root}/${p.slice(tree.length + 1)}`)
|
|
583
|
+
out.removed = result.removed.map((p) =>
|
|
584
|
+
`${root}/${p.slice(tree.length + 1)}`
|
|
585
|
+
)
|
|
586
|
+
} catch (e) {
|
|
587
|
+
if (e instanceof LandError) out.conflicts.push(e.message)
|
|
588
|
+
else if (e instanceof Stale) out.conflicts.push(e.message)
|
|
589
|
+
else if (e instanceof Refused) fail(e)
|
|
590
|
+
else throw e
|
|
591
|
+
} finally {
|
|
592
|
+
lock?.close()
|
|
593
|
+
}
|
|
594
|
+
return out
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
// Advisory locks are process-scoped on some platforms. Serialize callers in
|
|
598
|
+
// this process too; the persistent common-dir lock coordinates other processes.
|
|
599
|
+
// Callers are served in the order they called: roots resolve one after
|
|
600
|
+
// another, and each caller joins its root's queue as its lookup ends, so a
|
|
601
|
+
// later lookup cannot finish first and take the head.
|
|
602
|
+
let pending = new Map<string, Promise<unknown>>()
|
|
603
|
+
let resolving: Promise<unknown> = Promise.resolve()
|
|
604
|
+
let failed = (root: string, error: unknown) => {
|
|
605
|
+
if (!(error instanceof Error)) {
|
|
606
|
+
return new Error(`skills at ${root}: ${String(error)}`, { cause: error })
|
|
607
|
+
}
|
|
608
|
+
error.message = `skills at ${root}: ${error.message}`
|
|
609
|
+
return error
|
|
610
|
+
}
|
|
611
|
+
export let syncSkills = async (
|
|
612
|
+
g: Graph,
|
|
613
|
+
root: string,
|
|
614
|
+
memory: string,
|
|
615
|
+
opts: SkillSyncOpts = {},
|
|
616
|
+
): Promise<SkillSync> => {
|
|
617
|
+
let joined = resolving.then(async () => {
|
|
618
|
+
let real: string
|
|
619
|
+
try {
|
|
620
|
+
real = await fs.realPath(root)
|
|
621
|
+
} catch (error) {
|
|
622
|
+
throw failed(root, error)
|
|
623
|
+
}
|
|
624
|
+
let before = pending.get(real) ?? Promise.resolve()
|
|
625
|
+
let next = before.catch(() => {}).then(() =>
|
|
626
|
+
syncRepository(g, real, memory, opts)
|
|
627
|
+
).catch((error) => {
|
|
628
|
+
throw failed(real, error)
|
|
629
|
+
})
|
|
630
|
+
pending.set(real, next)
|
|
631
|
+
return { real, next }
|
|
632
|
+
})
|
|
633
|
+
resolving = joined.catch(() => {})
|
|
634
|
+
let { real, next } = await joined
|
|
635
|
+
try {
|
|
636
|
+
return await next
|
|
637
|
+
} finally {
|
|
638
|
+
if (pending.get(real) === next) pending.delete(real)
|
|
639
|
+
}
|
|
640
|
+
}
|