fix(git-sync): address PR #119 review #2 — throttle /git Basic auth, fix mcp schema drift + warnings/tests
Must-fix:
- Throttle the raw /git HTTP-Basic path: it bypasses Nest/ThrottlerGuard, so
verifyUserCredentials (bcrypt) ran unthrottled. Wrap it in the SAME
FailedLoginLimiter the /mcp path uses (5/60s; per-IP, per-IP+email, global
per-email keys; atomic tryReserve BEFORE bcrypt; success resets, non-credential
errors release). The (threshold+1)-th attempt now gets 429 pre-bcrypt. Sweep
timer + onModuleDestroy mirror McpService.
- Fix the mcp schema mirror drift: packages/mcp details `open` attr now reads via
hasAttribute (matches editor-ext canon + git-sync copy); getAttribute dropped a
bare `<details open>` state. (build/ is gitignored — rebuilt locally.)
Tests added:
- /git brute-force throttle: pre-bcrypt 429 on the 6th failure; success resets;
non-credential error releases the budget.
- git-http-backend lost-lock AbortSignal: already-aborted -> no spawn + 500;
live abort mid-request -> SIGTERM + response closed.
- orchestrator divergentDocmost -> WARN + flag surfaced in status (+ clean case).
- pollTick re-entrancy guard skips an overlapping tick.
- datasource NotFound early-throws (getPageJson/move/rename) + updatedAt:undefined
stale-read branch (importPageMarkdown/createPage).
Suggestions:
- space.repo updateGitSyncSettings: parameterize the jsonb key (`${prefKey}::text`)
instead of sql.raw (latent-injection footgun); value stays sql.lit. Spec updated.
- pollTick re-entrancy guard (private `polling` flag).
- page-change.listener docstring: honest about the move/rename/delete over-skip
(loop-guard keys only on lastUpdatedSource) -> ~poll-interval latency, not loss.
- AGENTS.md: document the root /git smart-HTTP route + GitSyncModule.
- Remove redundant redteam-provenance.spec.ts (covered e2e in
persistence.extension.spec.ts:145).
- Extract the duplicated SIGTERM->SIGKILL+finish block (watchdog + abort) into
terminateChild; centralize watchdog-timer teardown in done().
Architecture (deferred, documented): mcp schema header now carries the three-copy
keep-in-sync + schema-core note; the editor-ext contract test documents that the
mcp copy and attribute-behaviour drift (details `open`) are not mechanically
covered yet.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -314,6 +314,40 @@ describe('GitSyncOrchestrator', () => {
|
||||
expect(deps.settings.autoMergeConflicts).toBe(false);
|
||||
});
|
||||
|
||||
it("escalates a divergent-`docmost` push refusal to WARN and surfaces the flag in the status", async () => {
|
||||
const built = build();
|
||||
const warnSpy = jest
|
||||
.spyOn(Logger.prototype, 'warn')
|
||||
.mockImplementation(() => undefined);
|
||||
// The engine refused to fast-forward a divergent `docmost` mirror (§5).
|
||||
runCycleMock.mockResolvedValue({ ...OK_CYCLE, divergentDocmost: true });
|
||||
|
||||
const res = await built.orchestrator.runOnce('space-1', 'ws-1');
|
||||
|
||||
// The flag is surfaced in the returned status (consumable by /status).
|
||||
expect(res.divergentDocmost).toBe(true);
|
||||
// And escalated from the engine's info `log` to a WARN naming the space.
|
||||
expect(warnSpy).toHaveBeenCalledWith(
|
||||
expect.stringContaining('DIVERGENT'),
|
||||
);
|
||||
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('space-1'));
|
||||
});
|
||||
|
||||
it("does NOT warn when the cycle is clean (divergentDocmost falsy)", async () => {
|
||||
const built = build();
|
||||
const warnSpy = jest
|
||||
.spyOn(Logger.prototype, 'warn')
|
||||
.mockImplementation(() => undefined);
|
||||
runCycleMock.mockResolvedValue(OK_CYCLE);
|
||||
|
||||
const res = await built.orchestrator.runOnce('space-1', 'ws-1');
|
||||
|
||||
expect(res.divergentDocmost).toBeUndefined();
|
||||
expect(warnSpy).not.toHaveBeenCalledWith(
|
||||
expect.stringContaining('DIVERGENT'),
|
||||
);
|
||||
});
|
||||
|
||||
it("surfaces the engine's skipped status (e.g. merge-in-progress) verbatim", async () => {
|
||||
const built = build();
|
||||
runCycleMock.mockResolvedValue({ ran: false, skipped: 'merge-in-progress' });
|
||||
@@ -461,6 +495,40 @@ describe('GitSyncOrchestrator', () => {
|
||||
expect(runOnce).toHaveBeenNthCalledWith(2, 'space-2', 'ws-2');
|
||||
});
|
||||
|
||||
it('skips an overlapping tick while a previous pass is still in flight (re-entrancy guard)', async () => {
|
||||
const built = build();
|
||||
let release!: () => void;
|
||||
const gate = new Promise<void>((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
// Stall the first pass inside enabledSpaces so a second tick fires while it
|
||||
// is still running.
|
||||
const enabledSpy = jest
|
||||
.spyOn(built.orchestrator as any, 'enabledSpaces')
|
||||
.mockImplementation(async () => {
|
||||
await gate;
|
||||
return [{ spaceId: 'space-1', workspaceId: 'ws-1' }];
|
||||
});
|
||||
const runOnce = jest
|
||||
.spyOn(built.orchestrator, 'runOnce')
|
||||
.mockResolvedValue({ spaceId: 'space-1', ran: true });
|
||||
|
||||
const first = (built.orchestrator as any).pollTick();
|
||||
await Promise.resolve(); // let the first pass set polling=true + await gate
|
||||
|
||||
// A second tick during the first must be skipped: it never even enumerates.
|
||||
await (built.orchestrator as any).pollTick();
|
||||
expect(enabledSpy).toHaveBeenCalledTimes(1);
|
||||
|
||||
release();
|
||||
await first;
|
||||
expect(runOnce).toHaveBeenCalledTimes(1);
|
||||
|
||||
// After the first pass cleared the flag, a fresh tick runs normally.
|
||||
await (built.orchestrator as any).pollTick();
|
||||
expect(enabledSpy).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('does NOT throw and runs nothing when the enabled-spaces query throws (try/catch backstop)', async () => {
|
||||
jest.spyOn(Logger.prototype, 'error').mockImplementation(() => undefined);
|
||||
const built = build();
|
||||
|
||||
Reference in New Issue
Block a user