jg-agent-hq 2.0.0 → 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.
Files changed (75) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/README.md +4 -4
  3. package/api/README.md +51 -8
  4. package/dist/cli/cli.js +35 -3
  5. package/dist/cli/utilities.d.ts +3 -0
  6. package/dist/cli/utilities.js +30 -0
  7. package/dist/contracts/index.d.ts +2 -2
  8. package/dist/contracts/index.js +1 -1
  9. package/dist/contracts/types.d.ts +102 -0
  10. package/dist/contracts/types.js +24 -0
  11. package/dist/index.d.ts +2 -0
  12. package/dist/index.js +2 -0
  13. package/dist/plans/authority.d.ts +2 -0
  14. package/dist/plans/authority.js +8 -0
  15. package/dist/plans/documents.d.ts +17 -0
  16. package/dist/plans/documents.js +24 -2
  17. package/dist/plans/files.js +3 -1
  18. package/dist/plans/plans.js +3 -1
  19. package/dist/plans/types.d.ts +5 -0
  20. package/dist/providers/claude/claude.js +2 -0
  21. package/dist/providers/claude/history/types.d.ts +23 -0
  22. package/dist/providers/claude/transformers.js +14 -1
  23. package/dist/providers/codex/codex.js +4 -0
  24. package/dist/providers/codex/transformers.js +30 -3
  25. package/dist/providers/codex/types.d.ts +34 -1
  26. package/dist/providers/codex/types.js +11 -2
  27. package/dist/runtime/runtime.d.ts +2 -0
  28. package/dist/runtime/runtime.js +2 -0
  29. package/dist/runtime/state/index.d.ts +1 -1
  30. package/dist/runtime/state/index.js +1 -1
  31. package/dist/runtime/state/state.d.ts +16 -1
  32. package/dist/runtime/state/state.js +94 -3
  33. package/dist/snapshots/capture.d.ts +14 -0
  34. package/dist/snapshots/capture.js +142 -0
  35. package/dist/snapshots/database.d.ts +2 -0
  36. package/dist/snapshots/database.js +15 -51
  37. package/dist/snapshots/fences.d.ts +15 -0
  38. package/dist/snapshots/fences.js +64 -0
  39. package/dist/snapshots/index.d.ts +2 -0
  40. package/dist/snapshots/index.js +3 -0
  41. package/dist/snapshots/snapshots.d.ts +4 -1
  42. package/dist/snapshots/snapshots.js +27 -113
  43. package/dist/snapshots/snapshots.worker.js +5 -1
  44. package/dist/snapshots/types.d.ts +11 -0
  45. package/dist/snapshots/types.js +5 -1
  46. package/dist/storage/index.d.ts +2 -1
  47. package/dist/storage/index.js +2 -1
  48. package/dist/storage/schema/constants.d.ts +2 -0
  49. package/dist/storage/schema/constants.js +3 -0
  50. package/dist/storage/schema/index.d.ts +4 -0
  51. package/dist/storage/schema/index.js +5 -0
  52. package/dist/storage/schema/schema.d.ts +16 -0
  53. package/dist/storage/schema/schema.js +73 -0
  54. package/dist/storage/schema/types.d.ts +17 -0
  55. package/dist/storage/schema/types.js +8 -0
  56. package/dist/storage/schema/v1-to-v2.sql +1 -0
  57. package/dist/storage/schema/v1.sql +538 -0
  58. package/dist/storage/types.d.ts +0 -5
  59. package/dist/storage/types.js +0 -6
  60. package/dist/storage/upgrade/index.d.ts +2 -0
  61. package/dist/storage/upgrade/index.js +2 -0
  62. package/dist/storage/upgrade/types.d.ts +61 -0
  63. package/dist/storage/upgrade/types.js +42 -0
  64. package/dist/storage/upgrade/upgrade.d.ts +33 -0
  65. package/dist/storage/upgrade/upgrade.js +196 -0
  66. package/dist/storage/upgrade/upgrade.worker.d.ts +1 -0
  67. package/dist/storage/upgrade/upgrade.worker.js +220 -0
  68. package/dist/store/schema.d.ts +2 -0
  69. package/dist/store/schema.js +16 -25
  70. package/dist/store/schema.sql +2 -1
  71. package/dist/store/store.d.ts +2 -0
  72. package/dist/store/store.js +2 -0
  73. package/docs/recovery.md +15 -1
  74. package/errors/README.md +34 -28
  75. package/package.json +2 -2
