typeclaw 0.48.9 → 0.48.10

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.
@@ -298,10 +298,58 @@ function parseMintableTarget(subcommand: RemoteSubcommand, args: readonly string
298
298
  if (subcommand === 'push') return parseMintablePush(args)
299
299
  if (subcommand === 'fetch') return parseMintableFetch(args)
300
300
  if (subcommand === 'pull') return parseSimpleRemote(args, false)
301
- if (subcommand === 'clone') return parseExplicitUrl(args, 2)
301
+ if (subcommand === 'clone') return parseCloneTarget(args)
302
302
  return parseExplicitUrl(args, Number.POSITIVE_INFINITY)
303
303
  }
304
304
 
305
+ // Value-less clone flags that cannot redirect the fetch to another repo/host, run a
306
+ // command, or read/write a path outside the destination. Deliberately EXCLUDES
307
+ // --recurse-submodules (fetches other repos with the token live), --upload-pack and
308
+ // -c/--config (command execution / url.insteadOf rewriting), and --template,
309
+ // --reference, --separate-git-dir (arbitrary path read/write).
310
+ const SAFE_CLONE_FLAGS = new Set([
311
+ '-q',
312
+ '--quiet',
313
+ '--progress',
314
+ '-n',
315
+ '--no-checkout',
316
+ '--single-branch',
317
+ '--no-single-branch',
318
+ '--no-tags',
319
+ '--bare',
320
+ '--sparse',
321
+ ])
322
+
323
+ // Clone flags that consume the NEXT argv word as their value.
324
+ const SAFE_CLONE_VALUE_FLAGS = new Set(['--depth', '-b', '--branch'])
325
+
326
+ // `--flag=value` forms whose value is inert (a count, ref, date, or filter spec).
327
+ const SAFE_CLONE_INLINE_PREFIXES = ['--depth=', '--branch=', '--filter=', '--shallow-since=', '--shallow-exclude=']
328
+
329
+ // git clone's URL is a POSITIONAL, so the previous fixed-index parse rejected every
330
+ // flagged clone — `--depth 1`, the most common shape in code-analysis workflows, got
331
+ // no credential and failed on a private repo with git's opaque "could not read
332
+ // Username". Skipping a conservative flag allowlist finds the same positional while
333
+ // any unrecognized flag still falls through to no-credential (fails safe).
334
+ function parseCloneTarget(args: readonly string[]): TargetSpec | null {
335
+ const positionals: string[] = []
336
+ for (let i = 0; i < args.length; i++) {
337
+ const arg = args[i] as string
338
+ if (SAFE_CLONE_FLAGS.has(arg)) continue
339
+ if (SAFE_CLONE_VALUE_FLAGS.has(arg)) {
340
+ const value = args[++i]
341
+ if (value === undefined || !isOptionValue(value)) return null
342
+ continue
343
+ }
344
+ if (SAFE_CLONE_INLINE_PREFIXES.some((prefix) => arg.startsWith(prefix))) continue
345
+ if (!isOptionValue(arg)) return null
346
+ positionals.push(arg)
347
+ }
348
+ if (positionals.length === 0 || positionals.length > 2) return null
349
+ const url = positionals[0] as string
350
+ return looksLikeUrl(url) ? { kind: 'single', value: url, forPush: false } : null
351
+ }
352
+
305
353
  function parseMintablePush(args: readonly string[]): TargetSpec | null {
306
354
  const positionals: string[] = []
307
355
  let repository: string | null = null
@@ -798,7 +846,10 @@ function analyzeCloneThenInspect(command: string): GitCommandDecision | null {
798
846
  const parsed = parseStrictCloneHead(split.head)
799
847
  if (parsed === null) return isNonGithubCloneHead(split.head) ? { kind: 'pass-through' } : null
800
848
 
801
- const canonicalHead = `/usr/bin/git clone ${posixSingleQuote(parsed.url)} ${posixSingleQuote(parsed.destination)}`
849
+ const flagPart = parsed.flags.map(posixSingleQuote).join(' ')
850
+ const canonicalHead =
851
+ `/usr/bin/git clone ${flagPart === '' ? '' : `${flagPart} `}` +
852
+ `${posixSingleQuote(parsed.url)} ${posixSingleQuote(parsed.destination)}`
802
853
  return {
803
854
  kind: 'inject',
804
855
  repoSlug: parsed.repoSlug,
@@ -811,19 +862,53 @@ function tailContainsGitInvocation(tail: string): boolean {
811
862
  return extractEvidenceGitSegments(tail).length > 0
812
863
  }
813
864
 
814
- type StrictCloneHead = { repoSlug: string; url: string; destination: string }
865
+ type StrictCloneHead = { repoSlug: string; url: string; destination: string; flags: string[] }
815
866
 
816
- const STRICT_CLONE_HEAD_RE =
817
- /^[ \t]*git[ \t]+clone[ \t]+(https:\/\/github\.com\/([A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+?)(?:\.git)?)[ \t]+([A-Za-z0-9._/-]+)[ \t]*$/
867
+ const STRICT_CLONE_URL_RE = /^(https:\/\/github\.com\/([A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+?)(?:\.git)?)$/
868
+ const STRICT_CLONE_DESTINATION_RE = /^[A-Za-z0-9._/-]+$/
869
+ const SAFE_CLONE_FLAG_VALUE_RE = /^[A-Za-z0-9._:@/-]+$/
818
870
 
871
+ // The executed head is REBUILT from these parts rather than passed through, so the
872
+ // grammar stays tiny even though it now admits flags: every accepted token comes from
873
+ // the SAFE_CLONE_* allowlists, every value is charset-constrained, and each is
874
+ // re-emitted single-quoted. Without this a plain `git clone --depth 1 <url> <dir> &&
875
+ // <inspect>` — the natural code-analysis command — blocks outright.
819
876
  function parseStrictCloneHead(head: string): StrictCloneHead | null {
820
- const match = STRICT_CLONE_HEAD_RE.exec(head)
877
+ const words = tokenizeStandalone(head)
878
+ if (words === null || words[0] !== 'git' || words[1] !== 'clone') return null
879
+
880
+ const flags: string[] = []
881
+ const positionals: string[] = []
882
+ for (let i = 2; i < words.length; i++) {
883
+ const word = words[i] as string
884
+ if (SAFE_CLONE_FLAGS.has(word)) {
885
+ flags.push(word)
886
+ continue
887
+ }
888
+ if (SAFE_CLONE_VALUE_FLAGS.has(word)) {
889
+ const value = words[++i]
890
+ if (value === undefined || !SAFE_CLONE_FLAG_VALUE_RE.test(value)) return null
891
+ flags.push(word, value)
892
+ continue
893
+ }
894
+ const prefix = SAFE_CLONE_INLINE_PREFIXES.find((candidate) => word.startsWith(candidate))
895
+ if (prefix !== undefined) {
896
+ if (!SAFE_CLONE_FLAG_VALUE_RE.test(word.slice(prefix.length))) return null
897
+ flags.push(word)
898
+ continue
899
+ }
900
+ if (word.startsWith('-')) return null
901
+ positionals.push(word)
902
+ }
903
+
904
+ if (positionals.length !== 2) return null
905
+ const [rawUrl, destination] = positionals as [string, string]
906
+ const match = STRICT_CLONE_URL_RE.exec(rawUrl)
821
907
  if (match === null) return null
822
- const url = match[1] as string
823
908
  const repoSlug = match[2] as string
824
- const destination = match[3] as string
909
+ if (!STRICT_CLONE_DESTINATION_RE.test(destination)) return null
825
910
  if (destination.startsWith('-') || repoSlug.startsWith('/') || repoSlug.endsWith('/')) return null
826
- return { repoSlug, url, destination }
911
+ return { repoSlug, url: match[1] as string, destination, flags }
827
912
  }
828
913
 
829
914
  function isNonGithubCloneHead(head: string): boolean {
@@ -42,6 +42,35 @@ import { checkGraphqlAuthNudge } from './graphql-auth-nudge'
42
42
  import { commitReviewIfSucceeded, dismissalMutationSucceeded, noteReviewCommand } from './review-recorder'
43
43
  import { classifyGhToken, shouldMintAppToken } from './token-class'
44
44
 
45
+ const REPO_LIST_RE = /\bgh\s+repo\s+list\b/
46
+ const GH_API_RE = /\bgh\s+api\b/
47
+ const GH_REPO_VIEW_RE = /\bgh\s+repo\s+view\b/
48
+
49
+ // `gh api` rejects `-R`, and `gh repo view` takes a positional slug — so the
50
+ // blanket "-R owner/repo" advice is unrunnable for exactly the commands an agent
51
+ // reaches for when exploring an unfamiliar repo. A denial that suggests a command
52
+ // which cannot parse burns a turn and teaches the agent the repo is unreachable.
53
+ function ghRewriteExample(command: string, slug: string): string {
54
+ if (GH_API_RE.test(command)) return `\`gh api /repos/${slug}/...\``
55
+ if (GH_REPO_VIEW_RE.test(command)) return `\`gh repo view ${slug}\``
56
+ return `\`gh <cmd> -R ${slug}\``
57
+ }
58
+
59
+ // The shapes an agent reaches for when it wants to READ SOURCE. None is brokerable:
60
+ // `repo clone` is absent from SAFE_GH_OPERATIONS, and archive/search endpoints can
61
+ // write or exfiltrate. Brokered `git clone` IS supported for any allowlisted repo, so
62
+ // name it — otherwise the agent concludes the source is unreachable and says so.
63
+ const SOURCE_READ_RE = /\bgh\s+repo\s+clone\b|\bgh\s+search\s+code\b|\/(?:tarball|zipball|contents)\b/
64
+
65
+ function sourceReadRecovery(command: string, slug: string): string {
66
+ if (!SOURCE_READ_RE.test(command)) return ''
67
+ return (
68
+ ` To read this repository's source, clone it as its own standalone command:` +
69
+ ` \`git clone --depth 1 https://github.com/${slug}.git <dir>\` — TypeClaw brokers that clone for any` +
70
+ ' repo in `channels.github.repos[]`. Inspect the working copy with a separate command afterwards.'
71
+ )
72
+ }
73
+
45
74
  export default definePlugin({
46
75
  plugin: async (ctx) => {
47
76
  const resolveTokenForRepo = ctx.github.resolveTokenForRepo
@@ -224,15 +253,25 @@ export default definePlugin({
224
253
  // exact single-bare rewrite so the agent recovers in one step instead of
225
254
  // guessing. Composition blocks get a split-the-script instruction. The
226
255
  // returned text is appended to the block reason (synchronous, always seen).
227
- const buildGhBlockGuidance = (code: string, fallbackRepo: string | undefined): string => {
256
+ const buildGhBlockGuidance = (code: string, fallbackRepo: string | undefined, command: string): string => {
228
257
  const slug = fallbackRepo ?? 'owner/repo'
258
+ const example = ghRewriteExample(command, slug)
259
+ const sourceRead = sourceReadRecovery(command, slug)
229
260
  if (code === 'composition') {
230
261
  return (
231
- ` Run each gh as its own single bare command, e.g. \`gh label edit <name> -R ${slug} --name ...\` —` +
232
- ' not inside a function, `if`/`then`, `&&`, `;`, or `$(...)`.'
262
+ ` Run each gh as its own single bare command, e.g. ${example} —` +
263
+ ' not inside a function, `if`/`then`, `&&`, `;`, or `$(...)`.' +
264
+ sourceRead
265
+ )
266
+ }
267
+ if (REPO_LIST_RE.test(command)) {
268
+ return (
269
+ ' `gh repo list` targets an owner rather than one repository, so there is no per-repo' +
270
+ ` rewrite that can mint a token. Name a concrete repo instead, e.g. \`gh repo view ${slug}\`.` +
271
+ sourceRead
233
272
  )
234
273
  }
235
- return ` For example: \`gh <cmd> -R ${slug}\` as a single bare command.`
274
+ return ` For example: ${example} as a single bare command.` + sourceRead
236
275
  }
237
276
 
238
277
  // 'fall-through' means "not a repo-targeting gh command" so the caller can
@@ -273,7 +312,7 @@ export default definePlugin({
273
312
  reason:
274
313
  decision.code === 'credential-display'
275
314
  ? decision.reason
276
- : decision.reason + buildGhBlockGuidance(decision.code, fallbackRepo),
315
+ : decision.reason + buildGhBlockGuidance(decision.code, fallbackRepo, command),
277
316
  }
278
317
  }
279
318
 
@@ -2,7 +2,7 @@ import { createHmac, randomUUID, timingSafeEqual } from 'node:crypto'
2
2
 
3
3
  import { registerOrJoinReplyReviewRound } from '@/channels/github-review-verdict-coordinator'
4
4
  import type { GithubReviewOn } from '@/channels/schema'
5
- import type { GithubReviewFollowupRound, InboundMessage } from '@/channels/types'
5
+ import type { GithubReviewFollowupRound, GithubReviewThreadCloseout, InboundMessage } from '@/channels/types'
6
6
 
7
7
  import { describeError } from '../../describe-error'
8
8
  import type { GithubAuthContext } from './auth'
@@ -572,6 +572,10 @@ export function classifyGithubInbound(
572
572
  const parent =
573
573
  parentId !== null && options?.reviewCommentParent?.parentId === parentId ? options.reviewCommentParent : null
574
574
  const commenter = readUser(comment.user)
575
+ const githubReviewThreadCloseout: GithubReviewThreadCloseout | undefined =
576
+ parent?.isSelf === true && commenter !== null && !isBotUser(commenter)
577
+ ? { workspace: base.workspace, prNumber: number, rootCommentId: String(root) }
578
+ : undefined
575
579
  const directedAtBot =
576
580
  parentId === null &&
577
581
  isSelfPr(readUser(pr.user), selfLogin, options?.authType ?? 'pat') &&
@@ -591,6 +595,7 @@ export function classifyGithubInbound(
591
595
  ...(directedAtBot ? { forceBotMention: true } : {}),
592
596
  replyToBotMessageId: parent?.isSelf === true ? String(parent.parentId) : null,
593
597
  replyToOtherMessageId: parent?.isSelf === false ? String(parent.parentId) : null,
598
+ ...(githubReviewThreadCloseout !== undefined ? { githubReviewThreadCloseout } : {}),
594
599
  },
595
600
  )
596
601
  }
@@ -785,6 +790,7 @@ type BuildInboundOptions = {
785
790
  suppressSticky?: boolean
786
791
  replyToBotMessageId?: string | null
787
792
  replyToOtherMessageId?: string | null
793
+ githubReviewThreadCloseout?: GithubReviewThreadCloseout
788
794
  // Forces isBotMention=true with no @-handle in the body. A review (or
789
795
  // top-level review comment) on a PR the agent ITSELF authored is directed at
790
796
  // the bot — the inverse of review_requested — so it engages even though the
@@ -1009,9 +1015,12 @@ function buildInbound(
1009
1015
  ...(reactionTarget !== null ? { reactionRef: encodeGithubReactionRef(reactionTarget) } : {}),
1010
1016
  authorId: String(user.id),
1011
1017
  authorName: user.login,
1012
- authorIsBot: user.type === 'Bot',
1018
+ authorIsBot: isBotUser(user),
1013
1019
  isBotMention,
1014
1020
  ...(options?.suppressSticky === true ? { suppressSticky: true } : {}),
1021
+ ...(options?.githubReviewThreadCloseout !== undefined
1022
+ ? { githubReviewThreadCloseout: options.githubReviewThreadCloseout }
1023
+ : {}),
1015
1024
  replyToBotMessageId,
1016
1025
  replyToOtherMessageId,
1017
1026
  ts: typeof rawTs === 'string' ? Date.parse(rawTs) || 0 : 0,
@@ -1242,6 +1251,10 @@ function isSelfAuthor(author: GithubUser, selfId: string | null, selfLogin: stri
1242
1251
  return false
1243
1252
  }
1244
1253
 
1254
+ function isBotUser(user: GithubUser): boolean {
1255
+ return user.type === 'Bot'
1256
+ }
1257
+
1245
1258
  // Whether the PR's OPENER is this agent. Distinct from isSelfAuthor (which
1246
1259
  // guards self-loops on the event ACTOR by id/login): here we have only the
1247
1260
  // `pull_request.user` from a review payload, no id to compare, and under App
@@ -0,0 +1,141 @@
1
+ import { mkdir, readFile, rename, writeFile } from 'node:fs/promises'
2
+ import { dirname, join } from 'node:path'
3
+
4
+ import { describeError } from '../describe-error'
5
+
6
+ const FILE_VERSION = 1
7
+ const MAX_MESSAGE_IDS_PER_THREAD = 500
8
+
9
+ type ThreadState = {
10
+ accountId: string
11
+ threadId: string
12
+ messageIds: string[]
13
+ }
14
+
15
+ type FileV1 = {
16
+ version: 1
17
+ threads: ThreadState[]
18
+ }
19
+
20
+ export type InstagramContinuityLogger = {
21
+ warn: (msg: string) => void
22
+ error: (msg: string) => void
23
+ }
24
+
25
+ export type InstagramContinuityStore = {
26
+ knowsThread: (accountId: string, threadId: string) => boolean
27
+ hasMessage: (accountId: string, threadId: string, messageId: string) => boolean
28
+ seedThread: (accountId: string, threadId: string, messageIds: readonly string[]) => Promise<void>
29
+ markMessage: (accountId: string, threadId: string, messageId: string) => Promise<void>
30
+ }
31
+
32
+ export function instagramContinuityPath(agentDir: string): string {
33
+ return join(agentDir, 'channels', 'instagram-continuity.json')
34
+ }
35
+
36
+ export async function loadInstagramContinuityStore(
37
+ agentDir: string,
38
+ logger: InstagramContinuityLogger,
39
+ ): Promise<InstagramContinuityStore> {
40
+ const path = instagramContinuityPath(agentDir)
41
+ const threads = new Map<string, string[]>()
42
+ for (const state of await readStates(path, logger)) {
43
+ threads.set(threadKey(state.accountId, state.threadId), state.messageIds.slice(-MAX_MESSAGE_IDS_PER_THREAD))
44
+ }
45
+
46
+ const flush = async (): Promise<void> => {
47
+ const states: ThreadState[] = []
48
+ for (const [key, messageIds] of threads) {
49
+ const [accountId, threadId] = parseThreadKey(key)
50
+ states.push({ accountId, threadId, messageIds })
51
+ }
52
+ const payload: FileV1 = { version: FILE_VERSION, threads: states }
53
+ await mkdir(dirname(path), { recursive: true })
54
+ const tmp = `${path}.tmp`
55
+ await writeFile(tmp, `${JSON.stringify(payload, null, 2)}\n`, 'utf8')
56
+ await rename(tmp, path)
57
+ }
58
+
59
+ const saveIds = async (accountId: string, threadId: string, messageIds: readonly string[]): Promise<void> => {
60
+ const key = threadKey(accountId, threadId)
61
+ const previous = threads.get(key)
62
+ const next = Array.from(new Set(messageIds)).slice(-MAX_MESSAGE_IDS_PER_THREAD)
63
+ threads.set(key, next)
64
+ try {
65
+ await flush()
66
+ } catch (err) {
67
+ if (previous === undefined) threads.delete(key)
68
+ else threads.set(key, previous)
69
+ throw err
70
+ }
71
+ }
72
+
73
+ return {
74
+ knowsThread(accountId, threadId): boolean {
75
+ return threads.has(threadKey(accountId, threadId))
76
+ },
77
+ hasMessage(accountId, threadId, messageId): boolean {
78
+ return threads.get(threadKey(accountId, threadId))?.includes(messageId) ?? false
79
+ },
80
+ async seedThread(accountId, threadId, messageIds): Promise<void> {
81
+ await saveIds(accountId, threadId, messageIds)
82
+ },
83
+ async markMessage(accountId, threadId, messageId): Promise<void> {
84
+ const current = threads.get(threadKey(accountId, threadId)) ?? []
85
+ if (current.includes(messageId)) return
86
+ await saveIds(accountId, threadId, [...current, messageId])
87
+ },
88
+ }
89
+ }
90
+
91
+ function threadKey(accountId: string, threadId: string): string {
92
+ return `${accountId}\u0000${threadId}`
93
+ }
94
+
95
+ function parseThreadKey(key: string): [string, string] {
96
+ const separator = key.indexOf('\u0000')
97
+ return [key.slice(0, separator), key.slice(separator + 1)]
98
+ }
99
+
100
+ async function readStates(path: string, logger: InstagramContinuityLogger): Promise<ThreadState[]> {
101
+ let raw: string
102
+ try {
103
+ raw = await readFile(path, 'utf8')
104
+ } catch {
105
+ return []
106
+ }
107
+
108
+ let parsed: unknown
109
+ try {
110
+ parsed = JSON.parse(raw)
111
+ } catch (err) {
112
+ logger.error(`[instagram] ${path} corrupted: ${describeError(err)}; starting fresh`)
113
+ return []
114
+ }
115
+ if (!isObject(parsed)) {
116
+ logger.warn(`[instagram] ${path} not an object; ignored`)
117
+ return []
118
+ }
119
+ if (parsed.version !== FILE_VERSION) {
120
+ logger.warn(
121
+ `[instagram] ${path} version ${String(parsed.version)} not supported (expected ${FILE_VERSION}); ignored`,
122
+ )
123
+ return []
124
+ }
125
+ if (!Array.isArray(parsed.threads)) return []
126
+ return parsed.threads.filter(isThreadState)
127
+ }
128
+
129
+ function isObject(value: unknown): value is Record<string, unknown> {
130
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
131
+ }
132
+
133
+ function isThreadState(value: unknown): value is ThreadState {
134
+ if (!isObject(value)) return false
135
+ return (
136
+ typeof value.accountId === 'string' &&
137
+ typeof value.threadId === 'string' &&
138
+ Array.isArray(value.messageIds) &&
139
+ value.messageIds.every((id) => typeof id === 'string')
140
+ )
141
+ }
@@ -22,6 +22,7 @@ import type {
22
22
  import { describeError } from '../describe-error'
23
23
  import { createInstagramChannelResolver } from './instagram-channel-resolver'
24
24
  import { classifyInbound } from './instagram-classify'
25
+ import { loadInstagramContinuityStore, type InstagramContinuityStore } from './instagram-continuity-store'
25
26
  import { toInstagramPlainText } from './instagram-format'
26
27
 
27
28
  export interface InstagramClientShape {
@@ -89,6 +90,7 @@ const consoleLogger: InstagramAdapterLogger = {
89
90
  }
90
91
 
91
92
  export type InstagramAdapterOptions = {
93
+ agentDir?: string
92
94
  router: ChannelRouter
93
95
  configRef: () => ChannelAdapterConfig
94
96
  logger?: InstagramAdapterLogger
@@ -97,6 +99,7 @@ export type InstagramAdapterOptions = {
97
99
  client?: InstagramClientShape
98
100
  clientFactory?: (credManager?: InstagramCredentialManager) => InstagramClientShape
99
101
  listenerCtorResolver?: () => { ctor: InstagramListenerCtor; transport: 'hybrid' | 'polling' }
102
+ continuityStore?: InstagramContinuityStore
100
103
  now?: () => number
101
104
  }
102
105
 
@@ -107,6 +110,7 @@ export type InstagramAdapter = {
107
110
  }
108
111
 
109
112
  export const INSTAGRAM_HISTORY_LIMIT_MAX = 200
113
+ const INSTAGRAM_RECOVERY_CHAT_LIMIT = 100
110
114
 
111
115
  export function createOutboundCallback(deps: {
112
116
  client: Pick<InstagramClientShape, 'sendMessage'>
@@ -177,6 +181,13 @@ export function createInstagramAdapter(options: InstagramAdapterOptions): Instag
177
181
  let started = false
178
182
  let inflightInbounds = 0
179
183
  let stopWaiters: Array<() => void> = []
184
+ let continuityStore: InstagramContinuityStore | null = options.continuityStore ?? null
185
+ let activeTransport: 'realtime' | 'polling' = 'polling'
186
+ let inboundQueue = Promise.resolve()
187
+ let adapterStartedAt = 0
188
+ const bootstrapBaselines = new Map<string, readonly string[]>()
189
+ const recoveryLimitWarnedThreads = new Set<string>()
190
+ let recoveryChatLimitWarned = false
180
191
 
181
192
  const channelResolver = createInstagramChannelResolver({ client, logger })
182
193
 
@@ -190,7 +201,7 @@ export function createInstagramAdapter(options: InstagramAdapterOptions): Instag
190
201
  const historyCallback = createInstagramHistoryCallback({ client, logger, selfUserIdRef: () => selfUserId })
191
202
  const outboundCallback = createOutboundCallback({ client, logger, formatChannelTag })
192
203
 
193
- const processInbound = async (message: InstagramMessageSummary): Promise<void> => {
204
+ const processInbound = async (message: InstagramMessageSummary): Promise<boolean> => {
194
205
  inflightInbounds++
195
206
  try {
196
207
  if (channelResolver.lookupChat(message.thread_id) === null) {
@@ -216,15 +227,17 @@ export function createInstagramAdapter(options: InstagramAdapterOptions): Instag
216
227
  })
217
228
  if (verdict.kind === 'drop') {
218
229
  logger.info(`[instagram] dropped message_id=${message.id} reason=${verdict.reason}`)
219
- return
230
+ return true
220
231
  }
221
232
 
222
233
  logger.info(
223
234
  `[instagram] routed message_id=${message.id} ${inboundTag} mention=${verdict.payload.isBotMention} dm=${verdict.payload.isDm}`,
224
235
  )
225
236
  await options.router.route(verdict.payload)
237
+ return true
226
238
  } catch (err) {
227
239
  logger.error(`[instagram] handleInbound failed: ${describeError(err)}`)
240
+ return false
228
241
  } finally {
229
242
  inflightInbounds--
230
243
  if (inflightInbounds === 0 && stopWaiters.length > 0) {
@@ -235,10 +248,117 @@ export function createInstagramAdapter(options: InstagramAdapterOptions): Instag
235
248
  }
236
249
  }
237
250
 
251
+ const markDelivered = async (message: InstagramMessageSummary): Promise<void> => {
252
+ if (continuityStore === null || selfUserId === null) return
253
+ await continuityStore.markMessage(selfUserId, message.thread_id, message.id)
254
+ }
255
+
256
+ const deliverUnseen = async (message: InstagramMessageSummary): Promise<boolean> => {
257
+ if (
258
+ continuityStore !== null &&
259
+ selfUserId !== null &&
260
+ continuityStore.hasMessage(selfUserId, message.thread_id, message.id)
261
+ ) {
262
+ return true
263
+ }
264
+ if (!(await processInbound(message))) return false
265
+ // Checkpoint only after the router accepts the message. A crash can replay
266
+ // once, but persisting first would silently lose a message when routing fails.
267
+ await markDelivered(message)
268
+ return true
269
+ }
270
+
271
+ const fetchRecoveryMessages = async (threadId: string): Promise<InstagramMessageSummary[]> => {
272
+ const messages = await client.getMessages(threadId, INSTAGRAM_HISTORY_LIMIT_MAX)
273
+ if (messages.length === INSTAGRAM_HISTORY_LIMIT_MAX && !recoveryLimitWarnedThreads.has(threadId)) {
274
+ recoveryLimitWarnedThreads.add(threadId)
275
+ logger.warn(
276
+ `[instagram] recovery history reached limit=${INSTAGRAM_HISTORY_LIMIT_MAX} chat=${threadId}; older unseen messages cannot be recovered without upstream pagination`,
277
+ )
278
+ }
279
+ return messages.toSorted((left, right) => Date.parse(left.timestamp) - Date.parse(right.timestamp))
280
+ }
281
+
282
+ const establishUnknownThread = async (
283
+ threadId: string,
284
+ messages: readonly InstagramMessageSummary[],
285
+ ): Promise<void> => {
286
+ if (continuityStore === null || selfUserId === null) return
287
+ const bootstrapIds = bootstrapBaselines.get(threadId)
288
+ const baselineIds = bootstrapIds ?? messages.filter(isBeforeAdapterStart).map((message) => message.id)
289
+ await continuityStore.seedThread(selfUserId, threadId, baselineIds)
290
+ bootstrapBaselines.delete(threadId)
291
+ }
292
+
293
+ const reconcileThread = async (threadId: string): Promise<boolean> => {
294
+ if (continuityStore === null || selfUserId === null) return true
295
+ const messages = await fetchRecoveryMessages(threadId)
296
+ if (!continuityStore.knowsThread(selfUserId, threadId)) await establishUnknownThread(threadId, messages)
297
+ for (const message of messages) {
298
+ if (!(await deliverUnseen(message))) return false
299
+ }
300
+ return true
301
+ }
302
+
303
+ const reconcileAll = async (): Promise<void> => {
304
+ const chats = await client.listChats(INSTAGRAM_RECOVERY_CHAT_LIMIT)
305
+ warnIfChatLimitReached(chats.length)
306
+ for (const chat of chats) {
307
+ try {
308
+ await reconcileThread(chat.id)
309
+ } catch (err) {
310
+ logger.error(`[instagram] recovery failed chat=${chat.id}: ${describeError(err)}`)
311
+ }
312
+ }
313
+ }
314
+
315
+ const seedBootstrap = async (): Promise<void> => {
316
+ const chats = await client.listChats(INSTAGRAM_RECOVERY_CHAT_LIMIT)
317
+ warnIfChatLimitReached(chats.length)
318
+ for (const chat of chats) {
319
+ try {
320
+ if (continuityStore === null || selfUserId === null || continuityStore.knowsThread(selfUserId, chat.id)) {
321
+ await reconcileThread(chat.id)
322
+ continue
323
+ }
324
+ const messages = await fetchRecoveryMessages(chat.id)
325
+ const baselineIds = messages.filter(isBeforeAdapterStart).map((message) => message.id)
326
+ bootstrapBaselines.set(chat.id, baselineIds)
327
+ await continuityStore.seedThread(selfUserId, chat.id, baselineIds)
328
+ bootstrapBaselines.delete(chat.id)
329
+ for (const message of messages) {
330
+ if (!(await deliverUnseen(message))) break
331
+ }
332
+ } catch (err) {
333
+ logger.error(`[instagram] bootstrap failed chat=${chat.id}: ${describeError(err)}`)
334
+ }
335
+ }
336
+ }
337
+
338
+ const warnIfChatLimitReached = (chatCount: number): void => {
339
+ if (chatCount !== INSTAGRAM_RECOVERY_CHAT_LIMIT || recoveryChatLimitWarned) return
340
+ recoveryChatLimitWarned = true
341
+ logger.warn(
342
+ `[instagram] recovery chat list reached limit=${INSTAGRAM_RECOVERY_CHAT_LIMIT}; older chats cannot be recovered without upstream pagination`,
343
+ )
344
+ }
345
+
346
+ const isBeforeAdapterStart = (message: InstagramMessageSummary): boolean => {
347
+ const timestamp = Date.parse(message.timestamp)
348
+ return Number.isNaN(timestamp) || timestamp < adapterStartedAt
349
+ }
350
+
351
+ const enqueueInbound = (task: () => Promise<void>): void => {
352
+ inboundQueue = inboundQueue.then(task).catch((err: unknown) => {
353
+ logger.error(`[instagram] recovery failed: ${describeError(err)}`)
354
+ })
355
+ }
356
+
238
357
  return {
239
358
  async start(): Promise<void> {
240
359
  if (started) return
241
360
  started = true
361
+ adapterStartedAt = (options.now ?? Date.now)()
242
362
 
243
363
  try {
244
364
  const credentialStore = options.credentialsStore ?? null
@@ -269,28 +389,52 @@ export function createInstagramAdapter(options: InstagramAdapterOptions): Instag
269
389
  throw err
270
390
  }
271
391
 
392
+ if (continuityStore === null && options.agentDir !== undefined) {
393
+ continuityStore = await loadInstagramContinuityStore(options.agentDir, logger)
394
+ }
395
+
272
396
  try {
273
397
  await channelResolver.refresh()
274
398
  } catch (err) {
275
399
  logger.warn(`[instagram] initial chat list fetch failed: ${describeError(err)}`)
276
400
  }
277
401
 
402
+ try {
403
+ await seedBootstrap()
404
+ } catch (err) {
405
+ logger.warn(`[instagram] initial recovery failed: ${describeError(err)}`)
406
+ }
407
+
278
408
  const resolved = (options.listenerCtorResolver ?? resolveInstagramListenerCtor)()
279
409
  listener = new resolved.ctor(client, { pollInterval: 5_000 })
280
410
  logger.info(`[instagram] listener transport=${resolved.transport}`)
281
411
  listener.on('connected', ({ userId, transport = 'polling' }) => {
412
+ if (!started) return
282
413
  connected = true
414
+ activeTransport = transport
283
415
  logger.info(`[instagram] connected (user_id=${userId}, transport=${transport})`)
416
+ enqueueInbound(reconcileAll)
284
417
  })
285
418
  listener.on('disconnected', () => {
419
+ if (!started) return
286
420
  connected = false
287
421
  logger.warn('[instagram] disconnected; SDK will reconnect with backoff')
288
422
  })
289
423
  listener.on('error', (err) => {
424
+ if (!started) return
290
425
  logger.error(`[instagram] listener error: ${describeError(err)}`)
291
426
  })
292
427
  listener.on('message', (message) => {
293
- void processInbound(message)
428
+ if (!started) return
429
+ const transport = activeTransport
430
+ enqueueInbound(async () => {
431
+ const unknownThread =
432
+ continuityStore !== null &&
433
+ selfUserId !== null &&
434
+ !continuityStore.knowsThread(selfUserId, message.thread_id)
435
+ if ((transport === 'polling' || unknownThread) && !(await reconcileThread(message.thread_id))) return
436
+ await deliverUnseen(message)
437
+ })
294
438
  })
295
439
 
296
440
  try {
@@ -318,12 +462,13 @@ export function createInstagramAdapter(options: InstagramAdapterOptions): Instag
318
462
  options.router.unregisterOutbound('instagram', outboundCallback)
319
463
  options.router.unregisterChannelNameResolver('instagram', channelResolver.resolve)
320
464
  options.router.unregisterHistory('instagram', historyCallback)
465
+ listener?.stop()
466
+ await inboundQueue
321
467
  if (inflightInbounds > 0) {
322
468
  await new Promise<void>((resolve) => {
323
469
  stopWaiters.push(resolve)
324
470
  })
325
471
  }
326
- listener?.stop()
327
472
  listener = null
328
473
  selfUserId = null
329
474
  connected = false
@@ -61,9 +61,11 @@ export function renderPrVerdictStandDownReminder(args: { prNumber: number; verdi
61
61
  return (
62
62
  `<system-reminder>\n` +
63
63
  `Another session in this agent has already posted a formal ${args.verdict} review for PR #${args.prNumber}. ` +
64
- `Do not submit your own ${args.verdict} (or any redundant verdict) for this PR this turn unless new ` +
64
+ `This notice applies ONLY to formal PR-level verdicts. Do not submit your own ${args.verdict} (or any redundant ` +
65
+ `verdict) for this PR this turn unless new ` +
65
66
  `information genuinely invalidates that result (e.g. new commits, or a different verdict warranted by ` +
66
- `fresh evidence). You may still reply to inline review comments / threads as normal.\n` +
67
+ `fresh evidence). It does NOT discharge this session's inline review-thread close-out: you must still reply to ` +
68
+ `the inline thread as normal, resolving it if addressed or leaving it open with an explanation.\n` +
67
69
  `</system-reminder>`
68
70
  )
69
71
  }