Repository navigation
[kyo-system] watch a path for filesystem changes - #1867
Conversation
Heads-up: #1864 and #1865 are rebased onto
|
| 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 toservice.temp(prefix, suffix)def temp(prefix: String, suffix: String)(using Frame): Path.TempFileHandle < (S & Abort[FileStructureException])is a new abstract member onFileSystem.WritePath.tempis nowPath < (PathWrite & Sync & Scope), and itsScopefinalizer removes through the service that vended the handlePath.tempUnscopedandPath.tempDirUnscopedstay 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.
4fea820 to
8245bdb
Compare
8245bdb to
256d239
Compare
256d239 to
978c633
Compare
88365c7 to
d9231a1
Compare
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.
d9231a1 to
8efa3dd
Compare
| ) | ||
|
|
||
| /** Runs `program`, discharging [[PathWatch]] against the Local-selected watch backend. */ | ||
| def runWatch[A, S](program: A < (PathWatch & S))(using |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@fwbrasil - Would you prefer that I fold watch into the Read capability? I can do that.
There was a problem hiding this comment.
Good point on the collapse implying a requirement for all impls! What implementations do you foresee we having? Any where it'd become problematic?
There was a problem hiding this comment.
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]: |
There was a problem hiding this comment.
not sure about that one to be honest.
| abstract class Read[-S]: |
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.
What this adds
Filesystem watching as its own capability.
PathWatchis separate fromPathRead, discharged byPath.runWatchandPath.runWatchWith, so a program that only observes changes says so in its type.FileSystem.Watchsupplies the backend; the host watcher polls throughPathWatch.pollingrather 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.