@@ -158,8 +158,19 @@ export declare const SnapshotWorkerResponseSchema: z.ZodDiscriminatedUnion<[z.Zo
158
158
  }, z.core.$strip>, z.ZodObject<{
159
159
  kind: z.ZodLiteral<"error">;
160
160
  code: z.ZodEnum<{
161
+ JG_AGENT_HQ_STORAGE_VERSION_UNSUPPORTED: "JG_AGENT_HQ_STORAGE_VERSION_UNSUPPORTED";
161
162
  JG_AGENT_HQ_SNAPSHOT_INVALID: "JG_AGENT_HQ_SNAPSHOT_INVALID";
162
163
  JG_AGENT_HQ_SNAPSHOT_FAILED: "JG_AGENT_HQ_SNAPSHOT_FAILED";
163
164
  }>;
164
165
  message: z.ZodString;
165
166
  }, z.core.$strip>], "kind">;
167
+ export type ISnapshotSource = {
168
+ stateRoot: string;
169
+ coordinationRoot: string;
170
+ allowedRoots: string[];
171
+ actionKeyHash: string;
172
+ schemaHash: string;
173
+ backupDatabase: (path: string) => Promise<unknown>;
174
+ readPlans: (after: string) => Promise<string[]>;
175
+ verify: (directory: string) => Promise<IHqSnapshotInspection>;
176
+ };
@@ -60,7 +60,11 @@ export const SnapshotWorkerResponseSchema = z.discriminatedUnion('kind', [
60
60
  z.object({ kind: z.literal('result'), result: SnapshotInspectionSchema }),
61
61
  z.object({
62
62
  kind: z.literal('error'),
63
- code: z.enum(['JG_AGENT_HQ_SNAPSHOT_INVALID', 'JG_AGENT_HQ_SNAPSHOT_FAILED']),
63
+ code: z.enum([
64
+ 'JG_AGENT_HQ_SNAPSHOT_INVALID',
65
+ 'JG_AGENT_HQ_SNAPSHOT_FAILED',
66
+ 'JG_AGENT_HQ_STORAGE_VERSION_UNSUPPORTED',
67
+ ]),
64
68
  message: z.string(),
65
69
  }),
66
70
  ]);
@@ -1,4 +1,5 @@
1
- export { StorageDescriptorSchema, StorageStampSchema } from './types.js';
1
+ export { StorageDescriptorSchema } from './types.js';
2
+ export { StorageStampSchema } from './schema/index.js';
2
3
  export type { IStorageDescriptor, IStorageMaintenanceOperation } from './types.js';
3
4
  export { STORAGE_CONNECTION_SETTINGS } from './constants.js';
4
5
  export { createStorage } from './storage.js';
@@ -1,5 +1,6 @@
1
1
  // types and validated worker metadata
2
- export { StorageDescriptorSchema, StorageStampSchema } from './types.js';
2
+ export { StorageDescriptorSchema } from './types.js';
3
+ export { StorageStampSchema } from './schema/index.js';
3
4
  // required connection settings
4
5
  export { STORAGE_CONNECTION_SETTINGS } from './constants.js';
5
6
  // runtime maintenance admission
