Skip to content

Commit ccce48c

Browse files
committed
fix: excessive git commits in data repo on every server startup
1 parent 9f8d2c0 commit ccce48c

File tree

7 files changed

+56
-13
lines changed

7 files changed

+56
-13
lines changed

docs/changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v0.1.1
4+
5+
- Fix excessive git commits in data repo on every server startup (only `lastSeen` timestamp change in `machines.json`)
6+
37
## v0.1.0
48

59
- Add desktop notifications (macOS, Windows, Linux) when new conflicts are detected

landing/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
AI Sync
113113
<span
114114
class="rounded-full bg-muted px-1.5 py-0.5 text-xs h-fit font-medium text-muted-foreground"
115-
>v0.1.0</span
115+
>v0.1.1</span
116116
>
117117
</a>
118118
<div class="flex items-center gap-3">

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ai-sync",
33
"author": "Anh-Thi Dinh",
4-
"version": "0.1.0",
4+
"version": "0.1.1",
55
"repository": "https://github.com/anhthiding/ai-sync",
66
"private": true,
77
"license": "MIT",

packages/server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@ai-sync/server",
33
"author": "Anh-Thi Dinh",
4-
"version": "0.1.0",
4+
"version": "0.1.1",
55
"repository": "https://github.com/anhthiding/ai-sync",
66
"private": true,
77
"license": "MIT",

packages/server/src/services/__tests__/machines.test.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,29 @@ describe('registerCurrentMachine', () => {
146146
expect(data.machines[MACHINE_ID].lastSeen).toBeTruthy();
147147
});
148148

149-
it('updates lastSeen on subsequent calls', async () => {
149+
it('skips write when lastSeen is recent (within 1 day)', () => {
150150
registerCurrentMachine();
151151
const first = readMachinesFile().machines[MACHINE_ID].lastSeen;
152152

153-
// Small delay to ensure different timestamp
154-
await new Promise((r) => setTimeout(r, 10));
155153
registerCurrentMachine();
156154
const second = readMachinesFile().machines[MACHINE_ID].lastSeen;
157155

158-
expect(second >= first).toBe(true);
156+
// Should be identical since lastSeen is still fresh
157+
expect(second).toBe(first);
158+
});
159+
160+
it('updates lastSeen when it is older than 1 day', () => {
161+
// Manually write an old lastSeen
162+
const data = readMachinesFile();
163+
const oldDate = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000).toISOString();
164+
data.machines[MACHINE_ID] = { name: MACHINE_NAME, lastSeen: oldDate };
165+
writeMachinesFile(data);
166+
167+
registerCurrentMachine();
168+
const updated = readMachinesFile().machines[MACHINE_ID].lastSeen;
169+
170+
expect(updated).not.toBe(oldDate);
171+
expect(new Date(updated).getTime()).toBeGreaterThan(new Date(oldDate).getTime());
159172
});
160173
});
161174

packages/server/src/services/machines.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,39 @@ function sortKeys<T>(obj: Record<string, T>): Record<string, T> {
6363
return sorted;
6464
}
6565

66+
/**
67+
* Register/update the current machine in machines.json.
68+
* Only writes the file when something meaningful changed:
69+
* - Machine is new (not in file yet)
70+
* - Machine name changed
71+
* - lastSeen is older than 1 day (UI only shows date, not time)
72+
* This avoids creating a git commit on every server startup.
73+
*/
6674
export function registerCurrentMachine(): void {
6775
const data = readMachinesFile();
68-
data.machines[config.machineId] = {
69-
name: config.machineName,
70-
lastSeen: new Date().toISOString(),
71-
};
72-
writeMachinesFile(data);
76+
const existing = data.machines[config.machineId];
77+
const now = new Date();
78+
79+
let needsWrite = false;
80+
if (!existing) {
81+
needsWrite = true;
82+
} else if (existing.name !== config.machineName) {
83+
needsWrite = true;
84+
} else {
85+
const lastSeen = new Date(existing.lastSeen);
86+
const ageMs = now.getTime() - lastSeen.getTime();
87+
if (ageMs > 24 * 60 * 60 * 1000) {
88+
needsWrite = true;
89+
}
90+
}
91+
92+
if (needsWrite) {
93+
data.machines[config.machineId] = {
94+
name: config.machineName,
95+
lastSeen: now.toISOString(),
96+
};
97+
writeMachinesFile(data);
98+
}
7399
}
74100

75101
export function setRepoMapping(storePath: string, localPath: string): void {

packages/ui/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@ai-sync/ui",
33
"author": "Anh-Thi Dinh",
4-
"version": "0.1.0",
4+
"version": "0.1.1",
55
"repository": "https://github.com/anhthiding/ai-sync",
66
"private": true,
77
"license": "MIT",

0 commit comments

Comments
 (0)