Skip to content

An open-source, faster Devin alternative

TypeScript

Item files: one rule in the hook and the index (1-9 digits, not 0, a slug); pre-push refuses nothing without a base to compare with

Igor Zalutski via Computer96673e961c52
diff --git a/computer/hooks/knowledge/pre-push b/computer/hooks/knowledge/pre-push
index a8a34bc0..07f52c7e 100755
--- a/computer/hooks/knowledge/pre-push
+++ b/computer/hooks/knowledge/pre-push
@@ -4,12 +4,16 @@
# and replaced by it on every housekeeping pass: edits here do not last.
#
# Item numbers are unique. An item is a file `NN-slug.md` directly under
-# work/ or done/ (directories there are ignored), and its number, the leading
-# digits, is its identity. A push is refused when the tree it pushes to a
-# branch has more items with one number than the remote's head has: a push is
-# a compare-and-swap on that head, so this is a real uniqueness check, and
-# numbers already shared upstream (history) never block anyone. The message
-# names the number to take instead.
+# work/ or done/ (directories there are ignored): 1 to 9 digits whose value is
+# at least 1, `-`, a non-empty slug, `.md`, the rule of the Work index
+# (services/managed-agents/src/computer/work/item.ts, ITEM_FILE); its number is
+# its identity. A push is refused when the tree it pushes to a branch has more
+# items with one number than the remote's head has: a push is a
+# compare-and-swap on that head, so this is a real uniqueness check, and
+# numbers already shared upstream (history) never block anyone. Without a
+# head to compare with (a new branch and no remote HEAD known), nothing is
+# refused: every shared number would look new. The message names the number
+# to take instead.
#
# It also prints warnings, never refusals, for item files the push adds or
# changes: the cheap part of the Work index's parser (title, owner, `## Left`).
@@ -18,18 +22,26 @@
zero() { case $1 in *[!0]*) return 1 ;; esac; }
+# awk: whether a file name directly under work/ or done/ is an item (see above). No {m,n}: not every awk has them.
+IS_ITEM='function is_item(name) {
+ if (name !~ /^[0-9]+-[^\/]+\.md$/) return 0
+ match(name, /^[0-9]+/)
+ return RLENGTH <= 9 && substr(name, 1, RLENGTH) + 0 >= 1
+}'
+
# `<number>\t<path>` for every item file of commit $1.
items() {
- git -c core.quotePath=false ls-tree --full-tree "$1" -- work/ done/ 2>/dev/null | awk -F'\t' '
+ git -c core.quotePath=false ls-tree --full-tree "$1" -- work/ done/ 2>/dev/null | awk -F'\t' "$IS_ITEM"'
$1 ~ / blob / {
name = $2; sub(/^[^\/]*\//, "", name)
- if (name ~ /^[0-9]+-[^\/]*\.md$/) { match(name, /^[0-9]+/); print (substr(name, 1, RLENGTH) + 0) "\t" $2 }
+ if (is_item(name)) { match(name, /^[0-9]+/); print (substr(name, 1, RLENGTH) + 0) "\t" $2 }
}'
}
-# Exit 1, with a message, if commit $1 has duplicate numbers that the base commit $2 (may be empty) has fewer of.
+# Exit 1, with a message, if commit $1 has duplicate numbers that the base commit $2 has fewer of. No base: nothing to compare, nothing refused.
check() {
- { [ -z "$2" ] || items "$2"; echo '--'; items "$1"; } | awk -F'\t' '
+ [ -n "$2" ] || return 0
+ { items "$2"; echo '--'; items "$1"; } | awk -F'\t' '
!pushed && $0 == "--" { pushed = 1; next }
!pushed { before[$1 + 0]++; old[$2] = 1; next }
{ n = $1 + 0; count[n]++; path[n, count[n]] = $2; if (n > max) max = n }
@@ -66,10 +78,10 @@ warn() {
git -c core.quotePath=false diff --name-only --no-renames --diff-filter=AM "$2" "$1" -- ':/work' ':/done' 2>/dev/null | head -n 50 |
while IFS= read -r p; do
case $p in */*/*) continue ;; *.md) ;; *) continue ;; esac
- case ${p#*/} in
- [0-9]*-*.md) ;;
- *) echo "pre-push: warning: $p is not an item; items are named NN-slug.md" >&2; continue ;;
- esac
+ if ! printf '%s\n' "${p#*/}" | awk "$IS_ITEM"' { exit !is_item($0) }'; then
+ echo "pre-push: warning: $p is not an item; items are named NN-slug.md (NN: 1 to 9 digits, not 0)" >&2
+ continue
+ fi
git cat-file blob "$1:$p" 2>/dev/null | awk -v p="$p" '
NR == 1 && $0 == "---" { front = 1; next }
front && $0 == "---" { front = 0; next }
diff --git a/computer/src/workspaces/knowledge-hooks.test.ts b/computer/src/workspaces/knowledge-hooks.test.ts
index d1caeec7..cb39b308 100644
--- a/computer/src/workspaces/knowledge-hooks.test.ts
+++ b/computer/src/workspaces/knowledge-hooks.test.ts
@@ -221,4 +221,39 @@ describe('pre-push', () => {
expect(r.stderr).toContain('work/notes.md is not an item');
expect(r.stderr).not.toContain('12-ok');
});
+
+ it("counts only what the Work index counts as an item: 1 to 9 digits, not 0, then a slug", () => {
+ // Each pair would share a number if these were items; none is, so nothing is refused.
+ const odd = ['work/0-zero.md', 'done/00-zero.md', 'work/1234567890-big.md', 'done/1234567890-big.md', 'work/13a-x.md', 'work/14-.md'];
+ put(session, Object.fromEntries(odd.map((p) => [p, item('Odd')])));
+ git(session, ['add', '-A']);
+ git(session, ['commit', '-q', '-m', 'Not items']);
+ const r = push(session);
+ expect(r.stderr).not.toMatch(/refused/);
+ expect(r.status).toBe(0);
+ for (const p of odd) expect(r.stderr).toContain(`${p} is not an item`);
+ // Nine digits with leading zeros is an item, and its number is 7: taken.
+ put(session, { 'work/000000007-padded.md': item('Padded') });
+ git(session, ['add', '-A']);
+ git(session, ['commit', '-q', '-m', 'Padded']);
+ const p = push(session);
+ expect(p.status).not.toBe(0);
+ expect(p.stderr).toContain('7 is taken by');
+ git(session, ['reset', '-q', '--hard', 'HEAD~1']);
+ });
+
+ it('refuses nothing for a new branch when there is no remote head to compare with', () => {
+ git(clone, ['remote', 'set-head', 'origin', '-d']);
+ try {
+ put(session, { 'work/15-fresh.md': item('Fresh') });
+ git(session, ['add', '-A']);
+ git(session, ['commit', '-q', '-m', 'Fresh']);
+ // History shares 7 (work/07-a.md, done/7-b.md): with no base, that must not read as this push's collision.
+ const r = spawnSync('git', ['push', 'origin', 'HEAD:refs/heads/sp/nobase'], { cwd: session, env: env(), encoding: 'utf8' });
+ expect(r.stderr).not.toMatch(/refused/);
+ expect(r.status).toBe(0);
+ } finally {
+ git(clone, ['remote', 'set-head', 'origin', 'main']);
+ }
+ });
});
diff --git a/services/managed-agents/src/computer/work/item.ts b/services/managed-agents/src/computer/work/item.ts
index 5b176d45..9fe5b366 100644
--- a/services/managed-agents/src/computer/work/item.ts
+++ b/services/managed-agents/src/computer/work/item.ts
@@ -47,13 +47,18 @@ export interface ParsedItem {
warnings: string[];
}
+// An item file: 1 to 9 digits whose value is at least 1 (leading zeros allowed), `-`, a non-empty
+// slug, `.md`. The knowledge repository's pre-push hook (computer/hooks/knowledge/pre-push) and the
+// API's item number (api.ts NUMBER) apply the same rule; change them together.
const ITEM_FILE = /^(work|done)\/(\d{1,9})-([^/]+)\.md$/;
/** The item a path names, or null when the file is not an item. */
export function itemPath(path: string): ItemPath | null {
const m = ITEM_FILE.exec(path);
if (!m) return null;
- return { number: Number(m[2]), stage: m[1] === "work" ? "open" : "closed", slug: m[3]! };
+ const number = Number(m[2]);
+ if (number < 1) return null;
+ return { number, stage: m[1] === "work" ? "open" : "closed", slug: m[3]! };
}
/** The title an item has before its body is read: the slug. */
diff --git a/services/managed-agents/test/work-item.test.ts b/services/managed-agents/test/work-item.test.ts
index b10763fb..b52aee36 100644
--- a/services/managed-agents/test/work-item.test.ts
+++ b/services/managed-agents/test/work-item.test.ts
@@ -22,7 +22,9 @@ describe("itemPath", () => {
it("names only NN-slug.md directly under work/ or done/", () => {
expect(itemPath("work/7-board-api.md")).toEqual({ number: 7, stage: "open", slug: "board-api" });
expect(itemPath("done/012-old.md")).toEqual({ number: 12, stage: "closed", slug: "old" });
- for (const p of ["work/README.md", "work/x/7-a.md", "work/7-a.txt", "notes/7-a.md", "work/7.md", "work/-a.md", "done/1234567890-a.md"]) {
+ expect(itemPath("work/000000001-a.md")).toEqual({ number: 1, stage: "open", slug: "a" });
+ expect(itemPath("work/999999999-2026-plan.md")).toEqual({ number: 999999999, stage: "open", slug: "2026-plan" });
+ for (const p of ["work/README.md", "work/x/7-a.md", "work/7-a.txt", "notes/7-a.md", "work/7.md", "work/-a.md", "done/1234567890-a.md", "work/0-a.md", "done/000-a.md", "work/1a-x.md", "work/7-.md"]) {
expect(itemPath(p)).toBeNull();
}
});