@@ -0,0 +1,2 @@
1
+ export declare const V1_SCHEMA_HASH = "af0686f5f4a2af3cbe033a9aab2982d041a4cb0b2fc47088bdd9e15963c66fee";
2
+ export declare const CURRENT_SCHEMA_VERSION = 2;
@@ -0,0 +1,3 @@
1
+ // exact published 2.0.0 schema; this compatibility asset must never follow the current schema
2
+ export const V1_SCHEMA_HASH = 'af0686f5f4a2af3cbe033a9aab2982d041a4cb0b2fc47088bdd9e15963c66fee';
3
+ export const CURRENT_SCHEMA_VERSION = 2;
@@ -0,0 +1,4 @@
1
+ export { StorageStampSchema } from './types.js';
2
+ export type { IStorageSchema, IStorageMigration } from './types.js';
3
+ export { CURRENT_SCHEMA_VERSION, V1_SCHEMA_HASH } from './constants.js';
4
+ export { STORAGE_SCHEMAS, STORAGE_MIGRATIONS, readStorageSchema } from './schema.js';
@@ -0,0 +1,5 @@
1
+ // contracts
2
+ export { StorageStampSchema } from './types.js';
3
+ // lineage and recognition
4
+ export { CURRENT_SCHEMA_VERSION, V1_SCHEMA_HASH } from './constants.js';
5
+ export { STORAGE_SCHEMAS, STORAGE_MIGRATIONS, readStorageSchema } from './schema.js';
@@ -0,0 +1,16 @@
1
+ import type { DatabaseSync } from 'node:sqlite';
2
+ import { type IStorageSchema, type IStorageMigration } from './types.js';
3
+ export declare const STORAGE_SCHEMAS: IStorageSchema[];
4
+ export declare const STORAGE_MIGRATIONS: IStorageMigration[];
5
+ /**
6
+ * Recognizes one supported stamp without writing or adopting an arbitrary database.
7
+ * @returns The registered lineage and key hash, or null for a completely empty database.
8
+ * @throws
9
+ * - JG_AGENT_HQ_STORAGE_INVALID: The HQ storage is invalid
10
+ * - JG_AGENT_HQ_STORAGE_VERSION_UNSUPPORTED: The HQ storage version is unsupported
11
+ * - JG_AGENT_HQ_ACTION_KEY_UNAVAILABLE: The HQ action key is unavailable
12
+ */
13
+ export declare const readStorageSchema: (database: DatabaseSync, actionKeyHash?: string) => {
14
+ schema: IStorageSchema;
15
+ actionKeyHash: string;
16
+ } | null;
@@ -0,0 +1,73 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { hashValue } from 'jg-crypto';
3
+ import { Exception, isException, extractMessage } from 'jg-exception';
4
+ import { CURRENT_SCHEMA_VERSION, V1_SCHEMA_HASH } from './constants.js';
5
+ import { StorageStampSchema } from './types.js';
6
+ // package-owned SQL is small and immutable during a process lifetime
7
+ export const STORAGE_SCHEMAS = [
8
+ { version: 1, hash: V1_SCHEMA_HASH, restoreFenceVersion: 1 },
9
+ {
10
+ version: CURRENT_SCHEMA_VERSION,
11
+ hash: hashValue(readFileSync(new URL('../../store/schema.sql', import.meta.url))),
12
+ restoreFenceVersion: 1,
13
+ },
14
+ ];
15
+ export const STORAGE_MIGRATIONS = [
16
+ { fromVersion: 1, toVersion: 2, source: new URL('./v1-to-v2.sql', import.meta.url) },
17
+ ];
18
+ /**
19
+ * Recognizes one supported stamp without writing or adopting an arbitrary database.
20
+ * @returns The registered lineage and key hash, or null for a completely empty database.
21
+ * @throws
22
+ * - JG_AGENT_HQ_STORAGE_INVALID: The HQ storage is invalid
23
+ * - JG_AGENT_HQ_STORAGE_VERSION_UNSUPPORTED: The HQ storage version is unsupported
24
+ * - JG_AGENT_HQ_ACTION_KEY_UNAVAILABLE: The HQ action key is unavailable
25
+ */
26
+ export const readStorageSchema = (database, actionKeyHash) => {
27
+ try {
28
+ if (!database.prepare('SELECT 1 FROM sqlite_schema LIMIT 1').get())
29
+ return null;
30
+ const hasVersion = database
31
+ .prepare('PRAGMA table_xinfo(hq_schema)')
32
+ .all()
33
+ .some((column) => column.name === 'schema_version');
34
+ const rows = database
35
+ .prepare(`SELECT id, schema_hash, action_key_hash${hasVersion ? ', schema_version' : ''} FROM hq_schema LIMIT 2`)
36
+ .all();
37
+ const parsed = StorageStampSchema.safeParse(rows.length === 1 ? rows[0] : null);
38
+ if (!parsed.success)
39
+ throw new Exception('The HQ storage is invalid', {
40
+ code: 'JG_AGENT_HQ_STORAGE_INVALID',
41
+ data: { kind: 'state-conflict' },
42
+ });
43
+ const stamp = parsed.data;
44
+ const schema = hasVersion
45
+ ? STORAGE_SCHEMAS.find((entry) => entry.version === stamp.schema_version)
46
+ : STORAGE_SCHEMAS.find((entry) => entry.version === 1 && entry.hash === stamp.schema_hash);
47
+ if (!schema)
48
+ throw new Exception('The HQ storage version is unsupported', {
49
+ code: 'JG_AGENT_HQ_STORAGE_VERSION_UNSUPPORTED',
50
+ data: { kind: 'state-conflict' },
51
+ });
52
+ if (schema.hash !== stamp.schema_hash)
53
+ throw new Exception('The HQ storage is invalid', {
54
+ code: 'JG_AGENT_HQ_STORAGE_INVALID',
55
+ data: { kind: 'state-conflict' },
56
+ });
57
+ if (actionKeyHash !== undefined && stamp.action_key_hash !== actionKeyHash)
58
+ throw new Exception('The HQ action key is unavailable', {
59
+ code: 'JG_AGENT_HQ_ACTION_KEY_UNAVAILABLE',
60
+ data: { kind: 'state-conflict' },
61
+ });
62
+ return { schema, actionKeyHash: stamp.action_key_hash };
63
+ }
64
+ catch (cause) {
65
+ if (isException(cause))
66
+ throw cause;
67
+ throw new Exception(`The HQ storage is invalid: ${extractMessage(cause)}`, {
68
+ code: 'JG_AGENT_HQ_STORAGE_INVALID',
69
+ data: { kind: 'state-conflict' },
70
+ cause,
71
+ });
72
+ }
73
+ };
@@ -0,0 +1,17 @@
1
+ import { z } from 'zod';
2
+ export declare const StorageStampSchema: z.ZodObject<{
3
+ id: z.ZodLiteral<1>;
4
+ schema_hash: z.ZodString;
5
+ action_key_hash: z.ZodString;
6
+ schema_version: z.ZodOptional<z.ZodNumber>;
7
+ }, z.core.$strip>;
8
+ export type IStorageSchema = {
9
+ version: number;
10
+ hash: string;
11
+ restoreFenceVersion: number | null;
12
+ };
13
+ export type IStorageMigration = {
14
+ fromVersion: number;
15
+ toVersion: number;
16
+ source: URL;
17
+ };
@@ -0,0 +1,8 @@
1
+ import { z } from 'zod';
2
+ // singleton identity; an absent version is accepted only for the exact published baseline
3
+ export const StorageStampSchema = z.object({
4
+ id: z.literal(1),
5
+ schema_hash: z.string().regex(/^[a-f0-9]{64}$/),
6
+ action_key_hash: z.string().regex(/^[a-f0-9]{64}$/),
7
+ schema_version: z.number().int().positive().safe().optional(),
8
+ });
@@ -0,0 +1 @@
1
+ ALTER TABLE hq_schema ADD COLUMN schema_version INTEGER NOT NULL DEFAULT 2 CHECK (schema_version > 0);