@0001-labs/idv1 1.0.0 → 1.0.1
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 +15 -2
- package/bin/idv1.mjs +22 -0
- package/package.json +9 -2
- package/src/architecture.ts +129 -0
- package/src/index.ts +1 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# IDv1
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Six-character IDs for agents: five shapes, taxonomy and namespaces, and a command that reads a codebase's `.id` file.
|
|
4
4
|
|
|
5
5
|
```sh
|
|
6
6
|
npm install @0001-labs/idv1
|
|
@@ -15,10 +15,23 @@ const parsed = parseCode(candidate)
|
|
|
15
15
|
|
|
16
16
|
`parseCode(unknown)` returns a discriminated `{ kind, id }` with a shape-branded TypeScript `Id`, or `null`. `codeKind` and the `isCodeOf`/shape predicates use that parser. Validation establishes syntax, not an assigned record. `AccountId`, `ListId`, `ProjectId`, `ItemId` and `EventId` are distinct types.
|
|
17
17
|
|
|
18
|
-
`codeCandidate(kind)` draws with Web Crypto and returns the corresponding branded ID. All generators use mixed-case ASCII letters. Account and Project accept an optional exact two-letter prefix, preserving case: `codeCandidate('project', { letters: 'AR' })`. Candidates obey word filters; Project also excludes blocked digit pairs. A bounded draw fails after 1,000 attempts. An injected `random` must return finite numbers in `[0, 1)`. These are candidate generators, not allocators:
|
|
18
|
+
`codeCandidate(kind)` draws with Web Crypto and returns the corresponding branded ID. All generators use mixed-case ASCII letters. Account and Project accept an optional exact two-letter prefix, preserving case: `codeCandidate('project', { letters: 'AR' })`. Candidates obey word filters; Project also excludes blocked digit pairs. A bounded draw fails after 1,000 attempts. An injected `random` must return finite numbers in `[0, 1)`. These are candidate generators, not allocators: claim a candidate in its namespace and store it next to the record's UUID. A code is a short handle for agents, not a permanent identity; when a pool runs out it resets and its codes are issued again.
|
|
19
19
|
|
|
20
20
|
`ID_SPACE` counts the five structural pools before generation filters; `TOTAL_ID_SPACE` is their sum. Event is part of the main API, without a separate expanded classifier. Scan encoding retains its checksum and payload format, accepts exactly the same six-character IDs, and all scanner paths support all five shapes. Scan results are branded IDs; use `parseCode` to read their kind.
|
|
21
21
|
|
|
22
|
+
## The idv1 command
|
|
23
|
+
|
|
24
|
+
A codebase that uses IDv1 keeps one `.id` file at its root: its namespaces and taxonomies, each with its shorthand and real name, then its parts and the facts between them ([format](https://idv1.0001.dev/docs/annotate)). The `idv1` command reads it from any folder below:
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
npx idv1 architecture # namespaces, taxonomies, the parts as a tree, the facts
|
|
28
|
+
npx idv1 status # counts by shape and taxonomy, and whether it is healthy
|
|
29
|
+
npx idv1 health # every broken rule; exits 1 so it can run in a build
|
|
30
|
+
npx idv1 resolve co14Ch # one id, a -SH- qualified id or a junction, with its facts
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
It runs on Node 22.18 or later. The same functions are exported for apps: `readArchitecture`, `architectureHealth`, `resolveCode`, `taxonomyOf`, `describeArchitecture`, `describeStatus` and `runArchitectureCommand`.
|
|
34
|
+
|
|
22
35
|
## Upgrading from 0.3.x to Release 1
|
|
23
36
|
|
|
24
37
|
Release 1 uses five shapes. Here `L` is an ASCII letter and `N` is a digit.
|
package/bin/idv1.mjs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
3
|
+
import { dirname, join, resolve } from 'node:path'
|
|
4
|
+
import { register } from 'node:module'
|
|
5
|
+
|
|
6
|
+
register('data:text/javascript,' + encodeURIComponent(`export async function resolve(specifier, context, next) {
|
|
7
|
+
if (/^\\.\\.?\\//.test(specifier) && !/\\.[cm]?[jt]s$/.test(specifier) && context.parentURL?.includes('/src/')) return next(specifier + '.ts', context)
|
|
8
|
+
return next(specifier, context)
|
|
9
|
+
}`))
|
|
10
|
+
const { runArchitectureCommand } = await import('../src/architecture.ts')
|
|
11
|
+
|
|
12
|
+
const [command, ...args] = process.argv.slice(2)
|
|
13
|
+
let dir = resolve(process.cwd())
|
|
14
|
+
while (!existsSync(join(dir, '.id')) && dirname(dir) !== dir) dir = dirname(dir)
|
|
15
|
+
const file = join(dir, '.id')
|
|
16
|
+
if (!existsSync(file)) {
|
|
17
|
+
console.error('No .id file here or above. See https://idv1.0001.dev/docs/annotate')
|
|
18
|
+
process.exit(2)
|
|
19
|
+
}
|
|
20
|
+
const { code, out } = runArchitectureCommand(command, args, readFileSync(file, 'utf8'), path => existsSync(join(dir, path)))
|
|
21
|
+
console.log(out)
|
|
22
|
+
process.exit(code)
|
package/package.json
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@0001-labs/idv1",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.0.1",
|
|
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": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "git+https://github.com/0001-labs/idv1.git"
|
|
9
9
|
},
|
|
10
|
+
"bin": {
|
|
11
|
+
"idv1": "./bin/idv1.mjs"
|
|
12
|
+
},
|
|
10
13
|
"files": [
|
|
14
|
+
"bin/",
|
|
11
15
|
"src/",
|
|
12
16
|
"README.md",
|
|
13
17
|
"LICENSE"
|
|
@@ -16,6 +20,9 @@
|
|
|
16
20
|
"exports": {
|
|
17
21
|
".": "./src/index.ts"
|
|
18
22
|
},
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=22.18"
|
|
25
|
+
},
|
|
19
26
|
"devDependencies": {
|
|
20
27
|
"@types/node": "^22.13.1",
|
|
21
28
|
"typescript": "^6.0.3",
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { parseCode, type CodeKind } from './shapes'
|
|
2
|
+
import { blockedCodeLetters, blockedProjectCode } from './words'
|
|
3
|
+
import { JUNCTION_SEPARATOR } from './junction'
|
|
4
|
+
|
|
5
|
+
export type Namespace = { shorthand: string; name: string }
|
|
6
|
+
export type Taxonomy = { shorthand: string; name: string; shape: CodeKind; pattern: string }
|
|
7
|
+
export type Part = { id: string; shape: CodeKind; name: string; path: string | null }
|
|
8
|
+
export type Architecture = { namespaces: Namespace[]; taxonomies: Taxonomy[]; parts: Part[]; facts: string[] }
|
|
9
|
+
export type Resolved = { id: string; shape: CodeKind | null; namespace?: Namespace; taxonomy?: Taxonomy; part?: Part; facts: string[] }
|
|
10
|
+
|
|
11
|
+
const SHAPES: CodeKind[] = ['account', 'list', 'project', 'item', 'event']
|
|
12
|
+
const list = <T>(value: unknown): T[] => (Array.isArray(value) ? value : []) as T[]
|
|
13
|
+
|
|
14
|
+
export function readArchitecture(text: string): Architecture {
|
|
15
|
+
const value = JSON.parse(text) as Partial<Architecture>
|
|
16
|
+
return { namespaces: list(value.namespaces), taxonomies: list(value.taxonomies), parts: list(value.parts), facts: list(value.facts) }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function taxonomyOf(architecture: Architecture, id: string): Taxonomy | undefined {
|
|
20
|
+
const shape = parseCode(id)?.kind
|
|
21
|
+
return architecture.taxonomies.find(taxonomy => {
|
|
22
|
+
const at = taxonomy.pattern.replace(/\s/g, '').indexOf(taxonomy.shorthand)
|
|
23
|
+
return taxonomy.shape === shape && at >= 0 && id.slice(at, at + 2) === taxonomy.shorthand
|
|
24
|
+
})
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function architectureHealth(architecture: Architecture, exists: (path: string) => boolean = () => true): string[] {
|
|
28
|
+
const problems: string[] = []
|
|
29
|
+
const seen = (values: string[], what: string) => values.forEach((value, index) => { if (values.indexOf(value) !== index) problems.push(`${what} ${value} is declared twice`) })
|
|
30
|
+
for (const { shorthand, name } of architecture.namespaces) {
|
|
31
|
+
if (!/^[A-Z]{2}$/.test(shorthand)) problems.push(`Namespace ${shorthand} is not two capitals`)
|
|
32
|
+
if (!name) problems.push(`Namespace ${shorthand} has no name`)
|
|
33
|
+
}
|
|
34
|
+
for (const { shorthand, name, shape, pattern } of architecture.taxonomies) {
|
|
35
|
+
if (!/^[a-z]{2}$/.test(shorthand)) problems.push(`Taxonomy ${shorthand} is not two small letters`)
|
|
36
|
+
if (!name) problems.push(`Taxonomy ${shorthand} has no name`)
|
|
37
|
+
if (!SHAPES.includes(shape)) problems.push(`Taxonomy ${shorthand} has no shape`)
|
|
38
|
+
const compact = String(pattern ?? '').replace(/\s/g, '')
|
|
39
|
+
if (compact.length !== 6 || !compact.includes(shorthand)) problems.push(`Taxonomy ${shorthand} has no six-character pattern holding ${shorthand}`)
|
|
40
|
+
}
|
|
41
|
+
seen(architecture.namespaces.map(namespace => namespace.shorthand), 'Namespace')
|
|
42
|
+
seen(architecture.taxonomies.map(taxonomy => taxonomy.shorthand), 'Taxonomy')
|
|
43
|
+
seen(architecture.parts.map(part => part.id), 'Id')
|
|
44
|
+
const ids = new Set(architecture.parts.map(part => part.id))
|
|
45
|
+
for (const part of architecture.parts) {
|
|
46
|
+
const shape = parseCode(part.id)?.kind
|
|
47
|
+
if (shape !== part.shape) problems.push(`${part.id} reads as ${shape ?? 'no shape'}, not ${part.shape}`)
|
|
48
|
+
else if (shape === 'project' ? blockedProjectCode(part.id) : blockedCodeLetters(part.id)) problems.push(`${part.id} uses blocked letters or digits`)
|
|
49
|
+
if (!part.name) problems.push(`${part.id} has no name`)
|
|
50
|
+
if (part.path && !exists(part.path)) problems.push(`${part.id}: ${part.path} is gone`)
|
|
51
|
+
}
|
|
52
|
+
for (const fact of architecture.facts) {
|
|
53
|
+
const [subject, relation, object, extra] = fact.split(' ')
|
|
54
|
+
if (extra !== undefined || !/^r[A-Za-z]{5}$/.test(relation ?? '')) problems.push(`Not a fact: ${fact}`)
|
|
55
|
+
else for (const id of [subject, object]) if (!ids.has(id)) problems.push(`Unknown id ${id} in: ${fact}`)
|
|
56
|
+
}
|
|
57
|
+
return problems
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function resolveCode(architecture: Architecture, value: string): Resolved[] {
|
|
61
|
+
const qualified = /^-([A-Z]{2})-(.+)$/.exec(value)
|
|
62
|
+
const ids = (qualified ? qualified[2] : value).split(JUNCTION_SEPARATOR)
|
|
63
|
+
return ids.map(id => {
|
|
64
|
+
const shape = parseCode(id)?.kind ?? null
|
|
65
|
+
const shorthand = qualified?.[1] ?? (shape === 'project' ? id.slice(0, 2) : undefined)
|
|
66
|
+
const namespace = architecture.namespaces.find(namespace => namespace.shorthand === shorthand) ?? (architecture.namespaces.length === 1 ? architecture.namespaces[0] : undefined)
|
|
67
|
+
const facts = architecture.facts.filter(fact => { const [subject, , object] = fact.split(' '); return subject === id || object === id })
|
|
68
|
+
return { id, shape, namespace, taxonomy: shape ? taxonomyOf(architecture, id) : undefined, part: architecture.parts.find(part => part.id === id), facts }
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const nameOf = (architecture: Architecture, id: string) => architecture.parts.find(part => part.id === id)?.name ?? id
|
|
73
|
+
const readFact = (architecture: Architecture, fact: string) => { const [subject, relation, object] = fact.split(' '); return `${subject} ${relation} ${object} ${nameOf(architecture, subject)} → ${nameOf(architecture, object)}` }
|
|
74
|
+
|
|
75
|
+
export function describeResolved(architecture: Architecture, resolved: Resolved[]): string {
|
|
76
|
+
return resolved.map(({ id, shape, namespace, taxonomy, part, facts }) => [
|
|
77
|
+
`${id} ${part?.name ?? (shape ? 'not in .id' : 'not a code')}`,
|
|
78
|
+
` shape ${shape ?? '—'}`,
|
|
79
|
+
...(taxonomy ? [` taxonomy ${taxonomy.shorthand} · ${taxonomy.name}`] : []),
|
|
80
|
+
...(namespace ? [` namespace ${namespace.shorthand} · ${namespace.name}`] : []),
|
|
81
|
+
...(part?.path ? [` path ${part.path}`] : []),
|
|
82
|
+
...facts.map(fact => ` fact ${readFact(architecture, fact)}`),
|
|
83
|
+
].join('\n')).join('\n\n')
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function describeStatus(architecture: Architecture, problems: string[]): string {
|
|
87
|
+
const count = (n: number, one: string, many = `${one}s`) => `${n} ${n === 1 ? one : many}`
|
|
88
|
+
const shapes = SHAPES.map(shape => [shape, architecture.parts.filter(part => part.shape === shape).length] as const).filter(([, n]) => n)
|
|
89
|
+
return [
|
|
90
|
+
`${count(architecture.parts.length, 'part')} · ${count(architecture.facts.length, 'fact')} · ${count(architecture.namespaces.length, 'namespace')} · ${count(architecture.taxonomies.length, 'taxonomy', 'taxonomies')}`,
|
|
91
|
+
problems.length ? `${count(problems.length, 'problem')}: run idv1 health` : 'healthy',
|
|
92
|
+
'',
|
|
93
|
+
...shapes.map(([shape, n]) => ` ${shape.padEnd(9)}${n}`),
|
|
94
|
+
...architecture.taxonomies.map(taxonomy => ` ${taxonomy.shorthand.padEnd(9)}${architecture.parts.filter(part => taxonomyOf(architecture, part.id) === taxonomy).length} ${taxonomy.name}`),
|
|
95
|
+
].join('\n')
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function describeArchitecture(architecture: Architecture): string {
|
|
99
|
+
const contains = architecture.facts.map(fact => fact.split(' ')).filter(([, relation]) => relation === 'rHasPt')
|
|
100
|
+
const children = (id: string) => contains.filter(([subject]) => subject === id).map(([, , object]) => object)
|
|
101
|
+
const roots = architecture.parts.filter(part => !contains.some(([, , object]) => object === part.id))
|
|
102
|
+
const line = (part: Part | undefined, id: string, depth: number) => `${' '.repeat(depth)}${id} ${part?.name ?? '?'}${part && taxonomyOf(architecture, id) ? ` (${taxonomyOf(architecture, id)!.name})` : ''}${part?.path ? ` ${part.path}` : ''}`
|
|
103
|
+
const tree = (id: string, depth: number, trail: Set<string>): string[] => trail.has(id) ? [] : [line(architecture.parts.find(part => part.id === id), id, depth), ...children(id).flatMap(child => tree(child, depth + 1, new Set([...trail, id])))]
|
|
104
|
+
return [
|
|
105
|
+
'Namespaces',
|
|
106
|
+
...architecture.namespaces.map(({ shorthand, name }) => ` ${shorthand} ${name}`),
|
|
107
|
+
'',
|
|
108
|
+
'Taxonomies',
|
|
109
|
+
...architecture.taxonomies.map(({ shorthand, name, shape, pattern }) => ` ${shorthand} ${name} ${shape} ${pattern}`),
|
|
110
|
+
'',
|
|
111
|
+
'Parts',
|
|
112
|
+
...roots.flatMap(part => tree(part.id, 1, new Set())),
|
|
113
|
+
'',
|
|
114
|
+
'Facts',
|
|
115
|
+
...architecture.facts.filter(fact => fact.split(' ')[1] !== 'rHasPt').map(fact => ` ${readFact(architecture, fact)}`),
|
|
116
|
+
].join('\n')
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function runArchitectureCommand(command: string | undefined, args: string[], text: string, exists?: (path: string) => boolean): { code: number; out: string } {
|
|
120
|
+
const architecture = readArchitecture(text)
|
|
121
|
+
const problems = architectureHealth(architecture, exists)
|
|
122
|
+
switch (command) {
|
|
123
|
+
case 'health': return { code: problems.length ? 1 : 0, out: problems.length ? problems.join('\n') : 'healthy' }
|
|
124
|
+
case 'status': return { code: 0, out: describeStatus(architecture, problems) }
|
|
125
|
+
case 'resolve': return args.length ? { code: 0, out: args.map(value => describeResolved(architecture, resolveCode(architecture, value))).join('\n\n') } : { code: 2, out: 'idv1 resolve <id | junction | -NS-id>' }
|
|
126
|
+
case 'architecture': case undefined: return { code: 0, out: describeArchitecture(architecture) }
|
|
127
|
+
default: return { code: 2, out: 'idv1 architecture | status | health | resolve <id>' }
|
|
128
|
+
}
|
|
129
|
+
}
|
package/src/index.ts
CHANGED