@invictus-group-ai/core 0.5.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.
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Append-only per-dev JSONL event log (ARCHITECTURE §5).
3
+ *
4
+ * One file per developer: `<dir>/<devId>.jsonl` (canonically
5
+ * `.invictus/log/<devId>.jsonl`). Each line is one EventLogEntry.
6
+ *
7
+ * Chain design:
8
+ * - `h` = first 12 hex chars of sha256(canonicalJson(entry without `h`))
9
+ * - `prev` = previous entry's `h` by the same dev, or null for the head.
10
+ *
11
+ * verifyChain detects: edited entries (hash mismatch), deleted / inserted /
12
+ * reordered entries (prev-link breaks). Honest limitation: truncating the
13
+ * TAIL of the file is not detectable from the log alone — the external
14
+ * anchor is the trailer's `j`/`prev` fields living in immutable git history.
15
+ *
16
+ * Concurrency: one writer per dev file by design (the dev's own machine).
17
+ * There is no cross-process file locking.
18
+ */
19
+ import type { EventLogEntry, EventType } from './types.js';
20
+ /** Fields the caller provides; v/dev/prev/h are computed by appendEvent. */
21
+ export interface AppendEventInput {
22
+ /** Session ID (or short hash of it). */
23
+ sid: string;
24
+ type: EventType;
25
+ /** Type-specific payload. Must be JSON-serializable. Metrics, never content. */
26
+ data: Record<string, unknown>;
27
+ /** Unix seconds; defaults to now. */
28
+ ts?: number;
29
+ }
30
+ export type ChainIssueReason = 'invalid-json' | 'invalid-schema' | 'dev-mismatch' | 'hash-mismatch' | 'chain-broken';
31
+ export interface ChainIssue {
32
+ /** 1-based line number in the JSONL file. */
33
+ line: number;
34
+ reason: ChainIssueReason;
35
+ detail: string;
36
+ }
37
+ export interface ChainVerifyResult {
38
+ ok: boolean;
39
+ /** Number of non-empty lines examined. */
40
+ entries: number;
41
+ issues: ChainIssue[];
42
+ }
43
+ /** Canonical log directory for a client repo: `<rootDir>/.invictus/log`. */
44
+ export declare function defaultLogDir(rootDir: string): string;
45
+ /** Path of one dev's log file inside a log directory. */
46
+ export declare function logPath(dir: string, devId: string): string;
47
+ /** Hash of an entry: sha256 prefix over canonical JSON of the entry sans `h`. */
48
+ export declare function entryHash(entry: Omit<EventLogEntry, 'h'> & {
49
+ h?: string;
50
+ }): string;
51
+ /** The last entry in a dev's log, or null when the log is empty/missing. */
52
+ export declare function lastEntry(dir: string, devId: string): EventLogEntry | null;
53
+ /**
54
+ * Append one event to `<dir>/<devId>.jsonl`, computing `h` and linking
55
+ * `prev` to the last entry by that dev. Creates dir/file on first use.
56
+ * Returns the full entry as written.
57
+ */
58
+ export declare function appendEvent(dir: string, devId: string, input: AppendEventInput): EventLogEntry;
59
+ /**
60
+ * Read and validate a dev's full log. Throws (with line number) on any
61
+ * invalid line — use verifyChain for a non-throwing diagnostic.
62
+ * Missing file ⇒ empty array.
63
+ */
64
+ export declare function readLog(dir: string, devId: string): EventLogEntry[];
65
+ /**
66
+ * Verify a dev's hash chain. Collects every issue instead of throwing:
67
+ * - `hash-mismatch` → the entry was edited after being written
68
+ * - `chain-broken` → an entry was deleted, inserted, or reordered
69
+ * - `dev-mismatch` → an entry from another dev landed in this file
70
+ * - `invalid-json` / `invalid-schema` → corrupt or foreign lines
71
+ *
72
+ * An empty or missing log verifies ok with 0 entries.
73
+ */
74
+ export declare function verifyChain(dir: string, devId: string): ChainVerifyResult;
75
+ //# sourceMappingURL=eventlog.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"eventlog.d.ts","sourceRoot":"","sources":["../src/eventlog.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAMH,OAAO,KAAK,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE3D,4EAA4E;AAC5E,MAAM,WAAW,gBAAgB;IAC/B,wCAAwC;IACxC,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,SAAS,CAAC;IAChB,gFAAgF;IAChF,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC9B,qCAA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
1
+ /**
2
+ * Append-only per-dev JSONL event log (ARCHITECTURE §5).
3
+ *
4
+ * One file per developer: `<dir>/<devId>.jsonl` (canonically
5
+ * `.invictus/log/<devId>.jsonl`). Each line is one EventLogEntry.
6
+ *
7
+ * Chain design:
8
+ * - `h` = first 12 hex chars of sha256(canonicalJson(entry without `h`))
9
+ * - `prev` = previous entry's `h` by the same dev, or null for the head.
10
+ *
11
+ * verifyChain detects: edited entries (hash mismatch), deleted / inserted /
12
+ * reordered entries (prev-link breaks). Honest limitation: truncating the
13
+ * TAIL of the file is not detectable from the log alone — the external
14
+ * anchor is the trailer's `j`/`prev` fields living in immutable git history.
15
+ *
16
+ * Concurrency: one writer per dev file by design (the dev's own machine).
17
+ * There is no cross-process file locking.
18
+ */
19
+ import { appendFileSync, existsSync, mkdirSync, readFileSync } from 'node:fs';
20
+ import { join } from 'node:path';
21
+ import { canonicalJson, sha256Prefix } from './canonical.js';
22
+ import { DEV_ID_RE, eventLogEntrySchema, parseEventLogEntry } from './schemas.js';
23
+ function assertDevId(devId) {
24
+ if (!DEV_ID_RE.test(devId)) {
25
+ throw new TypeError(`invalid dev id: ${JSON.stringify(devId)} (expected d_xxxxxxxx)`);
26
+ }
27
+ }
28
+ /** Canonical log directory for a client repo: `<rootDir>/.invictus/log`. */
29
+ export function defaultLogDir(rootDir) {
30
+ return join(rootDir, '.invictus', 'log');
31
+ }
32
+ /** Path of one dev's log file inside a log directory. */
33
+ export function logPath(dir, devId) {
34
+ assertDevId(devId);
35
+ return join(dir, `${devId}.jsonl`);
36
+ }
37
+ /** Hash of an entry: sha256 prefix over canonical JSON of the entry sans `h`. */
38
+ export function entryHash(entry) {
39
+ const { h: _ignored, ...body } = entry;
40
+ return sha256Prefix(canonicalJson(body));
41
+ }
42
+ function readNonEmptyLines(file) {
43
+ if (!existsSync(file))
44
+ return [];
45
+ return readFileSync(file, 'utf8')
46
+ .split('\n')
47
+ .filter((line) => line.trim() !== '');
48
+ }
49
+ /** The last entry in a dev's log, or null when the log is empty/missing. */
50
+ export function lastEntry(dir, devId) {
51
+ const lines = readNonEmptyLines(logPath(dir, devId));
52
+ const last = lines[lines.length - 1];
53
+ if (last === undefined)
54
+ return null;
55
+ return parseEventLogEntry(JSON.parse(last));
56
+ }
57
+ /**
58
+ * Append one event to `<dir>/<devId>.jsonl`, computing `h` and linking
59
+ * `prev` to the last entry by that dev. Creates dir/file on first use.
60
+ * Returns the full entry as written.
61
+ */
62
+ export function appendEvent(dir, devId, input) {
63
+ assertDevId(devId);
64
+ const prev = lastEntry(dir, devId)?.h ?? null;
65
+ const body = {
66
+ v: 1,
67
+ ts: input.ts ?? Math.floor(Date.now() / 1000),
68
+ dev: devId,
69
+ sid: input.sid,
70
+ type: input.type,
71
+ data: input.data,
72
+ prev,
73
+ };
74
+ const entry = { ...body, h: sha256Prefix(canonicalJson(body)) };
75
+ // Validate before writing so a malformed entry never lands in the log.
76
+ parseEventLogEntry(entry);
77
+ mkdirSync(dir, { recursive: true });
78
+ appendFileSync(logPath(dir, devId), JSON.stringify(entry) + '\n', 'utf8');
79
+ return entry;
80
+ }
81
+ /**
82
+ * Read and validate a dev's full log. Throws (with line number) on any
83
+ * invalid line — use verifyChain for a non-throwing diagnostic.
84
+ * Missing file ⇒ empty array.
85
+ */
86
+ export function readLog(dir, devId) {
87
+ const lines = readNonEmptyLines(logPath(dir, devId));
88
+ return lines.map((line, i) => {
89
+ let raw;
90
+ try {
91
+ raw = JSON.parse(line);
92
+ }
93
+ catch {
94
+ throw new Error(`event log ${devId}.jsonl line ${i + 1}: invalid JSON`);
95
+ }
96
+ try {
97
+ return parseEventLogEntry(raw);
98
+ }
99
+ catch (err) {
100
+ throw new Error(`event log ${devId}.jsonl line ${i + 1}: ${err instanceof Error ? err.message : String(err)}`);
101
+ }
102
+ });
103
+ }
104
+ /**
105
+ * Verify a dev's hash chain. Collects every issue instead of throwing:
106
+ * - `hash-mismatch` → the entry was edited after being written
107
+ * - `chain-broken` → an entry was deleted, inserted, or reordered
108
+ * - `dev-mismatch` → an entry from another dev landed in this file
109
+ * - `invalid-json` / `invalid-schema` → corrupt or foreign lines
110
+ *
111
+ * An empty or missing log verifies ok with 0 entries.
112
+ */
113
+ export function verifyChain(dir, devId) {
114
+ assertDevId(devId);
115
+ const lines = readNonEmptyLines(logPath(dir, devId));
116
+ const issues = [];
117
+ let prevHash = null;
118
+ lines.forEach((line, i) => {
119
+ const lineNo = i + 1;
120
+ let raw;
121
+ try {
122
+ raw = JSON.parse(line);
123
+ }
124
+ catch {
125
+ issues.push({ line: lineNo, reason: 'invalid-json', detail: 'line is not valid JSON' });
126
+ return;
127
+ }
128
+ const parsed = eventLogEntrySchema.safeParse(raw);
129
+ if (!parsed.success) {
130
+ issues.push({
131
+ line: lineNo,
132
+ reason: 'invalid-schema',
133
+ detail: parsed.error.issues.map((iss) => `${iss.path.join('.')}: ${iss.message}`).join('; '),
134
+ });
135
+ return;
136
+ }
137
+ const entry = parsed.data;
138
+ if (entry.dev !== devId) {
139
+ issues.push({
140
+ line: lineNo,
141
+ reason: 'dev-mismatch',
142
+ detail: `entry belongs to ${entry.dev}, file belongs to ${devId}`,
143
+ });
144
+ }
145
+ const recomputed = entryHash(entry);
146
+ if (recomputed !== entry.h) {
147
+ issues.push({
148
+ line: lineNo,
149
+ reason: 'hash-mismatch',
150
+ detail: `recorded h=${entry.h} but canonical hash is ${recomputed} — entry was modified`,
151
+ });
152
+ }
153
+ if (entry.prev !== prevHash) {
154
+ issues.push({
155
+ line: lineNo,
156
+ reason: 'chain-broken',
157
+ detail: `prev=${entry.prev ?? 'null'} does not link to previous entry hash ${prevHash ?? 'null'} — deletion, insertion, or reordering`,
158
+ });
159
+ }
160
+ // Continue from the RECORDED hash so a single edit yields one localized
161
+ // issue instead of cascading chain-broken errors down the file.
162
+ prevHash = entry.h;
163
+ });
164
+ return { ok: issues.length === 0, entries: lines.length, issues };
165
+ }
166
+ //# sourceMappingURL=eventlog.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"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CAAC,EAAE,CAAC;YAC3B,MAAM,CAAC,IAAI,CAAC;gBACV,IAAI,EAAE,MAAM;gBACZ,MAAM,EAAE,eAAe;gBACvB,MAAM,EAAE,cAAc,KAAK,CAAC,CAAC,0BAA0B,UAAU,uBAAuB;aACzF,CAAC,CAAC;QACL,CAAC;QACD,IAAI,KAAK,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC5B,MAAM,CAAC,IAAI,CAAC;gBACV,IAAI,EAAE,MAAM;gBACZ,MAAM,EAAE,cAAc;gBACtB,MAAM,EAAE,QAAQ,KAAK,CAAC,IAAI,IAAI,MAAM,yCAAyC,QAAQ,IAAI,MAAM,uCAAuC;aACvI,CAAC,CAAC;QACL,CAAC;QACD,wEAAwE;QACxE,gEAAgE;QAChE,QAAQ,GAAG,KAAK,CAAC,CAAC,CAAC;IACrB,CAAC,CAAC,CAAC;IAEH,OAAO,EAAE,EAAE,EAAE,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,OAAO,EAAE,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC;AACpE,CAAC"}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * @invictus-group-ai/core — schemas, event log, trailer codec, manifest, dev IDs.
3
+ * Pure library: no CLI, no hooks, no side effects at import time.
4
+ */
5
+ export * from './types.js';
6
+ export * from './schemas.js';
7
+ export * from './canonical.js';
8
+ export * from './cbor.js';
9
+ export * from './eventlog.js';
10
+ export * from './trailer.js';
11
+ export * from './manifest.js';
12
+ export * from './devid.js';
13
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * @invictus-group-ai/core — schemas, event log, trailer codec, manifest, dev IDs.
3
+ * Pure library: no CLI, no hooks, no side effects at import time.
4
+ */
5
+ export * from './types.js';
6
+ export * from './schemas.js';
7
+ export * from './canonical.js';
8
+ export * from './cbor.js';
9
+ export * from './eventlog.js';
10
+ export * from './trailer.js';
11
+ export * from './manifest.js';
12
+ export * from './devid.js';
13
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC"}
@@ -0,0 +1,42 @@
1
+ /**
2
+ * `.invictus/manifest.json` — read / validate / write (ARCHITECTURE §3),
3
+ * plus the sha256 helpers used by the fragment file-hash entries that key
4
+ * the three-way merge on `invi update`.
5
+ */
6
+ import type { Manifest } from './types.js';
7
+ export declare const MANIFEST_RELATIVE_PATH = ".invictus/manifest.json";
8
+ /** Absolute path of the manifest inside a client repo root. */
9
+ export declare function manifestPath(rootDir: string): string;
10
+ /** Read and validate the manifest. Throws if missing or invalid. */
11
+ export declare function readManifest(rootDir: string): Manifest;
12
+ /** Validate and write the manifest (pretty-printed, trailing newline). */
13
+ export declare function writeManifest(rootDir: string, manifest: Manifest): void;
14
+ /** sha256 hex of a file's bytes. */
15
+ export declare function sha256File(filePath: string): string;
16
+ /**
17
+ * Installed filename of a fragment (§2 layout): the `category/slug` id maps
18
+ * to `<category>-<slug>.md` inside `.invictus/`, e.g.
19
+ * `stack/nextjs-app-router` → `stack-nextjs-app-router.md`.
20
+ */
21
+ export declare function fragmentFilename(fragmentId: string): string;
22
+ /** Absolute path of an installed fragment file. */
23
+ export declare function fragmentPath(rootDir: string, fragmentId: string): string;
24
+ export interface FragmentHashStatus {
25
+ id: string;
26
+ /** Absolute path checked. */
27
+ path: string;
28
+ /** true when the file exists and its sha256 matches the manifest entry. */
29
+ ok: boolean;
30
+ /** sha256 recorded in the manifest. */
31
+ expected: string;
32
+ /** sha256 of the file on disk, or null when the file is missing/unreadable. */
33
+ actual: string | null;
34
+ }
35
+ /**
36
+ * Compare every manifest fragment entry against the installed file on disk.
37
+ * `ok: false` with `actual: null` means the file is missing; with a differing
38
+ * `actual` it means the client edited the fragment (→ merge with conflict
39
+ * sidecar on update, per §3).
40
+ */
41
+ export declare function verifyFragmentHashes(rootDir: string, manifest: Manifest): FragmentHashStatus[];
42
+ //# sourceMappingURL=manifest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"manifest.d.ts","sourceRoot":"","sources":["../src/manifest.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAMH,OAAO,KAAK,EAAE,QAAQ,EAAoB,MAAM,YAAY,CAAC;AAE7D,eAAO,MAAM,sBAAsB,4BAA4B,CAAC;AAEhE,+DAA+D;AAC/D,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAEpD;AAED,oEAAoE;AACpE,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,QAAQ,CAiBtD;AAED,0EAA0E;AAC1E,wBAAgB,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,GAAG,IAAI,CAKvE;AAMD,oCAAoC;AACpC,wBAAgB,UAAU,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAEnD;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAE3D;AAED,mDAAmD;AACnD,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAExE;AAED,MAAM,WAAW,kBAAkB;IACjC,EAAE,EAAE,MAAM,CAAC;IACX,6BAA6B;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,2EAA2E;IAC3E,EAAE,EAAE,OAAO,CAAC;IACZ,uCAAuC;IACvC,QAAQ,EAAE,MAAM,CAAC;IACjB,+EAA+E;IAC/E,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;CACvB;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,GAAG,kBAAkB,EAAE,CAiB9F"}
@@ -0,0 +1,85 @@
1
+ /**
2
+ * `.invictus/manifest.json` — read / validate / write (ARCHITECTURE §3),
3
+ * plus the sha256 helpers used by the fragment file-hash entries that key
4
+ * the three-way merge on `invi update`.
5
+ */
6
+ import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
7
+ import { dirname, join } from 'node:path';
8
+ import { sha256Hex } from './canonical.js';
9
+ import { parseManifest } from './schemas.js';
10
+ export const MANIFEST_RELATIVE_PATH = '.invictus/manifest.json';
11
+ /** Absolute path of the manifest inside a client repo root. */
12
+ export function manifestPath(rootDir) {
13
+ return join(rootDir, '.invictus', 'manifest.json');
14
+ }
15
+ /** Read and validate the manifest. Throws if missing or invalid. */
16
+ export function readManifest(rootDir) {
17
+ const file = manifestPath(rootDir);
18
+ let raw;
19
+ try {
20
+ raw = readFileSync(file, 'utf8');
21
+ }
22
+ catch (err) {
23
+ throw new Error(`cannot read ${MANIFEST_RELATIVE_PATH} under ${rootDir}: ${err instanceof Error ? err.message : String(err)}`);
24
+ }
25
+ let parsed;
26
+ try {
27
+ parsed = JSON.parse(raw);
28
+ }
29
+ catch {
30
+ throw new Error(`${file} is not valid JSON`);
31
+ }
32
+ return parseManifest(parsed);
33
+ }
34
+ /** Validate and write the manifest (pretty-printed, trailing newline). */
35
+ export function writeManifest(rootDir, manifest) {
36
+ const validated = parseManifest(manifest);
37
+ const file = manifestPath(rootDir);
38
+ mkdirSync(dirname(file), { recursive: true });
39
+ writeFileSync(file, JSON.stringify(validated, null, 2) + '\n', 'utf8');
40
+ }
41
+ // ---------------------------------------------------------------------------
42
+ // sha256 helpers for fragment file-hash entries
43
+ // ---------------------------------------------------------------------------
44
+ /** sha256 hex of a file's bytes. */
45
+ export function sha256File(filePath) {
46
+ return sha256Hex(readFileSync(filePath));
47
+ }
48
+ /**
49
+ * Installed filename of a fragment (§2 layout): the `category/slug` id maps
50
+ * to `<category>-<slug>.md` inside `.invictus/`, e.g.
51
+ * `stack/nextjs-app-router` → `stack-nextjs-app-router.md`.
52
+ */
53
+ export function fragmentFilename(fragmentId) {
54
+ return `${fragmentId.replace(/\//g, '-')}.md`;
55
+ }
56
+ /** Absolute path of an installed fragment file. */
57
+ export function fragmentPath(rootDir, fragmentId) {
58
+ return join(rootDir, '.invictus', fragmentFilename(fragmentId));
59
+ }
60
+ /**
61
+ * Compare every manifest fragment entry against the installed file on disk.
62
+ * `ok: false` with `actual: null` means the file is missing; with a differing
63
+ * `actual` it means the client edited the fragment (→ merge with conflict
64
+ * sidecar on update, per §3).
65
+ */
66
+ export function verifyFragmentHashes(rootDir, manifest) {
67
+ return manifest.fragments.map((fragment) => {
68
+ const path = fragmentPath(rootDir, fragment.id);
69
+ let actual = null;
70
+ try {
71
+ actual = sha256File(path);
72
+ }
73
+ catch {
74
+ actual = null;
75
+ }
76
+ return {
77
+ id: fragment.id,
78
+ path,
79
+ ok: actual !== null && actual === fragment.sha256,
80
+ expected: fragment.sha256,
81
+ actual,
82
+ };
83
+ });
84
+ }
85
+ //# sourceMappingURL=manifest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"manifest.js","sourceRoot":"","sources":["../src/manifest.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACjE,OAAO,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