@jinn-network/core 0.1.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.
Files changed (111) hide show
  1. package/dist/canonical-json.d.ts +1 -0
  2. package/dist/canonical-json.js +67 -0
  3. package/dist/captured-task.d.ts +140 -0
  4. package/dist/captured-task.js +122 -0
  5. package/dist/contribution-store.d.ts +97 -0
  6. package/dist/contribution-store.js +770 -0
  7. package/dist/corpus-read/acquire.d.ts +39 -0
  8. package/dist/corpus-read/acquire.js +212 -0
  9. package/dist/corpus-read/cache.d.ts +14 -0
  10. package/dist/corpus-read/cache.js +17 -0
  11. package/dist/corpus-read/capture-meta.d.ts +16 -0
  12. package/dist/corpus-read/capture-meta.js +18 -0
  13. package/dist/corpus-read/create-corpus.d.ts +2 -0
  14. package/dist/corpus-read/create-corpus.js +97 -0
  15. package/dist/corpus-read/fetch-artifact.d.ts +21 -0
  16. package/dist/corpus-read/fetch-artifact.js +32 -0
  17. package/dist/corpus-read/fetch.d.ts +12 -0
  18. package/dist/corpus-read/fetch.js +24 -0
  19. package/dist/corpus-read/http-discovery.d.ts +9 -0
  20. package/dist/corpus-read/http-discovery.js +128 -0
  21. package/dist/corpus-read/index.d.ts +10 -0
  22. package/dist/corpus-read/index.js +10 -0
  23. package/dist/corpus-read/ipfs.d.ts +5 -0
  24. package/dist/corpus-read/ipfs.js +72 -0
  25. package/dist/corpus-read/route-resolver.d.ts +16 -0
  26. package/dist/corpus-read/route-resolver.js +19 -0
  27. package/dist/corpus-read/types.d.ts +177 -0
  28. package/dist/corpus-read/types.js +42 -0
  29. package/dist/envelope.d.ts +134 -0
  30. package/dist/envelope.js +162 -0
  31. package/dist/evidence-adapter.d.ts +26 -0
  32. package/dist/evidence-adapter.js +321 -0
  33. package/dist/evidence-filesystem.d.ts +40 -0
  34. package/dist/evidence-filesystem.js +267 -0
  35. package/dist/evidence-index.d.ts +117 -0
  36. package/dist/evidence-index.js +1083 -0
  37. package/dist/evidence-store-lock.d.ts +2 -0
  38. package/dist/evidence-store-lock.js +208 -0
  39. package/dist/execution-envelope.d.ts +3142 -0
  40. package/dist/execution-envelope.js +203 -0
  41. package/dist/index.d.ts +12 -0
  42. package/dist/index.js +12 -0
  43. package/dist/manifest.d.ts +75 -0
  44. package/dist/manifest.js +184 -0
  45. package/dist/paired.d.ts +68 -0
  46. package/dist/paired.js +92 -0
  47. package/dist/scrub/build.d.ts +52 -0
  48. package/dist/scrub/build.js +84 -0
  49. package/dist/scrub/emit-scrub.d.ts +41 -0
  50. package/dist/scrub/emit-scrub.js +87 -0
  51. package/dist/scrub/index.d.ts +12 -0
  52. package/dist/scrub/index.js +12 -0
  53. package/dist/scrub/key-policy.d.ts +20 -0
  54. package/dist/scrub/key-policy.js +42 -0
  55. package/dist/scrub/layer2.d.ts +23 -0
  56. package/dist/scrub/layer2.js +32 -0
  57. package/dist/scrub/ml-pii-stage.d.ts +24 -0
  58. package/dist/scrub/ml-pii-stage.js +42 -0
  59. package/dist/scrub/openredaction-stage.d.ts +196 -0
  60. package/dist/scrub/openredaction-stage.js +270 -0
  61. package/dist/scrub/pii-build.d.ts +38 -0
  62. package/dist/scrub/pii-build.js +67 -0
  63. package/dist/scrub/pipeline.d.ts +38 -0
  64. package/dist/scrub/pipeline.js +83 -0
  65. package/dist/scrub/plain-patterns-stage.d.ts +31 -0
  66. package/dist/scrub/plain-patterns-stage.js +84 -0
  67. package/dist/scrub/secretlint-stage.d.ts +36 -0
  68. package/dist/scrub/secretlint-stage.js +249 -0
  69. package/dist/scrub/transformers-detector.d.ts +35 -0
  70. package/dist/scrub/transformers-detector.js +67 -0
  71. package/dist/scrub/types.d.ts +25 -0
  72. package/dist/scrub/types.js +1 -0
  73. package/dist/session-provenance.d.ts +75 -0
  74. package/dist/session-provenance.js +20 -0
  75. package/dist/skill-artifact.d.ts +442 -0
  76. package/dist/skill-artifact.js +136 -0
  77. package/dist/trajectory/hash-chain.d.ts +18 -0
  78. package/dist/trajectory/hash-chain.js +47 -0
  79. package/dist/trajectory/index.d.ts +4 -0
  80. package/dist/trajectory/index.js +4 -0
  81. package/dist/trajectory/schema.d.ts +606 -0
  82. package/dist/trajectory/schema.js +110 -0
  83. package/dist/trajectory/transcript-parsers/aider-history.d.ts +42 -0
  84. package/dist/trajectory/transcript-parsers/aider-history.js +181 -0
  85. package/dist/trajectory/transcript-parsers/claude-code-jsonl.d.ts +37 -0
  86. package/dist/trajectory/transcript-parsers/claude-code-jsonl.js +142 -0
  87. package/dist/trajectory/transcript-parsers/codex-session.d.ts +42 -0
  88. package/dist/trajectory/transcript-parsers/codex-session.js +235 -0
  89. package/dist/trajectory/transcript-parsers/continue-devdata.d.ts +41 -0
  90. package/dist/trajectory/transcript-parsers/continue-devdata.js +190 -0
  91. package/dist/trajectory/transcript-parsers/cursor-sqlite.d.ts +38 -0
  92. package/dist/trajectory/transcript-parsers/cursor-sqlite.js +118 -0
  93. package/dist/trajectory/transcript-parsers/gemini-session.d.ts +35 -0
  94. package/dist/trajectory/transcript-parsers/gemini-session.js +137 -0
  95. package/dist/trajectory/transcript-parsers/index.d.ts +7 -0
  96. package/dist/trajectory/transcript-parsers/index.js +7 -0
  97. package/dist/trajectory/transcript-parsers/types.d.ts +101 -0
  98. package/dist/trajectory/transcript-parsers/types.js +32 -0
  99. package/dist/trajectory/transcript-to-spans/attrs.d.ts +4 -0
  100. package/dist/trajectory/transcript-to-spans/attrs.js +30 -0
  101. package/dist/trajectory/transcript-to-spans/claude-code-stream-json.d.ts +8 -0
  102. package/dist/trajectory/transcript-to-spans/claude-code-stream-json.js +162 -0
  103. package/dist/trajectory/transcript-to-spans/codex-exec-json.d.ts +8 -0
  104. package/dist/trajectory/transcript-to-spans/codex-exec-json.js +253 -0
  105. package/dist/trajectory/transcript-to-spans/index.d.ts +4 -0
  106. package/dist/trajectory/transcript-to-spans/index.js +4 -0
  107. package/dist/trajectory/transcript-to-spans/types.d.ts +18 -0
  108. package/dist/trajectory/transcript-to-spans/types.js +1 -0
  109. package/dist/window.d.ts +12 -0
  110. package/dist/window.js +5 -0
  111. package/package.json +67 -0
