Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion pkgs/id/src/fileops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,17 @@ use thiserror::Error;

use crate::tags::{TagStore, now_unix};

const ARCHIVE_IDENTITY_TAG_KEYS: [&[u8]; 3] = [b"name", b"file", b"path"];
const ARCHIVE_IDENTITY_TAG_KEYS: [&[u8]; 5] = [
b"name",
b"file",
b"path",
// Archive references describe the live file's archives, so they
// stay on the live name when its other metadata moves onto an
// archive. Letting one travel onto the archive subject orphans
// every earlier archive from the live file's delete (#4574).
b"archive.rename",
b"archive.replace",
];

/// Errors from file operations, distinguishable so callers can map them to
/// protocol errors or HTTP status codes.
Expand Down Expand Up @@ -241,6 +251,24 @@ impl<'a> FileOps<'a> {
let Some(existing) = self.store.tags().get(name).await? else {
return Ok(None);
};
// Archive references belong to the live file: they name the
// archives a future delete of this file must also remove. The
// transfer below discards them from this name (and the
// exclusion list keeps them off the archive), so snapshot them
// here and restore them onto the live name afterwards -
// otherwise each replacement orphans every earlier archive.
let mut archive_refs = Vec::new();
for key in [&b"archive.rename"[..], &b"archive.replace"[..]] {
for tag in self
.tags
.get_by_key(&self.tags.global, name.as_bytes(), key)
.await?
{
if let Some(value) = tag.value.as_ref().and_then(|value| value.as_str()) {
archive_refs.push((key, value.to_owned()));
}
}
}
let archive = claim_archive_name(self.store, name, ts, existing.hash)
.await
.with_context(|| format!("archiving replaced file {name}"))?;
Expand All @@ -257,6 +285,18 @@ impl<'a> FileOps<'a> {
.refresh_identity_tags(&self.tags.global, archive.as_bytes())
.await
.with_context(|| format!("refreshing identity for archive {archive}"))?;
for (key, value) in &archive_refs {
self.tags
.set_tag(
&self.tags.global,
name.as_bytes(),
key,
Some(value.as_bytes()),
b"",
)
.await
.with_context(|| format!("restoring archive references for {name}"))?;
}
Ok(Some(archive))
}

Expand Down
8 changes: 7 additions & 1 deletion pkgs/id/src/web/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2312,6 +2312,10 @@ fn protect_newline_destinations(markdown: &str) -> (String, Option<String>) {
// long as the opener — a shorter fence line inside a longer
// fence is literal content, not the end of the block (#1707).
let mut fence: Option<(u8, usize)> = None;
// Whether the current line is a fence line (opens, closes, or is
// checked against an open fence): its backticks are fence syntax,
// never an inline code span opener.
let mut fence_line = false;
// The open inline code span's backtick run length, when inside
// one: span content is literal text, so a destination-shaped
// string inside it must not be protected (#1707).
Expand Down Expand Up @@ -2342,11 +2346,13 @@ fn protect_newline_destinations(markdown: &str) -> (String, Option<String>) {
} else {
None
};
let was_in_fence = fence.is_some();
match (fence, fence_run) {
(None, Some(open)) => fence = Some(open),
(Some((ch, n)), Some((cch, cn))) if cch == ch && cn >= n => fence = None,
_ => {}
}
fence_line = fence_run.is_some() && (was_in_fence || fence.is_some());
// An indented code block starts on a 4+ indented line
// after a blank line (or at the start, or continuing an
// indented block); a 4+ indented line inside a paragraph
Expand All @@ -2355,7 +2361,7 @@ fn protect_newline_destinations(markdown: &str) -> (String, Option<String>) {
prev_line_indented = in_indented || (indent >= 4 && line_blank && prev_line_indented);
prev_line_blank = line_blank;
}
let literal_line = fence.is_some() || in_indented;
let literal_line = fence.is_some() || in_indented || fence_line;
if !literal_line && span.is_none() && (markdown[i..].starts_with("](<") || markdown[i..].starts_with("]: <")) {
let lt = if markdown[i..].starts_with("](<") {
i + 2
Expand Down
17 changes: 16 additions & 1 deletion pkgs/id/src/web/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2605,7 +2605,22 @@ mod upload_router_tests {
// binding through the public API alone (Codex P2 on PR #3894):
// parse [interfaces.web] with the re-exported parser and build
// the app router with the re-exported constructor.
let (_, state, _dir, _endpoint, _docs) = router_with_state().await;
let (_, mut state, _dir, _endpoint, _docs) = router_with_state().await;
// The world page handler 404s when no world is hosted; host
// one so the assertions below measure routing (the configured
// route registered, the default route absent), not hosting.
state.world = Some(crate::world_hub::WorldHub::single(
crate::world_session::WorldService::new(
crate::world::WorldHandle::spawn(
crate::world::WorldCore::new(
"lobby",
crate::world::WorldLimits::default(),
)
.unwrap(),
),
None,
),
));
let interface = crate::web::web_interface_from_app_toml(
"[interfaces.web]\nmode = \"adapter\"\nroute = \"/arena\"\nsocket = \"/ws/arena\"\n",
)
Expand Down
13 changes: 10 additions & 3 deletions pkgs/id/src/web/tags_ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ async fn handle_tags_socket(socket: axum::extract::ws::WebSocket, state: AppStat
let mut rx = state.tag_store.subscribe();

// Spawn a task to drain inbound messages (keeps the socket alive).
let drain_handle = tokio::spawn(async move {
let mut drain_handle = tokio::spawn(async move {
while let Some(Ok(msg)) = receiver.next().await {
// Respond to pings, ignore everything else.
if let Message::Close(_) = msg {
Expand All @@ -229,9 +229,16 @@ async fn handle_tags_socket(socket: axum::extract::ws::WebSocket, state: AppStat
}
});

// Forward broadcast events to the WebSocket.
// Forward broadcast events to the WebSocket. The loop also ends
// when the inbound side finishes (a disconnect, a close frame, or
// a protocol error such as an oversized message): nobody is
// reading any more, so the socket must close instead of lingering.
loop {
match rx.recv().await {
let received = tokio::select! {
received = rx.recv() => received,
_ = &mut drain_handle => break,
};
match received {
Ok(event) => {
let json = match serde_json::to_string(&event) {
Ok(j) => j,
Expand Down
4 changes: 2 additions & 2 deletions pkgs/id/src/web/world_ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1655,12 +1655,12 @@ mod tests {
// App A keeps the historical binding; app B binds its own
// socket from its [interfaces.web] section (#2325).
let addr_a = serve(world_routes().with_state(WorldWebState {
hub: None,
hub: Some(WorldHub::single(WorldService::new(lobby(), None))),
cookie_secure: false,
}))
.await;
let addr_b = serve(world_routes_at("/ws/arena").with_state(WorldWebState {
hub: None,
hub: Some(WorldHub::single(WorldService::new(lobby(), None))),
cookie_secure: false,
}))
.await;
Expand Down
Loading