@@ -4543,7 +4543,10 @@ describe("chat directive tag stripping for non-streaming final payloads", () =>
45434543 expect ( mockState . lastDispatchCtx ?. MediaStaged ) . toBe ( true ) ;
45444544 } ) ;
45454545
4546- it ( "wraps stageSandboxMedia infrastructure errors as 5xx UNAVAILABLE and cleans up media-store files" , async ( ) => {
4546+ it ( "wraps stageSandboxMedia infrastructure errors as 5xx UNAVAILABLE for non-fallback refs and cleans up media-store files" , async ( ) => {
4547+ // A non-PDF managed offload cannot fall back to a managed path, so an infra
4548+ // staging error stays a retryable 5xx. (Managed PDFs fall back instead — see
4549+ // the staging-throw fallback test below.) #90097
45474550 createTranscriptFixture ( "openclaw-chat-send-stage-unavailable-" ) ;
45484551 mockState . finalText = "ok" ;
45494552 mockState . sessionEntry = {
@@ -4559,7 +4562,10 @@ describe("chat directive tag stripping for non-streaming final payloads", () =>
45594562 } ,
45604563 ] ;
45614564 mockState . savedMediaResults = [
4562- { path : "/home/user/.openclaw/media/inbound/report.pdf" , contentType : "application/pdf" } ,
4565+ {
4566+ path : "/home/user/.openclaw/media/inbound/report.bin" ,
4567+ contentType : "application/octet-stream" ,
4568+ } ,
45634569 ] ;
45644570 mockState . sandboxWorkspace = { workspaceDir : "/sandbox/workspace" } ;
45654571 const stageError = Object . assign ( new Error ( "ENOSPC: no space left on device" ) , {
@@ -4570,7 +4576,7 @@ describe("chat directive tag stripping for non-streaming final payloads", () =>
45704576 mockState . stageSandboxMediaError = stageError ;
45714577 const respond = vi . fn ( ) ;
45724578 const context = createChatContext ( ) ;
4573- const pdf = Buffer . from ( "%PDF-1.4\n%µ¶\n1 0 obj\n<<>>\nendobj \n" ) . toString ( "base64" ) ;
4579+ const binPayload = Buffer . from ( "OPENCLAW-BINARY \n" ) . toString ( "base64" ) ;
45744580
45754581 await runNonStreamingChatSend ( {
45764582 context,
@@ -4581,9 +4587,9 @@ describe("chat directive tag stripping for non-streaming final payloads", () =>
45814587 attachments : [
45824588 {
45834589 type : "file" ,
4584- mimeType : "application/pdf " ,
4585- fileName : "report.pdf " ,
4586- content : pdf ,
4590+ mimeType : "application/octet-stream " ,
4591+ fileName : "report.bin " ,
4592+ content : binPayload ,
45874593 } ,
45884594 ] ,
45894595 } ,
@@ -4665,7 +4671,9 @@ describe("chat directive tag stripping for non-streaming final payloads", () =>
46654671 // absolute path, so a simple `stagedPaths.length === nonImage.length`
46664672 // check could not detect when one of the files silently fell out (e.g. a
46674673 // file between the RPC cap and the staging cap). Prestage must compare
4668- // the returned `staged` map against the input refs.
4674+ // the returned `staged` map against the input refs. Non-PDF refs cannot fall
4675+ // back to a managed path, so an incomplete stage stays a 5xx. (Managed PDFs
4676+ // fall back instead — see the staging-skip fallback test below.) #90097
46694677 createTranscriptFixture ( "openclaw-chat-send-partial-stage-" ) ;
46704678 mockState . finalText = "ok" ;
46714679 mockState . sessionEntry = {
@@ -4681,15 +4689,21 @@ describe("chat directive tag stripping for non-streaming final payloads", () =>
46814689 } ,
46824690 ] ;
46834691 mockState . savedMediaResults = [
4684- { path : "/home/user/.openclaw/media/inbound/report.pdf" , contentType : "application/pdf" } ,
4685- { path : "/home/user/.openclaw/media/inbound/oversize.pdf" , contentType : "application/pdf" } ,
4692+ {
4693+ path : "/home/user/.openclaw/media/inbound/report.bin" ,
4694+ contentType : "application/octet-stream" ,
4695+ } ,
4696+ {
4697+ path : "/home/user/.openclaw/media/inbound/data.bin" ,
4698+ contentType : "application/octet-stream" ,
4699+ } ,
46864700 ] ;
46874701 mockState . sandboxWorkspace = { workspaceDir : "/sandbox/workspace" } ;
4688- mockState . stagedRelativePaths = [ "media/inbound/report.pdf " , "media/inbound/oversize.pdf " ] ;
4689- mockState . unstagedSources = [ "/home/user/.openclaw/media/inbound/oversize.pdf " ] ;
4702+ mockState . stagedRelativePaths = [ "media/inbound/report.bin " , "media/inbound/data.bin " ] ;
4703+ mockState . unstagedSources = [ "/home/user/.openclaw/media/inbound/data.bin " ] ;
46904704 const respond = vi . fn ( ) ;
46914705 const context = createChatContext ( ) ;
4692- const pdf = Buffer . from ( "%PDF-1.4 \n" ) . toString ( "base64" ) ;
4706+ const binPayload = Buffer . from ( "OPENCLAW-BINARY \n" ) . toString ( "base64" ) ;
46934707
46944708 await runNonStreamingChatSend ( {
46954709 context,
@@ -4698,8 +4712,18 @@ describe("chat directive tag stripping for non-streaming final payloads", () =>
46984712 message : "read these" ,
46994713 requestParams : {
47004714 attachments : [
4701- { type : "file" , mimeType : "application/pdf" , fileName : "report.pdf" , content : pdf } ,
4702- { type : "file" , mimeType : "application/pdf" , fileName : "oversize.pdf" , content : pdf } ,
4715+ {
4716+ type : "file" ,
4717+ mimeType : "application/octet-stream" ,
4718+ fileName : "report.bin" ,
4719+ content : binPayload ,
4720+ } ,
4721+ {
4722+ type : "file" ,
4723+ mimeType : "application/octet-stream" ,
4724+ fileName : "data.bin" ,
4725+ content : binPayload ,
4726+ } ,
47034727 ] ,
47044728 } ,
47054729 expectBroadcast : false ,
@@ -4781,6 +4805,188 @@ describe("chat directive tag stripping for non-streaming final payloads", () =>
47814805 expect ( mockState . deleteMediaBufferCalls ) . toEqual ( [ ] ) ;
47824806 } ) ;
47834807
4808+ it ( "falls back to the managed path when sandbox staging throws for an already-managed PDF" , async ( ) => {
4809+ // #90097: an already-managed inbound PDF below the staging cap normally
4810+ // stages into the sandbox, but if staging throws (e.g. workspace mkdir
4811+ // ENOSPC) the PDF must still reach the agent via its managed media path
4812+ // instead of failing the send — host-side media-understanding reads it from
4813+ // the media-store root.
4814+ createTranscriptFixture ( "openclaw-chat-send-managed-pdf-stage-throw-" ) ;
4815+ mockState . finalText = "ok" ;
4816+ mockState . sessionEntry = {
4817+ modelProvider : "test-provider" ,
4818+ model : "vision-model" ,
4819+ } ;
4820+ mockState . modelCatalog = [
4821+ {
4822+ provider : "test-provider" ,
4823+ id : "vision-model" ,
4824+ name : "Vision model" ,
4825+ input : [ "text" , "image" ] ,
4826+ } ,
4827+ ] ;
4828+ mockState . savedMediaResults = [
4829+ { path : "/home/user/.openclaw/media/inbound/report.pdf" , contentType : "application/pdf" } ,
4830+ ] ;
4831+ mockState . sandboxWorkspace = { workspaceDir : "/sandbox/workspace" } ;
4832+ mockState . stageSandboxMediaError = Object . assign ( new Error ( "ENOSPC: no space left on device" ) , {
4833+ code : "ENOSPC" ,
4834+ } ) ;
4835+ const respond = vi . fn ( ) ;
4836+ const context = createChatContext ( ) ;
4837+ // Small PDF (below the 5MB staging cap) so it takes the staging path, not the
4838+ // oversized pass-through path.
4839+ const pdf = Buffer . from ( "%PDF-1.4\n%µ¶\nendobj\n" ) . toString ( "base64" ) ;
4840+
4841+ await runNonStreamingChatSend ( {
4842+ context,
4843+ respond,
4844+ idempotencyKey : "idem-managed-pdf-stage-throw" ,
4845+ message : "read this" ,
4846+ requestParams : {
4847+ attachments : [
4848+ { type : "file" , mimeType : "application/pdf" , fileName : "report.pdf" , content : pdf } ,
4849+ ] ,
4850+ } ,
4851+ expectBroadcast : false ,
4852+ } ) ;
4853+
4854+ // Falls back to the absolute managed path; nothing staged (so no workspace
4855+ // dir) and the media-store entry is preserved for host-side extraction.
4856+ expect ( mockState . lastDispatchCtx ?. MediaPath ) . toBe (
4857+ "/home/user/.openclaw/media/inbound/report.pdf" ,
4858+ ) ;
4859+ expect ( mockState . lastDispatchCtx ?. MediaPaths ) . toEqual ( [
4860+ "/home/user/.openclaw/media/inbound/report.pdf" ,
4861+ ] ) ;
4862+ expect ( mockState . lastDispatchCtx ?. MediaType ) . toBe ( "application/pdf" ) ;
4863+ expect ( mockState . lastDispatchCtx ?. MediaWorkspaceDir ) . toBeUndefined ( ) ;
4864+ expect ( mockState . lastDispatchCtx ?. MediaStaged ) . toBe ( true ) ;
4865+ expect ( mockState . deleteMediaBufferCalls ) . toEqual ( [ ] ) ;
4866+ } ) ;
4867+
4868+ it ( "falls back to the managed path when sandbox staging silently skips an already-managed PDF" , async ( ) => {
4869+ // #90097: stageSandboxMedia can silently skip a file (keeping its absolute
4870+ // path) and return it absent from the staged map. An already-managed PDF in
4871+ // that state falls back to its managed media path rather than failing the
4872+ // send; the staged workspace dir is still carried for any files that landed.
4873+ createTranscriptFixture ( "openclaw-chat-send-managed-pdf-stage-skip-" ) ;
4874+ mockState . finalText = "ok" ;
4875+ mockState . sessionEntry = {
4876+ modelProvider : "test-provider" ,
4877+ model : "vision-model" ,
4878+ } ;
4879+ mockState . modelCatalog = [
4880+ {
4881+ provider : "test-provider" ,
4882+ id : "vision-model" ,
4883+ name : "Vision model" ,
4884+ input : [ "text" , "image" ] ,
4885+ } ,
4886+ ] ;
4887+ mockState . savedMediaResults = [
4888+ { path : "/home/user/.openclaw/media/inbound/report.pdf" , contentType : "application/pdf" } ,
4889+ ] ;
4890+ mockState . sandboxWorkspace = { workspaceDir : "/sandbox/workspace" } ;
4891+ // No stagedRelativePaths → staged map is empty and ctx.MediaPaths keeps the
4892+ // absolute path, mirroring stageSandboxMedia silently skipping the file.
4893+ const respond = vi . fn ( ) ;
4894+ const context = createChatContext ( ) ;
4895+ const pdf = Buffer . from ( "%PDF-1.4\n%µ¶\nendobj\n" ) . toString ( "base64" ) ;
4896+
4897+ await runNonStreamingChatSend ( {
4898+ context,
4899+ respond,
4900+ idempotencyKey : "idem-managed-pdf-stage-skip" ,
4901+ message : "read this" ,
4902+ requestParams : {
4903+ attachments : [
4904+ { type : "file" , mimeType : "application/pdf" , fileName : "report.pdf" , content : pdf } ,
4905+ ] ,
4906+ } ,
4907+ expectBroadcast : false ,
4908+ } ) ;
4909+
4910+ expect ( mockState . lastDispatchCtx ?. MediaPath ) . toBe (
4911+ "/home/user/.openclaw/media/inbound/report.pdf" ,
4912+ ) ;
4913+ expect ( mockState . lastDispatchCtx ?. MediaPaths ) . toEqual ( [
4914+ "/home/user/.openclaw/media/inbound/report.pdf" ,
4915+ ] ) ;
4916+ expect ( mockState . lastDispatchCtx ?. MediaType ) . toBe ( "application/pdf" ) ;
4917+ expect ( mockState . lastDispatchCtx ?. MediaWorkspaceDir ) . toBe ( "/sandbox/workspace" ) ;
4918+ expect ( mockState . lastDispatchCtx ?. MediaStaged ) . toBe ( true ) ;
4919+ expect ( mockState . deleteMediaBufferCalls ) . toEqual ( [ ] ) ;
4920+ } ) ;
4921+
4922+ it ( "still fails the send when staging skips a non-PDF in a mixed managed batch" , async ( ) => {
4923+ // #90097: the PDF fallback is per-ref. A managed PDF that stages does not
4924+ // rescue a sibling non-PDF that silently fell out of staging; that batch must
4925+ // still surface a retryable 5xx and clean up every offloaded entry.
4926+ createTranscriptFixture ( "openclaw-chat-send-mixed-stage-skip-" ) ;
4927+ mockState . finalText = "ok" ;
4928+ mockState . sessionEntry = {
4929+ modelProvider : "test-provider" ,
4930+ model : "vision-model" ,
4931+ } ;
4932+ mockState . modelCatalog = [
4933+ {
4934+ provider : "test-provider" ,
4935+ id : "vision-model" ,
4936+ name : "Vision model" ,
4937+ input : [ "text" , "image" ] ,
4938+ } ,
4939+ ] ;
4940+ mockState . savedMediaResults = [
4941+ { path : "/home/user/.openclaw/media/inbound/report.pdf" , contentType : "application/pdf" } ,
4942+ {
4943+ path : "/home/user/.openclaw/media/inbound/data.bin" ,
4944+ contentType : "application/octet-stream" ,
4945+ } ,
4946+ ] ;
4947+ mockState . sandboxWorkspace = { workspaceDir : "/sandbox/workspace" } ;
4948+ mockState . stagedRelativePaths = [ "media/inbound/report.pdf" , "media/inbound/data.bin" ] ;
4949+ mockState . unstagedSources = [ "/home/user/.openclaw/media/inbound/data.bin" ] ;
4950+ const respond = vi . fn ( ) ;
4951+ const context = createChatContext ( ) ;
4952+ const pdf = Buffer . from ( "%PDF-1.4\n" ) . toString ( "base64" ) ;
4953+ const bin = Buffer . from ( "OPENCLAW-BINARY\n" ) . toString ( "base64" ) ;
4954+
4955+ await runNonStreamingChatSend ( {
4956+ context,
4957+ respond,
4958+ idempotencyKey : "idem-mixed-stage-skip" ,
4959+ message : "read these" ,
4960+ requestParams : {
4961+ attachments : [
4962+ { type : "file" , mimeType : "application/pdf" , fileName : "report.pdf" , content : pdf } ,
4963+ {
4964+ type : "file" ,
4965+ mimeType : "application/octet-stream" ,
4966+ fileName : "data.bin" ,
4967+ content : bin ,
4968+ } ,
4969+ ] ,
4970+ } ,
4971+ expectBroadcast : false ,
4972+ waitFor : "none" ,
4973+ } ) ;
4974+
4975+ expect ( mockState . lastDispatchCtx ) . toBeUndefined ( ) ;
4976+ expect ( respond ) . toHaveBeenCalledTimes ( 1 ) ;
4977+ const [ ok , payload , error ] = lastRespondCall ( respond ) ?? [ ] ;
4978+ expect ( ok ) . toBe ( false ) ;
4979+ expect ( payload ) . toBeUndefined ( ) ;
4980+ expect ( error ?. code ) . toBe ( ErrorCodes . UNAVAILABLE ) ;
4981+ expect ( responseErrorMessage ( error ) ) . toMatch ( / s t a g i n g i n c o m p l e t e / i) ;
4982+ // The whole batch is cleaned up — including the PDF that would have fallen
4983+ // back on its own — because the non-PDF cannot be delivered.
4984+ expect ( mockState . deleteMediaBufferCalls . map ( ( c ) => c . id ) . toSorted ( ) ) . toEqual ( [
4985+ "saved-media" ,
4986+ "saved-media" ,
4987+ ] ) ;
4988+ } ) ;
4989+
47844990 it ( "rejects sandbox-oversized non-image attachments as 4xx before staging" , async ( ) => {
47854991 // Regression: resolveChatAttachmentMaxBytes defaults to 20MB, but
47864992 // stageSandboxMedia caps each file at STAGED_MEDIA_MAX_BYTES (5MB) and
0 commit comments