@@ -0,0 +1,2 @@
1
+ export declare function withEvidenceStoreLockSync<T>(episodesDir: string, operation: () => T): T;
2
+ export declare function withEvidenceStoreLock<T>(episodesDir: string, operation: () => Promise<T>, afterUnlock?: (result: T) => Promise<void>): Promise<T>;
@@ -0,0 +1,208 @@
1
+ import { constants, closeSync, fchmodSync, fsyncSync, openSync } from 'node:fs';
2
+ import { join, resolve } from 'node:path';
3
+ import Database from 'better-sqlite3';
4
+ import { inspectRegularPath, nodeErrorCode, prepareEvidenceDirectory, recoverWriterPublicationAliases, secureRegularPath, } from './evidence-filesystem.js';
5
+ const LOCK_FILE = '.jinn-evidence-store-lock.sqlite';
6
+ const LOCK_APPLICATION_ID = 0x4a4c4f43;
7
+ const LOCK_SCHEMA_VERSION = '1';
8
+ const LOCK_BUSY_TIMEOUT_MS = 30_000;
9
+ const asyncTails = new Map();
10
+ const activeAsyncKeys = new Set();
11
+ function prepareLockFile(episodesDir) {
12
+ const directory = resolve(episodesDir);
13
+ prepareEvidenceDirectory(directory, 'evidence store', true);
14
+ const path = join(directory, LOCK_FILE);
15
+ let identity = inspectRegularPath(path, 'evidence store lock');
16
+ if (!identity) {
17
+ let fd;
18
+ try {
19
+ fd = openSync(path, constants.O_RDWR
20
+ | constants.O_CREAT
21
+ | constants.O_EXCL
22
+ | (constants.O_NOFOLLOW ?? 0), 0o600);
23
+ if (process.platform !== 'win32')
24
+ fchmodSync(fd, 0o600);
25
+ fsyncSync(fd);
26
+ }
27
+ catch (error) {
28
+ if (nodeErrorCode(error) !== 'EEXIST')
29
+ throw error;
30
+ }
31
+ finally {
32
+ if (fd !== undefined)
33
+ closeSync(fd);
34
+ }
35
+ identity = inspectRegularPath(path, 'evidence store lock');
36
+ }
37
+ if (!identity)
38
+ throw new Error(`evidence store lock disappeared: ${path}`);
39
+ return { path, identity };
40
+ }
41
+ function openStoreLock(episodesDir) {
42
+ const prepared = prepareLockFile(episodesDir);
43
+ const db = new Database(prepared.path);
44
+ try {
45
+ // Fail before even a writable pragma or schema transaction if the prepared
46
+ // pathname changed between creation/inspection and SQLite open.
47
+ secureRegularPath(prepared.path, 'evidence store lock', prepared.identity);
48
+ db.pragma(`busy_timeout = ${LOCK_BUSY_TIMEOUT_MS}`);
49
+ db.exec('BEGIN IMMEDIATE');
50
+ try {
51
+ const applicationId = Number(db.pragma('application_id', { simple: true }));
52
+ const tables = db.prepare(`
53
+ SELECT name FROM sqlite_master
54
+ WHERE type = 'table' AND name NOT LIKE 'sqlite_%'
55
+ ORDER BY name ASC
56
+ `).all();
57
+ if (applicationId === 0 && tables.length === 0) {
58
+ db.pragma(`application_id = ${LOCK_APPLICATION_ID}`);
59
+ db.exec(`
60
+ CREATE TABLE evidence_store_lock_meta (
61
+ key TEXT PRIMARY KEY,
62
+ value TEXT NOT NULL
63
+ );
64
+ INSERT INTO evidence_store_lock_meta(key, value)
65
+ VALUES ('schema_version', '${LOCK_SCHEMA_VERSION}');
66
+ `);
67
+ }
68
+ else {
69
+ if (applicationId !== LOCK_APPLICATION_ID) {
70
+ throw new Error(`refusing file that is not a Jinn evidence store lock: ${prepared.path}`);
71
+ }
72
+ if (JSON.stringify(tables.map((row) => row.name)) !== JSON.stringify([
73
+ 'evidence_store_lock_meta',
74
+ ])) {
75
+ throw new Error(`refusing evidence store lock with an unexpected schema: ${prepared.path}`);
76
+ }
77
+ const version = db.prepare("SELECT value FROM evidence_store_lock_meta WHERE key = 'schema_version'").get();
78
+ if (version?.value !== LOCK_SCHEMA_VERSION) {
79
+ throw new Error(`unsupported Jinn evidence store lock schema: ${String(version?.value)}`);
80
+ }
81
+ }
82
+ db.exec('COMMIT');
83
+ }
84
+ catch (error) {
85
+ if (db.inTransaction)
86
+ db.exec('ROLLBACK');
87
+ throw error;
88
+ }
89
+ secureRegularPath(prepared.path, 'evidence store lock', prepared.identity);
90
+ return { db, path: prepared.path, identity: prepared.identity };
91
+ }
92
+ catch (error) {
93
+ db.close();
94
+ throw error;
95
+ }
96
+ }
97
+ function closeStoreLock(lock) {
98
+ let failure;
99
+ try {
100
+ lock.db.close();
101
+ }
102
+ catch (error) {
103
+ failure = error;
104
+ }
105
+ try {
106
+ secureRegularPath(lock.path, 'evidence store lock', lock.identity);
107
+ }
108
+ catch (error) {
109
+ failure ??= error;
110
+ }
111
+ if (failure !== undefined)
112
+ throw failure;
113
+ }
114
+ function beginStoreLock(lock) {
115
+ secureRegularPath(lock.path, 'evidence store lock', lock.identity);
116
+ lock.db.exec('BEGIN IMMEDIATE');
117
+ }
118
+ function finishStoreLock(lock, commit) {
119
+ if (lock.db.inTransaction)
120
+ lock.db.exec(commit ? 'COMMIT' : 'ROLLBACK');
121
+ }
122
+ export function withEvidenceStoreLockSync(episodesDir, operation) {
123
+ const key = resolve(episodesDir);
124
+ if (activeAsyncKeys.has(key)) {
125
+ throw new Error(`cannot synchronously acquire an evidence store lock while an async `
126
+ + `operation is active in this process: ${key}`);
127
+ }
128
+ const lock = openStoreLock(episodesDir);
129
+ try {
130
+ beginStoreLock(lock);
131
+ try {
132
+ recoverWriterPublicationAliases(resolve(episodesDir));
133
+ const result = operation();
134
+ finishStoreLock(lock, true);
135
+ return result;
136
+ }
137
+ catch (error) {
138
+ finishStoreLock(lock, false);
139
+ throw error;
140
+ }
141
+ }
142
+ finally {
143
+ closeStoreLock(lock);
144
+ }
145
+ }
146
+ export async function withEvidenceStoreLock(episodesDir, operation, afterUnlock) {
147
+ const key = resolve(episodesDir);
148
+ const previous = asyncTails.get(key) ?? Promise.resolve();
149
+ let release;
150
+ const turn = new Promise((resolveTurn) => {
151
+ release = resolveTurn;
152
+ });
153
+ const tail = previous.catch(() => undefined).then(() => turn);
154
+ asyncTails.set(key, tail);
155
+ await previous.catch(() => undefined);
156
+ let released = false;
157
+ const releaseTurn = () => {
158
+ if (released)
159
+ return;
160
+ released = true;
161
+ release();
162
+ if (asyncTails.get(key) === tail)
163
+ asyncTails.delete(key);
164
+ };
165
+ let ownsActiveMarker = false;
166
+ const clearActiveMarker = () => {
167
+ if (!ownsActiveMarker)
168
+ return;
169
+ ownsActiveMarker = false;
170
+ activeAsyncKeys.delete(key);
171
+ };
172
+ try {
173
+ let result;
174
+ activeAsyncKeys.add(key);
175
+ ownsActiveMarker = true;
176
+ const lock = openStoreLock(episodesDir);
177
+ try {
178
+ beginStoreLock(lock);
179
+ try {
180
+ recoverWriterPublicationAliases(resolve(episodesDir));
181
+ result = await operation();
182
+ finishStoreLock(lock, true);
183
+ }
184
+ catch (error) {
185
+ finishStoreLock(lock, false);
186
+ throw error;
187
+ }
188
+ }
189
+ finally {
190
+ try {
191
+ closeStoreLock(lock);
192
+ }
193
+ finally {
194
+ clearActiveMarker();
195
+ }
196
+ }
197
+ // The cross-process SQLite lock now orders both writes and rebuilds. Let a
198
+ // refresh callback re-enter the same store without waiting on its own
199
+ // process-local queue turn.
200
+ releaseTurn();
201
+ await afterUnlock?.(result);
202
+ return result;
203
+ }
204
+ finally {
205
+ clearActiveMarker();
206
+ releaseTurn();
207
+ }
208
+ }