Draft attachments across Session switches
草稿附件跨会话切换
Switching to another Session and back emptied the composer's attachment rail while its text survived (issue #5070). The Session scope disposer released every attachment the retiring composer held and revoked image previews, and nothing could rebuild them.
Source compatibility: Chinese title uses a compatible non-canonical header
English
Problem
Switching to another Session and back emptied the composer's attachment rail while its text survived (issue #5070). The Session scope disposer released every attachment the retiring composer held and revoked image previews, and nothing could rebuild them.
Decision
A draft with attachments holds its Session's scope. While a composer draft has attachments and its Session is not removed, the input hub holds the Session with a draft reference that keeps no event connection, as Session retain facets and recently viewed Sessions defines. Switching away and evicting the Session from the recent views therefore keep the same composer shell and its attachments: an active upload keeps running, a failed upload keeps its in-place retry, and a pasted image keeps its bytes in memory. Removing the last attachment, sending it, or removing the Session releases the hold; scope teardown then releases every attachment the draft still holds.
A reload reports discarded attachments instead of restoring them. On pagehide, the input hub marks each Session whose draft holds attachments in browser storage; a back-forward cache restore clears the marks. A composer mounted with a mark shows the "add them again" notice.
Alternatives considered
Restoring attachments after a reload from durable browser references and a Host restore Remote. It would bring uploaded files and images back after a page reload, but it needs a Host Remote with its Liveshare and catalog classifications, a persisted browser format, a send gate during restoration, and an image byte payload. Desktop, where most users work, gains little: its Host binds a new port on every launch, so its storage origin, and with it every saved reference, does not survive an application restart.
Recording draft-attachment owners in the Conversation service and re-adopting them in the next composer. It keeps attachments without holding the scope, but it tracks Session lifetime a second time through a catalog watch and splits submission release across composer phases; holding the scope keeps the composer itself.
Releasing settled attachments on a switch and restoring them from the Host on return. It would save the memory of pasted images while a Session is not shown, but every return needs a Host round trip, fails on a weak network, and allocates the same bytes again.
Consequences
Switching never touches the Host, so a weak network cannot lose a draft attachment there. A reload or crash still discards attachments; a crash writes no mark and so shows no notice.
中文
问题
切到另一个会话再切回时,输入框的附件栏被清空,文字却还在(issue #5070)。会话 scope 销毁时会释放退役输入框持有的全部附件并回收图片预览,之后没有任何途径能重建它们。
决策
带附件的草稿持有其会话的作用域。 输入框草稿有附件且其会话未被删除时,输入 hub 以不保留事件连接的 draft 引用持有该会话,见 Session retain 子要素与最近访问会话。因此切走以及从最近访问会话中被淘汰时,同一个输入框 shell 和其中的附件都会保留:进行中的上传继续进行,失败的上传保留原位重试,粘贴的图片把字节保留在内存中。移除最后一个附件、发送它或删除会话会释放这条引用;随后作用域销毁时释放草稿仍持有的全部附件。
刷新时提示被丢弃的附件,而不恢复它们。 pagehide 时,输入 hub 在浏览器存储中为每个草稿持有附件的会话写一个标记;往返缓存恢复页面时清除这些标记。挂载时有标记的输入框会显示「请重新添加」提示。
考虑过的替代方案
刷新后通过浏览器 durable 引用与 Host restore Remote 恢复附件。 它能在页面刷新后找回已上传的文件和图片,但需要新增一个 Host Remote 及其 Liveshare 与目录分类、一种浏览器持久化格式、恢复期间的发送拦截和图片字节载荷。主要用户所在的桌面端收益很小:桌面端 Host 每次启动都绑定新端口,存储 origin 连同所有已存引用都无法跨应用重启保留。
在 Conversation 服务中记录草稿附件归属,并在下一个输入框中接回。 它不持有作用域也能保留附件,但要通过会话目录监听再跟踪一遍 Session 生命周期,并按输入框阶段拆分已提交附件的释放;持有作用域就能保住输入框本身。
切换时释放已结算附件,切回时由 Host 恢复。 它能在会话不显示期间省下粘贴图片的内存,但每次切回都需要一次 Host 往返,弱网下会失败,并且之后还要再分配同样的字节。
影响
切换会话从不访问 Host,因此弱网不会让草稿附件在切换中丢失。刷新或崩溃仍会丢弃附件;崩溃时不会写标记,因此没有提示。