@0001-labs/idv1 2.0.2 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -29,7 +29,7 @@ const parsed = parseCode(candidate)
29
29
 
30
30
  ## The idv1 command
31
31
 
32
- A codebase keeps a `.id` file. It may open with five lines naming the shapes in pattern order (`LLNNNN`, `NNLLLL`, `LLLLNN`, `LLNNXX`, `XXXXXX`); a line left as its pattern keeps no name. A shape name is a label, not a taxonomy reservation. Then each section name stands on its own line, followed by one entry a line, key first. A taxonomy is written as its pattern, which carries its pair and its shape; an id's shape is read from the id. `readDirectory` still reads the JSON form; `writeDirectory` writes this one.
32
+ A codebase keeps its vocabulary in a `.id` file of plain lines. The first five name the shapes in pattern order (`LLNNNN`, `NNLLLL`, `LLLLNN`, `LLNNXX`, `XXXXXX`); a line left as its pattern keeps no name, and a shape name is a label, not a taxonomy reservation. After them, two capitals and a name are a namespace, and a six-character pattern holding one lowercase pair and a name is a taxonomy, which carries its shape. Blank lines and `#` comments are free. Codes themselves live with their records, in a column beside the database's own key, never in `.id`.
33
33
 
34
34
  ```text
35
35
  User
@@ -38,30 +38,19 @@ Product
38
38
  Item
39
39
  Event
40
40
 
41
- namespaces
42
41
  SH Shop
43
42
 
44
- taxonomies
45
43
  coNNXX Component
46
44
  NNsvLL Service
47
-
48
- ids
49
- SHop01 Shop @ .
50
- co12Ca Cart @ src/cart
51
- 02svSt Stripe
52
-
53
- facts
54
- SHop01 rHasPt co12Ca
55
45
  ```
56
46
 
57
47
  ```sh
58
- npx idv1 directory
59
- npx idv1 status
60
- npx idv1 health
61
- npx idv1 resolve co14Ch
48
+ npx idv1 directory # the vocabulary, and whether it is healthy
49
+ npx idv1 health # exits 1 with each problem
50
+ npx idv1 resolve co12Ca # the shape, taxonomy and namespace a code reads as
62
51
  ```
63
52
 
64
- The same operations are exported: `readDirectory`, `directoryHealth`, `resolveCode`, `taxonomyOf`, `describeDirectory`, `describeStatus` and `runDirectoryCommand`.
53
+ The same operations are exported: `readDirectory`, `writeDirectory`, `directoryHealth`, `resolveCode`, `taxonomyOf`, `describeDirectory` and `runDirectoryCommand`.
65
54
 
66
55
  The package ships TypeScript source. Bundlers and `tsx` read it directly. Plain Node 22.18 or later imports `@0001-labs/idv1/register` first, then dynamically imports the package.
67
56
 
@@ -73,6 +62,8 @@ Optional `collections` carry source records and lazy `load` functions. Records m
73
62
 
74
63
  ## Upgrading from 1.x
75
64
 
65
+ Release 3 keeps `.id` to vocabulary: `Directory` holds shapes, namespaces and taxonomies; `Part`, facts and `idv1 status` are gone, and `idv1 health` names every other line it finds. `readDirectory` still reads the JSON form.
66
+
76
67
  Release 2 changes API names, not code grammar, scan payloads or capacity. Replace semantic shape arguments with patterns, `parseCode(...).kind` with `.shape`, and `codeKind` with `codeShape`. Use `codeCandidate(shape)` and `isCodeOf(shape, value)` in place of per-kind wrappers. Regex constants are pattern-named, such as `LLNNXX_CODE`; `blockedLLLLNNCode` replaces the semantic name of its filter. `.id` part and taxonomy `shape` values use patterns. Keep application words in the optional `shapes` map.
77
68
 
78
69
  ## Documentation
package/bin/idv1.mjs CHANGED
@@ -12,6 +12,6 @@ if (!existsSync(file)) {
12
12
  console.error('No .id file here or above. See https://idv1.0001.dev/docs/annotate')
13
13
  process.exit(2)
14
14
  }
