Skip to content

[kyo-system] watch a path for filesystem changes - #1867

Merged
fwbrasil merged 5 commits into
mainfrom
path-watch
Sep 10, 2026
Merged

fwbrasil merged 5 commits into
mainfrom
path-watch

Conversation

@DamianReeves

Copy link
Copy Markdown
Collaborator

What this adds

Filesystem watching as its own capability. PathWatch is separate from PathRead, discharged by Path.runWatch and Path.runWatchWith, so a program that only observes changes says so in its type. FileSystem.Watch supplies the backend; the host watcher polls through PathWatch.polling rather than binding a native notification API.

Stack

Part of the path-capability stack on #1859: process fixes, filesystem vocabulary, abstraction, capability flip, channels, locks, watch, durability, confinement, in-memory, zip, then overlay (#1799). Merge bottom up; GitHub retargets the stack above each merge.

Each branch carries one synthesized commit whose diff is exactly this pull request. The original incremental development history remains visible in #1799's earlier timeline and in the backup refs recorded in the working notes.

@DamianReeves

Copy link
Copy Markdown
Collaborator Author

Heads-up: #1864 and #1865 are rebased onto main

#1864 and #1865 now sit directly on main rather than on the old stack base, and #1864 picked up a change to the temp surface. This branch has not moved yet, so its diff on GitHub currently re-includes everything below it. That clears once it is rebased onto its new parent. Rebase bottom up.

Two groups of work are waiting when it does.

1. Renames that landed on main during review of the lower branches

The squash merges of #1862 and #1863 carried review renames that this branch predates. Each one will surface during the rebase:

On this branch On main
Path.follow, the private polling loop Path.watch
private enum Poll private enum Observed
PathDirectories.temp, the unscoped primitive tempUnscoped
PathDirectories.tempScoped temp
StreamFileExtensions StreamSystemExtensions
readStream(charset, bufferSize: Int) readStream(charset, bufferSize: ByteSize)
readBytesStream(bufferSize: Int) readBytesStream(bufferSize: ByteSize)
ReadHandle.size(): Result[_, ByteSize] Result[_, Long]
Jsonl.follow, Jsonl.followResults Jsonl.watch, Jsonl.watchResults

StreamSystemExtensions also gained a writeWith helper, append and options overloads, and append-preserving failure semantics. Its tests moved out of PathTest into StreamSystemExtensionsTest, which does not exist on this branch yet. readBufferCapacity now clamps rather than throwing, so a zero or oversized buffer is no longer an argument-validation failure.

The two temp renames are a swap, and that makes them the dangerous ones. temp and tempScoped did not move in the same direction: the unscoped primitive took the tempUnscoped name, and tempScoped took the now-vacant temp. So an existing Path.temp(...) callsite on this branch keeps compiling after the rebase while silently changing meaning, from "unscoped, caller removes it" to "scoped, removed at Scope exit through the service". Every Path.temp(...) callsite has to be read and classified rather than left alone. The ones that call remove themselves afterwards are the unscoped kind and want tempUnscoped. This bit the #1864 rebase in CommandTest.

2. Path.temp now goes through the service

#1864 closed a gap where Path.temp created and removed its file on the host regardless of the installed backend, while Path.tempDir correctly routed through the service. The temp file surface now mirrors tempDir:

  • case Temp(prefix: String, suffix: String) extends Op[Path.TempFileHandle], dispatched to service.temp(prefix, suffix)
  • def temp(prefix: String, suffix: String)(using Frame): Path.TempFileHandle < (S & Abort[FileStructureException]) is a new abstract member on FileSystem.Write
  • Path.temp is now Path < (PathWrite & Sync & Scope), and its Scope finalizer removes through the service that vended the handle
  • Path.tempUnscoped and Path.tempDirUnscoped stay host primitives deliberately, since an entry whose lifetime the caller owns has no scope for a service-vended handle to hang from

So for every branch above: Path.temp callsites need a runner, and every FileSystem.Write implementation needs a temp member.

One further note, because it caused two real test failures during the #1864 rebase. A backend failure is raised by the runner, not by the frame that suspended the op, so an Abort.run nested inside a Path.run cannot observe it. Code that needs to inspect a filesystem failure has to run its own runner at the point it catches:

result <- Abort.run[FileSystemException] {
    Path.run(Scope.run(stream.writeTo(file, options = Path.WriteOptions(createFolders = false))))
}

What this branch needs

The rename set above, nothing more. No FileSystem.Write implementation of its own, so the new temp member arrives already satisfied through HostFileSystem.

Worth settling on this branch: a naming collision that neither side saw coming. This branch adds a FileSystem.Watch[S] tier and PathWatch.scala for filesystem change notification. Independently, main renamed the private tail-polling loop Path.follow to Path.watch, and renamed Jsonl.follow to Jsonl.watch with it. After the rebase Path.watch means "poll an open file for appended bytes" while PathWatch and FileSystem.Watch mean "subscribe to filesystem change events". Two unrelated concepts, one word, same module. This is the branch that introduces the second meaning, so it is the natural place to decide whether one of them gets a different name.

Include creation time in platform file identities so an inode reused after deletion is not reported as a move. Synchronize capacity tests on the poller rearming instead of a wall-clock delay.
)

/** Runs `program`, discharging [[PathWatch]] against the Local-selected watch backend. */
def runWatch[A, S](program: A < (PathWatch & S))(using

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could track just PathRead for file watching? We've been very selective of what appears in the pending set so far.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered that, but PathRead cannot preserve the current optional capability boundary. If openWatcher suspends only PathRead, every PathRead handler must interpret it. A program passed to runReadOnlyWith(readBackend) would then typecheck even when that backend cannot watch, leaving us to reject at runtime or silently use the Local/host watcher, both of which break the existing negative-capability and backend-coherence guarantees. The sound way to use only PathRead is to make the full watch contract mandatory on every FileSystem.Read, including read-only and virtual fixtures. Keeping PathWatch separate means it enters the pending set only at openWatcher, while backends that only provide reads remain valid. I would keep the separate effect unless we want watching to become mandatory for all read backends.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fwbrasil - Would you prefer that I fold watch into the Read capability? I can do that.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point on the collapse implying a requirement for all impls! What implementations do you foresee we having? Any where it'd become problematic?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The next branches in this stack already provide both cases. InMemoryFileSystem and RootConfinedHostFileSystem implement Write & Watch, so they can preserve observation. The zip backend deliberately returns only FileSystem.Read[Sync]: it is an archive snapshot and has no complete change-notification contract. Folding watch into Read would make that backend implement a fake watcher or reject at runtime. The same issue applies to user-defined read-only adapters and plausible remote/object-store readers that can list and read but cannot provide loss-aware, scope-managed change streams. Given the concrete zip backend, I would keep Watch optional and PathWatch separate.

end match
end awaitLock

abstract class Read[S]:

@ahoy-jon ahoy-jon Sep 7, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure about that one to be honest.

Suggested change
abstract class Read[-S]:

@fwbrasil
fwbrasil merged commit 49ba07f into main Sep 10, 2026
14 checks passed
@fwbrasil
fwbrasil deleted the path-watch branch September 10, 2026 13:43
fwbrasil added a commit that referenced this pull request Sep 10, 2026
The merge brought in #1867, written against main's kernel: ArrowEffect.handle
is handleCont here, its clause receives the continuation as an Arrow so the
answer chains rather than maps, and Isolate.capture answers at Remove & S, so
isolateWatch cannot name Async in that row. The two sibling isolates in this
file already had the right shape.

kyo-systemJVM/Test/compile is green; the full-tree compile is still running.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants