hippo-memory 1.2.1 → 1.3.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 (97) hide show
  1. package/README.md +9 -0
  2. package/dist/cli.js +10 -0
  3. package/dist/cli.js.map +1 -1
  4. package/dist/connectors/github/backfill.d.ts +48 -0
  5. package/dist/connectors/github/backfill.d.ts.map +1 -0
  6. package/dist/connectors/github/backfill.js +257 -0
  7. package/dist/connectors/github/backfill.js.map +1 -0
  8. package/dist/connectors/github/cli-impl.d.ts +24 -0
  9. package/dist/connectors/github/cli-impl.d.ts.map +1 -0
  10. package/dist/connectors/github/cli-impl.js +152 -0
  11. package/dist/connectors/github/cli-impl.js.map +1 -0
  12. package/dist/connectors/github/deletion.d.ts +38 -0
  13. package/dist/connectors/github/deletion.d.ts.map +1 -0
  14. package/dist/connectors/github/deletion.js +78 -0
  15. package/dist/connectors/github/deletion.js.map +1 -0
  16. package/dist/connectors/github/dlq.d.ts +101 -0
  17. package/dist/connectors/github/dlq.d.ts.map +1 -0
  18. package/dist/connectors/github/dlq.js +181 -0
  19. package/dist/connectors/github/dlq.js.map +1 -0
  20. package/dist/connectors/github/idempotency.d.ts +19 -0
  21. package/dist/connectors/github/idempotency.d.ts.map +1 -0
  22. package/dist/connectors/github/idempotency.js +25 -0
  23. package/dist/connectors/github/idempotency.js.map +1 -0
  24. package/dist/connectors/github/ingest.d.ts +67 -0
  25. package/dist/connectors/github/ingest.d.ts.map +1 -0
  26. package/dist/connectors/github/ingest.js +107 -0
  27. package/dist/connectors/github/ingest.js.map +1 -0
  28. package/dist/connectors/github/octokit-client.d.ts +36 -0
  29. package/dist/connectors/github/octokit-client.d.ts.map +1 -0
  30. package/dist/connectors/github/octokit-client.js +65 -0
  31. package/dist/connectors/github/octokit-client.js.map +1 -0
  32. package/dist/connectors/github/ratelimit.d.ts +20 -0
  33. package/dist/connectors/github/ratelimit.d.ts.map +1 -0
  34. package/dist/connectors/github/ratelimit.js +31 -0
  35. package/dist/connectors/github/ratelimit.js.map +1 -0
  36. package/dist/connectors/github/scope.d.ts +8 -0
  37. package/dist/connectors/github/scope.d.ts.map +1 -0
  38. package/dist/connectors/github/scope.js +13 -0
  39. package/dist/connectors/github/scope.js.map +1 -0
  40. package/dist/connectors/github/signature.d.ts +24 -0
  41. package/dist/connectors/github/signature.d.ts.map +1 -0
  42. package/dist/connectors/github/signature.js +35 -0
  43. package/dist/connectors/github/signature.js.map +1 -0
  44. package/dist/connectors/github/tenant-routing.d.ts +33 -0
  45. package/dist/connectors/github/tenant-routing.d.ts.map +1 -0
  46. package/dist/connectors/github/tenant-routing.js +61 -0
  47. package/dist/connectors/github/tenant-routing.js.map +1 -0
  48. package/dist/connectors/github/transform.d.ts +7 -0
  49. package/dist/connectors/github/transform.d.ts.map +1 -0
  50. package/dist/connectors/github/transform.js +103 -0
  51. package/dist/connectors/github/transform.js.map +1 -0
  52. package/dist/connectors/github/types.d.ts +87 -0
  53. package/dist/connectors/github/types.d.ts.map +1 -0
  54. package/dist/connectors/github/types.js +94 -0
  55. package/dist/connectors/github/types.js.map +1 -0
  56. package/dist/db.d.ts.map +1 -1
  57. package/dist/db.js +72 -1
  58. package/dist/db.js.map +1 -1
  59. package/dist/server.d.ts.map +1 -1
  60. package/dist/server.js +310 -1
  61. package/dist/server.js.map +1 -1
  62. package/dist/src/cli.js +10 -0
  63. package/dist/src/cli.js.map +1 -1
  64. package/dist/src/connectors/github/backfill.js +257 -0
  65. package/dist/src/connectors/github/backfill.js.map +1 -0
  66. package/dist/src/connectors/github/cli-impl.js +152 -0
  67. package/dist/src/connectors/github/cli-impl.js.map +1 -0
  68. package/dist/src/connectors/github/deletion.js +78 -0
  69. package/dist/src/connectors/github/deletion.js.map +1 -0
  70. package/dist/src/connectors/github/dlq.js +181 -0
  71. package/dist/src/connectors/github/dlq.js.map +1 -0
  72. package/dist/src/connectors/github/idempotency.js +25 -0
  73. package/dist/src/connectors/github/idempotency.js.map +1 -0
  74. package/dist/src/connectors/github/ingest.js +107 -0
  75. package/dist/src/connectors/github/ingest.js.map +1 -0
  76. package/dist/src/connectors/github/octokit-client.js +65 -0
  77. package/dist/src/connectors/github/octokit-client.js.map +1 -0
  78. package/dist/src/connectors/github/ratelimit.js +31 -0
  79. package/dist/src/connectors/github/ratelimit.js.map +1 -0
  80. package/dist/src/connectors/github/scope.js +13 -0
  81. package/dist/src/connectors/github/scope.js.map +1 -0
  82. package/dist/src/connectors/github/signature.js +35 -0
  83. package/dist/src/connectors/github/signature.js.map +1 -0
  84. package/dist/src/connectors/github/tenant-routing.js +61 -0
  85. package/dist/src/connectors/github/tenant-routing.js.map +1 -0
  86. package/dist/src/connectors/github/transform.js +103 -0
  87. package/dist/src/connectors/github/transform.js.map +1 -0
  88. package/dist/src/connectors/github/types.js +94 -0
  89. package/dist/src/connectors/github/types.js.map +1 -0
  90. package/dist/src/db.js +72 -1
  91. package/dist/src/db.js.map +1 -1
  92. package/dist/src/server.js +310 -1
  93. package/dist/src/server.js.map +1 -1
  94. package/extensions/openclaw-plugin/openclaw.plugin.json +1 -1
  95. package/extensions/openclaw-plugin/package.json +1 -1
  96. package/openclaw.plugin.json +1 -1
  97. package/package.json +1 -1
@@ -0,0 +1,101 @@
1
+ import type { DatabaseSyncLike } from '../../db.js';
2
+ import type { Context } from '../../api.js';
3
+ /**
4
+ * GitHub webhook DLQ. Mirrors the Slack DLQ shape (src/connectors/slack/dlq.ts)
5
+ * but carries GitHub-specific metadata: event_name, delivery_id, signature,
6
+ * installation_id, repo_full_name. Codex P1 #5 mandates this rich context
7
+ * so a `hippo gh dlq replay` operator can triage without re-deriving anything
8
+ * from the raw payload.
9
+ *
10
+ * Buckets:
11
+ * - parse_error — raw_payload was not valid JSON
12
+ * - unroutable — no tenant resolved for installation_id / repo_full_name
13
+ * - signature_failed — HMAC did not verify against the active webhook secret
14
+ * - unhandled — parsed but no handler matched the event
15
+ */
16
+ export type DlqBucket = 'parse_error' | 'unroutable' | 'signature_failed' | 'unhandled';
17
+ export interface DlqItem {
18
+ id: number;
19
+ tenantId: string;
20
+ rawPayload: string;
21
+ error: string;
22
+ eventName: string | null;
23
+ deliveryId: string | null;
24
+ signature: string | null;
25
+ installationId: string | null;
26
+ repoFullName: string | null;
27
+ retryCount: number;
28
+ receivedAt: string;
29
+ retriedAt: string | null;
30
+ bucket: DlqBucket | string;
31
+ }
32
+ /**
33
+ * `tenantId: null` means the connector could not resolve a tenant for the
34
+ * envelope (unroutable installation/repo). Stored as the sentinel
35
+ * `'__unroutable__'` so the NOT NULL column is honored — same convention as
36
+ * Slack DLQ.
37
+ */
38
+ export interface WriteDlqOpts {
39
+ tenantId: string | null;
40
+ rawPayload: string;
41
+ error: string;
42
+ bucket?: DlqBucket;
43
+ eventName?: string | null;
44
+ deliveryId?: string | null;
45
+ signature?: string | null;
46
+ installationId?: string | null;
47
+ repoFullName?: string | null;
48
+ }
49
+ export declare function writeToDlq(db: DatabaseSyncLike, opts: WriteDlqOpts): number;
50
+ export declare function listDlq(db: DatabaseSyncLike, opts: {
51
+ tenantId: string;
52
+ limit?: number;
53
+ }): DlqItem[];
54
+ export declare function getDlqEntry(db: DatabaseSyncLike, id: number): DlqItem | null;
55
+ export interface ReplayDlqOpts {
56
+ /** Current webhook secret. If omitted, signature check is skipped (force-only path). */
57
+ webhookSecret?: string;
58
+ /** When true, skip signature verification (used for legacy entries after secret rotation). */
59
+ force?: boolean;
60
+ }
61
+ export type ReplayStatus = 'replayed' | 'parse_error' | 'sig_fail' | 'sig_missing' | 'unhandled' | 'not_found';
62
+ export interface ReplayResult {
63
+ ok: boolean;
64
+ status: ReplayStatus;
65
+ memoryId: string | null;
66
+ retryCount: number;
67
+ reason?: string;
68
+ }
69
+ /**
70
+ * Hook the webhook route injects to actually re-ingest a row. Decoupling
71
+ * the dispatch keeps this module free of every event-type handler — the
72
+ * route already knows how to route an envelope, so it passes that capability
73
+ * back in.
74
+ */
75
+ export type IngestHook = (ctx: Context, args: {
76
+ rawPayload: string;
77
+ eventName: string;
78
+ idempotencyKey: string;
79
+ deliveryId: string;
80
+ }) => Promise<{
81
+ memoryId: string | null;
82
+ }>;
83
+ /**
84
+ * Replay a DLQ row through the normal ingest path. Behavior:
85
+ * 1. Fetch row by id. Not found → `not_found`.
86
+ * 2. If !force and webhookSecret provided, verify signature with the
87
+ * current secret. Fail → bump retry_count, `sig_fail`.
88
+ * Missing signature on the row → `sig_missing` (no bump; --force required).
89
+ * 3. JSON.parse the raw payload. Fail → bump, `parse_error`.
90
+ * 4. Type-guard the envelope. Fail → bump, `unhandled`.
91
+ * 5. If an `ingestHook` is supplied, call it and return its memoryId.
92
+ * If not (dry-run path), bump retry_count and return status `replayed`
93
+ * with memoryId=null. The webhook route wires the real hook in Task 14.
94
+ *
95
+ * Mirrors Slack's "always use current routing" policy: replays use the
96
+ * deployment state NOW, not at the time of original DLQing.
97
+ */
98
+ export declare function replayDlqEntry(ctx: Context, id: number, opts?: ReplayDlqOpts & {
99
+ ingestHook?: IngestHook;
100
+ }): Promise<ReplayResult>;
101
+ //# sourceMappingURL=dlq.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dlq.d.ts","sourceRoot":"","sources":["../../../src/connectors/github/dlq.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAK5C;;;;;;;;;;;;GAYG;AACH,MAAM,MAAM,SAAS,GAAG,aAAa,GAAG,YAAY,GAAG,kBAAkB,GAAG,WAAW,CAAC;AAExF,MAAM,WAAW,OAAO;IACtB,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,SAAS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a,GACb,UAAU,GACV,aAAa,GACb,WAAW,GACX,WAAW,CAAC;AAEhB,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,OAAO,CAAC;IACZ,MAAM,EAAE,YAAY,CAAC;IACrB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;;;;GAKG;AACH,MAAM,MAAM,UAAU,GAAG,CACvB,GAAG,EAAE,OAAO,EACZ,IAAI,EAAE;IACJ,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,cAAc,EAAE,MAAM,CAAC;IACvB,UAAU,EAAE,MAAM,CAAC;CACpB,KACE,OAAO,CAAC;IAAE,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,CAAC,CAAC;AAE1C;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,OAAO,EACZ,EAAE,EAAE,MAAM,EACV,IAAI,GAAE,aAAa,GAAG;IAAE,UAAU,CAAC,EAAE,UAAU,CAAA;CAAO,GACrD,OAAO,CAAC,YAAY,CAAC,CA0GvB"}
@@ -0,0 +1,181 @@
1
+ import { openHippoDb, closeHippoDb } from '../../db.js';
2
+ import { verifyGitHubSignature } from './signature.js';
3
+ import { isGitHubWebhookEnvelope } from './types.js';
4
+ export function writeToDlq(db, opts) {
5
+ const result = db
6
+ .prepare(`INSERT INTO github_dlq
7
+ (tenant_id, raw_payload, error, event_name, delivery_id, signature,
8
+ installation_id, repo_full_name, received_at, bucket)
9
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
10
+ .run(opts.tenantId ?? '__unroutable__', opts.rawPayload, opts.error, opts.eventName ?? null, opts.deliveryId ?? null, opts.signature ?? null, opts.installationId ?? null, opts.repoFullName ?? null, new Date().toISOString(), opts.bucket ?? 'parse_error');
11
+ return Number(result.lastInsertRowid);
12
+ }
13
+ const SELECT_COLUMNS = `id, tenant_id, raw_payload, error, event_name, delivery_id,
14
+ signature, installation_id, repo_full_name, retry_count,
15
+ received_at, retried_at, bucket`;
16
+ export function listDlq(db, opts) {
17
+ const rows = db
18
+ .prepare(`SELECT ${SELECT_COLUMNS}
19
+ FROM github_dlq
20
+ WHERE tenant_id = ?
21
+ ORDER BY received_at ASC
22
+ LIMIT ?`)
23
+ .all(opts.tenantId, opts.limit ?? 100);
24
+ return rows.map(rowToItem);
25
+ }
26
+ export function getDlqEntry(db, id) {
27
+ const row = db
28
+ .prepare(`SELECT ${SELECT_COLUMNS}
29
+ FROM github_dlq
30
+ WHERE id = ?`)
31
+ .get(id);
32
+ if (!row)
33
+ return null;
34
+ return rowToItem(row);
35
+ }
36
+ function rowToItem(r) {
37
+ return {
38
+ id: Number(r.id),
39
+ tenantId: String(r.tenant_id),
40
+ rawPayload: String(r.raw_payload),
41
+ error: String(r.error),
42
+ eventName: r.event_name == null ? null : String(r.event_name),
43
+ deliveryId: r.delivery_id == null ? null : String(r.delivery_id),
44
+ signature: r.signature == null ? null : String(r.signature),
45
+ installationId: r.installation_id == null ? null : String(r.installation_id),
46
+ repoFullName: r.repo_full_name == null ? null : String(r.repo_full_name),
47
+ retryCount: Number(r.retry_count ?? 0),
48
+ receivedAt: String(r.received_at),
49
+ retriedAt: r.retried_at == null ? null : String(r.retried_at),
50
+ bucket: r.bucket == null ? 'parse_error' : String(r.bucket),
51
+ };
52
+ }
53
+ function bumpRetryCount(hippoRoot, id) {
54
+ const db = openHippoDb(hippoRoot);
55
+ try {
56
+ db.prepare(`UPDATE github_dlq
57
+ SET retry_count = retry_count + 1,
58
+ retried_at = ?
59
+ WHERE id = ?`).run(new Date().toISOString(), id);
60
+ }
61
+ finally {
62
+ closeHippoDb(db);
63
+ }
64
+ }
65
+ /**
66
+ * Replay a DLQ row through the normal ingest path. Behavior:
67
+ * 1. Fetch row by id. Not found → `not_found`.
68
+ * 2. If !force and webhookSecret provided, verify signature with the
69
+ * current secret. Fail → bump retry_count, `sig_fail`.
70
+ * Missing signature on the row → `sig_missing` (no bump; --force required).
71
+ * 3. JSON.parse the raw payload. Fail → bump, `parse_error`.
72
+ * 4. Type-guard the envelope. Fail → bump, `unhandled`.
73
+ * 5. If an `ingestHook` is supplied, call it and return its memoryId.
74
+ * If not (dry-run path), bump retry_count and return status `replayed`
75
+ * with memoryId=null. The webhook route wires the real hook in Task 14.
76
+ *
77
+ * Mirrors Slack's "always use current routing" policy: replays use the
78
+ * deployment state NOW, not at the time of original DLQing.
79
+ */
80
+ export async function replayDlqEntry(ctx, id, opts = {}) {
81
+ const db = openHippoDb(ctx.hippoRoot);
82
+ let row;
83
+ try {
84
+ row = getDlqEntry(db, id);
85
+ }
86
+ finally {
87
+ closeHippoDb(db);
88
+ }
89
+ if (!row) {
90
+ return {
91
+ ok: false,
92
+ status: 'not_found',
93
+ memoryId: null,
94
+ retryCount: 0,
95
+ reason: `dlq id ${id} not found`,
96
+ };
97
+ }
98
+ // Signature verification (current secret, not the one in effect when DLQed).
99
+ if (!opts.force && opts.webhookSecret) {
100
+ if (!row.signature) {
101
+ return {
102
+ ok: false,
103
+ status: 'sig_missing',
104
+ memoryId: null,
105
+ retryCount: row.retryCount,
106
+ reason: 'legacy row without signature; pass --force to replay',
107
+ };
108
+ }
109
+ const sigOk = verifyGitHubSignature({
110
+ rawBody: row.rawPayload,
111
+ signature: row.signature,
112
+ webhookSecret: opts.webhookSecret,
113
+ });
114
+ if (!sigOk) {
115
+ bumpRetryCount(ctx.hippoRoot, id);
116
+ return {
117
+ ok: false,
118
+ status: 'sig_fail',
119
+ memoryId: null,
120
+ retryCount: row.retryCount + 1,
121
+ reason: 'signature did not verify against current GITHUB_WEBHOOK_SECRET; pass --force to replay anyway',
122
+ };
123
+ }
124
+ }
125
+ // Parse + envelope guard.
126
+ let parsed;
127
+ try {
128
+ parsed = JSON.parse(row.rawPayload);
129
+ }
130
+ catch (e) {
131
+ bumpRetryCount(ctx.hippoRoot, id);
132
+ return {
133
+ ok: false,
134
+ status: 'parse_error',
135
+ memoryId: null,
136
+ retryCount: row.retryCount + 1,
137
+ reason: `still unparseable: ${e.message}`,
138
+ };
139
+ }
140
+ if (!isGitHubWebhookEnvelope(parsed)) {
141
+ bumpRetryCount(ctx.hippoRoot, id);
142
+ return {
143
+ ok: false,
144
+ status: 'unhandled',
145
+ memoryId: null,
146
+ retryCount: row.retryCount + 1,
147
+ reason: 'not a GitHub webhook envelope',
148
+ };
149
+ }
150
+ // Without an ingest hook this is a dry-run validation. Bump and report.
151
+ if (!opts.ingestHook) {
152
+ bumpRetryCount(ctx.hippoRoot, id);
153
+ return {
154
+ ok: true,
155
+ status: 'replayed',
156
+ memoryId: null,
157
+ retryCount: row.retryCount + 1,
158
+ reason: 'dry-run: no ingest hook supplied',
159
+ };
160
+ }
161
+ // Real replay path. The route's IngestHook is responsible for routing,
162
+ // idempotency, and writing the memory. The DLQ module only validates the
163
+ // surface and bumps the retry counter.
164
+ const eventName = row.eventName ?? '';
165
+ const deliveryId = row.deliveryId ?? '';
166
+ const idempotencyKey = (await import('./signature.js')).computeIdempotencyKey(eventName, row.rawPayload);
167
+ const { memoryId } = await opts.ingestHook(ctx, {
168
+ rawPayload: row.rawPayload,
169
+ eventName,
170
+ idempotencyKey,
171
+ deliveryId,
172
+ });
173
+ bumpRetryCount(ctx.hippoRoot, id);
174
+ return {
175
+ ok: true,
176
+ status: 'replayed',
177
+ memoryId,
178
+ retryCount: row.retryCount + 1,
179
+ };
180
+ }
181
+ //# sourceMappingURL=dlq.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dlq.js","sourceRoot":"","sources":["../../../src/connectors/github/dlq.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AACvD,OAAO,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAC;AAm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AAE,IAAI;YACR,MAAM,EAAE,UAAU;YAClB,QAAQ,EAAE,IAAI;YACd,UAAU,EAAE,GAAG,CAAC,UAAU,GAAG,CAAC;YAC9B,MAAM,EAAE,kCAAkC;SAC3C,CAAC;IACJ,CAAC;IAED,uEAAuE;IACvE,yEAAyE;IACzE,uCAAuC;IACvC,MAAM,SAAS,GAAG,GAAG,CAAC,SAAS,IAAI,EAAE,CAAC;IACtC,MAAM,UAAU,GAAG,GAAG,CAAC,UAAU,IAAI,EAAE,CAAC;IACxC,MAAM,cAAc,GAAG,CAAC,MAAM,MAAM,CAAC,gBAAgB,CAAC,CAAC,CAAC,qBAAqB,CAC3E,SAAS,EACT,GAAG,CAAC,UAAU,CACf,CAAC;IACF,MAAM,EAAE,QAAQ,EAAE,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,GAAG,EAAE;QAC9C,UAAU,EAAE,GAAG,CAAC,UAAU;QAC1B,SAAS;QACT,cAAc;QACd,UAAU;KACX,CAAC,CAAC;IACH,cAAc,CAAC,GAAG,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;IAClC,OAAO;QACL,EAAE,EAAE,IAAI;QACR,MAAM,EAAE,UAAU;QAClB,QAAQ;QACR,UAAU,EAAE,GAAG,CAAC,UAAU,GAAG,CAAC;KAC/B,CAAC;AACJ,CAAC"}
@@ -0,0 +1,19 @@
1
+ import type { DatabaseSyncLike } from '../../db.js';
2
+ /**
3
+ * Thrown by ingest's afterWrite hook when a concurrent worker has already
4
+ * inserted github_event_log for the same idempotency_key. Roll back the
5
+ * SAVEPOINT so exactly one memory row exists per key.
6
+ */
7
+ export declare class DuplicateIdempotencyError extends Error {
8
+ readonly idempotencyKey: string;
9
+ constructor(idempotencyKey: string);
10
+ }
11
+ export declare function hasSeenKey(db: DatabaseSyncLike, idempotencyKey: string): boolean;
12
+ export declare function markKeySeen(db: DatabaseSyncLike, args: {
13
+ idempotencyKey: string;
14
+ deliveryId: string;
15
+ eventName: string;
16
+ memoryId: string | null;
17
+ }): void;
18
+ export declare function lookupMemoryByKey(db: DatabaseSyncLike, idempotencyKey: string): string | null;
19
+ //# sourceMappingURL=idempotency.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"idempotency.d.ts","sourceRoot":"","sources":["../../../src/connectors/github/idempotency.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEpD;;;;GAIG;AACH,qBAAa,yBAA0B,SAAQ,KAAK;IAClD,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;gBACpB,cAAc,EAAE,MAAM;CAKnC;AAED,wBAAgB,UAAU,CAAC,EAAE,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,GAAG,OAAO,CAGhF;AAED,wBAAgB,WAAW,CACzB,EAAE,EAAE,gBAAgB,EACpB,IAAI,EAAE;IAAE,cAAc,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,GAC/F,IAAI,CAIN;AAED,wBAAgB,iBAAiB,CAAC,EAAE,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAK7F"}
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Thrown by ingest's afterWrite hook when a concurrent worker has already
3
+ * inserted github_event_log for the same idempotency_key. Roll back the
4
+ * SAVEPOINT so exactly one memory row exists per key.
5
+ */
6
+ export class DuplicateIdempotencyError extends Error {
7
+ idempotencyKey;
8
+ constructor(idempotencyKey) {
9
+ super(`duplicate github idempotency_key: ${idempotencyKey}`);
10
+ this.name = 'DuplicateIdempotencyError';
11
+ this.idempotencyKey = idempotencyKey;
12
+ }
13
+ }
14
+ export function hasSeenKey(db, idempotencyKey) {
15
+ const row = db.prepare(`SELECT 1 FROM github_event_log WHERE idempotency_key = ?`).get(idempotencyKey);
16
+ return !!row;
17
+ }
18
+ export function markKeySeen(db, args) {
19
+ db.prepare(`INSERT OR IGNORE INTO github_event_log (idempotency_key, delivery_id, event_name, ingested_at, memory_id) VALUES (?, ?, ?, ?, ?)`).run(args.idempotencyKey, args.deliveryId, args.eventName, new Date().toISOString(), args.memoryId);
20
+ }
21
+ export function lookupMemoryByKey(db, idempotencyKey) {
22
+ const row = db.prepare(`SELECT memory_id FROM github_event_log WHERE idempotency_key = ?`).get(idempotencyKey);
23
+ return row?.memory_id ?? null;
24
+ }
25
+ //# sourceMappingURL=idempotency.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"idempotency.js","sourceRoot":"","sources":["../../../src/connectors/github/idempotency.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,OAAO,yBAA0B,SAAQ,KAAK;IACzC,cAAc,CAAS;IAChC,YAAY,cAAsB;QAChC,KAAK,CAAC,qCAAqC,cAAc,EAAE,CAAC,CAAC;QAC7D,IAAI,CAAC,IAAI,GAAG,2BAA2B,CAAC;QACxC,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;IACvC,CAAC;CACF;AAED,MAAM,UAAU,UAAU,CAAC,EAAoB,EAAE,cAAsB;IACrE,MAAM,GAAG,GAAG,EAAE,CAAC,OAAO,CAAC,0DAA0D,CAAC,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;IACvG,OAAO,CAAC,CAAC,GAAG,CAAC;AACf,CAAC;AAED,MAAM,UAAU,WAAW,CACzB,EAAoB,EACpB,IAAgG;IAEhG,EAAE,CAAC,OAAO,CACR,kIAAkI,CACnI,CAAC,GAAG,CAAC,IAAI,CAAC,cAAc,EAAE,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;AACvG,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,EAAoB,EAAE,cAAsB;IAC5E,MAAM,GAAG,GAAG,EAAE,CAAC,OAAO,CAAC,kEAAkE,CAAC,CAAC,GAAG,CAAC,cAAc,CAEhG,CAAC;IACd,OAAO,GAAG,EAAE,SAAS,IAAI,IAAI,CAAC;AAChC,CAAC"}
@@ -0,0 +1,67 @@
1
+ /**
2
+ * GitHub event ingest with afterWrite race-safe idempotency.
3
+ *
4
+ * Mirrors src/connectors/slack/ingest.ts. The dedupe key is sha256(eventName +
5
+ * ':' + rawBody) (codex P0 #3) — derived from the signed body, not from the
6
+ * unsigned X-GitHub-Delivery header, so a replay attacker cannot bypass
7
+ * idempotency by rotating the delivery UUID.
8
+ *
9
+ * Race semantics (codex P1 #6):
10
+ * - Fast path: hasSeenKey pre-check returns 'duplicate' for the common case.
11
+ * - Slow path: hasSeenKey passes (no row yet). Two workers may race into
12
+ * remember() concurrently. Inside the writeEntry SAVEPOINT, INSERT OR
13
+ * IGNORE on github_event_log either inserts (changes=1, commit) or
14
+ * collides (changes=0, throw DuplicateIdempotencyError -> SAVEPOINT
15
+ * rolls back this worker's memory row). Exactly one memory exists per
16
+ * idempotency_key.
17
+ *
18
+ * The Slack precedent's race test was insufficient — it tested the fast path,
19
+ * not the SAVEPOINT collision. The `__testInjectBeforeLog` hook below lets
20
+ * tests pre-populate github_event_log inside the SAVEPOINT to actually
21
+ * exercise the changes=0 -> rollback path.
22
+ */
23
+ import { type Context } from '../../api.js';
24
+ import { type DatabaseSyncLike } from '../../db.js';
25
+ import type { GitHubIssueEvent, GitHubIssueCommentEvent, GitHubPullRequestEvent, GitHubPullRequestReviewCommentEvent } from './types.js';
26
+ export type IngestStatus = 'ingested' | 'duplicate' | 'skipped' | 'skipped_duplicate';
27
+ export interface IngestResult {
28
+ status: IngestStatus;
29
+ memoryId: string | null;
30
+ }
31
+ /**
32
+ * Discriminated union of the four event shapes V1 ingests. The eventName
33
+ * field MUST equal the X-GitHub-Event header value; computeIdempotencyKey
34
+ * folds it into the dedupe key so the same body posted under two different
35
+ * X-GitHub-Event headers produces two distinct keys (test 7).
36
+ */
37
+ export type IngestEvent = {
38
+ eventName: 'issues';
39
+ payload: GitHubIssueEvent;
40
+ } | {
41
+ eventName: 'issue_comment';
42
+ payload: GitHubIssueCommentEvent;
43
+ } | {
44
+ eventName: 'pull_request';
45
+ payload: GitHubPullRequestEvent;
46
+ } | {
47
+ eventName: 'pull_request_review_comment';
48
+ payload: GitHubPullRequestReviewCommentEvent;
49
+ };
50
+ export interface IngestInput {
51
+ /** The X-GitHub-Event header value + parsed body, discriminated. */
52
+ event: IngestEvent;
53
+ /** The raw HTTP body — used for the idempotency key (replay-safe per codex P0 #3). */
54
+ rawBody: string;
55
+ /** X-GitHub-Delivery header value, audit metadata only. */
56
+ deliveryId: string;
57
+ /**
58
+ * Test-only hook fired inside the SAVEPOINT, AFTER the memory row is
59
+ * inserted but BEFORE the github_event_log INSERT OR IGNORE. Tests pass
60
+ * a function that pre-inserts the github_event_log row using the
61
+ * provided db handle to simulate a concurrent worker winning the race.
62
+ * Production callers leave this undefined.
63
+ */
64
+ __testInjectBeforeLog?: (db: DatabaseSyncLike, idempotencyKey: string) => void;
65
+ }
66
+ export declare function ingestEvent(ctx: Context, input: IngestInput): IngestResult;
67
+ //# sourceMappingURL=ingest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ingest.d.ts","sourceRoot":"","sources":["../../../src/connectors/github/ingest.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAY,KAAK,OAAO,EAAqB,MAAM,cAAc,CAAC;AACzE,OAAO,EAA6B,KAAK,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAS/E,OAAO,KAAK,EACV,gBAAgB,EAChB,uBAAuB,EACvB,sBAAsB,EACtB,mCAAmC,EACpC,MAAM,YAAY,CAAC;AAEpB,MAAM,MAAM,YAAY,GAAG,UAAU,GAAG,WAAW,GAAG,SAAS,GAAG,mBAAmB,CAAC;AAEtF,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,YAAY,CAAC;IACrB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;CACzB;AAED;;;;;GAKG;AACH,MAAM,MAAM,WAAW,GACnB;IAAE,SAAS,EAAE,QAAQ,CAAC;IAAC,OAAO,EAAE,gBAAgB,CAAA;CAAE,GAClD;IAAE,SAAS,EAAE,eAAe,CAAC;IAAC,OAAO,EAAE,uBAAuB,CAAA;CAAE,GAChE;IAAE,SAAS,EAAE,cAAc,CAAC;IAAC,OAAO,EAAE,sBAAsB,CAAA;CAAE,GAC9D;IAAE,SAAS,EAAE,6BAA6B,CAAC;IAAC,OAAO,EAAE,mCAAmC,CAAA;CAAE,CAAC;AAE/F,MAAM,WAAW,WAAW;IAC1B,oEAAoE;IACpE,KAAK,EAAE,WAAW,CAAC;IACnB,sFAAsF;IACtF,OAAO,EAAE,MAAM,CAAC;IAChB,2DAA2D;IAC3D,UAAU,EAAE,MAAM,CAAC;IACnB;;;;;;OAMG;IACH,qBAAqB,CAAC,EAAE,CAAC,EAAE,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,KAAK,IAAI,CAAC;CAChF;AAeD,wBAAgB,WAAW,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,WAAW,GAAG,YAAY,CA+E1E"}
@@ -0,0 +1,107 @@
1
+ /**
2
+ * GitHub event ingest with afterWrite race-safe idempotency.
3
+ *
4
+ * Mirrors src/connectors/slack/ingest.ts. The dedupe key is sha256(eventName +
5
+ * ':' + rawBody) (codex P0 #3) — derived from the signed body, not from the
6
+ * unsigned X-GitHub-Delivery header, so a replay attacker cannot bypass
7
+ * idempotency by rotating the delivery UUID.
8
+ *
9
+ * Race semantics (codex P1 #6):
10
+ * - Fast path: hasSeenKey pre-check returns 'duplicate' for the common case.
11
+ * - Slow path: hasSeenKey passes (no row yet). Two workers may race into
12
+ * remember() concurrently. Inside the writeEntry SAVEPOINT, INSERT OR
13
+ * IGNORE on github_event_log either inserts (changes=1, commit) or
14
+ * collides (changes=0, throw DuplicateIdempotencyError -> SAVEPOINT
15
+ * rolls back this worker's memory row). Exactly one memory exists per
16
+ * idempotency_key.
17
+ *
18
+ * The Slack precedent's race test was insufficient — it tested the fast path,
19
+ * not the SAVEPOINT collision. The `__testInjectBeforeLog` hook below lets
20
+ * tests pre-populate github_event_log inside the SAVEPOINT to actually
21
+ * exercise the changes=0 -> rollback path.
22
+ */
23
+ import { remember } from '../../api.js';
24
+ import { openHippoDb, closeHippoDb } from '../../db.js';
25
+ import { hasSeenKey, lookupMemoryByKey, DuplicateIdempotencyError } from './idempotency.js';
26
+ import { computeIdempotencyKey } from './signature.js';
27
+ import { issueEventToRememberOpts, issueCommentEventToRememberOpts, pullRequestEventToRememberOpts, prReviewCommentEventToRememberOpts, } from './transform.js';
28
+ function transformEvent(event) {
29
+ switch (event.eventName) {
30
+ case 'issues':
31
+ return issueEventToRememberOpts(event.payload);
32
+ case 'issue_comment':
33
+ return issueCommentEventToRememberOpts(event.payload);
34
+ case 'pull_request':
35
+ return pullRequestEventToRememberOpts(event.payload);
36
+ case 'pull_request_review_comment':
37
+ return prReviewCommentEventToRememberOpts(event.payload);
38
+ }
39
+ }
40
+ export function ingestEvent(ctx, input) {
41
+ const idempotencyKey = computeIdempotencyKey(input.event.eventName, input.rawBody);
42
+ // Fast path: pre-check. Avoids running the transform / opening a write tx
43
+ // for the common already-seen case (GitHub auto-retries with the same body).
44
+ const db = openHippoDb(ctx.hippoRoot);
45
+ try {
46
+ if (hasSeenKey(db, idempotencyKey)) {
47
+ return { status: 'duplicate', memoryId: lookupMemoryByKey(db, idempotencyKey) };
48
+ }
49
+ }
50
+ finally {
51
+ closeHippoDb(db);
52
+ }
53
+ const opts = transformEvent(input.event);
54
+ if (!opts) {
55
+ // Empty body: no memory to write, but mark seen so a retry of the same
56
+ // empty event returns 'duplicate' (not 'skipped' again — that would
57
+ // re-run the transform on every retry).
58
+ const db2 = openHippoDb(ctx.hippoRoot);
59
+ try {
60
+ db2
61
+ .prepare(`INSERT OR IGNORE INTO github_event_log (idempotency_key, delivery_id, event_name, ingested_at, memory_id) VALUES (?, ?, ?, ?, ?)`)
62
+ .run(idempotencyKey, input.deliveryId, input.event.eventName, new Date().toISOString(), null);
63
+ }
64
+ finally {
65
+ closeHippoDb(db2);
66
+ }
67
+ return { status: 'skipped', memoryId: null };
68
+ }
69
+ // Atomic write via afterWrite. Inside writeEntry's SAVEPOINT:
70
+ // 1. memories row INSERT lands.
71
+ // 2. (test-only) __testInjectBeforeLog can race-inject a colliding key.
72
+ // 3. INSERT OR IGNORE on github_event_log; if a concurrent worker (or
73
+ // the test injection) beat us, changes=0 -> throw -> SAVEPOINT rolls
74
+ // back our memory row. Other worker's commit stands.
75
+ try {
76
+ const result = remember({ ...ctx, actor: ctx.actor || 'connector:github' }, {
77
+ ...opts,
78
+ afterWrite: (innerDb, memoryId) => {
79
+ if (input.__testInjectBeforeLog) {
80
+ input.__testInjectBeforeLog(innerDb, idempotencyKey);
81
+ }
82
+ const inserted = innerDb
83
+ .prepare(`INSERT OR IGNORE INTO github_event_log (idempotency_key, delivery_id, event_name, ingested_at, memory_id) VALUES (?, ?, ?, ?, ?)`)
84
+ .run(idempotencyKey, input.deliveryId, input.event.eventName, new Date().toISOString(), memoryId);
85
+ if (Number(inserted.changes ?? 0) === 0) {
86
+ throw new DuplicateIdempotencyError(idempotencyKey);
87
+ }
88
+ },
89
+ });
90
+ return { status: 'ingested', memoryId: result.id };
91
+ }
92
+ catch (e) {
93
+ if (e instanceof DuplicateIdempotencyError) {
94
+ // Other worker's row is committed. Return its memory_id so callers
95
+ // behave identically to the fast-path 'duplicate' branch.
96
+ const db3 = openHippoDb(ctx.hippoRoot);
97
+ try {
98
+ return { status: 'skipped_duplicate', memoryId: lookupMemoryByKey(db3, idempotencyKey) };
99
+ }
100
+ finally {
101
+ closeHippoDb(db3);
102
+ }
103
+ }
104
+ throw e;
105
+ }
106
+ }
107
+ //# sourceMappingURL=ingest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ing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EAChB,KAAK,CAAC,KAAK,CAAC,SAAS,EACrB,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,EACxB,QAAQ,CACT,CAAC;gBACJ,IAAI,MAAM,CAAC,QAAQ,CAAC,OAAO,IAAI,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC;oBACxC,MAAM,IAAI,yBAAyB,CAAC,cAAc,CAAC,CAAC;gBACtD,CAAC;YACH,CAAC;SACF,CACF,CAAC;QACF,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,CAAC,EAAE,EAAE,CAAC;IACrD,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,IAAI,CAAC,YAAY,yBAAyB,EAAE,CAAC;YAC3C,mEAAmE;YACnE,0DAA0D;YAC1D,MAAM,GAAG,GAAG,WAAW,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;YACvC,IAAI,CAAC;gBACH,OAAO,EAAE,MAAM,EAAE,mBAAmB,EAAE,QAAQ,EAAE,iBAAiB,CAAC,GAAG,EAAE,cAAc,CAAC,EAAE,CAAC;YAC3F,CAAC;oBAAS,CAAC;gBACT,YAAY,CAAC,GAAG,CAAC,CAAC;YACpB,CAAC;QACH,CAAC;QACD,MAAM,CAAC,CAAC;IACV,CAAC;AACH,CAAC"}
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Octokit-shaped HTTP fetcher for the GitHub connector backfill.
3
+ *
4
+ * Production uses `realGitHubFetcher` against `https://api.github.com`.
5
+ * Tests inject a fake `GitHubFetcher` so they never hit the network.
6
+ *
7
+ * Codex P1 #4 mandate: any non-200 response that is NOT a recognized
8
+ * rate-limit pause MUST throw `GitHubFetchError`. Silently turning
9
+ * 401/403/404/500 into empty pages produced empty backfills with no
10
+ * operator signal, so this code path is now load-bearing.
11
+ */
12
+ import { type RateLimitInfo } from './ratelimit.js';
13
+ export declare class GitHubFetchError extends Error {
14
+ readonly status: number;
15
+ readonly bodyExcerpt: string;
16
+ readonly url: string;
17
+ constructor(status: number, bodyExcerpt: string, url: string);
18
+ }
19
+ export interface GitHubBackfillPage {
20
+ readonly items: ReadonlyArray<unknown>;
21
+ readonly next: string | null;
22
+ readonly rateLimit: RateLimitInfo;
23
+ }
24
+ export type GitHubFetcher = (args: {
25
+ url: string;
26
+ token: string;
27
+ }) => Promise<GitHubBackfillPage>;
28
+ /**
29
+ * Parse the rel="next" URL from an RFC 5988 `Link` header.
30
+ *
31
+ * Header format: `<url1>; rel="next", <url2>; rel="last"`.
32
+ * Returns the URL whose rel parameter is exactly `"next"`, or null.
33
+ */
34
+ export declare function parseNextLink(linkHeader: string): string | null;
35
+ export declare const realGitHubFetcher: GitHubFetcher;
36
+ //# sourceMappingURL=octokit-client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"octokit-client.d.ts","sourceRoot":"","sources":["../../../src/connectors/github/octokit-client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAkB,KAAK,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAEpE,qBAAa,gBAAiB,SAAQ,KAAK;IAEvC,QAAQ,CAAC,MAAM,EAAE,MAAM;IACvB,QAAQ,CAAC,WAAW,EAAE,MAAM;IAC5B,QAAQ,CAAC,GAAG,EAAE,MAAM;gBAFX,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,MAAM,EACnB,GAAG,EAAE,MAAM;CAKvB;AAED,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,KAAK,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC;IACvC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,QAAQ,CAAC,SAAS,EAAE,aAAa,CAAC;CACnC;AAED,MAAM,MAAM,aAAa,GAAG,CAAC,IAAI,EAAE;IACjC,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;CACf,KAAK,OAAO,CAAC,kBAAkB,CAAC,CAAC;AAElC;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAK/D;AAUD,eAAO,MAAM,iBAAiB,EAAE,aAsB/B,CAAC"}
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Octokit-shaped HTTP fetcher for the GitHub connector backfill.
3
+ *
4
+ * Production uses `realGitHubFetcher` against `https://api.github.com`.
5
+ * Tests inject a fake `GitHubFetcher` so they never hit the network.
6
+ *
7
+ * Codex P1 #4 mandate: any non-200 response that is NOT a recognized
8
+ * rate-limit pause MUST throw `GitHubFetchError`. Silently turning
9
+ * 401/403/404/500 into empty pages produced empty backfills with no
10
+ * operator signal, so this code path is now load-bearing.
11
+ */
12
+ import { parseRateLimit } from './ratelimit.js';
13
+ export class GitHubFetchError extends Error {
14
+ status;
15
+ bodyExcerpt;
16
+ url;
17
+ constructor(status, bodyExcerpt, url) {
18
+ super(`GitHub ${status} on ${url}: ${bodyExcerpt}`);
19
+ this.status = status;
20
+ this.bodyExcerpt = bodyExcerpt;
21
+ this.url = url;
22
+ this.name = 'GitHubFetchError';
23
+ }
24
+ }
25
+ /**
26
+ * Parse the rel="next" URL from an RFC 5988 `Link` header.
27
+ *
28
+ * Header format: `<url1>; rel="next", <url2>; rel="last"`.
29
+ * Returns the URL whose rel parameter is exactly `"next"`, or null.
30
+ */
31
+ export function parseNextLink(linkHeader) {
32
+ if (!linkHeader)
33
+ return null;
34
+ const re = /<([^>]+)>\s*;\s*rel="next"/;
35
+ const m = linkHeader.match(re);
36
+ return m ? m[1] : null;
37
+ }
38
+ function headersToRecord(h) {
39
+ const out = {};
40
+ h.forEach((v, k) => {
41
+ out[k.toLowerCase()] = v;
42
+ });
43
+ return out;
44
+ }
45
+ export const realGitHubFetcher = async ({ url, token }) => {
46
+ const res = await fetch(url, {
47
+ headers: {
48
+ Authorization: `Bearer ${token}`,
49
+ Accept: 'application/vnd.github+json',
50
+ 'X-GitHub-Api-Version': '2022-11-28',
51
+ },
52
+ });
53
+ const headers = headersToRecord(res.headers);
54
+ const rateLimit = parseRateLimit(headers, res.status);
55
+ // Codex P1 #4: don't silently turn 401/403/404/500 into empty pages.
56
+ if (res.status !== 200 && rateLimit.reason === 'none') {
57
+ const body = await res.text().catch(() => '');
58
+ throw new GitHubFetchError(res.status, body.slice(0, 256), url);
59
+ }
60
+ const items = res.status === 200 ? (await res.json()) : [];
61
+ const link = res.headers.get('link') ?? '';
62
+ const next = parseNextLink(link);
63
+ return { items, next, rateLimit };
64
+ };
65
+ //# sourceMappingURL=octokit-client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"octokit-client.js","sourceRoot":"","sources":["../../../src/connectors/github/octokit-client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,cAAc,EAAsB,MAAM,gBAAgB,CAAC;AAEpE,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IAE9B;IACA;IACA;IAHX,YACW,MAAc,EACd,WAAmB,EACnB,GAAW;QAEpB,KAAK,CAAC,UAAU,MAAM,OAAO,GAAG,KAAK,WAAW,EAAE,CAAC,CAAC;QAJ3C,WAAM,GAAN,MAAM,CAAQ;QACd,gBAAW,GAAX,WAAW,CAAQ;QACnB,QAAG,GAAH,GAAG,CAAQ;QAGpB,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;IACjC,CAAC;CACF;AAaD;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAAC,UAAkB;IAC9C,IAAI,CAAC,UAAU;QAAE,OAAO,IAAI,CAAC;IAC7B,MAAM,EAAE,GAAG,4BAA4B,CAAC;IACxC,MAAM,CAAC,GAAG,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;IAC/B,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;AACzB,CAAC;AAED,SAAS,eAAe,CAAC,CAAU;IACjC,MAAM,GAAG,GAA2B,EAAE,CAAC;IACvC,CAAC,CAA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