15
- const { code, out } = runDirectoryCommand(command, args, readFileSync(file, 'utf8'), path => existsSync(join(dir, path)))
15
+ const { code, out } = runDirectoryCommand(command, args, readFileSync(file, 'utf8'))
16
16
  console.log(out)
17
17
  process.exit(code)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0001-labs/idv1",
3
- "version": "2.0.2",
3
+ "version": "3.0.0",
4
4
  "description": "Six-character IDs for agents: five shapes, taxonomy and namespaces, and a CLI that reads a codebase's .id file.",
5
5
  "license": "MIT",
6
6
  "repository": {
package/src/idv1.ts CHANGED
@@ -1,141 +1,90 @@
1
1
  import { JUNCTION_SEPARATOR, parseCode, type CodeShape } from './identifier'
2
- import { blockedCodeLetters, blockedLLLLNNCode } from './util/blockedWords'
3
2
  import { namespaceHealth, namespaceOf, type Namespace } from './namespace'
4
3
  import { taxonomyHealth, taxonomyOf, type Taxonomy } from './taxonomy'
5
4
 
6
- export type Part = { id: string; shape: CodeShape; name: string; path: string | null }
7
5
  export type ShapeNames = Partial<Record<CodeShape, string>>
8
- export type Directory = { shapes?: ShapeNames; namespaces: Namespace[]; taxonomies: Taxonomy[]; parts: Part[]; facts: string[] }
9
- export type Resolved = { id: string; shape: CodeShape | null; namespace?: Namespace; taxonomy?: Taxonomy; part?: Part; facts: string[] }
6
+ export type Directory = { shapes: ShapeNames; namespaces: Namespace[]; taxonomies: Taxonomy[]; unread?: string[] }
7
+ export type Resolved = { id: string; shape: CodeShape | null; namespace?: Namespace; taxonomy?: Taxonomy }
10
8
 
11
9
  const SHAPES: CodeShape[] = ['LLNNNN', 'NNLLLL', 'LLLLNN', 'LLNNXX', 'XXXXXX']
10
+ const NAMESPACE_KEY = /^[A-Z]{2}$/
11
+ const TAXONOMY_KEY = /^(?=[NLXOP]*[a-z]{2}[NLXOP]*$).{6}$/
12
12
  const list = <T>(value: unknown): T[] => (Array.isArray(value) ? value : []) as T[]
13
-
14
- const SECTIONS = ['namespaces', 'taxonomies', 'ids', 'facts'] as const
15
13
  export const patternShape = (pattern: string) => parseCode(pattern.replace(/N/g, '1').replace(/[LXOP]/g, 'a'))?.shape ?? null
16
14
 
17
15
  export function readDirectory(text: string): Directory {
18
16
  if (text.trimStart().startsWith('{')) {
19
- const value = JSON.parse(text) as Partial<Directory>
20
- return { shapes: value.shapes ?? {}, namespaces: list(value.namespaces), taxonomies: list(value.taxonomies), parts: list(value.parts), facts: list(value.facts) }
17
+ const value = JSON.parse(text) as Partial<Directory> & { parts?: { id: string }[]; facts?: string[] }
18
+ return { shapes: value.shapes ?? {}, namespaces: list(value.namespaces), taxonomies: list(value.taxonomies), unread: [...list<{ id: string }>(value.parts).map(part => part.id), ...list<string>(value.facts)] }
21
19
  }
22
- const directory: Directory = { shapes: {}, namespaces: [], taxonomies: [], parts: [], facts: [] }
23
- let section = '', shapeLine = 0
24
- for (const line of text.split('\n').map(line => line.trim()).filter(line => line && !line.startsWith('#'))) {
25
- if ((SECTIONS as readonly string[]).includes(line)) { section = line; continue }
20
+ const directory: Directory = { shapes: {}, namespaces: [], taxonomies: [], unread: [] }
21
+ text.split('\n').map(line => line.trim()).filter(line => line && !line.startsWith('#')).forEach((line, index) => {
26
22
  const [key, ...rest] = line.split(' '), name = rest.join(' ')
27
- if (!section) { const shape = SHAPES[shapeLine++]; if (shape && line !== shape) directory.shapes![shape] = line }
28
- else if (section === 'namespaces') directory.namespaces.push({ shorthand: key, name })
29
- else if (section === 'taxonomies') directory.taxonomies.push({ shorthand: key.replace(/[^a-z]/g, ''), name, shape: patternShape(key)!, pattern: key })
30
- else if (section === 'ids') { const [label, path] = name.split(' @ '); directory.parts.push({ id: key, shape: parseCode(key)?.shape!, name: label, path: path ?? null }) }
31
- else if (section === 'facts') directory.facts.push(line)
32
- }
23
+ if (index < SHAPES.length) { if (line !== SHAPES[index]) directory.shapes[SHAPES[index]] = line }
24
+ else if (NAMESPACE_KEY.test(key)) directory.namespaces.push({ shorthand: key, name })
25
+ else if (TAXONOMY_KEY.test(key)) directory.taxonomies.push({ shorthand: key.replace(/[^a-z]/g, ''), name, shape: patternShape(key)!, pattern: key })
26
+ else directory.unread!.push(line)
27
+ })
33
28
  return directory
34
29
  }
35
30
 
36
31
  export function writeDirectory(directory: Directory): string {
37
- const shapes = Object.keys(directory.shapes ?? {}).length ? SHAPES.map(shape => directory.shapes?.[shape] ?? shape).join('\n') + '\n\n' : ''
38
- return shapes + [
39
- ['namespaces', directory.namespaces.map(({ shorthand, name }) => `${shorthand} ${name}`)],
40
- ['taxonomies', directory.taxonomies.map(({ pattern, name }) => `${pattern.replace(/\s/g, '')} ${name}`)],
41
- ['ids', directory.parts.map(({ id, name, path }) => `${id} ${name}${path ? ` @ ${path}` : ''}`)],
42
- ['facts', directory.facts],
43
- ].filter(([, lines]) => lines.length).map(([section, lines]) => [section, ...lines].join('\n')).join('\n\n') + '\n'
32
+ return [
33
+ SHAPES.map(shape => directory.shapes[shape] ?? shape),
34
+ directory.namespaces.map(({ shorthand, name }) => `${shorthand} ${name}`),
35
+ directory.taxonomies.map(({ pattern, name }) => `${pattern.replace(/\s/g, '')} ${name}`),
36
+ ].filter(lines => lines.length).map(lines => lines.join('\n')).join('\n\n') + '\n'
44
37
  }
45
38
 
46
- export function directoryHealth(directory: Directory, exists: (path: string) => boolean = () => true): string[] {
39
+ export function directoryHealth(directory: Directory): string[] {
47
40
  const problems: string[] = []
48
- for (const [shape, name] of Object.entries(directory.shapes ?? {})) if (!SHAPES.includes(shape as CodeShape) || typeof name !== 'string' || !name.trim()) problems.push(`Invalid shape name: ${shape}`)
41
+ for (const [shape, name] of Object.entries(directory.shapes)) if (!SHAPES.includes(shape as CodeShape) || typeof name !== 'string' || !name.trim()) problems.push(`Invalid shape name: ${shape}`)
49
42
  const seen = (values: string[], what: string) => values.forEach((value, index) => { if (values.indexOf(value) !== index) problems.push(`${what} ${value} is declared twice`) })
50
43
  problems.push(...namespaceHealth(directory.namespaces), ...taxonomyHealth(directory.taxonomies))
51
44
  seen(directory.namespaces.map(namespace => namespace.shorthand), 'Namespace')
52
45
  seen(directory.taxonomies.map(taxonomy => taxonomy.shorthand), 'Taxonomy')
53
- seen(directory.parts.map(part => part.id), 'Id')
54
- const ids = new Set(directory.parts.map(part => part.id))
55
- for (const part of directory.parts) {
56
- const shape = parseCode(part.id)?.shape
57
- if (shape !== part.shape) problems.push(`${part.id} reads as ${shape ?? 'no shape'}, not ${part.shape}`)
58
- else if (shape === 'LLLLNN' ? blockedLLLLNNCode(part.id) : blockedCodeLetters(part.id)) problems.push(`${part.id} uses blocked letters or digits`)
59
- if (!part.name) problems.push(`${part.id} has no name`)
60
- if (part.path && !exists(part.path)) problems.push(`${part.id}: ${part.path} is gone`)
61
- }
62
- for (const fact of directory.facts) {
63
- const [subject, relation, object, extra] = fact.split(' ')
64
- if (extra !== undefined || !/^r[A-Za-z]{5}$/.test(relation ?? '')) problems.push(`Not a fact: ${fact}`)
65
- else for (const id of [subject, object]) if (!ids.has(id)) problems.push(`Unknown id ${id} in: ${fact}`)
66
- }
46
+ for (const line of directory.unread ?? []) problems.push(`Not vocabulary: ${line} (codes and links live with their records)`)
67
47
  return problems
68
48
  }
69
49
 
70
50
  export function resolveCode(directory: Directory, value: string): Resolved[] {
71
51
  const qualified = /^-([A-Z]{2})-(.+)$/.exec(value)
72
- const ids = (qualified ? qualified[2] : value).split(JUNCTION_SEPARATOR)
73
- return ids.map(id => {
52
+ return (qualified ? qualified[2] : value).split(JUNCTION_SEPARATOR).map(id => {
74
53
  const shape = parseCode(id)?.shape ?? null
75
- const namespace = namespaceOf(directory, id, qualified?.[1])
76
- const facts = directory.facts.filter(fact => { const [subject, , object] = fact.split(' '); return subject === id || object === id })
77
- return { id, shape, namespace, taxonomy: shape ? taxonomyOf(directory, id) : undefined, part: directory.parts.find(part => part.id === id), facts }
54
+ return { id, shape, namespace: namespaceOf(directory, id, qualified?.[1]), taxonomy: shape ? taxonomyOf(directory, id) : undefined }
78
55
  })
79
56
  }
80
57
 
81
- const nameOf = (directory: Directory, id: string) => directory.parts.find(part => part.id === id)?.name ?? id
82
- const readFact = (directory: Directory, fact: string) => { const [subject, relation, object] = fact.split(' '); return `${subject} ${relation} ${object} ${nameOf(directory, subject)} → ${nameOf(directory, object)}` }
58
+ const shapeName = (directory: Directory, shape: CodeShape) => directory.shapes[shape] ? `${shape} · ${directory.shapes[shape]}` : shape
83
59
 
84
60
  export function describeResolved(directory: Directory, resolved: Resolved[]): string {
85
- return resolved.map(({ id, shape, namespace, taxonomy, part, facts }) => [
86
- `${id} ${part?.name ?? (shape ? 'not in .id' : 'not a code')}`,
87
- ` shape ${shape ?? '—'}`,
61
+ return resolved.map(({ id, shape, namespace, taxonomy }) => [
62
+ `${id}${shape ? '' : ' not a code'}`,
63
+ ...(shape ? [` shape ${shapeName(directory, shape)}`] : []),
88
64
  ...(taxonomy ? [` taxonomy ${taxonomy.shorthand} · ${taxonomy.name}`] : []),
89
65
  ...(namespace ? [` namespace ${namespace.shorthand} · ${namespace.name}`] : []),
90
- ...(part?.path ? [` path ${part.path}`] : []),
91
- ...facts.map(fact => ` fact ${readFact(directory, fact)}`),
92
66
  ].join('\n')).join('\n\n')
93
67
  }
94
68
 
95
- export function describeStatus(directory: Directory, problems: string[]): string {
96
- const count = (n: number, one: string, many = `${one}s`) => `${n} ${n === 1 ? one : many}`
97
- const shapes = SHAPES.map(shape => [shape, directory.parts.filter(part => part.shape === shape).length] as const).filter(([, n]) => n)
69
+ export function describeDirectory(directory: Directory, problems: string[] = directoryHealth(directory)): string {
98
70
  return [
99
- `${count(directory.parts.length, 'part')} · ${count(directory.facts.length, 'fact')} · ${count(directory.namespaces.length, 'namespace')} · ${count(directory.taxonomies.length, 'taxonomy', 'taxonomies')}`,
100
- problems.length ? `${count(problems.length, 'problem')}: run idv1 health` : 'healthy',
101
- '',
102
- ...shapes.map(([shape, n]) => ` ${shape.padEnd(9)}${n} ${directory.shapes?.[shape] ?? shape}`),
103
- ...directory.taxonomies.map(taxonomy => ` ${taxonomy.shorthand.padEnd(9)}${directory.parts.filter(part => taxonomyOf(directory, part.id) === taxonomy).length} ${taxonomy.name}`),
104
- ].join('\n')
105
- }
106
-
107
- export function describeDirectory(directory: Directory): string {
108
- const contains = directory.facts.map(fact => fact.split(' ')).filter(([, relation]) => relation === 'rHasPt')
109
- const children = (id: string) => contains.filter(([subject]) => subject === id).map(([, , object]) => object)
110
- const roots = directory.parts.filter(part => !contains.some(([, , object]) => object === part.id))
111
- const line = (part: Part | undefined, id: string, depth: number) => `${' '.repeat(depth)}${id} ${part?.name ?? '?'}${part && taxonomyOf(directory, id) ? ` (${taxonomyOf(directory, id)!.name})` : ''}${part?.path ? ` ${part.path}` : ''}`
112
- const tree = (id: string, depth: number, trail: Set<string>): string[] => trail.has(id) ? [] : [line(directory.parts.find(part => part.id === id), id, depth), ...children(id).flatMap(child => tree(child, depth + 1, new Set([...trail, id])))]
113
- return [
114
- 'Shapes',
115
- ...SHAPES.map(shape => ` ${shape} ${directory.shapes?.[shape] ?? shape}`),
116
- '',
117
- 'Namespaces',
118
- ...directory.namespaces.map(({ shorthand, name }) => ` ${shorthand} ${name}`),
71
+ ...SHAPES.map(shape => `${shape} ${directory.shapes[shape] ?? ''}`.trimEnd()),
119
72
  '',
120
- 'Taxonomies',
121
- ...directory.taxonomies.map(({ shorthand, name, shape, pattern }) => ` ${shorthand} ${name} ${shape} ${pattern}`),
73
+ ...directory.namespaces.map(({ shorthand, name }) => `${shorthand} ${name}`),
122
74
  '',
123
- 'Parts',
124
- ...roots.flatMap(part => tree(part.id, 1, new Set())),
75
+ ...directory.taxonomies.map(({ pattern, name }) => `${pattern} ${name}`),
125
76
  '',
126
- 'Facts',
127
- ...directory.facts.filter(fact => fact.split(' ')[1] !== 'rHasPt').map(fact => ` ${readFact(directory, fact)}`),
77
+ problems.length ? `${problems.length} problem${problems.length === 1 ? '' : 's'}: run idv1 health` : 'healthy',
128
78
  ].join('\n')
129
79
  }
130
80
 
131
- export function runDirectoryCommand(command: string | undefined, args: string[], text: string, exists?: (path: string) => boolean): { code: number; out: string } {
81
+ export function runDirectoryCommand(command: string | undefined, args: string[], text: string): { code: number; out: string } {
132
82
  const directory = readDirectory(text)
133
- const problems = directoryHealth(directory, exists)
83
+ const problems = directoryHealth(directory)
134
84
  switch (command) {
135
85
  case 'health': return { code: problems.length ? 1 : 0, out: problems.length ? problems.join('\n') : 'healthy' }
136
- case 'status': return { code: 0, out: describeStatus(directory, problems) }
137
- case 'resolve': return args.length ? { code: 0, out: args.map(value => describeResolved(directory, resolveCode(directory, value))).join('\n\n') } : { code: 2, out: 'idv1 resolve <id | junction | -NS-id>' }
138
- case 'directory': case undefined: return { code: 0, out: describeDirectory(directory) }
139
- default: return { code: 2, out: 'idv1 directory | status | health | resolve <id>' }
86
+ case 'resolve': return args.length ? { code: 0, out: args.map(value => describeResolved(directory, resolveCode(directory, value))).join('\n\n') } : { code: 2, out: 'idv1 resolve <code | junction | -NS-code>' }
87
+ case 'directory': case undefined: return { code: 0, out: describeDirectory(directory, problems) }
88
+ default: return { code: 2, out: 'idv1 directory | health | resolve <code>' }
140
89
  }
141
90
  }