@vectorize-io/hindsight-coding-agents 0.3.1 → 0.3.2

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.
@@ -1088,10 +1088,10 @@ async function supportsAppend(client) {
1088
1088
  return false;
1089
1089
  }
1090
1090
  }
1091
+ var DEFAULT_RETRY_WINDOW_MS = 6e4;
1091
1092
  var RETAIN_RETRY_ATTEMPTS = 2;
1092
- var RETAIN_RETRY_BUDGET_MS = 6e3;
1093
- async function submitWithRetry(send, isStillNewest) {
1094
- let spent = 0;
1093
+ var REQUEST_BUDGET_MS = 15e3;
1094
+ async function submitWithRetry(send, isStillNewest, retryUntil) {
1095
1095
  for (let attempt = 0; ; attempt++) {
1096
1096
  try {
1097
1097
  return await send();
@@ -1099,8 +1099,7 @@ async function submitWithRetry(send, isStillNewest) {
1099
1099
  const limited = e instanceof RateLimitedError;
1100
1100
  if (!limited || attempt >= RETAIN_RETRY_ATTEMPTS || !isStillNewest()) throw e;
1101
1101
  const wait = e.retryAfterMs || 1e3;
1102
- if (wait > RETAIN_RETRY_BUDGET_MS - spent) throw e;
1103
- spent += wait;
1102
+ if (Date.now() + wait + REQUEST_BUDGET_MS > retryUntil) throw e;
1104
1103
  await sleep(wait);
1105
1104
  }
1106
1105
  }
@@ -1120,14 +1119,24 @@ function serialize(cursors, sessionId, write2) {
1120
1119
  }
1121
1120
  async function retainLiveSession(client, sessionId, turns, startTs, harness, opts = {}) {
1122
1121
  const cursors = opts.cursors;
1123
- if (!cursors) return writeSession(client, sessionId, turns, startTs, harness, opts.stamp);
1122
+ if (!cursors)
1123
+ return writeSession(
1124
+ client,
1125
+ sessionId,
1126
+ turns,
1127
+ startTs,
1128
+ harness,
1129
+ opts.stamp,
1130
+ void 0,
1131
+ opts.retryUntil
1132
+ );
1124
1133
  return serialize(
1125
1134
  cursors,
1126
1135
  sessionId,
1127
- () => writeSession(client, sessionId, turns, startTs, harness, opts.stamp, cursors)
1136
+ () => writeSession(client, sessionId, turns, startTs, harness, opts.stamp, cursors, opts.retryUntil)
1128
1137
  );
1129
1138
  }
1130
- async function writeSession(client, sessionId, turns, startTs, harness, stamp, cursors) {
1139
+ async function writeSession(client, sessionId, turns, startTs, harness, stamp, cursors, retryUntil = Date.now() + DEFAULT_RETRY_WINDOW_MS) {
1131
1140
  const refId = `conversation:${sessionId}`;
1132
1141
  const appendSupported = Boolean(cursors) && await supportsAppend(client);
1133
1142
  const plan = planRetain(turns, cursors?.read(sessionId), { appendSupported, bank: client.bank });
@@ -1179,7 +1188,8 @@ ${content}`),
1179
1188
  if (!cursors) return true;
1180
1189
  const now = cursors.read(sessionId);
1181
1190
  return now?.turns === claimed.turns && now?.fingerprint === claimed.fingerprint;
1182
- }
1191
+ },
1192
+ retryUntil
1183
1193
  );
1184
1194
  cursors?.write(sessionId, next);
1185
1195
  }
@@ -1495,6 +1505,7 @@ function readClaudeTranscript(path) {
1495
1505
  }
1496
1506
 
1497
1507
  // src/core/retain-hook.ts
1508
+ var HOST_DEADLINE_MARGIN_MS = 2e3;
1498
1509
  async function buildRetain(args) {
1499
1510
  const { harness, sessionId, transcriptPath, client } = args;
1500
1511
  const readTranscript = args.readTranscript ?? readClaudeTranscript;
@@ -1505,7 +1516,8 @@ async function buildRetain(args) {
1505
1516
  try {
1506
1517
  await retainLiveSession(client, sessionId, turns, startTs, harness, {
1507
1518
  cursors: args.cursors ?? fileCursorStore(harness),
1508
- stamp: args.stamp
1519
+ stamp: args.stamp,
1520
+ retryUntil: args.retryUntil
1509
1521
  });
1510
1522
  diag(harness, "retain_ok", { ms: Date.now() - t0, turns: turns.length, session: sessionId });
1511
1523
  } catch (e) {
@@ -1521,6 +1533,7 @@ async function buildRetain(args) {
1521
1533
  }
1522
1534
  async function runRetainHook(spec, makeClient = (o) => new HindsightClient(o)) {
1523
1535
  if (process.env.HINDSIGHT_DISABLE_HOOKS) return;
1536
+ const hostDeadline = Date.now() + spec.hostTimeoutSec * 1e3 - HOST_DEADLINE_MARGIN_MS;
1524
1537
  let ev = {};
1525
1538
  try {
1526
1539
  ev = JSON.parse(readFileSync4(0, "utf8"));
@@ -1550,6 +1563,7 @@ async function runRetainHook(spec, makeClient = (o) => new HindsightClient(o)) {
1550
1563
  transcriptPath,
1551
1564
  client,
1552
1565
  readTranscript: spec.readTranscript,
1566
+ retryUntil: hostDeadline,
1553
1567
  stamp: buildRetainStamp(cfg, {
1554
1568
  directory: cwd,
1555
1569
  harness: spec.harness,
@@ -1921,6 +1935,7 @@ var HOOK_HARNESSES = {
1921
1935
  sessionStart: standardSessionStart("claude-code"),
1922
1936
  prompt: claudePrompt,
1923
1937
  retain: {
1938
+ hostTimeoutSec: 60,
1924
1939
  harness: "claude-code",
1925
1940
  parse: (ev) => ({
1926
1941
  sessionId: ev.session_id,
@@ -1939,6 +1954,7 @@ var HOOK_HARNESSES = {
1939
1954
  sessionStart: standardSessionStart("codex"),
1940
1955
  prompt: codexPrompt,
1941
1956
  retain: {
1957
+ hostTimeoutSec: 60,
1942
1958
  harness: "codex",
1943
1959
  parse: (ev) => ({
1944
1960
  sessionId: ev.session_id,
@@ -1967,6 +1983,7 @@ var HOOK_HARNESSES = {
1967
1983
  },
1968
1984
  prompt: antigravityPrompt,
1969
1985
  retain: {
1986
+ hostTimeoutSec: 30,
1970
1987
  harness: "antigravity-cli",
1971
1988
  parse: (ev) => ({
1972
1989
  sessionId: ev.conversationId,
@@ -1995,6 +2012,7 @@ var HOOK_HARNESSES = {
1995
2012
  },
1996
2013
  prompt: cursorPrompt,
1997
2014
  retain: {
2015
+ hostTimeoutSec: 30,
1998
2016
  harness: "cursor-cli",
1999
2017
  parse: (ev) => ({
2000
2018
  sessionId: ev.conversation_id ?? ev.session_id,
@@ -2028,6 +2046,7 @@ var HOOK_HARNESSES = {
2028
2046
  },
2029
2047
  prompt: copilotPrompt,
2030
2048
  retain: {
2049
+ hostTimeoutSec: 60,
2031
2050
  harness: "copilot-cli",
2032
2051
  parse: (ev) => ({
2033
2052
  sessionId: ev.sessionId,
@@ -2056,6 +2075,7 @@ var HOOK_HARNESSES = {
2056
2075
  },
2057
2076
  prompt: devinPrompt,
2058
2077
  retain: {
2078
+ hostTimeoutSec: 60,
2059
2079
  harness: "devin-cli",
2060
2080
  parse: (ev) => {
2061
2081
  const sessionId = ev.session_id;
@@ -2095,6 +2115,7 @@ var HOOK_HARNESSES = {
2095
2115
  })
2096
2116
  },
2097
2117
  retain: {
2118
+ hostTimeoutSec: 60,
2098
2119
  harness: "grok-build",
2099
2120
  parse: (ev) => ({
2100
2121
  sessionId: ev.sessionId,
@@ -1729,6 +1729,7 @@ var HOOK_HARNESSES = {
1729
1729
  sessionStart: standardSessionStart("claude-code"),
1730
1730
  prompt: claudePrompt,
1731
1731
  retain: {
1732
+ hostTimeoutSec: 60,
1732
1733
  harness: "claude-code",
1733
1734
  parse: (ev) => ({
1734
1735
  sessionId: ev.session_id,
@@ -1747,6 +1748,7 @@ var HOOK_HARNESSES = {
1747
1748
  sessionStart: standardSessionStart("codex"),
1748
1749
  prompt: codexPrompt,
1749
1750
  retain: {
1751
+ hostTimeoutSec: 60,
1750
1752
  harness: "codex",
1751
1753
  parse: (ev) => ({
1752
1754
  sessionId: ev.session_id,
@@ -1775,6 +1777,7 @@ var HOOK_HARNESSES = {
1775
1777
  },
1776
1778
  prompt: antigravityPrompt,
1777
1779
  retain: {
1780
+ hostTimeoutSec: 30,
1778
1781
  harness: "antigravity-cli",
1779
1782
  parse: (ev) => ({
1780
1783
  sessionId: ev.conversationId,
@@ -1803,6 +1806,7 @@ var HOOK_HARNESSES = {
1803
1806
  },
1804
1807
  prompt: cursorPrompt,
1805
1808
  retain: {
1809
+ hostTimeoutSec: 30,
1806
1810
  harness: "cursor-cli",
1807
1811
  parse: (ev) => ({
1808
1812
  sessionId: ev.conversation_id ?? ev.session_id,
@@ -1836,6 +1840,7 @@ var HOOK_HARNESSES = {
1836
1840
  },
1837
1841
  prompt: copilotPrompt,
1838
1842
  retain: {
1843
+ hostTimeoutSec: 60,
1839
1844
  harness: "copilot-cli",
1840
1845
  parse: (ev) => ({
1841
1846
  sessionId: ev.sessionId,
@@ -1864,6 +1869,7 @@ var HOOK_HARNESSES = {
1864
1869
  },
1865
1870
  prompt: devinPrompt,
1866
1871
  retain: {
1872
+ hostTimeoutSec: 60,
1867
1873
  harness: "devin-cli",
1868
1874
  parse: (ev) => {
1869
1875
  const sessionId = ev.session_id;
@@ -1903,6 +1909,7 @@ var HOOK_HARNESSES = {
1903
1909
  })
1904
1910
  },
1905
1911
  retain: {
1912
+ hostTimeoutSec: 60,
1906
1913
  harness: "grok-build",
1907
1914
  parse: (ev) => ({
1908
1915
  sessionId: ev.sessionId,
@@ -2147,6 +2147,7 @@ var HOOK_HARNESSES = {
2147
2147
  sessionStart: standardSessionStart("claude-code"),
2148
2148
  prompt: claudePrompt,
2149
2149
  retain: {
2150
+ hostTimeoutSec: 60,
2150
2151
  harness: "claude-code",
2151
2152
  parse: (ev) => ({
2152
2153
  sessionId: ev.session_id,
@@ -2165,6 +2166,7 @@ var HOOK_HARNESSES = {
2165
2166
  sessionStart: standardSessionStart("codex"),
2166
2167
  prompt: codexPrompt,
2167
2168
  retain: {
2169
+ hostTimeoutSec: 60,
2168
2170
  harness: "codex",
2169
2171
  parse: (ev) => ({
2170
2172
  sessionId: ev.session_id,
@@ -2193,6 +2195,7 @@ var HOOK_HARNESSES = {
2193
2195
  },
2194
2196
  prompt: antigravityPrompt,
2195
2197
  retain: {
2198
+ hostTimeoutSec: 30,
2196
2199
  harness: "antigravity-cli",
2197
2200
  parse: (ev) => ({
2198
2201
  sessionId: ev.conversationId,
@@ -2221,6 +2224,7 @@ var HOOK_HARNESSES = {
2221
2224
  },
2222
2225
  prompt: cursorPrompt,
2223
2226
  retain: {
2227
+ hostTimeoutSec: 30,
2224
2228
  harness: "cursor-cli",
2225
2229
  parse: (ev) => ({
2226
2230
  sessionId: ev.conversation_id ?? ev.session_id,
@@ -2254,6 +2258,7 @@ var HOOK_HARNESSES = {
2254
2258
  },
2255
2259
  prompt: copilotPrompt,
2256
2260
  retain: {
2261
+ hostTimeoutSec: 60,
2257
2262
  harness: "copilot-cli",
2258
2263
  parse: (ev) => ({
2259
2264
  sessionId: ev.sessionId,
@@ -2282,6 +2287,7 @@ var HOOK_HARNESSES = {
2282
2287
  },
2283
2288
  prompt: devinPrompt,
2284
2289
  retain: {
2290
+ hostTimeoutSec: 60,
2285
2291
  harness: "devin-cli",
2286
2292
  parse: (ev) => {
2287
2293
  const sessionId = ev.session_id;
@@ -2321,6 +2327,7 @@ var HOOK_HARNESSES = {
2321
2327
  })
2322
2328
  },
2323
2329
  retain: {
2330
+ hostTimeoutSec: 60,
2324
2331
  harness: "grok-build",
2325
2332
  parse: (ev) => ({
2326
2333
  sessionId: ev.sessionId,
@@ -1088,10 +1088,10 @@ async function supportsAppend(client) {
1088
1088
  return false;
1089
1089
  }
1090
1090
  }
1091
+ var DEFAULT_RETRY_WINDOW_MS = 6e4;
1091
1092
  var RETAIN_RETRY_ATTEMPTS = 2;
1092
- var RETAIN_RETRY_BUDGET_MS = 6e3;
1093
- async function submitWithRetry(send, isStillNewest) {
1094
- let spent = 0;
1093
+ var REQUEST_BUDGET_MS = 15e3;
1094
+ async function submitWithRetry(send, isStillNewest, retryUntil) {
1095
1095
  for (let attempt = 0; ; attempt++) {
1096
1096
  try {
1097
1097
  return await send();
@@ -1099,8 +1099,7 @@ async function submitWithRetry(send, isStillNewest) {
1099
1099
  const limited = e instanceof RateLimitedError;
1100
1100
  if (!limited || attempt >= RETAIN_RETRY_ATTEMPTS || !isStillNewest()) throw e;
1101
1101
  const wait = e.retryAfterMs || 1e3;
1102
- if (wait > RETAIN_RETRY_BUDGET_MS - spent) throw e;
1103
- spent += wait;
1102
+ if (Date.now() + wait + REQUEST_BUDGET_MS > retryUntil) throw e;
1104
1103
  await sleep(wait);
1105
1104
  }
1106
1105
  }
@@ -1120,14 +1119,24 @@ function serialize(cursors, sessionId, write2) {
1120
1119
  }
1121
1120
  async function retainLiveSession(client, sessionId, turns, startTs, harness, opts = {}) {
1122
1121
  const cursors = opts.cursors;
1123
- if (!cursors) return writeSession(client, sessionId, turns, startTs, harness, opts.stamp);
1122
+ if (!cursors)
1123
+ return writeSession(
1124
+ client,
1125
+ sessionId,
1126
+ turns,
1127
+ startTs,
1128
+ harness,
1129
+ opts.stamp,
1130
+ void 0,
1131
+ opts.retryUntil
1132
+ );
1124
1133
  return serialize(
1125
1134
  cursors,
1126
1135
  sessionId,
1127
- () => writeSession(client, sessionId, turns, startTs, harness, opts.stamp, cursors)
1136
+ () => writeSession(client, sessionId, turns, startTs, harness, opts.stamp, cursors, opts.retryUntil)
1128
1137
  );
1129
1138
  }
1130
- async function writeSession(client, sessionId, turns, startTs, harness, stamp, cursors) {
1139
+ async function writeSession(client, sessionId, turns, startTs, harness, stamp, cursors, retryUntil = Date.now() + DEFAULT_RETRY_WINDOW_MS) {
1131
1140
  const refId = `conversation:${sessionId}`;
1132
1141
  const appendSupported = Boolean(cursors) && await supportsAppend(client);
1133
1142
  const plan = planRetain(turns, cursors?.read(sessionId), { appendSupported, bank: client.bank });
@@ -1179,7 +1188,8 @@ ${content}`),
1179
1188
  if (!cursors) return true;
1180
1189
  const now = cursors.read(sessionId);
1181
1190
  return now?.turns === claimed.turns && now?.fingerprint === claimed.fingerprint;
1182
- }
1191
+ },
1192
+ retryUntil
1183
1193
  );
1184
1194
  cursors?.write(sessionId, next);
1185
1195
  }
