@@ -1149,6 +1149,139 @@ describe("clawhub helpers", () => {
11491149 expect ( stalled . cancel . mock . calls [ 0 ] ?. [ 0 ] ) . toBeInstanceOf ( Error ) ;
11501150 } ) ;
11511151
1152+ it ( "keeps the ClawHub request deadline through stalled bodies after headers" , async ( ) => {
1153+ await expect (
1154+ searchClawHubSkills ( {
1155+ query : "calendar" ,
1156+ timeoutMs : 50 ,
1157+ fetchImpl : async ( _input , init ) => {
1158+ const signal = init ?. signal ;
1159+ if ( ! signal ) {
1160+ throw new Error ( "missing ClawHub request signal" ) ;
1161+ }
1162+ return new Response (
1163+ new ReadableStream ( {
1164+ start ( controller ) {
1165+ signal . addEventListener (
1166+ "abort" ,
1167+ ( ) => {
1168+ controller . error (
1169+ signal . reason instanceof Error
1170+ ? signal . reason
1171+ : new Error ( "ClawHub request aborted" ) ,
1172+ ) ;
1173+ } ,
1174+ { once : true } ,
1175+ ) ;
1176+ } ,
1177+ } ) ,
1178+ {
1179+ status : 200 ,
1180+ headers : { "content-type" : "application/json" } ,
1181+ } ,
1182+ ) ;
1183+ } ,
1184+ } ) ,
1185+ ) . rejects . toThrow ( / C l a w H u b r e q u e s t t i m e d o u t a f t e r 5 0 m s / ) ;
1186+ } ) ;
1187+
1188+ it ( "aborts ClawHub JSON bodies that drip past the request deadline" , async ( ) => {
1189+ await expect (
1190+ searchClawHubSkills ( {
1191+ query : "calendar" ,
1192+ timeoutMs : 50 ,
1193+ fetchImpl : async ( _input , init ) => {
1194+ const signal = init ?. signal ;
1195+ if ( ! signal ) {
1196+ throw new Error ( "missing ClawHub request signal" ) ;
1197+ }
1198+ return new Response (
1199+ new ReadableStream ( {
1200+ start ( controller ) {
1201+ const encoder = new TextEncoder ( ) ;
1202+ let dripTimer : ReturnType < typeof setTimeout > | undefined ;
1203+ const stop = ( ) => {
1204+ if ( dripTimer ) {
1205+ clearTimeout ( dripTimer ) ;
1206+ dripTimer = undefined ;
1207+ }
1208+ } ;
1209+ const drip = ( ) => {
1210+ if ( signal . aborted ) {
1211+ stop ( ) ;
1212+ return ;
1213+ }
1214+ // Chunks keep arriving so per-chunk idle alone cannot bound the read.
1215+ controller . enqueue ( encoder . encode ( "x" ) ) ;
1216+ dripTimer = setTimeout ( drip , 10 ) ;
1217+ } ;
1218+ signal . addEventListener (
1219+ "abort" ,
1220+ ( ) => {
1221+ stop ( ) ;
1222+ try {
1223+ controller . error (
1224+ signal . reason instanceof Error
1225+ ? signal . reason
1226+ : new Error ( "ClawHub request aborted" ) ,
1227+ ) ;
1228+ } catch {
1229+ // already closed by a racing drip/error
1230+ }
1231+ } ,
1232+ { once : true } ,
1233+ ) ;
1234+ drip ( ) ;
1235+ } ,
1236+ } ) ,
1237+ {
1238+ status : 200 ,
1239+ headers : { "content-type" : "application/json" } ,
1240+ } ,
1241+ ) ;
1242+ } ,
1243+ } ) ,
1244+ ) . rejects . toThrow ( / C l a w H u b r e q u e s t t i m e d o u t a f t e r 5 0 m s / ) ;
1245+ } ) ;
1246+
1247+ it ( "does not bound dripping ClawHub bodies when the request deadline ends at headers" , async ( ) => {
1248+ // Negative control: the pre-fix pattern cleared the AbortController timer as soon as
1249+ // fetch returned headers, so a slow drip that resets chunk-idle could hang forever.
1250+ vi . useFakeTimers ( ) ;
1251+ try {
1252+ const controller = new AbortController ( ) ;
1253+ const timeoutMs = 50 ;
1254+ const timeout = setTimeout (
1255+ ( ) => controller . abort ( new Error ( `ClawHub request timed out after ${ timeoutMs } ms` ) ) ,
1256+ timeoutMs ,
1257+ ) ;
1258+ // Simulate clawhubRequest clearing the deadline at headers.
1259+ clearTimeout ( timeout ) ;
1260+
1261+ let chunks = 0 ;
1262+ const body = new ReadableStream < Uint8Array > ( {
1263+ start ( streamController ) {
1264+ const encoder = new TextEncoder ( ) ;
1265+ const drip = ( ) => {
1266+ chunks += 1 ;
1267+ streamController . enqueue ( encoder . encode ( "x" ) ) ;
1268+ setTimeout ( drip , 10 ) ;
1269+ } ;
1270+ drip ( ) ;
1271+ } ,
1272+ } ) ;
1273+ const reader = body . getReader ( ) ;
1274+ await vi . advanceTimersByTimeAsync ( 10 ) ;
1275+ await reader . read ( ) ;
1276+ await vi . advanceTimersByTimeAsync ( 2000 ) ;
1277+ expect ( controller . signal . aborted ) . toBe ( false ) ;
1278+ expect ( chunks ) . toBeGreaterThan ( 5 ) ;
1279+ await reader . cancel ( ) ;
1280+ } finally {
1281+ vi . useRealTimers ( ) ;
1282+ }
1283+ } ) ;
1284+
11521285 it ( "times out and cancels stalled ClawHub error bodies" , async ( ) => {
11531286 const stalledResponses : ReturnType < typeof createStalledBodyResponse > [ ] = [ ] ;
11541287
@@ -1354,6 +1487,130 @@ describe("clawhub helpers", () => {
13541487 expect ( stalled . cancel . mock . calls [ 0 ] ?. [ 0 ] ) . toBeInstanceOf ( Error ) ;
13551488 } ) ;
13561489
1490+ it ( "lets a progressing archive body survive past the request deadline" , async ( ) => {
1491+ // Archive downloads release the metadata wall-clock after headers so a
1492+ // transfer that keeps receiving bytes is not cut off at the request budget.
1493+ // Per-chunk idle detection in readClawHubResponseBytes still bounds stalls.
1494+ vi . useFakeTimers ( ) ;
1495+ try {
1496+ let abortCount = 0 ;
1497+ let chunks = 0 ;
1498+ const archive = downloadClawHubSkillArchive ( {
1499+ slug : "large-skill" ,
1500+ version : "1.0.0" ,
1501+ timeoutMs : 50 ,
1502+ fetchImpl : async ( _input , init ) => {
1503+ const signal = init ?. signal ;
1504+ signal ?. addEventListener (
1505+ "abort" ,
1506+ ( ) => {
1507+ abortCount += 1 ;
1508+ } ,
1509+ { once : true } ,
1510+ ) ;
1511+ return new Response (
1512+ new ReadableStream < Uint8Array > ( {
1513+ start ( controller ) {
1514+ const encoder = new TextEncoder ( ) ;
1515+ const drip = ( ) => {
1516+ chunks += 1 ;
1517+ controller . enqueue ( encoder . encode ( "x" ) ) ;
1518+ if ( chunks >= 10 ) {
1519+ controller . close ( ) ;
1520+ return ;
1521+ }
1522+ setTimeout ( drip , 10 ) ;
1523+ } ;
1524+ drip ( ) ;
1525+ } ,
1526+ } ) ,
1527+ { status : 200 , headers : { "content-type" : "application/zip" } } ,
1528+ ) ;
1529+ } ,
1530+ } ) ;
1531+
1532+ await vi . advanceTimersByTimeAsync ( 200 ) ;
1533+ const result = await archive ;
1534+ expect ( abortCount ) . toBe ( 0 ) ;
1535+ expect ( chunks ) . toBe ( 10 ) ;
1536+ expect ( result . sha256Hex ) . toBeTruthy ( ) ;
1537+ await result . cleanup ( ) ;
1538+ } finally {
1539+ vi . useRealTimers ( ) ;
1540+ }
1541+ } ) ;
1542+
1543+ it ( "keeps archive error bodies under the request wall-clock deadline" , async ( ) => {
1544+ // Non-2xx archive responses must stay bounded by the wall-clock instead of
1545+ // releasing the deadline at headers. A continuously dripping 500 body must
1546+ // be cut off within the request budget.
1547+ const startedAt = performance . now ( ) ;
1548+ await expect (
1549+ downloadClawHubSkillArchive ( {
1550+ slug : "broken-skill" ,
1551+ version : "1.0.0" ,
1552+ timeoutMs : 50 ,
1553+ fetchImpl : async ( _input , init ) => {
1554+ const signal = init ?. signal ;
1555+ if ( ! signal ) {
1556+ throw new Error ( "missing ClawHub request signal" ) ;
1557+ }
1558+ return new Response (
1559+ new ReadableStream ( {
1560+ start ( controller ) {
1561+ const encoder = new TextEncoder ( ) ;
1562+ let dripTimer : ReturnType < typeof setTimeout > | undefined ;
1563+ const stop = ( ) => {
1564+ if ( dripTimer ) {
1565+ clearTimeout ( dripTimer ) ;
1566+ dripTimer = undefined ;
1567+ }
1568+ } ;
1569+ const drip = ( ) => {
1570+ if ( signal . aborted ) {
1571+ stop ( ) ;
1572+ return ;
1573+ }
1574+ try {
1575+ controller . enqueue ( encoder . encode ( "x" ) ) ;
1576+ } catch {
1577+ stop ( ) ;
1578+ return ;
1579+ }
1580+ dripTimer = setTimeout ( drip , 10 ) ;
1581+ } ;
1582+ signal . addEventListener (
1583+ "abort" ,
1584+ ( ) => {
1585+ stop ( ) ;
1586+ try {
1587+ controller . error (
1588+ signal . reason instanceof Error
1589+ ? signal . reason
1590+ : new Error ( "ClawHub request aborted" ) ,
1591+ ) ;
1592+ } catch {
1593+ // already closed by a racing drip/error
1594+ }
1595+ } ,
1596+ { once : true } ,
1597+ ) ;
1598+ drip ( ) ;
1599+ } ,
1600+ } ) ,
1601+ {
1602+ status : 400 ,
1603+ statusText : "Bad Request" ,
1604+ headers : { "content-type" : "text/plain" } ,
1605+ } ,
1606+ ) ;
1607+ } ,
1608+ } ) ,
1609+ ) . rejects . toThrow ( "ClawHub /api/v1/download failed (400)" ) ;
1610+ const elapsedMs = performance . now ( ) - startedAt ;
1611+ expect ( elapsedMs ) . toBeLessThan ( 200 ) ;
1612+ } ) ;
1613+
13571614 it ( "downloads skill archives to sanitized temp paths and cleans them up" , async ( ) => {
13581615 const archive = await downloadClawHubSkillArchive ( {
13591616 slug : "agentreceipt" ,
0 commit comments