From 8a88b85d38161fa1aa5aba73eca98bc1de9ce733 Mon Sep 17 00:00:00 2001 From: Mistium <92952823+Mistium@users.noreply.github.com> Date: Sat, 21 Nov 2026 03:58:58 +0000 Subject: [PATCH 1/2] Add last-commits inspection for per-path latest authored change --- native/gitinspection/inspection.go | 2 + native/gitinspection/last_commits.go | 210 ++++++++++++++++++++++ native/gitinspection/last_commits_test.go | 95 ++++++++++ opal.json | 2 +- src/classroom/classroom-policy.osl | 2 +- src/classroom/classroom-policy.test.osl | 1 + src/git/git-inspection.osl | 9 + src/git/routes.osl | 1 + 8 files changed, 320 insertions(+), 2 deletions(-) create mode 100644 native/gitinspection/last_commits.go create mode 100644 native/gitinspection/last_commits_test.go diff --git a/native/gitinspection/inspection.go b/native/gitinspection/inspection.go index 649765b..63ff6a1 100644 --- a/native/gitinspection/inspection.go +++ b/native/gitinspection/inspection.go @@ -351,6 +351,8 @@ func Inspect(workspacePath, sha, operation, requestedPath, allowedHeads string) return archive.inspectCommitChanges(sha, commit) case "summary": return archive.inspectCommitSummary(sha, commit) + case "last-commits": + return archive.inspectLastCommits(sha, commit) default: return archive.inspectCommit(sha, commit, false) } diff --git a/native/gitinspection/last_commits.go b/native/gitinspection/last_commits.go new file mode 100644 index 0000000..6225fa7 --- /dev/null +++ b/native/gitinspection/last_commits.go @@ -0,0 +1,210 @@ +package gitinspection + +import ( + "bytes" + "encoding/hex" + "strings" +) + +const ( + maxLastCommitWalk = 2000 + maxLastCommitParseBytes = 64 << 20 +) + +type treeEntry struct { + OID string + Dir bool +} + +func (a archive) readTreeEntries(oid string) (map[string]treeEntry, bool) { + kind, body, ok := a.readObject(oid) + if !ok || kind != "tree" { + return nil, false + } + entries := map[string]treeEntry{} + position := 0 + for position < len(body) { + space := bytes.IndexByte(body[position:], ' ') + if space < 1 { + return nil, false + } + space += position + nul := bytes.IndexByte(body[space+1:], 0) + if nul < 1 { + return nil, false + } + nul += space + 1 + if nul+21 > len(body) { + return nil, false + } + mode := string(body[position:space]) + name := string(body[space+1 : nul]) + if name == "" || name == "." || name == ".." || strings.ContainsAny(name, "/\\") { + return nil, false + } + entries[name] = treeEntry{OID: hex.EncodeToString(body[nul+1 : nul+21]), Dir: mode == "40000" || mode == "040000"} + position = nul + 21 + } + return entries, true +} + +func (a archive) headPaths(rootOID string) (map[string]bool, bool) { + paths := map[string]bool{} + var walk func(string, string, int) bool + walk = func(treeOID, prefix string, depth int) bool { + if depth > maxTreeDepth { + return false + } + entries, ok := a.readTreeEntries(treeOID) + if !ok { + return false + } + for name, entry := range entries { + path := joinTreePath(prefix, name) + paths[path] = true + if len(paths) > maxTreeFiles { + return false + } + if entry.Dir && !walk(entry.OID, path, depth+1) { + return false + } + } + return true + } + return paths, walk(rootOID, "", 0) +} + +func joinTreePath(prefix, name string) string { + if prefix == "" { + return name + } + return prefix + "/" + name +} + +type lastCommitWalk struct { + archive archive + wanted map[string]bool + budget int + visit func(string) +} + +func (w *lastCommitWalk) fractchChanged(beforeOID, afterOID string) bool { + _, before, beforeOK := w.archive.readObject(beforeOID) + _, after, afterOK := w.archive.readObject(afterOID) + if !beforeOK || !afterOK || len(before) > maxInlineDiffFileBytes || len(after) > maxInlineDiffFileBytes { + return true + } + if w.budget+len(before)+len(after) > maxLastCommitParseBytes { + return true + } + w.budget += len(before) + len(after) + changes := compareFractch(string(before), string(after)) + return len(changes.Scripts) > 0 || len(changes.Items) > 0 +} + +func (w *lastCommitWalk) changedPaths(currentOID, parentOID, prefix string, depth int) (bool, bool) { + if depth > maxTreeDepth { + return false, false + } + current, ok := w.archive.readTreeEntries(currentOID) + if !ok { + return false, false + } + parent := map[string]treeEntry{} + if parentOID != "" { + if parent, ok = w.archive.readTreeEntries(parentOID); !ok { + return false, false + } + } + changed := false + for name, entry := range current { + path := joinTreePath(prefix, name) + before, existed := parent[name] + if existed && before.OID == entry.OID { + continue + } + counted := true + if entry.Dir { + previous := "" + if existed && before.Dir { + previous = before.OID + } + nested, ok := w.changedPaths(entry.OID, previous, path, depth+1) + if !ok { + return false, false + } + counted = nested || previous == "" + } else if existed && !before.Dir && strings.HasSuffix(strings.ToLower(name), ".fractch") { + counted = w.fractchChanged(before.OID, entry.OID) + } + if !counted { + continue + } + changed = true + if w.wanted[path] { + w.visit(path) + } + } + for name := range parent { + if _, kept := current[name]; !kept { + changed = true + } + } + return changed, true +} + +func (a archive) inspectLastCommits(sha string, commit gitCommit) string { + wanted, ok := a.headPaths(commit.Tree) + if !ok { + return failure("commit tree is invalid") + } + paths := map[string]string{} + commits := map[string]any{} + remaining := len(wanted) + current, currentCommit := sha, commit + complete := true + walk := &lastCommitWalk{archive: a, wanted: wanted} + for walked := 0; remaining > 0; walked++ { + if walked >= maxLastCommitWalk { + complete = false + break + } + parentTree := "" + var parentCommit gitCommit + parentSHA := "" + if len(currentCommit.Parents) > 0 { + parentSHA = currentCommit.Parents[0] + parsed, found := a.parseCommit(parentSHA) + if !found { + complete = false + break + } + parentCommit = parsed + parentTree = parsed.Tree + } + used := false + walk.visit = func(path string) { + if _, done := paths[path]; done { + return + } + paths[path] = current + remaining-- + used = true + } + if _, ok := walk.changedPaths(currentCommit.Tree, parentTree, "", 0); !ok { + return failure("commit tree is invalid") + } + if used { + commits[current] = map[string]any{ + "author": currentCommit.Author, "date": currentCommit.Date, "message": currentCommit.Message, + } + } + if parentSHA == "" { + break + } + current, currentCommit = parentSHA, parentCommit + } + return encode(map[string]any{ + "ok": true, "sha": sha, "paths": paths, "commits": commits, "complete": complete && remaining == 0, + }, "could not encode last commits") +} diff --git a/native/gitinspection/last_commits_test.go b/native/gitinspection/last_commits_test.go new file mode 100644 index 0000000..22d72c4 --- /dev/null +++ b/native/gitinspection/last_commits_test.go @@ -0,0 +1,95 @@ +package gitinspection + +import ( + "strings" + "testing" +) + +func TestInspectLastCommitsFindsNewestAuthoredChangePerPath(t *testing.T) { + repo := newTestRepository() + catCode := repo.object("blob", []byte(fractchBase)) + catEdited := repo.object("blob", []byte(strings.Replace(fractchBase, "turn 15;", "turn 45;", 1))) + stageCode := repo.object("blob", []byte("var x = 1 id \"a\";\n")) + idle := repo.object("blob", []byte("")) + firstTree := repo.nestedTree(map[string]string{ + "Cat/main.fractch": catCode, + "Cat/assets/Idle.svg": idle, + "Stage/main.fractch": stageCode, + "fractch.json": repo.object("blob", []byte("{}")), + }) + first := repo.commit(firstTree, "", "Initial version") + secondTree := repo.nestedTree(map[string]string{ + "Cat/main.fractch": catEdited, + "Cat/assets/Idle.svg": idle, + "Stage/main.fractch": stageCode, + "fractch.json": repo.object("blob", []byte("{}")), + }) + second := repo.commit(secondTree, first, "Change the cat") + third := repo.commit(secondTree, second, "Message only") + fourthTree := repo.nestedTree(map[string]string{ + "Cat/main.fractch": catEdited, + "Cat/assets/Idle.svg": idle, + "Stage/main.fractch": repo.object("blob", []byte("var x = 2 id \"a\";\n")), + "Dog/main.fractch": repo.object("blob", []byte(fractchBase)), + "fractch.json": repo.object("blob", []byte("{}")), + }) + fourth := repo.commit(fourthTree, third, "Add a dog and save a new stage value") + + result := decodeResult(t, Inspect(repo.write(t), fourth, "last-commits", "", fourth)) + if result["ok"] != true || result["complete"] != true { + t.Fatalf("unexpected result: %#v", result) + } + paths := result["paths"].(map[string]any) + expected := map[string]string{ + "Cat": second, + "Cat/main.fractch": second, + "Cat/assets": first, + "Cat/assets/Idle.svg": first, + "Stage": first, + "Stage/main.fractch": first, + "Dog": fourth, + "Dog/main.fractch": fourth, + "fractch.json": first, + } + if len(paths) != len(expected) { + t.Fatalf("expected %d paths, got %#v", len(expected), paths) + } + for path, sha := range expected { + if paths[path] != sha { + t.Fatalf("%s: expected %s, got %v", path, sha, paths[path]) + } + } + commits := result["commits"].(map[string]any) + if _, listed := commits[third]; listed { + t.Fatalf("a commit that changed nothing should not be listed: %#v", commits) + } + if commits[second].(map[string]any)["message"] != "Change the cat" { + t.Fatalf("unexpected commit details: %#v", commits[second]) + } +} + +func TestInspectLastCommitsStopsAtTheWalkLimit(t *testing.T) { + repo := newTestRepository() + stable := repo.object("blob", []byte("stable\n")) + parent := "" + head := "" + for index := 0; index <= maxLastCommitWalk; index++ { + tree := repo.nestedTree(map[string]string{ + "stable.txt": stable, + "counter.txt": repo.object("blob", []byte{byte(index), byte(index >> 8)}), + }) + head = repo.commit(tree, parent, "Step") + parent = head + } + result := decodeResult(t, Inspect(repo.write(t), head, "last-commits", "", head)) + if result["ok"] != true || result["complete"] != false { + t.Fatalf("expected an incomplete result, got ok=%v complete=%v", result["ok"], result["complete"]) + } + paths := result["paths"].(map[string]any) + if paths["counter.txt"] != head { + t.Fatalf("counter should point at the head, got %v", paths["counter.txt"]) + } + if _, found := paths["stable.txt"]; found { + t.Fatalf("stable.txt should be unknown past the walk limit, got %v", paths["stable.txt"]) + } +} diff --git a/opal.json b/opal.json index 7da1afe..125615f 100644 --- a/opal.json +++ b/opal.json @@ -1,6 +1,6 @@ { "name": "mistwarp-api", - "version": "1.3.0", + "version": "1.4.0", "main": "main.osl", "scripts": { "dev": ["osl", "run", "main.osl"], diff --git a/src/classroom/classroom-policy.osl b/src/classroom/classroom-policy.osl index 7101107..6eff500 100644 --- a/src/classroom/classroom-policy.osl +++ b/src/classroom/classroom-policy.osl @@ -211,7 +211,7 @@ array studentReadRoutes = [ "^/projects/[^/]+/(project\\.json|trusted-extensions|editor|workspace\\.mwp|card\\.svg|commits|branches|game-inventory-config|me/save|me/inventory)$", "^/projects/[^/]+/releases/[^/]+/project\\.json$", "^/projects/[^/]+/extensions/[^/]+/source$", - "^/projects/[^/]+/commits/[^/]+(/(tree|file|co-authors|collaborators))?$" + "^/projects/[^/]+/commits/[^/]+(/(tree|file|last-commits|co-authors|collaborators))?$" ] def studentRouteAllowed(string method, string rawPath) boolean ( diff --git a/src/classroom/classroom-policy.test.osl b/src/classroom/classroom-policy.test.osl index ebf015f..79bdcc5 100644 --- a/src/classroom/classroom-policy.test.osl +++ b/src/classroom/classroom-policy.test.osl @@ -70,6 +70,7 @@ testing.assert(studentRouteAllowed("GET", "/v1/classroom/join/ABC"), "the class testing.assert(studentRouteAllowed("GET", "/v1/projects/p1"), "students can open a project they have a role on") testing.assert(studentRouteAllowed("GET", "/v1/projects/p1/project.json"), "students can load their project") testing.assert(studentRouteAllowed("GET", "/v1/projects/p1/commits/abc/tree"), "students can read their history") +testing.assert(studentRouteAllowed("GET", "/v1/projects/p1/commits/abc/last-commits"), "students can see when their files last changed") testing.assert(studentRouteAllowed("OPTIONS", "/v1/anything"), "preflight requests pass") testing.assert(studentRouteAllowed("GET", "/v1/me/backpack"), "students can load their synced backpack") testing.assert(studentRouteAllowed("GET", "/v1/projects/p1/trusted-extensions"), "the editor can read a class project's trusted extensions") diff --git a/src/git/git-inspection.osl b/src/git/git-inspection.osl index 8751153..91b31b5 100644 --- a/src/git/git-inspection.osl +++ b/src/git/git-inspection.osl @@ -277,6 +277,15 @@ def handleGetCommitTree(*serve.Context c) ( gitInspectionResponse(c, res.project.assert(object), c.param("sha"), "tree", "") ) +def handleGetCommitLastCommits(*serve.Context c) ( + auto res = loadProject(c.param("id")) + if !res.ok ( + c.notFound("project not found") + return + ) + gitInspectionResponse(c, res.project.assert(object), c.param("sha"), "last-commits", "") +) + def handleGetCommitFile(*serve.Context c) ( auto res = loadProject(c.param("id")) if !res.ok ( diff --git a/src/git/routes.osl b/src/git/routes.osl index 06c4d40..ef9ed2e 100644 --- a/src/git/routes.osl +++ b/src/git/routes.osl @@ -16,6 +16,7 @@ def registerGitRoutes(*serve.Router api) ( api.GET("/projects/:id/commits/:sha/tree", handleGetCommitTree) api.GET("/projects/:id/commits/:sha/file", handleGetCommitFile) + api.GET("/projects/:id/commits/:sha/last-commits", handleGetCommitLastCommits) api.GET("/projects/:id/pulls", handleListPulls) api.POST("/projects/:id/pulls", requireAuth, handleCreatePull) api.GET("/projects/:id/pulls/:index", handleGetPull) From 90f86b989d9675af20d570d2b0cfdc04fe147b1b Mon Sep 17 00:00:00 2001 From: Mistium <92952823+Mistium@users.noreply.github.com> Date: Sat, 21 Nov 2026 05:43:40 +0000 Subject: [PATCH 2/2] Report an exhausted parse budget as an incomplete result --- native/gitinspection/last_commits.go | 28 +++++++++++++++-------- native/gitinspection/last_commits_test.go | 24 +++++++++++++++++++ 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/native/gitinspection/last_commits.go b/native/gitinspection/last_commits.go index 6225fa7..9dd77ab 100644 --- a/native/gitinspection/last_commits.go +++ b/native/gitinspection/last_commits.go @@ -6,10 +6,9 @@ import ( "strings" ) -const ( - maxLastCommitWalk = 2000 - maxLastCommitParseBytes = 64 << 20 -) +const maxLastCommitWalk = 2000 + +var maxLastCommitParseBytes = 64 << 20 type treeEntry struct { OID string @@ -82,10 +81,11 @@ func joinTreePath(prefix, name string) string { } type lastCommitWalk struct { - archive archive - wanted map[string]bool - budget int - visit func(string) + archive archive + wanted map[string]bool + budget int + exhausted bool + visit func(string) } func (w *lastCommitWalk) fractchChanged(beforeOID, afterOID string) bool { @@ -95,7 +95,8 @@ func (w *lastCommitWalk) fractchChanged(beforeOID, afterOID string) bool { return true } if w.budget+len(before)+len(after) > maxLastCommitParseBytes { - return true + w.exhausted = true + return false } w.budget += len(before) + len(after) changes := compareFractch(string(before), string(after)) @@ -194,6 +195,15 @@ func (a archive) inspectLastCommits(sha string, commit gitCommit) string { if _, ok := walk.changedPaths(currentCommit.Tree, parentTree, "", 0); !ok { return failure("commit tree is invalid") } + if walk.exhausted { + complete = false + for path, owner := range paths { + if owner == current { + delete(paths, path) + } + } + break + } if used { commits[current] = map[string]any{ "author": currentCommit.Author, "date": currentCommit.Date, "message": currentCommit.Message, diff --git a/native/gitinspection/last_commits_test.go b/native/gitinspection/last_commits_test.go index 22d72c4..5373b2a 100644 --- a/native/gitinspection/last_commits_test.go +++ b/native/gitinspection/last_commits_test.go @@ -93,3 +93,27 @@ func TestInspectLastCommitsStopsAtTheWalkLimit(t *testing.T) { t.Fatalf("stable.txt should be unknown past the walk limit, got %v", paths["stable.txt"]) } } + +func TestInspectLastCommitsReportsAnExhaustedParseBudget(t *testing.T) { + repo := newTestRepository() + first := repo.commit(repo.nestedTree(map[string]string{ + "Cat/main.fractch": repo.object("blob", []byte(fractchBase)), + "notes.txt": repo.object("blob", []byte("one\n")), + }), "", "Initial version") + head := repo.commit(repo.nestedTree(map[string]string{ + "Cat/main.fractch": repo.object("blob", []byte(strings.Replace(fractchBase, "turn 15;", "turn 45;", 1))), + "notes.txt": repo.object("blob", []byte("two\n")), + }), first, "Change the cat") + previous := maxLastCommitParseBytes + maxLastCommitParseBytes = 1 + defer func() { maxLastCommitParseBytes = previous }() + + result := decodeResult(t, Inspect(repo.write(t), head, "last-commits", "", head)) + if result["ok"] != true || result["complete"] != false { + t.Fatalf("expected an incomplete result, got %#v", result) + } + paths := result["paths"].(map[string]any) + if len(paths) != 0 { + t.Fatalf("nothing should be attributed once the budget runs out, got %#v", paths) + } +}