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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gildforge/kit",
"version": "0.7.10",
"version": "0.7.11",
"description": "The shared engine, views and server behind gild: the git forge for agents on Cloudflare.",
"license": "Apache-2.0",
"type": "module",
Expand Down
20 changes: 20 additions & 0 deletions src/engine/repo-refs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { settleAdvertisedRefs } from '../../dist/engine/repo-refs.js'

const tagObject='8670bac8'.padEnd(40,'0'),peeled='0ae1ffac'.padEnd(40,'0'),pr='0de688f0'.padEnd(40,'0'),main='c1a89c38'.padEnd(40,'0')
const advertised=[
{ref:'refs/heads/main',oid:main},
{ref:'refs/tags/v0.1.0',oid:tagObject},
{ref:'refs/heads/_meta',oid:'b'.repeat(40)},
]

test('an annotated tag pushed as its tag object is settled by the advertisement', () => {
assert.deepEqual(settleAdvertisedRefs({'v0.1.0':tagObject},advertised),{'v0.1.0':tagObject})
})
test('a ref deleted outside the push path (import cleanup) is settled', () => {
assert.deepEqual(settleAdvertisedRefs({'import/pr/1':pr},advertised),{'import/pr/1':pr})
})
test('a ref advertised at another oid, and _meta, stay outstanding', () => {
assert.deepEqual(settleAdvertisedRefs({main:peeled,_meta:'c'.repeat(40)},advertised),{})
})
12 changes: 12 additions & 0 deletions src/engine/repo-refs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,15 @@ export function sameTips(a: Refs, b: Refs) {
])
return key(a) === key(b)
}

/** Pushed refs (short name → pushed oid) that an advertisement read after the
* push accounts for: advertised with exactly that oid (an annotated tag's own
* object, a hidden import ref) or no longer present (deleted). `_meta` is
* settled only by the index. Returns `settled` with them added. */
export function settleAdvertisedRefs(pushed: Record<string, string>, advertised: { ref: string; oid: string }[], settled: Record<string, string> = {}) {
const short = /^refs\/(heads|tags)\//
const oids = new Map(advertised.filter((r) => short.test(r.ref)).map((r) => [r.ref.replace(short, ''), r.oid]))
for (const [ref, sha] of Object.entries(pushed))
if (ref !== '_meta' && (oids.get(ref) === sha || !oids.has(ref))) settled[ref] = sha
return settled
}
35 changes: 26 additions & 9 deletions src/server/repo-index-do.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
type CFamilyIssue,
} from './cqx-heavy-limits.js'
import { scoreCacheKey } from './cqx-score-cache.js'
import {settleAdvertisedRefs} from '../engine/repo-refs.js'
import {fencePreparedRefs,preparedRefsEpoch,publishPreparedRefs,publishPreparedHealth,publishPreparedDiff,publishPreparedPullHeads} from './prepared-repository-producer.js'
import type { HeavyBinding } from './cqx-module.js'
import type { Change, CommitChange } from './changes.js'
Expand Down Expand Up @@ -352,7 +353,9 @@ export class RepoIndex extends DurableObject<Env> {
const last = Number(this.index.setting('conversations:nudged') ?? 0)
if (Date.now() - last < 5 * 60_000) return
this.index.setting('conversations:nudged', String(Date.now()))
if (!this.index.setting('owner')) return this.refresh(owner, repo)
// Outstanding pushes settle only against a fresh ref advertisement.
if (!this.index.setting('owner') || Object.keys(JSON.parse(this.index.setting('fragments:pending-refs') ?? '{}')).length)
return this.refresh(owner, repo)
await this.wake(Date.now() + 1)
}
async refresh(owner: string, repo: string, target?: Target) {
Expand Down Expand Up @@ -402,13 +405,23 @@ export class RepoIndex extends DurableObject<Env> {
}
/** A push fence is satisfied by authoritative indexed refs, independently
* of HTML rendering. Never clear a newer/unobserved ref or metadata push. */
/** A push the index can never observe by commit: an annotated tag (indexed
* peeled, pushed as the tag object), a hidden import ref, or a ref deleted
* outside the push path. The advertisement read after the push settles it. */
protected settleAdvertised(pushed:Record<string,string>,advertised:{ref:string;oid:string}[]){
const settled=JSON.parse(this.index.setting('fragments:settled-refs')??'{}') as Record<string,string>
this.index.setting('fragments:settled-refs',JSON.stringify(settleAdvertisedRefs(pushed,advertised,settled)))
}
protected acknowledgeIndexedRefs(){
const raw=this.index.setting('fragments:pending-refs')??'{}'
const pending=JSON.parse(raw) as Record<string,string>
const settled=JSON.parse(this.index.setting('fragments:settled-refs')??'{}') as Record<string,string>
for(const [ref,sha] of Object.entries(pending)){
const observed=ref==='_meta'?this.index.setting('meta'):this.index.resolve(ref)
if(observed===sha||/^0+$/.test(sha)&&!observed)delete pending[ref]
if(observed===sha||/^0+$/.test(sha)&&!observed||settled[ref]===sha)delete pending[ref]
}
for(const ref of Object.keys(settled))if(!(ref in pending))delete settled[ref]
this.index.setting('fragments:settled-refs',JSON.stringify(settled))
const next=JSON.stringify(pending)
if(next!==raw)this.index.setting('fragments:pending-refs',next)
}
Expand Down Expand Up @@ -1846,14 +1859,18 @@ export class RepoIndex extends DurableObject<Env> {
// one smart HTTP advertisement gives branches, peeled tags and _meta.
const { refsFromAdvertisement } = await import('../engine/repo-refs.js')
const { internalPrefix } = await import('../engine/import/visibility.js')
// Pushes recorded before this advertisement was read.
const pushed = JSON.parse(this.index.setting('fragments:pending-refs') ?? '{}') as Record<string,string>
const advertisement = await git.listServerRefs({
http,
url,
...(token ? { headers: { authorization: `Bearer ${token}` } } : {}),
symrefs: true,
peelTags: true,
})
this.settleAdvertised(pushed, advertisement)
const refs = refsFromAdvertisement(
await git.listServerRefs({
http,
url,
...(token ? { headers: { authorization: `Bearer ${token}` } } : {}),
symrefs: true,
peelTags: true,
}),
advertisement,
await internalPrefix(this.env.DB,`${this.index.setting('owner')}/${this.index.setting('repo')}`),
)
refs.defaultBranch =
Expand Down
Loading