@@ -1495,6 +1505,7 @@ function readClaudeTranscript(path) {
1495
1505
  }
1496
1506
 
1497
1507
  // src/core/retain-hook.ts
1508
+ var HOST_DEADLINE_MARGIN_MS = 2e3;
1498
1509
  async function buildRetain(args) {
1499
1510
  const { harness, sessionId, transcriptPath, client } = args;
1500
1511
  const readTranscript = args.readTranscript ?? readClaudeTranscript;
@@ -1505,7 +1516,8 @@ async function buildRetain(args) {
1505
1516
  try {
1506
1517
  await retainLiveSession(client, sessionId, turns, startTs, harness, {
1507
1518
  cursors: args.cursors ?? fileCursorStore(harness),
1508
- stamp: args.stamp
1519
+ stamp: args.stamp,
1520
+ retryUntil: args.retryUntil
1509
1521
  });
1510
1522
  diag(harness, "retain_ok", { ms: Date.now() - t0, turns: turns.length, session: sessionId });
1511
1523
  } catch (e) {
@@ -1521,6 +1533,7 @@ async function buildRetain(args) {
1521
1533
  }
1522
1534
  async function runRetainHook(spec, makeClient = (o) => new HindsightClient(o)) {
1523
1535
  if (process.env.HINDSIGHT_DISABLE_HOOKS) return;
1536
+ const hostDeadline = Date.now() + spec.hostTimeoutSec * 1e3 - HOST_DEADLINE_MARGIN_MS;
1524
1537
  let ev = {};
1525
1538
  try {
1526
1539
  ev = JSON.parse(readFileSync4(0, "utf8"));
@@ -1550,6 +1563,7 @@ async function runRetainHook(spec, makeClient = (o) => new HindsightClient(o)) {
1550
1563
  transcriptPath,
1551
1564
  client,
1552
1565
  readTranscript: spec.readTranscript,
1566
+ retryUntil: hostDeadline,
1553
1567
  stamp: buildRetainStamp(cfg, {
1554
1568
  directory: cwd,
1555
1569
  harness: spec.harness,
@@ -1921,6 +1935,7 @@ var HOOK_HARNESSES = {
1921
1935
  sessionStart: standardSessionStart("claude-code"),
1922
1936
  prompt: claudePrompt,
1923
1937
  retain: {
1938
+ hostTimeoutSec: 60,
1924
1939
  harness: "claude-code",
1925
1940
  parse: (ev) => ({
1926
1941
  sessionId: ev.session_id,
@@ -1939,6 +1954,7 @@ var HOOK_HARNESSES = {
1939
1954
  sessionStart: standardSessionStart("codex"),
1940
1955
  prompt: codexPrompt,
1941
1956
  retain: {
1957
+ hostTimeoutSec: 60,
1942
1958
  harness: "codex",
1943
1959
  parse: (ev) => ({
1944
1960
  sessionId: ev.session_id,
@@ -1967,6 +1983,7 @@ var HOOK_HARNESSES = {
1967
1983
  },
1968
1984
  prompt: antigravityPrompt,
1969
1985
  retain: {
1986
+ hostTimeoutSec: 30,
1970
1987
  harness: "antigravity-cli",
1971
1988
  parse: (ev) => ({
1972
1989
  sessionId: ev.conversationId,
@@ -1995,6 +2012,7 @@ var HOOK_HARNESSES = {
1995
2012
  },
1996
2013
  prompt: cursorPrompt,
1997
2014
  retain: {
2015
+ hostTimeoutSec: 30,
1998
2016
  harness: "cursor-cli",
1999
2017
  parse: (ev) => ({
2000
2018
  sessionId: ev.conversation_id ?? ev.session_id,
@@ -2028,6 +2046,7 @@ var HOOK_HARNESSES = {
2028
2046
  },
2029
2047
  prompt: copilotPrompt,
2030
2048
  retain: {
2049
+ hostTimeoutSec: 60,
2031
2050
  harness: "copilot-cli",
2032
2051
  parse: (ev) => ({
2033
2052
  sessionId: ev.sessionId,
@@ -2056,6 +2075,7 @@ var HOOK_HARNESSES = {
2056
2075
  },
2057
2076
  prompt: devinPrompt,
2058
2077
  retain: {
2078
+ hostTimeoutSec: 60,
2059
2079
  harness: "devin-cli",
2060
2080
  parse: (ev) => {
2061
2081
  const sessionId = ev.session_id;
@@ -2095,6 +2115,7 @@ var HOOK_HARNESSES = {
2095
2115
  })
2096
2116
  },
2097
2117
  retain: {
2118
+ hostTimeoutSec: 60,
2098
2119
  harness: "grok-build",
2099
2120
  parse: (ev) => ({
2100
2121
  sessionId: ev.sessionId,
@@ -1729,6 +1729,7 @@ var HOOK_HARNESSES = {
1729
1729
  sessionStart: standardSessionStart("claude-code"),
1730
1730
  prompt: claudePrompt,
1731
1731
  retain: {
1732
+ hostTimeoutSec: 60,
1732
1733
  harness: "claude-code",
1733
1734
  parse: (ev) => ({
1734
1735
  sessionId: ev.session_id,
@@ -1747,6 +1748,7 @@ var HOOK_HARNESSES = {
1747
1748
  sessionStart: standardSessionStart("codex"),
1748
1749
  prompt: codexPrompt,
1749
1750
  retain: {
1751
+ hostTimeoutSec: 60,
1750
1752
  harness: "codex",
1751
1753
  parse: (ev) => ({
1752
1754
  sessionId: ev.session_id,
@@ -1775,6 +1777,7 @@ var HOOK_HARNESSES = {
1775
1777
  },
1776
1778
  prompt: antigravityPrompt,
1777
1779
  retain: {
1780
+ hostTimeoutSec: 30,
1778
1781
  harness: "antigravity-cli",
1779
1782
  parse: (ev) => ({
1780
1783
  sessionId: ev.conversationId,
@@ -1803,6 +1806,7 @@ var HOOK_HARNESSES = {
1803
1806
  },
1804
1807
  prompt: cursorPrompt,
1805
1808
  retain: {
1809
+ hostTimeoutSec: 30,
1806
1810
  harness: "cursor-cli",
1807
1811
  parse: (ev) => ({
1808
1812
  sessionId: ev.conversation_id ?? ev.session_id,
@@ -1836,6 +1840,7 @@ var HOOK_HARNESSES = {
1836
1840
  },
1837
1841
  prompt: copilotPrompt,
1838
1842
  retain: {
1843
+ hostTimeoutSec: 60,
1839
1844
  harness: "copilot-cli",
1840
1845
  parse: (ev) => ({
1841
1846
  sessionId: ev.sessionId,
@@ -1864,6 +1869,7 @@ var HOOK_HARNESSES = {
1864
1869
  },
1865
1870
  prompt: devinPrompt,
1866
1871
  retain: {
1872
+ hostTimeoutSec: 60,
1867
1873
  harness: "devin-cli",
1868
1874
  parse: (ev) => {
1869
1875
  const sessionId = ev.session_id;
@@ -1903,6 +1909,7 @@ var HOOK_HARNESSES = {
1903
1909
  })
1904
1910
  },
1905
1911
  retain: {
1912
+ hostTimeoutSec: 60,
1906
1913
  harness: "grok-build",
1907
1914
  parse: (ev) => ({
1908
1915
  sessionId: ev.sessionId,
@@ -2147,6 +2147,7 @@ var HOOK_HARNESSES = {
2147
2147
  sessionStart: standardSessionStart("claude-code"),
2148
2148
  prompt: claudePrompt,
2149
2149
  retain: {
2150
+ hostTimeoutSec: 60,
2150
2151
  harness: "claude-code",
2151
2152
  parse: (ev) => ({
2152
2153
  sessionId: ev.session_id,
@@ -2165,6 +2166,7 @@ var HOOK_HARNESSES = {
2165
2166
  sessionStart: standardSessionStart("codex"),
2166
2167
  prompt: codexPrompt,
2167
2168
  retain: {
2169
+ hostTimeoutSec: 60,
2168
2170
  harness: "codex",
2169
2171
  parse: (ev) => ({
2170
2172
  sessionId: ev.session_id,
@@ -2193,6 +2195,7 @@ var HOOK_HARNESSES = {
2193
2195
  },
2194
2196
  prompt: antigravityPrompt,
2195
2197
  retain: {
2198
+ hostTimeoutSec: 30,
2196
2199
  harness: "antigravity-cli",
2197
2200
  parse: (ev) => ({
2198
2201
  sessionId: ev.conversationId,
@@ -2221,6 +2224,7 @@ var HOOK_HARNESSES = {
2221
2224
  },
2222
2225
  prompt: cursorPrompt,
2223
2226
  retain: {
2227
+ hostTimeoutSec: 30,
2224
2228
  harness: "cursor-cli",
2225
2229
  parse: (ev) => ({
2226
2230
  sessionId: ev.conversation_id ?? ev.session_id,
@@ -2254,6 +2258,7 @@ var HOOK_HARNESSES = {
2254
2258
  },
2255
2259
  prompt: copilotPrompt,
2256
2260
  retain: {
2261
+ hostTimeoutSec: 60,
2257
2262
  harness: "copilot-cli",
2258
2263
  parse: (ev) => ({
2259
2264
  sessionId: ev.sessionId,
@@ -2282,6 +2287,7 @@ var HOOK_HARNESSES = {
2282
2287
  },
2283
2288
  prompt: devinPrompt,
2284
2289
  retain: {
2290
+ hostTimeoutSec: 60,
2285
2291
  harness: "devin-cli",
2286
2292
  parse: (ev) => {
2287
2293
  const sessionId = ev.session_id;
@@ -2321,6 +2327,7 @@ var HOOK_HARNESSES = {
2321
2327
  })
2322
2328
  },
2323
2329
  retain: {
2330
+ hostTimeoutSec: 60,
2324
2331
  harness: "grok-build",
2325
2332
  parse: (ev) => ({
2326
2333
  sessionId: ev.sessionId,
@@ -1088,10 +1088,10 @@ async function supportsAppend(client) {
1088
1088
  return false;
1089
1089
  }
1090
1090
  }
1091
+ var DEFAULT_RETRY_WINDOW_MS = 6e4;
1091
1092
  var RETAIN_RETRY_ATTEMPTS = 2;
1092
- var RETAIN_RETRY_BUDGET_MS = 6e3;
1093
- async function submitWithRetry(send, isStillNewest) {
1094
- let spent = 0;
1093
+ var REQUEST_BUDGET_MS = 15e3;
1094
+ async function submitWithRetry(send, isStillNewest, retryUntil) {
1095
1095
  for (let attempt = 0; ; attempt++) {
1096
1096
  try {
1097
1097
  return await send();
@@ -1099,8 +1099,7 @@ async function submitWithRetry(send, isStillNewest) {
1099
1099
  const limited = e instanceof RateLimitedError;
1100
1100
  if (!limited || attempt >= RETAIN_RETRY_ATTEMPTS || !isStillNewest()) throw e;
1101
1101
  const wait = e.retryAfterMs || 1e3;
1102
- if (wait > RETAIN_RETRY_BUDGET_MS - spent) throw e;
1103
- spent += wait;
1102
+ if (Date.now() + wait + REQUEST_BUDGET_MS > retryUntil) throw e;
1104
1103
  await sleep(wait);
1105
1104
  }
1106
1105
  }
@@ -1120,14 +1119,24 @@ function serialize(cursors, sessionId, write2) {
1120
1119
  }
1121
1120
  async function retainLiveSession(client, sessionId, turns, startTs, harness, opts = {}) {
1122
1121
  const cursors = opts.cursors;
1123
- if (!cursors) return writeSession(client, sessionId, turns, startTs, harness, opts.stamp);
1122
+ if (!cursors)
1123
+ return writeSession(
1124
+ client,
1125
+ sessionId,
1126
+ turns,
1127
+ startTs,
1128
+ harness,
1129
+ opts.stamp,
1130
+ void 0,
1131
+ opts.retryUntil
1132
+ );
1124
1133
  return serialize(
1125
1134
  cursors,
1126
1135
  sessionId,
1127
- () => writeSession(client, sessionId, turns, startTs, harness, opts.stamp, cursors)
1136
+ () => writeSession(client, sessionId, turns, startTs, harness, opts.stamp, cursors, opts.retryUntil)
1128
1137
  );
1129
1138
  }
1130
- async function writeSession(client, sessionId, turns, startTs, harness, stamp, cursors) {
1139
+ async function writeSession(client, sessionId, turns, startTs, harness, stamp, cursors, retryUntil = Date.now() + DEFAULT_RETRY_WINDOW_MS) {
1131
1140
  const refId = `conversation:${sessionId}`;
1132
1141
  const appendSupported = Boolean(cursors) && await supportsAppend(client);
1133
1142
  const plan = planRetain(turns, cursors?.read(sessionId), { appendSupported, bank: client.bank });
@@ -1179,7 +1188,8 @@ ${content}`),
1179
1188
  if (!cursors) return true;
1180
1189
  const now = cursors.read(sessionId);
1181
1190
  return now?.turns === claimed.turns && now?.fingerprint === claimed.fingerprint;
1182
- }
1191
+ },
1192
+ retryUntil
1183
1193
  );
1184
1194
  cursors?.write(sessionId, next);
1185
1195
  }
@@ -1495,6 +1505,7 @@ function readClaudeTranscript(path) {
1495
1505
  }
1496
1506
 
1497
1507
  // src/core/retain-hook.ts
1508
+ var HOST_DEADLINE_MARGIN_MS = 2e3;
1498
1509
  async function buildRetain(args) {
1499
1510
  const { harness, sessionId, transcriptPath, client } = args;
1500
1511
  const readTranscript = args.readTranscript ?? readClaudeTranscript;
@@ -1505,7 +1516,8 @@ async function buildRetain(args) {
1505
1516
  try {
1506
1517
  await retainLiveSession(client, sessionId, turns, startTs, harness, {
1507
1518
  cursors: args.cursors ?? fileCursorStore(harness),
1508
- stamp: args.stamp
1519
+ stamp: args.stamp,
1520
+ retryUntil: args.retryUntil
1509
1521
  });
1510
1522
  diag(harness, "retain_ok", { ms: Date.now() - t0, turns: turns.length, session: sessionId });
1511
1523
  } catch (e) {
@@ -1521,6 +1533,7 @@ async function buildRetain(args) {
1521
1533
  }
1522
1534
  async function runRetainHook(spec, makeClient = (o) => new HindsightClient(o)) {
1523
1535
  if (process.env.HINDSIGHT_DISABLE_HOOKS) return;
1536
+ const hostDeadline = Date.now() + spec.hostTimeoutSec * 1e3 - HOST_DEADLINE_MARGIN_MS;
1524
1537
  let ev = {};
1525
1538
  try {
1526
1539
  ev = JSON.parse(readFileSync4(0, "utf8"));
@@ -1550,6 +1563,7 @@ async function runRetainHook(spec, makeClient = (o) => new HindsightClient(o)) {
1550
1563
  transcriptPath,
1551
1564
  client,
1552
1565
  readTranscript: spec.readTranscript,
1566
+ retryUntil: hostDeadline,
1553
1567
  stamp: buildRetainStamp(cfg, {
1554
1568
  directory: cwd,
1555
1569
  harness: spec.harness,
@@ -1921,6 +1935,7 @@ var HOOK_HARNESSES = {
1921
1935
  sessionStart: standardSessionStart("claude-code"),
1922
1936
  prompt: claudePrompt,
1923
1937
  retain: {
1938
+ hostTimeoutSec: 60,
1924
1939
  harness: "claude-code",
1925
1940
  parse: (ev) => ({
1926
1941
  sessionId: ev.session_id,
@@ -1939,6 +1954,7 @@ var HOOK_HARNESSES = {
1939
1954
  sessionStart: standardSessionStart("codex"),
1940
1955
  prompt: codexPrompt,
1941
1956
  retain: {
1957
+ hostTimeoutSec: 60,
1942
1958
  harness: "codex",
1943
1959
  parse: (ev) => ({
1944
1960
  sessionId: ev.session_id,
@@ -1967,6 +1983,7 @@ var HOOK_HARNESSES = {
1967
1983
  },
1968
1984
  prompt: antigravityPrompt,
1969
1985
  retain: {
1986
+ hostTimeoutSec: 30,
1970
1987
  harness: "antigravity-cli",
1971
1988
  parse: (ev) => ({
1972
1989
  sessionId: ev.conversationId,
@@ -1995,6 +2012,7 @@ var HOOK_HARNESSES = {
1995
2012
  },
1996
2013
  prompt: cursorPrompt,
1997
2014
  retain: {
2015
+ hostTimeoutSec: 30,
1998
2016
  harness: "cursor-cli",
1999
2017
  parse: (ev) => ({
2000
2018
  sessionId: ev.conversation_id ?? ev.session_id,
@@ -2028,6 +2046,7 @@ var HOOK_HARNESSES = {
2028
2046
  },
2029
2047
  prompt: copilotPrompt,
2030
2048
  retain: {
2049
+ hostTimeoutSec: 60,
2031
2050
  harness: "copilot-cli",
2032
2051
  parse: (ev) => ({
2033
2052
  sessionId: ev.sessionId,
@@ -2056,6 +2075,7 @@ var HOOK_HARNESSES = {
2056
2075
  },
2057
2076
  prompt: devinPrompt,
2058
2077
  retain: {
2078
+ hostTimeoutSec: 60,
2059
2079
  harness: "devin-cli",
2060
2080
  parse: (ev) => {
2061
2081
  const sessionId = ev.session_id;
@@ -2095,6 +2115,7 @@ var HOOK_HARNESSES = {
2095
2115
  })
2096
2116
  },
2097
2117
  retain: {
2118
+ hostTimeoutSec: 60,
2098
2119
  harness: "grok-build",
2099
2120
  parse: (ev) => ({
2100
2121
  sessionId: ev.sessionId,